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