macvlan.c 22 KB

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