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 * Based in linux/net/ipv4/ip_input.c 10 * 11 * This program is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public License 13 * as published by the Free Software Foundation; either version 14 * 2 of the License, or (at your option) any later version. 15 */ 16 /* Changes 17 * 18 * Mitsuru KANDA @USAGI and 19 * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs(). 20 */ 21 22 #include <linux/errno.h> 23 #include <linux/types.h> 24 #include <linux/socket.h> 25 #include <linux/sockios.h> 26 #include <linux/net.h> 27 #include <linux/netdevice.h> 28 #include <linux/in6.h> 29 #include <linux/icmpv6.h> 30 #include <linux/mroute6.h> 31 #include <linux/slab.h> 32 33 #include <linux/netfilter.h> 34 #include <linux/netfilter_ipv6.h> 35 36 #include <net/sock.h> 37 #include <net/snmp.h> 38 39 #include <net/ipv6.h> 40 #include <net/protocol.h> 41 #include <net/transp_v6.h> 42 #include <net/rawv6.h> 43 #include <net/ndisc.h> 44 #include <net/ip6_route.h> 45 #include <net/addrconf.h> 46 #include <net/xfrm.h> 47 #include <net/inet_ecn.h> 48 #include <net/dst_metadata.h> 49 50 int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 51 { 52 void (*edemux)(struct sk_buff *skb); 53 54 /* if ingress device is enslaved to an L3 master device pass the 55 * skb to its handler for processing 56 */ 57 skb = l3mdev_ip6_rcv(skb); 58 if (!skb) 59 return NET_RX_SUCCESS; 60 61 if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) { 62 const struct inet6_protocol *ipprot; 63 64 ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]); 65 if (ipprot && (edemux = READ_ONCE(ipprot->early_demux))) 66 edemux(skb); 67 } 68 if (!skb_valid_dst(skb)) 69 ip6_route_input(skb); 70 71 return dst_input(skb); 72 } 73 74 int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) 75 { 76 const struct ipv6hdr *hdr; 77 u32 pkt_len; 78 struct inet6_dev *idev; 79 struct net *net = dev_net(skb->dev); 80 81 if (skb->pkt_type == PACKET_OTHERHOST) { 82 kfree_skb(skb); 83 return NET_RX_DROP; 84 } 85 86 rcu_read_lock(); 87 88 idev = __in6_dev_get(skb->dev); 89 90 __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len); 91 92 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL || 93 !idev || unlikely(idev->cnf.disable_ipv6)) { 94 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 95 goto drop; 96 } 97 98 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 99 100 /* 101 * Store incoming device index. When the packet will 102 * be queued, we cannot refer to skb->dev anymore. 103 * 104 * BTW, when we send a packet for our own local address on a 105 * non-loopback interface (e.g. ethX), it is being delivered 106 * via the loopback interface (lo) here; skb->dev = loopback_dev. 107 * It, however, should be considered as if it is being 108 * arrived via the sending interface (ethX), because of the 109 * nature of scoping architecture. --yoshfuji 110 */ 111 IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex; 112 113 if (unlikely(!pskb_may_pull(skb, sizeof(*hdr)))) 114 goto err; 115 116 hdr = ipv6_hdr(skb); 117 118 if (hdr->version != 6) 119 goto err; 120 121 __IP6_ADD_STATS(net, idev, 122 IPSTATS_MIB_NOECTPKTS + 123 (ipv6_get_dsfield(hdr) & INET_ECN_MASK), 124 max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs)); 125 /* 126 * RFC4291 2.5.3 127 * A packet received on an interface with a destination address 128 * of loopback must be dropped. 129 */ 130 if (!(dev->flags & IFF_LOOPBACK) && 131 ipv6_addr_loopback(&hdr->daddr)) 132 goto err; 133 134 /* RFC4291 Errata ID: 3480 135 * Interface-Local scope spans only a single interface on a 136 * node and is useful only for loopback transmission of 137 * multicast. Packets with interface-local scope received 138 * from another node must be discarded. 139 */ 140 if (!(skb->pkt_type == PACKET_LOOPBACK || 141 dev->flags & IFF_LOOPBACK) && 142 ipv6_addr_is_multicast(&hdr->daddr) && 143 IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1) 144 goto err; 145 146 /* If enabled, drop unicast packets that were encapsulated in link-layer 147 * multicast or broadcast to protected against the so-called "hole-196" 148 * attack in 802.11 wireless. 149 */ 150 if (!ipv6_addr_is_multicast(&hdr->daddr) && 151 (skb->pkt_type == PACKET_BROADCAST || 152 skb->pkt_type == PACKET_MULTICAST) && 153 idev->cnf.drop_unicast_in_l2_multicast) 154 goto err; 155 156 /* RFC4291 2.7 157 * Nodes must not originate a packet to a multicast address whose scope 158 * field contains the reserved value 0; if such a packet is received, it 159 * must be silently dropped. 160 */ 161 if (ipv6_addr_is_multicast(&hdr->daddr) && 162 IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0) 163 goto err; 164 165 /* 166 * RFC4291 2.7 167 * Multicast addresses must not be used as source addresses in IPv6 168 * packets or appear in any Routing header. 169 */ 170 if (ipv6_addr_is_multicast(&hdr->saddr)) 171 goto err; 172 173 skb->transport_header = skb->network_header + sizeof(*hdr); 174 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); 175 176 pkt_len = ntohs(hdr->payload_len); 177 178 /* pkt_len may be zero if Jumbo payload option is present */ 179 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) { 180 if (pkt_len + sizeof(struct ipv6hdr) > skb->len) { 181 __IP6_INC_STATS(net, 182 idev, IPSTATS_MIB_INTRUNCATEDPKTS); 183 goto drop; 184 } 185 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) { 186 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 187 goto drop; 188 } 189 hdr = ipv6_hdr(skb); 190 } 191 192 if (hdr->nexthdr == NEXTHDR_HOP) { 193 if (ipv6_parse_hopopts(skb) < 0) { 194 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 195 rcu_read_unlock(); 196 return NET_RX_DROP; 197 } 198 } 199 200 rcu_read_unlock(); 201 202 /* Must drop socket now because of tproxy. */ 203 skb_orphan(skb); 204 205 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, 206 net, NULL, skb, dev, NULL, 207 ip6_rcv_finish); 208 err: 209 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 210 drop: 211 rcu_read_unlock(); 212 kfree_skb(skb); 213 return NET_RX_DROP; 214 } 215 216 /* 217 * Deliver the packet to the host 218 */ 219 220 221 static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 222 { 223 const struct inet6_protocol *ipprot; 224 struct inet6_dev *idev; 225 unsigned int nhoff; 226 int nexthdr; 227 bool raw; 228 bool have_final = false; 229 230 /* 231 * Parse extension headers 232 */ 233 234 rcu_read_lock(); 235 resubmit: 236 idev = ip6_dst_idev(skb_dst(skb)); 237 if (!pskb_pull(skb, skb_transport_offset(skb))) 238 goto discard; 239 nhoff = IP6CB(skb)->nhoff; 240 nexthdr = skb_network_header(skb)[nhoff]; 241 242 resubmit_final: 243 raw = raw6_local_deliver(skb, nexthdr); 244 ipprot = rcu_dereference(inet6_protos[nexthdr]); 245 if (ipprot) { 246 int ret; 247 248 if (have_final) { 249 if (!(ipprot->flags & INET6_PROTO_FINAL)) { 250 /* Once we've seen a final protocol don't 251 * allow encapsulation on any non-final 252 * ones. This allows foo in UDP encapsulation 253 * to work. 254 */ 255 goto discard; 256 } 257 } else if (ipprot->flags & INET6_PROTO_FINAL) { 258 const struct ipv6hdr *hdr; 259 260 /* Only do this once for first final protocol */ 261 have_final = true; 262 263 /* Free reference early: we don't need it any more, 264 and it may hold ip_conntrack module loaded 265 indefinitely. */ 266 nf_reset(skb); 267 268 skb_postpull_rcsum(skb, skb_network_header(skb), 269 skb_network_header_len(skb)); 270 hdr = ipv6_hdr(skb); 271 if (ipv6_addr_is_multicast(&hdr->daddr) && 272 !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, 273 &hdr->saddr) && 274 !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb))) 275 goto discard; 276 } 277 if (!(ipprot->flags & INET6_PROTO_NOPOLICY) && 278 !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 279 goto discard; 280 281 ret = ipprot->handler(skb); 282 if (ret > 0) { 283 if (ipprot->flags & INET6_PROTO_FINAL) { 284 /* Not an extension header, most likely UDP 285 * encapsulation. Use return value as nexthdr 286 * protocol not nhoff (which presumably is 287 * not set by handler). 288 */ 289 nexthdr = ret; 290 goto resubmit_final; 291 } else { 292 goto resubmit; 293 } 294 } else if (ret == 0) { 295 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); 296 } 297 } else { 298 if (!raw) { 299 if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { 300 __IP6_INC_STATS(net, idev, 301 IPSTATS_MIB_INUNKNOWNPROTOS); 302 icmpv6_send(skb, ICMPV6_PARAMPROB, 303 ICMPV6_UNK_NEXTHDR, nhoff); 304 } 305 kfree_skb(skb); 306 } else { 307 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); 308 consume_skb(skb); 309 } 310 } 311 rcu_read_unlock(); 312 return 0; 313 314 discard: 315 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 316 rcu_read_unlock(); 317 kfree_skb(skb); 318 return 0; 319 } 320 321 322 int ip6_input(struct sk_buff *skb) 323 { 324 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, 325 dev_net(skb->dev), NULL, skb, skb->dev, NULL, 326 ip6_input_finish); 327 } 328 EXPORT_SYMBOL_GPL(ip6_input); 329 330 int ip6_mc_input(struct sk_buff *skb) 331 { 332 const struct ipv6hdr *hdr; 333 bool deliver; 334 335 __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev), 336 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INMCAST, 337 skb->len); 338 339 hdr = ipv6_hdr(skb); 340 deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL); 341 342 #ifdef CONFIG_IPV6_MROUTE 343 /* 344 * IPv6 multicast router mode is now supported ;) 345 */ 346 if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding && 347 !(ipv6_addr_type(&hdr->daddr) & 348 (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) && 349 likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) { 350 /* 351 * Okay, we try to forward - split and duplicate 352 * packets. 353 */ 354 struct sk_buff *skb2; 355 struct inet6_skb_parm *opt = IP6CB(skb); 356 357 /* Check for MLD */ 358 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { 359 /* Check if this is a mld message */ 360 u8 nexthdr = hdr->nexthdr; 361 __be16 frag_off; 362 int offset; 363 364 /* Check if the value of Router Alert 365 * is for MLD (0x0000). 366 */ 367 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) { 368 deliver = false; 369 370 if (!ipv6_ext_hdr(nexthdr)) { 371 /* BUG */ 372 goto out; 373 } 374 offset = ipv6_skip_exthdr(skb, sizeof(*hdr), 375 &nexthdr, &frag_off); 376 if (offset < 0) 377 goto out; 378 379 if (ipv6_is_mld(skb, nexthdr, offset)) 380 deliver = true; 381 382 goto out; 383 } 384 /* unknown RA - process it normally */ 385 } 386 387 if (deliver) 388 skb2 = skb_clone(skb, GFP_ATOMIC); 389 else { 390 skb2 = skb; 391 skb = NULL; 392 } 393 394 if (skb2) { 395 ip6_mr_input(skb2); 396 } 397 } 398 out: 399 #endif 400 if (likely(deliver)) 401 ip6_input(skb); 402 else { 403 /* discard */ 404 kfree_skb(skb); 405 } 406 407 return 0; 408 } 409