^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) /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "ipvlan.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) static unsigned int ipvlan_netid __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) struct ipvlan_netns {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) unsigned int ipvl_nf_hook_refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static struct ipvl_addr *ipvlan_skb_to_addr(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct ipvl_addr *addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct ipvl_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) int addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) void *lyr3h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) if (!dev || !netif_is_ipvlan_port(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) port = ipvlan_port_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (!port || port->mode != IPVLAN_MODE_L3S)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!lyr3h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static struct sk_buff *ipvlan_l3_rcv(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct sk_buff *skb, u16 proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct ipvl_addr *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct net_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) addr = ipvlan_skb_to_addr(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) sdev = addr->master->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) switch (proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct iphdr *ip4h = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) err = ip_route_input_noref(skb, ip4h->daddr, ip4h->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ip4h->tos, sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct ipv6hdr *ip6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int flags = RT6_LOOKUP_F_HAS_SADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct flowi6 fl6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .flowi6_iif = sdev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .daddr = ip6h->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .saddr = ip6h->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .flowlabel = ip6_flowinfo(ip6h),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .flowi6_mark = skb->mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .flowi6_proto = ip6h->nexthdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dst = ip6_route_input_lookup(dev_net(sdev), sdev, &fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) skb, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return skb;
^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 const struct l3mdev_ops ipvl_l3mdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .l3mdev_l3_rcv = ipvlan_l3_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static unsigned int ipvlan_nf_input(void *priv, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) const struct nf_hook_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct ipvl_addr *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) addr = ipvlan_skb_to_addr(skb, skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) skb->dev = addr->master->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) len = skb->len + ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ipvlan_count_rx(addr->master, len, true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return NF_ACCEPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static const struct nf_hook_ops ipvl_nfops[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .hook = ipvlan_nf_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .pf = NFPROTO_IPV4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .hooknum = NF_INET_LOCAL_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .priority = INT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .hook = ipvlan_nf_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .pf = NFPROTO_IPV6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .hooknum = NF_INET_LOCAL_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .priority = INT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int ipvlan_register_nf_hook(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!vnet->ipvl_nf_hook_refcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) err = nf_register_net_hooks(net, ipvl_nfops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ARRAY_SIZE(ipvl_nfops));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) vnet->ipvl_nf_hook_refcnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) vnet->ipvl_nf_hook_refcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static void ipvlan_unregister_nf_hook(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) vnet->ipvl_nf_hook_refcnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!vnet->ipvl_nf_hook_refcnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) nf_unregister_net_hooks(net, ipvl_nfops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ARRAY_SIZE(ipvl_nfops));
^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) void ipvlan_migrate_l3s_hook(struct net *oldnet, struct net *newnet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct ipvlan_netns *old_vnet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) old_vnet = net_generic(oldnet, ipvlan_netid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (!old_vnet->ipvl_nf_hook_refcnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ipvlan_register_nf_hook(newnet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ipvlan_unregister_nf_hook(oldnet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void ipvlan_ns_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) vnet->ipvl_nf_hook_refcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) nf_unregister_net_hooks(net, ipvl_nfops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ARRAY_SIZE(ipvl_nfops));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static struct pernet_operations ipvlan_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .id = &ipvlan_netid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .size = sizeof(struct ipvlan_netns),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .exit = ipvlan_ns_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int ipvlan_l3s_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return register_pernet_subsys(&ipvlan_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) void ipvlan_l3s_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) unregister_pernet_subsys(&ipvlan_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int ipvlan_l3s_register(struct ipvl_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct net_device *dev = port->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) ret = ipvlan_register_nf_hook(read_pnet(&port->pnet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) dev->l3mdev_ops = &ipvl_l3mdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) dev->priv_flags |= IFF_L3MDEV_RX_HANDLER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) void ipvlan_l3s_unregister(struct ipvl_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct net_device *dev = port->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) dev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) dev->l3mdev_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }