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> |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 32 | #include <net/xfrm.h> |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 33 | |
| 34 | #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE) |
| 35 | |
| 36 | struct macvlan_port { |
| 37 | struct net_device *dev; |
| 38 | struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; |
| 39 | struct list_head vlans; |
| 40 | }; |
| 41 | |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 42 | /** |
| 43 | * struct macvlan_rx_stats - MACVLAN percpu rx stats |
| 44 | * @rx_packets: number of received packets |
| 45 | * @rx_bytes: number of received bytes |
| 46 | * @multicast: number of received multicast packets |
| 47 | * @rx_errors: number of errors |
| 48 | */ |
| 49 | struct macvlan_rx_stats { |
| 50 | unsigned long rx_packets; |
| 51 | unsigned long rx_bytes; |
| 52 | unsigned long multicast; |
| 53 | unsigned long rx_errors; |
| 54 | }; |
| 55 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 56 | struct macvlan_dev { |
| 57 | struct net_device *dev; |
| 58 | struct list_head list; |
| 59 | struct hlist_node hlist; |
| 60 | struct macvlan_port *port; |
| 61 | struct net_device *lowerdev; |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 62 | struct macvlan_rx_stats *rx_stats; |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 63 | enum macvlan_mode mode; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | |
| 67 | static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, |
| 68 | const unsigned char *addr) |
| 69 | { |
| 70 | struct macvlan_dev *vlan; |
| 71 | struct hlist_node *n; |
| 72 | |
| 73 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) { |
Eric Dumazet | ac06713 | 2009-09-01 05:46:05 +0000 | [diff] [blame] | 74 | if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr)) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 75 | return vlan; |
| 76 | } |
| 77 | return NULL; |
| 78 | } |
| 79 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 80 | static void macvlan_hash_add(struct macvlan_dev *vlan) |
| 81 | { |
| 82 | struct macvlan_port *port = vlan->port; |
| 83 | const unsigned char *addr = vlan->dev->dev_addr; |
| 84 | |
| 85 | hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); |
| 86 | } |
| 87 | |
| 88 | static void macvlan_hash_del(struct macvlan_dev *vlan) |
| 89 | { |
| 90 | hlist_del_rcu(&vlan->hlist); |
| 91 | synchronize_rcu(); |
| 92 | } |
| 93 | |
| 94 | static void macvlan_hash_change_addr(struct macvlan_dev *vlan, |
| 95 | const unsigned char *addr) |
| 96 | { |
| 97 | macvlan_hash_del(vlan); |
| 98 | /* Now that we are unhashed it is safe to change the device |
| 99 | * address without confusing packet delivery. |
| 100 | */ |
| 101 | memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); |
| 102 | macvlan_hash_add(vlan); |
| 103 | } |
| 104 | |
| 105 | static int macvlan_addr_busy(const struct macvlan_port *port, |
| 106 | const unsigned char *addr) |
| 107 | { |
| 108 | /* Test to see if the specified multicast address is |
| 109 | * currently in use by the underlying device or |
| 110 | * another macvlan. |
| 111 | */ |
Eric Dumazet | ac06713 | 2009-09-01 05:46:05 +0000 | [diff] [blame] | 112 | if (!compare_ether_addr_64bits(port->dev->dev_addr, addr)) |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 113 | return 1; |
| 114 | |
| 115 | if (macvlan_hash_lookup(port, addr)) |
| 116 | return 1; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 121 | static inline void macvlan_count_rx(const struct macvlan_dev *vlan, |
| 122 | unsigned int len, bool success, |
| 123 | bool multicast) |
| 124 | { |
| 125 | struct macvlan_rx_stats *rx_stats; |
| 126 | |
| 127 | rx_stats = per_cpu_ptr(vlan->rx_stats, smp_processor_id()); |
| 128 | if (likely(success)) { |
| 129 | rx_stats->rx_packets++;; |
| 130 | rx_stats->rx_bytes += len; |
| 131 | if (multicast) |
| 132 | rx_stats->multicast++; |
| 133 | } else { |
| 134 | rx_stats->rx_errors++; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static int macvlan_broadcast_one(struct sk_buff *skb, struct net_device *dev, |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 139 | const struct ethhdr *eth, bool local) |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 140 | { |
| 141 | if (!skb) |
| 142 | return NET_RX_DROP; |
| 143 | |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 144 | if (local) |
| 145 | return dev_forward_skb(dev, skb); |
| 146 | |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 147 | skb->dev = dev; |
| 148 | if (!compare_ether_addr_64bits(eth->h_dest, |
| 149 | dev->broadcast)) |
| 150 | skb->pkt_type = PACKET_BROADCAST; |
| 151 | else |
| 152 | skb->pkt_type = PACKET_MULTICAST; |
| 153 | |
| 154 | return netif_rx(skb); |
| 155 | } |
| 156 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 157 | static void macvlan_broadcast(struct sk_buff *skb, |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 158 | const struct macvlan_port *port, |
| 159 | struct net_device *src, |
| 160 | enum macvlan_mode mode) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 161 | { |
| 162 | const struct ethhdr *eth = eth_hdr(skb); |
| 163 | const struct macvlan_dev *vlan; |
| 164 | struct hlist_node *n; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 165 | struct sk_buff *nskb; |
| 166 | unsigned int i; |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 167 | int err; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 168 | |
Patrick McHardy | efbbced | 2008-11-26 15:30:48 -0800 | [diff] [blame] | 169 | if (skb->protocol == htons(ETH_P_PAUSE)) |
| 170 | return; |
| 171 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 172 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) { |
| 173 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 174 | if (vlan->dev == src || !(vlan->mode & mode)) |
| 175 | continue; |
| 176 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 177 | nskb = skb_clone(skb, GFP_ATOMIC); |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 178 | err = macvlan_broadcast_one(nskb, vlan->dev, eth, |
| 179 | mode == MACVLAN_MODE_BRIDGE); |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 180 | macvlan_count_rx(vlan, skb->len + ETH_HLEN, |
| 181 | err == NET_RX_SUCCESS, 1); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /* called under rcu_read_lock() from netif_receive_skb */ |
| 187 | static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb) |
| 188 | { |
| 189 | const struct ethhdr *eth = eth_hdr(skb); |
| 190 | const struct macvlan_port *port; |
| 191 | const struct macvlan_dev *vlan; |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 192 | const struct macvlan_dev *src; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 193 | struct net_device *dev; |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 194 | unsigned int len; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 195 | |
| 196 | port = rcu_dereference(skb->dev->macvlan_port); |
| 197 | if (port == NULL) |
| 198 | return skb; |
| 199 | |
| 200 | if (is_multicast_ether_addr(eth->h_dest)) { |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 201 | src = macvlan_hash_lookup(port, eth->h_source); |
| 202 | if (!src) |
| 203 | /* frame comes from an external address */ |
| 204 | macvlan_broadcast(skb, port, NULL, |
| 205 | MACVLAN_MODE_PRIVATE | |
| 206 | MACVLAN_MODE_VEPA | |
| 207 | MACVLAN_MODE_BRIDGE); |
| 208 | else if (src->mode == MACVLAN_MODE_VEPA) |
| 209 | /* flood to everyone except source */ |
| 210 | macvlan_broadcast(skb, port, src->dev, |
| 211 | MACVLAN_MODE_VEPA | |
| 212 | MACVLAN_MODE_BRIDGE); |
| 213 | else if (src->mode == MACVLAN_MODE_BRIDGE) |
| 214 | /* |
| 215 | * flood only to VEPA ports, bridge ports |
| 216 | * already saw the frame on the way out. |
| 217 | */ |
| 218 | macvlan_broadcast(skb, port, src->dev, |
| 219 | MACVLAN_MODE_VEPA); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 220 | return skb; |
| 221 | } |
| 222 | |
| 223 | vlan = macvlan_hash_lookup(port, eth->h_dest); |
| 224 | if (vlan == NULL) |
| 225 | return skb; |
| 226 | |
| 227 | dev = vlan->dev; |
| 228 | if (unlikely(!(dev->flags & IFF_UP))) { |
| 229 | kfree_skb(skb); |
| 230 | return NULL; |
| 231 | } |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 232 | len = skb->len + ETH_HLEN; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 233 | skb = skb_share_check(skb, GFP_ATOMIC); |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 234 | macvlan_count_rx(vlan, len, skb != NULL, 0); |
| 235 | if (!skb) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 236 | return NULL; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 237 | |
| 238 | skb->dev = dev; |
| 239 | skb->pkt_type = PACKET_HOST; |
| 240 | |
| 241 | netif_rx(skb); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 245 | static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) |
| 246 | { |
| 247 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 248 | const struct macvlan_port *port = vlan->port; |
| 249 | const struct macvlan_dev *dest; |
| 250 | |
| 251 | if (vlan->mode == MACVLAN_MODE_BRIDGE) { |
| 252 | const struct ethhdr *eth = (void *)skb->data; |
| 253 | |
| 254 | /* send to other bridge ports directly */ |
| 255 | if (is_multicast_ether_addr(eth->h_dest)) { |
| 256 | macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); |
| 257 | goto xmit_world; |
| 258 | } |
| 259 | |
| 260 | dest = macvlan_hash_lookup(port, eth->h_dest); |
| 261 | if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { |
| 262 | unsigned int length = skb->len + ETH_HLEN; |
| 263 | int ret = dev_forward_skb(dest->dev, skb); |
| 264 | macvlan_count_rx(dest, length, |
| 265 | ret == NET_RX_SUCCESS, 0); |
| 266 | |
| 267 | return NET_XMIT_SUCCESS; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | xmit_world: |
| 272 | skb->dev = vlan->lowerdev; |
| 273 | return dev_queue_xmit(skb); |
| 274 | } |
| 275 | |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 276 | static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, |
| 277 | struct net_device *dev) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 278 | { |
Eric Dumazet | 2c11455 | 2009-09-03 00:11:45 +0000 | [diff] [blame] | 279 | int i = skb_get_queue_mapping(skb); |
| 280 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 281 | unsigned int len = skb->len; |
| 282 | int ret; |
| 283 | |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 284 | ret = macvlan_queue_xmit(skb, dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 285 | if (likely(ret == NET_XMIT_SUCCESS)) { |
Eric Dumazet | 2c11455 | 2009-09-03 00:11:45 +0000 | [diff] [blame] | 286 | txq->tx_packets++; |
| 287 | txq->tx_bytes += len; |
| 288 | } else |
| 289 | txq->tx_dropped++; |
| 290 | |
Patrick McHardy | cbbef5e | 2009-11-10 06:14:24 +0000 | [diff] [blame] | 291 | return ret; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | 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] | 295 | unsigned short type, const void *daddr, |
| 296 | const void *saddr, unsigned len) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 297 | { |
| 298 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 299 | struct net_device *lowerdev = vlan->lowerdev; |
| 300 | |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 301 | return dev_hard_header(skb, lowerdev, type, daddr, |
| 302 | saddr ? : dev->dev_addr, len); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 305 | static const struct header_ops macvlan_hard_header_ops = { |
| 306 | .create = macvlan_hard_header, |
| 307 | .rebuild = eth_rebuild_header, |
| 308 | .parse = eth_header_parse, |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 309 | .cache = eth_header_cache, |
| 310 | .cache_update = eth_header_cache_update, |
| 311 | }; |
| 312 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 313 | static int macvlan_open(struct net_device *dev) |
| 314 | { |
| 315 | struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 316 | struct net_device *lowerdev = vlan->lowerdev; |
| 317 | int err; |
| 318 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 319 | err = -EBUSY; |
| 320 | if (macvlan_addr_busy(vlan->port, dev->dev_addr)) |
| 321 | goto out; |
| 322 | |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 323 | err = dev_unicast_add(lowerdev, dev->dev_addr); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 324 | if (err < 0) |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 325 | goto out; |
| 326 | if (dev->flags & IFF_ALLMULTI) { |
| 327 | err = dev_set_allmulti(lowerdev, 1); |
| 328 | if (err < 0) |
| 329 | goto del_unicast; |
| 330 | } |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 331 | macvlan_hash_add(vlan); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 332 | return 0; |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 333 | |
| 334 | del_unicast: |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 335 | dev_unicast_delete(lowerdev, dev->dev_addr); |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 336 | out: |
| 337 | return err; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static int macvlan_stop(struct net_device *dev) |
| 341 | { |
| 342 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 343 | struct net_device *lowerdev = vlan->lowerdev; |
| 344 | |
| 345 | dev_mc_unsync(lowerdev, dev); |
| 346 | if (dev->flags & IFF_ALLMULTI) |
| 347 | dev_set_allmulti(lowerdev, -1); |
| 348 | |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 349 | dev_unicast_delete(lowerdev, dev->dev_addr); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 350 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 351 | macvlan_hash_del(vlan); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 355 | static int macvlan_set_mac_address(struct net_device *dev, void *p) |
| 356 | { |
| 357 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 358 | struct net_device *lowerdev = vlan->lowerdev; |
| 359 | struct sockaddr *addr = p; |
| 360 | int err; |
| 361 | |
| 362 | if (!is_valid_ether_addr(addr->sa_data)) |
| 363 | return -EADDRNOTAVAIL; |
| 364 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 365 | if (!(dev->flags & IFF_UP)) { |
| 366 | /* Just copy in the new address */ |
| 367 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
| 368 | } else { |
| 369 | /* Rehash and update the device filters */ |
| 370 | if (macvlan_addr_busy(vlan->port, addr->sa_data)) |
| 371 | return -EBUSY; |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 372 | |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 373 | err = dev_unicast_add(lowerdev, addr->sa_data); |
| 374 | if (err) |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 375 | return err; |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 376 | |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 377 | dev_unicast_delete(lowerdev, dev->dev_addr); |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 378 | |
| 379 | macvlan_hash_change_addr(vlan, addr->sa_data); |
| 380 | } |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 384 | static void macvlan_change_rx_flags(struct net_device *dev, int change) |
| 385 | { |
| 386 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 387 | struct net_device *lowerdev = vlan->lowerdev; |
| 388 | |
| 389 | if (change & IFF_ALLMULTI) |
| 390 | dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); |
| 391 | } |
| 392 | |
| 393 | static void macvlan_set_multicast_list(struct net_device *dev) |
| 394 | { |
| 395 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 396 | |
| 397 | dev_mc_sync(vlan->lowerdev, dev); |
| 398 | } |
| 399 | |
| 400 | static int macvlan_change_mtu(struct net_device *dev, int new_mtu) |
| 401 | { |
| 402 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 403 | |
| 404 | if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) |
| 405 | return -EINVAL; |
| 406 | dev->mtu = new_mtu; |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * macvlan network devices have devices nesting below it and are a special |
| 412 | * "super class" of normal network devices; split their locks off into a |
| 413 | * separate class since they always nest. |
| 414 | */ |
| 415 | static struct lock_class_key macvlan_netdev_xmit_lock_key; |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 416 | static struct lock_class_key macvlan_netdev_addr_lock_key; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 417 | |
| 418 | #define MACVLAN_FEATURES \ |
| 419 | (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ |
| 420 | NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ |
| 421 | NETIF_F_TSO_ECN | NETIF_F_TSO6) |
| 422 | |
| 423 | #define MACVLAN_STATE_MASK \ |
| 424 | ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) |
| 425 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 426 | static void macvlan_set_lockdep_class_one(struct net_device *dev, |
| 427 | struct netdev_queue *txq, |
| 428 | void *_unused) |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 429 | { |
| 430 | lockdep_set_class(&txq->_xmit_lock, |
| 431 | &macvlan_netdev_xmit_lock_key); |
| 432 | } |
| 433 | |
| 434 | static void macvlan_set_lockdep_class(struct net_device *dev) |
| 435 | { |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 436 | lockdep_set_class(&dev->addr_list_lock, |
| 437 | &macvlan_netdev_addr_lock_key); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 438 | 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] | 439 | } |
| 440 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 441 | static int macvlan_init(struct net_device *dev) |
| 442 | { |
| 443 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 444 | const struct net_device *lowerdev = vlan->lowerdev; |
| 445 | |
| 446 | dev->state = (dev->state & ~MACVLAN_STATE_MASK) | |
| 447 | (lowerdev->state & MACVLAN_STATE_MASK); |
| 448 | dev->features = lowerdev->features & MACVLAN_FEATURES; |
Patrick McHardy | 8c2acc5 | 2009-11-23 14:18:53 -0800 | [diff] [blame] | 449 | dev->gso_max_size = lowerdev->gso_max_size; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 450 | dev->iflink = lowerdev->ifindex; |
sg.tweak@gmail.com | ef5c899 | 2009-06-10 09:55:02 +0000 | [diff] [blame] | 451 | dev->hard_header_len = lowerdev->hard_header_len; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 452 | |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 453 | macvlan_set_lockdep_class(dev); |
| 454 | |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 455 | vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats); |
| 456 | if (!vlan->rx_stats) |
| 457 | return -ENOMEM; |
| 458 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 462 | static void macvlan_uninit(struct net_device *dev) |
| 463 | { |
| 464 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 465 | |
| 466 | free_percpu(vlan->rx_stats); |
| 467 | } |
| 468 | |
| 469 | static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev) |
| 470 | { |
| 471 | struct net_device_stats *stats = &dev->stats; |
| 472 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 473 | |
| 474 | dev_txq_stats_fold(dev, stats); |
| 475 | |
| 476 | if (vlan->rx_stats) { |
| 477 | struct macvlan_rx_stats *p, rx = {0}; |
| 478 | int i; |
| 479 | |
| 480 | for_each_possible_cpu(i) { |
| 481 | p = per_cpu_ptr(vlan->rx_stats, i); |
| 482 | rx.rx_packets += p->rx_packets; |
| 483 | rx.rx_bytes += p->rx_bytes; |
| 484 | rx.rx_errors += p->rx_errors; |
| 485 | rx.multicast += p->multicast; |
| 486 | } |
| 487 | stats->rx_packets = rx.rx_packets; |
| 488 | stats->rx_bytes = rx.rx_bytes; |
| 489 | stats->rx_errors = rx.rx_errors; |
| 490 | stats->rx_dropped = rx.rx_errors; |
| 491 | stats->multicast = rx.multicast; |
| 492 | } |
| 493 | return stats; |
| 494 | } |
| 495 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 496 | static void macvlan_ethtool_get_drvinfo(struct net_device *dev, |
| 497 | struct ethtool_drvinfo *drvinfo) |
| 498 | { |
| 499 | snprintf(drvinfo->driver, 32, "macvlan"); |
| 500 | snprintf(drvinfo->version, 32, "0.1"); |
| 501 | } |
| 502 | |
| 503 | static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) |
| 504 | { |
| 505 | const struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b1b67dd | 2009-04-20 04:49:28 +0000 | [diff] [blame] | 506 | return dev_ethtool_get_rx_csum(vlan->lowerdev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 509 | static int macvlan_ethtool_get_settings(struct net_device *dev, |
| 510 | struct ethtool_cmd *cmd) |
| 511 | { |
| 512 | const struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b1b67dd | 2009-04-20 04:49:28 +0000 | [diff] [blame] | 513 | return dev_ethtool_get_settings(vlan->lowerdev, cmd); |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static u32 macvlan_ethtool_get_flags(struct net_device *dev) |
| 517 | { |
| 518 | const struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b1b67dd | 2009-04-20 04:49:28 +0000 | [diff] [blame] | 519 | return dev_ethtool_get_flags(vlan->lowerdev); |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 522 | static const struct ethtool_ops macvlan_ethtool_ops = { |
| 523 | .get_link = ethtool_op_get_link, |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 524 | .get_settings = macvlan_ethtool_get_settings, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 525 | .get_rx_csum = macvlan_ethtool_get_rx_csum, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 526 | .get_drvinfo = macvlan_ethtool_get_drvinfo, |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 527 | .get_flags = macvlan_ethtool_get_flags, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 528 | }; |
| 529 | |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 530 | static const struct net_device_ops macvlan_netdev_ops = { |
| 531 | .ndo_init = macvlan_init, |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 532 | .ndo_uninit = macvlan_uninit, |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 533 | .ndo_open = macvlan_open, |
| 534 | .ndo_stop = macvlan_stop, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 535 | .ndo_start_xmit = macvlan_start_xmit, |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 536 | .ndo_change_mtu = macvlan_change_mtu, |
| 537 | .ndo_change_rx_flags = macvlan_change_rx_flags, |
| 538 | .ndo_set_mac_address = macvlan_set_mac_address, |
| 539 | .ndo_set_multicast_list = macvlan_set_multicast_list, |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 540 | .ndo_get_stats = macvlan_dev_get_stats, |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 541 | .ndo_validate_addr = eth_validate_addr, |
| 542 | }; |
| 543 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 544 | static void macvlan_setup(struct net_device *dev) |
| 545 | { |
| 546 | ether_setup(dev); |
| 547 | |
Eric Dumazet | 93f154b | 2009-05-18 22:19:19 -0700 | [diff] [blame] | 548 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 549 | dev->netdev_ops = &macvlan_netdev_ops; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 550 | dev->destructor = free_netdev; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 551 | dev->header_ops = &macvlan_hard_header_ops, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 552 | dev->ethtool_ops = &macvlan_ethtool_ops; |
| 553 | dev->tx_queue_len = 0; |
| 554 | } |
| 555 | |
| 556 | static int macvlan_port_create(struct net_device *dev) |
| 557 | { |
| 558 | struct macvlan_port *port; |
| 559 | unsigned int i; |
| 560 | |
| 561 | if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) |
| 562 | return -EINVAL; |
| 563 | |
| 564 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
| 565 | if (port == NULL) |
| 566 | return -ENOMEM; |
| 567 | |
| 568 | port->dev = dev; |
| 569 | INIT_LIST_HEAD(&port->vlans); |
| 570 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) |
| 571 | INIT_HLIST_HEAD(&port->vlan_hash[i]); |
| 572 | rcu_assign_pointer(dev->macvlan_port, port); |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static void macvlan_port_destroy(struct net_device *dev) |
| 577 | { |
| 578 | struct macvlan_port *port = dev->macvlan_port; |
| 579 | |
| 580 | rcu_assign_pointer(dev->macvlan_port, NULL); |
| 581 | synchronize_rcu(); |
| 582 | kfree(port); |
| 583 | } |
| 584 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 585 | static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 586 | { |
| 587 | if (tb[IFLA_ADDRESS]) { |
| 588 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 589 | return -EINVAL; |
| 590 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 591 | return -EADDRNOTAVAIL; |
| 592 | } |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 593 | |
| 594 | if (data && data[IFLA_MACVLAN_MODE]) { |
| 595 | switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { |
| 596 | case MACVLAN_MODE_PRIVATE: |
| 597 | case MACVLAN_MODE_VEPA: |
| 598 | case MACVLAN_MODE_BRIDGE: |
| 599 | break; |
| 600 | default: |
| 601 | return -EINVAL; |
| 602 | } |
| 603 | } |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | |
Eric Dumazet | 2c11455 | 2009-09-03 00:11:45 +0000 | [diff] [blame] | 607 | static int macvlan_get_tx_queues(struct net *net, |
| 608 | struct nlattr *tb[], |
| 609 | unsigned int *num_tx_queues, |
| 610 | unsigned int *real_num_tx_queues) |
| 611 | { |
| 612 | struct net_device *real_dev; |
| 613 | |
| 614 | if (!tb[IFLA_LINK]) |
| 615 | return -EINVAL; |
| 616 | |
| 617 | real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK])); |
| 618 | if (!real_dev) |
| 619 | return -ENODEV; |
| 620 | |
| 621 | *num_tx_queues = real_dev->num_tx_queues; |
| 622 | *real_num_tx_queues = real_dev->real_num_tx_queues; |
| 623 | return 0; |
| 624 | } |
| 625 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 626 | static int macvlan_newlink(struct net *src_net, struct net_device *dev, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 627 | struct nlattr *tb[], struct nlattr *data[]) |
| 628 | { |
| 629 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 630 | struct macvlan_port *port; |
| 631 | struct net_device *lowerdev; |
| 632 | int err; |
| 633 | |
| 634 | if (!tb[IFLA_LINK]) |
| 635 | return -EINVAL; |
| 636 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 637 | lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 638 | if (lowerdev == NULL) |
| 639 | return -ENODEV; |
| 640 | |
Eric Biederman | b0832a2 | 2009-03-13 13:15:37 -0700 | [diff] [blame] | 641 | /* When creating macvlans on top of other macvlans - use |
| 642 | * the real device as the lowerdev. |
Patrick McHardy | a6ca5f1 | 2008-01-10 22:39:28 -0800 | [diff] [blame] | 643 | */ |
Eric Biederman | b0832a2 | 2009-03-13 13:15:37 -0700 | [diff] [blame] | 644 | if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { |
| 645 | struct macvlan_dev *lowervlan = netdev_priv(lowerdev); |
| 646 | lowerdev = lowervlan->lowerdev; |
| 647 | } |
Patrick McHardy | a6ca5f1 | 2008-01-10 22:39:28 -0800 | [diff] [blame] | 648 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 649 | if (!tb[IFLA_MTU]) |
| 650 | dev->mtu = lowerdev->mtu; |
| 651 | else if (dev->mtu > lowerdev->mtu) |
| 652 | return -EINVAL; |
| 653 | |
| 654 | if (!tb[IFLA_ADDRESS]) |
| 655 | random_ether_addr(dev->dev_addr); |
| 656 | |
| 657 | if (lowerdev->macvlan_port == NULL) { |
| 658 | err = macvlan_port_create(lowerdev); |
| 659 | if (err < 0) |
| 660 | return err; |
| 661 | } |
| 662 | port = lowerdev->macvlan_port; |
| 663 | |
| 664 | vlan->lowerdev = lowerdev; |
| 665 | vlan->dev = dev; |
| 666 | vlan->port = port; |
| 667 | |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 668 | vlan->mode = MACVLAN_MODE_VEPA; |
| 669 | if (data && data[IFLA_MACVLAN_MODE]) |
| 670 | vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); |
| 671 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 672 | err = register_netdevice(dev); |
| 673 | if (err < 0) |
| 674 | return err; |
| 675 | |
| 676 | list_add_tail(&vlan->list, &port->vlans); |
Patrick Mullaney | fc4a748 | 2009-12-03 15:59:22 -0800 | [diff] [blame] | 677 | netif_stacked_transfer_operstate(lowerdev, dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 681 | static void macvlan_dellink(struct net_device *dev, struct list_head *head) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 682 | { |
| 683 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 684 | struct macvlan_port *port = vlan->port; |
| 685 | |
| 686 | list_del(&vlan->list); |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 687 | unregister_netdevice_queue(dev, head); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 688 | |
| 689 | if (list_empty(&port->vlans)) |
Patrick McHardy | 7312096 | 2008-05-08 01:13:31 -0700 | [diff] [blame] | 690 | macvlan_port_destroy(port->dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 693 | static int macvlan_changelink(struct net_device *dev, |
| 694 | struct nlattr *tb[], struct nlattr *data[]) |
| 695 | { |
| 696 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 697 | if (data && data[IFLA_MACVLAN_MODE]) |
| 698 | vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static size_t macvlan_get_size(const struct net_device *dev) |
| 703 | { |
| 704 | return nla_total_size(4); |
| 705 | } |
| 706 | |
| 707 | static int macvlan_fill_info(struct sk_buff *skb, |
| 708 | const struct net_device *dev) |
| 709 | { |
| 710 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 711 | |
| 712 | NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode); |
| 713 | return 0; |
| 714 | |
| 715 | nla_put_failure: |
| 716 | return -EMSGSIZE; |
| 717 | } |
| 718 | |
| 719 | static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { |
| 720 | [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, |
| 721 | }; |
| 722 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 723 | static struct rtnl_link_ops macvlan_link_ops __read_mostly = { |
| 724 | .kind = "macvlan", |
| 725 | .priv_size = sizeof(struct macvlan_dev), |
Eric Dumazet | 2c11455 | 2009-09-03 00:11:45 +0000 | [diff] [blame] | 726 | .get_tx_queues = macvlan_get_tx_queues, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 727 | .setup = macvlan_setup, |
| 728 | .validate = macvlan_validate, |
| 729 | .newlink = macvlan_newlink, |
| 730 | .dellink = macvlan_dellink, |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 731 | .maxtype = IFLA_MACVLAN_MAX, |
| 732 | .policy = macvlan_policy, |
| 733 | .changelink = macvlan_changelink, |
| 734 | .get_size = macvlan_get_size, |
| 735 | .fill_info = macvlan_fill_info, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 736 | }; |
| 737 | |
| 738 | static int macvlan_device_event(struct notifier_block *unused, |
| 739 | unsigned long event, void *ptr) |
| 740 | { |
| 741 | struct net_device *dev = ptr; |
| 742 | struct macvlan_dev *vlan, *next; |
| 743 | struct macvlan_port *port; |
| 744 | |
| 745 | port = dev->macvlan_port; |
| 746 | if (port == NULL) |
| 747 | return NOTIFY_DONE; |
| 748 | |
| 749 | switch (event) { |
| 750 | case NETDEV_CHANGE: |
| 751 | list_for_each_entry(vlan, &port->vlans, list) |
Patrick Mullaney | fc4a748 | 2009-12-03 15:59:22 -0800 | [diff] [blame] | 752 | netif_stacked_transfer_operstate(vlan->lowerdev, |
| 753 | vlan->dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 754 | break; |
| 755 | case NETDEV_FEAT_CHANGE: |
| 756 | list_for_each_entry(vlan, &port->vlans, list) { |
| 757 | vlan->dev->features = dev->features & MACVLAN_FEATURES; |
Patrick McHardy | 8c2acc5 | 2009-11-23 14:18:53 -0800 | [diff] [blame] | 758 | vlan->dev->gso_max_size = dev->gso_max_size; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 759 | netdev_features_change(vlan->dev); |
| 760 | } |
| 761 | break; |
| 762 | case NETDEV_UNREGISTER: |
| 763 | list_for_each_entry_safe(vlan, next, &port->vlans, list) |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 764 | macvlan_dellink(vlan->dev, NULL); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 765 | break; |
| 766 | } |
| 767 | return NOTIFY_DONE; |
| 768 | } |
| 769 | |
| 770 | static struct notifier_block macvlan_notifier_block __read_mostly = { |
| 771 | .notifier_call = macvlan_device_event, |
| 772 | }; |
| 773 | |
| 774 | static int __init macvlan_init_module(void) |
| 775 | { |
| 776 | int err; |
| 777 | |
| 778 | register_netdevice_notifier(&macvlan_notifier_block); |
| 779 | macvlan_handle_frame_hook = macvlan_handle_frame; |
| 780 | |
| 781 | err = rtnl_link_register(&macvlan_link_ops); |
| 782 | if (err < 0) |
| 783 | goto err1; |
| 784 | return 0; |
| 785 | err1: |
Rami Rosen | 5291324 | 2008-01-31 16:56:03 -0800 | [diff] [blame] | 786 | macvlan_handle_frame_hook = NULL; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 787 | unregister_netdevice_notifier(&macvlan_notifier_block); |
| 788 | return err; |
| 789 | } |
| 790 | |
| 791 | static void __exit macvlan_cleanup_module(void) |
| 792 | { |
| 793 | rtnl_link_unregister(&macvlan_link_ops); |
| 794 | macvlan_handle_frame_hook = NULL; |
| 795 | unregister_netdevice_notifier(&macvlan_notifier_block); |
| 796 | } |
| 797 | |
| 798 | module_init(macvlan_init_module); |
| 799 | module_exit(macvlan_cleanup_module); |
| 800 | |
| 801 | MODULE_LICENSE("GPL"); |
| 802 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 803 | MODULE_DESCRIPTION("Driver for MAC address based VLANs"); |
| 804 | MODULE_ALIAS_RTNL_LINK("macvlan"); |