vxlan.c 90 KB

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