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