macvlan.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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 <linux/workqueue.h>
  34. #include <net/rtnetlink.h>
  35. #include <net/xfrm.h>
  36. #include <linux/netpoll.h>
  37. #define MACVLAN_HASH_BITS 8
  38. #define MACVLAN_HASH_SIZE (1<<MACVLAN_HASH_BITS)
  39. #define MACVLAN_BC_QUEUE_LEN 1000
  40. struct macvlan_port {
  41. struct net_device *dev;
  42. struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
  43. struct list_head vlans;
  44. struct rcu_head rcu;
  45. struct sk_buff_head bc_queue;
  46. struct work_struct bc_work;
  47. bool passthru;
  48. int count;
  49. struct hlist_head vlan_source_hash[MACVLAN_HASH_SIZE];
  50. };
  51. struct macvlan_source_entry {
  52. struct hlist_node hlist;
  53. struct macvlan_dev *vlan;
  54. unsigned char addr[6+2] __aligned(sizeof(u16));
  55. struct rcu_head rcu;
  56. };
  57. struct macvlan_skb_cb {
  58. const struct macvlan_dev *src;
  59. };
  60. #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
  61. static void macvlan_port_destroy(struct net_device *dev);
  62. /* Hash Ethernet address */
  63. static u32 macvlan_eth_hash(const unsigned char *addr)
  64. {
  65. u64 value = get_unaligned((u64 *)addr);
  66. /* only want 6 bytes */
  67. #ifdef __BIG_ENDIAN
  68. value >>= 16;
  69. #else
  70. value <<= 16;
  71. #endif
  72. return hash_64(value, MACVLAN_HASH_BITS);
  73. }
  74. static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
  75. {
  76. return rcu_dereference(dev->rx_handler_data);
  77. }
  78. static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
  79. {
  80. return rtnl_dereference(dev->rx_handler_data);
  81. }
  82. #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
  83. static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
  84. const unsigned char *addr)
  85. {
  86. struct macvlan_dev *vlan;
  87. u32 idx = macvlan_eth_hash(addr);
  88. hlist_for_each_entry_rcu(vlan, &port->vlan_hash[idx], hlist) {
  89. if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
  90. return vlan;
  91. }
  92. return NULL;
  93. }
  94. static struct macvlan_source_entry *macvlan_hash_lookup_source(
  95. const struct macvlan_dev *vlan,
  96. const unsigned char *addr)
  97. {
  98. struct macvlan_source_entry *entry;
  99. u32 idx = macvlan_eth_hash(addr);
  100. struct hlist_head *h = &vlan->port->vlan_source_hash[idx];
  101. hlist_for_each_entry_rcu(entry, h, hlist) {
  102. if (ether_addr_equal_64bits(entry->addr, addr) &&
  103. entry->vlan == vlan)
  104. return entry;
  105. }
  106. return NULL;
  107. }
  108. static int macvlan_hash_add_source(struct macvlan_dev *vlan,
  109. const unsigned char *addr)
  110. {
  111. struct macvlan_port *port = vlan->port;
  112. struct macvlan_source_entry *entry;
  113. struct hlist_head *h;
  114. entry = macvlan_hash_lookup_source(vlan, addr);
  115. if (entry)
  116. return 0;
  117. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  118. if (!entry)
  119. return -ENOMEM;
  120. ether_addr_copy(entry->addr, addr);
  121. entry->vlan = vlan;
  122. h = &port->vlan_source_hash[macvlan_eth_hash(addr)];
  123. hlist_add_head_rcu(&entry->hlist, h);
  124. vlan->macaddr_count++;
  125. return 0;
  126. }
  127. static void macvlan_hash_add(struct macvlan_dev *vlan)
  128. {
  129. struct macvlan_port *port = vlan->port;
  130. const unsigned char *addr = vlan->dev->dev_addr;
  131. u32 idx = macvlan_eth_hash(addr);
  132. hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]);
  133. }
  134. static void macvlan_hash_del_source(struct macvlan_source_entry *entry)
  135. {
  136. hlist_del_rcu(&entry->hlist);
  137. kfree_rcu(entry, rcu);
  138. }
  139. static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
  140. {
  141. hlist_del_rcu(&vlan->hlist);
  142. if (sync)
  143. synchronize_rcu();
  144. }
  145. static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
  146. const unsigned char *addr)
  147. {
  148. macvlan_hash_del(vlan, true);
  149. /* Now that we are unhashed it is safe to change the device
  150. * address without confusing packet delivery.
  151. */
  152. memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
  153. macvlan_hash_add(vlan);
  154. }
  155. static int macvlan_addr_busy(const struct macvlan_port *port,
  156. const unsigned char *addr)
  157. {
  158. /* Test to see if the specified multicast address is
  159. * currently in use by the underlying device or
  160. * another macvlan.
  161. */
  162. if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
  163. return 1;
  164. if (macvlan_hash_lookup(port, addr))
  165. return 1;
  166. return 0;
  167. }
  168. static int macvlan_broadcast_one(struct sk_buff *skb,
  169. const struct macvlan_dev *vlan,
  170. const struct ethhdr *eth, bool local)
  171. {
  172. struct net_device *dev = vlan->dev;
  173. if (local)
  174. return __dev_forward_skb(dev, skb);
  175. skb->dev = dev;
  176. if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
  177. skb->pkt_type = PACKET_BROADCAST;
  178. else
  179. skb->pkt_type = PACKET_MULTICAST;
  180. return 0;
  181. }
  182. static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
  183. {
  184. return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
  185. }
  186. static unsigned int mc_hash(const struct macvlan_dev *vlan,
  187. const unsigned char *addr)
  188. {
  189. u32 val = __get_unaligned_cpu32(addr + 2);
  190. val ^= macvlan_hash_mix(vlan);
  191. return hash_32(val, MACVLAN_MC_FILTER_BITS);
  192. }
  193. static void macvlan_broadcast(struct sk_buff *skb,
  194. const struct macvlan_port *port,
  195. struct net_device *src,
  196. enum macvlan_mode mode)
  197. {
  198. const struct ethhdr *eth = eth_hdr(skb);
  199. const struct macvlan_dev *vlan;
  200. struct sk_buff *nskb;
  201. unsigned int i;
  202. int err;
  203. unsigned int hash;
  204. if (skb->protocol == htons(ETH_P_PAUSE))
  205. return;
  206. for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
  207. hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
  208. if (vlan->dev == src || !(vlan->mode & mode))
  209. continue;
  210. hash = mc_hash(vlan, eth->h_dest);
  211. if (!test_bit(hash, vlan->mc_filter))
  212. continue;
  213. err = NET_RX_DROP;
  214. nskb = skb_clone(skb, GFP_ATOMIC);
  215. if (likely(nskb))
  216. err = macvlan_broadcast_one(
  217. nskb, vlan, eth,
  218. mode == MACVLAN_MODE_BRIDGE) ?:
  219. netif_rx_ni(nskb);
  220. macvlan_count_rx(vlan, skb->len + ETH_HLEN,
  221. err == NET_RX_SUCCESS, true);
  222. }
  223. }
  224. }
  225. static void macvlan_process_broadcast(struct work_struct *w)
  226. {
  227. struct macvlan_port *port = container_of(w, struct macvlan_port,
  228. bc_work);
  229. struct sk_buff *skb;
  230. struct sk_buff_head list;
  231. __skb_queue_head_init(&list);
  232. spin_lock_bh(&port->bc_queue.lock);
  233. skb_queue_splice_tail_init(&port->bc_queue, &list);
  234. spin_unlock_bh(&port->bc_queue.lock);
  235. while ((skb = __skb_dequeue(&list))) {
  236. const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
  237. rcu_read_lock();
  238. if (!src)
  239. /* frame comes from an external address */
  240. macvlan_broadcast(skb, port, NULL,
  241. MACVLAN_MODE_PRIVATE |
  242. MACVLAN_MODE_VEPA |
  243. MACVLAN_MODE_PASSTHRU|
  244. MACVLAN_MODE_BRIDGE);
  245. else if (src->mode == MACVLAN_MODE_VEPA)
  246. /* flood to everyone except source */
  247. macvlan_broadcast(skb, port, src->dev,
  248. MACVLAN_MODE_VEPA |
  249. MACVLAN_MODE_BRIDGE);
  250. else
  251. /*
  252. * flood only to VEPA ports, bridge ports
  253. * already saw the frame on the way out.
  254. */
  255. macvlan_broadcast(skb, port, src->dev,
  256. MACVLAN_MODE_VEPA);
  257. rcu_read_unlock();
  258. kfree_skb(skb);
  259. }
  260. }
  261. static void macvlan_broadcast_enqueue(struct macvlan_port *port,
  262. struct sk_buff *skb)
  263. {
  264. struct sk_buff *nskb;
  265. int err = -ENOMEM;
  266. nskb = skb_clone(skb, GFP_ATOMIC);
  267. if (!nskb)
  268. goto err;
  269. spin_lock(&port->bc_queue.lock);
  270. if (skb_queue_len(&port->bc_queue) < MACVLAN_BC_QUEUE_LEN) {
  271. __skb_queue_tail(&port->bc_queue, nskb);
  272. err = 0;
  273. }
  274. spin_unlock(&port->bc_queue.lock);
  275. if (err)
  276. goto free_nskb;
  277. schedule_work(&port->bc_work);
  278. return;
  279. free_nskb:
  280. kfree_skb(nskb);
  281. err:
  282. atomic_long_inc(&skb->dev->rx_dropped);
  283. }
  284. static void macvlan_flush_sources(struct macvlan_port *port,
  285. struct macvlan_dev *vlan)
  286. {
  287. int i;
  288. for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
  289. struct hlist_node *h, *n;
  290. hlist_for_each_safe(h, n, &port->vlan_source_hash[i]) {
  291. struct macvlan_source_entry *entry;
  292. entry = hlist_entry(h, struct macvlan_source_entry,
  293. hlist);
  294. if (entry->vlan == vlan)
  295. macvlan_hash_del_source(entry);
  296. }
  297. }
  298. vlan->macaddr_count = 0;
  299. }
  300. static void macvlan_forward_source_one(struct sk_buff *skb,
  301. struct macvlan_dev *vlan)
  302. {
  303. struct sk_buff *nskb;
  304. struct net_device *dev;
  305. int len;
  306. int ret;
  307. dev = vlan->dev;
  308. if (unlikely(!(dev->flags & IFF_UP)))
  309. return;
  310. nskb = skb_clone(skb, GFP_ATOMIC);
  311. if (!nskb)
  312. return;
  313. len = nskb->len + ETH_HLEN;
  314. nskb->dev = dev;
  315. nskb->pkt_type = PACKET_HOST;
  316. ret = netif_rx(nskb);
  317. macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
  318. }
  319. static void macvlan_forward_source(struct sk_buff *skb,
  320. struct macvlan_port *port,
  321. const unsigned char *addr)
  322. {
  323. struct macvlan_source_entry *entry;
  324. u32 idx = macvlan_eth_hash(addr);
  325. struct hlist_head *h = &port->vlan_source_hash[idx];
  326. hlist_for_each_entry_rcu(entry, h, hlist) {
  327. if (ether_addr_equal_64bits(entry->addr, addr))
  328. if (entry->vlan->dev->flags & IFF_UP)
  329. macvlan_forward_source_one(skb, entry->vlan);
  330. }
  331. }
  332. /* called under rcu_read_lock() from netif_receive_skb */
  333. static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
  334. {
  335. struct macvlan_port *port;
  336. struct sk_buff *skb = *pskb;
  337. const struct ethhdr *eth = eth_hdr(skb);
  338. const struct macvlan_dev *vlan;
  339. const struct macvlan_dev *src;
  340. struct net_device *dev;
  341. unsigned int len = 0;
  342. int ret;
  343. rx_handler_result_t handle_res;
  344. port = macvlan_port_get_rcu(skb->dev);
  345. if (is_multicast_ether_addr(eth->h_dest)) {
  346. skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
  347. if (!skb)
  348. return RX_HANDLER_CONSUMED;
  349. eth = eth_hdr(skb);
  350. macvlan_forward_source(skb, port, eth->h_source);
  351. src = macvlan_hash_lookup(port, eth->h_source);
  352. if (src && src->mode != MACVLAN_MODE_VEPA &&
  353. src->mode != MACVLAN_MODE_BRIDGE) {
  354. /* forward to original port. */
  355. vlan = src;
  356. ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
  357. netif_rx(skb);
  358. handle_res = RX_HANDLER_CONSUMED;
  359. goto out;
  360. }
  361. MACVLAN_SKB_CB(skb)->src = src;
  362. macvlan_broadcast_enqueue(port, skb);
  363. return RX_HANDLER_PASS;
  364. }
  365. macvlan_forward_source(skb, port, eth->h_source);
  366. if (port->passthru)
  367. vlan = list_first_or_null_rcu(&port->vlans,
  368. struct macvlan_dev, list);
  369. else
  370. vlan = macvlan_hash_lookup(port, eth->h_dest);
  371. if (vlan == NULL)
  372. return RX_HANDLER_PASS;
  373. dev = vlan->dev;
  374. if (unlikely(!(dev->flags & IFF_UP))) {
  375. kfree_skb(skb);
  376. return RX_HANDLER_CONSUMED;
  377. }
  378. len = skb->len + ETH_HLEN;
  379. skb = skb_share_check(skb, GFP_ATOMIC);
  380. if (!skb) {
  381. ret = NET_RX_DROP;
  382. handle_res = RX_HANDLER_CONSUMED;
  383. goto out;
  384. }
  385. skb->dev = dev;
  386. skb->pkt_type = PACKET_HOST;
  387. ret = NET_RX_SUCCESS;
  388. handle_res = RX_HANDLER_ANOTHER;
  389. out:
  390. macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
  391. return handle_res;
  392. }
  393. static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
  394. {
  395. const struct macvlan_dev *vlan = netdev_priv(dev);
  396. const struct macvlan_port *port = vlan->port;
  397. const struct macvlan_dev *dest;
  398. if (vlan->mode == MACVLAN_MODE_BRIDGE) {
  399. const struct ethhdr *eth = (void *)skb->data;
  400. /* send to other bridge ports directly */
  401. if (is_multicast_ether_addr(eth->h_dest)) {
  402. macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
  403. goto xmit_world;
  404. }
  405. dest = macvlan_hash_lookup(port, eth->h_dest);
  406. if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
  407. /* send to lowerdev first for its network taps */
  408. dev_forward_skb(vlan->lowerdev, skb);
  409. return NET_XMIT_SUCCESS;
  410. }
  411. }
  412. xmit_world:
  413. skb->dev = vlan->lowerdev;
  414. return dev_queue_xmit(skb);
  415. }
  416. static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
  417. {
  418. #ifdef CONFIG_NET_POLL_CONTROLLER
  419. if (vlan->netpoll)
  420. netpoll_send_skb(vlan->netpoll, skb);
  421. #else
  422. BUG();
  423. #endif
  424. return NETDEV_TX_OK;
  425. }
  426. static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
  427. struct net_device *dev)
  428. {
  429. unsigned int len = skb->len;
  430. int ret;
  431. struct macvlan_dev *vlan = netdev_priv(dev);
  432. if (unlikely(netpoll_tx_running(dev)))
  433. return macvlan_netpoll_send_skb(vlan, skb);
  434. if (vlan->fwd_priv) {
  435. skb->dev = vlan->lowerdev;
  436. ret = dev_queue_xmit_accel(skb, vlan->fwd_priv);
  437. } else {
  438. ret = macvlan_queue_xmit(skb, dev);
  439. }
  440. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  441. struct vlan_pcpu_stats *pcpu_stats;
  442. pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
  443. u64_stats_update_begin(&pcpu_stats->syncp);
  444. pcpu_stats->tx_packets++;
  445. pcpu_stats->tx_bytes += len;
  446. u64_stats_update_end(&pcpu_stats->syncp);
  447. } else {
  448. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  449. }
  450. return ret;
  451. }
  452. static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
  453. unsigned short type, const void *daddr,
  454. const void *saddr, unsigned len)
  455. {
  456. const struct macvlan_dev *vlan = netdev_priv(dev);
  457. struct net_device *lowerdev = vlan->lowerdev;
  458. return dev_hard_header(skb, lowerdev, type, daddr,
  459. saddr ? : dev->dev_addr, len);
  460. }
  461. static const struct header_ops macvlan_hard_header_ops = {
  462. .create = macvlan_hard_header,
  463. .parse = eth_header_parse,
  464. .cache = eth_header_cache,
  465. .cache_update = eth_header_cache_update,
  466. };
  467. static struct rtnl_link_ops macvlan_link_ops;
  468. static int macvlan_open(struct net_device *dev)
  469. {
  470. struct macvlan_dev *vlan = netdev_priv(dev);
  471. struct net_device *lowerdev = vlan->lowerdev;
  472. int err;
  473. if (vlan->port->passthru) {
  474. if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
  475. err = dev_set_promiscuity(lowerdev, 1);
  476. if (err < 0)
  477. goto out;
  478. }
  479. goto hash_add;
  480. }
  481. if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD &&
  482. dev->rtnl_link_ops == &macvlan_link_ops) {
  483. vlan->fwd_priv =
  484. lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
  485. /* If we get a NULL pointer back, or if we get an error
  486. * then we should just fall through to the non accelerated path
  487. */
  488. if (IS_ERR_OR_NULL(vlan->fwd_priv)) {
  489. vlan->fwd_priv = NULL;
  490. } else
  491. return 0;
  492. }
  493. err = -EBUSY;
  494. if (macvlan_addr_busy(vlan->port, dev->dev_addr))
  495. goto out;
  496. err = dev_uc_add(lowerdev, dev->dev_addr);
  497. if (err < 0)
  498. goto out;
  499. if (dev->flags & IFF_ALLMULTI) {
  500. err = dev_set_allmulti(lowerdev, 1);
  501. if (err < 0)
  502. goto del_unicast;
  503. }
  504. if (dev->flags & IFF_PROMISC) {
  505. err = dev_set_promiscuity(lowerdev, 1);
  506. if (err < 0)
  507. goto clear_multi;
  508. }
  509. hash_add:
  510. macvlan_hash_add(vlan);
  511. return 0;
  512. clear_multi:
  513. dev_set_allmulti(lowerdev, -1);
  514. del_unicast:
  515. dev_uc_del(lowerdev, dev->dev_addr);
  516. out:
  517. if (vlan->fwd_priv) {
  518. lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
  519. vlan->fwd_priv);
  520. vlan->fwd_priv = NULL;
  521. }
  522. return err;
  523. }
  524. static int macvlan_stop(struct net_device *dev)
  525. {
  526. struct macvlan_dev *vlan = netdev_priv(dev);
  527. struct net_device *lowerdev = vlan->lowerdev;
  528. if (vlan->fwd_priv) {
  529. lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
  530. vlan->fwd_priv);
  531. vlan->fwd_priv = NULL;
  532. return 0;
  533. }
  534. dev_uc_unsync(lowerdev, dev);
  535. dev_mc_unsync(lowerdev, dev);
  536. if (vlan->port->passthru) {
  537. if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
  538. dev_set_promiscuity(lowerdev, -1);
  539. goto hash_del;
  540. }
  541. if (dev->flags & IFF_ALLMULTI)
  542. dev_set_allmulti(lowerdev, -1);
  543. if (dev->flags & IFF_PROMISC)
  544. dev_set_promiscuity(lowerdev, -1);
  545. dev_uc_del(lowerdev, dev->dev_addr);
  546. hash_del:
  547. macvlan_hash_del(vlan, !dev->dismantle);
  548. return 0;
  549. }
  550. static int macvlan_sync_address(struct net_device *dev, unsigned char *addr)
  551. {
  552. struct macvlan_dev *vlan = netdev_priv(dev);
  553. struct net_device *lowerdev = vlan->lowerdev;
  554. int err;
  555. if (!(dev->flags & IFF_UP)) {
  556. /* Just copy in the new address */
  557. ether_addr_copy(dev->dev_addr, addr);
  558. } else {
  559. /* Rehash and update the device filters */
  560. if (macvlan_addr_busy(vlan->port, addr))
  561. return -EBUSY;
  562. if (!vlan->port->passthru) {
  563. err = dev_uc_add(lowerdev, addr);
  564. if (err)
  565. return err;
  566. dev_uc_del(lowerdev, dev->dev_addr);
  567. }
  568. macvlan_hash_change_addr(vlan, addr);
  569. }
  570. return 0;
  571. }
  572. static int macvlan_set_mac_address(struct net_device *dev, void *p)
  573. {
  574. struct macvlan_dev *vlan = netdev_priv(dev);
  575. struct sockaddr *addr = p;
  576. if (!is_valid_ether_addr(addr->sa_data))
  577. return -EADDRNOTAVAIL;
  578. if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
  579. dev_set_mac_address(vlan->lowerdev, addr);
  580. return 0;
  581. }
  582. return macvlan_sync_address(dev, addr->sa_data);
  583. }
  584. static void macvlan_change_rx_flags(struct net_device *dev, int change)
  585. {
  586. struct macvlan_dev *vlan = netdev_priv(dev);
  587. struct net_device *lowerdev = vlan->lowerdev;
  588. if (dev->flags & IFF_UP) {
  589. if (change & IFF_ALLMULTI)
  590. dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  591. if (change & IFF_PROMISC)
  592. dev_set_promiscuity(lowerdev,
  593. dev->flags & IFF_PROMISC ? 1 : -1);
  594. }
  595. }
  596. static void macvlan_set_mac_lists(struct net_device *dev)
  597. {
  598. struct macvlan_dev *vlan = netdev_priv(dev);
  599. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  600. bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
  601. } else {
  602. struct netdev_hw_addr *ha;
  603. DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
  604. bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
  605. netdev_for_each_mc_addr(ha, dev) {
  606. __set_bit(mc_hash(vlan, ha->addr), filter);
  607. }
  608. __set_bit(mc_hash(vlan, dev->broadcast), filter);
  609. bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
  610. }
  611. dev_uc_sync(vlan->lowerdev, dev);
  612. dev_mc_sync(vlan->lowerdev, dev);
  613. }
  614. static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
  615. {
  616. struct macvlan_dev *vlan = netdev_priv(dev);
  617. if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
  618. return -EINVAL;
  619. dev->mtu = new_mtu;
  620. return 0;
  621. }
  622. /*
  623. * macvlan network devices have devices nesting below it and are a special
  624. * "super class" of normal network devices; split their locks off into a
  625. * separate class since they always nest.
  626. */
  627. static struct lock_class_key macvlan_netdev_xmit_lock_key;
  628. static struct lock_class_key macvlan_netdev_addr_lock_key;
  629. #define ALWAYS_ON_FEATURES \
  630. (NETIF_F_SG | NETIF_F_GEN_CSUM | NETIF_F_GSO_SOFTWARE | NETIF_F_LLTX | \
  631. NETIF_F_GSO_ROBUST)
  632. #define MACVLAN_FEATURES \
  633. (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
  634. NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_LRO | \
  635. NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
  636. NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
  637. #define MACVLAN_STATE_MASK \
  638. ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
  639. static int macvlan_get_nest_level(struct net_device *dev)
  640. {
  641. return ((struct macvlan_dev *)netdev_priv(dev))->nest_level;
  642. }
  643. static void macvlan_set_lockdep_class_one(struct net_device *dev,
  644. struct netdev_queue *txq,
  645. void *_unused)
  646. {
  647. lockdep_set_class(&txq->_xmit_lock,
  648. &macvlan_netdev_xmit_lock_key);
  649. }
  650. static void macvlan_set_lockdep_class(struct net_device *dev)
  651. {
  652. lockdep_set_class_and_subclass(&dev->addr_list_lock,
  653. &macvlan_netdev_addr_lock_key,
  654. macvlan_get_nest_level(dev));
  655. netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
  656. }
  657. static int macvlan_init(struct net_device *dev)
  658. {
  659. struct macvlan_dev *vlan = netdev_priv(dev);
  660. const struct net_device *lowerdev = vlan->lowerdev;
  661. dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
  662. (lowerdev->state & MACVLAN_STATE_MASK);
  663. dev->features = lowerdev->features & MACVLAN_FEATURES;
  664. dev->features |= ALWAYS_ON_FEATURES;
  665. dev->hw_features |= NETIF_F_LRO;
  666. dev->vlan_features = lowerdev->vlan_features & MACVLAN_FEATURES;
  667. dev->gso_max_size = lowerdev->gso_max_size;
  668. dev->hard_header_len = lowerdev->hard_header_len;
  669. macvlan_set_lockdep_class(dev);
  670. vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
  671. if (!vlan->pcpu_stats)
  672. return -ENOMEM;
  673. return 0;
  674. }
  675. static void macvlan_uninit(struct net_device *dev)
  676. {
  677. struct macvlan_dev *vlan = netdev_priv(dev);
  678. struct macvlan_port *port = vlan->port;
  679. free_percpu(vlan->pcpu_stats);
  680. macvlan_flush_sources(port, vlan);
  681. port->count -= 1;
  682. if (!port->count)
  683. macvlan_port_destroy(port->dev);
  684. }
  685. static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
  686. struct rtnl_link_stats64 *stats)
  687. {
  688. struct macvlan_dev *vlan = netdev_priv(dev);
  689. if (vlan->pcpu_stats) {
  690. struct vlan_pcpu_stats *p;
  691. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  692. u32 rx_errors = 0, tx_dropped = 0;
  693. unsigned int start;
  694. int i;
  695. for_each_possible_cpu(i) {
  696. p = per_cpu_ptr(vlan->pcpu_stats, i);
  697. do {
  698. start = u64_stats_fetch_begin_irq(&p->syncp);
  699. rx_packets = p->rx_packets;
  700. rx_bytes = p->rx_bytes;
  701. rx_multicast = p->rx_multicast;
  702. tx_packets = p->tx_packets;
  703. tx_bytes = p->tx_bytes;
  704. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  705. stats->rx_packets += rx_packets;
  706. stats->rx_bytes += rx_bytes;
  707. stats->multicast += rx_multicast;
  708. stats->tx_packets += tx_packets;
  709. stats->tx_bytes += tx_bytes;
  710. /* rx_errors & tx_dropped are u32, updated
  711. * without syncp protection.
  712. */
  713. rx_errors += p->rx_errors;
  714. tx_dropped += p->tx_dropped;
  715. }
  716. stats->rx_errors = rx_errors;
  717. stats->rx_dropped = rx_errors;
  718. stats->tx_dropped = tx_dropped;
  719. }
  720. return stats;
  721. }
  722. static int macvlan_vlan_rx_add_vid(struct net_device *dev,
  723. __be16 proto, u16 vid)
  724. {
  725. struct macvlan_dev *vlan = netdev_priv(dev);
  726. struct net_device *lowerdev = vlan->lowerdev;
  727. return vlan_vid_add(lowerdev, proto, vid);
  728. }
  729. static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
  730. __be16 proto, u16 vid)
  731. {
  732. struct macvlan_dev *vlan = netdev_priv(dev);
  733. struct net_device *lowerdev = vlan->lowerdev;
  734. vlan_vid_del(lowerdev, proto, vid);
  735. return 0;
  736. }
  737. static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  738. struct net_device *dev,
  739. const unsigned char *addr, u16 vid,
  740. u16 flags)
  741. {
  742. struct macvlan_dev *vlan = netdev_priv(dev);
  743. int err = -EINVAL;
  744. /* Support unicast filter only on passthru devices.
  745. * Multicast filter should be allowed on all devices.
  746. */
  747. if (!vlan->port->passthru && is_unicast_ether_addr(addr))
  748. return -EOPNOTSUPP;
  749. if (flags & NLM_F_REPLACE)
  750. return -EOPNOTSUPP;
  751. if (is_unicast_ether_addr(addr))
  752. err = dev_uc_add_excl(dev, addr);
  753. else if (is_multicast_ether_addr(addr))
  754. err = dev_mc_add_excl(dev, addr);
  755. return err;
  756. }
  757. static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  758. struct net_device *dev,
  759. const unsigned char *addr, u16 vid)
  760. {
  761. struct macvlan_dev *vlan = netdev_priv(dev);
  762. int err = -EINVAL;
  763. /* Support unicast filter only on passthru devices.
  764. * Multicast filter should be allowed on all devices.
  765. */
  766. if (!vlan->port->passthru && is_unicast_ether_addr(addr))
  767. return -EOPNOTSUPP;
  768. if (is_unicast_ether_addr(addr))
  769. err = dev_uc_del(dev, addr);
  770. else if (is_multicast_ether_addr(addr))
  771. err = dev_mc_del(dev, addr);
  772. return err;
  773. }
  774. static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
  775. struct ethtool_drvinfo *drvinfo)
  776. {
  777. strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
  778. strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
  779. }
  780. static int macvlan_ethtool_get_settings(struct net_device *dev,
  781. struct ethtool_cmd *cmd)
  782. {
  783. const struct macvlan_dev *vlan = netdev_priv(dev);
  784. return __ethtool_get_settings(vlan->lowerdev, cmd);
  785. }
  786. static netdev_features_t macvlan_fix_features(struct net_device *dev,
  787. netdev_features_t features)
  788. {
  789. struct macvlan_dev *vlan = netdev_priv(dev);
  790. netdev_features_t lowerdev_features = vlan->lowerdev->features;
  791. netdev_features_t mask;
  792. features |= NETIF_F_ALL_FOR_ALL;
  793. features &= (vlan->set_features | ~MACVLAN_FEATURES);
  794. mask = features;
  795. lowerdev_features &= (features | ~NETIF_F_LRO);
  796. features = netdev_increment_features(lowerdev_features, features, mask);
  797. features |= ALWAYS_ON_FEATURES;
  798. features &= ~NETIF_F_NETNS_LOCAL;
  799. return features;
  800. }
  801. #ifdef CONFIG_NET_POLL_CONTROLLER
  802. static void macvlan_dev_poll_controller(struct net_device *dev)
  803. {
  804. return;
  805. }
  806. static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
  807. {
  808. struct macvlan_dev *vlan = netdev_priv(dev);
  809. struct net_device *real_dev = vlan->lowerdev;
  810. struct netpoll *netpoll;
  811. int err = 0;
  812. netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
  813. err = -ENOMEM;
  814. if (!netpoll)
  815. goto out;
  816. err = __netpoll_setup(netpoll, real_dev);
  817. if (err) {
  818. kfree(netpoll);
  819. goto out;
  820. }
  821. vlan->netpoll = netpoll;
  822. out:
  823. return err;
  824. }
  825. static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
  826. {
  827. struct macvlan_dev *vlan = netdev_priv(dev);
  828. struct netpoll *netpoll = vlan->netpoll;
  829. if (!netpoll)
  830. return;
  831. vlan->netpoll = NULL;
  832. __netpoll_free_async(netpoll);
  833. }
  834. #endif /* CONFIG_NET_POLL_CONTROLLER */
  835. static int macvlan_dev_get_iflink(const struct net_device *dev)
  836. {
  837. struct macvlan_dev *vlan = netdev_priv(dev);
  838. return vlan->lowerdev->ifindex;
  839. }
  840. static const struct ethtool_ops macvlan_ethtool_ops = {
  841. .get_link = ethtool_op_get_link,
  842. .get_settings = macvlan_ethtool_get_settings,
  843. .get_drvinfo = macvlan_ethtool_get_drvinfo,
  844. };
  845. static const struct net_device_ops macvlan_netdev_ops = {
  846. .ndo_init = macvlan_init,
  847. .ndo_uninit = macvlan_uninit,
  848. .ndo_open = macvlan_open,
  849. .ndo_stop = macvlan_stop,
  850. .ndo_start_xmit = macvlan_start_xmit,
  851. .ndo_change_mtu = macvlan_change_mtu,
  852. .ndo_fix_features = macvlan_fix_features,
  853. .ndo_change_rx_flags = macvlan_change_rx_flags,
  854. .ndo_set_mac_address = macvlan_set_mac_address,
  855. .ndo_set_rx_mode = macvlan_set_mac_lists,
  856. .ndo_get_stats64 = macvlan_dev_get_stats64,
  857. .ndo_validate_addr = eth_validate_addr,
  858. .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid,
  859. .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid,
  860. .ndo_fdb_add = macvlan_fdb_add,
  861. .ndo_fdb_del = macvlan_fdb_del,
  862. .ndo_fdb_dump = ndo_dflt_fdb_dump,
  863. .ndo_get_lock_subclass = macvlan_get_nest_level,
  864. #ifdef CONFIG_NET_POLL_CONTROLLER
  865. .ndo_poll_controller = macvlan_dev_poll_controller,
  866. .ndo_netpoll_setup = macvlan_dev_netpoll_setup,
  867. .ndo_netpoll_cleanup = macvlan_dev_netpoll_cleanup,
  868. #endif
  869. .ndo_get_iflink = macvlan_dev_get_iflink,
  870. };
  871. void macvlan_common_setup(struct net_device *dev)
  872. {
  873. ether_setup(dev);
  874. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  875. netif_keep_dst(dev);
  876. dev->priv_flags |= IFF_UNICAST_FLT;
  877. dev->netdev_ops = &macvlan_netdev_ops;
  878. dev->destructor = free_netdev;
  879. dev->header_ops = &macvlan_hard_header_ops;
  880. dev->ethtool_ops = &macvlan_ethtool_ops;
  881. }
  882. EXPORT_SYMBOL_GPL(macvlan_common_setup);
  883. static void macvlan_setup(struct net_device *dev)
  884. {
  885. macvlan_common_setup(dev);
  886. dev->tx_queue_len = 0;
  887. }
  888. static int macvlan_port_create(struct net_device *dev)
  889. {
  890. struct macvlan_port *port;
  891. unsigned int i;
  892. int err;
  893. if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
  894. return -EINVAL;
  895. if (netif_is_ipvlan_port(dev))
  896. return -EBUSY;
  897. port = kzalloc(sizeof(*port), GFP_KERNEL);
  898. if (port == NULL)
  899. return -ENOMEM;
  900. port->passthru = false;
  901. port->dev = dev;
  902. INIT_LIST_HEAD(&port->vlans);
  903. for (i = 0; i < MACVLAN_HASH_SIZE; i++)
  904. INIT_HLIST_HEAD(&port->vlan_hash[i]);
  905. for (i = 0; i < MACVLAN_HASH_SIZE; i++)
  906. INIT_HLIST_HEAD(&port->vlan_source_hash[i]);
  907. skb_queue_head_init(&port->bc_queue);
  908. INIT_WORK(&port->bc_work, macvlan_process_broadcast);
  909. err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
  910. if (err)
  911. kfree(port);
  912. else
  913. dev->priv_flags |= IFF_MACVLAN_PORT;
  914. return err;
  915. }
  916. static void macvlan_port_destroy(struct net_device *dev)
  917. {
  918. struct macvlan_port *port = macvlan_port_get_rtnl(dev);
  919. dev->priv_flags &= ~IFF_MACVLAN_PORT;
  920. netdev_rx_handler_unregister(dev);
  921. /* After this point, no packet can schedule bc_work anymore,
  922. * but we need to cancel it and purge left skbs if any.
  923. */
  924. cancel_work_sync(&port->bc_work);
  925. __skb_queue_purge(&port->bc_queue);
  926. kfree_rcu(port, rcu);
  927. }
  928. static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
  929. {
  930. if (tb[IFLA_ADDRESS]) {
  931. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  932. return -EINVAL;
  933. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  934. return -EADDRNOTAVAIL;
  935. }
  936. if (data && data[IFLA_MACVLAN_FLAGS] &&
  937. nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
  938. return -EINVAL;
  939. if (data && data[IFLA_MACVLAN_MODE]) {
  940. switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
  941. case MACVLAN_MODE_PRIVATE:
  942. case MACVLAN_MODE_VEPA:
  943. case MACVLAN_MODE_BRIDGE:
  944. case MACVLAN_MODE_PASSTHRU:
  945. case MACVLAN_MODE_SOURCE:
  946. break;
  947. default:
  948. return -EINVAL;
  949. }
  950. }
  951. if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
  952. switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) {
  953. case MACVLAN_MACADDR_ADD:
  954. case MACVLAN_MACADDR_DEL:
  955. case MACVLAN_MACADDR_FLUSH:
  956. case MACVLAN_MACADDR_SET:
  957. break;
  958. default:
  959. return -EINVAL;
  960. }
  961. }
  962. if (data && data[IFLA_MACVLAN_MACADDR]) {
  963. if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN)
  964. return -EINVAL;
  965. if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR])))
  966. return -EADDRNOTAVAIL;
  967. }
  968. if (data && data[IFLA_MACVLAN_MACADDR_COUNT])
  969. return -EINVAL;
  970. return 0;
  971. }
  972. /**
  973. * reconfigure list of remote source mac address
  974. * (only for macvlan devices in source mode)
  975. * Note regarding alignment: all netlink data is aligned to 4 Byte, which
  976. * suffices for both ether_addr_copy and ether_addr_equal_64bits usage.
  977. */
  978. static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode,
  979. struct nlattr *data[])
  980. {
  981. char *addr = NULL;
  982. int ret, rem, len;
  983. struct nlattr *nla, *head;
  984. struct macvlan_source_entry *entry;
  985. if (data[IFLA_MACVLAN_MACADDR])
  986. addr = nla_data(data[IFLA_MACVLAN_MACADDR]);
  987. if (mode == MACVLAN_MACADDR_ADD) {
  988. if (!addr)
  989. return -EINVAL;
  990. return macvlan_hash_add_source(vlan, addr);
  991. } else if (mode == MACVLAN_MACADDR_DEL) {
  992. if (!addr)
  993. return -EINVAL;
  994. entry = macvlan_hash_lookup_source(vlan, addr);
  995. if (entry) {
  996. macvlan_hash_del_source(entry);
  997. vlan->macaddr_count--;
  998. }
  999. } else if (mode == MACVLAN_MACADDR_FLUSH) {
  1000. macvlan_flush_sources(vlan->port, vlan);
  1001. } else if (mode == MACVLAN_MACADDR_SET) {
  1002. macvlan_flush_sources(vlan->port, vlan);
  1003. if (addr) {
  1004. ret = macvlan_hash_add_source(vlan, addr);
  1005. if (ret)
  1006. return ret;
  1007. }
  1008. if (!data || !data[IFLA_MACVLAN_MACADDR_DATA])
  1009. return 0;
  1010. head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
  1011. len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
  1012. nla_for_each_attr(nla, head, len, rem) {
  1013. if (nla_type(nla) != IFLA_MACVLAN_MACADDR ||
  1014. nla_len(nla) != ETH_ALEN)
  1015. continue;
  1016. addr = nla_data(nla);
  1017. ret = macvlan_hash_add_source(vlan, addr);
  1018. if (ret)
  1019. return ret;
  1020. }
  1021. } else {
  1022. return -EINVAL;
  1023. }
  1024. return 0;
  1025. }
  1026. int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
  1027. struct nlattr *tb[], struct nlattr *data[])
  1028. {
  1029. struct macvlan_dev *vlan = netdev_priv(dev);
  1030. struct macvlan_port *port;
  1031. struct net_device *lowerdev;
  1032. int err;
  1033. int macmode;
  1034. if (!tb[IFLA_LINK])
  1035. return -EINVAL;
  1036. lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  1037. if (lowerdev == NULL)
  1038. return -ENODEV;
  1039. /* When creating macvlans or macvtaps on top of other macvlans - use
  1040. * the real device as the lowerdev.
  1041. */
  1042. if (netif_is_macvlan(lowerdev))
  1043. lowerdev = macvlan_dev_real_dev(lowerdev);
  1044. if (!tb[IFLA_MTU])
  1045. dev->mtu = lowerdev->mtu;
  1046. else if (dev->mtu > lowerdev->mtu)
  1047. return -EINVAL;
  1048. if (!tb[IFLA_ADDRESS])
  1049. eth_hw_addr_random(dev);
  1050. if (!macvlan_port_exists(lowerdev)) {
  1051. err = macvlan_port_create(lowerdev);
  1052. if (err < 0)
  1053. return err;
  1054. }
  1055. port = macvlan_port_get_rtnl(lowerdev);
  1056. /* Only 1 macvlan device can be created in passthru mode */
  1057. if (port->passthru)
  1058. return -EINVAL;
  1059. vlan->lowerdev = lowerdev;
  1060. vlan->dev = dev;
  1061. vlan->port = port;
  1062. vlan->set_features = MACVLAN_FEATURES;
  1063. vlan->nest_level = dev_get_nest_level(lowerdev, netif_is_macvlan) + 1;
  1064. vlan->mode = MACVLAN_MODE_VEPA;
  1065. if (data && data[IFLA_MACVLAN_MODE])
  1066. vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
  1067. if (data && data[IFLA_MACVLAN_FLAGS])
  1068. vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
  1069. if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
  1070. if (port->count)
  1071. return -EINVAL;
  1072. port->passthru = true;
  1073. eth_hw_addr_inherit(dev, lowerdev);
  1074. }
  1075. if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
  1076. if (vlan->mode != MACVLAN_MODE_SOURCE)
  1077. return -EINVAL;
  1078. macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
  1079. err = macvlan_changelink_sources(vlan, macmode, data);
  1080. if (err)
  1081. return err;
  1082. }
  1083. port->count += 1;
  1084. err = register_netdevice(dev);
  1085. if (err < 0)
  1086. goto destroy_port;
  1087. dev->priv_flags |= IFF_MACVLAN;
  1088. err = netdev_upper_dev_link(lowerdev, dev);
  1089. if (err)
  1090. goto unregister_netdev;
  1091. list_add_tail_rcu(&vlan->list, &port->vlans);
  1092. netif_stacked_transfer_operstate(lowerdev, dev);
  1093. return 0;
  1094. unregister_netdev:
  1095. unregister_netdevice(dev);
  1096. destroy_port:
  1097. port->count -= 1;
  1098. if (!port->count)
  1099. macvlan_port_destroy(lowerdev);
  1100. return err;
  1101. }
  1102. EXPORT_SYMBOL_GPL(macvlan_common_newlink);
  1103. static int macvlan_newlink(struct net *src_net, struct net_device *dev,
  1104. struct nlattr *tb[], struct nlattr *data[])
  1105. {
  1106. return macvlan_common_newlink(src_net, dev, tb, data);
  1107. }
  1108. void macvlan_dellink(struct net_device *dev, struct list_head *head)
  1109. {
  1110. struct macvlan_dev *vlan = netdev_priv(dev);
  1111. if (vlan->mode == MACVLAN_MODE_SOURCE)
  1112. macvlan_flush_sources(vlan->port, vlan);
  1113. list_del_rcu(&vlan->list);
  1114. unregister_netdevice_queue(dev, head);
  1115. netdev_upper_dev_unlink(vlan->lowerdev, dev);
  1116. }
  1117. EXPORT_SYMBOL_GPL(macvlan_dellink);
  1118. static int macvlan_changelink(struct net_device *dev,
  1119. struct nlattr *tb[], struct nlattr *data[])
  1120. {
  1121. struct macvlan_dev *vlan = netdev_priv(dev);
  1122. enum macvlan_mode mode;
  1123. bool set_mode = false;
  1124. enum macvlan_macaddr_mode macmode;
  1125. int ret;
  1126. /* Validate mode, but don't set yet: setting flags may fail. */
  1127. if (data && data[IFLA_MACVLAN_MODE]) {
  1128. set_mode = true;
  1129. mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
  1130. /* Passthrough mode can't be set or cleared dynamically */
  1131. if ((mode == MACVLAN_MODE_PASSTHRU) !=
  1132. (vlan->mode == MACVLAN_MODE_PASSTHRU))
  1133. return -EINVAL;
  1134. if (vlan->mode == MACVLAN_MODE_SOURCE &&
  1135. vlan->mode != mode)
  1136. macvlan_flush_sources(vlan->port, vlan);
  1137. }
  1138. if (data && data[IFLA_MACVLAN_FLAGS]) {
  1139. __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
  1140. bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
  1141. if (vlan->port->passthru && promisc) {
  1142. int err;
  1143. if (flags & MACVLAN_FLAG_NOPROMISC)
  1144. err = dev_set_promiscuity(vlan->lowerdev, -1);
  1145. else
  1146. err = dev_set_promiscuity(vlan->lowerdev, 1);
  1147. if (err < 0)
  1148. return err;
  1149. }
  1150. vlan->flags = flags;
  1151. }
  1152. if (set_mode)
  1153. vlan->mode = mode;
  1154. if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
  1155. if (vlan->mode != MACVLAN_MODE_SOURCE)
  1156. return -EINVAL;
  1157. macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
  1158. ret = macvlan_changelink_sources(vlan, macmode, data);
  1159. if (ret)
  1160. return ret;
  1161. }
  1162. return 0;
  1163. }
  1164. static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan)
  1165. {
  1166. if (vlan->macaddr_count == 0)
  1167. return 0;
  1168. return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */
  1169. + vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN);
  1170. }
  1171. static size_t macvlan_get_size(const struct net_device *dev)
  1172. {
  1173. struct macvlan_dev *vlan = netdev_priv(dev);
  1174. return (0
  1175. + nla_total_size(4) /* IFLA_MACVLAN_MODE */
  1176. + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
  1177. + nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */
  1178. + macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */
  1179. );
  1180. }
  1181. static int macvlan_fill_info_macaddr(struct sk_buff *skb,
  1182. const struct macvlan_dev *vlan,
  1183. const int i)
  1184. {
  1185. struct hlist_head *h = &vlan->port->vlan_source_hash[i];
  1186. struct macvlan_source_entry *entry;
  1187. hlist_for_each_entry_rcu(entry, h, hlist) {
  1188. if (entry->vlan != vlan)
  1189. continue;
  1190. if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr))
  1191. return 1;
  1192. }
  1193. return 0;
  1194. }
  1195. static int macvlan_fill_info(struct sk_buff *skb,
  1196. const struct net_device *dev)
  1197. {
  1198. struct macvlan_dev *vlan = netdev_priv(dev);
  1199. int i;
  1200. struct nlattr *nest;
  1201. if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
  1202. goto nla_put_failure;
  1203. if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
  1204. goto nla_put_failure;
  1205. if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count))
  1206. goto nla_put_failure;
  1207. if (vlan->macaddr_count > 0) {
  1208. nest = nla_nest_start(skb, IFLA_MACVLAN_MACADDR_DATA);
  1209. if (nest == NULL)
  1210. goto nla_put_failure;
  1211. for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
  1212. if (macvlan_fill_info_macaddr(skb, vlan, i))
  1213. goto nla_put_failure;
  1214. }
  1215. nla_nest_end(skb, nest);
  1216. }
  1217. return 0;
  1218. nla_put_failure:
  1219. return -EMSGSIZE;
  1220. }
  1221. static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
  1222. [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
  1223. [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
  1224. [IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 },
  1225. [IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  1226. [IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED },
  1227. [IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 },
  1228. };
  1229. int macvlan_link_register(struct rtnl_link_ops *ops)
  1230. {
  1231. /* common fields */
  1232. ops->priv_size = sizeof(struct macvlan_dev);
  1233. ops->validate = macvlan_validate;
  1234. ops->maxtype = IFLA_MACVLAN_MAX;
  1235. ops->policy = macvlan_policy;
  1236. ops->changelink = macvlan_changelink;
  1237. ops->get_size = macvlan_get_size;
  1238. ops->fill_info = macvlan_fill_info;
  1239. return rtnl_link_register(ops);
  1240. };
  1241. EXPORT_SYMBOL_GPL(macvlan_link_register);
  1242. static struct net *macvlan_get_link_net(const struct net_device *dev)
  1243. {
  1244. return dev_net(macvlan_dev_real_dev(dev));
  1245. }
  1246. static struct rtnl_link_ops macvlan_link_ops = {
  1247. .kind = "macvlan",
  1248. .setup = macvlan_setup,
  1249. .newlink = macvlan_newlink,
  1250. .dellink = macvlan_dellink,
  1251. .get_link_net = macvlan_get_link_net,
  1252. };
  1253. static int macvlan_device_event(struct notifier_block *unused,
  1254. unsigned long event, void *ptr)
  1255. {
  1256. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1257. struct macvlan_dev *vlan, *next;
  1258. struct macvlan_port *port;
  1259. LIST_HEAD(list_kill);
  1260. if (!macvlan_port_exists(dev))
  1261. return NOTIFY_DONE;
  1262. port = macvlan_port_get_rtnl(dev);
  1263. switch (event) {
  1264. case NETDEV_CHANGE:
  1265. list_for_each_entry(vlan, &port->vlans, list)
  1266. netif_stacked_transfer_operstate(vlan->lowerdev,
  1267. vlan->dev);
  1268. break;
  1269. case NETDEV_FEAT_CHANGE:
  1270. list_for_each_entry(vlan, &port->vlans, list) {
  1271. vlan->dev->gso_max_size = dev->gso_max_size;
  1272. netdev_update_features(vlan->dev);
  1273. }
  1274. break;
  1275. case NETDEV_CHANGEMTU:
  1276. list_for_each_entry(vlan, &port->vlans, list) {
  1277. if (vlan->dev->mtu <= dev->mtu)
  1278. continue;
  1279. dev_set_mtu(vlan->dev, dev->mtu);
  1280. }
  1281. break;
  1282. case NETDEV_CHANGEADDR:
  1283. if (!port->passthru)
  1284. return NOTIFY_DONE;
  1285. vlan = list_first_entry_or_null(&port->vlans,
  1286. struct macvlan_dev,
  1287. list);
  1288. if (macvlan_sync_address(vlan->dev, dev->dev_addr))
  1289. return NOTIFY_BAD;
  1290. break;
  1291. case NETDEV_UNREGISTER:
  1292. /* twiddle thumbs on netns device moves */
  1293. if (dev->reg_state != NETREG_UNREGISTERING)
  1294. break;
  1295. list_for_each_entry_safe(vlan, next, &port->vlans, list)
  1296. vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
  1297. unregister_netdevice_many(&list_kill);
  1298. break;
  1299. case NETDEV_PRE_TYPE_CHANGE:
  1300. /* Forbid underlaying device to change its type. */
  1301. return NOTIFY_BAD;
  1302. case NETDEV_NOTIFY_PEERS:
  1303. case NETDEV_BONDING_FAILOVER:
  1304. case NETDEV_RESEND_IGMP:
  1305. /* Propagate to all vlans */
  1306. list_for_each_entry(vlan, &port->vlans, list)
  1307. call_netdevice_notifiers(event, vlan->dev);
  1308. }
  1309. return NOTIFY_DONE;
  1310. }
  1311. static struct notifier_block macvlan_notifier_block __read_mostly = {
  1312. .notifier_call = macvlan_device_event,
  1313. };
  1314. static int __init macvlan_init_module(void)
  1315. {
  1316. int err;
  1317. register_netdevice_notifier(&macvlan_notifier_block);
  1318. err = macvlan_link_register(&macvlan_link_ops);
  1319. if (err < 0)
  1320. goto err1;
  1321. return 0;
  1322. err1:
  1323. unregister_netdevice_notifier(&macvlan_notifier_block);
  1324. return err;
  1325. }
  1326. static void __exit macvlan_cleanup_module(void)
  1327. {
  1328. rtnl_link_unregister(&macvlan_link_ops);
  1329. unregister_netdevice_notifier(&macvlan_notifier_block);
  1330. }
  1331. module_init(macvlan_init_module);
  1332. module_exit(macvlan_cleanup_module);
  1333. MODULE_LICENSE("GPL");
  1334. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  1335. MODULE_DESCRIPTION("Driver for MAC address based VLANs");
  1336. MODULE_ALIAS_RTNL_LINK("macvlan");