macvlan.c 22 KB

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