vxlan.c 85 KB

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