vxlan.c 86 KB

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