vxlan.c 41 KB

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