974daf52ba76872147faeb0de40a009daf730b03
[pandora-kernel.git] / include / net / ip_vs.h
1 /*
2  *      IP Virtual Server
3  *      data structure and functionality definitions
4  */
5
6 #ifndef _NET_IP_VS_H
7 #define _NET_IP_VS_H
8
9 #include <linux/ip_vs.h>                /* definitions shared with userland */
10
11 /* old ipvsadm versions still include this file directly */
12 #ifdef __KERNEL__
13
14 #include <asm/types.h>                  /* for __uXX types */
15
16 #include <linux/sysctl.h>               /* for ctl_path */
17 #include <linux/list.h>                 /* for struct list_head */
18 #include <linux/spinlock.h>             /* for struct rwlock_t */
19 #include <asm/atomic.h>                 /* for struct atomic_t */
20 #include <linux/compiler.h>
21 #include <linux/timer.h>
22
23 #include <net/checksum.h>
24 #include <linux/netfilter.h>            /* for union nf_inet_addr */
25 #include <linux/ip.h>
26 #include <linux/ipv6.h>                 /* for struct ipv6hdr */
27 #include <net/ipv6.h>                   /* for ipv6_addr_copy */
28 #ifdef CONFIG_IP_VS_NFCT
29 #include <net/netfilter/nf_conntrack.h>
30 #endif
31
32 /* Connections' size value needed by ip_vs_ctl.c */
33 extern int ip_vs_conn_tab_size;
34
35
36 struct ip_vs_iphdr {
37         int len;
38         __u8 protocol;
39         union nf_inet_addr saddr;
40         union nf_inet_addr daddr;
41 };
42
43 static inline void
44 ip_vs_fill_iphdr(int af, const void *nh, struct ip_vs_iphdr *iphdr)
45 {
46 #ifdef CONFIG_IP_VS_IPV6
47         if (af == AF_INET6) {
48                 const struct ipv6hdr *iph = nh;
49                 iphdr->len = sizeof(struct ipv6hdr);
50                 iphdr->protocol = iph->nexthdr;
51                 ipv6_addr_copy(&iphdr->saddr.in6, &iph->saddr);
52                 ipv6_addr_copy(&iphdr->daddr.in6, &iph->daddr);
53         } else
54 #endif
55         {
56                 const struct iphdr *iph = nh;
57                 iphdr->len = iph->ihl * 4;
58                 iphdr->protocol = iph->protocol;
59                 iphdr->saddr.ip = iph->saddr;
60                 iphdr->daddr.ip = iph->daddr;
61         }
62 }
63
64 static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
65                                    const union nf_inet_addr *src)
66 {
67 #ifdef CONFIG_IP_VS_IPV6
68         if (af == AF_INET6)
69                 ipv6_addr_copy(&dst->in6, &src->in6);
70         else
71 #endif
72         dst->ip = src->ip;
73 }
74
75 static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
76                                    const union nf_inet_addr *b)
77 {
78 #ifdef CONFIG_IP_VS_IPV6
79         if (af == AF_INET6)
80                 return ipv6_addr_equal(&a->in6, &b->in6);
81 #endif
82         return a->ip == b->ip;
83 }
84
85 #ifdef CONFIG_IP_VS_DEBUG
86 #include <linux/net.h>
87
88 extern int ip_vs_get_debug_level(void);
89
90 static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
91                                          const union nf_inet_addr *addr,
92                                          int *idx)
93 {
94         int len;
95 #ifdef CONFIG_IP_VS_IPV6
96         if (af == AF_INET6)
97                 len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6]",
98                                &addr->in6) + 1;
99         else
100 #endif
101                 len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
102                                &addr->ip) + 1;
103
104         *idx += len;
105         BUG_ON(*idx > buf_len + 1);
106         return &buf[*idx - len];
107 }
108
109 #define IP_VS_DBG_BUF(level, msg, ...)                                  \
110         do {                                                            \
111                 char ip_vs_dbg_buf[160];                                \
112                 int ip_vs_dbg_idx = 0;                                  \
113                 if (level <= ip_vs_get_debug_level())                   \
114                         printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__);  \
115         } while (0)
116 #define IP_VS_ERR_BUF(msg...)                                           \
117         do {                                                            \
118                 char ip_vs_dbg_buf[160];                                \
119                 int ip_vs_dbg_idx = 0;                                  \
120                 pr_err(msg);                                            \
121         } while (0)
122
123 /* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
124 #define IP_VS_DBG_ADDR(af, addr)                                        \
125         ip_vs_dbg_addr(af, ip_vs_dbg_buf,                               \
126                        sizeof(ip_vs_dbg_buf), addr,                     \
127                        &ip_vs_dbg_idx)
128
129 #define IP_VS_DBG(level, msg, ...)                                      \
130         do {                                                            \
131                 if (level <= ip_vs_get_debug_level())                   \
132                         printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__);  \
133         } while (0)
134 #define IP_VS_DBG_RL(msg, ...)                                          \
135         do {                                                            \
136                 if (net_ratelimit())                                    \
137                         printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__);  \
138         } while (0)
139 #define IP_VS_DBG_PKT(level, pp, skb, ofs, msg)                         \
140         do {                                                            \
141                 if (level <= ip_vs_get_debug_level())                   \
142                         pp->debug_packet(pp, skb, ofs, msg);            \
143         } while (0)
144 #define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg)                      \
145         do {                                                            \
146                 if (level <= ip_vs_get_debug_level() &&                 \
147                     net_ratelimit())                                    \
148                         pp->debug_packet(pp, skb, ofs, msg);            \
149         } while (0)
150 #else   /* NO DEBUGGING at ALL */
151 #define IP_VS_DBG_BUF(level, msg...)  do {} while (0)
152 #define IP_VS_ERR_BUF(msg...)  do {} while (0)
153 #define IP_VS_DBG(level, msg...)  do {} while (0)
154 #define IP_VS_DBG_RL(msg...)  do {} while (0)
155 #define IP_VS_DBG_PKT(level, pp, skb, ofs, msg)         do {} while (0)
156 #define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg)      do {} while (0)
157 #endif
158
159 #define IP_VS_BUG() BUG()
160 #define IP_VS_ERR_RL(msg, ...)                                          \
161         do {                                                            \
162                 if (net_ratelimit())                                    \
163                         pr_err(msg, ##__VA_ARGS__);                     \
164         } while (0)
165
166 #ifdef CONFIG_IP_VS_DEBUG
167 #define EnterFunction(level)                                            \
168         do {                                                            \
169                 if (level <= ip_vs_get_debug_level())                   \
170                         printk(KERN_DEBUG                               \
171                                pr_fmt("Enter: %s, %s line %i\n"),       \
172                                __func__, __FILE__, __LINE__);           \
173         } while (0)
174 #define LeaveFunction(level)                                            \
175         do {                                                            \
176                 if (level <= ip_vs_get_debug_level())                   \
177                         printk(KERN_DEBUG                               \
178                                pr_fmt("Leave: %s, %s line %i\n"),       \
179                                __func__, __FILE__, __LINE__);           \
180         } while (0)
181 #else
182 #define EnterFunction(level)   do {} while (0)
183 #define LeaveFunction(level)   do {} while (0)
184 #endif
185
186 #define IP_VS_WAIT_WHILE(expr)  while (expr) { cpu_relax(); }
187
188
189 /*
190  *      The port number of FTP service (in network order).
191  */
192 #define FTPPORT  cpu_to_be16(21)
193 #define FTPDATA  cpu_to_be16(20)
194
195 /*
196  *      TCP State Values
197  */
198 enum {
199         IP_VS_TCP_S_NONE = 0,
200         IP_VS_TCP_S_ESTABLISHED,
201         IP_VS_TCP_S_SYN_SENT,
202         IP_VS_TCP_S_SYN_RECV,
203         IP_VS_TCP_S_FIN_WAIT,
204         IP_VS_TCP_S_TIME_WAIT,
205         IP_VS_TCP_S_CLOSE,
206         IP_VS_TCP_S_CLOSE_WAIT,
207         IP_VS_TCP_S_LAST_ACK,
208         IP_VS_TCP_S_LISTEN,
209         IP_VS_TCP_S_SYNACK,
210         IP_VS_TCP_S_LAST
211 };
212
213 /*
214  *      UDP State Values
215  */
216 enum {
217         IP_VS_UDP_S_NORMAL,
218         IP_VS_UDP_S_LAST,
219 };
220
221 /*
222  *      ICMP State Values
223  */
224 enum {
225         IP_VS_ICMP_S_NORMAL,
226         IP_VS_ICMP_S_LAST,
227 };
228
229 /*
230  *      SCTP State Values
231  */
232 enum ip_vs_sctp_states {
233         IP_VS_SCTP_S_NONE,
234         IP_VS_SCTP_S_INIT_CLI,
235         IP_VS_SCTP_S_INIT_SER,
236         IP_VS_SCTP_S_INIT_ACK_CLI,
237         IP_VS_SCTP_S_INIT_ACK_SER,
238         IP_VS_SCTP_S_ECHO_CLI,
239         IP_VS_SCTP_S_ECHO_SER,
240         IP_VS_SCTP_S_ESTABLISHED,
241         IP_VS_SCTP_S_SHUT_CLI,
242         IP_VS_SCTP_S_SHUT_SER,
243         IP_VS_SCTP_S_SHUT_ACK_CLI,
244         IP_VS_SCTP_S_SHUT_ACK_SER,
245         IP_VS_SCTP_S_CLOSED,
246         IP_VS_SCTP_S_LAST
247 };
248
249 /*
250  *      Delta sequence info structure
251  *      Each ip_vs_conn has 2 (output AND input seq. changes).
252  *      Only used in the VS/NAT.
253  */
254 struct ip_vs_seq {
255         __u32                   init_seq;       /* Add delta from this seq */
256         __u32                   delta;          /* Delta in sequence numbers */
257         __u32                   previous_delta; /* Delta in sequence numbers
258                                                    before last resized pkt */
259 };
260
261
262 /*
263  *      IPVS statistics objects
264  */
265 struct ip_vs_estimator {
266         struct list_head        list;
267
268         u64                     last_inbytes;
269         u64                     last_outbytes;
270         u32                     last_conns;
271         u32                     last_inpkts;
272         u32                     last_outpkts;
273
274         u32                     cps;
275         u32                     inpps;
276         u32                     outpps;
277         u32                     inbps;
278         u32                     outbps;
279 };
280
281 struct ip_vs_stats {
282         struct ip_vs_stats_user ustats;         /* statistics */
283         struct ip_vs_estimator  est;            /* estimator */
284
285         spinlock_t              lock;           /* spin lock */
286 };
287
288 struct dst_entry;
289 struct iphdr;
290 struct ip_vs_conn;
291 struct ip_vs_app;
292 struct sk_buff;
293
294 struct ip_vs_protocol {
295         struct ip_vs_protocol   *next;
296         char                    *name;
297         u16                     protocol;
298         u16                     num_states;
299         int                     dont_defrag;
300         atomic_t                appcnt;         /* counter of proto app incs */
301         int                     *timeout_table; /* protocol timeout table */
302
303         void (*init)(struct ip_vs_protocol *pp);
304
305         void (*exit)(struct ip_vs_protocol *pp);
306
307         int (*conn_schedule)(int af, struct sk_buff *skb,
308                              struct ip_vs_protocol *pp,
309                              int *verdict, struct ip_vs_conn **cpp);
310
311         struct ip_vs_conn *
312         (*conn_in_get)(int af,
313                        const struct sk_buff *skb,
314                        struct ip_vs_protocol *pp,
315                        const struct ip_vs_iphdr *iph,
316                        unsigned int proto_off,
317                        int inverse);
318
319         struct ip_vs_conn *
320         (*conn_out_get)(int af,
321                         const struct sk_buff *skb,
322                         struct ip_vs_protocol *pp,
323                         const struct ip_vs_iphdr *iph,
324                         unsigned int proto_off,
325                         int inverse);
326
327         int (*snat_handler)(struct sk_buff *skb,
328                             struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
329
330         int (*dnat_handler)(struct sk_buff *skb,
331                             struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
332
333         int (*csum_check)(int af, struct sk_buff *skb,
334                           struct ip_vs_protocol *pp);
335
336         const char *(*state_name)(int state);
337
338         int (*state_transition)(struct ip_vs_conn *cp, int direction,
339                                 const struct sk_buff *skb,
340                                 struct ip_vs_protocol *pp);
341
342         int (*register_app)(struct ip_vs_app *inc);
343
344         void (*unregister_app)(struct ip_vs_app *inc);
345
346         int (*app_conn_bind)(struct ip_vs_conn *cp);
347
348         void (*debug_packet)(struct ip_vs_protocol *pp,
349                              const struct sk_buff *skb,
350                              int offset,
351                              const char *msg);
352
353         void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
354
355         int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
356 };
357
358 extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
359
360 struct ip_vs_conn_param {
361         const union nf_inet_addr        *caddr;
362         const union nf_inet_addr        *vaddr;
363         __be16                          cport;
364         __be16                          vport;
365         __u16                           protocol;
366         u16                             af;
367
368         const struct ip_vs_pe           *pe;
369         char                            *pe_data;
370         __u8                            pe_data_len;
371 };
372
373 /*
374  *      IP_VS structure allocated for each dynamically scheduled connection
375  */
376 struct ip_vs_conn {
377         struct list_head        c_list;         /* hashed list heads */
378
379         /* Protocol, addresses and port numbers */
380         u16                      af;            /* address family */
381         union nf_inet_addr       caddr;          /* client address */
382         union nf_inet_addr       vaddr;          /* virtual address */
383         union nf_inet_addr       daddr;          /* destination address */
384         volatile __u32           flags;          /* status flags */
385         __be16                   cport;
386         __be16                   vport;
387         __be16                   dport;
388         __u16                   protocol;       /* Which protocol (TCP/UDP) */
389
390         /* counter and timer */
391         atomic_t                refcnt;         /* reference count */
392         struct timer_list       timer;          /* Expiration timer */
393         volatile unsigned long  timeout;        /* timeout */
394
395         /* Flags and state transition */
396         spinlock_t              lock;           /* lock for state transition */
397         volatile __u16          state;          /* state info */
398         volatile __u16          old_state;      /* old state, to be used for
399                                                  * state transition triggerd
400                                                  * synchronization
401                                                  */
402
403         /* Control members */
404         struct ip_vs_conn       *control;       /* Master control connection */
405         atomic_t                n_control;      /* Number of controlled ones */
406         struct ip_vs_dest       *dest;          /* real server */
407         atomic_t                in_pkts;        /* incoming packet counter */
408
409         /* packet transmitter for different forwarding methods.  If it
410            mangles the packet, it must return NF_DROP or better NF_STOLEN,
411            otherwise this must be changed to a sk_buff **.
412          */
413         int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
414                            struct ip_vs_protocol *pp);
415
416         /* Note: we can group the following members into a structure,
417            in order to save more space, and the following members are
418            only used in VS/NAT anyway */
419         struct ip_vs_app        *app;           /* bound ip_vs_app object */
420         void                    *app_data;      /* Application private data */
421         struct ip_vs_seq        in_seq;         /* incoming seq. struct */
422         struct ip_vs_seq        out_seq;        /* outgoing seq. struct */
423
424         char                    *pe_data;
425         __u8                    pe_data_len;
426 };
427
428
429 /*
430  *      Extended internal versions of struct ip_vs_service_user and
431  *      ip_vs_dest_user for IPv6 support.
432  *
433  *      We need these to conveniently pass around service and destination
434  *      options, but unfortunately, we also need to keep the old definitions to
435  *      maintain userspace backwards compatibility for the setsockopt interface.
436  */
437 struct ip_vs_service_user_kern {
438         /* virtual service addresses */
439         u16                     af;
440         u16                     protocol;
441         union nf_inet_addr      addr;           /* virtual ip address */
442         u16                     port;
443         u32                     fwmark;         /* firwall mark of service */
444
445         /* virtual service options */
446         char                    *sched_name;
447         unsigned                flags;          /* virtual service flags */
448         unsigned                timeout;        /* persistent timeout in sec */
449         u32                     netmask;        /* persistent netmask */
450 };
451
452
453 struct ip_vs_dest_user_kern {
454         /* destination server address */
455         union nf_inet_addr      addr;
456         u16                     port;
457
458         /* real server options */
459         unsigned                conn_flags;     /* connection flags */
460         int                     weight;         /* destination weight */
461
462         /* thresholds for active connections */
463         u32                     u_threshold;    /* upper threshold */
464         u32                     l_threshold;    /* lower threshold */
465 };
466
467
468 /*
469  *      The information about the virtual service offered to the net
470  *      and the forwarding entries
471  */
472 struct ip_vs_service {
473         struct list_head        s_list;   /* for normal service table */
474         struct list_head        f_list;   /* for fwmark-based service table */
475         atomic_t                refcnt;   /* reference counter */
476         atomic_t                usecnt;   /* use counter */
477
478         u16                     af;       /* address family */
479         __u16                   protocol; /* which protocol (TCP/UDP) */
480         union nf_inet_addr      addr;     /* IP address for virtual service */
481         __be16                  port;     /* port number for the service */
482         __u32                   fwmark;   /* firewall mark of the service */
483         unsigned                flags;    /* service status flags */
484         unsigned                timeout;  /* persistent timeout in ticks */
485         __be32                  netmask;  /* grouping granularity */
486
487         struct list_head        destinations;  /* real server d-linked list */
488         __u32                   num_dests;     /* number of servers */
489         struct ip_vs_stats      stats;         /* statistics for the service */
490         struct ip_vs_app        *inc;     /* bind conns to this app inc */
491
492         /* for scheduling */
493         struct ip_vs_scheduler  *scheduler;    /* bound scheduler object */
494         rwlock_t                sched_lock;    /* lock sched_data */
495         void                    *sched_data;   /* scheduler application data */
496
497         /* alternate persistence engine */
498         struct ip_vs_pe         *pe;
499 };
500
501
502 /*
503  *      The real server destination forwarding entry
504  *      with ip address, port number, and so on.
505  */
506 struct ip_vs_dest {
507         struct list_head        n_list;   /* for the dests in the service */
508         struct list_head        d_list;   /* for table with all the dests */
509
510         u16                     af;             /* address family */
511         union nf_inet_addr      addr;           /* IP address of the server */
512         __be16                  port;           /* port number of the server */
513         volatile unsigned       flags;          /* dest status flags */
514         atomic_t                conn_flags;     /* flags to copy to conn */
515         atomic_t                weight;         /* server weight */
516
517         atomic_t                refcnt;         /* reference counter */
518         struct ip_vs_stats      stats;          /* statistics */
519
520         /* connection counters and thresholds */
521         atomic_t                activeconns;    /* active connections */
522         atomic_t                inactconns;     /* inactive connections */
523         atomic_t                persistconns;   /* persistent connections */
524         __u32                   u_threshold;    /* upper threshold */
525         __u32                   l_threshold;    /* lower threshold */
526
527         /* for destination cache */
528         spinlock_t              dst_lock;       /* lock of dst_cache */
529         struct dst_entry        *dst_cache;     /* destination cache entry */
530         u32                     dst_rtos;       /* RT_TOS(tos) for dst */
531
532         /* for virtual service */
533         struct ip_vs_service    *svc;           /* service it belongs to */
534         __u16                   protocol;       /* which protocol (TCP/UDP) */
535         union nf_inet_addr      vaddr;          /* virtual IP address */
536         __be16                  vport;          /* virtual port number */
537         __u32                   vfwmark;        /* firewall mark of service */
538 };
539
540
541 /*
542  *      The scheduler object
543  */
544 struct ip_vs_scheduler {
545         struct list_head        n_list;         /* d-linked list head */
546         char                    *name;          /* scheduler name */
547         atomic_t                refcnt;         /* reference counter */
548         struct module           *module;        /* THIS_MODULE/NULL */
549
550         /* scheduler initializing service */
551         int (*init_service)(struct ip_vs_service *svc);
552         /* scheduling service finish */
553         int (*done_service)(struct ip_vs_service *svc);
554         /* scheduler updating service */
555         int (*update_service)(struct ip_vs_service *svc);
556
557         /* selecting a server from the given service */
558         struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
559                                        const struct sk_buff *skb);
560 };
561
562 /* The persistence engine object */
563 struct ip_vs_pe {
564         struct list_head        n_list;         /* d-linked list head */
565         char                    *name;          /* scheduler name */
566         atomic_t                refcnt;         /* reference counter */
567         struct module           *module;        /* THIS_MODULE/NULL */
568
569         /* get the connection template, if any */
570         int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
571         bool (*ct_match)(const struct ip_vs_conn_param *p,
572                          struct ip_vs_conn *ct);
573         u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
574                            bool inverse);
575         int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
576 };
577
578 /*
579  *      The application module object (a.k.a. app incarnation)
580  */
581 struct ip_vs_app {
582         struct list_head        a_list;         /* member in app list */
583         int                     type;           /* IP_VS_APP_TYPE_xxx */
584         char                    *name;          /* application module name */
585         __u16                   protocol;
586         struct module           *module;        /* THIS_MODULE/NULL */
587         struct list_head        incs_list;      /* list of incarnations */
588
589         /* members for application incarnations */
590         struct list_head        p_list;         /* member in proto app list */
591         struct ip_vs_app        *app;           /* its real application */
592         __be16                  port;           /* port number in net order */
593         atomic_t                usecnt;         /* usage counter */
594
595         /* output hook: return false if can't linearize. diff set for TCP.  */
596         int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
597                        struct sk_buff *, int *diff);
598
599         /* input hook: return false if can't linearize. diff set for TCP. */
600         int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
601                       struct sk_buff *, int *diff);
602
603         /* ip_vs_app initializer */
604         int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
605
606         /* ip_vs_app finish */
607         int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
608
609
610         /* not used now */
611         int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
612                          struct ip_vs_protocol *);
613
614         void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
615
616         int *                   timeout_table;
617         int *                   timeouts;
618         int                     timeouts_size;
619
620         int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
621                              int *verdict, struct ip_vs_conn **cpp);
622
623         struct ip_vs_conn *
624         (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
625                        const struct iphdr *iph, unsigned int proto_off,
626                        int inverse);
627
628         struct ip_vs_conn *
629         (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
630                         const struct iphdr *iph, unsigned int proto_off,
631                         int inverse);
632
633         int (*state_transition)(struct ip_vs_conn *cp, int direction,
634                                 const struct sk_buff *skb,
635                                 struct ip_vs_app *app);
636
637         void (*timeout_change)(struct ip_vs_app *app, int flags);
638 };
639
640
641 /*
642  *      IPVS core functions
643  *      (from ip_vs_core.c)
644  */
645 extern const char *ip_vs_proto_name(unsigned proto);
646 extern void ip_vs_init_hash_table(struct list_head *table, int rows);
647 #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
648
649 #define IP_VS_APP_TYPE_FTP      1
650
651 /*
652  *     ip_vs_conn handling functions
653  *     (from ip_vs_conn.c)
654  */
655
656 enum {
657         IP_VS_DIR_INPUT = 0,
658         IP_VS_DIR_OUTPUT,
659         IP_VS_DIR_INPUT_ONLY,
660         IP_VS_DIR_LAST,
661 };
662
663 static inline void ip_vs_conn_fill_param(int af, int protocol,
664                                          const union nf_inet_addr *caddr,
665                                          __be16 cport,
666                                          const union nf_inet_addr *vaddr,
667                                          __be16 vport,
668                                          struct ip_vs_conn_param *p)
669 {
670         p->af = af;
671         p->protocol = protocol;
672         p->caddr = caddr;
673         p->cport = cport;
674         p->vaddr = vaddr;
675         p->vport = vport;
676         p->pe = NULL;
677         p->pe_data = NULL;
678 }
679
680 struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
681 struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
682
683 struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
684                                             struct ip_vs_protocol *pp,
685                                             const struct ip_vs_iphdr *iph,
686                                             unsigned int proto_off,
687                                             int inverse);
688
689 struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
690
691 struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
692                                              struct ip_vs_protocol *pp,
693                                              const struct ip_vs_iphdr *iph,
694                                              unsigned int proto_off,
695                                              int inverse);
696
697 /* put back the conn without restarting its timer */
698 static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
699 {
700         atomic_dec(&cp->refcnt);
701 }
702 extern void ip_vs_conn_put(struct ip_vs_conn *cp);
703 extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
704
705 struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p,
706                                   const union nf_inet_addr *daddr,
707                                   __be16 dport, unsigned flags,
708                                   struct ip_vs_dest *dest);
709 extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
710
711 extern const char * ip_vs_state_name(__u16 proto, int state);
712
713 extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
714 extern int ip_vs_check_template(struct ip_vs_conn *ct);
715 extern void ip_vs_random_dropentry(void);
716 extern int ip_vs_conn_init(void);
717 extern void ip_vs_conn_cleanup(void);
718
719 static inline void ip_vs_control_del(struct ip_vs_conn *cp)
720 {
721         struct ip_vs_conn *ctl_cp = cp->control;
722         if (!ctl_cp) {
723                 IP_VS_ERR_BUF("request control DEL for uncontrolled: "
724                               "%s:%d to %s:%d\n",
725                               IP_VS_DBG_ADDR(cp->af, &cp->caddr),
726                               ntohs(cp->cport),
727                               IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
728                               ntohs(cp->vport));
729
730                 return;
731         }
732
733         IP_VS_DBG_BUF(7, "DELeting control for: "
734                       "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
735                       IP_VS_DBG_ADDR(cp->af, &cp->caddr),
736                       ntohs(cp->cport),
737                       IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
738                       ntohs(ctl_cp->cport));
739
740         cp->control = NULL;
741         if (atomic_read(&ctl_cp->n_control) == 0) {
742                 IP_VS_ERR_BUF("BUG control DEL with n=0 : "
743                               "%s:%d to %s:%d\n",
744                               IP_VS_DBG_ADDR(cp->af, &cp->caddr),
745                               ntohs(cp->cport),
746                               IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
747                               ntohs(cp->vport));
748
749                 return;
750         }
751         atomic_dec(&ctl_cp->n_control);
752 }
753
754 static inline void
755 ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
756 {
757         if (cp->control) {
758                 IP_VS_ERR_BUF("request control ADD for already controlled: "
759                               "%s:%d to %s:%d\n",
760                               IP_VS_DBG_ADDR(cp->af, &cp->caddr),
761                               ntohs(cp->cport),
762                               IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
763                               ntohs(cp->vport));
764
765                 ip_vs_control_del(cp);
766         }
767
768         IP_VS_DBG_BUF(7, "ADDing control for: "
769                       "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
770                       IP_VS_DBG_ADDR(cp->af, &cp->caddr),
771                       ntohs(cp->cport),
772                       IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
773                       ntohs(ctl_cp->cport));
774
775         cp->control = ctl_cp;
776         atomic_inc(&ctl_cp->n_control);
777 }
778
779
780 /*
781  *      IPVS application functions
782  *      (from ip_vs_app.c)
783  */
784 #define IP_VS_APP_MAX_PORTS  8
785 extern int register_ip_vs_app(struct ip_vs_app *app);
786 extern void unregister_ip_vs_app(struct ip_vs_app *app);
787 extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
788 extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
789 extern int
790 register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
791 extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
792 extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
793
794 extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
795 extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
796 extern int ip_vs_app_init(void);
797 extern void ip_vs_app_cleanup(void);
798
799
800 /*
801  *      IPVS protocol functions (from ip_vs_proto.c)
802  */
803 extern int ip_vs_protocol_init(void);
804 extern void ip_vs_protocol_cleanup(void);
805 extern void ip_vs_protocol_timeout_change(int flags);
806 extern int *ip_vs_create_timeout_table(int *table, int size);
807 extern int
808 ip_vs_set_state_timeout(int *table, int num, const char *const *names,
809                         const char *name, int to);
810 extern void
811 ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
812                           int offset, const char *msg);
813
814 extern struct ip_vs_protocol ip_vs_protocol_tcp;
815 extern struct ip_vs_protocol ip_vs_protocol_udp;
816 extern struct ip_vs_protocol ip_vs_protocol_icmp;
817 extern struct ip_vs_protocol ip_vs_protocol_esp;
818 extern struct ip_vs_protocol ip_vs_protocol_ah;
819 extern struct ip_vs_protocol ip_vs_protocol_sctp;
820
821 /*
822  *      Registering/unregistering scheduler functions
823  *      (from ip_vs_sched.c)
824  */
825 extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
826 extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
827 extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
828                                 struct ip_vs_scheduler *scheduler);
829 extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
830 extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
831 extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
832 extern struct ip_vs_conn *
833 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb);
834 extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
835                         struct ip_vs_protocol *pp);
836
837
838 /*
839  *      IPVS control data and functions (from ip_vs_ctl.c)
840  */
841 extern int sysctl_ip_vs_cache_bypass;
842 extern int sysctl_ip_vs_expire_nodest_conn;
843 extern int sysctl_ip_vs_expire_quiescent_template;
844 extern int sysctl_ip_vs_sync_threshold[2];
845 extern int sysctl_ip_vs_nat_icmp_send;
846 extern int sysctl_ip_vs_conntrack;
847 extern int sysctl_ip_vs_snat_reroute;
848 extern struct ip_vs_stats ip_vs_stats;
849 extern const struct ctl_path net_vs_ctl_path[];
850
851 extern struct ip_vs_service *
852 ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
853                   const union nf_inet_addr *vaddr, __be16 vport);
854
855 static inline void ip_vs_service_put(struct ip_vs_service *svc)
856 {
857         atomic_dec(&svc->usecnt);
858 }
859
860 extern struct ip_vs_dest *
861 ip_vs_lookup_real_service(int af, __u16 protocol,
862                           const union nf_inet_addr *daddr, __be16 dport);
863
864 extern int ip_vs_use_count_inc(void);
865 extern void ip_vs_use_count_dec(void);
866 extern int ip_vs_control_init(void);
867 extern void ip_vs_control_cleanup(void);
868 extern struct ip_vs_dest *
869 ip_vs_find_dest(int af, const union nf_inet_addr *daddr, __be16 dport,
870                 const union nf_inet_addr *vaddr, __be16 vport, __u16 protocol);
871 extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
872
873
874 /*
875  *      IPVS sync daemon data and function prototypes
876  *      (from ip_vs_sync.c)
877  */
878 extern volatile int ip_vs_sync_state;
879 extern volatile int ip_vs_master_syncid;
880 extern volatile int ip_vs_backup_syncid;
881 extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
882 extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
883 extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
884 extern int stop_sync_thread(int state);
885 extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
886
887
888 /*
889  *      IPVS rate estimator prototypes (from ip_vs_est.c)
890  */
891 extern int ip_vs_estimator_init(void);
892 extern void ip_vs_estimator_cleanup(void);
893 extern void ip_vs_new_estimator(struct ip_vs_stats *stats);
894 extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
895 extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
896
897 /*
898  *      Various IPVS packet transmitters (from ip_vs_xmit.c)
899  */
900 extern int ip_vs_null_xmit
901 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
902 extern int ip_vs_bypass_xmit
903 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
904 extern int ip_vs_nat_xmit
905 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
906 extern int ip_vs_tunnel_xmit
907 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
908 extern int ip_vs_dr_xmit
909 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
910 extern int ip_vs_icmp_xmit
911 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
912 extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
913
914 #ifdef CONFIG_IP_VS_IPV6
915 extern int ip_vs_bypass_xmit_v6
916 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
917 extern int ip_vs_nat_xmit_v6
918 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
919 extern int ip_vs_tunnel_xmit_v6
920 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
921 extern int ip_vs_dr_xmit_v6
922 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
923 extern int ip_vs_icmp_xmit_v6
924 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
925  int offset);
926 #endif
927
928 /*
929  *      This is a simple mechanism to ignore packets when
930  *      we are loaded. Just set ip_vs_drop_rate to 'n' and
931  *      we start to drop 1/rate of the packets
932  */
933 extern int ip_vs_drop_rate;
934 extern int ip_vs_drop_counter;
935
936 static __inline__ int ip_vs_todrop(void)
937 {
938         if (!ip_vs_drop_rate) return 0;
939         if (--ip_vs_drop_counter > 0) return 0;
940         ip_vs_drop_counter = ip_vs_drop_rate;
941         return 1;
942 }
943
944 /*
945  *      ip_vs_fwd_tag returns the forwarding tag of the connection
946  */
947 #define IP_VS_FWD_METHOD(cp)  (cp->flags & IP_VS_CONN_F_FWD_MASK)
948
949 static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
950 {
951         char fwd;
952
953         switch (IP_VS_FWD_METHOD(cp)) {
954         case IP_VS_CONN_F_MASQ:
955                 fwd = 'M'; break;
956         case IP_VS_CONN_F_LOCALNODE:
957                 fwd = 'L'; break;
958         case IP_VS_CONN_F_TUNNEL:
959                 fwd = 'T'; break;
960         case IP_VS_CONN_F_DROUTE:
961                 fwd = 'R'; break;
962         case IP_VS_CONN_F_BYPASS:
963                 fwd = 'B'; break;
964         default:
965                 fwd = '?'; break;
966         }
967         return fwd;
968 }
969
970 extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
971                            struct ip_vs_conn *cp, int dir);
972
973 #ifdef CONFIG_IP_VS_IPV6
974 extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
975                               struct ip_vs_conn *cp, int dir);
976 #endif
977
978 extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
979
980 static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
981 {
982         __be32 diff[2] = { ~old, new };
983
984         return csum_partial(diff, sizeof(diff), oldsum);
985 }
986
987 #ifdef CONFIG_IP_VS_IPV6
988 static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
989                                         __wsum oldsum)
990 {
991         __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
992                             new[3],  new[2],  new[1],  new[0] };
993
994         return csum_partial(diff, sizeof(diff), oldsum);
995 }
996 #endif
997
998 static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
999 {
1000         __be16 diff[2] = { ~old, new };
1001
1002         return csum_partial(diff, sizeof(diff), oldsum);
1003 }
1004
1005 #ifdef CONFIG_IP_VS_NFCT
1006 /*
1007  *      Netfilter connection tracking
1008  *      (from ip_vs_nfct.c)
1009  */
1010 static inline int ip_vs_conntrack_enabled(void)
1011 {
1012         return sysctl_ip_vs_conntrack;
1013 }
1014
1015 extern void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
1016                                    int outin);
1017 extern int ip_vs_confirm_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp);
1018 extern void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
1019                                       struct ip_vs_conn *cp, u_int8_t proto,
1020                                       const __be16 port, int from_rs);
1021 extern void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
1022
1023 #else
1024
1025 static inline int ip_vs_conntrack_enabled(void)
1026 {
1027         return 0;
1028 }
1029
1030 static inline void ip_vs_update_conntrack(struct sk_buff *skb,
1031                                           struct ip_vs_conn *cp, int outin)
1032 {
1033 }
1034
1035 static inline int ip_vs_confirm_conntrack(struct sk_buff *skb,
1036                                           struct ip_vs_conn *cp)
1037 {
1038         return NF_ACCEPT;
1039 }
1040
1041 static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
1042 {
1043 }
1044 /* CONFIG_IP_VS_NFCT */
1045 #endif
1046
1047 #endif /* __KERNEL__ */
1048
1049 #endif  /* _NET_IP_VS_H */