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