xref: /openbmc/linux/include/net/ip_vs.h (revision 4e857c58efeb99393cba5a5d0d8ec7117183137c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *      IP Virtual Server
31da177e4SLinus Torvalds  *      data structure and functionality definitions
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
6bc4768ebSJulius Volz #ifndef _NET_IP_VS_H
7bc4768ebSJulius Volz #define _NET_IP_VS_H
81da177e4SLinus Torvalds 
9bc4768ebSJulius Volz #include <linux/ip_vs.h>                /* definitions shared with userland */
101da177e4SLinus Torvalds 
11bc4768ebSJulius Volz #include <asm/types.h>                  /* for __uXX types */
12bc4768ebSJulius Volz 
131da177e4SLinus Torvalds #include <linux/list.h>                 /* for struct list_head */
141da177e4SLinus Torvalds #include <linux/spinlock.h>             /* for struct rwlock_t */
1560063497SArun Sharma #include <linux/atomic.h>                 /* for struct atomic_t */
161da177e4SLinus Torvalds #include <linux/compiler.h>
1714c85021SArnaldo Carvalho de Melo #include <linux/timer.h>
18187f1882SPaul Gortmaker #include <linux/bug.h>
191da177e4SLinus Torvalds 
2014c85021SArnaldo Carvalho de Melo #include <net/checksum.h>
21e7ade46aSJulius Volz #include <linux/netfilter.h>		/* for union nf_inet_addr */
221668e010SKOVACS Krisztian #include <linux/ip.h>
23e7ade46aSJulius Volz #include <linux/ipv6.h>			/* for struct ipv6hdr */
244e3fd7a0SAlexey Dobriyan #include <net/ipv6.h>
2554d83efaSJesper Dangaard Brouer #if IS_ENABLED(CONFIG_IP_VS_IPV6)
2663dca2c0SJesper Dangaard Brouer #include <linux/netfilter_ipv6/ip6_tables.h>
2763dca2c0SJesper Dangaard Brouer #endif
28a638e514SJesper Dangaard Brouer #if IS_ENABLED(CONFIG_NF_CONNTRACK)
29f4bc17cdSJulian Anastasov #include <net/netfilter/nf_conntrack.h>
30f4bc17cdSJulian Anastasov #endif
3161b1ab45SHans Schillstrom #include <net/net_namespace.h>		/* Netw namespace */
3261b1ab45SHans Schillstrom 
3361b1ab45SHans Schillstrom /*
3461b1ab45SHans Schillstrom  * Generic access of ipvs struct
3561b1ab45SHans Schillstrom  */
3661b1ab45SHans Schillstrom static inline struct netns_ipvs *net_ipvs(struct net* net)
3761b1ab45SHans Schillstrom {
3861b1ab45SHans Schillstrom 	return net->ipvs;
3961b1ab45SHans Schillstrom }
40fc723250SHans Schillstrom /*
41fc723250SHans Schillstrom  * Get net ptr from skb in traffic cases
42fc723250SHans Schillstrom  * use skb_sknet when call is from userland (ioctl or netlink)
43fc723250SHans Schillstrom  */
44a0840e2eSHans Schillstrom static inline struct net *skb_net(const struct sk_buff *skb)
45fc723250SHans Schillstrom {
46fc723250SHans Schillstrom #ifdef CONFIG_NET_NS
47fc723250SHans Schillstrom #ifdef CONFIG_IP_VS_DEBUG
48fc723250SHans Schillstrom 	/*
49fc723250SHans Schillstrom 	 * This is used for debug only.
50fc723250SHans Schillstrom 	 * Start with the most likely hit
51fc723250SHans Schillstrom 	 * End with BUG
52fc723250SHans Schillstrom 	 */
53fc723250SHans Schillstrom 	if (likely(skb->dev && skb->dev->nd_net))
54fc723250SHans Schillstrom 		return dev_net(skb->dev);
55a09d1977SHans Schillstrom 	if (skb_dst(skb) && skb_dst(skb)->dev)
56fc723250SHans Schillstrom 		return dev_net(skb_dst(skb)->dev);
57fc723250SHans Schillstrom 	WARN(skb->sk, "Maybe skb_sknet should be used in %s() at line:%d\n",
58fc723250SHans Schillstrom 		      __func__, __LINE__);
59fc723250SHans Schillstrom 	if (likely(skb->sk && skb->sk->sk_net))
60fc723250SHans Schillstrom 		return sock_net(skb->sk);
61fc723250SHans Schillstrom 	pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
62fc723250SHans Schillstrom 		__func__, __LINE__);
63fc723250SHans Schillstrom 	BUG();
64fc723250SHans Schillstrom #else
65fc723250SHans Schillstrom 	return dev_net(skb->dev ? : skb_dst(skb)->dev);
66fc723250SHans Schillstrom #endif
67fc723250SHans Schillstrom #else
68fc723250SHans Schillstrom 	return &init_net;
69fc723250SHans Schillstrom #endif
70fc723250SHans Schillstrom }
71fc723250SHans Schillstrom 
72a0840e2eSHans Schillstrom static inline struct net *skb_sknet(const struct sk_buff *skb)
73fc723250SHans Schillstrom {
74fc723250SHans Schillstrom #ifdef CONFIG_NET_NS
75fc723250SHans Schillstrom #ifdef CONFIG_IP_VS_DEBUG
76fc723250SHans Schillstrom 	/* Start with the most likely hit */
77fc723250SHans Schillstrom 	if (likely(skb->sk && skb->sk->sk_net))
78fc723250SHans Schillstrom 		return sock_net(skb->sk);
79fc723250SHans Schillstrom 	WARN(skb->dev, "Maybe skb_net should be used instead in %s() line:%d\n",
80fc723250SHans Schillstrom 		       __func__, __LINE__);
81fc723250SHans Schillstrom 	if (likely(skb->dev && skb->dev->nd_net))
82fc723250SHans Schillstrom 		return dev_net(skb->dev);
83fc723250SHans Schillstrom 	pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
84fc723250SHans Schillstrom 		__func__, __LINE__);
85fc723250SHans Schillstrom 	BUG();
86fc723250SHans Schillstrom #else
87fc723250SHans Schillstrom 	return sock_net(skb->sk);
88fc723250SHans Schillstrom #endif
89fc723250SHans Schillstrom #else
90fc723250SHans Schillstrom 	return &init_net;
91fc723250SHans Schillstrom #endif
92fc723250SHans Schillstrom }
93b17fc996SHans Schillstrom /*
94b17fc996SHans Schillstrom  * This one needed for single_open_net since net is stored directly in
9525985edcSLucas De Marchi  * private not as a struct i.e. seq_file_net can't be used.
96b17fc996SHans Schillstrom  */
97b17fc996SHans Schillstrom static inline struct net *seq_file_single_net(struct seq_file *seq)
98b17fc996SHans Schillstrom {
99b17fc996SHans Schillstrom #ifdef CONFIG_NET_NS
100b17fc996SHans Schillstrom 	return (struct net *)seq->private;
101b17fc996SHans Schillstrom #else
102b17fc996SHans Schillstrom 	return &init_net;
103b17fc996SHans Schillstrom #endif
104b17fc996SHans Schillstrom }
1056f7edb48SCatalin(ux) M. BOIE 
1066f7edb48SCatalin(ux) M. BOIE /* Connections' size value needed by ip_vs_ctl.c */
1076f7edb48SCatalin(ux) M. BOIE extern int ip_vs_conn_tab_size;
1086f7edb48SCatalin(ux) M. BOIE 
10964aae3cbSJulius Volz struct ip_vs_iphdr {
11063dca2c0SJesper Dangaard Brouer 	__u32 len;	/* IPv4 simply where L4 starts
11163dca2c0SJesper Dangaard Brouer 			   IPv6 where L4 Transport Header starts */
11263dca2c0SJesper Dangaard Brouer 	__u16 fragoffs; /* IPv6 fragment offset, 0 if first frag (or not frag)*/
11363dca2c0SJesper Dangaard Brouer 	__s16 protocol;
11463dca2c0SJesper Dangaard Brouer 	__s32 flags;
11564aae3cbSJulius Volz 	union nf_inet_addr saddr;
11664aae3cbSJulius Volz 	union nf_inet_addr daddr;
11764aae3cbSJulius Volz };
11864aae3cbSJulius Volz 
1192f74713dSJesper Dangaard Brouer static inline void *frag_safe_skb_hp(const struct sk_buff *skb, int offset,
1202f74713dSJesper Dangaard Brouer 				      int len, void *buffer,
1212f74713dSJesper Dangaard Brouer 				      const struct ip_vs_iphdr *ipvsh)
1222f74713dSJesper Dangaard Brouer {
1232f74713dSJesper Dangaard Brouer 	return skb_header_pointer(skb, offset, len, buffer);
1242f74713dSJesper Dangaard Brouer }
1252f74713dSJesper Dangaard Brouer 
12664aae3cbSJulius Volz static inline void
12763dca2c0SJesper Dangaard Brouer ip_vs_fill_ip4hdr(const void *nh, struct ip_vs_iphdr *iphdr)
12863dca2c0SJesper Dangaard Brouer {
12963dca2c0SJesper Dangaard Brouer 	const struct iphdr *iph = nh;
13063dca2c0SJesper Dangaard Brouer 
13163dca2c0SJesper Dangaard Brouer 	iphdr->len	= iph->ihl * 4;
13263dca2c0SJesper Dangaard Brouer 	iphdr->fragoffs	= 0;
13363dca2c0SJesper Dangaard Brouer 	iphdr->protocol	= iph->protocol;
13463dca2c0SJesper Dangaard Brouer 	iphdr->saddr.ip	= iph->saddr;
13563dca2c0SJesper Dangaard Brouer 	iphdr->daddr.ip	= iph->daddr;
13663dca2c0SJesper Dangaard Brouer }
13763dca2c0SJesper Dangaard Brouer 
13863dca2c0SJesper Dangaard Brouer /* This function handles filling *ip_vs_iphdr, both for IPv4 and IPv6.
13963dca2c0SJesper Dangaard Brouer  * IPv6 requires some extra work, as finding proper header position,
14063dca2c0SJesper Dangaard Brouer  * depend on the IPv6 extension headers.
14163dca2c0SJesper Dangaard Brouer  */
14263dca2c0SJesper Dangaard Brouer static inline void
14363dca2c0SJesper Dangaard Brouer ip_vs_fill_iph_skb(int af, const struct sk_buff *skb, struct ip_vs_iphdr *iphdr)
14464aae3cbSJulius Volz {
14564aae3cbSJulius Volz #ifdef CONFIG_IP_VS_IPV6
14664aae3cbSJulius Volz 	if (af == AF_INET6) {
14763dca2c0SJesper Dangaard Brouer 		const struct ipv6hdr *iph =
14863dca2c0SJesper Dangaard Brouer 			(struct ipv6hdr *)skb_network_header(skb);
1494e3fd7a0SAlexey Dobriyan 		iphdr->saddr.in6 = iph->saddr;
1504e3fd7a0SAlexey Dobriyan 		iphdr->daddr.in6 = iph->daddr;
1516aafeef0SJiri Pirko 		/* ipv6_find_hdr() updates len, flags */
15263dca2c0SJesper Dangaard Brouer 		iphdr->len	 = 0;
15363dca2c0SJesper Dangaard Brouer 		iphdr->flags	 = 0;
15463dca2c0SJesper Dangaard Brouer 		iphdr->protocol  = ipv6_find_hdr(skb, &iphdr->len, -1,
15563dca2c0SJesper Dangaard Brouer 						 &iphdr->fragoffs,
15663dca2c0SJesper Dangaard Brouer 						 &iphdr->flags);
15764aae3cbSJulius Volz 	} else
15864aae3cbSJulius Volz #endif
15964aae3cbSJulius Volz 	{
16063dca2c0SJesper Dangaard Brouer 		const struct iphdr *iph =
16163dca2c0SJesper Dangaard Brouer 			(struct iphdr *)skb_network_header(skb);
16264aae3cbSJulius Volz 		iphdr->len	= iph->ihl * 4;
16363dca2c0SJesper Dangaard Brouer 		iphdr->fragoffs	= 0;
16464aae3cbSJulius Volz 		iphdr->protocol	= iph->protocol;
16564aae3cbSJulius Volz 		iphdr->saddr.ip	= iph->saddr;
16664aae3cbSJulius Volz 		iphdr->daddr.ip	= iph->daddr;
16764aae3cbSJulius Volz 	}
16864aae3cbSJulius Volz }
16964aae3cbSJulius Volz 
17064aae3cbSJulius Volz static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
17164aae3cbSJulius Volz 				   const union nf_inet_addr *src)
17264aae3cbSJulius Volz {
17364aae3cbSJulius Volz #ifdef CONFIG_IP_VS_IPV6
17464aae3cbSJulius Volz 	if (af == AF_INET6)
1754e3fd7a0SAlexey Dobriyan 		dst->in6 = src->in6;
17664aae3cbSJulius Volz 	else
17764aae3cbSJulius Volz #endif
17864aae3cbSJulius Volz 	dst->ip = src->ip;
17964aae3cbSJulius Volz }
18064aae3cbSJulius Volz 
1819a05475cSJulian Anastasov static inline void ip_vs_addr_set(int af, union nf_inet_addr *dst,
1829a05475cSJulian Anastasov 				  const union nf_inet_addr *src)
1839a05475cSJulian Anastasov {
1849a05475cSJulian Anastasov #ifdef CONFIG_IP_VS_IPV6
1859a05475cSJulian Anastasov 	if (af == AF_INET6) {
1869a05475cSJulian Anastasov 		dst->in6 = src->in6;
1879a05475cSJulian Anastasov 		return;
1889a05475cSJulian Anastasov 	}
1899a05475cSJulian Anastasov #endif
1909a05475cSJulian Anastasov 	dst->ip = src->ip;
1919a05475cSJulian Anastasov 	dst->all[1] = 0;
1929a05475cSJulian Anastasov 	dst->all[2] = 0;
1939a05475cSJulian Anastasov 	dst->all[3] = 0;
1949a05475cSJulian Anastasov }
1959a05475cSJulian Anastasov 
19664aae3cbSJulius Volz static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
19764aae3cbSJulius Volz 				   const union nf_inet_addr *b)
19864aae3cbSJulius Volz {
19964aae3cbSJulius Volz #ifdef CONFIG_IP_VS_IPV6
20064aae3cbSJulius Volz 	if (af == AF_INET6)
20164aae3cbSJulius Volz 		return ipv6_addr_equal(&a->in6, &b->in6);
20264aae3cbSJulius Volz #endif
20364aae3cbSJulius Volz 	return a->ip == b->ip;
20464aae3cbSJulius Volz }
20564aae3cbSJulius Volz 
2061da177e4SLinus Torvalds #ifdef CONFIG_IP_VS_DEBUG
20714c85021SArnaldo Carvalho de Melo #include <linux/net.h>
20814c85021SArnaldo Carvalho de Melo 
2095c3a0fd7SJoe Perches int ip_vs_get_debug_level(void);
210c842a3adSJulius Volz 
211c842a3adSJulius Volz static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
212c842a3adSJulius Volz 					 const union nf_inet_addr *addr,
213c842a3adSJulius Volz 					 int *idx)
214c842a3adSJulius Volz {
215c842a3adSJulius Volz 	int len;
216c842a3adSJulius Volz #ifdef CONFIG_IP_VS_IPV6
217c842a3adSJulius Volz 	if (af == AF_INET6)
218120b9c14SJesper Dangaard Brouer 		len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6c]",
2190c6ce78aSHarvey Harrison 			       &addr->in6) + 1;
220c842a3adSJulius Volz 	else
221c842a3adSJulius Volz #endif
2223685f25dSHarvey Harrison 		len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
2233685f25dSHarvey Harrison 			       &addr->ip) + 1;
224c842a3adSJulius Volz 
225c842a3adSJulius Volz 	*idx += len;
226c842a3adSJulius Volz 	BUG_ON(*idx > buf_len + 1);
227c842a3adSJulius Volz 	return &buf[*idx - len];
228c842a3adSJulius Volz }
229c842a3adSJulius Volz 
2309aada7acSHannes Eder #define IP_VS_DBG_BUF(level, msg, ...)					\
231c842a3adSJulius Volz 	do {								\
232c842a3adSJulius Volz 		char ip_vs_dbg_buf[160];				\
233c842a3adSJulius Volz 		int ip_vs_dbg_idx = 0;					\
234c842a3adSJulius Volz 		if (level <= ip_vs_get_debug_level())			\
2359aada7acSHannes Eder 			printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__);	\
236c842a3adSJulius Volz 	} while (0)
237c842a3adSJulius Volz #define IP_VS_ERR_BUF(msg...)						\
238c842a3adSJulius Volz 	do {								\
239c842a3adSJulius Volz 		char ip_vs_dbg_buf[160];				\
240c842a3adSJulius Volz 		int ip_vs_dbg_idx = 0;					\
2419aada7acSHannes Eder 		pr_err(msg);						\
242c842a3adSJulius Volz 	} while (0)
243c842a3adSJulius Volz 
244c842a3adSJulius Volz /* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
245c842a3adSJulius Volz #define IP_VS_DBG_ADDR(af, addr)					\
246c842a3adSJulius Volz 	ip_vs_dbg_addr(af, ip_vs_dbg_buf,				\
247c842a3adSJulius Volz 		       sizeof(ip_vs_dbg_buf), addr,			\
248c842a3adSJulius Volz 		       &ip_vs_dbg_idx)
249c842a3adSJulius Volz 
2509aada7acSHannes Eder #define IP_VS_DBG(level, msg, ...)					\
2511da177e4SLinus Torvalds 	do {								\
2521da177e4SLinus Torvalds 		if (level <= ip_vs_get_debug_level())			\
2539aada7acSHannes Eder 			printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__);	\
2541da177e4SLinus Torvalds 	} while (0)
2559aada7acSHannes Eder #define IP_VS_DBG_RL(msg, ...)						\
2561da177e4SLinus Torvalds 	do {								\
2571da177e4SLinus Torvalds 		if (net_ratelimit())					\
2589aada7acSHannes Eder 			printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__);	\
2591da177e4SLinus Torvalds 	} while (0)
2600d79641aSJulian Anastasov #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg)			\
2611da177e4SLinus Torvalds 	do {								\
2621da177e4SLinus Torvalds 		if (level <= ip_vs_get_debug_level())			\
2630d79641aSJulian Anastasov 			pp->debug_packet(af, pp, skb, ofs, msg);	\
2641da177e4SLinus Torvalds 	} while (0)
2650d79641aSJulian Anastasov #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg)			\
2661da177e4SLinus Torvalds 	do {								\
2671da177e4SLinus Torvalds 		if (level <= ip_vs_get_debug_level() &&			\
2681da177e4SLinus Torvalds 		    net_ratelimit())					\
2690d79641aSJulian Anastasov 			pp->debug_packet(af, pp, skb, ofs, msg);	\
2701da177e4SLinus Torvalds 	} while (0)
2711da177e4SLinus Torvalds #else	/* NO DEBUGGING at ALL */
272c842a3adSJulius Volz #define IP_VS_DBG_BUF(level, msg...)  do {} while (0)
273c842a3adSJulius Volz #define IP_VS_ERR_BUF(msg...)  do {} while (0)
2741da177e4SLinus Torvalds #define IP_VS_DBG(level, msg...)  do {} while (0)
2751da177e4SLinus Torvalds #define IP_VS_DBG_RL(msg...)  do {} while (0)
2760d79641aSJulian Anastasov #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg)	do {} while (0)
2770d79641aSJulian Anastasov #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg)	do {} while (0)
2781da177e4SLinus Torvalds #endif
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds #define IP_VS_BUG() BUG()
2811e3e238eSHannes Eder #define IP_VS_ERR_RL(msg, ...)						\
2821da177e4SLinus Torvalds 	do {								\
2831da177e4SLinus Torvalds 		if (net_ratelimit())					\
2841e3e238eSHannes Eder 			pr_err(msg, ##__VA_ARGS__);			\
2851da177e4SLinus Torvalds 	} while (0)
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds #ifdef CONFIG_IP_VS_DEBUG
2881da177e4SLinus Torvalds #define EnterFunction(level)						\
2891da177e4SLinus Torvalds 	do {								\
2901da177e4SLinus Torvalds 		if (level <= ip_vs_get_debug_level())			\
2919aada7acSHannes Eder 			printk(KERN_DEBUG				\
2929aada7acSHannes Eder 			       pr_fmt("Enter: %s, %s line %i\n"),	\
293d5c003b4SHarvey Harrison 			       __func__, __FILE__, __LINE__);		\
2941da177e4SLinus Torvalds 	} while (0)
2951da177e4SLinus Torvalds #define LeaveFunction(level)						\
2961da177e4SLinus Torvalds 	do {								\
2971da177e4SLinus Torvalds 		if (level <= ip_vs_get_debug_level())			\
2989aada7acSHannes Eder 			printk(KERN_DEBUG				\
2999aada7acSHannes Eder 			       pr_fmt("Leave: %s, %s line %i\n"),	\
300d5c003b4SHarvey Harrison 			       __func__, __FILE__, __LINE__);		\
3011da177e4SLinus Torvalds 	} while (0)
3021da177e4SLinus Torvalds #else
3031da177e4SLinus Torvalds #define EnterFunction(level)   do {} while (0)
3041da177e4SLinus Torvalds #define LeaveFunction(level)   do {} while (0)
3051da177e4SLinus Torvalds #endif
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds /*
3091da177e4SLinus Torvalds  *      The port number of FTP service (in network order).
3101da177e4SLinus Torvalds  */
311f3a7c66bSHarvey Harrison #define FTPPORT  cpu_to_be16(21)
312f3a7c66bSHarvey Harrison #define FTPDATA  cpu_to_be16(20)
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds /*
3151da177e4SLinus Torvalds  *      TCP State Values
3161da177e4SLinus Torvalds  */
3171da177e4SLinus Torvalds enum {
3181da177e4SLinus Torvalds 	IP_VS_TCP_S_NONE = 0,
3191da177e4SLinus Torvalds 	IP_VS_TCP_S_ESTABLISHED,
3201da177e4SLinus Torvalds 	IP_VS_TCP_S_SYN_SENT,
3211da177e4SLinus Torvalds 	IP_VS_TCP_S_SYN_RECV,
3221da177e4SLinus Torvalds 	IP_VS_TCP_S_FIN_WAIT,
3231da177e4SLinus Torvalds 	IP_VS_TCP_S_TIME_WAIT,
3241da177e4SLinus Torvalds 	IP_VS_TCP_S_CLOSE,
3251da177e4SLinus Torvalds 	IP_VS_TCP_S_CLOSE_WAIT,
3261da177e4SLinus Torvalds 	IP_VS_TCP_S_LAST_ACK,
3271da177e4SLinus Torvalds 	IP_VS_TCP_S_LISTEN,
3281da177e4SLinus Torvalds 	IP_VS_TCP_S_SYNACK,
3291da177e4SLinus Torvalds 	IP_VS_TCP_S_LAST
3301da177e4SLinus Torvalds };
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /*
3331da177e4SLinus Torvalds  *	UDP State Values
3341da177e4SLinus Torvalds  */
3351da177e4SLinus Torvalds enum {
3361da177e4SLinus Torvalds 	IP_VS_UDP_S_NORMAL,
3371da177e4SLinus Torvalds 	IP_VS_UDP_S_LAST,
3381da177e4SLinus Torvalds };
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds /*
3411da177e4SLinus Torvalds  *	ICMP State Values
3421da177e4SLinus Torvalds  */
3431da177e4SLinus Torvalds enum {
3441da177e4SLinus Torvalds 	IP_VS_ICMP_S_NORMAL,
3451da177e4SLinus Torvalds 	IP_VS_ICMP_S_LAST,
3461da177e4SLinus Torvalds };
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /*
3492906f66aSVenkata Mohan Reddy  *	SCTP State Values
3502906f66aSVenkata Mohan Reddy  */
3512906f66aSVenkata Mohan Reddy enum ip_vs_sctp_states {
3522906f66aSVenkata Mohan Reddy 	IP_VS_SCTP_S_NONE,
35361e7c420SJulian Anastasov 	IP_VS_SCTP_S_INIT1,
35461e7c420SJulian Anastasov 	IP_VS_SCTP_S_INIT,
35561e7c420SJulian Anastasov 	IP_VS_SCTP_S_COOKIE_SENT,
35661e7c420SJulian Anastasov 	IP_VS_SCTP_S_COOKIE_REPLIED,
35761e7c420SJulian Anastasov 	IP_VS_SCTP_S_COOKIE_WAIT,
35861e7c420SJulian Anastasov 	IP_VS_SCTP_S_COOKIE,
35961e7c420SJulian Anastasov 	IP_VS_SCTP_S_COOKIE_ECHOED,
3602906f66aSVenkata Mohan Reddy 	IP_VS_SCTP_S_ESTABLISHED,
36161e7c420SJulian Anastasov 	IP_VS_SCTP_S_SHUTDOWN_SENT,
36261e7c420SJulian Anastasov 	IP_VS_SCTP_S_SHUTDOWN_RECEIVED,
36361e7c420SJulian Anastasov 	IP_VS_SCTP_S_SHUTDOWN_ACK_SENT,
36461e7c420SJulian Anastasov 	IP_VS_SCTP_S_REJECTED,
3652906f66aSVenkata Mohan Reddy 	IP_VS_SCTP_S_CLOSED,
3662906f66aSVenkata Mohan Reddy 	IP_VS_SCTP_S_LAST
3672906f66aSVenkata Mohan Reddy };
3682906f66aSVenkata Mohan Reddy 
3692906f66aSVenkata Mohan Reddy /*
3701da177e4SLinus Torvalds  *	Delta sequence info structure
3711da177e4SLinus Torvalds  *	Each ip_vs_conn has 2 (output AND input seq. changes).
3721da177e4SLinus Torvalds  *      Only used in the VS/NAT.
3731da177e4SLinus Torvalds  */
3741da177e4SLinus Torvalds struct ip_vs_seq {
3751da177e4SLinus Torvalds 	__u32			init_seq;	/* Add delta from this seq */
3761da177e4SLinus Torvalds 	__u32			delta;		/* Delta in sequence numbers */
3771da177e4SLinus Torvalds 	__u32			previous_delta;	/* Delta in sequence numbers
3781da177e4SLinus Torvalds 						   before last resized pkt */
3791da177e4SLinus Torvalds };
3801da177e4SLinus Torvalds 
381b17fc996SHans Schillstrom /*
382b17fc996SHans Schillstrom  * counters per cpu
383b17fc996SHans Schillstrom  */
384b17fc996SHans Schillstrom struct ip_vs_counters {
385b17fc996SHans Schillstrom 	__u32		conns;		/* connections scheduled */
386b17fc996SHans Schillstrom 	__u32		inpkts;		/* incoming packets */
387b17fc996SHans Schillstrom 	__u32		outpkts;	/* outgoing packets */
388b17fc996SHans Schillstrom 	__u64		inbytes;	/* incoming bytes */
389b17fc996SHans Schillstrom 	__u64		outbytes;	/* outgoing bytes */
390b17fc996SHans Schillstrom };
391b17fc996SHans Schillstrom /*
392b17fc996SHans Schillstrom  * Stats per cpu
393b17fc996SHans Schillstrom  */
394b17fc996SHans Schillstrom struct ip_vs_cpu_stats {
395b17fc996SHans Schillstrom 	struct ip_vs_counters   ustats;
396b17fc996SHans Schillstrom 	struct u64_stats_sync   syncp;
397b17fc996SHans Schillstrom };
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds /*
4003a14a313SSven Wegener  *	IPVS statistics objects
4011da177e4SLinus Torvalds  */
4023a14a313SSven Wegener struct ip_vs_estimator {
4033a14a313SSven Wegener 	struct list_head	list;
4043a14a313SSven Wegener 
4053a14a313SSven Wegener 	u64			last_inbytes;
4063a14a313SSven Wegener 	u64			last_outbytes;
4073a14a313SSven Wegener 	u32			last_conns;
4083a14a313SSven Wegener 	u32			last_inpkts;
4093a14a313SSven Wegener 	u32			last_outpkts;
4103a14a313SSven Wegener 
4113a14a313SSven Wegener 	u32			cps;
4123a14a313SSven Wegener 	u32			inpps;
4133a14a313SSven Wegener 	u32			outpps;
4143a14a313SSven Wegener 	u32			inbps;
4153a14a313SSven Wegener 	u32			outbps;
4163a14a313SSven Wegener };
4173a14a313SSven Wegener 
418fd2c3ef7SEric Dumazet struct ip_vs_stats {
419e9c0ce23SSven Wegener 	struct ip_vs_stats_user	ustats;		/* statistics */
420e9c0ce23SSven Wegener 	struct ip_vs_estimator	est;		/* estimator */
421b962abdcSJulian Anastasov 	struct ip_vs_cpu_stats __percpu	*cpustats;	/* per cpu counters */
4221da177e4SLinus Torvalds 	spinlock_t		lock;		/* spin lock */
42355a3d4e1SJulian Anastasov 	struct ip_vs_stats_user	ustats0;	/* reset values */
4241da177e4SLinus Torvalds };
4251da177e4SLinus Torvalds 
42614c85021SArnaldo Carvalho de Melo struct dst_entry;
42714c85021SArnaldo Carvalho de Melo struct iphdr;
4281da177e4SLinus Torvalds struct ip_vs_conn;
4291da177e4SLinus Torvalds struct ip_vs_app;
43014c85021SArnaldo Carvalho de Melo struct sk_buff;
431252c6410SHans Schillstrom struct ip_vs_proto_data;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds struct ip_vs_protocol {
4341da177e4SLinus Torvalds 	struct ip_vs_protocol	*next;
4351da177e4SLinus Torvalds 	char			*name;
4362ad17defSJulian Anastasov 	u16			protocol;
4372ad17defSJulian Anastasov 	u16			num_states;
4381da177e4SLinus Torvalds 	int			dont_defrag;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	void (*init)(struct ip_vs_protocol *pp);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	void (*exit)(struct ip_vs_protocol *pp);
4431da177e4SLinus Torvalds 
444582b8e3eSHans Schillstrom 	int (*init_netns)(struct net *net, struct ip_vs_proto_data *pd);
445252c6410SHans Schillstrom 
446252c6410SHans Schillstrom 	void (*exit_netns)(struct net *net, struct ip_vs_proto_data *pd);
447252c6410SHans Schillstrom 
44851ef348bSJulius Volz 	int (*conn_schedule)(int af, struct sk_buff *skb,
4499330419dSHans Schillstrom 			     struct ip_vs_proto_data *pd,
450d4383f04SJesper Dangaard Brouer 			     int *verdict, struct ip_vs_conn **cpp,
451d4383f04SJesper Dangaard Brouer 			     struct ip_vs_iphdr *iph);
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	struct ip_vs_conn *
45451ef348bSJulius Volz 	(*conn_in_get)(int af,
45551ef348bSJulius Volz 		       const struct sk_buff *skb,
45651ef348bSJulius Volz 		       const struct ip_vs_iphdr *iph,
4571da177e4SLinus Torvalds 		       int inverse);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	struct ip_vs_conn *
46051ef348bSJulius Volz 	(*conn_out_get)(int af,
46151ef348bSJulius Volz 			const struct sk_buff *skb,
46251ef348bSJulius Volz 			const struct ip_vs_iphdr *iph,
4631da177e4SLinus Torvalds 			int inverse);
4641da177e4SLinus Torvalds 
465d4383f04SJesper Dangaard Brouer 	int (*snat_handler)(struct sk_buff *skb, struct ip_vs_protocol *pp,
466d4383f04SJesper Dangaard Brouer 			    struct ip_vs_conn *cp, struct ip_vs_iphdr *iph);
4671da177e4SLinus Torvalds 
468d4383f04SJesper Dangaard Brouer 	int (*dnat_handler)(struct sk_buff *skb, struct ip_vs_protocol *pp,
469d4383f04SJesper Dangaard Brouer 			    struct ip_vs_conn *cp, struct ip_vs_iphdr *iph);
4701da177e4SLinus Torvalds 
47151ef348bSJulius Volz 	int (*csum_check)(int af, struct sk_buff *skb,
47251ef348bSJulius Volz 			  struct ip_vs_protocol *pp);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	const char *(*state_name)(int state);
4751da177e4SLinus Torvalds 
4764a516f11SSimon Horman 	void (*state_transition)(struct ip_vs_conn *cp, int direction,
4771da177e4SLinus Torvalds 				 const struct sk_buff *skb,
4789330419dSHans Schillstrom 				 struct ip_vs_proto_data *pd);
4791da177e4SLinus Torvalds 
480ab8a5e84SHans Schillstrom 	int (*register_app)(struct net *net, struct ip_vs_app *inc);
4811da177e4SLinus Torvalds 
482ab8a5e84SHans Schillstrom 	void (*unregister_app)(struct net *net, struct ip_vs_app *inc);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	int (*app_conn_bind)(struct ip_vs_conn *cp);
4851da177e4SLinus Torvalds 
4860d79641aSJulian Anastasov 	void (*debug_packet)(int af, struct ip_vs_protocol *pp,
4871da177e4SLinus Torvalds 			     const struct sk_buff *skb,
4881da177e4SLinus Torvalds 			     int offset,
4891da177e4SLinus Torvalds 			     const char *msg);
4901da177e4SLinus Torvalds 
4919330419dSHans Schillstrom 	void (*timeout_change)(struct ip_vs_proto_data *pd, int flags);
4921da177e4SLinus Torvalds };
4931da177e4SLinus Torvalds 
494252c6410SHans Schillstrom /*
495252c6410SHans Schillstrom  * protocol data per netns
496252c6410SHans Schillstrom  */
497252c6410SHans Schillstrom struct ip_vs_proto_data {
498252c6410SHans Schillstrom 	struct ip_vs_proto_data	*next;
499252c6410SHans Schillstrom 	struct ip_vs_protocol	*pp;
500252c6410SHans Schillstrom 	int			*timeout_table;	/* protocol timeout table */
501252c6410SHans Schillstrom 	atomic_t		appcnt;		/* counter of proto app incs. */
502252c6410SHans Schillstrom 	struct tcp_states_t	*tcp_state_table;
503252c6410SHans Schillstrom };
504252c6410SHans Schillstrom 
5055c3a0fd7SJoe Perches struct ip_vs_protocol   *ip_vs_proto_get(unsigned short proto);
5065c3a0fd7SJoe Perches struct ip_vs_proto_data *ip_vs_proto_data_get(struct net *net,
507252c6410SHans Schillstrom 					      unsigned short proto);
5081da177e4SLinus Torvalds 
509f11017ecSSimon Horman struct ip_vs_conn_param {
5106e67e586SHans Schillstrom 	struct net			*net;
511f11017ecSSimon Horman 	const union nf_inet_addr	*caddr;
512f11017ecSSimon Horman 	const union nf_inet_addr	*vaddr;
513f11017ecSSimon Horman 	__be16				cport;
514f11017ecSSimon Horman 	__be16				vport;
515f11017ecSSimon Horman 	__u16				protocol;
516f11017ecSSimon Horman 	u16				af;
51785999283SSimon Horman 
51885999283SSimon Horman 	const struct ip_vs_pe		*pe;
51985999283SSimon Horman 	char				*pe_data;
52085999283SSimon Horman 	__u8				pe_data_len;
521f11017ecSSimon Horman };
522f11017ecSSimon Horman 
5231da177e4SLinus Torvalds /*
5241da177e4SLinus Torvalds  *	IP_VS structure allocated for each dynamically scheduled connection
5251da177e4SLinus Torvalds  */
5261da177e4SLinus Torvalds struct ip_vs_conn {
527731109e7SChangli Gao 	struct hlist_node	c_list;         /* hashed list heads */
5281da177e4SLinus Torvalds 	/* Protocol, addresses and port numbers */
5296e67e586SHans Schillstrom 	__be16                  cport;
5306e67e586SHans Schillstrom 	__be16                  dport;
5311845ed0bSJulian Anastasov 	__be16                  vport;
5321845ed0bSJulian Anastasov 	u16			af;		/* address family */
533e7ade46aSJulius Volz 	union nf_inet_addr      caddr;          /* client address */
534e7ade46aSJulius Volz 	union nf_inet_addr      vaddr;          /* virtual address */
535e7ade46aSJulius Volz 	union nf_inet_addr      daddr;          /* destination address */
5363575792eSJulian Anastasov 	volatile __u32          flags;          /* status flags */
5371da177e4SLinus Torvalds 	__u16                   protocol;       /* Which protocol (TCP/UDP) */
5381845ed0bSJulian Anastasov #ifdef CONFIG_NET_NS
5391845ed0bSJulian Anastasov 	struct net              *net;           /* Name space */
5401845ed0bSJulian Anastasov #endif
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	/* counter and timer */
5431da177e4SLinus Torvalds 	atomic_t		refcnt;		/* reference count */
5441da177e4SLinus Torvalds 	struct timer_list	timer;		/* Expiration timer */
5451da177e4SLinus Torvalds 	volatile unsigned long	timeout;	/* timeout */
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	/* Flags and state transition */
5481da177e4SLinus Torvalds 	spinlock_t              lock;           /* lock for state transition */
5491da177e4SLinus Torvalds 	volatile __u16          state;          /* state info */
550efac5276SRumen G. Bogdanovski 	volatile __u16          old_state;      /* old state, to be used for
551efac5276SRumen G. Bogdanovski 						 * state transition triggerd
552efac5276SRumen G. Bogdanovski 						 * synchronization
553efac5276SRumen G. Bogdanovski 						 */
5541845ed0bSJulian Anastasov 	__u32			fwmark;		/* Fire wall mark from skb */
555749c42b6SJulian Anastasov 	unsigned long		sync_endtime;	/* jiffies + sent_retries */
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	/* Control members */
5581da177e4SLinus Torvalds 	struct ip_vs_conn       *control;       /* Master control connection */
5591da177e4SLinus Torvalds 	atomic_t                n_control;      /* Number of controlled ones */
5601da177e4SLinus Torvalds 	struct ip_vs_dest       *dest;          /* real server */
5611da177e4SLinus Torvalds 	atomic_t                in_pkts;        /* incoming packet counter */
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	/* packet transmitter for different forwarding methods.  If it
5641da177e4SLinus Torvalds 	   mangles the packet, it must return NF_DROP or better NF_STOLEN,
5651da177e4SLinus Torvalds 	   otherwise this must be changed to a sk_buff **.
566fc604767SJulian Anastasov 	   NF_ACCEPT can be returned when destination is local.
5671da177e4SLinus Torvalds 	 */
5681da177e4SLinus Torvalds 	int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
569d4383f04SJesper Dangaard Brouer 			   struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	/* Note: we can group the following members into a structure,
5721da177e4SLinus Torvalds 	   in order to save more space, and the following members are
5731da177e4SLinus Torvalds 	   only used in VS/NAT anyway */
5741da177e4SLinus Torvalds 	struct ip_vs_app        *app;           /* bound ip_vs_app object */
5751da177e4SLinus Torvalds 	void                    *app_data;      /* Application private data */
5761da177e4SLinus Torvalds 	struct ip_vs_seq        in_seq;         /* incoming seq. struct */
5771da177e4SLinus Torvalds 	struct ip_vs_seq        out_seq;        /* outgoing seq. struct */
57885999283SSimon Horman 
579e9e5eee8SSimon Horman 	const struct ip_vs_pe	*pe;
58085999283SSimon Horman 	char			*pe_data;
58185999283SSimon Horman 	__u8			pe_data_len;
582088339a5SJulian Anastasov 
583088339a5SJulian Anastasov 	struct rcu_head		rcu_head;
5841da177e4SLinus Torvalds };
5851da177e4SLinus Torvalds 
5866e67e586SHans Schillstrom /*
5876e67e586SHans Schillstrom  *  To save some memory in conn table when name space is disabled.
5886e67e586SHans Schillstrom  */
5896e67e586SHans Schillstrom static inline struct net *ip_vs_conn_net(const struct ip_vs_conn *cp)
5906e67e586SHans Schillstrom {
5916e67e586SHans Schillstrom #ifdef CONFIG_NET_NS
5926e67e586SHans Schillstrom 	return cp->net;
5936e67e586SHans Schillstrom #else
5946e67e586SHans Schillstrom 	return &init_net;
5956e67e586SHans Schillstrom #endif
5966e67e586SHans Schillstrom }
5976e67e586SHans Schillstrom static inline void ip_vs_conn_net_set(struct ip_vs_conn *cp, struct net *net)
5986e67e586SHans Schillstrom {
5996e67e586SHans Schillstrom #ifdef CONFIG_NET_NS
6006e67e586SHans Schillstrom 	cp->net = net;
6016e67e586SHans Schillstrom #endif
6026e67e586SHans Schillstrom }
6036e67e586SHans Schillstrom 
6046e67e586SHans Schillstrom static inline int ip_vs_conn_net_eq(const struct ip_vs_conn *cp,
6056e67e586SHans Schillstrom 				    struct net *net)
6066e67e586SHans Schillstrom {
6076e67e586SHans Schillstrom #ifdef CONFIG_NET_NS
6086e67e586SHans Schillstrom 	return cp->net == net;
6096e67e586SHans Schillstrom #else
6106e67e586SHans Schillstrom 	return 1;
6116e67e586SHans Schillstrom #endif
6126e67e586SHans Schillstrom }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds /*
615c860c6b1SJulius Volz  *	Extended internal versions of struct ip_vs_service_user and
616c860c6b1SJulius Volz  *	ip_vs_dest_user for IPv6 support.
617c860c6b1SJulius Volz  *
618c860c6b1SJulius Volz  *	We need these to conveniently pass around service and destination
619c860c6b1SJulius Volz  *	options, but unfortunately, we also need to keep the old definitions to
620c860c6b1SJulius Volz  *	maintain userspace backwards compatibility for the setsockopt interface.
621c860c6b1SJulius Volz  */
622c860c6b1SJulius Volz struct ip_vs_service_user_kern {
623c860c6b1SJulius Volz 	/* virtual service addresses */
624c860c6b1SJulius Volz 	u16			af;
625c860c6b1SJulius Volz 	u16			protocol;
626c860c6b1SJulius Volz 	union nf_inet_addr	addr;		/* virtual ip address */
6270a925864SJulian Anastasov 	__be16			port;
628c860c6b1SJulius Volz 	u32			fwmark;		/* firwall mark of service */
629c860c6b1SJulius Volz 
630c860c6b1SJulius Volz 	/* virtual service options */
631c860c6b1SJulius Volz 	char			*sched_name;
6320d1e71b0SSimon Horman 	char			*pe_name;
63395c96174SEric Dumazet 	unsigned int		flags;		/* virtual service flags */
63495c96174SEric Dumazet 	unsigned int		timeout;	/* persistent timeout in sec */
6350a925864SJulian Anastasov 	__be32			netmask;	/* persistent netmask or plen */
636c860c6b1SJulius Volz };
637c860c6b1SJulius Volz 
638c860c6b1SJulius Volz 
639c860c6b1SJulius Volz struct ip_vs_dest_user_kern {
640c860c6b1SJulius Volz 	/* destination server address */
641c860c6b1SJulius Volz 	union nf_inet_addr	addr;
6420a925864SJulian Anastasov 	__be16			port;
643c860c6b1SJulius Volz 
644c860c6b1SJulius Volz 	/* real server options */
64595c96174SEric Dumazet 	unsigned int		conn_flags;	/* connection flags */
646c860c6b1SJulius Volz 	int			weight;		/* destination weight */
647c860c6b1SJulius Volz 
648c860c6b1SJulius Volz 	/* thresholds for active connections */
649c860c6b1SJulius Volz 	u32			u_threshold;	/* upper threshold */
650c860c6b1SJulius Volz 	u32			l_threshold;	/* lower threshold */
651c860c6b1SJulius Volz };
652c860c6b1SJulius Volz 
653c860c6b1SJulius Volz 
654c860c6b1SJulius Volz /*
6551da177e4SLinus Torvalds  *	The information about the virtual service offered to the net
6561da177e4SLinus Torvalds  *	and the forwarding entries
6571da177e4SLinus Torvalds  */
6581da177e4SLinus Torvalds struct ip_vs_service {
659ceec4c38SJulian Anastasov 	struct hlist_node	s_list;   /* for normal service table */
660ceec4c38SJulian Anastasov 	struct hlist_node	f_list;   /* for fwmark-based service table */
6611da177e4SLinus Torvalds 	atomic_t		refcnt;   /* reference counter */
6621da177e4SLinus Torvalds 
663e7ade46aSJulius Volz 	u16			af;       /* address family */
6641da177e4SLinus Torvalds 	__u16			protocol; /* which protocol (TCP/UDP) */
665e7ade46aSJulius Volz 	union nf_inet_addr	addr;	  /* IP address for virtual service */
666014d730dSAl Viro 	__be16			port;	  /* port number for the service */
6671da177e4SLinus Torvalds 	__u32                   fwmark;   /* firewall mark of the service */
66895c96174SEric Dumazet 	unsigned int		flags;	  /* service status flags */
66995c96174SEric Dumazet 	unsigned int		timeout;  /* persistent timeout in ticks */
6700a925864SJulian Anastasov 	__be32			netmask;  /* grouping granularity, mask/plen */
671fc723250SHans Schillstrom 	struct net		*net;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	struct list_head	destinations;  /* real server d-linked list */
6741da177e4SLinus Torvalds 	__u32			num_dests;     /* number of servers */
6751da177e4SLinus Torvalds 	struct ip_vs_stats      stats;         /* statistics for the service */
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	/* for scheduling */
678ceec4c38SJulian Anastasov 	struct ip_vs_scheduler __rcu *scheduler; /* bound scheduler object */
679ba3a3ce1SJulian Anastasov 	spinlock_t		sched_lock;    /* lock sched_data */
6801da177e4SLinus Torvalds 	void			*sched_data;   /* scheduler application data */
68185999283SSimon Horman 
68285999283SSimon Horman 	/* alternate persistence engine */
683ceec4c38SJulian Anastasov 	struct ip_vs_pe __rcu	*pe;
684ceec4c38SJulian Anastasov 
685ceec4c38SJulian Anastasov 	struct rcu_head		rcu_head;
6861da177e4SLinus Torvalds };
6871da177e4SLinus Torvalds 
688026ace06SJulian Anastasov /* Information for cached dst */
689026ace06SJulian Anastasov struct ip_vs_dest_dst {
690026ace06SJulian Anastasov 	struct dst_entry	*dst_cache;	/* destination cache entry */
691026ace06SJulian Anastasov 	u32			dst_cookie;
692026ace06SJulian Anastasov 	union nf_inet_addr	dst_saddr;
693026ace06SJulian Anastasov 	struct rcu_head		rcu_head;
694026ace06SJulian Anastasov };
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds /*
6971da177e4SLinus Torvalds  *	The real server destination forwarding entry
6981da177e4SLinus Torvalds  *	with ip address, port number, and so on.
6991da177e4SLinus Torvalds  */
7001da177e4SLinus Torvalds struct ip_vs_dest {
7011da177e4SLinus Torvalds 	struct list_head	n_list;   /* for the dests in the service */
702276472eaSJulian Anastasov 	struct hlist_node	d_list;   /* for table with all the dests */
7031da177e4SLinus Torvalds 
704e7ade46aSJulius Volz 	u16			af;		/* address family */
705014d730dSAl Viro 	__be16			port;		/* port number of the server */
706f2431e6eSHans Schillstrom 	union nf_inet_addr	addr;		/* IP address of the server */
70795c96174SEric Dumazet 	volatile unsigned int	flags;		/* dest status flags */
7081da177e4SLinus Torvalds 	atomic_t		conn_flags;	/* flags to copy to conn */
7091da177e4SLinus Torvalds 	atomic_t		weight;		/* server weight */
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	atomic_t		refcnt;		/* reference counter */
7121da177e4SLinus Torvalds 	struct ip_vs_stats      stats;          /* statistics */
713bcbde4c0SJulian Anastasov 	unsigned long		idle_start;	/* start time, jiffies */
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	/* connection counters and thresholds */
7161da177e4SLinus Torvalds 	atomic_t		activeconns;	/* active connections */
7171da177e4SLinus Torvalds 	atomic_t		inactconns;	/* inactive connections */
7181da177e4SLinus Torvalds 	atomic_t		persistconns;	/* persistent connections */
7191da177e4SLinus Torvalds 	__u32			u_threshold;	/* upper threshold */
7201da177e4SLinus Torvalds 	__u32			l_threshold;	/* lower threshold */
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	/* for destination cache */
7231da177e4SLinus Torvalds 	spinlock_t		dst_lock;	/* lock of dst_cache */
724026ace06SJulian Anastasov 	struct ip_vs_dest_dst __rcu *dest_dst;	/* cached dst info */
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	/* for virtual service */
727bcbde4c0SJulian Anastasov 	struct ip_vs_service __rcu *svc;	/* service it belongs to */
7281da177e4SLinus Torvalds 	__u16			protocol;	/* which protocol (TCP/UDP) */
729014d730dSAl Viro 	__be16			vport;		/* virtual port number */
730f2431e6eSHans Schillstrom 	union nf_inet_addr	vaddr;		/* virtual IP address */
7311da177e4SLinus Torvalds 	__u32			vfwmark;	/* firewall mark of service */
732276472eaSJulian Anastasov 
733578bc3efSJulian Anastasov 	struct list_head	t_list;		/* in dest_trash */
734276472eaSJulian Anastasov 	unsigned int		in_rs_table:1;	/* we are in rs_table */
7351da177e4SLinus Torvalds };
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds /*
7391da177e4SLinus Torvalds  *	The scheduler object
7401da177e4SLinus Torvalds  */
7411da177e4SLinus Torvalds struct ip_vs_scheduler {
7421da177e4SLinus Torvalds 	struct list_head	n_list;		/* d-linked list head */
7431da177e4SLinus Torvalds 	char			*name;		/* scheduler name */
7441da177e4SLinus Torvalds 	atomic_t		refcnt;		/* reference counter */
7451da177e4SLinus Torvalds 	struct module		*module;	/* THIS_MODULE/NULL */
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	/* scheduler initializing service */
7481da177e4SLinus Torvalds 	int (*init_service)(struct ip_vs_service *svc);
7491da177e4SLinus Torvalds 	/* scheduling service finish */
750ed3ffc4eSJulian Anastasov 	void (*done_service)(struct ip_vs_service *svc);
7516b6df466SJulian Anastasov 	/* dest is linked */
7526b6df466SJulian Anastasov 	int (*add_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
7536b6df466SJulian Anastasov 	/* dest is unlinked */
7546b6df466SJulian Anastasov 	int (*del_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
7556b6df466SJulian Anastasov 	/* dest is updated */
7566b6df466SJulian Anastasov 	int (*upd_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	/* selecting a server from the given service */
7591da177e4SLinus Torvalds 	struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
760bba54de5SJulian Anastasov 				       const struct sk_buff *skb,
761bba54de5SJulian Anastasov 				       struct ip_vs_iphdr *iph);
7621da177e4SLinus Torvalds };
7631da177e4SLinus Torvalds 
76485999283SSimon Horman /* The persistence engine object */
76585999283SSimon Horman struct ip_vs_pe {
76685999283SSimon Horman 	struct list_head	n_list;		/* d-linked list head */
76785999283SSimon Horman 	char			*name;		/* scheduler name */
76885999283SSimon Horman 	atomic_t		refcnt;		/* reference counter */
76985999283SSimon Horman 	struct module		*module;	/* THIS_MODULE/NULL */
77085999283SSimon Horman 
77185999283SSimon Horman 	/* get the connection template, if any */
77285999283SSimon Horman 	int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
77385999283SSimon Horman 	bool (*ct_match)(const struct ip_vs_conn_param *p,
77485999283SSimon Horman 			 struct ip_vs_conn *ct);
77585999283SSimon Horman 	u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
77685999283SSimon Horman 			   bool inverse);
777a3c918acSSimon Horman 	int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
77885999283SSimon Horman };
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds /*
7811da177e4SLinus Torvalds  *	The application module object (a.k.a. app incarnation)
7821da177e4SLinus Torvalds  */
783fd2c3ef7SEric Dumazet struct ip_vs_app {
7841da177e4SLinus Torvalds 	struct list_head	a_list;		/* member in app list */
7851da177e4SLinus Torvalds 	int			type;		/* IP_VS_APP_TYPE_xxx */
7861da177e4SLinus Torvalds 	char			*name;		/* application module name */
7871da177e4SLinus Torvalds 	__u16			protocol;
7881da177e4SLinus Torvalds 	struct module		*module;	/* THIS_MODULE/NULL */
7891da177e4SLinus Torvalds 	struct list_head	incs_list;	/* list of incarnations */
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	/* members for application incarnations */
7921da177e4SLinus Torvalds 	struct list_head	p_list;		/* member in proto app list */
7931da177e4SLinus Torvalds 	struct ip_vs_app	*app;		/* its real application */
794014d730dSAl Viro 	__be16			port;		/* port number in net order */
7951da177e4SLinus Torvalds 	atomic_t		usecnt;		/* usage counter */
796363c97d7SJulian Anastasov 	struct rcu_head		rcu_head;
7971da177e4SLinus Torvalds 
7988b27b10fSJulian Anastasov 	/*
7998b27b10fSJulian Anastasov 	 * output hook: Process packet in inout direction, diff set for TCP.
8008b27b10fSJulian Anastasov 	 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
8018b27b10fSJulian Anastasov 	 *	   2=Mangled but checksum was not updated
8028b27b10fSJulian Anastasov 	 */
8031da177e4SLinus Torvalds 	int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
8043db05feaSHerbert Xu 		       struct sk_buff *, int *diff);
8051da177e4SLinus Torvalds 
8068b27b10fSJulian Anastasov 	/*
8078b27b10fSJulian Anastasov 	 * input hook: Process packet in outin direction, diff set for TCP.
8088b27b10fSJulian Anastasov 	 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
8098b27b10fSJulian Anastasov 	 *	   2=Mangled but checksum was not updated
8108b27b10fSJulian Anastasov 	 */
8111da177e4SLinus Torvalds 	int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
8123db05feaSHerbert Xu 		      struct sk_buff *, int *diff);
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	/* ip_vs_app initializer */
8151da177e4SLinus Torvalds 	int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	/* ip_vs_app finish */
8181da177e4SLinus Torvalds 	int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	/* not used now */
8221da177e4SLinus Torvalds 	int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
8231da177e4SLinus Torvalds 			 struct ip_vs_protocol *);
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	int *			timeout_table;
8281da177e4SLinus Torvalds 	int *			timeouts;
8291da177e4SLinus Torvalds 	int			timeouts_size;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
8321da177e4SLinus Torvalds 			     int *verdict, struct ip_vs_conn **cpp);
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	struct ip_vs_conn *
8351da177e4SLinus Torvalds 	(*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
836d4383f04SJesper Dangaard Brouer 		       const struct iphdr *iph, int inverse);
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	struct ip_vs_conn *
8391da177e4SLinus Torvalds 	(*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
840d4383f04SJesper Dangaard Brouer 			const struct iphdr *iph, int inverse);
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	int (*state_transition)(struct ip_vs_conn *cp, int direction,
8431da177e4SLinus Torvalds 				const struct sk_buff *skb,
8441da177e4SLinus Torvalds 				struct ip_vs_app *app);
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	void (*timeout_change)(struct ip_vs_app *app, int flags);
8471da177e4SLinus Torvalds };
8481da177e4SLinus Torvalds 
849f73181c8SPablo Neira Ayuso struct ipvs_master_sync_state {
850f73181c8SPablo Neira Ayuso 	struct list_head	sync_queue;
851f73181c8SPablo Neira Ayuso 	struct ip_vs_sync_buff	*sync_buff;
85207995674SZhang Yanfei 	unsigned long		sync_queue_len;
853f73181c8SPablo Neira Ayuso 	unsigned int		sync_queue_delay;
854f73181c8SPablo Neira Ayuso 	struct task_struct	*master_thread;
855f73181c8SPablo Neira Ayuso 	struct delayed_work	master_wakeup_work;
856f73181c8SPablo Neira Ayuso 	struct netns_ipvs	*ipvs;
857f73181c8SPablo Neira Ayuso };
858f73181c8SPablo Neira Ayuso 
859578bc3efSJulian Anastasov /* How much time to keep dests in trash */
860578bc3efSJulian Anastasov #define IP_VS_DEST_TRASH_PERIOD		(120 * HZ)
861578bc3efSJulian Anastasov 
8622553d064SJulian Anastasov /* IPVS in network namespace */
8632553d064SJulian Anastasov struct netns_ipvs {
8642553d064SJulian Anastasov 	int			gen;		/* Generation */
8657a4f0761SHans Schillstrom 	int			enable;		/* enable like nf_hooks do */
8662553d064SJulian Anastasov 	/*
8672553d064SJulian Anastasov 	 *	Hash table: for real service lookups
8682553d064SJulian Anastasov 	 */
8692553d064SJulian Anastasov 	#define IP_VS_RTAB_BITS 4
8702553d064SJulian Anastasov 	#define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
8712553d064SJulian Anastasov 	#define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
8722553d064SJulian Anastasov 
873276472eaSJulian Anastasov 	struct hlist_head	rs_table[IP_VS_RTAB_SIZE];
8742553d064SJulian Anastasov 	/* ip_vs_app */
8752553d064SJulian Anastasov 	struct list_head	app_list;
8762553d064SJulian Anastasov 	/* ip_vs_proto */
8772553d064SJulian Anastasov 	#define IP_VS_PROTO_TAB_SIZE	32	/* must be power of 2 */
8782553d064SJulian Anastasov 	struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE];
8792553d064SJulian Anastasov 	/* ip_vs_proto_tcp */
8802553d064SJulian Anastasov #ifdef CONFIG_IP_VS_PROTO_TCP
8812553d064SJulian Anastasov 	#define	TCP_APP_TAB_BITS	4
8822553d064SJulian Anastasov 	#define	TCP_APP_TAB_SIZE	(1 << TCP_APP_TAB_BITS)
8832553d064SJulian Anastasov 	#define	TCP_APP_TAB_MASK	(TCP_APP_TAB_SIZE - 1)
8842553d064SJulian Anastasov 	struct list_head	tcp_apps[TCP_APP_TAB_SIZE];
8852553d064SJulian Anastasov #endif
8862553d064SJulian Anastasov 	/* ip_vs_proto_udp */
8872553d064SJulian Anastasov #ifdef CONFIG_IP_VS_PROTO_UDP
8882553d064SJulian Anastasov 	#define	UDP_APP_TAB_BITS	4
8892553d064SJulian Anastasov 	#define	UDP_APP_TAB_SIZE	(1 << UDP_APP_TAB_BITS)
8902553d064SJulian Anastasov 	#define	UDP_APP_TAB_MASK	(UDP_APP_TAB_SIZE - 1)
8912553d064SJulian Anastasov 	struct list_head	udp_apps[UDP_APP_TAB_SIZE];
8922553d064SJulian Anastasov #endif
8932553d064SJulian Anastasov 	/* ip_vs_proto_sctp */
8942553d064SJulian Anastasov #ifdef CONFIG_IP_VS_PROTO_SCTP
8952553d064SJulian Anastasov 	#define SCTP_APP_TAB_BITS	4
8962553d064SJulian Anastasov 	#define SCTP_APP_TAB_SIZE	(1 << SCTP_APP_TAB_BITS)
8972553d064SJulian Anastasov 	#define SCTP_APP_TAB_MASK	(SCTP_APP_TAB_SIZE - 1)
8982553d064SJulian Anastasov 	/* Hash table for SCTP application incarnations	 */
8992553d064SJulian Anastasov 	struct list_head	sctp_apps[SCTP_APP_TAB_SIZE];
9002553d064SJulian Anastasov #endif
9012553d064SJulian Anastasov 	/* ip_vs_conn */
9022553d064SJulian Anastasov 	atomic_t		conn_count;      /*  connection counter */
9032553d064SJulian Anastasov 
9042553d064SJulian Anastasov 	/* ip_vs_ctl */
9052a0751afSJulian Anastasov 	struct ip_vs_stats		tot_stats;  /* Statistics & est. */
9062553d064SJulian Anastasov 
9072553d064SJulian Anastasov 	int			num_services;    /* no of virtual services */
908f2247fbdSSimon Horman 
9092553d064SJulian Anastasov 	/* Trash for destinations */
9102553d064SJulian Anastasov 	struct list_head	dest_trash;
911578bc3efSJulian Anastasov 	spinlock_t		dest_trash_lock;
912578bc3efSJulian Anastasov 	struct timer_list	dest_trash_timer; /* expiration timer */
9132553d064SJulian Anastasov 	/* Service counters */
9142553d064SJulian Anastasov 	atomic_t		ftpsvc_counter;
9152553d064SJulian Anastasov 	atomic_t		nullsvc_counter;
9162553d064SJulian Anastasov 
917f2247fbdSSimon Horman #ifdef CONFIG_SYSCTL
918f2247fbdSSimon Horman 	/* 1/rate drop and drop-entry variables */
919f2247fbdSSimon Horman 	struct delayed_work	defense_work;   /* Work handler */
920f2247fbdSSimon Horman 	int			drop_rate;
921f2247fbdSSimon Horman 	int			drop_counter;
922f2247fbdSSimon Horman 	atomic_t		dropentry;
923f2247fbdSSimon Horman 	/* locks in ctl.c */
924f2247fbdSSimon Horman 	spinlock_t		dropentry_lock;  /* drop entry handling */
925f2247fbdSSimon Horman 	spinlock_t		droppacket_lock; /* drop packet handling */
926f2247fbdSSimon Horman 	spinlock_t		securetcp_lock;  /* state and timeout tables */
927f2247fbdSSimon Horman 
9282553d064SJulian Anastasov 	/* sys-ctl struct */
9292553d064SJulian Anastasov 	struct ctl_table_header	*sysctl_hdr;
9302553d064SJulian Anastasov 	struct ctl_table	*sysctl_tbl;
931f2247fbdSSimon Horman #endif
932f2247fbdSSimon Horman 
9332553d064SJulian Anastasov 	/* sysctl variables */
9342553d064SJulian Anastasov 	int			sysctl_amemthresh;
9352553d064SJulian Anastasov 	int			sysctl_am_droprate;
9362553d064SJulian Anastasov 	int			sysctl_drop_entry;
9372553d064SJulian Anastasov 	int			sysctl_drop_packet;
9382553d064SJulian Anastasov 	int			sysctl_secure_tcp;
9392553d064SJulian Anastasov #ifdef CONFIG_IP_VS_NFCT
9402553d064SJulian Anastasov 	int			sysctl_conntrack;
9412553d064SJulian Anastasov #endif
9422553d064SJulian Anastasov 	int			sysctl_snat_reroute;
9432553d064SJulian Anastasov 	int			sysctl_sync_ver;
944f73181c8SPablo Neira Ayuso 	int			sysctl_sync_ports;
9454d0c875dSJulian Anastasov 	int			sysctl_sync_persist_mode;
94607995674SZhang Yanfei 	unsigned long		sysctl_sync_qlen_max;
9471c003b15SPablo Neira Ayuso 	int			sysctl_sync_sock_size;
9482553d064SJulian Anastasov 	int			sysctl_cache_bypass;
9492553d064SJulian Anastasov 	int			sysctl_expire_nodest_conn;
950c6c96c18SAlexander Frolkin 	int			sysctl_sloppy_tcp;
951c6c96c18SAlexander Frolkin 	int			sysctl_sloppy_sctp;
9522553d064SJulian Anastasov 	int			sysctl_expire_quiescent_template;
9532553d064SJulian Anastasov 	int			sysctl_sync_threshold[2];
954749c42b6SJulian Anastasov 	unsigned int		sysctl_sync_refresh_period;
955749c42b6SJulian Anastasov 	int			sysctl_sync_retries;
9562553d064SJulian Anastasov 	int			sysctl_nat_icmp_send;
9573654e611SJulian Anastasov 	int			sysctl_pmtu_disc;
9580c12582fSJulian Anastasov 	int			sysctl_backup_only;
9592553d064SJulian Anastasov 
9602553d064SJulian Anastasov 	/* ip_vs_lblc */
9612553d064SJulian Anastasov 	int			sysctl_lblc_expiration;
9622553d064SJulian Anastasov 	struct ctl_table_header	*lblc_ctl_header;
9632553d064SJulian Anastasov 	struct ctl_table	*lblc_ctl_table;
9642553d064SJulian Anastasov 	/* ip_vs_lblcr */
9652553d064SJulian Anastasov 	int			sysctl_lblcr_expiration;
9662553d064SJulian Anastasov 	struct ctl_table_header	*lblcr_ctl_header;
9672553d064SJulian Anastasov 	struct ctl_table	*lblcr_ctl_table;
9682553d064SJulian Anastasov 	/* ip_vs_est */
9692553d064SJulian Anastasov 	struct list_head	est_list;	/* estimator list */
9702553d064SJulian Anastasov 	spinlock_t		est_lock;
9712553d064SJulian Anastasov 	struct timer_list	est_timer;	/* Estimation timer */
9722553d064SJulian Anastasov 	/* ip_vs_sync */
9732553d064SJulian Anastasov 	spinlock_t		sync_lock;
974f73181c8SPablo Neira Ayuso 	struct ipvs_master_sync_state *ms;
9752553d064SJulian Anastasov 	spinlock_t		sync_buff_lock;
976f73181c8SPablo Neira Ayuso 	struct task_struct	**backup_threads;
977f73181c8SPablo Neira Ayuso 	int			threads_mask;
9782553d064SJulian Anastasov 	int			send_mesg_maxlen;
9792553d064SJulian Anastasov 	int			recv_mesg_maxlen;
9802553d064SJulian Anastasov 	volatile int		sync_state;
9812553d064SJulian Anastasov 	volatile int		master_syncid;
9822553d064SJulian Anastasov 	volatile int		backup_syncid;
983ae1d48b2SHans Schillstrom 	struct mutex		sync_mutex;
9842553d064SJulian Anastasov 	/* multicast interface name */
9852553d064SJulian Anastasov 	char			master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
9862553d064SJulian Anastasov 	char			backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
9872553d064SJulian Anastasov 	/* net name space ptr */
9882553d064SJulian Anastasov 	struct net		*net;            /* Needed by timer routines */
9892553d064SJulian Anastasov };
9901da177e4SLinus Torvalds 
99159e0350eSSimon Horman #define DEFAULT_SYNC_THRESHOLD	3
99259e0350eSSimon Horman #define DEFAULT_SYNC_PERIOD	50
9937532e8d4SSimon Horman #define DEFAULT_SYNC_VER	1
994c6c96c18SAlexander Frolkin #define DEFAULT_SLOPPY_TCP	0
995c6c96c18SAlexander Frolkin #define DEFAULT_SLOPPY_SCTP	0
996749c42b6SJulian Anastasov #define DEFAULT_SYNC_REFRESH_PERIOD	(0U * HZ)
997749c42b6SJulian Anastasov #define DEFAULT_SYNC_RETRIES		0
9981c003b15SPablo Neira Ayuso #define IPVS_SYNC_WAKEUP_RATE	8
9991c003b15SPablo Neira Ayuso #define IPVS_SYNC_QLEN_MAX	(IPVS_SYNC_WAKEUP_RATE * 4)
10001c003b15SPablo Neira Ayuso #define IPVS_SYNC_SEND_DELAY	(HZ / 50)
10011c003b15SPablo Neira Ayuso #define IPVS_SYNC_CHECK_PERIOD	HZ
1002749c42b6SJulian Anastasov #define IPVS_SYNC_FLUSH_TIME	(HZ * 2)
1003f73181c8SPablo Neira Ayuso #define IPVS_SYNC_PORTS_MAX	(1 << 6)
100459e0350eSSimon Horman 
100559e0350eSSimon Horman #ifdef CONFIG_SYSCTL
100659e0350eSSimon Horman 
100759e0350eSSimon Horman static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
100859e0350eSSimon Horman {
100959e0350eSSimon Horman 	return ipvs->sysctl_sync_threshold[0];
101059e0350eSSimon Horman }
101159e0350eSSimon Horman 
101259e0350eSSimon Horman static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
101359e0350eSSimon Horman {
1014749c42b6SJulian Anastasov 	return ACCESS_ONCE(ipvs->sysctl_sync_threshold[1]);
1015749c42b6SJulian Anastasov }
1016749c42b6SJulian Anastasov 
1017749c42b6SJulian Anastasov static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
1018749c42b6SJulian Anastasov {
1019749c42b6SJulian Anastasov 	return ACCESS_ONCE(ipvs->sysctl_sync_refresh_period);
1020749c42b6SJulian Anastasov }
1021749c42b6SJulian Anastasov 
1022749c42b6SJulian Anastasov static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
1023749c42b6SJulian Anastasov {
1024749c42b6SJulian Anastasov 	return ipvs->sysctl_sync_retries;
102559e0350eSSimon Horman }
102659e0350eSSimon Horman 
10277532e8d4SSimon Horman static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
10287532e8d4SSimon Horman {
10297532e8d4SSimon Horman 	return ipvs->sysctl_sync_ver;
10307532e8d4SSimon Horman }
10317532e8d4SSimon Horman 
1032c6c96c18SAlexander Frolkin static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
1033c6c96c18SAlexander Frolkin {
1034c6c96c18SAlexander Frolkin 	return ipvs->sysctl_sloppy_tcp;
1035c6c96c18SAlexander Frolkin }
1036c6c96c18SAlexander Frolkin 
1037c6c96c18SAlexander Frolkin static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
1038c6c96c18SAlexander Frolkin {
1039c6c96c18SAlexander Frolkin 	return ipvs->sysctl_sloppy_sctp;
1040c6c96c18SAlexander Frolkin }
1041c6c96c18SAlexander Frolkin 
1042f73181c8SPablo Neira Ayuso static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
1043f73181c8SPablo Neira Ayuso {
1044f73181c8SPablo Neira Ayuso 	return ACCESS_ONCE(ipvs->sysctl_sync_ports);
1045f73181c8SPablo Neira Ayuso }
1046f73181c8SPablo Neira Ayuso 
10474d0c875dSJulian Anastasov static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
10484d0c875dSJulian Anastasov {
10494d0c875dSJulian Anastasov 	return ipvs->sysctl_sync_persist_mode;
10504d0c875dSJulian Anastasov }
10514d0c875dSJulian Anastasov 
105207995674SZhang Yanfei static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
10531c003b15SPablo Neira Ayuso {
10541c003b15SPablo Neira Ayuso 	return ipvs->sysctl_sync_qlen_max;
10551c003b15SPablo Neira Ayuso }
10561c003b15SPablo Neira Ayuso 
10571c003b15SPablo Neira Ayuso static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
10581c003b15SPablo Neira Ayuso {
10591c003b15SPablo Neira Ayuso 	return ipvs->sysctl_sync_sock_size;
10601c003b15SPablo Neira Ayuso }
10611c003b15SPablo Neira Ayuso 
10623654e611SJulian Anastasov static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
10633654e611SJulian Anastasov {
10643654e611SJulian Anastasov 	return ipvs->sysctl_pmtu_disc;
10653654e611SJulian Anastasov }
10663654e611SJulian Anastasov 
10670c12582fSJulian Anastasov static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
10680c12582fSJulian Anastasov {
10690c12582fSJulian Anastasov 	return ipvs->sync_state & IP_VS_STATE_BACKUP &&
10700c12582fSJulian Anastasov 	       ipvs->sysctl_backup_only;
10710c12582fSJulian Anastasov }
10720c12582fSJulian Anastasov 
107359e0350eSSimon Horman #else
107459e0350eSSimon Horman 
107559e0350eSSimon Horman static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
107659e0350eSSimon Horman {
107759e0350eSSimon Horman 	return DEFAULT_SYNC_THRESHOLD;
107859e0350eSSimon Horman }
107959e0350eSSimon Horman 
108059e0350eSSimon Horman static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
108159e0350eSSimon Horman {
108259e0350eSSimon Horman 	return DEFAULT_SYNC_PERIOD;
108359e0350eSSimon Horman }
108459e0350eSSimon Horman 
1085749c42b6SJulian Anastasov static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
1086749c42b6SJulian Anastasov {
1087749c42b6SJulian Anastasov 	return DEFAULT_SYNC_REFRESH_PERIOD;
1088749c42b6SJulian Anastasov }
1089749c42b6SJulian Anastasov 
1090749c42b6SJulian Anastasov static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
1091749c42b6SJulian Anastasov {
1092749c42b6SJulian Anastasov 	return DEFAULT_SYNC_RETRIES & 3;
1093749c42b6SJulian Anastasov }
1094749c42b6SJulian Anastasov 
10957532e8d4SSimon Horman static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
10967532e8d4SSimon Horman {
10977532e8d4SSimon Horman 	return DEFAULT_SYNC_VER;
10987532e8d4SSimon Horman }
10997532e8d4SSimon Horman 
1100c6c96c18SAlexander Frolkin static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
1101c6c96c18SAlexander Frolkin {
1102c6c96c18SAlexander Frolkin 	return DEFAULT_SLOPPY_TCP;
1103c6c96c18SAlexander Frolkin }
1104c6c96c18SAlexander Frolkin 
1105c6c96c18SAlexander Frolkin static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
1106c6c96c18SAlexander Frolkin {
1107c6c96c18SAlexander Frolkin 	return DEFAULT_SLOPPY_SCTP;
1108c6c96c18SAlexander Frolkin }
1109c6c96c18SAlexander Frolkin 
1110f73181c8SPablo Neira Ayuso static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
1111f73181c8SPablo Neira Ayuso {
1112f73181c8SPablo Neira Ayuso 	return 1;
1113f73181c8SPablo Neira Ayuso }
1114f73181c8SPablo Neira Ayuso 
11154d0c875dSJulian Anastasov static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
11164d0c875dSJulian Anastasov {
11174d0c875dSJulian Anastasov 	return 0;
11184d0c875dSJulian Anastasov }
11194d0c875dSJulian Anastasov 
112007995674SZhang Yanfei static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
11211c003b15SPablo Neira Ayuso {
11221c003b15SPablo Neira Ayuso 	return IPVS_SYNC_QLEN_MAX;
11231c003b15SPablo Neira Ayuso }
11241c003b15SPablo Neira Ayuso 
11251c003b15SPablo Neira Ayuso static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
11261c003b15SPablo Neira Ayuso {
11271c003b15SPablo Neira Ayuso 	return 0;
11281c003b15SPablo Neira Ayuso }
11291c003b15SPablo Neira Ayuso 
11303654e611SJulian Anastasov static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
11313654e611SJulian Anastasov {
11323654e611SJulian Anastasov 	return 1;
11333654e611SJulian Anastasov }
11343654e611SJulian Anastasov 
11350c12582fSJulian Anastasov static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
11360c12582fSJulian Anastasov {
11370c12582fSJulian Anastasov 	return 0;
11380c12582fSJulian Anastasov }
11390c12582fSJulian Anastasov 
114059e0350eSSimon Horman #endif
114159e0350eSSimon Horman 
11421da177e4SLinus Torvalds /*
11431da177e4SLinus Torvalds  *      IPVS core functions
11441da177e4SLinus Torvalds  *      (from ip_vs_core.c)
11451da177e4SLinus Torvalds  */
11465c3a0fd7SJoe Perches const char *ip_vs_proto_name(unsigned int proto);
11475c3a0fd7SJoe Perches void ip_vs_init_hash_table(struct list_head *table, int rows);
1148afdd6140SSven Wegener #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds #define IP_VS_APP_TYPE_FTP	1
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds /*
11531da177e4SLinus Torvalds  *     ip_vs_conn handling functions
11541da177e4SLinus Torvalds  *     (from ip_vs_conn.c)
11551da177e4SLinus Torvalds  */
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds enum {
11581da177e4SLinus Torvalds 	IP_VS_DIR_INPUT = 0,
11591da177e4SLinus Torvalds 	IP_VS_DIR_OUTPUT,
11601da177e4SLinus Torvalds 	IP_VS_DIR_INPUT_ONLY,
11611da177e4SLinus Torvalds 	IP_VS_DIR_LAST,
11621da177e4SLinus Torvalds };
11631da177e4SLinus Torvalds 
11646e67e586SHans Schillstrom static inline void ip_vs_conn_fill_param(struct net *net, int af, int protocol,
1165f11017ecSSimon Horman 					 const union nf_inet_addr *caddr,
1166f11017ecSSimon Horman 					 __be16 cport,
1167f11017ecSSimon Horman 					 const union nf_inet_addr *vaddr,
1168f11017ecSSimon Horman 					 __be16 vport,
1169f11017ecSSimon Horman 					 struct ip_vs_conn_param *p)
1170f11017ecSSimon Horman {
11716e67e586SHans Schillstrom 	p->net = net;
1172f11017ecSSimon Horman 	p->af = af;
1173f11017ecSSimon Horman 	p->protocol = protocol;
1174f11017ecSSimon Horman 	p->caddr = caddr;
1175f11017ecSSimon Horman 	p->cport = cport;
1176f11017ecSSimon Horman 	p->vaddr = vaddr;
1177f11017ecSSimon Horman 	p->vport = vport;
117885999283SSimon Horman 	p->pe = NULL;
117985999283SSimon Horman 	p->pe_data = NULL;
1180f11017ecSSimon Horman }
118128364a59SJulius Volz 
1182f11017ecSSimon Horman struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
1183f11017ecSSimon Horman struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
118428364a59SJulius Volz 
11855c0d2374SSimon Horman struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
11865c0d2374SSimon Horman 					    const struct ip_vs_iphdr *iph,
11875c0d2374SSimon Horman 					    int inverse);
11885c0d2374SSimon Horman 
1189f11017ecSSimon Horman struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
11901da177e4SLinus Torvalds 
11915c0d2374SSimon Horman struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
11925c0d2374SSimon Horman 					     const struct ip_vs_iphdr *iph,
11935c0d2374SSimon Horman 					     int inverse);
11945c0d2374SSimon Horman 
1195088339a5SJulian Anastasov /* Get reference to gain full access to conn.
1196088339a5SJulian Anastasov  * By default, RCU read-side critical sections have access only to
1197088339a5SJulian Anastasov  * conn fields and its PE data, see ip_vs_conn_rcu_free() for reference.
1198088339a5SJulian Anastasov  */
1199088339a5SJulian Anastasov static inline bool __ip_vs_conn_get(struct ip_vs_conn *cp)
1200088339a5SJulian Anastasov {
1201088339a5SJulian Anastasov 	return atomic_inc_not_zero(&cp->refcnt);
1202088339a5SJulian Anastasov }
1203088339a5SJulian Anastasov 
12041da177e4SLinus Torvalds /* put back the conn without restarting its timer */
12051da177e4SLinus Torvalds static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
12061da177e4SLinus Torvalds {
1207*4e857c58SPeter Zijlstra 	smp_mb__before_atomic();
12081da177e4SLinus Torvalds 	atomic_dec(&cp->refcnt);
12091da177e4SLinus Torvalds }
12105c3a0fd7SJoe Perches void ip_vs_conn_put(struct ip_vs_conn *cp);
12115c3a0fd7SJoe Perches void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
12121da177e4SLinus Torvalds 
1213f11017ecSSimon Horman struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p,
1214f11017ecSSimon Horman 				  const union nf_inet_addr *daddr,
121595c96174SEric Dumazet 				  __be16 dport, unsigned int flags,
12160e051e68SHans Schillstrom 				  struct ip_vs_dest *dest, __u32 fwmark);
12175c3a0fd7SJoe Perches void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
12181da177e4SLinus Torvalds 
12195c3a0fd7SJoe Perches const char *ip_vs_state_name(__u16 proto, int state);
12201da177e4SLinus Torvalds 
12215c3a0fd7SJoe Perches void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp);
12225c3a0fd7SJoe Perches int ip_vs_check_template(struct ip_vs_conn *ct);
12235c3a0fd7SJoe Perches void ip_vs_random_dropentry(struct net *net);
12245c3a0fd7SJoe Perches int ip_vs_conn_init(void);
12255c3a0fd7SJoe Perches void ip_vs_conn_cleanup(void);
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds static inline void ip_vs_control_del(struct ip_vs_conn *cp)
12281da177e4SLinus Torvalds {
12291da177e4SLinus Torvalds 	struct ip_vs_conn *ctl_cp = cp->control;
12301da177e4SLinus Torvalds 	if (!ctl_cp) {
1231cfc78c5aSJulius Volz 		IP_VS_ERR_BUF("request control DEL for uncontrolled: "
1232cfc78c5aSJulius Volz 			      "%s:%d to %s:%d\n",
1233cfc78c5aSJulius Volz 			      IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1234cfc78c5aSJulius Volz 			      ntohs(cp->cport),
1235cfc78c5aSJulius Volz 			      IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
1236cfc78c5aSJulius Volz 			      ntohs(cp->vport));
1237cfc78c5aSJulius Volz 
12381da177e4SLinus Torvalds 		return;
12391da177e4SLinus Torvalds 	}
12401da177e4SLinus Torvalds 
1241cfc78c5aSJulius Volz 	IP_VS_DBG_BUF(7, "DELeting control for: "
1242cfc78c5aSJulius Volz 		      "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
1243cfc78c5aSJulius Volz 		      IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1244cfc78c5aSJulius Volz 		      ntohs(cp->cport),
1245cfc78c5aSJulius Volz 		      IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
1246cfc78c5aSJulius Volz 		      ntohs(ctl_cp->cport));
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	cp->control = NULL;
12491da177e4SLinus Torvalds 	if (atomic_read(&ctl_cp->n_control) == 0) {
1250cfc78c5aSJulius Volz 		IP_VS_ERR_BUF("BUG control DEL with n=0 : "
1251cfc78c5aSJulius Volz 			      "%s:%d to %s:%d\n",
1252cfc78c5aSJulius Volz 			      IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1253cfc78c5aSJulius Volz 			      ntohs(cp->cport),
1254cfc78c5aSJulius Volz 			      IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
1255cfc78c5aSJulius Volz 			      ntohs(cp->vport));
1256cfc78c5aSJulius Volz 
12571da177e4SLinus Torvalds 		return;
12581da177e4SLinus Torvalds 	}
12591da177e4SLinus Torvalds 	atomic_dec(&ctl_cp->n_control);
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds static inline void
12631da177e4SLinus Torvalds ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
12641da177e4SLinus Torvalds {
12651da177e4SLinus Torvalds 	if (cp->control) {
1266cfc78c5aSJulius Volz 		IP_VS_ERR_BUF("request control ADD for already controlled: "
1267cfc78c5aSJulius Volz 			      "%s:%d to %s:%d\n",
1268cfc78c5aSJulius Volz 			      IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1269cfc78c5aSJulius Volz 			      ntohs(cp->cport),
1270cfc78c5aSJulius Volz 			      IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
1271cfc78c5aSJulius Volz 			      ntohs(cp->vport));
1272cfc78c5aSJulius Volz 
12731da177e4SLinus Torvalds 		ip_vs_control_del(cp);
12741da177e4SLinus Torvalds 	}
12751da177e4SLinus Torvalds 
1276cfc78c5aSJulius Volz 	IP_VS_DBG_BUF(7, "ADDing control for: "
1277cfc78c5aSJulius Volz 		      "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
1278cfc78c5aSJulius Volz 		      IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1279cfc78c5aSJulius Volz 		      ntohs(cp->cport),
1280cfc78c5aSJulius Volz 		      IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
1281cfc78c5aSJulius Volz 		      ntohs(ctl_cp->cport));
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	cp->control = ctl_cp;
12841da177e4SLinus Torvalds 	atomic_inc(&ctl_cp->n_control);
12851da177e4SLinus Torvalds }
12861da177e4SLinus Torvalds 
12877a4f0761SHans Schillstrom /*
12887a4f0761SHans Schillstrom  * IPVS netns init & cleanup functions
12897a4f0761SHans Schillstrom  */
12905c3a0fd7SJoe Perches int ip_vs_estimator_net_init(struct net *net);
12915c3a0fd7SJoe Perches int ip_vs_control_net_init(struct net *net);
12925c3a0fd7SJoe Perches int ip_vs_protocol_net_init(struct net *net);
12935c3a0fd7SJoe Perches int ip_vs_app_net_init(struct net *net);
12945c3a0fd7SJoe Perches int ip_vs_conn_net_init(struct net *net);
12955c3a0fd7SJoe Perches int ip_vs_sync_net_init(struct net *net);
12965c3a0fd7SJoe Perches void ip_vs_conn_net_cleanup(struct net *net);
12975c3a0fd7SJoe Perches void ip_vs_app_net_cleanup(struct net *net);
12985c3a0fd7SJoe Perches void ip_vs_protocol_net_cleanup(struct net *net);
12995c3a0fd7SJoe Perches void ip_vs_control_net_cleanup(struct net *net);
13005c3a0fd7SJoe Perches void ip_vs_estimator_net_cleanup(struct net *net);
13015c3a0fd7SJoe Perches void ip_vs_sync_net_cleanup(struct net *net);
13025c3a0fd7SJoe Perches void ip_vs_service_net_cleanup(struct net *net);
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds /*
13051da177e4SLinus Torvalds  *      IPVS application functions
13061da177e4SLinus Torvalds  *      (from ip_vs_app.c)
13071da177e4SLinus Torvalds  */
13081da177e4SLinus Torvalds #define IP_VS_APP_MAX_PORTS  8
13095c3a0fd7SJoe Perches struct ip_vs_app *register_ip_vs_app(struct net *net, struct ip_vs_app *app);
13105c3a0fd7SJoe Perches void unregister_ip_vs_app(struct net *net, struct ip_vs_app *app);
13115c3a0fd7SJoe Perches int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
13125c3a0fd7SJoe Perches void ip_vs_unbind_app(struct ip_vs_conn *cp);
13135c3a0fd7SJoe Perches int register_ip_vs_app_inc(struct net *net, struct ip_vs_app *app, __u16 proto,
13145c3a0fd7SJoe Perches 			   __u16 port);
13155c3a0fd7SJoe Perches int ip_vs_app_inc_get(struct ip_vs_app *inc);
13165c3a0fd7SJoe Perches void ip_vs_app_inc_put(struct ip_vs_app *inc);
13171da177e4SLinus Torvalds 
13185c3a0fd7SJoe Perches int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
13195c3a0fd7SJoe Perches int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
13201da177e4SLinus Torvalds 
13218be67a66SSimon Horman int register_ip_vs_pe(struct ip_vs_pe *pe);
13228be67a66SSimon Horman int unregister_ip_vs_pe(struct ip_vs_pe *pe);
1323e9e5eee8SSimon Horman struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
1324fe5e7a1eSHans Schillstrom struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
1325e9e5eee8SSimon Horman 
132669e7dae4SPaul Gortmaker /*
132769e7dae4SPaul Gortmaker  * Use a #define to avoid all of module.h just for these trivial ops
132869e7dae4SPaul Gortmaker  */
132969e7dae4SPaul Gortmaker #define ip_vs_pe_get(pe)			\
133069e7dae4SPaul Gortmaker 	if (pe && pe->module)			\
1331e9e5eee8SSimon Horman 		__module_get(pe->module);
1332e9e5eee8SSimon Horman 
133369e7dae4SPaul Gortmaker #define ip_vs_pe_put(pe)			\
133469e7dae4SPaul Gortmaker 	if (pe && pe->module)			\
1335e9e5eee8SSimon Horman 		module_put(pe->module);
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds /*
13381da177e4SLinus Torvalds  *	IPVS protocol functions (from ip_vs_proto.c)
13391da177e4SLinus Torvalds  */
13405c3a0fd7SJoe Perches int ip_vs_protocol_init(void);
13415c3a0fd7SJoe Perches void ip_vs_protocol_cleanup(void);
13425c3a0fd7SJoe Perches void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
13435c3a0fd7SJoe Perches int *ip_vs_create_timeout_table(int *table, int size);
13445c3a0fd7SJoe Perches int ip_vs_set_state_timeout(int *table, int num, const char *const *names,
134536cbd3dcSJan Engelhardt 			    const char *name, int to);
13465c3a0fd7SJoe Perches void ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
13475c3a0fd7SJoe Perches 			       const struct sk_buff *skb, int offset,
13485c3a0fd7SJoe Perches 			       const char *msg);
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds extern struct ip_vs_protocol ip_vs_protocol_tcp;
13511da177e4SLinus Torvalds extern struct ip_vs_protocol ip_vs_protocol_udp;
13521da177e4SLinus Torvalds extern struct ip_vs_protocol ip_vs_protocol_icmp;
13531da177e4SLinus Torvalds extern struct ip_vs_protocol ip_vs_protocol_esp;
13541da177e4SLinus Torvalds extern struct ip_vs_protocol ip_vs_protocol_ah;
13552906f66aSVenkata Mohan Reddy extern struct ip_vs_protocol ip_vs_protocol_sctp;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds /*
13581da177e4SLinus Torvalds  *      Registering/unregistering scheduler functions
13591da177e4SLinus Torvalds  *      (from ip_vs_sched.c)
13601da177e4SLinus Torvalds  */
13615c3a0fd7SJoe Perches int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
13625c3a0fd7SJoe Perches int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
13635c3a0fd7SJoe Perches int ip_vs_bind_scheduler(struct ip_vs_service *svc,
13641da177e4SLinus Torvalds 			 struct ip_vs_scheduler *scheduler);
13655c3a0fd7SJoe Perches void ip_vs_unbind_scheduler(struct ip_vs_service *svc,
1366ceec4c38SJulian Anastasov 			    struct ip_vs_scheduler *sched);
13675c3a0fd7SJoe Perches struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
13685c3a0fd7SJoe Perches void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
13695c3a0fd7SJoe Perches struct ip_vs_conn *
1370190ecd27SJulian Anastasov ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
1371d4383f04SJesper Dangaard Brouer 	       struct ip_vs_proto_data *pd, int *ignored,
1372d4383f04SJesper Dangaard Brouer 	       struct ip_vs_iphdr *iph);
13735c3a0fd7SJoe Perches int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
1374d4383f04SJesper Dangaard Brouer 		struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph);
13751da177e4SLinus Torvalds 
13765c3a0fd7SJoe Perches void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
137741ac51eeSPatrick Schaaf 
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds /*
13801da177e4SLinus Torvalds  *      IPVS control data and functions (from ip_vs_ctl.c)
13811da177e4SLinus Torvalds  */
13821da177e4SLinus Torvalds extern struct ip_vs_stats ip_vs_stats;
1383b880c1f0SHans Schillstrom extern int sysctl_ip_vs_sync_ver;
13841da177e4SLinus Torvalds 
13855c3a0fd7SJoe Perches struct ip_vs_service *
1386ceec4c38SJulian Anastasov ip_vs_service_find(struct net *net, int af, __u32 fwmark, __u16 protocol,
13873c2e0505SJulius Volz 		  const union nf_inet_addr *vaddr, __be16 vport);
13881da177e4SLinus Torvalds 
13895c3a0fd7SJoe Perches bool ip_vs_has_real_service(struct net *net, int af, __u16 protocol,
13907937df15SJulius Volz 			    const union nf_inet_addr *daddr, __be16 dport);
13917937df15SJulius Volz 
13925c3a0fd7SJoe Perches int ip_vs_use_count_inc(void);
13935c3a0fd7SJoe Perches void ip_vs_use_count_dec(void);
13945c3a0fd7SJoe Perches int ip_vs_register_nl_ioctl(void);
13955c3a0fd7SJoe Perches void ip_vs_unregister_nl_ioctl(void);
13965c3a0fd7SJoe Perches int ip_vs_control_init(void);
13975c3a0fd7SJoe Perches void ip_vs_control_cleanup(void);
13985c3a0fd7SJoe Perches struct ip_vs_dest *
1399fc723250SHans Schillstrom ip_vs_find_dest(struct net *net, int af, const union nf_inet_addr *daddr,
1400fc723250SHans Schillstrom 		__be16 dport, const union nf_inet_addr *vaddr, __be16 vport,
140152793dbeSJulian Anastasov 		__u16 protocol, __u32 fwmark, __u32 flags);
14025c3a0fd7SJoe Perches void ip_vs_try_bind_dest(struct ip_vs_conn *cp);
14031da177e4SLinus Torvalds 
1404fca9c20aSJulian Anastasov static inline void ip_vs_dest_hold(struct ip_vs_dest *dest)
1405fca9c20aSJulian Anastasov {
1406fca9c20aSJulian Anastasov 	atomic_inc(&dest->refcnt);
1407fca9c20aSJulian Anastasov }
1408fca9c20aSJulian Anastasov 
1409fca9c20aSJulian Anastasov static inline void ip_vs_dest_put(struct ip_vs_dest *dest)
1410fca9c20aSJulian Anastasov {
1411*4e857c58SPeter Zijlstra 	smp_mb__before_atomic();
1412fca9c20aSJulian Anastasov 	atomic_dec(&dest->refcnt);
1413fca9c20aSJulian Anastasov }
14141da177e4SLinus Torvalds 
14159e4e948aSJulian Anastasov static inline void ip_vs_dest_put_and_free(struct ip_vs_dest *dest)
14169e4e948aSJulian Anastasov {
14179e4e948aSJulian Anastasov 	if (atomic_dec_return(&dest->refcnt) < 0)
14189e4e948aSJulian Anastasov 		kfree(dest);
14199e4e948aSJulian Anastasov }
14209e4e948aSJulian Anastasov 
14211da177e4SLinus Torvalds /*
14221da177e4SLinus Torvalds  *      IPVS sync daemon data and function prototypes
14231da177e4SLinus Torvalds  *      (from ip_vs_sync.c)
14241da177e4SLinus Torvalds  */
14255c3a0fd7SJoe Perches int start_sync_thread(struct net *net, int state, char *mcast_ifn, __u8 syncid);
14265c3a0fd7SJoe Perches int stop_sync_thread(struct net *net, int state);
14275c3a0fd7SJoe Perches void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp, int pkts);
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds /*
14301da177e4SLinus Torvalds  *      IPVS rate estimator prototypes (from ip_vs_est.c)
14311da177e4SLinus Torvalds  */
14325c3a0fd7SJoe Perches void ip_vs_start_estimator(struct net *net, struct ip_vs_stats *stats);
14335c3a0fd7SJoe Perches void ip_vs_stop_estimator(struct net *net, struct ip_vs_stats *stats);
14345c3a0fd7SJoe Perches void ip_vs_zero_estimator(struct ip_vs_stats *stats);
14355c3a0fd7SJoe Perches void ip_vs_read_estimator(struct ip_vs_stats_user *dst,
1436ea9f22ccSJulian Anastasov 			  struct ip_vs_stats *stats);
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds /*
14391da177e4SLinus Torvalds  *	Various IPVS packet transmitters (from ip_vs_xmit.c)
14401da177e4SLinus Torvalds  */
14415c3a0fd7SJoe Perches int ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1442d4383f04SJesper Dangaard Brouer 		    struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
14435c3a0fd7SJoe Perches int ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1444d4383f04SJesper Dangaard Brouer 		      struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
14455c3a0fd7SJoe Perches int ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1446d4383f04SJesper Dangaard Brouer 		   struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
14475c3a0fd7SJoe Perches int ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
14485c3a0fd7SJoe Perches 		      struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
14495c3a0fd7SJoe Perches int ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
14505c3a0fd7SJoe Perches 		  struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
14515c3a0fd7SJoe Perches int ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1452d4383f04SJesper Dangaard Brouer 		    struct ip_vs_protocol *pp, int offset,
1453d4383f04SJesper Dangaard Brouer 		    unsigned int hooknum, struct ip_vs_iphdr *iph);
14545c3a0fd7SJoe Perches void ip_vs_dest_dst_rcu_free(struct rcu_head *head);
14551da177e4SLinus Torvalds 
1456b3cdd2a7SJulius Volz #ifdef CONFIG_IP_VS_IPV6
14575c3a0fd7SJoe Perches int ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1458d4383f04SJesper Dangaard Brouer 			 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
14595c3a0fd7SJoe Perches int ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
14605c3a0fd7SJoe Perches 		      struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
14615c3a0fd7SJoe Perches int ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
14625c3a0fd7SJoe Perches 			 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
14635c3a0fd7SJoe Perches int ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
14645c3a0fd7SJoe Perches 		     struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
14655c3a0fd7SJoe Perches int ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1466d4383f04SJesper Dangaard Brouer 		       struct ip_vs_protocol *pp, int offset,
1467d4383f04SJesper Dangaard Brouer 		       unsigned int hooknum, struct ip_vs_iphdr *iph);
1468b3cdd2a7SJulius Volz #endif
14691da177e4SLinus Torvalds 
14703a1bbf18SSimon Horman #ifdef CONFIG_SYSCTL
14711da177e4SLinus Torvalds /*
14721da177e4SLinus Torvalds  *	This is a simple mechanism to ignore packets when
14731da177e4SLinus Torvalds  *	we are loaded. Just set ip_vs_drop_rate to 'n' and
14741da177e4SLinus Torvalds  *	we start to drop 1/rate of the packets
14751da177e4SLinus Torvalds  */
14761da177e4SLinus Torvalds 
1477a0840e2eSHans Schillstrom static inline int ip_vs_todrop(struct netns_ipvs *ipvs)
14781da177e4SLinus Torvalds {
1479a0840e2eSHans Schillstrom 	if (!ipvs->drop_rate)
1480a0840e2eSHans Schillstrom 		return 0;
1481a0840e2eSHans Schillstrom 	if (--ipvs->drop_counter > 0)
1482a0840e2eSHans Schillstrom 		return 0;
1483a0840e2eSHans Schillstrom 	ipvs->drop_counter = ipvs->drop_rate;
14841da177e4SLinus Torvalds 	return 1;
14851da177e4SLinus Torvalds }
14863a1bbf18SSimon Horman #else
14873a1bbf18SSimon Horman static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; }
14883a1bbf18SSimon Horman #endif
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds /*
14911da177e4SLinus Torvalds  *      ip_vs_fwd_tag returns the forwarding tag of the connection
14921da177e4SLinus Torvalds  */
14931da177e4SLinus Torvalds #define IP_VS_FWD_METHOD(cp)  (cp->flags & IP_VS_CONN_F_FWD_MASK)
14941da177e4SLinus Torvalds 
1495732db659SAdrian Bunk static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
14961da177e4SLinus Torvalds {
14971da177e4SLinus Torvalds 	char fwd;
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	switch (IP_VS_FWD_METHOD(cp)) {
15001da177e4SLinus Torvalds 	case IP_VS_CONN_F_MASQ:
15011da177e4SLinus Torvalds 		fwd = 'M'; break;
15021da177e4SLinus Torvalds 	case IP_VS_CONN_F_LOCALNODE:
15031da177e4SLinus Torvalds 		fwd = 'L'; break;
15041da177e4SLinus Torvalds 	case IP_VS_CONN_F_TUNNEL:
15051da177e4SLinus Torvalds 		fwd = 'T'; break;
15061da177e4SLinus Torvalds 	case IP_VS_CONN_F_DROUTE:
15071da177e4SLinus Torvalds 		fwd = 'R'; break;
15081da177e4SLinus Torvalds 	case IP_VS_CONN_F_BYPASS:
15091da177e4SLinus Torvalds 		fwd = 'B'; break;
15101da177e4SLinus Torvalds 	default:
15111da177e4SLinus Torvalds 		fwd = '?'; break;
15121da177e4SLinus Torvalds 	}
15131da177e4SLinus Torvalds 	return fwd;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
15165c3a0fd7SJoe Perches void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
15171da177e4SLinus Torvalds 		    struct ip_vs_conn *cp, int dir);
15181da177e4SLinus Torvalds 
1519b3cdd2a7SJulius Volz #ifdef CONFIG_IP_VS_IPV6
15205c3a0fd7SJoe Perches void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
1521b3cdd2a7SJulius Volz 		       struct ip_vs_conn *cp, int dir);
1522b3cdd2a7SJulius Volz #endif
1523b3cdd2a7SJulius Volz 
15245c3a0fd7SJoe Perches __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
15251da177e4SLinus Torvalds 
1526f9214b26SAl Viro static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
15271da177e4SLinus Torvalds {
1528f9214b26SAl Viro 	__be32 diff[2] = { ~old, new };
15291da177e4SLinus Torvalds 
153007f0757aSJoe Perches 	return csum_partial(diff, sizeof(diff), oldsum);
1531f9214b26SAl Viro }
1532f9214b26SAl Viro 
15330bbdd42bSJulius Volz #ifdef CONFIG_IP_VS_IPV6
15340bbdd42bSJulius Volz static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
15350bbdd42bSJulius Volz 					__wsum oldsum)
15360bbdd42bSJulius Volz {
15370bbdd42bSJulius Volz 	__be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
15380bbdd42bSJulius Volz 			    new[3],  new[2],  new[1],  new[0] };
15390bbdd42bSJulius Volz 
154007f0757aSJoe Perches 	return csum_partial(diff, sizeof(diff), oldsum);
15410bbdd42bSJulius Volz }
15420bbdd42bSJulius Volz #endif
15430bbdd42bSJulius Volz 
1544f9214b26SAl Viro static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
1545f9214b26SAl Viro {
1546f9214b26SAl Viro 	__be16 diff[2] = { ~old, new };
1547f9214b26SAl Viro 
154807f0757aSJoe Perches 	return csum_partial(diff, sizeof(diff), oldsum);
15491da177e4SLinus Torvalds }
15501da177e4SLinus Torvalds 
1551cf356d69SJulian Anastasov /*
1552cf356d69SJulian Anastasov  * Forget current conntrack (unconfirmed) and attach notrack entry
1553cf356d69SJulian Anastasov  */
1554cf356d69SJulian Anastasov static inline void ip_vs_notrack(struct sk_buff *skb)
1555cf356d69SJulian Anastasov {
1556cf356d69SJulian Anastasov #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1557cf356d69SJulian Anastasov 	enum ip_conntrack_info ctinfo;
155806b69390SJesper Juhl 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1559cf356d69SJulian Anastasov 
1560cf356d69SJulian Anastasov 	if (!ct || !nf_ct_is_untracked(ct)) {
15619e33ce45SLin Ming 		nf_conntrack_put(skb->nfct);
1562cf356d69SJulian Anastasov 		skb->nfct = &nf_ct_untracked_get()->ct_general;
1563cf356d69SJulian Anastasov 		skb->nfctinfo = IP_CT_NEW;
1564cf356d69SJulian Anastasov 		nf_conntrack_get(skb->nfct);
1565cf356d69SJulian Anastasov 	}
1566cf356d69SJulian Anastasov #endif
1567cf356d69SJulian Anastasov }
1568cf356d69SJulian Anastasov 
1569f4bc17cdSJulian Anastasov #ifdef CONFIG_IP_VS_NFCT
1570f4bc17cdSJulian Anastasov /*
1571f4bc17cdSJulian Anastasov  *      Netfilter connection tracking
1572f4bc17cdSJulian Anastasov  *      (from ip_vs_nfct.c)
1573f4bc17cdSJulian Anastasov  */
1574a0840e2eSHans Schillstrom static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
1575f4bc17cdSJulian Anastasov {
1576a4e2f5a7SSimon Horman #ifdef CONFIG_SYSCTL
1577a0840e2eSHans Schillstrom 	return ipvs->sysctl_conntrack;
1578a4e2f5a7SSimon Horman #else
1579a4e2f5a7SSimon Horman 	return 0;
1580a4e2f5a7SSimon Horman #endif
1581f4bc17cdSJulian Anastasov }
1582f4bc17cdSJulian Anastasov 
15835c3a0fd7SJoe Perches void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
15846523ce15SJulian Anastasov 			    int outin);
15855c3a0fd7SJoe Perches int ip_vs_confirm_conntrack(struct sk_buff *skb);
15865c3a0fd7SJoe Perches void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
1587f4bc17cdSJulian Anastasov 			       struct ip_vs_conn *cp, u_int8_t proto,
1588f4bc17cdSJulian Anastasov 			       const __be16 port, int from_rs);
15895c3a0fd7SJoe Perches void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
1590f4bc17cdSJulian Anastasov 
1591f4bc17cdSJulian Anastasov #else
1592f4bc17cdSJulian Anastasov 
1593a0840e2eSHans Schillstrom static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
1594f4bc17cdSJulian Anastasov {
1595f4bc17cdSJulian Anastasov 	return 0;
1596f4bc17cdSJulian Anastasov }
1597f4bc17cdSJulian Anastasov 
1598f4bc17cdSJulian Anastasov static inline void ip_vs_update_conntrack(struct sk_buff *skb,
1599f4bc17cdSJulian Anastasov 					  struct ip_vs_conn *cp, int outin)
1600f4bc17cdSJulian Anastasov {
1601f4bc17cdSJulian Anastasov }
1602f4bc17cdSJulian Anastasov 
1603e23ebf0fSKrzysztof Wilczynski static inline int ip_vs_confirm_conntrack(struct sk_buff *skb)
1604f4bc17cdSJulian Anastasov {
1605f4bc17cdSJulian Anastasov 	return NF_ACCEPT;
1606f4bc17cdSJulian Anastasov }
1607f4bc17cdSJulian Anastasov 
1608f4bc17cdSJulian Anastasov static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
1609f4bc17cdSJulian Anastasov {
1610f4bc17cdSJulian Anastasov }
1611f4bc17cdSJulian Anastasov /* CONFIG_IP_VS_NFCT */
1612f4bc17cdSJulian Anastasov #endif
16136523ce15SJulian Anastasov 
1614c16526a7SSimon Kirby static inline int
1615b552f7e3SChangli Gao ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
1616b552f7e3SChangli Gao {
1617b552f7e3SChangli Gao 	/*
1618b552f7e3SChangli Gao 	 * We think the overhead of processing active connections is 256
1619b552f7e3SChangli Gao 	 * times higher than that of inactive connections in average. (This
1620b552f7e3SChangli Gao 	 * 256 times might not be accurate, we will change it later) We
1621b552f7e3SChangli Gao 	 * use the following formula to estimate the overhead now:
1622b552f7e3SChangli Gao 	 *		  dest->activeconns*256 + dest->inactconns
1623b552f7e3SChangli Gao 	 */
1624b552f7e3SChangli Gao 	return (atomic_read(&dest->activeconns) << 8) +
1625b552f7e3SChangli Gao 		atomic_read(&dest->inactconns);
1626b552f7e3SChangli Gao }
1627b552f7e3SChangli Gao 
1628bc4768ebSJulius Volz #endif	/* _NET_IP_VS_H */
1629