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