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 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 "
306 NIPQUAD_FMT " -> " NIPQUAD_FMT "\n",
307 NIPQUAD(iph->saddr),
308 NIPQUAD(iph->daddr));
309 in_dev_put(in_dev);
310 goto drop;
311 }
312
313 in_dev_put(in_dev);
314 }
315
316 if (ip_options_rcv_srr(skb))
317 goto drop;
318 }
319
320 return 0;
321drop:
322 return -1;
323}
324
325static int ip_rcv_finish(struct sk_buff *skb)
326{
327 const struct iphdr *iph = ip_hdr(skb);
328 struct rtable *rt;
329
330
331
332
333
334 if (skb->dst == NULL) {
335 int err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
336 skb->dev);
337 if (unlikely(err)) {
338 if (err == -EHOSTUNREACH)
339 IP_INC_STATS_BH(dev_net(skb->dev),
340 IPSTATS_MIB_INADDRERRORS);
341 else if (err == -ENETUNREACH)
342 IP_INC_STATS_BH(dev_net(skb->dev),
343 IPSTATS_MIB_INNOROUTES);
344 goto drop;
345 }
346 }
347
348#ifdef CONFIG_NET_CLS_ROUTE
349 if (unlikely(skb->dst->tclassid)) {
350 struct ip_rt_acct *st = per_cpu_ptr(ip_rt_acct, smp_processor_id());
351 u32 idx = skb->dst->tclassid;
352 st[idx&0xFF].o_packets++;
353 st[idx&0xFF].o_bytes+=skb->len;
354 st[(idx>>16)&0xFF].i_packets++;
355 st[(idx>>16)&0xFF].i_bytes+=skb->len;
356 }
357#endif
358
359 if (iph->ihl > 5 && ip_rcv_options(skb))
360 goto drop;
361
362 rt = skb->rtable;
363 if (rt->rt_type == RTN_MULTICAST)
364 IP_INC_STATS_BH(dev_net(rt->u.dst.dev), IPSTATS_MIB_INMCASTPKTS);
365 else if (rt->rt_type == RTN_BROADCAST)
366 IP_INC_STATS_BH(dev_net(rt->u.dst.dev), IPSTATS_MIB_INBCASTPKTS);
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 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INRECEIVES);
390
391 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
392 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
393 goto out;
394 }
395
396 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
397 goto inhdr_error;
398
399 iph = ip_hdr(skb);
400
401
402
403
404
405
406
407
408
409
410
411
412 if (iph->ihl < 5 || iph->version != 4)
413 goto inhdr_error;
414
415 if (!pskb_may_pull(skb, iph->ihl*4))
416 goto inhdr_error;
417
418 iph = ip_hdr(skb);
419
420 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
421 goto inhdr_error;
422
423 len = ntohs(iph->tot_len);
424 if (skb->len < len) {
425 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
426 goto drop;
427 } else if (len < (iph->ihl*4))
428 goto inhdr_error;
429
430
431
432
433
434 if (pskb_trim_rcsum(skb, len)) {
435 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
436 goto drop;
437 }
438
439
440 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
441
442 return NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, dev, NULL,
443 ip_rcv_finish);
444
445inhdr_error:
446 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
447drop:
448 kfree_skb(skb);
449out:
450 return NET_RX_DROP;
451}