macvlan.c 24 KB

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