macvlan.c 22 KB

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