vxlan.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705
  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. rcu_assign_sk_user_data(vs->sock->sk, NULL);
  801. vxlan_notify_del_rx_port(sk);
  802. spin_unlock(&vn->sock_lock);
  803. queue_work(vxlan_wq, &vs->del_work);
  804. }
  805. EXPORT_SYMBOL_GPL(vxlan_sock_release);
  806. /* Callback to update multicast group membership when first VNI on
  807. * multicast asddress is brought up
  808. * Done as workqueue because ip_mc_join_group acquires RTNL.
  809. */
  810. static void vxlan_igmp_join(struct work_struct *work)
  811. {
  812. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
  813. struct vxlan_sock *vs = vxlan->vn_sock;
  814. struct sock *sk = vs->sock->sk;
  815. union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
  816. int ifindex = vxlan->default_dst.remote_ifindex;
  817. lock_sock(sk);
  818. if (ip->sa.sa_family == AF_INET) {
  819. struct ip_mreqn mreq = {
  820. .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
  821. .imr_ifindex = ifindex,
  822. };
  823. ip_mc_join_group(sk, &mreq);
  824. #if IS_ENABLED(CONFIG_IPV6)
  825. } else {
  826. ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
  827. &ip->sin6.sin6_addr);
  828. #endif
  829. }
  830. release_sock(sk);
  831. vxlan_sock_release(vs);
  832. dev_put(vxlan->dev);
  833. }
  834. /* Inverse of vxlan_igmp_join when last VNI is brought down */
  835. static void vxlan_igmp_leave(struct work_struct *work)
  836. {
  837. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
  838. struct vxlan_sock *vs = vxlan->vn_sock;
  839. struct sock *sk = vs->sock->sk;
  840. union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
  841. int ifindex = vxlan->default_dst.remote_ifindex;
  842. lock_sock(sk);
  843. if (ip->sa.sa_family == AF_INET) {
  844. struct ip_mreqn mreq = {
  845. .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
  846. .imr_ifindex = ifindex,
  847. };
  848. ip_mc_leave_group(sk, &mreq);
  849. #if IS_ENABLED(CONFIG_IPV6)
  850. } else {
  851. ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
  852. &ip->sin6.sin6_addr);
  853. #endif
  854. }
  855. release_sock(sk);
  856. vxlan_sock_release(vs);
  857. dev_put(vxlan->dev);
  858. }
  859. /* Callback from net/ipv4/udp.c to receive packets */
  860. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  861. {
  862. struct vxlan_sock *vs;
  863. struct vxlanhdr *vxh;
  864. __be16 port;
  865. /* Need Vxlan and inner Ethernet header to be present */
  866. if (!pskb_may_pull(skb, VXLAN_HLEN))
  867. goto error;
  868. /* Return packets with reserved bits set */
  869. vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
  870. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  871. (vxh->vx_vni & htonl(0xff))) {
  872. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  873. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  874. goto error;
  875. }
  876. if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
  877. goto drop;
  878. port = inet_sk(sk)->inet_sport;
  879. vs = rcu_dereference_sk_user_data(sk);
  880. if (!vs)
  881. goto drop;
  882. vs->rcv(vs, skb, vxh->vx_vni);
  883. return 0;
  884. drop:
  885. /* Consume bad packet */
  886. kfree_skb(skb);
  887. return 0;
  888. error:
  889. /* Return non vxlan pkt */
  890. return 1;
  891. }
  892. static void vxlan_rcv(struct vxlan_sock *vs,
  893. struct sk_buff *skb, __be32 vx_vni)
  894. {
  895. struct iphdr *oip = NULL;
  896. struct ipv6hdr *oip6 = NULL;
  897. struct vxlan_dev *vxlan;
  898. struct pcpu_tstats *stats;
  899. union vxlan_addr saddr;
  900. __u32 vni;
  901. int err = 0;
  902. union vxlan_addr *remote_ip;
  903. vni = ntohl(vx_vni) >> 8;
  904. /* Is this VNI defined? */
  905. vxlan = vxlan_vs_find_vni(vs, vni);
  906. if (!vxlan)
  907. goto drop;
  908. remote_ip = &vxlan->default_dst.remote_ip;
  909. skb_reset_mac_header(skb);
  910. skb->protocol = eth_type_trans(skb, vxlan->dev);
  911. /* Ignore packet loops (and multicast echo) */
  912. if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
  913. goto drop;
  914. /* Re-examine inner Ethernet packet */
  915. if (remote_ip->sa.sa_family == AF_INET) {
  916. oip = ip_hdr(skb);
  917. saddr.sin.sin_addr.s_addr = oip->saddr;
  918. saddr.sa.sa_family = AF_INET;
  919. #if IS_ENABLED(CONFIG_IPV6)
  920. } else {
  921. oip6 = ipv6_hdr(skb);
  922. saddr.sin6.sin6_addr = oip6->saddr;
  923. saddr.sa.sa_family = AF_INET6;
  924. #endif
  925. }
  926. if ((vxlan->flags & VXLAN_F_LEARN) &&
  927. vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
  928. goto drop;
  929. skb_reset_network_header(skb);
  930. /* If the NIC driver gave us an encapsulated packet with
  931. * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
  932. * leave the CHECKSUM_UNNECESSARY, the device checksummed it
  933. * for us. Otherwise force the upper layers to verify it.
  934. */
  935. if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
  936. !(vxlan->dev->features & NETIF_F_RXCSUM))
  937. skb->ip_summed = CHECKSUM_NONE;
  938. skb->encapsulation = 0;
  939. if (oip6)
  940. err = IP6_ECN_decapsulate(oip6, skb);
  941. if (oip)
  942. err = IP_ECN_decapsulate(oip, skb);
  943. if (unlikely(err)) {
  944. if (log_ecn_error) {
  945. if (oip6)
  946. net_info_ratelimited("non-ECT from %pI6\n",
  947. &oip6->saddr);
  948. if (oip)
  949. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  950. &oip->saddr, oip->tos);
  951. }
  952. if (err > 1) {
  953. ++vxlan->dev->stats.rx_frame_errors;
  954. ++vxlan->dev->stats.rx_errors;
  955. goto drop;
  956. }
  957. }
  958. stats = this_cpu_ptr(vxlan->dev->tstats);
  959. u64_stats_update_begin(&stats->syncp);
  960. stats->rx_packets++;
  961. stats->rx_bytes += skb->len;
  962. u64_stats_update_end(&stats->syncp);
  963. netif_rx(skb);
  964. return;
  965. drop:
  966. /* Consume bad packet */
  967. kfree_skb(skb);
  968. }
  969. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  970. {
  971. struct vxlan_dev *vxlan = netdev_priv(dev);
  972. struct arphdr *parp;
  973. u8 *arpptr, *sha;
  974. __be32 sip, tip;
  975. struct neighbour *n;
  976. if (dev->flags & IFF_NOARP)
  977. goto out;
  978. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  979. dev->stats.tx_dropped++;
  980. goto out;
  981. }
  982. parp = arp_hdr(skb);
  983. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  984. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  985. parp->ar_pro != htons(ETH_P_IP) ||
  986. parp->ar_op != htons(ARPOP_REQUEST) ||
  987. parp->ar_hln != dev->addr_len ||
  988. parp->ar_pln != 4)
  989. goto out;
  990. arpptr = (u8 *)parp + sizeof(struct arphdr);
  991. sha = arpptr;
  992. arpptr += dev->addr_len; /* sha */
  993. memcpy(&sip, arpptr, sizeof(sip));
  994. arpptr += sizeof(sip);
  995. arpptr += dev->addr_len; /* tha */
  996. memcpy(&tip, arpptr, sizeof(tip));
  997. if (ipv4_is_loopback(tip) ||
  998. ipv4_is_multicast(tip))
  999. goto out;
  1000. n = neigh_lookup(&arp_tbl, &tip, dev);
  1001. if (n) {
  1002. struct vxlan_fdb *f;
  1003. struct sk_buff *reply;
  1004. if (!(n->nud_state & NUD_CONNECTED)) {
  1005. neigh_release(n);
  1006. goto out;
  1007. }
  1008. f = vxlan_find_mac(vxlan, n->ha);
  1009. if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
  1010. /* bridge-local neighbor */
  1011. neigh_release(n);
  1012. goto out;
  1013. }
  1014. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  1015. n->ha, sha);
  1016. neigh_release(n);
  1017. skb_reset_mac_header(reply);
  1018. __skb_pull(reply, skb_network_offset(reply));
  1019. reply->ip_summed = CHECKSUM_UNNECESSARY;
  1020. reply->pkt_type = PACKET_HOST;
  1021. if (netif_rx_ni(reply) == NET_RX_DROP)
  1022. dev->stats.rx_dropped++;
  1023. } else if (vxlan->flags & VXLAN_F_L3MISS) {
  1024. union vxlan_addr ipa = {
  1025. .sin.sin_addr.s_addr = tip,
  1026. .sa.sa_family = AF_INET,
  1027. };
  1028. vxlan_ip_miss(dev, &ipa);
  1029. }
  1030. out:
  1031. consume_skb(skb);
  1032. return NETDEV_TX_OK;
  1033. }
  1034. #if IS_ENABLED(CONFIG_IPV6)
  1035. static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
  1036. {
  1037. struct vxlan_dev *vxlan = netdev_priv(dev);
  1038. struct neighbour *n;
  1039. union vxlan_addr ipa;
  1040. const struct ipv6hdr *iphdr;
  1041. const struct in6_addr *saddr, *daddr;
  1042. struct nd_msg *msg;
  1043. struct inet6_dev *in6_dev = NULL;
  1044. in6_dev = __in6_dev_get(dev);
  1045. if (!in6_dev)
  1046. goto out;
  1047. if (!pskb_may_pull(skb, skb->len))
  1048. goto out;
  1049. iphdr = ipv6_hdr(skb);
  1050. saddr = &iphdr->saddr;
  1051. daddr = &iphdr->daddr;
  1052. if (ipv6_addr_loopback(daddr) ||
  1053. ipv6_addr_is_multicast(daddr))
  1054. goto out;
  1055. msg = (struct nd_msg *)skb_transport_header(skb);
  1056. if (msg->icmph.icmp6_code != 0 ||
  1057. msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  1058. goto out;
  1059. n = neigh_lookup(ipv6_stub->nd_tbl, daddr, dev);
  1060. if (n) {
  1061. struct vxlan_fdb *f;
  1062. if (!(n->nud_state & NUD_CONNECTED)) {
  1063. neigh_release(n);
  1064. goto out;
  1065. }
  1066. f = vxlan_find_mac(vxlan, n->ha);
  1067. if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
  1068. /* bridge-local neighbor */
  1069. neigh_release(n);
  1070. goto out;
  1071. }
  1072. ipv6_stub->ndisc_send_na(dev, n, saddr, &msg->target,
  1073. !!in6_dev->cnf.forwarding,
  1074. true, false, false);
  1075. neigh_release(n);
  1076. } else if (vxlan->flags & VXLAN_F_L3MISS) {
  1077. ipa.sin6.sin6_addr = *daddr;
  1078. ipa.sa.sa_family = AF_INET6;
  1079. vxlan_ip_miss(dev, &ipa);
  1080. }
  1081. out:
  1082. consume_skb(skb);
  1083. return NETDEV_TX_OK;
  1084. }
  1085. #endif
  1086. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  1087. {
  1088. struct vxlan_dev *vxlan = netdev_priv(dev);
  1089. struct neighbour *n;
  1090. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  1091. return false;
  1092. n = NULL;
  1093. switch (ntohs(eth_hdr(skb)->h_proto)) {
  1094. case ETH_P_IP:
  1095. {
  1096. struct iphdr *pip;
  1097. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  1098. return false;
  1099. pip = ip_hdr(skb);
  1100. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  1101. if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
  1102. union vxlan_addr ipa = {
  1103. .sin.sin_addr.s_addr = pip->daddr,
  1104. .sa.sa_family = AF_INET,
  1105. };
  1106. vxlan_ip_miss(dev, &ipa);
  1107. return false;
  1108. }
  1109. break;
  1110. }
  1111. #if IS_ENABLED(CONFIG_IPV6)
  1112. case ETH_P_IPV6:
  1113. {
  1114. struct ipv6hdr *pip6;
  1115. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  1116. return false;
  1117. pip6 = ipv6_hdr(skb);
  1118. n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
  1119. if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
  1120. union vxlan_addr ipa = {
  1121. .sin6.sin6_addr = pip6->daddr,
  1122. .sa.sa_family = AF_INET6,
  1123. };
  1124. vxlan_ip_miss(dev, &ipa);
  1125. return false;
  1126. }
  1127. break;
  1128. }
  1129. #endif
  1130. default:
  1131. return false;
  1132. }
  1133. if (n) {
  1134. bool diff;
  1135. diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
  1136. if (diff) {
  1137. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  1138. dev->addr_len);
  1139. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  1140. }
  1141. neigh_release(n);
  1142. return diff;
  1143. }
  1144. return false;
  1145. }
  1146. static void vxlan_sock_put(struct sk_buff *skb)
  1147. {
  1148. sock_put(skb->sk);
  1149. }
  1150. /* On transmit, associate with the tunnel socket */
  1151. static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
  1152. {
  1153. skb_orphan(skb);
  1154. sock_hold(sk);
  1155. skb->sk = sk;
  1156. skb->destructor = vxlan_sock_put;
  1157. }
  1158. /* Compute source port for outgoing packet
  1159. * first choice to use L4 flow hash since it will spread
  1160. * better and maybe available from hardware
  1161. * secondary choice is to use jhash on the Ethernet header
  1162. */
  1163. __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
  1164. {
  1165. unsigned int range = (port_max - port_min) + 1;
  1166. u32 hash;
  1167. hash = skb_get_rxhash(skb);
  1168. if (!hash)
  1169. hash = jhash(skb->data, 2 * ETH_ALEN,
  1170. (__force u32) skb->protocol);
  1171. return htons((((u64) hash * range) >> 32) + port_min);
  1172. }
  1173. EXPORT_SYMBOL_GPL(vxlan_src_port);
  1174. static int handle_offloads(struct sk_buff *skb)
  1175. {
  1176. if (skb_is_gso(skb)) {
  1177. int err = skb_unclone(skb, GFP_ATOMIC);
  1178. if (unlikely(err))
  1179. return err;
  1180. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  1181. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  1182. skb->ip_summed = CHECKSUM_NONE;
  1183. return 0;
  1184. }
  1185. #if IS_ENABLED(CONFIG_IPV6)
  1186. static int vxlan6_xmit_skb(struct vxlan_sock *vs,
  1187. struct dst_entry *dst, struct sk_buff *skb,
  1188. struct net_device *dev, struct in6_addr *saddr,
  1189. struct in6_addr *daddr, __u8 prio, __u8 ttl,
  1190. __be16 src_port, __be16 dst_port, __be32 vni)
  1191. {
  1192. struct ipv6hdr *ip6h;
  1193. struct vxlanhdr *vxh;
  1194. struct udphdr *uh;
  1195. int min_headroom;
  1196. int err;
  1197. if (!skb->encapsulation) {
  1198. skb_reset_inner_headers(skb);
  1199. skb->encapsulation = 1;
  1200. }
  1201. skb_scrub_packet(skb, false);
  1202. min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
  1203. + VXLAN_HLEN + sizeof(struct ipv6hdr)
  1204. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  1205. /* Need space for new headers (invalidates iph ptr) */
  1206. err = skb_cow_head(skb, min_headroom);
  1207. if (unlikely(err))
  1208. return err;
  1209. if (vlan_tx_tag_present(skb)) {
  1210. if (WARN_ON(!__vlan_put_tag(skb,
  1211. skb->vlan_proto,
  1212. vlan_tx_tag_get(skb))))
  1213. return -ENOMEM;
  1214. skb->vlan_tci = 0;
  1215. }
  1216. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  1217. vxh->vx_flags = htonl(VXLAN_FLAGS);
  1218. vxh->vx_vni = vni;
  1219. __skb_push(skb, sizeof(*uh));
  1220. skb_reset_transport_header(skb);
  1221. uh = udp_hdr(skb);
  1222. uh->dest = dst_port;
  1223. uh->source = src_port;
  1224. uh->len = htons(skb->len);
  1225. uh->check = 0;
  1226. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1227. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  1228. IPSKB_REROUTED);
  1229. skb_dst_set(skb, dst);
  1230. if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) {
  1231. __wsum csum = skb_checksum(skb, 0, skb->len, 0);
  1232. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1233. uh->check = csum_ipv6_magic(saddr, daddr, skb->len,
  1234. IPPROTO_UDP, csum);
  1235. if (uh->check == 0)
  1236. uh->check = CSUM_MANGLED_0;
  1237. } else {
  1238. skb->ip_summed = CHECKSUM_PARTIAL;
  1239. skb->csum_start = skb_transport_header(skb) - skb->head;
  1240. skb->csum_offset = offsetof(struct udphdr, check);
  1241. uh->check = ~csum_ipv6_magic(saddr, daddr,
  1242. skb->len, IPPROTO_UDP, 0);
  1243. }
  1244. __skb_push(skb, sizeof(*ip6h));
  1245. skb_reset_network_header(skb);
  1246. ip6h = ipv6_hdr(skb);
  1247. ip6h->version = 6;
  1248. ip6h->priority = prio;
  1249. ip6h->flow_lbl[0] = 0;
  1250. ip6h->flow_lbl[1] = 0;
  1251. ip6h->flow_lbl[2] = 0;
  1252. ip6h->payload_len = htons(skb->len);
  1253. ip6h->nexthdr = IPPROTO_UDP;
  1254. ip6h->hop_limit = ttl;
  1255. ip6h->daddr = *daddr;
  1256. ip6h->saddr = *saddr;
  1257. vxlan_set_owner(vs->sock->sk, skb);
  1258. err = handle_offloads(skb);
  1259. if (err)
  1260. return err;
  1261. ip6tunnel_xmit(skb, dev);
  1262. return 0;
  1263. }
  1264. #endif
  1265. int vxlan_xmit_skb(struct vxlan_sock *vs,
  1266. struct rtable *rt, struct sk_buff *skb,
  1267. __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
  1268. __be16 src_port, __be16 dst_port, __be32 vni)
  1269. {
  1270. struct vxlanhdr *vxh;
  1271. struct udphdr *uh;
  1272. int min_headroom;
  1273. int err;
  1274. if (!skb->encapsulation) {
  1275. skb_reset_inner_headers(skb);
  1276. skb->encapsulation = 1;
  1277. }
  1278. min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
  1279. + VXLAN_HLEN + sizeof(struct iphdr)
  1280. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  1281. /* Need space for new headers (invalidates iph ptr) */
  1282. err = skb_cow_head(skb, min_headroom);
  1283. if (unlikely(err))
  1284. return err;
  1285. if (vlan_tx_tag_present(skb)) {
  1286. if (WARN_ON(!__vlan_put_tag(skb,
  1287. skb->vlan_proto,
  1288. vlan_tx_tag_get(skb))))
  1289. return -ENOMEM;
  1290. skb->vlan_tci = 0;
  1291. }
  1292. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  1293. vxh->vx_flags = htonl(VXLAN_FLAGS);
  1294. vxh->vx_vni = vni;
  1295. __skb_push(skb, sizeof(*uh));
  1296. skb_reset_transport_header(skb);
  1297. uh = udp_hdr(skb);
  1298. uh->dest = dst_port;
  1299. uh->source = src_port;
  1300. uh->len = htons(skb->len);
  1301. uh->check = 0;
  1302. vxlan_set_owner(vs->sock->sk, skb);
  1303. err = handle_offloads(skb);
  1304. if (err)
  1305. return err;
  1306. return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df,
  1307. false);
  1308. }
  1309. EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
  1310. /* Bypass encapsulation if the destination is local */
  1311. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  1312. struct vxlan_dev *dst_vxlan)
  1313. {
  1314. struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  1315. struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  1316. union vxlan_addr loopback;
  1317. union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
  1318. skb->pkt_type = PACKET_HOST;
  1319. skb->encapsulation = 0;
  1320. skb->dev = dst_vxlan->dev;
  1321. __skb_pull(skb, skb_network_offset(skb));
  1322. if (remote_ip->sa.sa_family == AF_INET) {
  1323. loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  1324. loopback.sa.sa_family = AF_INET;
  1325. #if IS_ENABLED(CONFIG_IPV6)
  1326. } else {
  1327. loopback.sin6.sin6_addr = in6addr_loopback;
  1328. loopback.sa.sa_family = AF_INET6;
  1329. #endif
  1330. }
  1331. if (dst_vxlan->flags & VXLAN_F_LEARN)
  1332. vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
  1333. u64_stats_update_begin(&tx_stats->syncp);
  1334. tx_stats->tx_packets++;
  1335. tx_stats->tx_bytes += skb->len;
  1336. u64_stats_update_end(&tx_stats->syncp);
  1337. if (netif_rx(skb) == NET_RX_SUCCESS) {
  1338. u64_stats_update_begin(&rx_stats->syncp);
  1339. rx_stats->rx_packets++;
  1340. rx_stats->rx_bytes += skb->len;
  1341. u64_stats_update_end(&rx_stats->syncp);
  1342. } else {
  1343. skb->dev->stats.rx_dropped++;
  1344. }
  1345. }
  1346. static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  1347. struct vxlan_rdst *rdst, bool did_rsc)
  1348. {
  1349. struct vxlan_dev *vxlan = netdev_priv(dev);
  1350. struct rtable *rt = NULL;
  1351. const struct iphdr *old_iph;
  1352. struct flowi4 fl4;
  1353. union vxlan_addr *dst;
  1354. __be16 src_port = 0, dst_port;
  1355. u32 vni;
  1356. __be16 df = 0;
  1357. __u8 tos, ttl;
  1358. int err;
  1359. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
  1360. vni = rdst->remote_vni;
  1361. dst = &rdst->remote_ip;
  1362. if (vxlan_addr_any(dst)) {
  1363. if (did_rsc) {
  1364. /* short-circuited back to local bridge */
  1365. vxlan_encap_bypass(skb, vxlan, vxlan);
  1366. return;
  1367. }
  1368. goto drop;
  1369. }
  1370. old_iph = ip_hdr(skb);
  1371. ttl = vxlan->ttl;
  1372. if (!ttl && vxlan_addr_multicast(dst))
  1373. ttl = 1;
  1374. tos = vxlan->tos;
  1375. if (tos == 1)
  1376. tos = ip_tunnel_get_dsfield(old_iph, skb);
  1377. src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
  1378. if (dst->sa.sa_family == AF_INET) {
  1379. memset(&fl4, 0, sizeof(fl4));
  1380. fl4.flowi4_oif = rdst->remote_ifindex;
  1381. fl4.flowi4_tos = RT_TOS(tos);
  1382. fl4.daddr = dst->sin.sin_addr.s_addr;
  1383. fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
  1384. rt = ip_route_output_key(dev_net(dev), &fl4);
  1385. if (IS_ERR(rt)) {
  1386. netdev_dbg(dev, "no route to %pI4\n",
  1387. &dst->sin.sin_addr.s_addr);
  1388. dev->stats.tx_carrier_errors++;
  1389. goto tx_error;
  1390. }
  1391. if (rt->dst.dev == dev) {
  1392. netdev_dbg(dev, "circular route to %pI4\n",
  1393. &dst->sin.sin_addr.s_addr);
  1394. dev->stats.collisions++;
  1395. goto tx_error;
  1396. }
  1397. /* Bypass encapsulation if the destination is local */
  1398. if (rt->rt_flags & RTCF_LOCAL &&
  1399. !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1400. struct vxlan_dev *dst_vxlan;
  1401. ip_rt_put(rt);
  1402. dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
  1403. if (!dst_vxlan)
  1404. goto tx_error;
  1405. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1406. return;
  1407. }
  1408. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  1409. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  1410. err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
  1411. fl4.saddr, dst->sin.sin_addr.s_addr,
  1412. tos, ttl, df, src_port, dst_port,
  1413. htonl(vni << 8));
  1414. if (err < 0)
  1415. goto rt_tx_error;
  1416. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  1417. #if IS_ENABLED(CONFIG_IPV6)
  1418. } else {
  1419. struct sock *sk = vxlan->vn_sock->sock->sk;
  1420. struct dst_entry *ndst;
  1421. struct flowi6 fl6;
  1422. u32 flags;
  1423. memset(&fl6, 0, sizeof(fl6));
  1424. fl6.flowi6_oif = rdst->remote_ifindex;
  1425. fl6.daddr = dst->sin6.sin6_addr;
  1426. fl6.saddr = vxlan->saddr.sin6.sin6_addr;
  1427. fl6.flowi6_proto = IPPROTO_UDP;
  1428. if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
  1429. netdev_dbg(dev, "no route to %pI6\n",
  1430. &dst->sin6.sin6_addr);
  1431. dev->stats.tx_carrier_errors++;
  1432. goto tx_error;
  1433. }
  1434. if (ndst->dev == dev) {
  1435. netdev_dbg(dev, "circular route to %pI6\n",
  1436. &dst->sin6.sin6_addr);
  1437. dst_release(ndst);
  1438. dev->stats.collisions++;
  1439. goto tx_error;
  1440. }
  1441. /* Bypass encapsulation if the destination is local */
  1442. flags = ((struct rt6_info *)ndst)->rt6i_flags;
  1443. if (flags & RTF_LOCAL &&
  1444. !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1445. struct vxlan_dev *dst_vxlan;
  1446. dst_release(ndst);
  1447. dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
  1448. if (!dst_vxlan)
  1449. goto tx_error;
  1450. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1451. return;
  1452. }
  1453. ttl = ttl ? : ip6_dst_hoplimit(ndst);
  1454. err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
  1455. dev, &fl6.saddr, &fl6.daddr, 0, ttl,
  1456. src_port, dst_port, htonl(vni << 8));
  1457. #endif
  1458. }
  1459. return;
  1460. drop:
  1461. dev->stats.tx_dropped++;
  1462. goto tx_free;
  1463. rt_tx_error:
  1464. ip_rt_put(rt);
  1465. tx_error:
  1466. dev->stats.tx_errors++;
  1467. tx_free:
  1468. dev_kfree_skb(skb);
  1469. }
  1470. /* Transmit local packets over Vxlan
  1471. *
  1472. * Outer IP header inherits ECN and DF from inner header.
  1473. * Outer UDP destination is the VXLAN assigned port.
  1474. * source port is based on hash of flow
  1475. */
  1476. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  1477. {
  1478. struct vxlan_dev *vxlan = netdev_priv(dev);
  1479. struct ethhdr *eth;
  1480. bool did_rsc = false;
  1481. struct vxlan_rdst *rdst;
  1482. struct vxlan_fdb *f;
  1483. skb_reset_mac_header(skb);
  1484. eth = eth_hdr(skb);
  1485. if ((vxlan->flags & VXLAN_F_PROXY)) {
  1486. if (ntohs(eth->h_proto) == ETH_P_ARP)
  1487. return arp_reduce(dev, skb);
  1488. #if IS_ENABLED(CONFIG_IPV6)
  1489. else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
  1490. skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
  1491. ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
  1492. struct nd_msg *msg;
  1493. msg = (struct nd_msg *)skb_transport_header(skb);
  1494. if (msg->icmph.icmp6_code == 0 &&
  1495. msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
  1496. return neigh_reduce(dev, skb);
  1497. }
  1498. #endif
  1499. }
  1500. f = vxlan_find_mac(vxlan, eth->h_dest);
  1501. did_rsc = false;
  1502. if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
  1503. (ntohs(eth->h_proto) == ETH_P_IP ||
  1504. ntohs(eth->h_proto) == ETH_P_IPV6)) {
  1505. did_rsc = route_shortcircuit(dev, skb);
  1506. if (did_rsc)
  1507. f = vxlan_find_mac(vxlan, eth->h_dest);
  1508. }
  1509. if (f == NULL) {
  1510. f = vxlan_find_mac(vxlan, all_zeros_mac);
  1511. if (f == NULL) {
  1512. if ((vxlan->flags & VXLAN_F_L2MISS) &&
  1513. !is_multicast_ether_addr(eth->h_dest))
  1514. vxlan_fdb_miss(vxlan, eth->h_dest);
  1515. dev->stats.tx_dropped++;
  1516. dev_kfree_skb(skb);
  1517. return NETDEV_TX_OK;
  1518. }
  1519. }
  1520. list_for_each_entry_rcu(rdst, &f->remotes, list) {
  1521. struct sk_buff *skb1;
  1522. skb1 = skb_clone(skb, GFP_ATOMIC);
  1523. if (skb1)
  1524. vxlan_xmit_one(skb1, dev, rdst, did_rsc);
  1525. }
  1526. dev_kfree_skb(skb);
  1527. return NETDEV_TX_OK;
  1528. }
  1529. /* Walk the forwarding table and purge stale entries */
  1530. static void vxlan_cleanup(unsigned long arg)
  1531. {
  1532. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  1533. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  1534. unsigned int h;
  1535. if (!netif_running(vxlan->dev))
  1536. return;
  1537. spin_lock_bh(&vxlan->hash_lock);
  1538. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1539. struct hlist_node *p, *n;
  1540. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1541. struct vxlan_fdb *f
  1542. = container_of(p, struct vxlan_fdb, hlist);
  1543. unsigned long timeout;
  1544. if (f->state & NUD_PERMANENT)
  1545. continue;
  1546. timeout = f->used + vxlan->age_interval * HZ;
  1547. if (time_before_eq(timeout, jiffies)) {
  1548. netdev_dbg(vxlan->dev,
  1549. "garbage collect %pM\n",
  1550. f->eth_addr);
  1551. f->state = NUD_STALE;
  1552. vxlan_fdb_destroy(vxlan, f);
  1553. } else if (time_before(timeout, next_timer))
  1554. next_timer = timeout;
  1555. }
  1556. }
  1557. spin_unlock_bh(&vxlan->hash_lock);
  1558. mod_timer(&vxlan->age_timer, next_timer);
  1559. }
  1560. static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
  1561. {
  1562. __u32 vni = vxlan->default_dst.remote_vni;
  1563. vxlan->vn_sock = vs;
  1564. hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
  1565. }
  1566. /* Setup stats when device is created */
  1567. static int vxlan_init(struct net_device *dev)
  1568. {
  1569. struct vxlan_dev *vxlan = netdev_priv(dev);
  1570. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1571. struct vxlan_sock *vs;
  1572. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1573. if (!dev->tstats)
  1574. return -ENOMEM;
  1575. spin_lock(&vn->sock_lock);
  1576. vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
  1577. if (vs) {
  1578. /* If we have a socket with same port already, reuse it */
  1579. atomic_inc(&vs->refcnt);
  1580. vxlan_vs_add_dev(vs, vxlan);
  1581. } else {
  1582. /* otherwise make new socket outside of RTNL */
  1583. dev_hold(dev);
  1584. queue_work(vxlan_wq, &vxlan->sock_work);
  1585. }
  1586. spin_unlock(&vn->sock_lock);
  1587. return 0;
  1588. }
  1589. static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
  1590. {
  1591. struct vxlan_fdb *f;
  1592. spin_lock_bh(&vxlan->hash_lock);
  1593. f = __vxlan_find_mac(vxlan, all_zeros_mac);
  1594. if (f)
  1595. vxlan_fdb_destroy(vxlan, f);
  1596. spin_unlock_bh(&vxlan->hash_lock);
  1597. }
  1598. static void vxlan_uninit(struct net_device *dev)
  1599. {
  1600. struct vxlan_dev *vxlan = netdev_priv(dev);
  1601. struct vxlan_sock *vs = vxlan->vn_sock;
  1602. vxlan_fdb_delete_default(vxlan);
  1603. if (vs)
  1604. vxlan_sock_release(vs);
  1605. free_percpu(dev->tstats);
  1606. }
  1607. /* Start ageing timer and join group when device is brought up */
  1608. static int vxlan_open(struct net_device *dev)
  1609. {
  1610. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1611. struct vxlan_dev *vxlan = netdev_priv(dev);
  1612. struct vxlan_sock *vs = vxlan->vn_sock;
  1613. /* socket hasn't been created */
  1614. if (!vs)
  1615. return -ENOTCONN;
  1616. if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
  1617. vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
  1618. vxlan_sock_hold(vs);
  1619. dev_hold(dev);
  1620. queue_work(vxlan_wq, &vxlan->igmp_join);
  1621. }
  1622. if (vxlan->age_interval)
  1623. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  1624. return 0;
  1625. }
  1626. /* Purge the forwarding table */
  1627. static void vxlan_flush(struct vxlan_dev *vxlan)
  1628. {
  1629. unsigned int h;
  1630. spin_lock_bh(&vxlan->hash_lock);
  1631. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1632. struct hlist_node *p, *n;
  1633. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1634. struct vxlan_fdb *f
  1635. = container_of(p, struct vxlan_fdb, hlist);
  1636. /* the all_zeros_mac entry is deleted at vxlan_uninit */
  1637. if (!is_zero_ether_addr(f->eth_addr))
  1638. vxlan_fdb_destroy(vxlan, f);
  1639. }
  1640. }
  1641. spin_unlock_bh(&vxlan->hash_lock);
  1642. }
  1643. /* Cleanup timer and forwarding table on shutdown */
  1644. static int vxlan_stop(struct net_device *dev)
  1645. {
  1646. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1647. struct vxlan_dev *vxlan = netdev_priv(dev);
  1648. struct vxlan_sock *vs = vxlan->vn_sock;
  1649. if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
  1650. ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
  1651. vxlan_sock_hold(vs);
  1652. dev_hold(dev);
  1653. queue_work(vxlan_wq, &vxlan->igmp_leave);
  1654. }
  1655. del_timer_sync(&vxlan->age_timer);
  1656. vxlan_flush(vxlan);
  1657. return 0;
  1658. }
  1659. /* Stub, nothing needs to be done. */
  1660. static void vxlan_set_multicast_list(struct net_device *dev)
  1661. {
  1662. }
  1663. static const struct net_device_ops vxlan_netdev_ops = {
  1664. .ndo_init = vxlan_init,
  1665. .ndo_uninit = vxlan_uninit,
  1666. .ndo_open = vxlan_open,
  1667. .ndo_stop = vxlan_stop,
  1668. .ndo_start_xmit = vxlan_xmit,
  1669. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1670. .ndo_set_rx_mode = vxlan_set_multicast_list,
  1671. .ndo_change_mtu = eth_change_mtu,
  1672. .ndo_validate_addr = eth_validate_addr,
  1673. .ndo_set_mac_address = eth_mac_addr,
  1674. .ndo_fdb_add = vxlan_fdb_add,
  1675. .ndo_fdb_del = vxlan_fdb_delete,
  1676. .ndo_fdb_dump = vxlan_fdb_dump,
  1677. };
  1678. /* Info for udev, that this is a virtual tunnel endpoint */
  1679. static struct device_type vxlan_type = {
  1680. .name = "vxlan",
  1681. };
  1682. /* Calls the ndo_add_vxlan_port of the caller in order to
  1683. * supply the listening VXLAN udp ports. Callers are expected
  1684. * to implement the ndo_add_vxlan_port.
  1685. */
  1686. void vxlan_get_rx_port(struct net_device *dev)
  1687. {
  1688. struct vxlan_sock *vs;
  1689. struct net *net = dev_net(dev);
  1690. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1691. sa_family_t sa_family;
  1692. __be16 port;
  1693. unsigned int i;
  1694. spin_lock(&vn->sock_lock);
  1695. for (i = 0; i < PORT_HASH_SIZE; ++i) {
  1696. hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
  1697. port = inet_sk(vs->sock->sk)->inet_sport;
  1698. sa_family = vs->sock->sk->sk_family;
  1699. dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
  1700. port);
  1701. }
  1702. }
  1703. spin_unlock(&vn->sock_lock);
  1704. }
  1705. EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
  1706. /* Initialize the device structure. */
  1707. static void vxlan_setup(struct net_device *dev)
  1708. {
  1709. struct vxlan_dev *vxlan = netdev_priv(dev);
  1710. unsigned int h;
  1711. int low, high;
  1712. eth_hw_addr_random(dev);
  1713. ether_setup(dev);
  1714. if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
  1715. dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM;
  1716. else
  1717. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  1718. dev->netdev_ops = &vxlan_netdev_ops;
  1719. dev->destructor = free_netdev;
  1720. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  1721. dev->tx_queue_len = 0;
  1722. dev->features |= NETIF_F_LLTX;
  1723. dev->features |= NETIF_F_NETNS_LOCAL;
  1724. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1725. dev->features |= NETIF_F_RXCSUM;
  1726. dev->features |= NETIF_F_GSO_SOFTWARE;
  1727. dev->vlan_features = dev->features;
  1728. dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1729. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1730. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1731. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1732. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1733. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1734. INIT_LIST_HEAD(&vxlan->next);
  1735. spin_lock_init(&vxlan->hash_lock);
  1736. INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
  1737. INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
  1738. INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
  1739. init_timer_deferrable(&vxlan->age_timer);
  1740. vxlan->age_timer.function = vxlan_cleanup;
  1741. vxlan->age_timer.data = (unsigned long) vxlan;
  1742. inet_get_local_port_range(&low, &high);
  1743. vxlan->port_min = low;
  1744. vxlan->port_max = high;
  1745. vxlan->dst_port = htons(vxlan_port);
  1746. vxlan->dev = dev;
  1747. for (h = 0; h < FDB_HASH_SIZE; ++h)
  1748. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  1749. }
  1750. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  1751. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1752. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1753. [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
  1754. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1755. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1756. [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
  1757. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1758. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1759. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1760. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1761. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1762. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1763. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1764. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1765. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1766. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1767. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  1768. };
  1769. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1770. {
  1771. if (tb[IFLA_ADDRESS]) {
  1772. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1773. pr_debug("invalid link address (not ethernet)\n");
  1774. return -EINVAL;
  1775. }
  1776. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1777. pr_debug("invalid all zero ethernet address\n");
  1778. return -EADDRNOTAVAIL;
  1779. }
  1780. }
  1781. if (!data)
  1782. return -EINVAL;
  1783. if (data[IFLA_VXLAN_ID]) {
  1784. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1785. if (id >= VXLAN_VID_MASK)
  1786. return -ERANGE;
  1787. }
  1788. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1789. const struct ifla_vxlan_port_range *p
  1790. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1791. if (ntohs(p->high) < ntohs(p->low)) {
  1792. pr_debug("port range %u .. %u not valid\n",
  1793. ntohs(p->low), ntohs(p->high));
  1794. return -EINVAL;
  1795. }
  1796. }
  1797. return 0;
  1798. }
  1799. static void vxlan_get_drvinfo(struct net_device *netdev,
  1800. struct ethtool_drvinfo *drvinfo)
  1801. {
  1802. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  1803. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  1804. }
  1805. static const struct ethtool_ops vxlan_ethtool_ops = {
  1806. .get_drvinfo = vxlan_get_drvinfo,
  1807. .get_link = ethtool_op_get_link,
  1808. };
  1809. static void vxlan_del_work(struct work_struct *work)
  1810. {
  1811. struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
  1812. sk_release_kernel(vs->sock->sk);
  1813. kfree_rcu(vs, rcu);
  1814. }
  1815. #if IS_ENABLED(CONFIG_IPV6)
  1816. /* Create UDP socket for encapsulation receive. AF_INET6 socket
  1817. * could be used for both IPv4 and IPv6 communications, but
  1818. * users may set bindv6only=1.
  1819. */
  1820. static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
  1821. {
  1822. struct sock *sk;
  1823. struct socket *sock;
  1824. struct sockaddr_in6 vxlan_addr = {
  1825. .sin6_family = AF_INET6,
  1826. .sin6_port = port,
  1827. };
  1828. int rc, val = 1;
  1829. rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
  1830. if (rc < 0) {
  1831. pr_debug("UDPv6 socket create failed\n");
  1832. return rc;
  1833. }
  1834. /* Put in proper namespace */
  1835. sk = sock->sk;
  1836. sk_change_net(sk, net);
  1837. kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
  1838. (char *)&val, sizeof(val));
  1839. rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
  1840. sizeof(struct sockaddr_in6));
  1841. if (rc < 0) {
  1842. pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
  1843. &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
  1844. sk_release_kernel(sk);
  1845. return rc;
  1846. }
  1847. /* At this point, IPv6 module should have been loaded in
  1848. * sock_create_kern().
  1849. */
  1850. BUG_ON(!ipv6_stub);
  1851. *psock = sock;
  1852. /* Disable multicast loopback */
  1853. inet_sk(sk)->mc_loop = 0;
  1854. return 0;
  1855. }
  1856. #else
  1857. static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
  1858. {
  1859. return -EPFNOSUPPORT;
  1860. }
  1861. #endif
  1862. static int create_v4_sock(struct net *net, __be16 port, struct socket **psock)
  1863. {
  1864. struct sock *sk;
  1865. struct socket *sock;
  1866. struct sockaddr_in vxlan_addr = {
  1867. .sin_family = AF_INET,
  1868. .sin_addr.s_addr = htonl(INADDR_ANY),
  1869. .sin_port = port,
  1870. };
  1871. int rc;
  1872. /* Create UDP socket for encapsulation receive. */
  1873. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
  1874. if (rc < 0) {
  1875. pr_debug("UDP socket create failed\n");
  1876. return rc;
  1877. }
  1878. /* Put in proper namespace */
  1879. sk = sock->sk;
  1880. sk_change_net(sk, net);
  1881. rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
  1882. sizeof(vxlan_addr));
  1883. if (rc < 0) {
  1884. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1885. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1886. sk_release_kernel(sk);
  1887. return rc;
  1888. }
  1889. *psock = sock;
  1890. /* Disable multicast loopback */
  1891. inet_sk(sk)->mc_loop = 0;
  1892. return 0;
  1893. }
  1894. /* Create new listen socket if needed */
  1895. static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
  1896. vxlan_rcv_t *rcv, void *data, bool ipv6)
  1897. {
  1898. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1899. struct vxlan_sock *vs;
  1900. struct socket *sock;
  1901. struct sock *sk;
  1902. int rc = 0;
  1903. unsigned int h;
  1904. vs = kmalloc(sizeof(*vs), GFP_KERNEL);
  1905. if (!vs)
  1906. return ERR_PTR(-ENOMEM);
  1907. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1908. INIT_HLIST_HEAD(&vs->vni_list[h]);
  1909. INIT_WORK(&vs->del_work, vxlan_del_work);
  1910. if (ipv6)
  1911. rc = create_v6_sock(net, port, &sock);
  1912. else
  1913. rc = create_v4_sock(net, port, &sock);
  1914. if (rc < 0) {
  1915. kfree(vs);
  1916. return ERR_PTR(rc);
  1917. }
  1918. vs->sock = sock;
  1919. sk = sock->sk;
  1920. atomic_set(&vs->refcnt, 1);
  1921. vs->rcv = rcv;
  1922. vs->data = data;
  1923. rcu_assign_sk_user_data(vs->sock->sk, vs);
  1924. spin_lock(&vn->sock_lock);
  1925. hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
  1926. vxlan_notify_add_rx_port(sk);
  1927. spin_unlock(&vn->sock_lock);
  1928. /* Mark socket as an encapsulation socket. */
  1929. udp_sk(sk)->encap_type = 1;
  1930. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1931. #if IS_ENABLED(CONFIG_IPV6)
  1932. if (ipv6)
  1933. ipv6_stub->udpv6_encap_enable();
  1934. else
  1935. #endif
  1936. udp_encap_enable();
  1937. return vs;
  1938. }
  1939. struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
  1940. vxlan_rcv_t *rcv, void *data,
  1941. bool no_share, bool ipv6)
  1942. {
  1943. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1944. struct vxlan_sock *vs;
  1945. vs = vxlan_socket_create(net, port, rcv, data, ipv6);
  1946. if (!IS_ERR(vs))
  1947. return vs;
  1948. if (no_share) /* Return error if sharing is not allowed. */
  1949. return vs;
  1950. spin_lock(&vn->sock_lock);
  1951. vs = vxlan_find_sock(net, port);
  1952. if (vs) {
  1953. if (vs->rcv == rcv)
  1954. atomic_inc(&vs->refcnt);
  1955. else
  1956. vs = ERR_PTR(-EBUSY);
  1957. }
  1958. spin_unlock(&vn->sock_lock);
  1959. if (!vs)
  1960. vs = ERR_PTR(-EINVAL);
  1961. return vs;
  1962. }
  1963. EXPORT_SYMBOL_GPL(vxlan_sock_add);
  1964. /* Scheduled at device creation to bind to a socket */
  1965. static void vxlan_sock_work(struct work_struct *work)
  1966. {
  1967. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
  1968. struct net *net = dev_net(vxlan->dev);
  1969. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1970. __be16 port = vxlan->dst_port;
  1971. struct vxlan_sock *nvs;
  1972. nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6);
  1973. spin_lock(&vn->sock_lock);
  1974. if (!IS_ERR(nvs))
  1975. vxlan_vs_add_dev(nvs, vxlan);
  1976. spin_unlock(&vn->sock_lock);
  1977. dev_put(vxlan->dev);
  1978. }
  1979. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1980. struct nlattr *tb[], struct nlattr *data[])
  1981. {
  1982. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1983. struct vxlan_dev *vxlan = netdev_priv(dev);
  1984. struct vxlan_rdst *dst = &vxlan->default_dst;
  1985. __u32 vni;
  1986. int err;
  1987. bool use_ipv6 = false;
  1988. if (!data[IFLA_VXLAN_ID])
  1989. return -EINVAL;
  1990. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1991. dst->remote_vni = vni;
  1992. if (data[IFLA_VXLAN_GROUP]) {
  1993. dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1994. dst->remote_ip.sa.sa_family = AF_INET;
  1995. } else if (data[IFLA_VXLAN_GROUP6]) {
  1996. if (!IS_ENABLED(CONFIG_IPV6))
  1997. return -EPFNOSUPPORT;
  1998. nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
  1999. sizeof(struct in6_addr));
  2000. dst->remote_ip.sa.sa_family = AF_INET6;
  2001. use_ipv6 = true;
  2002. }
  2003. if (data[IFLA_VXLAN_LOCAL]) {
  2004. vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  2005. vxlan->saddr.sa.sa_family = AF_INET;
  2006. } else if (data[IFLA_VXLAN_LOCAL6]) {
  2007. if (!IS_ENABLED(CONFIG_IPV6))
  2008. return -EPFNOSUPPORT;
  2009. /* TODO: respect scope id */
  2010. nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
  2011. sizeof(struct in6_addr));
  2012. vxlan->saddr.sa.sa_family = AF_INET6;
  2013. use_ipv6 = true;
  2014. }
  2015. if (data[IFLA_VXLAN_LINK] &&
  2016. (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  2017. struct net_device *lowerdev
  2018. = __dev_get_by_index(net, dst->remote_ifindex);
  2019. if (!lowerdev) {
  2020. pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
  2021. return -ENODEV;
  2022. }
  2023. #if IS_ENABLED(CONFIG_IPV6)
  2024. if (use_ipv6) {
  2025. struct inet6_dev *idev = __in6_dev_get(lowerdev);
  2026. if (idev && idev->cnf.disable_ipv6) {
  2027. pr_info("IPv6 is disabled via sysctl\n");
  2028. return -EPERM;
  2029. }
  2030. vxlan->flags |= VXLAN_F_IPV6;
  2031. }
  2032. #endif
  2033. if (!tb[IFLA_MTU])
  2034. dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
  2035. /* update header length based on lower device */
  2036. dev->hard_header_len = lowerdev->hard_header_len +
  2037. (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
  2038. }
  2039. if (data[IFLA_VXLAN_TOS])
  2040. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  2041. if (data[IFLA_VXLAN_TTL])
  2042. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  2043. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  2044. vxlan->flags |= VXLAN_F_LEARN;
  2045. if (data[IFLA_VXLAN_AGEING])
  2046. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  2047. else
  2048. vxlan->age_interval = FDB_AGE_DEFAULT;
  2049. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  2050. vxlan->flags |= VXLAN_F_PROXY;
  2051. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  2052. vxlan->flags |= VXLAN_F_RSC;
  2053. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  2054. vxlan->flags |= VXLAN_F_L2MISS;
  2055. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  2056. vxlan->flags |= VXLAN_F_L3MISS;
  2057. if (data[IFLA_VXLAN_LIMIT])
  2058. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  2059. if (data[IFLA_VXLAN_PORT_RANGE]) {
  2060. const struct ifla_vxlan_port_range *p
  2061. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  2062. vxlan->port_min = ntohs(p->low);
  2063. vxlan->port_max = ntohs(p->high);
  2064. }
  2065. if (data[IFLA_VXLAN_PORT])
  2066. vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  2067. if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
  2068. pr_info("duplicate VNI %u\n", vni);
  2069. return -EEXIST;
  2070. }
  2071. SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
  2072. /* create an fdb entry for a valid default destination */
  2073. if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
  2074. err = vxlan_fdb_create(vxlan, all_zeros_mac,
  2075. &vxlan->default_dst.remote_ip,
  2076. NUD_REACHABLE|NUD_PERMANENT,
  2077. NLM_F_EXCL|NLM_F_CREATE,
  2078. vxlan->dst_port,
  2079. vxlan->default_dst.remote_vni,
  2080. vxlan->default_dst.remote_ifindex,
  2081. NTF_SELF);
  2082. if (err)
  2083. return err;
  2084. }
  2085. err = register_netdevice(dev);
  2086. if (err) {
  2087. vxlan_fdb_delete_default(vxlan);
  2088. return err;
  2089. }
  2090. list_add(&vxlan->next, &vn->vxlan_list);
  2091. return 0;
  2092. }
  2093. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  2094. {
  2095. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  2096. struct vxlan_dev *vxlan = netdev_priv(dev);
  2097. spin_lock(&vn->sock_lock);
  2098. if (!hlist_unhashed(&vxlan->hlist))
  2099. hlist_del_rcu(&vxlan->hlist);
  2100. spin_unlock(&vn->sock_lock);
  2101. list_del(&vxlan->next);
  2102. unregister_netdevice_queue(dev, head);
  2103. }
  2104. static size_t vxlan_get_size(const struct net_device *dev)
  2105. {
  2106. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  2107. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
  2108. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  2109. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
  2110. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  2111. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  2112. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  2113. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  2114. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  2115. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  2116. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  2117. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  2118. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  2119. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  2120. nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
  2121. 0;
  2122. }
  2123. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  2124. {
  2125. const struct vxlan_dev *vxlan = netdev_priv(dev);
  2126. const struct vxlan_rdst *dst = &vxlan->default_dst;
  2127. struct ifla_vxlan_port_range ports = {
  2128. .low = htons(vxlan->port_min),
  2129. .high = htons(vxlan->port_max),
  2130. };
  2131. if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
  2132. goto nla_put_failure;
  2133. if (!vxlan_addr_any(&dst->remote_ip)) {
  2134. if (dst->remote_ip.sa.sa_family == AF_INET) {
  2135. if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
  2136. dst->remote_ip.sin.sin_addr.s_addr))
  2137. goto nla_put_failure;
  2138. #if IS_ENABLED(CONFIG_IPV6)
  2139. } else {
  2140. if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
  2141. &dst->remote_ip.sin6.sin6_addr))
  2142. goto nla_put_failure;
  2143. #endif
  2144. }
  2145. }
  2146. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  2147. goto nla_put_failure;
  2148. if (!vxlan_addr_any(&vxlan->saddr)) {
  2149. if (vxlan->saddr.sa.sa_family == AF_INET) {
  2150. if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
  2151. vxlan->saddr.sin.sin_addr.s_addr))
  2152. goto nla_put_failure;
  2153. #if IS_ENABLED(CONFIG_IPV6)
  2154. } else {
  2155. if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
  2156. &vxlan->saddr.sin6.sin6_addr))
  2157. goto nla_put_failure;
  2158. #endif
  2159. }
  2160. }
  2161. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  2162. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  2163. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  2164. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  2165. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  2166. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  2167. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  2168. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  2169. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  2170. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  2171. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  2172. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  2173. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
  2174. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
  2175. goto nla_put_failure;
  2176. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  2177. goto nla_put_failure;
  2178. return 0;
  2179. nla_put_failure:
  2180. return -EMSGSIZE;
  2181. }
  2182. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  2183. .kind = "vxlan",
  2184. .maxtype = IFLA_VXLAN_MAX,
  2185. .policy = vxlan_policy,
  2186. .priv_size = sizeof(struct vxlan_dev),
  2187. .setup = vxlan_setup,
  2188. .validate = vxlan_validate,
  2189. .newlink = vxlan_newlink,
  2190. .dellink = vxlan_dellink,
  2191. .get_size = vxlan_get_size,
  2192. .fill_info = vxlan_fill_info,
  2193. };
  2194. static __net_init int vxlan_init_net(struct net *net)
  2195. {
  2196. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2197. unsigned int h;
  2198. INIT_LIST_HEAD(&vn->vxlan_list);
  2199. spin_lock_init(&vn->sock_lock);
  2200. for (h = 0; h < PORT_HASH_SIZE; ++h)
  2201. INIT_HLIST_HEAD(&vn->sock_list[h]);
  2202. return 0;
  2203. }
  2204. static __net_exit void vxlan_exit_net(struct net *net)
  2205. {
  2206. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2207. struct vxlan_dev *vxlan;
  2208. LIST_HEAD(list);
  2209. rtnl_lock();
  2210. list_for_each_entry(vxlan, &vn->vxlan_list, next)
  2211. unregister_netdevice_queue(vxlan->dev, &list);
  2212. unregister_netdevice_many(&list);
  2213. rtnl_unlock();
  2214. }
  2215. static struct pernet_operations vxlan_net_ops = {
  2216. .init = vxlan_init_net,
  2217. .exit = vxlan_exit_net,
  2218. .id = &vxlan_net_id,
  2219. .size = sizeof(struct vxlan_net),
  2220. };
  2221. static int __init vxlan_init_module(void)
  2222. {
  2223. int rc;
  2224. vxlan_wq = alloc_workqueue("vxlan", 0, 0);
  2225. if (!vxlan_wq)
  2226. return -ENOMEM;
  2227. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  2228. rc = register_pernet_device(&vxlan_net_ops);
  2229. if (rc)
  2230. goto out1;
  2231. rc = rtnl_link_register(&vxlan_link_ops);
  2232. if (rc)
  2233. goto out2;
  2234. return 0;
  2235. out2:
  2236. unregister_pernet_device(&vxlan_net_ops);
  2237. out1:
  2238. destroy_workqueue(vxlan_wq);
  2239. return rc;
  2240. }
  2241. late_initcall(vxlan_init_module);
  2242. static void __exit vxlan_cleanup_module(void)
  2243. {
  2244. rtnl_link_unregister(&vxlan_link_ops);
  2245. destroy_workqueue(vxlan_wq);
  2246. unregister_pernet_device(&vxlan_net_ops);
  2247. rcu_barrier();
  2248. }
  2249. module_exit(vxlan_cleanup_module);
  2250. MODULE_LICENSE("GPL");
  2251. MODULE_VERSION(VXLAN_VERSION);
  2252. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  2253. MODULE_ALIAS_RTNL_LINK("vxlan");