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