vxlan.c 96 KB

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