blob: 33125fc009cfde20a257bee4559c51388eb9de27 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the NetFilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
6 * cluster of servers.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * Changes:
18 *
19 */
20
Hannes Eder9aada7a2009-07-30 14:29:44 -070021#define KMSG_COMPONENT "IPVS"
22#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/fs.h>
29#include <linux/sysctl.h>
30#include <linux/proc_fs.h>
31#include <linux/workqueue.h>
32#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <linux/netfilter.h>
37#include <linux/netfilter_ipv4.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080038#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020040#include <net/net_namespace.h>
Hans Schillstrom93304192011-01-03 14:44:51 +010041#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <net/ip.h>
Vince Busam09571c72008-09-02 15:55:52 +020043#ifdef CONFIG_IP_VS_IPV6
44#include <net/ipv6.h>
45#include <net/ip6_route.h>
46#endif
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020047#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <net/sock.h>
Julius Volz9a812192008-08-14 14:08:44 +020049#include <net/genetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/uaccess.h>
52
53#include <net/ip_vs.h>
54
55/* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -080056static DEFINE_MUTEX(__ip_vs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* sysctl variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#ifdef CONFIG_IP_VS_DEBUG
61static int sysctl_ip_vs_debug_level = 0;
62
63int ip_vs_get_debug_level(void)
64{
65 return sysctl_ip_vs_debug_level;
66}
67#endif
68
Hans Schillstrom7a4f0762011-05-03 22:09:31 +020069
70/* Protos */
Julian Anastasov578bc3e2013-03-22 11:46:49 +020071static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +020072
73
Vince Busam09571c72008-09-02 15:55:52 +020074#ifdef CONFIG_IP_VS_IPV6
75/* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
Eric Dumazetc24584c2012-06-24 21:58:23 +000076static bool __ip_vs_addr_is_local_v6(struct net *net,
77 const struct in6_addr *addr)
Vince Busam09571c72008-09-02 15:55:52 +020078{
David S. Miller4c9483b2011-03-12 16:22:43 -050079 struct flowi6 fl6 = {
80 .daddr = *addr,
Vince Busam09571c72008-09-02 15:55:52 +020081 };
Eric Dumazetc24584c2012-06-24 21:58:23 +000082 struct dst_entry *dst = ip6_route_output(net, NULL, &fl6);
83 bool is_local;
Vince Busam09571c72008-09-02 15:55:52 +020084
Eric Dumazetc24584c2012-06-24 21:58:23 +000085 is_local = !dst->error && dst->dev && (dst->dev->flags & IFF_LOOPBACK);
Vince Busam09571c72008-09-02 15:55:52 +020086
Eric Dumazetc24584c2012-06-24 21:58:23 +000087 dst_release(dst);
88 return is_local;
Vince Busam09571c72008-09-02 15:55:52 +020089}
90#endif
Simon Horman14e40542011-02-04 18:33:02 +090091
92#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
Julian Anastasovaf9debd2005-07-11 20:59:57 -070094 * update_defense_level is called from keventd and from sysctl,
95 * so it needs to protect itself from softirqs
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Hans Schillstrom93304192011-01-03 14:44:51 +010097static void update_defense_level(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct sysinfo i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 int availmem;
101 int nomem;
102 int to_change = -1;
103
104 /* we only count free and buffered memory (in pages) */
105 si_meminfo(&i);
106 availmem = i.freeram + i.bufferram;
107 /* however in linux 2.5 the i.bufferram is total page cache size,
108 we need adjust it */
109 /* si_swapinfo(&i); */
110 /* availmem = availmem - (i.totalswap - i.freeswap); */
111
Hans Schillstroma0840e22011-01-03 14:44:58 +0100112 nomem = (availmem < ipvs->sysctl_amemthresh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Julian Anastasovaf9debd2005-07-11 20:59:57 -0700114 local_bh_disable();
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* drop_entry */
Hans Schillstroma0840e22011-01-03 14:44:58 +0100117 spin_lock(&ipvs->dropentry_lock);
118 switch (ipvs->sysctl_drop_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 case 0:
Hans Schillstroma0840e22011-01-03 14:44:58 +0100120 atomic_set(&ipvs->dropentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 break;
122 case 1:
123 if (nomem) {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100124 atomic_set(&ipvs->dropentry, 1);
125 ipvs->sysctl_drop_entry = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 } else {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100127 atomic_set(&ipvs->dropentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 break;
130 case 2:
131 if (nomem) {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100132 atomic_set(&ipvs->dropentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 } else {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100134 atomic_set(&ipvs->dropentry, 0);
135 ipvs->sysctl_drop_entry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 };
137 break;
138 case 3:
Hans Schillstroma0840e22011-01-03 14:44:58 +0100139 atomic_set(&ipvs->dropentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 break;
141 }
Hans Schillstroma0840e22011-01-03 14:44:58 +0100142 spin_unlock(&ipvs->dropentry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* drop_packet */
Hans Schillstroma0840e22011-01-03 14:44:58 +0100145 spin_lock(&ipvs->droppacket_lock);
146 switch (ipvs->sysctl_drop_packet) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 case 0:
Hans Schillstroma0840e22011-01-03 14:44:58 +0100148 ipvs->drop_rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 break;
150 case 1:
151 if (nomem) {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100152 ipvs->drop_rate = ipvs->drop_counter
153 = ipvs->sysctl_amemthresh /
154 (ipvs->sysctl_amemthresh-availmem);
155 ipvs->sysctl_drop_packet = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 } else {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100157 ipvs->drop_rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 break;
160 case 2:
161 if (nomem) {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100162 ipvs->drop_rate = ipvs->drop_counter
163 = ipvs->sysctl_amemthresh /
164 (ipvs->sysctl_amemthresh-availmem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 } else {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100166 ipvs->drop_rate = 0;
167 ipvs->sysctl_drop_packet = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 break;
170 case 3:
Hans Schillstroma0840e22011-01-03 14:44:58 +0100171 ipvs->drop_rate = ipvs->sysctl_am_droprate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 break;
173 }
Hans Schillstroma0840e22011-01-03 14:44:58 +0100174 spin_unlock(&ipvs->droppacket_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /* secure_tcp */
Hans Schillstroma0840e22011-01-03 14:44:58 +0100177 spin_lock(&ipvs->securetcp_lock);
178 switch (ipvs->sysctl_secure_tcp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 case 0:
Eric Dumazeta4b012c2019-10-23 09:53:03 -0700180 if (ipvs->old_secure_tcp >= 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 to_change = 0;
182 break;
183 case 1:
184 if (nomem) {
Eric Dumazeta4b012c2019-10-23 09:53:03 -0700185 if (ipvs->old_secure_tcp < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 to_change = 1;
Hans Schillstroma0840e22011-01-03 14:44:58 +0100187 ipvs->sysctl_secure_tcp = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 } else {
Eric Dumazeta4b012c2019-10-23 09:53:03 -0700189 if (ipvs->old_secure_tcp >= 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 to_change = 0;
191 }
192 break;
193 case 2:
194 if (nomem) {
Eric Dumazeta4b012c2019-10-23 09:53:03 -0700195 if (ipvs->old_secure_tcp < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 to_change = 1;
197 } else {
Eric Dumazeta4b012c2019-10-23 09:53:03 -0700198 if (ipvs->old_secure_tcp >= 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 to_change = 0;
Hans Schillstroma0840e22011-01-03 14:44:58 +0100200 ipvs->sysctl_secure_tcp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 break;
203 case 3:
Eric Dumazeta4b012c2019-10-23 09:53:03 -0700204 if (ipvs->old_secure_tcp < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 to_change = 1;
206 break;
207 }
Eric Dumazeta4b012c2019-10-23 09:53:03 -0700208 ipvs->old_secure_tcp = ipvs->sysctl_secure_tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (to_change >= 0)
Hans Schillstrom93304192011-01-03 14:44:51 +0100210 ip_vs_protocol_timeout_change(ipvs,
Hans Schillstroma0840e22011-01-03 14:44:58 +0100211 ipvs->sysctl_secure_tcp > 1);
212 spin_unlock(&ipvs->securetcp_lock);
Julian Anastasovaf9debd2005-07-11 20:59:57 -0700213
214 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217
218/*
219 * Timer for checking the defense
220 */
221#define DEFENSE_TIMER_PERIOD 1*HZ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
David Howellsc4028952006-11-22 14:57:56 +0000223static void defense_work_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Hans Schillstromf6340ee2011-01-03 14:44:59 +0100225 struct netns_ipvs *ipvs =
226 container_of(work, struct netns_ipvs, defense_work.work);
Hans Schillstrom93304192011-01-03 14:44:51 +0100227
228 update_defense_level(ipvs);
Hans Schillstroma0840e22011-01-03 14:44:58 +0100229 if (atomic_read(&ipvs->dropentry))
Eric W. Biederman423b5592015-09-21 13:02:24 -0500230 ip_vs_random_dropentry(ipvs);
Hans Schillstromf6340ee2011-01-03 14:44:59 +0100231 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
Simon Horman14e40542011-02-04 18:33:02 +0900233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235int
236ip_vs_use_count_inc(void)
237{
238 return try_module_get(THIS_MODULE);
239}
240
241void
242ip_vs_use_count_dec(void)
243{
244 module_put(THIS_MODULE);
245}
246
247
248/*
249 * Hash table: for virtual service lookups
250 */
251#define IP_VS_SVC_TAB_BITS 8
252#define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
253#define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
254
255/* the service table hashed by <protocol, addr, port> */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200256static struct hlist_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/* the service table hashed by fwmark */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200258static struct hlist_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261/*
262 * Returns hash value for virtual service
263 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000264static inline unsigned int
Eric W. Biedermanba61f392015-09-21 13:01:47 -0500265ip_vs_svc_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
Hans Schillstromfc723252011-01-03 14:44:43 +0100266 const union nf_inet_addr *addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Eric Dumazet95c96172012-04-15 05:58:06 +0000268 register unsigned int porth = ntohs(port);
Julius Volzb18610d2008-09-02 15:55:37 +0200269 __be32 addr_fold = addr->ip;
Julian Anastasove9836f22013-03-09 23:25:07 +0200270 __u32 ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Julius Volzb18610d2008-09-02 15:55:37 +0200272#ifdef CONFIG_IP_VS_IPV6
273 if (af == AF_INET6)
274 addr_fold = addr->ip6[0]^addr->ip6[1]^
275 addr->ip6[2]^addr->ip6[3];
276#endif
Julian Anastasove9836f22013-03-09 23:25:07 +0200277 ahash = ntohl(addr_fold);
Eric W. Biedermanba61f392015-09-21 13:01:47 -0500278 ahash ^= ((size_t) ipvs >> 8);
Julius Volzb18610d2008-09-02 15:55:37 +0200279
Julian Anastasove9836f22013-03-09 23:25:07 +0200280 return (proto ^ ahash ^ (porth >> IP_VS_SVC_TAB_BITS) ^ porth) &
281 IP_VS_SVC_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/*
285 * Returns hash value of fwmark for virtual service lookup
286 */
Eric W. Biedermanf6510b22015-09-21 13:01:45 -0500287static inline unsigned int ip_vs_svc_fwm_hashkey(struct netns_ipvs *ipvs, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Eric W. Biedermanf6510b22015-09-21 13:01:45 -0500289 return (((size_t)ipvs>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/*
Hans Schillstromfc723252011-01-03 14:44:43 +0100293 * Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * or in the ip_vs_svc_fwm_table by fwmark.
295 * Should be called with locked tables.
296 */
297static int ip_vs_svc_hash(struct ip_vs_service *svc)
298{
Eric Dumazet95c96172012-04-15 05:58:06 +0000299 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (svc->flags & IP_VS_SVC_F_HASHED) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000302 pr_err("%s(): request for already hashed, called from %pF\n",
303 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305 }
306
307 if (svc->fwmark == 0) {
308 /*
Hans Schillstromfc723252011-01-03 14:44:43 +0100309 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Eric W. Biedermanba61f392015-09-21 13:01:47 -0500311 hash = ip_vs_svc_hashkey(svc->ipvs, svc->af, svc->protocol,
Hans Schillstromfc723252011-01-03 14:44:43 +0100312 &svc->addr, svc->port);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200313 hlist_add_head_rcu(&svc->s_list, &ip_vs_svc_table[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 } else {
315 /*
Hans Schillstromfc723252011-01-03 14:44:43 +0100316 * Hash it by fwmark in svc_fwm_table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Eric W. Biedermanf6510b22015-09-21 13:01:45 -0500318 hash = ip_vs_svc_fwm_hashkey(svc->ipvs, svc->fwmark);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200319 hlist_add_head_rcu(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
322 svc->flags |= IP_VS_SVC_F_HASHED;
323 /* increase its refcnt because it is referenced by the svc table */
324 atomic_inc(&svc->refcnt);
325 return 1;
326}
327
328
329/*
Hans Schillstromfc723252011-01-03 14:44:43 +0100330 * Unhashes a service from svc_table / svc_fwm_table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * Should be called with locked tables.
332 */
333static int ip_vs_svc_unhash(struct ip_vs_service *svc)
334{
335 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000336 pr_err("%s(): request for unhash flagged, called from %pF\n",
337 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339 }
340
341 if (svc->fwmark == 0) {
Hans Schillstromfc723252011-01-03 14:44:43 +0100342 /* Remove it from the svc_table table */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200343 hlist_del_rcu(&svc->s_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } else {
Hans Schillstromfc723252011-01-03 14:44:43 +0100345 /* Remove it from the svc_fwm_table table */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200346 hlist_del_rcu(&svc->f_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 svc->flags &= ~IP_VS_SVC_F_HASHED;
350 atomic_dec(&svc->refcnt);
351 return 1;
352}
353
354
355/*
Hans Schillstromfc723252011-01-03 14:44:43 +0100356 * Get service by {netns, proto,addr,port} in the service table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 */
Julius Volzb18610d2008-09-02 15:55:37 +0200358static inline struct ip_vs_service *
Eric W. Biedermanbb2e2a82015-09-21 13:01:48 -0500359__ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol,
Hans Schillstromfc723252011-01-03 14:44:43 +0100360 const union nf_inet_addr *vaddr, __be16 vport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Eric Dumazet95c96172012-04-15 05:58:06 +0000362 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct ip_vs_service *svc;
364
365 /* Check for "full" addressed entries */
Eric W. Biedermanba61f392015-09-21 13:01:47 -0500366 hash = ip_vs_svc_hashkey(ipvs, af, protocol, vaddr, vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Julian Anastasovceec4c32013-03-22 11:46:53 +0200368 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[hash], s_list) {
Julius Volzb18610d2008-09-02 15:55:37 +0200369 if ((svc->af == af)
370 && ip_vs_addr_equal(af, &svc->addr, vaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 && (svc->port == vport)
Hans Schillstromfc723252011-01-03 14:44:43 +0100372 && (svc->protocol == protocol)
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500373 && (svc->ipvs == ipvs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return svc;
376 }
377 }
378
379 return NULL;
380}
381
382
383/*
384 * Get service by {fwmark} in the service table.
385 */
Julius Volzb18610d2008-09-02 15:55:37 +0200386static inline struct ip_vs_service *
Eric W. Biederman1ed8b942015-09-21 13:01:46 -0500387__ip_vs_svc_fwm_find(struct netns_ipvs *ipvs, int af, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Eric Dumazet95c96172012-04-15 05:58:06 +0000389 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct ip_vs_service *svc;
391
392 /* Check for fwmark addressed entries */
Eric W. Biedermanf6510b22015-09-21 13:01:45 -0500393 hash = ip_vs_svc_fwm_hashkey(ipvs, fwmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Julian Anastasovceec4c32013-03-22 11:46:53 +0200395 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[hash], f_list) {
Hans Schillstromfc723252011-01-03 14:44:43 +0100396 if (svc->fwmark == fwmark && svc->af == af
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500397 && (svc->ipvs == ipvs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return svc;
400 }
401 }
402
403 return NULL;
404}
405
Julian Anastasovceec4c32013-03-22 11:46:53 +0200406/* Find service, called under RCU lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407struct ip_vs_service *
Eric W. Biederman0a4fd6c2015-09-21 13:01:49 -0500408ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
Julian Anastasovceec4c32013-03-22 11:46:53 +0200409 const union nf_inet_addr *vaddr, __be16 vport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct ip_vs_service *svc;
Julius Volz3c2e0502008-09-02 15:55:38 +0200412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /*
414 * Check the table hashed by fwmark first
415 */
Julian Anastasov097fc762011-03-04 12:26:17 +0200416 if (fwmark) {
Eric W. Biederman1ed8b942015-09-21 13:01:46 -0500417 svc = __ip_vs_svc_fwm_find(ipvs, af, fwmark);
Julian Anastasov097fc762011-03-04 12:26:17 +0200418 if (svc)
419 goto out;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /*
423 * Check the table hashed by <protocol,addr,port>
424 * for "full" addressed entries
425 */
Eric W. Biedermanbb2e2a82015-09-21 13:01:48 -0500426 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (svc == NULL
429 && protocol == IPPROTO_TCP
Hans Schillstrom763f8d02011-01-03 14:45:01 +0100430 && atomic_read(&ipvs->ftpsvc_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 && (vport == FTPDATA || ntohs(vport) >= PROT_SOCK)) {
432 /*
433 * Check if ftp service entry exists, the packet
434 * might belong to FTP data connections.
435 */
Eric W. Biedermanbb2e2a82015-09-21 13:01:48 -0500436 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, FTPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
439 if (svc == NULL
Hans Schillstrom763f8d02011-01-03 14:45:01 +0100440 && atomic_read(&ipvs->nullsvc_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
442 * Check if the catch-all port (port zero) exists
443 */
Eric W. Biedermanbb2e2a82015-09-21 13:01:48 -0500444 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
447 out:
Julius Volz3c2e0502008-09-02 15:55:38 +0200448 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
449 fwmark, ip_vs_proto_name(protocol),
450 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
451 svc ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 return svc;
454}
455
456
457static inline void
458__ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
459{
460 atomic_inc(&svc->refcnt);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300461 rcu_assign_pointer(dest->svc, svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Julian Anastasovceec4c32013-03-22 11:46:53 +0200464static void ip_vs_service_free(struct ip_vs_service *svc)
465{
Markus Elfring982f4052014-11-18 20:37:05 +0100466 free_percpu(svc->stats.cpustats);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200467 kfree(svc);
468}
469
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300470static void ip_vs_service_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300472 struct ip_vs_service *svc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300474 svc = container_of(head, struct ip_vs_service, rcu_head);
475 ip_vs_service_free(svc);
476}
477
478static void __ip_vs_svc_put(struct ip_vs_service *svc, bool do_delay)
479{
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200480 if (atomic_dec_and_test(&svc->refcnt)) {
Julian Anastasovceec4c32013-03-22 11:46:53 +0200481 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200482 svc->fwmark,
483 IP_VS_DBG_ADDR(svc->af, &svc->addr),
Julian Anastasovceec4c32013-03-22 11:46:53 +0200484 ntohs(svc->port));
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300485 if (do_delay)
486 call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
487 else
488 ip_vs_service_free(svc);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492
493/*
494 * Returns hash value for real service
495 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000496static inline unsigned int ip_vs_rs_hashkey(int af,
Julius Volz7937df12008-09-02 15:55:48 +0200497 const union nf_inet_addr *addr,
498 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Eric Dumazet95c96172012-04-15 05:58:06 +0000500 register unsigned int porth = ntohs(port);
Julius Volz7937df12008-09-02 15:55:48 +0200501 __be32 addr_fold = addr->ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Julius Volz7937df12008-09-02 15:55:48 +0200503#ifdef CONFIG_IP_VS_IPV6
504 if (af == AF_INET6)
505 addr_fold = addr->ip6[0]^addr->ip6[1]^
506 addr->ip6[2]^addr->ip6[3];
507#endif
508
509 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 & IP_VS_RTAB_MASK;
511}
512
Julian Anastasov276472e2013-03-21 11:58:08 +0200513/* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
514static void ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Eric Dumazet95c96172012-04-15 05:58:06 +0000516 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Julian Anastasov276472e2013-03-21 11:58:08 +0200518 if (dest->in_rs_table)
519 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /*
522 * Hash by proto,addr,port,
523 * which are the parameters of the real service.
524 */
Julius Volz7937df12008-09-02 15:55:48 +0200525 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
526
Julian Anastasov276472e2013-03-21 11:58:08 +0200527 hlist_add_head_rcu(&dest->d_list, &ipvs->rs_table[hash]);
528 dest->in_rs_table = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Julian Anastasov276472e2013-03-21 11:58:08 +0200531/* Unhash ip_vs_dest from rs_table. */
532static void ip_vs_rs_unhash(struct ip_vs_dest *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 /*
Hans Schillstromfc723252011-01-03 14:44:43 +0100535 * Remove it from the rs_table table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 */
Julian Anastasov276472e2013-03-21 11:58:08 +0200537 if (dest->in_rs_table) {
538 hlist_del_rcu(&dest->d_list);
539 dest->in_rs_table = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Julian Anastasov276472e2013-03-21 11:58:08 +0200543/* Check if real service by <proto,addr,port> is present */
Eric W. Biederman48aed1b2015-09-21 13:01:50 -0500544bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
Julian Anastasov276472e2013-03-21 11:58:08 +0200545 const union nf_inet_addr *daddr, __be16 dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Eric Dumazet95c96172012-04-15 05:58:06 +0000547 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct ip_vs_dest *dest;
549
Julian Anastasov276472e2013-03-21 11:58:08 +0200550 /* Check for "full" addressed entries */
Julius Volz7937df12008-09-02 15:55:48 +0200551 hash = ip_vs_rs_hashkey(af, daddr, dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Julian Anastasov276472e2013-03-21 11:58:08 +0200553 rcu_read_lock();
554 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
555 if (dest->port == dport &&
556 dest->af == af &&
557 ip_vs_addr_equal(af, &dest->addr, daddr) &&
558 (dest->protocol == protocol || dest->vfwmark)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* HIT */
Julian Anastasov276472e2013-03-21 11:58:08 +0200560 rcu_read_unlock();
561 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 }
Julian Anastasov276472e2013-03-21 11:58:08 +0200564 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Julian Anastasov276472e2013-03-21 11:58:08 +0200566 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Marco Angaroni39b97222016-04-05 18:26:29 +0200569/* Find real service record by <proto,addr,port>.
570 * In case of multiple records with the same <proto,addr,port>, only
571 * the first found record is returned.
572 *
573 * To be called under RCU lock.
574 */
575struct ip_vs_dest *ip_vs_find_real_service(struct netns_ipvs *ipvs, int af,
576 __u16 protocol,
577 const union nf_inet_addr *daddr,
578 __be16 dport)
579{
580 unsigned int hash;
581 struct ip_vs_dest *dest;
582
583 /* Check for "full" addressed entries */
584 hash = ip_vs_rs_hashkey(af, daddr, dport);
585
586 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
587 if (dest->port == dport &&
588 dest->af == af &&
589 ip_vs_addr_equal(af, &dest->addr, daddr) &&
590 (dest->protocol == protocol || dest->vfwmark)) {
591 /* HIT */
592 return dest;
593 }
594 }
595
596 return NULL;
597}
598
Julian Anastasov413c2d042013-03-22 11:46:52 +0200599/* Lookup destination by {addr,port} in the given service
600 * Called under RCU lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
602static struct ip_vs_dest *
Alex Gartrell655eef12014-09-09 16:40:21 -0700603ip_vs_lookup_dest(struct ip_vs_service *svc, int dest_af,
604 const union nf_inet_addr *daddr, __be16 dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct ip_vs_dest *dest;
607
608 /*
609 * Find the destination for the given service
610 */
Julian Anastasov413c2d042013-03-22 11:46:52 +0200611 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
Alex Gartrell655eef12014-09-09 16:40:21 -0700612 if ((dest->af == dest_af) &&
613 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
614 (dest->port == dport)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* HIT */
616 return dest;
617 }
618 }
619
620 return NULL;
621}
622
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800623/*
624 * Find destination by {daddr,dport,vaddr,protocol}
Julian Anastasov413c2d042013-03-22 11:46:52 +0200625 * Created to be used in ip_vs_process_message() in
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800626 * the backup synchronization daemon. It finds the
627 * destination to be bound to the received connection
628 * on the backup.
Julian Anastasov413c2d042013-03-22 11:46:52 +0200629 * Called under RCU lock, no refcnt is returned.
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800630 */
Eric W. Biedermandc2add62015-09-21 13:01:51 -0500631struct ip_vs_dest *ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
Hans Schillstromfc723252011-01-03 14:44:43 +0100632 const union nf_inet_addr *daddr,
Julius Volz7937df12008-09-02 15:55:48 +0200633 __be16 dport,
634 const union nf_inet_addr *vaddr,
Julian Anastasov52793db2011-12-30 14:19:02 +0900635 __be16 vport, __u16 protocol, __u32 fwmark,
636 __u32 flags)
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800637{
638 struct ip_vs_dest *dest;
639 struct ip_vs_service *svc;
Julian Anastasov52793db2011-12-30 14:19:02 +0900640 __be16 port = dport;
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800641
Eric W. Biedermandc2add62015-09-21 13:01:51 -0500642 svc = ip_vs_service_find(ipvs, svc_af, fwmark, protocol, vaddr, vport);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800643 if (!svc)
644 return NULL;
Julian Anastasov52793db2011-12-30 14:19:02 +0900645 if (fwmark && (flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ)
646 port = 0;
Alex Gartrell655eef12014-09-09 16:40:21 -0700647 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port);
Julian Anastasov52793db2011-12-30 14:19:02 +0900648 if (!dest)
Alex Gartrell655eef12014-09-09 16:40:21 -0700649 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port ^ dport);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800650 return dest;
651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Julian Anastasov026ace02013-03-21 11:58:06 +0200653void ip_vs_dest_dst_rcu_free(struct rcu_head *head)
654{
655 struct ip_vs_dest_dst *dest_dst = container_of(head,
656 struct ip_vs_dest_dst,
657 rcu_head);
658
659 dst_release(dest_dst->dst_cache);
660 kfree(dest_dst);
661}
662
663/* Release dest_dst and dst_cache for dest in user context */
Julian Anastasovd1deae42013-03-21 11:58:02 +0200664static void __ip_vs_dst_cache_reset(struct ip_vs_dest *dest)
665{
Julian Anastasov026ace02013-03-21 11:58:06 +0200666 struct ip_vs_dest_dst *old;
Julian Anastasovd1deae42013-03-21 11:58:02 +0200667
Julian Anastasov026ace02013-03-21 11:58:06 +0200668 old = rcu_dereference_protected(dest->dest_dst, 1);
669 if (old) {
670 RCU_INIT_POINTER(dest->dest_dst, NULL);
671 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
672 }
Julian Anastasovd1deae42013-03-21 11:58:02 +0200673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/*
676 * Lookup dest by {svc,addr,port} in the destination trash.
677 * The destination trash is used to hold the destinations that are removed
678 * from the service table but are still referenced by some conn entries.
679 * The reason to add the destination trash is when the dest is temporary
680 * down (either by administrator or by monitor program), the dest can be
681 * picked back from the trash, the remaining connections to the dest can
682 * continue, and the counting information of the dest is also useful for
683 * scheduling.
684 */
685static struct ip_vs_dest *
Alex Gartrellad147aa2014-09-09 16:40:22 -0700686ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af,
687 const union nf_inet_addr *daddr, __be16 dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200689 struct ip_vs_dest *dest;
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500690 struct netns_ipvs *ipvs = svc->ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /*
693 * Find the destination in trash
694 */
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200695 spin_lock_bh(&ipvs->dest_trash_lock);
696 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
Julius Volz7937df12008-09-02 15:55:48 +0200697 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
698 "dest->refcnt=%d\n",
699 dest->vfwmark,
Alex Gartrellad147aa2014-09-09 16:40:22 -0700700 IP_VS_DBG_ADDR(dest->af, &dest->addr),
Julius Volz7937df12008-09-02 15:55:48 +0200701 ntohs(dest->port),
702 atomic_read(&dest->refcnt));
Alex Gartrellad147aa2014-09-09 16:40:22 -0700703 if (dest->af == dest_af &&
704 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 dest->port == dport &&
706 dest->vfwmark == svc->fwmark &&
707 dest->protocol == svc->protocol &&
708 (svc->fwmark ||
Julius Volz7937df12008-09-02 15:55:48 +0200709 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 dest->vport == svc->port))) {
711 /* HIT */
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200712 list_del(&dest->t_list);
713 ip_vs_dest_hold(dest);
714 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716 }
717
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200718 dest = NULL;
719
720out:
721 spin_unlock_bh(&ipvs->dest_trash_lock);
722
723 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200726static void ip_vs_dest_free(struct ip_vs_dest *dest)
727{
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300728 struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
729
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200730 __ip_vs_dst_cache_reset(dest);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300731 __ip_vs_svc_put(svc, false);
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200732 free_percpu(dest->stats.cpustats);
Julian Anastasov9e4e9482013-10-09 09:24:27 +0300733 ip_vs_dest_put_and_free(dest);
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200734}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736/*
737 * Clean up all the destinations in the trash
738 * Called by the ip_vs_control_cleanup()
739 *
740 * When the ip_vs_control_clearup is activated by ipvs module exit,
741 * the service tables must have been flushed and all the connections
742 * are expired, and the refcnt of each destination in the trash must
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200743 * be 0, so we simply release them here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
Eric W. Biederman6c0e14f2015-09-21 13:01:52 -0500745static void ip_vs_trash_cleanup(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct ip_vs_dest *dest, *nxt;
748
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200749 del_timer_sync(&ipvs->dest_trash_timer);
750 /* No need to use dest_trash_lock */
751 list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, t_list) {
752 list_del(&dest->t_list);
753 ip_vs_dest_free(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755}
756
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200757static void
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200758ip_vs_copy_stats(struct ip_vs_kstats *dst, struct ip_vs_stats *src)
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200759{
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200760#define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200761
762 spin_lock_bh(&src->lock);
763
764 IP_VS_SHOW_STATS_COUNTER(conns);
765 IP_VS_SHOW_STATS_COUNTER(inpkts);
766 IP_VS_SHOW_STATS_COUNTER(outpkts);
767 IP_VS_SHOW_STATS_COUNTER(inbytes);
768 IP_VS_SHOW_STATS_COUNTER(outbytes);
769
Julian Anastasovea9f22c2011-03-14 01:41:54 +0200770 ip_vs_read_estimator(dst, src);
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200771
772 spin_unlock_bh(&src->lock);
773}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775static void
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200776ip_vs_export_stats_user(struct ip_vs_stats_user *dst, struct ip_vs_kstats *src)
777{
778 dst->conns = (u32)src->conns;
779 dst->inpkts = (u32)src->inpkts;
780 dst->outpkts = (u32)src->outpkts;
781 dst->inbytes = src->inbytes;
782 dst->outbytes = src->outbytes;
783 dst->cps = (u32)src->cps;
784 dst->inpps = (u32)src->inpps;
785 dst->outpps = (u32)src->outpps;
786 dst->inbps = (u32)src->inbps;
787 dst->outbps = (u32)src->outbps;
788}
789
790static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791ip_vs_zero_stats(struct ip_vs_stats *stats)
792{
793 spin_lock_bh(&stats->lock);
Simon Hormane93615d2008-08-11 17:19:14 +1000794
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200795 /* get current counters as zero point, rates are zeroed */
796
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200797#define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200798
799 IP_VS_ZERO_STATS_COUNTER(conns);
800 IP_VS_ZERO_STATS_COUNTER(inpkts);
801 IP_VS_ZERO_STATS_COUNTER(outpkts);
802 IP_VS_ZERO_STATS_COUNTER(inbytes);
803 IP_VS_ZERO_STATS_COUNTER(outbytes);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ip_vs_zero_estimator(stats);
Simon Hormane93615d2008-08-11 17:19:14 +1000806
Sven Wegener3a14a3132008-08-10 18:24:41 +0000807 spin_unlock_bh(&stats->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810/*
811 * Update a destination in the given service
812 */
813static void
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200814__ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
815 struct ip_vs_dest_user_kern *udest, int add)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500817 struct netns_ipvs *ipvs = svc->ipvs;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300818 struct ip_vs_service *old_svc;
Julian Anastasovceec4c32013-03-22 11:46:53 +0200819 struct ip_vs_scheduler *sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 int conn_flags;
821
Alex Gartrell391f503d2014-09-09 16:40:24 -0700822 /* We cannot modify an address and change the address family */
823 BUG_ON(!add && udest->af != dest->af);
824
825 if (add && udest->af != svc->af)
826 ipvs->mixed_address_family_dests++;
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* set the weight and the flags */
829 atomic_set(&dest->weight, udest->weight);
Julian Anastasov35757922010-09-17 14:18:16 +0200830 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
831 conn_flags |= IP_VS_CONN_F_INACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
Julian Anastasov35757922010-09-17 14:18:16 +0200834 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
836 } else {
837 /*
Hans Schillstromfc723252011-01-03 14:44:43 +0100838 * Put the real service in rs_table if not present.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 * For now only for NAT!
840 */
Hans Schillstromfc723252011-01-03 14:44:43 +0100841 ip_vs_rs_hash(ipvs, dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 atomic_set(&dest->conn_flags, conn_flags);
844
845 /* bind the service */
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300846 old_svc = rcu_dereference_protected(dest->svc, 1);
847 if (!old_svc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 __ip_vs_bind_svc(dest, svc);
849 } else {
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300850 if (old_svc != svc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 ip_vs_zero_stats(&dest->stats);
852 __ip_vs_bind_svc(dest, svc);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300853 __ip_vs_svc_put(old_svc, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 }
856
857 /* set the dest status flags */
858 dest->flags |= IP_VS_DEST_F_AVAILABLE;
859
860 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
861 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
862 dest->u_threshold = udest->u_threshold;
863 dest->l_threshold = udest->l_threshold;
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200864
Alex Gartrell6cff3392014-09-09 16:40:20 -0700865 dest->af = udest->af;
866
Julian Anastasovff75f402011-02-22 10:40:25 +0200867 spin_lock_bh(&dest->dst_lock);
Julian Anastasovd1deae42013-03-21 11:58:02 +0200868 __ip_vs_dst_cache_reset(dest);
Julian Anastasovff75f402011-02-22 10:40:25 +0200869 spin_unlock_bh(&dest->dst_lock);
Julian Anastasovfc604762010-10-17 16:38:15 +0300870
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200871 if (add) {
Eric W. Biederman0f34d542015-09-21 13:02:23 -0500872 ip_vs_start_estimator(svc->ipvs, &dest->stats);
Julian Anastasov413c2d042013-03-22 11:46:52 +0200873 list_add_rcu(&dest->n_list, &svc->destinations);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200874 svc->num_dests++;
Julian Anastasov05f00502015-06-29 21:51:40 +0300875 sched = rcu_dereference_protected(svc->scheduler, 1);
876 if (sched && sched->add_dest)
Julian Anastasovceec4c32013-03-22 11:46:53 +0200877 sched->add_dest(svc, dest);
Julian Anastasov6b6df462013-03-22 11:46:37 +0200878 } else {
Julian Anastasov05f00502015-06-29 21:51:40 +0300879 sched = rcu_dereference_protected(svc->scheduler, 1);
880 if (sched && sched->upd_dest)
Julian Anastasovceec4c32013-03-22 11:46:53 +0200881 sched->upd_dest(svc, dest);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885
886/*
887 * Create a destination for the given service
888 */
889static int
Julius Volzc860c6b2008-09-02 15:55:36 +0200890ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct ip_vs_dest **dest_p)
892{
893 struct ip_vs_dest *dest;
John Stultz827da442013-10-07 15:51:58 -0700894 unsigned int atype, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 EnterFunction(2);
897
Vince Busam09571c72008-09-02 15:55:52 +0200898#ifdef CONFIG_IP_VS_IPV6
Alex Gartrell6cff3392014-09-09 16:40:20 -0700899 if (udest->af == AF_INET6) {
Vince Busam09571c72008-09-02 15:55:52 +0200900 atype = ipv6_addr_type(&udest->addr.in6);
Sven Wegener3bfb92f2008-09-05 16:53:49 +0200901 if ((!(atype & IPV6_ADDR_UNICAST) ||
902 atype & IPV6_ADDR_LINKLOCAL) &&
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500903 !__ip_vs_addr_is_local_v6(svc->ipvs->net, &udest->addr.in6))
Vince Busam09571c72008-09-02 15:55:52 +0200904 return -EINVAL;
905 } else
906#endif
907 {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500908 atype = inet_addr_type(svc->ipvs->net, udest->addr.ip);
Vince Busam09571c72008-09-02 15:55:52 +0200909 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
910 return -EINVAL;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Simon Hormandee06e42010-08-26 02:54:31 +0000913 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
Joe Perches0a9ee812011-08-29 14:17:25 -0700914 if (dest == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -0700916
Hans Schillstromb17fc992011-01-03 14:44:56 +0100917 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
Joe Perches0a9ee812011-08-29 14:17:25 -0700918 if (!dest->stats.cpustats)
Hans Schillstromb17fc992011-01-03 14:44:56 +0100919 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
John Stultz827da442013-10-07 15:51:58 -0700921 for_each_possible_cpu(i) {
922 struct ip_vs_cpu_stats *ip_vs_dest_stats;
923 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
924 u64_stats_init(&ip_vs_dest_stats->syncp);
925 }
926
Alex Gartrell6cff3392014-09-09 16:40:20 -0700927 dest->af = udest->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 dest->protocol = svc->protocol;
Julius Volzc860c6b2008-09-02 15:55:36 +0200929 dest->vaddr = svc->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 dest->vport = svc->port;
931 dest->vfwmark = svc->fwmark;
Alex Gartrell6cff3392014-09-09 16:40:20 -0700932 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 dest->port = udest->port;
934
935 atomic_set(&dest->activeconns, 0);
936 atomic_set(&dest->inactconns, 0);
937 atomic_set(&dest->persistconns, 0);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200938 atomic_set(&dest->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Julian Anastasov276472e2013-03-21 11:58:08 +0200940 INIT_HLIST_NODE(&dest->d_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 spin_lock_init(&dest->dst_lock);
942 spin_lock_init(&dest->stats.lock);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200943 __ip_vs_update_dest(svc, dest, udest, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 *dest_p = dest;
946
947 LeaveFunction(2);
948 return 0;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100949
950err_alloc:
951 kfree(dest);
952 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955
956/*
957 * Add a destination into an existing service
958 */
959static int
Julius Volzc860c6b2008-09-02 15:55:36 +0200960ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 struct ip_vs_dest *dest;
Julius Volzc860c6b2008-09-02 15:55:36 +0200963 union nf_inet_addr daddr;
Al Viro014d7302006-09-28 14:29:52 -0700964 __be16 dport = udest->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 int ret;
966
967 EnterFunction(2);
968
969 if (udest->weight < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000970 pr_err("%s(): server weight less than zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return -ERANGE;
972 }
973
974 if (udest->l_threshold > udest->u_threshold) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000975 pr_err("%s(): lower threshold is higher than upper threshold\n",
976 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -ERANGE;
978 }
979
Alex Gartrell6cff3392014-09-09 16:40:20 -0700980 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
Julius Volzc860c6b2008-09-02 15:55:36 +0200981
Julian Anastasov413c2d042013-03-22 11:46:52 +0200982 /* We use function that requires RCU lock */
983 rcu_read_lock();
Alex Gartrell655eef12014-09-09 16:40:21 -0700984 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
Julian Anastasov413c2d042013-03-22 11:46:52 +0200985 rcu_read_unlock();
Julius Volz7937df12008-09-02 15:55:48 +0200986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (dest != NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000988 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return -EEXIST;
990 }
991
992 /*
993 * Check if the dest already exists in the trash and
994 * is from the same service
995 */
Alex Gartrellad147aa2014-09-09 16:40:22 -0700996 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
Julius Volz7937df12008-09-02 15:55:48 +0200997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (dest != NULL) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200999 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
1000 "dest->refcnt=%d, service %u/%s:%u\n",
Alex Gartrell6cff3392014-09-09 16:40:20 -07001001 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
Julius Volzcfc78c52008-09-02 15:55:53 +02001002 atomic_read(&dest->refcnt),
1003 dest->vfwmark,
1004 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
1005 ntohs(dest->vport));
1006
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001007 __ip_vs_update_dest(svc, dest, udest, 1);
1008 ret = 0;
1009 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /*
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001011 * Allocate and initialize the dest structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001013 ret = ip_vs_new_dest(svc, udest, &dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 LeaveFunction(2);
1016
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001017 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020
1021/*
1022 * Edit a destination in the given service
1023 */
1024static int
Julius Volzc860c6b2008-09-02 15:55:36 +02001025ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct ip_vs_dest *dest;
Julius Volzc860c6b2008-09-02 15:55:36 +02001028 union nf_inet_addr daddr;
Al Viro014d7302006-09-28 14:29:52 -07001029 __be16 dport = udest->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 EnterFunction(2);
1032
1033 if (udest->weight < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001034 pr_err("%s(): server weight less than zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return -ERANGE;
1036 }
1037
1038 if (udest->l_threshold > udest->u_threshold) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001039 pr_err("%s(): lower threshold is higher than upper threshold\n",
1040 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -ERANGE;
1042 }
1043
Alex Gartrell6cff3392014-09-09 16:40:20 -07001044 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
Julius Volzc860c6b2008-09-02 15:55:36 +02001045
Julian Anastasov413c2d042013-03-22 11:46:52 +02001046 /* We use function that requires RCU lock */
1047 rcu_read_lock();
Alex Gartrell655eef12014-09-09 16:40:21 -07001048 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
Julian Anastasov413c2d042013-03-22 11:46:52 +02001049 rcu_read_unlock();
Julius Volz7937df12008-09-02 15:55:48 +02001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (dest == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001052 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return -ENOENT;
1054 }
1055
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001056 __ip_vs_update_dest(svc, dest, udest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 LeaveFunction(2);
1058
1059 return 0;
1060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062/*
1063 * Delete a destination (must be already unlinked from the service)
1064 */
Eric W. Biederman79ac82e2015-09-21 13:01:53 -05001065static void __ip_vs_del_dest(struct netns_ipvs *ipvs, struct ip_vs_dest *dest,
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001066 bool cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Eric W. Biederman0f34d542015-09-21 13:02:23 -05001068 ip_vs_stop_estimator(ipvs, &dest->stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 /*
1071 * Remove it from the d-linked list with the real services.
1072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ip_vs_rs_unhash(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001075 spin_lock_bh(&ipvs->dest_trash_lock);
1076 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1077 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1078 atomic_read(&dest->refcnt));
1079 if (list_empty(&ipvs->dest_trash) && !cleanup)
1080 mod_timer(&ipvs->dest_trash_timer,
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001081 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001082 /* dest lives in trash without reference */
1083 list_add(&dest->t_list, &ipvs->dest_trash);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001084 dest->idle_start = 0;
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001085 spin_unlock_bh(&ipvs->dest_trash_lock);
1086 ip_vs_dest_put(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089
1090/*
1091 * Unlink a destination from the given service
1092 */
1093static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1094 struct ip_vs_dest *dest,
1095 int svcupd)
1096{
1097 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1098
1099 /*
1100 * Remove it from the d-linked destination list.
1101 */
Julian Anastasov413c2d042013-03-22 11:46:52 +02001102 list_del_rcu(&dest->n_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 svc->num_dests--;
Sven Wegener82dfb6f2008-08-11 19:36:06 +00001104
Alex Gartrell391f503d2014-09-09 16:40:24 -07001105 if (dest->af != svc->af)
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001106 svc->ipvs->mixed_address_family_dests--;
Alex Gartrell391f503d2014-09-09 16:40:24 -07001107
Julian Anastasovceec4c32013-03-22 11:46:53 +02001108 if (svcupd) {
1109 struct ip_vs_scheduler *sched;
Julian Anastasov6b6df462013-03-22 11:46:37 +02001110
Julian Anastasovceec4c32013-03-22 11:46:53 +02001111 sched = rcu_dereference_protected(svc->scheduler, 1);
Julian Anastasov05f00502015-06-29 21:51:40 +03001112 if (sched && sched->del_dest)
Julian Anastasovceec4c32013-03-22 11:46:53 +02001113 sched->del_dest(svc, dest);
1114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117
1118/*
1119 * Delete a destination server in the given service
1120 */
1121static int
Julius Volzc860c6b2008-09-02 15:55:36 +02001122ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 struct ip_vs_dest *dest;
Al Viro014d7302006-09-28 14:29:52 -07001125 __be16 dport = udest->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 EnterFunction(2);
1128
Julian Anastasov413c2d042013-03-22 11:46:52 +02001129 /* We use function that requires RCU lock */
1130 rcu_read_lock();
Alex Gartrell655eef12014-09-09 16:40:21 -07001131 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
Julian Anastasov413c2d042013-03-22 11:46:52 +02001132 rcu_read_unlock();
Julius Volzc860c6b2008-09-02 15:55:36 +02001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (dest == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001135 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return -ENOENT;
1137 }
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /*
1140 * Unlink dest from the service
1141 */
1142 __ip_vs_unlink_dest(svc, dest, 1);
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /*
1145 * Delete the destination
1146 */
Eric W. Biederman79ac82e2015-09-21 13:01:53 -05001147 __ip_vs_del_dest(svc->ipvs, dest, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 LeaveFunction(2);
1150
1151 return 0;
1152}
1153
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001154static void ip_vs_dest_trash_expire(unsigned long data)
1155{
Eric W. Biederman8e743f12015-09-21 13:01:54 -05001156 struct netns_ipvs *ipvs = (struct netns_ipvs *)data;
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001157 struct ip_vs_dest *dest, *next;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001158 unsigned long now = jiffies;
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001159
1160 spin_lock(&ipvs->dest_trash_lock);
1161 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001162 if (atomic_read(&dest->refcnt) > 0)
1163 continue;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001164 if (dest->idle_start) {
1165 if (time_before(now, dest->idle_start +
1166 IP_VS_DEST_TRASH_PERIOD))
1167 continue;
1168 } else {
1169 dest->idle_start = max(1UL, now);
1170 continue;
1171 }
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001172 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1173 dest->vfwmark,
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001174 IP_VS_DBG_ADDR(dest->af, &dest->addr),
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001175 ntohs(dest->port));
1176 list_del(&dest->t_list);
1177 ip_vs_dest_free(dest);
1178 }
1179 if (!list_empty(&ipvs->dest_trash))
1180 mod_timer(&ipvs->dest_trash_timer,
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001181 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001182 spin_unlock(&ipvs->dest_trash_lock);
1183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185/*
1186 * Add a service into the service hash table
1187 */
1188static int
Eric W. Biederman5060bd82015-09-21 13:01:56 -05001189ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
Julius Volzc860c6b2008-09-02 15:55:36 +02001190 struct ip_vs_service **svc_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
John Stultz827da442013-10-07 15:51:58 -07001192 int ret = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 struct ip_vs_scheduler *sched = NULL;
Simon Horman0d1e71b2010-08-22 21:37:54 +09001194 struct ip_vs_pe *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 struct ip_vs_service *svc = NULL;
1196
1197 /* increase the module use count */
1198 ip_vs_use_count_inc();
1199
1200 /* Lookup the scheduler by 'u->sched_name' */
Julian Anastasov05f00502015-06-29 21:51:40 +03001201 if (strcmp(u->sched_name, "none")) {
1202 sched = ip_vs_scheduler_get(u->sched_name);
1203 if (!sched) {
1204 pr_info("Scheduler module ip_vs_%s not found\n",
1205 u->sched_name);
1206 ret = -ENOENT;
1207 goto out_err;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210
Simon Horman0d1e71b2010-08-22 21:37:54 +09001211 if (u->pe_name && *u->pe_name) {
Simon Hormane9e5eee2010-11-08 20:05:57 +09001212 pe = ip_vs_pe_getbyname(u->pe_name);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001213 if (pe == NULL) {
1214 pr_info("persistence engine module ip_vs_pe_%s "
1215 "not found\n", u->pe_name);
1216 ret = -ENOENT;
1217 goto out_err;
1218 }
1219 }
1220
Julius Volzf94fd042008-09-02 15:55:55 +02001221#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov0a925862013-04-17 23:50:49 +03001222 if (u->af == AF_INET6) {
1223 __u32 plen = (__force __u32) u->netmask;
1224
1225 if (plen < 1 || plen > 128) {
1226 ret = -EINVAL;
1227 goto out_err;
1228 }
Julius Volzf94fd042008-09-02 15:55:55 +02001229 }
1230#endif
1231
Simon Hormandee06e42010-08-26 02:54:31 +00001232 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (svc == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001234 IP_VS_DBG(1, "%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ret = -ENOMEM;
1236 goto out_err;
1237 }
Hans Schillstromb17fc992011-01-03 14:44:56 +01001238 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
Julia Lawall0a54e932012-08-29 06:49:11 +00001239 if (!svc->stats.cpustats) {
1240 ret = -ENOMEM;
Hans Schillstromb17fc992011-01-03 14:44:56 +01001241 goto out_err;
Julia Lawall0a54e932012-08-29 06:49:11 +00001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
John Stultz827da442013-10-07 15:51:58 -07001244 for_each_possible_cpu(i) {
1245 struct ip_vs_cpu_stats *ip_vs_stats;
1246 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1247 u64_stats_init(&ip_vs_stats->syncp);
1248 }
1249
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /* I'm the first user of the service */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 atomic_set(&svc->refcnt, 0);
1253
Julius Volzc860c6b2008-09-02 15:55:36 +02001254 svc->af = u->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 svc->protocol = u->protocol;
Julius Volzc860c6b2008-09-02 15:55:36 +02001256 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 svc->port = u->port;
1258 svc->fwmark = u->fwmark;
1259 svc->flags = u->flags;
1260 svc->timeout = u->timeout * HZ;
1261 svc->netmask = u->netmask;
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001262 svc->ipvs = ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 INIT_LIST_HEAD(&svc->destinations);
Julian Anastasovba3a3ce2013-03-22 11:46:51 +02001265 spin_lock_init(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 spin_lock_init(&svc->stats.lock);
1267
1268 /* Bind the scheduler */
Julian Anastasov05f00502015-06-29 21:51:40 +03001269 if (sched) {
1270 ret = ip_vs_bind_scheduler(svc, sched);
1271 if (ret)
1272 goto out_err;
1273 sched = NULL;
1274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Simon Horman0d1e71b2010-08-22 21:37:54 +09001276 /* Bind the ct retriever */
Julian Anastasovceec4c32013-03-22 11:46:53 +02001277 RCU_INIT_POINTER(svc->pe, pe);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001278 pe = NULL;
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* Update the virtual service counters */
1281 if (svc->port == FTPPORT)
Hans Schillstrom763f8d02011-01-03 14:45:01 +01001282 atomic_inc(&ipvs->ftpsvc_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 else if (svc->port == 0)
Hans Schillstrom763f8d02011-01-03 14:45:01 +01001284 atomic_inc(&ipvs->nullsvc_counter);
Marco Angaroni39b97222016-04-05 18:26:29 +02001285 if (svc->pe && svc->pe->conn_out)
1286 atomic_inc(&ipvs->conn_out_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Eric W. Biederman0f34d542015-09-21 13:02:23 -05001288 ip_vs_start_estimator(ipvs, &svc->stats);
Julius Volzf94fd042008-09-02 15:55:55 +02001289
1290 /* Count only IPv4 services for old get/setsockopt interface */
1291 if (svc->af == AF_INET)
Hans Schillstroma0840e22011-01-03 14:44:58 +01001292 ipvs->num_services++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 /* Hash the service into the service table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 ip_vs_svc_hash(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 *svc_p = svc;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001298 /* Now there is a service - full throttle */
1299 ipvs->enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return 0;
1301
Hans Schillstromb17fc992011-01-03 14:44:56 +01001302
Simon Horman6e08bfb2010-08-22 21:37:52 +09001303 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (svc != NULL) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001305 ip_vs_unbind_scheduler(svc, sched);
1306 ip_vs_service_free(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308 ip_vs_scheduler_put(sched);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001309 ip_vs_pe_put(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /* decrease the module use count */
1312 ip_vs_use_count_dec();
1313
1314 return ret;
1315}
1316
1317
1318/*
1319 * Edit a service and bind it with a new scheduler
1320 */
1321static int
Julius Volzc860c6b2008-09-02 15:55:36 +02001322ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Julian Anastasov05f00502015-06-29 21:51:40 +03001324 struct ip_vs_scheduler *sched = NULL, *old_sched;
Simon Horman0d1e71b2010-08-22 21:37:54 +09001325 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 int ret = 0;
Marco Angaroni39b97222016-04-05 18:26:29 +02001327 bool new_pe_conn_out, old_pe_conn_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 /*
1330 * Lookup the scheduler, by 'u->sched_name'
1331 */
Julian Anastasov05f00502015-06-29 21:51:40 +03001332 if (strcmp(u->sched_name, "none")) {
1333 sched = ip_vs_scheduler_get(u->sched_name);
1334 if (!sched) {
1335 pr_info("Scheduler module ip_vs_%s not found\n",
1336 u->sched_name);
1337 return -ENOENT;
1338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 old_sched = sched;
1341
Simon Horman0d1e71b2010-08-22 21:37:54 +09001342 if (u->pe_name && *u->pe_name) {
Simon Hormane9e5eee2010-11-08 20:05:57 +09001343 pe = ip_vs_pe_getbyname(u->pe_name);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001344 if (pe == NULL) {
1345 pr_info("persistence engine module ip_vs_pe_%s "
1346 "not found\n", u->pe_name);
1347 ret = -ENOENT;
1348 goto out;
1349 }
1350 old_pe = pe;
1351 }
1352
Julius Volzf94fd042008-09-02 15:55:55 +02001353#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov0a925862013-04-17 23:50:49 +03001354 if (u->af == AF_INET6) {
1355 __u32 plen = (__force __u32) u->netmask;
1356
1357 if (plen < 1 || plen > 128) {
1358 ret = -EINVAL;
1359 goto out;
1360 }
Julius Volzf94fd042008-09-02 15:55:55 +02001361 }
1362#endif
1363
Julian Anastasovceec4c32013-03-22 11:46:53 +02001364 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1365 if (sched != old_sched) {
Julian Anastasov05f00502015-06-29 21:51:40 +03001366 if (old_sched) {
1367 ip_vs_unbind_scheduler(svc, old_sched);
1368 RCU_INIT_POINTER(svc->scheduler, NULL);
1369 /* Wait all svc->sched_data users */
1370 synchronize_rcu();
Julian Anastasovceec4c32013-03-22 11:46:53 +02001371 }
Julian Anastasov05f00502015-06-29 21:51:40 +03001372 /* Bind the new scheduler */
1373 if (sched) {
1374 ret = ip_vs_bind_scheduler(svc, sched);
1375 if (ret) {
1376 ip_vs_scheduler_put(sched);
1377 goto out;
1378 }
1379 }
Julian Anastasovceec4c32013-03-22 11:46:53 +02001380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 /*
1383 * Set the flags and timeout value
1384 */
1385 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1386 svc->timeout = u->timeout * HZ;
1387 svc->netmask = u->netmask;
1388
Julian Anastasovceec4c32013-03-22 11:46:53 +02001389 old_pe = rcu_dereference_protected(svc->pe, 1);
Marco Angaroni39b97222016-04-05 18:26:29 +02001390 if (pe != old_pe) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001391 rcu_assign_pointer(svc->pe, pe);
Marco Angaroni39b97222016-04-05 18:26:29 +02001392 /* check for optional methods in new pe */
1393 new_pe_conn_out = (pe && pe->conn_out) ? true : false;
1394 old_pe_conn_out = (old_pe && old_pe->conn_out) ? true : false;
1395 if (new_pe_conn_out && !old_pe_conn_out)
1396 atomic_inc(&svc->ipvs->conn_out_counter);
1397 if (old_pe_conn_out && !new_pe_conn_out)
1398 atomic_dec(&svc->ipvs->conn_out_counter);
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Hans Schillstrom552ad652011-06-13 12:19:26 +02001401out:
Simon Horman6e08bfb2010-08-22 21:37:52 +09001402 ip_vs_scheduler_put(old_sched);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001403 ip_vs_pe_put(old_pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return ret;
1405}
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407/*
1408 * Delete a service from the service list
1409 * - The service must be unlinked, unlocked and not referenced!
1410 * - We are called under _bh lock
1411 */
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001412static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 struct ip_vs_dest *dest, *nxt;
1415 struct ip_vs_scheduler *old_sched;
Simon Horman0d1e71b2010-08-22 21:37:54 +09001416 struct ip_vs_pe *old_pe;
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001417 struct netns_ipvs *ipvs = svc->ipvs;
Simon Horman0d1e71b2010-08-22 21:37:54 +09001418
Julius Volzf94fd042008-09-02 15:55:55 +02001419 /* Count only IPv4 services for old get/setsockopt interface */
1420 if (svc->af == AF_INET)
Hans Schillstroma0840e22011-01-03 14:44:58 +01001421 ipvs->num_services--;
Julius Volzf94fd042008-09-02 15:55:55 +02001422
Eric W. Biederman0f34d542015-09-21 13:02:23 -05001423 ip_vs_stop_estimator(svc->ipvs, &svc->stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 /* Unbind scheduler */
Julian Anastasovceec4c32013-03-22 11:46:53 +02001426 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1427 ip_vs_unbind_scheduler(svc, old_sched);
Simon Horman6e08bfb2010-08-22 21:37:52 +09001428 ip_vs_scheduler_put(old_sched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Julian Anastasovceec4c32013-03-22 11:46:53 +02001430 /* Unbind persistence engine, keep svc->pe */
1431 old_pe = rcu_dereference_protected(svc->pe, 1);
Marco Angaroni39b97222016-04-05 18:26:29 +02001432 if (old_pe && old_pe->conn_out)
1433 atomic_dec(&ipvs->conn_out_counter);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001434 ip_vs_pe_put(old_pe);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /*
1437 * Unlink the whole destination list
1438 */
1439 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1440 __ip_vs_unlink_dest(svc, dest, 0);
Eric W. Biederman79ac82e2015-09-21 13:01:53 -05001441 __ip_vs_del_dest(svc->ipvs, dest, cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
1444 /*
1445 * Update the virtual service counters
1446 */
1447 if (svc->port == FTPPORT)
Hans Schillstrom763f8d02011-01-03 14:45:01 +01001448 atomic_dec(&ipvs->ftpsvc_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 else if (svc->port == 0)
Hans Schillstrom763f8d02011-01-03 14:45:01 +01001450 atomic_dec(&ipvs->nullsvc_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 /*
1453 * Free the service if nobody refers to it
1454 */
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001455 __ip_vs_svc_put(svc, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /* decrease the module use count */
1458 ip_vs_use_count_dec();
1459}
1460
1461/*
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001462 * Unlink a service from list and try to delete it if its refcnt reached 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 */
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001464static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Julian Anastasovceec4c32013-03-22 11:46:53 +02001466 /* Hold svc to avoid double release from dest_trash */
1467 atomic_inc(&svc->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 /*
1469 * Unhash it from the service table
1470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ip_vs_svc_unhash(svc);
1472
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001473 __ip_vs_del_service(svc, cleanup);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001474}
1475
1476/*
1477 * Delete a service from the service list
1478 */
1479static int ip_vs_del_service(struct ip_vs_service *svc)
1480{
1481 if (svc == NULL)
1482 return -EEXIST;
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001483 ip_vs_unlink_service(svc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 return 0;
1486}
1487
1488
1489/*
1490 * Flush all the virtual services
1491 */
Eric W. Biedermanef7c5992015-09-21 13:01:57 -05001492static int ip_vs_flush(struct netns_ipvs *ipvs, bool cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 int idx;
Julian Anastasovceec4c32013-03-22 11:46:53 +02001495 struct ip_vs_service *svc;
1496 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 /*
Hans Schillstromfc723252011-01-03 14:44:43 +01001499 * Flush the service table hashed by <netns,protocol,addr,port>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 */
1501 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001502 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1503 s_list) {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001504 if (svc->ipvs == ipvs)
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001505 ip_vs_unlink_service(svc, cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507 }
1508
1509 /*
1510 * Flush the service table hashed by fwmark
1511 */
1512 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001513 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1514 f_list) {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001515 if (svc->ipvs == ipvs)
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001516 ip_vs_unlink_service(svc, cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 }
1519
1520 return 0;
1521}
1522
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001523/*
1524 * Delete service by {netns} in the service table.
1525 * Called by __ip_vs_cleanup()
1526 */
Eric W. Biederman56d21692015-09-21 13:01:58 -05001527void ip_vs_service_net_cleanup(struct netns_ipvs *ipvs)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001528{
1529 EnterFunction(2);
1530 /* Check for "full" addressed entries */
1531 mutex_lock(&__ip_vs_mutex);
Eric W. Biedermanef7c5992015-09-21 13:01:57 -05001532 ip_vs_flush(ipvs, true);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001533 mutex_unlock(&__ip_vs_mutex);
1534 LeaveFunction(2);
1535}
Julian Anastasovd1deae42013-03-21 11:58:02 +02001536
1537/* Put all references for device (dst_cache) */
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001538static inline void
Julian Anastasovd1deae42013-03-21 11:58:02 +02001539ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001540{
Julian Anastasovd717bb22013-04-17 23:50:45 +03001541 struct ip_vs_dest_dst *dest_dst;
1542
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001543 spin_lock_bh(&dest->dst_lock);
Julian Anastasovd717bb22013-04-17 23:50:45 +03001544 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1545 if (dest_dst && dest_dst->dst_cache->dev == dev) {
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001546 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1547 dev->name,
1548 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1549 ntohs(dest->port),
1550 atomic_read(&dest->refcnt));
Julian Anastasovd1deae42013-03-21 11:58:02 +02001551 __ip_vs_dst_cache_reset(dest);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001552 }
1553 spin_unlock_bh(&dest->dst_lock);
1554
1555}
Julian Anastasov313eae62013-03-21 11:58:00 +02001556/* Netdev event receiver
1557 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001558 */
1559static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00001560 void *ptr)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001561{
Jiri Pirko351638e2013-05-28 01:30:21 +00001562 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001563 struct net *net = dev_net(dev);
Julian Anastasov283283c2012-07-07 20:30:11 +03001564 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001565 struct ip_vs_service *svc;
1566 struct ip_vs_dest *dest;
1567 unsigned int idx;
1568
Julian Anastasov313eae62013-03-21 11:58:00 +02001569 if (event != NETDEV_DOWN || !ipvs)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001570 return NOTIFY_DONE;
1571 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1572 EnterFunction(2);
1573 mutex_lock(&__ip_vs_mutex);
1574 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001575 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001576 if (svc->ipvs == ipvs) {
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001577 list_for_each_entry(dest, &svc->destinations,
1578 n_list) {
Julian Anastasovd1deae42013-03-21 11:58:02 +02001579 ip_vs_forget_dev(dest, dev);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001580 }
1581 }
1582 }
1583
Julian Anastasovceec4c32013-03-22 11:46:53 +02001584 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001585 if (svc->ipvs == ipvs) {
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001586 list_for_each_entry(dest, &svc->destinations,
1587 n_list) {
Julian Anastasovd1deae42013-03-21 11:58:02 +02001588 ip_vs_forget_dev(dest, dev);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001589 }
1590 }
1591
1592 }
1593 }
1594
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001595 spin_lock_bh(&ipvs->dest_trash_lock);
1596 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
Julian Anastasovd1deae42013-03-21 11:58:02 +02001597 ip_vs_forget_dev(dest, dev);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001598 }
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001599 spin_unlock_bh(&ipvs->dest_trash_lock);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001600 mutex_unlock(&__ip_vs_mutex);
1601 LeaveFunction(2);
1602 return NOTIFY_DONE;
1603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605/*
1606 * Zero counters in a service or all services
1607 */
1608static int ip_vs_zero_service(struct ip_vs_service *svc)
1609{
1610 struct ip_vs_dest *dest;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 list_for_each_entry(dest, &svc->destinations, n_list) {
1613 ip_vs_zero_stats(&dest->stats);
1614 }
1615 ip_vs_zero_stats(&svc->stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return 0;
1617}
1618
Eric W. Biedermanc60856c2015-09-21 13:01:59 -05001619static int ip_vs_zero_all(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 int idx;
1622 struct ip_vs_service *svc;
1623
1624 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001625 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001626 if (svc->ipvs == ipvs)
Hans Schillstromfc723252011-01-03 14:44:43 +01001627 ip_vs_zero_service(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629 }
1630
1631 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001632 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001633 if (svc->ipvs == ipvs)
Hans Schillstromfc723252011-01-03 14:44:43 +01001634 ip_vs_zero_service(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636 }
1637
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001638 ip_vs_zero_stats(&ipvs->tot_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return 0;
1640}
1641
Simon Horman14e40542011-02-04 18:33:02 +09001642#ifdef CONFIG_SYSCTL
Julian Anastasov749c42b2012-04-24 23:46:40 +03001643
1644static int zero;
1645static int three = 3;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647static int
Joe Perchesfe2c6332013-06-11 23:04:25 -07001648proc_do_defense_mode(struct ctl_table *table, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 void __user *buffer, size_t *lenp, loff_t *ppos)
1650{
Eric W. Biederman717e9172015-09-21 13:01:39 -05001651 struct netns_ipvs *ipvs = table->extra2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 int *valp = table->data;
1653 int val = *valp;
1654 int rc;
1655
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001656 rc = proc_dointvec(table, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (write && (*valp != val)) {
1658 if ((*valp < 0) || (*valp > 3)) {
1659 /* Restore the correct value */
1660 *valp = val;
1661 } else {
Eric W. Biederman717e9172015-09-21 13:01:39 -05001662 update_defense_level(ipvs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664 }
1665 return rc;
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668static int
Joe Perchesfe2c6332013-06-11 23:04:25 -07001669proc_do_sync_threshold(struct ctl_table *table, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 void __user *buffer, size_t *lenp, loff_t *ppos)
1671{
1672 int *valp = table->data;
1673 int val[2];
1674 int rc;
1675
1676 /* backup the value first */
1677 memcpy(val, valp, sizeof(val));
1678
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001679 rc = proc_dointvec(table, write, buffer, lenp, ppos);
Julian Anastasov749c42b2012-04-24 23:46:40 +03001680 if (write && (valp[0] < 0 || valp[1] < 0 ||
1681 (valp[0] >= valp[1] && valp[1]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 /* Restore the correct value */
1683 memcpy(valp, val, sizeof(val));
1684 }
1685 return rc;
1686}
1687
Hans Schillstromb880c1f2010-11-19 14:25:14 +01001688static int
Joe Perchesfe2c6332013-06-11 23:04:25 -07001689proc_do_sync_mode(struct ctl_table *table, int write,
Hans Schillstromb880c1f2010-11-19 14:25:14 +01001690 void __user *buffer, size_t *lenp, loff_t *ppos)
1691{
1692 int *valp = table->data;
1693 int val = *valp;
1694 int rc;
1695
1696 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1697 if (write && (*valp != val)) {
1698 if ((*valp < 0) || (*valp > 1)) {
1699 /* Restore the correct value */
1700 *valp = val;
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +02001701 }
1702 }
1703 return rc;
1704}
1705
1706static int
Joe Perchesfe2c6332013-06-11 23:04:25 -07001707proc_do_sync_ports(struct ctl_table *table, int write,
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +02001708 void __user *buffer, size_t *lenp, loff_t *ppos)
1709{
1710 int *valp = table->data;
1711 int val = *valp;
1712 int rc;
1713
1714 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1715 if (write && (*valp != val)) {
1716 if (*valp < 1 || !is_power_of_2(*valp)) {
1717 /* Restore the correct value */
1718 *valp = val;
Hans Schillstromb880c1f2010-11-19 14:25:14 +01001719 }
1720 }
1721 return rc;
1722}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724/*
1725 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
Hans Schillstroma0840e22011-01-03 14:44:58 +01001726 * Do not change order or insert new entries without
Hans Schillstrom503cf152011-05-01 18:50:16 +02001727 * align with netns init in ip_vs_control_net_init()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 */
1729
1730static struct ctl_table vs_vars[] = {
1731 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 .procname = "amemthresh",
Hans Schillstroma0840e22011-01-03 14:44:58 +01001733 .maxlen = sizeof(int),
1734 .mode = 0644,
1735 .proc_handler = proc_dointvec,
1736 },
1737 {
1738 .procname = "am_droprate",
1739 .maxlen = sizeof(int),
1740 .mode = 0644,
1741 .proc_handler = proc_dointvec,
1742 },
1743 {
1744 .procname = "drop_entry",
1745 .maxlen = sizeof(int),
1746 .mode = 0644,
1747 .proc_handler = proc_do_defense_mode,
1748 },
1749 {
1750 .procname = "drop_packet",
1751 .maxlen = sizeof(int),
1752 .mode = 0644,
1753 .proc_handler = proc_do_defense_mode,
1754 },
1755#ifdef CONFIG_IP_VS_NFCT
1756 {
1757 .procname = "conntrack",
1758 .maxlen = sizeof(int),
1759 .mode = 0644,
1760 .proc_handler = &proc_dointvec,
1761 },
1762#endif
1763 {
1764 .procname = "secure_tcp",
1765 .maxlen = sizeof(int),
1766 .mode = 0644,
1767 .proc_handler = proc_do_defense_mode,
1768 },
1769 {
1770 .procname = "snat_reroute",
1771 .maxlen = sizeof(int),
1772 .mode = 0644,
1773 .proc_handler = &proc_dointvec,
1774 },
1775 {
1776 .procname = "sync_version",
1777 .maxlen = sizeof(int),
1778 .mode = 0644,
1779 .proc_handler = &proc_do_sync_mode,
1780 },
1781 {
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +02001782 .procname = "sync_ports",
1783 .maxlen = sizeof(int),
1784 .mode = 0644,
1785 .proc_handler = &proc_do_sync_ports,
1786 },
1787 {
Julian Anastasov4d0c8752013-06-24 22:44:41 +03001788 .procname = "sync_persist_mode",
1789 .maxlen = sizeof(int),
1790 .mode = 0644,
1791 .proc_handler = proc_dointvec,
1792 },
1793 {
Pablo Neira Ayuso1c003b12012-05-08 19:39:49 +02001794 .procname = "sync_qlen_max",
Zhang Yanfei07995672013-04-29 11:55:10 -07001795 .maxlen = sizeof(unsigned long),
Pablo Neira Ayuso1c003b12012-05-08 19:39:49 +02001796 .mode = 0644,
Zhang Yanfei07995672013-04-29 11:55:10 -07001797 .proc_handler = proc_doulongvec_minmax,
Pablo Neira Ayuso1c003b12012-05-08 19:39:49 +02001798 },
1799 {
1800 .procname = "sync_sock_size",
1801 .maxlen = sizeof(int),
1802 .mode = 0644,
1803 .proc_handler = proc_dointvec,
1804 },
1805 {
Hans Schillstroma0840e22011-01-03 14:44:58 +01001806 .procname = "cache_bypass",
1807 .maxlen = sizeof(int),
1808 .mode = 0644,
1809 .proc_handler = proc_dointvec,
1810 },
1811 {
1812 .procname = "expire_nodest_conn",
1813 .maxlen = sizeof(int),
1814 .mode = 0644,
1815 .proc_handler = proc_dointvec,
1816 },
1817 {
Alexander Frolkinc6c96c12013-06-13 08:56:15 +01001818 .procname = "sloppy_tcp",
1819 .maxlen = sizeof(int),
1820 .mode = 0644,
1821 .proc_handler = proc_dointvec,
1822 },
1823 {
1824 .procname = "sloppy_sctp",
1825 .maxlen = sizeof(int),
1826 .mode = 0644,
1827 .proc_handler = proc_dointvec,
1828 },
1829 {
Hans Schillstroma0840e22011-01-03 14:44:58 +01001830 .procname = "expire_quiescent_template",
1831 .maxlen = sizeof(int),
1832 .mode = 0644,
1833 .proc_handler = proc_dointvec,
1834 },
1835 {
1836 .procname = "sync_threshold",
1837 .maxlen =
1838 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1839 .mode = 0644,
1840 .proc_handler = proc_do_sync_threshold,
1841 },
1842 {
Julian Anastasov749c42b2012-04-24 23:46:40 +03001843 .procname = "sync_refresh_period",
1844 .maxlen = sizeof(int),
1845 .mode = 0644,
1846 .proc_handler = proc_dointvec_jiffies,
1847 },
1848 {
1849 .procname = "sync_retries",
1850 .maxlen = sizeof(int),
1851 .mode = 0644,
1852 .proc_handler = proc_dointvec_minmax,
1853 .extra1 = &zero,
1854 .extra2 = &three,
1855 },
1856 {
Hans Schillstroma0840e22011-01-03 14:44:58 +01001857 .procname = "nat_icmp_send",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 .maxlen = sizeof(int),
1859 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001860 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 },
Julian Anastasov3654e612012-07-20 11:59:53 +03001862 {
1863 .procname = "pmtu_disc",
1864 .maxlen = sizeof(int),
1865 .mode = 0644,
1866 .proc_handler = proc_dointvec,
1867 },
Julian Anastasov0c125822013-03-09 23:25:04 +02001868 {
1869 .procname = "backup_only",
1870 .maxlen = sizeof(int),
1871 .mode = 0644,
1872 .proc_handler = proc_dointvec,
1873 },
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001874 {
1875 .procname = "conn_reuse_mode",
1876 .maxlen = sizeof(int),
1877 .mode = 0644,
1878 .proc_handler = proc_dointvec,
1879 },
Alex Gartrell94485fe2015-08-26 09:40:34 -07001880 {
1881 .procname = "schedule_icmp",
1882 .maxlen = sizeof(int),
1883 .mode = 0644,
1884 .proc_handler = proc_dointvec,
1885 },
Alex Gartrell4e478092015-09-14 23:23:05 -07001886 {
1887 .procname = "ignore_tunneled",
1888 .maxlen = sizeof(int),
1889 .mode = 0644,
1890 .proc_handler = proc_dointvec,
1891 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892#ifdef CONFIG_IP_VS_DEBUG
1893 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 .procname = "debug_level",
1895 .data = &sysctl_ip_vs_debug_level,
1896 .maxlen = sizeof(int),
1897 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001898 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 },
1900#endif
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001901 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902};
1903
Simon Horman14e40542011-02-04 18:33:02 +09001904#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906#ifdef CONFIG_PROC_FS
1907
1908struct ip_vs_iter {
Hans Schillstromfc723252011-01-03 14:44:43 +01001909 struct seq_net_private p; /* Do not move this, netns depends upon it*/
Julian Anastasovceec4c32013-03-22 11:46:53 +02001910 struct hlist_head *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 int bucket;
1912};
1913
1914/*
1915 * Write the contents of the VS rule table to a PROCfs file.
1916 * (It is kept just for backward compatibility)
1917 */
Eric Dumazet95c96172012-04-15 05:58:06 +00001918static inline const char *ip_vs_fwd_name(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
1920 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1921 case IP_VS_CONN_F_LOCALNODE:
1922 return "Local";
1923 case IP_VS_CONN_F_TUNNEL:
1924 return "Tunnel";
1925 case IP_VS_CONN_F_DROUTE:
1926 return "Route";
1927 default:
1928 return "Masq";
1929 }
1930}
1931
1932
1933/* Get the Nth entry in the two lists */
1934static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1935{
Hans Schillstromfc723252011-01-03 14:44:43 +01001936 struct net *net = seq_file_net(seq);
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001937 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 struct ip_vs_iter *iter = seq->private;
1939 int idx;
1940 struct ip_vs_service *svc;
1941
1942 /* look in hash by protocol */
1943 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001944 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001945 if ((svc->ipvs == ipvs) && pos-- == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 iter->table = ip_vs_svc_table;
1947 iter->bucket = idx;
1948 return svc;
1949 }
1950 }
1951 }
1952
1953 /* keep looking in fwmark */
1954 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001955 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
1956 f_list) {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05001957 if ((svc->ipvs == ipvs) && pos-- == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 iter->table = ip_vs_svc_fwm_table;
1959 iter->bucket = idx;
1960 return svc;
1961 }
1962 }
1963 }
1964
1965 return NULL;
1966}
1967
1968static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
Julian Anastasov371990e2013-04-17 23:50:50 +03001969 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Julian Anastasovceec4c32013-03-22 11:46:53 +02001971 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1973}
1974
1975
1976static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1977{
Julian Anastasovceec4c32013-03-22 11:46:53 +02001978 struct hlist_node *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 struct ip_vs_iter *iter;
1980 struct ip_vs_service *svc;
1981
1982 ++*pos;
1983 if (v == SEQ_START_TOKEN)
1984 return ip_vs_info_array(seq,0);
1985
1986 svc = v;
1987 iter = seq->private;
1988
1989 if (iter->table == ip_vs_svc_table) {
1990 /* next service in table hashed by protocol */
Julian Anastasovceec4c32013-03-22 11:46:53 +02001991 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
1992 if (e)
1993 return hlist_entry(e, struct ip_vs_service, s_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001996 hlist_for_each_entry_rcu(svc,
1997 &ip_vs_svc_table[iter->bucket],
1998 s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return svc;
2000 }
2001 }
2002
2003 iter->table = ip_vs_svc_fwm_table;
2004 iter->bucket = -1;
2005 goto scan_fwmark;
2006 }
2007
2008 /* next service in hashed by fwmark */
Julian Anastasovceec4c32013-03-22 11:46:53 +02002009 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
2010 if (e)
2011 return hlist_entry(e, struct ip_vs_service, f_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 scan_fwmark:
2014 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02002015 hlist_for_each_entry_rcu(svc,
2016 &ip_vs_svc_fwm_table[iter->bucket],
2017 f_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return svc;
2019 }
2020
2021 return NULL;
2022}
2023
2024static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
Julian Anastasov371990e2013-04-17 23:50:50 +03002025 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Julian Anastasovceec4c32013-03-22 11:46:53 +02002027 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
2030
2031static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2032{
2033 if (v == SEQ_START_TOKEN) {
2034 seq_printf(seq,
2035 "IP Virtual Server version %d.%d.%d (size=%d)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01002036 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 seq_puts(seq,
2038 "Prot LocalAddress:Port Scheduler Flags\n");
2039 seq_puts(seq,
2040 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2041 } else {
KUWAZAWA Takuyaa463f9c2017-10-15 20:54:10 +09002042 struct net *net = seq_file_net(seq);
2043 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 const struct ip_vs_service *svc = v;
2045 const struct ip_vs_iter *iter = seq->private;
2046 const struct ip_vs_dest *dest;
Julian Anastasovceec4c32013-03-22 11:46:53 +02002047 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
Julian Anastasov05f00502015-06-29 21:51:40 +03002048 char *sched_name = sched ? sched->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
KUWAZAWA Takuyaa463f9c2017-10-15 20:54:10 +09002050 if (svc->ipvs != ipvs)
2051 return 0;
Vince Busam667a5f12008-09-02 15:55:49 +02002052 if (iter->table == ip_vs_svc_table) {
2053#ifdef CONFIG_IP_VS_IPV6
2054 if (svc->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002055 seq_printf(seq, "%s [%pI6]:%04X %s ",
Vince Busam667a5f12008-09-02 15:55:49 +02002056 ip_vs_proto_name(svc->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07002057 &svc->addr.in6,
Vince Busam667a5f12008-09-02 15:55:49 +02002058 ntohs(svc->port),
Julian Anastasov05f00502015-06-29 21:51:40 +03002059 sched_name);
Vince Busam667a5f12008-09-02 15:55:49 +02002060 else
2061#endif
Nick Chalk26ec0372010-06-22 08:07:01 +02002062 seq_printf(seq, "%s %08X:%04X %s %s ",
Vince Busam667a5f12008-09-02 15:55:49 +02002063 ip_vs_proto_name(svc->protocol),
2064 ntohl(svc->addr.ip),
2065 ntohs(svc->port),
Julian Anastasov05f00502015-06-29 21:51:40 +03002066 sched_name,
Nick Chalk26ec0372010-06-22 08:07:01 +02002067 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
Vince Busam667a5f12008-09-02 15:55:49 +02002068 } else {
Nick Chalk26ec0372010-06-22 08:07:01 +02002069 seq_printf(seq, "FWM %08X %s %s",
Julian Anastasov05f00502015-06-29 21:51:40 +03002070 svc->fwmark, sched_name,
Nick Chalk26ec0372010-06-22 08:07:01 +02002071 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
Vince Busam667a5f12008-09-02 15:55:49 +02002072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2075 seq_printf(seq, "persistent %d %08X\n",
2076 svc->timeout,
2077 ntohl(svc->netmask));
2078 else
2079 seq_putc(seq, '\n');
2080
Julian Anastasov413c2d042013-03-22 11:46:52 +02002081 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
Vince Busam667a5f12008-09-02 15:55:49 +02002082#ifdef CONFIG_IP_VS_IPV6
2083 if (dest->af == AF_INET6)
2084 seq_printf(seq,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002085 " -> [%pI6]:%04X"
Vince Busam667a5f12008-09-02 15:55:49 +02002086 " %-7s %-6d %-10d %-10d\n",
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07002087 &dest->addr.in6,
Vince Busam667a5f12008-09-02 15:55:49 +02002088 ntohs(dest->port),
2089 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2090 atomic_read(&dest->weight),
2091 atomic_read(&dest->activeconns),
2092 atomic_read(&dest->inactconns));
2093 else
2094#endif
2095 seq_printf(seq,
2096 " -> %08X:%04X "
2097 "%-7s %-6d %-10d %-10d\n",
2098 ntohl(dest->addr.ip),
2099 ntohs(dest->port),
2100 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2101 atomic_read(&dest->weight),
2102 atomic_read(&dest->activeconns),
2103 atomic_read(&dest->inactconns));
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106 }
2107 return 0;
2108}
2109
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002110static const struct seq_operations ip_vs_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 .start = ip_vs_info_seq_start,
2112 .next = ip_vs_info_seq_next,
2113 .stop = ip_vs_info_seq_stop,
2114 .show = ip_vs_info_seq_show,
2115};
2116
2117static int ip_vs_info_open(struct inode *inode, struct file *file)
2118{
Hans Schillstromfc723252011-01-03 14:44:43 +01002119 return seq_open_net(inode, file, &ip_vs_info_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002120 sizeof(struct ip_vs_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
Arjan van de Ven9a321442007-02-12 00:55:35 -08002123static const struct file_operations ip_vs_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 .owner = THIS_MODULE,
2125 .open = ip_vs_info_open,
2126 .read = seq_read,
2127 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02002128 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129};
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131static int ip_vs_stats_show(struct seq_file *seq, void *v)
2132{
Hans Schillstromb17fc992011-01-03 14:44:56 +01002133 struct net *net = seq_file_single_net(seq);
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002134 struct ip_vs_kstats show;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2137 seq_puts(seq,
2138 " Total Incoming Outgoing Incoming Outgoing\n");
2139 seq_printf(seq,
2140 " Conns Packets Packets Bytes Bytes\n");
2141
Julian Anastasov55a3d4e2011-03-14 01:37:49 +02002142 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002143 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2144 (unsigned long long)show.conns,
2145 (unsigned long long)show.inpkts,
2146 (unsigned long long)show.outpkts,
2147 (unsigned long long)show.inbytes,
2148 (unsigned long long)show.outbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002150/* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 seq_puts(seq,
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002152 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2153 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2154 (unsigned long long)show.cps,
2155 (unsigned long long)show.inpps,
2156 (unsigned long long)show.outpps,
2157 (unsigned long long)show.inbps,
2158 (unsigned long long)show.outbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 return 0;
2161}
2162
2163static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
2164{
Hans Schillstromfc723252011-01-03 14:44:43 +01002165 return single_open_net(inode, file, ip_vs_stats_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
Arjan van de Ven9a321442007-02-12 00:55:35 -08002168static const struct file_operations ip_vs_stats_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 .owner = THIS_MODULE,
2170 .open = ip_vs_stats_seq_open,
2171 .read = seq_read,
2172 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02002173 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174};
2175
Hans Schillstromb17fc992011-01-03 14:44:56 +01002176static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2177{
2178 struct net *net = seq_file_single_net(seq);
Julian Anastasov2a0751a2011-03-04 12:20:35 +02002179 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
Julian Anastasov371990e2013-04-17 23:50:50 +03002180 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002181 struct ip_vs_kstats kstats;
Hans Schillstromb17fc992011-01-03 14:44:56 +01002182 int i;
2183
2184/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2185 seq_puts(seq,
2186 " Total Incoming Outgoing Incoming Outgoing\n");
2187 seq_printf(seq,
2188 "CPU Conns Packets Packets Bytes Bytes\n");
2189
2190 for_each_possible_cpu(i) {
Julian Anastasov2a0751a2011-03-04 12:20:35 +02002191 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2192 unsigned int start;
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002193 u64 conns, inpkts, outpkts, inbytes, outbytes;
Julian Anastasov2a0751a2011-03-04 12:20:35 +02002194
2195 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07002196 start = u64_stats_fetch_begin_irq(&u->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002197 conns = u->cnt.conns;
2198 inpkts = u->cnt.inpkts;
2199 outpkts = u->cnt.outpkts;
2200 inbytes = u->cnt.inbytes;
2201 outbytes = u->cnt.outbytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07002202 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
Julian Anastasov2a0751a2011-03-04 12:20:35 +02002203
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002204 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2205 i, (u64)conns, (u64)inpkts,
2206 (u64)outpkts, (u64)inbytes,
2207 (u64)outbytes);
Hans Schillstromb17fc992011-01-03 14:44:56 +01002208 }
2209
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002210 ip_vs_copy_stats(&kstats, tot_stats);
Julian Anastasovea9f22c2011-03-14 01:41:54 +02002211
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002212 seq_printf(seq, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2213 (unsigned long long)kstats.conns,
2214 (unsigned long long)kstats.inpkts,
2215 (unsigned long long)kstats.outpkts,
2216 (unsigned long long)kstats.inbytes,
2217 (unsigned long long)kstats.outbytes);
Hans Schillstromb17fc992011-01-03 14:44:56 +01002218
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002219/* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
Hans Schillstromb17fc992011-01-03 14:44:56 +01002220 seq_puts(seq,
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002221 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2222 seq_printf(seq, " %8LX %8LX %8LX %16LX %16LX\n",
2223 kstats.cps,
2224 kstats.inpps,
2225 kstats.outpps,
2226 kstats.inbps,
2227 kstats.outbps);
Hans Schillstromb17fc992011-01-03 14:44:56 +01002228
2229 return 0;
2230}
2231
2232static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file)
2233{
2234 return single_open_net(inode, file, ip_vs_stats_percpu_show);
2235}
2236
2237static const struct file_operations ip_vs_stats_percpu_fops = {
2238 .owner = THIS_MODULE,
2239 .open = ip_vs_stats_percpu_seq_open,
2240 .read = seq_read,
2241 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02002242 .release = single_release_net,
Hans Schillstromb17fc992011-01-03 14:44:56 +01002243};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244#endif
2245
2246/*
2247 * Set timeout values for tcp tcpfin udp in the timeout_table.
2248 */
Eric W. Biedermanf1faa1e2015-09-21 13:02:02 -05002249static int ip_vs_set_timeout(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Changli Gao091bb342011-01-21 18:02:13 +08002251#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
Hans Schillstrom93304192011-01-03 14:44:51 +01002252 struct ip_vs_proto_data *pd;
Changli Gao091bb342011-01-21 18:02:13 +08002253#endif
Hans Schillstrom93304192011-01-03 14:44:51 +01002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2256 u->tcp_timeout,
2257 u->tcp_fin_timeout,
2258 u->udp_timeout);
2259
2260#ifdef CONFIG_IP_VS_PROTO_TCP
ZhangXiaoxu304e1f02019-01-10 16:39:06 +08002261 if (u->tcp_timeout < 0 || u->tcp_timeout > (INT_MAX / HZ) ||
2262 u->tcp_fin_timeout < 0 || u->tcp_fin_timeout > (INT_MAX / HZ)) {
2263 return -EINVAL;
2264 }
2265#endif
2266
2267#ifdef CONFIG_IP_VS_PROTO_UDP
2268 if (u->udp_timeout < 0 || u->udp_timeout > (INT_MAX / HZ))
2269 return -EINVAL;
2270#endif
2271
2272#ifdef CONFIG_IP_VS_PROTO_TCP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (u->tcp_timeout) {
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05002274 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
Hans Schillstrom93304192011-01-03 14:44:51 +01002275 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 = u->tcp_timeout * HZ;
2277 }
2278
2279 if (u->tcp_fin_timeout) {
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05002280 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
Hans Schillstrom93304192011-01-03 14:44:51 +01002281 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 = u->tcp_fin_timeout * HZ;
2283 }
2284#endif
2285
2286#ifdef CONFIG_IP_VS_PROTO_UDP
2287 if (u->udp_timeout) {
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05002288 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
Hans Schillstrom93304192011-01-03 14:44:51 +01002289 pd->timeout_table[IP_VS_UDP_S_NORMAL]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 = u->udp_timeout * HZ;
2291 }
2292#endif
2293 return 0;
2294}
2295
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002296#define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002298struct ip_vs_svcdest_user {
2299 struct ip_vs_service_user s;
2300 struct ip_vs_dest_user d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301};
2302
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002303static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2304 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2305 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2306 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2307 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2308 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2309 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2310 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2311 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2312 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2313 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2314};
2315
2316union ip_vs_set_arglen {
2317 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2318 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2319 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2320 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2321 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2322 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2323 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2324 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2325 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2326 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2327};
2328
2329#define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2330
Julius Volzc860c6b2008-09-02 15:55:36 +02002331static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2332 struct ip_vs_service_user *usvc_compat)
2333{
Simon Horman0d1e71b2010-08-22 21:37:54 +09002334 memset(usvc, 0, sizeof(*usvc));
2335
Julius Volzc860c6b2008-09-02 15:55:36 +02002336 usvc->af = AF_INET;
2337 usvc->protocol = usvc_compat->protocol;
2338 usvc->addr.ip = usvc_compat->addr;
2339 usvc->port = usvc_compat->port;
2340 usvc->fwmark = usvc_compat->fwmark;
2341
2342 /* Deep copy of sched_name is not needed here */
2343 usvc->sched_name = usvc_compat->sched_name;
2344
2345 usvc->flags = usvc_compat->flags;
2346 usvc->timeout = usvc_compat->timeout;
2347 usvc->netmask = usvc_compat->netmask;
2348}
2349
2350static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2351 struct ip_vs_dest_user *udest_compat)
2352{
Simon Horman0d1e71b2010-08-22 21:37:54 +09002353 memset(udest, 0, sizeof(*udest));
2354
Julius Volzc860c6b2008-09-02 15:55:36 +02002355 udest->addr.ip = udest_compat->addr;
2356 udest->port = udest_compat->port;
2357 udest->conn_flags = udest_compat->conn_flags;
2358 udest->weight = udest_compat->weight;
2359 udest->u_threshold = udest_compat->u_threshold;
2360 udest->l_threshold = udest_compat->l_threshold;
Alex Gartrell6cff3392014-09-09 16:40:20 -07002361 udest->af = AF_INET;
Julius Volzc860c6b2008-09-02 15:55:36 +02002362}
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364static int
2365do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2366{
Hans Schillstromfc723252011-01-03 14:44:43 +01002367 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 int ret;
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002369 unsigned char arg[MAX_SET_ARGLEN];
Julius Volzc860c6b2008-09-02 15:55:36 +02002370 struct ip_vs_service_user *usvc_compat;
2371 struct ip_vs_service_user_kern usvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 struct ip_vs_service *svc;
Julius Volzc860c6b2008-09-02 15:55:36 +02002373 struct ip_vs_dest_user *udest_compat;
2374 struct ip_vs_dest_user_kern udest;
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002375 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002377 BUILD_BUG_ON(sizeof(arg) > 255);
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002378 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return -EPERM;
2380
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002381 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2382 return -EINVAL;
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002383 if (len != set_arglen[CMDID(cmd)]) {
2384 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2385 len, set_arglen[CMDID(cmd)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return -EINVAL;
2387 }
2388
2389 if (copy_from_user(arg, user, len) != 0)
2390 return -EFAULT;
2391
2392 /* increase the module use count */
2393 ip_vs_use_count_inc();
2394
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002395 /* Handle daemons since they have another lock */
2396 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2397 cmd == IP_VS_SO_SET_STOPDAEMON) {
2398 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2399
Julian Anastasove0b26cc2015-07-26 14:57:34 +03002400 if (cmd == IP_VS_SO_SET_STARTDAEMON) {
Julian Anastasove4ff6752015-07-26 15:03:27 +03002401 struct ipvs_sync_daemon_cfg cfg;
2402
2403 memset(&cfg, 0, sizeof(cfg));
Julian Anastasov0063faa2018-05-19 18:22:35 +03002404 ret = -EINVAL;
2405 if (strscpy(cfg.mcast_ifn, dm->mcast_ifn,
2406 sizeof(cfg.mcast_ifn)) <= 0)
2407 goto out_dec;
Julian Anastasove4ff6752015-07-26 15:03:27 +03002408 cfg.syncid = dm->syncid;
Eric W. Biederman6ac121d2015-09-21 13:02:10 -05002409 ret = start_sync_thread(ipvs, &cfg, dm->state);
Julian Anastasove0b26cc2015-07-26 14:57:34 +03002410 } else {
2411 mutex_lock(&ipvs->sync_mutex);
Eric W. Biedermanb3cf3cb2015-09-21 13:02:11 -05002412 ret = stop_sync_thread(ipvs, dm->state);
Julian Anastasove0b26cc2015-07-26 14:57:34 +03002413 mutex_unlock(&ipvs->sync_mutex);
2414 }
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002415 goto out_dec;
2416 }
2417
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02002418 mutex_lock(&__ip_vs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (cmd == IP_VS_SO_SET_FLUSH) {
2420 /* Flush the virtual service */
Eric W. Biedermanef7c5992015-09-21 13:01:57 -05002421 ret = ip_vs_flush(ipvs, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 goto out_unlock;
2423 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2424 /* Set timeout values for (tcp tcpfin udp) */
Eric W. Biedermanf1faa1e2015-09-21 13:02:02 -05002425 ret = ip_vs_set_timeout(ipvs, (struct ip_vs_timeout_user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428
Julius Volzc860c6b2008-09-02 15:55:36 +02002429 usvc_compat = (struct ip_vs_service_user *)arg;
2430 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2431
2432 /* We only use the new structs internally, so copy userspace compat
2433 * structs to extended internal versions */
2434 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2435 ip_vs_copy_udest_compat(&udest, udest_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 if (cmd == IP_VS_SO_SET_ZERO) {
2438 /* if no service address is set, zero counters in all */
Julius Volzc860c6b2008-09-02 15:55:36 +02002439 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
Eric W. Biedermanc60856c2015-09-21 13:01:59 -05002440 ret = ip_vs_zero_all(ipvs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto out_unlock;
2442 }
2443 }
2444
Julian Anastasov0063faa2018-05-19 18:22:35 +03002445 if ((cmd == IP_VS_SO_SET_ADD || cmd == IP_VS_SO_SET_EDIT) &&
2446 strnlen(usvc.sched_name, IP_VS_SCHEDNAME_MAXLEN) ==
2447 IP_VS_SCHEDNAME_MAXLEN) {
2448 ret = -EINVAL;
2449 goto out_unlock;
2450 }
2451
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01002452 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2453 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2454 usvc.protocol != IPPROTO_SCTP) {
Julian Anastasov0063faa2018-05-19 18:22:35 +03002455 pr_err("set_ctl: invalid protocol: %d %pI4:%d\n",
Hannes Eder1e3e2382009-08-02 11:05:41 +00002456 usvc.protocol, &usvc.addr.ip,
Julian Anastasov0063faa2018-05-19 18:22:35 +03002457 ntohs(usvc.port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 ret = -EFAULT;
2459 goto out_unlock;
2460 }
2461
2462 /* Lookup the exact service by <protocol, addr, port> or fwmark */
Julian Anastasovceec4c32013-03-22 11:46:53 +02002463 rcu_read_lock();
Julius Volzc860c6b2008-09-02 15:55:36 +02002464 if (usvc.fwmark == 0)
Eric W. Biedermanbb2e2a82015-09-21 13:01:48 -05002465 svc = __ip_vs_service_find(ipvs, usvc.af, usvc.protocol,
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002466 &usvc.addr, usvc.port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 else
Eric W. Biederman1ed8b942015-09-21 13:01:46 -05002468 svc = __ip_vs_svc_fwm_find(ipvs, usvc.af, usvc.fwmark);
Julian Anastasovceec4c32013-03-22 11:46:53 +02002469 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 if (cmd != IP_VS_SO_SET_ADD
Julius Volzc860c6b2008-09-02 15:55:36 +02002472 && (svc == NULL || svc->protocol != usvc.protocol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 ret = -ESRCH;
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002474 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
2477 switch (cmd) {
2478 case IP_VS_SO_SET_ADD:
2479 if (svc != NULL)
2480 ret = -EEXIST;
2481 else
Eric W. Biederman5060bd82015-09-21 13:01:56 -05002482 ret = ip_vs_add_service(ipvs, &usvc, &svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 break;
2484 case IP_VS_SO_SET_EDIT:
Julius Volzc860c6b2008-09-02 15:55:36 +02002485 ret = ip_vs_edit_service(svc, &usvc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
2487 case IP_VS_SO_SET_DEL:
2488 ret = ip_vs_del_service(svc);
2489 if (!ret)
2490 goto out_unlock;
2491 break;
2492 case IP_VS_SO_SET_ZERO:
2493 ret = ip_vs_zero_service(svc);
2494 break;
2495 case IP_VS_SO_SET_ADDDEST:
Julius Volzc860c6b2008-09-02 15:55:36 +02002496 ret = ip_vs_add_dest(svc, &udest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 break;
2498 case IP_VS_SO_SET_EDITDEST:
Julius Volzc860c6b2008-09-02 15:55:36 +02002499 ret = ip_vs_edit_dest(svc, &udest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 break;
2501 case IP_VS_SO_SET_DELDEST:
Julius Volzc860c6b2008-09-02 15:55:36 +02002502 ret = ip_vs_del_dest(svc, &udest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 break;
2504 default:
2505 ret = -EINVAL;
2506 }
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 out_unlock:
Ingo Molnar14cc3e22006-03-26 01:37:14 -08002509 mutex_unlock(&__ip_vs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 out_dec:
2511 /* decrease the module use count */
2512 ip_vs_use_count_dec();
2513
2514 return ret;
2515}
2516
2517
2518static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2520{
Julian Anastasovceec4c32013-03-22 11:46:53 +02002521 struct ip_vs_scheduler *sched;
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002522 struct ip_vs_kstats kstats;
Julian Anastasov05f00502015-06-29 21:51:40 +03002523 char *sched_name;
Julian Anastasovceec4c32013-03-22 11:46:53 +02002524
2525 sched = rcu_dereference_protected(src->scheduler, 1);
Julian Anastasov05f00502015-06-29 21:51:40 +03002526 sched_name = sched ? sched->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 dst->protocol = src->protocol;
Julius Volze7ade462008-09-02 15:55:33 +02002528 dst->addr = src->addr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 dst->port = src->port;
2530 dst->fwmark = src->fwmark;
Julian Anastasov05f00502015-06-29 21:51:40 +03002531 strlcpy(dst->sched_name, sched_name, sizeof(dst->sched_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 dst->flags = src->flags;
2533 dst->timeout = src->timeout / HZ;
2534 dst->netmask = src->netmask;
2535 dst->num_dests = src->num_dests;
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002536 ip_vs_copy_stats(&kstats, &src->stats);
2537 ip_vs_export_stats_user(&dst->stats, &kstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
2540static inline int
Eric W. Biedermanb2876b72015-09-21 13:02:03 -05002541__ip_vs_get_service_entries(struct netns_ipvs *ipvs,
Hans Schillstromfc723252011-01-03 14:44:43 +01002542 const struct ip_vs_get_services *get,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct ip_vs_get_services __user *uptr)
2544{
2545 int idx, count=0;
2546 struct ip_vs_service *svc;
2547 struct ip_vs_service_entry entry;
2548 int ret = 0;
2549
2550 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02002551 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
Julius Volzf94fd042008-09-02 15:55:55 +02002552 /* Only expose IPv4 entries to old interface */
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05002553 if (svc->af != AF_INET || (svc->ipvs != ipvs))
Julius Volzf94fd042008-09-02 15:55:55 +02002554 continue;
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (count >= get->num_services)
2557 goto out;
pageexec4da62fc2005-06-26 16:00:19 -07002558 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 ip_vs_copy_service(&entry, svc);
2560 if (copy_to_user(&uptr->entrytable[count],
2561 &entry, sizeof(entry))) {
2562 ret = -EFAULT;
2563 goto out;
2564 }
2565 count++;
2566 }
2567 }
2568
2569 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02002570 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
Julius Volzf94fd042008-09-02 15:55:55 +02002571 /* Only expose IPv4 entries to old interface */
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05002572 if (svc->af != AF_INET || (svc->ipvs != ipvs))
Julius Volzf94fd042008-09-02 15:55:55 +02002573 continue;
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 if (count >= get->num_services)
2576 goto out;
pageexec4da62fc2005-06-26 16:00:19 -07002577 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 ip_vs_copy_service(&entry, svc);
2579 if (copy_to_user(&uptr->entrytable[count],
2580 &entry, sizeof(entry))) {
2581 ret = -EFAULT;
2582 goto out;
2583 }
2584 count++;
2585 }
2586 }
Hans Schillstrom552ad652011-06-13 12:19:26 +02002587out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return ret;
2589}
2590
2591static inline int
Eric W. Biederman08fff4c2015-09-21 13:02:04 -05002592__ip_vs_get_dest_entries(struct netns_ipvs *ipvs, const struct ip_vs_get_dests *get,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 struct ip_vs_get_dests __user *uptr)
2594{
2595 struct ip_vs_service *svc;
Julius Volzb18610d2008-09-02 15:55:37 +02002596 union nf_inet_addr addr = { .ip = get->addr };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 int ret = 0;
2598
Julian Anastasovceec4c32013-03-22 11:46:53 +02002599 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if (get->fwmark)
Eric W. Biederman1ed8b942015-09-21 13:01:46 -05002601 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, get->fwmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 else
Eric W. Biedermanbb2e2a82015-09-21 13:01:48 -05002603 svc = __ip_vs_service_find(ipvs, AF_INET, get->protocol, &addr,
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002604 get->port);
Julian Anastasovceec4c32013-03-22 11:46:53 +02002605 rcu_read_unlock();
Julius Volzb18610d2008-09-02 15:55:37 +02002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (svc) {
2608 int count = 0;
2609 struct ip_vs_dest *dest;
2610 struct ip_vs_dest_entry entry;
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002611 struct ip_vs_kstats kstats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Dan Carpentera8241c62013-06-03 12:00:49 +03002613 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 list_for_each_entry(dest, &svc->destinations, n_list) {
2615 if (count >= get->num_dests)
2616 break;
2617
Alex Gartrell6cff3392014-09-09 16:40:20 -07002618 /* Cannot expose heterogeneous members via sockopt
2619 * interface
2620 */
2621 if (dest->af != svc->af)
2622 continue;
2623
Julius Volze7ade462008-09-02 15:55:33 +02002624 entry.addr = dest->addr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 entry.port = dest->port;
2626 entry.conn_flags = atomic_read(&dest->conn_flags);
2627 entry.weight = atomic_read(&dest->weight);
2628 entry.u_threshold = dest->u_threshold;
2629 entry.l_threshold = dest->l_threshold;
2630 entry.activeconns = atomic_read(&dest->activeconns);
2631 entry.inactconns = atomic_read(&dest->inactconns);
2632 entry.persistconns = atomic_read(&dest->persistconns);
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002633 ip_vs_copy_stats(&kstats, &dest->stats);
2634 ip_vs_export_stats_user(&entry.stats, &kstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (copy_to_user(&uptr->entrytable[count],
2636 &entry, sizeof(entry))) {
2637 ret = -EFAULT;
2638 break;
2639 }
2640 count++;
2641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 } else
2643 ret = -ESRCH;
2644 return ret;
2645}
2646
2647static inline void
Eric W. Biedermanaf540342015-09-21 13:02:05 -05002648__ip_vs_get_timeouts(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Changli Gao091bb342011-01-21 18:02:13 +08002650#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
Hans Schillstrom93304192011-01-03 14:44:51 +01002651 struct ip_vs_proto_data *pd;
Changli Gao091bb342011-01-21 18:02:13 +08002652#endif
Hans Schillstrom93304192011-01-03 14:44:51 +01002653
Arnd Bergmannb61a6022012-10-09 13:04:34 +09002654 memset(u, 0, sizeof (*u));
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656#ifdef CONFIG_IP_VS_PROTO_TCP
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05002657 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
Hans Schillstrom93304192011-01-03 14:44:51 +01002658 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2659 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660#endif
2661#ifdef CONFIG_IP_VS_PROTO_UDP
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05002662 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 u->udp_timeout =
Hans Schillstrom93304192011-01-03 14:44:51 +01002664 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665#endif
2666}
2667
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002668static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2669 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2670 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2671 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2672 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2673 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2674 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2675 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676};
2677
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002678union ip_vs_get_arglen {
2679 char field_IP_VS_SO_GET_VERSION[64];
2680 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
2681 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
2682 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
2683 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
2684 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
2685 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
2686};
2687
2688#define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690static int
2691do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2692{
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002693 unsigned char arg[MAX_GET_ARGLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 int ret = 0;
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002695 unsigned int copylen;
Hans Schillstromfc723252011-01-03 14:44:43 +01002696 struct net *net = sock_net(sk);
Hans Schillstromf1313152011-01-03 14:44:55 +01002697 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Hans Schillstromfc723252011-01-03 14:44:43 +01002699 BUG_ON(!net);
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002700 BUILD_BUG_ON(sizeof(arg) > 255);
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002701 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 return -EPERM;
2703
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002704 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2705 return -EINVAL;
2706
Julian Anastasov5fcf0cf2014-09-03 00:02:49 +03002707 copylen = get_arglen[CMDID(cmd)];
2708 if (*len < (int) copylen) {
2709 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return -EINVAL;
2711 }
2712
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002713 if (copy_from_user(arg, user, copylen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 return -EFAULT;
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002715 /*
2716 * Handle daemons first since it has its own locking
2717 */
2718 if (cmd == IP_VS_SO_GET_DAEMON) {
2719 struct ip_vs_daemon_user d[2];
2720
2721 memset(&d, 0, sizeof(d));
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02002722 mutex_lock(&ipvs->sync_mutex);
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002723 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2724 d[0].state = IP_VS_STATE_MASTER;
Julian Anastasove4ff6752015-07-26 15:03:27 +03002725 strlcpy(d[0].mcast_ifn, ipvs->mcfg.mcast_ifn,
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002726 sizeof(d[0].mcast_ifn));
Julian Anastasove4ff6752015-07-26 15:03:27 +03002727 d[0].syncid = ipvs->mcfg.syncid;
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002728 }
2729 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2730 d[1].state = IP_VS_STATE_BACKUP;
Julian Anastasove4ff6752015-07-26 15:03:27 +03002731 strlcpy(d[1].mcast_ifn, ipvs->bcfg.mcast_ifn,
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002732 sizeof(d[1].mcast_ifn));
Julian Anastasove4ff6752015-07-26 15:03:27 +03002733 d[1].syncid = ipvs->bcfg.syncid;
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002734 }
2735 if (copy_to_user(user, &d, sizeof(d)) != 0)
2736 ret = -EFAULT;
2737 mutex_unlock(&ipvs->sync_mutex);
2738 return ret;
2739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02002741 mutex_lock(&__ip_vs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 switch (cmd) {
2743 case IP_VS_SO_GET_VERSION:
2744 {
2745 char buf[64];
2746
2747 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01002748 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2750 ret = -EFAULT;
2751 goto out;
2752 }
2753 *len = strlen(buf)+1;
2754 }
2755 break;
2756
2757 case IP_VS_SO_GET_INFO:
2758 {
2759 struct ip_vs_getinfo info;
2760 info.version = IP_VS_VERSION_CODE;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01002761 info.size = ip_vs_conn_tab_size;
Hans Schillstroma0840e22011-01-03 14:44:58 +01002762 info.num_services = ipvs->num_services;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (copy_to_user(user, &info, sizeof(info)) != 0)
2764 ret = -EFAULT;
2765 }
2766 break;
2767
2768 case IP_VS_SO_GET_SERVICES:
2769 {
2770 struct ip_vs_get_services *get;
2771 int size;
2772
2773 get = (struct ip_vs_get_services *)arg;
2774 size = sizeof(*get) +
2775 sizeof(struct ip_vs_service_entry) * get->num_services;
2776 if (*len != size) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002777 pr_err("length: %u != %u\n", *len, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 ret = -EINVAL;
2779 goto out;
2780 }
Eric W. Biedermanb2876b72015-09-21 13:02:03 -05002781 ret = __ip_vs_get_service_entries(ipvs, get, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
2783 break;
2784
2785 case IP_VS_SO_GET_SERVICE:
2786 {
2787 struct ip_vs_service_entry *entry;
2788 struct ip_vs_service *svc;
Julius Volzb18610d2008-09-02 15:55:37 +02002789 union nf_inet_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 entry = (struct ip_vs_service_entry *)arg;
Julius Volzb18610d2008-09-02 15:55:37 +02002792 addr.ip = entry->addr;
Julian Anastasovceec4c32013-03-22 11:46:53 +02002793 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (entry->fwmark)
Eric W. Biederman1ed8b942015-09-21 13:01:46 -05002795 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, entry->fwmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 else
Eric W. Biedermanbb2e2a82015-09-21 13:01:48 -05002797 svc = __ip_vs_service_find(ipvs, AF_INET,
Hans Schillstromfc723252011-01-03 14:44:43 +01002798 entry->protocol, &addr,
2799 entry->port);
Julian Anastasovceec4c32013-03-22 11:46:53 +02002800 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (svc) {
2802 ip_vs_copy_service(entry, svc);
2803 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2804 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 } else
2806 ret = -ESRCH;
2807 }
2808 break;
2809
2810 case IP_VS_SO_GET_DESTS:
2811 {
2812 struct ip_vs_get_dests *get;
2813 int size;
2814
2815 get = (struct ip_vs_get_dests *)arg;
2816 size = sizeof(*get) +
2817 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2818 if (*len != size) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002819 pr_err("length: %u != %u\n", *len, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 ret = -EINVAL;
2821 goto out;
2822 }
Eric W. Biederman08fff4c2015-09-21 13:02:04 -05002823 ret = __ip_vs_get_dest_entries(ipvs, get, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 }
2825 break;
2826
2827 case IP_VS_SO_GET_TIMEOUT:
2828 {
2829 struct ip_vs_timeout_user t;
2830
Eric W. Biedermanaf540342015-09-21 13:02:05 -05002831 __ip_vs_get_timeouts(ipvs, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (copy_to_user(user, &t, sizeof(t)) != 0)
2833 ret = -EFAULT;
2834 }
2835 break;
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 default:
2838 ret = -EINVAL;
2839 }
2840
Hans Schillstrom552ad652011-06-13 12:19:26 +02002841out:
Ingo Molnar14cc3e22006-03-26 01:37:14 -08002842 mutex_unlock(&__ip_vs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return ret;
2844}
2845
2846
2847static struct nf_sockopt_ops ip_vs_sockopts = {
2848 .pf = PF_INET,
2849 .set_optmin = IP_VS_BASE_CTL,
2850 .set_optmax = IP_VS_SO_SET_MAX+1,
2851 .set = do_ip_vs_set_ctl,
2852 .get_optmin = IP_VS_BASE_CTL,
2853 .get_optmax = IP_VS_SO_GET_MAX+1,
2854 .get = do_ip_vs_get_ctl,
Neil Horman16fcec32007-09-11 11:28:26 +02002855 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856};
2857
Julius Volz9a812192008-08-14 14:08:44 +02002858/*
2859 * Generic Netlink interface
2860 */
2861
2862/* IPVS genetlink family */
2863static struct genl_family ip_vs_genl_family = {
2864 .id = GENL_ID_GENERATE,
2865 .hdrsize = 0,
2866 .name = IPVS_GENL_NAME,
2867 .version = IPVS_GENL_VERSION,
WANG Cong8fbfef72016-11-03 17:14:03 -07002868 .maxattr = IPVS_CMD_ATTR_MAX,
Hans Schillstromc6d2d442011-01-03 14:45:03 +01002869 .netnsok = true, /* Make ipvsadm to work on netns */
Julius Volz9a812192008-08-14 14:08:44 +02002870};
2871
2872/* Policy used for first-level command attributes */
2873static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2874 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2875 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2876 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2877 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2878 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2879 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2880};
2881
2882/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2883static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2884 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2885 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
Julian Anastasov0063faa2018-05-19 18:22:35 +03002886 .len = IP_VS_IFNAME_MAXLEN - 1 },
Julius Volz9a812192008-08-14 14:08:44 +02002887 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
Julian Anastasove4ff6752015-07-26 15:03:27 +03002888 [IPVS_DAEMON_ATTR_SYNC_MAXLEN] = { .type = NLA_U16 },
Julian Anastasovd3328812015-07-26 15:03:28 +03002889 [IPVS_DAEMON_ATTR_MCAST_GROUP] = { .type = NLA_U32 },
2890 [IPVS_DAEMON_ATTR_MCAST_GROUP6] = { .len = sizeof(struct in6_addr) },
2891 [IPVS_DAEMON_ATTR_MCAST_PORT] = { .type = NLA_U16 },
2892 [IPVS_DAEMON_ATTR_MCAST_TTL] = { .type = NLA_U8 },
Julius Volz9a812192008-08-14 14:08:44 +02002893};
2894
2895/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2896static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2897 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2898 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2899 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2900 .len = sizeof(union nf_inet_addr) },
2901 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2902 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2903 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
Julian Anastasov0063faa2018-05-19 18:22:35 +03002904 .len = IP_VS_SCHEDNAME_MAXLEN - 1 },
Simon Horman0d1e71b2010-08-22 21:37:54 +09002905 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2906 .len = IP_VS_PENAME_MAXLEN },
Julius Volz9a812192008-08-14 14:08:44 +02002907 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2908 .len = sizeof(struct ip_vs_flags) },
2909 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2910 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2911 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2912};
2913
2914/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2915static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2916 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2917 .len = sizeof(union nf_inet_addr) },
2918 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2919 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2920 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2921 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2922 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2923 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2924 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2925 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2926 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
Alex Gartrell6cff3392014-09-09 16:40:20 -07002927 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
Julius Volz9a812192008-08-14 14:08:44 +02002928};
2929
2930static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002931 struct ip_vs_kstats *kstats)
Julius Volz9a812192008-08-14 14:08:44 +02002932{
2933 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002934
Julius Volz9a812192008-08-14 14:08:44 +02002935 if (!nl_stats)
2936 return -EMSGSIZE;
2937
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002938 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
2939 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
2940 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
Nicolas Dichtelcbdeafd2016-04-25 10:25:21 +02002941 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
2942 IPVS_STATS_ATTR_PAD) ||
2943 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
2944 IPVS_STATS_ATTR_PAD) ||
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002945 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
2946 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
2947 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
2948 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
2949 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
2950 goto nla_put_failure;
2951 nla_nest_end(skb, nl_stats);
Julius Volz9a812192008-08-14 14:08:44 +02002952
Julian Anastasovcd67cd52015-02-06 09:44:44 +02002953 return 0;
2954
2955nla_put_failure:
2956 nla_nest_cancel(skb, nl_stats);
2957 return -EMSGSIZE;
2958}
2959
2960static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
2961 struct ip_vs_kstats *kstats)
2962{
2963 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2964
2965 if (!nl_stats)
2966 return -EMSGSIZE;
2967
Nicolas Dichtelcbdeafd2016-04-25 10:25:21 +02002968 if (nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CONNS, kstats->conns,
2969 IPVS_STATS_ATTR_PAD) ||
2970 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts,
2971 IPVS_STATS_ATTR_PAD) ||
2972 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts,
2973 IPVS_STATS_ATTR_PAD) ||
2974 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
2975 IPVS_STATS_ATTR_PAD) ||
2976 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
2977 IPVS_STATS_ATTR_PAD) ||
2978 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CPS, kstats->cps,
2979 IPVS_STATS_ATTR_PAD) ||
2980 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps,
2981 IPVS_STATS_ATTR_PAD) ||
2982 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps,
2983 IPVS_STATS_ATTR_PAD) ||
2984 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps,
2985 IPVS_STATS_ATTR_PAD) ||
2986 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps,
2987 IPVS_STATS_ATTR_PAD))
David S. Miller969e8e22012-04-01 19:03:15 -04002988 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02002989 nla_nest_end(skb, nl_stats);
2990
2991 return 0;
2992
2993nla_put_failure:
Julius Volz9a812192008-08-14 14:08:44 +02002994 nla_nest_cancel(skb, nl_stats);
2995 return -EMSGSIZE;
2996}
2997
2998static int ip_vs_genl_fill_service(struct sk_buff *skb,
2999 struct ip_vs_service *svc)
3000{
Julian Anastasovceec4c32013-03-22 11:46:53 +02003001 struct ip_vs_scheduler *sched;
Julian Anastasov371990e2013-04-17 23:50:50 +03003002 struct ip_vs_pe *pe;
Julius Volz9a812192008-08-14 14:08:44 +02003003 struct nlattr *nl_service;
3004 struct ip_vs_flags flags = { .flags = svc->flags,
3005 .mask = ~0 };
Julian Anastasovcd67cd52015-02-06 09:44:44 +02003006 struct ip_vs_kstats kstats;
Julian Anastasov05f00502015-06-29 21:51:40 +03003007 char *sched_name;
Julius Volz9a812192008-08-14 14:08:44 +02003008
3009 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
3010 if (!nl_service)
3011 return -EMSGSIZE;
3012
David S. Miller969e8e22012-04-01 19:03:15 -04003013 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
3014 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003015 if (svc->fwmark) {
David S. Miller969e8e22012-04-01 19:03:15 -04003016 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
3017 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003018 } else {
David S. Miller969e8e22012-04-01 19:03:15 -04003019 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
3020 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
Julian Anastasov0a925862013-04-17 23:50:49 +03003021 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
David S. Miller969e8e22012-04-01 19:03:15 -04003022 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003023 }
3024
Julian Anastasovceec4c32013-03-22 11:46:53 +02003025 sched = rcu_dereference_protected(svc->scheduler, 1);
Julian Anastasov05f00502015-06-29 21:51:40 +03003026 sched_name = sched ? sched->name : "none";
Julian Anastasov371990e2013-04-17 23:50:50 +03003027 pe = rcu_dereference_protected(svc->pe, 1);
Julian Anastasov05f00502015-06-29 21:51:40 +03003028 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched_name) ||
Julian Anastasov371990e2013-04-17 23:50:50 +03003029 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
David S. Miller969e8e22012-04-01 19:03:15 -04003030 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
3031 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
Julian Anastasov0a925862013-04-17 23:50:49 +03003032 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
David S. Miller969e8e22012-04-01 19:03:15 -04003033 goto nla_put_failure;
Julian Anastasovcd67cd52015-02-06 09:44:44 +02003034 ip_vs_copy_stats(&kstats, &svc->stats);
3035 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
3036 goto nla_put_failure;
3037 if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
Julius Volz9a812192008-08-14 14:08:44 +02003038 goto nla_put_failure;
3039
3040 nla_nest_end(skb, nl_service);
3041
3042 return 0;
3043
3044nla_put_failure:
3045 nla_nest_cancel(skb, nl_service);
3046 return -EMSGSIZE;
3047}
3048
3049static int ip_vs_genl_dump_service(struct sk_buff *skb,
3050 struct ip_vs_service *svc,
3051 struct netlink_callback *cb)
3052{
3053 void *hdr;
3054
Eric W. Biederman15e47302012-09-07 20:12:54 +00003055 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julius Volz9a812192008-08-14 14:08:44 +02003056 &ip_vs_genl_family, NLM_F_MULTI,
3057 IPVS_CMD_NEW_SERVICE);
3058 if (!hdr)
3059 return -EMSGSIZE;
3060
3061 if (ip_vs_genl_fill_service(skb, svc) < 0)
3062 goto nla_put_failure;
3063
Johannes Berg053c0952015-01-16 22:09:00 +01003064 genlmsg_end(skb, hdr);
3065 return 0;
Julius Volz9a812192008-08-14 14:08:44 +02003066
3067nla_put_failure:
3068 genlmsg_cancel(skb, hdr);
3069 return -EMSGSIZE;
3070}
3071
3072static int ip_vs_genl_dump_services(struct sk_buff *skb,
3073 struct netlink_callback *cb)
3074{
3075 int idx = 0, i;
3076 int start = cb->args[0];
3077 struct ip_vs_service *svc;
Eric W. Biederman9cfdd752015-09-21 13:03:00 -05003078 struct net *net = sock_net(skb->sk);
3079 struct netns_ipvs *ipvs = net_ipvs(net);
Julius Volz9a812192008-08-14 14:08:44 +02003080
3081 mutex_lock(&__ip_vs_mutex);
3082 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02003083 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05003084 if (++idx <= start || (svc->ipvs != ipvs))
Julius Volz9a812192008-08-14 14:08:44 +02003085 continue;
3086 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3087 idx--;
3088 goto nla_put_failure;
3089 }
3090 }
3091 }
3092
3093 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02003094 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
Eric W. Biederman3109d2f2015-09-21 13:01:44 -05003095 if (++idx <= start || (svc->ipvs != ipvs))
Julius Volz9a812192008-08-14 14:08:44 +02003096 continue;
3097 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3098 idx--;
3099 goto nla_put_failure;
3100 }
3101 }
3102 }
3103
3104nla_put_failure:
3105 mutex_unlock(&__ip_vs_mutex);
3106 cb->args[0] = idx;
3107
3108 return skb->len;
3109}
3110
Paolo Abenidfe9c1d2017-04-20 11:44:16 +02003111static bool ip_vs_is_af_valid(int af)
3112{
3113 if (af == AF_INET)
3114 return true;
3115#ifdef CONFIG_IP_VS_IPV6
3116 if (af == AF_INET6 && ipv6_mod_enabled())
3117 return true;
3118#endif
3119 return false;
3120}
3121
Eric W. Biederman613fb832015-09-21 13:02:06 -05003122static int ip_vs_genl_parse_service(struct netns_ipvs *ipvs,
Hans Schillstromfc723252011-01-03 14:44:43 +01003123 struct ip_vs_service_user_kern *usvc,
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003124 struct nlattr *nla, int full_entry,
3125 struct ip_vs_service **ret_svc)
Julius Volz9a812192008-08-14 14:08:44 +02003126{
3127 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3128 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003129 struct ip_vs_service *svc;
Julius Volz9a812192008-08-14 14:08:44 +02003130
3131 /* Parse mandatory identifying service fields first */
3132 if (nla == NULL ||
3133 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
3134 return -EINVAL;
3135
3136 nla_af = attrs[IPVS_SVC_ATTR_AF];
3137 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3138 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3139 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3140 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3141
3142 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3143 return -EINVAL;
3144
Simon Horman258c8892009-12-15 17:01:25 +01003145 memset(usvc, 0, sizeof(*usvc));
3146
Julius Volzc860c6b2008-09-02 15:55:36 +02003147 usvc->af = nla_get_u16(nla_af);
Paolo Abenidfe9c1d2017-04-20 11:44:16 +02003148 if (!ip_vs_is_af_valid(usvc->af))
Julius Volz9a812192008-08-14 14:08:44 +02003149 return -EAFNOSUPPORT;
3150
3151 if (nla_fwmark) {
3152 usvc->protocol = IPPROTO_TCP;
3153 usvc->fwmark = nla_get_u32(nla_fwmark);
3154 } else {
3155 usvc->protocol = nla_get_u16(nla_protocol);
3156 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
Julian Anastasov0a925862013-04-17 23:50:49 +03003157 usvc->port = nla_get_be16(nla_port);
Julius Volz9a812192008-08-14 14:08:44 +02003158 usvc->fwmark = 0;
3159 }
3160
Julian Anastasovceec4c32013-03-22 11:46:53 +02003161 rcu_read_lock();
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003162 if (usvc->fwmark)
Eric W. Biederman1ed8b942015-09-21 13:01:46 -05003163 svc = __ip_vs_svc_fwm_find(ipvs, usvc->af, usvc->fwmark);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003164 else
Eric W. Biedermanbb2e2a82015-09-21 13:01:48 -05003165 svc = __ip_vs_service_find(ipvs, usvc->af, usvc->protocol,
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003166 &usvc->addr, usvc->port);
Julian Anastasovceec4c32013-03-22 11:46:53 +02003167 rcu_read_unlock();
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003168 *ret_svc = svc;
3169
Julius Volz9a812192008-08-14 14:08:44 +02003170 /* If a full entry was requested, check for the additional fields */
3171 if (full_entry) {
Simon Horman0d1e71b2010-08-22 21:37:54 +09003172 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
Julius Volz9a812192008-08-14 14:08:44 +02003173 *nla_netmask;
3174 struct ip_vs_flags flags;
Julius Volz9a812192008-08-14 14:08:44 +02003175
3176 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
Simon Horman0d1e71b2010-08-22 21:37:54 +09003177 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
Julius Volz9a812192008-08-14 14:08:44 +02003178 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3179 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3180 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3181
3182 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3183 return -EINVAL;
3184
3185 nla_memcpy(&flags, nla_flags, sizeof(flags));
3186
3187 /* prefill flags from service if it already exists */
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003188 if (svc)
Julius Volz9a812192008-08-14 14:08:44 +02003189 usvc->flags = svc->flags;
Julius Volz9a812192008-08-14 14:08:44 +02003190
3191 /* set new flags from userland */
3192 usvc->flags = (usvc->flags & ~flags.mask) |
3193 (flags.flags & flags.mask);
Julius Volzc860c6b2008-09-02 15:55:36 +02003194 usvc->sched_name = nla_data(nla_sched);
Simon Horman0d1e71b2010-08-22 21:37:54 +09003195 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
Julius Volz9a812192008-08-14 14:08:44 +02003196 usvc->timeout = nla_get_u32(nla_timeout);
Julian Anastasov0a925862013-04-17 23:50:49 +03003197 usvc->netmask = nla_get_be32(nla_netmask);
Julius Volz9a812192008-08-14 14:08:44 +02003198 }
3199
3200 return 0;
3201}
3202
Eric W. Biederman34c2f512015-09-21 13:02:07 -05003203static struct ip_vs_service *ip_vs_genl_find_service(struct netns_ipvs *ipvs,
Hans Schillstromfc723252011-01-03 14:44:43 +01003204 struct nlattr *nla)
Julius Volz9a812192008-08-14 14:08:44 +02003205{
Julius Volzc860c6b2008-09-02 15:55:36 +02003206 struct ip_vs_service_user_kern usvc;
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003207 struct ip_vs_service *svc;
Julius Volz9a812192008-08-14 14:08:44 +02003208 int ret;
3209
Eric W. Biederman613fb832015-09-21 13:02:06 -05003210 ret = ip_vs_genl_parse_service(ipvs, &usvc, nla, 0, &svc);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003211 return ret ? ERR_PTR(ret) : svc;
Julius Volz9a812192008-08-14 14:08:44 +02003212}
3213
3214static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3215{
3216 struct nlattr *nl_dest;
Julian Anastasovcd67cd52015-02-06 09:44:44 +02003217 struct ip_vs_kstats kstats;
Julius Volz9a812192008-08-14 14:08:44 +02003218
3219 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3220 if (!nl_dest)
3221 return -EMSGSIZE;
3222
David S. Miller969e8e22012-04-01 19:03:15 -04003223 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
Julian Anastasov0a925862013-04-17 23:50:49 +03003224 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
David S. Miller969e8e22012-04-01 19:03:15 -04003225 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3226 (atomic_read(&dest->conn_flags) &
3227 IP_VS_CONN_F_FWD_MASK)) ||
3228 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3229 atomic_read(&dest->weight)) ||
3230 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3231 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3232 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3233 atomic_read(&dest->activeconns)) ||
3234 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3235 atomic_read(&dest->inactconns)) ||
3236 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
Alex Gartrell6cff3392014-09-09 16:40:20 -07003237 atomic_read(&dest->persistconns)) ||
3238 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
David S. Miller969e8e22012-04-01 19:03:15 -04003239 goto nla_put_failure;
Julian Anastasovcd67cd52015-02-06 09:44:44 +02003240 ip_vs_copy_stats(&kstats, &dest->stats);
3241 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3242 goto nla_put_failure;
3243 if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
Julius Volz9a812192008-08-14 14:08:44 +02003244 goto nla_put_failure;
3245
3246 nla_nest_end(skb, nl_dest);
3247
3248 return 0;
3249
3250nla_put_failure:
3251 nla_nest_cancel(skb, nl_dest);
3252 return -EMSGSIZE;
3253}
3254
3255static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3256 struct netlink_callback *cb)
3257{
3258 void *hdr;
3259
Eric W. Biederman15e47302012-09-07 20:12:54 +00003260 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julius Volz9a812192008-08-14 14:08:44 +02003261 &ip_vs_genl_family, NLM_F_MULTI,
3262 IPVS_CMD_NEW_DEST);
3263 if (!hdr)
3264 return -EMSGSIZE;
3265
3266 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3267 goto nla_put_failure;
3268
Johannes Berg053c0952015-01-16 22:09:00 +01003269 genlmsg_end(skb, hdr);
3270 return 0;
Julius Volz9a812192008-08-14 14:08:44 +02003271
3272nla_put_failure:
3273 genlmsg_cancel(skb, hdr);
3274 return -EMSGSIZE;
3275}
3276
3277static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3278 struct netlink_callback *cb)
3279{
3280 int idx = 0;
3281 int start = cb->args[0];
3282 struct ip_vs_service *svc;
3283 struct ip_vs_dest *dest;
3284 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
Eric W. Biederman9cfdd752015-09-21 13:03:00 -05003285 struct net *net = sock_net(skb->sk);
Eric W. Biederman34c2f512015-09-21 13:02:07 -05003286 struct netns_ipvs *ipvs = net_ipvs(net);
Julius Volz9a812192008-08-14 14:08:44 +02003287
3288 mutex_lock(&__ip_vs_mutex);
3289
3290 /* Try to find the service for which to dump destinations */
3291 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
3292 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
3293 goto out_err;
3294
Hans Schillstroma0840e22011-01-03 14:44:58 +01003295
Eric W. Biederman34c2f512015-09-21 13:02:07 -05003296 svc = ip_vs_genl_find_service(ipvs, attrs[IPVS_CMD_ATTR_SERVICE]);
Julius Volz9a812192008-08-14 14:08:44 +02003297 if (IS_ERR(svc) || svc == NULL)
3298 goto out_err;
3299
3300 /* Dump the destinations */
3301 list_for_each_entry(dest, &svc->destinations, n_list) {
3302 if (++idx <= start)
3303 continue;
3304 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3305 idx--;
3306 goto nla_put_failure;
3307 }
3308 }
3309
3310nla_put_failure:
3311 cb->args[0] = idx;
Julius Volz9a812192008-08-14 14:08:44 +02003312
3313out_err:
3314 mutex_unlock(&__ip_vs_mutex);
3315
3316 return skb->len;
3317}
3318
Julius Volzc860c6b2008-09-02 15:55:36 +02003319static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
Julius Volz9a812192008-08-14 14:08:44 +02003320 struct nlattr *nla, int full_entry)
3321{
3322 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3323 struct nlattr *nla_addr, *nla_port;
Alex Gartrell6cff3392014-09-09 16:40:20 -07003324 struct nlattr *nla_addr_family;
Julius Volz9a812192008-08-14 14:08:44 +02003325
3326 /* Parse mandatory identifying destination fields first */
3327 if (nla == NULL ||
3328 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
3329 return -EINVAL;
3330
3331 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3332 nla_port = attrs[IPVS_DEST_ATTR_PORT];
Alex Gartrell6cff3392014-09-09 16:40:20 -07003333 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
Julius Volz9a812192008-08-14 14:08:44 +02003334
3335 if (!(nla_addr && nla_port))
3336 return -EINVAL;
3337
Simon Horman258c8892009-12-15 17:01:25 +01003338 memset(udest, 0, sizeof(*udest));
3339
Julius Volz9a812192008-08-14 14:08:44 +02003340 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
Julian Anastasov0a925862013-04-17 23:50:49 +03003341 udest->port = nla_get_be16(nla_port);
Julius Volz9a812192008-08-14 14:08:44 +02003342
Alex Gartrell6cff3392014-09-09 16:40:20 -07003343 if (nla_addr_family)
3344 udest->af = nla_get_u16(nla_addr_family);
3345 else
3346 udest->af = 0;
3347
Julius Volz9a812192008-08-14 14:08:44 +02003348 /* If a full entry was requested, check for the additional fields */
3349 if (full_entry) {
3350 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3351 *nla_l_thresh;
3352
3353 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3354 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3355 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3356 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3357
3358 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3359 return -EINVAL;
3360
3361 udest->conn_flags = nla_get_u32(nla_fwd)
3362 & IP_VS_CONN_F_FWD_MASK;
3363 udest->weight = nla_get_u32(nla_weight);
3364 udest->u_threshold = nla_get_u32(nla_u_thresh);
3365 udest->l_threshold = nla_get_u32(nla_l_thresh);
3366 }
3367
3368 return 0;
3369}
3370
Julian Anastasov0a925862013-04-17 23:50:49 +03003371static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
Julian Anastasove4ff6752015-07-26 15:03:27 +03003372 struct ipvs_sync_daemon_cfg *c)
Julius Volz9a812192008-08-14 14:08:44 +02003373{
3374 struct nlattr *nl_daemon;
3375
3376 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3377 if (!nl_daemon)
3378 return -EMSGSIZE;
3379
David S. Miller969e8e22012-04-01 19:03:15 -04003380 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
Julian Anastasove4ff6752015-07-26 15:03:27 +03003381 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, c->mcast_ifn) ||
3382 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, c->syncid) ||
Julian Anastasovd3328812015-07-26 15:03:28 +03003383 nla_put_u16(skb, IPVS_DAEMON_ATTR_SYNC_MAXLEN, c->sync_maxlen) ||
3384 nla_put_u16(skb, IPVS_DAEMON_ATTR_MCAST_PORT, c->mcast_port) ||
3385 nla_put_u8(skb, IPVS_DAEMON_ATTR_MCAST_TTL, c->mcast_ttl))
David S. Miller969e8e22012-04-01 19:03:15 -04003386 goto nla_put_failure;
Julian Anastasovd3328812015-07-26 15:03:28 +03003387#ifdef CONFIG_IP_VS_IPV6
3388 if (c->mcast_af == AF_INET6) {
3389 if (nla_put_in6_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP6,
3390 &c->mcast_group.in6))
3391 goto nla_put_failure;
3392 } else
3393#endif
3394 if (c->mcast_af == AF_INET &&
3395 nla_put_in_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP,
3396 c->mcast_group.ip))
3397 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003398 nla_nest_end(skb, nl_daemon);
3399
3400 return 0;
3401
3402nla_put_failure:
3403 nla_nest_cancel(skb, nl_daemon);
3404 return -EMSGSIZE;
3405}
3406
Julian Anastasov0a925862013-04-17 23:50:49 +03003407static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
Julian Anastasove4ff6752015-07-26 15:03:27 +03003408 struct ipvs_sync_daemon_cfg *c,
Julius Volz9a812192008-08-14 14:08:44 +02003409 struct netlink_callback *cb)
3410{
3411 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003412 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julius Volz9a812192008-08-14 14:08:44 +02003413 &ip_vs_genl_family, NLM_F_MULTI,
3414 IPVS_CMD_NEW_DAEMON);
3415 if (!hdr)
3416 return -EMSGSIZE;
3417
Julian Anastasove4ff6752015-07-26 15:03:27 +03003418 if (ip_vs_genl_fill_daemon(skb, state, c))
Julius Volz9a812192008-08-14 14:08:44 +02003419 goto nla_put_failure;
3420
Johannes Berg053c0952015-01-16 22:09:00 +01003421 genlmsg_end(skb, hdr);
3422 return 0;
Julius Volz9a812192008-08-14 14:08:44 +02003423
3424nla_put_failure:
3425 genlmsg_cancel(skb, hdr);
3426 return -EMSGSIZE;
3427}
3428
3429static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3430 struct netlink_callback *cb)
3431{
Eric W. Biederman9cfdd752015-09-21 13:03:00 -05003432 struct net *net = sock_net(skb->sk);
Hans Schillstromf1313152011-01-03 14:44:55 +01003433 struct netns_ipvs *ipvs = net_ipvs(net);
3434
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003435 mutex_lock(&ipvs->sync_mutex);
Hans Schillstromf1313152011-01-03 14:44:55 +01003436 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
Julius Volz9a812192008-08-14 14:08:44 +02003437 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
Julian Anastasove4ff6752015-07-26 15:03:27 +03003438 &ipvs->mcfg, cb) < 0)
Julius Volz9a812192008-08-14 14:08:44 +02003439 goto nla_put_failure;
3440
3441 cb->args[0] = 1;
3442 }
3443
Hans Schillstromf1313152011-01-03 14:44:55 +01003444 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
Julius Volz9a812192008-08-14 14:08:44 +02003445 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
Julian Anastasove4ff6752015-07-26 15:03:27 +03003446 &ipvs->bcfg, cb) < 0)
Julius Volz9a812192008-08-14 14:08:44 +02003447 goto nla_put_failure;
3448
3449 cb->args[1] = 1;
3450 }
3451
3452nla_put_failure:
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003453 mutex_unlock(&ipvs->sync_mutex);
Julius Volz9a812192008-08-14 14:08:44 +02003454
3455 return skb->len;
3456}
3457
Eric W. Biedermand8443c52015-09-21 13:02:08 -05003458static int ip_vs_genl_new_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
Julius Volz9a812192008-08-14 14:08:44 +02003459{
Julian Anastasove4ff6752015-07-26 15:03:27 +03003460 struct ipvs_sync_daemon_cfg c;
3461 struct nlattr *a;
Julian Anastasove0b26cc2015-07-26 14:57:34 +03003462 int ret;
3463
Julian Anastasove4ff6752015-07-26 15:03:27 +03003464 memset(&c, 0, sizeof(c));
Julius Volz9a812192008-08-14 14:08:44 +02003465 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3466 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3467 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3468 return -EINVAL;
Julian Anastasove4ff6752015-07-26 15:03:27 +03003469 strlcpy(c.mcast_ifn, nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3470 sizeof(c.mcast_ifn));
3471 c.syncid = nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]);
3472
3473 a = attrs[IPVS_DAEMON_ATTR_SYNC_MAXLEN];
3474 if (a)
3475 c.sync_maxlen = nla_get_u16(a);
Julius Volz9a812192008-08-14 14:08:44 +02003476
Julian Anastasovd3328812015-07-26 15:03:28 +03003477 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP];
3478 if (a) {
3479 c.mcast_af = AF_INET;
3480 c.mcast_group.ip = nla_get_in_addr(a);
3481 if (!ipv4_is_multicast(c.mcast_group.ip))
3482 return -EINVAL;
3483 } else {
3484 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP6];
3485 if (a) {
3486#ifdef CONFIG_IP_VS_IPV6
3487 int addr_type;
3488
3489 c.mcast_af = AF_INET6;
3490 c.mcast_group.in6 = nla_get_in6_addr(a);
3491 addr_type = ipv6_addr_type(&c.mcast_group.in6);
3492 if (!(addr_type & IPV6_ADDR_MULTICAST))
3493 return -EINVAL;
3494#else
3495 return -EAFNOSUPPORT;
3496#endif
3497 }
3498 }
3499
3500 a = attrs[IPVS_DAEMON_ATTR_MCAST_PORT];
3501 if (a)
3502 c.mcast_port = nla_get_u16(a);
3503
3504 a = attrs[IPVS_DAEMON_ATTR_MCAST_TTL];
3505 if (a)
3506 c.mcast_ttl = nla_get_u8(a);
3507
Alex Gartrell391f503d2014-09-09 16:40:24 -07003508 /* The synchronization protocol is incompatible with mixed family
3509 * services
3510 */
Julian Anastasove4ff6752015-07-26 15:03:27 +03003511 if (ipvs->mixed_address_family_dests > 0)
Alex Gartrell391f503d2014-09-09 16:40:24 -07003512 return -EINVAL;
3513
Eric W. Biederman6ac121d2015-09-21 13:02:10 -05003514 ret = start_sync_thread(ipvs, &c,
Julian Anastasove4ff6752015-07-26 15:03:27 +03003515 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
Julian Anastasove0b26cc2015-07-26 14:57:34 +03003516 return ret;
Julius Volz9a812192008-08-14 14:08:44 +02003517}
3518
Eric W. Biedermandf04ffb2015-09-21 13:02:09 -05003519static int ip_vs_genl_del_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
Julius Volz9a812192008-08-14 14:08:44 +02003520{
Julian Anastasove0b26cc2015-07-26 14:57:34 +03003521 int ret;
3522
Julius Volz9a812192008-08-14 14:08:44 +02003523 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3524 return -EINVAL;
3525
Julian Anastasove0b26cc2015-07-26 14:57:34 +03003526 mutex_lock(&ipvs->sync_mutex);
Eric W. Biedermanb3cf3cb2015-09-21 13:02:11 -05003527 ret = stop_sync_thread(ipvs,
Julian Anastasove0b26cc2015-07-26 14:57:34 +03003528 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3529 mutex_unlock(&ipvs->sync_mutex);
3530 return ret;
Julius Volz9a812192008-08-14 14:08:44 +02003531}
3532
Eric W. Biedermancacd1e62015-09-21 13:02:22 -05003533static int ip_vs_genl_set_config(struct netns_ipvs *ipvs, struct nlattr **attrs)
Julius Volz9a812192008-08-14 14:08:44 +02003534{
3535 struct ip_vs_timeout_user t;
3536
Eric W. Biedermanaf540342015-09-21 13:02:05 -05003537 __ip_vs_get_timeouts(ipvs, &t);
Julius Volz9a812192008-08-14 14:08:44 +02003538
3539 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3540 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3541
3542 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3543 t.tcp_fin_timeout =
3544 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3545
3546 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3547 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3548
Eric W. Biedermanf1faa1e2015-09-21 13:02:02 -05003549 return ip_vs_set_timeout(ipvs, &t);
Julius Volz9a812192008-08-14 14:08:44 +02003550}
3551
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003552static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3553{
Julian Anastasove0b26cc2015-07-26 14:57:34 +03003554 int ret = -EINVAL, cmd;
Eric W. Biederman9cfdd752015-09-21 13:03:00 -05003555 struct net *net = sock_net(skb->sk);
3556 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003557
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003558 cmd = info->genlhdr->cmd;
3559
3560 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3561 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3562
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003563 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3564 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3565 info->attrs[IPVS_CMD_ATTR_DAEMON],
Julian Anastasove0b26cc2015-07-26 14:57:34 +03003566 ip_vs_daemon_policy))
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003567 goto out;
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003568
3569 if (cmd == IPVS_CMD_NEW_DAEMON)
Eric W. Biedermand8443c52015-09-21 13:02:08 -05003570 ret = ip_vs_genl_new_daemon(ipvs, daemon_attrs);
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003571 else
Eric W. Biedermandf04ffb2015-09-21 13:02:09 -05003572 ret = ip_vs_genl_del_daemon(ipvs, daemon_attrs);
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003573 }
Julian Anastasove0b26cc2015-07-26 14:57:34 +03003574
3575out:
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003576 return ret;
3577}
3578
Julius Volz9a812192008-08-14 14:08:44 +02003579static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3580{
3581 struct ip_vs_service *svc = NULL;
Julius Volzc860c6b2008-09-02 15:55:36 +02003582 struct ip_vs_service_user_kern usvc;
3583 struct ip_vs_dest_user_kern udest;
Julius Volz9a812192008-08-14 14:08:44 +02003584 int ret = 0, cmd;
3585 int need_full_svc = 0, need_full_dest = 0;
Eric W. Biederman9cfdd752015-09-21 13:03:00 -05003586 struct net *net = sock_net(skb->sk);
3587 struct netns_ipvs *ipvs = net_ipvs(net);
Julius Volz9a812192008-08-14 14:08:44 +02003588
3589 cmd = info->genlhdr->cmd;
3590
3591 mutex_lock(&__ip_vs_mutex);
3592
3593 if (cmd == IPVS_CMD_FLUSH) {
Eric W. Biedermanef7c5992015-09-21 13:01:57 -05003594 ret = ip_vs_flush(ipvs, false);
Julius Volz9a812192008-08-14 14:08:44 +02003595 goto out;
3596 } else if (cmd == IPVS_CMD_SET_CONFIG) {
Eric W. Biedermancacd1e62015-09-21 13:02:22 -05003597 ret = ip_vs_genl_set_config(ipvs, info->attrs);
Julius Volz9a812192008-08-14 14:08:44 +02003598 goto out;
Julius Volz9a812192008-08-14 14:08:44 +02003599 } else if (cmd == IPVS_CMD_ZERO &&
3600 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
Eric W. Biedermanc60856c2015-09-21 13:01:59 -05003601 ret = ip_vs_zero_all(ipvs);
Julius Volz9a812192008-08-14 14:08:44 +02003602 goto out;
3603 }
3604
3605 /* All following commands require a service argument, so check if we
3606 * received a valid one. We need a full service specification when
3607 * adding / editing a service. Only identifying members otherwise. */
3608 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3609 need_full_svc = 1;
3610
Eric W. Biederman613fb832015-09-21 13:02:06 -05003611 ret = ip_vs_genl_parse_service(ipvs, &usvc,
Julius Volz9a812192008-08-14 14:08:44 +02003612 info->attrs[IPVS_CMD_ATTR_SERVICE],
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003613 need_full_svc, &svc);
Julius Volz9a812192008-08-14 14:08:44 +02003614 if (ret)
3615 goto out;
3616
Julius Volz9a812192008-08-14 14:08:44 +02003617 /* Unless we're adding a new service, the service must already exist */
3618 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3619 ret = -ESRCH;
3620 goto out;
3621 }
3622
3623 /* Destination commands require a valid destination argument. For
3624 * adding / editing a destination, we need a full destination
3625 * specification. */
3626 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3627 cmd == IPVS_CMD_DEL_DEST) {
3628 if (cmd != IPVS_CMD_DEL_DEST)
3629 need_full_dest = 1;
3630
3631 ret = ip_vs_genl_parse_dest(&udest,
3632 info->attrs[IPVS_CMD_ATTR_DEST],
3633 need_full_dest);
3634 if (ret)
3635 goto out;
Alex Gartrell6cff3392014-09-09 16:40:20 -07003636
3637 /* Old protocols did not allow the user to specify address
3638 * family, so we set it to zero instead. We also didn't
3639 * allow heterogeneous pools in the old code, so it's safe
3640 * to assume that this will have the same address family as
3641 * the service.
3642 */
3643 if (udest.af == 0)
3644 udest.af = svc->af;
Alex Gartrellbc18d372014-09-09 16:40:39 -07003645
Paolo Abenidfe9c1d2017-04-20 11:44:16 +02003646 if (!ip_vs_is_af_valid(udest.af)) {
3647 ret = -EAFNOSUPPORT;
3648 goto out;
3649 }
3650
Alexey Andriyanovdd3733b2015-02-06 22:32:20 +03003651 if (udest.af != svc->af && cmd != IPVS_CMD_DEL_DEST) {
Alex Gartrellbc18d372014-09-09 16:40:39 -07003652 /* The synchronization protocol is incompatible
3653 * with mixed family services
3654 */
Eric W. Biedermancd582782015-09-21 13:01:55 -05003655 if (ipvs->sync_state) {
Alex Gartrellbc18d372014-09-09 16:40:39 -07003656 ret = -EINVAL;
3657 goto out;
3658 }
3659
3660 /* Which connection types do we support? */
3661 switch (udest.conn_flags) {
3662 case IP_VS_CONN_F_TUNNEL:
3663 /* We are able to forward this */
3664 break;
3665 default:
3666 ret = -EINVAL;
3667 goto out;
3668 }
3669 }
Julius Volz9a812192008-08-14 14:08:44 +02003670 }
3671
3672 switch (cmd) {
3673 case IPVS_CMD_NEW_SERVICE:
3674 if (svc == NULL)
Eric W. Biederman5060bd82015-09-21 13:01:56 -05003675 ret = ip_vs_add_service(ipvs, &usvc, &svc);
Julius Volz9a812192008-08-14 14:08:44 +02003676 else
3677 ret = -EEXIST;
3678 break;
3679 case IPVS_CMD_SET_SERVICE:
3680 ret = ip_vs_edit_service(svc, &usvc);
3681 break;
3682 case IPVS_CMD_DEL_SERVICE:
3683 ret = ip_vs_del_service(svc);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003684 /* do not use svc, it can be freed */
Julius Volz9a812192008-08-14 14:08:44 +02003685 break;
3686 case IPVS_CMD_NEW_DEST:
3687 ret = ip_vs_add_dest(svc, &udest);
3688 break;
3689 case IPVS_CMD_SET_DEST:
3690 ret = ip_vs_edit_dest(svc, &udest);
3691 break;
3692 case IPVS_CMD_DEL_DEST:
3693 ret = ip_vs_del_dest(svc, &udest);
3694 break;
3695 case IPVS_CMD_ZERO:
3696 ret = ip_vs_zero_service(svc);
3697 break;
3698 default:
3699 ret = -EINVAL;
3700 }
3701
3702out:
Julius Volz9a812192008-08-14 14:08:44 +02003703 mutex_unlock(&__ip_vs_mutex);
3704
3705 return ret;
3706}
3707
3708static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3709{
3710 struct sk_buff *msg;
3711 void *reply;
3712 int ret, cmd, reply_cmd;
Eric W. Biederman9cfdd752015-09-21 13:03:00 -05003713 struct net *net = sock_net(skb->sk);
3714 struct netns_ipvs *ipvs = net_ipvs(net);
Julius Volz9a812192008-08-14 14:08:44 +02003715
3716 cmd = info->genlhdr->cmd;
3717
3718 if (cmd == IPVS_CMD_GET_SERVICE)
3719 reply_cmd = IPVS_CMD_NEW_SERVICE;
3720 else if (cmd == IPVS_CMD_GET_INFO)
3721 reply_cmd = IPVS_CMD_SET_INFO;
3722 else if (cmd == IPVS_CMD_GET_CONFIG)
3723 reply_cmd = IPVS_CMD_SET_CONFIG;
3724 else {
Hannes Eder1e3e2382009-08-02 11:05:41 +00003725 pr_err("unknown Generic Netlink command\n");
Julius Volz9a812192008-08-14 14:08:44 +02003726 return -EINVAL;
3727 }
3728
3729 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3730 if (!msg)
3731 return -ENOMEM;
3732
3733 mutex_lock(&__ip_vs_mutex);
3734
3735 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3736 if (reply == NULL)
3737 goto nla_put_failure;
3738
3739 switch (cmd) {
3740 case IPVS_CMD_GET_SERVICE:
3741 {
3742 struct ip_vs_service *svc;
3743
Eric W. Biederman34c2f512015-09-21 13:02:07 -05003744 svc = ip_vs_genl_find_service(ipvs,
Hans Schillstromfc723252011-01-03 14:44:43 +01003745 info->attrs[IPVS_CMD_ATTR_SERVICE]);
Julius Volz9a812192008-08-14 14:08:44 +02003746 if (IS_ERR(svc)) {
3747 ret = PTR_ERR(svc);
3748 goto out_err;
3749 } else if (svc) {
3750 ret = ip_vs_genl_fill_service(msg, svc);
Julius Volz9a812192008-08-14 14:08:44 +02003751 if (ret)
3752 goto nla_put_failure;
3753 } else {
3754 ret = -ESRCH;
3755 goto out_err;
3756 }
3757
3758 break;
3759 }
3760
3761 case IPVS_CMD_GET_CONFIG:
3762 {
3763 struct ip_vs_timeout_user t;
3764
Eric W. Biedermanaf540342015-09-21 13:02:05 -05003765 __ip_vs_get_timeouts(ipvs, &t);
Julius Volz9a812192008-08-14 14:08:44 +02003766#ifdef CONFIG_IP_VS_PROTO_TCP
David S. Miller969e8e22012-04-01 19:03:15 -04003767 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3768 t.tcp_timeout) ||
3769 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3770 t.tcp_fin_timeout))
3771 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003772#endif
3773#ifdef CONFIG_IP_VS_PROTO_UDP
David S. Miller969e8e22012-04-01 19:03:15 -04003774 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3775 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003776#endif
3777
3778 break;
3779 }
3780
3781 case IPVS_CMD_GET_INFO:
David S. Miller969e8e22012-04-01 19:03:15 -04003782 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3783 IP_VS_VERSION_CODE) ||
3784 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3785 ip_vs_conn_tab_size))
3786 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003787 break;
3788 }
3789
3790 genlmsg_end(msg, reply);
Johannes Berg134e6372009-07-10 09:51:34 +00003791 ret = genlmsg_reply(msg, info);
Julius Volz9a812192008-08-14 14:08:44 +02003792 goto out;
3793
3794nla_put_failure:
Hannes Eder1e3e2382009-08-02 11:05:41 +00003795 pr_err("not enough space in Netlink message\n");
Julius Volz9a812192008-08-14 14:08:44 +02003796 ret = -EMSGSIZE;
3797
3798out_err:
3799 nlmsg_free(msg);
3800out:
3801 mutex_unlock(&__ip_vs_mutex);
3802
3803 return ret;
3804}
3805
3806
Andi Kleenc61b0c12014-02-08 08:57:01 +01003807static const struct genl_ops ip_vs_genl_ops[] = {
Julius Volz9a812192008-08-14 14:08:44 +02003808 {
3809 .cmd = IPVS_CMD_NEW_SERVICE,
3810 .flags = GENL_ADMIN_PERM,
3811 .policy = ip_vs_cmd_policy,
3812 .doit = ip_vs_genl_set_cmd,
3813 },
3814 {
3815 .cmd = IPVS_CMD_SET_SERVICE,
3816 .flags = GENL_ADMIN_PERM,
3817 .policy = ip_vs_cmd_policy,
3818 .doit = ip_vs_genl_set_cmd,
3819 },
3820 {
3821 .cmd = IPVS_CMD_DEL_SERVICE,
3822 .flags = GENL_ADMIN_PERM,
3823 .policy = ip_vs_cmd_policy,
3824 .doit = ip_vs_genl_set_cmd,
3825 },
3826 {
3827 .cmd = IPVS_CMD_GET_SERVICE,
3828 .flags = GENL_ADMIN_PERM,
3829 .doit = ip_vs_genl_get_cmd,
3830 .dumpit = ip_vs_genl_dump_services,
3831 .policy = ip_vs_cmd_policy,
3832 },
3833 {
3834 .cmd = IPVS_CMD_NEW_DEST,
3835 .flags = GENL_ADMIN_PERM,
3836 .policy = ip_vs_cmd_policy,
3837 .doit = ip_vs_genl_set_cmd,
3838 },
3839 {
3840 .cmd = IPVS_CMD_SET_DEST,
3841 .flags = GENL_ADMIN_PERM,
3842 .policy = ip_vs_cmd_policy,
3843 .doit = ip_vs_genl_set_cmd,
3844 },
3845 {
3846 .cmd = IPVS_CMD_DEL_DEST,
3847 .flags = GENL_ADMIN_PERM,
3848 .policy = ip_vs_cmd_policy,
3849 .doit = ip_vs_genl_set_cmd,
3850 },
3851 {
3852 .cmd = IPVS_CMD_GET_DEST,
3853 .flags = GENL_ADMIN_PERM,
3854 .policy = ip_vs_cmd_policy,
3855 .dumpit = ip_vs_genl_dump_dests,
3856 },
3857 {
3858 .cmd = IPVS_CMD_NEW_DAEMON,
3859 .flags = GENL_ADMIN_PERM,
3860 .policy = ip_vs_cmd_policy,
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003861 .doit = ip_vs_genl_set_daemon,
Julius Volz9a812192008-08-14 14:08:44 +02003862 },
3863 {
3864 .cmd = IPVS_CMD_DEL_DAEMON,
3865 .flags = GENL_ADMIN_PERM,
3866 .policy = ip_vs_cmd_policy,
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003867 .doit = ip_vs_genl_set_daemon,
Julius Volz9a812192008-08-14 14:08:44 +02003868 },
3869 {
3870 .cmd = IPVS_CMD_GET_DAEMON,
3871 .flags = GENL_ADMIN_PERM,
3872 .dumpit = ip_vs_genl_dump_daemons,
3873 },
3874 {
3875 .cmd = IPVS_CMD_SET_CONFIG,
3876 .flags = GENL_ADMIN_PERM,
3877 .policy = ip_vs_cmd_policy,
3878 .doit = ip_vs_genl_set_cmd,
3879 },
3880 {
3881 .cmd = IPVS_CMD_GET_CONFIG,
3882 .flags = GENL_ADMIN_PERM,
3883 .doit = ip_vs_genl_get_cmd,
3884 },
3885 {
3886 .cmd = IPVS_CMD_GET_INFO,
3887 .flags = GENL_ADMIN_PERM,
3888 .doit = ip_vs_genl_get_cmd,
3889 },
3890 {
3891 .cmd = IPVS_CMD_ZERO,
3892 .flags = GENL_ADMIN_PERM,
3893 .policy = ip_vs_cmd_policy,
3894 .doit = ip_vs_genl_set_cmd,
3895 },
3896 {
3897 .cmd = IPVS_CMD_FLUSH,
3898 .flags = GENL_ADMIN_PERM,
3899 .doit = ip_vs_genl_set_cmd,
3900 },
3901};
3902
3903static int __init ip_vs_genl_register(void)
3904{
Michał Mirosław8f698d52009-05-21 10:34:05 +00003905 return genl_register_family_with_ops(&ip_vs_genl_family,
Johannes Bergc53ed742013-11-19 15:19:31 +01003906 ip_vs_genl_ops);
Julius Volz9a812192008-08-14 14:08:44 +02003907}
3908
3909static void ip_vs_genl_unregister(void)
3910{
3911 genl_unregister_family(&ip_vs_genl_family);
3912}
3913
3914/* End of Generic Netlink interface definitions */
3915
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01003916/*
3917 * per netns intit/exit func.
3918 */
Simon Horman14e40542011-02-04 18:33:02 +09003919#ifdef CONFIG_SYSCTL
Eric W. Biederman8b8237a2015-09-21 13:02:25 -05003920static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01003921{
Eric W. Biederman8b8237a2015-09-21 13:02:25 -05003922 struct net *net = ipvs->net;
Hans Schillstromfc723252011-01-03 14:44:43 +01003923 int idx;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003924 struct ctl_table *tbl;
Hans Schillstromfc723252011-01-03 14:44:43 +01003925
Hans Schillstroma0840e22011-01-03 14:44:58 +01003926 atomic_set(&ipvs->dropentry, 0);
3927 spin_lock_init(&ipvs->dropentry_lock);
3928 spin_lock_init(&ipvs->droppacket_lock);
3929 spin_lock_init(&ipvs->securetcp_lock);
Hans Schillstroma0840e22011-01-03 14:44:58 +01003930
3931 if (!net_eq(net, &init_net)) {
3932 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3933 if (tbl == NULL)
Simon Horman14e40542011-02-04 18:33:02 +09003934 return -ENOMEM;
Eric W. Biederman464dc802012-11-16 03:02:59 +00003935
3936 /* Don't export sysctls to unprivileged users */
3937 if (net->user_ns != &init_user_ns)
3938 tbl[0].procname = NULL;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003939 } else
3940 tbl = vs_vars;
3941 /* Initialize sysctl defaults */
Eric W. Biederman717e9172015-09-21 13:01:39 -05003942 for (idx = 0; idx < ARRAY_SIZE(vs_vars); idx++) {
3943 if (tbl[idx].proc_handler == proc_do_defense_mode)
3944 tbl[idx].extra2 = ipvs;
3945 }
Hans Schillstroma0840e22011-01-03 14:44:58 +01003946 idx = 0;
3947 ipvs->sysctl_amemthresh = 1024;
3948 tbl[idx++].data = &ipvs->sysctl_amemthresh;
3949 ipvs->sysctl_am_droprate = 10;
3950 tbl[idx++].data = &ipvs->sysctl_am_droprate;
3951 tbl[idx++].data = &ipvs->sysctl_drop_entry;
3952 tbl[idx++].data = &ipvs->sysctl_drop_packet;
3953#ifdef CONFIG_IP_VS_NFCT
3954 tbl[idx++].data = &ipvs->sysctl_conntrack;
3955#endif
3956 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3957 ipvs->sysctl_snat_reroute = 1;
3958 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3959 ipvs->sysctl_sync_ver = 1;
3960 tbl[idx++].data = &ipvs->sysctl_sync_ver;
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +02003961 ipvs->sysctl_sync_ports = 1;
3962 tbl[idx++].data = &ipvs->sysctl_sync_ports;
Julian Anastasov4d0c8752013-06-24 22:44:41 +03003963 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
Pablo Neira Ayuso1c003b12012-05-08 19:39:49 +02003964 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3965 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3966 ipvs->sysctl_sync_sock_size = 0;
3967 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003968 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3969 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
Alexander Frolkinc6c96c12013-06-13 08:56:15 +01003970 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
3971 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003972 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
Simon Horman59e03502011-02-04 18:33:01 +09003973 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3974 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003975 tbl[idx].data = &ipvs->sysctl_sync_threshold;
3976 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
Julian Anastasov749c42b2012-04-24 23:46:40 +03003977 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3978 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3979 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3980 tbl[idx++].data = &ipvs->sysctl_sync_retries;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003981 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
Julian Anastasov3654e612012-07-20 11:59:53 +03003982 ipvs->sysctl_pmtu_disc = 1;
3983 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
Julian Anastasov0c125822013-03-09 23:25:04 +02003984 tbl[idx++].data = &ipvs->sysctl_backup_only;
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03003985 ipvs->sysctl_conn_reuse_mode = 1;
3986 tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
Alex Gartrell94485fe2015-08-26 09:40:34 -07003987 tbl[idx++].data = &ipvs->sysctl_schedule_icmp;
Alex Gartrell4e478092015-09-14 23:23:05 -07003988 tbl[idx++].data = &ipvs->sysctl_ignore_tunneled;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003989
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +00003990 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
Simon Horman04439292011-02-01 18:29:04 +01003991 if (ipvs->sysctl_hdr == NULL) {
3992 if (!net_eq(net, &init_net))
3993 kfree(tbl);
Simon Horman14e40542011-02-04 18:33:02 +09003994 return -ENOMEM;
Simon Horman04439292011-02-01 18:29:04 +01003995 }
Eric W. Biederman0f34d542015-09-21 13:02:23 -05003996 ip_vs_start_estimator(ipvs, &ipvs->tot_stats);
Hans Schillstroma0840e22011-01-03 14:44:58 +01003997 ipvs->sysctl_tbl = tbl;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01003998 /* Schedule defense work */
3999 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
4000 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
Simon Horman14e40542011-02-04 18:33:02 +09004001
4002 return 0;
4003}
4004
Eric W. Biederman8b8237a2015-09-21 13:02:25 -05004005static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs)
Simon Horman14e40542011-02-04 18:33:02 +09004006{
Eric W. Biederman8b8237a2015-09-21 13:02:25 -05004007 struct net *net = ipvs->net;
Simon Horman14e40542011-02-04 18:33:02 +09004008
4009 cancel_delayed_work_sync(&ipvs->defense_work);
4010 cancel_work_sync(&ipvs->defense_work.work);
4011 unregister_net_sysctl_table(ipvs->sysctl_hdr);
Eric W. Biederman0f34d542015-09-21 13:02:23 -05004012 ip_vs_stop_estimator(ipvs, &ipvs->tot_stats);
Tommi Rantalaf30bf2a2015-05-07 15:12:21 +03004013
4014 if (!net_eq(net, &init_net))
4015 kfree(ipvs->sysctl_tbl);
Simon Horman14e40542011-02-04 18:33:02 +09004016}
4017
4018#else
4019
Eric W. Biederman8b8237a2015-09-21 13:02:25 -05004020static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs) { return 0; }
4021static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs) { }
Simon Horman14e40542011-02-04 18:33:02 +09004022
4023#endif
4024
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02004025static struct notifier_block ip_vs_dst_notifier = {
4026 .notifier_call = ip_vs_dst_event,
Xin Long3d7eec12018-11-15 15:14:30 +08004027#ifdef CONFIG_IP_VS_IPV6
4028 .priority = ADDRCONF_NOTIFY_PRIORITY + 5,
4029#endif
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02004030};
4031
Eric W. Biederman3d993762015-09-21 13:02:26 -05004032int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs)
Simon Horman14e40542011-02-04 18:33:02 +09004033{
John Stultz827da442013-10-07 15:51:58 -07004034 int i, idx;
Simon Horman14e40542011-02-04 18:33:02 +09004035
Simon Horman14e40542011-02-04 18:33:02 +09004036 /* Initialize rs_table */
4037 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
Julian Anastasov276472e2013-03-21 11:58:08 +02004038 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
Simon Horman14e40542011-02-04 18:33:02 +09004039
4040 INIT_LIST_HEAD(&ipvs->dest_trash);
Julian Anastasov578bc3e2013-03-22 11:46:49 +02004041 spin_lock_init(&ipvs->dest_trash_lock);
4042 setup_timer(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire,
Eric W. Biederman8e743f12015-09-21 13:01:54 -05004043 (unsigned long) ipvs);
Simon Horman14e40542011-02-04 18:33:02 +09004044 atomic_set(&ipvs->ftpsvc_counter, 0);
4045 atomic_set(&ipvs->nullsvc_counter, 0);
Marco Angaroni39b97222016-04-05 18:26:29 +02004046 atomic_set(&ipvs->conn_out_counter, 0);
Simon Horman14e40542011-02-04 18:33:02 +09004047
4048 /* procfs stats */
4049 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
Joe Perches0a9ee812011-08-29 14:17:25 -07004050 if (!ipvs->tot_stats.cpustats)
Simon Horman14e40542011-02-04 18:33:02 +09004051 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -07004052
John Stultz827da442013-10-07 15:51:58 -07004053 for_each_possible_cpu(i) {
4054 struct ip_vs_cpu_stats *ipvs_tot_stats;
4055 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
4056 u64_stats_init(&ipvs_tot_stats->syncp);
4057 }
4058
Simon Horman14e40542011-02-04 18:33:02 +09004059 spin_lock_init(&ipvs->tot_stats.lock);
4060
Arnd Bergmannf6ca9f42016-01-27 14:52:01 +01004061 proc_create("ip_vs", 0, ipvs->net->proc_net, &ip_vs_info_fops);
4062 proc_create("ip_vs_stats", 0, ipvs->net->proc_net, &ip_vs_stats_fops);
4063 proc_create("ip_vs_stats_percpu", 0, ipvs->net->proc_net,
Gao fengd4beaa62013-02-18 01:34:54 +00004064 &ip_vs_stats_percpu_fops);
Simon Horman14e40542011-02-04 18:33:02 +09004065
Eric W. Biederman8b8237a2015-09-21 13:02:25 -05004066 if (ip_vs_control_net_init_sysctl(ipvs))
Simon Horman14e40542011-02-04 18:33:02 +09004067 goto err;
4068
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01004069 return 0;
4070
Simon Horman14e40542011-02-04 18:33:02 +09004071err:
Julian Anastasov2a0751a2011-03-04 12:20:35 +02004072 free_percpu(ipvs->tot_stats.cpustats);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01004073 return -ENOMEM;
4074}
4075
Eric W. Biederman3d993762015-09-21 13:02:26 -05004076void __net_exit ip_vs_control_net_cleanup(struct netns_ipvs *ipvs)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01004077{
Eric W. Biederman6c0e14f2015-09-21 13:01:52 -05004078 ip_vs_trash_cleanup(ipvs);
Eric W. Biederman8b8237a2015-09-21 13:02:25 -05004079 ip_vs_control_net_cleanup_sysctl(ipvs);
Arnd Bergmannf6ca9f42016-01-27 14:52:01 +01004080 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4081 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4082 remove_proc_entry("ip_vs", ipvs->net->proc_net);
Julian Anastasov2a0751a2011-03-04 12:20:35 +02004083 free_percpu(ipvs->tot_stats.cpustats);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01004084}
4085
Hans Schillstrom8537de82012-04-26 07:47:44 +02004086int __init ip_vs_register_nl_ioctl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087{
Hans Schillstromfc723252011-01-03 14:44:43 +01004088 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 ret = nf_register_sockopt(&ip_vs_sockopts);
4091 if (ret) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00004092 pr_err("cannot register sockopt.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02004093 goto err_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 }
4095
Julius Volz9a812192008-08-14 14:08:44 +02004096 ret = ip_vs_genl_register();
4097 if (ret) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00004098 pr_err("cannot register Generic Netlink interface.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02004099 goto err_genl;
Julius Volz9a812192008-08-14 14:08:44 +02004100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 return 0;
Hans Schillstromfc723252011-01-03 14:44:43 +01004102
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02004103err_genl:
4104 nf_unregister_sockopt(&ip_vs_sockopts);
4105err_sock:
Hans Schillstromfc723252011-01-03 14:44:43 +01004106 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107}
4108
Hans Schillstrom8537de82012-04-26 07:47:44 +02004109void ip_vs_unregister_nl_ioctl(void)
4110{
4111 ip_vs_genl_unregister();
4112 nf_unregister_sockopt(&ip_vs_sockopts);
4113}
4114
4115int __init ip_vs_control_init(void)
4116{
4117 int idx;
4118 int ret;
4119
4120 EnterFunction(2);
4121
Julian Anastasov276472e2013-03-21 11:58:08 +02004122 /* Initialize svc_table, ip_vs_svc_fwm_table */
Hans Schillstrom8537de82012-04-26 07:47:44 +02004123 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02004124 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
4125 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
Hans Schillstrom8537de82012-04-26 07:47:44 +02004126 }
4127
4128 smp_wmb(); /* Do we really need it now ? */
4129
4130 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
4131 if (ret < 0)
4132 return ret;
4133
4134 LeaveFunction(2);
4135 return 0;
4136}
4137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
4139void ip_vs_control_cleanup(void)
4140{
4141 EnterFunction(2);
Simon Horman7676e342011-05-19 21:32:57 +09004142 unregister_netdevice_notifier(&ip_vs_dst_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 LeaveFunction(2);
4144}