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