vxlan.c 85 KB

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