vxlan.c 91 KB

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