vxlan.c 82 KB

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