16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
25 #include <linux/netdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_vlan.h>
30 #include <linux/if_link.h>
35 #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
48 #define macvlan_port_get_rcu(dev) \
49 ((struct macvlan_port *) rcu_dereference(dev->rx_handler_data))
50 #define macvlan_port_get(dev) ((struct macvlan_port *) dev->rx_handler_data)
51 #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
54 const unsigned char *
addr)
59 hlist_for_each_entry_rcu(vlan, n, &port->
vlan_hash[addr[5]],
hlist) {
60 if (ether_addr_equal_64bits(vlan->
dev->dev_addr, addr))
69 const unsigned char *
addr = vlan->
dev->dev_addr;
76 hlist_del_rcu(&vlan->
hlist);
81 static void macvlan_hash_change_addr(
struct macvlan_dev *vlan,
82 const unsigned char *
addr)
84 macvlan_hash_del(vlan,
true);
89 macvlan_hash_add(vlan);
93 const unsigned char *
addr)
99 if (ether_addr_equal_64bits(port->
dev->dev_addr, addr))
102 if (macvlan_hash_lookup(port, addr))
109 static int macvlan_broadcast_one(
struct sk_buff *
skb,
111 const struct ethhdr *eth,
bool local)
118 return vlan->
forward(dev, skb);
129 static void macvlan_broadcast(
struct sk_buff *skb,
134 const struct ethhdr *eth = eth_hdr(skb);
146 if (vlan->
dev == src || !(vlan->
mode & mode))
150 err = macvlan_broadcast_one(nskb, vlan, eth,
163 const struct ethhdr *eth = eth_hdr(skb);
167 unsigned int len = 0;
171 if (is_multicast_ether_addr(eth->
h_dest)) {
176 src = macvlan_hash_lookup(port, eth->
h_source);
179 macvlan_broadcast(skb, port,
NULL,
186 macvlan_broadcast(skb, port, src->
dev,
194 macvlan_broadcast(skb, port, src->
dev,
199 ret = macvlan_broadcast_one(skb, vlan, eth, 0);
209 vlan = macvlan_hash_lookup(port, eth->
h_dest);
245 if (is_multicast_ether_addr(eth->
h_dest)) {
250 dest = macvlan_hash_lookup(port, eth->
h_dest);
268 unsigned int len = skb->
len;
272 ret = macvlan_queue_xmit(skb, dev);
277 u64_stats_update_begin(&pcpu_stats->
syncp);
280 u64_stats_update_end(&pcpu_stats->
syncp);
290 const void *
saddr,
unsigned len)
295 return dev_hard_header(skb, lowerdev, type, daddr,
299 static const struct header_ops macvlan_hard_header_ops = {
300 .create = macvlan_hard_header,
307 static int macvlan_open(
struct net_device *dev)
313 if (vlan->
port->passthru) {
333 macvlan_hash_add(vlan);
342 static int macvlan_stop(
struct net_device *dev)
350 if (vlan->
port->passthru) {
366 static int macvlan_set_mac_address(
struct net_device *dev,
void *
p)
373 if (!is_valid_ether_addr(addr->
sa_data))
391 macvlan_hash_change_addr(vlan, addr->
sa_data);
396 static void macvlan_change_rx_flags(
struct net_device *dev,
int change)
405 static void macvlan_set_mac_lists(
struct net_device *dev)
413 static int macvlan_change_mtu(
struct net_device *dev,
int new_mtu)
417 if (new_mtu < 68 || vlan->lowerdev->
mtu < new_mtu)
431 #define MACVLAN_FEATURES \
432 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
433 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
434 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
435 NETIF_F_HW_VLAN_FILTER)
437 #define MACVLAN_STATE_MASK \
438 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
440 static void macvlan_set_lockdep_class_one(
struct net_device *dev,
445 &macvlan_netdev_xmit_lock_key);
448 static void macvlan_set_lockdep_class(
struct net_device *dev)
451 &macvlan_netdev_addr_lock_key);
452 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one,
NULL);
455 static int macvlan_init(
struct net_device *dev)
468 macvlan_set_lockdep_class(dev);
477 static void macvlan_uninit(
struct net_device *dev)
486 macvlan_port_destroy(port->
dev);
504 start = u64_stats_fetch_begin_bh(&p->
syncp);
510 }
while (u64_stats_fetch_retry_bh(&p->
syncp, start));
530 static int macvlan_vlan_rx_add_vid(
struct net_device *dev,
539 static int macvlan_vlan_rx_kill_vid(
struct net_device *dev,
549 static int macvlan_fdb_add(
struct ndmsg *ndm,
struct nlattr *
tb[],
551 const unsigned char *addr,
557 if (!vlan->
port->passthru)
560 if (is_unicast_ether_addr(addr))
562 else if (is_multicast_ether_addr(addr))
568 static int macvlan_fdb_del(
struct ndmsg *ndm,
570 const unsigned char *addr)
575 if (!vlan->
port->passthru)
578 if (is_unicast_ether_addr(addr))
580 else if (is_multicast_ether_addr(addr))
586 static void macvlan_ethtool_get_drvinfo(
struct net_device *dev,
593 static int macvlan_ethtool_get_settings(
struct net_device *dev,
601 static const struct ethtool_ops macvlan_ethtool_ops = {
603 .get_settings = macvlan_ethtool_get_settings,
604 .get_drvinfo = macvlan_ethtool_get_drvinfo,
608 .ndo_init = macvlan_init,
609 .ndo_uninit = macvlan_uninit,
610 .ndo_open = macvlan_open,
611 .ndo_stop = macvlan_stop,
613 .ndo_change_mtu = macvlan_change_mtu,
614 .ndo_change_rx_flags = macvlan_change_rx_flags,
615 .ndo_set_mac_address = macvlan_set_mac_address,
616 .ndo_set_rx_mode = macvlan_set_mac_lists,
617 .ndo_get_stats64 = macvlan_dev_get_stats64,
619 .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid,
620 .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid,
621 .ndo_fdb_add = macvlan_fdb_add,
622 .ndo_fdb_del = macvlan_fdb_del,
638 static void macvlan_setup(
struct net_device *dev)
644 static int macvlan_port_create(
struct net_device *dev)
659 INIT_LIST_HEAD(&port->
vlans);
671 static void macvlan_port_destroy(
struct net_device *dev)
683 if (nla_len(tb[IFLA_ADDRESS]) !=
ETH_ALEN)
685 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
690 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
705 int (*receive)(
struct sk_buff *skb),
718 if (lowerdev ==
NULL)
725 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
731 else if (dev->
mtu > lowerdev->
mtu)
734 if (!tb[IFLA_ADDRESS])
735 eth_hw_addr_random(dev);
738 err = macvlan_port_create(lowerdev);
755 if (data && data[IFLA_MACVLAN_MODE])
756 vlan->
mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
759 vlan->
flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
781 macvlan_port_destroy(lowerdev);
787 static int macvlan_newlink(
struct net *src_net,
struct net_device *dev,
804 static int macvlan_changelink(
struct net_device *dev,
808 if (data && data[IFLA_MACVLAN_MODE])
809 vlan->
mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
811 __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
816 else if (promisc && !(flags & MACVLAN_FLAG_NOPROMISC))
823 static size_t macvlan_get_size(
const struct net_device *dev)
825 return nla_total_size(4);
828 static int macvlan_fill_info(
struct sk_buff *skb,
833 if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->
mode))
834 goto nla_put_failure;
835 if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->
flags))
836 goto nla_put_failure;
854 ops->
policy = macvlan_policy;
865 .setup = macvlan_setup,
866 .newlink = macvlan_newlink,
892 vlan->dev->gso_max_size = dev->gso_max_size;
898 if (dev->
reg_state != NETREG_UNREGISTERING)
914 .notifier_call = macvlan_device_event,
917 static int __init macvlan_init_module(
void)
932 static void __exit macvlan_cleanup_module(
void)