vxlan.c 77 KB

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