vxlan.c 83 KB

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