]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/ipvlan/ipvlan_main.c
Merge tag 'for-linus-20170825' of git://git.infradead.org/linux-mtd
[mirror_ubuntu-artful-kernel.git] / drivers / net / ipvlan / ipvlan_main.c
CommitLineData
2ad7bf36
MB
1/* 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
3133822f
FW
12static unsigned int ipvlan_netid __read_mostly;
13
14struct ipvlan_netns {
15 unsigned int ipvl_nf_hook_refcnt;
16};
4fbae7d8
MB
17
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
ab530f63 33static const struct l3mdev_ops ipvl_l3mdev_ops = {
4fbae7d8
MB
34 .l3mdev_l3_rcv = ipvlan_l3_rcv,
35};
36
ab5b7013 37static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
2ad7bf36 38{
8f679ed8 39 ipvlan->dev->mtu = dev->mtu;
2ad7bf36
MB
40}
41
3133822f 42static int ipvlan_register_nf_hook(struct net *net)
4fbae7d8 43{
3133822f 44 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
4fbae7d8
MB
45 int err = 0;
46
3133822f
FW
47 if (!vnet->ipvl_nf_hook_refcnt) {
48 err = nf_register_net_hooks(net, ipvl_nfops,
49 ARRAY_SIZE(ipvl_nfops));
4fbae7d8 50 if (!err)
3133822f 51 vnet->ipvl_nf_hook_refcnt = 1;
4fbae7d8 52 } else {
3133822f 53 vnet->ipvl_nf_hook_refcnt++;
4fbae7d8
MB
54 }
55
56 return err;
57}
58
3133822f 59static void ipvlan_unregister_nf_hook(struct net *net)
4fbae7d8 60{
3133822f
FW
61 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
62
63 if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
64 return;
4fbae7d8 65
3133822f
FW
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));
4fbae7d8
MB
70}
71
72static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
2ad7bf36
MB
73{
74 struct ipvl_dev *ipvlan;
4fbae7d8
MB
75 struct net_device *mdev = port->dev;
76 int err = 0;
2ad7bf36 77
4fbae7d8 78 ASSERT_RTNL();
2ad7bf36 79 if (port->mode != nval) {
4fbae7d8
MB
80 if (nval == IPVLAN_MODE_L3S) {
81 /* New mode is L3S */
3133822f 82 err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
4fbae7d8
MB
83 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;
3133822f 91 ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
4fbae7d8
MB
92 mdev->l3mdev_ops = NULL;
93 }
2ad7bf36 94 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
4fbae7d8 95 if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S)
2ad7bf36
MB
96 ipvlan->dev->flags |= IFF_NOARP;
97 else
98 ipvlan->dev->flags &= ~IFF_NOARP;
99 }
100 port->mode = nval;
101 }
4fbae7d8 102 return err;
2ad7bf36
MB
103}
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 }
764e433b 114
c3262d9d
MB
115 if (netdev_is_rx_handler_busy(dev)) {
116 netdev_err(dev, "Device is already in use.\n");
764e433b
MB
117 return -EBUSY;
118 }
119
2ad7bf36
MB
120 port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
121 if (!port)
122 return -ENOMEM;
123
3133822f 124 write_pnet(&port->pnet, dev_net(dev));
2ad7bf36
MB
125 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
ba35f858
MB
131 skb_queue_head_init(&port->backlog);
132 INIT_WORK(&port->wq, ipvlan_process_multicast);
009146d1 133 ida_init(&port->ida);
da36e13c 134 port->dev_id_start = 1;
ba35f858 135
2ad7bf36
MB
136 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:
48140a21 144 kfree(port);
2ad7bf36
MB
145 return err;
146}
147
148static void ipvlan_port_destroy(struct net_device *dev)
149{
150 struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
b1227d01 151 struct sk_buff *skb;
2ad7bf36
MB
152
153 dev->priv_flags &= ~IFF_IPVLAN_MASTER;
4fbae7d8
MB
154 if (port->mode == IPVLAN_MODE_L3S) {
155 dev->priv_flags &= ~IFF_L3MDEV_MASTER;
3133822f 156 ipvlan_unregister_nf_hook(dev_net(dev));
4fbae7d8
MB
157 dev->l3mdev_ops = NULL;
158 }
2ad7bf36 159 netdev_rx_handler_unregister(dev);
ba35f858 160 cancel_work_sync(&port->wq);
b1227d01
ED
161 while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
162 if (skb->dev)
163 dev_put(skb->dev);
164 kfree_skb(skb);
165 }
009146d1 166 ida_destroy(&port->ida);
48140a21 167 kfree(port);
2ad7bf36
MB
168}
169
2ad7bf36 170#define IPVLAN_FEATURES \
a188222b 171 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
2ad7bf36
MB
172 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
2ad7bf36
MB
179static 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;
494e8489 183 struct ipvl_port *port = ipvlan->port;
2ad7bf36
MB
184
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;
f6773c5e 190 dev->gso_max_segs = phy_dev->gso_max_segs;
2ad7bf36
MB
191 dev->hard_header_len = phy_dev->hard_header_len;
192
0d7dd798 193 netdev_lockdep_set_classes(dev);
2ad7bf36 194
87173cd6 195 ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
2ad7bf36
MB
196 if (!ipvlan->pcpu_stats)
197 return -ENOMEM;
198
494e8489
MB
199 port->count += 1;
200
2ad7bf36
MB
201 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
04901cea 209 free_percpu(ipvlan->pcpu_stats);
2ad7bf36
MB
210
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
4fbae7d8
MB
222 if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
223 ipvlan->port->mode == IPVLAN_MODE_L3S)
2ad7bf36
MB
224 dev->flags |= IFF_NOARP;
225 else
226 dev->flags &= ~IFF_NOARP;
227
515866f8
KK
228 list_for_each_entry(addr, &ipvlan->addrs, anode)
229 ipvlan_ht_addr_add(ipvlan, addr);
230
2ad7bf36
MB
231 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
515866f8 245 list_for_each_entry(addr, &ipvlan->addrs, anode)
6640e673 246 ipvlan_ht_addr_del(addr);
515866f8 247
2ad7bf36
MB
248 return 0;
249}
250
92c7b0de
MB
251static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
252 struct net_device *dev)
2ad7bf36
MB
253{
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
2ad7bf36
MB
291static 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
f631c44b
MB
305 /* 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
2ad7bf36
MB
311 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
bc1f4470 318static void ipvlan_get_stats64(struct net_device *dev,
319 struct rtnl_link_stats64 *s)
2ad7bf36
MB
320{
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 }
2ad7bf36
MB
356}
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
7c411658
ND
376static 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
2ad7bf36
MB
383static 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,
7c411658 395 .ndo_get_iflink = ipvlan_get_iflink,
2ad7bf36
MB
396};
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,
2ad7bf36
MB
415 .parse = eth_header_parse,
416 .cache = eth_header_cache,
417 .cache_update = eth_header_cache_update,
418};
419
314d10d7
DD
420static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
421 struct ethtool_link_ksettings *cmd)
2ad7bf36
MB
422{
423 const struct ipvl_dev *ipvlan = netdev_priv(dev);
424
314d10d7 425 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
2ad7bf36
MB
426}
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,
314d10d7 451 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
2ad7bf36
MB
452 .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,
ad744b22
MS
458 struct nlattr *tb[], struct nlattr *data[],
459 struct netlink_ext_ack *extack)
2ad7bf36
MB
460{
461 struct ipvl_dev *ipvlan = netdev_priv(dev);
462 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
4fbae7d8 463 int err = 0;
2ad7bf36
MB
464
465 if (data && data[IFLA_IPVLAN_MODE]) {
466 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
467
4fbae7d8 468 err = ipvlan_set_port_mode(port, nmode);
2ad7bf36 469 }
4fbae7d8 470 return err;
2ad7bf36
MB
471}
472
473static size_t ipvlan_nl_getsize(const struct net_device *dev)
474{
475 return (0
476 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
477 );
478}
479
a8b8a889
MS
480static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
481 struct netlink_ext_ack *extack)
2ad7bf36
MB
482{
483 if (data && data[IFLA_IPVLAN_MODE]) {
484 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
485
486 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
487 return -EINVAL;
488 }
489 return 0;
490}
491
492static int ipvlan_nl_fillinfo(struct sk_buff *skb,
493 const struct net_device *dev)
494{
495 struct ipvl_dev *ipvlan = netdev_priv(dev);
496 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
497 int ret = -EINVAL;
498
499 if (!port)
500 goto err;
501
502 ret = -EMSGSIZE;
503 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
504 goto err;
505
506 return 0;
507
508err:
509 return ret;
510}
511
235a9d89 512int ipvlan_link_new(struct net *src_net, struct net_device *dev,
7a3f4a18
MS
513 struct nlattr *tb[], struct nlattr *data[],
514 struct netlink_ext_ack *extack)
2ad7bf36
MB
515{
516 struct ipvl_dev *ipvlan = netdev_priv(dev);
517 struct ipvl_port *port;
518 struct net_device *phy_dev;
519 int err;
e93fbc5a 520 u16 mode = IPVLAN_MODE_L3;
147fd287 521 bool create = false;
2ad7bf36
MB
522
523 if (!tb[IFLA_LINK])
524 return -EINVAL;
525
526 phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
527 if (!phy_dev)
528 return -ENODEV;
529
5933fea7 530 if (netif_is_ipvlan(phy_dev)) {
2ad7bf36
MB
531 struct ipvl_dev *tmp = netdev_priv(phy_dev);
532
533 phy_dev = tmp->phy_dev;
5933fea7 534 } else if (!netif_is_ipvlan_port(phy_dev)) {
2ad7bf36
MB
535 err = ipvlan_port_create(phy_dev);
536 if (err < 0)
537 return err;
147fd287 538 create = true;
2ad7bf36
MB
539 }
540
2ad7bf36 541 if (data && data[IFLA_IPVLAN_MODE])
e93fbc5a 542 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
2ad7bf36 543
e93fbc5a 544 port = ipvlan_port_get_rtnl(phy_dev);
2ad7bf36
MB
545 ipvlan->phy_dev = phy_dev;
546 ipvlan->dev = dev;
547 ipvlan->port = port;
548 ipvlan->sfeatures = IPVLAN_FEATURES;
296d4856 549 ipvlan_adjust_mtu(ipvlan, phy_dev);
2ad7bf36 550 INIT_LIST_HEAD(&ipvlan->addrs);
2ad7bf36 551
da36e13c
MB
552 /* If the port-id base is at the MAX value, then wrap it around and
553 * begin from 0x1 again. This may be due to a busy system where lots
554 * of slaves are getting created and deleted.
555 */
556 if (port->dev_id_start == 0xFFFE)
557 port->dev_id_start = 0x1;
558
009146d1
MB
559 /* Since L2 address is shared among all IPvlan slaves including
560 * master, use unique 16 bit dev-ids to diffentiate among them.
561 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
562 * slave link [see addrconf_ifid_eui48()].
563 */
da36e13c
MB
564 err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
565 GFP_KERNEL);
019ec003
MB
566 if (err < 0)
567 err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
568 GFP_KERNEL);
009146d1
MB
569 if (err < 0)
570 goto destroy_ipvlan_port;
571 dev->dev_id = err;
da36e13c
MB
572 /* Increment id-base to the next slot for the future assignment */
573 port->dev_id_start = err + 1;
009146d1 574
2ad7bf36
MB
575 /* TODO Probably put random address here to be presented to the
576 * world but keep using the physical-dev address for the outgoing
577 * packets.
578 */
579 memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
580
581 dev->priv_flags |= IFF_IPVLAN_SLAVE;
582
2ad7bf36
MB
583 err = register_netdevice(dev);
584 if (err < 0)
009146d1 585 goto remove_ida;
2ad7bf36
MB
586
587 err = netdev_upper_dev_link(phy_dev, dev);
494e8489 588 if (err) {
147fd287 589 goto unregister_netdev;
494e8489 590 }
4fbae7d8
MB
591 err = ipvlan_set_port_mode(port, mode);
592 if (err) {
1a31cc86 593 goto unlink_netdev;
4fbae7d8 594 }
2ad7bf36
MB
595
596 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
597 netif_stacked_transfer_operstate(phy_dev, dev);
598 return 0;
147fd287 599
1a31cc86
GF
600unlink_netdev:
601 netdev_upper_dev_unlink(phy_dev, dev);
147fd287
GF
602unregister_netdev:
603 unregister_netdevice(dev);
009146d1
MB
604remove_ida:
605 ida_simple_remove(&port->ida, dev->dev_id);
147fd287
GF
606destroy_ipvlan_port:
607 if (create)
608 ipvlan_port_destroy(phy_dev);
609 return err;
2ad7bf36 610}
235a9d89 611EXPORT_SYMBOL_GPL(ipvlan_link_new);
2ad7bf36 612
235a9d89 613void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
2ad7bf36
MB
614{
615 struct ipvl_dev *ipvlan = netdev_priv(dev);
616 struct ipvl_addr *addr, *next;
617
515866f8 618 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
6640e673 619 ipvlan_ht_addr_del(addr);
515866f8 620 list_del(&addr->anode);
6a725497 621 kfree_rcu(addr, rcu);
2ad7bf36 622 }
515866f8 623
009146d1 624 ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
2ad7bf36
MB
625 list_del_rcu(&ipvlan->pnode);
626 unregister_netdevice_queue(dev, head);
627 netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
628}
235a9d89 629EXPORT_SYMBOL_GPL(ipvlan_link_delete);
2ad7bf36 630
235a9d89 631void ipvlan_link_setup(struct net_device *dev)
2ad7bf36
MB
632{
633 ether_setup(dev);
634
635 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
bf485bcf 636 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
2ad7bf36 637 dev->netdev_ops = &ipvlan_netdev_ops;
cf124db5 638 dev->needs_free_netdev = true;
2ad7bf36
MB
639 dev->header_ops = &ipvlan_header_ops;
640 dev->ethtool_ops = &ipvlan_ethtool_ops;
2ad7bf36 641}
235a9d89 642EXPORT_SYMBOL_GPL(ipvlan_link_setup);
2ad7bf36
MB
643
644static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
645{
646 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
647};
648
649static struct rtnl_link_ops ipvlan_link_ops = {
650 .kind = "ipvlan",
651 .priv_size = sizeof(struct ipvl_dev),
652
2ad7bf36
MB
653 .setup = ipvlan_link_setup,
654 .newlink = ipvlan_link_new,
655 .dellink = ipvlan_link_delete,
656};
657
235a9d89 658int ipvlan_link_register(struct rtnl_link_ops *ops)
2ad7bf36 659{
235a9d89
SG
660 ops->get_size = ipvlan_nl_getsize;
661 ops->policy = ipvlan_nl_policy;
662 ops->validate = ipvlan_nl_validate;
663 ops->fill_info = ipvlan_nl_fillinfo;
664 ops->changelink = ipvlan_nl_changelink;
665 ops->maxtype = IFLA_IPVLAN_MAX;
2ad7bf36
MB
666 return rtnl_link_register(ops);
667}
235a9d89 668EXPORT_SYMBOL_GPL(ipvlan_link_register);
2ad7bf36
MB
669
670static int ipvlan_device_event(struct notifier_block *unused,
671 unsigned long event, void *ptr)
672{
673 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
674 struct ipvl_dev *ipvlan, *next;
675 struct ipvl_port *port;
676 LIST_HEAD(lst_kill);
677
5933fea7 678 if (!netif_is_ipvlan_port(dev))
2ad7bf36
MB
679 return NOTIFY_DONE;
680
681 port = ipvlan_port_get_rtnl(dev);
682
683 switch (event) {
684 case NETDEV_CHANGE:
685 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
686 netif_stacked_transfer_operstate(ipvlan->phy_dev,
687 ipvlan->dev);
688 break;
689
3133822f
FW
690 case NETDEV_REGISTER: {
691 struct net *oldnet, *newnet = dev_net(dev);
692 struct ipvlan_netns *old_vnet;
693
694 oldnet = read_pnet(&port->pnet);
695 if (net_eq(newnet, oldnet))
696 break;
697
698 write_pnet(&port->pnet, newnet);
699
700 old_vnet = net_generic(oldnet, ipvlan_netid);
701 if (!old_vnet->ipvl_nf_hook_refcnt)
702 break;
703
704 ipvlan_register_nf_hook(newnet);
705 ipvlan_unregister_nf_hook(oldnet);
706 break;
707 }
2ad7bf36
MB
708 case NETDEV_UNREGISTER:
709 if (dev->reg_state != NETREG_UNREGISTERING)
710 break;
711
712 list_for_each_entry_safe(ipvlan, next, &port->ipvlans,
713 pnode)
714 ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
715 &lst_kill);
716 unregister_netdevice_many(&lst_kill);
717 break;
718
719 case NETDEV_FEAT_CHANGE:
720 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
721 ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
722 ipvlan->dev->gso_max_size = dev->gso_max_size;
f6773c5e 723 ipvlan->dev->gso_max_segs = dev->gso_max_segs;
2ad7bf36
MB
724 netdev_features_change(ipvlan->dev);
725 }
726 break;
727
728 case NETDEV_CHANGEMTU:
729 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
730 ipvlan_adjust_mtu(ipvlan, dev);
731 break;
732
733 case NETDEV_PRE_TYPE_CHANGE:
734 /* Forbid underlying device to change its type. */
735 return NOTIFY_BAD;
736 }
737 return NOTIFY_DONE;
738}
739
86673982 740static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
2ad7bf36
MB
741{
742 struct ipvl_addr *addr;
743
2ad7bf36
MB
744 addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
745 if (!addr)
746 return -ENOMEM;
747
748 addr->master = ipvlan;
86673982
GF
749 if (is_v6) {
750 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
751 addr->atype = IPVL_IPV6;
752 } else {
753 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
754 addr->atype = IPVL_IPV4;
755 }
40891e8a 756 list_add_tail(&addr->anode, &ipvlan->addrs);
515866f8 757
27705f70
JB
758 /* If the interface is not up, the address will be added to the hash
759 * list by ipvlan_open.
760 */
761 if (netif_running(ipvlan->dev))
762 ipvlan_ht_addr_add(ipvlan, addr);
2ad7bf36
MB
763
764 return 0;
765}
766
86673982 767static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
2ad7bf36
MB
768{
769 struct ipvl_addr *addr;
770
86673982 771 addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
2ad7bf36
MB
772 if (!addr)
773 return;
774
6640e673 775 ipvlan_ht_addr_del(addr);
40891e8a 776 list_del(&addr->anode);
2ad7bf36
MB
777 kfree_rcu(addr, rcu);
778
779 return;
780}
781
86673982
GF
782static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
783{
784 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
785 netif_err(ipvlan, ifup, ipvlan->dev,
786 "Failed to add IPv6=%pI6c addr for %s intf\n",
787 ip6_addr, ipvlan->dev->name);
788 return -EINVAL;
789 }
790
791 return ipvlan_add_addr(ipvlan, ip6_addr, true);
792}
793
794static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
795{
796 return ipvlan_del_addr(ipvlan, ip6_addr, true);
797}
798
2ad7bf36
MB
799static int ipvlan_addr6_event(struct notifier_block *unused,
800 unsigned long event, void *ptr)
801{
802 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
803 struct net_device *dev = (struct net_device *)if6->idev->dev;
804 struct ipvl_dev *ipvlan = netdev_priv(dev);
805
23a5a49c
KK
806 /* FIXME IPv6 autoconf calls us from bh without RTNL */
807 if (in_softirq())
808 return NOTIFY_DONE;
809
5933fea7 810 if (!netif_is_ipvlan(dev))
2ad7bf36
MB
811 return NOTIFY_DONE;
812
813 if (!ipvlan || !ipvlan->port)
814 return NOTIFY_DONE;
815
816 switch (event) {
817 case NETDEV_UP:
818 if (ipvlan_add_addr6(ipvlan, &if6->addr))
819 return NOTIFY_BAD;
820 break;
821
822 case NETDEV_DOWN:
823 ipvlan_del_addr6(ipvlan, &if6->addr);
824 break;
825 }
826
827 return NOTIFY_OK;
828}
829
3ad7d246
KJ
830static int ipvlan_addr6_validator_event(struct notifier_block *unused,
831 unsigned long event, void *ptr)
832{
833 struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
834 struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
835 struct ipvl_dev *ipvlan = netdev_priv(dev);
836
837 /* FIXME IPv6 autoconf calls us from bh without RTNL */
838 if (in_softirq())
839 return NOTIFY_DONE;
840
841 if (!netif_is_ipvlan(dev))
842 return NOTIFY_DONE;
843
844 if (!ipvlan || !ipvlan->port)
845 return NOTIFY_DONE;
846
847 switch (event) {
848 case NETDEV_UP:
849 if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true))
850 return notifier_from_errno(-EADDRINUSE);
851 break;
852 }
853
854 return NOTIFY_OK;
855}
856
2ad7bf36
MB
857static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
858{
e9997c29 859 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
2ad7bf36
MB
860 netif_err(ipvlan, ifup, ipvlan->dev,
861 "Failed to add IPv4=%pI4 on %s intf.\n",
862 ip4_addr, ipvlan->dev->name);
863 return -EINVAL;
864 }
2ad7bf36 865
86673982 866 return ipvlan_add_addr(ipvlan, ip4_addr, false);
2ad7bf36
MB
867}
868
869static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
870{
86673982 871 return ipvlan_del_addr(ipvlan, ip4_addr, false);
2ad7bf36
MB
872}
873
874static int ipvlan_addr4_event(struct notifier_block *unused,
875 unsigned long event, void *ptr)
876{
877 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
878 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
879 struct ipvl_dev *ipvlan = netdev_priv(dev);
880 struct in_addr ip4_addr;
881
5933fea7 882 if (!netif_is_ipvlan(dev))
2ad7bf36
MB
883 return NOTIFY_DONE;
884
885 if (!ipvlan || !ipvlan->port)
886 return NOTIFY_DONE;
887
888 switch (event) {
889 case NETDEV_UP:
890 ip4_addr.s_addr = if4->ifa_address;
891 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
892 return NOTIFY_BAD;
893 break;
894
895 case NETDEV_DOWN:
896 ip4_addr.s_addr = if4->ifa_address;
897 ipvlan_del_addr4(ipvlan, &ip4_addr);
898 break;
899 }
900
901 return NOTIFY_OK;
902}
903
3ad7d246
KJ
904static int ipvlan_addr4_validator_event(struct notifier_block *unused,
905 unsigned long event, void *ptr)
906{
907 struct in_validator_info *ivi = (struct in_validator_info *)ptr;
908 struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
909 struct ipvl_dev *ipvlan = netdev_priv(dev);
910
911 if (!netif_is_ipvlan(dev))
912 return NOTIFY_DONE;
913
914 if (!ipvlan || !ipvlan->port)
915 return NOTIFY_DONE;
916
917 switch (event) {
918 case NETDEV_UP:
919 if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false))
920 return notifier_from_errno(-EADDRINUSE);
921 break;
922 }
923
924 return NOTIFY_OK;
925}
926
2ad7bf36
MB
927static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
928 .notifier_call = ipvlan_addr4_event,
929};
930
3ad7d246
KJ
931static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
932 .notifier_call = ipvlan_addr4_validator_event,
933};
934
2ad7bf36
MB
935static struct notifier_block ipvlan_notifier_block __read_mostly = {
936 .notifier_call = ipvlan_device_event,
937};
938
939static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
940 .notifier_call = ipvlan_addr6_event,
941};
942
3ad7d246
KJ
943static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
944 .notifier_call = ipvlan_addr6_validator_event,
945};
946
3133822f
FW
947static void ipvlan_ns_exit(struct net *net)
948{
949 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
950
951 if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
952 vnet->ipvl_nf_hook_refcnt = 0;
953 nf_unregister_net_hooks(net, ipvl_nfops,
954 ARRAY_SIZE(ipvl_nfops));
955 }
956}
957
958static struct pernet_operations ipvlan_net_ops = {
959 .id = &ipvlan_netid,
960 .size = sizeof(struct ipvlan_netns),
961 .exit = ipvlan_ns_exit,
962};
963
2ad7bf36
MB
964static int __init ipvlan_init_module(void)
965{
966 int err;
967
968 ipvlan_init_secret();
969 register_netdevice_notifier(&ipvlan_notifier_block);
970 register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
3ad7d246
KJ
971 register_inet6addr_validator_notifier(
972 &ipvlan_addr6_vtor_notifier_block);
2ad7bf36 973 register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
3ad7d246 974 register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
2ad7bf36 975
3133822f 976 err = register_pernet_subsys(&ipvlan_net_ops);
2ad7bf36
MB
977 if (err < 0)
978 goto error;
979
3133822f
FW
980 err = ipvlan_link_register(&ipvlan_link_ops);
981 if (err < 0) {
982 unregister_pernet_subsys(&ipvlan_net_ops);
983 goto error;
984 }
985
2ad7bf36
MB
986 return 0;
987error:
988 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
3ad7d246
KJ
989 unregister_inetaddr_validator_notifier(
990 &ipvlan_addr4_vtor_notifier_block);
2ad7bf36 991 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
3ad7d246
KJ
992 unregister_inet6addr_validator_notifier(
993 &ipvlan_addr6_vtor_notifier_block);
2ad7bf36
MB
994 unregister_netdevice_notifier(&ipvlan_notifier_block);
995 return err;
996}
997
998static void __exit ipvlan_cleanup_module(void)
999{
1000 rtnl_link_unregister(&ipvlan_link_ops);
3133822f 1001 unregister_pernet_subsys(&ipvlan_net_ops);
2ad7bf36
MB
1002 unregister_netdevice_notifier(&ipvlan_notifier_block);
1003 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
3ad7d246
KJ
1004 unregister_inetaddr_validator_notifier(
1005 &ipvlan_addr4_vtor_notifier_block);
2ad7bf36 1006 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
3ad7d246
KJ
1007 unregister_inet6addr_validator_notifier(
1008 &ipvlan_addr6_vtor_notifier_block);
2ad7bf36
MB
1009}
1010
1011module_init(ipvlan_init_module);
1012module_exit(ipvlan_cleanup_module);
1013
1014MODULE_LICENSE("GPL");
1015MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
1016MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
1017MODULE_ALIAS_RTNL_LINK("ipvlan");