Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / ipvlan / ipvlan_l3s.c
blobb4ef386bdb1ba54ece47c8e75406dfcfd09c084d
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
3 */
5 #include <net/ip.h>
7 #include "ipvlan.h"
9 static unsigned int ipvlan_netid __read_mostly;
11 struct ipvlan_netns {
12 unsigned int ipvl_nf_hook_refcnt;
15 static struct ipvl_addr *ipvlan_skb_to_addr(struct sk_buff *skb,
16 struct net_device *dev)
18 struct ipvl_addr *addr = NULL;
19 struct ipvl_port *port;
20 int addr_type;
21 void *lyr3h;
23 if (!dev || !netif_is_ipvlan_port(dev))
24 goto out;
26 port = ipvlan_port_get_rcu(dev);
27 if (!port || port->mode != IPVLAN_MODE_L3S)
28 goto out;
30 lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
31 if (!lyr3h)
32 goto out;
34 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
35 out:
36 return addr;
39 static struct sk_buff *ipvlan_l3_rcv(struct net_device *dev,
40 struct sk_buff *skb, u16 proto)
42 struct ipvl_addr *addr;
43 struct net_device *sdev;
45 addr = ipvlan_skb_to_addr(skb, dev);
46 if (!addr)
47 goto out;
49 sdev = addr->master->dev;
50 switch (proto) {
51 case AF_INET:
53 const struct iphdr *ip4h = ip_hdr(skb);
54 int err;
56 err = ip_route_input_noref(skb, ip4h->daddr, ip4h->saddr,
57 ip4h_dscp(ip4h), sdev);
58 if (unlikely(err))
59 goto out;
60 break;
62 #if IS_ENABLED(CONFIG_IPV6)
63 case AF_INET6:
65 struct dst_entry *dst;
66 struct ipv6hdr *ip6h = ipv6_hdr(skb);
67 int flags = RT6_LOOKUP_F_HAS_SADDR;
68 struct flowi6 fl6 = {
69 .flowi6_iif = sdev->ifindex,
70 .daddr = ip6h->daddr,
71 .saddr = ip6h->saddr,
72 .flowlabel = ip6_flowinfo(ip6h),
73 .flowi6_mark = skb->mark,
74 .flowi6_proto = ip6h->nexthdr,
77 skb_dst_drop(skb);
78 dst = ip6_route_input_lookup(dev_net(sdev), sdev, &fl6,
79 skb, flags);
80 skb_dst_set(skb, dst);
81 break;
83 #endif
84 default:
85 break;
87 out:
88 return skb;
91 static const struct l3mdev_ops ipvl_l3mdev_ops = {
92 .l3mdev_l3_rcv = ipvlan_l3_rcv,
95 static unsigned int ipvlan_nf_input(void *priv, struct sk_buff *skb,
96 const struct nf_hook_state *state)
98 struct ipvl_addr *addr;
99 unsigned int len;
101 addr = ipvlan_skb_to_addr(skb, skb->dev);
102 if (!addr)
103 goto out;
105 skb->dev = addr->master->dev;
106 skb->skb_iif = skb->dev->ifindex;
107 #if IS_ENABLED(CONFIG_IPV6)
108 if (addr->atype == IPVL_IPV6)
109 IP6CB(skb)->iif = skb->dev->ifindex;
110 #endif
111 len = skb->len + ETH_HLEN;
112 ipvlan_count_rx(addr->master, len, true, false);
113 out:
114 return NF_ACCEPT;
117 static const struct nf_hook_ops ipvl_nfops[] = {
119 .hook = ipvlan_nf_input,
120 .pf = NFPROTO_IPV4,
121 .hooknum = NF_INET_LOCAL_IN,
122 .priority = INT_MAX,
124 #if IS_ENABLED(CONFIG_IPV6)
126 .hook = ipvlan_nf_input,
127 .pf = NFPROTO_IPV6,
128 .hooknum = NF_INET_LOCAL_IN,
129 .priority = INT_MAX,
131 #endif
134 static int ipvlan_register_nf_hook(struct net *net)
136 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
137 int err = 0;
139 if (!vnet->ipvl_nf_hook_refcnt) {
140 err = nf_register_net_hooks(net, ipvl_nfops,
141 ARRAY_SIZE(ipvl_nfops));
142 if (!err)
143 vnet->ipvl_nf_hook_refcnt = 1;
144 } else {
145 vnet->ipvl_nf_hook_refcnt++;
148 return err;
151 static void ipvlan_unregister_nf_hook(struct net *net)
153 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
155 if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
156 return;
158 vnet->ipvl_nf_hook_refcnt--;
159 if (!vnet->ipvl_nf_hook_refcnt)
160 nf_unregister_net_hooks(net, ipvl_nfops,
161 ARRAY_SIZE(ipvl_nfops));
164 void ipvlan_migrate_l3s_hook(struct net *oldnet, struct net *newnet)
166 struct ipvlan_netns *old_vnet;
168 ASSERT_RTNL();
170 old_vnet = net_generic(oldnet, ipvlan_netid);
171 if (!old_vnet->ipvl_nf_hook_refcnt)
172 return;
174 ipvlan_register_nf_hook(newnet);
175 ipvlan_unregister_nf_hook(oldnet);
178 static void ipvlan_ns_exit(struct net *net)
180 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
182 if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
183 vnet->ipvl_nf_hook_refcnt = 0;
184 nf_unregister_net_hooks(net, ipvl_nfops,
185 ARRAY_SIZE(ipvl_nfops));
189 static struct pernet_operations ipvlan_net_ops = {
190 .id = &ipvlan_netid,
191 .size = sizeof(struct ipvlan_netns),
192 .exit = ipvlan_ns_exit,
195 int ipvlan_l3s_init(void)
197 return register_pernet_subsys(&ipvlan_net_ops);
200 void ipvlan_l3s_cleanup(void)
202 unregister_pernet_subsys(&ipvlan_net_ops);
205 int ipvlan_l3s_register(struct ipvl_port *port)
207 struct net_device *dev = port->dev;
208 int ret;
210 ASSERT_RTNL();
212 ret = ipvlan_register_nf_hook(read_pnet(&port->pnet));
213 if (!ret) {
214 dev->l3mdev_ops = &ipvl_l3mdev_ops;
215 dev->priv_flags |= IFF_L3MDEV_RX_HANDLER;
218 return ret;
221 void ipvlan_l3s_unregister(struct ipvl_port *port)
223 struct net_device *dev = port->dev;
225 ASSERT_RTNL();
227 dev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
228 ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
229 dev->l3mdev_ops = NULL;