vxlan.c 84 KB

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