macvlan.c 20 KB

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