1 /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
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.
12 static unsigned int ipvlan_netid __read_mostly
;
15 unsigned int ipvl_nf_hook_refcnt
;
18 static const struct nf_hook_ops ipvl_nfops
[] = {
20 .hook
= ipvlan_nf_input
,
22 .hooknum
= NF_INET_LOCAL_IN
,
25 #if IS_ENABLED(CONFIG_IPV6)
27 .hook
= ipvlan_nf_input
,
29 .hooknum
= NF_INET_LOCAL_IN
,
35 static const struct l3mdev_ops ipvl_l3mdev_ops
= {
36 .l3mdev_l3_rcv
= ipvlan_l3_rcv
,
39 static void ipvlan_adjust_mtu(struct ipvl_dev
*ipvlan
, struct net_device
*dev
)
41 ipvlan
->dev
->mtu
= dev
->mtu
;
44 static int ipvlan_register_nf_hook(struct net
*net
)
46 struct ipvlan_netns
*vnet
= net_generic(net
, ipvlan_netid
);
49 if (!vnet
->ipvl_nf_hook_refcnt
) {
50 err
= nf_register_net_hooks(net
, ipvl_nfops
,
51 ARRAY_SIZE(ipvl_nfops
));
53 vnet
->ipvl_nf_hook_refcnt
= 1;
55 vnet
->ipvl_nf_hook_refcnt
++;
61 static void ipvlan_unregister_nf_hook(struct net
*net
)
63 struct ipvlan_netns
*vnet
= net_generic(net
, ipvlan_netid
);
65 if (WARN_ON(!vnet
->ipvl_nf_hook_refcnt
))
68 vnet
->ipvl_nf_hook_refcnt
--;
69 if (!vnet
->ipvl_nf_hook_refcnt
)
70 nf_unregister_net_hooks(net
, ipvl_nfops
,
71 ARRAY_SIZE(ipvl_nfops
));
74 static int ipvlan_set_port_mode(struct ipvl_port
*port
, u16 nval
,
75 struct netlink_ext_ack
*extack
)
77 struct ipvl_dev
*ipvlan
;
78 struct net_device
*mdev
= port
->dev
;
83 if (port
->mode
!= nval
) {
84 list_for_each_entry(ipvlan
, &port
->ipvlans
, pnode
) {
85 flags
= ipvlan
->dev
->flags
;
86 if (nval
== IPVLAN_MODE_L3
|| nval
== IPVLAN_MODE_L3S
) {
87 err
= dev_change_flags(ipvlan
->dev
,
91 err
= dev_change_flags(ipvlan
->dev
,
98 if (nval
== IPVLAN_MODE_L3S
) {
100 err
= ipvlan_register_nf_hook(read_pnet(&port
->pnet
));
102 mdev
->l3mdev_ops
= &ipvl_l3mdev_ops
;
103 mdev
->priv_flags
|= IFF_L3MDEV_RX_HANDLER
;
106 } else if (port
->mode
== IPVLAN_MODE_L3S
) {
107 /* Old mode was L3S */
108 mdev
->priv_flags
&= ~IFF_L3MDEV_RX_HANDLER
;
109 ipvlan_unregister_nf_hook(read_pnet(&port
->pnet
));
110 mdev
->l3mdev_ops
= NULL
;
117 /* Undo the flags changes that have been done so far. */
118 list_for_each_entry_continue_reverse(ipvlan
, &port
->ipvlans
, pnode
) {
119 flags
= ipvlan
->dev
->flags
;
120 if (port
->mode
== IPVLAN_MODE_L3
||
121 port
->mode
== IPVLAN_MODE_L3S
)
122 dev_change_flags(ipvlan
->dev
, flags
| IFF_NOARP
,
125 dev_change_flags(ipvlan
->dev
, flags
& ~IFF_NOARP
,
132 static int ipvlan_port_create(struct net_device
*dev
)
134 struct ipvl_port
*port
;
137 port
= kzalloc(sizeof(struct ipvl_port
), GFP_KERNEL
);
141 write_pnet(&port
->pnet
, dev_net(dev
));
143 port
->mode
= IPVLAN_MODE_L3
;
144 INIT_LIST_HEAD(&port
->ipvlans
);
145 for (idx
= 0; idx
< IPVLAN_HASH_SIZE
; idx
++)
146 INIT_HLIST_HEAD(&port
->hlhead
[idx
]);
148 skb_queue_head_init(&port
->backlog
);
149 INIT_WORK(&port
->wq
, ipvlan_process_multicast
);
150 ida_init(&port
->ida
);
151 port
->dev_id_start
= 1;
153 err
= netdev_rx_handler_register(dev
, ipvlan_handle_frame
, port
);
164 static void ipvlan_port_destroy(struct net_device
*dev
)
166 struct ipvl_port
*port
= ipvlan_port_get_rtnl(dev
);
169 if (port
->mode
== IPVLAN_MODE_L3S
) {
170 dev
->priv_flags
&= ~IFF_L3MDEV_RX_HANDLER
;
171 ipvlan_unregister_nf_hook(dev_net(dev
));
172 dev
->l3mdev_ops
= NULL
;
174 netdev_rx_handler_unregister(dev
);
175 cancel_work_sync(&port
->wq
);
176 while ((skb
= __skb_dequeue(&port
->backlog
)) != NULL
) {
181 ida_destroy(&port
->ida
);
185 #define IPVLAN_FEATURES \
186 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
187 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \
188 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
189 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
191 #define IPVLAN_STATE_MASK \
192 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
194 static int ipvlan_init(struct net_device
*dev
)
196 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
197 struct net_device
*phy_dev
= ipvlan
->phy_dev
;
198 struct ipvl_port
*port
;
201 dev
->state
= (dev
->state
& ~IPVLAN_STATE_MASK
) |
202 (phy_dev
->state
& IPVLAN_STATE_MASK
);
203 dev
->features
= phy_dev
->features
& IPVLAN_FEATURES
;
204 dev
->features
|= NETIF_F_LLTX
| NETIF_F_VLAN_CHALLENGED
;
205 dev
->gso_max_size
= phy_dev
->gso_max_size
;
206 dev
->gso_max_segs
= phy_dev
->gso_max_segs
;
207 dev
->hard_header_len
= phy_dev
->hard_header_len
;
209 netdev_lockdep_set_classes(dev
);
211 ipvlan
->pcpu_stats
= netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats
);
212 if (!ipvlan
->pcpu_stats
)
215 if (!netif_is_ipvlan_port(phy_dev
)) {
216 err
= ipvlan_port_create(phy_dev
);
218 free_percpu(ipvlan
->pcpu_stats
);
222 port
= ipvlan_port_get_rtnl(phy_dev
);
227 static void ipvlan_uninit(struct net_device
*dev
)
229 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
230 struct net_device
*phy_dev
= ipvlan
->phy_dev
;
231 struct ipvl_port
*port
;
233 free_percpu(ipvlan
->pcpu_stats
);
235 port
= ipvlan_port_get_rtnl(phy_dev
);
238 ipvlan_port_destroy(port
->dev
);
241 static int ipvlan_open(struct net_device
*dev
)
243 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
244 struct net_device
*phy_dev
= ipvlan
->phy_dev
;
245 struct ipvl_addr
*addr
;
247 if (ipvlan
->port
->mode
== IPVLAN_MODE_L3
||
248 ipvlan
->port
->mode
== IPVLAN_MODE_L3S
)
249 dev
->flags
|= IFF_NOARP
;
251 dev
->flags
&= ~IFF_NOARP
;
254 list_for_each_entry_rcu(addr
, &ipvlan
->addrs
, anode
)
255 ipvlan_ht_addr_add(ipvlan
, addr
);
258 return dev_uc_add(phy_dev
, phy_dev
->dev_addr
);
261 static int ipvlan_stop(struct net_device
*dev
)
263 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
264 struct net_device
*phy_dev
= ipvlan
->phy_dev
;
265 struct ipvl_addr
*addr
;
267 dev_uc_unsync(phy_dev
, dev
);
268 dev_mc_unsync(phy_dev
, dev
);
270 dev_uc_del(phy_dev
, phy_dev
->dev_addr
);
273 list_for_each_entry_rcu(addr
, &ipvlan
->addrs
, anode
)
274 ipvlan_ht_addr_del(addr
);
280 static netdev_tx_t
ipvlan_start_xmit(struct sk_buff
*skb
,
281 struct net_device
*dev
)
283 const struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
284 int skblen
= skb
->len
;
287 ret
= ipvlan_queue_xmit(skb
, dev
);
288 if (likely(ret
== NET_XMIT_SUCCESS
|| ret
== NET_XMIT_CN
)) {
289 struct ipvl_pcpu_stats
*pcptr
;
291 pcptr
= this_cpu_ptr(ipvlan
->pcpu_stats
);
293 u64_stats_update_begin(&pcptr
->syncp
);
295 pcptr
->tx_bytes
+= skblen
;
296 u64_stats_update_end(&pcptr
->syncp
);
298 this_cpu_inc(ipvlan
->pcpu_stats
->tx_drps
);
303 static netdev_features_t
ipvlan_fix_features(struct net_device
*dev
,
304 netdev_features_t features
)
306 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
308 return features
& (ipvlan
->sfeatures
| ~IPVLAN_FEATURES
);
311 static void ipvlan_change_rx_flags(struct net_device
*dev
, int change
)
313 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
314 struct net_device
*phy_dev
= ipvlan
->phy_dev
;
316 if (change
& IFF_ALLMULTI
)
317 dev_set_allmulti(phy_dev
, dev
->flags
& IFF_ALLMULTI
? 1 : -1);
320 static void ipvlan_set_multicast_mac_filter(struct net_device
*dev
)
322 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
324 if (dev
->flags
& (IFF_PROMISC
| IFF_ALLMULTI
)) {
325 bitmap_fill(ipvlan
->mac_filters
, IPVLAN_MAC_FILTER_SIZE
);
327 struct netdev_hw_addr
*ha
;
328 DECLARE_BITMAP(mc_filters
, IPVLAN_MAC_FILTER_SIZE
);
330 bitmap_zero(mc_filters
, IPVLAN_MAC_FILTER_SIZE
);
331 netdev_for_each_mc_addr(ha
, dev
)
332 __set_bit(ipvlan_mac_hash(ha
->addr
), mc_filters
);
334 /* Turn-on broadcast bit irrespective of address family,
335 * since broadcast is deferred to a work-queue, hence no
336 * impact on fast-path processing.
338 __set_bit(ipvlan_mac_hash(dev
->broadcast
), mc_filters
);
340 bitmap_copy(ipvlan
->mac_filters
, mc_filters
,
341 IPVLAN_MAC_FILTER_SIZE
);
343 dev_uc_sync(ipvlan
->phy_dev
, dev
);
344 dev_mc_sync(ipvlan
->phy_dev
, dev
);
347 static void ipvlan_get_stats64(struct net_device
*dev
,
348 struct rtnl_link_stats64
*s
)
350 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
352 if (ipvlan
->pcpu_stats
) {
353 struct ipvl_pcpu_stats
*pcptr
;
354 u64 rx_pkts
, rx_bytes
, rx_mcast
, tx_pkts
, tx_bytes
;
355 u32 rx_errs
= 0, tx_drps
= 0;
359 for_each_possible_cpu(idx
) {
360 pcptr
= per_cpu_ptr(ipvlan
->pcpu_stats
, idx
);
362 strt
= u64_stats_fetch_begin_irq(&pcptr
->syncp
);
363 rx_pkts
= pcptr
->rx_pkts
;
364 rx_bytes
= pcptr
->rx_bytes
;
365 rx_mcast
= pcptr
->rx_mcast
;
366 tx_pkts
= pcptr
->tx_pkts
;
367 tx_bytes
= pcptr
->tx_bytes
;
368 } while (u64_stats_fetch_retry_irq(&pcptr
->syncp
,
371 s
->rx_packets
+= rx_pkts
;
372 s
->rx_bytes
+= rx_bytes
;
373 s
->multicast
+= rx_mcast
;
374 s
->tx_packets
+= tx_pkts
;
375 s
->tx_bytes
+= tx_bytes
;
377 /* u32 values are updated without syncp protection. */
378 rx_errs
+= pcptr
->rx_errs
;
379 tx_drps
+= pcptr
->tx_drps
;
381 s
->rx_errors
= rx_errs
;
382 s
->rx_dropped
= rx_errs
;
383 s
->tx_dropped
= tx_drps
;
387 static int ipvlan_vlan_rx_add_vid(struct net_device
*dev
, __be16 proto
, u16 vid
)
389 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
390 struct net_device
*phy_dev
= ipvlan
->phy_dev
;
392 return vlan_vid_add(phy_dev
, proto
, vid
);
395 static int ipvlan_vlan_rx_kill_vid(struct net_device
*dev
, __be16 proto
,
398 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
399 struct net_device
*phy_dev
= ipvlan
->phy_dev
;
401 vlan_vid_del(phy_dev
, proto
, vid
);
405 static int ipvlan_get_iflink(const struct net_device
*dev
)
407 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
409 return ipvlan
->phy_dev
->ifindex
;
412 static const struct net_device_ops ipvlan_netdev_ops
= {
413 .ndo_init
= ipvlan_init
,
414 .ndo_uninit
= ipvlan_uninit
,
415 .ndo_open
= ipvlan_open
,
416 .ndo_stop
= ipvlan_stop
,
417 .ndo_start_xmit
= ipvlan_start_xmit
,
418 .ndo_fix_features
= ipvlan_fix_features
,
419 .ndo_change_rx_flags
= ipvlan_change_rx_flags
,
420 .ndo_set_rx_mode
= ipvlan_set_multicast_mac_filter
,
421 .ndo_get_stats64
= ipvlan_get_stats64
,
422 .ndo_vlan_rx_add_vid
= ipvlan_vlan_rx_add_vid
,
423 .ndo_vlan_rx_kill_vid
= ipvlan_vlan_rx_kill_vid
,
424 .ndo_get_iflink
= ipvlan_get_iflink
,
427 static int ipvlan_hard_header(struct sk_buff
*skb
, struct net_device
*dev
,
428 unsigned short type
, const void *daddr
,
429 const void *saddr
, unsigned len
)
431 const struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
432 struct net_device
*phy_dev
= ipvlan
->phy_dev
;
434 /* TODO Probably use a different field than dev_addr so that the
435 * mac-address on the virtual device is portable and can be carried
436 * while the packets use the mac-addr on the physical device.
438 return dev_hard_header(skb
, phy_dev
, type
, daddr
,
439 saddr
? : phy_dev
->dev_addr
, len
);
442 static const struct header_ops ipvlan_header_ops
= {
443 .create
= ipvlan_hard_header
,
444 .parse
= eth_header_parse
,
445 .cache
= eth_header_cache
,
446 .cache_update
= eth_header_cache_update
,
449 static bool netif_is_ipvlan(const struct net_device
*dev
)
451 /* both ipvlan and ipvtap devices use the same netdev_ops */
452 return dev
->netdev_ops
== &ipvlan_netdev_ops
;
455 static int ipvlan_ethtool_get_link_ksettings(struct net_device
*dev
,
456 struct ethtool_link_ksettings
*cmd
)
458 const struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
460 return __ethtool_get_link_ksettings(ipvlan
->phy_dev
, cmd
);
463 static void ipvlan_ethtool_get_drvinfo(struct net_device
*dev
,
464 struct ethtool_drvinfo
*drvinfo
)
466 strlcpy(drvinfo
->driver
, IPVLAN_DRV
, sizeof(drvinfo
->driver
));
467 strlcpy(drvinfo
->version
, IPV_DRV_VER
, sizeof(drvinfo
->version
));
470 static u32
ipvlan_ethtool_get_msglevel(struct net_device
*dev
)
472 const struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
474 return ipvlan
->msg_enable
;
477 static void ipvlan_ethtool_set_msglevel(struct net_device
*dev
, u32 value
)
479 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
481 ipvlan
->msg_enable
= value
;
484 static const struct ethtool_ops ipvlan_ethtool_ops
= {
485 .get_link
= ethtool_op_get_link
,
486 .get_link_ksettings
= ipvlan_ethtool_get_link_ksettings
,
487 .get_drvinfo
= ipvlan_ethtool_get_drvinfo
,
488 .get_msglevel
= ipvlan_ethtool_get_msglevel
,
489 .set_msglevel
= ipvlan_ethtool_set_msglevel
,
492 static int ipvlan_nl_changelink(struct net_device
*dev
,
493 struct nlattr
*tb
[], struct nlattr
*data
[],
494 struct netlink_ext_ack
*extack
)
496 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
497 struct ipvl_port
*port
= ipvlan_port_get_rtnl(ipvlan
->phy_dev
);
502 if (!ns_capable(dev_net(ipvlan
->phy_dev
)->user_ns
, CAP_NET_ADMIN
))
505 if (data
[IFLA_IPVLAN_MODE
]) {
506 u16 nmode
= nla_get_u16(data
[IFLA_IPVLAN_MODE
]);
508 err
= ipvlan_set_port_mode(port
, nmode
, extack
);
511 if (!err
&& data
[IFLA_IPVLAN_FLAGS
]) {
512 u16 flags
= nla_get_u16(data
[IFLA_IPVLAN_FLAGS
]);
514 if (flags
& IPVLAN_F_PRIVATE
)
515 ipvlan_mark_private(port
);
517 ipvlan_clear_private(port
);
519 if (flags
& IPVLAN_F_VEPA
)
520 ipvlan_mark_vepa(port
);
522 ipvlan_clear_vepa(port
);
528 static size_t ipvlan_nl_getsize(const struct net_device
*dev
)
531 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
532 + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
536 static int ipvlan_nl_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
537 struct netlink_ext_ack
*extack
)
542 if (data
[IFLA_IPVLAN_MODE
]) {
543 u16 mode
= nla_get_u16(data
[IFLA_IPVLAN_MODE
]);
545 if (mode
>= IPVLAN_MODE_MAX
)
548 if (data
[IFLA_IPVLAN_FLAGS
]) {
549 u16 flags
= nla_get_u16(data
[IFLA_IPVLAN_FLAGS
]);
551 /* Only two bits are used at this moment. */
552 if (flags
& ~(IPVLAN_F_PRIVATE
| IPVLAN_F_VEPA
))
554 /* Also both flags can't be active at the same time. */
555 if ((flags
& (IPVLAN_F_PRIVATE
| IPVLAN_F_VEPA
)) ==
556 (IPVLAN_F_PRIVATE
| IPVLAN_F_VEPA
))
563 static int ipvlan_nl_fillinfo(struct sk_buff
*skb
,
564 const struct net_device
*dev
)
566 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
567 struct ipvl_port
*port
= ipvlan_port_get_rtnl(ipvlan
->phy_dev
);
574 if (nla_put_u16(skb
, IFLA_IPVLAN_MODE
, port
->mode
))
576 if (nla_put_u16(skb
, IFLA_IPVLAN_FLAGS
, port
->flags
))
585 int ipvlan_link_new(struct net
*src_net
, struct net_device
*dev
,
586 struct nlattr
*tb
[], struct nlattr
*data
[],
587 struct netlink_ext_ack
*extack
)
589 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
590 struct ipvl_port
*port
;
591 struct net_device
*phy_dev
;
593 u16 mode
= IPVLAN_MODE_L3
;
598 phy_dev
= __dev_get_by_index(src_net
, nla_get_u32(tb
[IFLA_LINK
]));
602 if (netif_is_ipvlan(phy_dev
)) {
603 struct ipvl_dev
*tmp
= netdev_priv(phy_dev
);
605 phy_dev
= tmp
->phy_dev
;
606 if (!ns_capable(dev_net(phy_dev
)->user_ns
, CAP_NET_ADMIN
))
608 } else if (!netif_is_ipvlan_port(phy_dev
)) {
609 /* Exit early if the underlying link is invalid or busy */
610 if (phy_dev
->type
!= ARPHRD_ETHER
||
611 phy_dev
->flags
& IFF_LOOPBACK
) {
613 "Master is either lo or non-ether device\n");
617 if (netdev_is_rx_handler_busy(phy_dev
)) {
618 netdev_err(phy_dev
, "Device is already in use.\n");
623 ipvlan
->phy_dev
= phy_dev
;
625 ipvlan
->sfeatures
= IPVLAN_FEATURES
;
627 ipvlan_adjust_mtu(ipvlan
, phy_dev
);
628 INIT_LIST_HEAD(&ipvlan
->addrs
);
629 spin_lock_init(&ipvlan
->addrs_lock
);
631 /* TODO Probably put random address here to be presented to the
632 * world but keep using the physical-dev address for the outgoing
635 memcpy(dev
->dev_addr
, phy_dev
->dev_addr
, ETH_ALEN
);
637 dev
->priv_flags
|= IFF_NO_RX_HANDLER
;
639 err
= register_netdevice(dev
);
643 /* ipvlan_init() would have created the port, if required */
644 port
= ipvlan_port_get_rtnl(phy_dev
);
647 /* If the port-id base is at the MAX value, then wrap it around and
648 * begin from 0x1 again. This may be due to a busy system where lots
649 * of slaves are getting created and deleted.
651 if (port
->dev_id_start
== 0xFFFE)
652 port
->dev_id_start
= 0x1;
654 /* Since L2 address is shared among all IPvlan slaves including
655 * master, use unique 16 bit dev-ids to diffentiate among them.
656 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
657 * slave link [see addrconf_ifid_eui48()].
659 err
= ida_simple_get(&port
->ida
, port
->dev_id_start
, 0xFFFE,
662 err
= ida_simple_get(&port
->ida
, 0x1, port
->dev_id_start
,
665 goto unregister_netdev
;
668 /* Increment id-base to the next slot for the future assignment */
669 port
->dev_id_start
= err
+ 1;
671 err
= netdev_upper_dev_link(phy_dev
, dev
, extack
);
675 /* Flags are per port and latest update overrides. User has
676 * to be consistent in setting it just like the mode attribute.
678 if (data
&& data
[IFLA_IPVLAN_FLAGS
])
679 port
->flags
= nla_get_u16(data
[IFLA_IPVLAN_FLAGS
]);
681 if (data
&& data
[IFLA_IPVLAN_MODE
])
682 mode
= nla_get_u16(data
[IFLA_IPVLAN_MODE
]);
684 err
= ipvlan_set_port_mode(port
, mode
, extack
);
688 list_add_tail_rcu(&ipvlan
->pnode
, &port
->ipvlans
);
689 netif_stacked_transfer_operstate(phy_dev
, dev
);
693 netdev_upper_dev_unlink(phy_dev
, dev
);
695 ida_simple_remove(&port
->ida
, dev
->dev_id
);
697 unregister_netdevice(dev
);
700 EXPORT_SYMBOL_GPL(ipvlan_link_new
);
702 void ipvlan_link_delete(struct net_device
*dev
, struct list_head
*head
)
704 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
705 struct ipvl_addr
*addr
, *next
;
707 spin_lock_bh(&ipvlan
->addrs_lock
);
708 list_for_each_entry_safe(addr
, next
, &ipvlan
->addrs
, anode
) {
709 ipvlan_ht_addr_del(addr
);
710 list_del_rcu(&addr
->anode
);
711 kfree_rcu(addr
, rcu
);
713 spin_unlock_bh(&ipvlan
->addrs_lock
);
715 ida_simple_remove(&ipvlan
->port
->ida
, dev
->dev_id
);
716 list_del_rcu(&ipvlan
->pnode
);
717 unregister_netdevice_queue(dev
, head
);
718 netdev_upper_dev_unlink(ipvlan
->phy_dev
, dev
);
720 EXPORT_SYMBOL_GPL(ipvlan_link_delete
);
722 void ipvlan_link_setup(struct net_device
*dev
)
726 dev
->max_mtu
= ETH_MAX_MTU
;
727 dev
->priv_flags
&= ~(IFF_XMIT_DST_RELEASE
| IFF_TX_SKB_SHARING
);
728 dev
->priv_flags
|= IFF_UNICAST_FLT
| IFF_NO_QUEUE
;
729 dev
->netdev_ops
= &ipvlan_netdev_ops
;
730 dev
->needs_free_netdev
= true;
731 dev
->header_ops
= &ipvlan_header_ops
;
732 dev
->ethtool_ops
= &ipvlan_ethtool_ops
;
734 EXPORT_SYMBOL_GPL(ipvlan_link_setup
);
736 static const struct nla_policy ipvlan_nl_policy
[IFLA_IPVLAN_MAX
+ 1] =
738 [IFLA_IPVLAN_MODE
] = { .type
= NLA_U16
},
739 [IFLA_IPVLAN_FLAGS
] = { .type
= NLA_U16
},
742 static struct rtnl_link_ops ipvlan_link_ops
= {
744 .priv_size
= sizeof(struct ipvl_dev
),
746 .setup
= ipvlan_link_setup
,
747 .newlink
= ipvlan_link_new
,
748 .dellink
= ipvlan_link_delete
,
751 int ipvlan_link_register(struct rtnl_link_ops
*ops
)
753 ops
->get_size
= ipvlan_nl_getsize
;
754 ops
->policy
= ipvlan_nl_policy
;
755 ops
->validate
= ipvlan_nl_validate
;
756 ops
->fill_info
= ipvlan_nl_fillinfo
;
757 ops
->changelink
= ipvlan_nl_changelink
;
758 ops
->maxtype
= IFLA_IPVLAN_MAX
;
759 return rtnl_link_register(ops
);
761 EXPORT_SYMBOL_GPL(ipvlan_link_register
);
763 static int ipvlan_device_event(struct notifier_block
*unused
,
764 unsigned long event
, void *ptr
)
766 struct netlink_ext_ack
*extack
= netdev_notifier_info_to_extack(ptr
);
767 struct netdev_notifier_pre_changeaddr_info
*prechaddr_info
;
768 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
769 struct ipvl_dev
*ipvlan
, *next
;
770 struct ipvl_port
*port
;
774 if (!netif_is_ipvlan_port(dev
))
777 port
= ipvlan_port_get_rtnl(dev
);
781 list_for_each_entry(ipvlan
, &port
->ipvlans
, pnode
)
782 netif_stacked_transfer_operstate(ipvlan
->phy_dev
,
786 case NETDEV_REGISTER
: {
787 struct net
*oldnet
, *newnet
= dev_net(dev
);
788 struct ipvlan_netns
*old_vnet
;
790 oldnet
= read_pnet(&port
->pnet
);
791 if (net_eq(newnet
, oldnet
))
794 write_pnet(&port
->pnet
, newnet
);
796 old_vnet
= net_generic(oldnet
, ipvlan_netid
);
797 if (!old_vnet
->ipvl_nf_hook_refcnt
)
800 ipvlan_register_nf_hook(newnet
);
801 ipvlan_unregister_nf_hook(oldnet
);
804 case NETDEV_UNREGISTER
:
805 if (dev
->reg_state
!= NETREG_UNREGISTERING
)
808 list_for_each_entry_safe(ipvlan
, next
, &port
->ipvlans
, pnode
)
809 ipvlan
->dev
->rtnl_link_ops
->dellink(ipvlan
->dev
,
811 unregister_netdevice_many(&lst_kill
);
814 case NETDEV_FEAT_CHANGE
:
815 list_for_each_entry(ipvlan
, &port
->ipvlans
, pnode
) {
816 ipvlan
->dev
->features
= dev
->features
& IPVLAN_FEATURES
;
817 ipvlan
->dev
->gso_max_size
= dev
->gso_max_size
;
818 ipvlan
->dev
->gso_max_segs
= dev
->gso_max_segs
;
819 netdev_features_change(ipvlan
->dev
);
823 case NETDEV_CHANGEMTU
:
824 list_for_each_entry(ipvlan
, &port
->ipvlans
, pnode
)
825 ipvlan_adjust_mtu(ipvlan
, dev
);
828 case NETDEV_PRE_CHANGEADDR
:
829 prechaddr_info
= ptr
;
830 list_for_each_entry(ipvlan
, &port
->ipvlans
, pnode
) {
831 err
= dev_pre_changeaddr_notify(ipvlan
->dev
,
832 prechaddr_info
->dev_addr
,
835 return notifier_from_errno(err
);
839 case NETDEV_CHANGEADDR
:
840 list_for_each_entry(ipvlan
, &port
->ipvlans
, pnode
) {
841 ether_addr_copy(ipvlan
->dev
->dev_addr
, dev
->dev_addr
);
842 call_netdevice_notifiers(NETDEV_CHANGEADDR
, ipvlan
->dev
);
846 case NETDEV_PRE_TYPE_CHANGE
:
847 /* Forbid underlying device to change its type. */
853 /* the caller must held the addrs lock */
854 static int ipvlan_add_addr(struct ipvl_dev
*ipvlan
, void *iaddr
, bool is_v6
)
856 struct ipvl_addr
*addr
;
858 addr
= kzalloc(sizeof(struct ipvl_addr
), GFP_ATOMIC
);
862 addr
->master
= ipvlan
;
864 memcpy(&addr
->ip4addr
, iaddr
, sizeof(struct in_addr
));
865 addr
->atype
= IPVL_IPV4
;
866 #if IS_ENABLED(CONFIG_IPV6)
868 memcpy(&addr
->ip6addr
, iaddr
, sizeof(struct in6_addr
));
869 addr
->atype
= IPVL_IPV6
;
873 list_add_tail_rcu(&addr
->anode
, &ipvlan
->addrs
);
875 /* If the interface is not up, the address will be added to the hash
876 * list by ipvlan_open.
878 if (netif_running(ipvlan
->dev
))
879 ipvlan_ht_addr_add(ipvlan
, addr
);
884 static void ipvlan_del_addr(struct ipvl_dev
*ipvlan
, void *iaddr
, bool is_v6
)
886 struct ipvl_addr
*addr
;
888 spin_lock_bh(&ipvlan
->addrs_lock
);
889 addr
= ipvlan_find_addr(ipvlan
, iaddr
, is_v6
);
891 spin_unlock_bh(&ipvlan
->addrs_lock
);
895 ipvlan_ht_addr_del(addr
);
896 list_del_rcu(&addr
->anode
);
897 spin_unlock_bh(&ipvlan
->addrs_lock
);
898 kfree_rcu(addr
, rcu
);
901 static bool ipvlan_is_valid_dev(const struct net_device
*dev
)
903 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
905 if (!netif_is_ipvlan(dev
))
908 if (!ipvlan
|| !ipvlan
->port
)
914 #if IS_ENABLED(CONFIG_IPV6)
915 static int ipvlan_add_addr6(struct ipvl_dev
*ipvlan
, struct in6_addr
*ip6_addr
)
919 spin_lock_bh(&ipvlan
->addrs_lock
);
920 if (ipvlan_addr_busy(ipvlan
->port
, ip6_addr
, true))
921 netif_err(ipvlan
, ifup
, ipvlan
->dev
,
922 "Failed to add IPv6=%pI6c addr for %s intf\n",
923 ip6_addr
, ipvlan
->dev
->name
);
925 ret
= ipvlan_add_addr(ipvlan
, ip6_addr
, true);
926 spin_unlock_bh(&ipvlan
->addrs_lock
);
930 static void ipvlan_del_addr6(struct ipvl_dev
*ipvlan
, struct in6_addr
*ip6_addr
)
932 return ipvlan_del_addr(ipvlan
, ip6_addr
, true);
935 static int ipvlan_addr6_event(struct notifier_block
*unused
,
936 unsigned long event
, void *ptr
)
938 struct inet6_ifaddr
*if6
= (struct inet6_ifaddr
*)ptr
;
939 struct net_device
*dev
= (struct net_device
*)if6
->idev
->dev
;
940 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
942 if (!ipvlan_is_valid_dev(dev
))
947 if (ipvlan_add_addr6(ipvlan
, &if6
->addr
))
952 ipvlan_del_addr6(ipvlan
, &if6
->addr
);
959 static int ipvlan_addr6_validator_event(struct notifier_block
*unused
,
960 unsigned long event
, void *ptr
)
962 struct in6_validator_info
*i6vi
= (struct in6_validator_info
*)ptr
;
963 struct net_device
*dev
= (struct net_device
*)i6vi
->i6vi_dev
->dev
;
964 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
966 if (!ipvlan_is_valid_dev(dev
))
971 if (ipvlan_addr_busy(ipvlan
->port
, &i6vi
->i6vi_addr
, true)) {
972 NL_SET_ERR_MSG(i6vi
->extack
,
973 "Address already assigned to an ipvlan device");
974 return notifier_from_errno(-EADDRINUSE
);
983 static int ipvlan_add_addr4(struct ipvl_dev
*ipvlan
, struct in_addr
*ip4_addr
)
987 spin_lock_bh(&ipvlan
->addrs_lock
);
988 if (ipvlan_addr_busy(ipvlan
->port
, ip4_addr
, false))
989 netif_err(ipvlan
, ifup
, ipvlan
->dev
,
990 "Failed to add IPv4=%pI4 on %s intf.\n",
991 ip4_addr
, ipvlan
->dev
->name
);
993 ret
= ipvlan_add_addr(ipvlan
, ip4_addr
, false);
994 spin_unlock_bh(&ipvlan
->addrs_lock
);
998 static void ipvlan_del_addr4(struct ipvl_dev
*ipvlan
, struct in_addr
*ip4_addr
)
1000 return ipvlan_del_addr(ipvlan
, ip4_addr
, false);
1003 static int ipvlan_addr4_event(struct notifier_block
*unused
,
1004 unsigned long event
, void *ptr
)
1006 struct in_ifaddr
*if4
= (struct in_ifaddr
*)ptr
;
1007 struct net_device
*dev
= (struct net_device
*)if4
->ifa_dev
->dev
;
1008 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
1009 struct in_addr ip4_addr
;
1011 if (!ipvlan_is_valid_dev(dev
))
1016 ip4_addr
.s_addr
= if4
->ifa_address
;
1017 if (ipvlan_add_addr4(ipvlan
, &ip4_addr
))
1022 ip4_addr
.s_addr
= if4
->ifa_address
;
1023 ipvlan_del_addr4(ipvlan
, &ip4_addr
);
1030 static int ipvlan_addr4_validator_event(struct notifier_block
*unused
,
1031 unsigned long event
, void *ptr
)
1033 struct in_validator_info
*ivi
= (struct in_validator_info
*)ptr
;
1034 struct net_device
*dev
= (struct net_device
*)ivi
->ivi_dev
->dev
;
1035 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
1037 if (!ipvlan_is_valid_dev(dev
))
1042 if (ipvlan_addr_busy(ipvlan
->port
, &ivi
->ivi_addr
, false)) {
1043 NL_SET_ERR_MSG(ivi
->extack
,
1044 "Address already assigned to an ipvlan device");
1045 return notifier_from_errno(-EADDRINUSE
);
1053 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly
= {
1054 .notifier_call
= ipvlan_addr4_event
,
1057 static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly
= {
1058 .notifier_call
= ipvlan_addr4_validator_event
,
1061 static struct notifier_block ipvlan_notifier_block __read_mostly
= {
1062 .notifier_call
= ipvlan_device_event
,
1065 #if IS_ENABLED(CONFIG_IPV6)
1066 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly
= {
1067 .notifier_call
= ipvlan_addr6_event
,
1070 static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly
= {
1071 .notifier_call
= ipvlan_addr6_validator_event
,
1075 static void ipvlan_ns_exit(struct net
*net
)
1077 struct ipvlan_netns
*vnet
= net_generic(net
, ipvlan_netid
);
1079 if (WARN_ON_ONCE(vnet
->ipvl_nf_hook_refcnt
)) {
1080 vnet
->ipvl_nf_hook_refcnt
= 0;
1081 nf_unregister_net_hooks(net
, ipvl_nfops
,
1082 ARRAY_SIZE(ipvl_nfops
));
1086 static struct pernet_operations ipvlan_net_ops
= {
1087 .id
= &ipvlan_netid
,
1088 .size
= sizeof(struct ipvlan_netns
),
1089 .exit
= ipvlan_ns_exit
,
1092 static int __init
ipvlan_init_module(void)
1096 ipvlan_init_secret();
1097 register_netdevice_notifier(&ipvlan_notifier_block
);
1098 #if IS_ENABLED(CONFIG_IPV6)
1099 register_inet6addr_notifier(&ipvlan_addr6_notifier_block
);
1100 register_inet6addr_validator_notifier(
1101 &ipvlan_addr6_vtor_notifier_block
);
1103 register_inetaddr_notifier(&ipvlan_addr4_notifier_block
);
1104 register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block
);
1106 err
= register_pernet_subsys(&ipvlan_net_ops
);
1110 err
= ipvlan_link_register(&ipvlan_link_ops
);
1112 unregister_pernet_subsys(&ipvlan_net_ops
);
1118 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block
);
1119 unregister_inetaddr_validator_notifier(
1120 &ipvlan_addr4_vtor_notifier_block
);
1121 #if IS_ENABLED(CONFIG_IPV6)
1122 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block
);
1123 unregister_inet6addr_validator_notifier(
1124 &ipvlan_addr6_vtor_notifier_block
);
1126 unregister_netdevice_notifier(&ipvlan_notifier_block
);
1130 static void __exit
ipvlan_cleanup_module(void)
1132 rtnl_link_unregister(&ipvlan_link_ops
);
1133 unregister_pernet_subsys(&ipvlan_net_ops
);
1134 unregister_netdevice_notifier(&ipvlan_notifier_block
);
1135 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block
);
1136 unregister_inetaddr_validator_notifier(
1137 &ipvlan_addr4_vtor_notifier_block
);
1138 #if IS_ENABLED(CONFIG_IPV6)
1139 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block
);
1140 unregister_inet6addr_validator_notifier(
1141 &ipvlan_addr6_vtor_notifier_block
);
1145 module_init(ipvlan_init_module
);
1146 module_exit(ipvlan_cleanup_module
);
1148 MODULE_LICENSE("GPL");
1149 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
1150 MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
1151 MODULE_ALIAS_RTNL_LINK("ipvlan");