vxlan.c 50 KB

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