vxlan.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  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. * Return true if packet is bogus and should be droppped.
  509. */
  510. static bool vxlan_snoop(struct net_device *dev,
  511. __be32 src_ip, const u8 *src_mac)
  512. {
  513. struct vxlan_dev *vxlan = netdev_priv(dev);
  514. struct vxlan_fdb *f;
  515. f = vxlan_find_mac(vxlan, src_mac);
  516. if (likely(f)) {
  517. if (likely(f->remote.remote_ip == src_ip))
  518. return false;
  519. /* Don't migrate static entries, drop packets */
  520. if (f->state & NUD_NOARP)
  521. return true;
  522. if (net_ratelimit())
  523. netdev_info(dev,
  524. "%pM migrated from %pI4 to %pI4\n",
  525. src_mac, &f->remote.remote_ip, &src_ip);
  526. f->remote.remote_ip = src_ip;
  527. f->updated = jiffies;
  528. } else {
  529. /* learned new entry */
  530. spin_lock(&vxlan->hash_lock);
  531. /* close off race between vxlan_flush and incoming packets */
  532. if (netif_running(dev))
  533. vxlan_fdb_create(vxlan, src_mac, src_ip,
  534. NUD_REACHABLE,
  535. NLM_F_EXCL|NLM_F_CREATE,
  536. vxlan->dst_port,
  537. vxlan->default_dst.remote_vni,
  538. 0, NTF_SELF);
  539. spin_unlock(&vxlan->hash_lock);
  540. }
  541. return false;
  542. }
  543. /* See if multicast group is already in use by other ID */
  544. static bool vxlan_group_used(struct vxlan_net *vn,
  545. const struct vxlan_dev *this)
  546. {
  547. struct vxlan_dev *vxlan;
  548. list_for_each_entry(vxlan, &vn->vxlan_list, next) {
  549. if (vxlan == this)
  550. continue;
  551. if (!netif_running(vxlan->dev))
  552. continue;
  553. if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
  554. return true;
  555. }
  556. return false;
  557. }
  558. /* kernel equivalent to IP_ADD_MEMBERSHIP */
  559. static int vxlan_join_group(struct net_device *dev)
  560. {
  561. struct vxlan_dev *vxlan = netdev_priv(dev);
  562. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  563. struct sock *sk = vxlan->vn_sock->sock->sk;
  564. struct ip_mreqn mreq = {
  565. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  566. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  567. };
  568. int err;
  569. /* Already a member of group */
  570. if (vxlan_group_used(vn, vxlan))
  571. return 0;
  572. /* Need to drop RTNL to call multicast join */
  573. rtnl_unlock();
  574. lock_sock(sk);
  575. err = ip_mc_join_group(sk, &mreq);
  576. release_sock(sk);
  577. rtnl_lock();
  578. return err;
  579. }
  580. /* kernel equivalent to IP_DROP_MEMBERSHIP */
  581. static int vxlan_leave_group(struct net_device *dev)
  582. {
  583. struct vxlan_dev *vxlan = netdev_priv(dev);
  584. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  585. int err = 0;
  586. struct sock *sk = vxlan->vn_sock->sock->sk;
  587. struct ip_mreqn mreq = {
  588. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  589. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  590. };
  591. /* Only leave group when last vxlan is done. */
  592. if (vxlan_group_used(vn, vxlan))
  593. return 0;
  594. /* Need to drop RTNL to call multicast leave */
  595. rtnl_unlock();
  596. lock_sock(sk);
  597. err = ip_mc_leave_group(sk, &mreq);
  598. release_sock(sk);
  599. rtnl_lock();
  600. return err;
  601. }
  602. /* Callback from net/ipv4/udp.c to receive packets */
  603. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  604. {
  605. struct iphdr *oip;
  606. struct vxlanhdr *vxh;
  607. struct vxlan_dev *vxlan;
  608. struct pcpu_tstats *stats;
  609. __be16 port;
  610. __u32 vni;
  611. int err;
  612. /* pop off outer UDP header */
  613. __skb_pull(skb, sizeof(struct udphdr));
  614. /* Need Vxlan and inner Ethernet header to be present */
  615. if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
  616. goto error;
  617. /* Drop packets with reserved bits set */
  618. vxh = (struct vxlanhdr *) skb->data;
  619. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  620. (vxh->vx_vni & htonl(0xff))) {
  621. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  622. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  623. goto error;
  624. }
  625. __skb_pull(skb, sizeof(struct vxlanhdr));
  626. /* Is this VNI defined? */
  627. vni = ntohl(vxh->vx_vni) >> 8;
  628. port = inet_sk(sk)->inet_sport;
  629. vxlan = vxlan_find_vni(sock_net(sk), vni, port);
  630. if (!vxlan) {
  631. netdev_dbg(skb->dev, "unknown vni %d port %u\n",
  632. vni, ntohs(port));
  633. goto drop;
  634. }
  635. if (!pskb_may_pull(skb, ETH_HLEN)) {
  636. vxlan->dev->stats.rx_length_errors++;
  637. vxlan->dev->stats.rx_errors++;
  638. goto drop;
  639. }
  640. skb_reset_mac_header(skb);
  641. /* Re-examine inner Ethernet packet */
  642. oip = ip_hdr(skb);
  643. skb->protocol = eth_type_trans(skb, vxlan->dev);
  644. /* Ignore packet loops (and multicast echo) */
  645. if (compare_ether_addr(eth_hdr(skb)->h_source,
  646. vxlan->dev->dev_addr) == 0)
  647. goto drop;
  648. if ((vxlan->flags & VXLAN_F_LEARN) &&
  649. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
  650. goto drop;
  651. __skb_tunnel_rx(skb, vxlan->dev);
  652. skb_reset_network_header(skb);
  653. /* If the NIC driver gave us an encapsulated packet with
  654. * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
  655. * leave the CHECKSUM_UNNECESSARY, the device checksummed it
  656. * for us. Otherwise force the upper layers to verify it.
  657. */
  658. if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
  659. !(vxlan->dev->features & NETIF_F_RXCSUM))
  660. skb->ip_summed = CHECKSUM_NONE;
  661. skb->encapsulation = 0;
  662. err = IP_ECN_decapsulate(oip, skb);
  663. if (unlikely(err)) {
  664. if (log_ecn_error)
  665. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  666. &oip->saddr, oip->tos);
  667. if (err > 1) {
  668. ++vxlan->dev->stats.rx_frame_errors;
  669. ++vxlan->dev->stats.rx_errors;
  670. goto drop;
  671. }
  672. }
  673. stats = this_cpu_ptr(vxlan->dev->tstats);
  674. u64_stats_update_begin(&stats->syncp);
  675. stats->rx_packets++;
  676. stats->rx_bytes += skb->len;
  677. u64_stats_update_end(&stats->syncp);
  678. netif_rx(skb);
  679. return 0;
  680. error:
  681. /* Put UDP header back */
  682. __skb_push(skb, sizeof(struct udphdr));
  683. return 1;
  684. drop:
  685. /* Consume bad packet */
  686. kfree_skb(skb);
  687. return 0;
  688. }
  689. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  690. {
  691. struct vxlan_dev *vxlan = netdev_priv(dev);
  692. struct arphdr *parp;
  693. u8 *arpptr, *sha;
  694. __be32 sip, tip;
  695. struct neighbour *n;
  696. if (dev->flags & IFF_NOARP)
  697. goto out;
  698. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  699. dev->stats.tx_dropped++;
  700. goto out;
  701. }
  702. parp = arp_hdr(skb);
  703. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  704. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  705. parp->ar_pro != htons(ETH_P_IP) ||
  706. parp->ar_op != htons(ARPOP_REQUEST) ||
  707. parp->ar_hln != dev->addr_len ||
  708. parp->ar_pln != 4)
  709. goto out;
  710. arpptr = (u8 *)parp + sizeof(struct arphdr);
  711. sha = arpptr;
  712. arpptr += dev->addr_len; /* sha */
  713. memcpy(&sip, arpptr, sizeof(sip));
  714. arpptr += sizeof(sip);
  715. arpptr += dev->addr_len; /* tha */
  716. memcpy(&tip, arpptr, sizeof(tip));
  717. if (ipv4_is_loopback(tip) ||
  718. ipv4_is_multicast(tip))
  719. goto out;
  720. n = neigh_lookup(&arp_tbl, &tip, dev);
  721. if (n) {
  722. struct vxlan_fdb *f;
  723. struct sk_buff *reply;
  724. if (!(n->nud_state & NUD_CONNECTED)) {
  725. neigh_release(n);
  726. goto out;
  727. }
  728. f = vxlan_find_mac(vxlan, n->ha);
  729. if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
  730. /* bridge-local neighbor */
  731. neigh_release(n);
  732. goto out;
  733. }
  734. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  735. n->ha, sha);
  736. neigh_release(n);
  737. skb_reset_mac_header(reply);
  738. __skb_pull(reply, skb_network_offset(reply));
  739. reply->ip_summed = CHECKSUM_UNNECESSARY;
  740. reply->pkt_type = PACKET_HOST;
  741. if (netif_rx_ni(reply) == NET_RX_DROP)
  742. dev->stats.rx_dropped++;
  743. } else if (vxlan->flags & VXLAN_F_L3MISS)
  744. vxlan_ip_miss(dev, tip);
  745. out:
  746. consume_skb(skb);
  747. return NETDEV_TX_OK;
  748. }
  749. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  750. {
  751. struct vxlan_dev *vxlan = netdev_priv(dev);
  752. struct neighbour *n;
  753. struct iphdr *pip;
  754. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  755. return false;
  756. n = NULL;
  757. switch (ntohs(eth_hdr(skb)->h_proto)) {
  758. case ETH_P_IP:
  759. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  760. return false;
  761. pip = ip_hdr(skb);
  762. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  763. break;
  764. default:
  765. return false;
  766. }
  767. if (n) {
  768. bool diff;
  769. diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
  770. if (diff) {
  771. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  772. dev->addr_len);
  773. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  774. }
  775. neigh_release(n);
  776. return diff;
  777. } else if (vxlan->flags & VXLAN_F_L3MISS)
  778. vxlan_ip_miss(dev, pip->daddr);
  779. return false;
  780. }
  781. static void vxlan_sock_put(struct sk_buff *skb)
  782. {
  783. sock_put(skb->sk);
  784. }
  785. /* On transmit, associate with the tunnel socket */
  786. static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
  787. {
  788. struct vxlan_dev *vxlan = netdev_priv(dev);
  789. struct sock *sk = vxlan->vn_sock->sock->sk;
  790. skb_orphan(skb);
  791. sock_hold(sk);
  792. skb->sk = sk;
  793. skb->destructor = vxlan_sock_put;
  794. }
  795. /* Compute source port for outgoing packet
  796. * first choice to use L4 flow hash since it will spread
  797. * better and maybe available from hardware
  798. * secondary choice is to use jhash on the Ethernet header
  799. */
  800. static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
  801. {
  802. unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
  803. u32 hash;
  804. hash = skb_get_rxhash(skb);
  805. if (!hash)
  806. hash = jhash(skb->data, 2 * ETH_ALEN,
  807. (__force u32) skb->protocol);
  808. return htons((((u64) hash * range) >> 32) + vxlan->port_min);
  809. }
  810. static int handle_offloads(struct sk_buff *skb)
  811. {
  812. if (skb_is_gso(skb)) {
  813. int err = skb_unclone(skb, GFP_ATOMIC);
  814. if (unlikely(err))
  815. return err;
  816. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  817. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  818. skb->ip_summed = CHECKSUM_NONE;
  819. return 0;
  820. }
  821. /* Bypass encapsulation if the destination is local */
  822. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  823. struct vxlan_dev *dst_vxlan)
  824. {
  825. struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  826. struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  827. skb->pkt_type = PACKET_HOST;
  828. skb->encapsulation = 0;
  829. skb->dev = dst_vxlan->dev;
  830. __skb_pull(skb, skb_network_offset(skb));
  831. if (dst_vxlan->flags & VXLAN_F_LEARN)
  832. vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
  833. eth_hdr(skb)->h_source);
  834. u64_stats_update_begin(&tx_stats->syncp);
  835. tx_stats->tx_packets++;
  836. tx_stats->tx_bytes += skb->len;
  837. u64_stats_update_end(&tx_stats->syncp);
  838. if (netif_rx(skb) == NET_RX_SUCCESS) {
  839. u64_stats_update_begin(&rx_stats->syncp);
  840. rx_stats->rx_packets++;
  841. rx_stats->rx_bytes += skb->len;
  842. u64_stats_update_end(&rx_stats->syncp);
  843. } else {
  844. skb->dev->stats.rx_dropped++;
  845. }
  846. }
  847. static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  848. struct vxlan_rdst *rdst, bool did_rsc)
  849. {
  850. struct vxlan_dev *vxlan = netdev_priv(dev);
  851. struct rtable *rt;
  852. const struct iphdr *old_iph;
  853. struct vxlanhdr *vxh;
  854. struct udphdr *uh;
  855. struct flowi4 fl4;
  856. __be32 dst;
  857. __be16 src_port, dst_port;
  858. u32 vni;
  859. __be16 df = 0;
  860. __u8 tos, ttl;
  861. int err;
  862. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
  863. vni = rdst->remote_vni;
  864. dst = rdst->remote_ip;
  865. if (!dst) {
  866. if (did_rsc) {
  867. /* short-circuited back to local bridge */
  868. vxlan_encap_bypass(skb, vxlan, vxlan);
  869. return NETDEV_TX_OK;
  870. }
  871. goto drop;
  872. }
  873. if (!skb->encapsulation) {
  874. skb_reset_inner_headers(skb);
  875. skb->encapsulation = 1;
  876. }
  877. /* Need space for new headers (invalidates iph ptr) */
  878. if (skb_cow_head(skb, VXLAN_HEADROOM))
  879. goto drop;
  880. old_iph = ip_hdr(skb);
  881. ttl = vxlan->ttl;
  882. if (!ttl && IN_MULTICAST(ntohl(dst)))
  883. ttl = 1;
  884. tos = vxlan->tos;
  885. if (tos == 1)
  886. tos = ip_tunnel_get_dsfield(old_iph, skb);
  887. src_port = vxlan_src_port(vxlan, skb);
  888. memset(&fl4, 0, sizeof(fl4));
  889. fl4.flowi4_oif = rdst->remote_ifindex;
  890. fl4.flowi4_tos = RT_TOS(tos);
  891. fl4.daddr = dst;
  892. fl4.saddr = vxlan->saddr;
  893. rt = ip_route_output_key(dev_net(dev), &fl4);
  894. if (IS_ERR(rt)) {
  895. netdev_dbg(dev, "no route to %pI4\n", &dst);
  896. dev->stats.tx_carrier_errors++;
  897. goto tx_error;
  898. }
  899. if (rt->dst.dev == dev) {
  900. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  901. ip_rt_put(rt);
  902. dev->stats.collisions++;
  903. goto tx_error;
  904. }
  905. /* Bypass encapsulation if the destination is local */
  906. if (rt->rt_flags & RTCF_LOCAL &&
  907. !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  908. struct vxlan_dev *dst_vxlan;
  909. ip_rt_put(rt);
  910. dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
  911. if (!dst_vxlan)
  912. goto tx_error;
  913. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  914. return NETDEV_TX_OK;
  915. }
  916. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  917. vxh->vx_flags = htonl(VXLAN_FLAGS);
  918. vxh->vx_vni = htonl(vni << 8);
  919. __skb_push(skb, sizeof(*uh));
  920. skb_reset_transport_header(skb);
  921. uh = udp_hdr(skb);
  922. uh->dest = dst_port;
  923. uh->source = src_port;
  924. uh->len = htons(skb->len);
  925. uh->check = 0;
  926. vxlan_set_owner(dev, skb);
  927. if (handle_offloads(skb))
  928. goto drop;
  929. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  930. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  931. err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
  932. IPPROTO_UDP, tos, ttl, df);
  933. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  934. return NETDEV_TX_OK;
  935. drop:
  936. dev->stats.tx_dropped++;
  937. goto tx_free;
  938. tx_error:
  939. dev->stats.tx_errors++;
  940. tx_free:
  941. dev_kfree_skb(skb);
  942. return NETDEV_TX_OK;
  943. }
  944. /* Transmit local packets over Vxlan
  945. *
  946. * Outer IP header inherits ECN and DF from inner header.
  947. * Outer UDP destination is the VXLAN assigned port.
  948. * source port is based on hash of flow
  949. */
  950. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  951. {
  952. struct vxlan_dev *vxlan = netdev_priv(dev);
  953. struct ethhdr *eth;
  954. bool did_rsc = false;
  955. struct vxlan_rdst *rdst0, *rdst;
  956. struct vxlan_fdb *f;
  957. int rc1, rc;
  958. skb_reset_mac_header(skb);
  959. eth = eth_hdr(skb);
  960. if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
  961. return arp_reduce(dev, skb);
  962. f = vxlan_find_mac(vxlan, eth->h_dest);
  963. did_rsc = false;
  964. if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
  965. ntohs(eth->h_proto) == ETH_P_IP) {
  966. did_rsc = route_shortcircuit(dev, skb);
  967. if (did_rsc)
  968. f = vxlan_find_mac(vxlan, eth->h_dest);
  969. }
  970. if (f == NULL) {
  971. rdst0 = &vxlan->default_dst;
  972. if (rdst0->remote_ip == htonl(INADDR_ANY) &&
  973. (vxlan->flags & VXLAN_F_L2MISS) &&
  974. !is_multicast_ether_addr(eth->h_dest))
  975. vxlan_fdb_miss(vxlan, eth->h_dest);
  976. } else
  977. rdst0 = &f->remote;
  978. rc = NETDEV_TX_OK;
  979. /* if there are multiple destinations, send copies */
  980. for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
  981. struct sk_buff *skb1;
  982. skb1 = skb_clone(skb, GFP_ATOMIC);
  983. if (skb1) {
  984. rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
  985. if (rc == NETDEV_TX_OK)
  986. rc = rc1;
  987. }
  988. }
  989. rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
  990. if (rc == NETDEV_TX_OK)
  991. rc = rc1;
  992. return rc;
  993. }
  994. /* Walk the forwarding table and purge stale entries */
  995. static void vxlan_cleanup(unsigned long arg)
  996. {
  997. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  998. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  999. unsigned int h;
  1000. if (!netif_running(vxlan->dev))
  1001. return;
  1002. spin_lock_bh(&vxlan->hash_lock);
  1003. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1004. struct hlist_node *p, *n;
  1005. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1006. struct vxlan_fdb *f
  1007. = container_of(p, struct vxlan_fdb, hlist);
  1008. unsigned long timeout;
  1009. if (f->state & NUD_PERMANENT)
  1010. continue;
  1011. timeout = f->used + vxlan->age_interval * HZ;
  1012. if (time_before_eq(timeout, jiffies)) {
  1013. netdev_dbg(vxlan->dev,
  1014. "garbage collect %pM\n",
  1015. f->eth_addr);
  1016. f->state = NUD_STALE;
  1017. vxlan_fdb_destroy(vxlan, f);
  1018. } else if (time_before(timeout, next_timer))
  1019. next_timer = timeout;
  1020. }
  1021. }
  1022. spin_unlock_bh(&vxlan->hash_lock);
  1023. mod_timer(&vxlan->age_timer, next_timer);
  1024. }
  1025. /* Setup stats when device is created */
  1026. static int vxlan_init(struct net_device *dev)
  1027. {
  1028. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1029. if (!dev->tstats)
  1030. return -ENOMEM;
  1031. return 0;
  1032. }
  1033. /* Start ageing timer and join group when device is brought up */
  1034. static int vxlan_open(struct net_device *dev)
  1035. {
  1036. struct vxlan_dev *vxlan = netdev_priv(dev);
  1037. int err;
  1038. if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
  1039. err = vxlan_join_group(dev);
  1040. if (err)
  1041. return err;
  1042. }
  1043. if (vxlan->age_interval)
  1044. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  1045. return 0;
  1046. }
  1047. /* Purge the forwarding table */
  1048. static void vxlan_flush(struct vxlan_dev *vxlan)
  1049. {
  1050. unsigned int h;
  1051. spin_lock_bh(&vxlan->hash_lock);
  1052. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1053. struct hlist_node *p, *n;
  1054. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1055. struct vxlan_fdb *f
  1056. = container_of(p, struct vxlan_fdb, hlist);
  1057. vxlan_fdb_destroy(vxlan, f);
  1058. }
  1059. }
  1060. spin_unlock_bh(&vxlan->hash_lock);
  1061. }
  1062. /* Cleanup timer and forwarding table on shutdown */
  1063. static int vxlan_stop(struct net_device *dev)
  1064. {
  1065. struct vxlan_dev *vxlan = netdev_priv(dev);
  1066. if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
  1067. vxlan_leave_group(dev);
  1068. del_timer_sync(&vxlan->age_timer);
  1069. vxlan_flush(vxlan);
  1070. return 0;
  1071. }
  1072. /* Stub, nothing needs to be done. */
  1073. static void vxlan_set_multicast_list(struct net_device *dev)
  1074. {
  1075. }
  1076. static const struct net_device_ops vxlan_netdev_ops = {
  1077. .ndo_init = vxlan_init,
  1078. .ndo_open = vxlan_open,
  1079. .ndo_stop = vxlan_stop,
  1080. .ndo_start_xmit = vxlan_xmit,
  1081. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1082. .ndo_set_rx_mode = vxlan_set_multicast_list,
  1083. .ndo_change_mtu = eth_change_mtu,
  1084. .ndo_validate_addr = eth_validate_addr,
  1085. .ndo_set_mac_address = eth_mac_addr,
  1086. .ndo_fdb_add = vxlan_fdb_add,
  1087. .ndo_fdb_del = vxlan_fdb_delete,
  1088. .ndo_fdb_dump = vxlan_fdb_dump,
  1089. };
  1090. /* Info for udev, that this is a virtual tunnel endpoint */
  1091. static struct device_type vxlan_type = {
  1092. .name = "vxlan",
  1093. };
  1094. static void vxlan_free(struct net_device *dev)
  1095. {
  1096. free_percpu(dev->tstats);
  1097. free_netdev(dev);
  1098. }
  1099. /* Initialize the device structure. */
  1100. static void vxlan_setup(struct net_device *dev)
  1101. {
  1102. struct vxlan_dev *vxlan = netdev_priv(dev);
  1103. unsigned int h;
  1104. int low, high;
  1105. eth_hw_addr_random(dev);
  1106. ether_setup(dev);
  1107. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  1108. dev->netdev_ops = &vxlan_netdev_ops;
  1109. dev->destructor = vxlan_free;
  1110. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  1111. dev->tx_queue_len = 0;
  1112. dev->features |= NETIF_F_LLTX;
  1113. dev->features |= NETIF_F_NETNS_LOCAL;
  1114. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1115. dev->features |= NETIF_F_RXCSUM;
  1116. dev->features |= NETIF_F_GSO_SOFTWARE;
  1117. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1118. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1119. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1120. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1121. INIT_LIST_HEAD(&vxlan->next);
  1122. spin_lock_init(&vxlan->hash_lock);
  1123. init_timer_deferrable(&vxlan->age_timer);
  1124. vxlan->age_timer.function = vxlan_cleanup;
  1125. vxlan->age_timer.data = (unsigned long) vxlan;
  1126. inet_get_local_port_range(&low, &high);
  1127. vxlan->port_min = low;
  1128. vxlan->port_max = high;
  1129. vxlan->dst_port = htons(vxlan_port);
  1130. vxlan->dev = dev;
  1131. for (h = 0; h < FDB_HASH_SIZE; ++h)
  1132. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  1133. }
  1134. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  1135. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1136. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1137. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1138. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1139. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1140. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1141. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1142. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1143. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1144. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1145. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1146. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1147. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1148. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1149. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  1150. };
  1151. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1152. {
  1153. if (tb[IFLA_ADDRESS]) {
  1154. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1155. pr_debug("invalid link address (not ethernet)\n");
  1156. return -EINVAL;
  1157. }
  1158. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1159. pr_debug("invalid all zero ethernet address\n");
  1160. return -EADDRNOTAVAIL;
  1161. }
  1162. }
  1163. if (!data)
  1164. return -EINVAL;
  1165. if (data[IFLA_VXLAN_ID]) {
  1166. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1167. if (id >= VXLAN_VID_MASK)
  1168. return -ERANGE;
  1169. }
  1170. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1171. const struct ifla_vxlan_port_range *p
  1172. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1173. if (ntohs(p->high) < ntohs(p->low)) {
  1174. pr_debug("port range %u .. %u not valid\n",
  1175. ntohs(p->low), ntohs(p->high));
  1176. return -EINVAL;
  1177. }
  1178. }
  1179. return 0;
  1180. }
  1181. static void vxlan_get_drvinfo(struct net_device *netdev,
  1182. struct ethtool_drvinfo *drvinfo)
  1183. {
  1184. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  1185. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  1186. }
  1187. static const struct ethtool_ops vxlan_ethtool_ops = {
  1188. .get_drvinfo = vxlan_get_drvinfo,
  1189. .get_link = ethtool_op_get_link,
  1190. };
  1191. static void vxlan_del_work(struct work_struct *work)
  1192. {
  1193. struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
  1194. sk_release_kernel(vs->sock->sk);
  1195. kfree_rcu(vs, rcu);
  1196. }
  1197. /* Create new listen socket if needed */
  1198. static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
  1199. {
  1200. struct vxlan_sock *vs;
  1201. struct sock *sk;
  1202. struct sockaddr_in vxlan_addr = {
  1203. .sin_family = AF_INET,
  1204. .sin_addr.s_addr = htonl(INADDR_ANY),
  1205. };
  1206. int rc;
  1207. unsigned int h;
  1208. vs = kmalloc(sizeof(*vs), GFP_KERNEL);
  1209. if (!vs)
  1210. return ERR_PTR(-ENOMEM);
  1211. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1212. INIT_HLIST_HEAD(&vs->vni_list[h]);
  1213. INIT_WORK(&vs->del_work, vxlan_del_work);
  1214. /* Create UDP socket for encapsulation receive. */
  1215. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
  1216. if (rc < 0) {
  1217. pr_debug("UDP socket create failed\n");
  1218. kfree(vs);
  1219. return ERR_PTR(rc);
  1220. }
  1221. /* Put in proper namespace */
  1222. sk = vs->sock->sk;
  1223. sk_change_net(sk, net);
  1224. vxlan_addr.sin_port = port;
  1225. rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
  1226. sizeof(vxlan_addr));
  1227. if (rc < 0) {
  1228. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1229. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1230. sk_release_kernel(sk);
  1231. kfree(vs);
  1232. return ERR_PTR(rc);
  1233. }
  1234. /* Disable multicast loopback */
  1235. inet_sk(sk)->mc_loop = 0;
  1236. /* Mark socket as an encapsulation socket. */
  1237. udp_sk(sk)->encap_type = 1;
  1238. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1239. udp_encap_enable();
  1240. vs->refcnt = 1;
  1241. return vs;
  1242. }
  1243. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1244. struct nlattr *tb[], struct nlattr *data[])
  1245. {
  1246. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1247. struct vxlan_dev *vxlan = netdev_priv(dev);
  1248. struct vxlan_rdst *dst = &vxlan->default_dst;
  1249. struct vxlan_sock *vs;
  1250. __u32 vni;
  1251. int err;
  1252. if (!data[IFLA_VXLAN_ID])
  1253. return -EINVAL;
  1254. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1255. dst->remote_vni = vni;
  1256. if (data[IFLA_VXLAN_GROUP])
  1257. dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1258. if (data[IFLA_VXLAN_LOCAL])
  1259. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  1260. if (data[IFLA_VXLAN_LINK] &&
  1261. (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  1262. struct net_device *lowerdev
  1263. = __dev_get_by_index(net, dst->remote_ifindex);
  1264. if (!lowerdev) {
  1265. pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
  1266. return -ENODEV;
  1267. }
  1268. if (!tb[IFLA_MTU])
  1269. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1270. /* update header length based on lower device */
  1271. dev->hard_header_len = lowerdev->hard_header_len +
  1272. VXLAN_HEADROOM;
  1273. }
  1274. if (data[IFLA_VXLAN_TOS])
  1275. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  1276. if (data[IFLA_VXLAN_TTL])
  1277. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  1278. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  1279. vxlan->flags |= VXLAN_F_LEARN;
  1280. if (data[IFLA_VXLAN_AGEING])
  1281. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  1282. else
  1283. vxlan->age_interval = FDB_AGE_DEFAULT;
  1284. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  1285. vxlan->flags |= VXLAN_F_PROXY;
  1286. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  1287. vxlan->flags |= VXLAN_F_RSC;
  1288. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  1289. vxlan->flags |= VXLAN_F_L2MISS;
  1290. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  1291. vxlan->flags |= VXLAN_F_L3MISS;
  1292. if (data[IFLA_VXLAN_LIMIT])
  1293. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  1294. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1295. const struct ifla_vxlan_port_range *p
  1296. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1297. vxlan->port_min = ntohs(p->low);
  1298. vxlan->port_max = ntohs(p->high);
  1299. }
  1300. if (data[IFLA_VXLAN_PORT])
  1301. vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  1302. if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
  1303. pr_info("duplicate VNI %u\n", vni);
  1304. return -EEXIST;
  1305. }
  1306. vs = vxlan_find_port(net, vxlan->dst_port);
  1307. if (vs)
  1308. ++vs->refcnt;
  1309. else {
  1310. /* Drop lock because socket create acquires RTNL lock */
  1311. rtnl_unlock();
  1312. vs = vxlan_socket_create(net, vxlan->dst_port);
  1313. rtnl_lock();
  1314. if (IS_ERR(vs))
  1315. return PTR_ERR(vs);
  1316. hlist_add_head_rcu(&vs->hlist, vs_head(net, vxlan->dst_port));
  1317. }
  1318. vxlan->vn_sock = vs;
  1319. SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
  1320. err = register_netdevice(dev);
  1321. if (err) {
  1322. if (--vs->refcnt == 0) {
  1323. rtnl_unlock();
  1324. sk_release_kernel(vs->sock->sk);
  1325. kfree(vs);
  1326. rtnl_lock();
  1327. }
  1328. return err;
  1329. }
  1330. list_add(&vxlan->next, &vn->vxlan_list);
  1331. hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
  1332. return 0;
  1333. }
  1334. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  1335. {
  1336. struct vxlan_dev *vxlan = netdev_priv(dev);
  1337. struct vxlan_sock *vs = vxlan->vn_sock;
  1338. hlist_del_rcu(&vxlan->hlist);
  1339. list_del(&vxlan->next);
  1340. unregister_netdevice_queue(dev, head);
  1341. if (--vs->refcnt == 0) {
  1342. hlist_del_rcu(&vs->hlist);
  1343. schedule_work(&vs->del_work);
  1344. }
  1345. }
  1346. static size_t vxlan_get_size(const struct net_device *dev)
  1347. {
  1348. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  1349. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  1350. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  1351. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  1352. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  1353. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  1354. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  1355. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  1356. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  1357. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  1358. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  1359. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  1360. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  1361. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  1362. nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
  1363. 0;
  1364. }
  1365. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1366. {
  1367. const struct vxlan_dev *vxlan = netdev_priv(dev);
  1368. const struct vxlan_rdst *dst = &vxlan->default_dst;
  1369. struct ifla_vxlan_port_range ports = {
  1370. .low = htons(vxlan->port_min),
  1371. .high = htons(vxlan->port_max),
  1372. };
  1373. if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
  1374. goto nla_put_failure;
  1375. if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
  1376. goto nla_put_failure;
  1377. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  1378. goto nla_put_failure;
  1379. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  1380. goto nla_put_failure;
  1381. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  1382. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  1383. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  1384. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  1385. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  1386. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  1387. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  1388. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  1389. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  1390. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  1391. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  1392. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  1393. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
  1394. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
  1395. goto nla_put_failure;
  1396. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  1397. goto nla_put_failure;
  1398. return 0;
  1399. nla_put_failure:
  1400. return -EMSGSIZE;
  1401. }
  1402. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  1403. .kind = "vxlan",
  1404. .maxtype = IFLA_VXLAN_MAX,
  1405. .policy = vxlan_policy,
  1406. .priv_size = sizeof(struct vxlan_dev),
  1407. .setup = vxlan_setup,
  1408. .validate = vxlan_validate,
  1409. .newlink = vxlan_newlink,
  1410. .dellink = vxlan_dellink,
  1411. .get_size = vxlan_get_size,
  1412. .fill_info = vxlan_fill_info,
  1413. };
  1414. static __net_init int vxlan_init_net(struct net *net)
  1415. {
  1416. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1417. unsigned int h;
  1418. INIT_LIST_HEAD(&vn->vxlan_list);
  1419. for (h = 0; h < PORT_HASH_SIZE; ++h)
  1420. INIT_HLIST_HEAD(&vn->sock_list[h]);
  1421. return 0;
  1422. }
  1423. static __net_exit void vxlan_exit_net(struct net *net)
  1424. {
  1425. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1426. struct vxlan_dev *vxlan;
  1427. rtnl_lock();
  1428. list_for_each_entry(vxlan, &vn->vxlan_list, next)
  1429. dev_close(vxlan->dev);
  1430. rtnl_unlock();
  1431. }
  1432. static struct pernet_operations vxlan_net_ops = {
  1433. .init = vxlan_init_net,
  1434. .exit = vxlan_exit_net,
  1435. .id = &vxlan_net_id,
  1436. .size = sizeof(struct vxlan_net),
  1437. };
  1438. static int __init vxlan_init_module(void)
  1439. {
  1440. int rc;
  1441. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  1442. rc = register_pernet_device(&vxlan_net_ops);
  1443. if (rc)
  1444. goto out1;
  1445. rc = rtnl_link_register(&vxlan_link_ops);
  1446. if (rc)
  1447. goto out2;
  1448. return 0;
  1449. out2:
  1450. unregister_pernet_device(&vxlan_net_ops);
  1451. out1:
  1452. return rc;
  1453. }
  1454. late_initcall(vxlan_init_module);
  1455. static void __exit vxlan_cleanup_module(void)
  1456. {
  1457. rtnl_link_unregister(&vxlan_link_ops);
  1458. unregister_pernet_device(&vxlan_net_ops);
  1459. rcu_barrier();
  1460. }
  1461. module_exit(vxlan_cleanup_module);
  1462. MODULE_LICENSE("GPL");
  1463. MODULE_VERSION(VXLAN_VERSION);
  1464. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  1465. MODULE_ALIAS_RTNL_LINK("vxlan");