^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * IPv6 input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Linux INET6 implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Pedro Roque <roque@di.fc.ul.pt>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Ian P. Morris <I.P.Morris@soton.ac.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Based in linux/net/ipv4/ip_input.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /* Changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Mitsuru KANDA @USAGI and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/icmpv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/mroute6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/indirect_call_wrapper.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/netfilter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/netfilter_ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <net/snmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <net/transp_v6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <net/rawv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <net/ndisc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <net/addrconf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <net/inet_ecn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) INDIRECT_CALLABLE_DECLARE(void udp_v6_early_demux(struct sk_buff *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) INDIRECT_CALLABLE_DECLARE(void tcp_v6_early_demux(struct sk_buff *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static void ip6_rcv_finish_core(struct net *net, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) void (*edemux)(struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) const struct inet6_protocol *ipprot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (ipprot && (edemux = READ_ONCE(ipprot->early_demux)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) INDIRECT_CALL_2(edemux, tcp_v6_early_demux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) udp_v6_early_demux, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (!skb_valid_dst(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ip6_route_input(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* if ingress device is enslaved to an L3 master device pass the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * skb to its handler for processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) skb = l3mdev_ip6_rcv(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ip6_rcv_finish_core(net, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return dst_input(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static void ip6_sublist_rcv_finish(struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct sk_buff *skb, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) list_for_each_entry_safe(skb, next, head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) skb_list_del_init(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) dst_input(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static bool ip6_can_use_hint(const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) const struct sk_buff *hint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return hint && !skb_dst(skb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ipv6_addr_equal(&ipv6_hdr(hint)->daddr, &ipv6_hdr(skb)->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static struct sk_buff *ip6_extract_route_hint(const struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (fib6_routes_require_src(net) || fib6_has_custom_rules(net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static void ip6_list_rcv_finish(struct net *net, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct sk_buff *skb, *next, *hint = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct dst_entry *curr_dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct list_head sublist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) INIT_LIST_HEAD(&sublist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) list_for_each_entry_safe(skb, next, head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) skb_list_del_init(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* if ingress device is enslaved to an L3 master device pass the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * skb to its handler for processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) skb = l3mdev_ip6_rcv(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (ip6_can_use_hint(skb, hint))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) skb_dst_copy(skb, hint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ip6_rcv_finish_core(net, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (curr_dst != dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) hint = ip6_extract_route_hint(net, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* dispatch old sublist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!list_empty(&sublist))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ip6_sublist_rcv_finish(&sublist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* start new sublist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) INIT_LIST_HEAD(&sublist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) curr_dst = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) list_add_tail(&skb->list, &sublist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* dispatch final sublist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ip6_sublist_rcv_finish(&sublist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) const struct ipv6hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) u32 pkt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (skb->pkt_type == PACKET_OTHERHOST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) idev = __in6_dev_get(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) !idev || unlikely(idev->cnf.disable_ipv6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * Store incoming device index. When the packet will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * be queued, we cannot refer to skb->dev anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * BTW, when we send a packet for our own local address on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * non-loopback interface (e.g. ethX), it is being delivered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * via the loopback interface (lo) here; skb->dev = loopback_dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * It, however, should be considered as if it is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * arrived via the sending interface (ethX), because of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * nature of scoping architecture. --yoshfuji
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (hdr->version != 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) __IP6_ADD_STATS(net, idev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) IPSTATS_MIB_NOECTPKTS +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) (ipv6_get_dsfield(hdr) & INET_ECN_MASK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * RFC4291 2.5.3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * The loopback address must not be used as the source address in IPv6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * packets that are sent outside of a single node. [..]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * A packet received on an interface with a destination address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * of loopback must be dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if ((ipv6_addr_loopback(&hdr->saddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) ipv6_addr_loopback(&hdr->daddr)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) !(dev->flags & IFF_LOOPBACK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) !netif_is_l3_master(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* RFC4291 Errata ID: 3480
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * Interface-Local scope spans only a single interface on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * node and is useful only for loopback transmission of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * multicast. Packets with interface-local scope received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * from another node must be discarded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (!(skb->pkt_type == PACKET_LOOPBACK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) dev->flags & IFF_LOOPBACK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) ipv6_addr_is_multicast(&hdr->daddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* If enabled, drop unicast packets that were encapsulated in link-layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * multicast or broadcast to protected against the so-called "hole-196"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * attack in 802.11 wireless.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (!ipv6_addr_is_multicast(&hdr->daddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) (skb->pkt_type == PACKET_BROADCAST ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) skb->pkt_type == PACKET_MULTICAST) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) idev->cnf.drop_unicast_in_l2_multicast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /* RFC4291 2.7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * Nodes must not originate a packet to a multicast address whose scope
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * field contains the reserved value 0; if such a packet is received, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * must be silently dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (ipv6_addr_is_multicast(&hdr->daddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * RFC4291 2.7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * Multicast addresses must not be used as source addresses in IPv6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * packets or appear in any Routing header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (ipv6_addr_is_multicast(&hdr->saddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) skb->transport_header = skb->network_header + sizeof(*hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) pkt_len = ntohs(hdr->payload_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* pkt_len may be zero if Jumbo payload option is present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) __IP6_INC_STATS(net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) idev, IPSTATS_MIB_INTRUNCATEDPKTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (hdr->nexthdr == NEXTHDR_HOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (ipv6_parse_hopopts(skb) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /* Must drop socket now because of tproxy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!skb_sk_is_prefetched(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) skb_orphan(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) skb = ip6_rcv_core(skb, dev, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) net, NULL, skb, dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) ip6_rcv_finish);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) head, dev, NULL, ip6_rcv_finish);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ip6_list_rcv_finish(net, NULL, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /* Receive a list of IPv6 packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) void ipv6_list_rcv(struct list_head *head, struct packet_type *pt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct net_device *orig_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct net_device *curr_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct net *curr_net = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct sk_buff *skb, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct list_head sublist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) INIT_LIST_HEAD(&sublist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) list_for_each_entry_safe(skb, next, head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct net_device *dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) skb_list_del_init(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) skb = ip6_rcv_core(skb, dev, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (curr_dev != dev || curr_net != net) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /* dispatch old sublist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (!list_empty(&sublist))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) ip6_sublist_rcv(&sublist, curr_dev, curr_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* start new sublist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) INIT_LIST_HEAD(&sublist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) curr_dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) curr_net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) list_add_tail(&skb->list, &sublist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /* dispatch final sublist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (!list_empty(&sublist))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) ip6_sublist_rcv(&sublist, curr_dev, curr_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * Deliver the packet to the host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) bool have_final)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) const struct inet6_protocol *ipprot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) unsigned int nhoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) bool raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * Parse extension headers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) resubmit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) idev = ip6_dst_idev(skb_dst(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) nhoff = IP6CB(skb)->nhoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (!have_final) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (!pskb_pull(skb, skb_transport_offset(skb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) goto discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) nexthdr = skb_network_header(skb)[nhoff];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) resubmit_final:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) raw = raw6_local_deliver(skb, nexthdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ipprot = rcu_dereference(inet6_protos[nexthdr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (ipprot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (have_final) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (!(ipprot->flags & INET6_PROTO_FINAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* Once we've seen a final protocol don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * allow encapsulation on any non-final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * ones. This allows foo in UDP encapsulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * to work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) goto discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) } else if (ipprot->flags & INET6_PROTO_FINAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) const struct ipv6hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) int sdif = inet6_sdif(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /* Only do this once for first final protocol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) have_final = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /* Free reference early: we don't need it any more,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) and it may hold ip_conntrack module loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) indefinitely. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) nf_reset_ct(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) skb_postpull_rcsum(skb, skb_network_header(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) skb_network_header_len(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) /* skb->dev passed may be master dev for vrfs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (sdif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) dev = dev_get_by_index_rcu(net, sdif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) goto discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (ipv6_addr_is_multicast(&hdr->daddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) !ipv6_chk_mcast_addr(dev, &hdr->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) &hdr->saddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) goto discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) goto discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (ipprot->flags & INET6_PROTO_FINAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) /* Not an extension header, most likely UDP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * encapsulation. Use return value as nexthdr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * protocol not nhoff (which presumably is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * not set by handler).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) nexthdr = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) goto resubmit_final;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) goto resubmit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) } else if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (!raw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) __IP6_INC_STATS(net, idev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) IPSTATS_MIB_INUNKNOWNPROTOS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) icmpv6_send(skb, ICMPV6_PARAMPROB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) ICMPV6_UNK_NEXTHDR, nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) discard:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) ip6_protocol_deliver_rcu(net, skb, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) int ip6_input(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) dev_net(skb->dev), NULL, skb, skb->dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ip6_input_finish);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) EXPORT_SYMBOL_GPL(ip6_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) int ip6_mc_input(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) int sdif = inet6_sdif(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) const struct ipv6hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) bool deliver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) /* skb->dev passed may be master dev for vrfs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (sdif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) dev = dev_get_by_index_rcu(dev_net(skb->dev), sdif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (sdif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) #ifdef CONFIG_IPV6_MROUTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * IPv6 multicast router mode is now supported ;)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) !(ipv6_addr_type(&hdr->daddr) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * Okay, we try to forward - split and duplicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct sk_buff *skb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct inet6_skb_parm *opt = IP6CB(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /* Check for MLD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) /* Check if this is a mld message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) u8 nexthdr = hdr->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) __be16 frag_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /* Check if the value of Router Alert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * is for MLD (0x0000).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) deliver = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (!ipv6_ext_hdr(nexthdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) /* BUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) &nexthdr, &frag_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (ipv6_is_mld(skb, nexthdr, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) deliver = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) /* unknown RA - process it normally */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (deliver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) skb2 = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) skb2 = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (skb2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) ip6_mr_input(skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (likely(deliver))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) ip6_input(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /* discard */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }