vxlan.c 96 KB

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