xref: /openbmc/linux/include/net/ip_vs.h (revision c860c6b1479992440e4962e9c95d258bfdce4fca)
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 /* old ipvsadm versions still include this file directly */
121da177e4SLinus Torvalds #ifdef __KERNEL__
131da177e4SLinus Torvalds 
14bc4768ebSJulius Volz #include <asm/types.h>                  /* for __uXX types */
15bc4768ebSJulius Volz 
16bc4768ebSJulius Volz #include <linux/sysctl.h>               /* for ctl_path */
171da177e4SLinus Torvalds #include <linux/list.h>                 /* for struct list_head */
181da177e4SLinus Torvalds #include <linux/spinlock.h>             /* for struct rwlock_t */
191da177e4SLinus Torvalds #include <asm/atomic.h>                 /* for struct atomic_t */
201da177e4SLinus Torvalds #include <linux/compiler.h>
2114c85021SArnaldo Carvalho de Melo #include <linux/timer.h>
221da177e4SLinus Torvalds 
2314c85021SArnaldo Carvalho de Melo #include <net/checksum.h>
24e7ade46aSJulius Volz #include <linux/netfilter.h>		/* for union nf_inet_addr */
25e7ade46aSJulius Volz #include <linux/ipv6.h>			/* for struct ipv6hdr */
26e7ade46aSJulius Volz #include <net/ipv6.h>			/* for ipv6_addr_copy */
271da177e4SLinus Torvalds 
2864aae3cbSJulius Volz struct ip_vs_iphdr {
2964aae3cbSJulius Volz 	int len;
3064aae3cbSJulius Volz 	__u8 protocol;
3164aae3cbSJulius Volz 	union nf_inet_addr saddr;
3264aae3cbSJulius Volz 	union nf_inet_addr daddr;
3364aae3cbSJulius Volz };
3464aae3cbSJulius Volz 
3564aae3cbSJulius Volz static inline void
3664aae3cbSJulius Volz ip_vs_fill_iphdr(int af, const void *nh, struct ip_vs_iphdr *iphdr)
3764aae3cbSJulius Volz {
3864aae3cbSJulius Volz #ifdef CONFIG_IP_VS_IPV6
3964aae3cbSJulius Volz 	if (af == AF_INET6) {
4064aae3cbSJulius Volz 		const struct ipv6hdr *iph = nh;
4164aae3cbSJulius Volz 		iphdr->len = sizeof(struct ipv6hdr);
4264aae3cbSJulius Volz 		iphdr->protocol = iph->nexthdr;
4364aae3cbSJulius Volz 		ipv6_addr_copy(&iphdr->saddr.in6, &iph->saddr);
4464aae3cbSJulius Volz 		ipv6_addr_copy(&iphdr->daddr.in6, &iph->daddr);
4564aae3cbSJulius Volz 	} else
4664aae3cbSJulius Volz #endif
4764aae3cbSJulius Volz 	{
4864aae3cbSJulius Volz 		const struct iphdr *iph = nh;
4964aae3cbSJulius Volz 		iphdr->len = iph->ihl * 4;
5064aae3cbSJulius Volz 		iphdr->protocol = iph->protocol;
5164aae3cbSJulius Volz 		iphdr->saddr.ip = iph->saddr;
5264aae3cbSJulius Volz 		iphdr->daddr.ip = iph->daddr;
5364aae3cbSJulius Volz 	}
5464aae3cbSJulius Volz }
5564aae3cbSJulius Volz 
5664aae3cbSJulius Volz static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
5764aae3cbSJulius Volz 				   const union nf_inet_addr *src)
5864aae3cbSJulius Volz {
5964aae3cbSJulius Volz #ifdef CONFIG_IP_VS_IPV6
6064aae3cbSJulius Volz 	if (af == AF_INET6)
6164aae3cbSJulius Volz 		ipv6_addr_copy(&dst->in6, &src->in6);
6264aae3cbSJulius Volz 	else
6364aae3cbSJulius Volz #endif
6464aae3cbSJulius Volz 	dst->ip = src->ip;
6564aae3cbSJulius Volz }
6664aae3cbSJulius Volz 
6764aae3cbSJulius Volz static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
6864aae3cbSJulius Volz 				   const union nf_inet_addr *b)
6964aae3cbSJulius Volz {
7064aae3cbSJulius Volz #ifdef CONFIG_IP_VS_IPV6
7164aae3cbSJulius Volz 	if (af == AF_INET6)
7264aae3cbSJulius Volz 		return ipv6_addr_equal(&a->in6, &b->in6);
7364aae3cbSJulius Volz #endif
7464aae3cbSJulius Volz 	return a->ip == b->ip;
7564aae3cbSJulius Volz }
7664aae3cbSJulius Volz 
771da177e4SLinus Torvalds #ifdef CONFIG_IP_VS_DEBUG
7814c85021SArnaldo Carvalho de Melo #include <linux/net.h>
7914c85021SArnaldo Carvalho de Melo 
801da177e4SLinus Torvalds extern int ip_vs_get_debug_level(void);
81c842a3adSJulius Volz 
82c842a3adSJulius Volz static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
83c842a3adSJulius Volz 					 const union nf_inet_addr *addr,
84c842a3adSJulius Volz 					 int *idx)
85c842a3adSJulius Volz {
86c842a3adSJulius Volz 	int len;
87c842a3adSJulius Volz #ifdef CONFIG_IP_VS_IPV6
88c842a3adSJulius Volz 	if (af == AF_INET6)
89c842a3adSJulius Volz 		len = snprintf(&buf[*idx], buf_len - *idx, "[" NIP6_FMT "]",
90c842a3adSJulius Volz 			       NIP6(addr->in6)) + 1;
91c842a3adSJulius Volz 	else
92c842a3adSJulius Volz #endif
93c842a3adSJulius Volz 		len = snprintf(&buf[*idx], buf_len - *idx, NIPQUAD_FMT,
94c842a3adSJulius Volz 			       NIPQUAD(addr->ip)) + 1;
95c842a3adSJulius Volz 
96c842a3adSJulius Volz 	*idx += len;
97c842a3adSJulius Volz 	BUG_ON(*idx > buf_len + 1);
98c842a3adSJulius Volz 	return &buf[*idx - len];
99c842a3adSJulius Volz }
100c842a3adSJulius Volz 
101c842a3adSJulius Volz #define IP_VS_DBG_BUF(level, msg...)			\
102c842a3adSJulius Volz     do {						\
103c842a3adSJulius Volz 	    char ip_vs_dbg_buf[160];			\
104c842a3adSJulius Volz 	    int ip_vs_dbg_idx = 0;			\
105c842a3adSJulius Volz 	    if (level <= ip_vs_get_debug_level())	\
106c842a3adSJulius Volz 		    printk(KERN_DEBUG "IPVS: " msg);	\
107c842a3adSJulius Volz     } while (0)
108c842a3adSJulius Volz #define IP_VS_ERR_BUF(msg...)				\
109c842a3adSJulius Volz     do {						\
110c842a3adSJulius Volz 	    char ip_vs_dbg_buf[160];			\
111c842a3adSJulius Volz 	    int ip_vs_dbg_idx = 0;			\
112c842a3adSJulius Volz 	    printk(KERN_ERR "IPVS: " msg);		\
113c842a3adSJulius Volz     } while (0)
114c842a3adSJulius Volz 
115c842a3adSJulius Volz /* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
116c842a3adSJulius Volz #define IP_VS_DBG_ADDR(af, addr)			\
117c842a3adSJulius Volz     ip_vs_dbg_addr(af, ip_vs_dbg_buf,			\
118c842a3adSJulius Volz 		   sizeof(ip_vs_dbg_buf), addr,		\
119c842a3adSJulius Volz 		   &ip_vs_dbg_idx)
120c842a3adSJulius Volz 
1211da177e4SLinus Torvalds #define IP_VS_DBG(level, msg...)			\
1221da177e4SLinus Torvalds     do {						\
1231da177e4SLinus Torvalds 	    if (level <= ip_vs_get_debug_level())	\
1241da177e4SLinus Torvalds 		    printk(KERN_DEBUG "IPVS: " msg);	\
1251da177e4SLinus Torvalds     } while (0)
1261da177e4SLinus Torvalds #define IP_VS_DBG_RL(msg...)				\
1271da177e4SLinus Torvalds     do {						\
1281da177e4SLinus Torvalds 	    if (net_ratelimit())			\
1291da177e4SLinus Torvalds 		    printk(KERN_DEBUG "IPVS: " msg);	\
1301da177e4SLinus Torvalds     } while (0)
1311da177e4SLinus Torvalds #define IP_VS_DBG_PKT(level, pp, skb, ofs, msg)		\
1321da177e4SLinus Torvalds     do {						\
1331da177e4SLinus Torvalds 	    if (level <= ip_vs_get_debug_level())	\
1341da177e4SLinus Torvalds 		pp->debug_packet(pp, skb, ofs, msg);	\
1351da177e4SLinus Torvalds     } while (0)
1361da177e4SLinus Torvalds #define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg)	\
1371da177e4SLinus Torvalds     do {						\
1381da177e4SLinus Torvalds 	    if (level <= ip_vs_get_debug_level() &&	\
1391da177e4SLinus Torvalds 		net_ratelimit())			\
1401da177e4SLinus Torvalds 		pp->debug_packet(pp, skb, ofs, msg);	\
1411da177e4SLinus Torvalds     } while (0)
1421da177e4SLinus Torvalds #else	/* NO DEBUGGING at ALL */
143c842a3adSJulius Volz #define IP_VS_DBG_BUF(level, msg...)  do {} while (0)
144c842a3adSJulius Volz #define IP_VS_ERR_BUF(msg...)  do {} while (0)
1451da177e4SLinus Torvalds #define IP_VS_DBG(level, msg...)  do {} while (0)
1461da177e4SLinus Torvalds #define IP_VS_DBG_RL(msg...)  do {} while (0)
1471da177e4SLinus Torvalds #define IP_VS_DBG_PKT(level, pp, skb, ofs, msg)		do {} while (0)
1481da177e4SLinus Torvalds #define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg)	do {} while (0)
1491da177e4SLinus Torvalds #endif
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds #define IP_VS_BUG() BUG()
1521da177e4SLinus Torvalds #define IP_VS_ERR(msg...) printk(KERN_ERR "IPVS: " msg)
1531da177e4SLinus Torvalds #define IP_VS_INFO(msg...) printk(KERN_INFO "IPVS: " msg)
1541da177e4SLinus Torvalds #define IP_VS_WARNING(msg...) \
1551da177e4SLinus Torvalds 	printk(KERN_WARNING "IPVS: " msg)
1561da177e4SLinus Torvalds #define IP_VS_ERR_RL(msg...)				\
1571da177e4SLinus Torvalds     do {						\
1581da177e4SLinus Torvalds 	    if (net_ratelimit())			\
1591da177e4SLinus Torvalds 		    printk(KERN_ERR "IPVS: " msg);	\
1601da177e4SLinus Torvalds     } while (0)
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds #ifdef CONFIG_IP_VS_DEBUG
1631da177e4SLinus Torvalds #define EnterFunction(level)						\
1641da177e4SLinus Torvalds     do {								\
1651da177e4SLinus Torvalds 	    if (level <= ip_vs_get_debug_level())			\
1661da177e4SLinus Torvalds 		    printk(KERN_DEBUG "Enter: %s, %s line %i\n",	\
1671da177e4SLinus Torvalds 			   __FUNCTION__, __FILE__, __LINE__);		\
1681da177e4SLinus Torvalds     } while (0)
1691da177e4SLinus Torvalds #define LeaveFunction(level)                                            \
1701da177e4SLinus Torvalds     do {                                                                \
1711da177e4SLinus Torvalds 	    if (level <= ip_vs_get_debug_level())                       \
1721da177e4SLinus Torvalds 			printk(KERN_DEBUG "Leave: %s, %s line %i\n",    \
1731da177e4SLinus Torvalds 			       __FUNCTION__, __FILE__, __LINE__);       \
1741da177e4SLinus Torvalds     } while (0)
1751da177e4SLinus Torvalds #else
1761da177e4SLinus Torvalds #define EnterFunction(level)   do {} while (0)
1771da177e4SLinus Torvalds #define LeaveFunction(level)   do {} while (0)
1781da177e4SLinus Torvalds #endif
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds #define	IP_VS_WAIT_WHILE(expr)	while (expr) { cpu_relax(); }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /*
1841da177e4SLinus Torvalds  *      The port number of FTP service (in network order).
1851da177e4SLinus Torvalds  */
1861da177e4SLinus Torvalds #define FTPPORT  __constant_htons(21)
1871da177e4SLinus Torvalds #define FTPDATA  __constant_htons(20)
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
1901da177e4SLinus Torvalds  *      TCP State Values
1911da177e4SLinus Torvalds  */
1921da177e4SLinus Torvalds enum {
1931da177e4SLinus Torvalds 	IP_VS_TCP_S_NONE = 0,
1941da177e4SLinus Torvalds 	IP_VS_TCP_S_ESTABLISHED,
1951da177e4SLinus Torvalds 	IP_VS_TCP_S_SYN_SENT,
1961da177e4SLinus Torvalds 	IP_VS_TCP_S_SYN_RECV,
1971da177e4SLinus Torvalds 	IP_VS_TCP_S_FIN_WAIT,
1981da177e4SLinus Torvalds 	IP_VS_TCP_S_TIME_WAIT,
1991da177e4SLinus Torvalds 	IP_VS_TCP_S_CLOSE,
2001da177e4SLinus Torvalds 	IP_VS_TCP_S_CLOSE_WAIT,
2011da177e4SLinus Torvalds 	IP_VS_TCP_S_LAST_ACK,
2021da177e4SLinus Torvalds 	IP_VS_TCP_S_LISTEN,
2031da177e4SLinus Torvalds 	IP_VS_TCP_S_SYNACK,
2041da177e4SLinus Torvalds 	IP_VS_TCP_S_LAST
2051da177e4SLinus Torvalds };
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /*
2081da177e4SLinus Torvalds  *	UDP State Values
2091da177e4SLinus Torvalds  */
2101da177e4SLinus Torvalds enum {
2111da177e4SLinus Torvalds 	IP_VS_UDP_S_NORMAL,
2121da177e4SLinus Torvalds 	IP_VS_UDP_S_LAST,
2131da177e4SLinus Torvalds };
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds /*
2161da177e4SLinus Torvalds  *	ICMP State Values
2171da177e4SLinus Torvalds  */
2181da177e4SLinus Torvalds enum {
2191da177e4SLinus Torvalds 	IP_VS_ICMP_S_NORMAL,
2201da177e4SLinus Torvalds 	IP_VS_ICMP_S_LAST,
2211da177e4SLinus Torvalds };
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds /*
2241da177e4SLinus Torvalds  *	Delta sequence info structure
2251da177e4SLinus Torvalds  *	Each ip_vs_conn has 2 (output AND input seq. changes).
2261da177e4SLinus Torvalds  *      Only used in the VS/NAT.
2271da177e4SLinus Torvalds  */
2281da177e4SLinus Torvalds struct ip_vs_seq {
2291da177e4SLinus Torvalds 	__u32			init_seq;	/* Add delta from this seq */
2301da177e4SLinus Torvalds 	__u32			delta;		/* Delta in sequence numbers */
2311da177e4SLinus Torvalds 	__u32			previous_delta;	/* Delta in sequence numbers
2321da177e4SLinus Torvalds 						   before last resized pkt */
2331da177e4SLinus Torvalds };
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds /*
2373a14a313SSven Wegener  *	IPVS statistics objects
2381da177e4SLinus Torvalds  */
2393a14a313SSven Wegener struct ip_vs_estimator {
2403a14a313SSven Wegener 	struct list_head	list;
2413a14a313SSven Wegener 
2423a14a313SSven Wegener 	u64			last_inbytes;
2433a14a313SSven Wegener 	u64			last_outbytes;
2443a14a313SSven Wegener 	u32			last_conns;
2453a14a313SSven Wegener 	u32			last_inpkts;
2463a14a313SSven Wegener 	u32			last_outpkts;
2473a14a313SSven Wegener 
2483a14a313SSven Wegener 	u32			cps;
2493a14a313SSven Wegener 	u32			inpps;
2503a14a313SSven Wegener 	u32			outpps;
2513a14a313SSven Wegener 	u32			inbps;
2523a14a313SSven Wegener 	u32			outbps;
2533a14a313SSven Wegener };
2543a14a313SSven Wegener 
2551da177e4SLinus Torvalds struct ip_vs_stats
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	__u32                   conns;          /* connections scheduled */
2581da177e4SLinus Torvalds 	__u32                   inpkts;         /* incoming packets */
2591da177e4SLinus Torvalds 	__u32                   outpkts;        /* outgoing packets */
2601da177e4SLinus Torvalds 	__u64                   inbytes;        /* incoming bytes */
2611da177e4SLinus Torvalds 	__u64                   outbytes;       /* outgoing bytes */
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	__u32			cps;		/* current connection rate */
2641da177e4SLinus Torvalds 	__u32			inpps;		/* current in packet rate */
2651da177e4SLinus Torvalds 	__u32			outpps;		/* current out packet rate */
2661da177e4SLinus Torvalds 	__u32			inbps;		/* current in byte rate */
2671da177e4SLinus Torvalds 	__u32			outbps;		/* current out byte rate */
2681da177e4SLinus Torvalds 
2693a14a313SSven Wegener 	/*
2703a14a313SSven Wegener 	 * Don't add anything before the lock, because we use memcpy() to copy
2713a14a313SSven Wegener 	 * the members before the lock to struct ip_vs_stats_user in
2723a14a313SSven Wegener 	 * ip_vs_ctl.c.
2733a14a313SSven Wegener 	 */
2743a14a313SSven Wegener 
2751da177e4SLinus Torvalds 	spinlock_t              lock;           /* spin lock */
2763a14a313SSven Wegener 
2773a14a313SSven Wegener 	struct ip_vs_estimator	est;		/* estimator */
2781da177e4SLinus Torvalds };
2791da177e4SLinus Torvalds 
28014c85021SArnaldo Carvalho de Melo struct dst_entry;
28114c85021SArnaldo Carvalho de Melo struct iphdr;
2821da177e4SLinus Torvalds struct ip_vs_conn;
2831da177e4SLinus Torvalds struct ip_vs_app;
28414c85021SArnaldo Carvalho de Melo struct sk_buff;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds struct ip_vs_protocol {
2871da177e4SLinus Torvalds 	struct ip_vs_protocol	*next;
2881da177e4SLinus Torvalds 	char			*name;
2892ad17defSJulian Anastasov 	u16			protocol;
2902ad17defSJulian Anastasov 	u16			num_states;
2911da177e4SLinus Torvalds 	int			dont_defrag;
2921da177e4SLinus Torvalds 	atomic_t		appcnt;		/* counter of proto app incs */
2931da177e4SLinus Torvalds 	int			*timeout_table;	/* protocol timeout table */
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	void (*init)(struct ip_vs_protocol *pp);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	void (*exit)(struct ip_vs_protocol *pp);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	int (*conn_schedule)(struct sk_buff *skb,
3001da177e4SLinus Torvalds 			     struct ip_vs_protocol *pp,
3011da177e4SLinus Torvalds 			     int *verdict, struct ip_vs_conn **cpp);
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	struct ip_vs_conn *
3041da177e4SLinus Torvalds 	(*conn_in_get)(const struct sk_buff *skb,
3051da177e4SLinus Torvalds 		       struct ip_vs_protocol *pp,
3061da177e4SLinus Torvalds 		       const struct iphdr *iph,
3071da177e4SLinus Torvalds 		       unsigned int proto_off,
3081da177e4SLinus Torvalds 		       int inverse);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	struct ip_vs_conn *
3111da177e4SLinus Torvalds 	(*conn_out_get)(const struct sk_buff *skb,
3121da177e4SLinus Torvalds 			struct ip_vs_protocol *pp,
3131da177e4SLinus Torvalds 			const struct iphdr *iph,
3141da177e4SLinus Torvalds 			unsigned int proto_off,
3151da177e4SLinus Torvalds 			int inverse);
3161da177e4SLinus Torvalds 
3173db05feaSHerbert Xu 	int (*snat_handler)(struct sk_buff *skb,
3181da177e4SLinus Torvalds 			    struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
3191da177e4SLinus Torvalds 
3203db05feaSHerbert Xu 	int (*dnat_handler)(struct sk_buff *skb,
3211da177e4SLinus Torvalds 			    struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	int (*csum_check)(struct sk_buff *skb, struct ip_vs_protocol *pp);
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	const char *(*state_name)(int state);
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	int (*state_transition)(struct ip_vs_conn *cp, int direction,
3281da177e4SLinus Torvalds 				const struct sk_buff *skb,
3291da177e4SLinus Torvalds 				struct ip_vs_protocol *pp);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	int (*register_app)(struct ip_vs_app *inc);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	void (*unregister_app)(struct ip_vs_app *inc);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	int (*app_conn_bind)(struct ip_vs_conn *cp);
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	void (*debug_packet)(struct ip_vs_protocol *pp,
3381da177e4SLinus Torvalds 			     const struct sk_buff *skb,
3391da177e4SLinus Torvalds 			     int offset,
3401da177e4SLinus Torvalds 			     const char *msg);
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
3451da177e4SLinus Torvalds };
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds /*
3501da177e4SLinus Torvalds  *	IP_VS structure allocated for each dynamically scheduled connection
3511da177e4SLinus Torvalds  */
3521da177e4SLinus Torvalds struct ip_vs_conn {
3531da177e4SLinus Torvalds 	struct list_head        c_list;         /* hashed list heads */
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	/* Protocol, addresses and port numbers */
356e7ade46aSJulius Volz 	u16                      af;		/* address family */
357e7ade46aSJulius Volz 	union nf_inet_addr       caddr;          /* client address */
358e7ade46aSJulius Volz 	union nf_inet_addr       vaddr;          /* virtual address */
359e7ade46aSJulius Volz 	union nf_inet_addr       daddr;          /* destination address */
360014d730dSAl Viro 	__be16                   cport;
361014d730dSAl Viro 	__be16                   vport;
362014d730dSAl Viro 	__be16                   dport;
3631da177e4SLinus Torvalds 	__u16                   protocol;       /* Which protocol (TCP/UDP) */
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	/* counter and timer */
3661da177e4SLinus Torvalds 	atomic_t		refcnt;		/* reference count */
3671da177e4SLinus Torvalds 	struct timer_list	timer;		/* Expiration timer */
3681da177e4SLinus Torvalds 	volatile unsigned long	timeout;	/* timeout */
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	/* Flags and state transition */
3711da177e4SLinus Torvalds 	spinlock_t              lock;           /* lock for state transition */
3721da177e4SLinus Torvalds 	volatile __u16          flags;          /* status flags */
3731da177e4SLinus Torvalds 	volatile __u16          state;          /* state info */
374efac5276SRumen G. Bogdanovski 	volatile __u16          old_state;      /* old state, to be used for
375efac5276SRumen G. Bogdanovski 						 * state transition triggerd
376efac5276SRumen G. Bogdanovski 						 * synchronization
377efac5276SRumen G. Bogdanovski 						 */
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	/* Control members */
3801da177e4SLinus Torvalds 	struct ip_vs_conn       *control;       /* Master control connection */
3811da177e4SLinus Torvalds 	atomic_t                n_control;      /* Number of controlled ones */
3821da177e4SLinus Torvalds 	struct ip_vs_dest       *dest;          /* real server */
3831da177e4SLinus Torvalds 	atomic_t                in_pkts;        /* incoming packet counter */
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	/* packet transmitter for different forwarding methods.  If it
3861da177e4SLinus Torvalds 	   mangles the packet, it must return NF_DROP or better NF_STOLEN,
3871da177e4SLinus Torvalds 	   otherwise this must be changed to a sk_buff **.
3881da177e4SLinus Torvalds 	 */
3891da177e4SLinus Torvalds 	int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
3901da177e4SLinus Torvalds 			   struct ip_vs_protocol *pp);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	/* Note: we can group the following members into a structure,
3931da177e4SLinus Torvalds 	   in order to save more space, and the following members are
3941da177e4SLinus Torvalds 	   only used in VS/NAT anyway */
3951da177e4SLinus Torvalds 	struct ip_vs_app        *app;           /* bound ip_vs_app object */
3961da177e4SLinus Torvalds 	void                    *app_data;      /* Application private data */
3971da177e4SLinus Torvalds 	struct ip_vs_seq        in_seq;         /* incoming seq. struct */
3981da177e4SLinus Torvalds 	struct ip_vs_seq        out_seq;        /* outgoing seq. struct */
3991da177e4SLinus Torvalds };
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /*
403*c860c6b1SJulius Volz  *	Extended internal versions of struct ip_vs_service_user and
404*c860c6b1SJulius Volz  *	ip_vs_dest_user for IPv6 support.
405*c860c6b1SJulius Volz  *
406*c860c6b1SJulius Volz  *	We need these to conveniently pass around service and destination
407*c860c6b1SJulius Volz  *	options, but unfortunately, we also need to keep the old definitions to
408*c860c6b1SJulius Volz  *	maintain userspace backwards compatibility for the setsockopt interface.
409*c860c6b1SJulius Volz  */
410*c860c6b1SJulius Volz struct ip_vs_service_user_kern {
411*c860c6b1SJulius Volz 	/* virtual service addresses */
412*c860c6b1SJulius Volz 	u16			af;
413*c860c6b1SJulius Volz 	u16			protocol;
414*c860c6b1SJulius Volz 	union nf_inet_addr	addr;		/* virtual ip address */
415*c860c6b1SJulius Volz 	u16			port;
416*c860c6b1SJulius Volz 	u32			fwmark;		/* firwall mark of service */
417*c860c6b1SJulius Volz 
418*c860c6b1SJulius Volz 	/* virtual service options */
419*c860c6b1SJulius Volz 	char			*sched_name;
420*c860c6b1SJulius Volz 	unsigned		flags;		/* virtual service flags */
421*c860c6b1SJulius Volz 	unsigned		timeout;	/* persistent timeout in sec */
422*c860c6b1SJulius Volz 	u32			netmask;	/* persistent netmask */
423*c860c6b1SJulius Volz };
424*c860c6b1SJulius Volz 
425*c860c6b1SJulius Volz 
426*c860c6b1SJulius Volz struct ip_vs_dest_user_kern {
427*c860c6b1SJulius Volz 	/* destination server address */
428*c860c6b1SJulius Volz 	union nf_inet_addr	addr;
429*c860c6b1SJulius Volz 	u16			port;
430*c860c6b1SJulius Volz 
431*c860c6b1SJulius Volz 	/* real server options */
432*c860c6b1SJulius Volz 	unsigned		conn_flags;	/* connection flags */
433*c860c6b1SJulius Volz 	int			weight;		/* destination weight */
434*c860c6b1SJulius Volz 
435*c860c6b1SJulius Volz 	/* thresholds for active connections */
436*c860c6b1SJulius Volz 	u32			u_threshold;	/* upper threshold */
437*c860c6b1SJulius Volz 	u32			l_threshold;	/* lower threshold */
438*c860c6b1SJulius Volz };
439*c860c6b1SJulius Volz 
440*c860c6b1SJulius Volz 
441*c860c6b1SJulius Volz /*
4421da177e4SLinus Torvalds  *	The information about the virtual service offered to the net
4431da177e4SLinus Torvalds  *	and the forwarding entries
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds struct ip_vs_service {
4461da177e4SLinus Torvalds 	struct list_head	s_list;   /* for normal service table */
4471da177e4SLinus Torvalds 	struct list_head	f_list;   /* for fwmark-based service table */
4481da177e4SLinus Torvalds 	atomic_t		refcnt;   /* reference counter */
4491da177e4SLinus Torvalds 	atomic_t		usecnt;   /* use counter */
4501da177e4SLinus Torvalds 
451e7ade46aSJulius Volz 	u16			af;       /* address family */
4521da177e4SLinus Torvalds 	__u16			protocol; /* which protocol (TCP/UDP) */
453e7ade46aSJulius Volz 	union nf_inet_addr	addr;	  /* IP address for virtual service */
454014d730dSAl Viro 	__be16			port;	  /* port number for the service */
4551da177e4SLinus Torvalds 	__u32                   fwmark;   /* firewall mark of the service */
4561da177e4SLinus Torvalds 	unsigned		flags;	  /* service status flags */
4571da177e4SLinus Torvalds 	unsigned		timeout;  /* persistent timeout in ticks */
458014d730dSAl Viro 	__be32			netmask;  /* grouping granularity */
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	struct list_head	destinations;  /* real server d-linked list */
4611da177e4SLinus Torvalds 	__u32			num_dests;     /* number of servers */
4621da177e4SLinus Torvalds 	struct ip_vs_stats      stats;         /* statistics for the service */
4631da177e4SLinus Torvalds 	struct ip_vs_app	*inc;	  /* bind conns to this app inc */
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	/* for scheduling */
4661da177e4SLinus Torvalds 	struct ip_vs_scheduler	*scheduler;    /* bound scheduler object */
4671da177e4SLinus Torvalds 	rwlock_t		sched_lock;    /* lock sched_data */
4681da177e4SLinus Torvalds 	void			*sched_data;   /* scheduler application data */
4691da177e4SLinus Torvalds };
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds /*
4731da177e4SLinus Torvalds  *	The real server destination forwarding entry
4741da177e4SLinus Torvalds  *	with ip address, port number, and so on.
4751da177e4SLinus Torvalds  */
4761da177e4SLinus Torvalds struct ip_vs_dest {
4771da177e4SLinus Torvalds 	struct list_head	n_list;   /* for the dests in the service */
4781da177e4SLinus Torvalds 	struct list_head	d_list;   /* for table with all the dests */
4791da177e4SLinus Torvalds 
480e7ade46aSJulius Volz 	u16			af;		/* address family */
481e7ade46aSJulius Volz 	union nf_inet_addr	addr;		/* IP address of the server */
482014d730dSAl Viro 	__be16			port;		/* port number of the server */
4831da177e4SLinus Torvalds 	volatile unsigned	flags;		/* dest status flags */
4841da177e4SLinus Torvalds 	atomic_t		conn_flags;	/* flags to copy to conn */
4851da177e4SLinus Torvalds 	atomic_t		weight;		/* server weight */
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	atomic_t		refcnt;		/* reference counter */
4881da177e4SLinus Torvalds 	struct ip_vs_stats      stats;          /* statistics */
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	/* connection counters and thresholds */
4911da177e4SLinus Torvalds 	atomic_t		activeconns;	/* active connections */
4921da177e4SLinus Torvalds 	atomic_t		inactconns;	/* inactive connections */
4931da177e4SLinus Torvalds 	atomic_t		persistconns;	/* persistent connections */
4941da177e4SLinus Torvalds 	__u32			u_threshold;	/* upper threshold */
4951da177e4SLinus Torvalds 	__u32			l_threshold;	/* lower threshold */
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	/* for destination cache */
4981da177e4SLinus Torvalds 	spinlock_t		dst_lock;	/* lock of dst_cache */
4991da177e4SLinus Torvalds 	struct dst_entry	*dst_cache;	/* destination cache entry */
5001da177e4SLinus Torvalds 	u32			dst_rtos;	/* RT_TOS(tos) for dst */
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	/* for virtual service */
5031da177e4SLinus Torvalds 	struct ip_vs_service	*svc;		/* service it belongs to */
5041da177e4SLinus Torvalds 	__u16			protocol;	/* which protocol (TCP/UDP) */
505e7ade46aSJulius Volz 	union nf_inet_addr	vaddr;		/* virtual IP address */
506014d730dSAl Viro 	__be16			vport;		/* virtual port number */
5071da177e4SLinus Torvalds 	__u32			vfwmark;	/* firewall mark of service */
5081da177e4SLinus Torvalds };
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds /*
5121da177e4SLinus Torvalds  *	The scheduler object
5131da177e4SLinus Torvalds  */
5141da177e4SLinus Torvalds struct ip_vs_scheduler {
5151da177e4SLinus Torvalds 	struct list_head	n_list;		/* d-linked list head */
5161da177e4SLinus Torvalds 	char			*name;		/* scheduler name */
5171da177e4SLinus Torvalds 	atomic_t		refcnt;		/* reference counter */
5181da177e4SLinus Torvalds 	struct module		*module;	/* THIS_MODULE/NULL */
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	/* scheduler initializing service */
5211da177e4SLinus Torvalds 	int (*init_service)(struct ip_vs_service *svc);
5221da177e4SLinus Torvalds 	/* scheduling service finish */
5231da177e4SLinus Torvalds 	int (*done_service)(struct ip_vs_service *svc);
5241da177e4SLinus Torvalds 	/* scheduler updating service */
5251da177e4SLinus Torvalds 	int (*update_service)(struct ip_vs_service *svc);
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	/* selecting a server from the given service */
5281da177e4SLinus Torvalds 	struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
5291da177e4SLinus Torvalds 				       const struct sk_buff *skb);
5301da177e4SLinus Torvalds };
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds /*
5341da177e4SLinus Torvalds  *	The application module object (a.k.a. app incarnation)
5351da177e4SLinus Torvalds  */
5361da177e4SLinus Torvalds struct ip_vs_app
5371da177e4SLinus Torvalds {
5381da177e4SLinus Torvalds 	struct list_head	a_list;		/* member in app list */
5391da177e4SLinus Torvalds 	int			type;		/* IP_VS_APP_TYPE_xxx */
5401da177e4SLinus Torvalds 	char			*name;		/* application module name */
5411da177e4SLinus Torvalds 	__u16			protocol;
5421da177e4SLinus Torvalds 	struct module		*module;	/* THIS_MODULE/NULL */
5431da177e4SLinus Torvalds 	struct list_head	incs_list;	/* list of incarnations */
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	/* members for application incarnations */
5461da177e4SLinus Torvalds 	struct list_head	p_list;		/* member in proto app list */
5471da177e4SLinus Torvalds 	struct ip_vs_app	*app;		/* its real application */
548014d730dSAl Viro 	__be16			port;		/* port number in net order */
5491da177e4SLinus Torvalds 	atomic_t		usecnt;		/* usage counter */
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	/* output hook: return false if can't linearize. diff set for TCP.  */
5521da177e4SLinus Torvalds 	int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
5533db05feaSHerbert Xu 		       struct sk_buff *, int *diff);
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	/* input hook: return false if can't linearize. diff set for TCP. */
5561da177e4SLinus Torvalds 	int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
5573db05feaSHerbert Xu 		      struct sk_buff *, int *diff);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	/* ip_vs_app initializer */
5601da177e4SLinus Torvalds 	int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	/* ip_vs_app finish */
5631da177e4SLinus Torvalds 	int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	/* not used now */
5671da177e4SLinus Torvalds 	int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
5681da177e4SLinus Torvalds 			 struct ip_vs_protocol *);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	int *			timeout_table;
5731da177e4SLinus Torvalds 	int *			timeouts;
5741da177e4SLinus Torvalds 	int			timeouts_size;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
5771da177e4SLinus Torvalds 			     int *verdict, struct ip_vs_conn **cpp);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	struct ip_vs_conn *
5801da177e4SLinus Torvalds 	(*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
5811da177e4SLinus Torvalds 		       const struct iphdr *iph, unsigned int proto_off,
5821da177e4SLinus Torvalds 		       int inverse);
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	struct ip_vs_conn *
5851da177e4SLinus Torvalds 	(*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
5861da177e4SLinus Torvalds 			const struct iphdr *iph, unsigned int proto_off,
5871da177e4SLinus Torvalds 			int inverse);
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	int (*state_transition)(struct ip_vs_conn *cp, int direction,
5901da177e4SLinus Torvalds 				const struct sk_buff *skb,
5911da177e4SLinus Torvalds 				struct ip_vs_app *app);
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	void (*timeout_change)(struct ip_vs_app *app, int flags);
5941da177e4SLinus Torvalds };
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds /*
5981da177e4SLinus Torvalds  *      IPVS core functions
5991da177e4SLinus Torvalds  *      (from ip_vs_core.c)
6001da177e4SLinus Torvalds  */
6011da177e4SLinus Torvalds extern const char *ip_vs_proto_name(unsigned proto);
6021da177e4SLinus Torvalds extern void ip_vs_init_hash_table(struct list_head *table, int rows);
603afdd6140SSven Wegener #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds #define IP_VS_APP_TYPE_FTP	1
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds /*
6081da177e4SLinus Torvalds  *     ip_vs_conn handling functions
6091da177e4SLinus Torvalds  *     (from ip_vs_conn.c)
6101da177e4SLinus Torvalds  */
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds /*
6131da177e4SLinus Torvalds  *     IPVS connection entry hash table
6141da177e4SLinus Torvalds  */
6151da177e4SLinus Torvalds #ifndef CONFIG_IP_VS_TAB_BITS
6161da177e4SLinus Torvalds #define CONFIG_IP_VS_TAB_BITS   12
6171da177e4SLinus Torvalds #endif
6181da177e4SLinus Torvalds /* make sure that IP_VS_CONN_TAB_BITS is located in [8, 20] */
6191da177e4SLinus Torvalds #if CONFIG_IP_VS_TAB_BITS < 8
6201da177e4SLinus Torvalds #define IP_VS_CONN_TAB_BITS	8
6211da177e4SLinus Torvalds #endif
6221da177e4SLinus Torvalds #if CONFIG_IP_VS_TAB_BITS > 20
6231da177e4SLinus Torvalds #define IP_VS_CONN_TAB_BITS	20
6241da177e4SLinus Torvalds #endif
6251da177e4SLinus Torvalds #if 8 <= CONFIG_IP_VS_TAB_BITS && CONFIG_IP_VS_TAB_BITS <= 20
6261da177e4SLinus Torvalds #define IP_VS_CONN_TAB_BITS	CONFIG_IP_VS_TAB_BITS
6271da177e4SLinus Torvalds #endif
6281da177e4SLinus Torvalds #define IP_VS_CONN_TAB_SIZE     (1 << IP_VS_CONN_TAB_BITS)
6291da177e4SLinus Torvalds #define IP_VS_CONN_TAB_MASK     (IP_VS_CONN_TAB_SIZE - 1)
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds enum {
6321da177e4SLinus Torvalds 	IP_VS_DIR_INPUT = 0,
6331da177e4SLinus Torvalds 	IP_VS_DIR_OUTPUT,
6341da177e4SLinus Torvalds 	IP_VS_DIR_INPUT_ONLY,
6351da177e4SLinus Torvalds 	IP_VS_DIR_LAST,
6361da177e4SLinus Torvalds };
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds extern struct ip_vs_conn *ip_vs_conn_in_get
639014d730dSAl Viro (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
64087375ab4SJulian Anastasov extern struct ip_vs_conn *ip_vs_ct_in_get
641014d730dSAl Viro (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
6421da177e4SLinus Torvalds extern struct ip_vs_conn *ip_vs_conn_out_get
643014d730dSAl Viro (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds /* put back the conn without restarting its timer */
6461da177e4SLinus Torvalds static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	atomic_dec(&cp->refcnt);
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds extern void ip_vs_conn_put(struct ip_vs_conn *cp);
651014d730dSAl Viro extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds extern struct ip_vs_conn *
654014d730dSAl Viro ip_vs_conn_new(int proto, __be32 caddr, __be16 cport, __be32 vaddr, __be16 vport,
655014d730dSAl Viro 	       __be32 daddr, __be16 dport, unsigned flags,
6561da177e4SLinus Torvalds 	       struct ip_vs_dest *dest);
6571da177e4SLinus Torvalds extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds extern const char * ip_vs_state_name(__u16 proto, int state);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
6621da177e4SLinus Torvalds extern int ip_vs_check_template(struct ip_vs_conn *ct);
6631da177e4SLinus Torvalds extern void ip_vs_random_dropentry(void);
6641da177e4SLinus Torvalds extern int ip_vs_conn_init(void);
6651da177e4SLinus Torvalds extern void ip_vs_conn_cleanup(void);
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds static inline void ip_vs_control_del(struct ip_vs_conn *cp)
6681da177e4SLinus Torvalds {
6691da177e4SLinus Torvalds 	struct ip_vs_conn *ctl_cp = cp->control;
6701da177e4SLinus Torvalds 	if (!ctl_cp) {
6711da177e4SLinus Torvalds 		IP_VS_ERR("request control DEL for uncontrolled: "
6721da177e4SLinus Torvalds 			  "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
6731da177e4SLinus Torvalds 			  NIPQUAD(cp->caddr),ntohs(cp->cport),
6741da177e4SLinus Torvalds 			  NIPQUAD(cp->vaddr),ntohs(cp->vport));
6751da177e4SLinus Torvalds 		return;
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	IP_VS_DBG(7, "DELeting control for: "
6791da177e4SLinus Torvalds 		  "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
6801da177e4SLinus Torvalds 		  NIPQUAD(cp->caddr),ntohs(cp->cport),
6811da177e4SLinus Torvalds 		  NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	cp->control = NULL;
6841da177e4SLinus Torvalds 	if (atomic_read(&ctl_cp->n_control) == 0) {
6851da177e4SLinus Torvalds 		IP_VS_ERR("BUG control DEL with n=0 : "
6861da177e4SLinus Torvalds 			  "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
6871da177e4SLinus Torvalds 			  NIPQUAD(cp->caddr),ntohs(cp->cport),
6881da177e4SLinus Torvalds 			  NIPQUAD(cp->vaddr),ntohs(cp->vport));
6891da177e4SLinus Torvalds 		return;
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 	atomic_dec(&ctl_cp->n_control);
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds static inline void
6951da177e4SLinus Torvalds ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
6961da177e4SLinus Torvalds {
6971da177e4SLinus Torvalds 	if (cp->control) {
6981da177e4SLinus Torvalds 		IP_VS_ERR("request control ADD for already controlled: "
6991da177e4SLinus Torvalds 			  "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
7001da177e4SLinus Torvalds 			  NIPQUAD(cp->caddr),ntohs(cp->cport),
7011da177e4SLinus Torvalds 			  NIPQUAD(cp->vaddr),ntohs(cp->vport));
7021da177e4SLinus Torvalds 		ip_vs_control_del(cp);
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	IP_VS_DBG(7, "ADDing control for: "
7061da177e4SLinus Torvalds 		  "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
7071da177e4SLinus Torvalds 		  NIPQUAD(cp->caddr),ntohs(cp->cport),
7081da177e4SLinus Torvalds 		  NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	cp->control = ctl_cp;
7111da177e4SLinus Torvalds 	atomic_inc(&ctl_cp->n_control);
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds /*
7161da177e4SLinus Torvalds  *      IPVS application functions
7171da177e4SLinus Torvalds  *      (from ip_vs_app.c)
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds #define IP_VS_APP_MAX_PORTS  8
7201da177e4SLinus Torvalds extern int register_ip_vs_app(struct ip_vs_app *app);
7211da177e4SLinus Torvalds extern void unregister_ip_vs_app(struct ip_vs_app *app);
7221da177e4SLinus Torvalds extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
7231da177e4SLinus Torvalds extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
7241da177e4SLinus Torvalds extern int
7251da177e4SLinus Torvalds register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
7261da177e4SLinus Torvalds extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
7271da177e4SLinus Torvalds extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
7281da177e4SLinus Torvalds 
7293db05feaSHerbert Xu extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
7303db05feaSHerbert Xu extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
731dd0fc66fSAl Viro extern int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
7321da177e4SLinus Torvalds 			     char *o_buf, int o_len, char *n_buf, int n_len);
7331da177e4SLinus Torvalds extern int ip_vs_app_init(void);
7341da177e4SLinus Torvalds extern void ip_vs_app_cleanup(void);
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds /*
7381da177e4SLinus Torvalds  *	IPVS protocol functions (from ip_vs_proto.c)
7391da177e4SLinus Torvalds  */
7401da177e4SLinus Torvalds extern int ip_vs_protocol_init(void);
7411da177e4SLinus Torvalds extern void ip_vs_protocol_cleanup(void);
7421da177e4SLinus Torvalds extern void ip_vs_protocol_timeout_change(int flags);
7431da177e4SLinus Torvalds extern int *ip_vs_create_timeout_table(int *table, int size);
7441da177e4SLinus Torvalds extern int
7451da177e4SLinus Torvalds ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to);
7461da177e4SLinus Torvalds extern void
7471da177e4SLinus Torvalds ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
7481da177e4SLinus Torvalds 			  int offset, const char *msg);
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds extern struct ip_vs_protocol ip_vs_protocol_tcp;
7511da177e4SLinus Torvalds extern struct ip_vs_protocol ip_vs_protocol_udp;
7521da177e4SLinus Torvalds extern struct ip_vs_protocol ip_vs_protocol_icmp;
7531da177e4SLinus Torvalds extern struct ip_vs_protocol ip_vs_protocol_esp;
7541da177e4SLinus Torvalds extern struct ip_vs_protocol ip_vs_protocol_ah;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds /*
7581da177e4SLinus Torvalds  *      Registering/unregistering scheduler functions
7591da177e4SLinus Torvalds  *      (from ip_vs_sched.c)
7601da177e4SLinus Torvalds  */
7611da177e4SLinus Torvalds extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
7621da177e4SLinus Torvalds extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
7631da177e4SLinus Torvalds extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
7641da177e4SLinus Torvalds 				struct ip_vs_scheduler *scheduler);
7651da177e4SLinus Torvalds extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
7661da177e4SLinus Torvalds extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
7671da177e4SLinus Torvalds extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
7681da177e4SLinus Torvalds extern struct ip_vs_conn *
7691da177e4SLinus Torvalds ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb);
7701da177e4SLinus Torvalds extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
7711da177e4SLinus Torvalds 			struct ip_vs_protocol *pp);
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds /*
7751da177e4SLinus Torvalds  *      IPVS control data and functions (from ip_vs_ctl.c)
7761da177e4SLinus Torvalds  */
7771da177e4SLinus Torvalds extern int sysctl_ip_vs_cache_bypass;
7781da177e4SLinus Torvalds extern int sysctl_ip_vs_expire_nodest_conn;
7791da177e4SLinus Torvalds extern int sysctl_ip_vs_expire_quiescent_template;
7801da177e4SLinus Torvalds extern int sysctl_ip_vs_sync_threshold[2];
7811da177e4SLinus Torvalds extern int sysctl_ip_vs_nat_icmp_send;
7821da177e4SLinus Torvalds extern struct ip_vs_stats ip_vs_stats;
7835587da55SSven Wegener extern const struct ctl_path net_vs_ctl_path[];
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds extern struct ip_vs_service *
786014d730dSAl Viro ip_vs_service_get(__u32 fwmark, __u16 protocol, __be32 vaddr, __be16 vport);
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds static inline void ip_vs_service_put(struct ip_vs_service *svc)
7891da177e4SLinus Torvalds {
7901da177e4SLinus Torvalds 	atomic_dec(&svc->usecnt);
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds extern struct ip_vs_dest *
794014d730dSAl Viro ip_vs_lookup_real_service(__u16 protocol, __be32 daddr, __be16 dport);
7951da177e4SLinus Torvalds extern int ip_vs_use_count_inc(void);
7961da177e4SLinus Torvalds extern void ip_vs_use_count_dec(void);
7971da177e4SLinus Torvalds extern int ip_vs_control_init(void);
7981da177e4SLinus Torvalds extern void ip_vs_control_cleanup(void);
7991e356f9cSRumen G. Bogdanovski extern struct ip_vs_dest *
8001e356f9cSRumen G. Bogdanovski ip_vs_find_dest(__be32 daddr, __be16 dport,
8011e356f9cSRumen G. Bogdanovski 		 __be32 vaddr, __be16 vport, __u16 protocol);
8021e356f9cSRumen G. Bogdanovski extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds /*
8061da177e4SLinus Torvalds  *      IPVS sync daemon data and function prototypes
8071da177e4SLinus Torvalds  *      (from ip_vs_sync.c)
8081da177e4SLinus Torvalds  */
8091da177e4SLinus Torvalds extern volatile int ip_vs_sync_state;
8101da177e4SLinus Torvalds extern volatile int ip_vs_master_syncid;
8111da177e4SLinus Torvalds extern volatile int ip_vs_backup_syncid;
8121da177e4SLinus Torvalds extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
8131da177e4SLinus Torvalds extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
8141da177e4SLinus Torvalds extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
8151da177e4SLinus Torvalds extern int stop_sync_thread(int state);
8161da177e4SLinus Torvalds extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds /*
8201da177e4SLinus Torvalds  *      IPVS rate estimator prototypes (from ip_vs_est.c)
8211da177e4SLinus Torvalds  */
822a919cf4bSSven Wegener extern int ip_vs_estimator_init(void);
823a919cf4bSSven Wegener extern void ip_vs_estimator_cleanup(void);
8243a14a313SSven Wegener extern void ip_vs_new_estimator(struct ip_vs_stats *stats);
8251da177e4SLinus Torvalds extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
8261da177e4SLinus Torvalds extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds /*
8291da177e4SLinus Torvalds  *	Various IPVS packet transmitters (from ip_vs_xmit.c)
8301da177e4SLinus Torvalds  */
8311da177e4SLinus Torvalds extern int ip_vs_null_xmit
8321da177e4SLinus Torvalds (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
8331da177e4SLinus Torvalds extern int ip_vs_bypass_xmit
8341da177e4SLinus Torvalds (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
8351da177e4SLinus Torvalds extern int ip_vs_nat_xmit
8361da177e4SLinus Torvalds (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
8371da177e4SLinus Torvalds extern int ip_vs_tunnel_xmit
8381da177e4SLinus Torvalds (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
8391da177e4SLinus Torvalds extern int ip_vs_dr_xmit
8401da177e4SLinus Torvalds (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
8411da177e4SLinus Torvalds extern int ip_vs_icmp_xmit
8421da177e4SLinus Torvalds (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
8431da177e4SLinus Torvalds extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds /*
8471da177e4SLinus Torvalds  *	This is a simple mechanism to ignore packets when
8481da177e4SLinus Torvalds  *	we are loaded. Just set ip_vs_drop_rate to 'n' and
8491da177e4SLinus Torvalds  *	we start to drop 1/rate of the packets
8501da177e4SLinus Torvalds  */
8511da177e4SLinus Torvalds extern int ip_vs_drop_rate;
8521da177e4SLinus Torvalds extern int ip_vs_drop_counter;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds static __inline__ int ip_vs_todrop(void)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	if (!ip_vs_drop_rate) return 0;
8571da177e4SLinus Torvalds 	if (--ip_vs_drop_counter > 0) return 0;
8581da177e4SLinus Torvalds 	ip_vs_drop_counter = ip_vs_drop_rate;
8591da177e4SLinus Torvalds 	return 1;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds /*
8631da177e4SLinus Torvalds  *      ip_vs_fwd_tag returns the forwarding tag of the connection
8641da177e4SLinus Torvalds  */
8651da177e4SLinus Torvalds #define IP_VS_FWD_METHOD(cp)  (cp->flags & IP_VS_CONN_F_FWD_MASK)
8661da177e4SLinus Torvalds 
867732db659SAdrian Bunk static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
8681da177e4SLinus Torvalds {
8691da177e4SLinus Torvalds 	char fwd;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	switch (IP_VS_FWD_METHOD(cp)) {
8721da177e4SLinus Torvalds 	case IP_VS_CONN_F_MASQ:
8731da177e4SLinus Torvalds 		fwd = 'M'; break;
8741da177e4SLinus Torvalds 	case IP_VS_CONN_F_LOCALNODE:
8751da177e4SLinus Torvalds 		fwd = 'L'; break;
8761da177e4SLinus Torvalds 	case IP_VS_CONN_F_TUNNEL:
8771da177e4SLinus Torvalds 		fwd = 'T'; break;
8781da177e4SLinus Torvalds 	case IP_VS_CONN_F_DROUTE:
8791da177e4SLinus Torvalds 		fwd = 'R'; break;
8801da177e4SLinus Torvalds 	case IP_VS_CONN_F_BYPASS:
8811da177e4SLinus Torvalds 		fwd = 'B'; break;
8821da177e4SLinus Torvalds 	default:
8831da177e4SLinus Torvalds 		fwd = '?'; break;
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 	return fwd;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
8891da177e4SLinus Torvalds 		struct ip_vs_conn *cp, int dir);
8901da177e4SLinus Torvalds 
891b1550f22SAl Viro extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
8921da177e4SLinus Torvalds 
893f9214b26SAl Viro static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
8941da177e4SLinus Torvalds {
895f9214b26SAl Viro 	__be32 diff[2] = { ~old, new };
8961da177e4SLinus Torvalds 
897f9214b26SAl Viro 	return csum_partial((char *) diff, sizeof(diff), oldsum);
898f9214b26SAl Viro }
899f9214b26SAl Viro 
900f9214b26SAl Viro static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
901f9214b26SAl Viro {
902f9214b26SAl Viro 	__be16 diff[2] = { ~old, new };
903f9214b26SAl Viro 
904f9214b26SAl Viro 	return csum_partial((char *) diff, sizeof(diff), oldsum);
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds #endif /* __KERNEL__ */
9081da177e4SLinus Torvalds 
909bc4768ebSJulius Volz #endif	/* _NET_IP_VS_H */
910