blob: f893cd41580a0197a962abe39fe17190163af155 [file] [log] [blame]
Patrick McHardyb863ceb2007-07-14 18:55:06 -07001/*
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-Huu82524742008-05-12 21:21:05 +020023#include <linux/rculist.h>
Patrick McHardyb863ceb2007-07-14 18:55:06 -070024#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>
32
33#define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
34
35struct macvlan_port {
36 struct net_device *dev;
37 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
38 struct list_head vlans;
39};
40
41struct macvlan_dev {
42 struct net_device *dev;
43 struct list_head list;
44 struct hlist_node hlist;
45 struct macvlan_port *port;
46 struct net_device *lowerdev;
47};
48
49
50static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
51 const unsigned char *addr)
52{
53 struct macvlan_dev *vlan;
54 struct hlist_node *n;
55
56 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
57 if (!compare_ether_addr(vlan->dev->dev_addr, addr))
58 return vlan;
59 }
60 return NULL;
61}
62
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070063static void macvlan_hash_add(struct macvlan_dev *vlan)
64{
65 struct macvlan_port *port = vlan->port;
66 const unsigned char *addr = vlan->dev->dev_addr;
67
68 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
69}
70
71static void macvlan_hash_del(struct macvlan_dev *vlan)
72{
73 hlist_del_rcu(&vlan->hlist);
74 synchronize_rcu();
75}
76
77static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
78 const unsigned char *addr)
79{
80 macvlan_hash_del(vlan);
81 /* Now that we are unhashed it is safe to change the device
82 * address without confusing packet delivery.
83 */
84 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
85 macvlan_hash_add(vlan);
86}
87
88static int macvlan_addr_busy(const struct macvlan_port *port,
89 const unsigned char *addr)
90{
91 /* Test to see if the specified multicast address is
92 * currently in use by the underlying device or
93 * another macvlan.
94 */
95 if (memcmp(port->dev->dev_addr, addr, ETH_ALEN) == 0)
96 return 1;
97
98 if (macvlan_hash_lookup(port, addr))
99 return 1;
100
101 return 0;
102}
103
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700104static void macvlan_broadcast(struct sk_buff *skb,
105 const struct macvlan_port *port)
106{
107 const struct ethhdr *eth = eth_hdr(skb);
108 const struct macvlan_dev *vlan;
109 struct hlist_node *n;
110 struct net_device *dev;
111 struct sk_buff *nskb;
112 unsigned int i;
113
Patrick McHardyefbbced2008-11-26 15:30:48 -0800114 if (skb->protocol == htons(ETH_P_PAUSE))
115 return;
116
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700117 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
118 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
119 dev = vlan->dev;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700120
121 nskb = skb_clone(skb, GFP_ATOMIC);
122 if (nskb == NULL) {
123 dev->stats.rx_errors++;
124 dev->stats.rx_dropped++;
125 continue;
126 }
127
128 dev->stats.rx_bytes += skb->len + ETH_HLEN;
129 dev->stats.rx_packets++;
130 dev->stats.multicast++;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700131
132 nskb->dev = dev;
133 if (!compare_ether_addr(eth->h_dest, dev->broadcast))
134 nskb->pkt_type = PACKET_BROADCAST;
135 else
136 nskb->pkt_type = PACKET_MULTICAST;
137
138 netif_rx(nskb);
139 }
140 }
141}
142
143/* called under rcu_read_lock() from netif_receive_skb */
144static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
145{
146 const struct ethhdr *eth = eth_hdr(skb);
147 const struct macvlan_port *port;
148 const struct macvlan_dev *vlan;
149 struct net_device *dev;
150
151 port = rcu_dereference(skb->dev->macvlan_port);
152 if (port == NULL)
153 return skb;
154
155 if (is_multicast_ether_addr(eth->h_dest)) {
156 macvlan_broadcast(skb, port);
157 return skb;
158 }
159
160 vlan = macvlan_hash_lookup(port, eth->h_dest);
161 if (vlan == NULL)
162 return skb;
163
164 dev = vlan->dev;
165 if (unlikely(!(dev->flags & IFF_UP))) {
166 kfree_skb(skb);
167 return NULL;
168 }
169
170 skb = skb_share_check(skb, GFP_ATOMIC);
171 if (skb == NULL) {
172 dev->stats.rx_errors++;
173 dev->stats.rx_dropped++;
174 return NULL;
175 }
176
177 dev->stats.rx_bytes += skb->len + ETH_HLEN;
178 dev->stats.rx_packets++;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700179
180 skb->dev = dev;
181 skb->pkt_type = PACKET_HOST;
182
183 netif_rx(skb);
184 return NULL;
185}
186
Stephen Hemminger424efe92009-08-31 19:50:51 +0000187static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
188 struct net_device *dev)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700189{
190 const struct macvlan_dev *vlan = netdev_priv(dev);
191 unsigned int len = skb->len;
192 int ret;
193
194 skb->dev = vlan->lowerdev;
195 ret = dev_queue_xmit(skb);
196
197 if (likely(ret == NET_XMIT_SUCCESS)) {
198 dev->stats.tx_packets++;
199 dev->stats.tx_bytes += len;
200 } else {
201 dev->stats.tx_errors++;
202 dev->stats.tx_aborted_errors++;
203 }
204 return NETDEV_TX_OK;
205}
206
207static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700208 unsigned short type, const void *daddr,
209 const void *saddr, unsigned len)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700210{
211 const struct macvlan_dev *vlan = netdev_priv(dev);
212 struct net_device *lowerdev = vlan->lowerdev;
213
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700214 return dev_hard_header(skb, lowerdev, type, daddr,
215 saddr ? : dev->dev_addr, len);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700216}
217
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700218static const struct header_ops macvlan_hard_header_ops = {
219 .create = macvlan_hard_header,
220 .rebuild = eth_rebuild_header,
221 .parse = eth_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700222 .cache = eth_header_cache,
223 .cache_update = eth_header_cache_update,
224};
225
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700226static int macvlan_open(struct net_device *dev)
227{
228 struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700229 struct net_device *lowerdev = vlan->lowerdev;
230 int err;
231
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700232 err = -EBUSY;
233 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
234 goto out;
235
Jiri Pirkoccffad252009-05-22 23:22:17 +0000236 err = dev_unicast_add(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700237 if (err < 0)
Wang Chenb89fb7d2008-07-14 20:57:07 -0700238 goto out;
239 if (dev->flags & IFF_ALLMULTI) {
240 err = dev_set_allmulti(lowerdev, 1);
241 if (err < 0)
242 goto del_unicast;
243 }
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700244 macvlan_hash_add(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700245 return 0;
Wang Chenb89fb7d2008-07-14 20:57:07 -0700246
247del_unicast:
Jiri Pirkoccffad252009-05-22 23:22:17 +0000248 dev_unicast_delete(lowerdev, dev->dev_addr);
Wang Chenb89fb7d2008-07-14 20:57:07 -0700249out:
250 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700251}
252
253static int macvlan_stop(struct net_device *dev)
254{
255 struct macvlan_dev *vlan = netdev_priv(dev);
256 struct net_device *lowerdev = vlan->lowerdev;
257
258 dev_mc_unsync(lowerdev, dev);
259 if (dev->flags & IFF_ALLMULTI)
260 dev_set_allmulti(lowerdev, -1);
261
Jiri Pirkoccffad252009-05-22 23:22:17 +0000262 dev_unicast_delete(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700263
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700264 macvlan_hash_del(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700265 return 0;
266}
267
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800268static int macvlan_set_mac_address(struct net_device *dev, void *p)
269{
270 struct macvlan_dev *vlan = netdev_priv(dev);
271 struct net_device *lowerdev = vlan->lowerdev;
272 struct sockaddr *addr = p;
273 int err;
274
275 if (!is_valid_ether_addr(addr->sa_data))
276 return -EADDRNOTAVAIL;
277
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700278 if (!(dev->flags & IFF_UP)) {
279 /* Just copy in the new address */
280 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
281 } else {
282 /* Rehash and update the device filters */
283 if (macvlan_addr_busy(vlan->port, addr->sa_data))
284 return -EBUSY;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800285
Jiri Pirkoccffad252009-05-22 23:22:17 +0000286 err = dev_unicast_add(lowerdev, addr->sa_data);
287 if (err)
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700288 return err;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800289
Jiri Pirkoccffad252009-05-22 23:22:17 +0000290 dev_unicast_delete(lowerdev, dev->dev_addr);
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700291
292 macvlan_hash_change_addr(vlan, addr->sa_data);
293 }
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800294 return 0;
295}
296
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700297static void macvlan_change_rx_flags(struct net_device *dev, int change)
298{
299 struct macvlan_dev *vlan = netdev_priv(dev);
300 struct net_device *lowerdev = vlan->lowerdev;
301
302 if (change & IFF_ALLMULTI)
303 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
304}
305
306static void macvlan_set_multicast_list(struct net_device *dev)
307{
308 struct macvlan_dev *vlan = netdev_priv(dev);
309
310 dev_mc_sync(vlan->lowerdev, dev);
311}
312
313static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
314{
315 struct macvlan_dev *vlan = netdev_priv(dev);
316
317 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
318 return -EINVAL;
319 dev->mtu = new_mtu;
320 return 0;
321}
322
323/*
324 * macvlan network devices have devices nesting below it and are a special
325 * "super class" of normal network devices; split their locks off into a
326 * separate class since they always nest.
327 */
328static struct lock_class_key macvlan_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -0700329static struct lock_class_key macvlan_netdev_addr_lock_key;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700330
331#define MACVLAN_FEATURES \
332 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
333 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
334 NETIF_F_TSO_ECN | NETIF_F_TSO6)
335
336#define MACVLAN_STATE_MASK \
337 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
338
David S. Millere8a04642008-07-17 00:34:19 -0700339static void macvlan_set_lockdep_class_one(struct net_device *dev,
340 struct netdev_queue *txq,
341 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -0700342{
343 lockdep_set_class(&txq->_xmit_lock,
344 &macvlan_netdev_xmit_lock_key);
345}
346
347static void macvlan_set_lockdep_class(struct net_device *dev)
348{
David S. Millercf508b12008-07-22 14:16:42 -0700349 lockdep_set_class(&dev->addr_list_lock,
350 &macvlan_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -0700351 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -0700352}
353
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700354static int macvlan_init(struct net_device *dev)
355{
356 struct macvlan_dev *vlan = netdev_priv(dev);
357 const struct net_device *lowerdev = vlan->lowerdev;
358
359 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
360 (lowerdev->state & MACVLAN_STATE_MASK);
361 dev->features = lowerdev->features & MACVLAN_FEATURES;
362 dev->iflink = lowerdev->ifindex;
sg.tweak@gmail.comef5c8992009-06-10 09:55:02 +0000363 dev->hard_header_len = lowerdev->hard_header_len;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700364
David S. Millerc773e842008-07-08 23:13:53 -0700365 macvlan_set_lockdep_class(dev);
366
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700367 return 0;
368}
369
370static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
371 struct ethtool_drvinfo *drvinfo)
372{
373 snprintf(drvinfo->driver, 32, "macvlan");
374 snprintf(drvinfo->version, 32, "0.1");
375}
376
377static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
378{
379 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000380 return dev_ethtool_get_rx_csum(vlan->lowerdev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700381}
382
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700383static int macvlan_ethtool_get_settings(struct net_device *dev,
384 struct ethtool_cmd *cmd)
385{
386 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000387 return dev_ethtool_get_settings(vlan->lowerdev, cmd);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700388}
389
390static u32 macvlan_ethtool_get_flags(struct net_device *dev)
391{
392 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000393 return dev_ethtool_get_flags(vlan->lowerdev);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700394}
395
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700396static const struct ethtool_ops macvlan_ethtool_ops = {
397 .get_link = ethtool_op_get_link,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700398 .get_settings = macvlan_ethtool_get_settings,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700399 .get_rx_csum = macvlan_ethtool_get_rx_csum,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700400 .get_drvinfo = macvlan_ethtool_get_drvinfo,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700401 .get_flags = macvlan_ethtool_get_flags,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700402};
403
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800404static const struct net_device_ops macvlan_netdev_ops = {
405 .ndo_init = macvlan_init,
406 .ndo_open = macvlan_open,
407 .ndo_stop = macvlan_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -0800408 .ndo_start_xmit = macvlan_start_xmit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800409 .ndo_change_mtu = macvlan_change_mtu,
410 .ndo_change_rx_flags = macvlan_change_rx_flags,
411 .ndo_set_mac_address = macvlan_set_mac_address,
412 .ndo_set_multicast_list = macvlan_set_multicast_list,
413 .ndo_validate_addr = eth_validate_addr,
414};
415
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700416static void macvlan_setup(struct net_device *dev)
417{
418 ether_setup(dev);
419
Eric Dumazet93f154b2009-05-18 22:19:19 -0700420 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800421 dev->netdev_ops = &macvlan_netdev_ops;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700422 dev->destructor = free_netdev;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700423 dev->header_ops = &macvlan_hard_header_ops,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700424 dev->ethtool_ops = &macvlan_ethtool_ops;
425 dev->tx_queue_len = 0;
426}
427
428static int macvlan_port_create(struct net_device *dev)
429{
430 struct macvlan_port *port;
431 unsigned int i;
432
433 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
434 return -EINVAL;
435
436 port = kzalloc(sizeof(*port), GFP_KERNEL);
437 if (port == NULL)
438 return -ENOMEM;
439
440 port->dev = dev;
441 INIT_LIST_HEAD(&port->vlans);
442 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
443 INIT_HLIST_HEAD(&port->vlan_hash[i]);
444 rcu_assign_pointer(dev->macvlan_port, port);
445 return 0;
446}
447
448static void macvlan_port_destroy(struct net_device *dev)
449{
450 struct macvlan_port *port = dev->macvlan_port;
451
452 rcu_assign_pointer(dev->macvlan_port, NULL);
453 synchronize_rcu();
454 kfree(port);
455}
456
457static void macvlan_transfer_operstate(struct net_device *dev)
458{
459 struct macvlan_dev *vlan = netdev_priv(dev);
460 const struct net_device *lowerdev = vlan->lowerdev;
461
462 if (lowerdev->operstate == IF_OPER_DORMANT)
463 netif_dormant_on(dev);
464 else
465 netif_dormant_off(dev);
466
467 if (netif_carrier_ok(lowerdev)) {
468 if (!netif_carrier_ok(dev))
469 netif_carrier_on(dev);
470 } else {
Patrick McHardyf12ca5f2008-01-21 00:47:08 -0800471 if (netif_carrier_ok(dev))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700472 netif_carrier_off(dev);
473 }
474}
475
476static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
477{
478 if (tb[IFLA_ADDRESS]) {
479 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
480 return -EINVAL;
481 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
482 return -EADDRNOTAVAIL;
483 }
484 return 0;
485}
486
487static int macvlan_newlink(struct net_device *dev,
488 struct nlattr *tb[], struct nlattr *data[])
489{
490 struct macvlan_dev *vlan = netdev_priv(dev);
491 struct macvlan_port *port;
492 struct net_device *lowerdev;
493 int err;
494
495 if (!tb[IFLA_LINK])
496 return -EINVAL;
497
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900498 lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700499 if (lowerdev == NULL)
500 return -ENODEV;
501
Eric Biedermanb0832a22009-03-13 13:15:37 -0700502 /* When creating macvlans on top of other macvlans - use
503 * the real device as the lowerdev.
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800504 */
Eric Biedermanb0832a22009-03-13 13:15:37 -0700505 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
506 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
507 lowerdev = lowervlan->lowerdev;
508 }
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800509
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700510 if (!tb[IFLA_MTU])
511 dev->mtu = lowerdev->mtu;
512 else if (dev->mtu > lowerdev->mtu)
513 return -EINVAL;
514
515 if (!tb[IFLA_ADDRESS])
516 random_ether_addr(dev->dev_addr);
517
518 if (lowerdev->macvlan_port == NULL) {
519 err = macvlan_port_create(lowerdev);
520 if (err < 0)
521 return err;
522 }
523 port = lowerdev->macvlan_port;
524
525 vlan->lowerdev = lowerdev;
526 vlan->dev = dev;
527 vlan->port = port;
528
529 err = register_netdevice(dev);
530 if (err < 0)
531 return err;
532
533 list_add_tail(&vlan->list, &port->vlans);
534 macvlan_transfer_operstate(dev);
535 return 0;
536}
537
538static void macvlan_dellink(struct net_device *dev)
539{
540 struct macvlan_dev *vlan = netdev_priv(dev);
541 struct macvlan_port *port = vlan->port;
542
543 list_del(&vlan->list);
544 unregister_netdevice(dev);
545
546 if (list_empty(&port->vlans))
Patrick McHardy73120962008-05-08 01:13:31 -0700547 macvlan_port_destroy(port->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700548}
549
550static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
551 .kind = "macvlan",
552 .priv_size = sizeof(struct macvlan_dev),
553 .setup = macvlan_setup,
554 .validate = macvlan_validate,
555 .newlink = macvlan_newlink,
556 .dellink = macvlan_dellink,
557};
558
559static int macvlan_device_event(struct notifier_block *unused,
560 unsigned long event, void *ptr)
561{
562 struct net_device *dev = ptr;
563 struct macvlan_dev *vlan, *next;
564 struct macvlan_port *port;
565
566 port = dev->macvlan_port;
567 if (port == NULL)
568 return NOTIFY_DONE;
569
570 switch (event) {
571 case NETDEV_CHANGE:
572 list_for_each_entry(vlan, &port->vlans, list)
573 macvlan_transfer_operstate(vlan->dev);
574 break;
575 case NETDEV_FEAT_CHANGE:
576 list_for_each_entry(vlan, &port->vlans, list) {
577 vlan->dev->features = dev->features & MACVLAN_FEATURES;
578 netdev_features_change(vlan->dev);
579 }
580 break;
581 case NETDEV_UNREGISTER:
582 list_for_each_entry_safe(vlan, next, &port->vlans, list)
583 macvlan_dellink(vlan->dev);
584 break;
585 }
586 return NOTIFY_DONE;
587}
588
589static struct notifier_block macvlan_notifier_block __read_mostly = {
590 .notifier_call = macvlan_device_event,
591};
592
593static int __init macvlan_init_module(void)
594{
595 int err;
596
597 register_netdevice_notifier(&macvlan_notifier_block);
598 macvlan_handle_frame_hook = macvlan_handle_frame;
599
600 err = rtnl_link_register(&macvlan_link_ops);
601 if (err < 0)
602 goto err1;
603 return 0;
604err1:
Rami Rosen52913242008-01-31 16:56:03 -0800605 macvlan_handle_frame_hook = NULL;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700606 unregister_netdevice_notifier(&macvlan_notifier_block);
607 return err;
608}
609
610static void __exit macvlan_cleanup_module(void)
611{
612 rtnl_link_unregister(&macvlan_link_ops);
613 macvlan_handle_frame_hook = NULL;
614 unregister_netdevice_notifier(&macvlan_notifier_block);
615}
616
617module_init(macvlan_init_module);
618module_exit(macvlan_cleanup_module);
619
620MODULE_LICENSE("GPL");
621MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
622MODULE_DESCRIPTION("Driver for MAC address based VLANs");
623MODULE_ALIAS_RTNL_LINK("macvlan");