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