1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116#define pr_fmt(fmt) "IPv4: " fmt
117
118#include <linux/module.h>
119#include <linux/types.h>
120#include <linux/kernel.h>
121#include <linux/string.h>
122#include <linux/errno.h>
123#include <linux/slab.h>
124
125#include <linux/net.h>
126#include <linux/socket.h>
127#include <linux/sockios.h>
128#include <linux/in.h>
129#include <linux/inet.h>
130#include <linux/inetdevice.h>
131#include <linux/netdevice.h>
132#include <linux/etherdevice.h>
133
134#include <net/snmp.h>
135#include <net/ip.h>
136#include <net/protocol.h>
137#include <net/route.h>
138#include <linux/skbuff.h>
139#include <net/sock.h>
140#include <net/arp.h>
141#include <net/icmp.h>
142#include <net/raw.h>
143#include <net/checksum.h>
144#include <linux/netfilter_ipv4.h>
145#include <net/xfrm.h>
146#include <linux/mroute.h>
147#include <linux/netlink.h>
148
149
150
151
152bool ip_call_ra_chain(struct sk_buff *skb)
153{
154 struct ip_ra_chain *ra;
155 u8 protocol = ip_hdr(skb)->protocol;
156 struct sock *last = NULL;
157 struct net_device *dev = skb->dev;
158
159 for (ra = rcu_dereference(ip_ra_chain); ra; ra = rcu_dereference(ra->next)) {
160 struct sock *sk = ra->sk;
161
162
163
164
165 if (sk && inet_sk(sk)->inet_num == protocol &&
166 (!sk->sk_bound_dev_if ||
167 sk->sk_bound_dev_if == dev->ifindex) &&
168 net_eq(sock_net(sk), dev_net(dev))) {
169 if (ip_is_fragment(ip_hdr(skb))) {
170 if (ip_defrag(skb, IP_DEFRAG_CALL_RA_CHAIN))
171 return true;
172 }
173 if (last) {
174 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
175 if (skb2)
176 raw_rcv(last, skb2);
177 }
178 last = sk;
179 }
180 }
181
182 if (last) {
183 raw_rcv(last, skb);
184 return true;
185 }
186 return false;
187}
188
189static int ip_local_deliver_finish(struct sk_buff *skb)
190{
191 struct net *net = dev_net(skb->dev);
192
193 __skb_pull(skb, skb_network_header_len(skb));
194
195 rcu_read_lock();
196 {
197 int protocol = ip_hdr(skb)->protocol;
198 const struct net_protocol *ipprot;
199 int raw;
200
201 resubmit:
202 raw = raw_local_deliver(skb, protocol);
203
204 ipprot = rcu_dereference(inet_protos[protocol]);
205 if (ipprot != NULL) {
206 int ret;
207
208 if (!ipprot->no_policy) {
209 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
210 kfree_skb(skb);
211 goto out;
212 }
213 nf_reset(skb);
214 }
215 ret = ipprot->handler(skb);
216 if (ret < 0) {
217 protocol = -ret;
218 goto resubmit;
219 }
220 IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS);
221 } else {
222 if (!raw) {
223 if (xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
224 IP_INC_STATS_BH(net, IPSTATS_MIB_INUNKNOWNPROTOS);
225 icmp_send(skb, ICMP_DEST_UNREACH,
226 ICMP_PROT_UNREACH, 0);
227 }
228 kfree_skb(skb);
229 } else {
230 IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS);
231 consume_skb(skb);
232 }
233 }
234 }
235 out:
236 rcu_read_unlock();
237
238 return 0;
239}
240
241
242
243
244int ip_local_deliver(struct sk_buff *skb)
245{
246
247
248
249
250 if (ip_is_fragment(ip_hdr(skb))) {
251 if (ip_defrag(skb, IP_DEFRAG_LOCAL_DELIVER))
252 return 0;
253 }
254
255 return NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_IN, skb, skb->dev, NULL,
256 ip_local_deliver_finish);
257}
258
259static inline bool ip_rcv_options(struct sk_buff *skb)
260{
261 struct ip_options *opt;
262 const struct iphdr *iph;
263 struct net_device *dev = skb->dev;
264
265
266
267
268
269
270
271
272 if (skb_cow(skb, skb_headroom(skb))) {
273 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
274 goto drop;
275 }
276
277 iph = ip_hdr(skb);
278 opt = &(IPCB(skb)->opt);
279 opt->optlen = iph->ihl*4 - sizeof(struct iphdr);
280
281 if (ip_options_compile(dev_net(dev), opt, skb)) {
282 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
283 goto drop;
284 }
285
286 if (unlikely(opt->srr)) {
287 struct in_device *in_dev = __in_dev_get_rcu(dev);
288
289 if (in_dev) {
290 if (!IN_DEV_SOURCE_ROUTE(in_dev)) {
291 if (IN_DEV_LOG_MARTIANS(in_dev))
292 net_info_ratelimited("source route option %pI4 -> %pI4\n",
293 &iph->saddr,
294 &iph->daddr);
295 goto drop;
296 }
297 }
298
299 if (ip_options_rcv_srr(skb))
300 goto drop;
301 }
302
303 return false;
304drop:
305 return true;
306}
307
308int sysctl_ip_early_demux __read_mostly = 1;
309EXPORT_SYMBOL(sysctl_ip_early_demux);
310
311static int ip_rcv_finish(struct sk_buff *skb)
312{
313 const struct iphdr *iph = ip_hdr(skb);
314 struct rtable *rt;
315
316 if (sysctl_ip_early_demux && !skb_dst(skb)) {
317 const struct net_protocol *ipprot;
318 int protocol = iph->protocol;
319
320 ipprot = rcu_dereference(inet_protos[protocol]);
321 if (ipprot && ipprot->early_demux) {
322 ipprot->early_demux(skb);
323
324 iph = ip_hdr(skb);
325 }
326 }
327
328
329
330
331
332 if (!skb_dst(skb)) {
333 int err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
334 iph->tos, skb->dev);
335 if (unlikely(err)) {
336 if (err == -EXDEV)
337 NET_INC_STATS_BH(dev_net(skb->dev),
338 LINUX_MIB_IPRPFILTER);
339 goto drop;
340 }
341 }
342
343#ifdef CONFIG_IP_ROUTE_CLASSID
344 if (unlikely(skb_dst(skb)->tclassid)) {
345 struct ip_rt_acct *st = this_cpu_ptr(ip_rt_acct);
346 u32 idx = skb_dst(skb)->tclassid;
347 st[idx&0xFF].o_packets++;
348 st[idx&0xFF].o_bytes += skb->len;
349 st[(idx>>16)&0xFF].i_packets++;
350 st[(idx>>16)&0xFF].i_bytes += skb->len;
351 }
352#endif
353
354 if (iph->ihl > 5 && ip_rcv_options(skb))
355 goto drop;
356
357 rt = skb_rtable(skb);
358 if (rt->rt_type == RTN_MULTICAST) {
359 IP_UPD_PO_STATS_BH(dev_net(rt->dst.dev), IPSTATS_MIB_INMCAST,
360 skb->len);
361 } else if (rt->rt_type == RTN_BROADCAST)
362 IP_UPD_PO_STATS_BH(dev_net(rt->dst.dev), IPSTATS_MIB_INBCAST,
363 skb->len);
364
365 return dst_input(skb);
366
367drop:
368 kfree_skb(skb);
369 return NET_RX_DROP;
370}
371
372
373
374
375int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
376{
377 const struct iphdr *iph;
378 u32 len;
379
380
381
382
383 if (skb->pkt_type == PACKET_OTHERHOST)
384 goto drop;
385
386
387 IP_UPD_PO_STATS_BH(dev_net(dev), IPSTATS_MIB_IN, skb->len);
388
389 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
390 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
391 goto out;
392 }
393
394 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
395 goto inhdr_error;
396
397 iph = ip_hdr(skb);
398
399
400
401
402
403
404
405
406
407
408
409
410 if (iph->ihl < 5 || iph->version != 4)
411 goto inhdr_error;
412
413 if (!pskb_may_pull(skb, iph->ihl*4))
414 goto inhdr_error;
415
416 iph = ip_hdr(skb);
417
418 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
419 goto csum_error;
420
421 len = ntohs(iph->tot_len);
422 if (skb->len < len) {
423 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
424 goto drop;
425 } else if (len < (iph->ihl*4))
426 goto inhdr_error;
427
428
429
430
431
432 if (pskb_trim_rcsum(skb, len)) {
433 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
434 goto drop;
435 }
436
437 skb->transport_header = skb->network_header + iph->ihl*4;
438
439
440 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
441
442
443 skb_orphan(skb);
444
445 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, dev, NULL,
446 ip_rcv_finish);
447
448csum_error:
449 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_CSUMERRORS);
450inhdr_error:
451 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
452drop:
453 kfree_skb(skb);
454out:
455 return NET_RX_DROP;
456}
457