vxlan.c 82 KB

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