vxlan.c 67 KB

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