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 63*f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan) 64*f9ac30f0SEric Biederman { 65*f9ac30f0SEric Biederman struct macvlan_port *port = vlan->port; 66*f9ac30f0SEric Biederman const unsigned char *addr = vlan->dev->dev_addr; 67*f9ac30f0SEric Biederman 68*f9ac30f0SEric Biederman hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); 69*f9ac30f0SEric Biederman } 70*f9ac30f0SEric Biederman 71*f9ac30f0SEric Biederman static void macvlan_hash_del(struct macvlan_dev *vlan) 72*f9ac30f0SEric Biederman { 73*f9ac30f0SEric Biederman hlist_del_rcu(&vlan->hlist); 74*f9ac30f0SEric Biederman synchronize_rcu(); 75*f9ac30f0SEric Biederman } 76*f9ac30f0SEric Biederman 77*f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan, 78*f9ac30f0SEric Biederman const unsigned char *addr) 79*f9ac30f0SEric Biederman { 80*f9ac30f0SEric Biederman macvlan_hash_del(vlan); 81*f9ac30f0SEric Biederman /* Now that we are unhashed it is safe to change the device 82*f9ac30f0SEric Biederman * address without confusing packet delivery. 83*f9ac30f0SEric Biederman */ 84*f9ac30f0SEric Biederman memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); 85*f9ac30f0SEric Biederman macvlan_hash_add(vlan); 86*f9ac30f0SEric Biederman } 87*f9ac30f0SEric Biederman 88*f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port, 89*f9ac30f0SEric Biederman const unsigned char *addr) 90*f9ac30f0SEric Biederman { 91*f9ac30f0SEric Biederman /* Test to see if the specified multicast address is 92*f9ac30f0SEric Biederman * currently in use by the underlying device or 93*f9ac30f0SEric Biederman * another macvlan. 94*f9ac30f0SEric Biederman */ 95*f9ac30f0SEric Biederman if (memcmp(port->dev->dev_addr, addr, ETH_ALEN) == 0) 96*f9ac30f0SEric Biederman return 1; 97*f9ac30f0SEric Biederman 98*f9ac30f0SEric Biederman if (macvlan_hash_lookup(port, addr)) 99*f9ac30f0SEric Biederman return 1; 100*f9ac30f0SEric Biederman 101*f9ac30f0SEric Biederman return 0; 102*f9ac30f0SEric Biederman } 103*f9ac30f0SEric 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 231*f9ac30f0SEric Biederman err = -EBUSY; 232*f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, dev->dev_addr)) 233*f9ac30f0SEric Biederman goto out; 234*f9ac30f0SEric 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 } 243*f9ac30f0SEric 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 263*f9ac30f0SEric 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 277*f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 278*f9ac30f0SEric Biederman /* Just copy in the new address */ 279*f9ac30f0SEric Biederman memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 280*f9ac30f0SEric Biederman } else { 281*f9ac30f0SEric Biederman /* Rehash and update the device filters */ 282*f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, addr->sa_data)) 283*f9ac30f0SEric Biederman return -EBUSY; 284ad5d20a6SPatrick McHardy 285*f9ac30f0SEric Biederman if ((err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN))) 286ad5d20a6SPatrick McHardy return err; 287*f9ac30f0SEric Biederman 288ad5d20a6SPatrick McHardy dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); 289ad5d20a6SPatrick McHardy 290*f9ac30f0SEric Biederman macvlan_hash_change_addr(vlan, addr->sa_data); 291*f9ac30f0SEric 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); 377b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 378b863ceb7SPatrick McHardy 379b863ceb7SPatrick McHardy if (lowerdev->ethtool_ops->get_rx_csum == NULL) 380b863ceb7SPatrick McHardy return 0; 381b863ceb7SPatrick McHardy return lowerdev->ethtool_ops->get_rx_csum(lowerdev); 382b863ceb7SPatrick McHardy } 383b863ceb7SPatrick McHardy 3849edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev, 3859edb8bb6SStephen Hemminger struct ethtool_cmd *cmd) 3869edb8bb6SStephen Hemminger { 3879edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 3889edb8bb6SStephen Hemminger struct net_device *lowerdev = vlan->lowerdev; 3899edb8bb6SStephen Hemminger 3909edb8bb6SStephen Hemminger if (!lowerdev->ethtool_ops->get_settings) 3919edb8bb6SStephen Hemminger return -EOPNOTSUPP; 3929edb8bb6SStephen Hemminger 3939edb8bb6SStephen Hemminger return lowerdev->ethtool_ops->get_settings(lowerdev, cmd); 3949edb8bb6SStephen Hemminger } 3959edb8bb6SStephen Hemminger 3969edb8bb6SStephen Hemminger static u32 macvlan_ethtool_get_flags(struct net_device *dev) 3979edb8bb6SStephen Hemminger { 3989edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 3999edb8bb6SStephen Hemminger struct net_device *lowerdev = vlan->lowerdev; 4009edb8bb6SStephen Hemminger 4019edb8bb6SStephen Hemminger if (!lowerdev->ethtool_ops->get_flags) 4029edb8bb6SStephen Hemminger return 0; 4039edb8bb6SStephen Hemminger return lowerdev->ethtool_ops->get_flags(lowerdev); 4049edb8bb6SStephen Hemminger } 4059edb8bb6SStephen Hemminger 406b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 407b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 4089edb8bb6SStephen Hemminger .get_settings = macvlan_ethtool_get_settings, 409b863ceb7SPatrick McHardy .get_rx_csum = macvlan_ethtool_get_rx_csum, 410b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 4119edb8bb6SStephen Hemminger .get_flags = macvlan_ethtool_get_flags, 412b863ceb7SPatrick McHardy }; 413b863ceb7SPatrick McHardy 41454a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 41554a30c97SStephen Hemminger .ndo_init = macvlan_init, 41654a30c97SStephen Hemminger .ndo_open = macvlan_open, 41754a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 41800829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 41954a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 42054a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 42154a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 42254a30c97SStephen Hemminger .ndo_set_multicast_list = macvlan_set_multicast_list, 42354a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 42454a30c97SStephen Hemminger }; 42554a30c97SStephen Hemminger 426b863ceb7SPatrick McHardy static void macvlan_setup(struct net_device *dev) 427b863ceb7SPatrick McHardy { 428b863ceb7SPatrick McHardy ether_setup(dev); 429b863ceb7SPatrick McHardy 43054a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 431b863ceb7SPatrick McHardy dev->destructor = free_netdev; 4323b04dddeSStephen Hemminger dev->header_ops = &macvlan_hard_header_ops, 433b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 434b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 435b863ceb7SPatrick McHardy } 436b863ceb7SPatrick McHardy 437b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 438b863ceb7SPatrick McHardy { 439b863ceb7SPatrick McHardy struct macvlan_port *port; 440b863ceb7SPatrick McHardy unsigned int i; 441b863ceb7SPatrick McHardy 442b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 443b863ceb7SPatrick McHardy return -EINVAL; 444b863ceb7SPatrick McHardy 445b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 446b863ceb7SPatrick McHardy if (port == NULL) 447b863ceb7SPatrick McHardy return -ENOMEM; 448b863ceb7SPatrick McHardy 449b863ceb7SPatrick McHardy port->dev = dev; 450b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 451b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 452b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 453b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, port); 454b863ceb7SPatrick McHardy return 0; 455b863ceb7SPatrick McHardy } 456b863ceb7SPatrick McHardy 457b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 458b863ceb7SPatrick McHardy { 459b863ceb7SPatrick McHardy struct macvlan_port *port = dev->macvlan_port; 460b863ceb7SPatrick McHardy 461b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, NULL); 462b863ceb7SPatrick McHardy synchronize_rcu(); 463b863ceb7SPatrick McHardy kfree(port); 464b863ceb7SPatrick McHardy } 465b863ceb7SPatrick McHardy 466b863ceb7SPatrick McHardy static void macvlan_transfer_operstate(struct net_device *dev) 467b863ceb7SPatrick McHardy { 468b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 469b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 470b863ceb7SPatrick McHardy 471b863ceb7SPatrick McHardy if (lowerdev->operstate == IF_OPER_DORMANT) 472b863ceb7SPatrick McHardy netif_dormant_on(dev); 473b863ceb7SPatrick McHardy else 474b863ceb7SPatrick McHardy netif_dormant_off(dev); 475b863ceb7SPatrick McHardy 476b863ceb7SPatrick McHardy if (netif_carrier_ok(lowerdev)) { 477b863ceb7SPatrick McHardy if (!netif_carrier_ok(dev)) 478b863ceb7SPatrick McHardy netif_carrier_on(dev); 479b863ceb7SPatrick McHardy } else { 480f12ca5f9SPatrick McHardy if (netif_carrier_ok(dev)) 481b863ceb7SPatrick McHardy netif_carrier_off(dev); 482b863ceb7SPatrick McHardy } 483b863ceb7SPatrick McHardy } 484b863ceb7SPatrick McHardy 485b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 486b863ceb7SPatrick McHardy { 487b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 488b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 489b863ceb7SPatrick McHardy return -EINVAL; 490b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 491b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 492b863ceb7SPatrick McHardy } 493b863ceb7SPatrick McHardy return 0; 494b863ceb7SPatrick McHardy } 495b863ceb7SPatrick McHardy 496b863ceb7SPatrick McHardy static int macvlan_newlink(struct net_device *dev, 497b863ceb7SPatrick McHardy struct nlattr *tb[], struct nlattr *data[]) 498b863ceb7SPatrick McHardy { 499b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 500b863ceb7SPatrick McHardy struct macvlan_port *port; 501b863ceb7SPatrick McHardy struct net_device *lowerdev; 502b863ceb7SPatrick McHardy int err; 503b863ceb7SPatrick McHardy 504b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 505b863ceb7SPatrick McHardy return -EINVAL; 506b863ceb7SPatrick McHardy 507c346dca1SYOSHIFUJI Hideaki lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK])); 508b863ceb7SPatrick McHardy if (lowerdev == NULL) 509b863ceb7SPatrick McHardy return -ENODEV; 510b863ceb7SPatrick McHardy 511b0832a29SEric Biederman /* When creating macvlans on top of other macvlans - use 512b0832a29SEric Biederman * the real device as the lowerdev. 513a6ca5f1dSPatrick McHardy */ 514b0832a29SEric Biederman if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { 515b0832a29SEric Biederman struct macvlan_dev *lowervlan = netdev_priv(lowerdev); 516b0832a29SEric Biederman lowerdev = lowervlan->lowerdev; 517b0832a29SEric Biederman } 518a6ca5f1dSPatrick McHardy 519b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 520b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 521b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 522b863ceb7SPatrick McHardy return -EINVAL; 523b863ceb7SPatrick McHardy 524b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 525b863ceb7SPatrick McHardy random_ether_addr(dev->dev_addr); 526b863ceb7SPatrick McHardy 527b863ceb7SPatrick McHardy if (lowerdev->macvlan_port == NULL) { 528b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 529b863ceb7SPatrick McHardy if (err < 0) 530b863ceb7SPatrick McHardy return err; 531b863ceb7SPatrick McHardy } 532b863ceb7SPatrick McHardy port = lowerdev->macvlan_port; 533b863ceb7SPatrick McHardy 534b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 535b863ceb7SPatrick McHardy vlan->dev = dev; 536b863ceb7SPatrick McHardy vlan->port = port; 537b863ceb7SPatrick McHardy 538b863ceb7SPatrick McHardy err = register_netdevice(dev); 539b863ceb7SPatrick McHardy if (err < 0) 540b863ceb7SPatrick McHardy return err; 541b863ceb7SPatrick McHardy 542b863ceb7SPatrick McHardy list_add_tail(&vlan->list, &port->vlans); 543b863ceb7SPatrick McHardy macvlan_transfer_operstate(dev); 544b863ceb7SPatrick McHardy return 0; 545b863ceb7SPatrick McHardy } 546b863ceb7SPatrick McHardy 547b863ceb7SPatrick McHardy static void macvlan_dellink(struct net_device *dev) 548b863ceb7SPatrick McHardy { 549b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 550b863ceb7SPatrick McHardy struct macvlan_port *port = vlan->port; 551b863ceb7SPatrick McHardy 552b863ceb7SPatrick McHardy list_del(&vlan->list); 553b863ceb7SPatrick McHardy unregister_netdevice(dev); 554b863ceb7SPatrick McHardy 555b863ceb7SPatrick McHardy if (list_empty(&port->vlans)) 55673120964SPatrick McHardy macvlan_port_destroy(port->dev); 557b863ceb7SPatrick McHardy } 558b863ceb7SPatrick McHardy 559b863ceb7SPatrick McHardy static struct rtnl_link_ops macvlan_link_ops __read_mostly = { 560b863ceb7SPatrick McHardy .kind = "macvlan", 561b863ceb7SPatrick McHardy .priv_size = sizeof(struct macvlan_dev), 562b863ceb7SPatrick McHardy .setup = macvlan_setup, 563b863ceb7SPatrick McHardy .validate = macvlan_validate, 564b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 565b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 566b863ceb7SPatrick McHardy }; 567b863ceb7SPatrick McHardy 568b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 569b863ceb7SPatrick McHardy unsigned long event, void *ptr) 570b863ceb7SPatrick McHardy { 571b863ceb7SPatrick McHardy struct net_device *dev = ptr; 572b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 573b863ceb7SPatrick McHardy struct macvlan_port *port; 574b863ceb7SPatrick McHardy 575b863ceb7SPatrick McHardy port = dev->macvlan_port; 576b863ceb7SPatrick McHardy if (port == NULL) 577b863ceb7SPatrick McHardy return NOTIFY_DONE; 578b863ceb7SPatrick McHardy 579b863ceb7SPatrick McHardy switch (event) { 580b863ceb7SPatrick McHardy case NETDEV_CHANGE: 581b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 582b863ceb7SPatrick McHardy macvlan_transfer_operstate(vlan->dev); 583b863ceb7SPatrick McHardy break; 584b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 585b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 586b863ceb7SPatrick McHardy vlan->dev->features = dev->features & MACVLAN_FEATURES; 587b863ceb7SPatrick McHardy netdev_features_change(vlan->dev); 588b863ceb7SPatrick McHardy } 589b863ceb7SPatrick McHardy break; 590b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 591b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 592b863ceb7SPatrick McHardy macvlan_dellink(vlan->dev); 593b863ceb7SPatrick McHardy break; 594b863ceb7SPatrick McHardy } 595b863ceb7SPatrick McHardy return NOTIFY_DONE; 596b863ceb7SPatrick McHardy } 597b863ceb7SPatrick McHardy 598b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 599b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 600b863ceb7SPatrick McHardy }; 601b863ceb7SPatrick McHardy 602b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 603b863ceb7SPatrick McHardy { 604b863ceb7SPatrick McHardy int err; 605b863ceb7SPatrick McHardy 606b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 607b863ceb7SPatrick McHardy macvlan_handle_frame_hook = macvlan_handle_frame; 608b863ceb7SPatrick McHardy 609b863ceb7SPatrick McHardy err = rtnl_link_register(&macvlan_link_ops); 610b863ceb7SPatrick McHardy if (err < 0) 611b863ceb7SPatrick McHardy goto err1; 612b863ceb7SPatrick McHardy return 0; 613b863ceb7SPatrick McHardy err1: 61452913246SRami Rosen macvlan_handle_frame_hook = NULL; 615b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 616b863ceb7SPatrick McHardy return err; 617b863ceb7SPatrick McHardy } 618b863ceb7SPatrick McHardy 619b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 620b863ceb7SPatrick McHardy { 621b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 622b863ceb7SPatrick McHardy macvlan_handle_frame_hook = NULL; 623b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 624b863ceb7SPatrick McHardy } 625b863ceb7SPatrick McHardy 626b863ceb7SPatrick McHardy module_init(macvlan_init_module); 627b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 628b863ceb7SPatrick McHardy 629b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 630b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 631b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 632b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 633