macvlan.c 41 KB

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