blob: 5fbabae2909ee447bb71d44088c04e2cba153cf0 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003 */
4
5#include "ipvlan.h"
6
David Brazdil0f672f62019-12-10 10:32:29 +00007static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval,
8 struct netlink_ext_ack *extack)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009{
10 struct ipvl_dev *ipvlan;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011 unsigned int flags;
12 int err;
13
14 ASSERT_RTNL();
15 if (port->mode != nval) {
16 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
17 flags = ipvlan->dev->flags;
18 if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) {
19 err = dev_change_flags(ipvlan->dev,
David Brazdil0f672f62019-12-10 10:32:29 +000020 flags | IFF_NOARP,
21 extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022 } else {
23 err = dev_change_flags(ipvlan->dev,
David Brazdil0f672f62019-12-10 10:32:29 +000024 flags & ~IFF_NOARP,
25 extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026 }
27 if (unlikely(err))
28 goto fail;
29 }
30 if (nval == IPVLAN_MODE_L3S) {
31 /* New mode is L3S */
David Brazdil0f672f62019-12-10 10:32:29 +000032 err = ipvlan_l3s_register(port);
33 if (err)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034 goto fail;
35 } else if (port->mode == IPVLAN_MODE_L3S) {
36 /* Old mode was L3S */
David Brazdil0f672f62019-12-10 10:32:29 +000037 ipvlan_l3s_unregister(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038 }
39 port->mode = nval;
40 }
41 return 0;
42
43fail:
44 /* Undo the flags changes that have been done so far. */
45 list_for_each_entry_continue_reverse(ipvlan, &port->ipvlans, pnode) {
46 flags = ipvlan->dev->flags;
47 if (port->mode == IPVLAN_MODE_L3 ||
48 port->mode == IPVLAN_MODE_L3S)
David Brazdil0f672f62019-12-10 10:32:29 +000049 dev_change_flags(ipvlan->dev, flags | IFF_NOARP,
50 NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051 else
David Brazdil0f672f62019-12-10 10:32:29 +000052 dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP,
53 NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054 }
55
56 return err;
57}
58
59static int ipvlan_port_create(struct net_device *dev)
60{
61 struct ipvl_port *port;
62 int err, idx;
63
64 port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
65 if (!port)
66 return -ENOMEM;
67
68 write_pnet(&port->pnet, dev_net(dev));
69 port->dev = dev;
70 port->mode = IPVLAN_MODE_L3;
71 INIT_LIST_HEAD(&port->ipvlans);
72 for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
73 INIT_HLIST_HEAD(&port->hlhead[idx]);
74
75 skb_queue_head_init(&port->backlog);
76 INIT_WORK(&port->wq, ipvlan_process_multicast);
77 ida_init(&port->ida);
78 port->dev_id_start = 1;
79
80 err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
81 if (err)
82 goto err;
83
84 return 0;
85
86err:
87 kfree(port);
88 return err;
89}
90
91static void ipvlan_port_destroy(struct net_device *dev)
92{
93 struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
94 struct sk_buff *skb;
95
David Brazdil0f672f62019-12-10 10:32:29 +000096 if (port->mode == IPVLAN_MODE_L3S)
97 ipvlan_l3s_unregister(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000098 netdev_rx_handler_unregister(dev);
99 cancel_work_sync(&port->wq);
100 while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
101 if (skb->dev)
102 dev_put(skb->dev);
103 kfree_skb(skb);
104 }
105 ida_destroy(&port->ida);
106 kfree(port);
107}
108
Olivier Deprez0e641232021-09-23 10:07:05 +0200109#define IPVLAN_ALWAYS_ON_OFLOADS \
110 (NETIF_F_SG | NETIF_F_HW_CSUM | \
111 NETIF_F_GSO_ROBUST | NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL)
112
113#define IPVLAN_ALWAYS_ON \
114 (IPVLAN_ALWAYS_ON_OFLOADS | NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED)
115
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116#define IPVLAN_FEATURES \
Olivier Deprez0e641232021-09-23 10:07:05 +0200117 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \
119 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
120 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
121
Olivier Deprez0e641232021-09-23 10:07:05 +0200122 /* NETIF_F_GSO_ENCAP_ALL NETIF_F_GSO_SOFTWARE Newly added */
123
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000124#define IPVLAN_STATE_MASK \
125 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
126
127static int ipvlan_init(struct net_device *dev)
128{
129 struct ipvl_dev *ipvlan = netdev_priv(dev);
130 struct net_device *phy_dev = ipvlan->phy_dev;
131 struct ipvl_port *port;
132 int err;
133
134 dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
135 (phy_dev->state & IPVLAN_STATE_MASK);
136 dev->features = phy_dev->features & IPVLAN_FEATURES;
Olivier Deprez0e641232021-09-23 10:07:05 +0200137 dev->features |= IPVLAN_ALWAYS_ON;
138 dev->vlan_features = phy_dev->vlan_features & IPVLAN_FEATURES;
139 dev->vlan_features |= IPVLAN_ALWAYS_ON_OFLOADS;
David Brazdil0f672f62019-12-10 10:32:29 +0000140 dev->hw_enc_features |= dev->features;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141 dev->gso_max_size = phy_dev->gso_max_size;
142 dev->gso_max_segs = phy_dev->gso_max_segs;
143 dev->hard_header_len = phy_dev->hard_header_len;
144
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000145 ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
146 if (!ipvlan->pcpu_stats)
147 return -ENOMEM;
148
149 if (!netif_is_ipvlan_port(phy_dev)) {
150 err = ipvlan_port_create(phy_dev);
151 if (err < 0) {
152 free_percpu(ipvlan->pcpu_stats);
153 return err;
154 }
155 }
156 port = ipvlan_port_get_rtnl(phy_dev);
157 port->count += 1;
158 return 0;
159}
160
161static void ipvlan_uninit(struct net_device *dev)
162{
163 struct ipvl_dev *ipvlan = netdev_priv(dev);
164 struct net_device *phy_dev = ipvlan->phy_dev;
165 struct ipvl_port *port;
166
167 free_percpu(ipvlan->pcpu_stats);
168
169 port = ipvlan_port_get_rtnl(phy_dev);
170 port->count -= 1;
171 if (!port->count)
172 ipvlan_port_destroy(port->dev);
173}
174
175static int ipvlan_open(struct net_device *dev)
176{
177 struct ipvl_dev *ipvlan = netdev_priv(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178 struct ipvl_addr *addr;
179
180 if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
181 ipvlan->port->mode == IPVLAN_MODE_L3S)
182 dev->flags |= IFF_NOARP;
183 else
184 dev->flags &= ~IFF_NOARP;
185
186 rcu_read_lock();
187 list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
188 ipvlan_ht_addr_add(ipvlan, addr);
189 rcu_read_unlock();
190
Olivier Deprez0e641232021-09-23 10:07:05 +0200191 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000192}
193
194static int ipvlan_stop(struct net_device *dev)
195{
196 struct ipvl_dev *ipvlan = netdev_priv(dev);
197 struct net_device *phy_dev = ipvlan->phy_dev;
198 struct ipvl_addr *addr;
199
200 dev_uc_unsync(phy_dev, dev);
201 dev_mc_unsync(phy_dev, dev);
202
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000203 rcu_read_lock();
204 list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
205 ipvlan_ht_addr_del(addr);
206 rcu_read_unlock();
207
208 return 0;
209}
210
211static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
212 struct net_device *dev)
213{
214 const struct ipvl_dev *ipvlan = netdev_priv(dev);
215 int skblen = skb->len;
216 int ret;
217
218 ret = ipvlan_queue_xmit(skb, dev);
219 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
220 struct ipvl_pcpu_stats *pcptr;
221
222 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
223
224 u64_stats_update_begin(&pcptr->syncp);
225 pcptr->tx_pkts++;
226 pcptr->tx_bytes += skblen;
227 u64_stats_update_end(&pcptr->syncp);
228 } else {
229 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
230 }
231 return ret;
232}
233
234static netdev_features_t ipvlan_fix_features(struct net_device *dev,
235 netdev_features_t features)
236{
237 struct ipvl_dev *ipvlan = netdev_priv(dev);
238
Olivier Deprez0e641232021-09-23 10:07:05 +0200239 features |= NETIF_F_ALL_FOR_ALL;
240 features &= (ipvlan->sfeatures | ~IPVLAN_FEATURES);
241 features = netdev_increment_features(ipvlan->phy_dev->features,
242 features, features);
243 features |= IPVLAN_ALWAYS_ON;
244 features &= (IPVLAN_FEATURES | IPVLAN_ALWAYS_ON);
245
246 return features;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247}
248
249static void ipvlan_change_rx_flags(struct net_device *dev, int change)
250{
251 struct ipvl_dev *ipvlan = netdev_priv(dev);
252 struct net_device *phy_dev = ipvlan->phy_dev;
253
254 if (change & IFF_ALLMULTI)
255 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
256}
257
258static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
259{
260 struct ipvl_dev *ipvlan = netdev_priv(dev);
261
262 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
263 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
264 } else {
265 struct netdev_hw_addr *ha;
266 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
267
268 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
269 netdev_for_each_mc_addr(ha, dev)
270 __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
271
272 /* Turn-on broadcast bit irrespective of address family,
273 * since broadcast is deferred to a work-queue, hence no
274 * impact on fast-path processing.
275 */
276 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
277
278 bitmap_copy(ipvlan->mac_filters, mc_filters,
279 IPVLAN_MAC_FILTER_SIZE);
280 }
281 dev_uc_sync(ipvlan->phy_dev, dev);
282 dev_mc_sync(ipvlan->phy_dev, dev);
283}
284
285static void ipvlan_get_stats64(struct net_device *dev,
286 struct rtnl_link_stats64 *s)
287{
288 struct ipvl_dev *ipvlan = netdev_priv(dev);
289
290 if (ipvlan->pcpu_stats) {
291 struct ipvl_pcpu_stats *pcptr;
292 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
293 u32 rx_errs = 0, tx_drps = 0;
294 u32 strt;
295 int idx;
296
297 for_each_possible_cpu(idx) {
298 pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
299 do {
300 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
301 rx_pkts = pcptr->rx_pkts;
302 rx_bytes = pcptr->rx_bytes;
303 rx_mcast = pcptr->rx_mcast;
304 tx_pkts = pcptr->tx_pkts;
305 tx_bytes = pcptr->tx_bytes;
306 } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
307 strt));
308
309 s->rx_packets += rx_pkts;
310 s->rx_bytes += rx_bytes;
311 s->multicast += rx_mcast;
312 s->tx_packets += tx_pkts;
313 s->tx_bytes += tx_bytes;
314
315 /* u32 values are updated without syncp protection. */
316 rx_errs += pcptr->rx_errs;
317 tx_drps += pcptr->tx_drps;
318 }
319 s->rx_errors = rx_errs;
320 s->rx_dropped = rx_errs;
321 s->tx_dropped = tx_drps;
322 }
323}
324
325static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
326{
327 struct ipvl_dev *ipvlan = netdev_priv(dev);
328 struct net_device *phy_dev = ipvlan->phy_dev;
329
330 return vlan_vid_add(phy_dev, proto, vid);
331}
332
333static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
334 u16 vid)
335{
336 struct ipvl_dev *ipvlan = netdev_priv(dev);
337 struct net_device *phy_dev = ipvlan->phy_dev;
338
339 vlan_vid_del(phy_dev, proto, vid);
340 return 0;
341}
342
343static int ipvlan_get_iflink(const struct net_device *dev)
344{
345 struct ipvl_dev *ipvlan = netdev_priv(dev);
346
347 return ipvlan->phy_dev->ifindex;
348}
349
350static const struct net_device_ops ipvlan_netdev_ops = {
351 .ndo_init = ipvlan_init,
352 .ndo_uninit = ipvlan_uninit,
353 .ndo_open = ipvlan_open,
354 .ndo_stop = ipvlan_stop,
355 .ndo_start_xmit = ipvlan_start_xmit,
356 .ndo_fix_features = ipvlan_fix_features,
357 .ndo_change_rx_flags = ipvlan_change_rx_flags,
358 .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
359 .ndo_get_stats64 = ipvlan_get_stats64,
360 .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
361 .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
362 .ndo_get_iflink = ipvlan_get_iflink,
363};
364
365static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
366 unsigned short type, const void *daddr,
367 const void *saddr, unsigned len)
368{
369 const struct ipvl_dev *ipvlan = netdev_priv(dev);
370 struct net_device *phy_dev = ipvlan->phy_dev;
371
372 /* TODO Probably use a different field than dev_addr so that the
373 * mac-address on the virtual device is portable and can be carried
374 * while the packets use the mac-addr on the physical device.
375 */
376 return dev_hard_header(skb, phy_dev, type, daddr,
377 saddr ? : phy_dev->dev_addr, len);
378}
379
380static const struct header_ops ipvlan_header_ops = {
381 .create = ipvlan_hard_header,
382 .parse = eth_header_parse,
383 .cache = eth_header_cache,
384 .cache_update = eth_header_cache_update,
385};
386
David Brazdil0f672f62019-12-10 10:32:29 +0000387static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
388{
389 ipvlan->dev->mtu = dev->mtu;
390}
391
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000392static bool netif_is_ipvlan(const struct net_device *dev)
393{
394 /* both ipvlan and ipvtap devices use the same netdev_ops */
395 return dev->netdev_ops == &ipvlan_netdev_ops;
396}
397
398static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
399 struct ethtool_link_ksettings *cmd)
400{
401 const struct ipvl_dev *ipvlan = netdev_priv(dev);
402
403 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
404}
405
406static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
407 struct ethtool_drvinfo *drvinfo)
408{
409 strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
410 strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
411}
412
413static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
414{
415 const struct ipvl_dev *ipvlan = netdev_priv(dev);
416
417 return ipvlan->msg_enable;
418}
419
420static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
421{
422 struct ipvl_dev *ipvlan = netdev_priv(dev);
423
424 ipvlan->msg_enable = value;
425}
426
427static const struct ethtool_ops ipvlan_ethtool_ops = {
428 .get_link = ethtool_op_get_link,
429 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
430 .get_drvinfo = ipvlan_ethtool_get_drvinfo,
431 .get_msglevel = ipvlan_ethtool_get_msglevel,
432 .set_msglevel = ipvlan_ethtool_set_msglevel,
433};
434
435static int ipvlan_nl_changelink(struct net_device *dev,
436 struct nlattr *tb[], struct nlattr *data[],
437 struct netlink_ext_ack *extack)
438{
439 struct ipvl_dev *ipvlan = netdev_priv(dev);
440 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
441 int err = 0;
442
443 if (!data)
444 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000445 if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN))
446 return -EPERM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000447
448 if (data[IFLA_IPVLAN_MODE]) {
449 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
450
David Brazdil0f672f62019-12-10 10:32:29 +0000451 err = ipvlan_set_port_mode(port, nmode, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000452 }
453
454 if (!err && data[IFLA_IPVLAN_FLAGS]) {
455 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
456
457 if (flags & IPVLAN_F_PRIVATE)
458 ipvlan_mark_private(port);
459 else
460 ipvlan_clear_private(port);
461
462 if (flags & IPVLAN_F_VEPA)
463 ipvlan_mark_vepa(port);
464 else
465 ipvlan_clear_vepa(port);
466 }
467
468 return err;
469}
470
471static size_t ipvlan_nl_getsize(const struct net_device *dev)
472{
473 return (0
474 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
475 + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
476 );
477}
478
479static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
480 struct netlink_ext_ack *extack)
481{
482 if (!data)
483 return 0;
484
485 if (data[IFLA_IPVLAN_MODE]) {
486 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
487
David Brazdil0f672f62019-12-10 10:32:29 +0000488 if (mode >= IPVLAN_MODE_MAX)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000489 return -EINVAL;
490 }
491 if (data[IFLA_IPVLAN_FLAGS]) {
492 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
493
494 /* Only two bits are used at this moment. */
495 if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
496 return -EINVAL;
497 /* Also both flags can't be active at the same time. */
498 if ((flags & (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) ==
499 (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
500 return -EINVAL;
501 }
502
503 return 0;
504}
505
506static int ipvlan_nl_fillinfo(struct sk_buff *skb,
507 const struct net_device *dev)
508{
509 struct ipvl_dev *ipvlan = netdev_priv(dev);
510 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
511 int ret = -EINVAL;
512
513 if (!port)
514 goto err;
515
516 ret = -EMSGSIZE;
517 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
518 goto err;
519 if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags))
520 goto err;
521
522 return 0;
523
524err:
525 return ret;
526}
527
528int ipvlan_link_new(struct net *src_net, struct net_device *dev,
529 struct nlattr *tb[], struct nlattr *data[],
530 struct netlink_ext_ack *extack)
531{
532 struct ipvl_dev *ipvlan = netdev_priv(dev);
533 struct ipvl_port *port;
534 struct net_device *phy_dev;
535 int err;
536 u16 mode = IPVLAN_MODE_L3;
537
538 if (!tb[IFLA_LINK])
539 return -EINVAL;
540
541 phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
542 if (!phy_dev)
543 return -ENODEV;
544
545 if (netif_is_ipvlan(phy_dev)) {
546 struct ipvl_dev *tmp = netdev_priv(phy_dev);
547
548 phy_dev = tmp->phy_dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000549 if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN))
550 return -EPERM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000551 } else if (!netif_is_ipvlan_port(phy_dev)) {
552 /* Exit early if the underlying link is invalid or busy */
553 if (phy_dev->type != ARPHRD_ETHER ||
554 phy_dev->flags & IFF_LOOPBACK) {
555 netdev_err(phy_dev,
556 "Master is either lo or non-ether device\n");
557 return -EINVAL;
558 }
559
560 if (netdev_is_rx_handler_busy(phy_dev)) {
561 netdev_err(phy_dev, "Device is already in use.\n");
562 return -EBUSY;
563 }
564 }
565
566 ipvlan->phy_dev = phy_dev;
567 ipvlan->dev = dev;
568 ipvlan->sfeatures = IPVLAN_FEATURES;
569 if (!tb[IFLA_MTU])
570 ipvlan_adjust_mtu(ipvlan, phy_dev);
571 INIT_LIST_HEAD(&ipvlan->addrs);
572 spin_lock_init(&ipvlan->addrs_lock);
573
574 /* TODO Probably put random address here to be presented to the
575 * world but keep using the physical-dev address for the outgoing
576 * packets.
577 */
578 memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
579
580 dev->priv_flags |= IFF_NO_RX_HANDLER;
581
582 err = register_netdevice(dev);
583 if (err < 0)
584 return err;
585
586 /* ipvlan_init() would have created the port, if required */
587 port = ipvlan_port_get_rtnl(phy_dev);
588 ipvlan->port = port;
589
590 /* If the port-id base is at the MAX value, then wrap it around and
591 * begin from 0x1 again. This may be due to a busy system where lots
592 * of slaves are getting created and deleted.
593 */
594 if (port->dev_id_start == 0xFFFE)
595 port->dev_id_start = 0x1;
596
597 /* Since L2 address is shared among all IPvlan slaves including
598 * master, use unique 16 bit dev-ids to diffentiate among them.
599 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
600 * slave link [see addrconf_ifid_eui48()].
601 */
602 err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
603 GFP_KERNEL);
604 if (err < 0)
605 err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
606 GFP_KERNEL);
607 if (err < 0)
608 goto unregister_netdev;
609 dev->dev_id = err;
610
611 /* Increment id-base to the next slot for the future assignment */
612 port->dev_id_start = err + 1;
613
614 err = netdev_upper_dev_link(phy_dev, dev, extack);
615 if (err)
616 goto remove_ida;
617
618 /* Flags are per port and latest update overrides. User has
619 * to be consistent in setting it just like the mode attribute.
620 */
621 if (data && data[IFLA_IPVLAN_FLAGS])
622 port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
623
624 if (data && data[IFLA_IPVLAN_MODE])
625 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
626
David Brazdil0f672f62019-12-10 10:32:29 +0000627 err = ipvlan_set_port_mode(port, mode, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000628 if (err)
629 goto unlink_netdev;
630
631 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
632 netif_stacked_transfer_operstate(phy_dev, dev);
633 return 0;
634
635unlink_netdev:
636 netdev_upper_dev_unlink(phy_dev, dev);
637remove_ida:
638 ida_simple_remove(&port->ida, dev->dev_id);
639unregister_netdev:
640 unregister_netdevice(dev);
641 return err;
642}
643EXPORT_SYMBOL_GPL(ipvlan_link_new);
644
645void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
646{
647 struct ipvl_dev *ipvlan = netdev_priv(dev);
648 struct ipvl_addr *addr, *next;
649
650 spin_lock_bh(&ipvlan->addrs_lock);
651 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
652 ipvlan_ht_addr_del(addr);
653 list_del_rcu(&addr->anode);
654 kfree_rcu(addr, rcu);
655 }
656 spin_unlock_bh(&ipvlan->addrs_lock);
657
658 ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
659 list_del_rcu(&ipvlan->pnode);
660 unregister_netdevice_queue(dev, head);
661 netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
662}
663EXPORT_SYMBOL_GPL(ipvlan_link_delete);
664
665void ipvlan_link_setup(struct net_device *dev)
666{
667 ether_setup(dev);
668
669 dev->max_mtu = ETH_MAX_MTU;
670 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
671 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
672 dev->netdev_ops = &ipvlan_netdev_ops;
673 dev->needs_free_netdev = true;
674 dev->header_ops = &ipvlan_header_ops;
675 dev->ethtool_ops = &ipvlan_ethtool_ops;
676}
677EXPORT_SYMBOL_GPL(ipvlan_link_setup);
678
679static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
680{
681 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
682 [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 },
683};
684
685static struct rtnl_link_ops ipvlan_link_ops = {
686 .kind = "ipvlan",
687 .priv_size = sizeof(struct ipvl_dev),
688
689 .setup = ipvlan_link_setup,
690 .newlink = ipvlan_link_new,
691 .dellink = ipvlan_link_delete,
692};
693
694int ipvlan_link_register(struct rtnl_link_ops *ops)
695{
696 ops->get_size = ipvlan_nl_getsize;
697 ops->policy = ipvlan_nl_policy;
698 ops->validate = ipvlan_nl_validate;
699 ops->fill_info = ipvlan_nl_fillinfo;
700 ops->changelink = ipvlan_nl_changelink;
701 ops->maxtype = IFLA_IPVLAN_MAX;
702 return rtnl_link_register(ops);
703}
704EXPORT_SYMBOL_GPL(ipvlan_link_register);
705
706static int ipvlan_device_event(struct notifier_block *unused,
707 unsigned long event, void *ptr)
708{
David Brazdil0f672f62019-12-10 10:32:29 +0000709 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
710 struct netdev_notifier_pre_changeaddr_info *prechaddr_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000711 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
712 struct ipvl_dev *ipvlan, *next;
713 struct ipvl_port *port;
714 LIST_HEAD(lst_kill);
David Brazdil0f672f62019-12-10 10:32:29 +0000715 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000716
717 if (!netif_is_ipvlan_port(dev))
718 return NOTIFY_DONE;
719
720 port = ipvlan_port_get_rtnl(dev);
721
722 switch (event) {
723 case NETDEV_CHANGE:
724 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
725 netif_stacked_transfer_operstate(ipvlan->phy_dev,
726 ipvlan->dev);
727 break;
728
729 case NETDEV_REGISTER: {
730 struct net *oldnet, *newnet = dev_net(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000731
732 oldnet = read_pnet(&port->pnet);
733 if (net_eq(newnet, oldnet))
734 break;
735
736 write_pnet(&port->pnet, newnet);
737
David Brazdil0f672f62019-12-10 10:32:29 +0000738 ipvlan_migrate_l3s_hook(oldnet, newnet);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000739 break;
740 }
741 case NETDEV_UNREGISTER:
742 if (dev->reg_state != NETREG_UNREGISTERING)
743 break;
744
745 list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
746 ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
747 &lst_kill);
748 unregister_netdevice_many(&lst_kill);
749 break;
750
751 case NETDEV_FEAT_CHANGE:
752 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000753 ipvlan->dev->gso_max_size = dev->gso_max_size;
754 ipvlan->dev->gso_max_segs = dev->gso_max_segs;
Olivier Deprez0e641232021-09-23 10:07:05 +0200755 netdev_update_features(ipvlan->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000756 }
757 break;
758
759 case NETDEV_CHANGEMTU:
760 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
761 ipvlan_adjust_mtu(ipvlan, dev);
762 break;
763
David Brazdil0f672f62019-12-10 10:32:29 +0000764 case NETDEV_PRE_CHANGEADDR:
765 prechaddr_info = ptr;
766 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
767 err = dev_pre_changeaddr_notify(ipvlan->dev,
768 prechaddr_info->dev_addr,
769 extack);
770 if (err)
771 return notifier_from_errno(err);
772 }
773 break;
774
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000775 case NETDEV_CHANGEADDR:
776 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
777 ether_addr_copy(ipvlan->dev->dev_addr, dev->dev_addr);
778 call_netdevice_notifiers(NETDEV_CHANGEADDR, ipvlan->dev);
779 }
780 break;
781
782 case NETDEV_PRE_TYPE_CHANGE:
783 /* Forbid underlying device to change its type. */
784 return NOTIFY_BAD;
785 }
786 return NOTIFY_DONE;
787}
788
789/* the caller must held the addrs lock */
790static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
791{
792 struct ipvl_addr *addr;
793
794 addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
795 if (!addr)
796 return -ENOMEM;
797
798 addr->master = ipvlan;
799 if (!is_v6) {
800 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
801 addr->atype = IPVL_IPV4;
802#if IS_ENABLED(CONFIG_IPV6)
803 } else {
804 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
805 addr->atype = IPVL_IPV6;
806#endif
807 }
808
809 list_add_tail_rcu(&addr->anode, &ipvlan->addrs);
810
811 /* If the interface is not up, the address will be added to the hash
812 * list by ipvlan_open.
813 */
814 if (netif_running(ipvlan->dev))
815 ipvlan_ht_addr_add(ipvlan, addr);
816
817 return 0;
818}
819
820static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
821{
822 struct ipvl_addr *addr;
823
824 spin_lock_bh(&ipvlan->addrs_lock);
825 addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
826 if (!addr) {
827 spin_unlock_bh(&ipvlan->addrs_lock);
828 return;
829 }
830
831 ipvlan_ht_addr_del(addr);
832 list_del_rcu(&addr->anode);
833 spin_unlock_bh(&ipvlan->addrs_lock);
834 kfree_rcu(addr, rcu);
835}
836
837static bool ipvlan_is_valid_dev(const struct net_device *dev)
838{
839 struct ipvl_dev *ipvlan = netdev_priv(dev);
840
841 if (!netif_is_ipvlan(dev))
842 return false;
843
844 if (!ipvlan || !ipvlan->port)
845 return false;
846
847 return true;
848}
849
850#if IS_ENABLED(CONFIG_IPV6)
851static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
852{
853 int ret = -EINVAL;
854
855 spin_lock_bh(&ipvlan->addrs_lock);
856 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true))
857 netif_err(ipvlan, ifup, ipvlan->dev,
858 "Failed to add IPv6=%pI6c addr for %s intf\n",
859 ip6_addr, ipvlan->dev->name);
860 else
861 ret = ipvlan_add_addr(ipvlan, ip6_addr, true);
862 spin_unlock_bh(&ipvlan->addrs_lock);
863 return ret;
864}
865
866static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
867{
868 return ipvlan_del_addr(ipvlan, ip6_addr, true);
869}
870
871static int ipvlan_addr6_event(struct notifier_block *unused,
872 unsigned long event, void *ptr)
873{
874 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
875 struct net_device *dev = (struct net_device *)if6->idev->dev;
876 struct ipvl_dev *ipvlan = netdev_priv(dev);
877
878 if (!ipvlan_is_valid_dev(dev))
879 return NOTIFY_DONE;
880
881 switch (event) {
882 case NETDEV_UP:
883 if (ipvlan_add_addr6(ipvlan, &if6->addr))
884 return NOTIFY_BAD;
885 break;
886
887 case NETDEV_DOWN:
888 ipvlan_del_addr6(ipvlan, &if6->addr);
889 break;
890 }
891
892 return NOTIFY_OK;
893}
894
895static int ipvlan_addr6_validator_event(struct notifier_block *unused,
896 unsigned long event, void *ptr)
897{
898 struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
899 struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
900 struct ipvl_dev *ipvlan = netdev_priv(dev);
901
902 if (!ipvlan_is_valid_dev(dev))
903 return NOTIFY_DONE;
904
905 switch (event) {
906 case NETDEV_UP:
907 if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true)) {
908 NL_SET_ERR_MSG(i6vi->extack,
909 "Address already assigned to an ipvlan device");
910 return notifier_from_errno(-EADDRINUSE);
911 }
912 break;
913 }
914
915 return NOTIFY_OK;
916}
917#endif
918
919static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
920{
921 int ret = -EINVAL;
922
923 spin_lock_bh(&ipvlan->addrs_lock);
924 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false))
925 netif_err(ipvlan, ifup, ipvlan->dev,
926 "Failed to add IPv4=%pI4 on %s intf.\n",
927 ip4_addr, ipvlan->dev->name);
928 else
929 ret = ipvlan_add_addr(ipvlan, ip4_addr, false);
930 spin_unlock_bh(&ipvlan->addrs_lock);
931 return ret;
932}
933
934static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
935{
936 return ipvlan_del_addr(ipvlan, ip4_addr, false);
937}
938
939static int ipvlan_addr4_event(struct notifier_block *unused,
940 unsigned long event, void *ptr)
941{
942 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
943 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
944 struct ipvl_dev *ipvlan = netdev_priv(dev);
945 struct in_addr ip4_addr;
946
947 if (!ipvlan_is_valid_dev(dev))
948 return NOTIFY_DONE;
949
950 switch (event) {
951 case NETDEV_UP:
952 ip4_addr.s_addr = if4->ifa_address;
953 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
954 return NOTIFY_BAD;
955 break;
956
957 case NETDEV_DOWN:
958 ip4_addr.s_addr = if4->ifa_address;
959 ipvlan_del_addr4(ipvlan, &ip4_addr);
960 break;
961 }
962
963 return NOTIFY_OK;
964}
965
966static int ipvlan_addr4_validator_event(struct notifier_block *unused,
967 unsigned long event, void *ptr)
968{
969 struct in_validator_info *ivi = (struct in_validator_info *)ptr;
970 struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
971 struct ipvl_dev *ipvlan = netdev_priv(dev);
972
973 if (!ipvlan_is_valid_dev(dev))
974 return NOTIFY_DONE;
975
976 switch (event) {
977 case NETDEV_UP:
978 if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false)) {
979 NL_SET_ERR_MSG(ivi->extack,
980 "Address already assigned to an ipvlan device");
981 return notifier_from_errno(-EADDRINUSE);
982 }
983 break;
984 }
985
986 return NOTIFY_OK;
987}
988
989static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
990 .notifier_call = ipvlan_addr4_event,
991};
992
993static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
994 .notifier_call = ipvlan_addr4_validator_event,
995};
996
997static struct notifier_block ipvlan_notifier_block __read_mostly = {
998 .notifier_call = ipvlan_device_event,
999};
1000
1001#if IS_ENABLED(CONFIG_IPV6)
1002static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
1003 .notifier_call = ipvlan_addr6_event,
1004};
1005
1006static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
1007 .notifier_call = ipvlan_addr6_validator_event,
1008};
1009#endif
1010
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001011static int __init ipvlan_init_module(void)
1012{
1013 int err;
1014
1015 ipvlan_init_secret();
1016 register_netdevice_notifier(&ipvlan_notifier_block);
1017#if IS_ENABLED(CONFIG_IPV6)
1018 register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1019 register_inet6addr_validator_notifier(
1020 &ipvlan_addr6_vtor_notifier_block);
1021#endif
1022 register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1023 register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
1024
David Brazdil0f672f62019-12-10 10:32:29 +00001025 err = ipvlan_l3s_init();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001026 if (err < 0)
1027 goto error;
1028
1029 err = ipvlan_link_register(&ipvlan_link_ops);
1030 if (err < 0) {
David Brazdil0f672f62019-12-10 10:32:29 +00001031 ipvlan_l3s_cleanup();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001032 goto error;
1033 }
1034
1035 return 0;
1036error:
1037 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1038 unregister_inetaddr_validator_notifier(
1039 &ipvlan_addr4_vtor_notifier_block);
1040#if IS_ENABLED(CONFIG_IPV6)
1041 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1042 unregister_inet6addr_validator_notifier(
1043 &ipvlan_addr6_vtor_notifier_block);
1044#endif
1045 unregister_netdevice_notifier(&ipvlan_notifier_block);
1046 return err;
1047}
1048
1049static void __exit ipvlan_cleanup_module(void)
1050{
1051 rtnl_link_unregister(&ipvlan_link_ops);
David Brazdil0f672f62019-12-10 10:32:29 +00001052 ipvlan_l3s_cleanup();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001053 unregister_netdevice_notifier(&ipvlan_notifier_block);
1054 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1055 unregister_inetaddr_validator_notifier(
1056 &ipvlan_addr4_vtor_notifier_block);
1057#if IS_ENABLED(CONFIG_IPV6)
1058 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1059 unregister_inet6addr_validator_notifier(
1060 &ipvlan_addr6_vtor_notifier_block);
1061#endif
1062}
1063
1064module_init(ipvlan_init_module);
1065module_exit(ipvlan_cleanup_module);
1066
1067MODULE_LICENSE("GPL");
1068MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
1069MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
1070MODULE_ALIAS_RTNL_LINK("ipvlan");