| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *	IPv6 input | 
 | 3 |  *	Linux INET6 implementation  | 
 | 4 |  * | 
 | 5 |  *	Authors: | 
 | 6 |  *	Pedro Roque		<roque@di.fc.ul.pt> | 
 | 7 |  *	Ian P. Morris		<I.P.Morris@soton.ac.uk> | 
 | 8 |  * | 
 | 9 |  *	$Id: ip6_input.c,v 1.19 2000/12/13 18:31:50 davem Exp $ | 
 | 10 |  * | 
 | 11 |  *	Based in linux/net/ipv4/ip_input.c | 
 | 12 |  * | 
 | 13 |  *	This program is free software; you can redistribute it and/or | 
 | 14 |  *      modify it under the terms of the GNU General Public License | 
 | 15 |  *      as published by the Free Software Foundation; either version | 
 | 16 |  *      2 of the License, or (at your option) any later version. | 
 | 17 |  */ | 
 | 18 | /* Changes | 
 | 19 |  * | 
 | 20 |  * 	Mitsuru KANDA @USAGI and | 
 | 21 |  * 	YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs(). | 
 | 22 |  */ | 
 | 23 |  | 
 | 24 | #include <linux/errno.h> | 
 | 25 | #include <linux/types.h> | 
 | 26 | #include <linux/socket.h> | 
 | 27 | #include <linux/sockios.h> | 
 | 28 | #include <linux/sched.h> | 
 | 29 | #include <linux/net.h> | 
 | 30 | #include <linux/netdevice.h> | 
 | 31 | #include <linux/in6.h> | 
 | 32 | #include <linux/icmpv6.h> | 
 | 33 |  | 
 | 34 | #include <linux/netfilter.h> | 
 | 35 | #include <linux/netfilter_ipv6.h> | 
 | 36 |  | 
 | 37 | #include <net/sock.h> | 
 | 38 | #include <net/snmp.h> | 
 | 39 |  | 
 | 40 | #include <net/ipv6.h> | 
 | 41 | #include <net/protocol.h> | 
 | 42 | #include <net/transp_v6.h> | 
 | 43 | #include <net/rawv6.h> | 
 | 44 | #include <net/ndisc.h> | 
 | 45 | #include <net/ip6_route.h> | 
 | 46 | #include <net/addrconf.h> | 
 | 47 | #include <net/xfrm.h> | 
 | 48 |  | 
 | 49 |  | 
 | 50 |  | 
| Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 51 | inline int ip6_rcv_finish( struct sk_buff *skb)  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { | 
 | 53 | 	if (skb->dst == NULL) | 
 | 54 | 		ip6_route_input(skb); | 
 | 55 |  | 
 | 56 | 	return dst_input(skb); | 
 | 57 | } | 
 | 58 |  | 
| David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 59 | int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { | 
 | 61 | 	struct ipv6hdr *hdr; | 
 | 62 | 	u32 		pkt_len; | 
 | 63 |  | 
 | 64 | 	if (skb->pkt_type == PACKET_OTHERHOST) | 
 | 65 | 		goto drop; | 
 | 66 |  | 
 | 67 | 	IP6_INC_STATS_BH(IPSTATS_MIB_INRECEIVES); | 
 | 68 |  | 
 | 69 | 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) { | 
 | 70 | 		IP6_INC_STATS_BH(IPSTATS_MIB_INDISCARDS); | 
 | 71 | 		goto out; | 
 | 72 | 	} | 
 | 73 |  | 
| Guillaume Chazarain | 6b7fdc3 | 2006-07-24 23:44:44 -0700 | [diff] [blame] | 74 | 	memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); | 
 | 75 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | 	/* | 
 | 77 | 	 * Store incoming device index. When the packet will | 
 | 78 | 	 * be queued, we cannot refer to skb->dev anymore. | 
 | 79 | 	 * | 
 | 80 | 	 * BTW, when we send a packet for our own local address on a | 
 | 81 | 	 * non-loopback interface (e.g. ethX), it is being delivered | 
 | 82 | 	 * via the loopback interface (lo) here; skb->dev = &loopback_dev. | 
 | 83 | 	 * It, however, should be considered as if it is being | 
 | 84 | 	 * arrived via the sending interface (ethX), because of the | 
 | 85 | 	 * nature of scoping architecture. --yoshfuji | 
 | 86 | 	 */ | 
 | 87 | 	IP6CB(skb)->iif = skb->dst ? ((struct rt6_info *)skb->dst)->rt6i_idev->dev->ifindex : dev->ifindex; | 
 | 88 |  | 
| Herbert Xu | 2889139 | 2006-06-30 13:35:46 -0700 | [diff] [blame] | 89 | 	if (unlikely(!pskb_may_pull(skb, sizeof(*hdr)))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | 		goto err; | 
 | 91 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | 	hdr = skb->nh.ipv6h; | 
 | 93 |  | 
 | 94 | 	if (hdr->version != 6) | 
 | 95 | 		goto err; | 
 | 96 |  | 
| Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 97 | 	skb->h.raw = (u8 *)(hdr + 1); | 
 | 98 | 	IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); | 
 | 99 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | 	pkt_len = ntohs(hdr->payload_len); | 
 | 101 |  | 
 | 102 | 	/* pkt_len may be zero if Jumbo payload option is present */ | 
 | 103 | 	if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) { | 
 | 104 | 		if (pkt_len + sizeof(struct ipv6hdr) > skb->len) | 
 | 105 | 			goto truncated; | 
 | 106 | 		if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) { | 
 | 107 | 			IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS); | 
 | 108 | 			goto drop; | 
 | 109 | 		} | 
 | 110 | 		hdr = skb->nh.ipv6h; | 
 | 111 | 	} | 
 | 112 |  | 
 | 113 | 	if (hdr->nexthdr == NEXTHDR_HOP) { | 
| Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 114 | 		if (ipv6_parse_hopopts(&skb) < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | 			IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS); | 
 | 116 | 			return 0; | 
 | 117 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | 	} | 
 | 119 |  | 
 | 120 | 	return NF_HOOK(PF_INET6,NF_IP6_PRE_ROUTING, skb, dev, NULL, ip6_rcv_finish); | 
 | 121 | truncated: | 
 | 122 | 	IP6_INC_STATS_BH(IPSTATS_MIB_INTRUNCATEDPKTS); | 
 | 123 | err: | 
 | 124 | 	IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS); | 
 | 125 | drop: | 
 | 126 | 	kfree_skb(skb); | 
 | 127 | out: | 
 | 128 | 	return 0; | 
 | 129 | } | 
 | 130 |  | 
 | 131 | /* | 
 | 132 |  *	Deliver the packet to the host | 
 | 133 |  */ | 
 | 134 |  | 
 | 135 |  | 
 | 136 | static inline int ip6_input_finish(struct sk_buff *skb) | 
 | 137 | { | 
 | 138 | 	struct inet6_protocol *ipprot; | 
 | 139 | 	struct sock *raw_sk; | 
 | 140 | 	unsigned int nhoff; | 
 | 141 | 	int nexthdr; | 
 | 142 | 	u8 hash; | 
 | 143 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | 	/* | 
 | 145 | 	 *	Parse extension headers | 
 | 146 | 	 */ | 
 | 147 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | 	rcu_read_lock(); | 
 | 149 | resubmit: | 
 | 150 | 	if (!pskb_pull(skb, skb->h.raw - skb->data)) | 
 | 151 | 		goto discard; | 
| Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 152 | 	nhoff = IP6CB(skb)->nhoff; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | 	nexthdr = skb->nh.raw[nhoff]; | 
 | 154 |  | 
 | 155 | 	raw_sk = sk_head(&raw_v6_htable[nexthdr & (MAX_INET_PROTOS - 1)]); | 
| Patrick McHardy | d13964f | 2005-08-09 19:45:02 -0700 | [diff] [blame] | 156 | 	if (raw_sk && !ipv6_raw_deliver(skb, nexthdr)) | 
 | 157 | 		raw_sk = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 |  | 
 | 159 | 	hash = nexthdr & (MAX_INET_PROTOS - 1); | 
 | 160 | 	if ((ipprot = rcu_dereference(inet6_protos[hash])) != NULL) { | 
 | 161 | 		int ret; | 
 | 162 | 		 | 
 | 163 | 		if (ipprot->flags & INET6_PROTO_FINAL) { | 
 | 164 | 			struct ipv6hdr *hdr;	 | 
 | 165 |  | 
| Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 166 | 			/* Free reference early: we don't need it any more, | 
 | 167 | 			   and it may hold ip_conntrack module loaded | 
 | 168 | 			   indefinitely. */ | 
 | 169 | 			nf_reset(skb); | 
 | 170 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | 			skb_postpull_rcsum(skb, skb->nh.raw, | 
 | 172 | 					   skb->h.raw - skb->nh.raw); | 
 | 173 | 			hdr = skb->nh.ipv6h; | 
 | 174 | 			if (ipv6_addr_is_multicast(&hdr->daddr) && | 
 | 175 | 			    !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, | 
 | 176 | 			    &hdr->saddr) && | 
 | 177 | 			    !ipv6_is_mld(skb, nexthdr)) | 
 | 178 | 				goto discard; | 
 | 179 | 		} | 
 | 180 | 		if (!(ipprot->flags & INET6_PROTO_NOPOLICY) && | 
 | 181 | 		    !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))  | 
 | 182 | 			goto discard; | 
 | 183 | 		 | 
| Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 184 | 		ret = ipprot->handler(&skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | 		if (ret > 0) | 
 | 186 | 			goto resubmit; | 
 | 187 | 		else if (ret == 0) | 
 | 188 | 			IP6_INC_STATS_BH(IPSTATS_MIB_INDELIVERS); | 
 | 189 | 	} else { | 
 | 190 | 		if (!raw_sk) { | 
 | 191 | 			if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { | 
 | 192 | 				IP6_INC_STATS_BH(IPSTATS_MIB_INUNKNOWNPROTOS); | 
| Patrick McHardy | fad87ac | 2005-08-16 21:03:41 -0700 | [diff] [blame] | 193 | 				icmpv6_send(skb, ICMPV6_PARAMPROB, | 
 | 194 | 				            ICMPV6_UNK_NEXTHDR, nhoff, | 
 | 195 | 				            skb->dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | 			} | 
| Patrick McHardy | fad87ac | 2005-08-16 21:03:41 -0700 | [diff] [blame] | 197 | 		} else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | 			IP6_INC_STATS_BH(IPSTATS_MIB_INDELIVERS); | 
| Patrick McHardy | fad87ac | 2005-08-16 21:03:41 -0700 | [diff] [blame] | 199 | 		kfree_skb(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | 	} | 
 | 201 | 	rcu_read_unlock(); | 
 | 202 | 	return 0; | 
 | 203 |  | 
 | 204 | discard: | 
 | 205 | 	IP6_INC_STATS_BH(IPSTATS_MIB_INDISCARDS); | 
 | 206 | 	rcu_read_unlock(); | 
 | 207 | 	kfree_skb(skb); | 
 | 208 | 	return 0; | 
 | 209 | } | 
 | 210 |  | 
 | 211 |  | 
 | 212 | int ip6_input(struct sk_buff *skb) | 
 | 213 | { | 
 | 214 | 	return NF_HOOK(PF_INET6,NF_IP6_LOCAL_IN, skb, skb->dev, NULL, ip6_input_finish); | 
 | 215 | } | 
 | 216 |  | 
 | 217 | int ip6_mc_input(struct sk_buff *skb) | 
 | 218 | { | 
 | 219 | 	struct ipv6hdr *hdr; | 
 | 220 | 	int deliver; | 
 | 221 |  | 
 | 222 | 	IP6_INC_STATS_BH(IPSTATS_MIB_INMCASTPKTS); | 
 | 223 |  | 
 | 224 | 	hdr = skb->nh.ipv6h; | 
 | 225 | 	deliver = likely(!(skb->dev->flags & (IFF_PROMISC|IFF_ALLMULTI))) || | 
 | 226 | 	    ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL); | 
 | 227 |  | 
 | 228 | 	/* | 
 | 229 | 	 *	IPv6 multicast router mode isnt currently supported. | 
 | 230 | 	 */ | 
 | 231 | #if 0 | 
 | 232 | 	if (ipv6_config.multicast_route) { | 
 | 233 | 		int addr_type; | 
 | 234 |  | 
 | 235 | 		addr_type = ipv6_addr_type(&hdr->daddr); | 
 | 236 |  | 
 | 237 | 		if (!(addr_type & (IPV6_ADDR_LOOPBACK | IPV6_ADDR_LINKLOCAL))) { | 
 | 238 | 			struct sk_buff *skb2; | 
 | 239 | 			struct dst_entry *dst; | 
 | 240 |  | 
 | 241 | 			dst = skb->dst; | 
 | 242 | 			 | 
 | 243 | 			if (deliver) { | 
 | 244 | 				skb2 = skb_clone(skb, GFP_ATOMIC); | 
 | 245 | 				dst_output(skb2); | 
 | 246 | 			} else { | 
 | 247 | 				dst_output(skb); | 
 | 248 | 				return 0; | 
 | 249 | 			} | 
 | 250 | 		} | 
 | 251 | 	} | 
 | 252 | #endif | 
 | 253 |  | 
 | 254 | 	if (likely(deliver)) { | 
 | 255 | 		ip6_input(skb); | 
 | 256 | 		return 0; | 
 | 257 | 	} | 
 | 258 | 	/* discard */ | 
 | 259 | 	kfree_skb(skb); | 
 | 260 |  | 
 | 261 | 	return 0; | 
 | 262 | } |