1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * IPVS An implementation of the IP virtual server support for the
4 * LINUX operating system. IPVS is now implemented as a module
5 * over the NetFilter framework. IPVS can be used to build a
6 * high-performance and highly available server based on a
7 * cluster of servers.
8 *
9 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
10 * Peter Kese <peter.kese@ijs.si>
11 * Julian Anastasov <ja@ssi.bg>
12 *
13 * Changes:
14 */
15
16 #define KMSG_COMPONENT "IPVS"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/capability.h>
23 #include <linux/fs.h>
24 #include <linux/sysctl.h>
25 #include <linux/proc_fs.h>
26 #include <linux/workqueue.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29
30 #include <linux/netfilter.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/mutex.h>
33
34 #include <net/net_namespace.h>
35 #include <linux/nsproxy.h>
36 #include <net/ip.h>
37 #ifdef CONFIG_IP_VS_IPV6
38 #include <net/ipv6.h>
39 #include <net/ip6_route.h>
40 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
41 #endif
42 #include <net/route.h>
43 #include <net/sock.h>
44 #include <net/genetlink.h>
45
46 #include <linux/uaccess.h>
47
48 #include <net/ip_vs.h>
49
50 MODULE_ALIAS_GENL_FAMILY(IPVS_GENL_NAME);
51
52 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
53 static DEFINE_MUTEX(__ip_vs_mutex);
54
55 /* sysctl variables */
56
57 #ifdef CONFIG_IP_VS_DEBUG
58 static int sysctl_ip_vs_debug_level = 0;
59
ip_vs_get_debug_level(void)60 int ip_vs_get_debug_level(void)
61 {
62 return sysctl_ip_vs_debug_level;
63 }
64 #endif
65
66
67 /* Protos */
68 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup);
69
70
71 #ifdef CONFIG_IP_VS_IPV6
72 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
__ip_vs_addr_is_local_v6(struct net * net,const struct in6_addr * addr)73 static bool __ip_vs_addr_is_local_v6(struct net *net,
74 const struct in6_addr *addr)
75 {
76 struct flowi6 fl6 = {
77 .daddr = *addr,
78 };
79 struct dst_entry *dst = ip6_route_output(net, NULL, &fl6);
80 bool is_local;
81
82 is_local = !dst->error && dst->dev && (dst->dev->flags & IFF_LOOPBACK);
83
84 dst_release(dst);
85 return is_local;
86 }
87 #endif
88
89 #ifdef CONFIG_SYSCTL
90 /*
91 * update_defense_level is called from keventd and from sysctl,
92 * so it needs to protect itself from softirqs
93 */
update_defense_level(struct netns_ipvs * ipvs)94 static void update_defense_level(struct netns_ipvs *ipvs)
95 {
96 struct sysinfo i;
97 int availmem;
98 int nomem;
99 int to_change = -1;
100
101 /* we only count free and buffered memory (in pages) */
102 si_meminfo(&i);
103 availmem = i.freeram + i.bufferram;
104 /* however in linux 2.5 the i.bufferram is total page cache size,
105 we need adjust it */
106 /* si_swapinfo(&i); */
107 /* availmem = availmem - (i.totalswap - i.freeswap); */
108
109 nomem = (availmem < ipvs->sysctl_amemthresh);
110
111 local_bh_disable();
112
113 /* drop_entry */
114 spin_lock(&ipvs->dropentry_lock);
115 switch (ipvs->sysctl_drop_entry) {
116 case 0:
117 atomic_set(&ipvs->dropentry, 0);
118 break;
119 case 1:
120 if (nomem) {
121 atomic_set(&ipvs->dropentry, 1);
122 ipvs->sysctl_drop_entry = 2;
123 } else {
124 atomic_set(&ipvs->dropentry, 0);
125 }
126 break;
127 case 2:
128 if (nomem) {
129 atomic_set(&ipvs->dropentry, 1);
130 } else {
131 atomic_set(&ipvs->dropentry, 0);
132 ipvs->sysctl_drop_entry = 1;
133 }
134 break;
135 case 3:
136 atomic_set(&ipvs->dropentry, 1);
137 break;
138 }
139 spin_unlock(&ipvs->dropentry_lock);
140
141 /* drop_packet */
142 spin_lock(&ipvs->droppacket_lock);
143 switch (ipvs->sysctl_drop_packet) {
144 case 0:
145 ipvs->drop_rate = 0;
146 break;
147 case 1:
148 if (nomem) {
149 ipvs->drop_rate = ipvs->drop_counter
150 = ipvs->sysctl_amemthresh /
151 (ipvs->sysctl_amemthresh-availmem);
152 ipvs->sysctl_drop_packet = 2;
153 } else {
154 ipvs->drop_rate = 0;
155 }
156 break;
157 case 2:
158 if (nomem) {
159 ipvs->drop_rate = ipvs->drop_counter
160 = ipvs->sysctl_amemthresh /
161 (ipvs->sysctl_amemthresh-availmem);
162 } else {
163 ipvs->drop_rate = 0;
164 ipvs->sysctl_drop_packet = 1;
165 }
166 break;
167 case 3:
168 ipvs->drop_rate = ipvs->sysctl_am_droprate;
169 break;
170 }
171 spin_unlock(&ipvs->droppacket_lock);
172
173 /* secure_tcp */
174 spin_lock(&ipvs->securetcp_lock);
175 switch (ipvs->sysctl_secure_tcp) {
176 case 0:
177 if (ipvs->old_secure_tcp >= 2)
178 to_change = 0;
179 break;
180 case 1:
181 if (nomem) {
182 if (ipvs->old_secure_tcp < 2)
183 to_change = 1;
184 ipvs->sysctl_secure_tcp = 2;
185 } else {
186 if (ipvs->old_secure_tcp >= 2)
187 to_change = 0;
188 }
189 break;
190 case 2:
191 if (nomem) {
192 if (ipvs->old_secure_tcp < 2)
193 to_change = 1;
194 } else {
195 if (ipvs->old_secure_tcp >= 2)
196 to_change = 0;
197 ipvs->sysctl_secure_tcp = 1;
198 }
199 break;
200 case 3:
201 if (ipvs->old_secure_tcp < 2)
202 to_change = 1;
203 break;
204 }
205 ipvs->old_secure_tcp = ipvs->sysctl_secure_tcp;
206 if (to_change >= 0)
207 ip_vs_protocol_timeout_change(ipvs,
208 ipvs->sysctl_secure_tcp > 1);
209 spin_unlock(&ipvs->securetcp_lock);
210
211 local_bh_enable();
212 }
213
214 /* Handler for delayed work for expiring no
215 * destination connections
216 */
expire_nodest_conn_handler(struct work_struct * work)217 static void expire_nodest_conn_handler(struct work_struct *work)
218 {
219 struct netns_ipvs *ipvs;
220
221 ipvs = container_of(work, struct netns_ipvs,
222 expire_nodest_conn_work.work);
223 ip_vs_expire_nodest_conn_flush(ipvs);
224 }
225
226 /*
227 * Timer for checking the defense
228 */
229 #define DEFENSE_TIMER_PERIOD 1*HZ
230
defense_work_handler(struct work_struct * work)231 static void defense_work_handler(struct work_struct *work)
232 {
233 struct netns_ipvs *ipvs =
234 container_of(work, struct netns_ipvs, defense_work.work);
235
236 update_defense_level(ipvs);
237 if (atomic_read(&ipvs->dropentry))
238 ip_vs_random_dropentry(ipvs);
239 queue_delayed_work(system_long_wq, &ipvs->defense_work,
240 DEFENSE_TIMER_PERIOD);
241 }
242 #endif
243
244 int
ip_vs_use_count_inc(void)245 ip_vs_use_count_inc(void)
246 {
247 return try_module_get(THIS_MODULE);
248 }
249
250 void
ip_vs_use_count_dec(void)251 ip_vs_use_count_dec(void)
252 {
253 module_put(THIS_MODULE);
254 }
255
256
257 /*
258 * Hash table: for virtual service lookups
259 */
260 #define IP_VS_SVC_TAB_BITS 8
261 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
262 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
263
264 /* the service table hashed by <protocol, addr, port> */
265 static struct hlist_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
266 /* the service table hashed by fwmark */
267 static struct hlist_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
268
269
270 /*
271 * Returns hash value for virtual service
272 */
273 static inline unsigned int
ip_vs_svc_hashkey(struct netns_ipvs * ipvs,int af,unsigned int proto,const union nf_inet_addr * addr,__be16 port)274 ip_vs_svc_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
275 const union nf_inet_addr *addr, __be16 port)
276 {
277 unsigned int porth = ntohs(port);
278 __be32 addr_fold = addr->ip;
279 __u32 ahash;
280
281 #ifdef CONFIG_IP_VS_IPV6
282 if (af == AF_INET6)
283 addr_fold = addr->ip6[0]^addr->ip6[1]^
284 addr->ip6[2]^addr->ip6[3];
285 #endif
286 ahash = ntohl(addr_fold);
287 ahash ^= ((size_t) ipvs >> 8);
288
289 return (proto ^ ahash ^ (porth >> IP_VS_SVC_TAB_BITS) ^ porth) &
290 IP_VS_SVC_TAB_MASK;
291 }
292
293 /*
294 * Returns hash value of fwmark for virtual service lookup
295 */
ip_vs_svc_fwm_hashkey(struct netns_ipvs * ipvs,__u32 fwmark)296 static inline unsigned int ip_vs_svc_fwm_hashkey(struct netns_ipvs *ipvs, __u32 fwmark)
297 {
298 return (((size_t)ipvs>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK;
299 }
300
301 /*
302 * Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
303 * or in the ip_vs_svc_fwm_table by fwmark.
304 * Should be called with locked tables.
305 */
ip_vs_svc_hash(struct ip_vs_service * svc)306 static int ip_vs_svc_hash(struct ip_vs_service *svc)
307 {
308 unsigned int hash;
309
310 if (svc->flags & IP_VS_SVC_F_HASHED) {
311 pr_err("%s(): request for already hashed, called from %pS\n",
312 __func__, __builtin_return_address(0));
313 return 0;
314 }
315
316 if (svc->fwmark == 0) {
317 /*
318 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
319 */
320 hash = ip_vs_svc_hashkey(svc->ipvs, svc->af, svc->protocol,
321 &svc->addr, svc->port);
322 hlist_add_head_rcu(&svc->s_list, &ip_vs_svc_table[hash]);
323 } else {
324 /*
325 * Hash it by fwmark in svc_fwm_table
326 */
327 hash = ip_vs_svc_fwm_hashkey(svc->ipvs, svc->fwmark);
328 hlist_add_head_rcu(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
329 }
330
331 svc->flags |= IP_VS_SVC_F_HASHED;
332 /* increase its refcnt because it is referenced by the svc table */
333 atomic_inc(&svc->refcnt);
334 return 1;
335 }
336
337
338 /*
339 * Unhashes a service from svc_table / svc_fwm_table.
340 * Should be called with locked tables.
341 */
ip_vs_svc_unhash(struct ip_vs_service * svc)342 static int ip_vs_svc_unhash(struct ip_vs_service *svc)
343 {
344 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
345 pr_err("%s(): request for unhash flagged, called from %pS\n",
346 __func__, __builtin_return_address(0));
347 return 0;
348 }
349
350 if (svc->fwmark == 0) {
351 /* Remove it from the svc_table table */
352 hlist_del_rcu(&svc->s_list);
353 } else {
354 /* Remove it from the svc_fwm_table table */
355 hlist_del_rcu(&svc->f_list);
356 }
357
358 svc->flags &= ~IP_VS_SVC_F_HASHED;
359 atomic_dec(&svc->refcnt);
360 return 1;
361 }
362
363
364 /*
365 * Get service by {netns, proto,addr,port} in the service table.
366 */
367 static inline struct ip_vs_service *
__ip_vs_service_find(struct netns_ipvs * ipvs,int af,__u16 protocol,const union nf_inet_addr * vaddr,__be16 vport)368 __ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol,
369 const union nf_inet_addr *vaddr, __be16 vport)
370 {
371 unsigned int hash;
372 struct ip_vs_service *svc;
373
374 /* Check for "full" addressed entries */
375 hash = ip_vs_svc_hashkey(ipvs, af, protocol, vaddr, vport);
376
377 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[hash], s_list) {
378 if ((svc->af == af)
379 && ip_vs_addr_equal(af, &svc->addr, vaddr)
380 && (svc->port == vport)
381 && (svc->protocol == protocol)
382 && (svc->ipvs == ipvs)) {
383 /* HIT */
384 return svc;
385 }
386 }
387
388 return NULL;
389 }
390
391
392 /*
393 * Get service by {fwmark} in the service table.
394 */
395 static inline struct ip_vs_service *
__ip_vs_svc_fwm_find(struct netns_ipvs * ipvs,int af,__u32 fwmark)396 __ip_vs_svc_fwm_find(struct netns_ipvs *ipvs, int af, __u32 fwmark)
397 {
398 unsigned int hash;
399 struct ip_vs_service *svc;
400
401 /* Check for fwmark addressed entries */
402 hash = ip_vs_svc_fwm_hashkey(ipvs, fwmark);
403
404 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[hash], f_list) {
405 if (svc->fwmark == fwmark && svc->af == af
406 && (svc->ipvs == ipvs)) {
407 /* HIT */
408 return svc;
409 }
410 }
411
412 return NULL;
413 }
414
415 /* Find service, called under RCU lock */
416 struct ip_vs_service *
ip_vs_service_find(struct netns_ipvs * ipvs,int af,__u32 fwmark,__u16 protocol,const union nf_inet_addr * vaddr,__be16 vport)417 ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
418 const union nf_inet_addr *vaddr, __be16 vport)
419 {
420 struct ip_vs_service *svc;
421
422 /*
423 * Check the table hashed by fwmark first
424 */
425 if (fwmark) {
426 svc = __ip_vs_svc_fwm_find(ipvs, af, fwmark);
427 if (svc)
428 goto out;
429 }
430
431 /*
432 * Check the table hashed by <protocol,addr,port>
433 * for "full" addressed entries
434 */
435 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, vport);
436
437 if (!svc && protocol == IPPROTO_TCP &&
438 atomic_read(&ipvs->ftpsvc_counter) &&
439 (vport == FTPDATA || !inet_port_requires_bind_service(ipvs->net, ntohs(vport)))) {
440 /*
441 * Check if ftp service entry exists, the packet
442 * might belong to FTP data connections.
443 */
444 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, FTPPORT);
445 }
446
447 if (svc == NULL
448 && atomic_read(&ipvs->nullsvc_counter)) {
449 /*
450 * Check if the catch-all port (port zero) exists
451 */
452 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, 0);
453 }
454
455 out:
456 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
457 fwmark, ip_vs_proto_name(protocol),
458 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
459 svc ? "hit" : "not hit");
460
461 return svc;
462 }
463
464
465 static inline void
__ip_vs_bind_svc(struct ip_vs_dest * dest,struct ip_vs_service * svc)466 __ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
467 {
468 atomic_inc(&svc->refcnt);
469 rcu_assign_pointer(dest->svc, svc);
470 }
471
ip_vs_service_free(struct ip_vs_service * svc)472 static void ip_vs_service_free(struct ip_vs_service *svc)
473 {
474 free_percpu(svc->stats.cpustats);
475 kfree(svc);
476 }
477
ip_vs_service_rcu_free(struct rcu_head * head)478 static void ip_vs_service_rcu_free(struct rcu_head *head)
479 {
480 struct ip_vs_service *svc;
481
482 svc = container_of(head, struct ip_vs_service, rcu_head);
483 ip_vs_service_free(svc);
484 }
485
__ip_vs_svc_put(struct ip_vs_service * svc,bool do_delay)486 static void __ip_vs_svc_put(struct ip_vs_service *svc, bool do_delay)
487 {
488 if (atomic_dec_and_test(&svc->refcnt)) {
489 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
490 svc->fwmark,
491 IP_VS_DBG_ADDR(svc->af, &svc->addr),
492 ntohs(svc->port));
493 if (do_delay)
494 call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
495 else
496 ip_vs_service_free(svc);
497 }
498 }
499
500
501 /*
502 * Returns hash value for real service
503 */
ip_vs_rs_hashkey(int af,const union nf_inet_addr * addr,__be16 port)504 static inline unsigned int ip_vs_rs_hashkey(int af,
505 const union nf_inet_addr *addr,
506 __be16 port)
507 {
508 unsigned int porth = ntohs(port);
509 __be32 addr_fold = addr->ip;
510
511 #ifdef CONFIG_IP_VS_IPV6
512 if (af == AF_INET6)
513 addr_fold = addr->ip6[0]^addr->ip6[1]^
514 addr->ip6[2]^addr->ip6[3];
515 #endif
516
517 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
518 & IP_VS_RTAB_MASK;
519 }
520
521 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
ip_vs_rs_hash(struct netns_ipvs * ipvs,struct ip_vs_dest * dest)522 static void ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
523 {
524 unsigned int hash;
525 __be16 port;
526
527 if (dest->in_rs_table)
528 return;
529
530 switch (IP_VS_DFWD_METHOD(dest)) {
531 case IP_VS_CONN_F_MASQ:
532 port = dest->port;
533 break;
534 case IP_VS_CONN_F_TUNNEL:
535 switch (dest->tun_type) {
536 case IP_VS_CONN_F_TUNNEL_TYPE_GUE:
537 port = dest->tun_port;
538 break;
539 case IP_VS_CONN_F_TUNNEL_TYPE_IPIP:
540 case IP_VS_CONN_F_TUNNEL_TYPE_GRE:
541 port = 0;
542 break;
543 default:
544 return;
545 }
546 break;
547 default:
548 return;
549 }
550
551 /*
552 * Hash by proto,addr,port,
553 * which are the parameters of the real service.
554 */
555 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, port);
556
557 hlist_add_head_rcu(&dest->d_list, &ipvs->rs_table[hash]);
558 dest->in_rs_table = 1;
559 }
560
561 /* Unhash ip_vs_dest from rs_table. */
ip_vs_rs_unhash(struct ip_vs_dest * dest)562 static void ip_vs_rs_unhash(struct ip_vs_dest *dest)
563 {
564 /*
565 * Remove it from the rs_table table.
566 */
567 if (dest->in_rs_table) {
568 hlist_del_rcu(&dest->d_list);
569 dest->in_rs_table = 0;
570 }
571 }
572
573 /* Check if real service by <proto,addr,port> is present */
ip_vs_has_real_service(struct netns_ipvs * ipvs,int af,__u16 protocol,const union nf_inet_addr * daddr,__be16 dport)574 bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
575 const union nf_inet_addr *daddr, __be16 dport)
576 {
577 unsigned int hash;
578 struct ip_vs_dest *dest;
579
580 /* Check for "full" addressed entries */
581 hash = ip_vs_rs_hashkey(af, daddr, dport);
582
583 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
584 if (dest->port == dport &&
585 dest->af == af &&
586 ip_vs_addr_equal(af, &dest->addr, daddr) &&
587 (dest->protocol == protocol || dest->vfwmark) &&
588 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_MASQ) {
589 /* HIT */
590 return true;
591 }
592 }
593
594 return false;
595 }
596
597 /* Find real service record by <proto,addr,port>.
598 * In case of multiple records with the same <proto,addr,port>, only
599 * the first found record is returned.
600 *
601 * To be called under RCU lock.
602 */
ip_vs_find_real_service(struct netns_ipvs * ipvs,int af,__u16 protocol,const union nf_inet_addr * daddr,__be16 dport)603 struct ip_vs_dest *ip_vs_find_real_service(struct netns_ipvs *ipvs, int af,
604 __u16 protocol,
605 const union nf_inet_addr *daddr,
606 __be16 dport)
607 {
608 unsigned int hash;
609 struct ip_vs_dest *dest;
610
611 /* Check for "full" addressed entries */
612 hash = ip_vs_rs_hashkey(af, daddr, dport);
613
614 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
615 if (dest->port == dport &&
616 dest->af == af &&
617 ip_vs_addr_equal(af, &dest->addr, daddr) &&
618 (dest->protocol == protocol || dest->vfwmark) &&
619 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_MASQ) {
620 /* HIT */
621 return dest;
622 }
623 }
624
625 return NULL;
626 }
627
628 /* Find real service record by <af,addr,tun_port>.
629 * In case of multiple records with the same <af,addr,tun_port>, only
630 * the first found record is returned.
631 *
632 * To be called under RCU lock.
633 */
ip_vs_find_tunnel(struct netns_ipvs * ipvs,int af,const union nf_inet_addr * daddr,__be16 tun_port)634 struct ip_vs_dest *ip_vs_find_tunnel(struct netns_ipvs *ipvs, int af,
635 const union nf_inet_addr *daddr,
636 __be16 tun_port)
637 {
638 struct ip_vs_dest *dest;
639 unsigned int hash;
640
641 /* Check for "full" addressed entries */
642 hash = ip_vs_rs_hashkey(af, daddr, tun_port);
643
644 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
645 if (dest->tun_port == tun_port &&
646 dest->af == af &&
647 ip_vs_addr_equal(af, &dest->addr, daddr) &&
648 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_TUNNEL) {
649 /* HIT */
650 return dest;
651 }
652 }
653
654 return NULL;
655 }
656
657 /* Lookup destination by {addr,port} in the given service
658 * Called under RCU lock.
659 */
660 static struct ip_vs_dest *
ip_vs_lookup_dest(struct ip_vs_service * svc,int dest_af,const union nf_inet_addr * daddr,__be16 dport)661 ip_vs_lookup_dest(struct ip_vs_service *svc, int dest_af,
662 const union nf_inet_addr *daddr, __be16 dport)
663 {
664 struct ip_vs_dest *dest;
665
666 /*
667 * Find the destination for the given service
668 */
669 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
670 if ((dest->af == dest_af) &&
671 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
672 (dest->port == dport)) {
673 /* HIT */
674 return dest;
675 }
676 }
677
678 return NULL;
679 }
680
681 /*
682 * Find destination by {daddr,dport,vaddr,protocol}
683 * Created to be used in ip_vs_process_message() in
684 * the backup synchronization daemon. It finds the
685 * destination to be bound to the received connection
686 * on the backup.
687 * Called under RCU lock, no refcnt is returned.
688 */
ip_vs_find_dest(struct netns_ipvs * ipvs,int svc_af,int dest_af,const union nf_inet_addr * daddr,__be16 dport,const union nf_inet_addr * vaddr,__be16 vport,__u16 protocol,__u32 fwmark,__u32 flags)689 struct ip_vs_dest *ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
690 const union nf_inet_addr *daddr,
691 __be16 dport,
692 const union nf_inet_addr *vaddr,
693 __be16 vport, __u16 protocol, __u32 fwmark,
694 __u32 flags)
695 {
696 struct ip_vs_dest *dest;
697 struct ip_vs_service *svc;
698 __be16 port = dport;
699
700 svc = ip_vs_service_find(ipvs, svc_af, fwmark, protocol, vaddr, vport);
701 if (!svc)
702 return NULL;
703 if (fwmark && (flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ)
704 port = 0;
705 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port);
706 if (!dest)
707 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port ^ dport);
708 return dest;
709 }
710
ip_vs_dest_dst_rcu_free(struct rcu_head * head)711 void ip_vs_dest_dst_rcu_free(struct rcu_head *head)
712 {
713 struct ip_vs_dest_dst *dest_dst = container_of(head,
714 struct ip_vs_dest_dst,
715 rcu_head);
716
717 dst_release(dest_dst->dst_cache);
718 kfree(dest_dst);
719 }
720
721 /* Release dest_dst and dst_cache for dest in user context */
__ip_vs_dst_cache_reset(struct ip_vs_dest * dest)722 static void __ip_vs_dst_cache_reset(struct ip_vs_dest *dest)
723 {
724 struct ip_vs_dest_dst *old;
725
726 old = rcu_dereference_protected(dest->dest_dst, 1);
727 if (old) {
728 RCU_INIT_POINTER(dest->dest_dst, NULL);
729 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
730 }
731 }
732
733 /*
734 * Lookup dest by {svc,addr,port} in the destination trash.
735 * The destination trash is used to hold the destinations that are removed
736 * from the service table but are still referenced by some conn entries.
737 * The reason to add the destination trash is when the dest is temporary
738 * down (either by administrator or by monitor program), the dest can be
739 * picked back from the trash, the remaining connections to the dest can
740 * continue, and the counting information of the dest is also useful for
741 * scheduling.
742 */
743 static struct ip_vs_dest *
ip_vs_trash_get_dest(struct ip_vs_service * svc,int dest_af,const union nf_inet_addr * daddr,__be16 dport)744 ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af,
745 const union nf_inet_addr *daddr, __be16 dport)
746 {
747 struct ip_vs_dest *dest;
748 struct netns_ipvs *ipvs = svc->ipvs;
749
750 /*
751 * Find the destination in trash
752 */
753 spin_lock_bh(&ipvs->dest_trash_lock);
754 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
755 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
756 "dest->refcnt=%d\n",
757 dest->vfwmark,
758 IP_VS_DBG_ADDR(dest->af, &dest->addr),
759 ntohs(dest->port),
760 refcount_read(&dest->refcnt));
761 if (dest->af == dest_af &&
762 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
763 dest->port == dport &&
764 dest->vfwmark == svc->fwmark &&
765 dest->protocol == svc->protocol &&
766 (svc->fwmark ||
767 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
768 dest->vport == svc->port))) {
769 /* HIT */
770 list_del(&dest->t_list);
771 goto out;
772 }
773 }
774
775 dest = NULL;
776
777 out:
778 spin_unlock_bh(&ipvs->dest_trash_lock);
779
780 return dest;
781 }
782
ip_vs_dest_free(struct ip_vs_dest * dest)783 static void ip_vs_dest_free(struct ip_vs_dest *dest)
784 {
785 struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
786
787 __ip_vs_dst_cache_reset(dest);
788 __ip_vs_svc_put(svc, false);
789 free_percpu(dest->stats.cpustats);
790 ip_vs_dest_put_and_free(dest);
791 }
792
793 /*
794 * Clean up all the destinations in the trash
795 * Called by the ip_vs_control_cleanup()
796 *
797 * When the ip_vs_control_clearup is activated by ipvs module exit,
798 * the service tables must have been flushed and all the connections
799 * are expired, and the refcnt of each destination in the trash must
800 * be 1, so we simply release them here.
801 */
ip_vs_trash_cleanup(struct netns_ipvs * ipvs)802 static void ip_vs_trash_cleanup(struct netns_ipvs *ipvs)
803 {
804 struct ip_vs_dest *dest, *nxt;
805
806 del_timer_sync(&ipvs->dest_trash_timer);
807 /* No need to use dest_trash_lock */
808 list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, t_list) {
809 list_del(&dest->t_list);
810 ip_vs_dest_free(dest);
811 }
812 }
813
814 static void
ip_vs_copy_stats(struct ip_vs_kstats * dst,struct ip_vs_stats * src)815 ip_vs_copy_stats(struct ip_vs_kstats *dst, struct ip_vs_stats *src)
816 {
817 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
818
819 spin_lock_bh(&src->lock);
820
821 IP_VS_SHOW_STATS_COUNTER(conns);
822 IP_VS_SHOW_STATS_COUNTER(inpkts);
823 IP_VS_SHOW_STATS_COUNTER(outpkts);
824 IP_VS_SHOW_STATS_COUNTER(inbytes);
825 IP_VS_SHOW_STATS_COUNTER(outbytes);
826
827 ip_vs_read_estimator(dst, src);
828
829 spin_unlock_bh(&src->lock);
830 }
831
832 static void
ip_vs_export_stats_user(struct ip_vs_stats_user * dst,struct ip_vs_kstats * src)833 ip_vs_export_stats_user(struct ip_vs_stats_user *dst, struct ip_vs_kstats *src)
834 {
835 dst->conns = (u32)src->conns;
836 dst->inpkts = (u32)src->inpkts;
837 dst->outpkts = (u32)src->outpkts;
838 dst->inbytes = src->inbytes;
839 dst->outbytes = src->outbytes;
840 dst->cps = (u32)src->cps;
841 dst->inpps = (u32)src->inpps;
842 dst->outpps = (u32)src->outpps;
843 dst->inbps = (u32)src->inbps;
844 dst->outbps = (u32)src->outbps;
845 }
846
847 static void
ip_vs_zero_stats(struct ip_vs_stats * stats)848 ip_vs_zero_stats(struct ip_vs_stats *stats)
849 {
850 spin_lock_bh(&stats->lock);
851
852 /* get current counters as zero point, rates are zeroed */
853
854 #define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
855
856 IP_VS_ZERO_STATS_COUNTER(conns);
857 IP_VS_ZERO_STATS_COUNTER(inpkts);
858 IP_VS_ZERO_STATS_COUNTER(outpkts);
859 IP_VS_ZERO_STATS_COUNTER(inbytes);
860 IP_VS_ZERO_STATS_COUNTER(outbytes);
861
862 ip_vs_zero_estimator(stats);
863
864 spin_unlock_bh(&stats->lock);
865 }
866
867 /*
868 * Update a destination in the given service
869 */
870 static void
__ip_vs_update_dest(struct ip_vs_service * svc,struct ip_vs_dest * dest,struct ip_vs_dest_user_kern * udest,int add)871 __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
872 struct ip_vs_dest_user_kern *udest, int add)
873 {
874 struct netns_ipvs *ipvs = svc->ipvs;
875 struct ip_vs_service *old_svc;
876 struct ip_vs_scheduler *sched;
877 int conn_flags;
878
879 /* We cannot modify an address and change the address family */
880 BUG_ON(!add && udest->af != dest->af);
881
882 if (add && udest->af != svc->af)
883 ipvs->mixed_address_family_dests++;
884
885 /* keep the last_weight with latest non-0 weight */
886 if (add || udest->weight != 0)
887 atomic_set(&dest->last_weight, udest->weight);
888
889 /* set the weight and the flags */
890 atomic_set(&dest->weight, udest->weight);
891 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
892 conn_flags |= IP_VS_CONN_F_INACTIVE;
893
894 /* Need to rehash? */
895 if ((udest->conn_flags & IP_VS_CONN_F_FWD_MASK) !=
896 IP_VS_DFWD_METHOD(dest) ||
897 udest->tun_type != dest->tun_type ||
898 udest->tun_port != dest->tun_port)
899 ip_vs_rs_unhash(dest);
900
901 /* set the tunnel info */
902 dest->tun_type = udest->tun_type;
903 dest->tun_port = udest->tun_port;
904 dest->tun_flags = udest->tun_flags;
905
906 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
907 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
908 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
909 } else {
910 /* FTP-NAT requires conntrack for mangling */
911 if (svc->port == FTPPORT)
912 ip_vs_register_conntrack(svc);
913 }
914 atomic_set(&dest->conn_flags, conn_flags);
915 /* Put the real service in rs_table if not present. */
916 ip_vs_rs_hash(ipvs, dest);
917
918 /* bind the service */
919 old_svc = rcu_dereference_protected(dest->svc, 1);
920 if (!old_svc) {
921 __ip_vs_bind_svc(dest, svc);
922 } else {
923 if (old_svc != svc) {
924 ip_vs_zero_stats(&dest->stats);
925 __ip_vs_bind_svc(dest, svc);
926 __ip_vs_svc_put(old_svc, true);
927 }
928 }
929
930 /* set the dest status flags */
931 dest->flags |= IP_VS_DEST_F_AVAILABLE;
932
933 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
934 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
935 dest->u_threshold = udest->u_threshold;
936 dest->l_threshold = udest->l_threshold;
937
938 dest->af = udest->af;
939
940 spin_lock_bh(&dest->dst_lock);
941 __ip_vs_dst_cache_reset(dest);
942 spin_unlock_bh(&dest->dst_lock);
943
944 if (add) {
945 ip_vs_start_estimator(svc->ipvs, &dest->stats);
946 list_add_rcu(&dest->n_list, &svc->destinations);
947 svc->num_dests++;
948 sched = rcu_dereference_protected(svc->scheduler, 1);
949 if (sched && sched->add_dest)
950 sched->add_dest(svc, dest);
951 } else {
952 sched = rcu_dereference_protected(svc->scheduler, 1);
953 if (sched && sched->upd_dest)
954 sched->upd_dest(svc, dest);
955 }
956 }
957
958
959 /*
960 * Create a destination for the given service
961 */
962 static int
ip_vs_new_dest(struct ip_vs_service * svc,struct ip_vs_dest_user_kern * udest)963 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
964 {
965 struct ip_vs_dest *dest;
966 unsigned int atype, i;
967
968 EnterFunction(2);
969
970 #ifdef CONFIG_IP_VS_IPV6
971 if (udest->af == AF_INET6) {
972 int ret;
973
974 atype = ipv6_addr_type(&udest->addr.in6);
975 if ((!(atype & IPV6_ADDR_UNICAST) ||
976 atype & IPV6_ADDR_LINKLOCAL) &&
977 !__ip_vs_addr_is_local_v6(svc->ipvs->net, &udest->addr.in6))
978 return -EINVAL;
979
980 ret = nf_defrag_ipv6_enable(svc->ipvs->net);
981 if (ret)
982 return ret;
983 } else
984 #endif
985 {
986 atype = inet_addr_type(svc->ipvs->net, udest->addr.ip);
987 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
988 return -EINVAL;
989 }
990
991 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
992 if (dest == NULL)
993 return -ENOMEM;
994
995 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
996 if (!dest->stats.cpustats)
997 goto err_alloc;
998
999 for_each_possible_cpu(i) {
1000 struct ip_vs_cpu_stats *ip_vs_dest_stats;
1001 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
1002 u64_stats_init(&ip_vs_dest_stats->syncp);
1003 }
1004
1005 dest->af = udest->af;
1006 dest->protocol = svc->protocol;
1007 dest->vaddr = svc->addr;
1008 dest->vport = svc->port;
1009 dest->vfwmark = svc->fwmark;
1010 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
1011 dest->port = udest->port;
1012
1013 atomic_set(&dest->activeconns, 0);
1014 atomic_set(&dest->inactconns, 0);
1015 atomic_set(&dest->persistconns, 0);
1016 refcount_set(&dest->refcnt, 1);
1017
1018 INIT_HLIST_NODE(&dest->d_list);
1019 spin_lock_init(&dest->dst_lock);
1020 spin_lock_init(&dest->stats.lock);
1021 __ip_vs_update_dest(svc, dest, udest, 1);
1022
1023 LeaveFunction(2);
1024 return 0;
1025
1026 err_alloc:
1027 kfree(dest);
1028 return -ENOMEM;
1029 }
1030
1031
1032 /*
1033 * Add a destination into an existing service
1034 */
1035 static int
ip_vs_add_dest(struct ip_vs_service * svc,struct ip_vs_dest_user_kern * udest)1036 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1037 {
1038 struct ip_vs_dest *dest;
1039 union nf_inet_addr daddr;
1040 __be16 dport = udest->port;
1041 int ret;
1042
1043 EnterFunction(2);
1044
1045 if (udest->weight < 0) {
1046 pr_err("%s(): server weight less than zero\n", __func__);
1047 return -ERANGE;
1048 }
1049
1050 if (udest->l_threshold > udest->u_threshold) {
1051 pr_err("%s(): lower threshold is higher than upper threshold\n",
1052 __func__);
1053 return -ERANGE;
1054 }
1055
1056 if (udest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1057 if (udest->tun_port == 0) {
1058 pr_err("%s(): tunnel port is zero\n", __func__);
1059 return -EINVAL;
1060 }
1061 }
1062
1063 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1064
1065 /* We use function that requires RCU lock */
1066 rcu_read_lock();
1067 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1068 rcu_read_unlock();
1069
1070 if (dest != NULL) {
1071 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
1072 return -EEXIST;
1073 }
1074
1075 /*
1076 * Check if the dest already exists in the trash and
1077 * is from the same service
1078 */
1079 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
1080
1081 if (dest != NULL) {
1082 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
1083 "dest->refcnt=%d, service %u/%s:%u\n",
1084 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
1085 refcount_read(&dest->refcnt),
1086 dest->vfwmark,
1087 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
1088 ntohs(dest->vport));
1089
1090 __ip_vs_update_dest(svc, dest, udest, 1);
1091 ret = 0;
1092 } else {
1093 /*
1094 * Allocate and initialize the dest structure
1095 */
1096 ret = ip_vs_new_dest(svc, udest);
1097 }
1098 LeaveFunction(2);
1099
1100 return ret;
1101 }
1102
1103
1104 /*
1105 * Edit a destination in the given service
1106 */
1107 static int
ip_vs_edit_dest(struct ip_vs_service * svc,struct ip_vs_dest_user_kern * udest)1108 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1109 {
1110 struct ip_vs_dest *dest;
1111 union nf_inet_addr daddr;
1112 __be16 dport = udest->port;
1113
1114 EnterFunction(2);
1115
1116 if (udest->weight < 0) {
1117 pr_err("%s(): server weight less than zero\n", __func__);
1118 return -ERANGE;
1119 }
1120
1121 if (udest->l_threshold > udest->u_threshold) {
1122 pr_err("%s(): lower threshold is higher than upper threshold\n",
1123 __func__);
1124 return -ERANGE;
1125 }
1126
1127 if (udest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1128 if (udest->tun_port == 0) {
1129 pr_err("%s(): tunnel port is zero\n", __func__);
1130 return -EINVAL;
1131 }
1132 }
1133
1134 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1135
1136 /* We use function that requires RCU lock */
1137 rcu_read_lock();
1138 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1139 rcu_read_unlock();
1140
1141 if (dest == NULL) {
1142 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1143 return -ENOENT;
1144 }
1145
1146 __ip_vs_update_dest(svc, dest, udest, 0);
1147 LeaveFunction(2);
1148
1149 return 0;
1150 }
1151
1152 /*
1153 * Delete a destination (must be already unlinked from the service)
1154 */
__ip_vs_del_dest(struct netns_ipvs * ipvs,struct ip_vs_dest * dest,bool cleanup)1155 static void __ip_vs_del_dest(struct netns_ipvs *ipvs, struct ip_vs_dest *dest,
1156 bool cleanup)
1157 {
1158 ip_vs_stop_estimator(ipvs, &dest->stats);
1159
1160 /*
1161 * Remove it from the d-linked list with the real services.
1162 */
1163 ip_vs_rs_unhash(dest);
1164
1165 spin_lock_bh(&ipvs->dest_trash_lock);
1166 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1167 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1168 refcount_read(&dest->refcnt));
1169 if (list_empty(&ipvs->dest_trash) && !cleanup)
1170 mod_timer(&ipvs->dest_trash_timer,
1171 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1172 /* dest lives in trash with reference */
1173 list_add(&dest->t_list, &ipvs->dest_trash);
1174 dest->idle_start = 0;
1175 spin_unlock_bh(&ipvs->dest_trash_lock);
1176
1177 /* Queue up delayed work to expire all no destination connections.
1178 * No-op when CONFIG_SYSCTL is disabled.
1179 */
1180 if (!cleanup)
1181 ip_vs_enqueue_expire_nodest_conns(ipvs);
1182 }
1183
1184
1185 /*
1186 * Unlink a destination from the given service
1187 */
__ip_vs_unlink_dest(struct ip_vs_service * svc,struct ip_vs_dest * dest,int svcupd)1188 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1189 struct ip_vs_dest *dest,
1190 int svcupd)
1191 {
1192 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1193
1194 /*
1195 * Remove it from the d-linked destination list.
1196 */
1197 list_del_rcu(&dest->n_list);
1198 svc->num_dests--;
1199
1200 if (dest->af != svc->af)
1201 svc->ipvs->mixed_address_family_dests--;
1202
1203 if (svcupd) {
1204 struct ip_vs_scheduler *sched;
1205
1206 sched = rcu_dereference_protected(svc->scheduler, 1);
1207 if (sched && sched->del_dest)
1208 sched->del_dest(svc, dest);
1209 }
1210 }
1211
1212
1213 /*
1214 * Delete a destination server in the given service
1215 */
1216 static int
ip_vs_del_dest(struct ip_vs_service * svc,struct ip_vs_dest_user_kern * udest)1217 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1218 {
1219 struct ip_vs_dest *dest;
1220 __be16 dport = udest->port;
1221
1222 EnterFunction(2);
1223
1224 /* We use function that requires RCU lock */
1225 rcu_read_lock();
1226 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1227 rcu_read_unlock();
1228
1229 if (dest == NULL) {
1230 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1231 return -ENOENT;
1232 }
1233
1234 /*
1235 * Unlink dest from the service
1236 */
1237 __ip_vs_unlink_dest(svc, dest, 1);
1238
1239 /*
1240 * Delete the destination
1241 */
1242 __ip_vs_del_dest(svc->ipvs, dest, false);
1243
1244 LeaveFunction(2);
1245
1246 return 0;
1247 }
1248
ip_vs_dest_trash_expire(struct timer_list * t)1249 static void ip_vs_dest_trash_expire(struct timer_list *t)
1250 {
1251 struct netns_ipvs *ipvs = from_timer(ipvs, t, dest_trash_timer);
1252 struct ip_vs_dest *dest, *next;
1253 unsigned long now = jiffies;
1254
1255 spin_lock(&ipvs->dest_trash_lock);
1256 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1257 if (refcount_read(&dest->refcnt) > 1)
1258 continue;
1259 if (dest->idle_start) {
1260 if (time_before(now, dest->idle_start +
1261 IP_VS_DEST_TRASH_PERIOD))
1262 continue;
1263 } else {
1264 dest->idle_start = max(1UL, now);
1265 continue;
1266 }
1267 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1268 dest->vfwmark,
1269 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1270 ntohs(dest->port));
1271 list_del(&dest->t_list);
1272 ip_vs_dest_free(dest);
1273 }
1274 if (!list_empty(&ipvs->dest_trash))
1275 mod_timer(&ipvs->dest_trash_timer,
1276 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1277 spin_unlock(&ipvs->dest_trash_lock);
1278 }
1279
1280 /*
1281 * Add a service into the service hash table
1282 */
1283 static int
ip_vs_add_service(struct netns_ipvs * ipvs,struct ip_vs_service_user_kern * u,struct ip_vs_service ** svc_p)1284 ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
1285 struct ip_vs_service **svc_p)
1286 {
1287 int ret = 0, i;
1288 struct ip_vs_scheduler *sched = NULL;
1289 struct ip_vs_pe *pe = NULL;
1290 struct ip_vs_service *svc = NULL;
1291 int ret_hooks = -1;
1292
1293 /* increase the module use count */
1294 if (!ip_vs_use_count_inc())
1295 return -ENOPROTOOPT;
1296
1297 /* Lookup the scheduler by 'u->sched_name' */
1298 if (strcmp(u->sched_name, "none")) {
1299 sched = ip_vs_scheduler_get(u->sched_name);
1300 if (!sched) {
1301 pr_info("Scheduler module ip_vs_%s not found\n",
1302 u->sched_name);
1303 ret = -ENOENT;
1304 goto out_err;
1305 }
1306 }
1307
1308 if (u->pe_name && *u->pe_name) {
1309 pe = ip_vs_pe_getbyname(u->pe_name);
1310 if (pe == NULL) {
1311 pr_info("persistence engine module ip_vs_pe_%s "
1312 "not found\n", u->pe_name);
1313 ret = -ENOENT;
1314 goto out_err;
1315 }
1316 }
1317
1318 #ifdef CONFIG_IP_VS_IPV6
1319 if (u->af == AF_INET6) {
1320 __u32 plen = (__force __u32) u->netmask;
1321
1322 if (plen < 1 || plen > 128) {
1323 ret = -EINVAL;
1324 goto out_err;
1325 }
1326
1327 ret = nf_defrag_ipv6_enable(ipvs->net);
1328 if (ret)
1329 goto out_err;
1330 }
1331 #endif
1332
1333 if ((u->af == AF_INET && !ipvs->num_services) ||
1334 (u->af == AF_INET6 && !ipvs->num_services6)) {
1335 ret = ip_vs_register_hooks(ipvs, u->af);
1336 if (ret < 0)
1337 goto out_err;
1338 ret_hooks = ret;
1339 }
1340
1341 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1342 if (svc == NULL) {
1343 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1344 ret = -ENOMEM;
1345 goto out_err;
1346 }
1347 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1348 if (!svc->stats.cpustats) {
1349 ret = -ENOMEM;
1350 goto out_err;
1351 }
1352
1353 for_each_possible_cpu(i) {
1354 struct ip_vs_cpu_stats *ip_vs_stats;
1355 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1356 u64_stats_init(&ip_vs_stats->syncp);
1357 }
1358
1359
1360 /* I'm the first user of the service */
1361 atomic_set(&svc->refcnt, 0);
1362
1363 svc->af = u->af;
1364 svc->protocol = u->protocol;
1365 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1366 svc->port = u->port;
1367 svc->fwmark = u->fwmark;
1368 svc->flags = u->flags & ~IP_VS_SVC_F_HASHED;
1369 svc->timeout = u->timeout * HZ;
1370 svc->netmask = u->netmask;
1371 svc->ipvs = ipvs;
1372
1373 INIT_LIST_HEAD(&svc->destinations);
1374 spin_lock_init(&svc->sched_lock);
1375 spin_lock_init(&svc->stats.lock);
1376
1377 /* Bind the scheduler */
1378 if (sched) {
1379 ret = ip_vs_bind_scheduler(svc, sched);
1380 if (ret)
1381 goto out_err;
1382 sched = NULL;
1383 }
1384
1385 /* Bind the ct retriever */
1386 RCU_INIT_POINTER(svc->pe, pe);
1387 pe = NULL;
1388
1389 /* Update the virtual service counters */
1390 if (svc->port == FTPPORT)
1391 atomic_inc(&ipvs->ftpsvc_counter);
1392 else if (svc->port == 0)
1393 atomic_inc(&ipvs->nullsvc_counter);
1394 if (svc->pe && svc->pe->conn_out)
1395 atomic_inc(&ipvs->conn_out_counter);
1396
1397 ip_vs_start_estimator(ipvs, &svc->stats);
1398
1399 /* Count only IPv4 services for old get/setsockopt interface */
1400 if (svc->af == AF_INET)
1401 ipvs->num_services++;
1402 else if (svc->af == AF_INET6)
1403 ipvs->num_services6++;
1404
1405 /* Hash the service into the service table */
1406 ip_vs_svc_hash(svc);
1407
1408 *svc_p = svc;
1409 /* Now there is a service - full throttle */
1410 ipvs->enable = 1;
1411 return 0;
1412
1413
1414 out_err:
1415 if (ret_hooks >= 0)
1416 ip_vs_unregister_hooks(ipvs, u->af);
1417 if (svc != NULL) {
1418 ip_vs_unbind_scheduler(svc, sched);
1419 ip_vs_service_free(svc);
1420 }
1421 ip_vs_scheduler_put(sched);
1422 ip_vs_pe_put(pe);
1423
1424 /* decrease the module use count */
1425 ip_vs_use_count_dec();
1426
1427 return ret;
1428 }
1429
1430
1431 /*
1432 * Edit a service and bind it with a new scheduler
1433 */
1434 static int
ip_vs_edit_service(struct ip_vs_service * svc,struct ip_vs_service_user_kern * u)1435 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1436 {
1437 struct ip_vs_scheduler *sched = NULL, *old_sched;
1438 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1439 int ret = 0;
1440 bool new_pe_conn_out, old_pe_conn_out;
1441
1442 /*
1443 * Lookup the scheduler, by 'u->sched_name'
1444 */
1445 if (strcmp(u->sched_name, "none")) {
1446 sched = ip_vs_scheduler_get(u->sched_name);
1447 if (!sched) {
1448 pr_info("Scheduler module ip_vs_%s not found\n",
1449 u->sched_name);
1450 return -ENOENT;
1451 }
1452 }
1453 old_sched = sched;
1454
1455 if (u->pe_name && *u->pe_name) {
1456 pe = ip_vs_pe_getbyname(u->pe_name);
1457 if (pe == NULL) {
1458 pr_info("persistence engine module ip_vs_pe_%s "
1459 "not found\n", u->pe_name);
1460 ret = -ENOENT;
1461 goto out;
1462 }
1463 old_pe = pe;
1464 }
1465
1466 #ifdef CONFIG_IP_VS_IPV6
1467 if (u->af == AF_INET6) {
1468 __u32 plen = (__force __u32) u->netmask;
1469
1470 if (plen < 1 || plen > 128) {
1471 ret = -EINVAL;
1472 goto out;
1473 }
1474 }
1475 #endif
1476
1477 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1478 if (sched != old_sched) {
1479 if (old_sched) {
1480 ip_vs_unbind_scheduler(svc, old_sched);
1481 RCU_INIT_POINTER(svc->scheduler, NULL);
1482 /* Wait all svc->sched_data users */
1483 synchronize_rcu();
1484 }
1485 /* Bind the new scheduler */
1486 if (sched) {
1487 ret = ip_vs_bind_scheduler(svc, sched);
1488 if (ret) {
1489 ip_vs_scheduler_put(sched);
1490 goto out;
1491 }
1492 }
1493 }
1494
1495 /*
1496 * Set the flags and timeout value
1497 */
1498 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1499 svc->timeout = u->timeout * HZ;
1500 svc->netmask = u->netmask;
1501
1502 old_pe = rcu_dereference_protected(svc->pe, 1);
1503 if (pe != old_pe) {
1504 rcu_assign_pointer(svc->pe, pe);
1505 /* check for optional methods in new pe */
1506 new_pe_conn_out = (pe && pe->conn_out) ? true : false;
1507 old_pe_conn_out = (old_pe && old_pe->conn_out) ? true : false;
1508 if (new_pe_conn_out && !old_pe_conn_out)
1509 atomic_inc(&svc->ipvs->conn_out_counter);
1510 if (old_pe_conn_out && !new_pe_conn_out)
1511 atomic_dec(&svc->ipvs->conn_out_counter);
1512 }
1513
1514 out:
1515 ip_vs_scheduler_put(old_sched);
1516 ip_vs_pe_put(old_pe);
1517 return ret;
1518 }
1519
1520 /*
1521 * Delete a service from the service list
1522 * - The service must be unlinked, unlocked and not referenced!
1523 * - We are called under _bh lock
1524 */
__ip_vs_del_service(struct ip_vs_service * svc,bool cleanup)1525 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1526 {
1527 struct ip_vs_dest *dest, *nxt;
1528 struct ip_vs_scheduler *old_sched;
1529 struct ip_vs_pe *old_pe;
1530 struct netns_ipvs *ipvs = svc->ipvs;
1531
1532 if (svc->af == AF_INET) {
1533 ipvs->num_services--;
1534 if (!ipvs->num_services)
1535 ip_vs_unregister_hooks(ipvs, svc->af);
1536 } else if (svc->af == AF_INET6) {
1537 ipvs->num_services6--;
1538 if (!ipvs->num_services6)
1539 ip_vs_unregister_hooks(ipvs, svc->af);
1540 }
1541
1542 ip_vs_stop_estimator(svc->ipvs, &svc->stats);
1543
1544 /* Unbind scheduler */
1545 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1546 ip_vs_unbind_scheduler(svc, old_sched);
1547 ip_vs_scheduler_put(old_sched);
1548
1549 /* Unbind persistence engine, keep svc->pe */
1550 old_pe = rcu_dereference_protected(svc->pe, 1);
1551 if (old_pe && old_pe->conn_out)
1552 atomic_dec(&ipvs->conn_out_counter);
1553 ip_vs_pe_put(old_pe);
1554
1555 /*
1556 * Unlink the whole destination list
1557 */
1558 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1559 __ip_vs_unlink_dest(svc, dest, 0);
1560 __ip_vs_del_dest(svc->ipvs, dest, cleanup);
1561 }
1562
1563 /*
1564 * Update the virtual service counters
1565 */
1566 if (svc->port == FTPPORT)
1567 atomic_dec(&ipvs->ftpsvc_counter);
1568 else if (svc->port == 0)
1569 atomic_dec(&ipvs->nullsvc_counter);
1570
1571 /*
1572 * Free the service if nobody refers to it
1573 */
1574 __ip_vs_svc_put(svc, true);
1575
1576 /* decrease the module use count */
1577 ip_vs_use_count_dec();
1578 }
1579
1580 /*
1581 * Unlink a service from list and try to delete it if its refcnt reached 0
1582 */
ip_vs_unlink_service(struct ip_vs_service * svc,bool cleanup)1583 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1584 {
1585 ip_vs_unregister_conntrack(svc);
1586 /* Hold svc to avoid double release from dest_trash */
1587 atomic_inc(&svc->refcnt);
1588 /*
1589 * Unhash it from the service table
1590 */
1591 ip_vs_svc_unhash(svc);
1592
1593 __ip_vs_del_service(svc, cleanup);
1594 }
1595
1596 /*
1597 * Delete a service from the service list
1598 */
ip_vs_del_service(struct ip_vs_service * svc)1599 static int ip_vs_del_service(struct ip_vs_service *svc)
1600 {
1601 if (svc == NULL)
1602 return -EEXIST;
1603 ip_vs_unlink_service(svc, false);
1604
1605 return 0;
1606 }
1607
1608
1609 /*
1610 * Flush all the virtual services
1611 */
ip_vs_flush(struct netns_ipvs * ipvs,bool cleanup)1612 static int ip_vs_flush(struct netns_ipvs *ipvs, bool cleanup)
1613 {
1614 int idx;
1615 struct ip_vs_service *svc;
1616 struct hlist_node *n;
1617
1618 /*
1619 * Flush the service table hashed by <netns,protocol,addr,port>
1620 */
1621 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1622 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1623 s_list) {
1624 if (svc->ipvs == ipvs)
1625 ip_vs_unlink_service(svc, cleanup);
1626 }
1627 }
1628
1629 /*
1630 * Flush the service table hashed by fwmark
1631 */
1632 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1633 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1634 f_list) {
1635 if (svc->ipvs == ipvs)
1636 ip_vs_unlink_service(svc, cleanup);
1637 }
1638 }
1639
1640 return 0;
1641 }
1642
1643 /*
1644 * Delete service by {netns} in the service table.
1645 * Called by __ip_vs_batch_cleanup()
1646 */
ip_vs_service_nets_cleanup(struct list_head * net_list)1647 void ip_vs_service_nets_cleanup(struct list_head *net_list)
1648 {
1649 struct netns_ipvs *ipvs;
1650 struct net *net;
1651
1652 EnterFunction(2);
1653 /* Check for "full" addressed entries */
1654 mutex_lock(&__ip_vs_mutex);
1655 list_for_each_entry(net, net_list, exit_list) {
1656 ipvs = net_ipvs(net);
1657 ip_vs_flush(ipvs, true);
1658 }
1659 mutex_unlock(&__ip_vs_mutex);
1660 LeaveFunction(2);
1661 }
1662
1663 /* Put all references for device (dst_cache) */
1664 static inline void
ip_vs_forget_dev(struct ip_vs_dest * dest,struct net_device * dev)1665 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1666 {
1667 struct ip_vs_dest_dst *dest_dst;
1668
1669 spin_lock_bh(&dest->dst_lock);
1670 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1671 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1672 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1673 dev->name,
1674 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1675 ntohs(dest->port),
1676 refcount_read(&dest->refcnt));
1677 __ip_vs_dst_cache_reset(dest);
1678 }
1679 spin_unlock_bh(&dest->dst_lock);
1680
1681 }
1682 /* Netdev event receiver
1683 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1684 */
ip_vs_dst_event(struct notifier_block * this,unsigned long event,void * ptr)1685 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1686 void *ptr)
1687 {
1688 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1689 struct net *net = dev_net(dev);
1690 struct netns_ipvs *ipvs = net_ipvs(net);
1691 struct ip_vs_service *svc;
1692 struct ip_vs_dest *dest;
1693 unsigned int idx;
1694
1695 if (event != NETDEV_DOWN || !ipvs)
1696 return NOTIFY_DONE;
1697 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1698 EnterFunction(2);
1699 mutex_lock(&__ip_vs_mutex);
1700 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1701 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1702 if (svc->ipvs == ipvs) {
1703 list_for_each_entry(dest, &svc->destinations,
1704 n_list) {
1705 ip_vs_forget_dev(dest, dev);
1706 }
1707 }
1708 }
1709
1710 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1711 if (svc->ipvs == ipvs) {
1712 list_for_each_entry(dest, &svc->destinations,
1713 n_list) {
1714 ip_vs_forget_dev(dest, dev);
1715 }
1716 }
1717
1718 }
1719 }
1720
1721 spin_lock_bh(&ipvs->dest_trash_lock);
1722 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1723 ip_vs_forget_dev(dest, dev);
1724 }
1725 spin_unlock_bh(&ipvs->dest_trash_lock);
1726 mutex_unlock(&__ip_vs_mutex);
1727 LeaveFunction(2);
1728 return NOTIFY_DONE;
1729 }
1730
1731 /*
1732 * Zero counters in a service or all services
1733 */
ip_vs_zero_service(struct ip_vs_service * svc)1734 static int ip_vs_zero_service(struct ip_vs_service *svc)
1735 {
1736 struct ip_vs_dest *dest;
1737
1738 list_for_each_entry(dest, &svc->destinations, n_list) {
1739 ip_vs_zero_stats(&dest->stats);
1740 }
1741 ip_vs_zero_stats(&svc->stats);
1742 return 0;
1743 }
1744
ip_vs_zero_all(struct netns_ipvs * ipvs)1745 static int ip_vs_zero_all(struct netns_ipvs *ipvs)
1746 {
1747 int idx;
1748 struct ip_vs_service *svc;
1749
1750 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1751 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1752 if (svc->ipvs == ipvs)
1753 ip_vs_zero_service(svc);
1754 }
1755 }
1756
1757 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1758 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1759 if (svc->ipvs == ipvs)
1760 ip_vs_zero_service(svc);
1761 }
1762 }
1763
1764 ip_vs_zero_stats(&ipvs->tot_stats);
1765 return 0;
1766 }
1767
1768 #ifdef CONFIG_SYSCTL
1769
1770 static int
proc_do_defense_mode(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1771 proc_do_defense_mode(struct ctl_table *table, int write,
1772 void *buffer, size_t *lenp, loff_t *ppos)
1773 {
1774 struct netns_ipvs *ipvs = table->extra2;
1775 int *valp = table->data;
1776 int val = *valp;
1777 int rc;
1778
1779 struct ctl_table tmp = {
1780 .data = &val,
1781 .maxlen = sizeof(int),
1782 .mode = table->mode,
1783 };
1784
1785 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1786 if (write && (*valp != val)) {
1787 if (val < 0 || val > 3) {
1788 rc = -EINVAL;
1789 } else {
1790 *valp = val;
1791 update_defense_level(ipvs);
1792 }
1793 }
1794 return rc;
1795 }
1796
1797 static int
proc_do_sync_threshold(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1798 proc_do_sync_threshold(struct ctl_table *table, int write,
1799 void *buffer, size_t *lenp, loff_t *ppos)
1800 {
1801 struct netns_ipvs *ipvs = table->extra2;
1802 int *valp = table->data;
1803 int val[2];
1804 int rc;
1805 struct ctl_table tmp = {
1806 .data = &val,
1807 .maxlen = table->maxlen,
1808 .mode = table->mode,
1809 };
1810
1811 mutex_lock(&ipvs->sync_mutex);
1812 memcpy(val, valp, sizeof(val));
1813 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1814 if (write) {
1815 if (val[0] < 0 || val[1] < 0 ||
1816 (val[0] >= val[1] && val[1]))
1817 rc = -EINVAL;
1818 else
1819 memcpy(valp, val, sizeof(val));
1820 }
1821 mutex_unlock(&ipvs->sync_mutex);
1822 return rc;
1823 }
1824
1825 static int
proc_do_sync_ports(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1826 proc_do_sync_ports(struct ctl_table *table, int write,
1827 void *buffer, size_t *lenp, loff_t *ppos)
1828 {
1829 int *valp = table->data;
1830 int val = *valp;
1831 int rc;
1832
1833 struct ctl_table tmp = {
1834 .data = &val,
1835 .maxlen = sizeof(int),
1836 .mode = table->mode,
1837 };
1838
1839 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1840 if (write && (*valp != val)) {
1841 if (val < 1 || !is_power_of_2(val))
1842 rc = -EINVAL;
1843 else
1844 *valp = val;
1845 }
1846 return rc;
1847 }
1848
1849 /*
1850 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1851 * Do not change order or insert new entries without
1852 * align with netns init in ip_vs_control_net_init()
1853 */
1854
1855 static struct ctl_table vs_vars[] = {
1856 {
1857 .procname = "amemthresh",
1858 .maxlen = sizeof(int),
1859 .mode = 0644,
1860 .proc_handler = proc_dointvec,
1861 },
1862 {
1863 .procname = "am_droprate",
1864 .maxlen = sizeof(int),
1865 .mode = 0644,
1866 .proc_handler = proc_dointvec,
1867 },
1868 {
1869 .procname = "drop_entry",
1870 .maxlen = sizeof(int),
1871 .mode = 0644,
1872 .proc_handler = proc_do_defense_mode,
1873 },
1874 {
1875 .procname = "drop_packet",
1876 .maxlen = sizeof(int),
1877 .mode = 0644,
1878 .proc_handler = proc_do_defense_mode,
1879 },
1880 #ifdef CONFIG_IP_VS_NFCT
1881 {
1882 .procname = "conntrack",
1883 .maxlen = sizeof(int),
1884 .mode = 0644,
1885 .proc_handler = &proc_dointvec,
1886 },
1887 #endif
1888 {
1889 .procname = "secure_tcp",
1890 .maxlen = sizeof(int),
1891 .mode = 0644,
1892 .proc_handler = proc_do_defense_mode,
1893 },
1894 {
1895 .procname = "snat_reroute",
1896 .maxlen = sizeof(int),
1897 .mode = 0644,
1898 .proc_handler = &proc_dointvec,
1899 },
1900 {
1901 .procname = "sync_version",
1902 .maxlen = sizeof(int),
1903 .mode = 0644,
1904 .proc_handler = proc_dointvec_minmax,
1905 .extra1 = SYSCTL_ZERO,
1906 .extra2 = SYSCTL_ONE,
1907 },
1908 {
1909 .procname = "sync_ports",
1910 .maxlen = sizeof(int),
1911 .mode = 0644,
1912 .proc_handler = proc_do_sync_ports,
1913 },
1914 {
1915 .procname = "sync_persist_mode",
1916 .maxlen = sizeof(int),
1917 .mode = 0644,
1918 .proc_handler = proc_dointvec,
1919 },
1920 {
1921 .procname = "sync_qlen_max",
1922 .maxlen = sizeof(unsigned long),
1923 .mode = 0644,
1924 .proc_handler = proc_doulongvec_minmax,
1925 },
1926 {
1927 .procname = "sync_sock_size",
1928 .maxlen = sizeof(int),
1929 .mode = 0644,
1930 .proc_handler = proc_dointvec,
1931 },
1932 {
1933 .procname = "cache_bypass",
1934 .maxlen = sizeof(int),
1935 .mode = 0644,
1936 .proc_handler = proc_dointvec,
1937 },
1938 {
1939 .procname = "expire_nodest_conn",
1940 .maxlen = sizeof(int),
1941 .mode = 0644,
1942 .proc_handler = proc_dointvec,
1943 },
1944 {
1945 .procname = "sloppy_tcp",
1946 .maxlen = sizeof(int),
1947 .mode = 0644,
1948 .proc_handler = proc_dointvec,
1949 },
1950 {
1951 .procname = "sloppy_sctp",
1952 .maxlen = sizeof(int),
1953 .mode = 0644,
1954 .proc_handler = proc_dointvec,
1955 },
1956 {
1957 .procname = "expire_quiescent_template",
1958 .maxlen = sizeof(int),
1959 .mode = 0644,
1960 .proc_handler = proc_dointvec,
1961 },
1962 {
1963 .procname = "sync_threshold",
1964 .maxlen =
1965 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1966 .mode = 0644,
1967 .proc_handler = proc_do_sync_threshold,
1968 },
1969 {
1970 .procname = "sync_refresh_period",
1971 .maxlen = sizeof(int),
1972 .mode = 0644,
1973 .proc_handler = proc_dointvec_jiffies,
1974 },
1975 {
1976 .procname = "sync_retries",
1977 .maxlen = sizeof(int),
1978 .mode = 0644,
1979 .proc_handler = proc_dointvec_minmax,
1980 .extra1 = SYSCTL_ZERO,
1981 .extra2 = SYSCTL_THREE,
1982 },
1983 {
1984 .procname = "nat_icmp_send",
1985 .maxlen = sizeof(int),
1986 .mode = 0644,
1987 .proc_handler = proc_dointvec,
1988 },
1989 {
1990 .procname = "pmtu_disc",
1991 .maxlen = sizeof(int),
1992 .mode = 0644,
1993 .proc_handler = proc_dointvec,
1994 },
1995 {
1996 .procname = "backup_only",
1997 .maxlen = sizeof(int),
1998 .mode = 0644,
1999 .proc_handler = proc_dointvec,
2000 },
2001 {
2002 .procname = "conn_reuse_mode",
2003 .maxlen = sizeof(int),
2004 .mode = 0644,
2005 .proc_handler = proc_dointvec,
2006 },
2007 {
2008 .procname = "schedule_icmp",
2009 .maxlen = sizeof(int),
2010 .mode = 0644,
2011 .proc_handler = proc_dointvec,
2012 },
2013 {
2014 .procname = "ignore_tunneled",
2015 .maxlen = sizeof(int),
2016 .mode = 0644,
2017 .proc_handler = proc_dointvec,
2018 },
2019 {
2020 .procname = "run_estimation",
2021 .maxlen = sizeof(int),
2022 .mode = 0644,
2023 .proc_handler = proc_dointvec,
2024 },
2025 #ifdef CONFIG_IP_VS_DEBUG
2026 {
2027 .procname = "debug_level",
2028 .data = &sysctl_ip_vs_debug_level,
2029 .maxlen = sizeof(int),
2030 .mode = 0644,
2031 .proc_handler = proc_dointvec,
2032 },
2033 #endif
2034 { }
2035 };
2036
2037 #endif
2038
2039 #ifdef CONFIG_PROC_FS
2040
2041 struct ip_vs_iter {
2042 struct seq_net_private p; /* Do not move this, netns depends upon it*/
2043 struct hlist_head *table;
2044 int bucket;
2045 };
2046
2047 /*
2048 * Write the contents of the VS rule table to a PROCfs file.
2049 * (It is kept just for backward compatibility)
2050 */
ip_vs_fwd_name(unsigned int flags)2051 static inline const char *ip_vs_fwd_name(unsigned int flags)
2052 {
2053 switch (flags & IP_VS_CONN_F_FWD_MASK) {
2054 case IP_VS_CONN_F_LOCALNODE:
2055 return "Local";
2056 case IP_VS_CONN_F_TUNNEL:
2057 return "Tunnel";
2058 case IP_VS_CONN_F_DROUTE:
2059 return "Route";
2060 default:
2061 return "Masq";
2062 }
2063 }
2064
2065
2066 /* Get the Nth entry in the two lists */
ip_vs_info_array(struct seq_file * seq,loff_t pos)2067 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
2068 {
2069 struct net *net = seq_file_net(seq);
2070 struct netns_ipvs *ipvs = net_ipvs(net);
2071 struct ip_vs_iter *iter = seq->private;
2072 int idx;
2073 struct ip_vs_service *svc;
2074
2075 /* look in hash by protocol */
2076 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2077 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
2078 if ((svc->ipvs == ipvs) && pos-- == 0) {
2079 iter->table = ip_vs_svc_table;
2080 iter->bucket = idx;
2081 return svc;
2082 }
2083 }
2084 }
2085
2086 /* keep looking in fwmark */
2087 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2088 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
2089 f_list) {
2090 if ((svc->ipvs == ipvs) && pos-- == 0) {
2091 iter->table = ip_vs_svc_fwm_table;
2092 iter->bucket = idx;
2093 return svc;
2094 }
2095 }
2096 }
2097
2098 return NULL;
2099 }
2100
ip_vs_info_seq_start(struct seq_file * seq,loff_t * pos)2101 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
2102 __acquires(RCU)
2103 {
2104 rcu_read_lock();
2105 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
2106 }
2107
2108
ip_vs_info_seq_next(struct seq_file * seq,void * v,loff_t * pos)2109 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2110 {
2111 struct hlist_node *e;
2112 struct ip_vs_iter *iter;
2113 struct ip_vs_service *svc;
2114
2115 ++*pos;
2116 if (v == SEQ_START_TOKEN)
2117 return ip_vs_info_array(seq,0);
2118
2119 svc = v;
2120 iter = seq->private;
2121
2122 if (iter->table == ip_vs_svc_table) {
2123 /* next service in table hashed by protocol */
2124 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
2125 if (e)
2126 return hlist_entry(e, struct ip_vs_service, s_list);
2127
2128 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2129 hlist_for_each_entry_rcu(svc,
2130 &ip_vs_svc_table[iter->bucket],
2131 s_list) {
2132 return svc;
2133 }
2134 }
2135
2136 iter->table = ip_vs_svc_fwm_table;
2137 iter->bucket = -1;
2138 goto scan_fwmark;
2139 }
2140
2141 /* next service in hashed by fwmark */
2142 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
2143 if (e)
2144 return hlist_entry(e, struct ip_vs_service, f_list);
2145
2146 scan_fwmark:
2147 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2148 hlist_for_each_entry_rcu(svc,
2149 &ip_vs_svc_fwm_table[iter->bucket],
2150 f_list)
2151 return svc;
2152 }
2153
2154 return NULL;
2155 }
2156
ip_vs_info_seq_stop(struct seq_file * seq,void * v)2157 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
2158 __releases(RCU)
2159 {
2160 rcu_read_unlock();
2161 }
2162
2163
ip_vs_info_seq_show(struct seq_file * seq,void * v)2164 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2165 {
2166 if (v == SEQ_START_TOKEN) {
2167 seq_printf(seq,
2168 "IP Virtual Server version %d.%d.%d (size=%d)\n",
2169 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2170 seq_puts(seq,
2171 "Prot LocalAddress:Port Scheduler Flags\n");
2172 seq_puts(seq,
2173 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2174 } else {
2175 struct net *net = seq_file_net(seq);
2176 struct netns_ipvs *ipvs = net_ipvs(net);
2177 const struct ip_vs_service *svc = v;
2178 const struct ip_vs_iter *iter = seq->private;
2179 const struct ip_vs_dest *dest;
2180 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
2181 char *sched_name = sched ? sched->name : "none";
2182
2183 if (svc->ipvs != ipvs)
2184 return 0;
2185 if (iter->table == ip_vs_svc_table) {
2186 #ifdef CONFIG_IP_VS_IPV6
2187 if (svc->af == AF_INET6)
2188 seq_printf(seq, "%s [%pI6]:%04X %s ",
2189 ip_vs_proto_name(svc->protocol),
2190 &svc->addr.in6,
2191 ntohs(svc->port),
2192 sched_name);
2193 else
2194 #endif
2195 seq_printf(seq, "%s %08X:%04X %s %s ",
2196 ip_vs_proto_name(svc->protocol),
2197 ntohl(svc->addr.ip),
2198 ntohs(svc->port),
2199 sched_name,
2200 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2201 } else {
2202 seq_printf(seq, "FWM %08X %s %s",
2203 svc->fwmark, sched_name,
2204 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2205 }
2206
2207 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2208 seq_printf(seq, "persistent %d %08X\n",
2209 svc->timeout,
2210 ntohl(svc->netmask));
2211 else
2212 seq_putc(seq, '\n');
2213
2214 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
2215 #ifdef CONFIG_IP_VS_IPV6
2216 if (dest->af == AF_INET6)
2217 seq_printf(seq,
2218 " -> [%pI6]:%04X"
2219 " %-7s %-6d %-10d %-10d\n",
2220 &dest->addr.in6,
2221 ntohs(dest->port),
2222 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2223 atomic_read(&dest->weight),
2224 atomic_read(&dest->activeconns),
2225 atomic_read(&dest->inactconns));
2226 else
2227 #endif
2228 seq_printf(seq,
2229 " -> %08X:%04X "
2230 "%-7s %-6d %-10d %-10d\n",
2231 ntohl(dest->addr.ip),
2232 ntohs(dest->port),
2233 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2234 atomic_read(&dest->weight),
2235 atomic_read(&dest->activeconns),
2236 atomic_read(&dest->inactconns));
2237
2238 }
2239 }
2240 return 0;
2241 }
2242
2243 static const struct seq_operations ip_vs_info_seq_ops = {
2244 .start = ip_vs_info_seq_start,
2245 .next = ip_vs_info_seq_next,
2246 .stop = ip_vs_info_seq_stop,
2247 .show = ip_vs_info_seq_show,
2248 };
2249
ip_vs_stats_show(struct seq_file * seq,void * v)2250 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2251 {
2252 struct net *net = seq_file_single_net(seq);
2253 struct ip_vs_kstats show;
2254
2255 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2256 seq_puts(seq,
2257 " Total Incoming Outgoing Incoming Outgoing\n");
2258 seq_puts(seq,
2259 " Conns Packets Packets Bytes Bytes\n");
2260
2261 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2262 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2263 (unsigned long long)show.conns,
2264 (unsigned long long)show.inpkts,
2265 (unsigned long long)show.outpkts,
2266 (unsigned long long)show.inbytes,
2267 (unsigned long long)show.outbytes);
2268
2269 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2270 seq_puts(seq,
2271 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2272 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2273 (unsigned long long)show.cps,
2274 (unsigned long long)show.inpps,
2275 (unsigned long long)show.outpps,
2276 (unsigned long long)show.inbps,
2277 (unsigned long long)show.outbps);
2278
2279 return 0;
2280 }
2281
ip_vs_stats_percpu_show(struct seq_file * seq,void * v)2282 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2283 {
2284 struct net *net = seq_file_single_net(seq);
2285 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2286 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2287 struct ip_vs_kstats kstats;
2288 int i;
2289
2290 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2291 seq_puts(seq,
2292 " Total Incoming Outgoing Incoming Outgoing\n");
2293 seq_puts(seq,
2294 "CPU Conns Packets Packets Bytes Bytes\n");
2295
2296 for_each_possible_cpu(i) {
2297 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2298 unsigned int start;
2299 u64 conns, inpkts, outpkts, inbytes, outbytes;
2300
2301 do {
2302 start = u64_stats_fetch_begin_irq(&u->syncp);
2303 conns = u64_stats_read(&u->cnt.conns);
2304 inpkts = u64_stats_read(&u->cnt.inpkts);
2305 outpkts = u64_stats_read(&u->cnt.outpkts);
2306 inbytes = u64_stats_read(&u->cnt.inbytes);
2307 outbytes = u64_stats_read(&u->cnt.outbytes);
2308 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2309
2310 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2311 i, (u64)conns, (u64)inpkts,
2312 (u64)outpkts, (u64)inbytes,
2313 (u64)outbytes);
2314 }
2315
2316 ip_vs_copy_stats(&kstats, tot_stats);
2317
2318 seq_printf(seq, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2319 (unsigned long long)kstats.conns,
2320 (unsigned long long)kstats.inpkts,
2321 (unsigned long long)kstats.outpkts,
2322 (unsigned long long)kstats.inbytes,
2323 (unsigned long long)kstats.outbytes);
2324
2325 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2326 seq_puts(seq,
2327 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2328 seq_printf(seq, " %8LX %8LX %8LX %16LX %16LX\n",
2329 kstats.cps,
2330 kstats.inpps,
2331 kstats.outpps,
2332 kstats.inbps,
2333 kstats.outbps);
2334
2335 return 0;
2336 }
2337 #endif
2338
2339 /*
2340 * Set timeout values for tcp tcpfin udp in the timeout_table.
2341 */
ip_vs_set_timeout(struct netns_ipvs * ipvs,struct ip_vs_timeout_user * u)2342 static int ip_vs_set_timeout(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2343 {
2344 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2345 struct ip_vs_proto_data *pd;
2346 #endif
2347
2348 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2349 u->tcp_timeout,
2350 u->tcp_fin_timeout,
2351 u->udp_timeout);
2352
2353 #ifdef CONFIG_IP_VS_PROTO_TCP
2354 if (u->tcp_timeout < 0 || u->tcp_timeout > (INT_MAX / HZ) ||
2355 u->tcp_fin_timeout < 0 || u->tcp_fin_timeout > (INT_MAX / HZ)) {
2356 return -EINVAL;
2357 }
2358 #endif
2359
2360 #ifdef CONFIG_IP_VS_PROTO_UDP
2361 if (u->udp_timeout < 0 || u->udp_timeout > (INT_MAX / HZ))
2362 return -EINVAL;
2363 #endif
2364
2365 #ifdef CONFIG_IP_VS_PROTO_TCP
2366 if (u->tcp_timeout) {
2367 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2368 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2369 = u->tcp_timeout * HZ;
2370 }
2371
2372 if (u->tcp_fin_timeout) {
2373 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2374 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2375 = u->tcp_fin_timeout * HZ;
2376 }
2377 #endif
2378
2379 #ifdef CONFIG_IP_VS_PROTO_UDP
2380 if (u->udp_timeout) {
2381 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2382 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2383 = u->udp_timeout * HZ;
2384 }
2385 #endif
2386 return 0;
2387 }
2388
2389 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2390
2391 struct ip_vs_svcdest_user {
2392 struct ip_vs_service_user s;
2393 struct ip_vs_dest_user d;
2394 };
2395
2396 static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2397 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2398 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2399 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2400 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2401 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2402 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2403 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2404 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2405 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2406 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2407 };
2408
2409 union ip_vs_set_arglen {
2410 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2411 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2412 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2413 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2414 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2415 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2416 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2417 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2418 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2419 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2420 };
2421
2422 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2423
ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern * usvc,struct ip_vs_service_user * usvc_compat)2424 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2425 struct ip_vs_service_user *usvc_compat)
2426 {
2427 memset(usvc, 0, sizeof(*usvc));
2428
2429 usvc->af = AF_INET;
2430 usvc->protocol = usvc_compat->protocol;
2431 usvc->addr.ip = usvc_compat->addr;
2432 usvc->port = usvc_compat->port;
2433 usvc->fwmark = usvc_compat->fwmark;
2434
2435 /* Deep copy of sched_name is not needed here */
2436 usvc->sched_name = usvc_compat->sched_name;
2437
2438 usvc->flags = usvc_compat->flags;
2439 usvc->timeout = usvc_compat->timeout;
2440 usvc->netmask = usvc_compat->netmask;
2441 }
2442
ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern * udest,struct ip_vs_dest_user * udest_compat)2443 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2444 struct ip_vs_dest_user *udest_compat)
2445 {
2446 memset(udest, 0, sizeof(*udest));
2447
2448 udest->addr.ip = udest_compat->addr;
2449 udest->port = udest_compat->port;
2450 udest->conn_flags = udest_compat->conn_flags;
2451 udest->weight = udest_compat->weight;
2452 udest->u_threshold = udest_compat->u_threshold;
2453 udest->l_threshold = udest_compat->l_threshold;
2454 udest->af = AF_INET;
2455 udest->tun_type = IP_VS_CONN_F_TUNNEL_TYPE_IPIP;
2456 }
2457
2458 static int
do_ip_vs_set_ctl(struct sock * sk,int cmd,sockptr_t ptr,unsigned int len)2459 do_ip_vs_set_ctl(struct sock *sk, int cmd, sockptr_t ptr, unsigned int len)
2460 {
2461 struct net *net = sock_net(sk);
2462 int ret;
2463 unsigned char arg[MAX_SET_ARGLEN];
2464 struct ip_vs_service_user *usvc_compat;
2465 struct ip_vs_service_user_kern usvc;
2466 struct ip_vs_service *svc;
2467 struct ip_vs_dest_user *udest_compat;
2468 struct ip_vs_dest_user_kern udest;
2469 struct netns_ipvs *ipvs = net_ipvs(net);
2470
2471 BUILD_BUG_ON(sizeof(arg) > 255);
2472 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2473 return -EPERM;
2474
2475 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2476 return -EINVAL;
2477 if (len != set_arglen[CMDID(cmd)]) {
2478 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2479 len, set_arglen[CMDID(cmd)]);
2480 return -EINVAL;
2481 }
2482
2483 if (copy_from_sockptr(arg, ptr, len) != 0)
2484 return -EFAULT;
2485
2486 /* Handle daemons since they have another lock */
2487 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2488 cmd == IP_VS_SO_SET_STOPDAEMON) {
2489 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2490
2491 if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2492 struct ipvs_sync_daemon_cfg cfg;
2493
2494 memset(&cfg, 0, sizeof(cfg));
2495 ret = -EINVAL;
2496 if (strscpy(cfg.mcast_ifn, dm->mcast_ifn,
2497 sizeof(cfg.mcast_ifn)) <= 0)
2498 return ret;
2499 cfg.syncid = dm->syncid;
2500 ret = start_sync_thread(ipvs, &cfg, dm->state);
2501 } else {
2502 ret = stop_sync_thread(ipvs, dm->state);
2503 }
2504 return ret;
2505 }
2506
2507 mutex_lock(&__ip_vs_mutex);
2508 if (cmd == IP_VS_SO_SET_FLUSH) {
2509 /* Flush the virtual service */
2510 ret = ip_vs_flush(ipvs, false);
2511 goto out_unlock;
2512 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2513 /* Set timeout values for (tcp tcpfin udp) */
2514 ret = ip_vs_set_timeout(ipvs, (struct ip_vs_timeout_user *)arg);
2515 goto out_unlock;
2516 } else if (!len) {
2517 /* No more commands with len == 0 below */
2518 ret = -EINVAL;
2519 goto out_unlock;
2520 }
2521
2522 usvc_compat = (struct ip_vs_service_user *)arg;
2523 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2524
2525 /* We only use the new structs internally, so copy userspace compat
2526 * structs to extended internal versions */
2527 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2528 ip_vs_copy_udest_compat(&udest, udest_compat);
2529
2530 if (cmd == IP_VS_SO_SET_ZERO) {
2531 /* if no service address is set, zero counters in all */
2532 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2533 ret = ip_vs_zero_all(ipvs);
2534 goto out_unlock;
2535 }
2536 }
2537
2538 if ((cmd == IP_VS_SO_SET_ADD || cmd == IP_VS_SO_SET_EDIT) &&
2539 strnlen(usvc.sched_name, IP_VS_SCHEDNAME_MAXLEN) ==
2540 IP_VS_SCHEDNAME_MAXLEN) {
2541 ret = -EINVAL;
2542 goto out_unlock;
2543 }
2544
2545 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2546 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2547 usvc.protocol != IPPROTO_SCTP) {
2548 pr_err("set_ctl: invalid protocol: %d %pI4:%d\n",
2549 usvc.protocol, &usvc.addr.ip,
2550 ntohs(usvc.port));
2551 ret = -EFAULT;
2552 goto out_unlock;
2553 }
2554
2555 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2556 rcu_read_lock();
2557 if (usvc.fwmark == 0)
2558 svc = __ip_vs_service_find(ipvs, usvc.af, usvc.protocol,
2559 &usvc.addr, usvc.port);
2560 else
2561 svc = __ip_vs_svc_fwm_find(ipvs, usvc.af, usvc.fwmark);
2562 rcu_read_unlock();
2563
2564 if (cmd != IP_VS_SO_SET_ADD
2565 && (svc == NULL || svc->protocol != usvc.protocol)) {
2566 ret = -ESRCH;
2567 goto out_unlock;
2568 }
2569
2570 switch (cmd) {
2571 case IP_VS_SO_SET_ADD:
2572 if (svc != NULL)
2573 ret = -EEXIST;
2574 else
2575 ret = ip_vs_add_service(ipvs, &usvc, &svc);
2576 break;
2577 case IP_VS_SO_SET_EDIT:
2578 ret = ip_vs_edit_service(svc, &usvc);
2579 break;
2580 case IP_VS_SO_SET_DEL:
2581 ret = ip_vs_del_service(svc);
2582 if (!ret)
2583 goto out_unlock;
2584 break;
2585 case IP_VS_SO_SET_ZERO:
2586 ret = ip_vs_zero_service(svc);
2587 break;
2588 case IP_VS_SO_SET_ADDDEST:
2589 ret = ip_vs_add_dest(svc, &udest);
2590 break;
2591 case IP_VS_SO_SET_EDITDEST:
2592 ret = ip_vs_edit_dest(svc, &udest);
2593 break;
2594 case IP_VS_SO_SET_DELDEST:
2595 ret = ip_vs_del_dest(svc, &udest);
2596 }
2597
2598 out_unlock:
2599 mutex_unlock(&__ip_vs_mutex);
2600 return ret;
2601 }
2602
2603
2604 static void
ip_vs_copy_service(struct ip_vs_service_entry * dst,struct ip_vs_service * src)2605 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2606 {
2607 struct ip_vs_scheduler *sched;
2608 struct ip_vs_kstats kstats;
2609 char *sched_name;
2610
2611 sched = rcu_dereference_protected(src->scheduler, 1);
2612 sched_name = sched ? sched->name : "none";
2613 dst->protocol = src->protocol;
2614 dst->addr = src->addr.ip;
2615 dst->port = src->port;
2616 dst->fwmark = src->fwmark;
2617 strscpy(dst->sched_name, sched_name, sizeof(dst->sched_name));
2618 dst->flags = src->flags;
2619 dst->timeout = src->timeout / HZ;
2620 dst->netmask = src->netmask;
2621 dst->num_dests = src->num_dests;
2622 ip_vs_copy_stats(&kstats, &src->stats);
2623 ip_vs_export_stats_user(&dst->stats, &kstats);
2624 }
2625
2626 static inline int
__ip_vs_get_service_entries(struct netns_ipvs * ipvs,const struct ip_vs_get_services * get,struct ip_vs_get_services __user * uptr)2627 __ip_vs_get_service_entries(struct netns_ipvs *ipvs,
2628 const struct ip_vs_get_services *get,
2629 struct ip_vs_get_services __user *uptr)
2630 {
2631 int idx, count=0;
2632 struct ip_vs_service *svc;
2633 struct ip_vs_service_entry entry;
2634 int ret = 0;
2635
2636 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2637 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2638 /* Only expose IPv4 entries to old interface */
2639 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2640 continue;
2641
2642 if (count >= get->num_services)
2643 goto out;
2644 memset(&entry, 0, sizeof(entry));
2645 ip_vs_copy_service(&entry, svc);
2646 if (copy_to_user(&uptr->entrytable[count],
2647 &entry, sizeof(entry))) {
2648 ret = -EFAULT;
2649 goto out;
2650 }
2651 count++;
2652 }
2653 }
2654
2655 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2656 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2657 /* Only expose IPv4 entries to old interface */
2658 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2659 continue;
2660
2661 if (count >= get->num_services)
2662 goto out;
2663 memset(&entry, 0, sizeof(entry));
2664 ip_vs_copy_service(&entry, svc);
2665 if (copy_to_user(&uptr->entrytable[count],
2666 &entry, sizeof(entry))) {
2667 ret = -EFAULT;
2668 goto out;
2669 }
2670 count++;
2671 }
2672 }
2673 out:
2674 return ret;
2675 }
2676
2677 static inline int
__ip_vs_get_dest_entries(struct netns_ipvs * ipvs,const struct ip_vs_get_dests * get,struct ip_vs_get_dests __user * uptr)2678 __ip_vs_get_dest_entries(struct netns_ipvs *ipvs, const struct ip_vs_get_dests *get,
2679 struct ip_vs_get_dests __user *uptr)
2680 {
2681 struct ip_vs_service *svc;
2682 union nf_inet_addr addr = { .ip = get->addr };
2683 int ret = 0;
2684
2685 rcu_read_lock();
2686 if (get->fwmark)
2687 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, get->fwmark);
2688 else
2689 svc = __ip_vs_service_find(ipvs, AF_INET, get->protocol, &addr,
2690 get->port);
2691 rcu_read_unlock();
2692
2693 if (svc) {
2694 int count = 0;
2695 struct ip_vs_dest *dest;
2696 struct ip_vs_dest_entry entry;
2697 struct ip_vs_kstats kstats;
2698
2699 memset(&entry, 0, sizeof(entry));
2700 list_for_each_entry(dest, &svc->destinations, n_list) {
2701 if (count >= get->num_dests)
2702 break;
2703
2704 /* Cannot expose heterogeneous members via sockopt
2705 * interface
2706 */
2707 if (dest->af != svc->af)
2708 continue;
2709
2710 entry.addr = dest->addr.ip;
2711 entry.port = dest->port;
2712 entry.conn_flags = atomic_read(&dest->conn_flags);
2713 entry.weight = atomic_read(&dest->weight);
2714 entry.u_threshold = dest->u_threshold;
2715 entry.l_threshold = dest->l_threshold;
2716 entry.activeconns = atomic_read(&dest->activeconns);
2717 entry.inactconns = atomic_read(&dest->inactconns);
2718 entry.persistconns = atomic_read(&dest->persistconns);
2719 ip_vs_copy_stats(&kstats, &dest->stats);
2720 ip_vs_export_stats_user(&entry.stats, &kstats);
2721 if (copy_to_user(&uptr->entrytable[count],
2722 &entry, sizeof(entry))) {
2723 ret = -EFAULT;
2724 break;
2725 }
2726 count++;
2727 }
2728 } else
2729 ret = -ESRCH;
2730 return ret;
2731 }
2732
2733 static inline void
__ip_vs_get_timeouts(struct netns_ipvs * ipvs,struct ip_vs_timeout_user * u)2734 __ip_vs_get_timeouts(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2735 {
2736 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2737 struct ip_vs_proto_data *pd;
2738 #endif
2739
2740 memset(u, 0, sizeof (*u));
2741
2742 #ifdef CONFIG_IP_VS_PROTO_TCP
2743 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2744 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2745 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2746 #endif
2747 #ifdef CONFIG_IP_VS_PROTO_UDP
2748 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2749 u->udp_timeout =
2750 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2751 #endif
2752 }
2753
2754 static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2755 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2756 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2757 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2758 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2759 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2760 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2761 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
2762 };
2763
2764 union ip_vs_get_arglen {
2765 char field_IP_VS_SO_GET_VERSION[64];
2766 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
2767 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
2768 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
2769 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
2770 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
2771 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
2772 };
2773
2774 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2775
2776 static int
do_ip_vs_get_ctl(struct sock * sk,int cmd,void __user * user,int * len)2777 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2778 {
2779 unsigned char arg[MAX_GET_ARGLEN];
2780 int ret = 0;
2781 unsigned int copylen;
2782 struct net *net = sock_net(sk);
2783 struct netns_ipvs *ipvs = net_ipvs(net);
2784
2785 BUG_ON(!net);
2786 BUILD_BUG_ON(sizeof(arg) > 255);
2787 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2788 return -EPERM;
2789
2790 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2791 return -EINVAL;
2792
2793 copylen = get_arglen[CMDID(cmd)];
2794 if (*len < (int) copylen) {
2795 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
2796 return -EINVAL;
2797 }
2798
2799 if (copy_from_user(arg, user, copylen) != 0)
2800 return -EFAULT;
2801 /*
2802 * Handle daemons first since it has its own locking
2803 */
2804 if (cmd == IP_VS_SO_GET_DAEMON) {
2805 struct ip_vs_daemon_user d[2];
2806
2807 memset(&d, 0, sizeof(d));
2808 mutex_lock(&ipvs->sync_mutex);
2809 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2810 d[0].state = IP_VS_STATE_MASTER;
2811 strscpy(d[0].mcast_ifn, ipvs->mcfg.mcast_ifn,
2812 sizeof(d[0].mcast_ifn));
2813 d[0].syncid = ipvs->mcfg.syncid;
2814 }
2815 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2816 d[1].state = IP_VS_STATE_BACKUP;
2817 strscpy(d[1].mcast_ifn, ipvs->bcfg.mcast_ifn,
2818 sizeof(d[1].mcast_ifn));
2819 d[1].syncid = ipvs->bcfg.syncid;
2820 }
2821 if (copy_to_user(user, &d, sizeof(d)) != 0)
2822 ret = -EFAULT;
2823 mutex_unlock(&ipvs->sync_mutex);
2824 return ret;
2825 }
2826
2827 mutex_lock(&__ip_vs_mutex);
2828 switch (cmd) {
2829 case IP_VS_SO_GET_VERSION:
2830 {
2831 char buf[64];
2832
2833 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2834 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2835 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2836 ret = -EFAULT;
2837 goto out;
2838 }
2839 *len = strlen(buf)+1;
2840 }
2841 break;
2842
2843 case IP_VS_SO_GET_INFO:
2844 {
2845 struct ip_vs_getinfo info;
2846 info.version = IP_VS_VERSION_CODE;
2847 info.size = ip_vs_conn_tab_size;
2848 info.num_services = ipvs->num_services;
2849 if (copy_to_user(user, &info, sizeof(info)) != 0)
2850 ret = -EFAULT;
2851 }
2852 break;
2853
2854 case IP_VS_SO_GET_SERVICES:
2855 {
2856 struct ip_vs_get_services *get;
2857 int size;
2858
2859 get = (struct ip_vs_get_services *)arg;
2860 size = struct_size(get, entrytable, get->num_services);
2861 if (*len != size) {
2862 pr_err("length: %u != %u\n", *len, size);
2863 ret = -EINVAL;
2864 goto out;
2865 }
2866 ret = __ip_vs_get_service_entries(ipvs, get, user);
2867 }
2868 break;
2869
2870 case IP_VS_SO_GET_SERVICE:
2871 {
2872 struct ip_vs_service_entry *entry;
2873 struct ip_vs_service *svc;
2874 union nf_inet_addr addr;
2875
2876 entry = (struct ip_vs_service_entry *)arg;
2877 addr.ip = entry->addr;
2878 rcu_read_lock();
2879 if (entry->fwmark)
2880 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, entry->fwmark);
2881 else
2882 svc = __ip_vs_service_find(ipvs, AF_INET,
2883 entry->protocol, &addr,
2884 entry->port);
2885 rcu_read_unlock();
2886 if (svc) {
2887 ip_vs_copy_service(entry, svc);
2888 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2889 ret = -EFAULT;
2890 } else
2891 ret = -ESRCH;
2892 }
2893 break;
2894
2895 case IP_VS_SO_GET_DESTS:
2896 {
2897 struct ip_vs_get_dests *get;
2898 int size;
2899
2900 get = (struct ip_vs_get_dests *)arg;
2901 size = struct_size(get, entrytable, get->num_dests);
2902 if (*len != size) {
2903 pr_err("length: %u != %u\n", *len, size);
2904 ret = -EINVAL;
2905 goto out;
2906 }
2907 ret = __ip_vs_get_dest_entries(ipvs, get, user);
2908 }
2909 break;
2910
2911 case IP_VS_SO_GET_TIMEOUT:
2912 {
2913 struct ip_vs_timeout_user t;
2914
2915 __ip_vs_get_timeouts(ipvs, &t);
2916 if (copy_to_user(user, &t, sizeof(t)) != 0)
2917 ret = -EFAULT;
2918 }
2919 break;
2920
2921 default:
2922 ret = -EINVAL;
2923 }
2924
2925 out:
2926 mutex_unlock(&__ip_vs_mutex);
2927 return ret;
2928 }
2929
2930
2931 static struct nf_sockopt_ops ip_vs_sockopts = {
2932 .pf = PF_INET,
2933 .set_optmin = IP_VS_BASE_CTL,
2934 .set_optmax = IP_VS_SO_SET_MAX+1,
2935 .set = do_ip_vs_set_ctl,
2936 .get_optmin = IP_VS_BASE_CTL,
2937 .get_optmax = IP_VS_SO_GET_MAX+1,
2938 .get = do_ip_vs_get_ctl,
2939 .owner = THIS_MODULE,
2940 };
2941
2942 /*
2943 * Generic Netlink interface
2944 */
2945
2946 /* IPVS genetlink family */
2947 static struct genl_family ip_vs_genl_family;
2948
2949 /* Policy used for first-level command attributes */
2950 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2951 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2952 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2953 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2954 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2955 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2956 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2957 };
2958
2959 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2960 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2961 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2962 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2963 .len = IP_VS_IFNAME_MAXLEN - 1 },
2964 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2965 [IPVS_DAEMON_ATTR_SYNC_MAXLEN] = { .type = NLA_U16 },
2966 [IPVS_DAEMON_ATTR_MCAST_GROUP] = { .type = NLA_U32 },
2967 [IPVS_DAEMON_ATTR_MCAST_GROUP6] = { .len = sizeof(struct in6_addr) },
2968 [IPVS_DAEMON_ATTR_MCAST_PORT] = { .type = NLA_U16 },
2969 [IPVS_DAEMON_ATTR_MCAST_TTL] = { .type = NLA_U8 },
2970 };
2971
2972 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2973 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2974 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2975 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2976 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2977 .len = sizeof(union nf_inet_addr) },
2978 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2979 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2980 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2981 .len = IP_VS_SCHEDNAME_MAXLEN - 1 },
2982 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2983 .len = IP_VS_PENAME_MAXLEN },
2984 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2985 .len = sizeof(struct ip_vs_flags) },
2986 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2987 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2988 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2989 };
2990
2991 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2992 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2993 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2994 .len = sizeof(union nf_inet_addr) },
2995 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2996 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2997 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2998 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2999 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
3000 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
3001 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
3002 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
3003 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
3004 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
3005 [IPVS_DEST_ATTR_TUN_TYPE] = { .type = NLA_U8 },
3006 [IPVS_DEST_ATTR_TUN_PORT] = { .type = NLA_U16 },
3007 [IPVS_DEST_ATTR_TUN_FLAGS] = { .type = NLA_U16 },
3008 };
3009
ip_vs_genl_fill_stats(struct sk_buff * skb,int container_type,struct ip_vs_kstats * kstats)3010 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
3011 struct ip_vs_kstats *kstats)
3012 {
3013 struct nlattr *nl_stats = nla_nest_start_noflag(skb, container_type);
3014
3015 if (!nl_stats)
3016 return -EMSGSIZE;
3017
3018 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
3019 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
3020 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
3021 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
3022 IPVS_STATS_ATTR_PAD) ||
3023 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
3024 IPVS_STATS_ATTR_PAD) ||
3025 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
3026 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
3027 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
3028 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
3029 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
3030 goto nla_put_failure;
3031 nla_nest_end(skb, nl_stats);
3032
3033 return 0;
3034
3035 nla_put_failure:
3036 nla_nest_cancel(skb, nl_stats);
3037 return -EMSGSIZE;
3038 }
3039
ip_vs_genl_fill_stats64(struct sk_buff * skb,int container_type,struct ip_vs_kstats * kstats)3040 static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
3041 struct ip_vs_kstats *kstats)
3042 {
3043 struct nlattr *nl_stats = nla_nest_start_noflag(skb, container_type);
3044
3045 if (!nl_stats)
3046 return -EMSGSIZE;
3047
3048 if (nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CONNS, kstats->conns,
3049 IPVS_STATS_ATTR_PAD) ||
3050 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts,
3051 IPVS_STATS_ATTR_PAD) ||
3052 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts,
3053 IPVS_STATS_ATTR_PAD) ||
3054 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
3055 IPVS_STATS_ATTR_PAD) ||
3056 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
3057 IPVS_STATS_ATTR_PAD) ||
3058 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CPS, kstats->cps,
3059 IPVS_STATS_ATTR_PAD) ||
3060 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps,
3061 IPVS_STATS_ATTR_PAD) ||
3062 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps,
3063 IPVS_STATS_ATTR_PAD) ||
3064 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps,
3065 IPVS_STATS_ATTR_PAD) ||
3066 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps,
3067 IPVS_STATS_ATTR_PAD))
3068 goto nla_put_failure;
3069 nla_nest_end(skb, nl_stats);
3070
3071 return 0;
3072
3073 nla_put_failure:
3074 nla_nest_cancel(skb, nl_stats);
3075 return -EMSGSIZE;
3076 }
3077
ip_vs_genl_fill_service(struct sk_buff * skb,struct ip_vs_service * svc)3078 static int ip_vs_genl_fill_service(struct sk_buff *skb,
3079 struct ip_vs_service *svc)
3080 {
3081 struct ip_vs_scheduler *sched;
3082 struct ip_vs_pe *pe;
3083 struct nlattr *nl_service;
3084 struct ip_vs_flags flags = { .flags = svc->flags,
3085 .mask = ~0 };
3086 struct ip_vs_kstats kstats;
3087 char *sched_name;
3088
3089 nl_service = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_SERVICE);
3090 if (!nl_service)
3091 return -EMSGSIZE;
3092
3093 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
3094 goto nla_put_failure;
3095 if (svc->fwmark) {
3096 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
3097 goto nla_put_failure;
3098 } else {
3099 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
3100 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
3101 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
3102 goto nla_put_failure;
3103 }
3104
3105 sched = rcu_dereference_protected(svc->scheduler, 1);
3106 sched_name = sched ? sched->name : "none";
3107 pe = rcu_dereference_protected(svc->pe, 1);
3108 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched_name) ||
3109 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
3110 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
3111 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
3112 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
3113 goto nla_put_failure;
3114 ip_vs_copy_stats(&kstats, &svc->stats);
3115 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
3116 goto nla_put_failure;
3117 if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
3118 goto nla_put_failure;
3119
3120 nla_nest_end(skb, nl_service);
3121
3122 return 0;
3123
3124 nla_put_failure:
3125 nla_nest_cancel(skb, nl_service);
3126 return -EMSGSIZE;
3127 }
3128
ip_vs_genl_dump_service(struct sk_buff * skb,struct ip_vs_service * svc,struct netlink_callback * cb)3129 static int ip_vs_genl_dump_service(struct sk_buff *skb,
3130 struct ip_vs_service *svc,
3131 struct netlink_callback *cb)
3132 {
3133 void *hdr;
3134
3135 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3136 &ip_vs_genl_family, NLM_F_MULTI,
3137 IPVS_CMD_NEW_SERVICE);
3138 if (!hdr)
3139 return -EMSGSIZE;
3140
3141 if (ip_vs_genl_fill_service(skb, svc) < 0)
3142 goto nla_put_failure;
3143
3144 genlmsg_end(skb, hdr);
3145 return 0;
3146
3147 nla_put_failure:
3148 genlmsg_cancel(skb, hdr);
3149 return -EMSGSIZE;
3150 }
3151
ip_vs_genl_dump_services(struct sk_buff * skb,struct netlink_callback * cb)3152 static int ip_vs_genl_dump_services(struct sk_buff *skb,
3153 struct netlink_callback *cb)
3154 {
3155 int idx = 0, i;
3156 int start = cb->args[0];
3157 struct ip_vs_service *svc;
3158 struct net *net = sock_net(skb->sk);
3159 struct netns_ipvs *ipvs = net_ipvs(net);
3160
3161 mutex_lock(&__ip_vs_mutex);
3162 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3163 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
3164 if (++idx <= start || (svc->ipvs != ipvs))
3165 continue;
3166 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3167 idx--;
3168 goto nla_put_failure;
3169 }
3170 }
3171 }
3172
3173 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3174 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
3175 if (++idx <= start || (svc->ipvs != ipvs))
3176 continue;
3177 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3178 idx--;
3179 goto nla_put_failure;
3180 }
3181 }
3182 }
3183
3184 nla_put_failure:
3185 mutex_unlock(&__ip_vs_mutex);
3186 cb->args[0] = idx;
3187
3188 return skb->len;
3189 }
3190
ip_vs_is_af_valid(int af)3191 static bool ip_vs_is_af_valid(int af)
3192 {
3193 if (af == AF_INET)
3194 return true;
3195 #ifdef CONFIG_IP_VS_IPV6
3196 if (af == AF_INET6 && ipv6_mod_enabled())
3197 return true;
3198 #endif
3199 return false;
3200 }
3201
ip_vs_genl_parse_service(struct netns_ipvs * ipvs,struct ip_vs_service_user_kern * usvc,struct nlattr * nla,bool full_entry,struct ip_vs_service ** ret_svc)3202 static int ip_vs_genl_parse_service(struct netns_ipvs *ipvs,
3203 struct ip_vs_service_user_kern *usvc,
3204 struct nlattr *nla, bool full_entry,
3205 struct ip_vs_service **ret_svc)
3206 {
3207 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3208 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
3209 struct ip_vs_service *svc;
3210
3211 /* Parse mandatory identifying service fields first */
3212 if (nla == NULL ||
3213 nla_parse_nested_deprecated(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy, NULL))
3214 return -EINVAL;
3215
3216 nla_af = attrs[IPVS_SVC_ATTR_AF];
3217 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3218 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3219 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3220 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3221
3222 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3223 return -EINVAL;
3224
3225 memset(usvc, 0, sizeof(*usvc));
3226
3227 usvc->af = nla_get_u16(nla_af);
3228 if (!ip_vs_is_af_valid(usvc->af))
3229 return -EAFNOSUPPORT;
3230
3231 if (nla_fwmark) {
3232 usvc->protocol = IPPROTO_TCP;
3233 usvc->fwmark = nla_get_u32(nla_fwmark);
3234 } else {
3235 usvc->protocol = nla_get_u16(nla_protocol);
3236 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3237 usvc->port = nla_get_be16(nla_port);
3238 usvc->fwmark = 0;
3239 }
3240
3241 rcu_read_lock();
3242 if (usvc->fwmark)
3243 svc = __ip_vs_svc_fwm_find(ipvs, usvc->af, usvc->fwmark);
3244 else
3245 svc = __ip_vs_service_find(ipvs, usvc->af, usvc->protocol,
3246 &usvc->addr, usvc->port);
3247 rcu_read_unlock();
3248 *ret_svc = svc;
3249
3250 /* If a full entry was requested, check for the additional fields */
3251 if (full_entry) {
3252 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
3253 *nla_netmask;
3254 struct ip_vs_flags flags;
3255
3256 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
3257 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
3258 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3259 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3260 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3261
3262 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3263 return -EINVAL;
3264
3265 nla_memcpy(&flags, nla_flags, sizeof(flags));
3266
3267 /* prefill flags from service if it already exists */
3268 if (svc)
3269 usvc->flags = svc->flags;
3270
3271 /* set new flags from userland */
3272 usvc->flags = (usvc->flags & ~flags.mask) |
3273 (flags.flags & flags.mask);
3274 usvc->sched_name = nla_data(nla_sched);
3275 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3276 usvc->timeout = nla_get_u32(nla_timeout);
3277 usvc->netmask = nla_get_be32(nla_netmask);
3278 }
3279
3280 return 0;
3281 }
3282
ip_vs_genl_find_service(struct netns_ipvs * ipvs,struct nlattr * nla)3283 static struct ip_vs_service *ip_vs_genl_find_service(struct netns_ipvs *ipvs,
3284 struct nlattr *nla)
3285 {
3286 struct ip_vs_service_user_kern usvc;
3287 struct ip_vs_service *svc;
3288 int ret;
3289
3290 ret = ip_vs_genl_parse_service(ipvs, &usvc, nla, false, &svc);
3291 return ret ? ERR_PTR(ret) : svc;
3292 }
3293
ip_vs_genl_fill_dest(struct sk_buff * skb,struct ip_vs_dest * dest)3294 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3295 {
3296 struct nlattr *nl_dest;
3297 struct ip_vs_kstats kstats;
3298
3299 nl_dest = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_DEST);
3300 if (!nl_dest)
3301 return -EMSGSIZE;
3302
3303 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3304 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3305 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3306 (atomic_read(&dest->conn_flags) &
3307 IP_VS_CONN_F_FWD_MASK)) ||
3308 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3309 atomic_read(&dest->weight)) ||
3310 nla_put_u8(skb, IPVS_DEST_ATTR_TUN_TYPE,
3311 dest->tun_type) ||
3312 nla_put_be16(skb, IPVS_DEST_ATTR_TUN_PORT,
3313 dest->tun_port) ||
3314 nla_put_u16(skb, IPVS_DEST_ATTR_TUN_FLAGS,
3315 dest->tun_flags) ||
3316 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3317 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3318 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3319 atomic_read(&dest->activeconns)) ||
3320 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3321 atomic_read(&dest->inactconns)) ||
3322 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3323 atomic_read(&dest->persistconns)) ||
3324 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3325 goto nla_put_failure;
3326 ip_vs_copy_stats(&kstats, &dest->stats);
3327 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3328 goto nla_put_failure;
3329 if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
3330 goto nla_put_failure;
3331
3332 nla_nest_end(skb, nl_dest);
3333
3334 return 0;
3335
3336 nla_put_failure:
3337 nla_nest_cancel(skb, nl_dest);
3338 return -EMSGSIZE;
3339 }
3340
ip_vs_genl_dump_dest(struct sk_buff * skb,struct ip_vs_dest * dest,struct netlink_callback * cb)3341 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3342 struct netlink_callback *cb)
3343 {
3344 void *hdr;
3345
3346 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3347 &ip_vs_genl_family, NLM_F_MULTI,
3348 IPVS_CMD_NEW_DEST);
3349 if (!hdr)
3350 return -EMSGSIZE;
3351
3352 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3353 goto nla_put_failure;
3354
3355 genlmsg_end(skb, hdr);
3356 return 0;
3357
3358 nla_put_failure:
3359 genlmsg_cancel(skb, hdr);
3360 return -EMSGSIZE;
3361 }
3362
ip_vs_genl_dump_dests(struct sk_buff * skb,struct netlink_callback * cb)3363 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3364 struct netlink_callback *cb)
3365 {
3366 int idx = 0;
3367 int start = cb->args[0];
3368 struct ip_vs_service *svc;
3369 struct ip_vs_dest *dest;
3370 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3371 struct net *net = sock_net(skb->sk);
3372 struct netns_ipvs *ipvs = net_ipvs(net);
3373
3374 mutex_lock(&__ip_vs_mutex);
3375
3376 /* Try to find the service for which to dump destinations */
3377 if (nlmsg_parse_deprecated(cb->nlh, GENL_HDRLEN, attrs, IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy, cb->extack))
3378 goto out_err;
3379
3380
3381 svc = ip_vs_genl_find_service(ipvs, attrs[IPVS_CMD_ATTR_SERVICE]);
3382 if (IS_ERR_OR_NULL(svc))
3383 goto out_err;
3384
3385 /* Dump the destinations */
3386 list_for_each_entry(dest, &svc->destinations, n_list) {
3387 if (++idx <= start)
3388 continue;
3389 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3390 idx--;
3391 goto nla_put_failure;
3392 }
3393 }
3394
3395 nla_put_failure:
3396 cb->args[0] = idx;
3397
3398 out_err:
3399 mutex_unlock(&__ip_vs_mutex);
3400
3401 return skb->len;
3402 }
3403
ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern * udest,struct nlattr * nla,bool full_entry)3404 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3405 struct nlattr *nla, bool full_entry)
3406 {
3407 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3408 struct nlattr *nla_addr, *nla_port;
3409 struct nlattr *nla_addr_family;
3410
3411 /* Parse mandatory identifying destination fields first */
3412 if (nla == NULL ||
3413 nla_parse_nested_deprecated(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy, NULL))
3414 return -EINVAL;
3415
3416 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3417 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3418 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3419
3420 if (!(nla_addr && nla_port))
3421 return -EINVAL;
3422
3423 memset(udest, 0, sizeof(*udest));
3424
3425 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3426 udest->port = nla_get_be16(nla_port);
3427
3428 if (nla_addr_family)
3429 udest->af = nla_get_u16(nla_addr_family);
3430 else
3431 udest->af = 0;
3432
3433 /* If a full entry was requested, check for the additional fields */
3434 if (full_entry) {
3435 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3436 *nla_l_thresh, *nla_tun_type, *nla_tun_port,
3437 *nla_tun_flags;
3438
3439 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3440 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3441 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3442 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3443 nla_tun_type = attrs[IPVS_DEST_ATTR_TUN_TYPE];
3444 nla_tun_port = attrs[IPVS_DEST_ATTR_TUN_PORT];
3445 nla_tun_flags = attrs[IPVS_DEST_ATTR_TUN_FLAGS];
3446
3447 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3448 return -EINVAL;
3449
3450 udest->conn_flags = nla_get_u32(nla_fwd)
3451 & IP_VS_CONN_F_FWD_MASK;
3452 udest->weight = nla_get_u32(nla_weight);
3453 udest->u_threshold = nla_get_u32(nla_u_thresh);
3454 udest->l_threshold = nla_get_u32(nla_l_thresh);
3455
3456 if (nla_tun_type)
3457 udest->tun_type = nla_get_u8(nla_tun_type);
3458
3459 if (nla_tun_port)
3460 udest->tun_port = nla_get_be16(nla_tun_port);
3461
3462 if (nla_tun_flags)
3463 udest->tun_flags = nla_get_u16(nla_tun_flags);
3464 }
3465
3466 return 0;
3467 }
3468
ip_vs_genl_fill_daemon(struct sk_buff * skb,__u32 state,struct ipvs_sync_daemon_cfg * c)3469 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3470 struct ipvs_sync_daemon_cfg *c)
3471 {
3472 struct nlattr *nl_daemon;
3473
3474 nl_daemon = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_DAEMON);
3475 if (!nl_daemon)
3476 return -EMSGSIZE;
3477
3478 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3479 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, c->mcast_ifn) ||
3480 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, c->syncid) ||
3481 nla_put_u16(skb, IPVS_DAEMON_ATTR_SYNC_MAXLEN, c->sync_maxlen) ||
3482 nla_put_u16(skb, IPVS_DAEMON_ATTR_MCAST_PORT, c->mcast_port) ||
3483 nla_put_u8(skb, IPVS_DAEMON_ATTR_MCAST_TTL, c->mcast_ttl))
3484 goto nla_put_failure;
3485 #ifdef CONFIG_IP_VS_IPV6
3486 if (c->mcast_af == AF_INET6) {
3487 if (nla_put_in6_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP6,
3488 &c->mcast_group.in6))
3489 goto nla_put_failure;
3490 } else
3491 #endif
3492 if (c->mcast_af == AF_INET &&
3493 nla_put_in_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP,
3494 c->mcast_group.ip))
3495 goto nla_put_failure;
3496 nla_nest_end(skb, nl_daemon);
3497
3498 return 0;
3499
3500 nla_put_failure:
3501 nla_nest_cancel(skb, nl_daemon);
3502 return -EMSGSIZE;
3503 }
3504
ip_vs_genl_dump_daemon(struct sk_buff * skb,__u32 state,struct ipvs_sync_daemon_cfg * c,struct netlink_callback * cb)3505 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3506 struct ipvs_sync_daemon_cfg *c,
3507 struct netlink_callback *cb)
3508 {
3509 void *hdr;
3510 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3511 &ip_vs_genl_family, NLM_F_MULTI,
3512 IPVS_CMD_NEW_DAEMON);
3513 if (!hdr)
3514 return -EMSGSIZE;
3515
3516 if (ip_vs_genl_fill_daemon(skb, state, c))
3517 goto nla_put_failure;
3518
3519 genlmsg_end(skb, hdr);
3520 return 0;
3521
3522 nla_put_failure:
3523 genlmsg_cancel(skb, hdr);
3524 return -EMSGSIZE;
3525 }
3526
ip_vs_genl_dump_daemons(struct sk_buff * skb,struct netlink_callback * cb)3527 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3528 struct netlink_callback *cb)
3529 {
3530 struct net *net = sock_net(skb->sk);
3531 struct netns_ipvs *ipvs = net_ipvs(net);
3532
3533 mutex_lock(&ipvs->sync_mutex);
3534 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3535 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3536 &ipvs->mcfg, cb) < 0)
3537 goto nla_put_failure;
3538
3539 cb->args[0] = 1;
3540 }
3541
3542 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3543 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3544 &ipvs->bcfg, cb) < 0)
3545 goto nla_put_failure;
3546
3547 cb->args[1] = 1;
3548 }
3549
3550 nla_put_failure:
3551 mutex_unlock(&ipvs->sync_mutex);
3552
3553 return skb->len;
3554 }
3555
ip_vs_genl_new_daemon(struct netns_ipvs * ipvs,struct nlattr ** attrs)3556 static int ip_vs_genl_new_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3557 {
3558 struct ipvs_sync_daemon_cfg c;
3559 struct nlattr *a;
3560 int ret;
3561
3562 memset(&c, 0, sizeof(c));
3563 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3564 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3565 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3566 return -EINVAL;
3567 strscpy(c.mcast_ifn, nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3568 sizeof(c.mcast_ifn));
3569 c.syncid = nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]);
3570
3571 a = attrs[IPVS_DAEMON_ATTR_SYNC_MAXLEN];
3572 if (a)
3573 c.sync_maxlen = nla_get_u16(a);
3574
3575 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP];
3576 if (a) {
3577 c.mcast_af = AF_INET;
3578 c.mcast_group.ip = nla_get_in_addr(a);
3579 if (!ipv4_is_multicast(c.mcast_group.ip))
3580 return -EINVAL;
3581 } else {
3582 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP6];
3583 if (a) {
3584 #ifdef CONFIG_IP_VS_IPV6
3585 int addr_type;
3586
3587 c.mcast_af = AF_INET6;
3588 c.mcast_group.in6 = nla_get_in6_addr(a);
3589 addr_type = ipv6_addr_type(&c.mcast_group.in6);
3590 if (!(addr_type & IPV6_ADDR_MULTICAST))
3591 return -EINVAL;
3592 #else
3593 return -EAFNOSUPPORT;
3594 #endif
3595 }
3596 }
3597
3598 a = attrs[IPVS_DAEMON_ATTR_MCAST_PORT];
3599 if (a)
3600 c.mcast_port = nla_get_u16(a);
3601
3602 a = attrs[IPVS_DAEMON_ATTR_MCAST_TTL];
3603 if (a)
3604 c.mcast_ttl = nla_get_u8(a);
3605
3606 /* The synchronization protocol is incompatible with mixed family
3607 * services
3608 */
3609 if (ipvs->mixed_address_family_dests > 0)
3610 return -EINVAL;
3611
3612 ret = start_sync_thread(ipvs, &c,
3613 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3614 return ret;
3615 }
3616
ip_vs_genl_del_daemon(struct netns_ipvs * ipvs,struct nlattr ** attrs)3617 static int ip_vs_genl_del_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3618 {
3619 int ret;
3620
3621 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3622 return -EINVAL;
3623
3624 ret = stop_sync_thread(ipvs,
3625 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3626 return ret;
3627 }
3628
ip_vs_genl_set_config(struct netns_ipvs * ipvs,struct nlattr ** attrs)3629 static int ip_vs_genl_set_config(struct netns_ipvs *ipvs, struct nlattr **attrs)
3630 {
3631 struct ip_vs_timeout_user t;
3632
3633 __ip_vs_get_timeouts(ipvs, &t);
3634
3635 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3636 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3637
3638 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3639 t.tcp_fin_timeout =
3640 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3641
3642 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3643 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3644
3645 return ip_vs_set_timeout(ipvs, &t);
3646 }
3647
ip_vs_genl_set_daemon(struct sk_buff * skb,struct genl_info * info)3648 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3649 {
3650 int ret = -EINVAL, cmd;
3651 struct net *net = sock_net(skb->sk);
3652 struct netns_ipvs *ipvs = net_ipvs(net);
3653
3654 cmd = info->genlhdr->cmd;
3655
3656 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3657 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3658
3659 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3660 nla_parse_nested_deprecated(daemon_attrs, IPVS_DAEMON_ATTR_MAX, info->attrs[IPVS_CMD_ATTR_DAEMON], ip_vs_daemon_policy, info->extack))
3661 goto out;
3662
3663 if (cmd == IPVS_CMD_NEW_DAEMON)
3664 ret = ip_vs_genl_new_daemon(ipvs, daemon_attrs);
3665 else
3666 ret = ip_vs_genl_del_daemon(ipvs, daemon_attrs);
3667 }
3668
3669 out:
3670 return ret;
3671 }
3672
ip_vs_genl_set_cmd(struct sk_buff * skb,struct genl_info * info)3673 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3674 {
3675 bool need_full_svc = false, need_full_dest = false;
3676 struct ip_vs_service *svc = NULL;
3677 struct ip_vs_service_user_kern usvc;
3678 struct ip_vs_dest_user_kern udest;
3679 int ret = 0, cmd;
3680 struct net *net = sock_net(skb->sk);
3681 struct netns_ipvs *ipvs = net_ipvs(net);
3682
3683 cmd = info->genlhdr->cmd;
3684
3685 mutex_lock(&__ip_vs_mutex);
3686
3687 if (cmd == IPVS_CMD_FLUSH) {
3688 ret = ip_vs_flush(ipvs, false);
3689 goto out;
3690 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3691 ret = ip_vs_genl_set_config(ipvs, info->attrs);
3692 goto out;
3693 } else if (cmd == IPVS_CMD_ZERO &&
3694 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3695 ret = ip_vs_zero_all(ipvs);
3696 goto out;
3697 }
3698
3699 /* All following commands require a service argument, so check if we
3700 * received a valid one. We need a full service specification when
3701 * adding / editing a service. Only identifying members otherwise. */
3702 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3703 need_full_svc = true;
3704
3705 ret = ip_vs_genl_parse_service(ipvs, &usvc,
3706 info->attrs[IPVS_CMD_ATTR_SERVICE],
3707 need_full_svc, &svc);
3708 if (ret)
3709 goto out;
3710
3711 /* Unless we're adding a new service, the service must already exist */
3712 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3713 ret = -ESRCH;
3714 goto out;
3715 }
3716
3717 /* Destination commands require a valid destination argument. For
3718 * adding / editing a destination, we need a full destination
3719 * specification. */
3720 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3721 cmd == IPVS_CMD_DEL_DEST) {
3722 if (cmd != IPVS_CMD_DEL_DEST)
3723 need_full_dest = true;
3724
3725 ret = ip_vs_genl_parse_dest(&udest,
3726 info->attrs[IPVS_CMD_ATTR_DEST],
3727 need_full_dest);
3728 if (ret)
3729 goto out;
3730
3731 /* Old protocols did not allow the user to specify address
3732 * family, so we set it to zero instead. We also didn't
3733 * allow heterogeneous pools in the old code, so it's safe
3734 * to assume that this will have the same address family as
3735 * the service.
3736 */
3737 if (udest.af == 0)
3738 udest.af = svc->af;
3739
3740 if (!ip_vs_is_af_valid(udest.af)) {
3741 ret = -EAFNOSUPPORT;
3742 goto out;
3743 }
3744
3745 if (udest.af != svc->af && cmd != IPVS_CMD_DEL_DEST) {
3746 /* The synchronization protocol is incompatible
3747 * with mixed family services
3748 */
3749 if (ipvs->sync_state) {
3750 ret = -EINVAL;
3751 goto out;
3752 }
3753
3754 /* Which connection types do we support? */
3755 switch (udest.conn_flags) {
3756 case IP_VS_CONN_F_TUNNEL:
3757 /* We are able to forward this */
3758 break;
3759 default:
3760 ret = -EINVAL;
3761 goto out;
3762 }
3763 }
3764 }
3765
3766 switch (cmd) {
3767 case IPVS_CMD_NEW_SERVICE:
3768 if (svc == NULL)
3769 ret = ip_vs_add_service(ipvs, &usvc, &svc);
3770 else
3771 ret = -EEXIST;
3772 break;
3773 case IPVS_CMD_SET_SERVICE:
3774 ret = ip_vs_edit_service(svc, &usvc);
3775 break;
3776 case IPVS_CMD_DEL_SERVICE:
3777 ret = ip_vs_del_service(svc);
3778 /* do not use svc, it can be freed */
3779 break;
3780 case IPVS_CMD_NEW_DEST:
3781 ret = ip_vs_add_dest(svc, &udest);
3782 break;
3783 case IPVS_CMD_SET_DEST:
3784 ret = ip_vs_edit_dest(svc, &udest);
3785 break;
3786 case IPVS_CMD_DEL_DEST:
3787 ret = ip_vs_del_dest(svc, &udest);
3788 break;
3789 case IPVS_CMD_ZERO:
3790 ret = ip_vs_zero_service(svc);
3791 break;
3792 default:
3793 ret = -EINVAL;
3794 }
3795
3796 out:
3797 mutex_unlock(&__ip_vs_mutex);
3798
3799 return ret;
3800 }
3801
ip_vs_genl_get_cmd(struct sk_buff * skb,struct genl_info * info)3802 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3803 {
3804 struct sk_buff *msg;
3805 void *reply;
3806 int ret, cmd, reply_cmd;
3807 struct net *net = sock_net(skb->sk);
3808 struct netns_ipvs *ipvs = net_ipvs(net);
3809
3810 cmd = info->genlhdr->cmd;
3811
3812 if (cmd == IPVS_CMD_GET_SERVICE)
3813 reply_cmd = IPVS_CMD_NEW_SERVICE;
3814 else if (cmd == IPVS_CMD_GET_INFO)
3815 reply_cmd = IPVS_CMD_SET_INFO;
3816 else if (cmd == IPVS_CMD_GET_CONFIG)
3817 reply_cmd = IPVS_CMD_SET_CONFIG;
3818 else {
3819 pr_err("unknown Generic Netlink command\n");
3820 return -EINVAL;
3821 }
3822
3823 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3824 if (!msg)
3825 return -ENOMEM;
3826
3827 mutex_lock(&__ip_vs_mutex);
3828
3829 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3830 if (reply == NULL)
3831 goto nla_put_failure;
3832
3833 switch (cmd) {
3834 case IPVS_CMD_GET_SERVICE:
3835 {
3836 struct ip_vs_service *svc;
3837
3838 svc = ip_vs_genl_find_service(ipvs,
3839 info->attrs[IPVS_CMD_ATTR_SERVICE]);
3840 if (IS_ERR(svc)) {
3841 ret = PTR_ERR(svc);
3842 goto out_err;
3843 } else if (svc) {
3844 ret = ip_vs_genl_fill_service(msg, svc);
3845 if (ret)
3846 goto nla_put_failure;
3847 } else {
3848 ret = -ESRCH;
3849 goto out_err;
3850 }
3851
3852 break;
3853 }
3854
3855 case IPVS_CMD_GET_CONFIG:
3856 {
3857 struct ip_vs_timeout_user t;
3858
3859 __ip_vs_get_timeouts(ipvs, &t);
3860 #ifdef CONFIG_IP_VS_PROTO_TCP
3861 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3862 t.tcp_timeout) ||
3863 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3864 t.tcp_fin_timeout))
3865 goto nla_put_failure;
3866 #endif
3867 #ifdef CONFIG_IP_VS_PROTO_UDP
3868 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3869 goto nla_put_failure;
3870 #endif
3871
3872 break;
3873 }
3874
3875 case IPVS_CMD_GET_INFO:
3876 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3877 IP_VS_VERSION_CODE) ||
3878 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3879 ip_vs_conn_tab_size))
3880 goto nla_put_failure;
3881 break;
3882 }
3883
3884 genlmsg_end(msg, reply);
3885 ret = genlmsg_reply(msg, info);
3886 goto out;
3887
3888 nla_put_failure:
3889 pr_err("not enough space in Netlink message\n");
3890 ret = -EMSGSIZE;
3891
3892 out_err:
3893 nlmsg_free(msg);
3894 out:
3895 mutex_unlock(&__ip_vs_mutex);
3896
3897 return ret;
3898 }
3899
3900
3901 static const struct genl_small_ops ip_vs_genl_ops[] = {
3902 {
3903 .cmd = IPVS_CMD_NEW_SERVICE,
3904 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3905 .flags = GENL_ADMIN_PERM,
3906 .doit = ip_vs_genl_set_cmd,
3907 },
3908 {
3909 .cmd = IPVS_CMD_SET_SERVICE,
3910 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3911 .flags = GENL_ADMIN_PERM,
3912 .doit = ip_vs_genl_set_cmd,
3913 },
3914 {
3915 .cmd = IPVS_CMD_DEL_SERVICE,
3916 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3917 .flags = GENL_ADMIN_PERM,
3918 .doit = ip_vs_genl_set_cmd,
3919 },
3920 {
3921 .cmd = IPVS_CMD_GET_SERVICE,
3922 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3923 .flags = GENL_ADMIN_PERM,
3924 .doit = ip_vs_genl_get_cmd,
3925 .dumpit = ip_vs_genl_dump_services,
3926 },
3927 {
3928 .cmd = IPVS_CMD_NEW_DEST,
3929 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3930 .flags = GENL_ADMIN_PERM,
3931 .doit = ip_vs_genl_set_cmd,
3932 },
3933 {
3934 .cmd = IPVS_CMD_SET_DEST,
3935 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3936 .flags = GENL_ADMIN_PERM,
3937 .doit = ip_vs_genl_set_cmd,
3938 },
3939 {
3940 .cmd = IPVS_CMD_DEL_DEST,
3941 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3942 .flags = GENL_ADMIN_PERM,
3943 .doit = ip_vs_genl_set_cmd,
3944 },
3945 {
3946 .cmd = IPVS_CMD_GET_DEST,
3947 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3948 .flags = GENL_ADMIN_PERM,
3949 .dumpit = ip_vs_genl_dump_dests,
3950 },
3951 {
3952 .cmd = IPVS_CMD_NEW_DAEMON,
3953 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3954 .flags = GENL_ADMIN_PERM,
3955 .doit = ip_vs_genl_set_daemon,
3956 },
3957 {
3958 .cmd = IPVS_CMD_DEL_DAEMON,
3959 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3960 .flags = GENL_ADMIN_PERM,
3961 .doit = ip_vs_genl_set_daemon,
3962 },
3963 {
3964 .cmd = IPVS_CMD_GET_DAEMON,
3965 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3966 .flags = GENL_ADMIN_PERM,
3967 .dumpit = ip_vs_genl_dump_daemons,
3968 },
3969 {
3970 .cmd = IPVS_CMD_SET_CONFIG,
3971 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3972 .flags = GENL_ADMIN_PERM,
3973 .doit = ip_vs_genl_set_cmd,
3974 },
3975 {
3976 .cmd = IPVS_CMD_GET_CONFIG,
3977 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3978 .flags = GENL_ADMIN_PERM,
3979 .doit = ip_vs_genl_get_cmd,
3980 },
3981 {
3982 .cmd = IPVS_CMD_GET_INFO,
3983 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3984 .flags = GENL_ADMIN_PERM,
3985 .doit = ip_vs_genl_get_cmd,
3986 },
3987 {
3988 .cmd = IPVS_CMD_ZERO,
3989 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3990 .flags = GENL_ADMIN_PERM,
3991 .doit = ip_vs_genl_set_cmd,
3992 },
3993 {
3994 .cmd = IPVS_CMD_FLUSH,
3995 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3996 .flags = GENL_ADMIN_PERM,
3997 .doit = ip_vs_genl_set_cmd,
3998 },
3999 };
4000
4001 static struct genl_family ip_vs_genl_family __ro_after_init = {
4002 .hdrsize = 0,
4003 .name = IPVS_GENL_NAME,
4004 .version = IPVS_GENL_VERSION,
4005 .maxattr = IPVS_CMD_ATTR_MAX,
4006 .policy = ip_vs_cmd_policy,
4007 .netnsok = true, /* Make ipvsadm to work on netns */
4008 .module = THIS_MODULE,
4009 .small_ops = ip_vs_genl_ops,
4010 .n_small_ops = ARRAY_SIZE(ip_vs_genl_ops),
4011 .resv_start_op = IPVS_CMD_FLUSH + 1,
4012 };
4013
ip_vs_genl_register(void)4014 static int __init ip_vs_genl_register(void)
4015 {
4016 return genl_register_family(&ip_vs_genl_family);
4017 }
4018
ip_vs_genl_unregister(void)4019 static void ip_vs_genl_unregister(void)
4020 {
4021 genl_unregister_family(&ip_vs_genl_family);
4022 }
4023
4024 /* End of Generic Netlink interface definitions */
4025
4026 /*
4027 * per netns intit/exit func.
4028 */
4029 #ifdef CONFIG_SYSCTL
ip_vs_control_net_init_sysctl(struct netns_ipvs * ipvs)4030 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs)
4031 {
4032 struct net *net = ipvs->net;
4033 int idx;
4034 struct ctl_table *tbl;
4035
4036 atomic_set(&ipvs->dropentry, 0);
4037 spin_lock_init(&ipvs->dropentry_lock);
4038 spin_lock_init(&ipvs->droppacket_lock);
4039 spin_lock_init(&ipvs->securetcp_lock);
4040
4041 if (!net_eq(net, &init_net)) {
4042 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
4043 if (tbl == NULL)
4044 return -ENOMEM;
4045
4046 /* Don't export sysctls to unprivileged users */
4047 if (net->user_ns != &init_user_ns)
4048 tbl[0].procname = NULL;
4049 } else
4050 tbl = vs_vars;
4051 /* Initialize sysctl defaults */
4052 for (idx = 0; idx < ARRAY_SIZE(vs_vars); idx++) {
4053 if (tbl[idx].proc_handler == proc_do_defense_mode)
4054 tbl[idx].extra2 = ipvs;
4055 }
4056 idx = 0;
4057 ipvs->sysctl_amemthresh = 1024;
4058 tbl[idx++].data = &ipvs->sysctl_amemthresh;
4059 ipvs->sysctl_am_droprate = 10;
4060 tbl[idx++].data = &ipvs->sysctl_am_droprate;
4061 tbl[idx++].data = &ipvs->sysctl_drop_entry;
4062 tbl[idx++].data = &ipvs->sysctl_drop_packet;
4063 #ifdef CONFIG_IP_VS_NFCT
4064 tbl[idx++].data = &ipvs->sysctl_conntrack;
4065 #endif
4066 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
4067 ipvs->sysctl_snat_reroute = 1;
4068 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
4069 ipvs->sysctl_sync_ver = 1;
4070 tbl[idx++].data = &ipvs->sysctl_sync_ver;
4071 ipvs->sysctl_sync_ports = 1;
4072 tbl[idx++].data = &ipvs->sysctl_sync_ports;
4073 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
4074 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
4075 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
4076 ipvs->sysctl_sync_sock_size = 0;
4077 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
4078 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
4079 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
4080 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
4081 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
4082 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
4083 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
4084 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
4085 tbl[idx].data = &ipvs->sysctl_sync_threshold;
4086 tbl[idx].extra2 = ipvs;
4087 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
4088 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
4089 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
4090 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
4091 tbl[idx++].data = &ipvs->sysctl_sync_retries;
4092 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
4093 ipvs->sysctl_pmtu_disc = 1;
4094 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
4095 tbl[idx++].data = &ipvs->sysctl_backup_only;
4096 ipvs->sysctl_conn_reuse_mode = 1;
4097 tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
4098 tbl[idx++].data = &ipvs->sysctl_schedule_icmp;
4099 tbl[idx++].data = &ipvs->sysctl_ignore_tunneled;
4100 ipvs->sysctl_run_estimation = 1;
4101 tbl[idx++].data = &ipvs->sysctl_run_estimation;
4102 #ifdef CONFIG_IP_VS_DEBUG
4103 /* Global sysctls must be ro in non-init netns */
4104 if (!net_eq(net, &init_net))
4105 tbl[idx++].mode = 0444;
4106 #endif
4107
4108 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
4109 if (ipvs->sysctl_hdr == NULL) {
4110 if (!net_eq(net, &init_net))
4111 kfree(tbl);
4112 return -ENOMEM;
4113 }
4114 ip_vs_start_estimator(ipvs, &ipvs->tot_stats);
4115 ipvs->sysctl_tbl = tbl;
4116 /* Schedule defense work */
4117 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
4118 queue_delayed_work(system_long_wq, &ipvs->defense_work,
4119 DEFENSE_TIMER_PERIOD);
4120
4121 /* Init delayed work for expiring no dest conn */
4122 INIT_DELAYED_WORK(&ipvs->expire_nodest_conn_work,
4123 expire_nodest_conn_handler);
4124
4125 return 0;
4126 }
4127
ip_vs_control_net_cleanup_sysctl(struct netns_ipvs * ipvs)4128 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs)
4129 {
4130 struct net *net = ipvs->net;
4131
4132 cancel_delayed_work_sync(&ipvs->expire_nodest_conn_work);
4133 cancel_delayed_work_sync(&ipvs->defense_work);
4134 cancel_work_sync(&ipvs->defense_work.work);
4135 unregister_net_sysctl_table(ipvs->sysctl_hdr);
4136 ip_vs_stop_estimator(ipvs, &ipvs->tot_stats);
4137
4138 if (!net_eq(net, &init_net))
4139 kfree(ipvs->sysctl_tbl);
4140 }
4141
4142 #else
4143
ip_vs_control_net_init_sysctl(struct netns_ipvs * ipvs)4144 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs) { return 0; }
ip_vs_control_net_cleanup_sysctl(struct netns_ipvs * ipvs)4145 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs) { }
4146
4147 #endif
4148
4149 static struct notifier_block ip_vs_dst_notifier = {
4150 .notifier_call = ip_vs_dst_event,
4151 #ifdef CONFIG_IP_VS_IPV6
4152 .priority = ADDRCONF_NOTIFY_PRIORITY + 5,
4153 #endif
4154 };
4155
ip_vs_control_net_init(struct netns_ipvs * ipvs)4156 int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs)
4157 {
4158 int i, idx;
4159
4160 /* Initialize rs_table */
4161 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
4162 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
4163
4164 INIT_LIST_HEAD(&ipvs->dest_trash);
4165 spin_lock_init(&ipvs->dest_trash_lock);
4166 timer_setup(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire, 0);
4167 atomic_set(&ipvs->ftpsvc_counter, 0);
4168 atomic_set(&ipvs->nullsvc_counter, 0);
4169 atomic_set(&ipvs->conn_out_counter, 0);
4170
4171 /* procfs stats */
4172 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
4173 if (!ipvs->tot_stats.cpustats)
4174 return -ENOMEM;
4175
4176 for_each_possible_cpu(i) {
4177 struct ip_vs_cpu_stats *ipvs_tot_stats;
4178 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
4179 u64_stats_init(&ipvs_tot_stats->syncp);
4180 }
4181
4182 spin_lock_init(&ipvs->tot_stats.lock);
4183
4184 #ifdef CONFIG_PROC_FS
4185 if (!proc_create_net("ip_vs", 0, ipvs->net->proc_net,
4186 &ip_vs_info_seq_ops, sizeof(struct ip_vs_iter)))
4187 goto err_vs;
4188 if (!proc_create_net_single("ip_vs_stats", 0, ipvs->net->proc_net,
4189 ip_vs_stats_show, NULL))
4190 goto err_stats;
4191 if (!proc_create_net_single("ip_vs_stats_percpu", 0,
4192 ipvs->net->proc_net,
4193 ip_vs_stats_percpu_show, NULL))
4194 goto err_percpu;
4195 #endif
4196
4197 if (ip_vs_control_net_init_sysctl(ipvs))
4198 goto err;
4199
4200 return 0;
4201
4202 err:
4203 #ifdef CONFIG_PROC_FS
4204 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4205
4206 err_percpu:
4207 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4208
4209 err_stats:
4210 remove_proc_entry("ip_vs", ipvs->net->proc_net);
4211
4212 err_vs:
4213 #endif
4214 free_percpu(ipvs->tot_stats.cpustats);
4215 return -ENOMEM;
4216 }
4217
ip_vs_control_net_cleanup(struct netns_ipvs * ipvs)4218 void __net_exit ip_vs_control_net_cleanup(struct netns_ipvs *ipvs)
4219 {
4220 ip_vs_trash_cleanup(ipvs);
4221 ip_vs_control_net_cleanup_sysctl(ipvs);
4222 #ifdef CONFIG_PROC_FS
4223 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4224 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4225 remove_proc_entry("ip_vs", ipvs->net->proc_net);
4226 #endif
4227 free_percpu(ipvs->tot_stats.cpustats);
4228 }
4229
ip_vs_register_nl_ioctl(void)4230 int __init ip_vs_register_nl_ioctl(void)
4231 {
4232 int ret;
4233
4234 ret = nf_register_sockopt(&ip_vs_sockopts);
4235 if (ret) {
4236 pr_err("cannot register sockopt.\n");
4237 goto err_sock;
4238 }
4239
4240 ret = ip_vs_genl_register();
4241 if (ret) {
4242 pr_err("cannot register Generic Netlink interface.\n");
4243 goto err_genl;
4244 }
4245 return 0;
4246
4247 err_genl:
4248 nf_unregister_sockopt(&ip_vs_sockopts);
4249 err_sock:
4250 return ret;
4251 }
4252
ip_vs_unregister_nl_ioctl(void)4253 void ip_vs_unregister_nl_ioctl(void)
4254 {
4255 ip_vs_genl_unregister();
4256 nf_unregister_sockopt(&ip_vs_sockopts);
4257 }
4258
ip_vs_control_init(void)4259 int __init ip_vs_control_init(void)
4260 {
4261 int idx;
4262 int ret;
4263
4264 EnterFunction(2);
4265
4266 /* Initialize svc_table, ip_vs_svc_fwm_table */
4267 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
4268 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
4269 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
4270 }
4271
4272 smp_wmb(); /* Do we really need it now ? */
4273
4274 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
4275 if (ret < 0)
4276 return ret;
4277
4278 LeaveFunction(2);
4279 return 0;
4280 }
4281
4282
ip_vs_control_cleanup(void)4283 void ip_vs_control_cleanup(void)
4284 {
4285 EnterFunction(2);
4286 unregister_netdevice_notifier(&ip_vs_dst_notifier);
4287 LeaveFunction(2);
4288 }
4289