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