vxlan.c 75 KB

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