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> |
| 32 | |
| 33 | #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE) |
| 34 | |
| 35 | struct macvlan_port { |
| 36 | struct net_device *dev; |
| 37 | struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; |
| 38 | struct list_head vlans; |
| 39 | }; |
| 40 | |
| 41 | struct 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 | |
| 50 | static 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 | |
| 63 | static void macvlan_broadcast(struct sk_buff *skb, |
| 64 | const struct macvlan_port *port) |
| 65 | { |
| 66 | const struct ethhdr *eth = eth_hdr(skb); |
| 67 | const struct macvlan_dev *vlan; |
| 68 | struct hlist_node *n; |
| 69 | struct net_device *dev; |
| 70 | struct sk_buff *nskb; |
| 71 | unsigned int i; |
| 72 | |
| 73 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) { |
| 74 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { |
| 75 | dev = vlan->dev; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 76 | |
| 77 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 78 | if (nskb == NULL) { |
| 79 | dev->stats.rx_errors++; |
| 80 | dev->stats.rx_dropped++; |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | dev->stats.rx_bytes += skb->len + ETH_HLEN; |
| 85 | dev->stats.rx_packets++; |
| 86 | dev->stats.multicast++; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 87 | |
| 88 | nskb->dev = dev; |
| 89 | if (!compare_ether_addr(eth->h_dest, dev->broadcast)) |
| 90 | nskb->pkt_type = PACKET_BROADCAST; |
| 91 | else |
| 92 | nskb->pkt_type = PACKET_MULTICAST; |
| 93 | |
| 94 | netif_rx(nskb); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* called under rcu_read_lock() from netif_receive_skb */ |
| 100 | static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb) |
| 101 | { |
| 102 | const struct ethhdr *eth = eth_hdr(skb); |
| 103 | const struct macvlan_port *port; |
| 104 | const struct macvlan_dev *vlan; |
| 105 | struct net_device *dev; |
| 106 | |
| 107 | port = rcu_dereference(skb->dev->macvlan_port); |
| 108 | if (port == NULL) |
| 109 | return skb; |
| 110 | |
| 111 | if (is_multicast_ether_addr(eth->h_dest)) { |
| 112 | macvlan_broadcast(skb, port); |
| 113 | return skb; |
| 114 | } |
| 115 | |
| 116 | vlan = macvlan_hash_lookup(port, eth->h_dest); |
| 117 | if (vlan == NULL) |
| 118 | return skb; |
| 119 | |
| 120 | dev = vlan->dev; |
| 121 | if (unlikely(!(dev->flags & IFF_UP))) { |
| 122 | kfree_skb(skb); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 127 | if (skb == NULL) { |
| 128 | dev->stats.rx_errors++; |
| 129 | dev->stats.rx_dropped++; |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | dev->stats.rx_bytes += skb->len + ETH_HLEN; |
| 134 | dev->stats.rx_packets++; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 135 | |
| 136 | skb->dev = dev; |
| 137 | skb->pkt_type = PACKET_HOST; |
| 138 | |
| 139 | netif_rx(skb); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | static int macvlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 144 | { |
| 145 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 146 | unsigned int len = skb->len; |
| 147 | int ret; |
| 148 | |
| 149 | skb->dev = vlan->lowerdev; |
| 150 | ret = dev_queue_xmit(skb); |
| 151 | |
| 152 | if (likely(ret == NET_XMIT_SUCCESS)) { |
| 153 | dev->stats.tx_packets++; |
| 154 | dev->stats.tx_bytes += len; |
| 155 | } else { |
| 156 | dev->stats.tx_errors++; |
| 157 | dev->stats.tx_aborted_errors++; |
| 158 | } |
| 159 | return NETDEV_TX_OK; |
| 160 | } |
| 161 | |
| 162 | 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] | 163 | unsigned short type, const void *daddr, |
| 164 | const void *saddr, unsigned len) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 165 | { |
| 166 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 167 | struct net_device *lowerdev = vlan->lowerdev; |
| 168 | |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 169 | return dev_hard_header(skb, lowerdev, type, daddr, |
| 170 | saddr ? : dev->dev_addr, len); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 173 | static const struct header_ops macvlan_hard_header_ops = { |
| 174 | .create = macvlan_hard_header, |
| 175 | .rebuild = eth_rebuild_header, |
| 176 | .parse = eth_header_parse, |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 177 | .cache = eth_header_cache, |
| 178 | .cache_update = eth_header_cache_update, |
| 179 | }; |
| 180 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 181 | static int macvlan_open(struct net_device *dev) |
| 182 | { |
| 183 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 184 | struct macvlan_port *port = vlan->port; |
| 185 | struct net_device *lowerdev = vlan->lowerdev; |
| 186 | int err; |
| 187 | |
| 188 | err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN); |
| 189 | if (err < 0) |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 190 | goto out; |
| 191 | if (dev->flags & IFF_ALLMULTI) { |
| 192 | err = dev_set_allmulti(lowerdev, 1); |
| 193 | if (err < 0) |
| 194 | goto del_unicast; |
| 195 | } |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 196 | |
| 197 | hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[dev->dev_addr[5]]); |
| 198 | return 0; |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 199 | |
| 200 | del_unicast: |
| 201 | dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); |
| 202 | out: |
| 203 | return err; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static int macvlan_stop(struct net_device *dev) |
| 207 | { |
| 208 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 209 | struct net_device *lowerdev = vlan->lowerdev; |
| 210 | |
| 211 | dev_mc_unsync(lowerdev, dev); |
| 212 | if (dev->flags & IFF_ALLMULTI) |
| 213 | dev_set_allmulti(lowerdev, -1); |
| 214 | |
| 215 | dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); |
| 216 | |
| 217 | hlist_del_rcu(&vlan->hlist); |
| 218 | synchronize_rcu(); |
| 219 | return 0; |
| 220 | } |
| 221 | |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 222 | static int macvlan_set_mac_address(struct net_device *dev, void *p) |
| 223 | { |
| 224 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 225 | struct net_device *lowerdev = vlan->lowerdev; |
| 226 | struct sockaddr *addr = p; |
| 227 | int err; |
| 228 | |
| 229 | if (!is_valid_ether_addr(addr->sa_data)) |
| 230 | return -EADDRNOTAVAIL; |
| 231 | |
| 232 | if (!(dev->flags & IFF_UP)) |
| 233 | goto out; |
| 234 | |
| 235 | err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN); |
| 236 | if (err < 0) |
| 237 | return err; |
| 238 | dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); |
| 239 | |
| 240 | out: |
| 241 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
| 242 | return 0; |
| 243 | } |
| 244 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 245 | static void macvlan_change_rx_flags(struct net_device *dev, int change) |
| 246 | { |
| 247 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 248 | struct net_device *lowerdev = vlan->lowerdev; |
| 249 | |
| 250 | if (change & IFF_ALLMULTI) |
| 251 | dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); |
| 252 | } |
| 253 | |
| 254 | static void macvlan_set_multicast_list(struct net_device *dev) |
| 255 | { |
| 256 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 257 | |
| 258 | dev_mc_sync(vlan->lowerdev, dev); |
| 259 | } |
| 260 | |
| 261 | static int macvlan_change_mtu(struct net_device *dev, int new_mtu) |
| 262 | { |
| 263 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 264 | |
| 265 | if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) |
| 266 | return -EINVAL; |
| 267 | dev->mtu = new_mtu; |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * macvlan network devices have devices nesting below it and are a special |
| 273 | * "super class" of normal network devices; split their locks off into a |
| 274 | * separate class since they always nest. |
| 275 | */ |
| 276 | static struct lock_class_key macvlan_netdev_xmit_lock_key; |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 277 | static struct lock_class_key macvlan_netdev_addr_lock_key; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 278 | |
| 279 | #define MACVLAN_FEATURES \ |
| 280 | (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ |
| 281 | NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ |
| 282 | NETIF_F_TSO_ECN | NETIF_F_TSO6) |
| 283 | |
| 284 | #define MACVLAN_STATE_MASK \ |
| 285 | ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) |
| 286 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 287 | static void macvlan_set_lockdep_class_one(struct net_device *dev, |
| 288 | struct netdev_queue *txq, |
| 289 | void *_unused) |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 290 | { |
| 291 | lockdep_set_class(&txq->_xmit_lock, |
| 292 | &macvlan_netdev_xmit_lock_key); |
| 293 | } |
| 294 | |
| 295 | static void macvlan_set_lockdep_class(struct net_device *dev) |
| 296 | { |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 297 | lockdep_set_class(&dev->addr_list_lock, |
| 298 | &macvlan_netdev_addr_lock_key); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 299 | 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] | 300 | } |
| 301 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 302 | static int macvlan_init(struct net_device *dev) |
| 303 | { |
| 304 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 305 | const struct net_device *lowerdev = vlan->lowerdev; |
| 306 | |
| 307 | dev->state = (dev->state & ~MACVLAN_STATE_MASK) | |
| 308 | (lowerdev->state & MACVLAN_STATE_MASK); |
| 309 | dev->features = lowerdev->features & MACVLAN_FEATURES; |
| 310 | dev->iflink = lowerdev->ifindex; |
| 311 | |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 312 | macvlan_set_lockdep_class(dev); |
| 313 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static void macvlan_ethtool_get_drvinfo(struct net_device *dev, |
| 318 | struct ethtool_drvinfo *drvinfo) |
| 319 | { |
| 320 | snprintf(drvinfo->driver, 32, "macvlan"); |
| 321 | snprintf(drvinfo->version, 32, "0.1"); |
| 322 | } |
| 323 | |
| 324 | static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) |
| 325 | { |
| 326 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 327 | struct net_device *lowerdev = vlan->lowerdev; |
| 328 | |
| 329 | if (lowerdev->ethtool_ops->get_rx_csum == NULL) |
| 330 | return 0; |
| 331 | return lowerdev->ethtool_ops->get_rx_csum(lowerdev); |
| 332 | } |
| 333 | |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 334 | static int macvlan_ethtool_get_settings(struct net_device *dev, |
| 335 | struct ethtool_cmd *cmd) |
| 336 | { |
| 337 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 338 | struct net_device *lowerdev = vlan->lowerdev; |
| 339 | |
| 340 | if (!lowerdev->ethtool_ops->get_settings) |
| 341 | return -EOPNOTSUPP; |
| 342 | |
| 343 | return lowerdev->ethtool_ops->get_settings(lowerdev, cmd); |
| 344 | } |
| 345 | |
| 346 | static u32 macvlan_ethtool_get_flags(struct net_device *dev) |
| 347 | { |
| 348 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 349 | struct net_device *lowerdev = vlan->lowerdev; |
| 350 | |
| 351 | if (!lowerdev->ethtool_ops->get_flags) |
| 352 | return 0; |
| 353 | return lowerdev->ethtool_ops->get_flags(lowerdev); |
| 354 | } |
| 355 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 356 | static const struct ethtool_ops macvlan_ethtool_ops = { |
| 357 | .get_link = ethtool_op_get_link, |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 358 | .get_settings = macvlan_ethtool_get_settings, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 359 | .get_rx_csum = macvlan_ethtool_get_rx_csum, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 360 | .get_drvinfo = macvlan_ethtool_get_drvinfo, |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 361 | .get_flags = macvlan_ethtool_get_flags, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | static void macvlan_setup(struct net_device *dev) |
| 365 | { |
| 366 | ether_setup(dev); |
| 367 | |
| 368 | dev->init = macvlan_init; |
| 369 | dev->open = macvlan_open; |
| 370 | dev->stop = macvlan_stop; |
| 371 | dev->change_mtu = macvlan_change_mtu; |
| 372 | dev->change_rx_flags = macvlan_change_rx_flags; |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 373 | dev->set_mac_address = macvlan_set_mac_address; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 374 | dev->set_multicast_list = macvlan_set_multicast_list; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 375 | dev->hard_start_xmit = macvlan_hard_start_xmit; |
| 376 | dev->destructor = free_netdev; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 377 | dev->header_ops = &macvlan_hard_header_ops, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 378 | dev->ethtool_ops = &macvlan_ethtool_ops; |
| 379 | dev->tx_queue_len = 0; |
| 380 | } |
| 381 | |
| 382 | static int macvlan_port_create(struct net_device *dev) |
| 383 | { |
| 384 | struct macvlan_port *port; |
| 385 | unsigned int i; |
| 386 | |
| 387 | if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) |
| 388 | return -EINVAL; |
| 389 | |
| 390 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
| 391 | if (port == NULL) |
| 392 | return -ENOMEM; |
| 393 | |
| 394 | port->dev = dev; |
| 395 | INIT_LIST_HEAD(&port->vlans); |
| 396 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) |
| 397 | INIT_HLIST_HEAD(&port->vlan_hash[i]); |
| 398 | rcu_assign_pointer(dev->macvlan_port, port); |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static void macvlan_port_destroy(struct net_device *dev) |
| 403 | { |
| 404 | struct macvlan_port *port = dev->macvlan_port; |
| 405 | |
| 406 | rcu_assign_pointer(dev->macvlan_port, NULL); |
| 407 | synchronize_rcu(); |
| 408 | kfree(port); |
| 409 | } |
| 410 | |
| 411 | static void macvlan_transfer_operstate(struct net_device *dev) |
| 412 | { |
| 413 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 414 | const struct net_device *lowerdev = vlan->lowerdev; |
| 415 | |
| 416 | if (lowerdev->operstate == IF_OPER_DORMANT) |
| 417 | netif_dormant_on(dev); |
| 418 | else |
| 419 | netif_dormant_off(dev); |
| 420 | |
| 421 | if (netif_carrier_ok(lowerdev)) { |
| 422 | if (!netif_carrier_ok(dev)) |
| 423 | netif_carrier_on(dev); |
| 424 | } else { |
Patrick McHardy | f12ca5f | 2008-01-21 00:47:08 -0800 | [diff] [blame] | 425 | if (netif_carrier_ok(dev)) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 426 | netif_carrier_off(dev); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 431 | { |
| 432 | if (tb[IFLA_ADDRESS]) { |
| 433 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 434 | return -EINVAL; |
| 435 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 436 | return -EADDRNOTAVAIL; |
| 437 | } |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static int macvlan_newlink(struct net_device *dev, |
| 442 | struct nlattr *tb[], struct nlattr *data[]) |
| 443 | { |
| 444 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 445 | struct macvlan_port *port; |
| 446 | struct net_device *lowerdev; |
| 447 | int err; |
| 448 | |
| 449 | if (!tb[IFLA_LINK]) |
| 450 | return -EINVAL; |
| 451 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 452 | lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK])); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 453 | if (lowerdev == NULL) |
| 454 | return -ENODEV; |
| 455 | |
Patrick McHardy | a6ca5f1 | 2008-01-10 22:39:28 -0800 | [diff] [blame] | 456 | /* Don't allow macvlans on top of other macvlans - its not really |
| 457 | * wrong, but lockdep can't handle it and its not useful for anything |
| 458 | * you couldn't do directly on top of the real device. |
| 459 | */ |
| 460 | if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) |
| 461 | return -ENODEV; |
| 462 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 463 | if (!tb[IFLA_MTU]) |
| 464 | dev->mtu = lowerdev->mtu; |
| 465 | else if (dev->mtu > lowerdev->mtu) |
| 466 | return -EINVAL; |
| 467 | |
| 468 | if (!tb[IFLA_ADDRESS]) |
| 469 | random_ether_addr(dev->dev_addr); |
| 470 | |
| 471 | if (lowerdev->macvlan_port == NULL) { |
| 472 | err = macvlan_port_create(lowerdev); |
| 473 | if (err < 0) |
| 474 | return err; |
| 475 | } |
| 476 | port = lowerdev->macvlan_port; |
| 477 | |
| 478 | vlan->lowerdev = lowerdev; |
| 479 | vlan->dev = dev; |
| 480 | vlan->port = port; |
| 481 | |
| 482 | err = register_netdevice(dev); |
| 483 | if (err < 0) |
| 484 | return err; |
| 485 | |
| 486 | list_add_tail(&vlan->list, &port->vlans); |
| 487 | macvlan_transfer_operstate(dev); |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static void macvlan_dellink(struct net_device *dev) |
| 492 | { |
| 493 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 494 | struct macvlan_port *port = vlan->port; |
| 495 | |
| 496 | list_del(&vlan->list); |
| 497 | unregister_netdevice(dev); |
| 498 | |
| 499 | if (list_empty(&port->vlans)) |
Patrick McHardy | 7312096 | 2008-05-08 01:13:31 -0700 | [diff] [blame] | 500 | macvlan_port_destroy(port->dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static struct rtnl_link_ops macvlan_link_ops __read_mostly = { |
| 504 | .kind = "macvlan", |
| 505 | .priv_size = sizeof(struct macvlan_dev), |
| 506 | .setup = macvlan_setup, |
| 507 | .validate = macvlan_validate, |
| 508 | .newlink = macvlan_newlink, |
| 509 | .dellink = macvlan_dellink, |
| 510 | }; |
| 511 | |
| 512 | static int macvlan_device_event(struct notifier_block *unused, |
| 513 | unsigned long event, void *ptr) |
| 514 | { |
| 515 | struct net_device *dev = ptr; |
| 516 | struct macvlan_dev *vlan, *next; |
| 517 | struct macvlan_port *port; |
| 518 | |
| 519 | port = dev->macvlan_port; |
| 520 | if (port == NULL) |
| 521 | return NOTIFY_DONE; |
| 522 | |
| 523 | switch (event) { |
| 524 | case NETDEV_CHANGE: |
| 525 | list_for_each_entry(vlan, &port->vlans, list) |
| 526 | macvlan_transfer_operstate(vlan->dev); |
| 527 | break; |
| 528 | case NETDEV_FEAT_CHANGE: |
| 529 | list_for_each_entry(vlan, &port->vlans, list) { |
| 530 | vlan->dev->features = dev->features & MACVLAN_FEATURES; |
| 531 | netdev_features_change(vlan->dev); |
| 532 | } |
| 533 | break; |
| 534 | case NETDEV_UNREGISTER: |
| 535 | list_for_each_entry_safe(vlan, next, &port->vlans, list) |
| 536 | macvlan_dellink(vlan->dev); |
| 537 | break; |
| 538 | } |
| 539 | return NOTIFY_DONE; |
| 540 | } |
| 541 | |
| 542 | static struct notifier_block macvlan_notifier_block __read_mostly = { |
| 543 | .notifier_call = macvlan_device_event, |
| 544 | }; |
| 545 | |
| 546 | static int __init macvlan_init_module(void) |
| 547 | { |
| 548 | int err; |
| 549 | |
| 550 | register_netdevice_notifier(&macvlan_notifier_block); |
| 551 | macvlan_handle_frame_hook = macvlan_handle_frame; |
| 552 | |
| 553 | err = rtnl_link_register(&macvlan_link_ops); |
| 554 | if (err < 0) |
| 555 | goto err1; |
| 556 | return 0; |
| 557 | err1: |
Rami Rosen | 5291324 | 2008-01-31 16:56:03 -0800 | [diff] [blame] | 558 | macvlan_handle_frame_hook = NULL; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 559 | unregister_netdevice_notifier(&macvlan_notifier_block); |
| 560 | return err; |
| 561 | } |
| 562 | |
| 563 | static void __exit macvlan_cleanup_module(void) |
| 564 | { |
| 565 | rtnl_link_unregister(&macvlan_link_ops); |
| 566 | macvlan_handle_frame_hook = NULL; |
| 567 | unregister_netdevice_notifier(&macvlan_notifier_block); |
| 568 | } |
| 569 | |
| 570 | module_init(macvlan_init_module); |
| 571 | module_exit(macvlan_cleanup_module); |
| 572 | |
| 573 | MODULE_LICENSE("GPL"); |
| 574 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 575 | MODULE_DESCRIPTION("Driver for MAC address based VLANs"); |
| 576 | MODULE_ALIAS_RTNL_LINK("macvlan"); |