vxlan.c 84 KB

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