1 /*
2  *      IP Virtual Server
3  *      data structure and functionality definitions
4  */
5 
6 #ifndef _IP_VS_H
7 #define _IP_VS_H
8 
9 #include <asm/types.h>          /* For __uXX types */
10 
11 #define IP_VS_VERSION_CODE            0x01000C
12 #define NVERSION(version)                       \
13 	(version >> 16) & 0xFF,                 \
14 	(version >> 8) & 0xFF,                  \
15 	version & 0xFF
16 
17 /*
18  *      Virtual Service Flags
19  */
20 #define IP_VS_SVC_F_PERSISTENT        0x0001    /* persistent port */
21 #define IP_VS_SVC_F_HASHED            0x0002    /* hashed entry */
22 
23 /*
24  *      Destination Server Flags
25  */
26 #define IP_VS_DEST_F_AVAILABLE        0x0001    /* Available tag */
27 
28 /*
29  *      IPVS sync daemon states
30  */
31 #define IP_VS_STATE_NONE    0           /* daemon is stopped */
32 #define IP_VS_STATE_MASTER  1           /* started as master */
33 #define IP_VS_STATE_BACKUP  2           /* started as backup */
34 
35 /*
36  *      IPVS socket options
37  */
38 #define IP_VS_BASE_CTL		(64+1024+64)		/* base */
39 
40 #define IP_VS_SO_SET_NONE	IP_VS_BASE_CTL	        /* just peek */
41 #define IP_VS_SO_SET_INSERT	(IP_VS_BASE_CTL+1)
42 #define IP_VS_SO_SET_ADD	(IP_VS_BASE_CTL+2)
43 #define IP_VS_SO_SET_EDIT	(IP_VS_BASE_CTL+3)
44 #define IP_VS_SO_SET_DEL	(IP_VS_BASE_CTL+4)
45 #define IP_VS_SO_SET_FLUSH	(IP_VS_BASE_CTL+5)
46 #define IP_VS_SO_SET_LIST	(IP_VS_BASE_CTL+6)
47 #define IP_VS_SO_SET_ADDDEST	(IP_VS_BASE_CTL+7)
48 #define IP_VS_SO_SET_DELDEST	(IP_VS_BASE_CTL+8)
49 #define IP_VS_SO_SET_EDITDEST	(IP_VS_BASE_CTL+9)
50 #define IP_VS_SO_SET_TIMEOUTS	(IP_VS_BASE_CTL+10)
51 #define IP_VS_SO_SET_STARTDAEMON (IP_VS_BASE_CTL+11)
52 #define IP_VS_SO_SET_STOPDAEMON (IP_VS_BASE_CTL+12)
53 #define IP_VS_SO_SET_RESTORE    (IP_VS_BASE_CTL+13)
54 #define IP_VS_SO_SET_SAVE       (IP_VS_BASE_CTL+14)
55 #define IP_VS_SO_SET_ZERO	(IP_VS_BASE_CTL+15)
56 #define IP_VS_SO_SET_MAX	IP_VS_SO_SET_ZERO
57 
58 #define IP_VS_SO_GET_VERSION	IP_VS_BASE_CTL
59 #define IP_VS_SO_GET_INFO	(IP_VS_BASE_CTL+1)
60 #define IP_VS_SO_GET_SERVICES	(IP_VS_BASE_CTL+2)
61 #define IP_VS_SO_GET_SERVICE	(IP_VS_BASE_CTL+3)
62 #define IP_VS_SO_GET_DESTS	(IP_VS_BASE_CTL+4)
63 #define IP_VS_SO_GET_DEST	(IP_VS_BASE_CTL+5)	/* not used now */
64 #define IP_VS_SO_GET_TIMEOUTS	(IP_VS_BASE_CTL+6)
65 #define IP_VS_SO_GET_DAEMON	(IP_VS_BASE_CTL+7)
66 #define IP_VS_SO_GET_MAX	IP_VS_SO_GET_DAEMON
67 
68 
69 /*
70  *      IPVS Connection Flags
71  */
72 #define IP_VS_CONN_F_FWD_MASK         0x0007    /* mask for the fwd methods */
73 #define IP_VS_CONN_F_MASQ	      0x0000    /* masquerading */
74 #define IP_VS_CONN_F_LOCALNODE	      0x0001    /* local node */
75 #define IP_VS_CONN_F_TUNNEL	      0x0002    /* tunneling */
76 #define IP_VS_CONN_F_DROUTE           0x0003    /* direct routing */
77 #define IP_VS_CONN_F_BYPASS           0x0004    /* cache bypass */
78 #define IP_VS_CONN_F_HASHED	      0x0040	/* hashed entry */
79 #define IP_VS_CONN_F_NOOUTPUT         0x0080    /* no output packets */
80 #define IP_VS_CONN_F_INACTIVE         0x0100    /* not established */
81 #define IP_VS_CONN_F_OUT_SEQ          0x0200    /* must do output seq adjust */
82 #define IP_VS_CONN_F_IN_SEQ           0x0400    /* must do input seq adjust */
83 #define IP_VS_CONN_F_SEQ_MASK         0x0600    /* in/out sequence mask */
84 #define IP_VS_CONN_F_NO_CPORT         0x0800    /* no client port set yet */
85 #define IP_VS_CONN_F_TEMPLATE         0x1000    /* template, not connection */
86 
87 /* Move it to better place one day, for now keep it unique */
88 #define NFC_IPVS_PROPERTY	0x10000
89 
90 #define IP_VS_SCHEDNAME_MAXLEN         16
91 #define IP_VS_IFNAME_MAXLEN            16
92 
93 struct ip_vs_rule_user {
94 	/* global options */
95 	int             tcp_timeout;    /* timeout values */
96 	int             tcp_fin_timeout;
97 	int             udp_timeout;
98 	int             state;          /* sync daemon state */
99 	char            mcast_ifn[IP_VS_IFNAME_MAXLEN];
100 					/* multicast interface name */
101 	int		syncid;
102 
103 	/* virtual service options */
104 	u_int16_t	protocol;
105 	u_int32_t	vaddr;          /* virtual address */
106 	u_int16_t	vport;
107 	u_int32_t       vfwmark;        /* firwall mark of virtual service*/
108 	char            sched_name[IP_VS_SCHEDNAME_MAXLEN];
109 	unsigned	vs_flags;       /* virtual service flags */
110 	unsigned        timeout;        /* persistent timeout in ticks */
111 	u_int32_t	netmask;        /* persistent netmask */
112 
113 	/* destination specific options */
114 	u_int32_t	daddr;          /* destination address */
115 	u_int16_t	dport;
116 	unsigned        conn_flags;     /* destination flags */
117 	int             weight;         /* destination weight */
118 };
119 
120 
121 /*
122  *	IPVS statistics object (for user space)
123  */
124 struct ip_vs_stats_user
125 {
126 	__u32                   conns;          /* connections scheduled */
127 	__u32                   inpkts;         /* incoming packets */
128 	__u32                   outpkts;        /* outgoing packets */
129 	__u64                   inbytes;        /* incoming bytes */
130 	__u64                   outbytes;       /* outgoing bytes */
131 
132 	__u32			cps;		/* current connection rate */
133 	__u32			inpps;		/* current in packet rate */
134 	__u32			outpps;		/* current out packet rate */
135 	__u32			inbps;		/* current in byte rate */
136 	__u32			outbps;		/* current out byte rate */
137 };
138 
139 
140 /* The argument to IP_VS_SO_GET_INFO */
141 struct ip_vs_getinfo {
142 	/* version number */
143 	unsigned int	version;
144 
145 	/* size of connection hash table */
146 	unsigned int	size;
147 
148 	/* number of virtual services */
149 	unsigned int	num_services;
150 };
151 
152 /* The argument to IP_VS_SO_GET_SERVICE */
153 struct ip_vs_service_user {
154 	/* which service: user fills this in */
155 	u_int16_t	protocol;
156 	u_int32_t	addr;           /* virtual address */
157 	u_int16_t	port;
158 	u_int32_t       fwmark;         /* firwall mark of virtual service */
159 
160 	/* service options */
161 	char            sched_name[IP_VS_SCHEDNAME_MAXLEN];
162 	unsigned	flags;          /* virtual service flags */
163 	unsigned        timeout;        /* persistent timeout in ticks */
164 	u_int32_t	netmask;        /* persistent netmask */
165 
166 	/* number of real servers */
167 	unsigned int    num_dests;
168 
169 	/* statistics */
170 	struct ip_vs_stats_user stats;
171 };
172 
173 struct ip_vs_dest_user {
174 	u_int32_t	addr;           /* destination address */
175 	u_int16_t	port;
176 	unsigned	flags;		/* destination flags */
177 	int		weight;         /* destination weight */
178 	u_int32_t	activeconns;	/* active connections */
179 	u_int32_t	inactconns;	/* inactive connections */
180 
181 	/* statistics */
182 	struct ip_vs_stats_user stats;
183 };
184 
185 /* The argument to IP_VS_SO_GET_DESTS */
186 struct ip_vs_get_dests {
187 	/* which service: user fills this in */
188 	u_int16_t	protocol;
189 	u_int32_t	addr;           /* virtual address */
190 	u_int16_t	port;
191 	u_int32_t       fwmark;         /* firwall mark of virtual service */
192 
193 	/* number of real servers */
194 	unsigned int    num_dests;
195 
196 	/* the real servers */
197 	struct ip_vs_dest_user entrytable[0];
198 };
199 
200 /* The argument to IP_VS_SO_GET_SERVICES */
201 struct ip_vs_get_services {
202 	/* number of virtual services */
203 	unsigned int num_services;
204 
205 	/* service table */
206 	struct ip_vs_service_user entrytable[0];
207 };
208 
209 /* The argument to IP_VS_SO_GET_TIMEOUTS */
210 struct ip_vs_timeout_user {
211 	int             tcp_timeout;
212 	int             tcp_fin_timeout;
213 	int             udp_timeout;
214 };
215 
216 /* The argument to IP_VS_SO_GET_DAEMON */
217 struct ip_vs_daemon_user {
218 	int	state;					/* sync daemon state */
219 	char	mcast_master_ifn[IP_VS_IFNAME_MAXLEN];	/* mcast master interface name */
220 	char	mcast_backup_ifn[IP_VS_IFNAME_MAXLEN];	/* mcast backup interface name */
221 };
222 
223 
224 #ifdef __KERNEL__
225 
226 #include <linux/config.h>
227 #include <linux/list.h>                 /* for struct list_head */
228 #include <linux/spinlock.h>             /* for struct rwlock_t */
229 #include <linux/skbuff.h>               /* for struct sk_buff */
230 #include <linux/ip.h>                   /* for struct iphdr */
231 #include <asm/atomic.h>                 /* for struct atomic_t */
232 #include <linux/netdevice.h>		/* for struct neighbour; */
233 #include <net/dst.h>			/* for struct dst_entry */
234 #include <net/route.h>			/* for ip_route_output */
235 #include <net/tcp.h>
236 #include <net/udp.h>
237 
238 
239 #ifdef CONFIG_IP_VS_DEBUG
240 extern int ip_vs_get_debug_level(void);
241 #define IP_VS_DBG(level, msg...)			\
242     do {						\
243 	    if (level <= ip_vs_get_debug_level())	\
244 		    printk(KERN_DEBUG "IPVS: " msg);	\
245     } while (0)
246 #define IP_VS_DBG_RL(msg...)				\
247     do {						\
248 	    if (net_ratelimit())			\
249 		    printk(KERN_DEBUG "IPVS: " msg);	\
250     } while (0)
251 #else	/* NO DEBUGGING at ALL */
252 #define IP_VS_DBG(level, msg...)  do {} while (0)
253 #define IP_VS_DBG_RL(msg...)  do {} while (0)
254 #endif
255 
256 #define IP_VS_BUG() BUG()
257 #define IP_VS_ERR(msg...) printk(KERN_ERR "IPVS: " msg)
258 #define IP_VS_INFO(msg...) printk(KERN_INFO "IPVS: " msg)
259 #define IP_VS_WARNING(msg...) \
260 	printk(KERN_WARNING "IPVS: " msg)
261 #define IP_VS_ERR_RL(msg...)				\
262     do {						\
263 	    if (net_ratelimit())			\
264 		    printk(KERN_ERR "IPVS: " msg);	\
265     } while (0)
266 
267 #ifdef CONFIG_IP_VS_DEBUG
268 #define EnterFunction(level)						\
269     do {								\
270 	    if (level <= ip_vs_get_debug_level())			\
271 		    printk(KERN_DEBUG "Enter: %s, %s line %i\n",	\
272 			   __FUNCTION__, __FILE__, __LINE__);		\
273     } while (0)
274 #define LeaveFunction(level)                                            \
275     do {                                                                \
276 	    if (level <= ip_vs_get_debug_level())                       \
277 			printk(KERN_DEBUG "Leave: %s, %s line %i\n",    \
278 			       __FUNCTION__, __FILE__, __LINE__);       \
279     } while (0)
280 #else
281 #define EnterFunction(level)   do {} while (0)
282 #define LeaveFunction(level)   do {} while (0)
283 #endif
284 
285 
286 /*
287  *      The port number of FTP service (in network order).
288  */
289 #define FTPPORT  __constant_htons(21)
290 #define FTPDATA  __constant_htons(20)
291 
292 
293 /*
294  *      IPVS sysctl variables under the /proc/sys/net/ipv4/vs/
295  */
296 #define NET_IPV4_VS              21
297 
298 enum {
299 	NET_IPV4_VS_DEBUG_LEVEL=1,
300 	NET_IPV4_VS_AMEMTHRESH=2,
301 	NET_IPV4_VS_AMDROPRATE=3,
302 	NET_IPV4_VS_DROP_ENTRY=4,
303 	NET_IPV4_VS_DROP_PACKET=5,
304 	NET_IPV4_VS_SECURE_TCP=6,
305 	NET_IPV4_VS_TO_ES=7,
306 	NET_IPV4_VS_TO_SS=8,
307 	NET_IPV4_VS_TO_SR=9,
308 	NET_IPV4_VS_TO_FW=10,
309 	NET_IPV4_VS_TO_TW=11,
310 	NET_IPV4_VS_TO_CL=12,
311 	NET_IPV4_VS_TO_CW=13,
312 	NET_IPV4_VS_TO_LA=14,
313 	NET_IPV4_VS_TO_LI=15,
314 	NET_IPV4_VS_TO_SA=16,
315 	NET_IPV4_VS_TO_UDP=17,
316 	NET_IPV4_VS_TO_ICMP=18,
317 	NET_IPV4_VS_LBLC_EXPIRE=19,
318 	NET_IPV4_VS_LBLCR_EXPIRE=20,
319 	NET_IPV4_VS_CACHE_BYPASS=22,
320 	NET_IPV4_VS_EXPIRE_NODEST_CONN=23,
321 	NET_IPV4_VS_SYNC_THRESHOLD=24,
322 	NET_IPV4_VS_NAT_ICMP_SEND=25,
323 	NET_IPV4_VS_EXPIRE_QUIESCENT_TEMPLATE=26,
324 	NET_IPV4_VS_LAST
325 };
326 
327 
328 /*
329  *      IPVS State Values
330  */
331 enum {
332 	IP_VS_S_NONE = 0,
333 	IP_VS_S_ESTABLISHED,
334 	IP_VS_S_SYN_SENT,
335 	IP_VS_S_SYN_RECV,
336 	IP_VS_S_FIN_WAIT,
337 	IP_VS_S_TIME_WAIT,
338 	IP_VS_S_CLOSE,
339 	IP_VS_S_CLOSE_WAIT,
340 	IP_VS_S_LAST_ACK,
341 	IP_VS_S_LISTEN,
342 	IP_VS_S_SYNACK,
343 	IP_VS_S_UDP,
344 	IP_VS_S_ICMP,
345 	IP_VS_S_LAST
346 };
347 
348 
349 struct ip_vs_timeout_table {
350 	atomic_t refcnt;
351 	int scale;
352 	int timeout[IP_VS_S_LAST+1];
353 };
354 
355 
356 /*
357  *	Transport protocol header
358  */
359 union ip_vs_tphdr {
360 	unsigned char *raw;
361 	struct udphdr *uh;
362 	struct tcphdr *th;
363 	struct icmphdr *icmph;
364 	__u16 *portp;
365 };
366 
367 
368 /*
369  *	Delta sequence info structure
370  *	Each ip_vs_conn has 2 (output AND input seq. changes).
371  *      Only used in the VS/NAT.
372  */
373 struct ip_vs_seq {
374 	__u32           init_seq;       /* Add delta from this seq */
375 	__u32           delta;          /* Delta in sequence numbers */
376 	__u32           previous_delta; /* Delta in sequence numbers
377 					   before last resized pkt */
378 };
379 
380 
381 /*
382  *	IPVS statistics object
383  */
384 struct ip_vs_stats
385 {
386 	__u32                   conns;          /* connections scheduled */
387 	__u32                   inpkts;         /* incoming packets */
388 	__u32                   outpkts;        /* outgoing packets */
389 	__u64                   inbytes;        /* incoming bytes */
390 	__u64                   outbytes;       /* outgoing bytes */
391 
392 	__u32			cps;		/* current connection rate */
393 	__u32			inpps;		/* current in packet rate */
394 	__u32			outpps;		/* current out packet rate */
395 	__u32			inbps;		/* current in byte rate */
396 	__u32			outbps;		/* current out byte rate */
397 
398 	spinlock_t              lock;           /* spin lock */
399 };
400 
401 
402 /*
403  *	IP_VS structure allocated for each dynamically scheduled connection
404  */
405 struct ip_vs_conn {
406 	struct list_head        c_list;         /* hashed list heads */
407 
408 	/* Protocol, addresses and port numbers */
409 	__u32                   caddr;          /* client address */
410 	__u32                   vaddr;          /* virtual address */
411 	__u32                   daddr;          /* destination address */
412 	__u16                   cport;
413 	__u16                   vport;
414 	__u16                   dport;
415 	__u16                   protocol;       /* Which protocol (TCP/UDP) */
416 
417 	/* counter and timer */
418 	atomic_t		refcnt;		/* reference count */
419 	struct timer_list	timer;		/* Expiration timer */
420 	volatile unsigned long	timeout;	/* timeout */
421 	struct ip_vs_timeout_table *timeout_table;
422 
423 	/* Flags and state transition */
424 	spinlock_t              lock;           /* lock for state transition */
425 	volatile __u16          flags;          /* status flags */
426 	volatile __u16          state;          /* state info */
427 
428 	/* Control members */
429 	struct ip_vs_conn       *control;       /* Master control connection */
430 	atomic_t                n_control;      /* Number of controlled ones */
431 	struct ip_vs_dest       *dest;          /* real server */
432 	atomic_t                in_pkts;        /* incoming packet counter */
433 
434 	/* packet transmitter for different forwarding methods */
435 	int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp);
436 
437 	/* Note: we can group the following members into a structure,
438 	   in order to save more space, and the following members are
439 	   only used in VS/NAT anyway */
440 	struct ip_vs_app        *app;           /* bound ip_vs_app object */
441 	void                    *app_data;      /* Application private data */
442 	struct ip_vs_seq        in_seq;         /* incoming seq. struct */
443 	struct ip_vs_seq        out_seq;        /* outgoing seq. struct */
444 };
445 
446 
447 /*
448  *	The information about the virtual service offered to the net
449  *	and the forwarding entries
450  */
451 struct ip_vs_service {
452 	struct list_head	s_list;   /* for normal service table */
453 	struct list_head	f_list;   /* for fwmark-based service table */
454 	atomic_t		refcnt;   /* reference counter */
455 	atomic_t		usecnt;   /* use counter */
456 
457 	__u16			protocol; /* which protocol (TCP/UDP) */
458 	__u32			addr;	  /* IP address for virtual service */
459 	__u16			port;	  /* port number for the service */
460 	__u32                   fwmark;   /* firewall mark of the service */
461 	unsigned		flags;	  /* service status flags */
462 	unsigned		timeout;  /* persistent timeout in ticks */
463 	__u32			netmask;  /* grouping granularity */
464 
465 	struct list_head	destinations;  /* real server d-linked list */
466 	__u32			num_dests;     /* number of servers */
467 	struct ip_vs_stats      stats;         /* statistics for the service */
468 
469 	/* for scheduling */
470 	struct ip_vs_scheduler	*scheduler;    /* bound scheduler object */
471 	rwlock_t		sched_lock;    /* lock sched_data */
472 	void			*sched_data;   /* scheduler application data */
473 };
474 
475 
476 /*
477  *	The real server destination forwarding entry
478  *	with ip address, port number, and so on.
479  */
480 struct ip_vs_dest {
481 	struct list_head	n_list;   /* for the dests in the service */
482 	struct list_head	d_list;   /* for table with all the dests */
483 
484 	__u32			addr;	  /* IP address of real server */
485 	__u16			port;	  /* port number of the service */
486 	unsigned		flags;	  /* dest status flags */
487 	atomic_t		weight;	  /* server weight */
488 	atomic_t		conn_flags;	/* flags to copy to conn */
489 	atomic_t		activeconns;	/* active connections */
490 	atomic_t		inactconns;     /* inactive connections */
491 	atomic_t		refcnt;	        /* reference counter */
492 	struct ip_vs_stats      stats;          /* statistics */
493 
494 	/* for destination cache */
495 	spinlock_t		dst_lock;	/* lock dst_cache */
496 	struct dst_entry	*dst_cache;	/* destination cache entry */
497 	u32			dst_rtos;	/* RT_TOS(tos) for dst */
498 
499 	/* for virtual service */
500 	struct ip_vs_service    *svc;     /* service that it belongs to */
501 	__u16			protocol; /* which protocol (TCP/UDP) */
502 	__u32			vaddr;	  /* IP address for virtual service */
503 	__u16			vport;	  /* port number for the service */
504 	__u32                   vfwmark;  /* firewall mark of the service */
505 };
506 
507 
508 /*
509  *	The scheduler object
510  */
511 struct ip_vs_scheduler {
512 	struct list_head        n_list;   /* d-linked list head */
513 	char			*name;    /* scheduler name */
514 	atomic_t                refcnt;   /* reference counter */
515 	struct module		*module;  /* THIS_MODULE/NULL */
516 
517 	/* scheduler initializing service */
518 	int (*init_service)(struct ip_vs_service *svc);
519 	/* scheduling service finish */
520 	int (*done_service)(struct ip_vs_service *svc);
521 	/* scheduler updating service */
522 	int (*update_service)(struct ip_vs_service *svc);
523 
524 	/* selecting a server from the given service */
525 	struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
526 				       struct iphdr *iph);
527 };
528 
529 
530 /*
531  *	The application module object
532  */
533 struct ip_vs_app
534 {
535 	struct list_head        n_list;   /* d-linked list head */
536 	char                    *name;    /* name of application module */
537 	unsigned                type;     /* type = proto<<16 | port
538 					     (host byte order)*/
539 	struct module		*module;  /* THIS_MODULE/NULL */
540 
541 	/* ip_vs_app initializer */
542 	int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
543 	/* ip_vs_app finish */
544 	int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
545 	/* output hook */
546 	int (*pkt_out)(struct ip_vs_app *,
547 		       struct ip_vs_conn *, struct sk_buff *);
548 	/* input hook */
549 	int (*pkt_in)(struct ip_vs_app *,
550 		      struct ip_vs_conn *, struct sk_buff *);
551 };
552 
553 
554 /*
555  *      IPVS core functions
556  *      (from ip_vs_core.c)
557  */
558 extern const char *ip_vs_proto_name(unsigned proto);
559 extern unsigned int check_for_ip_vs_out(struct sk_buff **skb_p,
560 					int (*okfn)(struct sk_buff *));
561 
562 
563 /*
564  *     ip_vs_conn handling functions
565  *     (from ip_vs_conn.c)
566  */
567 
568 /*
569  *     IPVS connection entry hash table
570  */
571 #ifndef CONFIG_IP_VS_TAB_BITS
572 #define CONFIG_IP_VS_TAB_BITS   12
573 #endif
574 /* make sure that IP_VS_CONN_TAB_BITS is located in [8, 20] */
575 #if CONFIG_IP_VS_TAB_BITS < 8
576 #define IP_VS_CONN_TAB_BITS	8
577 #endif
578 #if CONFIG_IP_VS_TAB_BITS > 20
579 #define IP_VS_CONN_TAB_BITS	20
580 #endif
581 #if 8 <= CONFIG_IP_VS_TAB_BITS && CONFIG_IP_VS_TAB_BITS <= 20
582 #define IP_VS_CONN_TAB_BITS	CONFIG_IP_VS_TAB_BITS
583 #endif
584 #define IP_VS_CONN_TAB_SIZE     (1 << IP_VS_CONN_TAB_BITS)
585 #define IP_VS_CONN_TAB_MASK     (IP_VS_CONN_TAB_SIZE - 1)
586 
587 #define VS_STATE_INPUT	        0
588 #define VS_STATE_OUTPUT	        4
589 #define VS_STATE_INPUT_ONLY	8
590 
591 extern struct ip_vs_timeout_table vs_timeout_table;
592 extern struct ip_vs_timeout_table vs_timeout_table_dos;
593 
594 extern struct ip_vs_conn *ip_vs_conn_in_get
595 (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port);
596 extern struct ip_vs_conn *ip_vs_ct_in_get
597 (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port);
598 extern struct ip_vs_conn *ip_vs_conn_out_get
599 (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port);
600 
601 /* put back the conn without restarting its timer */
__ip_vs_conn_put(struct ip_vs_conn * cp)602 static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
603 {
604 	atomic_dec(&cp->refcnt);
605 }
606 extern void ip_vs_conn_put(struct ip_vs_conn *cp);
607 
608 extern struct ip_vs_conn *
609 ip_vs_conn_new(int proto, __u32 caddr, __u16 cport, __u32 vaddr, __u16 vport,
610 	       __u32 daddr, __u16 dport, unsigned flags,
611 	       struct ip_vs_dest *dest);
612 extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
613 
614 extern const char * ip_vs_state_name(int state);
615 extern int ip_vs_set_state(struct ip_vs_conn *cp, int state_off,
616 			   struct iphdr *iph, void *tp);
617 extern int ip_vs_conn_listen(struct ip_vs_conn *cp);
618 extern int ip_vs_check_template(struct ip_vs_conn *ct);
619 extern void ip_vs_secure_tcp_set(int on);
620 extern void ip_vs_random_dropentry(void);
621 extern int ip_vs_conn_init(void);
622 extern void ip_vs_conn_cleanup(void);
623 
ip_vs_control_del(struct ip_vs_conn * cp)624 static inline void ip_vs_control_del(struct ip_vs_conn *cp)
625 {
626 	struct ip_vs_conn *ctl_cp = cp->control;
627 	if (!ctl_cp) {
628 		IP_VS_ERR("request control DEL for uncontrolled: "
629 			  "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
630 			  NIPQUAD(cp->caddr),ntohs(cp->cport),
631 			  NIPQUAD(cp->vaddr),ntohs(cp->vport));
632 		return;
633 	}
634 
635 	IP_VS_DBG(7, "DELeting control for: "
636 		  "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
637 		  NIPQUAD(cp->caddr),ntohs(cp->cport),
638 		  NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
639 
640 	cp->control = NULL;
641 	if (atomic_read(&ctl_cp->n_control) == 0) {
642 		IP_VS_ERR("BUG control DEL with n=0 : "
643 			  "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
644 			  NIPQUAD(cp->caddr),ntohs(cp->cport),
645 			  NIPQUAD(cp->vaddr),ntohs(cp->vport));
646 		return;
647 	}
648 	atomic_dec(&ctl_cp->n_control);
649 }
650 
651 static inline void
ip_vs_control_add(struct ip_vs_conn * cp,struct ip_vs_conn * ctl_cp)652 ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
653 {
654 	if (cp->control) {
655 		IP_VS_ERR("request control ADD for already controlled: "
656 			  "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
657 			  NIPQUAD(cp->caddr),ntohs(cp->cport),
658 			  NIPQUAD(cp->vaddr),ntohs(cp->vport));
659 		ip_vs_control_del(cp);
660 	}
661 
662 	IP_VS_DBG(7, "ADDing control for: "
663 		  "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
664 		  NIPQUAD(cp->caddr),ntohs(cp->cport),
665 		  NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
666 
667 	cp->control = ctl_cp;
668 	atomic_inc(&ctl_cp->n_control);
669 }
670 
671 
672 /*
673  *      IPVS application functions
674  *      (from ip_vs_app.c)
675  */
676 #define IP_VS_APP_MAX_PORTS  8
677 extern int register_ip_vs_app(struct ip_vs_app *mapp,
678 			      unsigned short proto, __u16 port);
679 extern int unregister_ip_vs_app(struct ip_vs_app *mapp);
680 extern struct ip_vs_app * ip_vs_bind_app(struct ip_vs_conn *cp);
681 extern int ip_vs_unbind_app(struct ip_vs_conn *cp);
682 extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
683 extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
684 extern int ip_vs_skb_replace(struct sk_buff *skb, int pri,
685 			     char *o_buf, int o_len, char *n_buf, int n_len);
686 extern int ip_vs_app_init(void);
687 extern void ip_vs_app_cleanup(void);
688 
689 
690 /*
691  *      Registering/unregistering scheduler functions
692  *      (from ip_vs_sched.c)
693  */
694 extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
695 extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
696 extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
697 				struct ip_vs_scheduler *scheduler);
698 extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
699 extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
700 extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
701 
702 
703 /*
704  *      IPVS control data and functions
705  *      (from ip_vs_ctl.c)
706  */
707 extern int sysctl_ip_vs_cache_bypass;
708 extern int sysctl_ip_vs_expire_nodest_conn;
709 extern int sysctl_ip_vs_expire_quiescent_template;
710 extern int sysctl_ip_vs_sync_threshold;
711 extern int sysctl_ip_vs_nat_icmp_send;
712 extern struct ip_vs_stats ip_vs_stats;
713 
714 extern struct ip_vs_service *ip_vs_service_get(__u32 fwmark,
715 					       __u16 protocol,
716 					       __u32 vaddr, __u16 vport);
ip_vs_service_put(struct ip_vs_service * svc)717 static inline void ip_vs_service_put(struct ip_vs_service *svc)
718 {
719 	atomic_dec(&svc->usecnt);
720 }
721 
722 extern struct ip_vs_dest *
723 ip_vs_lookup_real_service(__u16 protocol, __u32 daddr, __u16 dport);
724 extern void ip_vs_random_dropentry(void);
725 extern int ip_vs_control_init(void);
726 extern void ip_vs_control_cleanup(void);
727 
728 
729 /*
730  *      IPVS sync daemon data and function prototypes
731  *      (from ip_vs_sync.c)
732  */
733 extern volatile int ip_vs_sync_state;
734 extern char ip_vs_mcast_master_ifn[IP_VS_IFNAME_MAXLEN];
735 extern char ip_vs_mcast_backup_ifn[IP_VS_IFNAME_MAXLEN];
736 extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
737 extern int stop_sync_thread(int state);
738 extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
739 
740 
741 /*
742  *      IPVS rate estimator prototypes (from ip_vs_est.c)
743  */
744 extern int ip_vs_new_estimator(struct ip_vs_stats *stats);
745 extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
746 extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
747 
748 
749 /*
750  *	This is a simple mechanism to ignore packets when
751  *	we are loaded. Just set ip_vs_drop_rate to 'n' and
752  *	we start to drop 1/rate of the packets
753  */
754 extern int ip_vs_drop_rate;
755 extern int ip_vs_drop_counter;
756 
ip_vs_todrop(void)757 static __inline__ int ip_vs_todrop(void)
758 {
759 	if (!ip_vs_drop_rate) return 0;
760 	if (--ip_vs_drop_counter > 0) return 0;
761 	ip_vs_drop_counter = ip_vs_drop_rate;
762 	return 1;
763 }
764 
765 
766 /*
767  *      ip_vs_fwd_tag returns the forwarding tag of the connection
768  */
769 #define IP_VS_FWD_METHOD(cp)  (cp->flags & IP_VS_CONN_F_FWD_MASK)
770 
ip_vs_fwd_tag(struct ip_vs_conn * cp)771 extern __inline__ char ip_vs_fwd_tag(struct ip_vs_conn *cp)
772 {
773 	char fwd;
774 
775 	switch (IP_VS_FWD_METHOD(cp)) {
776 	case IP_VS_CONN_F_MASQ:
777 		fwd = 'M'; break;
778 	case IP_VS_CONN_F_LOCALNODE:
779 		fwd = 'L'; break;
780 	case IP_VS_CONN_F_TUNNEL:
781 		fwd = 'T'; break;
782 	case IP_VS_CONN_F_DROUTE:
783 		fwd = 'R'; break;
784 	case IP_VS_CONN_F_BYPASS:
785 		fwd = 'B'; break;
786 	default:
787 		fwd = '?'; break;
788 	}
789 	return fwd;
790 }
791 
792 
793 /*
794  *	transport layer header checking
795  */
ip_vs_header_check(struct sk_buff * skb,int proto,int ihl)796 extern inline int ip_vs_header_check(struct sk_buff *skb, int proto, int ihl)
797 {
798 	int len;
799 
800 	switch (proto) {
801 	case IPPROTO_TCP:
802 		len = ihl + sizeof(struct tcphdr);
803 		/* we don't care about TCP options */
804 		break;
805 	case IPPROTO_UDP:
806 		len = ihl + sizeof(struct udphdr);
807 		break;
808 	default:
809 		len = 0;
810 	}
811 
812 	/* guarantee protocol header available in skb data area */
813 	if (!pskb_may_pull(skb, len))
814 		return -1;
815 	else
816 		return 0;
817 }
818 
819 
820 /*
821  *      Destination cache
822  */
823 static inline void
__ip_vs_dst_set(struct ip_vs_dest * dest,u32 rtos,struct dst_entry * dst)824 __ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst)
825 {
826 	struct dst_entry *old_dst;
827 
828 	old_dst = dest->dst_cache;
829 	dest->dst_cache = dst;
830 	dest->dst_rtos = rtos;
831 	dst_release(old_dst);
832 }
833 
834 static inline void
__ip_vs_dst_reset(struct ip_vs_dest * dest)835 __ip_vs_dst_reset(struct ip_vs_dest *dest)
836 {
837 	struct dst_entry *old_dst;
838 
839 	old_dst = dest->dst_cache;
840 	dest->dst_cache = NULL;
841 	dst_release(old_dst);
842 }
843 
844 static inline struct dst_entry *
__ip_vs_dst_check(struct ip_vs_dest * dest,u32 rtos,u32 cookie)845 __ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos, u32 cookie)
846 {
847 	struct dst_entry *dst = dest->dst_cache;
848 
849 	if (!dst)
850 		return NULL;
851 	if ((dst->obsolete || rtos != dest->dst_rtos) &&
852 	    dst->ops->check(dst, cookie) == NULL) {
853 		dest->dst_cache = 0;
854 		return NULL;
855 	}
856 	dst_hold(dst);
857 	return dst;
858 }
859 
860 static inline struct rtable *
__ip_vs_get_out_rt(struct ip_vs_conn * cp,u32 rtos)861 __ip_vs_get_out_rt(struct ip_vs_conn *cp, u32 rtos)
862 {
863 	struct rtable *rt;			/* Route to the other host */
864 	struct ip_vs_dest *dest = cp->dest;
865 
866 	if (dest) {
867 		spin_lock(&dest->dst_lock);
868 		if (!(rt = (struct rtable *)
869 		      __ip_vs_dst_check(dest, rtos, 0))) {
870 			if (ip_route_output(&rt, dest->addr, 0, rtos, 0)) {
871 				spin_unlock(&dest->dst_lock);
872 				IP_VS_DBG_RL("ip_route_output error, "
873 					     "dest: %u.%u.%u.%u\n",
874 					     NIPQUAD(dest->addr));
875 				return NULL;
876 			}
877 			__ip_vs_dst_set(dest, rtos, dst_clone(&rt->u.dst));
878 			IP_VS_DBG(10, "new dst %u.%u.%u.%u, refcnt=%d, rtos=%X\n",
879 				  NIPQUAD(dest->addr),
880 				  atomic_read(&rt->u.dst.__refcnt), rtos);
881 		}
882 		spin_unlock(&dest->dst_lock);
883 	} else {
884 		if (ip_route_output(&rt, cp->daddr, 0, rtos, 0)) {
885 			IP_VS_DBG_RL("ip_route_output error, dest: "
886 				     "%u.%u.%u.%u\n", NIPQUAD(cp->daddr));
887 			return NULL;
888 		}
889 	}
890 
891 	return rt;
892 }
893 
ip_vs_check_diff(u32 old,u32 new,u16 oldsum)894 static inline u16 ip_vs_check_diff(u32 old, u32 new, u16 oldsum)
895 {
896 	u32 diff[2] = { old, new };
897 
898 	return csum_fold(csum_partial((char *) diff, sizeof(diff),
899 				      oldsum ^ 0xFFFF));
900 }
901 
ip_vs_fast_check_update(union ip_vs_tphdr * h,u32 oldip,u32 newip,u16 oldport,u16 newport,u8 protocol)902 static inline void ip_vs_fast_check_update(union ip_vs_tphdr *h,
903 	u32 oldip, u32 newip, u16 oldport, u16 newport, u8 protocol)
904 {
905 	u16 *checkp;
906 
907 	if (protocol == IPPROTO_TCP)
908 		checkp = &h->th->check;
909 	else
910 		checkp = &h->uh->check;
911 	*checkp = ip_vs_check_diff(~oldip, newip,
912 				   ip_vs_check_diff(oldport ^ 0xFFFF,
913 						    newport, *checkp));
914 	if (!*checkp && protocol == IPPROTO_UDP)
915 		*checkp = 0xFFFF;
916 }
917 
918 static inline int
ip_vs_skb_cow(struct sk_buff * skb,unsigned int headroom,struct iphdr ** iph_p,unsigned char ** t_p)919 ip_vs_skb_cow(struct sk_buff *skb, unsigned int headroom,
920 	      struct iphdr **iph_p, unsigned char **t_p)
921 {
922 	int delta = (headroom > 16 ? headroom : 16) - skb_headroom(skb);
923 
924 	if (delta < 0)
925 		delta = 0;
926 
927 	if (delta || skb_cloned(skb)) {
928 		if (pskb_expand_head(skb, (delta+15)&~15, 0, GFP_ATOMIC))
929 			return -ENOMEM;
930 
931 		/* skb data changed, update pointers */
932 		*iph_p = skb->nh.iph;
933 		*t_p = (char*) (*iph_p) + (*iph_p)->ihl * 4;
934 	}
935 	return 0;
936 }
937 
938 #endif /* __KERNEL__ */
939 
940 #endif	/* _IP_VS_H */
941