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#include <asm/system.h>
117#include <linux/module.h>
118#include <linux/types.h>
119#include <linux/kernel.h>
120#include <linux/string.h>
121#include <linux/errno.h>
122
123#include <linux/net.h>
124#include <linux/socket.h>
125#include <linux/sockios.h>
126#include <linux/in.h>
127#include <linux/inet.h>
128#include <linux/inetdevice.h>
129#include <linux/netdevice.h>
130#include <linux/etherdevice.h>
131
132#include <net/snmp.h>
133#include <net/ip.h>
134#include <net/protocol.h>
135#include <net/route.h>
136#include <linux/skbuff.h>
137#include <net/sock.h>
138#include <net/arp.h>
139#include <net/icmp.h>
140#include <net/raw.h>
141#include <net/checksum.h>
142#include <linux/netfilter_ipv4.h>
143#include <net/xfrm.h>
144#include <linux/mroute.h>
145#include <linux/netlink.h>
146
147
148
149
150int ip_call_ra_chain(struct sk_buff *skb)
151{
152 struct ip_ra_chain *ra;
153 u8 protocol = ip_hdr(skb)->protocol;
154 struct sock *last = NULL;
155 struct net_device *dev = skb->dev;
156
157 read_lock(&ip_ra_lock);
158 for (ra = ip_ra_chain; ra; ra = ra->next) {
159 struct sock *sk = ra->sk;
160
161
162
163
164 if (sk && inet_sk(sk)->num == protocol &&
165 (!sk->sk_bound_dev_if ||
166 sk->sk_bound_dev_if == dev->ifindex) &&
167 sock_net(sk) == dev_net(dev)) {
168 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
169 if (ip_defrag(skb, IP_DEFRAG_CALL_RA_CHAIN)) {
170 read_unlock(&ip_ra_lock);
171 return 1;
172 }
173 }
174 if (last) {
175 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
176 if (skb2)
177 raw_rcv(last, skb2);
178 }
179 last = sk;
180 }
181 }
182
183 if (last) {
184 raw_rcv(last, skb);
185 read_unlock(&ip_ra_lock);
186 return 1;
187 }
188 read_unlock(&ip_ra_lock);
189 return 0;
190}
191
192static int ip_local_deliver_finish(struct sk_buff *skb)
193{
194 struct net *net = dev_net(skb->dev);
195
196 __skb_pull(skb, ip_hdrlen(skb));
197
198
199 skb_reset_transport_header(skb);
200
201 rcu_read_lock();
202 {
203 int protocol = ip_hdr(skb)->protocol;
204 int hash, raw;
205 const struct net_protocol *ipprot;
206
207 resubmit:
208 raw = raw_local_deliver(skb, protocol);
209
210 hash = protocol & (MAX_INET_PROTOS - 1);
211 ipprot = rcu_dereference(inet_protos[hash]);
212 if (ipprot != NULL) {
213 int ret;
214
215 if (!net_eq(net, &init_net) && !ipprot->netns_ok) {
216 if (net_ratelimit())
217 printk("%s: proto %d isn't netns-ready\n",
218 __func__, protocol);
219 kfree_skb(skb);
220 goto out;
221 }
222
223 if (!ipprot->no_policy) {
224 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
225 kfree_skb(skb);
226 goto out;
227 }
228 nf_reset(skb);
229 }
230 ret = ipprot->handler(skb);
231 if (ret < 0) {
232 protocol = -ret;
233 goto resubmit;
234 }
235 IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS);
236 } else {
237 if (!raw) {
238 if (xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
239 IP_INC_STATS_BH(net, IPSTATS_MIB_INUNKNOWNPROTOS);
240 icmp_send(skb, ICMP_DEST_UNREACH,
241 ICMP_PROT_UNREACH, 0);
242 }
243 } else
244 IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS);
245 kfree_skb(skb);
246 }
247 }
248 out:
249 rcu_read_unlock();
250
251 return 0;
252}
253
254
255
256
257int ip_local_deliver(struct sk_buff *skb)
258{
259
260
261
262
263 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
264 if (ip_defrag(skb, IP_DEFRAG_LOCAL_DELIVER))
265 return 0;
266 }
267
268 return NF_HOOK(PF_INET, NF_INET_LOCAL_IN, skb, skb->dev, NULL,
269 ip_local_deliver_finish);
270}
271
272static inline int ip_rcv_options(struct sk_buff *skb)
273{
274 struct ip_options *opt;
275 struct iphdr *iph;
276 struct net_device *dev = skb->dev;
277
278
279
280
281
282
283
284
285 if (skb_cow(skb, skb_headroom(skb))) {
286 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
287 goto drop;
288 }
289
290 iph = ip_hdr(skb);
291 opt = &(IPCB(skb)->opt);
292 opt->optlen = iph->ihl*4 - sizeof(struct iphdr);
293
294 if (ip_options_compile(dev_net(dev), opt, skb)) {
295 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
296 goto drop;
297 }
298
299 if (unlikely(opt->srr)) {
300 struct in_device *in_dev = in_dev_get(dev);
301 if (in_dev) {
302 if (!IN_DEV_SOURCE_ROUTE(in_dev)) {
303 if (IN_DEV_LOG_MARTIANS(in_dev) &&
304 net_ratelimit())
305 printk(KERN_INFO "source route option %pI4 -> %pI4\n",
306 &iph->saddr, &iph->daddr);
307 in_dev_put(in_dev);
308 goto drop;
309 }
310
311 in_dev_put(in_dev);
312 }
313
314 if (ip_options_rcv_srr(skb))
315 goto drop;
316 }
317
318 return 0;
319drop:
320 return -1;
321}
322
323static int ip_rcv_finish(struct sk_buff *skb)
324{
325 const struct iphdr *iph = ip_hdr(skb);
326 struct rtable *rt;
327
328
329
330
331
332 if (skb_dst(skb) == NULL) {
333 int err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
334 skb->dev);
335 if (unlikely(err)) {
336 if (err == -EHOSTUNREACH)
337 IP_INC_STATS_BH(dev_net(skb->dev),
338 IPSTATS_MIB_INADDRERRORS);
339 else if (err == -ENETUNREACH)
340 IP_INC_STATS_BH(dev_net(skb->dev),
341 IPSTATS_MIB_INNOROUTES);
342 goto drop;
343 }
344 }
345
346#ifdef CONFIG_NET_CLS_ROUTE
347 if (unlikely(skb_dst(skb)->tclassid)) {
348 struct ip_rt_acct *st = per_cpu_ptr(ip_rt_acct, smp_processor_id());
349 u32 idx = skb_dst(skb)->tclassid;
350 st[idx&0xFF].o_packets++;
351 st[idx&0xFF].o_bytes += skb->len;
352 st[(idx>>16)&0xFF].i_packets++;
353 st[(idx>>16)&0xFF].i_bytes += skb->len;
354 }
355#endif
356
357 if (iph->ihl > 5 && ip_rcv_options(skb))
358 goto drop;
359
360 rt = skb_rtable(skb);
361 if (rt->rt_type == RTN_MULTICAST) {
362 IP_UPD_PO_STATS_BH(dev_net(rt->u.dst.dev), IPSTATS_MIB_INMCAST,
363 skb->len);
364 } else if (rt->rt_type == RTN_BROADCAST)
365 IP_UPD_PO_STATS_BH(dev_net(rt->u.dst.dev), IPSTATS_MIB_INBCAST,
366 skb->len);
367
368 return dst_input(skb);
369
370drop:
371 kfree_skb(skb);
372 return NET_RX_DROP;
373}
374
375
376
377
378int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
379{
380 struct iphdr *iph;
381 u32 len;
382
383
384
385
386 if (skb->pkt_type == PACKET_OTHERHOST)
387 goto drop;
388
389
390 IP_UPD_PO_STATS_BH(dev_net(dev), IPSTATS_MIB_IN, skb->len);
391
392 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
393 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
394 goto out;
395 }
396
397 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
398 goto inhdr_error;
399
400 iph = ip_hdr(skb);
401
402
403
404
405
406
407
408
409
410
411
412
413 if (iph->ihl < 5 || iph->version != 4)
414 goto inhdr_error;
415
416 if (!pskb_may_pull(skb, iph->ihl*4))
417 goto inhdr_error;
418
419 iph = ip_hdr(skb);
420
421 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
422 goto inhdr_error;
423
424 len = ntohs(iph->tot_len);
425 if (skb->len < len) {
426 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
427 goto drop;
428 } else if (len < (iph->ihl*4))
429 goto inhdr_error;
430
431
432
433
434
435 if (pskb_trim_rcsum(skb, len)) {
436 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
437 goto drop;
438 }
439
440
441 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
442
443
444 skb_orphan(skb);
445
446 return NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, dev, NULL,
447 ip_rcv_finish);
448
449inhdr_error:
450 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
451drop:
452 kfree_skb(skb);
453out:
454 return NET_RX_DROP;
455}
456