summaryrefslogtreecommitdiffstats
path: root/net/ipv6/ipv6_input.c
blob: 64a9d79f01b153a31990d5eab84b2539f76861fd (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 *	IPv6 input
 *	Linux INET6 implementation 
 *
 *	Authors:
 *	Pedro Roque		<roque@di.fc.ul.pt>
 *	Ian P. Morris		<I.P.Morris@soton.ac.uk>
 *
 *	Based in linux/net/ipv4/ip_input.c
 *
 *	$Id: ipv6_input.c,v 1.13 1996/10/11 16:03:06 roque Exp $
 *
 *	This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */


#include <linux/errno.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/sched.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/in6.h>
#include <linux/icmpv6.h>

#include <net/sock.h>
#include <net/snmp.h>

#include <net/ipv6.h>
#include <net/protocol.h>
#include <net/transp_v6.h>
#include <net/rawv6.h>
#include <net/ndisc.h>
#include <net/ipv6_route.h>
#include <net/addrconf.h>

/*
 *	Header processing function list
 *	We process headers in order (as per RFC)
 *	If the processing function returns 0 the packet is considered 
 *	delivered else it returns the value of the nexthdr.
 *	The ptr field of the function points to the previous nexthdr field.
 *	This is allows the processing function to change it if it's sematics
 *	is: return a new packet without this header (like fragmentation).
 *	When a next_header value is not within the list 
 *	the inet protocol list is searched (i.e. to deliver to 
 *	TCP for instance)
 */

static int ipv6_dest_opt(struct sk_buff **skb_ptr, struct device *dev, __u8 *nhptr,
			 struct ipv6_options *opt);


struct hdrtype_proc {
	u8	type;
	int	(*func) (struct sk_buff **, struct device *dev, __u8 *ptr,
			 struct ipv6_options *opt);
} hdrproc_lst[] = {
  /*
	TODO

	{NEXTHDR_HOP,		ipv6_hop_by_hop}
   */
	{NEXTHDR_ROUTING,	ipv6_routing_header},
	{NEXTHDR_FRAGMENT,	ipv6_reassembly},
  
	{NEXTHDR_DEST,		ipv6_dest_opt},
   /*	
	{NEXTHDR_AUTH,		ipv6_auth_hdr},
	{NEXTHDR_ESP,		ipv6_esp_hdr},
    */
	{NEXTHDR_MAX,		NULL}
};

/* New header structures */


struct ipv6_tlvtype {
	u8 type;
	u8 len;
};

struct ipv6_destopt_hdr {
	u8 nexthdr;
	u8 hdrlen;
};


struct tlvtype_proc {
	u8	type;
	int	(*func) (struct sk_buff *, struct device *dev, __u8 *ptr,
			 struct ipv6_options *opt); 
	
	/* these functions do NOT  update skb->h.raw */
			 
} tlvprocdestopt_lst[] = {
	{255,			NULL}
};


static int parse_tlv(struct tlvtype_proc *procs, struct sk_buff *skb,
		     struct device *dev, __u8 *nhptr, struct ipv6_options *opt,
		     void *lastopt)
{
	struct ipv6_tlvtype *hdr;
	struct tlvtype_proc *curr;
	int pos;

	while ((hdr=(struct ipv6_tlvtype *)skb->h.raw) != lastopt)
		switch (hdr->type & 0x3F)
		{
		case 0: /* TLV encoded Pad1 */
			skb->h.raw++;
			break;

		case 1: /* TLV encoded PadN */
			skb->h.raw += hdr->len+2;
			break;

		default: /* Other TLV code so scan list */
			for (curr=procs; curr->type != 255; curr++)
				if (curr->type == (hdr->type & 0x3F))
				{
					curr->func(skb, dev, nhptr, opt);
					skb->h.raw += hdr->len+2;
					break;
				}
		
			if (curr->type==255)
			{ 
				/* unkown type */
				pos= (__u8 *) skb->h.raw - (__u8 *) skb->ipv6_hdr;
				/* I think this is correct please check - IPM */

				switch ((hdr->type & 0xC0) >> 6) {
					case 0: /* ignore */
						skb->h.raw += hdr->len+2;
						break;
						
					case 1: /* drop packet */
						kfree_skb(skb, FREE_READ);
						return 0;

					case 2: /* send ICMP PARM PROB regardless and 
						   drop packet */
						icmpv6_send(skb, ICMPV6_PARAMETER_PROB, 
							    2, pos, dev);
						kfree_skb(skb, FREE_READ);
						return 0;

					case 3: /* Send ICMP if not a multicast address 
						   and drop packet */
						if (!(ipv6_addr_type(&(skb->ipv6_hdr->daddr)) & IPV6_ADDR_MULTICAST) )
							icmpv6_send(skb, ICMPV6_PARAMETER_PROB, 2, pos, dev);
						kfree_skb(skb, FREE_READ);
						return 0;
					}
			}
			break;
		}
	
	return 1;
}
 


static int ipv6_dest_opt(struct sk_buff **skb_ptr, struct device *dev, __u8 *nhptr,
			 struct ipv6_options *opt)
{
	struct sk_buff *skb=*skb_ptr;
	struct ipv6_destopt_hdr *hdr = (struct ipv6_destopt_hdr *) skb->h.raw;
	
	if (parse_tlv(tlvprocdestopt_lst, skb, dev, nhptr, opt,skb->h.raw+hdr->hdrlen))
		return hdr->nexthdr;
	else
		return 0;
}



/*
 *	0 - deliver
 *	1 - block
 */
static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb)
{
	struct icmpv6hdr *icmph;
	struct raw6_opt *opt;

	opt = &sk->tp_pinfo.tp_raw;
	icmph = (struct icmpv6hdr *) (skb->ipv6_hdr + 1);
	return test_bit(icmph->type, &opt->filter);
}

/*
 *	demultiplex raw sockets.
 *	(should consider queueing the skb in the sock receive_queue
 *	without calling rawv6.c)
 */
static struct sock * ipv6_raw_deliver(struct sk_buff *skb,
				      struct device *dev,
				      struct ipv6_options *opt,
				      __u16 nexthdr,
				      __u16 len,
				      struct in6_addr *saddr,
				      struct in6_addr *daddr)
{
	struct sock *sk, *sk2;
	__u8 hash;

	hash = nexthdr & (SOCK_ARRAY_SIZE-1);

	sk = rawv6_prot.sock_array[hash];
	

	/*
	 *	The first socket found will be delivered after
	 *	delivery to transport protocols.
	 */

	if (sk == NULL)
		return NULL;
	
	sk = inet6_get_sock_raw(sk, nexthdr, daddr, saddr);

	if (sk)
	{
		sk2 = sk;

		while ((sk2 = inet6_get_sock_raw(sk2->next, nexthdr, 
						 daddr, saddr)))
		{
			struct sk_buff *buff;

			if (nexthdr == IPPROTO_ICMPV6 &&
			    icmpv6_filter(sk2, skb))
			{
				continue;
			}
			buff = skb_clone(skb, GFP_ATOMIC);
			buff->sk = sk2;
			rawv6_rcv(buff, dev, saddr, daddr, opt, len);
		}
	}

	if (sk && nexthdr == IPPROTO_ICMPV6 && icmpv6_filter(sk, skb))
	{
		sk = NULL;
	}   
	
	return sk;
}

int ipv6_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
{
	struct inet6_ifaddr	*ifp;
	struct ipv6_options	*opt = (struct ipv6_options *) skb->proto_priv;
	struct ipv6hdr		*hdr;
	u8			hash;
	u8			addr_type;
	struct inet6_protocol	*ipprot;
	struct sock		*raw_sk;
	int			found = 0;
	int			nexthdr = 0;
	__u8			*nhptr;
	int			pkt_len;

	hdr = skb->ipv6_hdr = (struct ipv6hdr *) skb->h.raw;

	if (skb->len < sizeof(struct ipv6hdr) || hdr->version != 6)
	{
		ipv6_statistics.Ip6InHdrErrors++;
		printk(KERN_DEBUG "ipv6_rcv: broken header\n");
		kfree_skb(skb, FREE_READ);
		return 0;
	}

	pkt_len = ntohs(hdr->payload_len);

	if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
	{
		printk(KERN_DEBUG "ipv6_rcv: invalid payload length\n");
		kfree_skb(skb, FREE_READ);
		return 0;
	}

	skb_trim(skb, pkt_len + sizeof(struct ipv6hdr));

	/* check daddr */

	/* Accounting & Firewall check */

	addr_type = ipv6_addr_type(&hdr->daddr);

	if (addr_type & IPV6_ADDR_MULTICAST)
	{
		/* 
		 * if mcast address is not for one of our groups
		 * either pass it to mcast router or discard it 
		 */

		if (ipv6_chk_mcast_addr(dev, &hdr->daddr) == 0)
		{
			/* something like:
			   if (acting_as_router)
				ipv6_mcast_route(skb, ...)
			   else	
			   */
			kfree_skb(skb, FREE_READ);
			return 0;
		}
	}

	if (addr_type & IPV6_ADDR_MULTICAST ||
	    (ifp = ipv6_chk_addr(&hdr->daddr)))	    
	{

		/* loop in a cicle parsing nexthdrs */

		skb->h.raw   += sizeof(struct ipv6hdr);
 
		/* extension header processing must update skb->h.raw */

		nexthdr = hdr->nexthdr;
		nhptr = &hdr->nexthdr;


		while(1)
		{
			struct hdrtype_proc *hdrt;

			/* check for extension header */

			for (hdrt=hdrproc_lst; hdrt->type != NEXTHDR_MAX; hdrt++)
			{
				if (hdrt->type == nexthdr)
				{
					if ((nexthdr = hdrt->func(&skb, dev, nhptr, opt)))
					{
						nhptr = skb->h.raw;
						hdr = skb->ipv6_hdr;
						continue;
					}
					return 0;
				}
			}
			break;

		}

		/* 
		 * deliver to raw sockets
		 * should we deliver raw after or before parsing 
		 * extension headers ?
		 * delivering after means we do reassembly of datagrams
		 * in ip.
		 */

		pkt_len = skb->tail - skb->h.raw;

		raw_sk = ipv6_raw_deliver(skb, dev, opt, nexthdr, pkt_len,
					  &hdr->saddr, &hdr->daddr);

		/* check inet6_protocol list */

		hash = nexthdr & (MAX_INET_PROTOS -1);
		for (ipprot = (struct inet6_protocol *) inet6_protos[hash]; 
		     ipprot != NULL; 
		     ipprot = (struct inet6_protocol *) ipprot->next)
		{
			struct sk_buff *buff = skb;

			if (ipprot->protocol != nexthdr)
				continue;

			if (ipprot->copy || raw_sk)
				buff = skb_clone(skb, GFP_ATOMIC);


			ipprot->handler(buff, dev, 
					&hdr->saddr, &hdr->daddr,
					opt, pkt_len,
					0, ipprot);
			found = 1;
		}

		if (raw_sk)
		{
			skb->sk = raw_sk;
			rawv6_rcv(skb, dev, &hdr->saddr, &hdr->daddr, opt,
				  htons(hdr->payload_len));
			found = 1;
		}
 	       
		/* not found: send ICMP parameter problem back */

		if (!found)
		{
			printk(KERN_DEBUG "proto not found %d\n", nexthdr);
			skb->sk = NULL;
			kfree_skb(skb, FREE_READ);
		}
			
	}
	else
	{
		if (ipv6_forwarding)
		{
			if (addr_type & IPV6_ADDR_LINKLOCAL)
			{
				printk(KERN_DEBUG
				       "link local pkt to forward\n");
				kfree_skb(skb, FREE_READ);
				return 0;
			}
			ipv6_forward(skb, dev, 0);
		}
		else
		{
			printk(KERN_WARNING "IPV6: packet to forward -"
			       "host not configured as router\n");
			kfree_skb(skb, FREE_READ);
		}
	}

	return 0;
}

/*
 * Local variables:
 * c-file-style: "Linux"
 * End:
 */