vxlan.c 91 KB

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