vxlan.c 98 KB

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