| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (c) 2007 Patrick McHardy <kaber@trash.net> | 
|  | 3 | * | 
|  | 4 | * This program is free software; you can redistribute it and/or | 
|  | 5 | * modify it under the terms of the GNU General Public License as | 
|  | 6 | * published by the Free Software Foundation; either version 2 of | 
|  | 7 | * the License, or (at your option) any later version. | 
|  | 8 | * | 
|  | 9 | * The code this is based on carried the following copyright notice: | 
|  | 10 | * --- | 
|  | 11 | * (C) Copyright 2001-2006 | 
|  | 12 | * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com | 
|  | 13 | * Re-worked by Ben Greear <greearb@candelatech.com> | 
|  | 14 | * --- | 
|  | 15 | */ | 
|  | 16 | #include <linux/kernel.h> | 
|  | 17 | #include <linux/types.h> | 
|  | 18 | #include <linux/module.h> | 
|  | 19 | #include <linux/init.h> | 
|  | 20 | #include <linux/errno.h> | 
|  | 21 | #include <linux/slab.h> | 
|  | 22 | #include <linux/string.h> | 
| Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 23 | #include <linux/rculist.h> | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 24 | #include <linux/notifier.h> | 
|  | 25 | #include <linux/netdevice.h> | 
|  | 26 | #include <linux/etherdevice.h> | 
|  | 27 | #include <linux/ethtool.h> | 
|  | 28 | #include <linux/if_arp.h> | 
|  | 29 | #include <linux/if_link.h> | 
|  | 30 | #include <linux/if_macvlan.h> | 
|  | 31 | #include <net/rtnetlink.h> | 
|  | 32 |  | 
|  | 33 | #define MACVLAN_HASH_SIZE	(1 << BITS_PER_BYTE) | 
|  | 34 |  | 
|  | 35 | struct macvlan_port { | 
|  | 36 | struct net_device	*dev; | 
|  | 37 | struct hlist_head	vlan_hash[MACVLAN_HASH_SIZE]; | 
|  | 38 | struct list_head	vlans; | 
|  | 39 | }; | 
|  | 40 |  | 
|  | 41 | struct macvlan_dev { | 
|  | 42 | struct net_device	*dev; | 
|  | 43 | struct list_head	list; | 
|  | 44 | struct hlist_node	hlist; | 
|  | 45 | struct macvlan_port	*port; | 
|  | 46 | struct net_device	*lowerdev; | 
|  | 47 | }; | 
|  | 48 |  | 
|  | 49 |  | 
|  | 50 | static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, | 
|  | 51 | const unsigned char *addr) | 
|  | 52 | { | 
|  | 53 | struct macvlan_dev *vlan; | 
|  | 54 | struct hlist_node *n; | 
|  | 55 |  | 
|  | 56 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) { | 
|  | 57 | if (!compare_ether_addr(vlan->dev->dev_addr, addr)) | 
|  | 58 | return vlan; | 
|  | 59 | } | 
|  | 60 | return NULL; | 
|  | 61 | } | 
|  | 62 |  | 
| Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 63 | static void macvlan_hash_add(struct macvlan_dev *vlan) | 
|  | 64 | { | 
|  | 65 | struct macvlan_port *port = vlan->port; | 
|  | 66 | const unsigned char *addr = vlan->dev->dev_addr; | 
|  | 67 |  | 
|  | 68 | hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | static void macvlan_hash_del(struct macvlan_dev *vlan) | 
|  | 72 | { | 
|  | 73 | hlist_del_rcu(&vlan->hlist); | 
|  | 74 | synchronize_rcu(); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | static void macvlan_hash_change_addr(struct macvlan_dev *vlan, | 
|  | 78 | const unsigned char *addr) | 
|  | 79 | { | 
|  | 80 | macvlan_hash_del(vlan); | 
|  | 81 | /* Now that we are unhashed it is safe to change the device | 
|  | 82 | * address without confusing packet delivery. | 
|  | 83 | */ | 
|  | 84 | memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); | 
|  | 85 | macvlan_hash_add(vlan); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | static int macvlan_addr_busy(const struct macvlan_port *port, | 
|  | 89 | const unsigned char *addr) | 
|  | 90 | { | 
|  | 91 | /* Test to see if the specified multicast address is | 
|  | 92 | * currently in use by the underlying device or | 
|  | 93 | * another macvlan. | 
|  | 94 | */ | 
|  | 95 | if (memcmp(port->dev->dev_addr, addr, ETH_ALEN) == 0) | 
|  | 96 | return 1; | 
|  | 97 |  | 
|  | 98 | if (macvlan_hash_lookup(port, addr)) | 
|  | 99 | return 1; | 
|  | 100 |  | 
|  | 101 | return 0; | 
|  | 102 | } | 
|  | 103 |  | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 104 | static void macvlan_broadcast(struct sk_buff *skb, | 
|  | 105 | const struct macvlan_port *port) | 
|  | 106 | { | 
|  | 107 | const struct ethhdr *eth = eth_hdr(skb); | 
|  | 108 | const struct macvlan_dev *vlan; | 
|  | 109 | struct hlist_node *n; | 
|  | 110 | struct net_device *dev; | 
|  | 111 | struct sk_buff *nskb; | 
|  | 112 | unsigned int i; | 
|  | 113 |  | 
| Patrick McHardy | efbbced | 2008-11-26 15:30:48 -0800 | [diff] [blame] | 114 | if (skb->protocol == htons(ETH_P_PAUSE)) | 
|  | 115 | return; | 
|  | 116 |  | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 117 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) { | 
|  | 118 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { | 
|  | 119 | dev = vlan->dev; | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | nskb = skb_clone(skb, GFP_ATOMIC); | 
|  | 122 | if (nskb == NULL) { | 
|  | 123 | dev->stats.rx_errors++; | 
|  | 124 | dev->stats.rx_dropped++; | 
|  | 125 | continue; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | dev->stats.rx_bytes += skb->len + ETH_HLEN; | 
|  | 129 | dev->stats.rx_packets++; | 
|  | 130 | dev->stats.multicast++; | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | nskb->dev = dev; | 
|  | 133 | if (!compare_ether_addr(eth->h_dest, dev->broadcast)) | 
|  | 134 | nskb->pkt_type = PACKET_BROADCAST; | 
|  | 135 | else | 
|  | 136 | nskb->pkt_type = PACKET_MULTICAST; | 
|  | 137 |  | 
|  | 138 | netif_rx(nskb); | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | /* called under rcu_read_lock() from netif_receive_skb */ | 
|  | 144 | static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb) | 
|  | 145 | { | 
|  | 146 | const struct ethhdr *eth = eth_hdr(skb); | 
|  | 147 | const struct macvlan_port *port; | 
|  | 148 | const struct macvlan_dev *vlan; | 
|  | 149 | struct net_device *dev; | 
|  | 150 |  | 
|  | 151 | port = rcu_dereference(skb->dev->macvlan_port); | 
|  | 152 | if (port == NULL) | 
|  | 153 | return skb; | 
|  | 154 |  | 
|  | 155 | if (is_multicast_ether_addr(eth->h_dest)) { | 
|  | 156 | macvlan_broadcast(skb, port); | 
|  | 157 | return skb; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | vlan = macvlan_hash_lookup(port, eth->h_dest); | 
|  | 161 | if (vlan == NULL) | 
|  | 162 | return skb; | 
|  | 163 |  | 
|  | 164 | dev = vlan->dev; | 
|  | 165 | if (unlikely(!(dev->flags & IFF_UP))) { | 
|  | 166 | kfree_skb(skb); | 
|  | 167 | return NULL; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | skb = skb_share_check(skb, GFP_ATOMIC); | 
|  | 171 | if (skb == NULL) { | 
|  | 172 | dev->stats.rx_errors++; | 
|  | 173 | dev->stats.rx_dropped++; | 
|  | 174 | return NULL; | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | dev->stats.rx_bytes += skb->len + ETH_HLEN; | 
|  | 178 | dev->stats.rx_packets++; | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 179 |  | 
|  | 180 | skb->dev = dev; | 
|  | 181 | skb->pkt_type = PACKET_HOST; | 
|  | 182 |  | 
|  | 183 | netif_rx(skb); | 
|  | 184 | return NULL; | 
|  | 185 | } | 
|  | 186 |  | 
| Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 187 | static int macvlan_start_xmit(struct sk_buff *skb, struct net_device *dev) | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 188 | { | 
|  | 189 | const struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 190 | unsigned int len = skb->len; | 
|  | 191 | int ret; | 
|  | 192 |  | 
|  | 193 | skb->dev = vlan->lowerdev; | 
|  | 194 | ret = dev_queue_xmit(skb); | 
|  | 195 |  | 
|  | 196 | if (likely(ret == NET_XMIT_SUCCESS)) { | 
|  | 197 | dev->stats.tx_packets++; | 
|  | 198 | dev->stats.tx_bytes += len; | 
|  | 199 | } else { | 
|  | 200 | dev->stats.tx_errors++; | 
|  | 201 | dev->stats.tx_aborted_errors++; | 
|  | 202 | } | 
|  | 203 | return NETDEV_TX_OK; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, | 
| Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 207 | unsigned short type, const void *daddr, | 
|  | 208 | const void *saddr, unsigned len) | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 209 | { | 
|  | 210 | const struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 211 | struct net_device *lowerdev = vlan->lowerdev; | 
|  | 212 |  | 
| Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 213 | return dev_hard_header(skb, lowerdev, type, daddr, | 
|  | 214 | saddr ? : dev->dev_addr, len); | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
| Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 217 | static const struct header_ops macvlan_hard_header_ops = { | 
|  | 218 | .create  	= macvlan_hard_header, | 
|  | 219 | .rebuild	= eth_rebuild_header, | 
|  | 220 | .parse		= eth_header_parse, | 
| Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 221 | .cache		= eth_header_cache, | 
|  | 222 | .cache_update	= eth_header_cache_update, | 
|  | 223 | }; | 
|  | 224 |  | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 225 | static int macvlan_open(struct net_device *dev) | 
|  | 226 | { | 
|  | 227 | struct macvlan_dev *vlan = netdev_priv(dev); | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 228 | struct net_device *lowerdev = vlan->lowerdev; | 
|  | 229 | int err; | 
|  | 230 |  | 
| Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 231 | err = -EBUSY; | 
|  | 232 | if (macvlan_addr_busy(vlan->port, dev->dev_addr)) | 
|  | 233 | goto out; | 
|  | 234 |  | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 235 | err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN); | 
|  | 236 | if (err < 0) | 
| Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 237 | goto out; | 
|  | 238 | if (dev->flags & IFF_ALLMULTI) { | 
|  | 239 | err = dev_set_allmulti(lowerdev, 1); | 
|  | 240 | if (err < 0) | 
|  | 241 | goto del_unicast; | 
|  | 242 | } | 
| Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 243 | macvlan_hash_add(vlan); | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 244 | return 0; | 
| Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 245 |  | 
|  | 246 | del_unicast: | 
|  | 247 | dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); | 
|  | 248 | out: | 
|  | 249 | return err; | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 250 | } | 
|  | 251 |  | 
|  | 252 | static int macvlan_stop(struct net_device *dev) | 
|  | 253 | { | 
|  | 254 | struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 255 | struct net_device *lowerdev = vlan->lowerdev; | 
|  | 256 |  | 
|  | 257 | dev_mc_unsync(lowerdev, dev); | 
|  | 258 | if (dev->flags & IFF_ALLMULTI) | 
|  | 259 | dev_set_allmulti(lowerdev, -1); | 
|  | 260 |  | 
|  | 261 | dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); | 
|  | 262 |  | 
| Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 263 | macvlan_hash_del(vlan); | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 264 | return 0; | 
|  | 265 | } | 
|  | 266 |  | 
| Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 267 | static int macvlan_set_mac_address(struct net_device *dev, void *p) | 
|  | 268 | { | 
|  | 269 | struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 270 | struct net_device *lowerdev = vlan->lowerdev; | 
|  | 271 | struct sockaddr *addr = p; | 
|  | 272 | int err; | 
|  | 273 |  | 
|  | 274 | if (!is_valid_ether_addr(addr->sa_data)) | 
|  | 275 | return -EADDRNOTAVAIL; | 
|  | 276 |  | 
| Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 277 | if (!(dev->flags & IFF_UP)) { | 
|  | 278 | /* Just copy in the new address */ | 
|  | 279 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); | 
|  | 280 | } else { | 
|  | 281 | /* Rehash and update the device filters */ | 
|  | 282 | if (macvlan_addr_busy(vlan->port, addr->sa_data)) | 
|  | 283 | return -EBUSY; | 
| Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 284 |  | 
| Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 285 | if ((err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN))) | 
|  | 286 | return err; | 
| Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 287 |  | 
| Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 288 | dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); | 
|  | 289 |  | 
|  | 290 | macvlan_hash_change_addr(vlan, addr->sa_data); | 
|  | 291 | } | 
| Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 292 | return 0; | 
|  | 293 | } | 
|  | 294 |  | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 295 | static void macvlan_change_rx_flags(struct net_device *dev, int change) | 
|  | 296 | { | 
|  | 297 | struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 298 | struct net_device *lowerdev = vlan->lowerdev; | 
|  | 299 |  | 
|  | 300 | if (change & IFF_ALLMULTI) | 
|  | 301 | dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | static void macvlan_set_multicast_list(struct net_device *dev) | 
|  | 305 | { | 
|  | 306 | struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 307 |  | 
|  | 308 | dev_mc_sync(vlan->lowerdev, dev); | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | static int macvlan_change_mtu(struct net_device *dev, int new_mtu) | 
|  | 312 | { | 
|  | 313 | struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 314 |  | 
|  | 315 | if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) | 
|  | 316 | return -EINVAL; | 
|  | 317 | dev->mtu = new_mtu; | 
|  | 318 | return 0; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | /* | 
|  | 322 | * macvlan network devices have devices nesting below it and are a special | 
|  | 323 | * "super class" of normal network devices; split their locks off into a | 
|  | 324 | * separate class since they always nest. | 
|  | 325 | */ | 
|  | 326 | static struct lock_class_key macvlan_netdev_xmit_lock_key; | 
| David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 327 | static struct lock_class_key macvlan_netdev_addr_lock_key; | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 328 |  | 
|  | 329 | #define MACVLAN_FEATURES \ | 
|  | 330 | (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ | 
|  | 331 | NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ | 
|  | 332 | NETIF_F_TSO_ECN | NETIF_F_TSO6) | 
|  | 333 |  | 
|  | 334 | #define MACVLAN_STATE_MASK \ | 
|  | 335 | ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) | 
|  | 336 |  | 
| David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 337 | static void macvlan_set_lockdep_class_one(struct net_device *dev, | 
|  | 338 | struct netdev_queue *txq, | 
|  | 339 | void *_unused) | 
| David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 340 | { | 
|  | 341 | lockdep_set_class(&txq->_xmit_lock, | 
|  | 342 | &macvlan_netdev_xmit_lock_key); | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | static void macvlan_set_lockdep_class(struct net_device *dev) | 
|  | 346 | { | 
| David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 347 | lockdep_set_class(&dev->addr_list_lock, | 
|  | 348 | &macvlan_netdev_addr_lock_key); | 
| David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 349 | netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); | 
| David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 350 | } | 
|  | 351 |  | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 352 | static int macvlan_init(struct net_device *dev) | 
|  | 353 | { | 
|  | 354 | struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 355 | const struct net_device *lowerdev = vlan->lowerdev; | 
|  | 356 |  | 
|  | 357 | dev->state		= (dev->state & ~MACVLAN_STATE_MASK) | | 
|  | 358 | (lowerdev->state & MACVLAN_STATE_MASK); | 
|  | 359 | dev->features 		= lowerdev->features & MACVLAN_FEATURES; | 
|  | 360 | dev->iflink		= lowerdev->ifindex; | 
|  | 361 |  | 
| David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 362 | macvlan_set_lockdep_class(dev); | 
|  | 363 |  | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 364 | return 0; | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | static void macvlan_ethtool_get_drvinfo(struct net_device *dev, | 
|  | 368 | struct ethtool_drvinfo *drvinfo) | 
|  | 369 | { | 
|  | 370 | snprintf(drvinfo->driver, 32, "macvlan"); | 
|  | 371 | snprintf(drvinfo->version, 32, "0.1"); | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) | 
|  | 375 | { | 
|  | 376 | const struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 377 | struct net_device *lowerdev = vlan->lowerdev; | 
|  | 378 |  | 
| Patrick McHardy | 7816a0a | 2009-04-17 15:59:23 -0700 | [diff] [blame] | 379 | if (lowerdev->ethtool_ops == NULL || | 
|  | 380 | lowerdev->ethtool_ops->get_rx_csum == NULL) | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 381 | return 0; | 
|  | 382 | return lowerdev->ethtool_ops->get_rx_csum(lowerdev); | 
|  | 383 | } | 
|  | 384 |  | 
| Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 385 | static int macvlan_ethtool_get_settings(struct net_device *dev, | 
|  | 386 | struct ethtool_cmd *cmd) | 
|  | 387 | { | 
|  | 388 | const struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 389 | struct net_device *lowerdev = vlan->lowerdev; | 
|  | 390 |  | 
| Patrick McHardy | 7816a0a | 2009-04-17 15:59:23 -0700 | [diff] [blame] | 391 | if (!lowerdev->ethtool_ops || | 
|  | 392 | !lowerdev->ethtool_ops->get_settings) | 
| Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 393 | return -EOPNOTSUPP; | 
|  | 394 |  | 
|  | 395 | return lowerdev->ethtool_ops->get_settings(lowerdev, cmd); | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | static u32 macvlan_ethtool_get_flags(struct net_device *dev) | 
|  | 399 | { | 
|  | 400 | const struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 401 | struct net_device *lowerdev = vlan->lowerdev; | 
|  | 402 |  | 
| Patrick McHardy | 7816a0a | 2009-04-17 15:59:23 -0700 | [diff] [blame] | 403 | if (!lowerdev->ethtool_ops || | 
|  | 404 | !lowerdev->ethtool_ops->get_flags) | 
| Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 405 | return 0; | 
|  | 406 | return lowerdev->ethtool_ops->get_flags(lowerdev); | 
|  | 407 | } | 
|  | 408 |  | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 409 | static const struct ethtool_ops macvlan_ethtool_ops = { | 
|  | 410 | .get_link		= ethtool_op_get_link, | 
| Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 411 | .get_settings		= macvlan_ethtool_get_settings, | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 412 | .get_rx_csum		= macvlan_ethtool_get_rx_csum, | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 413 | .get_drvinfo		= macvlan_ethtool_get_drvinfo, | 
| Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 414 | .get_flags		= macvlan_ethtool_get_flags, | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 415 | }; | 
|  | 416 |  | 
| Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 417 | static const struct net_device_ops macvlan_netdev_ops = { | 
|  | 418 | .ndo_init		= macvlan_init, | 
|  | 419 | .ndo_open		= macvlan_open, | 
|  | 420 | .ndo_stop		= macvlan_stop, | 
| Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 421 | .ndo_start_xmit		= macvlan_start_xmit, | 
| Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 422 | .ndo_change_mtu		= macvlan_change_mtu, | 
|  | 423 | .ndo_change_rx_flags	= macvlan_change_rx_flags, | 
|  | 424 | .ndo_set_mac_address	= macvlan_set_mac_address, | 
|  | 425 | .ndo_set_multicast_list	= macvlan_set_multicast_list, | 
|  | 426 | .ndo_validate_addr	= eth_validate_addr, | 
|  | 427 | }; | 
|  | 428 |  | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 429 | static void macvlan_setup(struct net_device *dev) | 
|  | 430 | { | 
|  | 431 | ether_setup(dev); | 
|  | 432 |  | 
| Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 433 | dev->netdev_ops		= &macvlan_netdev_ops; | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 434 | dev->destructor		= free_netdev; | 
| Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 435 | dev->header_ops		= &macvlan_hard_header_ops, | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 436 | dev->ethtool_ops	= &macvlan_ethtool_ops; | 
|  | 437 | dev->tx_queue_len	= 0; | 
|  | 438 | } | 
|  | 439 |  | 
|  | 440 | static int macvlan_port_create(struct net_device *dev) | 
|  | 441 | { | 
|  | 442 | struct macvlan_port *port; | 
|  | 443 | unsigned int i; | 
|  | 444 |  | 
|  | 445 | if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) | 
|  | 446 | return -EINVAL; | 
|  | 447 |  | 
|  | 448 | port = kzalloc(sizeof(*port), GFP_KERNEL); | 
|  | 449 | if (port == NULL) | 
|  | 450 | return -ENOMEM; | 
|  | 451 |  | 
|  | 452 | port->dev = dev; | 
|  | 453 | INIT_LIST_HEAD(&port->vlans); | 
|  | 454 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) | 
|  | 455 | INIT_HLIST_HEAD(&port->vlan_hash[i]); | 
|  | 456 | rcu_assign_pointer(dev->macvlan_port, port); | 
|  | 457 | return 0; | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | static void macvlan_port_destroy(struct net_device *dev) | 
|  | 461 | { | 
|  | 462 | struct macvlan_port *port = dev->macvlan_port; | 
|  | 463 |  | 
|  | 464 | rcu_assign_pointer(dev->macvlan_port, NULL); | 
|  | 465 | synchronize_rcu(); | 
|  | 466 | kfree(port); | 
|  | 467 | } | 
|  | 468 |  | 
|  | 469 | static void macvlan_transfer_operstate(struct net_device *dev) | 
|  | 470 | { | 
|  | 471 | struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 472 | const struct net_device *lowerdev = vlan->lowerdev; | 
|  | 473 |  | 
|  | 474 | if (lowerdev->operstate == IF_OPER_DORMANT) | 
|  | 475 | netif_dormant_on(dev); | 
|  | 476 | else | 
|  | 477 | netif_dormant_off(dev); | 
|  | 478 |  | 
|  | 479 | if (netif_carrier_ok(lowerdev)) { | 
|  | 480 | if (!netif_carrier_ok(dev)) | 
|  | 481 | netif_carrier_on(dev); | 
|  | 482 | } else { | 
| Patrick McHardy | f12ca5f | 2008-01-21 00:47:08 -0800 | [diff] [blame] | 483 | if (netif_carrier_ok(dev)) | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 484 | netif_carrier_off(dev); | 
|  | 485 | } | 
|  | 486 | } | 
|  | 487 |  | 
|  | 488 | static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) | 
|  | 489 | { | 
|  | 490 | if (tb[IFLA_ADDRESS]) { | 
|  | 491 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | 
|  | 492 | return -EINVAL; | 
|  | 493 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | 
|  | 494 | return -EADDRNOTAVAIL; | 
|  | 495 | } | 
|  | 496 | return 0; | 
|  | 497 | } | 
|  | 498 |  | 
|  | 499 | static int macvlan_newlink(struct net_device *dev, | 
|  | 500 | struct nlattr *tb[], struct nlattr *data[]) | 
|  | 501 | { | 
|  | 502 | struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 503 | struct macvlan_port *port; | 
|  | 504 | struct net_device *lowerdev; | 
|  | 505 | int err; | 
|  | 506 |  | 
|  | 507 | if (!tb[IFLA_LINK]) | 
|  | 508 | return -EINVAL; | 
|  | 509 |  | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 510 | lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK])); | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 511 | if (lowerdev == NULL) | 
|  | 512 | return -ENODEV; | 
|  | 513 |  | 
| Eric Biederman | b0832a2 | 2009-03-13 13:15:37 -0700 | [diff] [blame] | 514 | /* When creating macvlans on top of other macvlans - use | 
|  | 515 | * the real device as the lowerdev. | 
| Patrick McHardy | a6ca5f1 | 2008-01-10 22:39:28 -0800 | [diff] [blame] | 516 | */ | 
| Eric Biederman | b0832a2 | 2009-03-13 13:15:37 -0700 | [diff] [blame] | 517 | if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { | 
|  | 518 | struct macvlan_dev *lowervlan = netdev_priv(lowerdev); | 
|  | 519 | lowerdev = lowervlan->lowerdev; | 
|  | 520 | } | 
| Patrick McHardy | a6ca5f1 | 2008-01-10 22:39:28 -0800 | [diff] [blame] | 521 |  | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 522 | if (!tb[IFLA_MTU]) | 
|  | 523 | dev->mtu = lowerdev->mtu; | 
|  | 524 | else if (dev->mtu > lowerdev->mtu) | 
|  | 525 | return -EINVAL; | 
|  | 526 |  | 
|  | 527 | if (!tb[IFLA_ADDRESS]) | 
|  | 528 | random_ether_addr(dev->dev_addr); | 
|  | 529 |  | 
|  | 530 | if (lowerdev->macvlan_port == NULL) { | 
|  | 531 | err = macvlan_port_create(lowerdev); | 
|  | 532 | if (err < 0) | 
|  | 533 | return err; | 
|  | 534 | } | 
|  | 535 | port = lowerdev->macvlan_port; | 
|  | 536 |  | 
|  | 537 | vlan->lowerdev = lowerdev; | 
|  | 538 | vlan->dev      = dev; | 
|  | 539 | vlan->port     = port; | 
|  | 540 |  | 
|  | 541 | err = register_netdevice(dev); | 
|  | 542 | if (err < 0) | 
|  | 543 | return err; | 
|  | 544 |  | 
|  | 545 | list_add_tail(&vlan->list, &port->vlans); | 
|  | 546 | macvlan_transfer_operstate(dev); | 
|  | 547 | return 0; | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | static void macvlan_dellink(struct net_device *dev) | 
|  | 551 | { | 
|  | 552 | struct macvlan_dev *vlan = netdev_priv(dev); | 
|  | 553 | struct macvlan_port *port = vlan->port; | 
|  | 554 |  | 
|  | 555 | list_del(&vlan->list); | 
|  | 556 | unregister_netdevice(dev); | 
|  | 557 |  | 
|  | 558 | if (list_empty(&port->vlans)) | 
| Patrick McHardy | 7312096 | 2008-05-08 01:13:31 -0700 | [diff] [blame] | 559 | macvlan_port_destroy(port->dev); | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 560 | } | 
|  | 561 |  | 
|  | 562 | static struct rtnl_link_ops macvlan_link_ops __read_mostly = { | 
|  | 563 | .kind		= "macvlan", | 
|  | 564 | .priv_size	= sizeof(struct macvlan_dev), | 
|  | 565 | .setup		= macvlan_setup, | 
|  | 566 | .validate	= macvlan_validate, | 
|  | 567 | .newlink	= macvlan_newlink, | 
|  | 568 | .dellink	= macvlan_dellink, | 
|  | 569 | }; | 
|  | 570 |  | 
|  | 571 | static int macvlan_device_event(struct notifier_block *unused, | 
|  | 572 | unsigned long event, void *ptr) | 
|  | 573 | { | 
|  | 574 | struct net_device *dev = ptr; | 
|  | 575 | struct macvlan_dev *vlan, *next; | 
|  | 576 | struct macvlan_port *port; | 
|  | 577 |  | 
|  | 578 | port = dev->macvlan_port; | 
|  | 579 | if (port == NULL) | 
|  | 580 | return NOTIFY_DONE; | 
|  | 581 |  | 
|  | 582 | switch (event) { | 
|  | 583 | case NETDEV_CHANGE: | 
|  | 584 | list_for_each_entry(vlan, &port->vlans, list) | 
|  | 585 | macvlan_transfer_operstate(vlan->dev); | 
|  | 586 | break; | 
|  | 587 | case NETDEV_FEAT_CHANGE: | 
|  | 588 | list_for_each_entry(vlan, &port->vlans, list) { | 
|  | 589 | vlan->dev->features = dev->features & MACVLAN_FEATURES; | 
|  | 590 | netdev_features_change(vlan->dev); | 
|  | 591 | } | 
|  | 592 | break; | 
|  | 593 | case NETDEV_UNREGISTER: | 
|  | 594 | list_for_each_entry_safe(vlan, next, &port->vlans, list) | 
|  | 595 | macvlan_dellink(vlan->dev); | 
|  | 596 | break; | 
|  | 597 | } | 
|  | 598 | return NOTIFY_DONE; | 
|  | 599 | } | 
|  | 600 |  | 
|  | 601 | static struct notifier_block macvlan_notifier_block __read_mostly = { | 
|  | 602 | .notifier_call	= macvlan_device_event, | 
|  | 603 | }; | 
|  | 604 |  | 
|  | 605 | static int __init macvlan_init_module(void) | 
|  | 606 | { | 
|  | 607 | int err; | 
|  | 608 |  | 
|  | 609 | register_netdevice_notifier(&macvlan_notifier_block); | 
|  | 610 | macvlan_handle_frame_hook = macvlan_handle_frame; | 
|  | 611 |  | 
|  | 612 | err = rtnl_link_register(&macvlan_link_ops); | 
|  | 613 | if (err < 0) | 
|  | 614 | goto err1; | 
|  | 615 | return 0; | 
|  | 616 | err1: | 
| Rami Rosen | 5291324 | 2008-01-31 16:56:03 -0800 | [diff] [blame] | 617 | macvlan_handle_frame_hook = NULL; | 
| Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 618 | unregister_netdevice_notifier(&macvlan_notifier_block); | 
|  | 619 | return err; | 
|  | 620 | } | 
|  | 621 |  | 
|  | 622 | static void __exit macvlan_cleanup_module(void) | 
|  | 623 | { | 
|  | 624 | rtnl_link_unregister(&macvlan_link_ops); | 
|  | 625 | macvlan_handle_frame_hook = NULL; | 
|  | 626 | unregister_netdevice_notifier(&macvlan_notifier_block); | 
|  | 627 | } | 
|  | 628 |  | 
|  | 629 | module_init(macvlan_init_module); | 
|  | 630 | module_exit(macvlan_cleanup_module); | 
|  | 631 |  | 
|  | 632 | MODULE_LICENSE("GPL"); | 
|  | 633 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | 
|  | 634 | MODULE_DESCRIPTION("Driver for MAC address based VLANs"); | 
|  | 635 | MODULE_ALIAS_RTNL_LINK("macvlan"); |