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, ip_hdrlen(skb));
194
195
196 skb_reset_transport_header(skb);
197
198 rcu_read_lock();
199 {
200 int protocol = ip_hdr(skb)->protocol;
201 const struct net_protocol *ipprot;
202 int raw;
203
204 resubmit:
205 raw = raw_local_deliver(skb, protocol);
206
207 ipprot = rcu_dereference(inet_protos[protocol]);
208 if (ipprot != NULL) {
209 int ret;
210
211 if (!net_eq(net, &init_net) && !ipprot->netns_ok) {
212 net_info_ratelimited("%s: proto %d isn't netns-ready\n",
213 __func__, protocol);
214 kfree_skb(skb);
215 goto out;
216 }
217
218 if (!ipprot->no_policy) {
219 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
220 kfree_skb(skb);
221 goto out;
222 }
223 nf_reset(skb);
224 }
225 ret = ipprot->handler(skb);
226 if (ret < 0) {
227 protocol = -ret;
228 goto resubmit;
229 }
230 IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS);
231 } else {
232 if (!raw) {
233 if (xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
234 IP_INC_STATS_BH(net, IPSTATS_MIB_INUNKNOWNPROTOS);
235 icmp_send(skb, ICMP_DEST_UNREACH,
236 ICMP_PROT_UNREACH, 0);
237 }
238 } else
239 IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS);
240 kfree_skb(skb);
241 }
242 }
243 out:
244 rcu_read_unlock();
245
246 return 0;
247}
248
249
250
251
252int ip_local_deliver(struct sk_buff *skb)
253{
254
255
256
257
258 if (ip_is_fragment(ip_hdr(skb))) {
259 if (ip_defrag(skb, IP_DEFRAG_LOCAL_DELIVER))
260 return 0;
261 }
262
263 return NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_IN, skb, skb->dev, NULL,
264 ip_local_deliver_finish);
265}
266
267static inline bool ip_rcv_options(struct sk_buff *skb)
268{
269 struct ip_options *opt;
270 const struct iphdr *iph;
271 struct net_device *dev = skb->dev;
272
273
274
275
276
277
278
279
280 if (skb_cow(skb, skb_headroom(skb))) {
281 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
282 goto drop;
283 }
284
285 iph = ip_hdr(skb);
286 opt = &(IPCB(skb)->opt);
287 opt->optlen = iph->ihl*4 - sizeof(struct iphdr);
288
289 if (ip_options_compile(dev_net(dev), opt, skb)) {
290 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
291 goto drop;
292 }
293
294 if (unlikely(opt->srr)) {
295 struct in_device *in_dev = __in_dev_get_rcu(dev);
296
297 if (in_dev) {
298 if (!IN_DEV_SOURCE_ROUTE(in_dev)) {
299 if (IN_DEV_LOG_MARTIANS(in_dev))
300 net_info_ratelimited("source route option %pI4 -> %pI4\n",
301 &iph->saddr,
302 &iph->daddr);
303 goto drop;
304 }
305 }
306
307 if (ip_options_rcv_srr(skb))
308 goto drop;
309 }
310
311 return false;
312drop:
313 return true;
314}
315
316int sysctl_ip_early_demux __read_mostly = 1;
317EXPORT_SYMBOL(sysctl_ip_early_demux);
318
319static int ip_rcv_finish(struct sk_buff *skb)
320{
321 const struct iphdr *iph = ip_hdr(skb);
322 struct rtable *rt;
323
324 if (sysctl_ip_early_demux && !skb_dst(skb)) {
325 const struct net_protocol *ipprot;
326 int protocol = iph->protocol;
327
328 ipprot = rcu_dereference(inet_protos[protocol]);
329 if (ipprot && ipprot->early_demux) {
330 ipprot->early_demux(skb);
331
332 iph = ip_hdr(skb);
333 }
334 }
335
336
337
338
339
340 if (!skb_dst(skb)) {
341 int err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
342 iph->tos, skb->dev);
343 if (unlikely(err)) {
344 if (err == -EXDEV)
345 NET_INC_STATS_BH(dev_net(skb->dev),
346 LINUX_MIB_IPRPFILTER);
347 goto drop;
348 }
349 }
350
351#ifdef CONFIG_IP_ROUTE_CLASSID
352 if (unlikely(skb_dst(skb)->tclassid)) {
353 struct ip_rt_acct *st = this_cpu_ptr(ip_rt_acct);
354 u32 idx = skb_dst(skb)->tclassid;
355 st[idx&0xFF].o_packets++;
356 st[idx&0xFF].o_bytes += skb->len;
357 st[(idx>>16)&0xFF].i_packets++;
358 st[(idx>>16)&0xFF].i_bytes += skb->len;
359 }
360#endif
361
362 if (iph->ihl > 5 && ip_rcv_options(skb))
363 goto drop;
364
365 rt = skb_rtable(skb);
366 if (rt->rt_type == RTN_MULTICAST) {
367 IP_UPD_PO_STATS_BH(dev_net(rt->dst.dev), IPSTATS_MIB_INMCAST,
368 skb->len);
369 } else if (rt->rt_type == RTN_BROADCAST)
370 IP_UPD_PO_STATS_BH(dev_net(rt->dst.dev), IPSTATS_MIB_INBCAST,
371 skb->len);
372
373 return dst_input(skb);
374
375drop:
376 kfree_skb(skb);
377 return NET_RX_DROP;
378}
379
380
381
382
383int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
384{
385 const struct iphdr *iph;
386 u32 len;
387
388
389
390
391 if (skb->pkt_type == PACKET_OTHERHOST)
392 goto drop;
393
394
395 IP_UPD_PO_STATS_BH(dev_net(dev), IPSTATS_MIB_IN, skb->len);
396
397 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
398 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
399 goto out;
400 }
401
402 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
403 goto inhdr_error;
404
405 iph = ip_hdr(skb);
406
407
408
409
410
411
412
413
414
415
416
417
418 if (iph->ihl < 5 || iph->version != 4)
419 goto inhdr_error;
420
421 if (!pskb_may_pull(skb, iph->ihl*4))
422 goto inhdr_error;
423
424 iph = ip_hdr(skb);
425
426 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
427 goto inhdr_error;
428
429 len = ntohs(iph->tot_len);
430 if (skb->len < len) {
431 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
432 goto drop;
433 } else if (len < (iph->ihl*4))
434 goto inhdr_error;
435
436
437
438
439
440 if (pskb_trim_rcsum(skb, len)) {
441 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
442 goto drop;
443 }
444
445
446 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
447
448
449 skb_orphan(skb);
450
451 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, dev, NULL,
452 ip_rcv_finish);
453
454inhdr_error:
455 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
456drop:
457 kfree_skb(skb);
458out:
459 return NET_RX_DROP;
460}
461