macvlan.c 25 KB

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