blob: 7c7680c8f0e32e149167f7943dc02bdc9646da23 [file] [log] [blame]
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001/* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
7 *
8 */
9
10#include "ipvlan.h"
11
Florian Westphal31338222017-04-20 18:08:15 +020012static unsigned int ipvlan_netid __read_mostly;
13
14struct ipvlan_netns {
15 unsigned int ipvl_nf_hook_refcnt;
16};
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070017
18static struct nf_hook_ops ipvl_nfops[] __read_mostly = {
19 {
20 .hook = ipvlan_nf_input,
21 .pf = NFPROTO_IPV4,
22 .hooknum = NF_INET_LOCAL_IN,
23 .priority = INT_MAX,
24 },
25 {
26 .hook = ipvlan_nf_input,
27 .pf = NFPROTO_IPV6,
28 .hooknum = NF_INET_LOCAL_IN,
29 .priority = INT_MAX,
30 },
31};
32
Julia Lawallab530f62016-10-15 17:40:30 +020033static const struct l3mdev_ops ipvl_l3mdev_ops = {
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070034 .l3mdev_l3_rcv = ipvlan_l3_rcv,
35};
36
Mahesh Bandewarab5b7012016-02-20 19:31:41 -080037static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080038{
Gao Feng8f679ed2016-11-30 08:48:44 +080039 ipvlan->dev->mtu = dev->mtu;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080040}
41
Florian Westphal31338222017-04-20 18:08:15 +020042static int ipvlan_register_nf_hook(struct net *net)
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070043{
Florian Westphal31338222017-04-20 18:08:15 +020044 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070045 int err = 0;
46
Florian Westphal31338222017-04-20 18:08:15 +020047 if (!vnet->ipvl_nf_hook_refcnt) {
48 err = nf_register_net_hooks(net, ipvl_nfops,
49 ARRAY_SIZE(ipvl_nfops));
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070050 if (!err)
Florian Westphal31338222017-04-20 18:08:15 +020051 vnet->ipvl_nf_hook_refcnt = 1;
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070052 } else {
Florian Westphal31338222017-04-20 18:08:15 +020053 vnet->ipvl_nf_hook_refcnt++;
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070054 }
55
56 return err;
57}
58
Florian Westphal31338222017-04-20 18:08:15 +020059static void ipvlan_unregister_nf_hook(struct net *net)
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070060{
Florian Westphal31338222017-04-20 18:08:15 +020061 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070062
Florian Westphal31338222017-04-20 18:08:15 +020063 if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
64 return;
65
66 vnet->ipvl_nf_hook_refcnt--;
67 if (!vnet->ipvl_nf_hook_refcnt)
68 nf_unregister_net_hooks(net, ipvl_nfops,
69 ARRAY_SIZE(ipvl_nfops));
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070070}
71
72static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080073{
74 struct ipvl_dev *ipvlan;
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070075 struct net_device *mdev = port->dev;
76 int err = 0;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080077
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070078 ASSERT_RTNL();
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080079 if (port->mode != nval) {
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070080 if (nval == IPVLAN_MODE_L3S) {
81 /* New mode is L3S */
Florian Westphal31338222017-04-20 18:08:15 +020082 err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070083 if (!err) {
84 mdev->l3mdev_ops = &ipvl_l3mdev_ops;
85 mdev->priv_flags |= IFF_L3MDEV_MASTER;
86 } else
87 return err;
88 } else if (port->mode == IPVLAN_MODE_L3S) {
89 /* Old mode was L3S */
90 mdev->priv_flags &= ~IFF_L3MDEV_MASTER;
Florian Westphal31338222017-04-20 18:08:15 +020091 ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070092 mdev->l3mdev_ops = NULL;
93 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080094 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070095 if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080096 ipvlan->dev->flags |= IFF_NOARP;
97 else
98 ipvlan->dev->flags &= ~IFF_NOARP;
99 }
100 port->mode = nval;
101 }
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700102 return err;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800103}
104
105static int ipvlan_port_create(struct net_device *dev)
106{
107 struct ipvl_port *port;
108 int err, idx;
109
110 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) {
111 netdev_err(dev, "Master is either lo or non-ether device\n");
112 return -EINVAL;
113 }
Mahesh Bandewar764e4332014-12-06 15:53:19 -0800114
Mahesh Bandewarc3262d92017-01-18 15:02:53 -0800115 if (netdev_is_rx_handler_busy(dev)) {
116 netdev_err(dev, "Device is already in use.\n");
Mahesh Bandewar764e4332014-12-06 15:53:19 -0800117 return -EBUSY;
118 }
119
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800120 port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
121 if (!port)
122 return -ENOMEM;
123
Florian Westphal31338222017-04-20 18:08:15 +0200124 write_pnet(&port->pnet, dev_net(dev));
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800125 port->dev = dev;
126 port->mode = IPVLAN_MODE_L3;
127 INIT_LIST_HEAD(&port->ipvlans);
128 for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
129 INIT_HLIST_HEAD(&port->hlhead[idx]);
130
Mahesh Bandewarba35f852015-05-04 17:06:03 -0700131 skb_queue_head_init(&port->backlog);
132 INIT_WORK(&port->wq, ipvlan_process_multicast);
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800133 ida_init(&port->ida);
Mahesh Bandewarda36e132017-01-09 15:05:54 -0800134 port->dev_id_start = 1;
Mahesh Bandewarba35f852015-05-04 17:06:03 -0700135
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800136 err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
137 if (err)
138 goto err;
139
140 dev->priv_flags |= IFF_IPVLAN_MASTER;
141 return 0;
142
143err:
Gao Feng48140a22016-12-07 08:44:47 +0800144 kfree(port);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800145 return err;
146}
147
148static void ipvlan_port_destroy(struct net_device *dev)
149{
150 struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
Eric Dumazetb1227d02016-12-21 18:00:24 -0800151 struct sk_buff *skb;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800152
153 dev->priv_flags &= ~IFF_IPVLAN_MASTER;
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700154 if (port->mode == IPVLAN_MODE_L3S) {
155 dev->priv_flags &= ~IFF_L3MDEV_MASTER;
Florian Westphal31338222017-04-20 18:08:15 +0200156 ipvlan_unregister_nf_hook(dev_net(dev));
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700157 dev->l3mdev_ops = NULL;
158 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800159 netdev_rx_handler_unregister(dev);
Mahesh Bandewarba35f852015-05-04 17:06:03 -0700160 cancel_work_sync(&port->wq);
Eric Dumazetb1227d02016-12-21 18:00:24 -0800161 while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
162 if (skb->dev)
163 dev_put(skb->dev);
164 kfree_skb(skb);
165 }
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800166 ida_destroy(&port->ida);
Gao Feng48140a22016-12-07 08:44:47 +0800167 kfree(port);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800168}
169
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800170#define IPVLAN_FEATURES \
Tom Herberta1882222015-12-14 11:19:43 -0800171 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800172 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
173 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
174 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
175
176#define IPVLAN_STATE_MASK \
177 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
178
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800179static int ipvlan_init(struct net_device *dev)
180{
181 struct ipvl_dev *ipvlan = netdev_priv(dev);
182 const struct net_device *phy_dev = ipvlan->phy_dev;
Mahesh Bandewar494e8482016-04-27 14:59:27 -0700183 struct ipvl_port *port = ipvlan->port;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800184
185 dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
186 (phy_dev->state & IPVLAN_STATE_MASK);
187 dev->features = phy_dev->features & IPVLAN_FEATURES;
188 dev->features |= NETIF_F_LLTX;
189 dev->gso_max_size = phy_dev->gso_max_size;
Eric Dumazetf6773c52016-03-16 21:59:49 -0700190 dev->gso_max_segs = phy_dev->gso_max_segs;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800191 dev->hard_header_len = phy_dev->hard_header_len;
192
Eric Dumazet0d7dd792016-06-09 07:45:15 -0700193 netdev_lockdep_set_classes(dev);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800194
195 ipvlan->pcpu_stats = alloc_percpu(struct ipvl_pcpu_stats);
196 if (!ipvlan->pcpu_stats)
197 return -ENOMEM;
198
Mahesh Bandewar494e8482016-04-27 14:59:27 -0700199 port->count += 1;
200
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800201 return 0;
202}
203
204static void ipvlan_uninit(struct net_device *dev)
205{
206 struct ipvl_dev *ipvlan = netdev_priv(dev);
207 struct ipvl_port *port = ipvlan->port;
208
Markus Elfring04901ce2014-11-29 16:23:20 +0100209 free_percpu(ipvlan->pcpu_stats);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800210
211 port->count -= 1;
212 if (!port->count)
213 ipvlan_port_destroy(port->dev);
214}
215
216static int ipvlan_open(struct net_device *dev)
217{
218 struct ipvl_dev *ipvlan = netdev_priv(dev);
219 struct net_device *phy_dev = ipvlan->phy_dev;
220 struct ipvl_addr *addr;
221
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700222 if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
223 ipvlan->port->mode == IPVLAN_MODE_L3S)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800224 dev->flags |= IFF_NOARP;
225 else
226 dev->flags &= ~IFF_NOARP;
227
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300228 list_for_each_entry(addr, &ipvlan->addrs, anode)
229 ipvlan_ht_addr_add(ipvlan, addr);
230
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800231 return dev_uc_add(phy_dev, phy_dev->dev_addr);
232}
233
234static int ipvlan_stop(struct net_device *dev)
235{
236 struct ipvl_dev *ipvlan = netdev_priv(dev);
237 struct net_device *phy_dev = ipvlan->phy_dev;
238 struct ipvl_addr *addr;
239
240 dev_uc_unsync(phy_dev, dev);
241 dev_mc_unsync(phy_dev, dev);
242
243 dev_uc_del(phy_dev, phy_dev->dev_addr);
244
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300245 list_for_each_entry(addr, &ipvlan->addrs, anode)
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300246 ipvlan_ht_addr_del(addr);
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300247
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800248 return 0;
249}
250
Mahesh Bandewar92c7b0d2014-11-25 21:24:43 -0800251static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
252 struct net_device *dev)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800253{
254 const struct ipvl_dev *ipvlan = netdev_priv(dev);
255 int skblen = skb->len;
256 int ret;
257
258 ret = ipvlan_queue_xmit(skb, dev);
259 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
260 struct ipvl_pcpu_stats *pcptr;
261
262 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
263
264 u64_stats_update_begin(&pcptr->syncp);
265 pcptr->tx_pkts++;
266 pcptr->tx_bytes += skblen;
267 u64_stats_update_end(&pcptr->syncp);
268 } else {
269 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
270 }
271 return ret;
272}
273
274static netdev_features_t ipvlan_fix_features(struct net_device *dev,
275 netdev_features_t features)
276{
277 struct ipvl_dev *ipvlan = netdev_priv(dev);
278
279 return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
280}
281
282static void ipvlan_change_rx_flags(struct net_device *dev, int change)
283{
284 struct ipvl_dev *ipvlan = netdev_priv(dev);
285 struct net_device *phy_dev = ipvlan->phy_dev;
286
287 if (change & IFF_ALLMULTI)
288 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
289}
290
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800291static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
292{
293 struct ipvl_dev *ipvlan = netdev_priv(dev);
294
295 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
296 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
297 } else {
298 struct netdev_hw_addr *ha;
299 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
300
301 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
302 netdev_for_each_mc_addr(ha, dev)
303 __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
304
Mahesh Bandewarf631c442015-05-04 17:06:11 -0700305 /* Turn-on broadcast bit irrespective of address family,
306 * since broadcast is deferred to a work-queue, hence no
307 * impact on fast-path processing.
308 */
309 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
310
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800311 bitmap_copy(ipvlan->mac_filters, mc_filters,
312 IPVLAN_MAC_FILTER_SIZE);
313 }
314 dev_uc_sync(ipvlan->phy_dev, dev);
315 dev_mc_sync(ipvlan->phy_dev, dev);
316}
317
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800318static void ipvlan_get_stats64(struct net_device *dev,
319 struct rtnl_link_stats64 *s)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800320{
321 struct ipvl_dev *ipvlan = netdev_priv(dev);
322
323 if (ipvlan->pcpu_stats) {
324 struct ipvl_pcpu_stats *pcptr;
325 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
326 u32 rx_errs = 0, tx_drps = 0;
327 u32 strt;
328 int idx;
329
330 for_each_possible_cpu(idx) {
331 pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
332 do {
333 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
334 rx_pkts = pcptr->rx_pkts;
335 rx_bytes = pcptr->rx_bytes;
336 rx_mcast = pcptr->rx_mcast;
337 tx_pkts = pcptr->tx_pkts;
338 tx_bytes = pcptr->tx_bytes;
339 } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
340 strt));
341
342 s->rx_packets += rx_pkts;
343 s->rx_bytes += rx_bytes;
344 s->multicast += rx_mcast;
345 s->tx_packets += tx_pkts;
346 s->tx_bytes += tx_bytes;
347
348 /* u32 values are updated without syncp protection. */
349 rx_errs += pcptr->rx_errs;
350 tx_drps += pcptr->tx_drps;
351 }
352 s->rx_errors = rx_errs;
353 s->rx_dropped = rx_errs;
354 s->tx_dropped = tx_drps;
355 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800356}
357
358static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
359{
360 struct ipvl_dev *ipvlan = netdev_priv(dev);
361 struct net_device *phy_dev = ipvlan->phy_dev;
362
363 return vlan_vid_add(phy_dev, proto, vid);
364}
365
366static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
367 u16 vid)
368{
369 struct ipvl_dev *ipvlan = netdev_priv(dev);
370 struct net_device *phy_dev = ipvlan->phy_dev;
371
372 vlan_vid_del(phy_dev, proto, vid);
373 return 0;
374}
375
Nicolas Dichtel7c411652015-04-02 17:07:06 +0200376static int ipvlan_get_iflink(const struct net_device *dev)
377{
378 struct ipvl_dev *ipvlan = netdev_priv(dev);
379
380 return ipvlan->phy_dev->ifindex;
381}
382
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800383static const struct net_device_ops ipvlan_netdev_ops = {
384 .ndo_init = ipvlan_init,
385 .ndo_uninit = ipvlan_uninit,
386 .ndo_open = ipvlan_open,
387 .ndo_stop = ipvlan_stop,
388 .ndo_start_xmit = ipvlan_start_xmit,
389 .ndo_fix_features = ipvlan_fix_features,
390 .ndo_change_rx_flags = ipvlan_change_rx_flags,
391 .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
392 .ndo_get_stats64 = ipvlan_get_stats64,
393 .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
394 .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
Nicolas Dichtel7c411652015-04-02 17:07:06 +0200395 .ndo_get_iflink = ipvlan_get_iflink,
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800396};
397
398static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
399 unsigned short type, const void *daddr,
400 const void *saddr, unsigned len)
401{
402 const struct ipvl_dev *ipvlan = netdev_priv(dev);
403 struct net_device *phy_dev = ipvlan->phy_dev;
404
405 /* TODO Probably use a different field than dev_addr so that the
406 * mac-address on the virtual device is portable and can be carried
407 * while the packets use the mac-addr on the physical device.
408 */
409 return dev_hard_header(skb, phy_dev, type, daddr,
410 saddr ? : dev->dev_addr, len);
411}
412
413static const struct header_ops ipvlan_header_ops = {
414 .create = ipvlan_hard_header,
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800415 .parse = eth_header_parse,
416 .cache = eth_header_cache,
417 .cache_update = eth_header_cache_update,
418};
419
David Decotigny314d10d2016-02-24 10:58:03 -0800420static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
421 struct ethtool_link_ksettings *cmd)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800422{
423 const struct ipvl_dev *ipvlan = netdev_priv(dev);
424
David Decotigny314d10d2016-02-24 10:58:03 -0800425 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800426}
427
428static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
429 struct ethtool_drvinfo *drvinfo)
430{
431 strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
432 strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
433}
434
435static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
436{
437 const struct ipvl_dev *ipvlan = netdev_priv(dev);
438
439 return ipvlan->msg_enable;
440}
441
442static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
443{
444 struct ipvl_dev *ipvlan = netdev_priv(dev);
445
446 ipvlan->msg_enable = value;
447}
448
449static const struct ethtool_ops ipvlan_ethtool_ops = {
450 .get_link = ethtool_op_get_link,
David Decotigny314d10d2016-02-24 10:58:03 -0800451 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800452 .get_drvinfo = ipvlan_ethtool_get_drvinfo,
453 .get_msglevel = ipvlan_ethtool_get_msglevel,
454 .set_msglevel = ipvlan_ethtool_set_msglevel,
455};
456
457static int ipvlan_nl_changelink(struct net_device *dev,
458 struct nlattr *tb[], struct nlattr *data[])
459{
460 struct ipvl_dev *ipvlan = netdev_priv(dev);
461 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700462 int err = 0;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800463
464 if (data && data[IFLA_IPVLAN_MODE]) {
465 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
466
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700467 err = ipvlan_set_port_mode(port, nmode);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800468 }
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700469 return err;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800470}
471
472static size_t ipvlan_nl_getsize(const struct net_device *dev)
473{
474 return (0
475 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
476 );
477}
478
479static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[])
480{
481 if (data && data[IFLA_IPVLAN_MODE]) {
482 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
483
484 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
485 return -EINVAL;
486 }
487 return 0;
488}
489
490static int ipvlan_nl_fillinfo(struct sk_buff *skb,
491 const struct net_device *dev)
492{
493 struct ipvl_dev *ipvlan = netdev_priv(dev);
494 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
495 int ret = -EINVAL;
496
497 if (!port)
498 goto err;
499
500 ret = -EMSGSIZE;
501 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
502 goto err;
503
504 return 0;
505
506err:
507 return ret;
508}
509
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800510int ipvlan_link_new(struct net *src_net, struct net_device *dev,
511 struct nlattr *tb[], struct nlattr *data[])
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800512{
513 struct ipvl_dev *ipvlan = netdev_priv(dev);
514 struct ipvl_port *port;
515 struct net_device *phy_dev;
516 int err;
Mahesh Bandeware93fbc52016-02-20 19:31:36 -0800517 u16 mode = IPVLAN_MODE_L3;
Gao Feng147fd282016-11-24 23:39:59 +0800518 bool create = false;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800519
520 if (!tb[IFLA_LINK])
521 return -EINVAL;
522
523 phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
524 if (!phy_dev)
525 return -ENODEV;
526
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800527 if (netif_is_ipvlan(phy_dev)) {
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800528 struct ipvl_dev *tmp = netdev_priv(phy_dev);
529
530 phy_dev = tmp->phy_dev;
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800531 } else if (!netif_is_ipvlan_port(phy_dev)) {
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800532 err = ipvlan_port_create(phy_dev);
533 if (err < 0)
534 return err;
Gao Feng147fd282016-11-24 23:39:59 +0800535 create = true;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800536 }
537
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800538 if (data && data[IFLA_IPVLAN_MODE])
Mahesh Bandeware93fbc52016-02-20 19:31:36 -0800539 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800540
Mahesh Bandeware93fbc52016-02-20 19:31:36 -0800541 port = ipvlan_port_get_rtnl(phy_dev);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800542 ipvlan->phy_dev = phy_dev;
543 ipvlan->dev = dev;
544 ipvlan->port = port;
545 ipvlan->sfeatures = IPVLAN_FEATURES;
Mahesh Bandewar296d4852016-01-27 23:33:28 -0800546 ipvlan_adjust_mtu(ipvlan, phy_dev);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800547 INIT_LIST_HEAD(&ipvlan->addrs);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800548
Mahesh Bandewarda36e132017-01-09 15:05:54 -0800549 /* If the port-id base is at the MAX value, then wrap it around and
550 * begin from 0x1 again. This may be due to a busy system where lots
551 * of slaves are getting created and deleted.
552 */
553 if (port->dev_id_start == 0xFFFE)
554 port->dev_id_start = 0x1;
555
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800556 /* Since L2 address is shared among all IPvlan slaves including
557 * master, use unique 16 bit dev-ids to diffentiate among them.
558 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
559 * slave link [see addrconf_ifid_eui48()].
560 */
Mahesh Bandewarda36e132017-01-09 15:05:54 -0800561 err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
562 GFP_KERNEL);
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800563 if (err < 0)
Mahesh Bandewar019ec002017-01-13 15:48:30 -0800564 err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
565 GFP_KERNEL);
566 if (err < 0)
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800567 goto destroy_ipvlan_port;
568 dev->dev_id = err;
Mahesh Bandewarda36e132017-01-09 15:05:54 -0800569 /* Increment id-base to the next slot for the future assignment */
570 port->dev_id_start = err + 1;
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800571
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800572 /* TODO Probably put random address here to be presented to the
573 * world but keep using the physical-dev address for the outgoing
574 * packets.
575 */
576 memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
577
578 dev->priv_flags |= IFF_IPVLAN_SLAVE;
579
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800580 err = register_netdevice(dev);
581 if (err < 0)
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800582 goto remove_ida;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800583
584 err = netdev_upper_dev_link(phy_dev, dev);
Mahesh Bandewar494e8482016-04-27 14:59:27 -0700585 if (err) {
Gao Feng147fd282016-11-24 23:39:59 +0800586 goto unregister_netdev;
Mahesh Bandewar494e8482016-04-27 14:59:27 -0700587 }
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700588 err = ipvlan_set_port_mode(port, mode);
589 if (err) {
Gao Feng1a31cc82016-12-08 11:16:58 +0800590 goto unlink_netdev;
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700591 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800592
593 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
594 netif_stacked_transfer_operstate(phy_dev, dev);
595 return 0;
Gao Feng147fd282016-11-24 23:39:59 +0800596
Gao Feng1a31cc82016-12-08 11:16:58 +0800597unlink_netdev:
598 netdev_upper_dev_unlink(phy_dev, dev);
Gao Feng147fd282016-11-24 23:39:59 +0800599unregister_netdev:
600 unregister_netdevice(dev);
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800601remove_ida:
602 ida_simple_remove(&port->ida, dev->dev_id);
Gao Feng147fd282016-11-24 23:39:59 +0800603destroy_ipvlan_port:
604 if (create)
605 ipvlan_port_destroy(phy_dev);
606 return err;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800607}
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800608EXPORT_SYMBOL_GPL(ipvlan_link_new);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800609
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800610void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800611{
612 struct ipvl_dev *ipvlan = netdev_priv(dev);
613 struct ipvl_addr *addr, *next;
614
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300615 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300616 ipvlan_ht_addr_del(addr);
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300617 list_del(&addr->anode);
Konstantin Khlebnikov6a725492015-07-14 16:35:51 +0300618 kfree_rcu(addr, rcu);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800619 }
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300620
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800621 ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800622 list_del_rcu(&ipvlan->pnode);
623 unregister_netdevice_queue(dev, head);
624 netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
625}
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800626EXPORT_SYMBOL_GPL(ipvlan_link_delete);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800627
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800628void ipvlan_link_setup(struct net_device *dev)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800629{
630 ether_setup(dev);
631
632 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
Phil Sutterbf485bc2015-08-18 10:30:40 +0200633 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800634 dev->netdev_ops = &ipvlan_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400635 dev->needs_free_netdev = true;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800636 dev->header_ops = &ipvlan_header_ops;
637 dev->ethtool_ops = &ipvlan_ethtool_ops;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800638}
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800639EXPORT_SYMBOL_GPL(ipvlan_link_setup);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800640
641static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
642{
643 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
644};
645
646static struct rtnl_link_ops ipvlan_link_ops = {
647 .kind = "ipvlan",
648 .priv_size = sizeof(struct ipvl_dev),
649
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800650 .setup = ipvlan_link_setup,
651 .newlink = ipvlan_link_new,
652 .dellink = ipvlan_link_delete,
653};
654
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800655int ipvlan_link_register(struct rtnl_link_ops *ops)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800656{
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800657 ops->get_size = ipvlan_nl_getsize;
658 ops->policy = ipvlan_nl_policy;
659 ops->validate = ipvlan_nl_validate;
660 ops->fill_info = ipvlan_nl_fillinfo;
661 ops->changelink = ipvlan_nl_changelink;
662 ops->maxtype = IFLA_IPVLAN_MAX;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800663 return rtnl_link_register(ops);
664}
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800665EXPORT_SYMBOL_GPL(ipvlan_link_register);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800666
667static int ipvlan_device_event(struct notifier_block *unused,
668 unsigned long event, void *ptr)
669{
670 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
671 struct ipvl_dev *ipvlan, *next;
672 struct ipvl_port *port;
673 LIST_HEAD(lst_kill);
674
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800675 if (!netif_is_ipvlan_port(dev))
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800676 return NOTIFY_DONE;
677
678 port = ipvlan_port_get_rtnl(dev);
679
680 switch (event) {
681 case NETDEV_CHANGE:
682 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
683 netif_stacked_transfer_operstate(ipvlan->phy_dev,
684 ipvlan->dev);
685 break;
686
Florian Westphal31338222017-04-20 18:08:15 +0200687 case NETDEV_REGISTER: {
688 struct net *oldnet, *newnet = dev_net(dev);
689 struct ipvlan_netns *old_vnet;
690
691 oldnet = read_pnet(&port->pnet);
692 if (net_eq(newnet, oldnet))
693 break;
694
695 write_pnet(&port->pnet, newnet);
696
697 old_vnet = net_generic(oldnet, ipvlan_netid);
698 if (!old_vnet->ipvl_nf_hook_refcnt)
699 break;
700
701 ipvlan_register_nf_hook(newnet);
702 ipvlan_unregister_nf_hook(oldnet);
703 break;
704 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800705 case NETDEV_UNREGISTER:
706 if (dev->reg_state != NETREG_UNREGISTERING)
707 break;
708
709 list_for_each_entry_safe(ipvlan, next, &port->ipvlans,
710 pnode)
711 ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
712 &lst_kill);
713 unregister_netdevice_many(&lst_kill);
714 break;
715
716 case NETDEV_FEAT_CHANGE:
717 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
718 ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
719 ipvlan->dev->gso_max_size = dev->gso_max_size;
Eric Dumazetf6773c52016-03-16 21:59:49 -0700720 ipvlan->dev->gso_max_segs = dev->gso_max_segs;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800721 netdev_features_change(ipvlan->dev);
722 }
723 break;
724
725 case NETDEV_CHANGEMTU:
726 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
727 ipvlan_adjust_mtu(ipvlan, dev);
728 break;
729
730 case NETDEV_PRE_TYPE_CHANGE:
731 /* Forbid underlying device to change its type. */
732 return NOTIFY_BAD;
733 }
734 return NOTIFY_DONE;
735}
736
Gao Feng86673982016-12-28 16:46:51 +0800737static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800738{
739 struct ipvl_addr *addr;
740
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800741 addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
742 if (!addr)
743 return -ENOMEM;
744
745 addr->master = ipvlan;
Gao Feng86673982016-12-28 16:46:51 +0800746 if (is_v6) {
747 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
748 addr->atype = IPVL_IPV6;
749 } else {
750 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
751 addr->atype = IPVL_IPV4;
752 }
Jiri Benc40891e82015-03-28 19:13:24 +0100753 list_add_tail(&addr->anode, &ipvlan->addrs);
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300754
Jiri Benc27705f72015-03-28 19:13:22 +0100755 /* If the interface is not up, the address will be added to the hash
756 * list by ipvlan_open.
757 */
758 if (netif_running(ipvlan->dev))
759 ipvlan_ht_addr_add(ipvlan, addr);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800760
761 return 0;
762}
763
Gao Feng86673982016-12-28 16:46:51 +0800764static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800765{
766 struct ipvl_addr *addr;
767
Gao Feng86673982016-12-28 16:46:51 +0800768 addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800769 if (!addr)
770 return;
771
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300772 ipvlan_ht_addr_del(addr);
Jiri Benc40891e82015-03-28 19:13:24 +0100773 list_del(&addr->anode);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800774 kfree_rcu(addr, rcu);
775
776 return;
777}
778
Gao Feng86673982016-12-28 16:46:51 +0800779static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
780{
781 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
782 netif_err(ipvlan, ifup, ipvlan->dev,
783 "Failed to add IPv6=%pI6c addr for %s intf\n",
784 ip6_addr, ipvlan->dev->name);
785 return -EINVAL;
786 }
787
788 return ipvlan_add_addr(ipvlan, ip6_addr, true);
789}
790
791static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
792{
793 return ipvlan_del_addr(ipvlan, ip6_addr, true);
794}
795
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800796static int ipvlan_addr6_event(struct notifier_block *unused,
797 unsigned long event, void *ptr)
798{
799 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
800 struct net_device *dev = (struct net_device *)if6->idev->dev;
801 struct ipvl_dev *ipvlan = netdev_priv(dev);
802
Konstantin Khlebnikov23a5a492015-07-14 16:35:55 +0300803 /* FIXME IPv6 autoconf calls us from bh without RTNL */
804 if (in_softirq())
805 return NOTIFY_DONE;
806
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800807 if (!netif_is_ipvlan(dev))
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800808 return NOTIFY_DONE;
809
810 if (!ipvlan || !ipvlan->port)
811 return NOTIFY_DONE;
812
813 switch (event) {
814 case NETDEV_UP:
815 if (ipvlan_add_addr6(ipvlan, &if6->addr))
816 return NOTIFY_BAD;
817 break;
818
819 case NETDEV_DOWN:
820 ipvlan_del_addr6(ipvlan, &if6->addr);
821 break;
822 }
823
824 return NOTIFY_OK;
825}
826
827static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
828{
Jiri Bence9997c22015-03-28 19:13:25 +0100829 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800830 netif_err(ipvlan, ifup, ipvlan->dev,
831 "Failed to add IPv4=%pI4 on %s intf.\n",
832 ip4_addr, ipvlan->dev->name);
833 return -EINVAL;
834 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800835
Gao Feng86673982016-12-28 16:46:51 +0800836 return ipvlan_add_addr(ipvlan, ip4_addr, false);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800837}
838
839static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
840{
Gao Feng86673982016-12-28 16:46:51 +0800841 return ipvlan_del_addr(ipvlan, ip4_addr, false);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800842}
843
844static int ipvlan_addr4_event(struct notifier_block *unused,
845 unsigned long event, void *ptr)
846{
847 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
848 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
849 struct ipvl_dev *ipvlan = netdev_priv(dev);
850 struct in_addr ip4_addr;
851
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800852 if (!netif_is_ipvlan(dev))
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800853 return NOTIFY_DONE;
854
855 if (!ipvlan || !ipvlan->port)
856 return NOTIFY_DONE;
857
858 switch (event) {
859 case NETDEV_UP:
860 ip4_addr.s_addr = if4->ifa_address;
861 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
862 return NOTIFY_BAD;
863 break;
864
865 case NETDEV_DOWN:
866 ip4_addr.s_addr = if4->ifa_address;
867 ipvlan_del_addr4(ipvlan, &ip4_addr);
868 break;
869 }
870
871 return NOTIFY_OK;
872}
873
874static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
875 .notifier_call = ipvlan_addr4_event,
876};
877
878static struct notifier_block ipvlan_notifier_block __read_mostly = {
879 .notifier_call = ipvlan_device_event,
880};
881
882static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
883 .notifier_call = ipvlan_addr6_event,
884};
885
Florian Westphal31338222017-04-20 18:08:15 +0200886static void ipvlan_ns_exit(struct net *net)
887{
888 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
889
890 if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
891 vnet->ipvl_nf_hook_refcnt = 0;
892 nf_unregister_net_hooks(net, ipvl_nfops,
893 ARRAY_SIZE(ipvl_nfops));
894 }
895}
896
897static struct pernet_operations ipvlan_net_ops = {
898 .id = &ipvlan_netid,
899 .size = sizeof(struct ipvlan_netns),
900 .exit = ipvlan_ns_exit,
901};
902
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800903static int __init ipvlan_init_module(void)
904{
905 int err;
906
907 ipvlan_init_secret();
908 register_netdevice_notifier(&ipvlan_notifier_block);
909 register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
910 register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
911
Florian Westphal31338222017-04-20 18:08:15 +0200912 err = register_pernet_subsys(&ipvlan_net_ops);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800913 if (err < 0)
914 goto error;
915
Florian Westphal31338222017-04-20 18:08:15 +0200916 err = ipvlan_link_register(&ipvlan_link_ops);
917 if (err < 0) {
918 unregister_pernet_subsys(&ipvlan_net_ops);
919 goto error;
920 }
921
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800922 return 0;
923error:
924 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
925 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
926 unregister_netdevice_notifier(&ipvlan_notifier_block);
927 return err;
928}
929
930static void __exit ipvlan_cleanup_module(void)
931{
932 rtnl_link_unregister(&ipvlan_link_ops);
Florian Westphal31338222017-04-20 18:08:15 +0200933 unregister_pernet_subsys(&ipvlan_net_ops);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800934 unregister_netdevice_notifier(&ipvlan_notifier_block);
935 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
936 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
937}
938
939module_init(ipvlan_init_module);
940module_exit(ipvlan_cleanup_module);
941
942MODULE_LICENSE("GPL");
943MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
944MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
945MODULE_ALIAS_RTNL_LINK("ipvlan");