vxlan.c 79 KB

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