vxlan.c 40 KB

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