vxlan.c 40 KB

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