vxlan.c 44 KB

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