vxlan.c 41 KB

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