vxlan.c 84 KB

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