vxlan.c 121 KB

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