vxlan.c 41 KB

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