1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* IP Virtual Server 3 * data structure and functionality definitions 4 */ 5 6 #ifndef _NET_IP_VS_H 7 #define _NET_IP_VS_H 8 9 #include <linux/ip_vs.h> /* definitions shared with userland */ 10 11 #include <asm/types.h> /* for __uXX types */ 12 13 #include <linux/list.h> /* for struct list_head */ 14 #include <linux/rculist_bl.h> /* for struct hlist_bl_head */ 15 #include <linux/spinlock.h> /* for struct rwlock_t */ 16 #include <linux/atomic.h> /* for struct atomic_t */ 17 #include <linux/refcount.h> /* for struct refcount_t */ 18 #include <linux/workqueue.h> 19 20 #include <linux/compiler.h> 21 #include <linux/timer.h> 22 #include <linux/bug.h> 23 24 #include <net/checksum.h> 25 #include <linux/netfilter.h> /* for union nf_inet_addr */ 26 #include <linux/ip.h> 27 #include <linux/ipv6.h> /* for struct ipv6hdr */ 28 #include <net/ipv6.h> 29 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 30 #include <net/netfilter/nf_conntrack.h> 31 #endif 32 #include <net/net_namespace.h> /* Netw namespace */ 33 #include <linux/sched/isolation.h> 34 #include <linux/siphash.h> 35 36 #define IP_VS_HDR_INVERSE 1 37 #define IP_VS_HDR_ICMP 2 38 39 /* conn_tab limits (as per Kconfig) */ 40 #define IP_VS_CONN_TAB_MIN_BITS 8 41 #if BITS_PER_LONG > 32 42 #define IP_VS_CONN_TAB_MAX_BITS 27 43 #else 44 #define IP_VS_CONN_TAB_MAX_BITS 20 45 #endif 46 47 /* conn_max limits */ 48 #if BITS_PER_LONG > 32 49 /* Limit of atomic_t but restricted by roundup_pow_of_two() in ip_vs_core.c */ 50 #define IP_VS_CONN_MAX (1 << 30) 51 #else 52 #define IP_VS_CONN_MAX (1 << 24) 53 #endif 54 55 /* svc_table limits */ 56 #define IP_VS_SVC_TAB_MIN_BITS 4 57 #define IP_VS_SVC_TAB_MAX_BITS 20 58 59 /* Generic access of ipvs struct */ 60 static inline struct netns_ipvs *net_ipvs(struct net* net) 61 { 62 return net->ipvs; 63 } 64 65 /* Connections' size value needed by ip_vs_ctl.c */ 66 extern int ip_vs_conn_tab_size; 67 68 struct ip_vs_iphdr { 69 int hdr_flags; /* ipvs flags */ 70 __u32 off; /* Where IP or IPv4 header starts */ 71 __u32 len; /* IPv4 simply where L4 starts 72 * IPv6 where L4 Transport Header starts */ 73 __u16 fragoffs; /* IPv6 fragment offset, 0 if first frag (or not frag)*/ 74 __s16 protocol; 75 __s32 flags; 76 union nf_inet_addr saddr; 77 union nf_inet_addr daddr; 78 }; 79 80 static inline void *frag_safe_skb_hp(const struct sk_buff *skb, int offset, 81 int len, void *buffer) 82 { 83 return skb_header_pointer(skb, offset, len, buffer); 84 } 85 86 /* This function handles filling *ip_vs_iphdr, both for IPv4 and IPv6. 87 * IPv6 requires some extra work, as finding proper header position, 88 * depend on the IPv6 extension headers. 89 */ 90 static inline int 91 ip_vs_fill_iph_skb_off(int af, const struct sk_buff *skb, int offset, 92 int hdr_flags, struct ip_vs_iphdr *iphdr) 93 { 94 iphdr->hdr_flags = hdr_flags; 95 iphdr->off = offset; 96 97 #ifdef CONFIG_IP_VS_IPV6 98 if (af == AF_INET6) { 99 struct ipv6hdr _iph; 100 const struct ipv6hdr *iph = skb_header_pointer( 101 skb, offset, sizeof(_iph), &_iph); 102 if (!iph) 103 return 0; 104 105 iphdr->saddr.in6 = iph->saddr; 106 iphdr->daddr.in6 = iph->daddr; 107 /* ipv6_find_hdr() updates len, flags */ 108 iphdr->len = offset; 109 iphdr->flags = 0; 110 iphdr->protocol = ipv6_find_hdr(skb, &iphdr->len, -1, 111 &iphdr->fragoffs, 112 &iphdr->flags); 113 if (iphdr->protocol < 0) 114 return 0; 115 } else 116 #endif 117 { 118 struct iphdr _iph; 119 const struct iphdr *iph = skb_header_pointer( 120 skb, offset, sizeof(_iph), &_iph); 121 if (!iph) 122 return 0; 123 124 iphdr->len = offset + iph->ihl * 4; 125 iphdr->fragoffs = 0; 126 iphdr->protocol = iph->protocol; 127 iphdr->saddr.ip = iph->saddr; 128 iphdr->daddr.ip = iph->daddr; 129 } 130 131 return 1; 132 } 133 134 static inline int 135 ip_vs_fill_iph_skb_icmp(int af, const struct sk_buff *skb, int offset, 136 bool inverse, struct ip_vs_iphdr *iphdr) 137 { 138 int hdr_flags = IP_VS_HDR_ICMP; 139 140 if (inverse) 141 hdr_flags |= IP_VS_HDR_INVERSE; 142 143 return ip_vs_fill_iph_skb_off(af, skb, offset, hdr_flags, iphdr); 144 } 145 146 static inline int 147 ip_vs_fill_iph_skb(int af, const struct sk_buff *skb, bool inverse, 148 struct ip_vs_iphdr *iphdr) 149 { 150 int hdr_flags = 0; 151 152 if (inverse) 153 hdr_flags |= IP_VS_HDR_INVERSE; 154 155 return ip_vs_fill_iph_skb_off(af, skb, skb_network_offset(skb), 156 hdr_flags, iphdr); 157 } 158 159 static inline bool 160 ip_vs_iph_inverse(const struct ip_vs_iphdr *iph) 161 { 162 return !!(iph->hdr_flags & IP_VS_HDR_INVERSE); 163 } 164 165 static inline bool 166 ip_vs_iph_icmp(const struct ip_vs_iphdr *iph) 167 { 168 return !!(iph->hdr_flags & IP_VS_HDR_ICMP); 169 } 170 171 static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst, 172 const union nf_inet_addr *src) 173 { 174 #ifdef CONFIG_IP_VS_IPV6 175 if (af == AF_INET6) 176 dst->in6 = src->in6; 177 else 178 #endif 179 dst->ip = src->ip; 180 } 181 182 static inline void ip_vs_addr_set(int af, union nf_inet_addr *dst, 183 const union nf_inet_addr *src) 184 { 185 #ifdef CONFIG_IP_VS_IPV6 186 if (af == AF_INET6) { 187 dst->in6 = src->in6; 188 return; 189 } 190 #endif 191 dst->ip = src->ip; 192 dst->all[1] = 0; 193 dst->all[2] = 0; 194 dst->all[3] = 0; 195 } 196 197 static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a, 198 const union nf_inet_addr *b) 199 { 200 #ifdef CONFIG_IP_VS_IPV6 201 if (af == AF_INET6) 202 return ipv6_addr_equal(&a->in6, &b->in6); 203 #endif 204 return a->ip == b->ip; 205 } 206 207 #ifdef CONFIG_IP_VS_DEBUG 208 #include <linux/net.h> 209 210 int ip_vs_get_debug_level(void); 211 212 static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len, 213 const union nf_inet_addr *addr, 214 int *idx) 215 { 216 int len; 217 #ifdef CONFIG_IP_VS_IPV6 218 if (af == AF_INET6) 219 len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6c]", 220 &addr->in6) + 1; 221 else 222 #endif 223 len = snprintf(&buf[*idx], buf_len - *idx, "%pI4", 224 &addr->ip) + 1; 225 226 *idx += len; 227 BUG_ON(*idx > buf_len + 1); 228 return &buf[*idx - len]; 229 } 230 231 #define IP_VS_DBG_BUF(level, msg, ...) \ 232 do { \ 233 char ip_vs_dbg_buf[160]; \ 234 int ip_vs_dbg_idx = 0; \ 235 if (level <= ip_vs_get_debug_level()) \ 236 printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \ 237 } while (0) 238 #define IP_VS_ERR_BUF(msg...) \ 239 do { \ 240 char ip_vs_dbg_buf[160]; \ 241 int ip_vs_dbg_idx = 0; \ 242 pr_err(msg); \ 243 } while (0) 244 245 /* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */ 246 #define IP_VS_DBG_ADDR(af, addr) \ 247 ip_vs_dbg_addr(af, ip_vs_dbg_buf, \ 248 sizeof(ip_vs_dbg_buf), addr, \ 249 &ip_vs_dbg_idx) 250 251 #define IP_VS_DBG(level, msg, ...) \ 252 do { \ 253 if (level <= ip_vs_get_debug_level()) \ 254 printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \ 255 } while (0) 256 #define IP_VS_DBG_RL(msg, ...) \ 257 do { \ 258 if (net_ratelimit()) \ 259 printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \ 260 } while (0) 261 #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) \ 262 do { \ 263 if (level <= ip_vs_get_debug_level()) \ 264 pp->debug_packet(af, pp, skb, ofs, msg); \ 265 } while (0) 266 #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) \ 267 do { \ 268 if (level <= ip_vs_get_debug_level() && \ 269 net_ratelimit()) \ 270 pp->debug_packet(af, pp, skb, ofs, msg); \ 271 } while (0) 272 #else /* NO DEBUGGING at ALL */ 273 #define IP_VS_DBG_BUF(level, msg...) do {} while (0) 274 #define IP_VS_ERR_BUF(msg...) do {} while (0) 275 #define IP_VS_DBG(level, msg...) do {} while (0) 276 #define IP_VS_DBG_RL(msg...) do {} while (0) 277 #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) do {} while (0) 278 #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) do {} while (0) 279 #endif 280 281 #define IP_VS_BUG() BUG() 282 #define IP_VS_ERR_RL(msg, ...) \ 283 do { \ 284 if (net_ratelimit()) \ 285 pr_err(msg, ##__VA_ARGS__); \ 286 } while (0) 287 288 struct ip_vs_aligned_lock { 289 spinlock_t l; /* Protect buckets */ 290 } ____cacheline_aligned_in_smp; 291 292 /* For arrays per family */ 293 enum { 294 IP_VS_AF_INET, 295 IP_VS_AF_INET6, 296 IP_VS_AF_MAX 297 }; 298 299 static inline int ip_vs_af_index(int af) 300 { 301 return af == AF_INET6 ? IP_VS_AF_INET6 : IP_VS_AF_INET; 302 } 303 304 /* work_flags */ 305 enum { 306 IP_VS_WORK_SVC_RESIZE, /* Schedule svc_resize_work */ 307 IP_VS_WORK_SVC_NORESIZE, /* Stopping svc_resize_work */ 308 IP_VS_WORK_CONN_RESIZE, /* Schedule conn_resize_work */ 309 }; 310 311 /* The port number of FTP service (in network order). */ 312 #define FTPPORT cpu_to_be16(21) 313 #define FTPDATA cpu_to_be16(20) 314 315 /* TCP State Values */ 316 enum { 317 IP_VS_TCP_S_NONE = 0, 318 IP_VS_TCP_S_ESTABLISHED, 319 IP_VS_TCP_S_SYN_SENT, 320 IP_VS_TCP_S_SYN_RECV, 321 IP_VS_TCP_S_FIN_WAIT, 322 IP_VS_TCP_S_TIME_WAIT, 323 IP_VS_TCP_S_CLOSE, 324 IP_VS_TCP_S_CLOSE_WAIT, 325 IP_VS_TCP_S_LAST_ACK, 326 IP_VS_TCP_S_LISTEN, 327 IP_VS_TCP_S_SYNACK, 328 IP_VS_TCP_S_LAST 329 }; 330 331 /* UDP State Values */ 332 enum { 333 IP_VS_UDP_S_NORMAL, 334 IP_VS_UDP_S_LAST, 335 }; 336 337 /* ICMP State Values */ 338 enum { 339 IP_VS_ICMP_S_NORMAL, 340 IP_VS_ICMP_S_LAST, 341 }; 342 343 /* SCTP State Values */ 344 enum ip_vs_sctp_states { 345 IP_VS_SCTP_S_NONE, 346 IP_VS_SCTP_S_INIT1, 347 IP_VS_SCTP_S_INIT, 348 IP_VS_SCTP_S_COOKIE_SENT, 349 IP_VS_SCTP_S_COOKIE_REPLIED, 350 IP_VS_SCTP_S_COOKIE_WAIT, 351 IP_VS_SCTP_S_COOKIE, 352 IP_VS_SCTP_S_COOKIE_ECHOED, 353 IP_VS_SCTP_S_ESTABLISHED, 354 IP_VS_SCTP_S_SHUTDOWN_SENT, 355 IP_VS_SCTP_S_SHUTDOWN_RECEIVED, 356 IP_VS_SCTP_S_SHUTDOWN_ACK_SENT, 357 IP_VS_SCTP_S_REJECTED, 358 IP_VS_SCTP_S_CLOSED, 359 IP_VS_SCTP_S_LAST 360 }; 361 362 /* Connection templates use bits from state */ 363 #define IP_VS_CTPL_S_NONE 0x0000 364 #define IP_VS_CTPL_S_ASSURED 0x0001 365 #define IP_VS_CTPL_S_LAST 0x0002 366 367 /* Delta sequence info structure 368 * Each ip_vs_conn has 2 (output AND input seq. changes). 369 * Only used in the VS/NAT. 370 */ 371 struct ip_vs_seq { 372 __u32 init_seq; /* Add delta from this seq */ 373 __u32 delta; /* Delta in sequence numbers */ 374 __u32 previous_delta; /* Delta in sequence numbers 375 * before last resized pkt */ 376 }; 377 378 /* counters per cpu */ 379 struct ip_vs_counters { 380 u64_stats_t conns; /* connections scheduled */ 381 u64_stats_t inpkts; /* incoming packets */ 382 u64_stats_t outpkts; /* outgoing packets */ 383 u64_stats_t inbytes; /* incoming bytes */ 384 u64_stats_t outbytes; /* outgoing bytes */ 385 }; 386 /* Stats per cpu */ 387 struct ip_vs_cpu_stats { 388 struct ip_vs_counters cnt; 389 struct u64_stats_sync syncp; 390 }; 391 392 /* Default nice for estimator kthreads */ 393 #define IPVS_EST_NICE 0 394 395 /* IPVS statistics objects */ 396 struct ip_vs_estimator { 397 struct hlist_node list; 398 399 u64 last_inbytes; 400 u64 last_outbytes; 401 u64 last_conns; 402 u64 last_inpkts; 403 u64 last_outpkts; 404 405 u64 cps; 406 u64 inpps; 407 u64 outpps; 408 u64 inbps; 409 u64 outbps; 410 411 s32 ktid:16, /* kthread ID, -1=temp list */ 412 ktrow:8, /* row/tick ID for kthread */ 413 ktcid:8; /* chain ID for kthread tick */ 414 }; 415 416 /* 417 * IPVS statistics object, 64-bit kernel version of struct ip_vs_stats_user 418 */ 419 struct ip_vs_kstats { 420 u64 conns; /* connections scheduled */ 421 u64 inpkts; /* incoming packets */ 422 u64 outpkts; /* outgoing packets */ 423 u64 inbytes; /* incoming bytes */ 424 u64 outbytes; /* outgoing bytes */ 425 426 u64 cps; /* current connection rate */ 427 u64 inpps; /* current in packet rate */ 428 u64 outpps; /* current out packet rate */ 429 u64 inbps; /* current in byte rate */ 430 u64 outbps; /* current out byte rate */ 431 }; 432 433 struct ip_vs_stats { 434 struct ip_vs_kstats kstats; /* kernel statistics */ 435 struct ip_vs_estimator est; /* estimator */ 436 struct ip_vs_cpu_stats __percpu *cpustats; /* per cpu counters */ 437 spinlock_t lock; /* spin lock */ 438 struct ip_vs_kstats kstats0; /* reset values */ 439 }; 440 441 struct ip_vs_stats_rcu { 442 struct ip_vs_stats s; 443 struct rcu_head rcu_head; 444 }; 445 446 int ip_vs_stats_init_alloc(struct ip_vs_stats *s); 447 struct ip_vs_stats *ip_vs_stats_alloc(void); 448 void ip_vs_stats_release(struct ip_vs_stats *stats); 449 void ip_vs_stats_free(struct ip_vs_stats *stats); 450 451 /* Process estimators in multiple timer ticks (20/50/100, see ktrow) */ 452 #define IPVS_EST_NTICKS 50 453 /* Estimation uses a 2-second period containing ticks (in jiffies) */ 454 #define IPVS_EST_TICK ((2 * HZ) / IPVS_EST_NTICKS) 455 456 /* Limit of CPU load per kthread (8 for 12.5%), ratio of CPU capacity (1/C). 457 * Value of 4 and above ensures kthreads will take work without exceeding 458 * the CPU capacity under different circumstances. 459 */ 460 #define IPVS_EST_LOAD_DIVISOR 8 461 462 /* Kthreads should not have work that exceeds the CPU load above 50% */ 463 #define IPVS_EST_CPU_KTHREADS (IPVS_EST_LOAD_DIVISOR / 2) 464 465 /* Desired number of chains per timer tick (chain load factor in 100us units), 466 * 48=4.8ms of 40ms tick (12% CPU usage): 467 * 2 sec * 1000 ms in sec * 10 (100us in ms) / 8 (12.5%) / 50 468 */ 469 #define IPVS_EST_CHAIN_FACTOR \ 470 ALIGN_DOWN(2 * 1000 * 10 / IPVS_EST_LOAD_DIVISOR / IPVS_EST_NTICKS, 8) 471 472 /* Compiled number of chains per tick 473 * The defines should match cond_resched_rcu 474 */ 475 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU) 476 #define IPVS_EST_TICK_CHAINS IPVS_EST_CHAIN_FACTOR 477 #else 478 #define IPVS_EST_TICK_CHAINS 1 479 #endif 480 481 #if IPVS_EST_NTICKS > 127 482 #error Too many timer ticks for ktrow 483 #endif 484 485 /* Multiple chains processed in same tick */ 486 struct ip_vs_est_tick_data { 487 struct rcu_head rcu_head; 488 struct hlist_head chains[IPVS_EST_TICK_CHAINS]; 489 DECLARE_BITMAP(present, IPVS_EST_TICK_CHAINS); 490 DECLARE_BITMAP(full, IPVS_EST_TICK_CHAINS); 491 int chain_len[IPVS_EST_TICK_CHAINS]; 492 }; 493 494 /* Context for estimation kthread */ 495 struct ip_vs_est_kt_data { 496 struct netns_ipvs *ipvs; 497 struct task_struct *task; /* task if running */ 498 struct ip_vs_est_tick_data __rcu *ticks[IPVS_EST_NTICKS]; 499 DECLARE_BITMAP(avail, IPVS_EST_NTICKS); /* tick has space for ests */ 500 unsigned long est_timer; /* estimation timer (jiffies) */ 501 struct ip_vs_stats *calc_stats; /* Used for calculation */ 502 int needed; /* task is needed */ 503 int tick_len[IPVS_EST_NTICKS]; /* est count */ 504 int id; /* ktid per netns */ 505 int chain_max; /* max ests per tick chain */ 506 int tick_max; /* max ests per tick */ 507 int est_count; /* attached ests to kthread */ 508 int est_max_count; /* max ests per kthread */ 509 int add_row; /* row for new ests */ 510 int est_row; /* estimated row */ 511 }; 512 513 /* IPVS resizable hash tables */ 514 struct ip_vs_rht { 515 struct hlist_bl_head *buckets; 516 struct ip_vs_rht __rcu *new_tbl; /* New/Same table */ 517 seqcount_t *seqc; /* Protects moves */ 518 struct ip_vs_aligned_lock *lock; /* Protect seqc */ 519 int mask; /* Buckets mask */ 520 int size; /* Buckets */ 521 int seqc_mask; /* seqc mask */ 522 int lock_mask; /* lock mask */ 523 u32 table_id; 524 int u_thresh; /* upper threshold */ 525 int l_thresh; /* lower threshold */ 526 int lfactor; /* Load Factor (shift)*/ 527 int bits; /* size = 1 << bits */ 528 siphash_key_t hash_key; 529 struct rcu_head rcu_head; 530 }; 531 532 /** 533 * ip_vs_rht_for_each_table() - Walk the hash tables 534 * @table: struct ip_vs_rht __rcu *table 535 * @t: current table, used as cursor, struct ip_vs_rht *var 536 * @p: previous table, temp struct ip_vs_rht *var 537 * 538 * Walk tables assuming others can not change the installed tables 539 */ 540 #define ip_vs_rht_for_each_table(table, t, p) \ 541 for (p = NULL, t = rcu_dereference_protected(table, 1); \ 542 t != p; \ 543 p = t, t = rcu_dereference_protected(t->new_tbl, 1)) 544 545 /** 546 * ip_vs_rht_for_each_table_rcu() - Walk the hash tables under RCU reader lock 547 * @table: struct ip_vs_rht __rcu *table 548 * @t: current table, used as cursor, struct ip_vs_rht *var 549 * @p: previous table, temp struct ip_vs_rht *var 550 * 551 * We usually search in one table and also in second table on resizing 552 */ 553 #define ip_vs_rht_for_each_table_rcu(table, t, p) \ 554 for (p = NULL, t = rcu_dereference(table); \ 555 t != p; \ 556 p = t, t = rcu_dereference(t->new_tbl)) 557 558 /** 559 * ip_vs_rht_for_each_bucket() - Walk all table buckets 560 * @t: current table, used as cursor, struct ip_vs_rht *var 561 * @bucket: bucket index, used as cursor, u32 var 562 * @head: bucket address, used as cursor, struct hlist_bl_head *var 563 */ 564 #define ip_vs_rht_for_each_bucket(t, bucket, head) \ 565 for (bucket = 0, head = (t)->buckets; \ 566 bucket < t->size; bucket++, head++) 567 568 /** 569 * ip_vs_rht_for_bucket_retry() - Retry bucket if entries are moved 570 * @t: current table, used as cursor, struct ip_vs_rht *var 571 * @bucket: index of current bucket or hash key 572 * @sc: temp seqcount_t *var 573 * @seq: temp unsigned int var for sequence count 574 * @retry: temp int var 575 */ 576 #define ip_vs_rht_for_bucket_retry(t, bucket, sc, seq, retry) \ 577 for (retry = 1, sc = &(t)->seqc[(bucket) & (t)->seqc_mask]; \ 578 retry && ({ seq = read_seqcount_begin(sc); 1; }); \ 579 retry = read_seqcount_retry(sc, seq)) 580 581 /** 582 * DECLARE_IP_VS_RHT_WALK_BUCKETS_RCU() - Declare variables 583 * 584 * Variables for ip_vs_rht_walk_buckets_rcu 585 */ 586 #define DECLARE_IP_VS_RHT_WALK_BUCKETS_RCU() \ 587 struct ip_vs_rht *_t, *_p; \ 588 unsigned int _seq; \ 589 seqcount_t *_sc; \ 590 u32 _bucket; \ 591 int _retry 592 /** 593 * ip_vs_rht_walk_buckets_rcu() - Walk all buckets under RCU read lock 594 * @table: struct ip_vs_rht __rcu *table 595 * @head: bucket address, used as cursor, struct hlist_bl_head *var 596 * 597 * Can be used while others add/delete/move entries 598 * Not suitable if duplicates are not desired 599 * Possible cases for reader that uses cond_resched_rcu() in the loop: 600 * - new table can not be installed, no need to repeat 601 * - new table can be installed => check and repeat if new table is 602 * installed, needed for !PREEMPT_RCU 603 */ 604 #define ip_vs_rht_walk_buckets_rcu(table, head) \ 605 ip_vs_rht_for_each_table_rcu(table, _t, _p) \ 606 ip_vs_rht_for_each_bucket(_t, _bucket, head) \ 607 ip_vs_rht_for_bucket_retry(_t, _bucket, _sc, \ 608 _seq, _retry) 609 610 /** 611 * DECLARE_IP_VS_RHT_WALK_BUCKET_RCU() - Declare variables 612 * 613 * Variables for ip_vs_rht_walk_bucket_rcu 614 */ 615 #define DECLARE_IP_VS_RHT_WALK_BUCKET_RCU() \ 616 unsigned int _seq; \ 617 seqcount_t *_sc; \ 618 int _retry 619 /** 620 * ip_vs_rht_walk_bucket_rcu() - Walk bucket under RCU read lock 621 * @t: current table, struct ip_vs_rht *var 622 * @bucket: index of current bucket or hash key 623 * @head: bucket address, used as cursor, struct hlist_bl_head *var 624 * 625 * Can be used while others add/delete/move entries 626 * Not suitable if duplicates are not desired 627 * Possible cases for reader that uses cond_resched_rcu() in the loop: 628 * - new table can not be installed, no need to repeat 629 * - new table can be installed => check and repeat if new table is 630 * installed, needed for !PREEMPT_RCU 631 */ 632 #define ip_vs_rht_walk_bucket_rcu(t, bucket, head) \ 633 if (({ head = (t)->buckets + ((bucket) & (t)->mask); 0; })) \ 634 {} \ 635 else \ 636 ip_vs_rht_for_bucket_retry(t, (bucket), _sc, _seq, _retry) 637 638 /** 639 * DECLARE_IP_VS_RHT_WALK_BUCKETS_SAFE_RCU() - Declare variables 640 * 641 * Variables for ip_vs_rht_walk_buckets_safe_rcu 642 */ 643 #define DECLARE_IP_VS_RHT_WALK_BUCKETS_SAFE_RCU() \ 644 struct ip_vs_rht *_t, *_p; \ 645 u32 _bucket 646 /** 647 * ip_vs_rht_walk_buckets_safe_rcu() - Walk all buckets under RCU read lock 648 * @table: struct ip_vs_rht __rcu *table 649 * @head: bucket address, used as cursor, struct hlist_bl_head *var 650 * 651 * Can be used while others add/delete entries but moving is disabled 652 * Using cond_resched_rcu() should be safe if tables do not change 653 */ 654 #define ip_vs_rht_walk_buckets_safe_rcu(table, head) \ 655 ip_vs_rht_for_each_table_rcu(table, _t, _p) \ 656 ip_vs_rht_for_each_bucket(_t, _bucket, head) 657 658 /** 659 * DECLARE_IP_VS_RHT_WALK_BUCKETS() - Declare variables 660 * 661 * Variables for ip_vs_rht_walk_buckets 662 */ 663 #define DECLARE_IP_VS_RHT_WALK_BUCKETS() \ 664 struct ip_vs_rht *_t, *_p; \ 665 u32 _bucket 666 667 /** 668 * ip_vs_rht_walk_buckets() - Walk all buckets 669 * @table: struct ip_vs_rht __rcu *table 670 * @head: bucket address, used as cursor, struct hlist_bl_head *var 671 * 672 * Use if others can not add/delete/move entries 673 */ 674 #define ip_vs_rht_walk_buckets(table, head) \ 675 ip_vs_rht_for_each_table(table, _t, _p) \ 676 ip_vs_rht_for_each_bucket(_t, _bucket, head) 677 678 /* Entries can be in one of two tables, so we flip bit when new table is 679 * created and store it as highest bit in hash keys 680 */ 681 #define IP_VS_RHT_TABLE_ID_MASK BIT(31) 682 683 /* Check if hash key is from this table */ 684 static inline bool ip_vs_rht_same_table(struct ip_vs_rht *t, u32 hash_key) 685 { 686 return !((t->table_id ^ hash_key) & IP_VS_RHT_TABLE_ID_MASK); 687 } 688 689 /* Build per-table hash key from hash value */ 690 static inline u32 ip_vs_rht_build_hash_key(struct ip_vs_rht *t, u32 hash) 691 { 692 return t->table_id | (hash & ~IP_VS_RHT_TABLE_ID_MASK); 693 } 694 695 void ip_vs_rht_free(struct ip_vs_rht *t); 696 void ip_vs_rht_rcu_free(struct rcu_head *head); 697 struct ip_vs_rht *ip_vs_rht_alloc(int buckets, int scounts, int locks); 698 int ip_vs_rht_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t, int n, 699 int lfactor, int min_bits, int max_bits); 700 void ip_vs_rht_set_thresholds(struct ip_vs_rht *t, int size, int lfactor, 701 int min_bits, int max_bits); 702 u32 ip_vs_rht_hash_linfo(struct ip_vs_rht *t, int af, 703 const union nf_inet_addr *addr, u32 v1, u32 v2); 704 705 struct dst_entry; 706 struct iphdr; 707 struct ip_vs_conn; 708 struct ip_vs_app; 709 struct sk_buff; 710 struct ip_vs_proto_data; 711 712 struct ip_vs_protocol { 713 struct ip_vs_protocol *next; 714 char *name; 715 u16 protocol; 716 u16 num_states; 717 int dont_defrag; 718 719 void (*init)(struct ip_vs_protocol *pp); 720 721 void (*exit)(struct ip_vs_protocol *pp); 722 723 int (*init_netns)(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd); 724 725 void (*exit_netns)(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd); 726 727 int (*conn_schedule)(struct netns_ipvs *ipvs, 728 int af, struct sk_buff *skb, 729 struct ip_vs_proto_data *pd, 730 int *verdict, struct ip_vs_conn **cpp, 731 struct ip_vs_iphdr *iph); 732 733 struct ip_vs_conn * 734 (*conn_in_get)(struct netns_ipvs *ipvs, 735 int af, 736 const struct sk_buff *skb, 737 const struct ip_vs_iphdr *iph); 738 739 struct ip_vs_conn * 740 (*conn_out_get)(struct netns_ipvs *ipvs, 741 int af, 742 const struct sk_buff *skb, 743 const struct ip_vs_iphdr *iph); 744 745 int (*snat_handler)(struct sk_buff *skb, struct ip_vs_protocol *pp, 746 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph); 747 748 int (*dnat_handler)(struct sk_buff *skb, struct ip_vs_protocol *pp, 749 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph); 750 751 const char *(*state_name)(int state); 752 753 void (*state_transition)(struct ip_vs_conn *cp, int direction, 754 const struct sk_buff *skb, 755 struct ip_vs_proto_data *pd, 756 unsigned int iph_len); 757 758 int (*register_app)(struct netns_ipvs *ipvs, struct ip_vs_app *inc); 759 760 void (*unregister_app)(struct netns_ipvs *ipvs, struct ip_vs_app *inc); 761 762 int (*app_conn_bind)(struct ip_vs_conn *cp); 763 764 void (*debug_packet)(int af, struct ip_vs_protocol *pp, 765 const struct sk_buff *skb, 766 int offset, 767 const char *msg); 768 769 void (*timeout_change)(struct ip_vs_proto_data *pd, int flags); 770 }; 771 772 /* protocol data per netns */ 773 struct ip_vs_proto_data { 774 struct ip_vs_proto_data *next; 775 struct ip_vs_protocol *pp; 776 int *timeout_table; /* protocol timeout table */ 777 atomic_t appcnt; /* counter of proto app incs. */ 778 struct tcp_states_t *tcp_state_table; 779 }; 780 781 struct ip_vs_protocol *ip_vs_proto_get(unsigned short proto); 782 struct ip_vs_proto_data *ip_vs_proto_data_get(struct netns_ipvs *ipvs, 783 unsigned short proto); 784 785 struct ip_vs_conn_param { 786 struct netns_ipvs *ipvs; 787 const union nf_inet_addr *caddr; 788 const union nf_inet_addr *vaddr; 789 __be16 cport; 790 __be16 vport; 791 __u16 protocol; 792 u16 af; 793 794 const struct ip_vs_pe *pe; 795 char *pe_data; 796 __u8 pe_data_len; 797 }; 798 799 /* Hash node in conn_tab */ 800 struct ip_vs_conn_hnode { 801 struct hlist_bl_node node; /* node in conn_tab */ 802 u32 hash_key; /* Key for the hash table */ 803 u8 dir; /* 0=out->in, 1=in->out */ 804 } __packed; 805 806 /* IP_VS structure allocated for each dynamically scheduled connection */ 807 struct ip_vs_conn { 808 /* Cacheline for hash table nodes - rarely modified */ 809 810 struct ip_vs_conn_hnode hn0; /* Original direction */ 811 u8 af; /* address family */ 812 __be16 cport; 813 struct ip_vs_conn_hnode hn1; /* Reply direction */ 814 u8 daf; /* Address family of the dest */ 815 __be16 dport; 816 struct ip_vs_dest *dest; /* real server */ 817 atomic_t n_control; /* Number of controlled ones */ 818 volatile __u32 flags; /* status flags */ 819 /* 44/64 */ 820 821 struct ip_vs_conn *control; /* Master control connection */ 822 const struct ip_vs_pe *pe; 823 char *pe_data; 824 __u8 pe_data_len; 825 volatile __u16 state; /* state info */ 826 volatile __u16 old_state; /* old state, to be used for 827 * state transition triggered 828 * synchronization 829 */ 830 /* 2-byte hole */ 831 /* 64/96 */ 832 833 union nf_inet_addr caddr; /* client address */ 834 union nf_inet_addr vaddr; /* virtual address */ 835 /* 96/128 */ 836 837 union nf_inet_addr daddr; /* destination address */ 838 __u32 fwmark; /* Fire wall mark from skb */ 839 __be16 vport; 840 __u16 protocol; /* Which protocol (TCP/UDP) */ 841 842 /* Note: we can group the following members into a structure, 843 * in order to save more space, and the following members are 844 * only used in VS/NAT anyway 845 */ 846 struct ip_vs_app *app; /* bound ip_vs_app object */ 847 void *app_data; /* Application private data */ 848 /* 128/168 */ 849 struct_group(sync_conn_opt, 850 struct ip_vs_seq in_seq; /* incoming seq. struct */ 851 struct ip_vs_seq out_seq; /* outgoing seq. struct */ 852 ); 853 /* 152/192 */ 854 855 struct timer_list timer; /* Expiration timer */ 856 volatile unsigned long timeout; /* timeout */ 857 spinlock_t lock; /* lock for state transition */ 858 refcount_t refcnt; /* reference count */ 859 atomic_t in_pkts; /* incoming packet counter */ 860 /* 64-bit: 4-byte gap */ 861 862 /* 188/256 */ 863 unsigned long sync_endtime; /* jiffies + sent_retries */ 864 struct netns_ipvs *ipvs; 865 866 /* Packet transmitter for different forwarding methods. If it 867 * mangles the packet, it must return NF_DROP or better NF_STOLEN, 868 * otherwise this must be changed to a sk_buff **. 869 * NF_ACCEPT can be returned when destination is local. 870 */ 871 int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp, 872 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph); 873 874 struct rcu_head rcu_head; 875 }; 876 877 /* Extended internal versions of struct ip_vs_service_user and ip_vs_dest_user 878 * for IPv6 support. 879 * 880 * We need these to conveniently pass around service and destination 881 * options, but unfortunately, we also need to keep the old definitions to 882 * maintain userspace backwards compatibility for the setsockopt interface. 883 */ 884 struct ip_vs_service_user_kern { 885 /* virtual service addresses */ 886 u16 af; 887 u16 protocol; 888 union nf_inet_addr addr; /* virtual ip address */ 889 __be16 port; 890 u32 fwmark; /* firewall mark of service */ 891 892 /* virtual service options */ 893 char *sched_name; 894 char *pe_name; 895 unsigned int flags; /* virtual service flags */ 896 unsigned int timeout; /* persistent timeout in sec */ 897 __be32 netmask; /* persistent netmask or plen */ 898 }; 899 900 901 struct ip_vs_dest_user_kern { 902 /* destination server address */ 903 union nf_inet_addr addr; 904 __be16 port; 905 906 /* real server options */ 907 unsigned int conn_flags; /* connection flags */ 908 int weight; /* destination weight */ 909 910 /* thresholds for active connections */ 911 u32 u_threshold; /* upper threshold */ 912 u32 l_threshold; /* lower threshold */ 913 914 /* Address family of addr */ 915 u16 af; 916 917 u16 tun_type; /* tunnel type */ 918 __be16 tun_port; /* tunnel port */ 919 u16 tun_flags; /* tunnel flags */ 920 }; 921 922 923 /* 924 * The information about the virtual service offered to the net and the 925 * forwarding entries. 926 */ 927 struct ip_vs_service { 928 struct hlist_bl_node s_list; /* node in service table */ 929 u32 hash_key; /* Key for the hash table */ 930 u16 af; /* address family */ 931 __u16 protocol; /* which protocol (TCP/UDP) */ 932 933 union nf_inet_addr addr; /* IP address for virtual service */ 934 __u32 fwmark; /* firewall mark of the service */ 935 atomic_t refcnt; /* reference counter */ 936 __be16 port; /* port number for the service */ 937 unsigned int flags; /* service status flags */ 938 unsigned int timeout; /* persistent timeout in ticks */ 939 __be32 netmask; /* grouping granularity, mask/plen */ 940 struct netns_ipvs *ipvs; 941 942 struct list_head destinations; /* real server d-linked list */ 943 __u32 num_dests; /* number of servers */ 944 struct ip_vs_stats stats; /* statistics for the service */ 945 946 /* for scheduling */ 947 struct ip_vs_scheduler __rcu *scheduler; /* bound scheduler object */ 948 spinlock_t sched_lock; /* lock sched_data */ 949 void *sched_data; /* scheduler application data */ 950 951 /* alternate persistence engine */ 952 struct ip_vs_pe __rcu *pe; 953 int conntrack_afmask; 954 955 struct rcu_head rcu_head; 956 }; 957 958 /* Information for cached dst */ 959 struct ip_vs_dest_dst { 960 struct dst_entry *dst_cache; /* destination cache entry */ 961 u32 dst_cookie; 962 union nf_inet_addr dst_saddr; 963 struct rcu_head rcu_head; 964 }; 965 966 /* The real server destination forwarding entry with ip address, port number, 967 * and so on. 968 */ 969 struct ip_vs_dest { 970 struct list_head n_list; /* for the dests in the service */ 971 struct hlist_node d_list; /* for table with all the dests */ 972 973 u16 af; /* address family */ 974 __be16 port; /* port number of the server */ 975 union nf_inet_addr addr; /* IP address of the server */ 976 volatile unsigned int flags; /* dest status flags */ 977 atomic_t conn_flags; /* flags to copy to conn */ 978 atomic_t weight; /* server weight */ 979 atomic_t last_weight; /* server latest weight */ 980 __u16 tun_type; /* tunnel type */ 981 __be16 tun_port; /* tunnel port */ 982 __u16 tun_flags; /* tunnel flags */ 983 984 refcount_t refcnt; /* reference counter */ 985 struct ip_vs_stats stats; /* statistics */ 986 unsigned long idle_start; /* start time, jiffies */ 987 988 /* connection counters and thresholds */ 989 atomic_t activeconns; /* active connections */ 990 atomic_t inactconns; /* inactive connections */ 991 atomic_t persistconns; /* persistent connections */ 992 __u32 u_threshold; /* upper threshold */ 993 __u32 l_threshold; /* lower threshold */ 994 995 /* for destination cache */ 996 spinlock_t dst_lock; /* lock of dst_cache */ 997 struct ip_vs_dest_dst __rcu *dest_dst; /* cached dst info */ 998 999 /* for virtual service */ 1000 struct ip_vs_service __rcu *svc; /* service it belongs to */ 1001 __u16 protocol; /* which protocol (TCP/UDP) */ 1002 __be16 vport; /* virtual port number */ 1003 union nf_inet_addr vaddr; /* virtual IP address */ 1004 __u32 vfwmark; /* firewall mark of service */ 1005 1006 struct rcu_head rcu_head; 1007 struct list_head t_list; /* in dest_trash */ 1008 unsigned int in_rs_table:1; /* we are in rs_table */ 1009 }; 1010 1011 /* The scheduler object */ 1012 struct ip_vs_scheduler { 1013 struct list_head n_list; /* d-linked list head */ 1014 char *name; /* scheduler name */ 1015 atomic_t refcnt; /* reference counter */ 1016 struct module *module; /* THIS_MODULE/NULL */ 1017 1018 /* scheduler initializing service */ 1019 int (*init_service)(struct ip_vs_service *svc); 1020 /* scheduling service finish */ 1021 void (*done_service)(struct ip_vs_service *svc); 1022 /* dest is linked */ 1023 int (*add_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest); 1024 /* dest is unlinked */ 1025 int (*del_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest); 1026 /* dest is updated */ 1027 int (*upd_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest); 1028 1029 /* selecting a server from the given service */ 1030 struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc, 1031 const struct sk_buff *skb, 1032 struct ip_vs_iphdr *iph); 1033 }; 1034 1035 /* The persistence engine object */ 1036 struct ip_vs_pe { 1037 struct list_head n_list; /* d-linked list head */ 1038 char *name; /* scheduler name */ 1039 atomic_t refcnt; /* reference counter */ 1040 struct module *module; /* THIS_MODULE/NULL */ 1041 1042 /* get the connection template, if any */ 1043 int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb); 1044 bool (*ct_match)(const struct ip_vs_conn_param *p, 1045 struct ip_vs_conn *ct); 1046 u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, 1047 struct ip_vs_rht *t, bool inverse); 1048 int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf); 1049 /* create connections for real-server outgoing packets */ 1050 struct ip_vs_conn* (*conn_out)(struct ip_vs_service *svc, 1051 struct ip_vs_dest *dest, 1052 struct sk_buff *skb, 1053 const struct ip_vs_iphdr *iph, 1054 __be16 dport, __be16 cport); 1055 }; 1056 1057 /* The application module object (a.k.a. app incarnation) */ 1058 struct ip_vs_app { 1059 struct list_head a_list; /* member in app list */ 1060 int type; /* IP_VS_APP_TYPE_xxx */ 1061 char *name; /* application module name */ 1062 __u16 protocol; 1063 struct module *module; /* THIS_MODULE/NULL */ 1064 struct list_head incs_list; /* list of incarnations */ 1065 1066 /* members for application incarnations */ 1067 struct list_head p_list; /* member in proto app list */ 1068 struct ip_vs_app *app; /* its real application */ 1069 __be16 port; /* port number in net order */ 1070 atomic_t usecnt; /* usage counter */ 1071 struct rcu_head rcu_head; 1072 1073 /* output hook: Process packet in inout direction, diff set for TCP. 1074 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok, 1075 * 2=Mangled but checksum was not updated 1076 */ 1077 int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *, 1078 struct sk_buff *, int *diff, struct ip_vs_iphdr *ipvsh); 1079 1080 /* input hook: Process packet in outin direction, diff set for TCP. 1081 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok, 1082 * 2=Mangled but checksum was not updated 1083 */ 1084 int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *, 1085 struct sk_buff *, int *diff, struct ip_vs_iphdr *ipvsh); 1086 1087 /* ip_vs_app initializer */ 1088 int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *); 1089 1090 /* ip_vs_app finish */ 1091 int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *); 1092 1093 1094 /* not used now */ 1095 int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *, 1096 struct ip_vs_protocol *); 1097 1098 void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *); 1099 1100 int * timeout_table; 1101 int * timeouts; 1102 int timeouts_size; 1103 1104 int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app, 1105 int *verdict, struct ip_vs_conn **cpp); 1106 1107 struct ip_vs_conn * 1108 (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app, 1109 const struct iphdr *iph, int inverse); 1110 1111 struct ip_vs_conn * 1112 (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app, 1113 const struct iphdr *iph, int inverse); 1114 1115 int (*state_transition)(struct ip_vs_conn *cp, int direction, 1116 const struct sk_buff *skb, 1117 struct ip_vs_app *app); 1118 1119 void (*timeout_change)(struct ip_vs_app *app, int flags); 1120 }; 1121 1122 struct ipvs_master_sync_state { 1123 struct list_head sync_queue; 1124 struct ip_vs_sync_buff *sync_buff; 1125 unsigned long sync_queue_len; 1126 unsigned int sync_queue_delay; 1127 struct delayed_work master_wakeup_work; 1128 struct netns_ipvs *ipvs; 1129 }; 1130 1131 struct ip_vs_sync_thread_data; 1132 1133 /* How much time to keep dests in trash */ 1134 #define IP_VS_DEST_TRASH_PERIOD (120 * HZ) 1135 1136 struct ipvs_sync_daemon_cfg { 1137 union nf_inet_addr mcast_group; 1138 int syncid; 1139 u16 sync_maxlen; 1140 u16 mcast_port; 1141 u8 mcast_af; 1142 u8 mcast_ttl; 1143 /* multicast interface name */ 1144 char mcast_ifn[IP_VS_IFNAME_MAXLEN]; 1145 }; 1146 1147 /* IPVS in network namespace */ 1148 struct netns_ipvs { 1149 int gen; /* Generation */ 1150 int enable; /* enable like nf_hooks do */ 1151 /* Hash table: for real service lookups */ 1152 #define IP_VS_RTAB_BITS 4 1153 #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS) 1154 #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1) 1155 1156 struct hlist_head rs_table[IP_VS_RTAB_SIZE]; 1157 /* ip_vs_app */ 1158 struct list_head app_list; 1159 /* ip_vs_proto */ 1160 #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */ 1161 struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE]; 1162 /* ip_vs_proto_tcp */ 1163 #ifdef CONFIG_IP_VS_PROTO_TCP 1164 #define TCP_APP_TAB_BITS 4 1165 #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS) 1166 #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1) 1167 struct list_head tcp_apps[TCP_APP_TAB_SIZE]; 1168 #endif 1169 /* ip_vs_proto_udp */ 1170 #ifdef CONFIG_IP_VS_PROTO_UDP 1171 #define UDP_APP_TAB_BITS 4 1172 #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS) 1173 #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1) 1174 struct list_head udp_apps[UDP_APP_TAB_SIZE]; 1175 #endif 1176 /* ip_vs_proto_sctp */ 1177 #ifdef CONFIG_IP_VS_PROTO_SCTP 1178 #define SCTP_APP_TAB_BITS 4 1179 #define SCTP_APP_TAB_SIZE (1 << SCTP_APP_TAB_BITS) 1180 #define SCTP_APP_TAB_MASK (SCTP_APP_TAB_SIZE - 1) 1181 /* Hash table for SCTP application incarnations */ 1182 struct list_head sctp_apps[SCTP_APP_TAB_SIZE]; 1183 #endif 1184 /* ip_vs_conn */ 1185 atomic_t conn_count; /* connection counter */ 1186 atomic_t no_cport_conns[IP_VS_AF_MAX]; 1187 struct delayed_work conn_resize_work;/* resize conn_tab */ 1188 1189 /* ip_vs_ctl */ 1190 struct ip_vs_stats_rcu *tot_stats; /* Statistics & est. */ 1191 1192 /* Trash for destinations */ 1193 struct list_head dest_trash; 1194 spinlock_t dest_trash_lock; 1195 struct timer_list dest_trash_timer; /* expiration timer */ 1196 struct mutex service_mutex; /* service reconfig */ 1197 struct rw_semaphore svc_resize_sem; /* svc_table resizing */ 1198 struct rw_semaphore svc_replace_sem; /* svc_table replace */ 1199 struct delayed_work svc_resize_work; /* resize svc_table */ 1200 atomic_t svc_table_changes;/* ++ on table changes */ 1201 /* Service counters */ 1202 atomic_t num_services[IP_VS_AF_MAX]; /* Services */ 1203 atomic_t fwm_services[IP_VS_AF_MAX]; /* Services */ 1204 atomic_t nonfwm_services[IP_VS_AF_MAX];/* Services */ 1205 atomic_t ftpsvc_counter[IP_VS_AF_MAX]; /* FTPPORT */ 1206 atomic_t nullsvc_counter[IP_VS_AF_MAX];/* Zero port */ 1207 atomic_t conn_out_counter[IP_VS_AF_MAX];/* out conn */ 1208 1209 #ifdef CONFIG_SYSCTL 1210 /* delayed work for expiring no dest connections */ 1211 struct delayed_work expire_nodest_conn_work; 1212 /* 1/rate drop and drop-entry variables */ 1213 struct delayed_work defense_work; /* Work handler */ 1214 int drop_rate; 1215 int drop_counter; 1216 int old_secure_tcp; 1217 atomic_t dropentry; 1218 s8 dropentry_counters[8]; 1219 /* locks in ctl.c */ 1220 spinlock_t dropentry_lock; /* drop entry handling */ 1221 spinlock_t droppacket_lock; /* drop packet handling */ 1222 spinlock_t securetcp_lock; /* state and timeout tables */ 1223 1224 /* sys-ctl struct */ 1225 struct ctl_table_header *sysctl_hdr; 1226 struct ctl_table *sysctl_tbl; 1227 #endif 1228 1229 /* sysctl variables */ 1230 int sysctl_amemthresh; 1231 int sysctl_am_droprate; 1232 #ifdef CONFIG_SYSCTL 1233 int sysctl_conn_max;/* soft limit for conns */ 1234 int conn_max_limit; /* hard limit for conn_max */ 1235 #endif 1236 int sysctl_drop_entry; 1237 int sysctl_drop_packet; 1238 int sysctl_secure_tcp; 1239 #ifdef CONFIG_IP_VS_NFCT 1240 int sysctl_conntrack; 1241 #endif 1242 int sysctl_snat_reroute; 1243 int sysctl_sync_ver; 1244 int sysctl_sync_ports; 1245 int sysctl_sync_persist_mode; 1246 unsigned long sysctl_sync_qlen_max; 1247 int sysctl_sync_sock_size; 1248 int sysctl_cache_bypass; 1249 int sysctl_expire_nodest_conn; 1250 int sysctl_sloppy_tcp; 1251 int sysctl_sloppy_sctp; 1252 int sysctl_expire_quiescent_template; 1253 int sysctl_sync_threshold[2]; 1254 unsigned int sysctl_sync_refresh_period; 1255 int sysctl_sync_retries; 1256 int sysctl_nat_icmp_send; 1257 int sysctl_pmtu_disc; 1258 int sysctl_backup_only; 1259 int sysctl_conn_reuse_mode; 1260 int sysctl_schedule_icmp; 1261 int sysctl_ignore_tunneled; 1262 int sysctl_run_estimation; 1263 #ifdef CONFIG_SYSCTL 1264 cpumask_var_t sysctl_est_cpulist; /* kthread cpumask */ 1265 int est_cpulist_valid; /* cpulist set */ 1266 int sysctl_est_nice; /* kthread nice */ 1267 int est_stopped; /* stop tasks */ 1268 #endif 1269 int sysctl_conn_lfactor; 1270 int sysctl_svc_lfactor; 1271 1272 /* ip_vs_lblc */ 1273 int sysctl_lblc_expiration; 1274 struct ctl_table_header *lblc_ctl_header; 1275 struct ctl_table *lblc_ctl_table; 1276 /* ip_vs_lblcr */ 1277 int sysctl_lblcr_expiration; 1278 struct ctl_table_header *lblcr_ctl_header; 1279 struct ctl_table *lblcr_ctl_table; 1280 unsigned long work_flags; /* IP_VS_WORK_* flags */ 1281 /* ip_vs_est */ 1282 struct delayed_work est_reload_work;/* Reload kthread tasks */ 1283 struct mutex est_mutex; /* protect kthread tasks */ 1284 struct hlist_head est_temp_list; /* Ests during calc phase */ 1285 struct ip_vs_est_kt_data **est_kt_arr; /* Array of kthread data ptrs */ 1286 unsigned long est_max_threads;/* Hard limit of kthreads */ 1287 int est_calc_phase; /* Calculation phase */ 1288 int est_chain_max; /* Calculated chain_max */ 1289 int est_kt_count; /* Allocated ptrs */ 1290 int est_add_ktid; /* ktid where to add ests */ 1291 atomic_t est_genid; /* kthreads reload genid */ 1292 atomic_t est_genid_done; /* applied genid */ 1293 /* ip_vs_sync */ 1294 spinlock_t sync_lock; 1295 struct ipvs_master_sync_state *ms; 1296 spinlock_t sync_buff_lock; 1297 struct ip_vs_sync_thread_data *master_tinfo; 1298 struct ip_vs_sync_thread_data *backup_tinfo; 1299 int threads_mask; 1300 volatile int sync_state; 1301 struct mutex sync_mutex; 1302 struct ipvs_sync_daemon_cfg mcfg; /* Master Configuration */ 1303 struct ipvs_sync_daemon_cfg bcfg; /* Backup Configuration */ 1304 /* net name space ptr */ 1305 struct net *net; /* Needed by timer routines */ 1306 /* Number of heterogeneous destinations, needed because heterogeneous 1307 * are not supported when synchronization is enabled. 1308 */ 1309 unsigned int mixed_address_family_dests; 1310 unsigned int hooks_afmask; /* &1=AF_INET, &2=AF_INET6 */ 1311 1312 struct ip_vs_rht __rcu *svc_table; /* Services */ 1313 struct ip_vs_rht __rcu *conn_tab; /* Connections */ 1314 atomic_t conn_tab_changes;/* ++ on new table */ 1315 }; 1316 1317 #define DEFAULT_SYNC_THRESHOLD 3 1318 #define DEFAULT_SYNC_PERIOD 50 1319 #define DEFAULT_SYNC_VER 1 1320 #define DEFAULT_SLOPPY_TCP 0 1321 #define DEFAULT_SLOPPY_SCTP 0 1322 #define DEFAULT_SYNC_REFRESH_PERIOD (0U * HZ) 1323 #define DEFAULT_SYNC_RETRIES 0 1324 #define IPVS_SYNC_WAKEUP_RATE 8 1325 #define IPVS_SYNC_QLEN_MAX (IPVS_SYNC_WAKEUP_RATE * 4) 1326 #define IPVS_SYNC_SEND_DELAY (HZ / 50) 1327 #define IPVS_SYNC_CHECK_PERIOD HZ 1328 #define IPVS_SYNC_FLUSH_TIME (HZ * 2) 1329 #define IPVS_SYNC_PORTS_MAX (1 << 6) 1330 1331 #ifdef CONFIG_SYSCTL 1332 1333 static inline int sysctl_conn_max(struct netns_ipvs *ipvs) 1334 { 1335 return READ_ONCE(ipvs->sysctl_conn_max); 1336 } 1337 1338 static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs) 1339 { 1340 return ipvs->sysctl_sync_threshold[0]; 1341 } 1342 1343 static inline int sysctl_sync_period(struct netns_ipvs *ipvs) 1344 { 1345 return READ_ONCE(ipvs->sysctl_sync_threshold[1]); 1346 } 1347 1348 static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs) 1349 { 1350 return READ_ONCE(ipvs->sysctl_sync_refresh_period); 1351 } 1352 1353 static inline int sysctl_sync_retries(struct netns_ipvs *ipvs) 1354 { 1355 return ipvs->sysctl_sync_retries; 1356 } 1357 1358 static inline int sysctl_sync_ver(struct netns_ipvs *ipvs) 1359 { 1360 return ipvs->sysctl_sync_ver; 1361 } 1362 1363 static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs) 1364 { 1365 return ipvs->sysctl_sloppy_tcp; 1366 } 1367 1368 static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs) 1369 { 1370 return ipvs->sysctl_sloppy_sctp; 1371 } 1372 1373 static inline int sysctl_sync_ports(struct netns_ipvs *ipvs) 1374 { 1375 return READ_ONCE(ipvs->sysctl_sync_ports); 1376 } 1377 1378 static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs) 1379 { 1380 return ipvs->sysctl_sync_persist_mode; 1381 } 1382 1383 static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs) 1384 { 1385 return ipvs->sysctl_sync_qlen_max; 1386 } 1387 1388 static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs) 1389 { 1390 return ipvs->sysctl_sync_sock_size; 1391 } 1392 1393 static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs) 1394 { 1395 return ipvs->sysctl_pmtu_disc; 1396 } 1397 1398 static inline int sysctl_backup_only(struct netns_ipvs *ipvs) 1399 { 1400 return ipvs->sync_state & IP_VS_STATE_BACKUP && 1401 ipvs->sysctl_backup_only; 1402 } 1403 1404 static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs) 1405 { 1406 return ipvs->sysctl_conn_reuse_mode; 1407 } 1408 1409 static inline int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) 1410 { 1411 return ipvs->sysctl_expire_nodest_conn; 1412 } 1413 1414 static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs) 1415 { 1416 return ipvs->sysctl_schedule_icmp; 1417 } 1418 1419 static inline int sysctl_ignore_tunneled(struct netns_ipvs *ipvs) 1420 { 1421 return ipvs->sysctl_ignore_tunneled; 1422 } 1423 1424 static inline int sysctl_cache_bypass(struct netns_ipvs *ipvs) 1425 { 1426 return ipvs->sysctl_cache_bypass; 1427 } 1428 1429 static inline int sysctl_run_estimation(struct netns_ipvs *ipvs) 1430 { 1431 return ipvs->sysctl_run_estimation; 1432 } 1433 1434 static inline const struct cpumask *__sysctl_est_cpulist(struct netns_ipvs *ipvs) 1435 { 1436 if (ipvs->est_cpulist_valid) 1437 return ipvs->sysctl_est_cpulist; 1438 else 1439 return housekeeping_cpumask(HK_TYPE_KTHREAD); 1440 } 1441 1442 static inline const struct cpumask *sysctl_est_preferred_cpulist(struct netns_ipvs *ipvs) 1443 { 1444 if (ipvs->est_cpulist_valid) 1445 return ipvs->sysctl_est_cpulist; 1446 else 1447 return NULL; 1448 } 1449 1450 static inline int sysctl_est_nice(struct netns_ipvs *ipvs) 1451 { 1452 return ipvs->sysctl_est_nice; 1453 } 1454 1455 #else 1456 1457 static inline int sysctl_conn_max(struct netns_ipvs *ipvs) 1458 { 1459 return IP_VS_CONN_MAX; 1460 } 1461 1462 static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs) 1463 { 1464 return DEFAULT_SYNC_THRESHOLD; 1465 } 1466 1467 static inline int sysctl_sync_period(struct netns_ipvs *ipvs) 1468 { 1469 return DEFAULT_SYNC_PERIOD; 1470 } 1471 1472 static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs) 1473 { 1474 return DEFAULT_SYNC_REFRESH_PERIOD; 1475 } 1476 1477 static inline int sysctl_sync_retries(struct netns_ipvs *ipvs) 1478 { 1479 return DEFAULT_SYNC_RETRIES & 3; 1480 } 1481 1482 static inline int sysctl_sync_ver(struct netns_ipvs *ipvs) 1483 { 1484 return DEFAULT_SYNC_VER; 1485 } 1486 1487 static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs) 1488 { 1489 return DEFAULT_SLOPPY_TCP; 1490 } 1491 1492 static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs) 1493 { 1494 return DEFAULT_SLOPPY_SCTP; 1495 } 1496 1497 static inline int sysctl_sync_ports(struct netns_ipvs *ipvs) 1498 { 1499 return 1; 1500 } 1501 1502 static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs) 1503 { 1504 return 0; 1505 } 1506 1507 static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs) 1508 { 1509 return IPVS_SYNC_QLEN_MAX; 1510 } 1511 1512 static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs) 1513 { 1514 return 0; 1515 } 1516 1517 static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs) 1518 { 1519 return 1; 1520 } 1521 1522 static inline int sysctl_backup_only(struct netns_ipvs *ipvs) 1523 { 1524 return 0; 1525 } 1526 1527 static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs) 1528 { 1529 return 1; 1530 } 1531 1532 static inline int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) 1533 { 1534 return 0; 1535 } 1536 1537 static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs) 1538 { 1539 return 0; 1540 } 1541 1542 static inline int sysctl_ignore_tunneled(struct netns_ipvs *ipvs) 1543 { 1544 return 0; 1545 } 1546 1547 static inline int sysctl_cache_bypass(struct netns_ipvs *ipvs) 1548 { 1549 return 0; 1550 } 1551 1552 static inline int sysctl_run_estimation(struct netns_ipvs *ipvs) 1553 { 1554 return 1; 1555 } 1556 1557 static inline const struct cpumask *__sysctl_est_cpulist(struct netns_ipvs *ipvs) 1558 { 1559 return housekeeping_cpumask(HK_TYPE_KTHREAD); 1560 } 1561 1562 static inline const struct cpumask *sysctl_est_preferred_cpulist(struct netns_ipvs *ipvs) 1563 { 1564 return NULL; 1565 } 1566 1567 static inline int sysctl_est_nice(struct netns_ipvs *ipvs) 1568 { 1569 return IPVS_EST_NICE; 1570 } 1571 1572 #endif 1573 1574 /* Get load factor to map conn_count/u_thresh to t->size */ 1575 static inline int sysctl_conn_lfactor(struct netns_ipvs *ipvs) 1576 { 1577 return READ_ONCE(ipvs->sysctl_conn_lfactor); 1578 } 1579 1580 /* Get load factor to map num_services/u_thresh to t->size 1581 * Smaller value decreases u_thresh to reduce collisions but increases 1582 * the table size 1583 * Returns factor where: 1584 * - <0: u_thresh = size >> -factor, eg. lfactor -2 = 25% load 1585 * - >=0: u_thresh = size << factor, eg. lfactor 1 = 200% load 1586 */ 1587 static inline int sysctl_svc_lfactor(struct netns_ipvs *ipvs) 1588 { 1589 return READ_ONCE(ipvs->sysctl_svc_lfactor); 1590 } 1591 1592 static inline bool sysctl_est_cpulist_empty(struct netns_ipvs *ipvs) 1593 { 1594 guard(rcu)(); 1595 return cpumask_empty(__sysctl_est_cpulist(ipvs)); 1596 } 1597 1598 static inline unsigned int sysctl_est_cpulist_weight(struct netns_ipvs *ipvs) 1599 { 1600 guard(rcu)(); 1601 return cpumask_weight(__sysctl_est_cpulist(ipvs)); 1602 } 1603 1604 /* IPVS core functions 1605 * (from ip_vs_core.c) 1606 */ 1607 const char *ip_vs_proto_name(unsigned int proto); 1608 void ip_vs_init_hash_table(struct list_head *table, int rows); 1609 struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc, 1610 struct ip_vs_dest *dest, 1611 struct sk_buff *skb, 1612 const struct ip_vs_iphdr *iph, 1613 __be16 dport, 1614 __be16 cport); 1615 #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t))) 1616 1617 #define IP_VS_APP_TYPE_FTP 1 1618 1619 /* ip_vs_conn handling functions 1620 * (from ip_vs_conn.c) 1621 */ 1622 enum { 1623 IP_VS_DIR_INPUT = 0, 1624 IP_VS_DIR_OUTPUT, 1625 IP_VS_DIR_INPUT_ONLY, 1626 IP_VS_DIR_LAST, 1627 }; 1628 1629 static inline void ip_vs_conn_fill_param(struct netns_ipvs *ipvs, int af, int protocol, 1630 const union nf_inet_addr *caddr, 1631 __be16 cport, 1632 const union nf_inet_addr *vaddr, 1633 __be16 vport, 1634 struct ip_vs_conn_param *p) 1635 { 1636 p->ipvs = ipvs; 1637 p->af = af; 1638 p->protocol = protocol; 1639 p->caddr = caddr; 1640 p->cport = cport; 1641 p->vaddr = vaddr; 1642 p->vport = vport; 1643 p->pe = NULL; 1644 p->pe_data = NULL; 1645 } 1646 1647 struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p); 1648 struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p); 1649 1650 struct ip_vs_conn * ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af, 1651 const struct sk_buff *skb, 1652 const struct ip_vs_iphdr *iph); 1653 1654 struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p); 1655 1656 struct ip_vs_conn * ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af, 1657 const struct sk_buff *skb, 1658 const struct ip_vs_iphdr *iph); 1659 1660 /* Get reference to gain full access to conn. 1661 * By default, RCU read-side critical sections have access only to 1662 * conn fields and its PE data, see ip_vs_conn_rcu_free() for reference. 1663 */ 1664 static inline bool __ip_vs_conn_get(struct ip_vs_conn *cp) 1665 { 1666 return refcount_inc_not_zero(&cp->refcnt); 1667 } 1668 1669 /* put back the conn without restarting its timer */ 1670 static inline void __ip_vs_conn_put(struct ip_vs_conn *cp) 1671 { 1672 smp_mb__before_atomic(); 1673 refcount_dec(&cp->refcnt); 1674 } 1675 void ip_vs_conn_put(struct ip_vs_conn *cp); 1676 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport); 1677 int ip_vs_conn_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t, 1678 int lfactor); 1679 struct ip_vs_rht *ip_vs_conn_tab_alloc(struct netns_ipvs *ipvs, int buckets, 1680 int lfactor); 1681 1682 static inline struct ip_vs_conn * 1683 ip_vs_hn0_to_conn(struct ip_vs_conn_hnode *hn) 1684 { 1685 return container_of(hn, struct ip_vs_conn, hn0); 1686 } 1687 1688 static inline struct ip_vs_conn * 1689 ip_vs_hn_to_conn(struct ip_vs_conn_hnode *hn) 1690 { 1691 return hn->dir ? container_of(hn, struct ip_vs_conn, hn1) : 1692 container_of(hn, struct ip_vs_conn, hn0); 1693 } 1694 1695 struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af, 1696 const union nf_inet_addr *daddr, 1697 __be16 dport, unsigned int flags, 1698 struct ip_vs_dest *dest, __u32 fwmark); 1699 void ip_vs_conn_expire_now(struct ip_vs_conn *cp); 1700 1701 const char *ip_vs_state_name(const struct ip_vs_conn *cp); 1702 1703 void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp); 1704 int ip_vs_check_template(struct ip_vs_conn *ct, struct ip_vs_dest *cdest); 1705 void ip_vs_random_dropentry(struct netns_ipvs *ipvs); 1706 int ip_vs_conn_init(void); 1707 void ip_vs_conn_cleanup(void); 1708 1709 static inline void ip_vs_control_del(struct ip_vs_conn *cp) 1710 { 1711 struct ip_vs_conn *ctl_cp = cp->control; 1712 if (!ctl_cp) { 1713 IP_VS_ERR_BUF("request control DEL for uncontrolled: " 1714 "%s:%d to %s:%d\n", 1715 IP_VS_DBG_ADDR(cp->af, &cp->caddr), 1716 ntohs(cp->cport), 1717 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), 1718 ntohs(cp->vport)); 1719 1720 return; 1721 } 1722 1723 IP_VS_DBG_BUF(7, "DELeting control for: " 1724 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n", 1725 IP_VS_DBG_ADDR(cp->af, &cp->caddr), 1726 ntohs(cp->cport), 1727 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr), 1728 ntohs(ctl_cp->cport)); 1729 1730 cp->control = NULL; 1731 if (atomic_read(&ctl_cp->n_control) == 0) { 1732 IP_VS_ERR_BUF("BUG control DEL with n=0 : " 1733 "%s:%d to %s:%d\n", 1734 IP_VS_DBG_ADDR(cp->af, &cp->caddr), 1735 ntohs(cp->cport), 1736 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), 1737 ntohs(cp->vport)); 1738 1739 return; 1740 } 1741 atomic_dec(&ctl_cp->n_control); 1742 } 1743 1744 static inline void 1745 ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp) 1746 { 1747 if (cp->control) { 1748 IP_VS_ERR_BUF("request control ADD for already controlled: " 1749 "%s:%d to %s:%d\n", 1750 IP_VS_DBG_ADDR(cp->af, &cp->caddr), 1751 ntohs(cp->cport), 1752 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), 1753 ntohs(cp->vport)); 1754 1755 ip_vs_control_del(cp); 1756 } 1757 1758 IP_VS_DBG_BUF(7, "ADDing control for: " 1759 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n", 1760 IP_VS_DBG_ADDR(cp->af, &cp->caddr), 1761 ntohs(cp->cport), 1762 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr), 1763 ntohs(ctl_cp->cport)); 1764 1765 cp->control = ctl_cp; 1766 atomic_inc(&ctl_cp->n_control); 1767 } 1768 1769 /* Mark our template as assured */ 1770 static inline void 1771 ip_vs_control_assure_ct(struct ip_vs_conn *cp) 1772 { 1773 struct ip_vs_conn *ct = cp->control; 1774 1775 if (ct && !(ct->state & IP_VS_CTPL_S_ASSURED) && 1776 (ct->flags & IP_VS_CONN_F_TEMPLATE)) 1777 ct->state |= IP_VS_CTPL_S_ASSURED; 1778 } 1779 1780 /* IPVS netns init & cleanup functions */ 1781 int ip_vs_estimator_net_init(struct netns_ipvs *ipvs); 1782 int ip_vs_control_net_init(struct netns_ipvs *ipvs); 1783 int ip_vs_protocol_net_init(struct netns_ipvs *ipvs); 1784 int ip_vs_app_net_init(struct netns_ipvs *ipvs); 1785 int ip_vs_conn_net_init(struct netns_ipvs *ipvs); 1786 int ip_vs_sync_net_init(struct netns_ipvs *ipvs); 1787 void ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs); 1788 void ip_vs_app_net_cleanup(struct netns_ipvs *ipvs); 1789 void ip_vs_protocol_net_cleanup(struct netns_ipvs *ipvs); 1790 void ip_vs_control_net_cleanup(struct netns_ipvs *ipvs); 1791 void ip_vs_estimator_net_cleanup(struct netns_ipvs *ipvs); 1792 void ip_vs_sync_net_cleanup(struct netns_ipvs *ipvs); 1793 void ip_vs_service_nets_cleanup(struct list_head *net_list); 1794 1795 /* IPVS application functions 1796 * (from ip_vs_app.c) 1797 */ 1798 #define IP_VS_APP_MAX_PORTS 8 1799 struct ip_vs_app *register_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app); 1800 void unregister_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app); 1801 int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp); 1802 void ip_vs_unbind_app(struct ip_vs_conn *cp); 1803 int register_ip_vs_app_inc(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto, 1804 __u16 port); 1805 int ip_vs_app_inc_get(struct ip_vs_app *inc); 1806 void ip_vs_app_inc_put(struct ip_vs_app *inc); 1807 1808 int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb, 1809 struct ip_vs_iphdr *ipvsh); 1810 int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb, 1811 struct ip_vs_iphdr *ipvsh); 1812 1813 int register_ip_vs_pe(struct ip_vs_pe *pe); 1814 int unregister_ip_vs_pe(struct ip_vs_pe *pe); 1815 struct ip_vs_pe *ip_vs_pe_getbyname(const char *name); 1816 struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name); 1817 1818 /* Use a #define to avoid all of module.h just for these trivial ops */ 1819 #define ip_vs_pe_get(pe) \ 1820 if (pe && pe->module) \ 1821 __module_get(pe->module); 1822 1823 #define ip_vs_pe_put(pe) \ 1824 if (pe && pe->module) \ 1825 module_put(pe->module); 1826 1827 /* IPVS protocol functions (from ip_vs_proto.c) */ 1828 int ip_vs_protocol_init(void); 1829 void ip_vs_protocol_cleanup(void); 1830 void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags); 1831 int *ip_vs_create_timeout_table(int *table, int size); 1832 void ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp, 1833 const struct sk_buff *skb, int offset, 1834 const char *msg); 1835 1836 extern struct ip_vs_protocol ip_vs_protocol_tcp; 1837 extern struct ip_vs_protocol ip_vs_protocol_udp; 1838 extern struct ip_vs_protocol ip_vs_protocol_icmp; 1839 extern struct ip_vs_protocol ip_vs_protocol_esp; 1840 extern struct ip_vs_protocol ip_vs_protocol_ah; 1841 extern struct ip_vs_protocol ip_vs_protocol_sctp; 1842 1843 /* Registering/unregistering scheduler functions 1844 * (from ip_vs_sched.c) 1845 */ 1846 int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler); 1847 int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler); 1848 int ip_vs_bind_scheduler(struct ip_vs_service *svc, 1849 struct ip_vs_scheduler *scheduler); 1850 void ip_vs_unbind_scheduler(struct ip_vs_service *svc); 1851 struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name); 1852 void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler); 1853 struct ip_vs_conn * 1854 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, 1855 struct ip_vs_proto_data *pd, int *ignored, 1856 struct ip_vs_iphdr *iph); 1857 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb, 1858 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph); 1859 1860 void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg); 1861 1862 /* IPVS control data and functions (from ip_vs_ctl.c) */ 1863 extern struct ip_vs_stats ip_vs_stats; 1864 extern int sysctl_ip_vs_sync_ver; 1865 1866 struct ip_vs_service * 1867 ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol, 1868 const union nf_inet_addr *vaddr, __be16 vport); 1869 1870 bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol, 1871 const union nf_inet_addr *daddr, __be16 dport); 1872 1873 struct ip_vs_dest * 1874 ip_vs_find_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol, 1875 const union nf_inet_addr *daddr, __be16 dport); 1876 struct ip_vs_dest *ip_vs_find_tunnel(struct netns_ipvs *ipvs, int af, 1877 const union nf_inet_addr *daddr, 1878 __be16 tun_port); 1879 1880 int ip_vs_use_count_inc(void); 1881 void ip_vs_use_count_dec(void); 1882 int ip_vs_register_nl_ioctl(void); 1883 void ip_vs_unregister_nl_ioctl(void); 1884 int ip_vs_control_init(void); 1885 void ip_vs_control_cleanup(void); 1886 struct ip_vs_dest * 1887 ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af, 1888 const union nf_inet_addr *daddr, __be16 dport, 1889 const union nf_inet_addr *vaddr, __be16 vport, 1890 __u16 protocol, __u32 fwmark, __u32 flags); 1891 void ip_vs_try_bind_dest(struct ip_vs_conn *cp); 1892 1893 static inline void ip_vs_dest_hold(struct ip_vs_dest *dest) 1894 { 1895 refcount_inc(&dest->refcnt); 1896 } 1897 1898 static inline void ip_vs_dest_put(struct ip_vs_dest *dest) 1899 { 1900 smp_mb__before_atomic(); 1901 refcount_dec(&dest->refcnt); 1902 } 1903 1904 static inline void ip_vs_dest_put_and_free(struct ip_vs_dest *dest) 1905 { 1906 if (refcount_dec_and_test(&dest->refcnt)) 1907 kfree(dest); 1908 } 1909 1910 /* IPVS sync daemon data and function prototypes 1911 * (from ip_vs_sync.c) 1912 */ 1913 int start_sync_thread(struct netns_ipvs *ipvs, struct ipvs_sync_daemon_cfg *cfg, 1914 int state); 1915 int stop_sync_thread(struct netns_ipvs *ipvs, int state); 1916 void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts); 1917 1918 /* IPVS rate estimator prototypes (from ip_vs_est.c) */ 1919 int ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats); 1920 void ip_vs_stop_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats); 1921 void ip_vs_zero_estimator(struct ip_vs_stats *stats); 1922 void ip_vs_read_estimator(struct ip_vs_kstats *dst, struct ip_vs_stats *stats); 1923 void ip_vs_est_reload_start(struct netns_ipvs *ipvs, bool restart); 1924 int ip_vs_est_kthread_start(struct netns_ipvs *ipvs, 1925 struct ip_vs_est_kt_data *kd); 1926 void ip_vs_est_kthread_stop(struct ip_vs_est_kt_data *kd); 1927 1928 static inline void ip_vs_stop_estimator_tot_stats(struct netns_ipvs *ipvs) 1929 { 1930 #ifdef CONFIG_SYSCTL 1931 ip_vs_stop_estimator(ipvs, &ipvs->tot_stats->s); 1932 ipvs->tot_stats->s.est.ktid = -2; 1933 #endif 1934 } 1935 1936 static inline void ip_vs_est_stopped_recalc(struct netns_ipvs *ipvs) 1937 { 1938 #ifdef CONFIG_SYSCTL 1939 /* Stop tasks while cpulist is empty or if disabled with flag */ 1940 ipvs->est_stopped = !sysctl_run_estimation(ipvs) || 1941 (ipvs->est_cpulist_valid && 1942 sysctl_est_cpulist_empty(ipvs)); 1943 #endif 1944 } 1945 1946 static inline bool ip_vs_est_stopped(struct netns_ipvs *ipvs) 1947 { 1948 #ifdef CONFIG_SYSCTL 1949 return ipvs->est_stopped; 1950 #else 1951 return false; 1952 #endif 1953 } 1954 1955 static inline int ip_vs_est_max_threads(struct netns_ipvs *ipvs) 1956 { 1957 unsigned int limit = IPVS_EST_CPU_KTHREADS * 1958 sysctl_est_cpulist_weight(ipvs); 1959 1960 return max(1U, limit); 1961 } 1962 1963 /* Various IPVS packet transmitters (from ip_vs_xmit.c) */ 1964 int ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, 1965 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph); 1966 int ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, 1967 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph); 1968 int ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, 1969 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph); 1970 int ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, 1971 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph); 1972 int ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, 1973 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph); 1974 int ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, 1975 struct ip_vs_protocol *pp, int offset, 1976 unsigned int hooknum, struct ip_vs_iphdr *iph); 1977 void ip_vs_dest_dst_rcu_free(struct rcu_head *head); 1978 1979 #ifdef CONFIG_IP_VS_IPV6 1980 int ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, 1981 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph); 1982 int ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, 1983 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph); 1984 int ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, 1985 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph); 1986 int ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, 1987 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph); 1988 int ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, 1989 struct ip_vs_protocol *pp, int offset, 1990 unsigned int hooknum, struct ip_vs_iphdr *iph); 1991 #endif 1992 1993 #ifdef CONFIG_SYSCTL 1994 /* This is a simple mechanism to ignore packets when 1995 * we are loaded. Just set ip_vs_drop_rate to 'n' and 1996 * we start to drop 1/rate of the packets 1997 */ 1998 static inline int ip_vs_todrop(struct netns_ipvs *ipvs) 1999 { 2000 if (!ipvs->drop_rate) 2001 return 0; 2002 if (--ipvs->drop_counter > 0) 2003 return 0; 2004 ipvs->drop_counter = ipvs->drop_rate; 2005 return 1; 2006 } 2007 #else 2008 static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; } 2009 #endif 2010 2011 #ifdef CONFIG_SYSCTL 2012 /* Enqueue delayed work for expiring no dest connections 2013 * Only run when sysctl_expire_nodest=1 2014 */ 2015 static inline void ip_vs_enqueue_expire_nodest_conns(struct netns_ipvs *ipvs) 2016 { 2017 if (sysctl_expire_nodest_conn(ipvs)) 2018 queue_delayed_work(system_long_wq, 2019 &ipvs->expire_nodest_conn_work, 1); 2020 } 2021 2022 void ip_vs_expire_nodest_conn_flush(struct netns_ipvs *ipvs); 2023 #else 2024 static inline void ip_vs_enqueue_expire_nodest_conns(struct netns_ipvs *ipvs) {} 2025 #endif 2026 2027 #define IP_VS_DFWD_METHOD(dest) (atomic_read(&(dest)->conn_flags) & \ 2028 IP_VS_CONN_F_FWD_MASK) 2029 2030 /* ip_vs_fwd_tag returns the forwarding tag of the connection */ 2031 #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK) 2032 2033 static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp) 2034 { 2035 char fwd; 2036 2037 switch (IP_VS_FWD_METHOD(cp)) { 2038 case IP_VS_CONN_F_MASQ: 2039 fwd = 'M'; break; 2040 case IP_VS_CONN_F_LOCALNODE: 2041 fwd = 'L'; break; 2042 case IP_VS_CONN_F_TUNNEL: 2043 fwd = 'T'; break; 2044 case IP_VS_CONN_F_DROUTE: 2045 fwd = 'R'; break; 2046 case IP_VS_CONN_F_BYPASS: 2047 fwd = 'B'; break; 2048 default: 2049 fwd = '?'; break; 2050 } 2051 return fwd; 2052 } 2053 2054 /* Check if connection uses double hashing */ 2055 static inline bool ip_vs_conn_use_hash2(struct ip_vs_conn *cp) 2056 { 2057 return IP_VS_FWD_METHOD(cp) == IP_VS_CONN_F_MASQ && 2058 !(cp->flags & IP_VS_CONN_F_TEMPLATE); 2059 } 2060 2061 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp, 2062 struct ip_vs_conn *cp, int dir); 2063 2064 #ifdef CONFIG_IP_VS_IPV6 2065 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp, 2066 struct ip_vs_conn *cp, int dir); 2067 #endif 2068 2069 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset); 2070 2071 static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum) 2072 { 2073 __be32 diff[2] = { ~old, new }; 2074 2075 return csum_partial(diff, sizeof(diff), oldsum); 2076 } 2077 2078 #ifdef CONFIG_IP_VS_IPV6 2079 static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new, 2080 __wsum oldsum) 2081 { 2082 __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0], 2083 new[3], new[2], new[1], new[0] }; 2084 2085 return csum_partial(diff, sizeof(diff), oldsum); 2086 } 2087 #endif 2088 2089 static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum) 2090 { 2091 __be16 diff[2] = { ~old, new }; 2092 2093 return csum_partial(diff, sizeof(diff), oldsum); 2094 } 2095 2096 /* Forget current conntrack (unconfirmed) and attach notrack entry */ 2097 static inline void ip_vs_notrack(struct sk_buff *skb) 2098 { 2099 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 2100 enum ip_conntrack_info ctinfo; 2101 struct nf_conn *ct = nf_ct_get(skb, &ctinfo); 2102 2103 if (ct) { 2104 nf_conntrack_put(&ct->ct_general); 2105 nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 2106 } 2107 #endif 2108 } 2109 2110 #ifdef CONFIG_IP_VS_NFCT 2111 /* Netfilter connection tracking 2112 * (from ip_vs_nfct.c) 2113 */ 2114 static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs) 2115 { 2116 #ifdef CONFIG_SYSCTL 2117 return ipvs->sysctl_conntrack; 2118 #else 2119 return 0; 2120 #endif 2121 } 2122 2123 void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp, 2124 int outin); 2125 int ip_vs_confirm_conntrack(struct sk_buff *skb); 2126 void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct, 2127 struct ip_vs_conn *cp, u_int8_t proto, 2128 const __be16 port, int from_rs); 2129 void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp); 2130 2131 #else 2132 2133 static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs) 2134 { 2135 return 0; 2136 } 2137 2138 static inline void ip_vs_update_conntrack(struct sk_buff *skb, 2139 struct ip_vs_conn *cp, int outin) 2140 { 2141 } 2142 2143 static inline int ip_vs_confirm_conntrack(struct sk_buff *skb) 2144 { 2145 return NF_ACCEPT; 2146 } 2147 2148 static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp) 2149 { 2150 } 2151 #endif /* CONFIG_IP_VS_NFCT */ 2152 2153 /* Using old conntrack that can not be redirected to another real server? */ 2154 static inline bool ip_vs_conn_uses_old_conntrack(struct ip_vs_conn *cp, 2155 struct sk_buff *skb) 2156 { 2157 #ifdef CONFIG_IP_VS_NFCT 2158 enum ip_conntrack_info ctinfo; 2159 struct nf_conn *ct; 2160 2161 ct = nf_ct_get(skb, &ctinfo); 2162 if (ct && nf_ct_is_confirmed(ct)) 2163 return true; 2164 #endif 2165 return false; 2166 } 2167 2168 static inline int ip_vs_register_conntrack(struct ip_vs_service *svc) 2169 { 2170 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 2171 int afmask = (svc->af == AF_INET6) ? 2 : 1; 2172 int ret = 0; 2173 2174 if (!(svc->conntrack_afmask & afmask)) { 2175 ret = nf_ct_netns_get(svc->ipvs->net, svc->af); 2176 if (ret >= 0) 2177 svc->conntrack_afmask |= afmask; 2178 } 2179 return ret; 2180 #else 2181 return 0; 2182 #endif 2183 } 2184 2185 static inline void ip_vs_unregister_conntrack(struct ip_vs_service *svc) 2186 { 2187 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 2188 int afmask = (svc->af == AF_INET6) ? 2 : 1; 2189 2190 if (svc->conntrack_afmask & afmask) { 2191 nf_ct_netns_put(svc->ipvs->net, svc->af); 2192 svc->conntrack_afmask &= ~afmask; 2193 } 2194 #endif 2195 } 2196 2197 int ip_vs_register_hooks(struct netns_ipvs *ipvs, unsigned int af); 2198 void ip_vs_unregister_hooks(struct netns_ipvs *ipvs, unsigned int af); 2199 2200 static inline int 2201 ip_vs_dest_conn_overhead(struct ip_vs_dest *dest) 2202 { 2203 /* We think the overhead of processing active connections is 256 2204 * times higher than that of inactive connections in average. (This 2205 * 256 times might not be accurate, we will change it later) We 2206 * use the following formula to estimate the overhead now: 2207 * dest->activeconns*256 + dest->inactconns 2208 */ 2209 return (atomic_read(&dest->activeconns) << 8) + 2210 atomic_read(&dest->inactconns); 2211 } 2212 2213 #ifdef CONFIG_IP_VS_PROTO_TCP 2214 INDIRECT_CALLABLE_DECLARE(int 2215 tcp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp, 2216 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)); 2217 #endif 2218 2219 #ifdef CONFIG_IP_VS_PROTO_UDP 2220 INDIRECT_CALLABLE_DECLARE(int 2221 udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp, 2222 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)); 2223 #endif 2224 #endif /* _NET_IP_VS_H */ 2225