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