vxlan.c 45 KB

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