3 * data structure and functionality definitions
9 #include <linux/ip_vs.h> /* definitions shared with userland */
11 /* old ipvsadm versions still include this file directly */
14 #include <asm/types.h> /* for __uXX types */
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>
23 #include <net/checksum.h>
24 #include <linux/netfilter.h> /* for union nf_inet_addr */
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>
31 #include <net/net_namespace.h> /* Netw namespace */
34 * Generic access of ipvs struct
36 static inline struct netns_ipvs *net_ipvs(struct net* net)
41 * Get net ptr from skb in traffic cases
42 * use skb_sknet when call is from userland (ioctl or netlink)
44 static inline struct net *skb_net(struct sk_buff *skb)
47 #ifdef CONFIG_IP_VS_DEBUG
49 * This is used for debug only.
50 * Start with the most likely hit
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",
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",
65 return dev_net(skb->dev ? : skb_dst(skb)->dev);
72 static inline struct net *skb_sknet(struct sk_buff *skb)
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",
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",
87 return sock_net(skb->sk);
94 /* Connections' size value needed by ip_vs_ctl.c */
95 extern int ip_vs_conn_tab_size;
101 union nf_inet_addr saddr;
102 union nf_inet_addr daddr;
106 ip_vs_fill_iphdr(int af, const void *nh, struct ip_vs_iphdr *iphdr)
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);
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;
126 static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
127 const union nf_inet_addr *src)
129 #ifdef CONFIG_IP_VS_IPV6
131 ipv6_addr_copy(&dst->in6, &src->in6);
137 static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
138 const union nf_inet_addr *b)
140 #ifdef CONFIG_IP_VS_IPV6
142 return ipv6_addr_equal(&a->in6, &b->in6);
144 return a->ip == b->ip;
147 #ifdef CONFIG_IP_VS_DEBUG
148 #include <linux/net.h>
150 extern int ip_vs_get_debug_level(void);
152 static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
153 const union nf_inet_addr *addr,
157 #ifdef CONFIG_IP_VS_IPV6
159 len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6]",
163 len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
167 BUG_ON(*idx > buf_len + 1);
168 return &buf[*idx - len];
171 #define IP_VS_DBG_BUF(level, msg, ...) \
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__); \
178 #define IP_VS_ERR_BUF(msg...) \
180 char ip_vs_dbg_buf[160]; \
181 int ip_vs_dbg_idx = 0; \
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, \
191 #define IP_VS_DBG(level, msg, ...) \
193 if (level <= ip_vs_get_debug_level()) \
194 printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
196 #define IP_VS_DBG_RL(msg, ...) \
198 if (net_ratelimit()) \
199 printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
201 #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) \
203 if (level <= ip_vs_get_debug_level()) \
204 pp->debug_packet(af, pp, skb, ofs, msg); \
206 #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) \
208 if (level <= ip_vs_get_debug_level() && \
210 pp->debug_packet(af, pp, skb, ofs, msg); \
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)
221 #define IP_VS_BUG() BUG()
222 #define IP_VS_ERR_RL(msg, ...) \
224 if (net_ratelimit()) \
225 pr_err(msg, ##__VA_ARGS__); \
228 #ifdef CONFIG_IP_VS_DEBUG
229 #define EnterFunction(level) \
231 if (level <= ip_vs_get_debug_level()) \
233 pr_fmt("Enter: %s, %s line %i\n"), \
234 __func__, __FILE__, __LINE__); \
236 #define LeaveFunction(level) \
238 if (level <= ip_vs_get_debug_level()) \
240 pr_fmt("Leave: %s, %s line %i\n"), \
241 __func__, __FILE__, __LINE__); \
244 #define EnterFunction(level) do {} while (0)
245 #define LeaveFunction(level) do {} while (0)
248 #define IP_VS_WAIT_WHILE(expr) while (expr) { cpu_relax(); }
252 * The port number of FTP service (in network order).
254 #define FTPPORT cpu_to_be16(21)
255 #define FTPDATA cpu_to_be16(20)
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,
268 IP_VS_TCP_S_CLOSE_WAIT,
269 IP_VS_TCP_S_LAST_ACK,
294 enum ip_vs_sctp_states {
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,
312 * Delta sequence info structure
313 * Each ip_vs_conn has 2 (output AND input seq. changes).
314 * Only used in the VS/NAT.
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 */
325 * IPVS statistics objects
327 struct ip_vs_estimator {
328 struct list_head list;
344 struct ip_vs_stats_user ustats; /* statistics */
345 struct ip_vs_estimator est; /* estimator */
347 spinlock_t lock; /* spin lock */
355 struct ip_vs_proto_data;
357 struct ip_vs_protocol {
358 struct ip_vs_protocol *next;
363 atomic_t appcnt; /* counter of proto app incs */
364 int *timeout_table; /* protocol timeout table */
366 void (*init)(struct ip_vs_protocol *pp);
368 void (*exit)(struct ip_vs_protocol *pp);
370 void (*init_netns)(struct net *net, struct ip_vs_proto_data *pd);
372 void (*exit_netns)(struct net *net, struct ip_vs_proto_data *pd);
374 int (*conn_schedule)(int af, struct sk_buff *skb,
375 struct ip_vs_protocol *pp,
376 int *verdict, struct ip_vs_conn **cpp);
379 (*conn_in_get)(int af,
380 const struct sk_buff *skb,
381 struct ip_vs_protocol *pp,
382 const struct ip_vs_iphdr *iph,
383 unsigned int proto_off,
387 (*conn_out_get)(int af,
388 const struct sk_buff *skb,
389 struct ip_vs_protocol *pp,
390 const struct ip_vs_iphdr *iph,
391 unsigned int proto_off,
394 int (*snat_handler)(struct sk_buff *skb,
395 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
397 int (*dnat_handler)(struct sk_buff *skb,
398 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
400 int (*csum_check)(int af, struct sk_buff *skb,
401 struct ip_vs_protocol *pp);
403 const char *(*state_name)(int state);
405 int (*state_transition)(struct ip_vs_conn *cp, int direction,
406 const struct sk_buff *skb,
407 struct ip_vs_protocol *pp);
409 int (*register_app)(struct ip_vs_app *inc);
411 void (*unregister_app)(struct ip_vs_app *inc);
413 int (*app_conn_bind)(struct ip_vs_conn *cp);
415 void (*debug_packet)(int af, struct ip_vs_protocol *pp,
416 const struct sk_buff *skb,
420 void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
422 int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
426 * protocol data per netns
428 struct ip_vs_proto_data {
429 struct ip_vs_proto_data *next;
430 struct ip_vs_protocol *pp;
431 int *timeout_table; /* protocol timeout table */
432 atomic_t appcnt; /* counter of proto app incs. */
433 struct tcp_states_t *tcp_state_table;
436 extern struct ip_vs_protocol *ip_vs_proto_get(unsigned short proto);
437 extern struct ip_vs_proto_data *ip_vs_proto_data_get(struct net *net,
438 unsigned short proto);
440 struct ip_vs_conn_param {
441 const union nf_inet_addr *caddr;
442 const union nf_inet_addr *vaddr;
448 const struct ip_vs_pe *pe;
454 * IP_VS structure allocated for each dynamically scheduled connection
457 struct list_head c_list; /* hashed list heads */
459 /* Protocol, addresses and port numbers */
460 u16 af; /* address family */
461 union nf_inet_addr caddr; /* client address */
462 union nf_inet_addr vaddr; /* virtual address */
463 union nf_inet_addr daddr; /* destination address */
464 volatile __u32 flags; /* status flags */
465 __u32 fwmark; /* Fire wall mark from skb */
469 __u16 protocol; /* Which protocol (TCP/UDP) */
471 /* counter and timer */
472 atomic_t refcnt; /* reference count */
473 struct timer_list timer; /* Expiration timer */
474 volatile unsigned long timeout; /* timeout */
476 /* Flags and state transition */
477 spinlock_t lock; /* lock for state transition */
478 volatile __u16 state; /* state info */
479 volatile __u16 old_state; /* old state, to be used for
480 * state transition triggerd
484 /* Control members */
485 struct ip_vs_conn *control; /* Master control connection */
486 atomic_t n_control; /* Number of controlled ones */
487 struct ip_vs_dest *dest; /* real server */
488 atomic_t in_pkts; /* incoming packet counter */
490 /* packet transmitter for different forwarding methods. If it
491 mangles the packet, it must return NF_DROP or better NF_STOLEN,
492 otherwise this must be changed to a sk_buff **.
493 NF_ACCEPT can be returned when destination is local.
495 int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
496 struct ip_vs_protocol *pp);
498 /* Note: we can group the following members into a structure,
499 in order to save more space, and the following members are
500 only used in VS/NAT anyway */
501 struct ip_vs_app *app; /* bound ip_vs_app object */
502 void *app_data; /* Application private data */
503 struct ip_vs_seq in_seq; /* incoming seq. struct */
504 struct ip_vs_seq out_seq; /* outgoing seq. struct */
506 const struct ip_vs_pe *pe;
513 * Extended internal versions of struct ip_vs_service_user and
514 * ip_vs_dest_user for IPv6 support.
516 * We need these to conveniently pass around service and destination
517 * options, but unfortunately, we also need to keep the old definitions to
518 * maintain userspace backwards compatibility for the setsockopt interface.
520 struct ip_vs_service_user_kern {
521 /* virtual service addresses */
524 union nf_inet_addr addr; /* virtual ip address */
526 u32 fwmark; /* firwall mark of service */
528 /* virtual service options */
531 unsigned flags; /* virtual service flags */
532 unsigned timeout; /* persistent timeout in sec */
533 u32 netmask; /* persistent netmask */
537 struct ip_vs_dest_user_kern {
538 /* destination server address */
539 union nf_inet_addr addr;
542 /* real server options */
543 unsigned conn_flags; /* connection flags */
544 int weight; /* destination weight */
546 /* thresholds for active connections */
547 u32 u_threshold; /* upper threshold */
548 u32 l_threshold; /* lower threshold */
553 * The information about the virtual service offered to the net
554 * and the forwarding entries
556 struct ip_vs_service {
557 struct list_head s_list; /* for normal service table */
558 struct list_head f_list; /* for fwmark-based service table */
559 atomic_t refcnt; /* reference counter */
560 atomic_t usecnt; /* use counter */
562 u16 af; /* address family */
563 __u16 protocol; /* which protocol (TCP/UDP) */
564 union nf_inet_addr addr; /* IP address for virtual service */
565 __be16 port; /* port number for the service */
566 __u32 fwmark; /* firewall mark of the service */
567 unsigned flags; /* service status flags */
568 unsigned timeout; /* persistent timeout in ticks */
569 __be32 netmask; /* grouping granularity */
572 struct list_head destinations; /* real server d-linked list */
573 __u32 num_dests; /* number of servers */
574 struct ip_vs_stats stats; /* statistics for the service */
575 struct ip_vs_app *inc; /* bind conns to this app inc */
578 struct ip_vs_scheduler *scheduler; /* bound scheduler object */
579 rwlock_t sched_lock; /* lock sched_data */
580 void *sched_data; /* scheduler application data */
582 /* alternate persistence engine */
588 * The real server destination forwarding entry
589 * with ip address, port number, and so on.
592 struct list_head n_list; /* for the dests in the service */
593 struct list_head d_list; /* for table with all the dests */
595 u16 af; /* address family */
596 union nf_inet_addr addr; /* IP address of the server */
597 __be16 port; /* port number of the server */
598 volatile unsigned flags; /* dest status flags */
599 atomic_t conn_flags; /* flags to copy to conn */
600 atomic_t weight; /* server weight */
602 atomic_t refcnt; /* reference counter */
603 struct ip_vs_stats stats; /* statistics */
605 /* connection counters and thresholds */
606 atomic_t activeconns; /* active connections */
607 atomic_t inactconns; /* inactive connections */
608 atomic_t persistconns; /* persistent connections */
609 __u32 u_threshold; /* upper threshold */
610 __u32 l_threshold; /* lower threshold */
612 /* for destination cache */
613 spinlock_t dst_lock; /* lock of dst_cache */
614 struct dst_entry *dst_cache; /* destination cache entry */
615 u32 dst_rtos; /* RT_TOS(tos) for dst */
617 #ifdef CONFIG_IP_VS_IPV6
618 struct in6_addr dst_saddr;
621 /* for virtual service */
622 struct ip_vs_service *svc; /* service it belongs to */
623 __u16 protocol; /* which protocol (TCP/UDP) */
624 union nf_inet_addr vaddr; /* virtual IP address */
625 __be16 vport; /* virtual port number */
626 __u32 vfwmark; /* firewall mark of service */
631 * The scheduler object
633 struct ip_vs_scheduler {
634 struct list_head n_list; /* d-linked list head */
635 char *name; /* scheduler name */
636 atomic_t refcnt; /* reference counter */
637 struct module *module; /* THIS_MODULE/NULL */
639 /* scheduler initializing service */
640 int (*init_service)(struct ip_vs_service *svc);
641 /* scheduling service finish */
642 int (*done_service)(struct ip_vs_service *svc);
643 /* scheduler updating service */
644 int (*update_service)(struct ip_vs_service *svc);
646 /* selecting a server from the given service */
647 struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
648 const struct sk_buff *skb);
651 /* The persistence engine object */
653 struct list_head n_list; /* d-linked list head */
654 char *name; /* scheduler name */
655 atomic_t refcnt; /* reference counter */
656 struct module *module; /* THIS_MODULE/NULL */
658 /* get the connection template, if any */
659 int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
660 bool (*ct_match)(const struct ip_vs_conn_param *p,
661 struct ip_vs_conn *ct);
662 u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
664 int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
668 * The application module object (a.k.a. app incarnation)
671 struct list_head a_list; /* member in app list */
672 int type; /* IP_VS_APP_TYPE_xxx */
673 char *name; /* application module name */
675 struct module *module; /* THIS_MODULE/NULL */
676 struct list_head incs_list; /* list of incarnations */
678 /* members for application incarnations */
679 struct list_head p_list; /* member in proto app list */
680 struct ip_vs_app *app; /* its real application */
681 __be16 port; /* port number in net order */
682 atomic_t usecnt; /* usage counter */
685 * output hook: Process packet in inout direction, diff set for TCP.
686 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
687 * 2=Mangled but checksum was not updated
689 int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
690 struct sk_buff *, int *diff);
693 * input hook: Process packet in outin direction, diff set for TCP.
694 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
695 * 2=Mangled but checksum was not updated
697 int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
698 struct sk_buff *, int *diff);
700 /* ip_vs_app initializer */
701 int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
703 /* ip_vs_app finish */
704 int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
708 int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
709 struct ip_vs_protocol *);
711 void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
717 int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
718 int *verdict, struct ip_vs_conn **cpp);
721 (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
722 const struct iphdr *iph, unsigned int proto_off,
726 (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
727 const struct iphdr *iph, unsigned int proto_off,
730 int (*state_transition)(struct ip_vs_conn *cp, int direction,
731 const struct sk_buff *skb,
732 struct ip_vs_app *app);
734 void (*timeout_change)(struct ip_vs_app *app, int flags);
739 * IPVS core functions
740 * (from ip_vs_core.c)
742 extern const char *ip_vs_proto_name(unsigned proto);
743 extern void ip_vs_init_hash_table(struct list_head *table, int rows);
744 #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
746 #define IP_VS_APP_TYPE_FTP 1
749 * ip_vs_conn handling functions
750 * (from ip_vs_conn.c)
756 IP_VS_DIR_INPUT_ONLY,
760 static inline void ip_vs_conn_fill_param(int af, int protocol,
761 const union nf_inet_addr *caddr,
763 const union nf_inet_addr *vaddr,
765 struct ip_vs_conn_param *p)
768 p->protocol = protocol;
777 struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
778 struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
780 struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
781 struct ip_vs_protocol *pp,
782 const struct ip_vs_iphdr *iph,
783 unsigned int proto_off,
786 struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
788 struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
789 struct ip_vs_protocol *pp,
790 const struct ip_vs_iphdr *iph,
791 unsigned int proto_off,
794 /* put back the conn without restarting its timer */
795 static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
797 atomic_dec(&cp->refcnt);
799 extern void ip_vs_conn_put(struct ip_vs_conn *cp);
800 extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
802 struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p,
803 const union nf_inet_addr *daddr,
804 __be16 dport, unsigned flags,
805 struct ip_vs_dest *dest, __u32 fwmark);
806 extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
808 extern const char * ip_vs_state_name(__u16 proto, int state);
810 extern void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp);
811 extern int ip_vs_check_template(struct ip_vs_conn *ct);
812 extern void ip_vs_random_dropentry(void);
813 extern int ip_vs_conn_init(void);
814 extern void ip_vs_conn_cleanup(void);
816 static inline void ip_vs_control_del(struct ip_vs_conn *cp)
818 struct ip_vs_conn *ctl_cp = cp->control;
820 IP_VS_ERR_BUF("request control DEL for uncontrolled: "
822 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
824 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
830 IP_VS_DBG_BUF(7, "DELeting control for: "
831 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
832 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
834 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
835 ntohs(ctl_cp->cport));
838 if (atomic_read(&ctl_cp->n_control) == 0) {
839 IP_VS_ERR_BUF("BUG control DEL with n=0 : "
841 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
843 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
848 atomic_dec(&ctl_cp->n_control);
852 ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
855 IP_VS_ERR_BUF("request control ADD for already controlled: "
857 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
859 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
862 ip_vs_control_del(cp);
865 IP_VS_DBG_BUF(7, "ADDing control for: "
866 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
867 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
869 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
870 ntohs(ctl_cp->cport));
872 cp->control = ctl_cp;
873 atomic_inc(&ctl_cp->n_control);
878 * IPVS application functions
881 #define IP_VS_APP_MAX_PORTS 8
882 extern int register_ip_vs_app(struct ip_vs_app *app);
883 extern void unregister_ip_vs_app(struct ip_vs_app *app);
884 extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
885 extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
887 register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
888 extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
889 extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
891 extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
892 extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
893 extern int ip_vs_app_init(void);
894 extern void ip_vs_app_cleanup(void);
896 void ip_vs_bind_pe(struct ip_vs_service *svc, struct ip_vs_pe *pe);
897 void ip_vs_unbind_pe(struct ip_vs_service *svc);
898 int register_ip_vs_pe(struct ip_vs_pe *pe);
899 int unregister_ip_vs_pe(struct ip_vs_pe *pe);
900 struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
901 struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
903 static inline void ip_vs_pe_get(const struct ip_vs_pe *pe)
905 if (pe && pe->module)
906 __module_get(pe->module);
909 static inline void ip_vs_pe_put(const struct ip_vs_pe *pe)
911 if (pe && pe->module)
912 module_put(pe->module);
916 * IPVS protocol functions (from ip_vs_proto.c)
918 extern int ip_vs_protocol_init(void);
919 extern void ip_vs_protocol_cleanup(void);
920 extern void ip_vs_protocol_timeout_change(int flags);
921 extern int *ip_vs_create_timeout_table(int *table, int size);
923 ip_vs_set_state_timeout(int *table, int num, const char *const *names,
924 const char *name, int to);
926 ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
927 const struct sk_buff *skb,
928 int offset, const char *msg);
930 extern struct ip_vs_protocol ip_vs_protocol_tcp;
931 extern struct ip_vs_protocol ip_vs_protocol_udp;
932 extern struct ip_vs_protocol ip_vs_protocol_icmp;
933 extern struct ip_vs_protocol ip_vs_protocol_esp;
934 extern struct ip_vs_protocol ip_vs_protocol_ah;
935 extern struct ip_vs_protocol ip_vs_protocol_sctp;
938 * Registering/unregistering scheduler functions
939 * (from ip_vs_sched.c)
941 extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
942 extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
943 extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
944 struct ip_vs_scheduler *scheduler);
945 extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
946 extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
947 extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
948 extern struct ip_vs_conn *
949 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
950 struct ip_vs_protocol *pp, int *ignored);
951 extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
952 struct ip_vs_protocol *pp);
956 * IPVS control data and functions (from ip_vs_ctl.c)
958 extern int sysctl_ip_vs_cache_bypass;
959 extern int sysctl_ip_vs_expire_nodest_conn;
960 extern int sysctl_ip_vs_expire_quiescent_template;
961 extern int sysctl_ip_vs_sync_threshold[2];
962 extern int sysctl_ip_vs_nat_icmp_send;
963 extern int sysctl_ip_vs_conntrack;
964 extern int sysctl_ip_vs_snat_reroute;
965 extern struct ip_vs_stats ip_vs_stats;
966 extern const struct ctl_path net_vs_ctl_path[];
967 extern int sysctl_ip_vs_sync_ver;
969 extern void ip_vs_sync_switch_mode(int mode);
970 extern struct ip_vs_service *
971 ip_vs_service_get(struct net *net, int af, __u32 fwmark, __u16 protocol,
972 const union nf_inet_addr *vaddr, __be16 vport);
974 static inline void ip_vs_service_put(struct ip_vs_service *svc)
976 atomic_dec(&svc->usecnt);
979 extern struct ip_vs_dest *
980 ip_vs_lookup_real_service(struct net *net, int af, __u16 protocol,
981 const union nf_inet_addr *daddr, __be16 dport);
983 extern int ip_vs_use_count_inc(void);
984 extern void ip_vs_use_count_dec(void);
985 extern int ip_vs_control_init(void);
986 extern void ip_vs_control_cleanup(void);
987 extern struct ip_vs_dest *
988 ip_vs_find_dest(struct net *net, int af, const union nf_inet_addr *daddr,
989 __be16 dport, const union nf_inet_addr *vaddr, __be16 vport,
990 __u16 protocol, __u32 fwmark);
991 extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
995 * IPVS sync daemon data and function prototypes
996 * (from ip_vs_sync.c)
998 extern volatile int ip_vs_sync_state;
999 extern volatile int ip_vs_master_syncid;
1000 extern volatile int ip_vs_backup_syncid;
1001 extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
1002 extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
1003 extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
1004 extern int stop_sync_thread(int state);
1005 extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
1006 extern int ip_vs_sync_init(void);
1007 extern void ip_vs_sync_cleanup(void);
1011 * IPVS rate estimator prototypes (from ip_vs_est.c)
1013 extern int ip_vs_estimator_init(void);
1014 extern void ip_vs_estimator_cleanup(void);
1015 extern void ip_vs_new_estimator(struct ip_vs_stats *stats);
1016 extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
1017 extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
1020 * Various IPVS packet transmitters (from ip_vs_xmit.c)
1022 extern int ip_vs_null_xmit
1023 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1024 extern int ip_vs_bypass_xmit
1025 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1026 extern int ip_vs_nat_xmit
1027 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1028 extern int ip_vs_tunnel_xmit
1029 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1030 extern int ip_vs_dr_xmit
1031 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1032 extern int ip_vs_icmp_xmit
1033 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
1034 extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
1036 #ifdef CONFIG_IP_VS_IPV6
1037 extern int ip_vs_bypass_xmit_v6
1038 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1039 extern int ip_vs_nat_xmit_v6
1040 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1041 extern int ip_vs_tunnel_xmit_v6
1042 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1043 extern int ip_vs_dr_xmit_v6
1044 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1045 extern int ip_vs_icmp_xmit_v6
1046 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
1051 * This is a simple mechanism to ignore packets when
1052 * we are loaded. Just set ip_vs_drop_rate to 'n' and
1053 * we start to drop 1/rate of the packets
1055 extern int ip_vs_drop_rate;
1056 extern int ip_vs_drop_counter;
1058 static __inline__ int ip_vs_todrop(void)
1060 if (!ip_vs_drop_rate) return 0;
1061 if (--ip_vs_drop_counter > 0) return 0;
1062 ip_vs_drop_counter = ip_vs_drop_rate;
1067 * ip_vs_fwd_tag returns the forwarding tag of the connection
1069 #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
1071 static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
1075 switch (IP_VS_FWD_METHOD(cp)) {
1076 case IP_VS_CONN_F_MASQ:
1078 case IP_VS_CONN_F_LOCALNODE:
1080 case IP_VS_CONN_F_TUNNEL:
1082 case IP_VS_CONN_F_DROUTE:
1084 case IP_VS_CONN_F_BYPASS:
1092 extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
1093 struct ip_vs_conn *cp, int dir);
1095 #ifdef CONFIG_IP_VS_IPV6
1096 extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
1097 struct ip_vs_conn *cp, int dir);
1100 extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
1102 static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
1104 __be32 diff[2] = { ~old, new };
1106 return csum_partial(diff, sizeof(diff), oldsum);
1109 #ifdef CONFIG_IP_VS_IPV6
1110 static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
1113 __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
1114 new[3], new[2], new[1], new[0] };
1116 return csum_partial(diff, sizeof(diff), oldsum);
1120 static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
1122 __be16 diff[2] = { ~old, new };
1124 return csum_partial(diff, sizeof(diff), oldsum);
1128 * Forget current conntrack (unconfirmed) and attach notrack entry
1130 static inline void ip_vs_notrack(struct sk_buff *skb)
1132 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1133 enum ip_conntrack_info ctinfo;
1134 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1136 if (!ct || !nf_ct_is_untracked(ct)) {
1138 skb->nfct = &nf_ct_untracked_get()->ct_general;
1139 skb->nfctinfo = IP_CT_NEW;
1140 nf_conntrack_get(skb->nfct);
1145 #ifdef CONFIG_IP_VS_NFCT
1147 * Netfilter connection tracking
1148 * (from ip_vs_nfct.c)
1150 static inline int ip_vs_conntrack_enabled(void)
1152 return sysctl_ip_vs_conntrack;
1155 extern void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
1157 extern int ip_vs_confirm_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp);
1158 extern void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
1159 struct ip_vs_conn *cp, u_int8_t proto,
1160 const __be16 port, int from_rs);
1161 extern void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
1165 static inline int ip_vs_conntrack_enabled(void)
1170 static inline void ip_vs_update_conntrack(struct sk_buff *skb,
1171 struct ip_vs_conn *cp, int outin)
1175 static inline int ip_vs_confirm_conntrack(struct sk_buff *skb,
1176 struct ip_vs_conn *cp)
1181 static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
1184 /* CONFIG_IP_VS_NFCT */
1187 #endif /* __KERNEL__ */
1189 #endif /* _NET_IP_VS_H */