vxlan.c 80 KB

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