vxlan.c 40 KB

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