blob: 35be035ee0cec79b5b797efb46c287661b8dbc93 [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;
100 static int old_secure_tcp = 0;
101 int availmem;
102 int nomem;
103 int to_change = -1;
104
105 /* we only count free and buffered memory (in pages) */
106 si_meminfo(&i);
107 availmem = i.freeram + i.bufferram;
108 /* however in linux 2.5 the i.bufferram is total page cache size,
109 we need adjust it */
110 /* si_swapinfo(&i); */
111 /* availmem = availmem - (i.totalswap - i.freeswap); */
112
Hans Schillstroma0840e22011-01-03 14:44:58 +0100113 nomem = (availmem < ipvs->sysctl_amemthresh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Julian Anastasovaf9debd2005-07-11 20:59:57 -0700115 local_bh_disable();
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* drop_entry */
Hans Schillstroma0840e22011-01-03 14:44:58 +0100118 spin_lock(&ipvs->dropentry_lock);
119 switch (ipvs->sysctl_drop_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 case 0:
Hans Schillstroma0840e22011-01-03 14:44:58 +0100121 atomic_set(&ipvs->dropentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 break;
123 case 1:
124 if (nomem) {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100125 atomic_set(&ipvs->dropentry, 1);
126 ipvs->sysctl_drop_entry = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 } else {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100128 atomic_set(&ipvs->dropentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130 break;
131 case 2:
132 if (nomem) {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100133 atomic_set(&ipvs->dropentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 } else {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100135 atomic_set(&ipvs->dropentry, 0);
136 ipvs->sysctl_drop_entry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 };
138 break;
139 case 3:
Hans Schillstroma0840e22011-01-03 14:44:58 +0100140 atomic_set(&ipvs->dropentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142 }
Hans Schillstroma0840e22011-01-03 14:44:58 +0100143 spin_unlock(&ipvs->dropentry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* drop_packet */
Hans Schillstroma0840e22011-01-03 14:44:58 +0100146 spin_lock(&ipvs->droppacket_lock);
147 switch (ipvs->sysctl_drop_packet) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 case 0:
Hans Schillstroma0840e22011-01-03 14:44:58 +0100149 ipvs->drop_rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 break;
151 case 1:
152 if (nomem) {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100153 ipvs->drop_rate = ipvs->drop_counter
154 = ipvs->sysctl_amemthresh /
155 (ipvs->sysctl_amemthresh-availmem);
156 ipvs->sysctl_drop_packet = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 } else {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100158 ipvs->drop_rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160 break;
161 case 2:
162 if (nomem) {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100163 ipvs->drop_rate = ipvs->drop_counter
164 = ipvs->sysctl_amemthresh /
165 (ipvs->sysctl_amemthresh-availmem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 } else {
Hans Schillstroma0840e22011-01-03 14:44:58 +0100167 ipvs->drop_rate = 0;
168 ipvs->sysctl_drop_packet = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 break;
171 case 3:
Hans Schillstroma0840e22011-01-03 14:44:58 +0100172 ipvs->drop_rate = ipvs->sysctl_am_droprate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174 }
Hans Schillstroma0840e22011-01-03 14:44:58 +0100175 spin_unlock(&ipvs->droppacket_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* secure_tcp */
Hans Schillstroma0840e22011-01-03 14:44:58 +0100178 spin_lock(&ipvs->securetcp_lock);
179 switch (ipvs->sysctl_secure_tcp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 case 0:
181 if (old_secure_tcp >= 2)
182 to_change = 0;
183 break;
184 case 1:
185 if (nomem) {
186 if (old_secure_tcp < 2)
187 to_change = 1;
Hans Schillstroma0840e22011-01-03 14:44:58 +0100188 ipvs->sysctl_secure_tcp = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 } else {
190 if (old_secure_tcp >= 2)
191 to_change = 0;
192 }
193 break;
194 case 2:
195 if (nomem) {
196 if (old_secure_tcp < 2)
197 to_change = 1;
198 } else {
199 if (old_secure_tcp >= 2)
200 to_change = 0;
Hans Schillstroma0840e22011-01-03 14:44:58 +0100201 ipvs->sysctl_secure_tcp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 break;
204 case 3:
205 if (old_secure_tcp < 2)
206 to_change = 1;
207 break;
208 }
Hans Schillstroma0840e22011-01-03 14:44:58 +0100209 old_secure_tcp = ipvs->sysctl_secure_tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (to_change >= 0)
Hans Schillstrom93304192011-01-03 14:44:51 +0100211 ip_vs_protocol_timeout_change(ipvs,
Hans Schillstroma0840e22011-01-03 14:44:58 +0100212 ipvs->sysctl_secure_tcp > 1);
213 spin_unlock(&ipvs->securetcp_lock);
Julian Anastasovaf9debd2005-07-11 20:59:57 -0700214
215 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218
219/*
220 * Timer for checking the defense
221 */
222#define DEFENSE_TIMER_PERIOD 1*HZ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
David Howellsc4028952006-11-22 14:57:56 +0000224static void defense_work_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Hans Schillstromf6340ee2011-01-03 14:44:59 +0100226 struct netns_ipvs *ipvs =
227 container_of(work, struct netns_ipvs, defense_work.work);
Hans Schillstrom93304192011-01-03 14:44:51 +0100228
229 update_defense_level(ipvs);
Hans Schillstroma0840e22011-01-03 14:44:58 +0100230 if (atomic_read(&ipvs->dropentry))
Hans Schillstromf6340ee2011-01-03 14:44:59 +0100231 ip_vs_random_dropentry(ipvs->net);
232 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
Simon Horman14e40542011-02-04 18:33:02 +0900234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236int
237ip_vs_use_count_inc(void)
238{
239 return try_module_get(THIS_MODULE);
240}
241
242void
243ip_vs_use_count_dec(void)
244{
245 module_put(THIS_MODULE);
246}
247
248
249/*
250 * Hash table: for virtual service lookups
251 */
252#define IP_VS_SVC_TAB_BITS 8
253#define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
254#define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
255
256/* the service table hashed by <protocol, addr, port> */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200257static struct hlist_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/* the service table hashed by fwmark */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200259static struct hlist_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/*
263 * Returns hash value for virtual service
264 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000265static inline unsigned int
266ip_vs_svc_hashkey(struct net *net, int af, unsigned int proto,
Hans Schillstromfc723252011-01-03 14:44:43 +0100267 const union nf_inet_addr *addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Eric Dumazet95c96172012-04-15 05:58:06 +0000269 register unsigned int porth = ntohs(port);
Julius Volzb18610d2008-09-02 15:55:37 +0200270 __be32 addr_fold = addr->ip;
Julian Anastasove9836f22013-03-09 23:25:07 +0200271 __u32 ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Julius Volzb18610d2008-09-02 15:55:37 +0200273#ifdef CONFIG_IP_VS_IPV6
274 if (af == AF_INET6)
275 addr_fold = addr->ip6[0]^addr->ip6[1]^
276 addr->ip6[2]^addr->ip6[3];
277#endif
Julian Anastasove9836f22013-03-09 23:25:07 +0200278 ahash = ntohl(addr_fold);
279 ahash ^= ((size_t) net >> 8);
Julius Volzb18610d2008-09-02 15:55:37 +0200280
Julian Anastasove9836f22013-03-09 23:25:07 +0200281 return (proto ^ ahash ^ (porth >> IP_VS_SVC_TAB_BITS) ^ porth) &
282 IP_VS_SVC_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/*
286 * Returns hash value of fwmark for virtual service lookup
287 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000288static inline unsigned int ip_vs_svc_fwm_hashkey(struct net *net, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Hans Schillstromfc723252011-01-03 14:44:43 +0100290 return (((size_t)net>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/*
Hans Schillstromfc723252011-01-03 14:44:43 +0100294 * Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * or in the ip_vs_svc_fwm_table by fwmark.
296 * Should be called with locked tables.
297 */
298static int ip_vs_svc_hash(struct ip_vs_service *svc)
299{
Eric Dumazet95c96172012-04-15 05:58:06 +0000300 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (svc->flags & IP_VS_SVC_F_HASHED) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000303 pr_err("%s(): request for already hashed, called from %pF\n",
304 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
306 }
307
308 if (svc->fwmark == 0) {
309 /*
Hans Schillstromfc723252011-01-03 14:44:43 +0100310 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
Hans Schillstromfc723252011-01-03 14:44:43 +0100312 hash = ip_vs_svc_hashkey(svc->net, svc->af, svc->protocol,
313 &svc->addr, svc->port);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200314 hlist_add_head_rcu(&svc->s_list, &ip_vs_svc_table[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 } else {
316 /*
Hans Schillstromfc723252011-01-03 14:44:43 +0100317 * Hash it by fwmark in svc_fwm_table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Hans Schillstromfc723252011-01-03 14:44:43 +0100319 hash = ip_vs_svc_fwm_hashkey(svc->net, svc->fwmark);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200320 hlist_add_head_rcu(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
323 svc->flags |= IP_VS_SVC_F_HASHED;
324 /* increase its refcnt because it is referenced by the svc table */
325 atomic_inc(&svc->refcnt);
326 return 1;
327}
328
329
330/*
Hans Schillstromfc723252011-01-03 14:44:43 +0100331 * Unhashes a service from svc_table / svc_fwm_table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * Should be called with locked tables.
333 */
334static int ip_vs_svc_unhash(struct ip_vs_service *svc)
335{
336 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000337 pr_err("%s(): request for unhash flagged, called from %pF\n",
338 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
340 }
341
342 if (svc->fwmark == 0) {
Hans Schillstromfc723252011-01-03 14:44:43 +0100343 /* Remove it from the svc_table table */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200344 hlist_del_rcu(&svc->s_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 } else {
Hans Schillstromfc723252011-01-03 14:44:43 +0100346 /* Remove it from the svc_fwm_table table */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200347 hlist_del_rcu(&svc->f_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350 svc->flags &= ~IP_VS_SVC_F_HASHED;
351 atomic_dec(&svc->refcnt);
352 return 1;
353}
354
355
356/*
Hans Schillstromfc723252011-01-03 14:44:43 +0100357 * Get service by {netns, proto,addr,port} in the service table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
Julius Volzb18610d2008-09-02 15:55:37 +0200359static inline struct ip_vs_service *
Hans Schillstromfc723252011-01-03 14:44:43 +0100360__ip_vs_service_find(struct net *net, int af, __u16 protocol,
361 const union nf_inet_addr *vaddr, __be16 vport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Eric Dumazet95c96172012-04-15 05:58:06 +0000363 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct ip_vs_service *svc;
365
366 /* Check for "full" addressed entries */
Hans Schillstromfc723252011-01-03 14:44:43 +0100367 hash = ip_vs_svc_hashkey(net, af, protocol, vaddr, vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Julian Anastasovceec4c32013-03-22 11:46:53 +0200369 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[hash], s_list) {
Julius Volzb18610d2008-09-02 15:55:37 +0200370 if ((svc->af == af)
371 && ip_vs_addr_equal(af, &svc->addr, vaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 && (svc->port == vport)
Hans Schillstromfc723252011-01-03 14:44:43 +0100373 && (svc->protocol == protocol)
374 && net_eq(svc->net, net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return svc;
377 }
378 }
379
380 return NULL;
381}
382
383
384/*
385 * Get service by {fwmark} in the service table.
386 */
Julius Volzb18610d2008-09-02 15:55:37 +0200387static inline struct ip_vs_service *
Hans Schillstromfc723252011-01-03 14:44:43 +0100388__ip_vs_svc_fwm_find(struct net *net, int af, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Eric Dumazet95c96172012-04-15 05:58:06 +0000390 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct ip_vs_service *svc;
392
393 /* Check for fwmark addressed entries */
Hans Schillstromfc723252011-01-03 14:44:43 +0100394 hash = ip_vs_svc_fwm_hashkey(net, fwmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Julian Anastasovceec4c32013-03-22 11:46:53 +0200396 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[hash], f_list) {
Hans Schillstromfc723252011-01-03 14:44:43 +0100397 if (svc->fwmark == fwmark && svc->af == af
398 && net_eq(svc->net, net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return svc;
401 }
402 }
403
404 return NULL;
405}
406
Julian Anastasovceec4c32013-03-22 11:46:53 +0200407/* Find service, called under RCU lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408struct ip_vs_service *
Julian Anastasovceec4c32013-03-22 11:46:53 +0200409ip_vs_service_find(struct net *net, int af, __u32 fwmark, __u16 protocol,
410 const union nf_inet_addr *vaddr, __be16 vport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 struct ip_vs_service *svc;
Hans Schillstrom763f8d02011-01-03 14:45:01 +0100413 struct netns_ipvs *ipvs = net_ipvs(net);
Julius Volz3c2e0502008-09-02 15:55:38 +0200414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
416 * Check the table hashed by fwmark first
417 */
Julian Anastasov097fc762011-03-04 12:26:17 +0200418 if (fwmark) {
419 svc = __ip_vs_svc_fwm_find(net, af, fwmark);
420 if (svc)
421 goto out;
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /*
425 * Check the table hashed by <protocol,addr,port>
426 * for "full" addressed entries
427 */
Hans Schillstromfc723252011-01-03 14:44:43 +0100428 svc = __ip_vs_service_find(net, af, protocol, vaddr, vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (svc == NULL
431 && protocol == IPPROTO_TCP
Hans Schillstrom763f8d02011-01-03 14:45:01 +0100432 && atomic_read(&ipvs->ftpsvc_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 && (vport == FTPDATA || ntohs(vport) >= PROT_SOCK)) {
434 /*
435 * Check if ftp service entry exists, the packet
436 * might belong to FTP data connections.
437 */
Hans Schillstromfc723252011-01-03 14:44:43 +0100438 svc = __ip_vs_service_find(net, af, protocol, vaddr, FTPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
441 if (svc == NULL
Hans Schillstrom763f8d02011-01-03 14:45:01 +0100442 && atomic_read(&ipvs->nullsvc_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
444 * Check if the catch-all port (port zero) exists
445 */
Hans Schillstromfc723252011-01-03 14:44:43 +0100446 svc = __ip_vs_service_find(net, af, protocol, vaddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 out:
Julius Volz3c2e0502008-09-02 15:55:38 +0200450 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
451 fwmark, ip_vs_proto_name(protocol),
452 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
453 svc ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 return svc;
456}
457
458
459static inline void
460__ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
461{
462 atomic_inc(&svc->refcnt);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300463 rcu_assign_pointer(dest->svc, svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Julian Anastasovceec4c32013-03-22 11:46:53 +0200466static void ip_vs_service_free(struct ip_vs_service *svc)
467{
468 if (svc->stats.cpustats)
469 free_percpu(svc->stats.cpustats);
470 kfree(svc);
471}
472
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300473static void ip_vs_service_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300475 struct ip_vs_service *svc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300477 svc = container_of(head, struct ip_vs_service, rcu_head);
478 ip_vs_service_free(svc);
479}
480
481static void __ip_vs_svc_put(struct ip_vs_service *svc, bool do_delay)
482{
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200483 if (atomic_dec_and_test(&svc->refcnt)) {
Julian Anastasovceec4c32013-03-22 11:46:53 +0200484 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200485 svc->fwmark,
486 IP_VS_DBG_ADDR(svc->af, &svc->addr),
Julian Anastasovceec4c32013-03-22 11:46:53 +0200487 ntohs(svc->port));
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300488 if (do_delay)
489 call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
490 else
491 ip_vs_service_free(svc);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495
496/*
497 * Returns hash value for real service
498 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000499static inline unsigned int ip_vs_rs_hashkey(int af,
Julius Volz7937df12008-09-02 15:55:48 +0200500 const union nf_inet_addr *addr,
501 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Eric Dumazet95c96172012-04-15 05:58:06 +0000503 register unsigned int porth = ntohs(port);
Julius Volz7937df12008-09-02 15:55:48 +0200504 __be32 addr_fold = addr->ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Julius Volz7937df12008-09-02 15:55:48 +0200506#ifdef CONFIG_IP_VS_IPV6
507 if (af == AF_INET6)
508 addr_fold = addr->ip6[0]^addr->ip6[1]^
509 addr->ip6[2]^addr->ip6[3];
510#endif
511
512 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 & IP_VS_RTAB_MASK;
514}
515
Julian Anastasov276472e2013-03-21 11:58:08 +0200516/* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
517static void ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Eric Dumazet95c96172012-04-15 05:58:06 +0000519 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Julian Anastasov276472e2013-03-21 11:58:08 +0200521 if (dest->in_rs_table)
522 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /*
525 * Hash by proto,addr,port,
526 * which are the parameters of the real service.
527 */
Julius Volz7937df12008-09-02 15:55:48 +0200528 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
529
Julian Anastasov276472e2013-03-21 11:58:08 +0200530 hlist_add_head_rcu(&dest->d_list, &ipvs->rs_table[hash]);
531 dest->in_rs_table = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Julian Anastasov276472e2013-03-21 11:58:08 +0200534/* Unhash ip_vs_dest from rs_table. */
535static void ip_vs_rs_unhash(struct ip_vs_dest *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 /*
Hans Schillstromfc723252011-01-03 14:44:43 +0100538 * Remove it from the rs_table table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
Julian Anastasov276472e2013-03-21 11:58:08 +0200540 if (dest->in_rs_table) {
541 hlist_del_rcu(&dest->d_list);
542 dest->in_rs_table = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Julian Anastasov276472e2013-03-21 11:58:08 +0200546/* Check if real service by <proto,addr,port> is present */
547bool ip_vs_has_real_service(struct net *net, int af, __u16 protocol,
548 const union nf_inet_addr *daddr, __be16 dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Hans Schillstromfc723252011-01-03 14:44:43 +0100550 struct netns_ipvs *ipvs = net_ipvs(net);
Eric Dumazet95c96172012-04-15 05:58:06 +0000551 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct ip_vs_dest *dest;
553
Julian Anastasov276472e2013-03-21 11:58:08 +0200554 /* Check for "full" addressed entries */
Julius Volz7937df12008-09-02 15:55:48 +0200555 hash = ip_vs_rs_hashkey(af, daddr, dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Julian Anastasov276472e2013-03-21 11:58:08 +0200557 rcu_read_lock();
558 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
559 if (dest->port == dport &&
560 dest->af == af &&
561 ip_vs_addr_equal(af, &dest->addr, daddr) &&
562 (dest->protocol == protocol || dest->vfwmark)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* HIT */
Julian Anastasov276472e2013-03-21 11:58:08 +0200564 rcu_read_unlock();
565 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 }
Julian Anastasov276472e2013-03-21 11:58:08 +0200568 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Julian Anastasov276472e2013-03-21 11:58:08 +0200570 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Julian Anastasov413c2d042013-03-22 11:46:52 +0200573/* Lookup destination by {addr,port} in the given service
574 * Called under RCU lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
576static struct ip_vs_dest *
Julius Volz7937df12008-09-02 15:55:48 +0200577ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
578 __be16 dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct ip_vs_dest *dest;
581
582 /*
583 * Find the destination for the given service
584 */
Julian Anastasov413c2d042013-03-22 11:46:52 +0200585 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
Julius Volz7937df12008-09-02 15:55:48 +0200586 if ((dest->af == svc->af)
587 && ip_vs_addr_equal(svc->af, &dest->addr, daddr)
588 && (dest->port == dport)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* HIT */
590 return dest;
591 }
592 }
593
594 return NULL;
595}
596
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800597/*
598 * Find destination by {daddr,dport,vaddr,protocol}
Julian Anastasov413c2d042013-03-22 11:46:52 +0200599 * Created to be used in ip_vs_process_message() in
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800600 * the backup synchronization daemon. It finds the
601 * destination to be bound to the received connection
602 * on the backup.
Julian Anastasov413c2d042013-03-22 11:46:52 +0200603 * Called under RCU lock, no refcnt is returned.
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800604 */
Hans Schillstromfc723252011-01-03 14:44:43 +0100605struct ip_vs_dest *ip_vs_find_dest(struct net *net, int af,
606 const union nf_inet_addr *daddr,
Julius Volz7937df12008-09-02 15:55:48 +0200607 __be16 dport,
608 const union nf_inet_addr *vaddr,
Julian Anastasov52793db2011-12-30 14:19:02 +0900609 __be16 vport, __u16 protocol, __u32 fwmark,
610 __u32 flags)
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800611{
612 struct ip_vs_dest *dest;
613 struct ip_vs_service *svc;
Julian Anastasov52793db2011-12-30 14:19:02 +0900614 __be16 port = dport;
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800615
Julian Anastasovceec4c32013-03-22 11:46:53 +0200616 svc = ip_vs_service_find(net, af, fwmark, protocol, vaddr, vport);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800617 if (!svc)
618 return NULL;
Julian Anastasov52793db2011-12-30 14:19:02 +0900619 if (fwmark && (flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ)
620 port = 0;
621 dest = ip_vs_lookup_dest(svc, daddr, port);
622 if (!dest)
623 dest = ip_vs_lookup_dest(svc, daddr, port ^ dport);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800624 return dest;
625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Julian Anastasov026ace02013-03-21 11:58:06 +0200627void ip_vs_dest_dst_rcu_free(struct rcu_head *head)
628{
629 struct ip_vs_dest_dst *dest_dst = container_of(head,
630 struct ip_vs_dest_dst,
631 rcu_head);
632
633 dst_release(dest_dst->dst_cache);
634 kfree(dest_dst);
635}
636
637/* Release dest_dst and dst_cache for dest in user context */
Julian Anastasovd1deae42013-03-21 11:58:02 +0200638static void __ip_vs_dst_cache_reset(struct ip_vs_dest *dest)
639{
Julian Anastasov026ace02013-03-21 11:58:06 +0200640 struct ip_vs_dest_dst *old;
Julian Anastasovd1deae42013-03-21 11:58:02 +0200641
Julian Anastasov026ace02013-03-21 11:58:06 +0200642 old = rcu_dereference_protected(dest->dest_dst, 1);
643 if (old) {
644 RCU_INIT_POINTER(dest->dest_dst, NULL);
645 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
646 }
Julian Anastasovd1deae42013-03-21 11:58:02 +0200647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/*
650 * Lookup dest by {svc,addr,port} in the destination trash.
651 * The destination trash is used to hold the destinations that are removed
652 * from the service table but are still referenced by some conn entries.
653 * The reason to add the destination trash is when the dest is temporary
654 * down (either by administrator or by monitor program), the dest can be
655 * picked back from the trash, the remaining connections to the dest can
656 * continue, and the counting information of the dest is also useful for
657 * scheduling.
658 */
659static struct ip_vs_dest *
Julius Volz7937df12008-09-02 15:55:48 +0200660ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
661 __be16 dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200663 struct ip_vs_dest *dest;
Hans Schillstromf2431e62011-01-03 14:45:00 +0100664 struct netns_ipvs *ipvs = net_ipvs(svc->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /*
667 * Find the destination in trash
668 */
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200669 spin_lock_bh(&ipvs->dest_trash_lock);
670 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
Julius Volz7937df12008-09-02 15:55:48 +0200671 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
672 "dest->refcnt=%d\n",
673 dest->vfwmark,
674 IP_VS_DBG_ADDR(svc->af, &dest->addr),
675 ntohs(dest->port),
676 atomic_read(&dest->refcnt));
677 if (dest->af == svc->af &&
678 ip_vs_addr_equal(svc->af, &dest->addr, daddr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 dest->port == dport &&
680 dest->vfwmark == svc->fwmark &&
681 dest->protocol == svc->protocol &&
682 (svc->fwmark ||
Julius Volz7937df12008-09-02 15:55:48 +0200683 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 dest->vport == svc->port))) {
685 /* HIT */
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200686 list_del(&dest->t_list);
687 ip_vs_dest_hold(dest);
688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 }
691
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200692 dest = NULL;
693
694out:
695 spin_unlock_bh(&ipvs->dest_trash_lock);
696
697 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200700static void ip_vs_dest_free(struct ip_vs_dest *dest)
701{
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300702 struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
703
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200704 __ip_vs_dst_cache_reset(dest);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300705 __ip_vs_svc_put(svc, false);
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200706 free_percpu(dest->stats.cpustats);
Julian Anastasov9e4e9482013-10-09 09:24:27 +0300707 ip_vs_dest_put_and_free(dest);
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710/*
711 * Clean up all the destinations in the trash
712 * Called by the ip_vs_control_cleanup()
713 *
714 * When the ip_vs_control_clearup is activated by ipvs module exit,
715 * the service tables must have been flushed and all the connections
716 * are expired, and the refcnt of each destination in the trash must
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200717 * be 0, so we simply release them here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Hans Schillstromf2431e62011-01-03 14:45:00 +0100719static void ip_vs_trash_cleanup(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 struct ip_vs_dest *dest, *nxt;
Hans Schillstromf2431e62011-01-03 14:45:00 +0100722 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Julian Anastasov578bc3e2013-03-22 11:46:49 +0200724 del_timer_sync(&ipvs->dest_trash_timer);
725 /* No need to use dest_trash_lock */
726 list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, t_list) {
727 list_del(&dest->t_list);
728 ip_vs_dest_free(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730}
731
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200732static void
733ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
734{
735#define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->ustats.c - src->ustats0.c
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200736
737 spin_lock_bh(&src->lock);
738
739 IP_VS_SHOW_STATS_COUNTER(conns);
740 IP_VS_SHOW_STATS_COUNTER(inpkts);
741 IP_VS_SHOW_STATS_COUNTER(outpkts);
742 IP_VS_SHOW_STATS_COUNTER(inbytes);
743 IP_VS_SHOW_STATS_COUNTER(outbytes);
744
Julian Anastasovea9f22c2011-03-14 01:41:54 +0200745 ip_vs_read_estimator(dst, src);
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200746
747 spin_unlock_bh(&src->lock);
748}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750static void
751ip_vs_zero_stats(struct ip_vs_stats *stats)
752{
753 spin_lock_bh(&stats->lock);
Simon Hormane93615d2008-08-11 17:19:14 +1000754
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200755 /* get current counters as zero point, rates are zeroed */
756
757#define IP_VS_ZERO_STATS_COUNTER(c) stats->ustats0.c = stats->ustats.c
Julian Anastasov55a3d4e2011-03-14 01:37:49 +0200758
759 IP_VS_ZERO_STATS_COUNTER(conns);
760 IP_VS_ZERO_STATS_COUNTER(inpkts);
761 IP_VS_ZERO_STATS_COUNTER(outpkts);
762 IP_VS_ZERO_STATS_COUNTER(inbytes);
763 IP_VS_ZERO_STATS_COUNTER(outbytes);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 ip_vs_zero_estimator(stats);
Simon Hormane93615d2008-08-11 17:19:14 +1000766
Sven Wegener3a14a3132008-08-10 18:24:41 +0000767 spin_unlock_bh(&stats->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770/*
771 * Update a destination in the given service
772 */
773static void
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200774__ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
775 struct ip_vs_dest_user_kern *udest, int add)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Hans Schillstromfc723252011-01-03 14:44:43 +0100777 struct netns_ipvs *ipvs = net_ipvs(svc->net);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300778 struct ip_vs_service *old_svc;
Julian Anastasovceec4c32013-03-22 11:46:53 +0200779 struct ip_vs_scheduler *sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int conn_flags;
781
782 /* set the weight and the flags */
783 atomic_set(&dest->weight, udest->weight);
Julian Anastasov35757922010-09-17 14:18:16 +0200784 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
785 conn_flags |= IP_VS_CONN_F_INACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
Julian Anastasov35757922010-09-17 14:18:16 +0200788 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
790 } else {
791 /*
Hans Schillstromfc723252011-01-03 14:44:43 +0100792 * Put the real service in rs_table if not present.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * For now only for NAT!
794 */
Hans Schillstromfc723252011-01-03 14:44:43 +0100795 ip_vs_rs_hash(ipvs, dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 atomic_set(&dest->conn_flags, conn_flags);
798
799 /* bind the service */
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300800 old_svc = rcu_dereference_protected(dest->svc, 1);
801 if (!old_svc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 __ip_vs_bind_svc(dest, svc);
803 } else {
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300804 if (old_svc != svc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ip_vs_zero_stats(&dest->stats);
806 __ip_vs_bind_svc(dest, svc);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300807 __ip_vs_svc_put(old_svc, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 }
810
811 /* set the dest status flags */
812 dest->flags |= IP_VS_DEST_F_AVAILABLE;
813
814 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
815 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
816 dest->u_threshold = udest->u_threshold;
817 dest->l_threshold = udest->l_threshold;
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200818
Julian Anastasovff75f402011-02-22 10:40:25 +0200819 spin_lock_bh(&dest->dst_lock);
Julian Anastasovd1deae42013-03-21 11:58:02 +0200820 __ip_vs_dst_cache_reset(dest);
Julian Anastasovff75f402011-02-22 10:40:25 +0200821 spin_unlock_bh(&dest->dst_lock);
Julian Anastasovfc604762010-10-17 16:38:15 +0300822
Julian Anastasovceec4c32013-03-22 11:46:53 +0200823 sched = rcu_dereference_protected(svc->scheduler, 1);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200824 if (add) {
Julian Anastasovceec4c32013-03-22 11:46:53 +0200825 ip_vs_start_estimator(svc->net, &dest->stats);
Julian Anastasov413c2d042013-03-22 11:46:52 +0200826 list_add_rcu(&dest->n_list, &svc->destinations);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200827 svc->num_dests++;
Julian Anastasovceec4c32013-03-22 11:46:53 +0200828 if (sched->add_dest)
829 sched->add_dest(svc, dest);
Julian Anastasov6b6df462013-03-22 11:46:37 +0200830 } else {
Julian Anastasovceec4c32013-03-22 11:46:53 +0200831 if (sched->upd_dest)
832 sched->upd_dest(svc, dest);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836
837/*
838 * Create a destination for the given service
839 */
840static int
Julius Volzc860c6b2008-09-02 15:55:36 +0200841ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 struct ip_vs_dest **dest_p)
843{
844 struct ip_vs_dest *dest;
John Stultz827da442013-10-07 15:51:58 -0700845 unsigned int atype, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 EnterFunction(2);
848
Vince Busam09571c72008-09-02 15:55:52 +0200849#ifdef CONFIG_IP_VS_IPV6
850 if (svc->af == AF_INET6) {
851 atype = ipv6_addr_type(&udest->addr.in6);
Sven Wegener3bfb92f2008-09-05 16:53:49 +0200852 if ((!(atype & IPV6_ADDR_UNICAST) ||
853 atype & IPV6_ADDR_LINKLOCAL) &&
Hans Schillstrom4a984802011-01-03 14:45:02 +0100854 !__ip_vs_addr_is_local_v6(svc->net, &udest->addr.in6))
Vince Busam09571c72008-09-02 15:55:52 +0200855 return -EINVAL;
856 } else
857#endif
858 {
Hans Schillstrom4a984802011-01-03 14:45:02 +0100859 atype = inet_addr_type(svc->net, udest->addr.ip);
Vince Busam09571c72008-09-02 15:55:52 +0200860 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
861 return -EINVAL;
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Simon Hormandee06e42010-08-26 02:54:31 +0000864 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
Joe Perches0a9ee812011-08-29 14:17:25 -0700865 if (dest == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -0700867
Hans Schillstromb17fc992011-01-03 14:44:56 +0100868 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
Joe Perches0a9ee812011-08-29 14:17:25 -0700869 if (!dest->stats.cpustats)
Hans Schillstromb17fc992011-01-03 14:44:56 +0100870 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
John Stultz827da442013-10-07 15:51:58 -0700872 for_each_possible_cpu(i) {
873 struct ip_vs_cpu_stats *ip_vs_dest_stats;
874 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
875 u64_stats_init(&ip_vs_dest_stats->syncp);
876 }
877
Julius Volzc860c6b2008-09-02 15:55:36 +0200878 dest->af = svc->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 dest->protocol = svc->protocol;
Julius Volzc860c6b2008-09-02 15:55:36 +0200880 dest->vaddr = svc->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 dest->vport = svc->port;
882 dest->vfwmark = svc->fwmark;
Julius Volzc860c6b2008-09-02 15:55:36 +0200883 ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 dest->port = udest->port;
885
886 atomic_set(&dest->activeconns, 0);
887 atomic_set(&dest->inactconns, 0);
888 atomic_set(&dest->persistconns, 0);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200889 atomic_set(&dest->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Julian Anastasov276472e2013-03-21 11:58:08 +0200891 INIT_HLIST_NODE(&dest->d_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 spin_lock_init(&dest->dst_lock);
893 spin_lock_init(&dest->stats.lock);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200894 __ip_vs_update_dest(svc, dest, udest, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 *dest_p = dest;
897
898 LeaveFunction(2);
899 return 0;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100900
901err_alloc:
902 kfree(dest);
903 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906
907/*
908 * Add a destination into an existing service
909 */
910static int
Julius Volzc860c6b2008-09-02 15:55:36 +0200911ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 struct ip_vs_dest *dest;
Julius Volzc860c6b2008-09-02 15:55:36 +0200914 union nf_inet_addr daddr;
Al Viro014d7302006-09-28 14:29:52 -0700915 __be16 dport = udest->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int ret;
917
918 EnterFunction(2);
919
920 if (udest->weight < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000921 pr_err("%s(): server weight less than zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -ERANGE;
923 }
924
925 if (udest->l_threshold > udest->u_threshold) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000926 pr_err("%s(): lower threshold is higher than upper threshold\n",
927 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return -ERANGE;
929 }
930
Julius Volzc860c6b2008-09-02 15:55:36 +0200931 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
932
Julian Anastasov413c2d042013-03-22 11:46:52 +0200933 /* We use function that requires RCU lock */
934 rcu_read_lock();
Julius Volz7937df12008-09-02 15:55:48 +0200935 dest = ip_vs_lookup_dest(svc, &daddr, dport);
Julian Anastasov413c2d042013-03-22 11:46:52 +0200936 rcu_read_unlock();
Julius Volz7937df12008-09-02 15:55:48 +0200937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (dest != NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000939 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EEXIST;
941 }
942
943 /*
944 * Check if the dest already exists in the trash and
945 * is from the same service
946 */
Julius Volz7937df12008-09-02 15:55:48 +0200947 dest = ip_vs_trash_get_dest(svc, &daddr, dport);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (dest != NULL) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200950 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
951 "dest->refcnt=%d, service %u/%s:%u\n",
952 IP_VS_DBG_ADDR(svc->af, &daddr), ntohs(dport),
953 atomic_read(&dest->refcnt),
954 dest->vfwmark,
955 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
956 ntohs(dest->vport));
957
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200958 __ip_vs_update_dest(svc, dest, udest, 1);
959 ret = 0;
960 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /*
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200962 * Allocate and initialize the dest structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200964 ret = ip_vs_new_dest(svc, udest, &dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 LeaveFunction(2);
967
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200968 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971
972/*
973 * Edit a destination in the given service
974 */
975static int
Julius Volzc860c6b2008-09-02 15:55:36 +0200976ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 struct ip_vs_dest *dest;
Julius Volzc860c6b2008-09-02 15:55:36 +0200979 union nf_inet_addr daddr;
Al Viro014d7302006-09-28 14:29:52 -0700980 __be16 dport = udest->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 EnterFunction(2);
983
984 if (udest->weight < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000985 pr_err("%s(): server weight less than zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return -ERANGE;
987 }
988
989 if (udest->l_threshold > udest->u_threshold) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000990 pr_err("%s(): lower threshold is higher than upper threshold\n",
991 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return -ERANGE;
993 }
994
Julius Volzc860c6b2008-09-02 15:55:36 +0200995 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
996
Julian Anastasov413c2d042013-03-22 11:46:52 +0200997 /* We use function that requires RCU lock */
998 rcu_read_lock();
Julius Volz7937df12008-09-02 15:55:48 +0200999 dest = ip_vs_lookup_dest(svc, &daddr, dport);
Julian Anastasov413c2d042013-03-22 11:46:52 +02001000 rcu_read_unlock();
Julius Volz7937df12008-09-02 15:55:48 +02001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (dest == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001003 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return -ENOENT;
1005 }
1006
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001007 __ip_vs_update_dest(svc, dest, udest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 LeaveFunction(2);
1009
1010 return 0;
1011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013/*
1014 * Delete a destination (must be already unlinked from the service)
1015 */
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001016static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest,
1017 bool cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Hans Schillstroma0840e22011-01-03 14:44:58 +01001019 struct netns_ipvs *ipvs = net_ipvs(net);
1020
Julian Anastasov6ef757f2011-03-14 01:44:28 +02001021 ip_vs_stop_estimator(net, &dest->stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /*
1024 * Remove it from the d-linked list with the real services.
1025 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 ip_vs_rs_unhash(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001028 spin_lock_bh(&ipvs->dest_trash_lock);
1029 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1030 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1031 atomic_read(&dest->refcnt));
1032 if (list_empty(&ipvs->dest_trash) && !cleanup)
1033 mod_timer(&ipvs->dest_trash_timer,
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001034 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001035 /* dest lives in trash without reference */
1036 list_add(&dest->t_list, &ipvs->dest_trash);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001037 dest->idle_start = 0;
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001038 spin_unlock_bh(&ipvs->dest_trash_lock);
1039 ip_vs_dest_put(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042
1043/*
1044 * Unlink a destination from the given service
1045 */
1046static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1047 struct ip_vs_dest *dest,
1048 int svcupd)
1049{
1050 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1051
1052 /*
1053 * Remove it from the d-linked destination list.
1054 */
Julian Anastasov413c2d042013-03-22 11:46:52 +02001055 list_del_rcu(&dest->n_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 svc->num_dests--;
Sven Wegener82dfb6f2008-08-11 19:36:06 +00001057
Julian Anastasovceec4c32013-03-22 11:46:53 +02001058 if (svcupd) {
1059 struct ip_vs_scheduler *sched;
Julian Anastasov6b6df462013-03-22 11:46:37 +02001060
Julian Anastasovceec4c32013-03-22 11:46:53 +02001061 sched = rcu_dereference_protected(svc->scheduler, 1);
1062 if (sched->del_dest)
1063 sched->del_dest(svc, dest);
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067
1068/*
1069 * Delete a destination server in the given service
1070 */
1071static int
Julius Volzc860c6b2008-09-02 15:55:36 +02001072ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 struct ip_vs_dest *dest;
Al Viro014d7302006-09-28 14:29:52 -07001075 __be16 dport = udest->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 EnterFunction(2);
1078
Julian Anastasov413c2d042013-03-22 11:46:52 +02001079 /* We use function that requires RCU lock */
1080 rcu_read_lock();
Julius Volz7937df12008-09-02 15:55:48 +02001081 dest = ip_vs_lookup_dest(svc, &udest->addr, dport);
Julian Anastasov413c2d042013-03-22 11:46:52 +02001082 rcu_read_unlock();
Julius Volzc860c6b2008-09-02 15:55:36 +02001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (dest == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001085 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -ENOENT;
1087 }
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /*
1090 * Unlink dest from the service
1091 */
1092 __ip_vs_unlink_dest(svc, dest, 1);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /*
1095 * Delete the destination
1096 */
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001097 __ip_vs_del_dest(svc->net, dest, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 LeaveFunction(2);
1100
1101 return 0;
1102}
1103
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001104static void ip_vs_dest_trash_expire(unsigned long data)
1105{
1106 struct net *net = (struct net *) data;
1107 struct netns_ipvs *ipvs = net_ipvs(net);
1108 struct ip_vs_dest *dest, *next;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001109 unsigned long now = jiffies;
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001110
1111 spin_lock(&ipvs->dest_trash_lock);
1112 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001113 if (atomic_read(&dest->refcnt) > 0)
1114 continue;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001115 if (dest->idle_start) {
1116 if (time_before(now, dest->idle_start +
1117 IP_VS_DEST_TRASH_PERIOD))
1118 continue;
1119 } else {
1120 dest->idle_start = max(1UL, now);
1121 continue;
1122 }
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001123 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1124 dest->vfwmark,
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001125 IP_VS_DBG_ADDR(dest->af, &dest->addr),
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001126 ntohs(dest->port));
1127 list_del(&dest->t_list);
1128 ip_vs_dest_free(dest);
1129 }
1130 if (!list_empty(&ipvs->dest_trash))
1131 mod_timer(&ipvs->dest_trash_timer,
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001132 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001133 spin_unlock(&ipvs->dest_trash_lock);
1134}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136/*
1137 * Add a service into the service hash table
1138 */
1139static int
Hans Schillstromfc723252011-01-03 14:44:43 +01001140ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u,
Julius Volzc860c6b2008-09-02 15:55:36 +02001141 struct ip_vs_service **svc_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
John Stultz827da442013-10-07 15:51:58 -07001143 int ret = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 struct ip_vs_scheduler *sched = NULL;
Simon Horman0d1e71b2010-08-22 21:37:54 +09001145 struct ip_vs_pe *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 struct ip_vs_service *svc = NULL;
Hans Schillstroma0840e22011-01-03 14:44:58 +01001147 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 /* increase the module use count */
1150 ip_vs_use_count_inc();
1151
1152 /* Lookup the scheduler by 'u->sched_name' */
1153 sched = ip_vs_scheduler_get(u->sched_name);
1154 if (sched == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001155 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 ret = -ENOENT;
Simon Horman6e08bfb2010-08-22 21:37:52 +09001157 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
Simon Horman0d1e71b2010-08-22 21:37:54 +09001160 if (u->pe_name && *u->pe_name) {
Simon Hormane9e5eee2010-11-08 20:05:57 +09001161 pe = ip_vs_pe_getbyname(u->pe_name);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001162 if (pe == NULL) {
1163 pr_info("persistence engine module ip_vs_pe_%s "
1164 "not found\n", u->pe_name);
1165 ret = -ENOENT;
1166 goto out_err;
1167 }
1168 }
1169
Julius Volzf94fd042008-09-02 15:55:55 +02001170#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov0a925862013-04-17 23:50:49 +03001171 if (u->af == AF_INET6) {
1172 __u32 plen = (__force __u32) u->netmask;
1173
1174 if (plen < 1 || plen > 128) {
1175 ret = -EINVAL;
1176 goto out_err;
1177 }
Julius Volzf94fd042008-09-02 15:55:55 +02001178 }
1179#endif
1180
Simon Hormandee06e42010-08-26 02:54:31 +00001181 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (svc == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001183 IP_VS_DBG(1, "%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ret = -ENOMEM;
1185 goto out_err;
1186 }
Hans Schillstromb17fc992011-01-03 14:44:56 +01001187 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
Julia Lawall0a54e932012-08-29 06:49:11 +00001188 if (!svc->stats.cpustats) {
1189 ret = -ENOMEM;
Hans Schillstromb17fc992011-01-03 14:44:56 +01001190 goto out_err;
Julia Lawall0a54e932012-08-29 06:49:11 +00001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
John Stultz827da442013-10-07 15:51:58 -07001193 for_each_possible_cpu(i) {
1194 struct ip_vs_cpu_stats *ip_vs_stats;
1195 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1196 u64_stats_init(&ip_vs_stats->syncp);
1197 }
1198
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* I'm the first user of the service */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 atomic_set(&svc->refcnt, 0);
1202
Julius Volzc860c6b2008-09-02 15:55:36 +02001203 svc->af = u->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 svc->protocol = u->protocol;
Julius Volzc860c6b2008-09-02 15:55:36 +02001205 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 svc->port = u->port;
1207 svc->fwmark = u->fwmark;
1208 svc->flags = u->flags;
1209 svc->timeout = u->timeout * HZ;
1210 svc->netmask = u->netmask;
Hans Schillstromfc723252011-01-03 14:44:43 +01001211 svc->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 INIT_LIST_HEAD(&svc->destinations);
Julian Anastasovba3a3ce2013-03-22 11:46:51 +02001214 spin_lock_init(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 spin_lock_init(&svc->stats.lock);
1216
1217 /* Bind the scheduler */
1218 ret = ip_vs_bind_scheduler(svc, sched);
1219 if (ret)
1220 goto out_err;
1221 sched = NULL;
1222
Simon Horman0d1e71b2010-08-22 21:37:54 +09001223 /* Bind the ct retriever */
Julian Anastasovceec4c32013-03-22 11:46:53 +02001224 RCU_INIT_POINTER(svc->pe, pe);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001225 pe = NULL;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /* Update the virtual service counters */
1228 if (svc->port == FTPPORT)
Hans Schillstrom763f8d02011-01-03 14:45:01 +01001229 atomic_inc(&ipvs->ftpsvc_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 else if (svc->port == 0)
Hans Schillstrom763f8d02011-01-03 14:45:01 +01001231 atomic_inc(&ipvs->nullsvc_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Julian Anastasov6ef757f2011-03-14 01:44:28 +02001233 ip_vs_start_estimator(net, &svc->stats);
Julius Volzf94fd042008-09-02 15:55:55 +02001234
1235 /* Count only IPv4 services for old get/setsockopt interface */
1236 if (svc->af == AF_INET)
Hans Schillstroma0840e22011-01-03 14:44:58 +01001237 ipvs->num_services++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /* Hash the service into the service table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 ip_vs_svc_hash(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 *svc_p = svc;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001243 /* Now there is a service - full throttle */
1244 ipvs->enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return 0;
1246
Hans Schillstromb17fc992011-01-03 14:44:56 +01001247
Simon Horman6e08bfb2010-08-22 21:37:52 +09001248 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (svc != NULL) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001250 ip_vs_unbind_scheduler(svc, sched);
1251 ip_vs_service_free(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253 ip_vs_scheduler_put(sched);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001254 ip_vs_pe_put(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* decrease the module use count */
1257 ip_vs_use_count_dec();
1258
1259 return ret;
1260}
1261
1262
1263/*
1264 * Edit a service and bind it with a new scheduler
1265 */
1266static int
Julius Volzc860c6b2008-09-02 15:55:36 +02001267ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 struct ip_vs_scheduler *sched, *old_sched;
Simon Horman0d1e71b2010-08-22 21:37:54 +09001270 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int ret = 0;
1272
1273 /*
1274 * Lookup the scheduler, by 'u->sched_name'
1275 */
1276 sched = ip_vs_scheduler_get(u->sched_name);
1277 if (sched == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001278 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return -ENOENT;
1280 }
1281 old_sched = sched;
1282
Simon Horman0d1e71b2010-08-22 21:37:54 +09001283 if (u->pe_name && *u->pe_name) {
Simon Hormane9e5eee2010-11-08 20:05:57 +09001284 pe = ip_vs_pe_getbyname(u->pe_name);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001285 if (pe == NULL) {
1286 pr_info("persistence engine module ip_vs_pe_%s "
1287 "not found\n", u->pe_name);
1288 ret = -ENOENT;
1289 goto out;
1290 }
1291 old_pe = pe;
1292 }
1293
Julius Volzf94fd042008-09-02 15:55:55 +02001294#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov0a925862013-04-17 23:50:49 +03001295 if (u->af == AF_INET6) {
1296 __u32 plen = (__force __u32) u->netmask;
1297
1298 if (plen < 1 || plen > 128) {
1299 ret = -EINVAL;
1300 goto out;
1301 }
Julius Volzf94fd042008-09-02 15:55:55 +02001302 }
1303#endif
1304
Julian Anastasovceec4c32013-03-22 11:46:53 +02001305 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1306 if (sched != old_sched) {
1307 /* Bind the new scheduler */
1308 ret = ip_vs_bind_scheduler(svc, sched);
1309 if (ret) {
1310 old_sched = sched;
1311 goto out;
1312 }
1313 /* Unbind the old scheduler on success */
1314 ip_vs_unbind_scheduler(svc, old_sched);
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /*
1318 * Set the flags and timeout value
1319 */
1320 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1321 svc->timeout = u->timeout * HZ;
1322 svc->netmask = u->netmask;
1323
Julian Anastasovceec4c32013-03-22 11:46:53 +02001324 old_pe = rcu_dereference_protected(svc->pe, 1);
1325 if (pe != old_pe)
1326 rcu_assign_pointer(svc->pe, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Hans Schillstrom552ad652011-06-13 12:19:26 +02001328out:
Simon Horman6e08bfb2010-08-22 21:37:52 +09001329 ip_vs_scheduler_put(old_sched);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001330 ip_vs_pe_put(old_pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return ret;
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/*
1335 * Delete a service from the service list
1336 * - The service must be unlinked, unlocked and not referenced!
1337 * - We are called under _bh lock
1338 */
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001339static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct ip_vs_dest *dest, *nxt;
1342 struct ip_vs_scheduler *old_sched;
Simon Horman0d1e71b2010-08-22 21:37:54 +09001343 struct ip_vs_pe *old_pe;
Hans Schillstroma0840e22011-01-03 14:44:58 +01001344 struct netns_ipvs *ipvs = net_ipvs(svc->net);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001345
1346 pr_info("%s: enter\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Julius Volzf94fd042008-09-02 15:55:55 +02001348 /* Count only IPv4 services for old get/setsockopt interface */
1349 if (svc->af == AF_INET)
Hans Schillstroma0840e22011-01-03 14:44:58 +01001350 ipvs->num_services--;
Julius Volzf94fd042008-09-02 15:55:55 +02001351
Julian Anastasov6ef757f2011-03-14 01:44:28 +02001352 ip_vs_stop_estimator(svc->net, &svc->stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /* Unbind scheduler */
Julian Anastasovceec4c32013-03-22 11:46:53 +02001355 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1356 ip_vs_unbind_scheduler(svc, old_sched);
Simon Horman6e08bfb2010-08-22 21:37:52 +09001357 ip_vs_scheduler_put(old_sched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Julian Anastasovceec4c32013-03-22 11:46:53 +02001359 /* Unbind persistence engine, keep svc->pe */
1360 old_pe = rcu_dereference_protected(svc->pe, 1);
Simon Horman0d1e71b2010-08-22 21:37:54 +09001361 ip_vs_pe_put(old_pe);
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /*
1364 * Unlink the whole destination list
1365 */
1366 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1367 __ip_vs_unlink_dest(svc, dest, 0);
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001368 __ip_vs_del_dest(svc->net, dest, cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
1371 /*
1372 * Update the virtual service counters
1373 */
1374 if (svc->port == FTPPORT)
Hans Schillstrom763f8d02011-01-03 14:45:01 +01001375 atomic_dec(&ipvs->ftpsvc_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 else if (svc->port == 0)
Hans Schillstrom763f8d02011-01-03 14:45:01 +01001377 atomic_dec(&ipvs->nullsvc_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 /*
1380 * Free the service if nobody refers to it
1381 */
Julian Anastasovbcbde4c2013-09-12 11:21:07 +03001382 __ip_vs_svc_put(svc, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /* decrease the module use count */
1385 ip_vs_use_count_dec();
1386}
1387
1388/*
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001389 * Unlink a service from list and try to delete it if its refcnt reached 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001391static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Julian Anastasovceec4c32013-03-22 11:46:53 +02001393 /* Hold svc to avoid double release from dest_trash */
1394 atomic_inc(&svc->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /*
1396 * Unhash it from the service table
1397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 ip_vs_svc_unhash(svc);
1399
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001400 __ip_vs_del_service(svc, cleanup);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001401}
1402
1403/*
1404 * Delete a service from the service list
1405 */
1406static int ip_vs_del_service(struct ip_vs_service *svc)
1407{
1408 if (svc == NULL)
1409 return -EEXIST;
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001410 ip_vs_unlink_service(svc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 return 0;
1413}
1414
1415
1416/*
1417 * Flush all the virtual services
1418 */
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001419static int ip_vs_flush(struct net *net, bool cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 int idx;
Julian Anastasovceec4c32013-03-22 11:46:53 +02001422 struct ip_vs_service *svc;
1423 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 /*
Hans Schillstromfc723252011-01-03 14:44:43 +01001426 * Flush the service table hashed by <netns,protocol,addr,port>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 */
1428 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001429 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1430 s_list) {
Hans Schillstromfc723252011-01-03 14:44:43 +01001431 if (net_eq(svc->net, net))
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001432 ip_vs_unlink_service(svc, cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 }
1435
1436 /*
1437 * Flush the service table hashed by fwmark
1438 */
1439 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001440 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1441 f_list) {
Hans Schillstromfc723252011-01-03 14:44:43 +01001442 if (net_eq(svc->net, net))
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001443 ip_vs_unlink_service(svc, cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 }
1446
1447 return 0;
1448}
1449
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001450/*
1451 * Delete service by {netns} in the service table.
1452 * Called by __ip_vs_cleanup()
1453 */
Hans Schillstrom503cf152011-05-01 18:50:16 +02001454void ip_vs_service_net_cleanup(struct net *net)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001455{
1456 EnterFunction(2);
1457 /* Check for "full" addressed entries */
1458 mutex_lock(&__ip_vs_mutex);
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001459 ip_vs_flush(net, true);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001460 mutex_unlock(&__ip_vs_mutex);
1461 LeaveFunction(2);
1462}
Julian Anastasovd1deae42013-03-21 11:58:02 +02001463
1464/* Put all references for device (dst_cache) */
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001465static inline void
Julian Anastasovd1deae42013-03-21 11:58:02 +02001466ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001467{
Julian Anastasovd717bb22013-04-17 23:50:45 +03001468 struct ip_vs_dest_dst *dest_dst;
1469
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001470 spin_lock_bh(&dest->dst_lock);
Julian Anastasovd717bb22013-04-17 23:50:45 +03001471 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1472 if (dest_dst && dest_dst->dst_cache->dev == dev) {
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001473 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1474 dev->name,
1475 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1476 ntohs(dest->port),
1477 atomic_read(&dest->refcnt));
Julian Anastasovd1deae42013-03-21 11:58:02 +02001478 __ip_vs_dst_cache_reset(dest);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001479 }
1480 spin_unlock_bh(&dest->dst_lock);
1481
1482}
Julian Anastasov313eae62013-03-21 11:58:00 +02001483/* Netdev event receiver
1484 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001485 */
1486static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00001487 void *ptr)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001488{
Jiri Pirko351638e2013-05-28 01:30:21 +00001489 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001490 struct net *net = dev_net(dev);
Julian Anastasov283283c2012-07-07 20:30:11 +03001491 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001492 struct ip_vs_service *svc;
1493 struct ip_vs_dest *dest;
1494 unsigned int idx;
1495
Julian Anastasov313eae62013-03-21 11:58:00 +02001496 if (event != NETDEV_DOWN || !ipvs)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001497 return NOTIFY_DONE;
1498 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1499 EnterFunction(2);
1500 mutex_lock(&__ip_vs_mutex);
1501 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001502 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001503 if (net_eq(svc->net, net)) {
1504 list_for_each_entry(dest, &svc->destinations,
1505 n_list) {
Julian Anastasovd1deae42013-03-21 11:58:02 +02001506 ip_vs_forget_dev(dest, dev);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001507 }
1508 }
1509 }
1510
Julian Anastasovceec4c32013-03-22 11:46:53 +02001511 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001512 if (net_eq(svc->net, net)) {
1513 list_for_each_entry(dest, &svc->destinations,
1514 n_list) {
Julian Anastasovd1deae42013-03-21 11:58:02 +02001515 ip_vs_forget_dev(dest, dev);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001516 }
1517 }
1518
1519 }
1520 }
1521
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001522 spin_lock_bh(&ipvs->dest_trash_lock);
1523 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
Julian Anastasovd1deae42013-03-21 11:58:02 +02001524 ip_vs_forget_dev(dest, dev);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001525 }
Julian Anastasov578bc3e2013-03-22 11:46:49 +02001526 spin_unlock_bh(&ipvs->dest_trash_lock);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001527 mutex_unlock(&__ip_vs_mutex);
1528 LeaveFunction(2);
1529 return NOTIFY_DONE;
1530}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532/*
1533 * Zero counters in a service or all services
1534 */
1535static int ip_vs_zero_service(struct ip_vs_service *svc)
1536{
1537 struct ip_vs_dest *dest;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 list_for_each_entry(dest, &svc->destinations, n_list) {
1540 ip_vs_zero_stats(&dest->stats);
1541 }
1542 ip_vs_zero_stats(&svc->stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return 0;
1544}
1545
Hans Schillstromfc723252011-01-03 14:44:43 +01001546static int ip_vs_zero_all(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 int idx;
1549 struct ip_vs_service *svc;
1550
1551 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001552 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
Hans Schillstromfc723252011-01-03 14:44:43 +01001553 if (net_eq(svc->net, net))
1554 ip_vs_zero_service(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 }
1557
1558 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001559 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
Hans Schillstromfc723252011-01-03 14:44:43 +01001560 if (net_eq(svc->net, net))
1561 ip_vs_zero_service(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563 }
1564
Julian Anastasov2a0751a2011-03-04 12:20:35 +02001565 ip_vs_zero_stats(&net_ipvs(net)->tot_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return 0;
1567}
1568
Simon Horman14e40542011-02-04 18:33:02 +09001569#ifdef CONFIG_SYSCTL
Julian Anastasov749c42b2012-04-24 23:46:40 +03001570
1571static int zero;
1572static int three = 3;
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574static int
Joe Perchesfe2c6332013-06-11 23:04:25 -07001575proc_do_defense_mode(struct ctl_table *table, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 void __user *buffer, size_t *lenp, loff_t *ppos)
1577{
Hans Schillstrom93304192011-01-03 14:44:51 +01001578 struct net *net = current->nsproxy->net_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 int *valp = table->data;
1580 int val = *valp;
1581 int rc;
1582
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001583 rc = proc_dointvec(table, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (write && (*valp != val)) {
1585 if ((*valp < 0) || (*valp > 3)) {
1586 /* Restore the correct value */
1587 *valp = val;
1588 } else {
Hans Schillstrom93304192011-01-03 14:44:51 +01001589 update_defense_level(net_ipvs(net));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591 }
1592 return rc;
1593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595static int
Joe Perchesfe2c6332013-06-11 23:04:25 -07001596proc_do_sync_threshold(struct ctl_table *table, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 void __user *buffer, size_t *lenp, loff_t *ppos)
1598{
1599 int *valp = table->data;
1600 int val[2];
1601 int rc;
1602
1603 /* backup the value first */
1604 memcpy(val, valp, sizeof(val));
1605
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001606 rc = proc_dointvec(table, write, buffer, lenp, ppos);
Julian Anastasov749c42b2012-04-24 23:46:40 +03001607 if (write && (valp[0] < 0 || valp[1] < 0 ||
1608 (valp[0] >= valp[1] && valp[1]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /* Restore the correct value */
1610 memcpy(valp, val, sizeof(val));
1611 }
1612 return rc;
1613}
1614
Hans Schillstromb880c1f2010-11-19 14:25:14 +01001615static int
Joe Perchesfe2c6332013-06-11 23:04:25 -07001616proc_do_sync_mode(struct ctl_table *table, int write,
Hans Schillstromb880c1f2010-11-19 14:25:14 +01001617 void __user *buffer, size_t *lenp, loff_t *ppos)
1618{
1619 int *valp = table->data;
1620 int val = *valp;
1621 int rc;
1622
1623 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1624 if (write && (*valp != val)) {
1625 if ((*valp < 0) || (*valp > 1)) {
1626 /* Restore the correct value */
1627 *valp = val;
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +02001628 }
1629 }
1630 return rc;
1631}
1632
1633static int
Joe Perchesfe2c6332013-06-11 23:04:25 -07001634proc_do_sync_ports(struct ctl_table *table, int write,
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +02001635 void __user *buffer, size_t *lenp, loff_t *ppos)
1636{
1637 int *valp = table->data;
1638 int val = *valp;
1639 int rc;
1640
1641 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1642 if (write && (*valp != val)) {
1643 if (*valp < 1 || !is_power_of_2(*valp)) {
1644 /* Restore the correct value */
1645 *valp = val;
Hans Schillstromb880c1f2010-11-19 14:25:14 +01001646 }
1647 }
1648 return rc;
1649}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651/*
1652 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
Hans Schillstroma0840e22011-01-03 14:44:58 +01001653 * Do not change order or insert new entries without
Hans Schillstrom503cf152011-05-01 18:50:16 +02001654 * align with netns init in ip_vs_control_net_init()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 */
1656
1657static struct ctl_table vs_vars[] = {
1658 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 .procname = "amemthresh",
Hans Schillstroma0840e22011-01-03 14:44:58 +01001660 .maxlen = sizeof(int),
1661 .mode = 0644,
1662 .proc_handler = proc_dointvec,
1663 },
1664 {
1665 .procname = "am_droprate",
1666 .maxlen = sizeof(int),
1667 .mode = 0644,
1668 .proc_handler = proc_dointvec,
1669 },
1670 {
1671 .procname = "drop_entry",
1672 .maxlen = sizeof(int),
1673 .mode = 0644,
1674 .proc_handler = proc_do_defense_mode,
1675 },
1676 {
1677 .procname = "drop_packet",
1678 .maxlen = sizeof(int),
1679 .mode = 0644,
1680 .proc_handler = proc_do_defense_mode,
1681 },
1682#ifdef CONFIG_IP_VS_NFCT
1683 {
1684 .procname = "conntrack",
1685 .maxlen = sizeof(int),
1686 .mode = 0644,
1687 .proc_handler = &proc_dointvec,
1688 },
1689#endif
1690 {
1691 .procname = "secure_tcp",
1692 .maxlen = sizeof(int),
1693 .mode = 0644,
1694 .proc_handler = proc_do_defense_mode,
1695 },
1696 {
1697 .procname = "snat_reroute",
1698 .maxlen = sizeof(int),
1699 .mode = 0644,
1700 .proc_handler = &proc_dointvec,
1701 },
1702 {
1703 .procname = "sync_version",
1704 .maxlen = sizeof(int),
1705 .mode = 0644,
1706 .proc_handler = &proc_do_sync_mode,
1707 },
1708 {
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +02001709 .procname = "sync_ports",
1710 .maxlen = sizeof(int),
1711 .mode = 0644,
1712 .proc_handler = &proc_do_sync_ports,
1713 },
1714 {
Julian Anastasov4d0c8752013-06-24 22:44:41 +03001715 .procname = "sync_persist_mode",
1716 .maxlen = sizeof(int),
1717 .mode = 0644,
1718 .proc_handler = proc_dointvec,
1719 },
1720 {
Pablo Neira Ayuso1c003b12012-05-08 19:39:49 +02001721 .procname = "sync_qlen_max",
Zhang Yanfei07995672013-04-29 11:55:10 -07001722 .maxlen = sizeof(unsigned long),
Pablo Neira Ayuso1c003b12012-05-08 19:39:49 +02001723 .mode = 0644,
Zhang Yanfei07995672013-04-29 11:55:10 -07001724 .proc_handler = proc_doulongvec_minmax,
Pablo Neira Ayuso1c003b12012-05-08 19:39:49 +02001725 },
1726 {
1727 .procname = "sync_sock_size",
1728 .maxlen = sizeof(int),
1729 .mode = 0644,
1730 .proc_handler = proc_dointvec,
1731 },
1732 {
Hans Schillstroma0840e22011-01-03 14:44:58 +01001733 .procname = "cache_bypass",
1734 .maxlen = sizeof(int),
1735 .mode = 0644,
1736 .proc_handler = proc_dointvec,
1737 },
1738 {
1739 .procname = "expire_nodest_conn",
1740 .maxlen = sizeof(int),
1741 .mode = 0644,
1742 .proc_handler = proc_dointvec,
1743 },
1744 {
Alexander Frolkinc6c96c12013-06-13 08:56:15 +01001745 .procname = "sloppy_tcp",
1746 .maxlen = sizeof(int),
1747 .mode = 0644,
1748 .proc_handler = proc_dointvec,
1749 },
1750 {
1751 .procname = "sloppy_sctp",
1752 .maxlen = sizeof(int),
1753 .mode = 0644,
1754 .proc_handler = proc_dointvec,
1755 },
1756 {
Hans Schillstroma0840e22011-01-03 14:44:58 +01001757 .procname = "expire_quiescent_template",
1758 .maxlen = sizeof(int),
1759 .mode = 0644,
1760 .proc_handler = proc_dointvec,
1761 },
1762 {
1763 .procname = "sync_threshold",
1764 .maxlen =
1765 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1766 .mode = 0644,
1767 .proc_handler = proc_do_sync_threshold,
1768 },
1769 {
Julian Anastasov749c42b2012-04-24 23:46:40 +03001770 .procname = "sync_refresh_period",
1771 .maxlen = sizeof(int),
1772 .mode = 0644,
1773 .proc_handler = proc_dointvec_jiffies,
1774 },
1775 {
1776 .procname = "sync_retries",
1777 .maxlen = sizeof(int),
1778 .mode = 0644,
1779 .proc_handler = proc_dointvec_minmax,
1780 .extra1 = &zero,
1781 .extra2 = &three,
1782 },
1783 {
Hans Schillstroma0840e22011-01-03 14:44:58 +01001784 .procname = "nat_icmp_send",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 .maxlen = sizeof(int),
1786 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001787 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 },
Julian Anastasov3654e612012-07-20 11:59:53 +03001789 {
1790 .procname = "pmtu_disc",
1791 .maxlen = sizeof(int),
1792 .mode = 0644,
1793 .proc_handler = proc_dointvec,
1794 },
Julian Anastasov0c125822013-03-09 23:25:04 +02001795 {
1796 .procname = "backup_only",
1797 .maxlen = sizeof(int),
1798 .mode = 0644,
1799 .proc_handler = proc_dointvec,
1800 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801#ifdef CONFIG_IP_VS_DEBUG
1802 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 .procname = "debug_level",
1804 .data = &sysctl_ip_vs_debug_level,
1805 .maxlen = sizeof(int),
1806 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001807 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 },
1809#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810#if 0
1811 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 .procname = "timeout_established",
1813 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
1814 .maxlen = sizeof(int),
1815 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001816 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 },
1818 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 .procname = "timeout_synsent",
1820 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
1821 .maxlen = sizeof(int),
1822 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001823 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 },
1825 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 .procname = "timeout_synrecv",
1827 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
1828 .maxlen = sizeof(int),
1829 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001830 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 },
1832 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 .procname = "timeout_finwait",
1834 .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
1835 .maxlen = sizeof(int),
1836 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001837 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 },
1839 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 .procname = "timeout_timewait",
1841 .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
1842 .maxlen = sizeof(int),
1843 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001844 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 },
1846 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 .procname = "timeout_close",
1848 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
1849 .maxlen = sizeof(int),
1850 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001851 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 },
1853 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 .procname = "timeout_closewait",
1855 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
1856 .maxlen = sizeof(int),
1857 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001858 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 },
1860 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 .procname = "timeout_lastack",
1862 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
1863 .maxlen = sizeof(int),
1864 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001865 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 },
1867 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 .procname = "timeout_listen",
1869 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
1870 .maxlen = sizeof(int),
1871 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001872 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 },
1874 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 .procname = "timeout_synack",
1876 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
1877 .maxlen = sizeof(int),
1878 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001879 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 },
1881 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 .procname = "timeout_udp",
1883 .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
1884 .maxlen = sizeof(int),
1885 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001886 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 },
1888 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 .procname = "timeout_icmp",
1890 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
1891 .maxlen = sizeof(int),
1892 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001893 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 },
1895#endif
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001896 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897};
1898
Simon Horman14e40542011-02-04 18:33:02 +09001899#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901#ifdef CONFIG_PROC_FS
1902
1903struct ip_vs_iter {
Hans Schillstromfc723252011-01-03 14:44:43 +01001904 struct seq_net_private p; /* Do not move this, netns depends upon it*/
Julian Anastasovceec4c32013-03-22 11:46:53 +02001905 struct hlist_head *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 int bucket;
1907};
1908
1909/*
1910 * Write the contents of the VS rule table to a PROCfs file.
1911 * (It is kept just for backward compatibility)
1912 */
Eric Dumazet95c96172012-04-15 05:58:06 +00001913static inline const char *ip_vs_fwd_name(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1916 case IP_VS_CONN_F_LOCALNODE:
1917 return "Local";
1918 case IP_VS_CONN_F_TUNNEL:
1919 return "Tunnel";
1920 case IP_VS_CONN_F_DROUTE:
1921 return "Route";
1922 default:
1923 return "Masq";
1924 }
1925}
1926
1927
1928/* Get the Nth entry in the two lists */
1929static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1930{
Hans Schillstromfc723252011-01-03 14:44:43 +01001931 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 struct ip_vs_iter *iter = seq->private;
1933 int idx;
1934 struct ip_vs_service *svc;
1935
1936 /* look in hash by protocol */
1937 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001938 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
Hans Schillstromfc723252011-01-03 14:44:43 +01001939 if (net_eq(svc->net, net) && pos-- == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 iter->table = ip_vs_svc_table;
1941 iter->bucket = idx;
1942 return svc;
1943 }
1944 }
1945 }
1946
1947 /* keep looking in fwmark */
1948 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001949 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
1950 f_list) {
Hans Schillstromfc723252011-01-03 14:44:43 +01001951 if (net_eq(svc->net, net) && pos-- == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 iter->table = ip_vs_svc_fwm_table;
1953 iter->bucket = idx;
1954 return svc;
1955 }
1956 }
1957 }
1958
1959 return NULL;
1960}
1961
1962static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
Julian Anastasov371990e2013-04-17 23:50:50 +03001963 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Julian Anastasovceec4c32013-03-22 11:46:53 +02001965 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1967}
1968
1969
1970static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1971{
Julian Anastasovceec4c32013-03-22 11:46:53 +02001972 struct hlist_node *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 struct ip_vs_iter *iter;
1974 struct ip_vs_service *svc;
1975
1976 ++*pos;
1977 if (v == SEQ_START_TOKEN)
1978 return ip_vs_info_array(seq,0);
1979
1980 svc = v;
1981 iter = seq->private;
1982
1983 if (iter->table == ip_vs_svc_table) {
1984 /* next service in table hashed by protocol */
Julian Anastasovceec4c32013-03-22 11:46:53 +02001985 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
1986 if (e)
1987 return hlist_entry(e, struct ip_vs_service, s_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02001990 hlist_for_each_entry_rcu(svc,
1991 &ip_vs_svc_table[iter->bucket],
1992 s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return svc;
1994 }
1995 }
1996
1997 iter->table = ip_vs_svc_fwm_table;
1998 iter->bucket = -1;
1999 goto scan_fwmark;
2000 }
2001
2002 /* next service in hashed by fwmark */
Julian Anastasovceec4c32013-03-22 11:46:53 +02002003 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
2004 if (e)
2005 return hlist_entry(e, struct ip_vs_service, f_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 scan_fwmark:
2008 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02002009 hlist_for_each_entry_rcu(svc,
2010 &ip_vs_svc_fwm_table[iter->bucket],
2011 f_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return svc;
2013 }
2014
2015 return NULL;
2016}
2017
2018static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
Julian Anastasov371990e2013-04-17 23:50:50 +03002019 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Julian Anastasovceec4c32013-03-22 11:46:53 +02002021 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
2024
2025static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2026{
2027 if (v == SEQ_START_TOKEN) {
2028 seq_printf(seq,
2029 "IP Virtual Server version %d.%d.%d (size=%d)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01002030 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 seq_puts(seq,
2032 "Prot LocalAddress:Port Scheduler Flags\n");
2033 seq_puts(seq,
2034 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2035 } else {
2036 const struct ip_vs_service *svc = v;
2037 const struct ip_vs_iter *iter = seq->private;
2038 const struct ip_vs_dest *dest;
Julian Anastasovceec4c32013-03-22 11:46:53 +02002039 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Vince Busam667a5f12008-09-02 15:55:49 +02002041 if (iter->table == ip_vs_svc_table) {
2042#ifdef CONFIG_IP_VS_IPV6
2043 if (svc->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002044 seq_printf(seq, "%s [%pI6]:%04X %s ",
Vince Busam667a5f12008-09-02 15:55:49 +02002045 ip_vs_proto_name(svc->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07002046 &svc->addr.in6,
Vince Busam667a5f12008-09-02 15:55:49 +02002047 ntohs(svc->port),
Julian Anastasovceec4c32013-03-22 11:46:53 +02002048 sched->name);
Vince Busam667a5f12008-09-02 15:55:49 +02002049 else
2050#endif
Nick Chalk26ec0372010-06-22 08:07:01 +02002051 seq_printf(seq, "%s %08X:%04X %s %s ",
Vince Busam667a5f12008-09-02 15:55:49 +02002052 ip_vs_proto_name(svc->protocol),
2053 ntohl(svc->addr.ip),
2054 ntohs(svc->port),
Julian Anastasovceec4c32013-03-22 11:46:53 +02002055 sched->name,
Nick Chalk26ec0372010-06-22 08:07:01 +02002056 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
Vince Busam667a5f12008-09-02 15:55:49 +02002057 } else {
Nick Chalk26ec0372010-06-22 08:07:01 +02002058 seq_printf(seq, "FWM %08X %s %s",
Julian Anastasovceec4c32013-03-22 11:46:53 +02002059 svc->fwmark, sched->name,
Nick Chalk26ec0372010-06-22 08:07:01 +02002060 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
Vince Busam667a5f12008-09-02 15:55:49 +02002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2064 seq_printf(seq, "persistent %d %08X\n",
2065 svc->timeout,
2066 ntohl(svc->netmask));
2067 else
2068 seq_putc(seq, '\n');
2069
Julian Anastasov413c2d042013-03-22 11:46:52 +02002070 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
Vince Busam667a5f12008-09-02 15:55:49 +02002071#ifdef CONFIG_IP_VS_IPV6
2072 if (dest->af == AF_INET6)
2073 seq_printf(seq,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002074 " -> [%pI6]:%04X"
Vince Busam667a5f12008-09-02 15:55:49 +02002075 " %-7s %-6d %-10d %-10d\n",
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07002076 &dest->addr.in6,
Vince Busam667a5f12008-09-02 15:55:49 +02002077 ntohs(dest->port),
2078 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2079 atomic_read(&dest->weight),
2080 atomic_read(&dest->activeconns),
2081 atomic_read(&dest->inactconns));
2082 else
2083#endif
2084 seq_printf(seq,
2085 " -> %08X:%04X "
2086 "%-7s %-6d %-10d %-10d\n",
2087 ntohl(dest->addr.ip),
2088 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095 }
2096 return 0;
2097}
2098
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002099static const struct seq_operations ip_vs_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 .start = ip_vs_info_seq_start,
2101 .next = ip_vs_info_seq_next,
2102 .stop = ip_vs_info_seq_stop,
2103 .show = ip_vs_info_seq_show,
2104};
2105
2106static int ip_vs_info_open(struct inode *inode, struct file *file)
2107{
Hans Schillstromfc723252011-01-03 14:44:43 +01002108 return seq_open_net(inode, file, &ip_vs_info_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002109 sizeof(struct ip_vs_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Arjan van de Ven9a321442007-02-12 00:55:35 -08002112static const struct file_operations ip_vs_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 .owner = THIS_MODULE,
2114 .open = ip_vs_info_open,
2115 .read = seq_read,
2116 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02002117 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118};
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120static int ip_vs_stats_show(struct seq_file *seq, void *v)
2121{
Hans Schillstromb17fc992011-01-03 14:44:56 +01002122 struct net *net = seq_file_single_net(seq);
Julian Anastasov55a3d4e2011-03-14 01:37:49 +02002123 struct ip_vs_stats_user show;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2126 seq_puts(seq,
2127 " Total Incoming Outgoing Incoming Outgoing\n");
2128 seq_printf(seq,
2129 " Conns Packets Packets Bytes Bytes\n");
2130
Julian Anastasov55a3d4e2011-03-14 01:37:49 +02002131 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2132 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", show.conns,
2133 show.inpkts, show.outpkts,
2134 (unsigned long long) show.inbytes,
2135 (unsigned long long) show.outbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2138 seq_puts(seq,
2139 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
Julian Anastasov55a3d4e2011-03-14 01:37:49 +02002140 seq_printf(seq, "%8X %8X %8X %16X %16X\n",
2141 show.cps, show.inpps, show.outpps,
2142 show.inbps, show.outbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 return 0;
2145}
2146
2147static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
2148{
Hans Schillstromfc723252011-01-03 14:44:43 +01002149 return single_open_net(inode, file, ip_vs_stats_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Arjan van de Ven9a321442007-02-12 00:55:35 -08002152static const struct file_operations ip_vs_stats_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 .owner = THIS_MODULE,
2154 .open = ip_vs_stats_seq_open,
2155 .read = seq_read,
2156 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02002157 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158};
2159
Hans Schillstromb17fc992011-01-03 14:44:56 +01002160static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2161{
2162 struct net *net = seq_file_single_net(seq);
Julian Anastasov2a0751a2011-03-04 12:20:35 +02002163 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
Julian Anastasov371990e2013-04-17 23:50:50 +03002164 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
Julian Anastasovea9f22c2011-03-14 01:41:54 +02002165 struct ip_vs_stats_user rates;
Hans Schillstromb17fc992011-01-03 14:44:56 +01002166 int i;
2167
2168/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2169 seq_puts(seq,
2170 " Total Incoming Outgoing Incoming Outgoing\n");
2171 seq_printf(seq,
2172 "CPU Conns Packets Packets Bytes Bytes\n");
2173
2174 for_each_possible_cpu(i) {
Julian Anastasov2a0751a2011-03-04 12:20:35 +02002175 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2176 unsigned int start;
2177 __u64 inbytes, outbytes;
2178
2179 do {
2180 start = u64_stats_fetch_begin_bh(&u->syncp);
2181 inbytes = u->ustats.inbytes;
2182 outbytes = u->ustats.outbytes;
2183 } while (u64_stats_fetch_retry_bh(&u->syncp, start));
2184
Hans Schillstromb17fc992011-01-03 14:44:56 +01002185 seq_printf(seq, "%3X %8X %8X %8X %16LX %16LX\n",
Julian Anastasov2a0751a2011-03-04 12:20:35 +02002186 i, u->ustats.conns, u->ustats.inpkts,
2187 u->ustats.outpkts, (__u64)inbytes,
2188 (__u64)outbytes);
Hans Schillstromb17fc992011-01-03 14:44:56 +01002189 }
2190
2191 spin_lock_bh(&tot_stats->lock);
Julian Anastasovea9f22c2011-03-14 01:41:54 +02002192
Hans Schillstromb17fc992011-01-03 14:44:56 +01002193 seq_printf(seq, " ~ %8X %8X %8X %16LX %16LX\n\n",
2194 tot_stats->ustats.conns, tot_stats->ustats.inpkts,
2195 tot_stats->ustats.outpkts,
2196 (unsigned long long) tot_stats->ustats.inbytes,
2197 (unsigned long long) tot_stats->ustats.outbytes);
2198
Julian Anastasovea9f22c2011-03-14 01:41:54 +02002199 ip_vs_read_estimator(&rates, tot_stats);
2200
2201 spin_unlock_bh(&tot_stats->lock);
2202
Hans Schillstromb17fc992011-01-03 14:44:56 +01002203/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2204 seq_puts(seq,
2205 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2206 seq_printf(seq, " %8X %8X %8X %16X %16X\n",
Julian Anastasovea9f22c2011-03-14 01:41:54 +02002207 rates.cps,
2208 rates.inpps,
2209 rates.outpps,
2210 rates.inbps,
2211 rates.outbps);
Hans Schillstromb17fc992011-01-03 14:44:56 +01002212
2213 return 0;
2214}
2215
2216static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file)
2217{
2218 return single_open_net(inode, file, ip_vs_stats_percpu_show);
2219}
2220
2221static const struct file_operations ip_vs_stats_percpu_fops = {
2222 .owner = THIS_MODULE,
2223 .open = ip_vs_stats_percpu_seq_open,
2224 .read = seq_read,
2225 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02002226 .release = single_release_net,
Hans Schillstromb17fc992011-01-03 14:44:56 +01002227};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228#endif
2229
2230/*
2231 * Set timeout values for tcp tcpfin udp in the timeout_table.
2232 */
Hans Schillstrom93304192011-01-03 14:44:51 +01002233static int ip_vs_set_timeout(struct net *net, struct ip_vs_timeout_user *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Changli Gao091bb342011-01-21 18:02:13 +08002235#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
Hans Schillstrom93304192011-01-03 14:44:51 +01002236 struct ip_vs_proto_data *pd;
Changli Gao091bb342011-01-21 18:02:13 +08002237#endif
Hans Schillstrom93304192011-01-03 14:44:51 +01002238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2240 u->tcp_timeout,
2241 u->tcp_fin_timeout,
2242 u->udp_timeout);
2243
2244#ifdef CONFIG_IP_VS_PROTO_TCP
2245 if (u->tcp_timeout) {
Hans Schillstrom93304192011-01-03 14:44:51 +01002246 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2247 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 = u->tcp_timeout * HZ;
2249 }
2250
2251 if (u->tcp_fin_timeout) {
Hans Schillstrom93304192011-01-03 14:44:51 +01002252 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2253 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 = u->tcp_fin_timeout * HZ;
2255 }
2256#endif
2257
2258#ifdef CONFIG_IP_VS_PROTO_UDP
2259 if (u->udp_timeout) {
Hans Schillstrom93304192011-01-03 14:44:51 +01002260 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2261 pd->timeout_table[IP_VS_UDP_S_NORMAL]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 = u->udp_timeout * HZ;
2263 }
2264#endif
2265 return 0;
2266}
2267
2268
2269#define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2270#define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2271#define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2272 sizeof(struct ip_vs_dest_user))
2273#define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2274#define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2275#define MAX_ARG_LEN SVCDEST_ARG_LEN
2276
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08002277static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
2279 [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
2280 [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
2281 [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
2282 [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
2283 [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
2284 [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
2285 [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
2286 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
2287 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
2288 [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
2289};
2290
Julius Volzc860c6b2008-09-02 15:55:36 +02002291static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2292 struct ip_vs_service_user *usvc_compat)
2293{
Simon Horman0d1e71b2010-08-22 21:37:54 +09002294 memset(usvc, 0, sizeof(*usvc));
2295
Julius Volzc860c6b2008-09-02 15:55:36 +02002296 usvc->af = AF_INET;
2297 usvc->protocol = usvc_compat->protocol;
2298 usvc->addr.ip = usvc_compat->addr;
2299 usvc->port = usvc_compat->port;
2300 usvc->fwmark = usvc_compat->fwmark;
2301
2302 /* Deep copy of sched_name is not needed here */
2303 usvc->sched_name = usvc_compat->sched_name;
2304
2305 usvc->flags = usvc_compat->flags;
2306 usvc->timeout = usvc_compat->timeout;
2307 usvc->netmask = usvc_compat->netmask;
2308}
2309
2310static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2311 struct ip_vs_dest_user *udest_compat)
2312{
Simon Horman0d1e71b2010-08-22 21:37:54 +09002313 memset(udest, 0, sizeof(*udest));
2314
Julius Volzc860c6b2008-09-02 15:55:36 +02002315 udest->addr.ip = udest_compat->addr;
2316 udest->port = udest_compat->port;
2317 udest->conn_flags = udest_compat->conn_flags;
2318 udest->weight = udest_compat->weight;
2319 udest->u_threshold = udest_compat->u_threshold;
2320 udest->l_threshold = udest_compat->l_threshold;
2321}
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323static int
2324do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2325{
Hans Schillstromfc723252011-01-03 14:44:43 +01002326 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 int ret;
2328 unsigned char arg[MAX_ARG_LEN];
Julius Volzc860c6b2008-09-02 15:55:36 +02002329 struct ip_vs_service_user *usvc_compat;
2330 struct ip_vs_service_user_kern usvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct ip_vs_service *svc;
Julius Volzc860c6b2008-09-02 15:55:36 +02002332 struct ip_vs_dest_user *udest_compat;
2333 struct ip_vs_dest_user_kern udest;
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002334 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002336 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return -EPERM;
2338
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002339 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2340 return -EINVAL;
2341 if (len < 0 || len > MAX_ARG_LEN)
2342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (len != set_arglen[SET_CMDID(cmd)]) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002344 pr_err("set_ctl: len %u != %u\n",
2345 len, set_arglen[SET_CMDID(cmd)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return -EINVAL;
2347 }
2348
2349 if (copy_from_user(arg, user, len) != 0)
2350 return -EFAULT;
2351
2352 /* increase the module use count */
2353 ip_vs_use_count_inc();
2354
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002355 /* Handle daemons since they have another lock */
2356 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2357 cmd == IP_VS_SO_SET_STOPDAEMON) {
2358 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2359
2360 if (mutex_lock_interruptible(&ipvs->sync_mutex)) {
2361 ret = -ERESTARTSYS;
2362 goto out_dec;
2363 }
2364 if (cmd == IP_VS_SO_SET_STARTDAEMON)
2365 ret = start_sync_thread(net, dm->state, dm->mcast_ifn,
2366 dm->syncid);
2367 else
2368 ret = stop_sync_thread(net, dm->state);
2369 mutex_unlock(&ipvs->sync_mutex);
2370 goto out_dec;
2371 }
2372
Ingo Molnar14cc3e22006-03-26 01:37:14 -08002373 if (mutex_lock_interruptible(&__ip_vs_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 ret = -ERESTARTSYS;
2375 goto out_dec;
2376 }
2377
2378 if (cmd == IP_VS_SO_SET_FLUSH) {
2379 /* Flush the virtual service */
Julian Anastasov578bc3e2013-03-22 11:46:49 +02002380 ret = ip_vs_flush(net, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 goto out_unlock;
2382 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2383 /* Set timeout values for (tcp tcpfin udp) */
Hans Schillstrom93304192011-01-03 14:44:51 +01002384 ret = ip_vs_set_timeout(net, (struct ip_vs_timeout_user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
Julius Volzc860c6b2008-09-02 15:55:36 +02002388 usvc_compat = (struct ip_vs_service_user *)arg;
2389 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2390
2391 /* We only use the new structs internally, so copy userspace compat
2392 * structs to extended internal versions */
2393 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2394 ip_vs_copy_udest_compat(&udest, udest_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 if (cmd == IP_VS_SO_SET_ZERO) {
2397 /* if no service address is set, zero counters in all */
Julius Volzc860c6b2008-09-02 15:55:36 +02002398 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
Hans Schillstromfc723252011-01-03 14:44:43 +01002399 ret = ip_vs_zero_all(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 goto out_unlock;
2401 }
2402 }
2403
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01002404 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2405 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2406 usvc.protocol != IPPROTO_SCTP) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002407 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2408 usvc.protocol, &usvc.addr.ip,
2409 ntohs(usvc.port), usvc.sched_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 ret = -EFAULT;
2411 goto out_unlock;
2412 }
2413
2414 /* Lookup the exact service by <protocol, addr, port> or fwmark */
Julian Anastasovceec4c32013-03-22 11:46:53 +02002415 rcu_read_lock();
Julius Volzc860c6b2008-09-02 15:55:36 +02002416 if (usvc.fwmark == 0)
Hans Schillstromfc723252011-01-03 14:44:43 +01002417 svc = __ip_vs_service_find(net, usvc.af, usvc.protocol,
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002418 &usvc.addr, usvc.port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 else
Hans Schillstromfc723252011-01-03 14:44:43 +01002420 svc = __ip_vs_svc_fwm_find(net, usvc.af, usvc.fwmark);
Julian Anastasovceec4c32013-03-22 11:46:53 +02002421 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 if (cmd != IP_VS_SO_SET_ADD
Julius Volzc860c6b2008-09-02 15:55:36 +02002424 && (svc == NULL || svc->protocol != usvc.protocol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 ret = -ESRCH;
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002426 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428
2429 switch (cmd) {
2430 case IP_VS_SO_SET_ADD:
2431 if (svc != NULL)
2432 ret = -EEXIST;
2433 else
Hans Schillstromfc723252011-01-03 14:44:43 +01002434 ret = ip_vs_add_service(net, &usvc, &svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 break;
2436 case IP_VS_SO_SET_EDIT:
Julius Volzc860c6b2008-09-02 15:55:36 +02002437 ret = ip_vs_edit_service(svc, &usvc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 break;
2439 case IP_VS_SO_SET_DEL:
2440 ret = ip_vs_del_service(svc);
2441 if (!ret)
2442 goto out_unlock;
2443 break;
2444 case IP_VS_SO_SET_ZERO:
2445 ret = ip_vs_zero_service(svc);
2446 break;
2447 case IP_VS_SO_SET_ADDDEST:
Julius Volzc860c6b2008-09-02 15:55:36 +02002448 ret = ip_vs_add_dest(svc, &udest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 break;
2450 case IP_VS_SO_SET_EDITDEST:
Julius Volzc860c6b2008-09-02 15:55:36 +02002451 ret = ip_vs_edit_dest(svc, &udest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 break;
2453 case IP_VS_SO_SET_DELDEST:
Julius Volzc860c6b2008-09-02 15:55:36 +02002454 ret = ip_vs_del_dest(svc, &udest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 break;
2456 default:
2457 ret = -EINVAL;
2458 }
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 out_unlock:
Ingo Molnar14cc3e22006-03-26 01:37:14 -08002461 mutex_unlock(&__ip_vs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 out_dec:
2463 /* decrease the module use count */
2464 ip_vs_use_count_dec();
2465
2466 return ret;
2467}
2468
2469
2470static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2472{
Julian Anastasovceec4c32013-03-22 11:46:53 +02002473 struct ip_vs_scheduler *sched;
2474
2475 sched = rcu_dereference_protected(src->scheduler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 dst->protocol = src->protocol;
Julius Volze7ade462008-09-02 15:55:33 +02002477 dst->addr = src->addr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 dst->port = src->port;
2479 dst->fwmark = src->fwmark;
Julian Anastasovceec4c32013-03-22 11:46:53 +02002480 strlcpy(dst->sched_name, sched->name, sizeof(dst->sched_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 dst->flags = src->flags;
2482 dst->timeout = src->timeout / HZ;
2483 dst->netmask = src->netmask;
2484 dst->num_dests = src->num_dests;
2485 ip_vs_copy_stats(&dst->stats, &src->stats);
2486}
2487
2488static inline int
Hans Schillstromfc723252011-01-03 14:44:43 +01002489__ip_vs_get_service_entries(struct net *net,
2490 const struct ip_vs_get_services *get,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 struct ip_vs_get_services __user *uptr)
2492{
2493 int idx, count=0;
2494 struct ip_vs_service *svc;
2495 struct ip_vs_service_entry entry;
2496 int ret = 0;
2497
2498 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02002499 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
Julius Volzf94fd042008-09-02 15:55:55 +02002500 /* Only expose IPv4 entries to old interface */
Hans Schillstromfc723252011-01-03 14:44:43 +01002501 if (svc->af != AF_INET || !net_eq(svc->net, net))
Julius Volzf94fd042008-09-02 15:55:55 +02002502 continue;
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (count >= get->num_services)
2505 goto out;
pageexec4da62fc2005-06-26 16:00:19 -07002506 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 ip_vs_copy_service(&entry, svc);
2508 if (copy_to_user(&uptr->entrytable[count],
2509 &entry, sizeof(entry))) {
2510 ret = -EFAULT;
2511 goto out;
2512 }
2513 count++;
2514 }
2515 }
2516
2517 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02002518 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
Julius Volzf94fd042008-09-02 15:55:55 +02002519 /* Only expose IPv4 entries to old interface */
Hans Schillstromfc723252011-01-03 14:44:43 +01002520 if (svc->af != AF_INET || !net_eq(svc->net, net))
Julius Volzf94fd042008-09-02 15:55:55 +02002521 continue;
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (count >= get->num_services)
2524 goto out;
pageexec4da62fc2005-06-26 16:00:19 -07002525 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 ip_vs_copy_service(&entry, svc);
2527 if (copy_to_user(&uptr->entrytable[count],
2528 &entry, sizeof(entry))) {
2529 ret = -EFAULT;
2530 goto out;
2531 }
2532 count++;
2533 }
2534 }
Hans Schillstrom552ad652011-06-13 12:19:26 +02002535out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return ret;
2537}
2538
2539static inline int
Hans Schillstromfc723252011-01-03 14:44:43 +01002540__ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 struct ip_vs_get_dests __user *uptr)
2542{
2543 struct ip_vs_service *svc;
Julius Volzb18610d2008-09-02 15:55:37 +02002544 union nf_inet_addr addr = { .ip = get->addr };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 int ret = 0;
2546
Julian Anastasovceec4c32013-03-22 11:46:53 +02002547 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (get->fwmark)
Hans Schillstromfc723252011-01-03 14:44:43 +01002549 svc = __ip_vs_svc_fwm_find(net, AF_INET, get->fwmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 else
Hans Schillstromfc723252011-01-03 14:44:43 +01002551 svc = __ip_vs_service_find(net, AF_INET, get->protocol, &addr,
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002552 get->port);
Julian Anastasovceec4c32013-03-22 11:46:53 +02002553 rcu_read_unlock();
Julius Volzb18610d2008-09-02 15:55:37 +02002554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (svc) {
2556 int count = 0;
2557 struct ip_vs_dest *dest;
2558 struct ip_vs_dest_entry entry;
2559
Dan Carpentera8241c62013-06-03 12:00:49 +03002560 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 list_for_each_entry(dest, &svc->destinations, n_list) {
2562 if (count >= get->num_dests)
2563 break;
2564
Julius Volze7ade462008-09-02 15:55:33 +02002565 entry.addr = dest->addr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 entry.port = dest->port;
2567 entry.conn_flags = atomic_read(&dest->conn_flags);
2568 entry.weight = atomic_read(&dest->weight);
2569 entry.u_threshold = dest->u_threshold;
2570 entry.l_threshold = dest->l_threshold;
2571 entry.activeconns = atomic_read(&dest->activeconns);
2572 entry.inactconns = atomic_read(&dest->inactconns);
2573 entry.persistconns = atomic_read(&dest->persistconns);
2574 ip_vs_copy_stats(&entry.stats, &dest->stats);
2575 if (copy_to_user(&uptr->entrytable[count],
2576 &entry, sizeof(entry))) {
2577 ret = -EFAULT;
2578 break;
2579 }
2580 count++;
2581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 } else
2583 ret = -ESRCH;
2584 return ret;
2585}
2586
2587static inline void
Hans Schillstrom93304192011-01-03 14:44:51 +01002588__ip_vs_get_timeouts(struct net *net, struct ip_vs_timeout_user *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Changli Gao091bb342011-01-21 18:02:13 +08002590#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
Hans Schillstrom93304192011-01-03 14:44:51 +01002591 struct ip_vs_proto_data *pd;
Changli Gao091bb342011-01-21 18:02:13 +08002592#endif
Hans Schillstrom93304192011-01-03 14:44:51 +01002593
Arnd Bergmannb61a6022012-10-09 13:04:34 +09002594 memset(u, 0, sizeof (*u));
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596#ifdef CONFIG_IP_VS_PROTO_TCP
Hans Schillstrom93304192011-01-03 14:44:51 +01002597 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2598 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2599 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600#endif
2601#ifdef CONFIG_IP_VS_PROTO_UDP
Hans Schillstrom93304192011-01-03 14:44:51 +01002602 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 u->udp_timeout =
Hans Schillstrom93304192011-01-03 14:44:51 +01002604 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605#endif
2606}
2607
2608
2609#define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2610#define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
2611#define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
2612#define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
2613#define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
2614#define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2615#define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
2616
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08002617static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 [GET_CMDID(IP_VS_SO_GET_VERSION)] = 64,
2619 [GET_CMDID(IP_VS_SO_GET_INFO)] = GET_INFO_ARG_LEN,
2620 [GET_CMDID(IP_VS_SO_GET_SERVICES)] = GET_SERVICES_ARG_LEN,
2621 [GET_CMDID(IP_VS_SO_GET_SERVICE)] = GET_SERVICE_ARG_LEN,
2622 [GET_CMDID(IP_VS_SO_GET_DESTS)] = GET_DESTS_ARG_LEN,
2623 [GET_CMDID(IP_VS_SO_GET_TIMEOUT)] = GET_TIMEOUT_ARG_LEN,
2624 [GET_CMDID(IP_VS_SO_GET_DAEMON)] = GET_DAEMON_ARG_LEN,
2625};
2626
2627static int
2628do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2629{
2630 unsigned char arg[128];
2631 int ret = 0;
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002632 unsigned int copylen;
Hans Schillstromfc723252011-01-03 14:44:43 +01002633 struct net *net = sock_net(sk);
Hans Schillstromf1313152011-01-03 14:44:55 +01002634 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Hans Schillstromfc723252011-01-03 14:44:43 +01002636 BUG_ON(!net);
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002637 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return -EPERM;
2639
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002640 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2641 return -EINVAL;
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 if (*len < get_arglen[GET_CMDID(cmd)]) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002644 pr_err("get_ctl: len %u < %u\n",
2645 *len, get_arglen[GET_CMDID(cmd)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 return -EINVAL;
2647 }
2648
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002649 copylen = get_arglen[GET_CMDID(cmd)];
2650 if (copylen > 128)
2651 return -EINVAL;
2652
2653 if (copy_from_user(arg, user, copylen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return -EFAULT;
Hans Schillstromae1d48b2011-10-11 10:54:35 +09002655 /*
2656 * Handle daemons first since it has its own locking
2657 */
2658 if (cmd == IP_VS_SO_GET_DAEMON) {
2659 struct ip_vs_daemon_user d[2];
2660
2661 memset(&d, 0, sizeof(d));
2662 if (mutex_lock_interruptible(&ipvs->sync_mutex))
2663 return -ERESTARTSYS;
2664
2665 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2666 d[0].state = IP_VS_STATE_MASTER;
2667 strlcpy(d[0].mcast_ifn, ipvs->master_mcast_ifn,
2668 sizeof(d[0].mcast_ifn));
2669 d[0].syncid = ipvs->master_syncid;
2670 }
2671 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2672 d[1].state = IP_VS_STATE_BACKUP;
2673 strlcpy(d[1].mcast_ifn, ipvs->backup_mcast_ifn,
2674 sizeof(d[1].mcast_ifn));
2675 d[1].syncid = ipvs->backup_syncid;
2676 }
2677 if (copy_to_user(user, &d, sizeof(d)) != 0)
2678 ret = -EFAULT;
2679 mutex_unlock(&ipvs->sync_mutex);
2680 return ret;
2681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Ingo Molnar14cc3e22006-03-26 01:37:14 -08002683 if (mutex_lock_interruptible(&__ip_vs_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return -ERESTARTSYS;
2685
2686 switch (cmd) {
2687 case IP_VS_SO_GET_VERSION:
2688 {
2689 char buf[64];
2690
2691 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01002692 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2694 ret = -EFAULT;
2695 goto out;
2696 }
2697 *len = strlen(buf)+1;
2698 }
2699 break;
2700
2701 case IP_VS_SO_GET_INFO:
2702 {
2703 struct ip_vs_getinfo info;
2704 info.version = IP_VS_VERSION_CODE;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01002705 info.size = ip_vs_conn_tab_size;
Hans Schillstroma0840e22011-01-03 14:44:58 +01002706 info.num_services = ipvs->num_services;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (copy_to_user(user, &info, sizeof(info)) != 0)
2708 ret = -EFAULT;
2709 }
2710 break;
2711
2712 case IP_VS_SO_GET_SERVICES:
2713 {
2714 struct ip_vs_get_services *get;
2715 int size;
2716
2717 get = (struct ip_vs_get_services *)arg;
2718 size = sizeof(*get) +
2719 sizeof(struct ip_vs_service_entry) * get->num_services;
2720 if (*len != size) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002721 pr_err("length: %u != %u\n", *len, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 ret = -EINVAL;
2723 goto out;
2724 }
Hans Schillstromfc723252011-01-03 14:44:43 +01002725 ret = __ip_vs_get_service_entries(net, get, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 }
2727 break;
2728
2729 case IP_VS_SO_GET_SERVICE:
2730 {
2731 struct ip_vs_service_entry *entry;
2732 struct ip_vs_service *svc;
Julius Volzb18610d2008-09-02 15:55:37 +02002733 union nf_inet_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735 entry = (struct ip_vs_service_entry *)arg;
Julius Volzb18610d2008-09-02 15:55:37 +02002736 addr.ip = entry->addr;
Julian Anastasovceec4c32013-03-22 11:46:53 +02002737 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (entry->fwmark)
Hans Schillstromfc723252011-01-03 14:44:43 +01002739 svc = __ip_vs_svc_fwm_find(net, AF_INET, entry->fwmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 else
Hans Schillstromfc723252011-01-03 14:44:43 +01002741 svc = __ip_vs_service_find(net, AF_INET,
2742 entry->protocol, &addr,
2743 entry->port);
Julian Anastasovceec4c32013-03-22 11:46:53 +02002744 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (svc) {
2746 ip_vs_copy_service(entry, svc);
2747 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2748 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 } else
2750 ret = -ESRCH;
2751 }
2752 break;
2753
2754 case IP_VS_SO_GET_DESTS:
2755 {
2756 struct ip_vs_get_dests *get;
2757 int size;
2758
2759 get = (struct ip_vs_get_dests *)arg;
2760 size = sizeof(*get) +
2761 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2762 if (*len != size) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002763 pr_err("length: %u != %u\n", *len, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 ret = -EINVAL;
2765 goto out;
2766 }
Hans Schillstromfc723252011-01-03 14:44:43 +01002767 ret = __ip_vs_get_dest_entries(net, get, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
2769 break;
2770
2771 case IP_VS_SO_GET_TIMEOUT:
2772 {
2773 struct ip_vs_timeout_user t;
2774
Hans Schillstrom93304192011-01-03 14:44:51 +01002775 __ip_vs_get_timeouts(net, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (copy_to_user(user, &t, sizeof(t)) != 0)
2777 ret = -EFAULT;
2778 }
2779 break;
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 default:
2782 ret = -EINVAL;
2783 }
2784
Hans Schillstrom552ad652011-06-13 12:19:26 +02002785out:
Ingo Molnar14cc3e22006-03-26 01:37:14 -08002786 mutex_unlock(&__ip_vs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return ret;
2788}
2789
2790
2791static struct nf_sockopt_ops ip_vs_sockopts = {
2792 .pf = PF_INET,
2793 .set_optmin = IP_VS_BASE_CTL,
2794 .set_optmax = IP_VS_SO_SET_MAX+1,
2795 .set = do_ip_vs_set_ctl,
2796 .get_optmin = IP_VS_BASE_CTL,
2797 .get_optmax = IP_VS_SO_GET_MAX+1,
2798 .get = do_ip_vs_get_ctl,
Neil Horman16fcec32007-09-11 11:28:26 +02002799 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800};
2801
Julius Volz9a812192008-08-14 14:08:44 +02002802/*
2803 * Generic Netlink interface
2804 */
2805
2806/* IPVS genetlink family */
2807static struct genl_family ip_vs_genl_family = {
2808 .id = GENL_ID_GENERATE,
2809 .hdrsize = 0,
2810 .name = IPVS_GENL_NAME,
2811 .version = IPVS_GENL_VERSION,
2812 .maxattr = IPVS_CMD_MAX,
Hans Schillstromc6d2d442011-01-03 14:45:03 +01002813 .netnsok = true, /* Make ipvsadm to work on netns */
Julius Volz9a812192008-08-14 14:08:44 +02002814};
2815
2816/* Policy used for first-level command attributes */
2817static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2818 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2819 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2820 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2821 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2822 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2823 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2824};
2825
2826/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2827static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2828 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2829 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2830 .len = IP_VS_IFNAME_MAXLEN },
2831 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2832};
2833
2834/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2835static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2836 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2837 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2838 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2839 .len = sizeof(union nf_inet_addr) },
2840 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2841 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2842 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2843 .len = IP_VS_SCHEDNAME_MAXLEN },
Simon Horman0d1e71b2010-08-22 21:37:54 +09002844 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2845 .len = IP_VS_PENAME_MAXLEN },
Julius Volz9a812192008-08-14 14:08:44 +02002846 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2847 .len = sizeof(struct ip_vs_flags) },
2848 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2849 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2850 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2851};
2852
2853/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2854static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2855 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2856 .len = sizeof(union nf_inet_addr) },
2857 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2858 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2859 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2860 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2861 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2862 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2863 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2864 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2865 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2866};
2867
2868static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2869 struct ip_vs_stats *stats)
2870{
Julian Anastasov55a3d4e2011-03-14 01:37:49 +02002871 struct ip_vs_stats_user ustats;
Julius Volz9a812192008-08-14 14:08:44 +02002872 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2873 if (!nl_stats)
2874 return -EMSGSIZE;
2875
Julian Anastasov55a3d4e2011-03-14 01:37:49 +02002876 ip_vs_copy_stats(&ustats, stats);
Julius Volz9a812192008-08-14 14:08:44 +02002877
David S. Miller969e8e22012-04-01 19:03:15 -04002878 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, ustats.conns) ||
2879 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, ustats.inpkts) ||
2880 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, ustats.outpkts) ||
2881 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, ustats.inbytes) ||
2882 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, ustats.outbytes) ||
2883 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, ustats.cps) ||
2884 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, ustats.inpps) ||
2885 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, ustats.outpps) ||
2886 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, ustats.inbps) ||
2887 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, ustats.outbps))
2888 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02002889 nla_nest_end(skb, nl_stats);
2890
2891 return 0;
2892
2893nla_put_failure:
Julius Volz9a812192008-08-14 14:08:44 +02002894 nla_nest_cancel(skb, nl_stats);
2895 return -EMSGSIZE;
2896}
2897
2898static int ip_vs_genl_fill_service(struct sk_buff *skb,
2899 struct ip_vs_service *svc)
2900{
Julian Anastasovceec4c32013-03-22 11:46:53 +02002901 struct ip_vs_scheduler *sched;
Julian Anastasov371990e2013-04-17 23:50:50 +03002902 struct ip_vs_pe *pe;
Julius Volz9a812192008-08-14 14:08:44 +02002903 struct nlattr *nl_service;
2904 struct ip_vs_flags flags = { .flags = svc->flags,
2905 .mask = ~0 };
2906
2907 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2908 if (!nl_service)
2909 return -EMSGSIZE;
2910
David S. Miller969e8e22012-04-01 19:03:15 -04002911 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
2912 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02002913 if (svc->fwmark) {
David S. Miller969e8e22012-04-01 19:03:15 -04002914 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
2915 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02002916 } else {
David S. Miller969e8e22012-04-01 19:03:15 -04002917 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
2918 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
Julian Anastasov0a925862013-04-17 23:50:49 +03002919 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
David S. Miller969e8e22012-04-01 19:03:15 -04002920 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02002921 }
2922
Julian Anastasovceec4c32013-03-22 11:46:53 +02002923 sched = rcu_dereference_protected(svc->scheduler, 1);
Julian Anastasov371990e2013-04-17 23:50:50 +03002924 pe = rcu_dereference_protected(svc->pe, 1);
Julian Anastasovceec4c32013-03-22 11:46:53 +02002925 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched->name) ||
Julian Anastasov371990e2013-04-17 23:50:50 +03002926 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
David S. Miller969e8e22012-04-01 19:03:15 -04002927 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
2928 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
Julian Anastasov0a925862013-04-17 23:50:49 +03002929 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
David S. Miller969e8e22012-04-01 19:03:15 -04002930 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02002931 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2932 goto nla_put_failure;
2933
2934 nla_nest_end(skb, nl_service);
2935
2936 return 0;
2937
2938nla_put_failure:
2939 nla_nest_cancel(skb, nl_service);
2940 return -EMSGSIZE;
2941}
2942
2943static int ip_vs_genl_dump_service(struct sk_buff *skb,
2944 struct ip_vs_service *svc,
2945 struct netlink_callback *cb)
2946{
2947 void *hdr;
2948
Eric W. Biederman15e47302012-09-07 20:12:54 +00002949 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julius Volz9a812192008-08-14 14:08:44 +02002950 &ip_vs_genl_family, NLM_F_MULTI,
2951 IPVS_CMD_NEW_SERVICE);
2952 if (!hdr)
2953 return -EMSGSIZE;
2954
2955 if (ip_vs_genl_fill_service(skb, svc) < 0)
2956 goto nla_put_failure;
2957
2958 return genlmsg_end(skb, hdr);
2959
2960nla_put_failure:
2961 genlmsg_cancel(skb, hdr);
2962 return -EMSGSIZE;
2963}
2964
2965static int ip_vs_genl_dump_services(struct sk_buff *skb,
2966 struct netlink_callback *cb)
2967{
2968 int idx = 0, i;
2969 int start = cb->args[0];
2970 struct ip_vs_service *svc;
Hans Schillstromfc723252011-01-03 14:44:43 +01002971 struct net *net = skb_sknet(skb);
Julius Volz9a812192008-08-14 14:08:44 +02002972
2973 mutex_lock(&__ip_vs_mutex);
2974 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02002975 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
Hans Schillstromfc723252011-01-03 14:44:43 +01002976 if (++idx <= start || !net_eq(svc->net, net))
Julius Volz9a812192008-08-14 14:08:44 +02002977 continue;
2978 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2979 idx--;
2980 goto nla_put_failure;
2981 }
2982 }
2983 }
2984
2985 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02002986 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
Hans Schillstromfc723252011-01-03 14:44:43 +01002987 if (++idx <= start || !net_eq(svc->net, net))
Julius Volz9a812192008-08-14 14:08:44 +02002988 continue;
2989 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2990 idx--;
2991 goto nla_put_failure;
2992 }
2993 }
2994 }
2995
2996nla_put_failure:
2997 mutex_unlock(&__ip_vs_mutex);
2998 cb->args[0] = idx;
2999
3000 return skb->len;
3001}
3002
Hans Schillstromfc723252011-01-03 14:44:43 +01003003static int ip_vs_genl_parse_service(struct net *net,
3004 struct ip_vs_service_user_kern *usvc,
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003005 struct nlattr *nla, int full_entry,
3006 struct ip_vs_service **ret_svc)
Julius Volz9a812192008-08-14 14:08:44 +02003007{
3008 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3009 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003010 struct ip_vs_service *svc;
Julius Volz9a812192008-08-14 14:08:44 +02003011
3012 /* Parse mandatory identifying service fields first */
3013 if (nla == NULL ||
3014 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
3015 return -EINVAL;
3016
3017 nla_af = attrs[IPVS_SVC_ATTR_AF];
3018 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3019 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3020 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3021 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3022
3023 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3024 return -EINVAL;
3025
Simon Horman258c8892009-12-15 17:01:25 +01003026 memset(usvc, 0, sizeof(*usvc));
3027
Julius Volzc860c6b2008-09-02 15:55:36 +02003028 usvc->af = nla_get_u16(nla_af);
Julius Volzf94fd042008-09-02 15:55:55 +02003029#ifdef CONFIG_IP_VS_IPV6
3030 if (usvc->af != AF_INET && usvc->af != AF_INET6)
3031#else
3032 if (usvc->af != AF_INET)
3033#endif
Julius Volz9a812192008-08-14 14:08:44 +02003034 return -EAFNOSUPPORT;
3035
3036 if (nla_fwmark) {
3037 usvc->protocol = IPPROTO_TCP;
3038 usvc->fwmark = nla_get_u32(nla_fwmark);
3039 } else {
3040 usvc->protocol = nla_get_u16(nla_protocol);
3041 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
Julian Anastasov0a925862013-04-17 23:50:49 +03003042 usvc->port = nla_get_be16(nla_port);
Julius Volz9a812192008-08-14 14:08:44 +02003043 usvc->fwmark = 0;
3044 }
3045
Julian Anastasovceec4c32013-03-22 11:46:53 +02003046 rcu_read_lock();
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003047 if (usvc->fwmark)
Hans Schillstromfc723252011-01-03 14:44:43 +01003048 svc = __ip_vs_svc_fwm_find(net, usvc->af, usvc->fwmark);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003049 else
Hans Schillstromfc723252011-01-03 14:44:43 +01003050 svc = __ip_vs_service_find(net, usvc->af, usvc->protocol,
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003051 &usvc->addr, usvc->port);
Julian Anastasovceec4c32013-03-22 11:46:53 +02003052 rcu_read_unlock();
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003053 *ret_svc = svc;
3054
Julius Volz9a812192008-08-14 14:08:44 +02003055 /* If a full entry was requested, check for the additional fields */
3056 if (full_entry) {
Simon Horman0d1e71b2010-08-22 21:37:54 +09003057 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
Julius Volz9a812192008-08-14 14:08:44 +02003058 *nla_netmask;
3059 struct ip_vs_flags flags;
Julius Volz9a812192008-08-14 14:08:44 +02003060
3061 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
Simon Horman0d1e71b2010-08-22 21:37:54 +09003062 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
Julius Volz9a812192008-08-14 14:08:44 +02003063 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3064 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3065 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3066
3067 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3068 return -EINVAL;
3069
3070 nla_memcpy(&flags, nla_flags, sizeof(flags));
3071
3072 /* prefill flags from service if it already exists */
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003073 if (svc)
Julius Volz9a812192008-08-14 14:08:44 +02003074 usvc->flags = svc->flags;
Julius Volz9a812192008-08-14 14:08:44 +02003075
3076 /* set new flags from userland */
3077 usvc->flags = (usvc->flags & ~flags.mask) |
3078 (flags.flags & flags.mask);
Julius Volzc860c6b2008-09-02 15:55:36 +02003079 usvc->sched_name = nla_data(nla_sched);
Simon Horman0d1e71b2010-08-22 21:37:54 +09003080 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
Julius Volz9a812192008-08-14 14:08:44 +02003081 usvc->timeout = nla_get_u32(nla_timeout);
Julian Anastasov0a925862013-04-17 23:50:49 +03003082 usvc->netmask = nla_get_be32(nla_netmask);
Julius Volz9a812192008-08-14 14:08:44 +02003083 }
3084
3085 return 0;
3086}
3087
Hans Schillstromfc723252011-01-03 14:44:43 +01003088static struct ip_vs_service *ip_vs_genl_find_service(struct net *net,
3089 struct nlattr *nla)
Julius Volz9a812192008-08-14 14:08:44 +02003090{
Julius Volzc860c6b2008-09-02 15:55:36 +02003091 struct ip_vs_service_user_kern usvc;
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003092 struct ip_vs_service *svc;
Julius Volz9a812192008-08-14 14:08:44 +02003093 int ret;
3094
Hans Schillstromfc723252011-01-03 14:44:43 +01003095 ret = ip_vs_genl_parse_service(net, &usvc, nla, 0, &svc);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003096 return ret ? ERR_PTR(ret) : svc;
Julius Volz9a812192008-08-14 14:08:44 +02003097}
3098
3099static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3100{
3101 struct nlattr *nl_dest;
3102
3103 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3104 if (!nl_dest)
3105 return -EMSGSIZE;
3106
David S. Miller969e8e22012-04-01 19:03:15 -04003107 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
Julian Anastasov0a925862013-04-17 23:50:49 +03003108 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
David S. Miller969e8e22012-04-01 19:03:15 -04003109 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3110 (atomic_read(&dest->conn_flags) &
3111 IP_VS_CONN_F_FWD_MASK)) ||
3112 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3113 atomic_read(&dest->weight)) ||
3114 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3115 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3116 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3117 atomic_read(&dest->activeconns)) ||
3118 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3119 atomic_read(&dest->inactconns)) ||
3120 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3121 atomic_read(&dest->persistconns)))
3122 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003123 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
3124 goto nla_put_failure;
3125
3126 nla_nest_end(skb, nl_dest);
3127
3128 return 0;
3129
3130nla_put_failure:
3131 nla_nest_cancel(skb, nl_dest);
3132 return -EMSGSIZE;
3133}
3134
3135static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3136 struct netlink_callback *cb)
3137{
3138 void *hdr;
3139
Eric W. Biederman15e47302012-09-07 20:12:54 +00003140 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julius Volz9a812192008-08-14 14:08:44 +02003141 &ip_vs_genl_family, NLM_F_MULTI,
3142 IPVS_CMD_NEW_DEST);
3143 if (!hdr)
3144 return -EMSGSIZE;
3145
3146 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3147 goto nla_put_failure;
3148
3149 return genlmsg_end(skb, hdr);
3150
3151nla_put_failure:
3152 genlmsg_cancel(skb, hdr);
3153 return -EMSGSIZE;
3154}
3155
3156static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3157 struct netlink_callback *cb)
3158{
3159 int idx = 0;
3160 int start = cb->args[0];
3161 struct ip_vs_service *svc;
3162 struct ip_vs_dest *dest;
3163 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
Hans Schillstroma0840e22011-01-03 14:44:58 +01003164 struct net *net = skb_sknet(skb);
Julius Volz9a812192008-08-14 14:08:44 +02003165
3166 mutex_lock(&__ip_vs_mutex);
3167
3168 /* Try to find the service for which to dump destinations */
3169 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
3170 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
3171 goto out_err;
3172
Hans Schillstroma0840e22011-01-03 14:44:58 +01003173
Hans Schillstromfc723252011-01-03 14:44:43 +01003174 svc = ip_vs_genl_find_service(net, attrs[IPVS_CMD_ATTR_SERVICE]);
Julius Volz9a812192008-08-14 14:08:44 +02003175 if (IS_ERR(svc) || svc == NULL)
3176 goto out_err;
3177
3178 /* Dump the destinations */
3179 list_for_each_entry(dest, &svc->destinations, n_list) {
3180 if (++idx <= start)
3181 continue;
3182 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3183 idx--;
3184 goto nla_put_failure;
3185 }
3186 }
3187
3188nla_put_failure:
3189 cb->args[0] = idx;
Julius Volz9a812192008-08-14 14:08:44 +02003190
3191out_err:
3192 mutex_unlock(&__ip_vs_mutex);
3193
3194 return skb->len;
3195}
3196
Julius Volzc860c6b2008-09-02 15:55:36 +02003197static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
Julius Volz9a812192008-08-14 14:08:44 +02003198 struct nlattr *nla, int full_entry)
3199{
3200 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3201 struct nlattr *nla_addr, *nla_port;
3202
3203 /* Parse mandatory identifying destination fields first */
3204 if (nla == NULL ||
3205 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
3206 return -EINVAL;
3207
3208 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3209 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3210
3211 if (!(nla_addr && nla_port))
3212 return -EINVAL;
3213
Simon Horman258c8892009-12-15 17:01:25 +01003214 memset(udest, 0, sizeof(*udest));
3215
Julius Volz9a812192008-08-14 14:08:44 +02003216 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
Julian Anastasov0a925862013-04-17 23:50:49 +03003217 udest->port = nla_get_be16(nla_port);
Julius Volz9a812192008-08-14 14:08:44 +02003218
3219 /* If a full entry was requested, check for the additional fields */
3220 if (full_entry) {
3221 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3222 *nla_l_thresh;
3223
3224 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3225 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3226 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3227 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3228
3229 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3230 return -EINVAL;
3231
3232 udest->conn_flags = nla_get_u32(nla_fwd)
3233 & IP_VS_CONN_F_FWD_MASK;
3234 udest->weight = nla_get_u32(nla_weight);
3235 udest->u_threshold = nla_get_u32(nla_u_thresh);
3236 udest->l_threshold = nla_get_u32(nla_l_thresh);
3237 }
3238
3239 return 0;
3240}
3241
Julian Anastasov0a925862013-04-17 23:50:49 +03003242static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3243 const char *mcast_ifn, __u32 syncid)
Julius Volz9a812192008-08-14 14:08:44 +02003244{
3245 struct nlattr *nl_daemon;
3246
3247 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3248 if (!nl_daemon)
3249 return -EMSGSIZE;
3250
David S. Miller969e8e22012-04-01 19:03:15 -04003251 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3252 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn) ||
3253 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid))
3254 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003255 nla_nest_end(skb, nl_daemon);
3256
3257 return 0;
3258
3259nla_put_failure:
3260 nla_nest_cancel(skb, nl_daemon);
3261 return -EMSGSIZE;
3262}
3263
Julian Anastasov0a925862013-04-17 23:50:49 +03003264static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3265 const char *mcast_ifn, __u32 syncid,
Julius Volz9a812192008-08-14 14:08:44 +02003266 struct netlink_callback *cb)
3267{
3268 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003269 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julius Volz9a812192008-08-14 14:08:44 +02003270 &ip_vs_genl_family, NLM_F_MULTI,
3271 IPVS_CMD_NEW_DAEMON);
3272 if (!hdr)
3273 return -EMSGSIZE;
3274
3275 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
3276 goto nla_put_failure;
3277
3278 return genlmsg_end(skb, hdr);
3279
3280nla_put_failure:
3281 genlmsg_cancel(skb, hdr);
3282 return -EMSGSIZE;
3283}
3284
3285static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3286 struct netlink_callback *cb)
3287{
Hans Schillstroma09d1972011-04-04 15:25:18 +02003288 struct net *net = skb_sknet(skb);
Hans Schillstromf1313152011-01-03 14:44:55 +01003289 struct netns_ipvs *ipvs = net_ipvs(net);
3290
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003291 mutex_lock(&ipvs->sync_mutex);
Hans Schillstromf1313152011-01-03 14:44:55 +01003292 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
Julius Volz9a812192008-08-14 14:08:44 +02003293 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
Hans Schillstromf1313152011-01-03 14:44:55 +01003294 ipvs->master_mcast_ifn,
3295 ipvs->master_syncid, cb) < 0)
Julius Volz9a812192008-08-14 14:08:44 +02003296 goto nla_put_failure;
3297
3298 cb->args[0] = 1;
3299 }
3300
Hans Schillstromf1313152011-01-03 14:44:55 +01003301 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
Julius Volz9a812192008-08-14 14:08:44 +02003302 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
Hans Schillstromf1313152011-01-03 14:44:55 +01003303 ipvs->backup_mcast_ifn,
3304 ipvs->backup_syncid, cb) < 0)
Julius Volz9a812192008-08-14 14:08:44 +02003305 goto nla_put_failure;
3306
3307 cb->args[1] = 1;
3308 }
3309
3310nla_put_failure:
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003311 mutex_unlock(&ipvs->sync_mutex);
Julius Volz9a812192008-08-14 14:08:44 +02003312
3313 return skb->len;
3314}
3315
Hans Schillstromf1313152011-01-03 14:44:55 +01003316static int ip_vs_genl_new_daemon(struct net *net, struct nlattr **attrs)
Julius Volz9a812192008-08-14 14:08:44 +02003317{
3318 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3319 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3320 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3321 return -EINVAL;
3322
Hans Schillstromf1313152011-01-03 14:44:55 +01003323 return start_sync_thread(net,
3324 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
Julius Volz9a812192008-08-14 14:08:44 +02003325 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3326 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3327}
3328
Hans Schillstromf1313152011-01-03 14:44:55 +01003329static int ip_vs_genl_del_daemon(struct net *net, struct nlattr **attrs)
Julius Volz9a812192008-08-14 14:08:44 +02003330{
3331 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3332 return -EINVAL;
3333
Hans Schillstromf1313152011-01-03 14:44:55 +01003334 return stop_sync_thread(net,
3335 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
Julius Volz9a812192008-08-14 14:08:44 +02003336}
3337
Hans Schillstrom93304192011-01-03 14:44:51 +01003338static int ip_vs_genl_set_config(struct net *net, struct nlattr **attrs)
Julius Volz9a812192008-08-14 14:08:44 +02003339{
3340 struct ip_vs_timeout_user t;
3341
Hans Schillstrom93304192011-01-03 14:44:51 +01003342 __ip_vs_get_timeouts(net, &t);
Julius Volz9a812192008-08-14 14:08:44 +02003343
3344 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3345 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3346
3347 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3348 t.tcp_fin_timeout =
3349 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3350
3351 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3352 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3353
Hans Schillstrom93304192011-01-03 14:44:51 +01003354 return ip_vs_set_timeout(net, &t);
Julius Volz9a812192008-08-14 14:08:44 +02003355}
3356
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003357static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3358{
3359 int ret = 0, cmd;
3360 struct net *net;
3361 struct netns_ipvs *ipvs;
3362
3363 net = skb_sknet(skb);
3364 ipvs = net_ipvs(net);
3365 cmd = info->genlhdr->cmd;
3366
3367 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3368 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3369
3370 mutex_lock(&ipvs->sync_mutex);
3371 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3372 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3373 info->attrs[IPVS_CMD_ATTR_DAEMON],
3374 ip_vs_daemon_policy)) {
3375 ret = -EINVAL;
3376 goto out;
3377 }
3378
3379 if (cmd == IPVS_CMD_NEW_DAEMON)
3380 ret = ip_vs_genl_new_daemon(net, daemon_attrs);
3381 else
3382 ret = ip_vs_genl_del_daemon(net, daemon_attrs);
3383out:
3384 mutex_unlock(&ipvs->sync_mutex);
3385 }
3386 return ret;
3387}
3388
Julius Volz9a812192008-08-14 14:08:44 +02003389static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3390{
3391 struct ip_vs_service *svc = NULL;
Julius Volzc860c6b2008-09-02 15:55:36 +02003392 struct ip_vs_service_user_kern usvc;
3393 struct ip_vs_dest_user_kern udest;
Julius Volz9a812192008-08-14 14:08:44 +02003394 int ret = 0, cmd;
3395 int need_full_svc = 0, need_full_dest = 0;
Hans Schillstromfc723252011-01-03 14:44:43 +01003396 struct net *net;
Julius Volz9a812192008-08-14 14:08:44 +02003397
Hans Schillstromfc723252011-01-03 14:44:43 +01003398 net = skb_sknet(skb);
Julius Volz9a812192008-08-14 14:08:44 +02003399 cmd = info->genlhdr->cmd;
3400
3401 mutex_lock(&__ip_vs_mutex);
3402
3403 if (cmd == IPVS_CMD_FLUSH) {
Julian Anastasov578bc3e2013-03-22 11:46:49 +02003404 ret = ip_vs_flush(net, false);
Julius Volz9a812192008-08-14 14:08:44 +02003405 goto out;
3406 } else if (cmd == IPVS_CMD_SET_CONFIG) {
Hans Schillstrom93304192011-01-03 14:44:51 +01003407 ret = ip_vs_genl_set_config(net, info->attrs);
Julius Volz9a812192008-08-14 14:08:44 +02003408 goto out;
Julius Volz9a812192008-08-14 14:08:44 +02003409 } else if (cmd == IPVS_CMD_ZERO &&
3410 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
Hans Schillstromfc723252011-01-03 14:44:43 +01003411 ret = ip_vs_zero_all(net);
Julius Volz9a812192008-08-14 14:08:44 +02003412 goto out;
3413 }
3414
3415 /* All following commands require a service argument, so check if we
3416 * received a valid one. We need a full service specification when
3417 * adding / editing a service. Only identifying members otherwise. */
3418 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3419 need_full_svc = 1;
3420
Hans Schillstromfc723252011-01-03 14:44:43 +01003421 ret = ip_vs_genl_parse_service(net, &usvc,
Julius Volz9a812192008-08-14 14:08:44 +02003422 info->attrs[IPVS_CMD_ATTR_SERVICE],
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003423 need_full_svc, &svc);
Julius Volz9a812192008-08-14 14:08:44 +02003424 if (ret)
3425 goto out;
3426
Julius Volz9a812192008-08-14 14:08:44 +02003427 /* Unless we're adding a new service, the service must already exist */
3428 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3429 ret = -ESRCH;
3430 goto out;
3431 }
3432
3433 /* Destination commands require a valid destination argument. For
3434 * adding / editing a destination, we need a full destination
3435 * specification. */
3436 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3437 cmd == IPVS_CMD_DEL_DEST) {
3438 if (cmd != IPVS_CMD_DEL_DEST)
3439 need_full_dest = 1;
3440
3441 ret = ip_vs_genl_parse_dest(&udest,
3442 info->attrs[IPVS_CMD_ATTR_DEST],
3443 need_full_dest);
3444 if (ret)
3445 goto out;
3446 }
3447
3448 switch (cmd) {
3449 case IPVS_CMD_NEW_SERVICE:
3450 if (svc == NULL)
Hans Schillstromfc723252011-01-03 14:44:43 +01003451 ret = ip_vs_add_service(net, &usvc, &svc);
Julius Volz9a812192008-08-14 14:08:44 +02003452 else
3453 ret = -EEXIST;
3454 break;
3455 case IPVS_CMD_SET_SERVICE:
3456 ret = ip_vs_edit_service(svc, &usvc);
3457 break;
3458 case IPVS_CMD_DEL_SERVICE:
3459 ret = ip_vs_del_service(svc);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003460 /* do not use svc, it can be freed */
Julius Volz9a812192008-08-14 14:08:44 +02003461 break;
3462 case IPVS_CMD_NEW_DEST:
3463 ret = ip_vs_add_dest(svc, &udest);
3464 break;
3465 case IPVS_CMD_SET_DEST:
3466 ret = ip_vs_edit_dest(svc, &udest);
3467 break;
3468 case IPVS_CMD_DEL_DEST:
3469 ret = ip_vs_del_dest(svc, &udest);
3470 break;
3471 case IPVS_CMD_ZERO:
3472 ret = ip_vs_zero_service(svc);
3473 break;
3474 default:
3475 ret = -EINVAL;
3476 }
3477
3478out:
Julius Volz9a812192008-08-14 14:08:44 +02003479 mutex_unlock(&__ip_vs_mutex);
3480
3481 return ret;
3482}
3483
3484static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3485{
3486 struct sk_buff *msg;
3487 void *reply;
3488 int ret, cmd, reply_cmd;
Hans Schillstromfc723252011-01-03 14:44:43 +01003489 struct net *net;
Julius Volz9a812192008-08-14 14:08:44 +02003490
Hans Schillstromfc723252011-01-03 14:44:43 +01003491 net = skb_sknet(skb);
Julius Volz9a812192008-08-14 14:08:44 +02003492 cmd = info->genlhdr->cmd;
3493
3494 if (cmd == IPVS_CMD_GET_SERVICE)
3495 reply_cmd = IPVS_CMD_NEW_SERVICE;
3496 else if (cmd == IPVS_CMD_GET_INFO)
3497 reply_cmd = IPVS_CMD_SET_INFO;
3498 else if (cmd == IPVS_CMD_GET_CONFIG)
3499 reply_cmd = IPVS_CMD_SET_CONFIG;
3500 else {
Hannes Eder1e3e2382009-08-02 11:05:41 +00003501 pr_err("unknown Generic Netlink command\n");
Julius Volz9a812192008-08-14 14:08:44 +02003502 return -EINVAL;
3503 }
3504
3505 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3506 if (!msg)
3507 return -ENOMEM;
3508
3509 mutex_lock(&__ip_vs_mutex);
3510
3511 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3512 if (reply == NULL)
3513 goto nla_put_failure;
3514
3515 switch (cmd) {
3516 case IPVS_CMD_GET_SERVICE:
3517 {
3518 struct ip_vs_service *svc;
3519
Hans Schillstromfc723252011-01-03 14:44:43 +01003520 svc = ip_vs_genl_find_service(net,
3521 info->attrs[IPVS_CMD_ATTR_SERVICE]);
Julius Volz9a812192008-08-14 14:08:44 +02003522 if (IS_ERR(svc)) {
3523 ret = PTR_ERR(svc);
3524 goto out_err;
3525 } else if (svc) {
3526 ret = ip_vs_genl_fill_service(msg, svc);
Julius Volz9a812192008-08-14 14:08:44 +02003527 if (ret)
3528 goto nla_put_failure;
3529 } else {
3530 ret = -ESRCH;
3531 goto out_err;
3532 }
3533
3534 break;
3535 }
3536
3537 case IPVS_CMD_GET_CONFIG:
3538 {
3539 struct ip_vs_timeout_user t;
3540
Hans Schillstrom93304192011-01-03 14:44:51 +01003541 __ip_vs_get_timeouts(net, &t);
Julius Volz9a812192008-08-14 14:08:44 +02003542#ifdef CONFIG_IP_VS_PROTO_TCP
David S. Miller969e8e22012-04-01 19:03:15 -04003543 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3544 t.tcp_timeout) ||
3545 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3546 t.tcp_fin_timeout))
3547 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003548#endif
3549#ifdef CONFIG_IP_VS_PROTO_UDP
David S. Miller969e8e22012-04-01 19:03:15 -04003550 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3551 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003552#endif
3553
3554 break;
3555 }
3556
3557 case IPVS_CMD_GET_INFO:
David S. Miller969e8e22012-04-01 19:03:15 -04003558 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3559 IP_VS_VERSION_CODE) ||
3560 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3561 ip_vs_conn_tab_size))
3562 goto nla_put_failure;
Julius Volz9a812192008-08-14 14:08:44 +02003563 break;
3564 }
3565
3566 genlmsg_end(msg, reply);
Johannes Berg134e6372009-07-10 09:51:34 +00003567 ret = genlmsg_reply(msg, info);
Julius Volz9a812192008-08-14 14:08:44 +02003568 goto out;
3569
3570nla_put_failure:
Hannes Eder1e3e2382009-08-02 11:05:41 +00003571 pr_err("not enough space in Netlink message\n");
Julius Volz9a812192008-08-14 14:08:44 +02003572 ret = -EMSGSIZE;
3573
3574out_err:
3575 nlmsg_free(msg);
3576out:
3577 mutex_unlock(&__ip_vs_mutex);
3578
3579 return ret;
3580}
3581
3582
Johannes Berg4534de82013-11-14 17:14:46 +01003583static const struct genl_ops ip_vs_genl_ops[] __read_mostly = {
Julius Volz9a812192008-08-14 14:08:44 +02003584 {
3585 .cmd = IPVS_CMD_NEW_SERVICE,
3586 .flags = GENL_ADMIN_PERM,
3587 .policy = ip_vs_cmd_policy,
3588 .doit = ip_vs_genl_set_cmd,
3589 },
3590 {
3591 .cmd = IPVS_CMD_SET_SERVICE,
3592 .flags = GENL_ADMIN_PERM,
3593 .policy = ip_vs_cmd_policy,
3594 .doit = ip_vs_genl_set_cmd,
3595 },
3596 {
3597 .cmd = IPVS_CMD_DEL_SERVICE,
3598 .flags = GENL_ADMIN_PERM,
3599 .policy = ip_vs_cmd_policy,
3600 .doit = ip_vs_genl_set_cmd,
3601 },
3602 {
3603 .cmd = IPVS_CMD_GET_SERVICE,
3604 .flags = GENL_ADMIN_PERM,
3605 .doit = ip_vs_genl_get_cmd,
3606 .dumpit = ip_vs_genl_dump_services,
3607 .policy = ip_vs_cmd_policy,
3608 },
3609 {
3610 .cmd = IPVS_CMD_NEW_DEST,
3611 .flags = GENL_ADMIN_PERM,
3612 .policy = ip_vs_cmd_policy,
3613 .doit = ip_vs_genl_set_cmd,
3614 },
3615 {
3616 .cmd = IPVS_CMD_SET_DEST,
3617 .flags = GENL_ADMIN_PERM,
3618 .policy = ip_vs_cmd_policy,
3619 .doit = ip_vs_genl_set_cmd,
3620 },
3621 {
3622 .cmd = IPVS_CMD_DEL_DEST,
3623 .flags = GENL_ADMIN_PERM,
3624 .policy = ip_vs_cmd_policy,
3625 .doit = ip_vs_genl_set_cmd,
3626 },
3627 {
3628 .cmd = IPVS_CMD_GET_DEST,
3629 .flags = GENL_ADMIN_PERM,
3630 .policy = ip_vs_cmd_policy,
3631 .dumpit = ip_vs_genl_dump_dests,
3632 },
3633 {
3634 .cmd = IPVS_CMD_NEW_DAEMON,
3635 .flags = GENL_ADMIN_PERM,
3636 .policy = ip_vs_cmd_policy,
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003637 .doit = ip_vs_genl_set_daemon,
Julius Volz9a812192008-08-14 14:08:44 +02003638 },
3639 {
3640 .cmd = IPVS_CMD_DEL_DAEMON,
3641 .flags = GENL_ADMIN_PERM,
3642 .policy = ip_vs_cmd_policy,
Hans Schillstromae1d48b2011-10-11 10:54:35 +09003643 .doit = ip_vs_genl_set_daemon,
Julius Volz9a812192008-08-14 14:08:44 +02003644 },
3645 {
3646 .cmd = IPVS_CMD_GET_DAEMON,
3647 .flags = GENL_ADMIN_PERM,
3648 .dumpit = ip_vs_genl_dump_daemons,
3649 },
3650 {
3651 .cmd = IPVS_CMD_SET_CONFIG,
3652 .flags = GENL_ADMIN_PERM,
3653 .policy = ip_vs_cmd_policy,
3654 .doit = ip_vs_genl_set_cmd,
3655 },
3656 {
3657 .cmd = IPVS_CMD_GET_CONFIG,
3658 .flags = GENL_ADMIN_PERM,
3659 .doit = ip_vs_genl_get_cmd,
3660 },
3661 {
3662 .cmd = IPVS_CMD_GET_INFO,
3663 .flags = GENL_ADMIN_PERM,
3664 .doit = ip_vs_genl_get_cmd,
3665 },
3666 {
3667 .cmd = IPVS_CMD_ZERO,
3668 .flags = GENL_ADMIN_PERM,
3669 .policy = ip_vs_cmd_policy,
3670 .doit = ip_vs_genl_set_cmd,
3671 },
3672 {
3673 .cmd = IPVS_CMD_FLUSH,
3674 .flags = GENL_ADMIN_PERM,
3675 .doit = ip_vs_genl_set_cmd,
3676 },
3677};
3678
3679static int __init ip_vs_genl_register(void)
3680{
Michał Mirosław8f698d52009-05-21 10:34:05 +00003681 return genl_register_family_with_ops(&ip_vs_genl_family,
Johannes Bergc53ed742013-11-19 15:19:31 +01003682 ip_vs_genl_ops);
Julius Volz9a812192008-08-14 14:08:44 +02003683}
3684
3685static void ip_vs_genl_unregister(void)
3686{
3687 genl_unregister_family(&ip_vs_genl_family);
3688}
3689
3690/* End of Generic Netlink interface definitions */
3691
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01003692/*
3693 * per netns intit/exit func.
3694 */
Simon Horman14e40542011-02-04 18:33:02 +09003695#ifdef CONFIG_SYSCTL
Claudiu Ghioc2b2d2802012-07-18 12:10:22 +03003696static int __net_init ip_vs_control_net_init_sysctl(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01003697{
Hans Schillstromfc723252011-01-03 14:44:43 +01003698 int idx;
3699 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstroma0840e22011-01-03 14:44:58 +01003700 struct ctl_table *tbl;
Hans Schillstromfc723252011-01-03 14:44:43 +01003701
Hans Schillstroma0840e22011-01-03 14:44:58 +01003702 atomic_set(&ipvs->dropentry, 0);
3703 spin_lock_init(&ipvs->dropentry_lock);
3704 spin_lock_init(&ipvs->droppacket_lock);
3705 spin_lock_init(&ipvs->securetcp_lock);
Hans Schillstroma0840e22011-01-03 14:44:58 +01003706
3707 if (!net_eq(net, &init_net)) {
3708 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3709 if (tbl == NULL)
Simon Horman14e40542011-02-04 18:33:02 +09003710 return -ENOMEM;
Eric W. Biederman464dc802012-11-16 03:02:59 +00003711
3712 /* Don't export sysctls to unprivileged users */
3713 if (net->user_ns != &init_user_ns)
3714 tbl[0].procname = NULL;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003715 } else
3716 tbl = vs_vars;
3717 /* Initialize sysctl defaults */
3718 idx = 0;
3719 ipvs->sysctl_amemthresh = 1024;
3720 tbl[idx++].data = &ipvs->sysctl_amemthresh;
3721 ipvs->sysctl_am_droprate = 10;
3722 tbl[idx++].data = &ipvs->sysctl_am_droprate;
3723 tbl[idx++].data = &ipvs->sysctl_drop_entry;
3724 tbl[idx++].data = &ipvs->sysctl_drop_packet;
3725#ifdef CONFIG_IP_VS_NFCT
3726 tbl[idx++].data = &ipvs->sysctl_conntrack;
3727#endif
3728 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3729 ipvs->sysctl_snat_reroute = 1;
3730 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3731 ipvs->sysctl_sync_ver = 1;
3732 tbl[idx++].data = &ipvs->sysctl_sync_ver;
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +02003733 ipvs->sysctl_sync_ports = 1;
3734 tbl[idx++].data = &ipvs->sysctl_sync_ports;
Julian Anastasov4d0c8752013-06-24 22:44:41 +03003735 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
Pablo Neira Ayuso1c003b12012-05-08 19:39:49 +02003736 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3737 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3738 ipvs->sysctl_sync_sock_size = 0;
3739 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003740 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3741 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
Alexander Frolkinc6c96c12013-06-13 08:56:15 +01003742 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
3743 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003744 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
Simon Horman59e03502011-02-04 18:33:01 +09003745 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3746 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003747 tbl[idx].data = &ipvs->sysctl_sync_threshold;
3748 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
Julian Anastasov749c42b2012-04-24 23:46:40 +03003749 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3750 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3751 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3752 tbl[idx++].data = &ipvs->sysctl_sync_retries;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003753 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
Julian Anastasov3654e612012-07-20 11:59:53 +03003754 ipvs->sysctl_pmtu_disc = 1;
3755 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
Julian Anastasov0c125822013-03-09 23:25:04 +02003756 tbl[idx++].data = &ipvs->sysctl_backup_only;
Hans Schillstroma0840e22011-01-03 14:44:58 +01003757
3758
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +00003759 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
Simon Horman04439292011-02-01 18:29:04 +01003760 if (ipvs->sysctl_hdr == NULL) {
3761 if (!net_eq(net, &init_net))
3762 kfree(tbl);
Simon Horman14e40542011-02-04 18:33:02 +09003763 return -ENOMEM;
Simon Horman04439292011-02-01 18:29:04 +01003764 }
Julian Anastasov6ef757f2011-03-14 01:44:28 +02003765 ip_vs_start_estimator(net, &ipvs->tot_stats);
Hans Schillstroma0840e22011-01-03 14:44:58 +01003766 ipvs->sysctl_tbl = tbl;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01003767 /* Schedule defense work */
3768 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
3769 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
Simon Horman14e40542011-02-04 18:33:02 +09003770
3771 return 0;
3772}
3773
Claudiu Ghioc2b2d2802012-07-18 12:10:22 +03003774static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net)
Simon Horman14e40542011-02-04 18:33:02 +09003775{
3776 struct netns_ipvs *ipvs = net_ipvs(net);
3777
3778 cancel_delayed_work_sync(&ipvs->defense_work);
3779 cancel_work_sync(&ipvs->defense_work.work);
3780 unregister_net_sysctl_table(ipvs->sysctl_hdr);
3781}
3782
3783#else
3784
Claudiu Ghioc2b2d2802012-07-18 12:10:22 +03003785static int __net_init ip_vs_control_net_init_sysctl(struct net *net) { return 0; }
3786static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net) { }
Simon Horman14e40542011-02-04 18:33:02 +09003787
3788#endif
3789
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02003790static struct notifier_block ip_vs_dst_notifier = {
3791 .notifier_call = ip_vs_dst_event,
3792};
3793
Hans Schillstrom503cf152011-05-01 18:50:16 +02003794int __net_init ip_vs_control_net_init(struct net *net)
Simon Horman14e40542011-02-04 18:33:02 +09003795{
John Stultz827da442013-10-07 15:51:58 -07003796 int i, idx;
Simon Horman14e40542011-02-04 18:33:02 +09003797 struct netns_ipvs *ipvs = net_ipvs(net);
3798
Simon Horman14e40542011-02-04 18:33:02 +09003799 /* Initialize rs_table */
3800 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
Julian Anastasov276472e2013-03-21 11:58:08 +02003801 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
Simon Horman14e40542011-02-04 18:33:02 +09003802
3803 INIT_LIST_HEAD(&ipvs->dest_trash);
Julian Anastasov578bc3e2013-03-22 11:46:49 +02003804 spin_lock_init(&ipvs->dest_trash_lock);
3805 setup_timer(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire,
3806 (unsigned long) net);
Simon Horman14e40542011-02-04 18:33:02 +09003807 atomic_set(&ipvs->ftpsvc_counter, 0);
3808 atomic_set(&ipvs->nullsvc_counter, 0);
3809
3810 /* procfs stats */
3811 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
Joe Perches0a9ee812011-08-29 14:17:25 -07003812 if (!ipvs->tot_stats.cpustats)
Simon Horman14e40542011-02-04 18:33:02 +09003813 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -07003814
John Stultz827da442013-10-07 15:51:58 -07003815 for_each_possible_cpu(i) {
3816 struct ip_vs_cpu_stats *ipvs_tot_stats;
3817 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
3818 u64_stats_init(&ipvs_tot_stats->syncp);
3819 }
3820
Simon Horman14e40542011-02-04 18:33:02 +09003821 spin_lock_init(&ipvs->tot_stats.lock);
3822
Gao fengd4beaa62013-02-18 01:34:54 +00003823 proc_create("ip_vs", 0, net->proc_net, &ip_vs_info_fops);
3824 proc_create("ip_vs_stats", 0, net->proc_net, &ip_vs_stats_fops);
3825 proc_create("ip_vs_stats_percpu", 0, net->proc_net,
3826 &ip_vs_stats_percpu_fops);
Simon Horman14e40542011-02-04 18:33:02 +09003827
Hans Schillstrom503cf152011-05-01 18:50:16 +02003828 if (ip_vs_control_net_init_sysctl(net))
Simon Horman14e40542011-02-04 18:33:02 +09003829 goto err;
3830
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01003831 return 0;
3832
Simon Horman14e40542011-02-04 18:33:02 +09003833err:
Julian Anastasov2a0751a2011-03-04 12:20:35 +02003834 free_percpu(ipvs->tot_stats.cpustats);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01003835 return -ENOMEM;
3836}
3837
Hans Schillstrom503cf152011-05-01 18:50:16 +02003838void __net_exit ip_vs_control_net_cleanup(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01003839{
Hans Schillstromb17fc992011-01-03 14:44:56 +01003840 struct netns_ipvs *ipvs = net_ipvs(net);
3841
Hans Schillstromf2431e62011-01-03 14:45:00 +01003842 ip_vs_trash_cleanup(net);
Julian Anastasov6ef757f2011-03-14 01:44:28 +02003843 ip_vs_stop_estimator(net, &ipvs->tot_stats);
Hans Schillstrom503cf152011-05-01 18:50:16 +02003844 ip_vs_control_net_cleanup_sysctl(net);
Gao fengece31ff2013-02-18 01:34:56 +00003845 remove_proc_entry("ip_vs_stats_percpu", net->proc_net);
3846 remove_proc_entry("ip_vs_stats", net->proc_net);
3847 remove_proc_entry("ip_vs", net->proc_net);
Julian Anastasov2a0751a2011-03-04 12:20:35 +02003848 free_percpu(ipvs->tot_stats.cpustats);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01003849}
3850
Hans Schillstrom8537de82012-04-26 07:47:44 +02003851int __init ip_vs_register_nl_ioctl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852{
Hans Schillstromfc723252011-01-03 14:44:43 +01003853 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 ret = nf_register_sockopt(&ip_vs_sockopts);
3856 if (ret) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00003857 pr_err("cannot register sockopt.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02003858 goto err_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 }
3860
Julius Volz9a812192008-08-14 14:08:44 +02003861 ret = ip_vs_genl_register();
3862 if (ret) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00003863 pr_err("cannot register Generic Netlink interface.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02003864 goto err_genl;
Julius Volz9a812192008-08-14 14:08:44 +02003865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return 0;
Hans Schillstromfc723252011-01-03 14:44:43 +01003867
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02003868err_genl:
3869 nf_unregister_sockopt(&ip_vs_sockopts);
3870err_sock:
Hans Schillstromfc723252011-01-03 14:44:43 +01003871 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872}
3873
Hans Schillstrom8537de82012-04-26 07:47:44 +02003874void ip_vs_unregister_nl_ioctl(void)
3875{
3876 ip_vs_genl_unregister();
3877 nf_unregister_sockopt(&ip_vs_sockopts);
3878}
3879
3880int __init ip_vs_control_init(void)
3881{
3882 int idx;
3883 int ret;
3884
3885 EnterFunction(2);
3886
Julian Anastasov276472e2013-03-21 11:58:08 +02003887 /* Initialize svc_table, ip_vs_svc_fwm_table */
Hans Schillstrom8537de82012-04-26 07:47:44 +02003888 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
Julian Anastasovceec4c32013-03-22 11:46:53 +02003889 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
3890 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
Hans Schillstrom8537de82012-04-26 07:47:44 +02003891 }
3892
3893 smp_wmb(); /* Do we really need it now ? */
3894
3895 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
3896 if (ret < 0)
3897 return ret;
3898
3899 LeaveFunction(2);
3900 return 0;
3901}
3902
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
3904void ip_vs_control_cleanup(void)
3905{
3906 EnterFunction(2);
Simon Horman7676e342011-05-19 21:32:57 +09003907 unregister_netdevice_notifier(&ip_vs_dst_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 LeaveFunction(2);
3909}