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