vxlan.c 41 KB

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