1b863ceb7SPatrick McHardy /* 2b863ceb7SPatrick McHardy * Copyright (c) 2007 Patrick McHardy <kaber@trash.net> 3b863ceb7SPatrick McHardy * 4b863ceb7SPatrick McHardy * This program is free software; you can redistribute it and/or 5b863ceb7SPatrick McHardy * modify it under the terms of the GNU General Public License as 6b863ceb7SPatrick McHardy * published by the Free Software Foundation; either version 2 of 7b863ceb7SPatrick McHardy * the License, or (at your option) any later version. 8b863ceb7SPatrick McHardy * 9b863ceb7SPatrick McHardy * The code this is based on carried the following copyright notice: 10b863ceb7SPatrick McHardy * --- 11b863ceb7SPatrick McHardy * (C) Copyright 2001-2006 12b863ceb7SPatrick McHardy * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com 13b863ceb7SPatrick McHardy * Re-worked by Ben Greear <greearb@candelatech.com> 14b863ceb7SPatrick McHardy * --- 15b863ceb7SPatrick McHardy */ 16b863ceb7SPatrick McHardy #include <linux/kernel.h> 17b863ceb7SPatrick McHardy #include <linux/types.h> 18b863ceb7SPatrick McHardy #include <linux/module.h> 19b863ceb7SPatrick McHardy #include <linux/init.h> 20b863ceb7SPatrick McHardy #include <linux/errno.h> 21b863ceb7SPatrick McHardy #include <linux/slab.h> 22b863ceb7SPatrick McHardy #include <linux/string.h> 23b863ceb7SPatrick McHardy #include <linux/list.h> 24b863ceb7SPatrick McHardy #include <linux/notifier.h> 25b863ceb7SPatrick McHardy #include <linux/netdevice.h> 26b863ceb7SPatrick McHardy #include <linux/etherdevice.h> 27b863ceb7SPatrick McHardy #include <linux/ethtool.h> 28b863ceb7SPatrick McHardy #include <linux/if_arp.h> 29b863ceb7SPatrick McHardy #include <linux/if_link.h> 30b863ceb7SPatrick McHardy #include <linux/if_macvlan.h> 31b863ceb7SPatrick McHardy #include <net/rtnetlink.h> 32b863ceb7SPatrick McHardy 33b863ceb7SPatrick McHardy #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE) 34b863ceb7SPatrick McHardy 35b863ceb7SPatrick McHardy struct macvlan_port { 36b863ceb7SPatrick McHardy struct net_device *dev; 37b863ceb7SPatrick McHardy struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; 38b863ceb7SPatrick McHardy struct list_head vlans; 39b863ceb7SPatrick McHardy }; 40b863ceb7SPatrick McHardy 41b863ceb7SPatrick McHardy struct macvlan_dev { 42b863ceb7SPatrick McHardy struct net_device *dev; 43b863ceb7SPatrick McHardy struct list_head list; 44b863ceb7SPatrick McHardy struct hlist_node hlist; 45b863ceb7SPatrick McHardy struct macvlan_port *port; 46b863ceb7SPatrick McHardy struct net_device *lowerdev; 47b863ceb7SPatrick McHardy }; 48b863ceb7SPatrick McHardy 49b863ceb7SPatrick McHardy 50b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, 51b863ceb7SPatrick McHardy const unsigned char *addr) 52b863ceb7SPatrick McHardy { 53b863ceb7SPatrick McHardy struct macvlan_dev *vlan; 54b863ceb7SPatrick McHardy struct hlist_node *n; 55b863ceb7SPatrick McHardy 56b863ceb7SPatrick McHardy hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) { 57b863ceb7SPatrick McHardy if (!compare_ether_addr(vlan->dev->dev_addr, addr)) 58b863ceb7SPatrick McHardy return vlan; 59b863ceb7SPatrick McHardy } 60b863ceb7SPatrick McHardy return NULL; 61b863ceb7SPatrick McHardy } 62b863ceb7SPatrick McHardy 63b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb, 64b863ceb7SPatrick McHardy const struct macvlan_port *port) 65b863ceb7SPatrick McHardy { 66b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 67b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 68b863ceb7SPatrick McHardy struct hlist_node *n; 69b863ceb7SPatrick McHardy struct net_device *dev; 70b863ceb7SPatrick McHardy struct sk_buff *nskb; 71b863ceb7SPatrick McHardy unsigned int i; 72b863ceb7SPatrick McHardy 73b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 74b863ceb7SPatrick McHardy hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { 75b863ceb7SPatrick McHardy dev = vlan->dev; 76b863ceb7SPatrick McHardy 77b863ceb7SPatrick McHardy nskb = skb_clone(skb, GFP_ATOMIC); 78b863ceb7SPatrick McHardy if (nskb == NULL) { 79b863ceb7SPatrick McHardy dev->stats.rx_errors++; 80b863ceb7SPatrick McHardy dev->stats.rx_dropped++; 81b863ceb7SPatrick McHardy continue; 82b863ceb7SPatrick McHardy } 83b863ceb7SPatrick McHardy 84b863ceb7SPatrick McHardy dev->stats.rx_bytes += skb->len + ETH_HLEN; 85b863ceb7SPatrick McHardy dev->stats.rx_packets++; 86b863ceb7SPatrick McHardy dev->stats.multicast++; 87b863ceb7SPatrick McHardy dev->last_rx = jiffies; 88b863ceb7SPatrick McHardy 89b863ceb7SPatrick McHardy nskb->dev = dev; 90b863ceb7SPatrick McHardy if (!compare_ether_addr(eth->h_dest, dev->broadcast)) 91b863ceb7SPatrick McHardy nskb->pkt_type = PACKET_BROADCAST; 92b863ceb7SPatrick McHardy else 93b863ceb7SPatrick McHardy nskb->pkt_type = PACKET_MULTICAST; 94b863ceb7SPatrick McHardy 95b863ceb7SPatrick McHardy netif_rx(nskb); 96b863ceb7SPatrick McHardy } 97b863ceb7SPatrick McHardy } 98b863ceb7SPatrick McHardy } 99b863ceb7SPatrick McHardy 100b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */ 101b863ceb7SPatrick McHardy static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb) 102b863ceb7SPatrick McHardy { 103b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 104b863ceb7SPatrick McHardy const struct macvlan_port *port; 105b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 106b863ceb7SPatrick McHardy struct net_device *dev; 107b863ceb7SPatrick McHardy 108b863ceb7SPatrick McHardy port = rcu_dereference(skb->dev->macvlan_port); 109b863ceb7SPatrick McHardy if (port == NULL) 110b863ceb7SPatrick McHardy return skb; 111b863ceb7SPatrick McHardy 112b863ceb7SPatrick McHardy if (is_multicast_ether_addr(eth->h_dest)) { 113b863ceb7SPatrick McHardy macvlan_broadcast(skb, port); 114b863ceb7SPatrick McHardy return skb; 115b863ceb7SPatrick McHardy } 116b863ceb7SPatrick McHardy 117b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 118b863ceb7SPatrick McHardy if (vlan == NULL) 119b863ceb7SPatrick McHardy return skb; 120b863ceb7SPatrick McHardy 121b863ceb7SPatrick McHardy dev = vlan->dev; 122b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 123b863ceb7SPatrick McHardy kfree_skb(skb); 124b863ceb7SPatrick McHardy return NULL; 125b863ceb7SPatrick McHardy } 126b863ceb7SPatrick McHardy 127b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 128b863ceb7SPatrick McHardy if (skb == NULL) { 129b863ceb7SPatrick McHardy dev->stats.rx_errors++; 130b863ceb7SPatrick McHardy dev->stats.rx_dropped++; 131b863ceb7SPatrick McHardy return NULL; 132b863ceb7SPatrick McHardy } 133b863ceb7SPatrick McHardy 134b863ceb7SPatrick McHardy dev->stats.rx_bytes += skb->len + ETH_HLEN; 135b863ceb7SPatrick McHardy dev->stats.rx_packets++; 136b863ceb7SPatrick McHardy dev->last_rx = jiffies; 137b863ceb7SPatrick McHardy 138b863ceb7SPatrick McHardy skb->dev = dev; 139b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 140b863ceb7SPatrick McHardy 141b863ceb7SPatrick McHardy netif_rx(skb); 142b863ceb7SPatrick McHardy return NULL; 143b863ceb7SPatrick McHardy } 144b863ceb7SPatrick McHardy 145b863ceb7SPatrick McHardy static int macvlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev) 146b863ceb7SPatrick McHardy { 147b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 148b863ceb7SPatrick McHardy unsigned int len = skb->len; 149b863ceb7SPatrick McHardy int ret; 150b863ceb7SPatrick McHardy 151b863ceb7SPatrick McHardy skb->dev = vlan->lowerdev; 152b863ceb7SPatrick McHardy ret = dev_queue_xmit(skb); 153b863ceb7SPatrick McHardy 154b863ceb7SPatrick McHardy if (likely(ret == NET_XMIT_SUCCESS)) { 155b863ceb7SPatrick McHardy dev->stats.tx_packets++; 156b863ceb7SPatrick McHardy dev->stats.tx_bytes += len; 157b863ceb7SPatrick McHardy } else { 158b863ceb7SPatrick McHardy dev->stats.tx_errors++; 159b863ceb7SPatrick McHardy dev->stats.tx_aborted_errors++; 160b863ceb7SPatrick McHardy } 161b863ceb7SPatrick McHardy return NETDEV_TX_OK; 162b863ceb7SPatrick McHardy } 163b863ceb7SPatrick McHardy 164b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 1653b04dddeSStephen Hemminger unsigned short type, const void *daddr, 1663b04dddeSStephen Hemminger const void *saddr, unsigned len) 167b863ceb7SPatrick McHardy { 168b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 169b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 170b863ceb7SPatrick McHardy 1710c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 172b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 173b863ceb7SPatrick McHardy } 174b863ceb7SPatrick McHardy 1753b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 1763b04dddeSStephen Hemminger .create = macvlan_hard_header, 1773b04dddeSStephen Hemminger .rebuild = eth_rebuild_header, 1783b04dddeSStephen Hemminger .parse = eth_header_parse, 1793b04dddeSStephen Hemminger .cache = eth_header_cache, 1803b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 1813b04dddeSStephen Hemminger }; 1823b04dddeSStephen Hemminger 183b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 184b863ceb7SPatrick McHardy { 185b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 186b863ceb7SPatrick McHardy struct macvlan_port *port = vlan->port; 187b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 188b863ceb7SPatrick McHardy int err; 189b863ceb7SPatrick McHardy 190b863ceb7SPatrick McHardy err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN); 191b863ceb7SPatrick McHardy if (err < 0) 192b863ceb7SPatrick McHardy return err; 193b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 194b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, 1); 195b863ceb7SPatrick McHardy 196b863ceb7SPatrick McHardy hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[dev->dev_addr[5]]); 197b863ceb7SPatrick McHardy return 0; 198b863ceb7SPatrick McHardy } 199b863ceb7SPatrick McHardy 200b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 201b863ceb7SPatrick McHardy { 202b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 203b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 204b863ceb7SPatrick McHardy 205b863ceb7SPatrick McHardy dev_mc_unsync(lowerdev, dev); 206b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 207b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 208b863ceb7SPatrick McHardy 209b863ceb7SPatrick McHardy dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); 210b863ceb7SPatrick McHardy 211b863ceb7SPatrick McHardy hlist_del_rcu(&vlan->hlist); 212b863ceb7SPatrick McHardy synchronize_rcu(); 213b863ceb7SPatrick McHardy return 0; 214b863ceb7SPatrick McHardy } 215b863ceb7SPatrick McHardy 216*ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p) 217*ad5d20a6SPatrick McHardy { 218*ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 219*ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 220*ad5d20a6SPatrick McHardy struct sockaddr *addr = p; 221*ad5d20a6SPatrick McHardy int err; 222*ad5d20a6SPatrick McHardy 223*ad5d20a6SPatrick McHardy if (!is_valid_ether_addr(addr->sa_data)) 224*ad5d20a6SPatrick McHardy return -EADDRNOTAVAIL; 225*ad5d20a6SPatrick McHardy 226*ad5d20a6SPatrick McHardy if (!(dev->flags & IFF_UP)) 227*ad5d20a6SPatrick McHardy goto out; 228*ad5d20a6SPatrick McHardy 229*ad5d20a6SPatrick McHardy err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN); 230*ad5d20a6SPatrick McHardy if (err < 0) 231*ad5d20a6SPatrick McHardy return err; 232*ad5d20a6SPatrick McHardy dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); 233*ad5d20a6SPatrick McHardy 234*ad5d20a6SPatrick McHardy out: 235*ad5d20a6SPatrick McHardy memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 236*ad5d20a6SPatrick McHardy return 0; 237*ad5d20a6SPatrick McHardy } 238*ad5d20a6SPatrick McHardy 239b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 240b863ceb7SPatrick McHardy { 241b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 242b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 243b863ceb7SPatrick McHardy 244b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 245b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 246b863ceb7SPatrick McHardy } 247b863ceb7SPatrick McHardy 248b863ceb7SPatrick McHardy static void macvlan_set_multicast_list(struct net_device *dev) 249b863ceb7SPatrick McHardy { 250b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 251b863ceb7SPatrick McHardy 252b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 253b863ceb7SPatrick McHardy } 254b863ceb7SPatrick McHardy 255b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 256b863ceb7SPatrick McHardy { 257b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 258b863ceb7SPatrick McHardy 259b863ceb7SPatrick McHardy if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) 260b863ceb7SPatrick McHardy return -EINVAL; 261b863ceb7SPatrick McHardy dev->mtu = new_mtu; 262b863ceb7SPatrick McHardy return 0; 263b863ceb7SPatrick McHardy } 264b863ceb7SPatrick McHardy 265b863ceb7SPatrick McHardy /* 266b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 267b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 268b863ceb7SPatrick McHardy * separate class since they always nest. 269b863ceb7SPatrick McHardy */ 270b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key; 271b863ceb7SPatrick McHardy 272b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 273b863ceb7SPatrick McHardy (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 274b863ceb7SPatrick McHardy NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ 275b863ceb7SPatrick McHardy NETIF_F_TSO_ECN | NETIF_F_TSO6) 276b863ceb7SPatrick McHardy 277b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 278b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 279b863ceb7SPatrick McHardy 280b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 281b863ceb7SPatrick McHardy { 282b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 283b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 284b863ceb7SPatrick McHardy 285b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 286b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 287b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 288b863ceb7SPatrick McHardy dev->iflink = lowerdev->ifindex; 289b863ceb7SPatrick McHardy 290b863ceb7SPatrick McHardy lockdep_set_class(&dev->_xmit_lock, &macvlan_netdev_xmit_lock_key); 291b863ceb7SPatrick McHardy return 0; 292b863ceb7SPatrick McHardy } 293b863ceb7SPatrick McHardy 294b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 295b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 296b863ceb7SPatrick McHardy { 297b863ceb7SPatrick McHardy snprintf(drvinfo->driver, 32, "macvlan"); 298b863ceb7SPatrick McHardy snprintf(drvinfo->version, 32, "0.1"); 299b863ceb7SPatrick McHardy } 300b863ceb7SPatrick McHardy 301b863ceb7SPatrick McHardy static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) 302b863ceb7SPatrick McHardy { 303b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 304b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 305b863ceb7SPatrick McHardy 306b863ceb7SPatrick McHardy if (lowerdev->ethtool_ops->get_rx_csum == NULL) 307b863ceb7SPatrick McHardy return 0; 308b863ceb7SPatrick McHardy return lowerdev->ethtool_ops->get_rx_csum(lowerdev); 309b863ceb7SPatrick McHardy } 310b863ceb7SPatrick McHardy 311b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 312b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 313b863ceb7SPatrick McHardy .get_rx_csum = macvlan_ethtool_get_rx_csum, 314b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 315b863ceb7SPatrick McHardy }; 316b863ceb7SPatrick McHardy 317b863ceb7SPatrick McHardy static void macvlan_setup(struct net_device *dev) 318b863ceb7SPatrick McHardy { 319b863ceb7SPatrick McHardy ether_setup(dev); 320b863ceb7SPatrick McHardy 321b863ceb7SPatrick McHardy dev->init = macvlan_init; 322b863ceb7SPatrick McHardy dev->open = macvlan_open; 323b863ceb7SPatrick McHardy dev->stop = macvlan_stop; 324b863ceb7SPatrick McHardy dev->change_mtu = macvlan_change_mtu; 325b863ceb7SPatrick McHardy dev->change_rx_flags = macvlan_change_rx_flags; 326*ad5d20a6SPatrick McHardy dev->set_mac_address = macvlan_set_mac_address; 327b863ceb7SPatrick McHardy dev->set_multicast_list = macvlan_set_multicast_list; 328b863ceb7SPatrick McHardy dev->hard_start_xmit = macvlan_hard_start_xmit; 329b863ceb7SPatrick McHardy dev->destructor = free_netdev; 3303b04dddeSStephen Hemminger dev->header_ops = &macvlan_hard_header_ops, 331b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 332b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 333b863ceb7SPatrick McHardy } 334b863ceb7SPatrick McHardy 335b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 336b863ceb7SPatrick McHardy { 337b863ceb7SPatrick McHardy struct macvlan_port *port; 338b863ceb7SPatrick McHardy unsigned int i; 339b863ceb7SPatrick McHardy 340b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 341b863ceb7SPatrick McHardy return -EINVAL; 342b863ceb7SPatrick McHardy 343b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 344b863ceb7SPatrick McHardy if (port == NULL) 345b863ceb7SPatrick McHardy return -ENOMEM; 346b863ceb7SPatrick McHardy 347b863ceb7SPatrick McHardy port->dev = dev; 348b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 349b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 350b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 351b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, port); 352b863ceb7SPatrick McHardy return 0; 353b863ceb7SPatrick McHardy } 354b863ceb7SPatrick McHardy 355b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 356b863ceb7SPatrick McHardy { 357b863ceb7SPatrick McHardy struct macvlan_port *port = dev->macvlan_port; 358b863ceb7SPatrick McHardy 359b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, NULL); 360b863ceb7SPatrick McHardy synchronize_rcu(); 361b863ceb7SPatrick McHardy kfree(port); 362b863ceb7SPatrick McHardy } 363b863ceb7SPatrick McHardy 364b863ceb7SPatrick McHardy static void macvlan_transfer_operstate(struct net_device *dev) 365b863ceb7SPatrick McHardy { 366b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 367b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 368b863ceb7SPatrick McHardy 369b863ceb7SPatrick McHardy if (lowerdev->operstate == IF_OPER_DORMANT) 370b863ceb7SPatrick McHardy netif_dormant_on(dev); 371b863ceb7SPatrick McHardy else 372b863ceb7SPatrick McHardy netif_dormant_off(dev); 373b863ceb7SPatrick McHardy 374b863ceb7SPatrick McHardy if (netif_carrier_ok(lowerdev)) { 375b863ceb7SPatrick McHardy if (!netif_carrier_ok(dev)) 376b863ceb7SPatrick McHardy netif_carrier_on(dev); 377b863ceb7SPatrick McHardy } else { 378b863ceb7SPatrick McHardy if (netif_carrier_ok(lowerdev)) 379b863ceb7SPatrick McHardy netif_carrier_off(dev); 380b863ceb7SPatrick McHardy } 381b863ceb7SPatrick McHardy } 382b863ceb7SPatrick McHardy 383b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 384b863ceb7SPatrick McHardy { 385b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 386b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 387b863ceb7SPatrick McHardy return -EINVAL; 388b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 389b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 390b863ceb7SPatrick McHardy } 391b863ceb7SPatrick McHardy return 0; 392b863ceb7SPatrick McHardy } 393b863ceb7SPatrick McHardy 394b863ceb7SPatrick McHardy static int macvlan_newlink(struct net_device *dev, 395b863ceb7SPatrick McHardy struct nlattr *tb[], struct nlattr *data[]) 396b863ceb7SPatrick McHardy { 397b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 398b863ceb7SPatrick McHardy struct macvlan_port *port; 399b863ceb7SPatrick McHardy struct net_device *lowerdev; 400b863ceb7SPatrick McHardy int err; 401b863ceb7SPatrick McHardy 402b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 403b863ceb7SPatrick McHardy return -EINVAL; 404b863ceb7SPatrick McHardy 405881d966bSEric W. Biederman lowerdev = __dev_get_by_index(dev->nd_net, nla_get_u32(tb[IFLA_LINK])); 406b863ceb7SPatrick McHardy if (lowerdev == NULL) 407b863ceb7SPatrick McHardy return -ENODEV; 408b863ceb7SPatrick McHardy 409a6ca5f1dSPatrick McHardy /* Don't allow macvlans on top of other macvlans - its not really 410a6ca5f1dSPatrick McHardy * wrong, but lockdep can't handle it and its not useful for anything 411a6ca5f1dSPatrick McHardy * you couldn't do directly on top of the real device. 412a6ca5f1dSPatrick McHardy */ 413a6ca5f1dSPatrick McHardy if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) 414a6ca5f1dSPatrick McHardy return -ENODEV; 415a6ca5f1dSPatrick McHardy 416b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 417b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 418b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 419b863ceb7SPatrick McHardy return -EINVAL; 420b863ceb7SPatrick McHardy 421b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 422b863ceb7SPatrick McHardy random_ether_addr(dev->dev_addr); 423b863ceb7SPatrick McHardy 424b863ceb7SPatrick McHardy if (lowerdev->macvlan_port == NULL) { 425b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 426b863ceb7SPatrick McHardy if (err < 0) 427b863ceb7SPatrick McHardy return err; 428b863ceb7SPatrick McHardy } 429b863ceb7SPatrick McHardy port = lowerdev->macvlan_port; 430b863ceb7SPatrick McHardy 431b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 432b863ceb7SPatrick McHardy vlan->dev = dev; 433b863ceb7SPatrick McHardy vlan->port = port; 434b863ceb7SPatrick McHardy 435b863ceb7SPatrick McHardy err = register_netdevice(dev); 436b863ceb7SPatrick McHardy if (err < 0) 437b863ceb7SPatrick McHardy return err; 438b863ceb7SPatrick McHardy 439b863ceb7SPatrick McHardy list_add_tail(&vlan->list, &port->vlans); 440b863ceb7SPatrick McHardy macvlan_transfer_operstate(dev); 441b863ceb7SPatrick McHardy return 0; 442b863ceb7SPatrick McHardy } 443b863ceb7SPatrick McHardy 444b863ceb7SPatrick McHardy static void macvlan_dellink(struct net_device *dev) 445b863ceb7SPatrick McHardy { 446b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 447b863ceb7SPatrick McHardy struct macvlan_port *port = vlan->port; 448b863ceb7SPatrick McHardy 449b863ceb7SPatrick McHardy list_del(&vlan->list); 450b863ceb7SPatrick McHardy unregister_netdevice(dev); 451b863ceb7SPatrick McHardy 452b863ceb7SPatrick McHardy if (list_empty(&port->vlans)) 453b863ceb7SPatrick McHardy macvlan_port_destroy(dev); 454b863ceb7SPatrick McHardy } 455b863ceb7SPatrick McHardy 456b863ceb7SPatrick McHardy static struct rtnl_link_ops macvlan_link_ops __read_mostly = { 457b863ceb7SPatrick McHardy .kind = "macvlan", 458b863ceb7SPatrick McHardy .priv_size = sizeof(struct macvlan_dev), 459b863ceb7SPatrick McHardy .setup = macvlan_setup, 460b863ceb7SPatrick McHardy .validate = macvlan_validate, 461b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 462b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 463b863ceb7SPatrick McHardy }; 464b863ceb7SPatrick McHardy 465b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 466b863ceb7SPatrick McHardy unsigned long event, void *ptr) 467b863ceb7SPatrick McHardy { 468b863ceb7SPatrick McHardy struct net_device *dev = ptr; 469b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 470b863ceb7SPatrick McHardy struct macvlan_port *port; 471b863ceb7SPatrick McHardy 472b863ceb7SPatrick McHardy port = dev->macvlan_port; 473b863ceb7SPatrick McHardy if (port == NULL) 474b863ceb7SPatrick McHardy return NOTIFY_DONE; 475b863ceb7SPatrick McHardy 476b863ceb7SPatrick McHardy switch (event) { 477b863ceb7SPatrick McHardy case NETDEV_CHANGE: 478b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 479b863ceb7SPatrick McHardy macvlan_transfer_operstate(vlan->dev); 480b863ceb7SPatrick McHardy break; 481b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 482b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 483b863ceb7SPatrick McHardy vlan->dev->features = dev->features & MACVLAN_FEATURES; 484b863ceb7SPatrick McHardy netdev_features_change(vlan->dev); 485b863ceb7SPatrick McHardy } 486b863ceb7SPatrick McHardy break; 487b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 488b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 489b863ceb7SPatrick McHardy macvlan_dellink(vlan->dev); 490b863ceb7SPatrick McHardy break; 491b863ceb7SPatrick McHardy } 492b863ceb7SPatrick McHardy return NOTIFY_DONE; 493b863ceb7SPatrick McHardy } 494b863ceb7SPatrick McHardy 495b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 496b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 497b863ceb7SPatrick McHardy }; 498b863ceb7SPatrick McHardy 499b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 500b863ceb7SPatrick McHardy { 501b863ceb7SPatrick McHardy int err; 502b863ceb7SPatrick McHardy 503b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 504b863ceb7SPatrick McHardy macvlan_handle_frame_hook = macvlan_handle_frame; 505b863ceb7SPatrick McHardy 506b863ceb7SPatrick McHardy err = rtnl_link_register(&macvlan_link_ops); 507b863ceb7SPatrick McHardy if (err < 0) 508b863ceb7SPatrick McHardy goto err1; 509b863ceb7SPatrick McHardy return 0; 510b863ceb7SPatrick McHardy err1: 511b863ceb7SPatrick McHardy macvlan_handle_frame_hook = macvlan_handle_frame; 512b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 513b863ceb7SPatrick McHardy return err; 514b863ceb7SPatrick McHardy } 515b863ceb7SPatrick McHardy 516b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 517b863ceb7SPatrick McHardy { 518b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 519b863ceb7SPatrick McHardy macvlan_handle_frame_hook = NULL; 520b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 521b863ceb7SPatrick McHardy } 522b863ceb7SPatrick McHardy 523b863ceb7SPatrick McHardy module_init(macvlan_init_module); 524b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 525b863ceb7SPatrick McHardy 526b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 527b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 528b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 529b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 530