vxlan.c 80 KB

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