vxlan.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. /*
  2. * VXLAN: Virtual eXtensible Local Area Network
  3. *
  4. * Copyright (c) 2012-2013 Vyatta Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * TODO
  11. * - IPv6 (not in RFC)
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/module.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/rculist.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/in.h>
  23. #include <linux/ip.h>
  24. #include <linux/udp.h>
  25. #include <linux/igmp.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/hash.h>
  29. #include <linux/ethtool.h>
  30. #include <net/arp.h>
  31. #include <net/ndisc.h>
  32. #include <net/ip.h>
  33. #include <net/ip_tunnels.h>
  34. #include <net/icmp.h>
  35. #include <net/udp.h>
  36. #include <net/rtnetlink.h>
  37. #include <net/route.h>
  38. #include <net/dsfield.h>
  39. #include <net/inet_ecn.h>
  40. #include <net/net_namespace.h>
  41. #include <net/netns/generic.h>
  42. #define VXLAN_VERSION "0.1"
  43. #define PORT_HASH_BITS 8
  44. #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
  45. #define VNI_HASH_BITS 10
  46. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  47. #define FDB_HASH_BITS 8
  48. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  49. #define FDB_AGE_DEFAULT 300 /* 5 min */
  50. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  51. #define VXLAN_N_VID (1u << 24)
  52. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  53. /* IP header + UDP + VXLAN + Ethernet header */
  54. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  55. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  56. /* VXLAN protocol header */
  57. struct vxlanhdr {
  58. __be32 vx_flags;
  59. __be32 vx_vni;
  60. };
  61. /* UDP port for VXLAN traffic.
  62. * The IANA assigned port is 4789, but the Linux default is 8472
  63. * for compatability with early adopters.
  64. */
  65. static unsigned int vxlan_port __read_mostly = 8472;
  66. module_param_named(udp_port, vxlan_port, uint, 0444);
  67. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  68. static bool log_ecn_error = true;
  69. module_param(log_ecn_error, bool, 0644);
  70. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  71. static unsigned int vxlan_net_id;
  72. /* per UDP socket information */
  73. struct vxlan_sock {
  74. struct hlist_node hlist;
  75. struct rcu_head rcu;
  76. struct work_struct del_work;
  77. unsigned int refcnt;
  78. struct socket *sock;
  79. struct hlist_head vni_list[VNI_HASH_SIZE];
  80. };
  81. /* per-network namespace private data for this module */
  82. struct vxlan_net {
  83. struct list_head vxlan_list;
  84. struct hlist_head sock_list[PORT_HASH_SIZE];
  85. };
  86. struct vxlan_rdst {
  87. __be32 remote_ip;
  88. __be16 remote_port;
  89. u32 remote_vni;
  90. u32 remote_ifindex;
  91. struct vxlan_rdst *remote_next;
  92. };
  93. /* Forwarding table entry */
  94. struct vxlan_fdb {
  95. struct hlist_node hlist; /* linked list of entries */
  96. struct rcu_head rcu;
  97. unsigned long updated; /* jiffies */
  98. unsigned long used;
  99. struct vxlan_rdst remote;
  100. u16 state; /* see ndm_state */
  101. u8 flags; /* see ndm_flags */
  102. u8 eth_addr[ETH_ALEN];
  103. };
  104. /* Pseudo network device */
  105. struct vxlan_dev {
  106. struct hlist_node hlist; /* vni hash table */
  107. struct list_head next; /* vxlan's per namespace list */
  108. struct vxlan_sock *vn_sock; /* listening socket */
  109. struct net_device *dev;
  110. struct vxlan_rdst default_dst; /* default destination */
  111. __be32 saddr; /* source address */
  112. __be16 dst_port;
  113. __u16 port_min; /* source port range */
  114. __u16 port_max;
  115. __u8 tos; /* TOS override */
  116. __u8 ttl;
  117. u32 flags; /* VXLAN_F_* below */
  118. unsigned long age_interval;
  119. struct timer_list age_timer;
  120. spinlock_t hash_lock;
  121. unsigned int addrcnt;
  122. unsigned int addrmax;
  123. struct hlist_head fdb_head[FDB_HASH_SIZE];
  124. };
  125. #define VXLAN_F_LEARN 0x01
  126. #define VXLAN_F_PROXY 0x02
  127. #define VXLAN_F_RSC 0x04
  128. #define VXLAN_F_L2MISS 0x08
  129. #define VXLAN_F_L3MISS 0x10
  130. /* salt for hash table */
  131. static u32 vxlan_salt __read_mostly;
  132. static struct workqueue_struct *vxlan_wq;
  133. /* Virtual Network hash table head */
  134. static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
  135. {
  136. return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
  137. }
  138. /* Socket hash table head */
  139. static inline struct hlist_head *vs_head(struct net *net, __be16 port)
  140. {
  141. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  142. return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
  143. }
  144. /* Find VXLAN socket based on network namespace and UDP port */
  145. static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
  146. {
  147. struct vxlan_sock *vs;
  148. hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
  149. if (inet_sk(vs->sock->sk)->inet_sport == port)
  150. return vs;
  151. }
  152. return NULL;
  153. }
  154. /* Look up VNI in a per net namespace table */
  155. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
  156. {
  157. struct vxlan_sock *vs;
  158. struct vxlan_dev *vxlan;
  159. vs = vxlan_find_port(net, port);
  160. if (!vs)
  161. return NULL;
  162. hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
  163. if (vxlan->default_dst.remote_vni == id)
  164. return vxlan;
  165. }
  166. return NULL;
  167. }
  168. /* Fill in neighbour message in skbuff. */
  169. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  170. const struct vxlan_fdb *fdb,
  171. u32 portid, u32 seq, int type, unsigned int flags,
  172. const struct vxlan_rdst *rdst)
  173. {
  174. unsigned long now = jiffies;
  175. struct nda_cacheinfo ci;
  176. struct nlmsghdr *nlh;
  177. struct ndmsg *ndm;
  178. bool send_ip, send_eth;
  179. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  180. if (nlh == NULL)
  181. return -EMSGSIZE;
  182. ndm = nlmsg_data(nlh);
  183. memset(ndm, 0, sizeof(*ndm));
  184. send_eth = send_ip = true;
  185. if (type == RTM_GETNEIGH) {
  186. ndm->ndm_family = AF_INET;
  187. send_ip = rdst->remote_ip != htonl(INADDR_ANY);
  188. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  189. } else
  190. ndm->ndm_family = AF_BRIDGE;
  191. ndm->ndm_state = fdb->state;
  192. ndm->ndm_ifindex = vxlan->dev->ifindex;
  193. ndm->ndm_flags = fdb->flags;
  194. ndm->ndm_type = NDA_DST;
  195. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  196. goto nla_put_failure;
  197. if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
  198. goto nla_put_failure;
  199. if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
  200. nla_put_be16(skb, NDA_PORT, rdst->remote_port))
  201. goto nla_put_failure;
  202. if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
  203. nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
  204. goto nla_put_failure;
  205. if (rdst->remote_ifindex &&
  206. nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
  207. goto nla_put_failure;
  208. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  209. ci.ndm_confirmed = 0;
  210. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  211. ci.ndm_refcnt = 0;
  212. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  213. goto nla_put_failure;
  214. return nlmsg_end(skb, nlh);
  215. nla_put_failure:
  216. nlmsg_cancel(skb, nlh);
  217. return -EMSGSIZE;
  218. }
  219. static inline size_t vxlan_nlmsg_size(void)
  220. {
  221. return NLMSG_ALIGN(sizeof(struct ndmsg))
  222. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  223. + nla_total_size(sizeof(__be32)) /* NDA_DST */
  224. + nla_total_size(sizeof(__be16)) /* NDA_PORT */
  225. + nla_total_size(sizeof(__be32)) /* NDA_VNI */
  226. + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
  227. + nla_total_size(sizeof(struct nda_cacheinfo));
  228. }
  229. static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
  230. const struct vxlan_fdb *fdb, int type)
  231. {
  232. struct net *net = dev_net(vxlan->dev);
  233. struct sk_buff *skb;
  234. int err = -ENOBUFS;
  235. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  236. if (skb == NULL)
  237. goto errout;
  238. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
  239. if (err < 0) {
  240. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  241. WARN_ON(err == -EMSGSIZE);
  242. kfree_skb(skb);
  243. goto errout;
  244. }
  245. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  246. return;
  247. errout:
  248. if (err < 0)
  249. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  250. }
  251. static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
  252. {
  253. struct vxlan_dev *vxlan = netdev_priv(dev);
  254. struct vxlan_fdb f;
  255. memset(&f, 0, sizeof f);
  256. f.state = NUD_STALE;
  257. f.remote.remote_ip = ipa; /* goes to NDA_DST */
  258. f.remote.remote_vni = VXLAN_N_VID;
  259. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  260. }
  261. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  262. {
  263. struct vxlan_fdb f;
  264. memset(&f, 0, sizeof f);
  265. f.state = NUD_STALE;
  266. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  267. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  268. }
  269. /* Hash Ethernet address */
  270. static u32 eth_hash(const unsigned char *addr)
  271. {
  272. u64 value = get_unaligned((u64 *)addr);
  273. /* only want 6 bytes */
  274. #ifdef __BIG_ENDIAN
  275. value >>= 16;
  276. #else
  277. value <<= 16;
  278. #endif
  279. return hash_64(value, FDB_HASH_BITS);
  280. }
  281. /* Hash chain to use given mac address */
  282. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  283. const u8 *mac)
  284. {
  285. return &vxlan->fdb_head[eth_hash(mac)];
  286. }
  287. /* Look up Ethernet address in forwarding table */
  288. static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
  289. const u8 *mac)
  290. {
  291. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  292. struct vxlan_fdb *f;
  293. hlist_for_each_entry_rcu(f, head, hlist) {
  294. if (compare_ether_addr(mac, f->eth_addr) == 0)
  295. return f;
  296. }
  297. return NULL;
  298. }
  299. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  300. const u8 *mac)
  301. {
  302. struct vxlan_fdb *f;
  303. f = __vxlan_find_mac(vxlan, mac);
  304. if (f)
  305. f->used = jiffies;
  306. return f;
  307. }
  308. /* Add/update destinations for multicast */
  309. static int vxlan_fdb_append(struct vxlan_fdb *f,
  310. __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
  311. {
  312. struct vxlan_rdst *rd_prev, *rd;
  313. rd_prev = NULL;
  314. for (rd = &f->remote; rd; rd = rd->remote_next) {
  315. if (rd->remote_ip == ip &&
  316. rd->remote_port == port &&
  317. rd->remote_vni == vni &&
  318. rd->remote_ifindex == ifindex)
  319. return 0;
  320. rd_prev = rd;
  321. }
  322. rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
  323. if (rd == NULL)
  324. return -ENOBUFS;
  325. rd->remote_ip = ip;
  326. rd->remote_port = port;
  327. rd->remote_vni = vni;
  328. rd->remote_ifindex = ifindex;
  329. rd->remote_next = NULL;
  330. rd_prev->remote_next = rd;
  331. return 1;
  332. }
  333. /* Add new entry to forwarding table -- assumes lock held */
  334. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  335. const u8 *mac, __be32 ip,
  336. __u16 state, __u16 flags,
  337. __be16 port, __u32 vni, __u32 ifindex,
  338. __u8 ndm_flags)
  339. {
  340. struct vxlan_fdb *f;
  341. int notify = 0;
  342. f = __vxlan_find_mac(vxlan, mac);
  343. if (f) {
  344. if (flags & NLM_F_EXCL) {
  345. netdev_dbg(vxlan->dev,
  346. "lost race to create %pM\n", mac);
  347. return -EEXIST;
  348. }
  349. if (f->state != state) {
  350. f->state = state;
  351. f->updated = jiffies;
  352. notify = 1;
  353. }
  354. if (f->flags != ndm_flags) {
  355. f->flags = ndm_flags;
  356. f->updated = jiffies;
  357. notify = 1;
  358. }
  359. if ((flags & NLM_F_APPEND) &&
  360. is_multicast_ether_addr(f->eth_addr)) {
  361. int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
  362. if (rc < 0)
  363. return rc;
  364. notify |= rc;
  365. }
  366. } else {
  367. if (!(flags & NLM_F_CREATE))
  368. return -ENOENT;
  369. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  370. return -ENOSPC;
  371. netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
  372. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  373. if (!f)
  374. return -ENOMEM;
  375. notify = 1;
  376. f->remote.remote_ip = ip;
  377. f->remote.remote_port = port;
  378. f->remote.remote_vni = vni;
  379. f->remote.remote_ifindex = ifindex;
  380. f->remote.remote_next = NULL;
  381. f->state = state;
  382. f->flags = ndm_flags;
  383. f->updated = f->used = jiffies;
  384. memcpy(f->eth_addr, mac, ETH_ALEN);
  385. ++vxlan->addrcnt;
  386. hlist_add_head_rcu(&f->hlist,
  387. vxlan_fdb_head(vxlan, mac));
  388. }
  389. if (notify)
  390. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  391. return 0;
  392. }
  393. static void vxlan_fdb_free(struct rcu_head *head)
  394. {
  395. struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
  396. while (f->remote.remote_next) {
  397. struct vxlan_rdst *rd = f->remote.remote_next;
  398. f->remote.remote_next = rd->remote_next;
  399. kfree(rd);
  400. }
  401. kfree(f);
  402. }
  403. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  404. {
  405. netdev_dbg(vxlan->dev,
  406. "delete %pM\n", f->eth_addr);
  407. --vxlan->addrcnt;
  408. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  409. hlist_del_rcu(&f->hlist);
  410. call_rcu(&f->rcu, vxlan_fdb_free);
  411. }
  412. /* Add static entry (via netlink) */
  413. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  414. struct net_device *dev,
  415. const unsigned char *addr, u16 flags)
  416. {
  417. struct vxlan_dev *vxlan = netdev_priv(dev);
  418. struct net *net = dev_net(vxlan->dev);
  419. __be32 ip;
  420. __be16 port;
  421. u32 vni, ifindex;
  422. int err;
  423. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  424. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  425. ndm->ndm_state);
  426. return -EINVAL;
  427. }
  428. if (tb[NDA_DST] == NULL)
  429. return -EINVAL;
  430. if (nla_len(tb[NDA_DST]) != sizeof(__be32))
  431. return -EAFNOSUPPORT;
  432. ip = nla_get_be32(tb[NDA_DST]);
  433. if (tb[NDA_PORT]) {
  434. if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
  435. return -EINVAL;
  436. port = nla_get_be16(tb[NDA_PORT]);
  437. } else
  438. port = vxlan->dst_port;
  439. if (tb[NDA_VNI]) {
  440. if (nla_len(tb[NDA_VNI]) != sizeof(u32))
  441. return -EINVAL;
  442. vni = nla_get_u32(tb[NDA_VNI]);
  443. } else
  444. vni = vxlan->default_dst.remote_vni;
  445. if (tb[NDA_IFINDEX]) {
  446. struct net_device *tdev;
  447. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
  448. return -EINVAL;
  449. ifindex = nla_get_u32(tb[NDA_IFINDEX]);
  450. tdev = dev_get_by_index(net, ifindex);
  451. if (!tdev)
  452. return -EADDRNOTAVAIL;
  453. dev_put(tdev);
  454. } else
  455. ifindex = 0;
  456. spin_lock_bh(&vxlan->hash_lock);
  457. err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
  458. port, vni, ifindex, ndm->ndm_flags);
  459. spin_unlock_bh(&vxlan->hash_lock);
  460. return err;
  461. }
  462. /* Delete entry (via netlink) */
  463. static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  464. struct net_device *dev,
  465. const unsigned char *addr)
  466. {
  467. struct vxlan_dev *vxlan = netdev_priv(dev);
  468. struct vxlan_fdb *f;
  469. int err = -ENOENT;
  470. spin_lock_bh(&vxlan->hash_lock);
  471. f = vxlan_find_mac(vxlan, addr);
  472. if (f) {
  473. vxlan_fdb_destroy(vxlan, f);
  474. err = 0;
  475. }
  476. spin_unlock_bh(&vxlan->hash_lock);
  477. return err;
  478. }
  479. /* Dump forwarding table */
  480. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  481. struct net_device *dev, int idx)
  482. {
  483. struct vxlan_dev *vxlan = netdev_priv(dev);
  484. unsigned int h;
  485. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  486. struct vxlan_fdb *f;
  487. int err;
  488. hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
  489. struct vxlan_rdst *rd;
  490. for (rd = &f->remote; rd; rd = rd->remote_next) {
  491. if (idx < cb->args[0])
  492. goto skip;
  493. err = vxlan_fdb_info(skb, vxlan, f,
  494. NETLINK_CB(cb->skb).portid,
  495. cb->nlh->nlmsg_seq,
  496. RTM_NEWNEIGH,
  497. NLM_F_MULTI, rd);
  498. if (err < 0)
  499. break;
  500. skip:
  501. ++idx;
  502. }
  503. }
  504. }
  505. return idx;
  506. }
  507. /* Watch incoming packets to learn mapping between Ethernet address
  508. * and Tunnel endpoint.
  509. * Return true if packet is bogus and should be droppped.
  510. */
  511. static bool vxlan_snoop(struct net_device *dev,
  512. __be32 src_ip, const u8 *src_mac)
  513. {
  514. struct vxlan_dev *vxlan = netdev_priv(dev);
  515. struct vxlan_fdb *f;
  516. f = vxlan_find_mac(vxlan, src_mac);
  517. if (likely(f)) {
  518. if (likely(f->remote.remote_ip == src_ip))
  519. return false;
  520. /* Don't migrate static entries, drop packets */
  521. if (f->state & NUD_NOARP)
  522. return true;
  523. if (net_ratelimit())
  524. netdev_info(dev,
  525. "%pM migrated from %pI4 to %pI4\n",
  526. src_mac, &f->remote.remote_ip, &src_ip);
  527. f->remote.remote_ip = src_ip;
  528. f->updated = jiffies;
  529. } else {
  530. /* learned new entry */
  531. spin_lock(&vxlan->hash_lock);
  532. /* close off race between vxlan_flush and incoming packets */
  533. if (netif_running(dev))
  534. vxlan_fdb_create(vxlan, src_mac, src_ip,
  535. NUD_REACHABLE,
  536. NLM_F_EXCL|NLM_F_CREATE,
  537. vxlan->dst_port,
  538. vxlan->default_dst.remote_vni,
  539. 0, NTF_SELF);
  540. spin_unlock(&vxlan->hash_lock);
  541. }
  542. return false;
  543. }
  544. /* See if multicast group is already in use by other ID */
  545. static bool vxlan_group_used(struct vxlan_net *vn,
  546. const struct vxlan_dev *this)
  547. {
  548. struct vxlan_dev *vxlan;
  549. list_for_each_entry(vxlan, &vn->vxlan_list, next) {
  550. if (vxlan == this)
  551. continue;
  552. if (!netif_running(vxlan->dev))
  553. continue;
  554. if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
  555. return true;
  556. }
  557. return false;
  558. }
  559. /* kernel equivalent to IP_ADD_MEMBERSHIP */
  560. static int vxlan_join_group(struct net_device *dev)
  561. {
  562. struct vxlan_dev *vxlan = netdev_priv(dev);
  563. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  564. struct sock *sk = vxlan->vn_sock->sock->sk;
  565. struct ip_mreqn mreq = {
  566. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  567. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  568. };
  569. int err;
  570. /* Already a member of group */
  571. if (vxlan_group_used(vn, vxlan))
  572. return 0;
  573. /* Need to drop RTNL to call multicast join */
  574. rtnl_unlock();
  575. lock_sock(sk);
  576. err = ip_mc_join_group(sk, &mreq);
  577. release_sock(sk);
  578. rtnl_lock();
  579. return err;
  580. }
  581. /* kernel equivalent to IP_DROP_MEMBERSHIP */
  582. static int vxlan_leave_group(struct net_device *dev)
  583. {
  584. struct vxlan_dev *vxlan = netdev_priv(dev);
  585. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  586. int err = 0;
  587. struct sock *sk = vxlan->vn_sock->sock->sk;
  588. struct ip_mreqn mreq = {
  589. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  590. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  591. };
  592. /* Only leave group when last vxlan is done. */
  593. if (vxlan_group_used(vn, vxlan))
  594. return 0;
  595. /* Need to drop RTNL to call multicast leave */
  596. rtnl_unlock();
  597. lock_sock(sk);
  598. err = ip_mc_leave_group(sk, &mreq);
  599. release_sock(sk);
  600. rtnl_lock();
  601. return err;
  602. }
  603. /* Callback from net/ipv4/udp.c to receive packets */
  604. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  605. {
  606. struct iphdr *oip;
  607. struct vxlanhdr *vxh;
  608. struct vxlan_dev *vxlan;
  609. struct pcpu_tstats *stats;
  610. __be16 port;
  611. __u32 vni;
  612. int err;
  613. /* pop off outer UDP header */
  614. __skb_pull(skb, sizeof(struct udphdr));
  615. /* Need Vxlan and inner Ethernet header to be present */
  616. if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
  617. goto error;
  618. /* Drop packets with reserved bits set */
  619. vxh = (struct vxlanhdr *) skb->data;
  620. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  621. (vxh->vx_vni & htonl(0xff))) {
  622. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  623. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  624. goto error;
  625. }
  626. __skb_pull(skb, sizeof(struct vxlanhdr));
  627. /* Is this VNI defined? */
  628. vni = ntohl(vxh->vx_vni) >> 8;
  629. port = inet_sk(sk)->inet_sport;
  630. vxlan = vxlan_find_vni(sock_net(sk), vni, port);
  631. if (!vxlan) {
  632. netdev_dbg(skb->dev, "unknown vni %d port %u\n",
  633. vni, ntohs(port));
  634. goto drop;
  635. }
  636. if (!pskb_may_pull(skb, ETH_HLEN)) {
  637. vxlan->dev->stats.rx_length_errors++;
  638. vxlan->dev->stats.rx_errors++;
  639. goto drop;
  640. }
  641. skb_reset_mac_header(skb);
  642. /* Re-examine inner Ethernet packet */
  643. oip = ip_hdr(skb);
  644. skb->protocol = eth_type_trans(skb, vxlan->dev);
  645. /* Ignore packet loops (and multicast echo) */
  646. if (compare_ether_addr(eth_hdr(skb)->h_source,
  647. vxlan->dev->dev_addr) == 0)
  648. goto drop;
  649. if ((vxlan->flags & VXLAN_F_LEARN) &&
  650. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
  651. goto drop;
  652. __skb_tunnel_rx(skb, vxlan->dev);
  653. skb_reset_network_header(skb);
  654. /* If the NIC driver gave us an encapsulated packet with
  655. * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
  656. * leave the CHECKSUM_UNNECESSARY, the device checksummed it
  657. * for us. Otherwise force the upper layers to verify it.
  658. */
  659. if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
  660. !(vxlan->dev->features & NETIF_F_RXCSUM))
  661. skb->ip_summed = CHECKSUM_NONE;
  662. skb->encapsulation = 0;
  663. err = IP_ECN_decapsulate(oip, skb);
  664. if (unlikely(err)) {
  665. if (log_ecn_error)
  666. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  667. &oip->saddr, oip->tos);
  668. if (err > 1) {
  669. ++vxlan->dev->stats.rx_frame_errors;
  670. ++vxlan->dev->stats.rx_errors;
  671. goto drop;
  672. }
  673. }
  674. stats = this_cpu_ptr(vxlan->dev->tstats);
  675. u64_stats_update_begin(&stats->syncp);
  676. stats->rx_packets++;
  677. stats->rx_bytes += skb->len;
  678. u64_stats_update_end(&stats->syncp);
  679. netif_rx(skb);
  680. return 0;
  681. error:
  682. /* Put UDP header back */
  683. __skb_push(skb, sizeof(struct udphdr));
  684. return 1;
  685. drop:
  686. /* Consume bad packet */
  687. kfree_skb(skb);
  688. return 0;
  689. }
  690. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  691. {
  692. struct vxlan_dev *vxlan = netdev_priv(dev);
  693. struct arphdr *parp;
  694. u8 *arpptr, *sha;
  695. __be32 sip, tip;
  696. struct neighbour *n;
  697. if (dev->flags & IFF_NOARP)
  698. goto out;
  699. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  700. dev->stats.tx_dropped++;
  701. goto out;
  702. }
  703. parp = arp_hdr(skb);
  704. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  705. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  706. parp->ar_pro != htons(ETH_P_IP) ||
  707. parp->ar_op != htons(ARPOP_REQUEST) ||
  708. parp->ar_hln != dev->addr_len ||
  709. parp->ar_pln != 4)
  710. goto out;
  711. arpptr = (u8 *)parp + sizeof(struct arphdr);
  712. sha = arpptr;
  713. arpptr += dev->addr_len; /* sha */
  714. memcpy(&sip, arpptr, sizeof(sip));
  715. arpptr += sizeof(sip);
  716. arpptr += dev->addr_len; /* tha */
  717. memcpy(&tip, arpptr, sizeof(tip));
  718. if (ipv4_is_loopback(tip) ||
  719. ipv4_is_multicast(tip))
  720. goto out;
  721. n = neigh_lookup(&arp_tbl, &tip, dev);
  722. if (n) {
  723. struct vxlan_fdb *f;
  724. struct sk_buff *reply;
  725. if (!(n->nud_state & NUD_CONNECTED)) {
  726. neigh_release(n);
  727. goto out;
  728. }
  729. f = vxlan_find_mac(vxlan, n->ha);
  730. if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
  731. /* bridge-local neighbor */
  732. neigh_release(n);
  733. goto out;
  734. }
  735. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  736. n->ha, sha);
  737. neigh_release(n);
  738. skb_reset_mac_header(reply);
  739. __skb_pull(reply, skb_network_offset(reply));
  740. reply->ip_summed = CHECKSUM_UNNECESSARY;
  741. reply->pkt_type = PACKET_HOST;
  742. if (netif_rx_ni(reply) == NET_RX_DROP)
  743. dev->stats.rx_dropped++;
  744. } else if (vxlan->flags & VXLAN_F_L3MISS)
  745. vxlan_ip_miss(dev, tip);
  746. out:
  747. consume_skb(skb);
  748. return NETDEV_TX_OK;
  749. }
  750. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  751. {
  752. struct vxlan_dev *vxlan = netdev_priv(dev);
  753. struct neighbour *n;
  754. struct iphdr *pip;
  755. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  756. return false;
  757. n = NULL;
  758. switch (ntohs(eth_hdr(skb)->h_proto)) {
  759. case ETH_P_IP:
  760. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  761. return false;
  762. pip = ip_hdr(skb);
  763. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  764. break;
  765. default:
  766. return false;
  767. }
  768. if (n) {
  769. bool diff;
  770. diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
  771. if (diff) {
  772. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  773. dev->addr_len);
  774. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  775. }
  776. neigh_release(n);
  777. return diff;
  778. } else if (vxlan->flags & VXLAN_F_L3MISS)
  779. vxlan_ip_miss(dev, pip->daddr);
  780. return false;
  781. }
  782. static void vxlan_sock_put(struct sk_buff *skb)
  783. {
  784. sock_put(skb->sk);
  785. }
  786. /* On transmit, associate with the tunnel socket */
  787. static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
  788. {
  789. struct vxlan_dev *vxlan = netdev_priv(dev);
  790. struct sock *sk = vxlan->vn_sock->sock->sk;
  791. skb_orphan(skb);
  792. sock_hold(sk);
  793. skb->sk = sk;
  794. skb->destructor = vxlan_sock_put;
  795. }
  796. /* Compute source port for outgoing packet
  797. * first choice to use L4 flow hash since it will spread
  798. * better and maybe available from hardware
  799. * secondary choice is to use jhash on the Ethernet header
  800. */
  801. static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
  802. {
  803. unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
  804. u32 hash;
  805. hash = skb_get_rxhash(skb);
  806. if (!hash)
  807. hash = jhash(skb->data, 2 * ETH_ALEN,
  808. (__force u32) skb->protocol);
  809. return htons((((u64) hash * range) >> 32) + vxlan->port_min);
  810. }
  811. static int handle_offloads(struct sk_buff *skb)
  812. {
  813. if (skb_is_gso(skb)) {
  814. int err = skb_unclone(skb, GFP_ATOMIC);
  815. if (unlikely(err))
  816. return err;
  817. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  818. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  819. skb->ip_summed = CHECKSUM_NONE;
  820. return 0;
  821. }
  822. /* Bypass encapsulation if the destination is local */
  823. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  824. struct vxlan_dev *dst_vxlan)
  825. {
  826. struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  827. struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  828. skb->pkt_type = PACKET_HOST;
  829. skb->encapsulation = 0;
  830. skb->dev = dst_vxlan->dev;
  831. __skb_pull(skb, skb_network_offset(skb));
  832. if (dst_vxlan->flags & VXLAN_F_LEARN)
  833. vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
  834. eth_hdr(skb)->h_source);
  835. u64_stats_update_begin(&tx_stats->syncp);
  836. tx_stats->tx_packets++;
  837. tx_stats->tx_bytes += skb->len;
  838. u64_stats_update_end(&tx_stats->syncp);
  839. if (netif_rx(skb) == NET_RX_SUCCESS) {
  840. u64_stats_update_begin(&rx_stats->syncp);
  841. rx_stats->rx_packets++;
  842. rx_stats->rx_bytes += skb->len;
  843. u64_stats_update_end(&rx_stats->syncp);
  844. } else {
  845. skb->dev->stats.rx_dropped++;
  846. }
  847. }
  848. static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  849. struct vxlan_rdst *rdst, bool did_rsc)
  850. {
  851. struct vxlan_dev *vxlan = netdev_priv(dev);
  852. struct rtable *rt;
  853. const struct iphdr *old_iph;
  854. struct vxlanhdr *vxh;
  855. struct udphdr *uh;
  856. struct flowi4 fl4;
  857. __be32 dst;
  858. __be16 src_port, dst_port;
  859. u32 vni;
  860. __be16 df = 0;
  861. __u8 tos, ttl;
  862. int err;
  863. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
  864. vni = rdst->remote_vni;
  865. dst = rdst->remote_ip;
  866. if (!dst) {
  867. if (did_rsc) {
  868. /* short-circuited back to local bridge */
  869. vxlan_encap_bypass(skb, vxlan, vxlan);
  870. return NETDEV_TX_OK;
  871. }
  872. goto drop;
  873. }
  874. if (!skb->encapsulation) {
  875. skb_reset_inner_headers(skb);
  876. skb->encapsulation = 1;
  877. }
  878. /* Need space for new headers (invalidates iph ptr) */
  879. if (skb_cow_head(skb, VXLAN_HEADROOM))
  880. goto drop;
  881. old_iph = ip_hdr(skb);
  882. ttl = vxlan->ttl;
  883. if (!ttl && IN_MULTICAST(ntohl(dst)))
  884. ttl = 1;
  885. tos = vxlan->tos;
  886. if (tos == 1)
  887. tos = ip_tunnel_get_dsfield(old_iph, skb);
  888. src_port = vxlan_src_port(vxlan, skb);
  889. memset(&fl4, 0, sizeof(fl4));
  890. fl4.flowi4_oif = rdst->remote_ifindex;
  891. fl4.flowi4_tos = RT_TOS(tos);
  892. fl4.daddr = dst;
  893. fl4.saddr = vxlan->saddr;
  894. rt = ip_route_output_key(dev_net(dev), &fl4);
  895. if (IS_ERR(rt)) {
  896. netdev_dbg(dev, "no route to %pI4\n", &dst);
  897. dev->stats.tx_carrier_errors++;
  898. goto tx_error;
  899. }
  900. if (rt->dst.dev == dev) {
  901. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  902. ip_rt_put(rt);
  903. dev->stats.collisions++;
  904. goto tx_error;
  905. }
  906. /* Bypass encapsulation if the destination is local */
  907. if (rt->rt_flags & RTCF_LOCAL &&
  908. !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  909. struct vxlan_dev *dst_vxlan;
  910. ip_rt_put(rt);
  911. dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
  912. if (!dst_vxlan)
  913. goto tx_error;
  914. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  915. return NETDEV_TX_OK;
  916. }
  917. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  918. vxh->vx_flags = htonl(VXLAN_FLAGS);
  919. vxh->vx_vni = htonl(vni << 8);
  920. __skb_push(skb, sizeof(*uh));
  921. skb_reset_transport_header(skb);
  922. uh = udp_hdr(skb);
  923. uh->dest = dst_port;
  924. uh->source = src_port;
  925. uh->len = htons(skb->len);
  926. uh->check = 0;
  927. vxlan_set_owner(dev, skb);
  928. if (handle_offloads(skb))
  929. goto drop;
  930. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  931. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  932. err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
  933. IPPROTO_UDP, tos, ttl, df);
  934. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  935. return NETDEV_TX_OK;
  936. drop:
  937. dev->stats.tx_dropped++;
  938. goto tx_free;
  939. tx_error:
  940. dev->stats.tx_errors++;
  941. tx_free:
  942. dev_kfree_skb(skb);
  943. return NETDEV_TX_OK;
  944. }
  945. /* Transmit local packets over Vxlan
  946. *
  947. * Outer IP header inherits ECN and DF from inner header.
  948. * Outer UDP destination is the VXLAN assigned port.
  949. * source port is based on hash of flow
  950. */
  951. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  952. {
  953. struct vxlan_dev *vxlan = netdev_priv(dev);
  954. struct ethhdr *eth;
  955. bool did_rsc = false;
  956. struct vxlan_rdst *rdst0, *rdst;
  957. struct vxlan_fdb *f;
  958. int rc1, rc;
  959. skb_reset_mac_header(skb);
  960. eth = eth_hdr(skb);
  961. if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
  962. return arp_reduce(dev, skb);
  963. f = vxlan_find_mac(vxlan, eth->h_dest);
  964. did_rsc = false;
  965. if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
  966. ntohs(eth->h_proto) == ETH_P_IP) {
  967. did_rsc = route_shortcircuit(dev, skb);
  968. if (did_rsc)
  969. f = vxlan_find_mac(vxlan, eth->h_dest);
  970. }
  971. if (f == NULL) {
  972. rdst0 = &vxlan->default_dst;
  973. if (rdst0->remote_ip == htonl(INADDR_ANY) &&
  974. (vxlan->flags & VXLAN_F_L2MISS) &&
  975. !is_multicast_ether_addr(eth->h_dest))
  976. vxlan_fdb_miss(vxlan, eth->h_dest);
  977. } else
  978. rdst0 = &f->remote;
  979. rc = NETDEV_TX_OK;
  980. /* if there are multiple destinations, send copies */
  981. for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
  982. struct sk_buff *skb1;
  983. skb1 = skb_clone(skb, GFP_ATOMIC);
  984. if (skb1) {
  985. rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
  986. if (rc == NETDEV_TX_OK)
  987. rc = rc1;
  988. }
  989. }
  990. rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
  991. if (rc == NETDEV_TX_OK)
  992. rc = rc1;
  993. return rc;
  994. }
  995. /* Walk the forwarding table and purge stale entries */
  996. static void vxlan_cleanup(unsigned long arg)
  997. {
  998. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  999. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  1000. unsigned int h;
  1001. if (!netif_running(vxlan->dev))
  1002. return;
  1003. spin_lock_bh(&vxlan->hash_lock);
  1004. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1005. struct hlist_node *p, *n;
  1006. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1007. struct vxlan_fdb *f
  1008. = container_of(p, struct vxlan_fdb, hlist);
  1009. unsigned long timeout;
  1010. if (f->state & NUD_PERMANENT)
  1011. continue;
  1012. timeout = f->used + vxlan->age_interval * HZ;
  1013. if (time_before_eq(timeout, jiffies)) {
  1014. netdev_dbg(vxlan->dev,
  1015. "garbage collect %pM\n",
  1016. f->eth_addr);
  1017. f->state = NUD_STALE;
  1018. vxlan_fdb_destroy(vxlan, f);
  1019. } else if (time_before(timeout, next_timer))
  1020. next_timer = timeout;
  1021. }
  1022. }
  1023. spin_unlock_bh(&vxlan->hash_lock);
  1024. mod_timer(&vxlan->age_timer, next_timer);
  1025. }
  1026. /* Setup stats when device is created */
  1027. static int vxlan_init(struct net_device *dev)
  1028. {
  1029. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1030. if (!dev->tstats)
  1031. return -ENOMEM;
  1032. return 0;
  1033. }
  1034. /* Start ageing timer and join group when device is brought up */
  1035. static int vxlan_open(struct net_device *dev)
  1036. {
  1037. struct vxlan_dev *vxlan = netdev_priv(dev);
  1038. int err;
  1039. if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
  1040. err = vxlan_join_group(dev);
  1041. if (err)
  1042. return err;
  1043. }
  1044. if (vxlan->age_interval)
  1045. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  1046. return 0;
  1047. }
  1048. /* Purge the forwarding table */
  1049. static void vxlan_flush(struct vxlan_dev *vxlan)
  1050. {
  1051. unsigned int h;
  1052. spin_lock_bh(&vxlan->hash_lock);
  1053. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1054. struct hlist_node *p, *n;
  1055. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1056. struct vxlan_fdb *f
  1057. = container_of(p, struct vxlan_fdb, hlist);
  1058. vxlan_fdb_destroy(vxlan, f);
  1059. }
  1060. }
  1061. spin_unlock_bh(&vxlan->hash_lock);
  1062. }
  1063. /* Cleanup timer and forwarding table on shutdown */
  1064. static int vxlan_stop(struct net_device *dev)
  1065. {
  1066. struct vxlan_dev *vxlan = netdev_priv(dev);
  1067. if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
  1068. vxlan_leave_group(dev);
  1069. del_timer_sync(&vxlan->age_timer);
  1070. vxlan_flush(vxlan);
  1071. return 0;
  1072. }
  1073. /* Stub, nothing needs to be done. */
  1074. static void vxlan_set_multicast_list(struct net_device *dev)
  1075. {
  1076. }
  1077. static const struct net_device_ops vxlan_netdev_ops = {
  1078. .ndo_init = vxlan_init,
  1079. .ndo_open = vxlan_open,
  1080. .ndo_stop = vxlan_stop,
  1081. .ndo_start_xmit = vxlan_xmit,
  1082. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1083. .ndo_set_rx_mode = vxlan_set_multicast_list,
  1084. .ndo_change_mtu = eth_change_mtu,
  1085. .ndo_validate_addr = eth_validate_addr,
  1086. .ndo_set_mac_address = eth_mac_addr,
  1087. .ndo_fdb_add = vxlan_fdb_add,
  1088. .ndo_fdb_del = vxlan_fdb_delete,
  1089. .ndo_fdb_dump = vxlan_fdb_dump,
  1090. };
  1091. /* Info for udev, that this is a virtual tunnel endpoint */
  1092. static struct device_type vxlan_type = {
  1093. .name = "vxlan",
  1094. };
  1095. static void vxlan_free(struct net_device *dev)
  1096. {
  1097. free_percpu(dev->tstats);
  1098. free_netdev(dev);
  1099. }
  1100. /* Initialize the device structure. */
  1101. static void vxlan_setup(struct net_device *dev)
  1102. {
  1103. struct vxlan_dev *vxlan = netdev_priv(dev);
  1104. unsigned int h;
  1105. int low, high;
  1106. eth_hw_addr_random(dev);
  1107. ether_setup(dev);
  1108. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  1109. dev->netdev_ops = &vxlan_netdev_ops;
  1110. dev->destructor = vxlan_free;
  1111. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  1112. dev->tx_queue_len = 0;
  1113. dev->features |= NETIF_F_LLTX;
  1114. dev->features |= NETIF_F_NETNS_LOCAL;
  1115. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1116. dev->features |= NETIF_F_RXCSUM;
  1117. dev->features |= NETIF_F_GSO_SOFTWARE;
  1118. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1119. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1120. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1121. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1122. INIT_LIST_HEAD(&vxlan->next);
  1123. spin_lock_init(&vxlan->hash_lock);
  1124. init_timer_deferrable(&vxlan->age_timer);
  1125. vxlan->age_timer.function = vxlan_cleanup;
  1126. vxlan->age_timer.data = (unsigned long) vxlan;
  1127. inet_get_local_port_range(&low, &high);
  1128. vxlan->port_min = low;
  1129. vxlan->port_max = high;
  1130. vxlan->dst_port = htons(vxlan_port);
  1131. vxlan->dev = dev;
  1132. for (h = 0; h < FDB_HASH_SIZE; ++h)
  1133. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  1134. }
  1135. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  1136. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1137. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1138. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1139. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1140. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1141. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1142. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1143. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1144. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1145. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1146. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1147. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1148. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1149. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1150. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  1151. };
  1152. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1153. {
  1154. if (tb[IFLA_ADDRESS]) {
  1155. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1156. pr_debug("invalid link address (not ethernet)\n");
  1157. return -EINVAL;
  1158. }
  1159. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1160. pr_debug("invalid all zero ethernet address\n");
  1161. return -EADDRNOTAVAIL;
  1162. }
  1163. }
  1164. if (!data)
  1165. return -EINVAL;
  1166. if (data[IFLA_VXLAN_ID]) {
  1167. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1168. if (id >= VXLAN_VID_MASK)
  1169. return -ERANGE;
  1170. }
  1171. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1172. const struct ifla_vxlan_port_range *p
  1173. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1174. if (ntohs(p->high) < ntohs(p->low)) {
  1175. pr_debug("port range %u .. %u not valid\n",
  1176. ntohs(p->low), ntohs(p->high));
  1177. return -EINVAL;
  1178. }
  1179. }
  1180. return 0;
  1181. }
  1182. static void vxlan_get_drvinfo(struct net_device *netdev,
  1183. struct ethtool_drvinfo *drvinfo)
  1184. {
  1185. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  1186. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  1187. }
  1188. static const struct ethtool_ops vxlan_ethtool_ops = {
  1189. .get_drvinfo = vxlan_get_drvinfo,
  1190. .get_link = ethtool_op_get_link,
  1191. };
  1192. static void vxlan_del_work(struct work_struct *work)
  1193. {
  1194. struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
  1195. sk_release_kernel(vs->sock->sk);
  1196. kfree_rcu(vs, rcu);
  1197. }
  1198. /* Create new listen socket if needed */
  1199. static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
  1200. {
  1201. struct vxlan_sock *vs;
  1202. struct sock *sk;
  1203. struct sockaddr_in vxlan_addr = {
  1204. .sin_family = AF_INET,
  1205. .sin_addr.s_addr = htonl(INADDR_ANY),
  1206. };
  1207. int rc;
  1208. unsigned int h;
  1209. vs = kmalloc(sizeof(*vs), GFP_KERNEL);
  1210. if (!vs)
  1211. return ERR_PTR(-ENOMEM);
  1212. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1213. INIT_HLIST_HEAD(&vs->vni_list[h]);
  1214. INIT_WORK(&vs->del_work, vxlan_del_work);
  1215. /* Create UDP socket for encapsulation receive. */
  1216. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
  1217. if (rc < 0) {
  1218. pr_debug("UDP socket create failed\n");
  1219. kfree(vs);
  1220. return ERR_PTR(rc);
  1221. }
  1222. /* Put in proper namespace */
  1223. sk = vs->sock->sk;
  1224. sk_change_net(sk, net);
  1225. vxlan_addr.sin_port = port;
  1226. rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
  1227. sizeof(vxlan_addr));
  1228. if (rc < 0) {
  1229. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1230. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1231. sk_release_kernel(sk);
  1232. kfree(vs);
  1233. return ERR_PTR(rc);
  1234. }
  1235. /* Disable multicast loopback */
  1236. inet_sk(sk)->mc_loop = 0;
  1237. /* Mark socket as an encapsulation socket. */
  1238. udp_sk(sk)->encap_type = 1;
  1239. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1240. udp_encap_enable();
  1241. vs->refcnt = 1;
  1242. return vs;
  1243. }
  1244. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1245. struct nlattr *tb[], struct nlattr *data[])
  1246. {
  1247. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1248. struct vxlan_dev *vxlan = netdev_priv(dev);
  1249. struct vxlan_rdst *dst = &vxlan->default_dst;
  1250. struct vxlan_sock *vs;
  1251. __u32 vni;
  1252. int err;
  1253. if (!data[IFLA_VXLAN_ID])
  1254. return -EINVAL;
  1255. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1256. dst->remote_vni = vni;
  1257. if (data[IFLA_VXLAN_GROUP])
  1258. dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1259. if (data[IFLA_VXLAN_LOCAL])
  1260. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  1261. if (data[IFLA_VXLAN_LINK] &&
  1262. (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  1263. struct net_device *lowerdev
  1264. = __dev_get_by_index(net, dst->remote_ifindex);
  1265. if (!lowerdev) {
  1266. pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
  1267. return -ENODEV;
  1268. }
  1269. if (!tb[IFLA_MTU])
  1270. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1271. /* update header length based on lower device */
  1272. dev->hard_header_len = lowerdev->hard_header_len +
  1273. VXLAN_HEADROOM;
  1274. }
  1275. if (data[IFLA_VXLAN_TOS])
  1276. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  1277. if (data[IFLA_VXLAN_TTL])
  1278. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  1279. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  1280. vxlan->flags |= VXLAN_F_LEARN;
  1281. if (data[IFLA_VXLAN_AGEING])
  1282. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  1283. else
  1284. vxlan->age_interval = FDB_AGE_DEFAULT;
  1285. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  1286. vxlan->flags |= VXLAN_F_PROXY;
  1287. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  1288. vxlan->flags |= VXLAN_F_RSC;
  1289. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  1290. vxlan->flags |= VXLAN_F_L2MISS;
  1291. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  1292. vxlan->flags |= VXLAN_F_L3MISS;
  1293. if (data[IFLA_VXLAN_LIMIT])
  1294. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  1295. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1296. const struct ifla_vxlan_port_range *p
  1297. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1298. vxlan->port_min = ntohs(p->low);
  1299. vxlan->port_max = ntohs(p->high);
  1300. }
  1301. if (data[IFLA_VXLAN_PORT])
  1302. vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  1303. if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
  1304. pr_info("duplicate VNI %u\n", vni);
  1305. return -EEXIST;
  1306. }
  1307. vs = vxlan_find_port(net, vxlan->dst_port);
  1308. if (vs)
  1309. ++vs->refcnt;
  1310. else {
  1311. /* Drop lock because socket create acquires RTNL lock */
  1312. rtnl_unlock();
  1313. vs = vxlan_socket_create(net, vxlan->dst_port);
  1314. rtnl_lock();
  1315. if (IS_ERR(vs))
  1316. return PTR_ERR(vs);
  1317. hlist_add_head_rcu(&vs->hlist, vs_head(net, vxlan->dst_port));
  1318. }
  1319. vxlan->vn_sock = vs;
  1320. SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
  1321. err = register_netdevice(dev);
  1322. if (err) {
  1323. if (--vs->refcnt == 0) {
  1324. rtnl_unlock();
  1325. sk_release_kernel(vs->sock->sk);
  1326. kfree(vs);
  1327. rtnl_lock();
  1328. }
  1329. return err;
  1330. }
  1331. list_add(&vxlan->next, &vn->vxlan_list);
  1332. hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
  1333. return 0;
  1334. }
  1335. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  1336. {
  1337. struct vxlan_dev *vxlan = netdev_priv(dev);
  1338. struct vxlan_sock *vs = vxlan->vn_sock;
  1339. hlist_del_rcu(&vxlan->hlist);
  1340. list_del(&vxlan->next);
  1341. unregister_netdevice_queue(dev, head);
  1342. if (--vs->refcnt == 0) {
  1343. hlist_del_rcu(&vs->hlist);
  1344. queue_work(vxlan_wq, &vs->del_work);
  1345. }
  1346. }
  1347. static size_t vxlan_get_size(const struct net_device *dev)
  1348. {
  1349. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  1350. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  1351. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  1352. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  1353. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  1354. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  1355. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  1356. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  1357. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  1358. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  1359. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  1360. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  1361. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  1362. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  1363. nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
  1364. 0;
  1365. }
  1366. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1367. {
  1368. const struct vxlan_dev *vxlan = netdev_priv(dev);
  1369. const struct vxlan_rdst *dst = &vxlan->default_dst;
  1370. struct ifla_vxlan_port_range ports = {
  1371. .low = htons(vxlan->port_min),
  1372. .high = htons(vxlan->port_max),
  1373. };
  1374. if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
  1375. goto nla_put_failure;
  1376. if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
  1377. goto nla_put_failure;
  1378. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  1379. goto nla_put_failure;
  1380. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  1381. goto nla_put_failure;
  1382. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  1383. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  1384. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  1385. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  1386. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  1387. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  1388. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  1389. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  1390. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  1391. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  1392. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  1393. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  1394. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
  1395. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
  1396. goto nla_put_failure;
  1397. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  1398. goto nla_put_failure;
  1399. return 0;
  1400. nla_put_failure:
  1401. return -EMSGSIZE;
  1402. }
  1403. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  1404. .kind = "vxlan",
  1405. .maxtype = IFLA_VXLAN_MAX,
  1406. .policy = vxlan_policy,
  1407. .priv_size = sizeof(struct vxlan_dev),
  1408. .setup = vxlan_setup,
  1409. .validate = vxlan_validate,
  1410. .newlink = vxlan_newlink,
  1411. .dellink = vxlan_dellink,
  1412. .get_size = vxlan_get_size,
  1413. .fill_info = vxlan_fill_info,
  1414. };
  1415. static __net_init int vxlan_init_net(struct net *net)
  1416. {
  1417. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1418. unsigned int h;
  1419. INIT_LIST_HEAD(&vn->vxlan_list);
  1420. for (h = 0; h < PORT_HASH_SIZE; ++h)
  1421. INIT_HLIST_HEAD(&vn->sock_list[h]);
  1422. return 0;
  1423. }
  1424. static __net_exit void vxlan_exit_net(struct net *net)
  1425. {
  1426. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1427. struct vxlan_dev *vxlan;
  1428. rtnl_lock();
  1429. list_for_each_entry(vxlan, &vn->vxlan_list, next)
  1430. dev_close(vxlan->dev);
  1431. rtnl_unlock();
  1432. }
  1433. static struct pernet_operations vxlan_net_ops = {
  1434. .init = vxlan_init_net,
  1435. .exit = vxlan_exit_net,
  1436. .id = &vxlan_net_id,
  1437. .size = sizeof(struct vxlan_net),
  1438. };
  1439. static int __init vxlan_init_module(void)
  1440. {
  1441. int rc;
  1442. vxlan_wq = alloc_workqueue("vxlan", 0, 0);
  1443. if (!vxlan_wq)
  1444. return -ENOMEM;
  1445. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  1446. rc = register_pernet_device(&vxlan_net_ops);
  1447. if (rc)
  1448. goto out1;
  1449. rc = rtnl_link_register(&vxlan_link_ops);
  1450. if (rc)
  1451. goto out2;
  1452. return 0;
  1453. out2:
  1454. unregister_pernet_device(&vxlan_net_ops);
  1455. out1:
  1456. destroy_workqueue(vxlan_wq);
  1457. return rc;
  1458. }
  1459. late_initcall(vxlan_init_module);
  1460. static void __exit vxlan_cleanup_module(void)
  1461. {
  1462. unregister_pernet_device(&vxlan_net_ops);
  1463. rtnl_link_unregister(&vxlan_link_ops);
  1464. destroy_workqueue(vxlan_wq);
  1465. rcu_barrier();
  1466. }
  1467. module_exit(vxlan_cleanup_module);
  1468. MODULE_LICENSE("GPL");
  1469. MODULE_VERSION(VXLAN_VERSION);
  1470. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  1471. MODULE_ALIAS_RTNL_LINK("vxlan");