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) 192*b89fb7daSWang Chen goto out; 193*b89fb7daSWang Chen if (dev->flags & IFF_ALLMULTI) { 194*b89fb7daSWang Chen err = dev_set_allmulti(lowerdev, 1); 195*b89fb7daSWang Chen if (err < 0) 196*b89fb7daSWang Chen goto del_unicast; 197*b89fb7daSWang Chen } 198b863ceb7SPatrick McHardy 199b863ceb7SPatrick McHardy hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[dev->dev_addr[5]]); 200b863ceb7SPatrick McHardy return 0; 201*b89fb7daSWang Chen 202*b89fb7daSWang Chen del_unicast: 203*b89fb7daSWang Chen dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); 204*b89fb7daSWang Chen out: 205*b89fb7daSWang Chen return err; 206b863ceb7SPatrick McHardy } 207b863ceb7SPatrick McHardy 208b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 209b863ceb7SPatrick McHardy { 210b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 211b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 212b863ceb7SPatrick McHardy 213b863ceb7SPatrick McHardy dev_mc_unsync(lowerdev, dev); 214b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 215b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 216b863ceb7SPatrick McHardy 217b863ceb7SPatrick McHardy dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); 218b863ceb7SPatrick McHardy 219b863ceb7SPatrick McHardy hlist_del_rcu(&vlan->hlist); 220b863ceb7SPatrick McHardy synchronize_rcu(); 221b863ceb7SPatrick McHardy return 0; 222b863ceb7SPatrick McHardy } 223b863ceb7SPatrick McHardy 224ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p) 225ad5d20a6SPatrick McHardy { 226ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 227ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 228ad5d20a6SPatrick McHardy struct sockaddr *addr = p; 229ad5d20a6SPatrick McHardy int err; 230ad5d20a6SPatrick McHardy 231ad5d20a6SPatrick McHardy if (!is_valid_ether_addr(addr->sa_data)) 232ad5d20a6SPatrick McHardy return -EADDRNOTAVAIL; 233ad5d20a6SPatrick McHardy 234ad5d20a6SPatrick McHardy if (!(dev->flags & IFF_UP)) 235ad5d20a6SPatrick McHardy goto out; 236ad5d20a6SPatrick McHardy 237ad5d20a6SPatrick McHardy err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN); 238ad5d20a6SPatrick McHardy if (err < 0) 239ad5d20a6SPatrick McHardy return err; 240ad5d20a6SPatrick McHardy dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); 241ad5d20a6SPatrick McHardy 242ad5d20a6SPatrick McHardy out: 243ad5d20a6SPatrick McHardy memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 244ad5d20a6SPatrick McHardy return 0; 245ad5d20a6SPatrick McHardy } 246ad5d20a6SPatrick McHardy 247b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 248b863ceb7SPatrick McHardy { 249b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 250b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 251b863ceb7SPatrick McHardy 252b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 253b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 254b863ceb7SPatrick McHardy } 255b863ceb7SPatrick McHardy 256b863ceb7SPatrick McHardy static void macvlan_set_multicast_list(struct net_device *dev) 257b863ceb7SPatrick McHardy { 258b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 259b863ceb7SPatrick McHardy 260b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 261b863ceb7SPatrick McHardy } 262b863ceb7SPatrick McHardy 263b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 264b863ceb7SPatrick McHardy { 265b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 266b863ceb7SPatrick McHardy 267b863ceb7SPatrick McHardy if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) 268b863ceb7SPatrick McHardy return -EINVAL; 269b863ceb7SPatrick McHardy dev->mtu = new_mtu; 270b863ceb7SPatrick McHardy return 0; 271b863ceb7SPatrick McHardy } 272b863ceb7SPatrick McHardy 273b863ceb7SPatrick McHardy /* 274b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 275b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 276b863ceb7SPatrick McHardy * separate class since they always nest. 277b863ceb7SPatrick McHardy */ 278b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key; 279b863ceb7SPatrick McHardy 280b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 281b863ceb7SPatrick McHardy (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 282b863ceb7SPatrick McHardy NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ 283b863ceb7SPatrick McHardy NETIF_F_TSO_ECN | NETIF_F_TSO6) 284b863ceb7SPatrick McHardy 285b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 286b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 287b863ceb7SPatrick McHardy 288c773e847SDavid S. Miller static void macvlan_set_lockdep_class_one(struct netdev_queue *txq) 289c773e847SDavid S. Miller { 290c773e847SDavid S. Miller lockdep_set_class(&txq->_xmit_lock, 291c773e847SDavid S. Miller &macvlan_netdev_xmit_lock_key); 292c773e847SDavid S. Miller } 293c773e847SDavid S. Miller 294c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 295c773e847SDavid S. Miller { 296c773e847SDavid S. Miller macvlan_set_lockdep_class_one(&dev->tx_queue); 297c773e847SDavid S. Miller } 298c773e847SDavid S. Miller 299b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 300b863ceb7SPatrick McHardy { 301b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 302b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 303b863ceb7SPatrick McHardy 304b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 305b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 306b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 307b863ceb7SPatrick McHardy dev->iflink = lowerdev->ifindex; 308b863ceb7SPatrick McHardy 309c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 310c773e847SDavid S. Miller 311b863ceb7SPatrick McHardy return 0; 312b863ceb7SPatrick McHardy } 313b863ceb7SPatrick McHardy 314b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 315b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 316b863ceb7SPatrick McHardy { 317b863ceb7SPatrick McHardy snprintf(drvinfo->driver, 32, "macvlan"); 318b863ceb7SPatrick McHardy snprintf(drvinfo->version, 32, "0.1"); 319b863ceb7SPatrick McHardy } 320b863ceb7SPatrick McHardy 321b863ceb7SPatrick McHardy static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) 322b863ceb7SPatrick McHardy { 323b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 324b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 325b863ceb7SPatrick McHardy 326b863ceb7SPatrick McHardy if (lowerdev->ethtool_ops->get_rx_csum == NULL) 327b863ceb7SPatrick McHardy return 0; 328b863ceb7SPatrick McHardy return lowerdev->ethtool_ops->get_rx_csum(lowerdev); 329b863ceb7SPatrick McHardy } 330b863ceb7SPatrick McHardy 331b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 332b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 333b863ceb7SPatrick McHardy .get_rx_csum = macvlan_ethtool_get_rx_csum, 334b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 335b863ceb7SPatrick McHardy }; 336b863ceb7SPatrick McHardy 337b863ceb7SPatrick McHardy static void macvlan_setup(struct net_device *dev) 338b863ceb7SPatrick McHardy { 339b863ceb7SPatrick McHardy ether_setup(dev); 340b863ceb7SPatrick McHardy 341b863ceb7SPatrick McHardy dev->init = macvlan_init; 342b863ceb7SPatrick McHardy dev->open = macvlan_open; 343b863ceb7SPatrick McHardy dev->stop = macvlan_stop; 344b863ceb7SPatrick McHardy dev->change_mtu = macvlan_change_mtu; 345b863ceb7SPatrick McHardy dev->change_rx_flags = macvlan_change_rx_flags; 346ad5d20a6SPatrick McHardy dev->set_mac_address = macvlan_set_mac_address; 347b863ceb7SPatrick McHardy dev->set_multicast_list = macvlan_set_multicast_list; 348b863ceb7SPatrick McHardy dev->hard_start_xmit = macvlan_hard_start_xmit; 349b863ceb7SPatrick McHardy dev->destructor = free_netdev; 3503b04dddeSStephen Hemminger dev->header_ops = &macvlan_hard_header_ops, 351b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 352b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 353b863ceb7SPatrick McHardy } 354b863ceb7SPatrick McHardy 355b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 356b863ceb7SPatrick McHardy { 357b863ceb7SPatrick McHardy struct macvlan_port *port; 358b863ceb7SPatrick McHardy unsigned int i; 359b863ceb7SPatrick McHardy 360b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 361b863ceb7SPatrick McHardy return -EINVAL; 362b863ceb7SPatrick McHardy 363b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 364b863ceb7SPatrick McHardy if (port == NULL) 365b863ceb7SPatrick McHardy return -ENOMEM; 366b863ceb7SPatrick McHardy 367b863ceb7SPatrick McHardy port->dev = dev; 368b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 369b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 370b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 371b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, port); 372b863ceb7SPatrick McHardy return 0; 373b863ceb7SPatrick McHardy } 374b863ceb7SPatrick McHardy 375b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 376b863ceb7SPatrick McHardy { 377b863ceb7SPatrick McHardy struct macvlan_port *port = dev->macvlan_port; 378b863ceb7SPatrick McHardy 379b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, NULL); 380b863ceb7SPatrick McHardy synchronize_rcu(); 381b863ceb7SPatrick McHardy kfree(port); 382b863ceb7SPatrick McHardy } 383b863ceb7SPatrick McHardy 384b863ceb7SPatrick McHardy static void macvlan_transfer_operstate(struct net_device *dev) 385b863ceb7SPatrick McHardy { 386b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 387b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 388b863ceb7SPatrick McHardy 389b863ceb7SPatrick McHardy if (lowerdev->operstate == IF_OPER_DORMANT) 390b863ceb7SPatrick McHardy netif_dormant_on(dev); 391b863ceb7SPatrick McHardy else 392b863ceb7SPatrick McHardy netif_dormant_off(dev); 393b863ceb7SPatrick McHardy 394b863ceb7SPatrick McHardy if (netif_carrier_ok(lowerdev)) { 395b863ceb7SPatrick McHardy if (!netif_carrier_ok(dev)) 396b863ceb7SPatrick McHardy netif_carrier_on(dev); 397b863ceb7SPatrick McHardy } else { 398f12ca5f9SPatrick McHardy if (netif_carrier_ok(dev)) 399b863ceb7SPatrick McHardy netif_carrier_off(dev); 400b863ceb7SPatrick McHardy } 401b863ceb7SPatrick McHardy } 402b863ceb7SPatrick McHardy 403b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 404b863ceb7SPatrick McHardy { 405b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 406b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 407b863ceb7SPatrick McHardy return -EINVAL; 408b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 409b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 410b863ceb7SPatrick McHardy } 411b863ceb7SPatrick McHardy return 0; 412b863ceb7SPatrick McHardy } 413b863ceb7SPatrick McHardy 414b863ceb7SPatrick McHardy static int macvlan_newlink(struct net_device *dev, 415b863ceb7SPatrick McHardy struct nlattr *tb[], struct nlattr *data[]) 416b863ceb7SPatrick McHardy { 417b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 418b863ceb7SPatrick McHardy struct macvlan_port *port; 419b863ceb7SPatrick McHardy struct net_device *lowerdev; 420b863ceb7SPatrick McHardy int err; 421b863ceb7SPatrick McHardy 422b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 423b863ceb7SPatrick McHardy return -EINVAL; 424b863ceb7SPatrick McHardy 425c346dca1SYOSHIFUJI Hideaki lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK])); 426b863ceb7SPatrick McHardy if (lowerdev == NULL) 427b863ceb7SPatrick McHardy return -ENODEV; 428b863ceb7SPatrick McHardy 429a6ca5f1dSPatrick McHardy /* Don't allow macvlans on top of other macvlans - its not really 430a6ca5f1dSPatrick McHardy * wrong, but lockdep can't handle it and its not useful for anything 431a6ca5f1dSPatrick McHardy * you couldn't do directly on top of the real device. 432a6ca5f1dSPatrick McHardy */ 433a6ca5f1dSPatrick McHardy if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) 434a6ca5f1dSPatrick McHardy return -ENODEV; 435a6ca5f1dSPatrick McHardy 436b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 437b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 438b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 439b863ceb7SPatrick McHardy return -EINVAL; 440b863ceb7SPatrick McHardy 441b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 442b863ceb7SPatrick McHardy random_ether_addr(dev->dev_addr); 443b863ceb7SPatrick McHardy 444b863ceb7SPatrick McHardy if (lowerdev->macvlan_port == NULL) { 445b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 446b863ceb7SPatrick McHardy if (err < 0) 447b863ceb7SPatrick McHardy return err; 448b863ceb7SPatrick McHardy } 449b863ceb7SPatrick McHardy port = lowerdev->macvlan_port; 450b863ceb7SPatrick McHardy 451b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 452b863ceb7SPatrick McHardy vlan->dev = dev; 453b863ceb7SPatrick McHardy vlan->port = port; 454b863ceb7SPatrick McHardy 455b863ceb7SPatrick McHardy err = register_netdevice(dev); 456b863ceb7SPatrick McHardy if (err < 0) 457b863ceb7SPatrick McHardy return err; 458b863ceb7SPatrick McHardy 459b863ceb7SPatrick McHardy list_add_tail(&vlan->list, &port->vlans); 460b863ceb7SPatrick McHardy macvlan_transfer_operstate(dev); 461b863ceb7SPatrick McHardy return 0; 462b863ceb7SPatrick McHardy } 463b863ceb7SPatrick McHardy 464b863ceb7SPatrick McHardy static void macvlan_dellink(struct net_device *dev) 465b863ceb7SPatrick McHardy { 466b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 467b863ceb7SPatrick McHardy struct macvlan_port *port = vlan->port; 468b863ceb7SPatrick McHardy 469b863ceb7SPatrick McHardy list_del(&vlan->list); 470b863ceb7SPatrick McHardy unregister_netdevice(dev); 471b863ceb7SPatrick McHardy 472b863ceb7SPatrick McHardy if (list_empty(&port->vlans)) 47373120964SPatrick McHardy macvlan_port_destroy(port->dev); 474b863ceb7SPatrick McHardy } 475b863ceb7SPatrick McHardy 476b863ceb7SPatrick McHardy static struct rtnl_link_ops macvlan_link_ops __read_mostly = { 477b863ceb7SPatrick McHardy .kind = "macvlan", 478b863ceb7SPatrick McHardy .priv_size = sizeof(struct macvlan_dev), 479b863ceb7SPatrick McHardy .setup = macvlan_setup, 480b863ceb7SPatrick McHardy .validate = macvlan_validate, 481b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 482b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 483b863ceb7SPatrick McHardy }; 484b863ceb7SPatrick McHardy 485b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 486b863ceb7SPatrick McHardy unsigned long event, void *ptr) 487b863ceb7SPatrick McHardy { 488b863ceb7SPatrick McHardy struct net_device *dev = ptr; 489b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 490b863ceb7SPatrick McHardy struct macvlan_port *port; 491b863ceb7SPatrick McHardy 492b863ceb7SPatrick McHardy port = dev->macvlan_port; 493b863ceb7SPatrick McHardy if (port == NULL) 494b863ceb7SPatrick McHardy return NOTIFY_DONE; 495b863ceb7SPatrick McHardy 496b863ceb7SPatrick McHardy switch (event) { 497b863ceb7SPatrick McHardy case NETDEV_CHANGE: 498b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 499b863ceb7SPatrick McHardy macvlan_transfer_operstate(vlan->dev); 500b863ceb7SPatrick McHardy break; 501b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 502b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 503b863ceb7SPatrick McHardy vlan->dev->features = dev->features & MACVLAN_FEATURES; 504b863ceb7SPatrick McHardy netdev_features_change(vlan->dev); 505b863ceb7SPatrick McHardy } 506b863ceb7SPatrick McHardy break; 507b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 508b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 509b863ceb7SPatrick McHardy macvlan_dellink(vlan->dev); 510b863ceb7SPatrick McHardy break; 511b863ceb7SPatrick McHardy } 512b863ceb7SPatrick McHardy return NOTIFY_DONE; 513b863ceb7SPatrick McHardy } 514b863ceb7SPatrick McHardy 515b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 516b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 517b863ceb7SPatrick McHardy }; 518b863ceb7SPatrick McHardy 519b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 520b863ceb7SPatrick McHardy { 521b863ceb7SPatrick McHardy int err; 522b863ceb7SPatrick McHardy 523b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 524b863ceb7SPatrick McHardy macvlan_handle_frame_hook = macvlan_handle_frame; 525b863ceb7SPatrick McHardy 526b863ceb7SPatrick McHardy err = rtnl_link_register(&macvlan_link_ops); 527b863ceb7SPatrick McHardy if (err < 0) 528b863ceb7SPatrick McHardy goto err1; 529b863ceb7SPatrick McHardy return 0; 530b863ceb7SPatrick McHardy err1: 53152913246SRami Rosen macvlan_handle_frame_hook = NULL; 532b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 533b863ceb7SPatrick McHardy return err; 534b863ceb7SPatrick McHardy } 535b863ceb7SPatrick McHardy 536b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 537b863ceb7SPatrick McHardy { 538b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 539b863ceb7SPatrick McHardy macvlan_handle_frame_hook = NULL; 540b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 541b863ceb7SPatrick McHardy } 542b863ceb7SPatrick McHardy 543b863ceb7SPatrick McHardy module_init(macvlan_init_module); 544b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 545b863ceb7SPatrick McHardy 546b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 547b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 548b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 549b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 550