vxlan.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/rculist.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/in.h>
  20. #include <linux/ip.h>
  21. #include <linux/udp.h>
  22. #include <linux/igmp.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/hash.h>
  27. #include <linux/ethtool.h>
  28. #include <net/arp.h>
  29. #include <net/ndisc.h>
  30. #include <net/ip.h>
  31. #include <net/ip_tunnels.h>
  32. #include <net/icmp.h>
  33. #include <net/udp.h>
  34. #include <net/udp_tunnel.h>
  35. #include <net/rtnetlink.h>
  36. #include <net/route.h>
  37. #include <net/dsfield.h>
  38. #include <net/inet_ecn.h>
  39. #include <net/net_namespace.h>
  40. #include <net/netns/generic.h>
  41. #include <net/vxlan.h>
  42. #include <net/protocol.h>
  43. #include <net/udp_tunnel.h>
  44. #if IS_ENABLED(CONFIG_IPV6)
  45. #include <net/ipv6.h>
  46. #include <net/addrconf.h>
  47. #include <net/ip6_tunnel.h>
  48. #include <net/ip6_checksum.h>
  49. #endif
  50. #define VXLAN_VERSION "0.1"
  51. #define PORT_HASH_BITS 8
  52. #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
  53. #define VNI_HASH_BITS 10
  54. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  55. #define FDB_HASH_BITS 8
  56. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  57. #define FDB_AGE_DEFAULT 300 /* 5 min */
  58. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  59. #define VXLAN_N_VID (1u << 24)
  60. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  61. #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
  62. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  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. union vxlan_addr {
  82. struct sockaddr_in sin;
  83. struct sockaddr_in6 sin6;
  84. struct sockaddr sa;
  85. };
  86. struct vxlan_rdst {
  87. union vxlan_addr remote_ip;
  88. __be16 remote_port;
  89. u32 remote_vni;
  90. u32 remote_ifindex;
  91. struct list_head list;
  92. struct rcu_head rcu;
  93. };
  94. /* Forwarding table entry */
  95. struct vxlan_fdb {
  96. struct hlist_node hlist; /* linked list of entries */
  97. struct rcu_head rcu;
  98. unsigned long updated; /* jiffies */
  99. unsigned long used;
  100. struct list_head remotes;
  101. u16 state; /* see ndm_state */
  102. u8 flags; /* see ndm_flags */
  103. u8 eth_addr[ETH_ALEN];
  104. };
  105. /* Pseudo network device */
  106. struct vxlan_dev {
  107. struct hlist_node hlist; /* vni hash table */
  108. struct list_head next; /* vxlan's per namespace list */
  109. struct vxlan_sock *vn_sock; /* listening socket */
  110. struct net_device *dev;
  111. struct net *net; /* netns for packet i/o */
  112. struct vxlan_rdst default_dst; /* default destination */
  113. union vxlan_addr saddr; /* source address */
  114. __be16 dst_port;
  115. __u16 port_min; /* source port range */
  116. __u16 port_max;
  117. __u8 tos; /* TOS override */
  118. __u8 ttl;
  119. u32 flags; /* VXLAN_F_* in vxlan.h */
  120. struct work_struct sock_work;
  121. struct work_struct igmp_join;
  122. struct work_struct igmp_leave;
  123. unsigned long age_interval;
  124. struct timer_list age_timer;
  125. spinlock_t hash_lock;
  126. unsigned int addrcnt;
  127. unsigned int addrmax;
  128. struct hlist_head fdb_head[FDB_HASH_SIZE];
  129. };
  130. /* salt for hash table */
  131. static u32 vxlan_salt __read_mostly;
  132. static struct workqueue_struct *vxlan_wq;
  133. static void vxlan_sock_work(struct work_struct *work);
  134. #if IS_ENABLED(CONFIG_IPV6)
  135. static inline
  136. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  137. {
  138. if (a->sa.sa_family != b->sa.sa_family)
  139. return false;
  140. if (a->sa.sa_family == AF_INET6)
  141. return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
  142. else
  143. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  144. }
  145. static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
  146. {
  147. if (ipa->sa.sa_family == AF_INET6)
  148. return ipv6_addr_any(&ipa->sin6.sin6_addr);
  149. else
  150. return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
  151. }
  152. static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
  153. {
  154. if (ipa->sa.sa_family == AF_INET6)
  155. return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
  156. else
  157. return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
  158. }
  159. static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
  160. {
  161. if (nla_len(nla) >= sizeof(struct in6_addr)) {
  162. nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
  163. ip->sa.sa_family = AF_INET6;
  164. return 0;
  165. } else if (nla_len(nla) >= sizeof(__be32)) {
  166. ip->sin.sin_addr.s_addr = nla_get_be32(nla);
  167. ip->sa.sa_family = AF_INET;
  168. return 0;
  169. } else {
  170. return -EAFNOSUPPORT;
  171. }
  172. }
  173. static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
  174. const union vxlan_addr *ip)
  175. {
  176. if (ip->sa.sa_family == AF_INET6)
  177. return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
  178. else
  179. return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
  180. }
  181. #else /* !CONFIG_IPV6 */
  182. static inline
  183. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  184. {
  185. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  186. }
  187. static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
  188. {
  189. return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
  190. }
  191. static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
  192. {
  193. return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
  194. }
  195. static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
  196. {
  197. if (nla_len(nla) >= sizeof(struct in6_addr)) {
  198. return -EAFNOSUPPORT;
  199. } else if (nla_len(nla) >= sizeof(__be32)) {
  200. ip->sin.sin_addr.s_addr = nla_get_be32(nla);
  201. ip->sa.sa_family = AF_INET;
  202. return 0;
  203. } else {
  204. return -EAFNOSUPPORT;
  205. }
  206. }
  207. static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
  208. const union vxlan_addr *ip)
  209. {
  210. return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
  211. }
  212. #endif
  213. /* Virtual Network hash table head */
  214. static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
  215. {
  216. return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
  217. }
  218. /* Socket hash table head */
  219. static inline struct hlist_head *vs_head(struct net *net, __be16 port)
  220. {
  221. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  222. return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
  223. }
  224. /* First remote destination for a forwarding entry.
  225. * Guaranteed to be non-NULL because remotes are never deleted.
  226. */
  227. static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
  228. {
  229. return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
  230. }
  231. static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
  232. {
  233. return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
  234. }
  235. /* Find VXLAN socket based on network namespace, address family and UDP port */
  236. static struct vxlan_sock *vxlan_find_sock(struct net *net,
  237. sa_family_t family, __be16 port)
  238. {
  239. struct vxlan_sock *vs;
  240. hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
  241. if (inet_sk(vs->sock->sk)->inet_sport == port &&
  242. inet_sk(vs->sock->sk)->sk.sk_family == family)
  243. return vs;
  244. }
  245. return NULL;
  246. }
  247. static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
  248. {
  249. struct vxlan_dev *vxlan;
  250. hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
  251. if (vxlan->default_dst.remote_vni == id)
  252. return vxlan;
  253. }
  254. return NULL;
  255. }
  256. /* Look up VNI in a per net namespace table */
  257. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
  258. sa_family_t family, __be16 port)
  259. {
  260. struct vxlan_sock *vs;
  261. vs = vxlan_find_sock(net, family, port);
  262. if (!vs)
  263. return NULL;
  264. return vxlan_vs_find_vni(vs, id);
  265. }
  266. /* Fill in neighbour message in skbuff. */
  267. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  268. const struct vxlan_fdb *fdb,
  269. u32 portid, u32 seq, int type, unsigned int flags,
  270. const struct vxlan_rdst *rdst)
  271. {
  272. unsigned long now = jiffies;
  273. struct nda_cacheinfo ci;
  274. struct nlmsghdr *nlh;
  275. struct ndmsg *ndm;
  276. bool send_ip, send_eth;
  277. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  278. if (nlh == NULL)
  279. return -EMSGSIZE;
  280. ndm = nlmsg_data(nlh);
  281. memset(ndm, 0, sizeof(*ndm));
  282. send_eth = send_ip = true;
  283. if (type == RTM_GETNEIGH) {
  284. ndm->ndm_family = AF_INET;
  285. send_ip = !vxlan_addr_any(&rdst->remote_ip);
  286. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  287. } else
  288. ndm->ndm_family = AF_BRIDGE;
  289. ndm->ndm_state = fdb->state;
  290. ndm->ndm_ifindex = vxlan->dev->ifindex;
  291. ndm->ndm_flags = fdb->flags;
  292. ndm->ndm_type = RTN_UNICAST;
  293. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  294. goto nla_put_failure;
  295. if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
  296. goto nla_put_failure;
  297. if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
  298. nla_put_be16(skb, NDA_PORT, rdst->remote_port))
  299. goto nla_put_failure;
  300. if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
  301. nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
  302. goto nla_put_failure;
  303. if (rdst->remote_ifindex &&
  304. nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
  305. goto nla_put_failure;
  306. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  307. ci.ndm_confirmed = 0;
  308. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  309. ci.ndm_refcnt = 0;
  310. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  311. goto nla_put_failure;
  312. return nlmsg_end(skb, nlh);
  313. nla_put_failure:
  314. nlmsg_cancel(skb, nlh);
  315. return -EMSGSIZE;
  316. }
  317. static inline size_t vxlan_nlmsg_size(void)
  318. {
  319. return NLMSG_ALIGN(sizeof(struct ndmsg))
  320. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  321. + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
  322. + nla_total_size(sizeof(__be16)) /* NDA_PORT */
  323. + nla_total_size(sizeof(__be32)) /* NDA_VNI */
  324. + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
  325. + nla_total_size(sizeof(struct nda_cacheinfo));
  326. }
  327. static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
  328. struct vxlan_rdst *rd, int type)
  329. {
  330. struct net *net = dev_net(vxlan->dev);
  331. struct sk_buff *skb;
  332. int err = -ENOBUFS;
  333. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  334. if (skb == NULL)
  335. goto errout;
  336. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
  337. if (err < 0) {
  338. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  339. WARN_ON(err == -EMSGSIZE);
  340. kfree_skb(skb);
  341. goto errout;
  342. }
  343. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  344. return;
  345. errout:
  346. if (err < 0)
  347. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  348. }
  349. static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
  350. {
  351. struct vxlan_dev *vxlan = netdev_priv(dev);
  352. struct vxlan_fdb f = {
  353. .state = NUD_STALE,
  354. };
  355. struct vxlan_rdst remote = {
  356. .remote_ip = *ipa, /* goes to NDA_DST */
  357. .remote_vni = VXLAN_N_VID,
  358. };
  359. vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
  360. }
  361. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  362. {
  363. struct vxlan_fdb f = {
  364. .state = NUD_STALE,
  365. };
  366. struct vxlan_rdst remote = { };
  367. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  368. vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
  369. }
  370. /* Hash Ethernet address */
  371. static u32 eth_hash(const unsigned char *addr)
  372. {
  373. u64 value = get_unaligned((u64 *)addr);
  374. /* only want 6 bytes */
  375. #ifdef __BIG_ENDIAN
  376. value >>= 16;
  377. #else
  378. value <<= 16;
  379. #endif
  380. return hash_64(value, FDB_HASH_BITS);
  381. }
  382. /* Hash chain to use given mac address */
  383. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  384. const u8 *mac)
  385. {
  386. return &vxlan->fdb_head[eth_hash(mac)];
  387. }
  388. /* Look up Ethernet address in forwarding table */
  389. static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
  390. const u8 *mac)
  391. {
  392. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  393. struct vxlan_fdb *f;
  394. hlist_for_each_entry_rcu(f, head, hlist) {
  395. if (ether_addr_equal(mac, f->eth_addr))
  396. return f;
  397. }
  398. return NULL;
  399. }
  400. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  401. const u8 *mac)
  402. {
  403. struct vxlan_fdb *f;
  404. f = __vxlan_find_mac(vxlan, mac);
  405. if (f)
  406. f->used = jiffies;
  407. return f;
  408. }
  409. /* caller should hold vxlan->hash_lock */
  410. static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
  411. union vxlan_addr *ip, __be16 port,
  412. __u32 vni, __u32 ifindex)
  413. {
  414. struct vxlan_rdst *rd;
  415. list_for_each_entry(rd, &f->remotes, list) {
  416. if (vxlan_addr_equal(&rd->remote_ip, ip) &&
  417. rd->remote_port == port &&
  418. rd->remote_vni == vni &&
  419. rd->remote_ifindex == ifindex)
  420. return rd;
  421. }
  422. return NULL;
  423. }
  424. /* Replace destination of unicast mac */
  425. static int vxlan_fdb_replace(struct vxlan_fdb *f,
  426. union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
  427. {
  428. struct vxlan_rdst *rd;
  429. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  430. if (rd)
  431. return 0;
  432. rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
  433. if (!rd)
  434. return 0;
  435. rd->remote_ip = *ip;
  436. rd->remote_port = port;
  437. rd->remote_vni = vni;
  438. rd->remote_ifindex = ifindex;
  439. return 1;
  440. }
  441. /* Add/update destinations for multicast */
  442. static int vxlan_fdb_append(struct vxlan_fdb *f,
  443. union vxlan_addr *ip, __be16 port, __u32 vni,
  444. __u32 ifindex, struct vxlan_rdst **rdp)
  445. {
  446. struct vxlan_rdst *rd;
  447. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  448. if (rd)
  449. return 0;
  450. rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
  451. if (rd == NULL)
  452. return -ENOBUFS;
  453. rd->remote_ip = *ip;
  454. rd->remote_port = port;
  455. rd->remote_vni = vni;
  456. rd->remote_ifindex = ifindex;
  457. list_add_tail_rcu(&rd->list, &f->remotes);
  458. *rdp = rd;
  459. return 1;
  460. }
  461. static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  462. {
  463. struct sk_buff *p, **pp = NULL;
  464. struct vxlanhdr *vh, *vh2;
  465. unsigned int hlen, off_vx;
  466. int flush = 1;
  467. off_vx = skb_gro_offset(skb);
  468. hlen = off_vx + sizeof(*vh);
  469. vh = skb_gro_header_fast(skb, off_vx);
  470. if (skb_gro_header_hard(skb, hlen)) {
  471. vh = skb_gro_header_slow(skb, hlen, off_vx);
  472. if (unlikely(!vh))
  473. goto out;
  474. }
  475. flush = 0;
  476. for (p = *head; p; p = p->next) {
  477. if (!NAPI_GRO_CB(p)->same_flow)
  478. continue;
  479. vh2 = (struct vxlanhdr *)(p->data + off_vx);
  480. if (vh->vx_vni != vh2->vx_vni) {
  481. NAPI_GRO_CB(p)->same_flow = 0;
  482. continue;
  483. }
  484. }
  485. skb_gro_pull(skb, sizeof(struct vxlanhdr));
  486. skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
  487. pp = eth_gro_receive(head, skb);
  488. out:
  489. NAPI_GRO_CB(skb)->flush |= flush;
  490. return pp;
  491. }
  492. static int vxlan_gro_complete(struct sk_buff *skb, int nhoff)
  493. {
  494. udp_tunnel_gro_complete(skb, nhoff);
  495. return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
  496. }
  497. /* Notify netdevs that UDP port started listening */
  498. static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
  499. {
  500. struct net_device *dev;
  501. struct sock *sk = vs->sock->sk;
  502. struct net *net = sock_net(sk);
  503. sa_family_t sa_family = sk->sk_family;
  504. __be16 port = inet_sk(sk)->inet_sport;
  505. int err;
  506. if (sa_family == AF_INET) {
  507. err = udp_add_offload(&vs->udp_offloads);
  508. if (err)
  509. pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
  510. }
  511. rcu_read_lock();
  512. for_each_netdev_rcu(net, dev) {
  513. if (dev->netdev_ops->ndo_add_vxlan_port)
  514. dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
  515. port);
  516. }
  517. rcu_read_unlock();
  518. }
  519. /* Notify netdevs that UDP port is no more listening */
  520. static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
  521. {
  522. struct net_device *dev;
  523. struct sock *sk = vs->sock->sk;
  524. struct net *net = sock_net(sk);
  525. sa_family_t sa_family = sk->sk_family;
  526. __be16 port = inet_sk(sk)->inet_sport;
  527. rcu_read_lock();
  528. for_each_netdev_rcu(net, dev) {
  529. if (dev->netdev_ops->ndo_del_vxlan_port)
  530. dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
  531. port);
  532. }
  533. rcu_read_unlock();
  534. if (sa_family == AF_INET)
  535. udp_del_offload(&vs->udp_offloads);
  536. }
  537. /* Add new entry to forwarding table -- assumes lock held */
  538. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  539. const u8 *mac, union vxlan_addr *ip,
  540. __u16 state, __u16 flags,
  541. __be16 port, __u32 vni, __u32 ifindex,
  542. __u8 ndm_flags)
  543. {
  544. struct vxlan_rdst *rd = NULL;
  545. struct vxlan_fdb *f;
  546. int notify = 0;
  547. f = __vxlan_find_mac(vxlan, mac);
  548. if (f) {
  549. if (flags & NLM_F_EXCL) {
  550. netdev_dbg(vxlan->dev,
  551. "lost race to create %pM\n", mac);
  552. return -EEXIST;
  553. }
  554. if (f->state != state) {
  555. f->state = state;
  556. f->updated = jiffies;
  557. notify = 1;
  558. }
  559. if (f->flags != ndm_flags) {
  560. f->flags = ndm_flags;
  561. f->updated = jiffies;
  562. notify = 1;
  563. }
  564. if ((flags & NLM_F_REPLACE)) {
  565. /* Only change unicasts */
  566. if (!(is_multicast_ether_addr(f->eth_addr) ||
  567. is_zero_ether_addr(f->eth_addr))) {
  568. int rc = vxlan_fdb_replace(f, ip, port, vni,
  569. ifindex);
  570. if (rc < 0)
  571. return rc;
  572. notify |= rc;
  573. } else
  574. return -EOPNOTSUPP;
  575. }
  576. if ((flags & NLM_F_APPEND) &&
  577. (is_multicast_ether_addr(f->eth_addr) ||
  578. is_zero_ether_addr(f->eth_addr))) {
  579. int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
  580. &rd);
  581. if (rc < 0)
  582. return rc;
  583. notify |= rc;
  584. }
  585. } else {
  586. if (!(flags & NLM_F_CREATE))
  587. return -ENOENT;
  588. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  589. return -ENOSPC;
  590. /* Disallow replace to add a multicast entry */
  591. if ((flags & NLM_F_REPLACE) &&
  592. (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
  593. return -EOPNOTSUPP;
  594. netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
  595. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  596. if (!f)
  597. return -ENOMEM;
  598. notify = 1;
  599. f->state = state;
  600. f->flags = ndm_flags;
  601. f->updated = f->used = jiffies;
  602. INIT_LIST_HEAD(&f->remotes);
  603. memcpy(f->eth_addr, mac, ETH_ALEN);
  604. vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
  605. ++vxlan->addrcnt;
  606. hlist_add_head_rcu(&f->hlist,
  607. vxlan_fdb_head(vxlan, mac));
  608. }
  609. if (notify) {
  610. if (rd == NULL)
  611. rd = first_remote_rtnl(f);
  612. vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
  613. }
  614. return 0;
  615. }
  616. static void vxlan_fdb_free(struct rcu_head *head)
  617. {
  618. struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
  619. struct vxlan_rdst *rd, *nd;
  620. list_for_each_entry_safe(rd, nd, &f->remotes, list)
  621. kfree(rd);
  622. kfree(f);
  623. }
  624. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  625. {
  626. netdev_dbg(vxlan->dev,
  627. "delete %pM\n", f->eth_addr);
  628. --vxlan->addrcnt;
  629. vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
  630. hlist_del_rcu(&f->hlist);
  631. call_rcu(&f->rcu, vxlan_fdb_free);
  632. }
  633. static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
  634. union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
  635. {
  636. struct net *net = dev_net(vxlan->dev);
  637. int err;
  638. if (tb[NDA_DST]) {
  639. err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
  640. if (err)
  641. return err;
  642. } else {
  643. union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
  644. if (remote->sa.sa_family == AF_INET) {
  645. ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
  646. ip->sa.sa_family = AF_INET;
  647. #if IS_ENABLED(CONFIG_IPV6)
  648. } else {
  649. ip->sin6.sin6_addr = in6addr_any;
  650. ip->sa.sa_family = AF_INET6;
  651. #endif
  652. }
  653. }
  654. if (tb[NDA_PORT]) {
  655. if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
  656. return -EINVAL;
  657. *port = nla_get_be16(tb[NDA_PORT]);
  658. } else {
  659. *port = vxlan->dst_port;
  660. }
  661. if (tb[NDA_VNI]) {
  662. if (nla_len(tb[NDA_VNI]) != sizeof(u32))
  663. return -EINVAL;
  664. *vni = nla_get_u32(tb[NDA_VNI]);
  665. } else {
  666. *vni = vxlan->default_dst.remote_vni;
  667. }
  668. if (tb[NDA_IFINDEX]) {
  669. struct net_device *tdev;
  670. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
  671. return -EINVAL;
  672. *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
  673. tdev = __dev_get_by_index(net, *ifindex);
  674. if (!tdev)
  675. return -EADDRNOTAVAIL;
  676. } else {
  677. *ifindex = 0;
  678. }
  679. return 0;
  680. }
  681. /* Add static entry (via netlink) */
  682. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  683. struct net_device *dev,
  684. const unsigned char *addr, u16 vid, u16 flags)
  685. {
  686. struct vxlan_dev *vxlan = netdev_priv(dev);
  687. /* struct net *net = dev_net(vxlan->dev); */
  688. union vxlan_addr ip;
  689. __be16 port;
  690. u32 vni, ifindex;
  691. int err;
  692. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  693. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  694. ndm->ndm_state);
  695. return -EINVAL;
  696. }
  697. if (tb[NDA_DST] == NULL)
  698. return -EINVAL;
  699. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  700. if (err)
  701. return err;
  702. if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
  703. return -EAFNOSUPPORT;
  704. spin_lock_bh(&vxlan->hash_lock);
  705. err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
  706. port, vni, ifindex, ndm->ndm_flags);
  707. spin_unlock_bh(&vxlan->hash_lock);
  708. return err;
  709. }
  710. /* Delete entry (via netlink) */
  711. static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  712. struct net_device *dev,
  713. const unsigned char *addr, u16 vid)
  714. {
  715. struct vxlan_dev *vxlan = netdev_priv(dev);
  716. struct vxlan_fdb *f;
  717. struct vxlan_rdst *rd = NULL;
  718. union vxlan_addr ip;
  719. __be16 port;
  720. u32 vni, ifindex;
  721. int err;
  722. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  723. if (err)
  724. return err;
  725. err = -ENOENT;
  726. spin_lock_bh(&vxlan->hash_lock);
  727. f = vxlan_find_mac(vxlan, addr);
  728. if (!f)
  729. goto out;
  730. if (!vxlan_addr_any(&ip)) {
  731. rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
  732. if (!rd)
  733. goto out;
  734. }
  735. err = 0;
  736. /* remove a destination if it's not the only one on the list,
  737. * otherwise destroy the fdb entry
  738. */
  739. if (rd && !list_is_singular(&f->remotes)) {
  740. list_del_rcu(&rd->list);
  741. vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
  742. kfree_rcu(rd, rcu);
  743. goto out;
  744. }
  745. vxlan_fdb_destroy(vxlan, f);
  746. out:
  747. spin_unlock_bh(&vxlan->hash_lock);
  748. return err;
  749. }
  750. /* Dump forwarding table */
  751. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  752. struct net_device *dev,
  753. struct net_device *filter_dev, int idx)
  754. {
  755. struct vxlan_dev *vxlan = netdev_priv(dev);
  756. unsigned int h;
  757. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  758. struct vxlan_fdb *f;
  759. int err;
  760. hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
  761. struct vxlan_rdst *rd;
  762. if (idx < cb->args[0])
  763. goto skip;
  764. list_for_each_entry_rcu(rd, &f->remotes, list) {
  765. err = vxlan_fdb_info(skb, vxlan, f,
  766. NETLINK_CB(cb->skb).portid,
  767. cb->nlh->nlmsg_seq,
  768. RTM_NEWNEIGH,
  769. NLM_F_MULTI, rd);
  770. if (err < 0)
  771. goto out;
  772. }
  773. skip:
  774. ++idx;
  775. }
  776. }
  777. out:
  778. return idx;
  779. }
  780. /* Watch incoming packets to learn mapping between Ethernet address
  781. * and Tunnel endpoint.
  782. * Return true if packet is bogus and should be droppped.
  783. */
  784. static bool vxlan_snoop(struct net_device *dev,
  785. union vxlan_addr *src_ip, const u8 *src_mac)
  786. {
  787. struct vxlan_dev *vxlan = netdev_priv(dev);
  788. struct vxlan_fdb *f;
  789. f = vxlan_find_mac(vxlan, src_mac);
  790. if (likely(f)) {
  791. struct vxlan_rdst *rdst = first_remote_rcu(f);
  792. if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
  793. return false;
  794. /* Don't migrate static entries, drop packets */
  795. if (f->state & NUD_NOARP)
  796. return true;
  797. if (net_ratelimit())
  798. netdev_info(dev,
  799. "%pM migrated from %pIS to %pIS\n",
  800. src_mac, &rdst->remote_ip, &src_ip);
  801. rdst->remote_ip = *src_ip;
  802. f->updated = jiffies;
  803. vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
  804. } else {
  805. /* learned new entry */
  806. spin_lock(&vxlan->hash_lock);
  807. /* close off race between vxlan_flush and incoming packets */
  808. if (netif_running(dev))
  809. vxlan_fdb_create(vxlan, src_mac, src_ip,
  810. NUD_REACHABLE,
  811. NLM_F_EXCL|NLM_F_CREATE,
  812. vxlan->dst_port,
  813. vxlan->default_dst.remote_vni,
  814. 0, NTF_SELF);
  815. spin_unlock(&vxlan->hash_lock);
  816. }
  817. return false;
  818. }
  819. /* See if multicast group is already in use by other ID */
  820. static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
  821. {
  822. struct vxlan_dev *vxlan;
  823. /* The vxlan_sock is only used by dev, leaving group has
  824. * no effect on other vxlan devices.
  825. */
  826. if (atomic_read(&dev->vn_sock->refcnt) == 1)
  827. return false;
  828. list_for_each_entry(vxlan, &vn->vxlan_list, next) {
  829. if (!netif_running(vxlan->dev) || vxlan == dev)
  830. continue;
  831. if (vxlan->vn_sock != dev->vn_sock)
  832. continue;
  833. if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
  834. &dev->default_dst.remote_ip))
  835. continue;
  836. if (vxlan->default_dst.remote_ifindex !=
  837. dev->default_dst.remote_ifindex)
  838. continue;
  839. return true;
  840. }
  841. return false;
  842. }
  843. static void vxlan_sock_hold(struct vxlan_sock *vs)
  844. {
  845. atomic_inc(&vs->refcnt);
  846. }
  847. void vxlan_sock_release(struct vxlan_sock *vs)
  848. {
  849. struct sock *sk = vs->sock->sk;
  850. struct net *net = sock_net(sk);
  851. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  852. if (!atomic_dec_and_test(&vs->refcnt))
  853. return;
  854. spin_lock(&vn->sock_lock);
  855. hlist_del_rcu(&vs->hlist);
  856. vxlan_notify_del_rx_port(vs);
  857. spin_unlock(&vn->sock_lock);
  858. queue_work(vxlan_wq, &vs->del_work);
  859. }
  860. EXPORT_SYMBOL_GPL(vxlan_sock_release);
  861. /* Callback to update multicast group membership when first VNI on
  862. * multicast asddress is brought up
  863. * Done as workqueue because ip_mc_join_group acquires RTNL.
  864. */
  865. static void vxlan_igmp_join(struct work_struct *work)
  866. {
  867. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
  868. struct vxlan_sock *vs = vxlan->vn_sock;
  869. struct sock *sk = vs->sock->sk;
  870. union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
  871. int ifindex = vxlan->default_dst.remote_ifindex;
  872. lock_sock(sk);
  873. if (ip->sa.sa_family == AF_INET) {
  874. struct ip_mreqn mreq = {
  875. .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
  876. .imr_ifindex = ifindex,
  877. };
  878. ip_mc_join_group(sk, &mreq);
  879. #if IS_ENABLED(CONFIG_IPV6)
  880. } else {
  881. ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
  882. &ip->sin6.sin6_addr);
  883. #endif
  884. }
  885. release_sock(sk);
  886. vxlan_sock_release(vs);
  887. dev_put(vxlan->dev);
  888. }
  889. /* Inverse of vxlan_igmp_join when last VNI is brought down */
  890. static void vxlan_igmp_leave(struct work_struct *work)
  891. {
  892. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
  893. struct vxlan_sock *vs = vxlan->vn_sock;
  894. struct sock *sk = vs->sock->sk;
  895. union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
  896. int ifindex = vxlan->default_dst.remote_ifindex;
  897. lock_sock(sk);
  898. if (ip->sa.sa_family == AF_INET) {
  899. struct ip_mreqn mreq = {
  900. .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
  901. .imr_ifindex = ifindex,
  902. };
  903. ip_mc_leave_group(sk, &mreq);
  904. #if IS_ENABLED(CONFIG_IPV6)
  905. } else {
  906. ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
  907. &ip->sin6.sin6_addr);
  908. #endif
  909. }
  910. release_sock(sk);
  911. vxlan_sock_release(vs);
  912. dev_put(vxlan->dev);
  913. }
  914. /* Callback from net/ipv4/udp.c to receive packets */
  915. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  916. {
  917. struct vxlan_sock *vs;
  918. struct vxlanhdr *vxh;
  919. /* Need Vxlan and inner Ethernet header to be present */
  920. if (!pskb_may_pull(skb, VXLAN_HLEN))
  921. goto error;
  922. /* Return packets with reserved bits set */
  923. vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
  924. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  925. (vxh->vx_vni & htonl(0xff))) {
  926. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  927. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  928. goto error;
  929. }
  930. if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
  931. goto drop;
  932. vs = rcu_dereference_sk_user_data(sk);
  933. if (!vs)
  934. goto drop;
  935. vs->rcv(vs, skb, vxh->vx_vni);
  936. return 0;
  937. drop:
  938. /* Consume bad packet */
  939. kfree_skb(skb);
  940. return 0;
  941. error:
  942. /* Return non vxlan pkt */
  943. return 1;
  944. }
  945. static void vxlan_rcv(struct vxlan_sock *vs,
  946. struct sk_buff *skb, __be32 vx_vni)
  947. {
  948. struct iphdr *oip = NULL;
  949. struct ipv6hdr *oip6 = NULL;
  950. struct vxlan_dev *vxlan;
  951. struct pcpu_sw_netstats *stats;
  952. union vxlan_addr saddr;
  953. __u32 vni;
  954. int err = 0;
  955. union vxlan_addr *remote_ip;
  956. vni = ntohl(vx_vni) >> 8;
  957. /* Is this VNI defined? */
  958. vxlan = vxlan_vs_find_vni(vs, vni);
  959. if (!vxlan)
  960. goto drop;
  961. remote_ip = &vxlan->default_dst.remote_ip;
  962. skb_reset_mac_header(skb);
  963. skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
  964. skb->protocol = eth_type_trans(skb, vxlan->dev);
  965. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  966. /* Ignore packet loops (and multicast echo) */
  967. if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
  968. goto drop;
  969. /* Re-examine inner Ethernet packet */
  970. if (remote_ip->sa.sa_family == AF_INET) {
  971. oip = ip_hdr(skb);
  972. saddr.sin.sin_addr.s_addr = oip->saddr;
  973. saddr.sa.sa_family = AF_INET;
  974. #if IS_ENABLED(CONFIG_IPV6)
  975. } else {
  976. oip6 = ipv6_hdr(skb);
  977. saddr.sin6.sin6_addr = oip6->saddr;
  978. saddr.sa.sa_family = AF_INET6;
  979. #endif
  980. }
  981. if ((vxlan->flags & VXLAN_F_LEARN) &&
  982. vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
  983. goto drop;
  984. skb_reset_network_header(skb);
  985. if (oip6)
  986. err = IP6_ECN_decapsulate(oip6, skb);
  987. if (oip)
  988. err = IP_ECN_decapsulate(oip, skb);
  989. if (unlikely(err)) {
  990. if (log_ecn_error) {
  991. if (oip6)
  992. net_info_ratelimited("non-ECT from %pI6\n",
  993. &oip6->saddr);
  994. if (oip)
  995. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  996. &oip->saddr, oip->tos);
  997. }
  998. if (err > 1) {
  999. ++vxlan->dev->stats.rx_frame_errors;
  1000. ++vxlan->dev->stats.rx_errors;
  1001. goto drop;
  1002. }
  1003. }
  1004. stats = this_cpu_ptr(vxlan->dev->tstats);
  1005. u64_stats_update_begin(&stats->syncp);
  1006. stats->rx_packets++;
  1007. stats->rx_bytes += skb->len;
  1008. u64_stats_update_end(&stats->syncp);
  1009. netif_rx(skb);
  1010. return;
  1011. drop:
  1012. /* Consume bad packet */
  1013. kfree_skb(skb);
  1014. }
  1015. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  1016. {
  1017. struct vxlan_dev *vxlan = netdev_priv(dev);
  1018. struct arphdr *parp;
  1019. u8 *arpptr, *sha;
  1020. __be32 sip, tip;
  1021. struct neighbour *n;
  1022. if (dev->flags & IFF_NOARP)
  1023. goto out;
  1024. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  1025. dev->stats.tx_dropped++;
  1026. goto out;
  1027. }
  1028. parp = arp_hdr(skb);
  1029. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  1030. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  1031. parp->ar_pro != htons(ETH_P_IP) ||
  1032. parp->ar_op != htons(ARPOP_REQUEST) ||
  1033. parp->ar_hln != dev->addr_len ||
  1034. parp->ar_pln != 4)
  1035. goto out;
  1036. arpptr = (u8 *)parp + sizeof(struct arphdr);
  1037. sha = arpptr;
  1038. arpptr += dev->addr_len; /* sha */
  1039. memcpy(&sip, arpptr, sizeof(sip));
  1040. arpptr += sizeof(sip);
  1041. arpptr += dev->addr_len; /* tha */
  1042. memcpy(&tip, arpptr, sizeof(tip));
  1043. if (ipv4_is_loopback(tip) ||
  1044. ipv4_is_multicast(tip))
  1045. goto out;
  1046. n = neigh_lookup(&arp_tbl, &tip, dev);
  1047. if (n) {
  1048. struct vxlan_fdb *f;
  1049. struct sk_buff *reply;
  1050. if (!(n->nud_state & NUD_CONNECTED)) {
  1051. neigh_release(n);
  1052. goto out;
  1053. }
  1054. f = vxlan_find_mac(vxlan, n->ha);
  1055. if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
  1056. /* bridge-local neighbor */
  1057. neigh_release(n);
  1058. goto out;
  1059. }
  1060. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  1061. n->ha, sha);
  1062. neigh_release(n);
  1063. if (reply == NULL)
  1064. goto out;
  1065. skb_reset_mac_header(reply);
  1066. __skb_pull(reply, skb_network_offset(reply));
  1067. reply->ip_summed = CHECKSUM_UNNECESSARY;
  1068. reply->pkt_type = PACKET_HOST;
  1069. if (netif_rx_ni(reply) == NET_RX_DROP)
  1070. dev->stats.rx_dropped++;
  1071. } else if (vxlan->flags & VXLAN_F_L3MISS) {
  1072. union vxlan_addr ipa = {
  1073. .sin.sin_addr.s_addr = tip,
  1074. .sin.sin_family = AF_INET,
  1075. };
  1076. vxlan_ip_miss(dev, &ipa);
  1077. }
  1078. out:
  1079. consume_skb(skb);
  1080. return NETDEV_TX_OK;
  1081. }
  1082. #if IS_ENABLED(CONFIG_IPV6)
  1083. static struct sk_buff *vxlan_na_create(struct sk_buff *request,
  1084. struct neighbour *n, bool isrouter)
  1085. {
  1086. struct net_device *dev = request->dev;
  1087. struct sk_buff *reply;
  1088. struct nd_msg *ns, *na;
  1089. struct ipv6hdr *pip6;
  1090. u8 *daddr;
  1091. int na_olen = 8; /* opt hdr + ETH_ALEN for target */
  1092. int ns_olen;
  1093. int i, len;
  1094. if (dev == NULL)
  1095. return NULL;
  1096. len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
  1097. sizeof(*na) + na_olen + dev->needed_tailroom;
  1098. reply = alloc_skb(len, GFP_ATOMIC);
  1099. if (reply == NULL)
  1100. return NULL;
  1101. reply->protocol = htons(ETH_P_IPV6);
  1102. reply->dev = dev;
  1103. skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
  1104. skb_push(reply, sizeof(struct ethhdr));
  1105. skb_set_mac_header(reply, 0);
  1106. ns = (struct nd_msg *)skb_transport_header(request);
  1107. daddr = eth_hdr(request)->h_source;
  1108. ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
  1109. for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
  1110. if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
  1111. daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
  1112. break;
  1113. }
  1114. }
  1115. /* Ethernet header */
  1116. ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
  1117. ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
  1118. eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
  1119. reply->protocol = htons(ETH_P_IPV6);
  1120. skb_pull(reply, sizeof(struct ethhdr));
  1121. skb_set_network_header(reply, 0);
  1122. skb_put(reply, sizeof(struct ipv6hdr));
  1123. /* IPv6 header */
  1124. pip6 = ipv6_hdr(reply);
  1125. memset(pip6, 0, sizeof(struct ipv6hdr));
  1126. pip6->version = 6;
  1127. pip6->priority = ipv6_hdr(request)->priority;
  1128. pip6->nexthdr = IPPROTO_ICMPV6;
  1129. pip6->hop_limit = 255;
  1130. pip6->daddr = ipv6_hdr(request)->saddr;
  1131. pip6->saddr = *(struct in6_addr *)n->primary_key;
  1132. skb_pull(reply, sizeof(struct ipv6hdr));
  1133. skb_set_transport_header(reply, 0);
  1134. na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
  1135. /* Neighbor Advertisement */
  1136. memset(na, 0, sizeof(*na)+na_olen);
  1137. na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
  1138. na->icmph.icmp6_router = isrouter;
  1139. na->icmph.icmp6_override = 1;
  1140. na->icmph.icmp6_solicited = 1;
  1141. na->target = ns->target;
  1142. ether_addr_copy(&na->opt[2], n->ha);
  1143. na->opt[0] = ND_OPT_TARGET_LL_ADDR;
  1144. na->opt[1] = na_olen >> 3;
  1145. na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
  1146. &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
  1147. csum_partial(na, sizeof(*na)+na_olen, 0));
  1148. pip6->payload_len = htons(sizeof(*na)+na_olen);
  1149. skb_push(reply, sizeof(struct ipv6hdr));
  1150. reply->ip_summed = CHECKSUM_UNNECESSARY;
  1151. return reply;
  1152. }
  1153. static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
  1154. {
  1155. struct vxlan_dev *vxlan = netdev_priv(dev);
  1156. struct nd_msg *msg;
  1157. const struct ipv6hdr *iphdr;
  1158. const struct in6_addr *saddr, *daddr;
  1159. struct neighbour *n;
  1160. struct inet6_dev *in6_dev;
  1161. in6_dev = __in6_dev_get(dev);
  1162. if (!in6_dev)
  1163. goto out;
  1164. iphdr = ipv6_hdr(skb);
  1165. saddr = &iphdr->saddr;
  1166. daddr = &iphdr->daddr;
  1167. msg = (struct nd_msg *)skb_transport_header(skb);
  1168. if (msg->icmph.icmp6_code != 0 ||
  1169. msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  1170. goto out;
  1171. if (ipv6_addr_loopback(daddr) ||
  1172. ipv6_addr_is_multicast(&msg->target))
  1173. goto out;
  1174. n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
  1175. if (n) {
  1176. struct vxlan_fdb *f;
  1177. struct sk_buff *reply;
  1178. if (!(n->nud_state & NUD_CONNECTED)) {
  1179. neigh_release(n);
  1180. goto out;
  1181. }
  1182. f = vxlan_find_mac(vxlan, n->ha);
  1183. if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
  1184. /* bridge-local neighbor */
  1185. neigh_release(n);
  1186. goto out;
  1187. }
  1188. reply = vxlan_na_create(skb, n,
  1189. !!(f ? f->flags & NTF_ROUTER : 0));
  1190. neigh_release(n);
  1191. if (reply == NULL)
  1192. goto out;
  1193. if (netif_rx_ni(reply) == NET_RX_DROP)
  1194. dev->stats.rx_dropped++;
  1195. } else if (vxlan->flags & VXLAN_F_L3MISS) {
  1196. union vxlan_addr ipa = {
  1197. .sin6.sin6_addr = msg->target,
  1198. .sin6.sin6_family = AF_INET6,
  1199. };
  1200. vxlan_ip_miss(dev, &ipa);
  1201. }
  1202. out:
  1203. consume_skb(skb);
  1204. return NETDEV_TX_OK;
  1205. }
  1206. #endif
  1207. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  1208. {
  1209. struct vxlan_dev *vxlan = netdev_priv(dev);
  1210. struct neighbour *n;
  1211. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  1212. return false;
  1213. n = NULL;
  1214. switch (ntohs(eth_hdr(skb)->h_proto)) {
  1215. case ETH_P_IP:
  1216. {
  1217. struct iphdr *pip;
  1218. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  1219. return false;
  1220. pip = ip_hdr(skb);
  1221. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  1222. if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
  1223. union vxlan_addr ipa = {
  1224. .sin.sin_addr.s_addr = pip->daddr,
  1225. .sin.sin_family = AF_INET,
  1226. };
  1227. vxlan_ip_miss(dev, &ipa);
  1228. return false;
  1229. }
  1230. break;
  1231. }
  1232. #if IS_ENABLED(CONFIG_IPV6)
  1233. case ETH_P_IPV6:
  1234. {
  1235. struct ipv6hdr *pip6;
  1236. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  1237. return false;
  1238. pip6 = ipv6_hdr(skb);
  1239. n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
  1240. if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
  1241. union vxlan_addr ipa = {
  1242. .sin6.sin6_addr = pip6->daddr,
  1243. .sin6.sin6_family = AF_INET6,
  1244. };
  1245. vxlan_ip_miss(dev, &ipa);
  1246. return false;
  1247. }
  1248. break;
  1249. }
  1250. #endif
  1251. default:
  1252. return false;
  1253. }
  1254. if (n) {
  1255. bool diff;
  1256. diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
  1257. if (diff) {
  1258. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  1259. dev->addr_len);
  1260. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  1261. }
  1262. neigh_release(n);
  1263. return diff;
  1264. }
  1265. return false;
  1266. }
  1267. #if IS_ENABLED(CONFIG_IPV6)
  1268. static int vxlan6_xmit_skb(struct vxlan_sock *vs,
  1269. struct dst_entry *dst, struct sk_buff *skb,
  1270. struct net_device *dev, struct in6_addr *saddr,
  1271. struct in6_addr *daddr, __u8 prio, __u8 ttl,
  1272. __be16 src_port, __be16 dst_port, __be32 vni,
  1273. bool xnet)
  1274. {
  1275. struct vxlanhdr *vxh;
  1276. int min_headroom;
  1277. int err;
  1278. bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk);
  1279. skb = udp_tunnel_handle_offloads(skb, udp_sum);
  1280. if (IS_ERR(skb)) {
  1281. err = -EINVAL;
  1282. goto err;
  1283. }
  1284. skb_scrub_packet(skb, xnet);
  1285. min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
  1286. + VXLAN_HLEN + sizeof(struct ipv6hdr)
  1287. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  1288. /* Need space for new headers (invalidates iph ptr) */
  1289. err = skb_cow_head(skb, min_headroom);
  1290. if (unlikely(err)) {
  1291. kfree_skb(skb);
  1292. goto err;
  1293. }
  1294. skb = vlan_hwaccel_push_inside(skb);
  1295. if (WARN_ON(!skb)) {
  1296. err = -ENOMEM;
  1297. goto err;
  1298. }
  1299. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  1300. vxh->vx_flags = htonl(VXLAN_FLAGS);
  1301. vxh->vx_vni = vni;
  1302. skb_set_inner_protocol(skb, htons(ETH_P_TEB));
  1303. udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio,
  1304. ttl, src_port, dst_port);
  1305. return 0;
  1306. err:
  1307. dst_release(dst);
  1308. return err;
  1309. }
  1310. #endif
  1311. int vxlan_xmit_skb(struct vxlan_sock *vs,
  1312. struct rtable *rt, struct sk_buff *skb,
  1313. __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
  1314. __be16 src_port, __be16 dst_port, __be32 vni, bool xnet)
  1315. {
  1316. struct vxlanhdr *vxh;
  1317. int min_headroom;
  1318. int err;
  1319. bool udp_sum = !vs->sock->sk->sk_no_check_tx;
  1320. skb = udp_tunnel_handle_offloads(skb, udp_sum);
  1321. if (IS_ERR(skb))
  1322. return PTR_ERR(skb);
  1323. min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
  1324. + VXLAN_HLEN + sizeof(struct iphdr)
  1325. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  1326. /* Need space for new headers (invalidates iph ptr) */
  1327. err = skb_cow_head(skb, min_headroom);
  1328. if (unlikely(err)) {
  1329. kfree_skb(skb);
  1330. return err;
  1331. }
  1332. skb = vlan_hwaccel_push_inside(skb);
  1333. if (WARN_ON(!skb))
  1334. return -ENOMEM;
  1335. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  1336. vxh->vx_flags = htonl(VXLAN_FLAGS);
  1337. vxh->vx_vni = vni;
  1338. skb_set_inner_protocol(skb, htons(ETH_P_TEB));
  1339. return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos,
  1340. ttl, df, src_port, dst_port, xnet);
  1341. }
  1342. EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
  1343. /* Bypass encapsulation if the destination is local */
  1344. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  1345. struct vxlan_dev *dst_vxlan)
  1346. {
  1347. struct pcpu_sw_netstats *tx_stats, *rx_stats;
  1348. union vxlan_addr loopback;
  1349. union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
  1350. struct net_device *dev = skb->dev;
  1351. int len = skb->len;
  1352. tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  1353. rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  1354. skb->pkt_type = PACKET_HOST;
  1355. skb->encapsulation = 0;
  1356. skb->dev = dst_vxlan->dev;
  1357. __skb_pull(skb, skb_network_offset(skb));
  1358. if (remote_ip->sa.sa_family == AF_INET) {
  1359. loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  1360. loopback.sa.sa_family = AF_INET;
  1361. #if IS_ENABLED(CONFIG_IPV6)
  1362. } else {
  1363. loopback.sin6.sin6_addr = in6addr_loopback;
  1364. loopback.sa.sa_family = AF_INET6;
  1365. #endif
  1366. }
  1367. if (dst_vxlan->flags & VXLAN_F_LEARN)
  1368. vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
  1369. u64_stats_update_begin(&tx_stats->syncp);
  1370. tx_stats->tx_packets++;
  1371. tx_stats->tx_bytes += len;
  1372. u64_stats_update_end(&tx_stats->syncp);
  1373. if (netif_rx(skb) == NET_RX_SUCCESS) {
  1374. u64_stats_update_begin(&rx_stats->syncp);
  1375. rx_stats->rx_packets++;
  1376. rx_stats->rx_bytes += len;
  1377. u64_stats_update_end(&rx_stats->syncp);
  1378. } else {
  1379. dev->stats.rx_dropped++;
  1380. }
  1381. }
  1382. static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  1383. struct vxlan_rdst *rdst, bool did_rsc)
  1384. {
  1385. struct vxlan_dev *vxlan = netdev_priv(dev);
  1386. struct rtable *rt = NULL;
  1387. const struct iphdr *old_iph;
  1388. struct flowi4 fl4;
  1389. union vxlan_addr *dst;
  1390. __be16 src_port = 0, dst_port;
  1391. u32 vni;
  1392. __be16 df = 0;
  1393. __u8 tos, ttl;
  1394. int err;
  1395. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
  1396. vni = rdst->remote_vni;
  1397. dst = &rdst->remote_ip;
  1398. if (vxlan_addr_any(dst)) {
  1399. if (did_rsc) {
  1400. /* short-circuited back to local bridge */
  1401. vxlan_encap_bypass(skb, vxlan, vxlan);
  1402. return;
  1403. }
  1404. goto drop;
  1405. }
  1406. old_iph = ip_hdr(skb);
  1407. ttl = vxlan->ttl;
  1408. if (!ttl && vxlan_addr_multicast(dst))
  1409. ttl = 1;
  1410. tos = vxlan->tos;
  1411. if (tos == 1)
  1412. tos = ip_tunnel_get_dsfield(old_iph, skb);
  1413. src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
  1414. vxlan->port_max, true);
  1415. if (dst->sa.sa_family == AF_INET) {
  1416. memset(&fl4, 0, sizeof(fl4));
  1417. fl4.flowi4_oif = rdst->remote_ifindex;
  1418. fl4.flowi4_tos = RT_TOS(tos);
  1419. fl4.daddr = dst->sin.sin_addr.s_addr;
  1420. fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
  1421. rt = ip_route_output_key(vxlan->net, &fl4);
  1422. if (IS_ERR(rt)) {
  1423. netdev_dbg(dev, "no route to %pI4\n",
  1424. &dst->sin.sin_addr.s_addr);
  1425. dev->stats.tx_carrier_errors++;
  1426. goto tx_error;
  1427. }
  1428. if (rt->dst.dev == dev) {
  1429. netdev_dbg(dev, "circular route to %pI4\n",
  1430. &dst->sin.sin_addr.s_addr);
  1431. dev->stats.collisions++;
  1432. goto rt_tx_error;
  1433. }
  1434. /* Bypass encapsulation if the destination is local */
  1435. if (rt->rt_flags & RTCF_LOCAL &&
  1436. !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1437. struct vxlan_dev *dst_vxlan;
  1438. ip_rt_put(rt);
  1439. dst_vxlan = vxlan_find_vni(vxlan->net, vni,
  1440. dst->sa.sa_family, dst_port);
  1441. if (!dst_vxlan)
  1442. goto tx_error;
  1443. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1444. return;
  1445. }
  1446. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  1447. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  1448. err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
  1449. fl4.saddr, dst->sin.sin_addr.s_addr,
  1450. tos, ttl, df, src_port, dst_port,
  1451. htonl(vni << 8),
  1452. !net_eq(vxlan->net, dev_net(vxlan->dev)));
  1453. if (err < 0) {
  1454. /* skb is already freed. */
  1455. skb = NULL;
  1456. goto rt_tx_error;
  1457. }
  1458. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  1459. #if IS_ENABLED(CONFIG_IPV6)
  1460. } else {
  1461. struct sock *sk = vxlan->vn_sock->sock->sk;
  1462. struct dst_entry *ndst;
  1463. struct flowi6 fl6;
  1464. u32 flags;
  1465. memset(&fl6, 0, sizeof(fl6));
  1466. fl6.flowi6_oif = rdst->remote_ifindex;
  1467. fl6.daddr = dst->sin6.sin6_addr;
  1468. fl6.saddr = vxlan->saddr.sin6.sin6_addr;
  1469. fl6.flowi6_proto = IPPROTO_UDP;
  1470. if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
  1471. netdev_dbg(dev, "no route to %pI6\n",
  1472. &dst->sin6.sin6_addr);
  1473. dev->stats.tx_carrier_errors++;
  1474. goto tx_error;
  1475. }
  1476. if (ndst->dev == dev) {
  1477. netdev_dbg(dev, "circular route to %pI6\n",
  1478. &dst->sin6.sin6_addr);
  1479. dst_release(ndst);
  1480. dev->stats.collisions++;
  1481. goto tx_error;
  1482. }
  1483. /* Bypass encapsulation if the destination is local */
  1484. flags = ((struct rt6_info *)ndst)->rt6i_flags;
  1485. if (flags & RTF_LOCAL &&
  1486. !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1487. struct vxlan_dev *dst_vxlan;
  1488. dst_release(ndst);
  1489. dst_vxlan = vxlan_find_vni(vxlan->net, vni,
  1490. dst->sa.sa_family, dst_port);
  1491. if (!dst_vxlan)
  1492. goto tx_error;
  1493. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1494. return;
  1495. }
  1496. ttl = ttl ? : ip6_dst_hoplimit(ndst);
  1497. err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
  1498. dev, &fl6.saddr, &fl6.daddr, 0, ttl,
  1499. src_port, dst_port, htonl(vni << 8),
  1500. !net_eq(vxlan->net, dev_net(vxlan->dev)));
  1501. #endif
  1502. }
  1503. return;
  1504. drop:
  1505. dev->stats.tx_dropped++;
  1506. goto tx_free;
  1507. rt_tx_error:
  1508. ip_rt_put(rt);
  1509. tx_error:
  1510. dev->stats.tx_errors++;
  1511. tx_free:
  1512. dev_kfree_skb(skb);
  1513. }
  1514. /* Transmit local packets over Vxlan
  1515. *
  1516. * Outer IP header inherits ECN and DF from inner header.
  1517. * Outer UDP destination is the VXLAN assigned port.
  1518. * source port is based on hash of flow
  1519. */
  1520. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  1521. {
  1522. struct vxlan_dev *vxlan = netdev_priv(dev);
  1523. struct ethhdr *eth;
  1524. bool did_rsc = false;
  1525. struct vxlan_rdst *rdst, *fdst = NULL;
  1526. struct vxlan_fdb *f;
  1527. skb_reset_mac_header(skb);
  1528. eth = eth_hdr(skb);
  1529. if ((vxlan->flags & VXLAN_F_PROXY)) {
  1530. if (ntohs(eth->h_proto) == ETH_P_ARP)
  1531. return arp_reduce(dev, skb);
  1532. #if IS_ENABLED(CONFIG_IPV6)
  1533. else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
  1534. pskb_may_pull(skb, sizeof(struct ipv6hdr)
  1535. + sizeof(struct nd_msg)) &&
  1536. ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
  1537. struct nd_msg *msg;
  1538. msg = (struct nd_msg *)skb_transport_header(skb);
  1539. if (msg->icmph.icmp6_code == 0 &&
  1540. msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
  1541. return neigh_reduce(dev, skb);
  1542. }
  1543. eth = eth_hdr(skb);
  1544. #endif
  1545. }
  1546. f = vxlan_find_mac(vxlan, eth->h_dest);
  1547. did_rsc = false;
  1548. if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
  1549. (ntohs(eth->h_proto) == ETH_P_IP ||
  1550. ntohs(eth->h_proto) == ETH_P_IPV6)) {
  1551. did_rsc = route_shortcircuit(dev, skb);
  1552. if (did_rsc)
  1553. f = vxlan_find_mac(vxlan, eth->h_dest);
  1554. }
  1555. if (f == NULL) {
  1556. f = vxlan_find_mac(vxlan, all_zeros_mac);
  1557. if (f == NULL) {
  1558. if ((vxlan->flags & VXLAN_F_L2MISS) &&
  1559. !is_multicast_ether_addr(eth->h_dest))
  1560. vxlan_fdb_miss(vxlan, eth->h_dest);
  1561. dev->stats.tx_dropped++;
  1562. kfree_skb(skb);
  1563. return NETDEV_TX_OK;
  1564. }
  1565. }
  1566. list_for_each_entry_rcu(rdst, &f->remotes, list) {
  1567. struct sk_buff *skb1;
  1568. if (!fdst) {
  1569. fdst = rdst;
  1570. continue;
  1571. }
  1572. skb1 = skb_clone(skb, GFP_ATOMIC);
  1573. if (skb1)
  1574. vxlan_xmit_one(skb1, dev, rdst, did_rsc);
  1575. }
  1576. if (fdst)
  1577. vxlan_xmit_one(skb, dev, fdst, did_rsc);
  1578. else
  1579. kfree_skb(skb);
  1580. return NETDEV_TX_OK;
  1581. }
  1582. /* Walk the forwarding table and purge stale entries */
  1583. static void vxlan_cleanup(unsigned long arg)
  1584. {
  1585. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  1586. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  1587. unsigned int h;
  1588. if (!netif_running(vxlan->dev))
  1589. return;
  1590. spin_lock_bh(&vxlan->hash_lock);
  1591. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1592. struct hlist_node *p, *n;
  1593. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1594. struct vxlan_fdb *f
  1595. = container_of(p, struct vxlan_fdb, hlist);
  1596. unsigned long timeout;
  1597. if (f->state & NUD_PERMANENT)
  1598. continue;
  1599. timeout = f->used + vxlan->age_interval * HZ;
  1600. if (time_before_eq(timeout, jiffies)) {
  1601. netdev_dbg(vxlan->dev,
  1602. "garbage collect %pM\n",
  1603. f->eth_addr);
  1604. f->state = NUD_STALE;
  1605. vxlan_fdb_destroy(vxlan, f);
  1606. } else if (time_before(timeout, next_timer))
  1607. next_timer = timeout;
  1608. }
  1609. }
  1610. spin_unlock_bh(&vxlan->hash_lock);
  1611. mod_timer(&vxlan->age_timer, next_timer);
  1612. }
  1613. static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
  1614. {
  1615. __u32 vni = vxlan->default_dst.remote_vni;
  1616. vxlan->vn_sock = vs;
  1617. hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
  1618. }
  1619. /* Setup stats when device is created */
  1620. static int vxlan_init(struct net_device *dev)
  1621. {
  1622. struct vxlan_dev *vxlan = netdev_priv(dev);
  1623. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  1624. struct vxlan_sock *vs;
  1625. bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
  1626. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1627. if (!dev->tstats)
  1628. return -ENOMEM;
  1629. spin_lock(&vn->sock_lock);
  1630. vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
  1631. vxlan->dst_port);
  1632. if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
  1633. /* If we have a socket with same port already, reuse it */
  1634. vxlan_vs_add_dev(vs, vxlan);
  1635. } else {
  1636. /* otherwise make new socket outside of RTNL */
  1637. dev_hold(dev);
  1638. queue_work(vxlan_wq, &vxlan->sock_work);
  1639. }
  1640. spin_unlock(&vn->sock_lock);
  1641. return 0;
  1642. }
  1643. static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
  1644. {
  1645. struct vxlan_fdb *f;
  1646. spin_lock_bh(&vxlan->hash_lock);
  1647. f = __vxlan_find_mac(vxlan, all_zeros_mac);
  1648. if (f)
  1649. vxlan_fdb_destroy(vxlan, f);
  1650. spin_unlock_bh(&vxlan->hash_lock);
  1651. }
  1652. static void vxlan_uninit(struct net_device *dev)
  1653. {
  1654. struct vxlan_dev *vxlan = netdev_priv(dev);
  1655. struct vxlan_sock *vs = vxlan->vn_sock;
  1656. vxlan_fdb_delete_default(vxlan);
  1657. if (vs)
  1658. vxlan_sock_release(vs);
  1659. free_percpu(dev->tstats);
  1660. }
  1661. /* Start ageing timer and join group when device is brought up */
  1662. static int vxlan_open(struct net_device *dev)
  1663. {
  1664. struct vxlan_dev *vxlan = netdev_priv(dev);
  1665. struct vxlan_sock *vs = vxlan->vn_sock;
  1666. /* socket hasn't been created */
  1667. if (!vs)
  1668. return -ENOTCONN;
  1669. if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
  1670. vxlan_sock_hold(vs);
  1671. dev_hold(dev);
  1672. queue_work(vxlan_wq, &vxlan->igmp_join);
  1673. }
  1674. if (vxlan->age_interval)
  1675. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  1676. return 0;
  1677. }
  1678. /* Purge the forwarding table */
  1679. static void vxlan_flush(struct vxlan_dev *vxlan)
  1680. {
  1681. unsigned int h;
  1682. spin_lock_bh(&vxlan->hash_lock);
  1683. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1684. struct hlist_node *p, *n;
  1685. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1686. struct vxlan_fdb *f
  1687. = container_of(p, struct vxlan_fdb, hlist);
  1688. /* the all_zeros_mac entry is deleted at vxlan_uninit */
  1689. if (!is_zero_ether_addr(f->eth_addr))
  1690. vxlan_fdb_destroy(vxlan, f);
  1691. }
  1692. }
  1693. spin_unlock_bh(&vxlan->hash_lock);
  1694. }
  1695. /* Cleanup timer and forwarding table on shutdown */
  1696. static int vxlan_stop(struct net_device *dev)
  1697. {
  1698. struct vxlan_dev *vxlan = netdev_priv(dev);
  1699. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  1700. struct vxlan_sock *vs = vxlan->vn_sock;
  1701. if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
  1702. !vxlan_group_used(vn, vxlan)) {
  1703. vxlan_sock_hold(vs);
  1704. dev_hold(dev);
  1705. queue_work(vxlan_wq, &vxlan->igmp_leave);
  1706. }
  1707. del_timer_sync(&vxlan->age_timer);
  1708. vxlan_flush(vxlan);
  1709. return 0;
  1710. }
  1711. /* Stub, nothing needs to be done. */
  1712. static void vxlan_set_multicast_list(struct net_device *dev)
  1713. {
  1714. }
  1715. static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
  1716. {
  1717. struct vxlan_dev *vxlan = netdev_priv(dev);
  1718. struct vxlan_rdst *dst = &vxlan->default_dst;
  1719. struct net_device *lowerdev;
  1720. int max_mtu;
  1721. lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
  1722. if (lowerdev == NULL)
  1723. return eth_change_mtu(dev, new_mtu);
  1724. if (dst->remote_ip.sa.sa_family == AF_INET6)
  1725. max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
  1726. else
  1727. max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1728. if (new_mtu < 68 || new_mtu > max_mtu)
  1729. return -EINVAL;
  1730. dev->mtu = new_mtu;
  1731. return 0;
  1732. }
  1733. static const struct net_device_ops vxlan_netdev_ops = {
  1734. .ndo_init = vxlan_init,
  1735. .ndo_uninit = vxlan_uninit,
  1736. .ndo_open = vxlan_open,
  1737. .ndo_stop = vxlan_stop,
  1738. .ndo_start_xmit = vxlan_xmit,
  1739. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1740. .ndo_set_rx_mode = vxlan_set_multicast_list,
  1741. .ndo_change_mtu = vxlan_change_mtu,
  1742. .ndo_validate_addr = eth_validate_addr,
  1743. .ndo_set_mac_address = eth_mac_addr,
  1744. .ndo_fdb_add = vxlan_fdb_add,
  1745. .ndo_fdb_del = vxlan_fdb_delete,
  1746. .ndo_fdb_dump = vxlan_fdb_dump,
  1747. };
  1748. /* Info for udev, that this is a virtual tunnel endpoint */
  1749. static struct device_type vxlan_type = {
  1750. .name = "vxlan",
  1751. };
  1752. /* Calls the ndo_add_vxlan_port of the caller in order to
  1753. * supply the listening VXLAN udp ports. Callers are expected
  1754. * to implement the ndo_add_vxlan_port.
  1755. */
  1756. void vxlan_get_rx_port(struct net_device *dev)
  1757. {
  1758. struct vxlan_sock *vs;
  1759. struct net *net = dev_net(dev);
  1760. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1761. sa_family_t sa_family;
  1762. __be16 port;
  1763. unsigned int i;
  1764. spin_lock(&vn->sock_lock);
  1765. for (i = 0; i < PORT_HASH_SIZE; ++i) {
  1766. hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
  1767. port = inet_sk(vs->sock->sk)->inet_sport;
  1768. sa_family = vs->sock->sk->sk_family;
  1769. dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
  1770. port);
  1771. }
  1772. }
  1773. spin_unlock(&vn->sock_lock);
  1774. }
  1775. EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
  1776. /* Initialize the device structure. */
  1777. static void vxlan_setup(struct net_device *dev)
  1778. {
  1779. struct vxlan_dev *vxlan = netdev_priv(dev);
  1780. unsigned int h;
  1781. eth_hw_addr_random(dev);
  1782. ether_setup(dev);
  1783. if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
  1784. dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
  1785. else
  1786. dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
  1787. dev->netdev_ops = &vxlan_netdev_ops;
  1788. dev->destructor = free_netdev;
  1789. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  1790. dev->tx_queue_len = 0;
  1791. dev->features |= NETIF_F_LLTX;
  1792. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1793. dev->features |= NETIF_F_RXCSUM;
  1794. dev->features |= NETIF_F_GSO_SOFTWARE;
  1795. dev->vlan_features = dev->features;
  1796. dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1797. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1798. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1799. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1800. netif_keep_dst(dev);
  1801. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1802. INIT_LIST_HEAD(&vxlan->next);
  1803. spin_lock_init(&vxlan->hash_lock);
  1804. INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
  1805. INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
  1806. INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
  1807. init_timer_deferrable(&vxlan->age_timer);
  1808. vxlan->age_timer.function = vxlan_cleanup;
  1809. vxlan->age_timer.data = (unsigned long) vxlan;
  1810. vxlan->dst_port = htons(vxlan_port);
  1811. vxlan->dev = dev;
  1812. for (h = 0; h < FDB_HASH_SIZE; ++h)
  1813. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  1814. }
  1815. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  1816. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1817. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1818. [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
  1819. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1820. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1821. [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
  1822. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1823. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1824. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1825. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1826. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1827. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1828. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1829. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1830. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1831. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1832. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  1833. [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
  1834. [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
  1835. [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
  1836. };
  1837. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1838. {
  1839. if (tb[IFLA_ADDRESS]) {
  1840. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1841. pr_debug("invalid link address (not ethernet)\n");
  1842. return -EINVAL;
  1843. }
  1844. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1845. pr_debug("invalid all zero ethernet address\n");
  1846. return -EADDRNOTAVAIL;
  1847. }
  1848. }
  1849. if (!data)
  1850. return -EINVAL;
  1851. if (data[IFLA_VXLAN_ID]) {
  1852. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1853. if (id >= VXLAN_VID_MASK)
  1854. return -ERANGE;
  1855. }
  1856. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1857. const struct ifla_vxlan_port_range *p
  1858. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1859. if (ntohs(p->high) < ntohs(p->low)) {
  1860. pr_debug("port range %u .. %u not valid\n",
  1861. ntohs(p->low), ntohs(p->high));
  1862. return -EINVAL;
  1863. }
  1864. }
  1865. return 0;
  1866. }
  1867. static void vxlan_get_drvinfo(struct net_device *netdev,
  1868. struct ethtool_drvinfo *drvinfo)
  1869. {
  1870. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  1871. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  1872. }
  1873. static const struct ethtool_ops vxlan_ethtool_ops = {
  1874. .get_drvinfo = vxlan_get_drvinfo,
  1875. .get_link = ethtool_op_get_link,
  1876. };
  1877. static void vxlan_del_work(struct work_struct *work)
  1878. {
  1879. struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
  1880. udp_tunnel_sock_release(vs->sock);
  1881. kfree_rcu(vs, rcu);
  1882. }
  1883. static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
  1884. __be16 port, u32 flags)
  1885. {
  1886. struct socket *sock;
  1887. struct udp_port_cfg udp_conf;
  1888. int err;
  1889. memset(&udp_conf, 0, sizeof(udp_conf));
  1890. if (ipv6) {
  1891. udp_conf.family = AF_INET6;
  1892. udp_conf.use_udp6_tx_checksums =
  1893. !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
  1894. udp_conf.use_udp6_rx_checksums =
  1895. !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
  1896. } else {
  1897. udp_conf.family = AF_INET;
  1898. udp_conf.local_ip.s_addr = INADDR_ANY;
  1899. udp_conf.use_udp_checksums =
  1900. !!(flags & VXLAN_F_UDP_CSUM);
  1901. }
  1902. udp_conf.local_udp_port = port;
  1903. /* Open UDP socket */
  1904. err = udp_sock_create(net, &udp_conf, &sock);
  1905. if (err < 0)
  1906. return ERR_PTR(err);
  1907. return sock;
  1908. }
  1909. /* Create new listen socket if needed */
  1910. static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
  1911. vxlan_rcv_t *rcv, void *data,
  1912. u32 flags)
  1913. {
  1914. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1915. struct vxlan_sock *vs;
  1916. struct socket *sock;
  1917. unsigned int h;
  1918. bool ipv6 = !!(flags & VXLAN_F_IPV6);
  1919. struct udp_tunnel_sock_cfg tunnel_cfg;
  1920. vs = kzalloc(sizeof(*vs), GFP_KERNEL);
  1921. if (!vs)
  1922. return ERR_PTR(-ENOMEM);
  1923. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1924. INIT_HLIST_HEAD(&vs->vni_list[h]);
  1925. INIT_WORK(&vs->del_work, vxlan_del_work);
  1926. sock = vxlan_create_sock(net, ipv6, port, flags);
  1927. if (IS_ERR(sock)) {
  1928. kfree(vs);
  1929. return ERR_CAST(sock);
  1930. }
  1931. vs->sock = sock;
  1932. atomic_set(&vs->refcnt, 1);
  1933. vs->rcv = rcv;
  1934. vs->data = data;
  1935. /* Initialize the vxlan udp offloads structure */
  1936. vs->udp_offloads.port = port;
  1937. vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
  1938. vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
  1939. spin_lock(&vn->sock_lock);
  1940. hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
  1941. vxlan_notify_add_rx_port(vs);
  1942. spin_unlock(&vn->sock_lock);
  1943. /* Mark socket as an encapsulation socket. */
  1944. tunnel_cfg.sk_user_data = vs;
  1945. tunnel_cfg.encap_type = 1;
  1946. tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
  1947. tunnel_cfg.encap_destroy = NULL;
  1948. setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
  1949. return vs;
  1950. }
  1951. struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
  1952. vxlan_rcv_t *rcv, void *data,
  1953. bool no_share, u32 flags)
  1954. {
  1955. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1956. struct vxlan_sock *vs;
  1957. bool ipv6 = flags & VXLAN_F_IPV6;
  1958. vs = vxlan_socket_create(net, port, rcv, data, flags);
  1959. if (!IS_ERR(vs))
  1960. return vs;
  1961. if (no_share) /* Return error if sharing is not allowed. */
  1962. return vs;
  1963. spin_lock(&vn->sock_lock);
  1964. vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port);
  1965. if (vs && ((vs->rcv != rcv) ||
  1966. !atomic_add_unless(&vs->refcnt, 1, 0)))
  1967. vs = ERR_PTR(-EBUSY);
  1968. spin_unlock(&vn->sock_lock);
  1969. if (!vs)
  1970. vs = ERR_PTR(-EINVAL);
  1971. return vs;
  1972. }
  1973. EXPORT_SYMBOL_GPL(vxlan_sock_add);
  1974. /* Scheduled at device creation to bind to a socket */
  1975. static void vxlan_sock_work(struct work_struct *work)
  1976. {
  1977. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
  1978. struct net *net = vxlan->net;
  1979. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1980. __be16 port = vxlan->dst_port;
  1981. struct vxlan_sock *nvs;
  1982. nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
  1983. spin_lock(&vn->sock_lock);
  1984. if (!IS_ERR(nvs))
  1985. vxlan_vs_add_dev(nvs, vxlan);
  1986. spin_unlock(&vn->sock_lock);
  1987. dev_put(vxlan->dev);
  1988. }
  1989. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1990. struct nlattr *tb[], struct nlattr *data[])
  1991. {
  1992. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1993. struct vxlan_dev *vxlan = netdev_priv(dev);
  1994. struct vxlan_rdst *dst = &vxlan->default_dst;
  1995. __u32 vni;
  1996. int err;
  1997. bool use_ipv6 = false;
  1998. if (!data[IFLA_VXLAN_ID])
  1999. return -EINVAL;
  2000. vxlan->net = dev_net(dev);
  2001. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  2002. dst->remote_vni = vni;
  2003. /* Unless IPv6 is explicitly requested, assume IPv4 */
  2004. dst->remote_ip.sa.sa_family = AF_INET;
  2005. if (data[IFLA_VXLAN_GROUP]) {
  2006. dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  2007. } else if (data[IFLA_VXLAN_GROUP6]) {
  2008. if (!IS_ENABLED(CONFIG_IPV6))
  2009. return -EPFNOSUPPORT;
  2010. nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
  2011. sizeof(struct in6_addr));
  2012. dst->remote_ip.sa.sa_family = AF_INET6;
  2013. use_ipv6 = true;
  2014. }
  2015. if (data[IFLA_VXLAN_LOCAL]) {
  2016. vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  2017. vxlan->saddr.sa.sa_family = AF_INET;
  2018. } else if (data[IFLA_VXLAN_LOCAL6]) {
  2019. if (!IS_ENABLED(CONFIG_IPV6))
  2020. return -EPFNOSUPPORT;
  2021. /* TODO: respect scope id */
  2022. nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
  2023. sizeof(struct in6_addr));
  2024. vxlan->saddr.sa.sa_family = AF_INET6;
  2025. use_ipv6 = true;
  2026. }
  2027. if (data[IFLA_VXLAN_LINK] &&
  2028. (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  2029. struct net_device *lowerdev
  2030. = __dev_get_by_index(net, dst->remote_ifindex);
  2031. if (!lowerdev) {
  2032. pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
  2033. return -ENODEV;
  2034. }
  2035. #if IS_ENABLED(CONFIG_IPV6)
  2036. if (use_ipv6) {
  2037. struct inet6_dev *idev = __in6_dev_get(lowerdev);
  2038. if (idev && idev->cnf.disable_ipv6) {
  2039. pr_info("IPv6 is disabled via sysctl\n");
  2040. return -EPERM;
  2041. }
  2042. vxlan->flags |= VXLAN_F_IPV6;
  2043. }
  2044. #endif
  2045. if (!tb[IFLA_MTU])
  2046. dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
  2047. dev->needed_headroom = lowerdev->hard_header_len +
  2048. (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
  2049. } else if (use_ipv6)
  2050. vxlan->flags |= VXLAN_F_IPV6;
  2051. if (data[IFLA_VXLAN_TOS])
  2052. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  2053. if (data[IFLA_VXLAN_TTL])
  2054. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  2055. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  2056. vxlan->flags |= VXLAN_F_LEARN;
  2057. if (data[IFLA_VXLAN_AGEING])
  2058. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  2059. else
  2060. vxlan->age_interval = FDB_AGE_DEFAULT;
  2061. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  2062. vxlan->flags |= VXLAN_F_PROXY;
  2063. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  2064. vxlan->flags |= VXLAN_F_RSC;
  2065. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  2066. vxlan->flags |= VXLAN_F_L2MISS;
  2067. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  2068. vxlan->flags |= VXLAN_F_L3MISS;
  2069. if (data[IFLA_VXLAN_LIMIT])
  2070. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  2071. if (data[IFLA_VXLAN_PORT_RANGE]) {
  2072. const struct ifla_vxlan_port_range *p
  2073. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  2074. vxlan->port_min = ntohs(p->low);
  2075. vxlan->port_max = ntohs(p->high);
  2076. }
  2077. if (data[IFLA_VXLAN_PORT])
  2078. vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  2079. if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
  2080. vxlan->flags |= VXLAN_F_UDP_CSUM;
  2081. if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
  2082. nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
  2083. vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
  2084. if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
  2085. nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
  2086. vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
  2087. if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
  2088. vxlan->dst_port)) {
  2089. pr_info("duplicate VNI %u\n", vni);
  2090. return -EEXIST;
  2091. }
  2092. dev->ethtool_ops = &vxlan_ethtool_ops;
  2093. /* create an fdb entry for a valid default destination */
  2094. if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
  2095. err = vxlan_fdb_create(vxlan, all_zeros_mac,
  2096. &vxlan->default_dst.remote_ip,
  2097. NUD_REACHABLE|NUD_PERMANENT,
  2098. NLM_F_EXCL|NLM_F_CREATE,
  2099. vxlan->dst_port,
  2100. vxlan->default_dst.remote_vni,
  2101. vxlan->default_dst.remote_ifindex,
  2102. NTF_SELF);
  2103. if (err)
  2104. return err;
  2105. }
  2106. err = register_netdevice(dev);
  2107. if (err) {
  2108. vxlan_fdb_delete_default(vxlan);
  2109. return err;
  2110. }
  2111. list_add(&vxlan->next, &vn->vxlan_list);
  2112. return 0;
  2113. }
  2114. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  2115. {
  2116. struct vxlan_dev *vxlan = netdev_priv(dev);
  2117. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  2118. spin_lock(&vn->sock_lock);
  2119. if (!hlist_unhashed(&vxlan->hlist))
  2120. hlist_del_rcu(&vxlan->hlist);
  2121. spin_unlock(&vn->sock_lock);
  2122. list_del(&vxlan->next);
  2123. unregister_netdevice_queue(dev, head);
  2124. }
  2125. static size_t vxlan_get_size(const struct net_device *dev)
  2126. {
  2127. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  2128. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
  2129. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  2130. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
  2131. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  2132. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  2133. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  2134. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  2135. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  2136. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  2137. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  2138. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  2139. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  2140. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  2141. nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
  2142. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
  2143. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
  2144. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
  2145. 0;
  2146. }
  2147. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  2148. {
  2149. const struct vxlan_dev *vxlan = netdev_priv(dev);
  2150. const struct vxlan_rdst *dst = &vxlan->default_dst;
  2151. struct ifla_vxlan_port_range ports = {
  2152. .low = htons(vxlan->port_min),
  2153. .high = htons(vxlan->port_max),
  2154. };
  2155. if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
  2156. goto nla_put_failure;
  2157. if (!vxlan_addr_any(&dst->remote_ip)) {
  2158. if (dst->remote_ip.sa.sa_family == AF_INET) {
  2159. if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
  2160. dst->remote_ip.sin.sin_addr.s_addr))
  2161. goto nla_put_failure;
  2162. #if IS_ENABLED(CONFIG_IPV6)
  2163. } else {
  2164. if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
  2165. &dst->remote_ip.sin6.sin6_addr))
  2166. goto nla_put_failure;
  2167. #endif
  2168. }
  2169. }
  2170. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  2171. goto nla_put_failure;
  2172. if (!vxlan_addr_any(&vxlan->saddr)) {
  2173. if (vxlan->saddr.sa.sa_family == AF_INET) {
  2174. if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
  2175. vxlan->saddr.sin.sin_addr.s_addr))
  2176. goto nla_put_failure;
  2177. #if IS_ENABLED(CONFIG_IPV6)
  2178. } else {
  2179. if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
  2180. &vxlan->saddr.sin6.sin6_addr))
  2181. goto nla_put_failure;
  2182. #endif
  2183. }
  2184. }
  2185. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  2186. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  2187. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  2188. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  2189. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  2190. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  2191. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  2192. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  2193. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  2194. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  2195. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  2196. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  2197. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
  2198. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
  2199. nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
  2200. !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
  2201. nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
  2202. !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
  2203. nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
  2204. !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)))
  2205. goto nla_put_failure;
  2206. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  2207. goto nla_put_failure;
  2208. return 0;
  2209. nla_put_failure:
  2210. return -EMSGSIZE;
  2211. }
  2212. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  2213. .kind = "vxlan",
  2214. .maxtype = IFLA_VXLAN_MAX,
  2215. .policy = vxlan_policy,
  2216. .priv_size = sizeof(struct vxlan_dev),
  2217. .setup = vxlan_setup,
  2218. .validate = vxlan_validate,
  2219. .newlink = vxlan_newlink,
  2220. .dellink = vxlan_dellink,
  2221. .get_size = vxlan_get_size,
  2222. .fill_info = vxlan_fill_info,
  2223. };
  2224. static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
  2225. struct net_device *dev)
  2226. {
  2227. struct vxlan_dev *vxlan, *next;
  2228. LIST_HEAD(list_kill);
  2229. list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
  2230. struct vxlan_rdst *dst = &vxlan->default_dst;
  2231. /* In case we created vxlan device with carrier
  2232. * and we loose the carrier due to module unload
  2233. * we also need to remove vxlan device. In other
  2234. * cases, it's not necessary and remote_ifindex
  2235. * is 0 here, so no matches.
  2236. */
  2237. if (dst->remote_ifindex == dev->ifindex)
  2238. vxlan_dellink(vxlan->dev, &list_kill);
  2239. }
  2240. unregister_netdevice_many(&list_kill);
  2241. }
  2242. static int vxlan_lowerdev_event(struct notifier_block *unused,
  2243. unsigned long event, void *ptr)
  2244. {
  2245. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2246. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  2247. if (event == NETDEV_UNREGISTER)
  2248. vxlan_handle_lowerdev_unregister(vn, dev);
  2249. return NOTIFY_DONE;
  2250. }
  2251. static struct notifier_block vxlan_notifier_block __read_mostly = {
  2252. .notifier_call = vxlan_lowerdev_event,
  2253. };
  2254. static __net_init int vxlan_init_net(struct net *net)
  2255. {
  2256. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2257. unsigned int h;
  2258. INIT_LIST_HEAD(&vn->vxlan_list);
  2259. spin_lock_init(&vn->sock_lock);
  2260. for (h = 0; h < PORT_HASH_SIZE; ++h)
  2261. INIT_HLIST_HEAD(&vn->sock_list[h]);
  2262. return 0;
  2263. }
  2264. static void __net_exit vxlan_exit_net(struct net *net)
  2265. {
  2266. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2267. struct vxlan_dev *vxlan, *next;
  2268. struct net_device *dev, *aux;
  2269. LIST_HEAD(list);
  2270. rtnl_lock();
  2271. for_each_netdev_safe(net, dev, aux)
  2272. if (dev->rtnl_link_ops == &vxlan_link_ops)
  2273. unregister_netdevice_queue(dev, &list);
  2274. list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
  2275. /* If vxlan->dev is in the same netns, it has already been added
  2276. * to the list by the previous loop.
  2277. */
  2278. if (!net_eq(dev_net(vxlan->dev), net))
  2279. unregister_netdevice_queue(dev, &list);
  2280. }
  2281. unregister_netdevice_many(&list);
  2282. rtnl_unlock();
  2283. }
  2284. static struct pernet_operations vxlan_net_ops = {
  2285. .init = vxlan_init_net,
  2286. .exit = vxlan_exit_net,
  2287. .id = &vxlan_net_id,
  2288. .size = sizeof(struct vxlan_net),
  2289. };
  2290. static int __init vxlan_init_module(void)
  2291. {
  2292. int rc;
  2293. vxlan_wq = alloc_workqueue("vxlan", 0, 0);
  2294. if (!vxlan_wq)
  2295. return -ENOMEM;
  2296. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  2297. rc = register_pernet_subsys(&vxlan_net_ops);
  2298. if (rc)
  2299. goto out1;
  2300. rc = register_netdevice_notifier(&vxlan_notifier_block);
  2301. if (rc)
  2302. goto out2;
  2303. rc = rtnl_link_register(&vxlan_link_ops);
  2304. if (rc)
  2305. goto out3;
  2306. return 0;
  2307. out3:
  2308. unregister_netdevice_notifier(&vxlan_notifier_block);
  2309. out2:
  2310. unregister_pernet_subsys(&vxlan_net_ops);
  2311. out1:
  2312. destroy_workqueue(vxlan_wq);
  2313. return rc;
  2314. }
  2315. late_initcall(vxlan_init_module);
  2316. static void __exit vxlan_cleanup_module(void)
  2317. {
  2318. rtnl_link_unregister(&vxlan_link_ops);
  2319. unregister_netdevice_notifier(&vxlan_notifier_block);
  2320. destroy_workqueue(vxlan_wq);
  2321. unregister_pernet_subsys(&vxlan_net_ops);
  2322. /* rcu_barrier() is called by netns */
  2323. }
  2324. module_exit(vxlan_cleanup_module);
  2325. MODULE_LICENSE("GPL");
  2326. MODULE_VERSION(VXLAN_VERSION);
  2327. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  2328. MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
  2329. MODULE_ALIAS_RTNL_LINK("vxlan");