vxlan.c 114 KB

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