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> 2382524746SFranck Bui-Huu #include <linux/rculist.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 63f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan) 64f9ac30f0SEric Biederman { 65f9ac30f0SEric Biederman struct macvlan_port *port = vlan->port; 66f9ac30f0SEric Biederman const unsigned char *addr = vlan->dev->dev_addr; 67f9ac30f0SEric Biederman 68f9ac30f0SEric Biederman hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); 69f9ac30f0SEric Biederman } 70f9ac30f0SEric Biederman 71f9ac30f0SEric Biederman static void macvlan_hash_del(struct macvlan_dev *vlan) 72f9ac30f0SEric Biederman { 73f9ac30f0SEric Biederman hlist_del_rcu(&vlan->hlist); 74f9ac30f0SEric Biederman synchronize_rcu(); 75f9ac30f0SEric Biederman } 76f9ac30f0SEric Biederman 77f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan, 78f9ac30f0SEric Biederman const unsigned char *addr) 79f9ac30f0SEric Biederman { 80f9ac30f0SEric Biederman macvlan_hash_del(vlan); 81f9ac30f0SEric Biederman /* Now that we are unhashed it is safe to change the device 82f9ac30f0SEric Biederman * address without confusing packet delivery. 83f9ac30f0SEric Biederman */ 84f9ac30f0SEric Biederman memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); 85f9ac30f0SEric Biederman macvlan_hash_add(vlan); 86f9ac30f0SEric Biederman } 87f9ac30f0SEric Biederman 88f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port, 89f9ac30f0SEric Biederman const unsigned char *addr) 90f9ac30f0SEric Biederman { 91f9ac30f0SEric Biederman /* Test to see if the specified multicast address is 92f9ac30f0SEric Biederman * currently in use by the underlying device or 93f9ac30f0SEric Biederman * another macvlan. 94f9ac30f0SEric Biederman */ 95f9ac30f0SEric Biederman if (memcmp(port->dev->dev_addr, addr, ETH_ALEN) == 0) 96f9ac30f0SEric Biederman return 1; 97f9ac30f0SEric Biederman 98f9ac30f0SEric Biederman if (macvlan_hash_lookup(port, addr)) 99f9ac30f0SEric Biederman return 1; 100f9ac30f0SEric Biederman 101f9ac30f0SEric Biederman return 0; 102f9ac30f0SEric Biederman } 103f9ac30f0SEric Biederman 104b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb, 105b863ceb7SPatrick McHardy const struct macvlan_port *port) 106b863ceb7SPatrick McHardy { 107b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 108b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 109b863ceb7SPatrick McHardy struct hlist_node *n; 110b863ceb7SPatrick McHardy struct net_device *dev; 111b863ceb7SPatrick McHardy struct sk_buff *nskb; 112b863ceb7SPatrick McHardy unsigned int i; 113b863ceb7SPatrick McHardy 114efbbced3SPatrick McHardy if (skb->protocol == htons(ETH_P_PAUSE)) 115efbbced3SPatrick McHardy return; 116efbbced3SPatrick McHardy 117b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 118b863ceb7SPatrick McHardy hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { 119b863ceb7SPatrick McHardy dev = vlan->dev; 120b863ceb7SPatrick McHardy 121b863ceb7SPatrick McHardy nskb = skb_clone(skb, GFP_ATOMIC); 122b863ceb7SPatrick McHardy if (nskb == NULL) { 123b863ceb7SPatrick McHardy dev->stats.rx_errors++; 124b863ceb7SPatrick McHardy dev->stats.rx_dropped++; 125b863ceb7SPatrick McHardy continue; 126b863ceb7SPatrick McHardy } 127b863ceb7SPatrick McHardy 128b863ceb7SPatrick McHardy dev->stats.rx_bytes += skb->len + ETH_HLEN; 129b863ceb7SPatrick McHardy dev->stats.rx_packets++; 130b863ceb7SPatrick McHardy dev->stats.multicast++; 131b863ceb7SPatrick McHardy 132b863ceb7SPatrick McHardy nskb->dev = dev; 133b863ceb7SPatrick McHardy if (!compare_ether_addr(eth->h_dest, dev->broadcast)) 134b863ceb7SPatrick McHardy nskb->pkt_type = PACKET_BROADCAST; 135b863ceb7SPatrick McHardy else 136b863ceb7SPatrick McHardy nskb->pkt_type = PACKET_MULTICAST; 137b863ceb7SPatrick McHardy 138b863ceb7SPatrick McHardy netif_rx(nskb); 139b863ceb7SPatrick McHardy } 140b863ceb7SPatrick McHardy } 141b863ceb7SPatrick McHardy } 142b863ceb7SPatrick McHardy 143b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */ 144b863ceb7SPatrick McHardy static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb) 145b863ceb7SPatrick McHardy { 146b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 147b863ceb7SPatrick McHardy const struct macvlan_port *port; 148b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 149b863ceb7SPatrick McHardy struct net_device *dev; 150b863ceb7SPatrick McHardy 151b863ceb7SPatrick McHardy port = rcu_dereference(skb->dev->macvlan_port); 152b863ceb7SPatrick McHardy if (port == NULL) 153b863ceb7SPatrick McHardy return skb; 154b863ceb7SPatrick McHardy 155b863ceb7SPatrick McHardy if (is_multicast_ether_addr(eth->h_dest)) { 156b863ceb7SPatrick McHardy macvlan_broadcast(skb, port); 157b863ceb7SPatrick McHardy return skb; 158b863ceb7SPatrick McHardy } 159b863ceb7SPatrick McHardy 160b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 161b863ceb7SPatrick McHardy if (vlan == NULL) 162b863ceb7SPatrick McHardy return skb; 163b863ceb7SPatrick McHardy 164b863ceb7SPatrick McHardy dev = vlan->dev; 165b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 166b863ceb7SPatrick McHardy kfree_skb(skb); 167b863ceb7SPatrick McHardy return NULL; 168b863ceb7SPatrick McHardy } 169b863ceb7SPatrick McHardy 170b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 171b863ceb7SPatrick McHardy if (skb == NULL) { 172b863ceb7SPatrick McHardy dev->stats.rx_errors++; 173b863ceb7SPatrick McHardy dev->stats.rx_dropped++; 174b863ceb7SPatrick McHardy return NULL; 175b863ceb7SPatrick McHardy } 176b863ceb7SPatrick McHardy 177b863ceb7SPatrick McHardy dev->stats.rx_bytes += skb->len + ETH_HLEN; 178b863ceb7SPatrick McHardy dev->stats.rx_packets++; 179b863ceb7SPatrick McHardy 180b863ceb7SPatrick McHardy skb->dev = dev; 181b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 182b863ceb7SPatrick McHardy 183b863ceb7SPatrick McHardy netif_rx(skb); 184b863ceb7SPatrick McHardy return NULL; 185b863ceb7SPatrick McHardy } 186b863ceb7SPatrick McHardy 18700829823SStephen Hemminger static int macvlan_start_xmit(struct sk_buff *skb, struct net_device *dev) 188b863ceb7SPatrick McHardy { 189b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 190b863ceb7SPatrick McHardy unsigned int len = skb->len; 191b863ceb7SPatrick McHardy int ret; 192b863ceb7SPatrick McHardy 193b863ceb7SPatrick McHardy skb->dev = vlan->lowerdev; 194b863ceb7SPatrick McHardy ret = dev_queue_xmit(skb); 195b863ceb7SPatrick McHardy 196b863ceb7SPatrick McHardy if (likely(ret == NET_XMIT_SUCCESS)) { 197b863ceb7SPatrick McHardy dev->stats.tx_packets++; 198b863ceb7SPatrick McHardy dev->stats.tx_bytes += len; 199b863ceb7SPatrick McHardy } else { 200b863ceb7SPatrick McHardy dev->stats.tx_errors++; 201b863ceb7SPatrick McHardy dev->stats.tx_aborted_errors++; 202b863ceb7SPatrick McHardy } 203b863ceb7SPatrick McHardy return NETDEV_TX_OK; 204b863ceb7SPatrick McHardy } 205b863ceb7SPatrick McHardy 206b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 2073b04dddeSStephen Hemminger unsigned short type, const void *daddr, 2083b04dddeSStephen Hemminger const void *saddr, unsigned len) 209b863ceb7SPatrick McHardy { 210b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 211b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 212b863ceb7SPatrick McHardy 2130c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 214b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 215b863ceb7SPatrick McHardy } 216b863ceb7SPatrick McHardy 2173b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 2183b04dddeSStephen Hemminger .create = macvlan_hard_header, 2193b04dddeSStephen Hemminger .rebuild = eth_rebuild_header, 2203b04dddeSStephen Hemminger .parse = eth_header_parse, 2213b04dddeSStephen Hemminger .cache = eth_header_cache, 2223b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 2233b04dddeSStephen Hemminger }; 2243b04dddeSStephen Hemminger 225b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 226b863ceb7SPatrick McHardy { 227b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 228b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 229b863ceb7SPatrick McHardy int err; 230b863ceb7SPatrick McHardy 231f9ac30f0SEric Biederman err = -EBUSY; 232f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, dev->dev_addr)) 233f9ac30f0SEric Biederman goto out; 234f9ac30f0SEric Biederman 235b863ceb7SPatrick McHardy err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN); 236b863ceb7SPatrick McHardy if (err < 0) 237b89fb7daSWang Chen goto out; 238b89fb7daSWang Chen if (dev->flags & IFF_ALLMULTI) { 239b89fb7daSWang Chen err = dev_set_allmulti(lowerdev, 1); 240b89fb7daSWang Chen if (err < 0) 241b89fb7daSWang Chen goto del_unicast; 242b89fb7daSWang Chen } 243f9ac30f0SEric Biederman macvlan_hash_add(vlan); 244b863ceb7SPatrick McHardy return 0; 245b89fb7daSWang Chen 246b89fb7daSWang Chen del_unicast: 247b89fb7daSWang Chen dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); 248b89fb7daSWang Chen out: 249b89fb7daSWang Chen return err; 250b863ceb7SPatrick McHardy } 251b863ceb7SPatrick McHardy 252b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 253b863ceb7SPatrick McHardy { 254b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 255b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 256b863ceb7SPatrick McHardy 257b863ceb7SPatrick McHardy dev_mc_unsync(lowerdev, dev); 258b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 259b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 260b863ceb7SPatrick McHardy 261b863ceb7SPatrick McHardy dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); 262b863ceb7SPatrick McHardy 263f9ac30f0SEric Biederman macvlan_hash_del(vlan); 264b863ceb7SPatrick McHardy return 0; 265b863ceb7SPatrick McHardy } 266b863ceb7SPatrick McHardy 267ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p) 268ad5d20a6SPatrick McHardy { 269ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 270ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 271ad5d20a6SPatrick McHardy struct sockaddr *addr = p; 272ad5d20a6SPatrick McHardy int err; 273ad5d20a6SPatrick McHardy 274ad5d20a6SPatrick McHardy if (!is_valid_ether_addr(addr->sa_data)) 275ad5d20a6SPatrick McHardy return -EADDRNOTAVAIL; 276ad5d20a6SPatrick McHardy 277f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 278f9ac30f0SEric Biederman /* Just copy in the new address */ 279f9ac30f0SEric Biederman memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 280f9ac30f0SEric Biederman } else { 281f9ac30f0SEric Biederman /* Rehash and update the device filters */ 282f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, addr->sa_data)) 283f9ac30f0SEric Biederman return -EBUSY; 284ad5d20a6SPatrick McHardy 285f9ac30f0SEric Biederman if ((err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN))) 286ad5d20a6SPatrick McHardy return err; 287f9ac30f0SEric Biederman 288ad5d20a6SPatrick McHardy dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); 289ad5d20a6SPatrick McHardy 290f9ac30f0SEric Biederman macvlan_hash_change_addr(vlan, addr->sa_data); 291f9ac30f0SEric Biederman } 292ad5d20a6SPatrick McHardy return 0; 293ad5d20a6SPatrick McHardy } 294ad5d20a6SPatrick McHardy 295b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 296b863ceb7SPatrick McHardy { 297b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 298b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 299b863ceb7SPatrick McHardy 300b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 301b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 302b863ceb7SPatrick McHardy } 303b863ceb7SPatrick McHardy 304b863ceb7SPatrick McHardy static void macvlan_set_multicast_list(struct net_device *dev) 305b863ceb7SPatrick McHardy { 306b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 307b863ceb7SPatrick McHardy 308b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 309b863ceb7SPatrick McHardy } 310b863ceb7SPatrick McHardy 311b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 312b863ceb7SPatrick McHardy { 313b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 314b863ceb7SPatrick McHardy 315b863ceb7SPatrick McHardy if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) 316b863ceb7SPatrick McHardy return -EINVAL; 317b863ceb7SPatrick McHardy dev->mtu = new_mtu; 318b863ceb7SPatrick McHardy return 0; 319b863ceb7SPatrick McHardy } 320b863ceb7SPatrick McHardy 321b863ceb7SPatrick McHardy /* 322b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 323b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 324b863ceb7SPatrick McHardy * separate class since they always nest. 325b863ceb7SPatrick McHardy */ 326b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key; 327cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key; 328b863ceb7SPatrick McHardy 329b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 330b863ceb7SPatrick McHardy (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 331b863ceb7SPatrick McHardy NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ 332b863ceb7SPatrick McHardy NETIF_F_TSO_ECN | NETIF_F_TSO6) 333b863ceb7SPatrick McHardy 334b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 335b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 336b863ceb7SPatrick McHardy 337e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev, 338e8a0464cSDavid S. Miller struct netdev_queue *txq, 339e8a0464cSDavid S. Miller void *_unused) 340c773e847SDavid S. Miller { 341c773e847SDavid S. Miller lockdep_set_class(&txq->_xmit_lock, 342c773e847SDavid S. Miller &macvlan_netdev_xmit_lock_key); 343c773e847SDavid S. Miller } 344c773e847SDavid S. Miller 345c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 346c773e847SDavid S. Miller { 347cf508b12SDavid S. Miller lockdep_set_class(&dev->addr_list_lock, 348cf508b12SDavid S. Miller &macvlan_netdev_addr_lock_key); 349e8a0464cSDavid S. Miller netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); 350c773e847SDavid S. Miller } 351c773e847SDavid S. Miller 352b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 353b863ceb7SPatrick McHardy { 354b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 355b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 356b863ceb7SPatrick McHardy 357b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 358b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 359b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 360b863ceb7SPatrick McHardy dev->iflink = lowerdev->ifindex; 361b863ceb7SPatrick McHardy 362c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 363c773e847SDavid S. Miller 364b863ceb7SPatrick McHardy return 0; 365b863ceb7SPatrick McHardy } 366b863ceb7SPatrick McHardy 367b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 368b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 369b863ceb7SPatrick McHardy { 370b863ceb7SPatrick McHardy snprintf(drvinfo->driver, 32, "macvlan"); 371b863ceb7SPatrick McHardy snprintf(drvinfo->version, 32, "0.1"); 372b863ceb7SPatrick McHardy } 373b863ceb7SPatrick McHardy 374b863ceb7SPatrick McHardy static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) 375b863ceb7SPatrick McHardy { 376b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 377b1b67dd4SPatrick McHardy return dev_ethtool_get_rx_csum(vlan->lowerdev); 378b863ceb7SPatrick McHardy } 379b863ceb7SPatrick McHardy 3809edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev, 3819edb8bb6SStephen Hemminger struct ethtool_cmd *cmd) 3829edb8bb6SStephen Hemminger { 3839edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 384b1b67dd4SPatrick McHardy return dev_ethtool_get_settings(vlan->lowerdev, cmd); 3859edb8bb6SStephen Hemminger } 3869edb8bb6SStephen Hemminger 3879edb8bb6SStephen Hemminger static u32 macvlan_ethtool_get_flags(struct net_device *dev) 3889edb8bb6SStephen Hemminger { 3899edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 390b1b67dd4SPatrick McHardy return dev_ethtool_get_flags(vlan->lowerdev); 3919edb8bb6SStephen Hemminger } 3929edb8bb6SStephen Hemminger 393b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 394b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 3959edb8bb6SStephen Hemminger .get_settings = macvlan_ethtool_get_settings, 396b863ceb7SPatrick McHardy .get_rx_csum = macvlan_ethtool_get_rx_csum, 397b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 3989edb8bb6SStephen Hemminger .get_flags = macvlan_ethtool_get_flags, 399b863ceb7SPatrick McHardy }; 400b863ceb7SPatrick McHardy 40154a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 40254a30c97SStephen Hemminger .ndo_init = macvlan_init, 40354a30c97SStephen Hemminger .ndo_open = macvlan_open, 40454a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 40500829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 40654a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 40754a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 40854a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 40954a30c97SStephen Hemminger .ndo_set_multicast_list = macvlan_set_multicast_list, 41054a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 41154a30c97SStephen Hemminger }; 41254a30c97SStephen Hemminger 413b863ceb7SPatrick McHardy static void macvlan_setup(struct net_device *dev) 414b863ceb7SPatrick McHardy { 415b863ceb7SPatrick McHardy ether_setup(dev); 416b863ceb7SPatrick McHardy 417*93f154b5SEric Dumazet dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 41854a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 419b863ceb7SPatrick McHardy dev->destructor = free_netdev; 4203b04dddeSStephen Hemminger dev->header_ops = &macvlan_hard_header_ops, 421b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 422b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 423b863ceb7SPatrick McHardy } 424b863ceb7SPatrick McHardy 425b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 426b863ceb7SPatrick McHardy { 427b863ceb7SPatrick McHardy struct macvlan_port *port; 428b863ceb7SPatrick McHardy unsigned int i; 429b863ceb7SPatrick McHardy 430b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 431b863ceb7SPatrick McHardy return -EINVAL; 432b863ceb7SPatrick McHardy 433b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 434b863ceb7SPatrick McHardy if (port == NULL) 435b863ceb7SPatrick McHardy return -ENOMEM; 436b863ceb7SPatrick McHardy 437b863ceb7SPatrick McHardy port->dev = dev; 438b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 439b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 440b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 441b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, port); 442b863ceb7SPatrick McHardy return 0; 443b863ceb7SPatrick McHardy } 444b863ceb7SPatrick McHardy 445b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 446b863ceb7SPatrick McHardy { 447b863ceb7SPatrick McHardy struct macvlan_port *port = dev->macvlan_port; 448b863ceb7SPatrick McHardy 449b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, NULL); 450b863ceb7SPatrick McHardy synchronize_rcu(); 451b863ceb7SPatrick McHardy kfree(port); 452b863ceb7SPatrick McHardy } 453b863ceb7SPatrick McHardy 454b863ceb7SPatrick McHardy static void macvlan_transfer_operstate(struct net_device *dev) 455b863ceb7SPatrick McHardy { 456b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 457b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 458b863ceb7SPatrick McHardy 459b863ceb7SPatrick McHardy if (lowerdev->operstate == IF_OPER_DORMANT) 460b863ceb7SPatrick McHardy netif_dormant_on(dev); 461b863ceb7SPatrick McHardy else 462b863ceb7SPatrick McHardy netif_dormant_off(dev); 463b863ceb7SPatrick McHardy 464b863ceb7SPatrick McHardy if (netif_carrier_ok(lowerdev)) { 465b863ceb7SPatrick McHardy if (!netif_carrier_ok(dev)) 466b863ceb7SPatrick McHardy netif_carrier_on(dev); 467b863ceb7SPatrick McHardy } else { 468f12ca5f9SPatrick McHardy if (netif_carrier_ok(dev)) 469b863ceb7SPatrick McHardy netif_carrier_off(dev); 470b863ceb7SPatrick McHardy } 471b863ceb7SPatrick McHardy } 472b863ceb7SPatrick McHardy 473b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 474b863ceb7SPatrick McHardy { 475b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 476b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 477b863ceb7SPatrick McHardy return -EINVAL; 478b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 479b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 480b863ceb7SPatrick McHardy } 481b863ceb7SPatrick McHardy return 0; 482b863ceb7SPatrick McHardy } 483b863ceb7SPatrick McHardy 484b863ceb7SPatrick McHardy static int macvlan_newlink(struct net_device *dev, 485b863ceb7SPatrick McHardy struct nlattr *tb[], struct nlattr *data[]) 486b863ceb7SPatrick McHardy { 487b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 488b863ceb7SPatrick McHardy struct macvlan_port *port; 489b863ceb7SPatrick McHardy struct net_device *lowerdev; 490b863ceb7SPatrick McHardy int err; 491b863ceb7SPatrick McHardy 492b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 493b863ceb7SPatrick McHardy return -EINVAL; 494b863ceb7SPatrick McHardy 495c346dca1SYOSHIFUJI Hideaki lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK])); 496b863ceb7SPatrick McHardy if (lowerdev == NULL) 497b863ceb7SPatrick McHardy return -ENODEV; 498b863ceb7SPatrick McHardy 499b0832a29SEric Biederman /* When creating macvlans on top of other macvlans - use 500b0832a29SEric Biederman * the real device as the lowerdev. 501a6ca5f1dSPatrick McHardy */ 502b0832a29SEric Biederman if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { 503b0832a29SEric Biederman struct macvlan_dev *lowervlan = netdev_priv(lowerdev); 504b0832a29SEric Biederman lowerdev = lowervlan->lowerdev; 505b0832a29SEric Biederman } 506a6ca5f1dSPatrick McHardy 507b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 508b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 509b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 510b863ceb7SPatrick McHardy return -EINVAL; 511b863ceb7SPatrick McHardy 512b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 513b863ceb7SPatrick McHardy random_ether_addr(dev->dev_addr); 514b863ceb7SPatrick McHardy 515b863ceb7SPatrick McHardy if (lowerdev->macvlan_port == NULL) { 516b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 517b863ceb7SPatrick McHardy if (err < 0) 518b863ceb7SPatrick McHardy return err; 519b863ceb7SPatrick McHardy } 520b863ceb7SPatrick McHardy port = lowerdev->macvlan_port; 521b863ceb7SPatrick McHardy 522b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 523b863ceb7SPatrick McHardy vlan->dev = dev; 524b863ceb7SPatrick McHardy vlan->port = port; 525b863ceb7SPatrick McHardy 526b863ceb7SPatrick McHardy err = register_netdevice(dev); 527b863ceb7SPatrick McHardy if (err < 0) 528b863ceb7SPatrick McHardy return err; 529b863ceb7SPatrick McHardy 530b863ceb7SPatrick McHardy list_add_tail(&vlan->list, &port->vlans); 531b863ceb7SPatrick McHardy macvlan_transfer_operstate(dev); 532b863ceb7SPatrick McHardy return 0; 533b863ceb7SPatrick McHardy } 534b863ceb7SPatrick McHardy 535b863ceb7SPatrick McHardy static void macvlan_dellink(struct net_device *dev) 536b863ceb7SPatrick McHardy { 537b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 538b863ceb7SPatrick McHardy struct macvlan_port *port = vlan->port; 539b863ceb7SPatrick McHardy 540b863ceb7SPatrick McHardy list_del(&vlan->list); 541b863ceb7SPatrick McHardy unregister_netdevice(dev); 542b863ceb7SPatrick McHardy 543b863ceb7SPatrick McHardy if (list_empty(&port->vlans)) 54473120964SPatrick McHardy macvlan_port_destroy(port->dev); 545b863ceb7SPatrick McHardy } 546b863ceb7SPatrick McHardy 547b863ceb7SPatrick McHardy static struct rtnl_link_ops macvlan_link_ops __read_mostly = { 548b863ceb7SPatrick McHardy .kind = "macvlan", 549b863ceb7SPatrick McHardy .priv_size = sizeof(struct macvlan_dev), 550b863ceb7SPatrick McHardy .setup = macvlan_setup, 551b863ceb7SPatrick McHardy .validate = macvlan_validate, 552b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 553b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 554b863ceb7SPatrick McHardy }; 555b863ceb7SPatrick McHardy 556b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 557b863ceb7SPatrick McHardy unsigned long event, void *ptr) 558b863ceb7SPatrick McHardy { 559b863ceb7SPatrick McHardy struct net_device *dev = ptr; 560b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 561b863ceb7SPatrick McHardy struct macvlan_port *port; 562b863ceb7SPatrick McHardy 563b863ceb7SPatrick McHardy port = dev->macvlan_port; 564b863ceb7SPatrick McHardy if (port == NULL) 565b863ceb7SPatrick McHardy return NOTIFY_DONE; 566b863ceb7SPatrick McHardy 567b863ceb7SPatrick McHardy switch (event) { 568b863ceb7SPatrick McHardy case NETDEV_CHANGE: 569b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 570b863ceb7SPatrick McHardy macvlan_transfer_operstate(vlan->dev); 571b863ceb7SPatrick McHardy break; 572b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 573b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 574b863ceb7SPatrick McHardy vlan->dev->features = dev->features & MACVLAN_FEATURES; 575b863ceb7SPatrick McHardy netdev_features_change(vlan->dev); 576b863ceb7SPatrick McHardy } 577b863ceb7SPatrick McHardy break; 578b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 579b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 580b863ceb7SPatrick McHardy macvlan_dellink(vlan->dev); 581b863ceb7SPatrick McHardy break; 582b863ceb7SPatrick McHardy } 583b863ceb7SPatrick McHardy return NOTIFY_DONE; 584b863ceb7SPatrick McHardy } 585b863ceb7SPatrick McHardy 586b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 587b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 588b863ceb7SPatrick McHardy }; 589b863ceb7SPatrick McHardy 590b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 591b863ceb7SPatrick McHardy { 592b863ceb7SPatrick McHardy int err; 593b863ceb7SPatrick McHardy 594b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 595b863ceb7SPatrick McHardy macvlan_handle_frame_hook = macvlan_handle_frame; 596b863ceb7SPatrick McHardy 597b863ceb7SPatrick McHardy err = rtnl_link_register(&macvlan_link_ops); 598b863ceb7SPatrick McHardy if (err < 0) 599b863ceb7SPatrick McHardy goto err1; 600b863ceb7SPatrick McHardy return 0; 601b863ceb7SPatrick McHardy err1: 60252913246SRami Rosen macvlan_handle_frame_hook = NULL; 603b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 604b863ceb7SPatrick McHardy return err; 605b863ceb7SPatrick McHardy } 606b863ceb7SPatrick McHardy 607b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 608b863ceb7SPatrick McHardy { 609b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 610b863ceb7SPatrick McHardy macvlan_handle_frame_hook = NULL; 611b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 612b863ceb7SPatrick McHardy } 613b863ceb7SPatrick McHardy 614b863ceb7SPatrick McHardy module_init(macvlan_init_module); 615b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 616b863ceb7SPatrick McHardy 617b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 618b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 619b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 620b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 621