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