blob: 0e4618470cee66e5308c4fc7e0facd8c3341df5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP Virtual Server
3 * data structure and functionality definitions
4 */
5
Julius Volzbc4768e2008-07-31 20:45:24 -07006#ifndef _NET_IP_VS_H
7#define _NET_IP_VS_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Julius Volzbc4768e2008-07-31 20:45:24 -07009#include <linux/ip_vs.h> /* definitions shared with userland */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Julius Volzbc4768e2008-07-31 20:45:24 -070011/* old ipvsadm versions still include this file directly */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#ifdef __KERNEL__
13
Julius Volzbc4768e2008-07-31 20:45:24 -070014#include <asm/types.h> /* for __uXX types */
15
16#include <linux/sysctl.h> /* for ctl_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/list.h> /* for struct list_head */
18#include <linux/spinlock.h> /* for struct rwlock_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/atomic.h> /* for struct atomic_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/compiler.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020021#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020023#include <net/checksum.h>
Julius Volze7ade462008-09-02 15:55:33 +020024#include <linux/netfilter.h> /* for union nf_inet_addr */
KOVACS Krisztian1668e012008-10-01 07:33:10 -070025#include <linux/ip.h>
Julius Volze7ade462008-09-02 15:55:33 +020026#include <linux/ipv6.h> /* for struct ipv6hdr */
27#include <net/ipv6.h> /* for ipv6_addr_copy */
Julian Anastasovcf356d62010-10-17 16:21:07 +030028#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Julian Anastasovf4bc17c2010-09-21 17:35:41 +020029#include <net/netfilter/nf_conntrack.h>
30#endif
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +010031
32/* Connections' size value needed by ip_vs_ctl.c */
33extern int ip_vs_conn_tab_size;
34
35
Julius Volz64aae3c2008-09-02 15:55:34 +020036struct ip_vs_iphdr {
37 int len;
38 __u8 protocol;
39 union nf_inet_addr saddr;
40 union nf_inet_addr daddr;
41};
42
43static inline void
44ip_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
64static 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
75static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#ifdef CONFIG_IP_VS_DEBUG
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020086#include <linux/net.h>
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088extern int ip_vs_get_debug_level(void);
Julius Volzc842a3a2008-09-02 15:55:35 +020089
90static 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)
Harvey Harrison5b095d9892008-10-29 12:52:50 -070097 len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6]",
Harvey Harrison0c6ce782008-10-28 16:09:23 -070098 &addr->in6) + 1;
Julius Volzc842a3a2008-09-02 15:55:35 +020099 else
100#endif
Harvey Harrison3685f25d2008-10-31 00:56:49 -0700101 len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
102 &addr->ip) + 1;
Julius Volzc842a3a2008-09-02 15:55:35 +0200103
104 *idx += len;
105 BUG_ON(*idx > buf_len + 1);
106 return &buf[*idx - len];
107}
108
Hannes Eder9aada7a2009-07-30 14:29:44 -0700109#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)
Julius Volzc842a3a2008-09-02 15:55:35 +0200122
123/* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
Hannes Eder9aada7a2009-07-30 14:29:44 -0700124#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)
Julius Volzc842a3a2008-09-02 15:55:35 +0200128
Hannes Eder9aada7a2009-07-30 14:29:44 -0700129#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#else /* NO DEBUGGING at ALL */
Julius Volzc842a3a2008-09-02 15:55:35 +0200151#define IP_VS_DBG_BUF(level, msg...) do {} while (0)
152#define IP_VS_ERR_BUF(msg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#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()
Hannes Eder1e3e2382009-08-02 11:05:41 +0000160#define IP_VS_ERR_RL(msg, ...) \
Hannes Eder9aada7a2009-07-30 14:29:44 -0700161 do { \
162 if (net_ratelimit()) \
Hannes Eder1e3e2382009-08-02 11:05:41 +0000163 pr_err(msg, ##__VA_ARGS__); \
Hannes Eder9aada7a2009-07-30 14:29:44 -0700164 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166#ifdef CONFIG_IP_VS_DEBUG
167#define EnterFunction(level) \
Hannes Eder9aada7a2009-07-30 14:29:44 -0700168 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#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 */
Harvey Harrisonf3a7c662009-02-14 22:58:35 -0800192#define FTPPORT cpu_to_be16(21)
193#define FTPDATA cpu_to_be16(20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * TCP State Values
197 */
198enum {
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 */
216enum {
217 IP_VS_UDP_S_NORMAL,
218 IP_VS_UDP_S_LAST,
219};
220
221/*
222 * ICMP State Values
223 */
224enum {
225 IP_VS_ICMP_S_NORMAL,
226 IP_VS_ICMP_S_LAST,
227};
228
229/*
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100230 * SCTP State Values
231 */
232enum 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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 * Delta sequence info structure
251 * Each ip_vs_conn has 2 (output AND input seq. changes).
252 * Only used in the VS/NAT.
253 */
254struct 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/*
Sven Wegener3a14a3132008-08-10 18:24:41 +0000263 * IPVS statistics objects
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Sven Wegener3a14a3132008-08-10 18:24:41 +0000265struct 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
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000281struct ip_vs_stats {
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200282 struct ip_vs_stats_user ustats; /* statistics */
283 struct ip_vs_estimator est; /* estimator */
Sven Wegener3a14a3132008-08-10 18:24:41 +0000284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 spinlock_t lock; /* spin lock */
286};
287
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200288struct dst_entry;
289struct iphdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290struct ip_vs_conn;
291struct ip_vs_app;
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200292struct sk_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294struct ip_vs_protocol {
295 struct ip_vs_protocol *next;
296 char *name;
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700297 u16 protocol;
298 u16 num_states;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 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
Julius Volz51ef3482008-09-02 15:55:40 +0200307 int (*conn_schedule)(int af, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct ip_vs_protocol *pp,
309 int *verdict, struct ip_vs_conn **cpp);
310
311 struct ip_vs_conn *
Julius Volz51ef3482008-09-02 15:55:40 +0200312 (*conn_in_get)(int af,
313 const struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct ip_vs_protocol *pp,
Julius Volz51ef3482008-09-02 15:55:40 +0200315 const struct ip_vs_iphdr *iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unsigned int proto_off,
317 int inverse);
318
319 struct ip_vs_conn *
Julius Volz51ef3482008-09-02 15:55:40 +0200320 (*conn_out_get)(int af,
321 const struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct ip_vs_protocol *pp,
Julius Volz51ef3482008-09-02 15:55:40 +0200323 const struct ip_vs_iphdr *iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned int proto_off,
325 int inverse);
326
Herbert Xu3db05fe2007-10-15 00:53:15 -0700327 int (*snat_handler)(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
329
Herbert Xu3db05fe2007-10-15 00:53:15 -0700330 int (*dnat_handler)(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
332
Julius Volz51ef3482008-09-02 15:55:40 +0200333 int (*csum_check)(int af, struct sk_buff *skb,
334 struct ip_vs_protocol *pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
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
358extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
359
Simon Hormanf11017e2010-08-22 21:37:52 +0900360struct 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;
Simon Horman85999282010-08-22 21:37:53 +0900367
368 const struct ip_vs_pe *pe;
369 char *pe_data;
370 __u8 pe_data_len;
Simon Hormanf11017e2010-08-22 21:37:52 +0900371};
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/*
374 * IP_VS structure allocated for each dynamically scheduled connection
375 */
376struct ip_vs_conn {
377 struct list_head c_list; /* hashed list heads */
378
379 /* Protocol, addresses and port numbers */
Julius Volze7ade462008-09-02 15:55:33 +0200380 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 */
Julian Anastasov35757922010-09-17 14:18:16 +0200384 volatile __u32 flags; /* status flags */
Al Viro014d7302006-09-28 14:29:52 -0700385 __be16 cport;
386 __be16 vport;
387 __be16 dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 __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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 volatile __u16 state; /* state info */
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -0800398 volatile __u16 old_state; /* old state, to be used for
399 * state transition triggerd
400 * synchronization
401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
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 */
Simon Horman85999282010-08-22 21:37:53 +0900423
424 char *pe_data;
425 __u8 pe_data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426};
427
428
429/*
Julius Volzc860c6b2008-09-02 15:55:36 +0200430 * 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 */
437struct 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;
Simon Horman0d1e71b2010-08-22 21:37:54 +0900447 char *pe_name;
Julius Volzc860c6b2008-09-02 15:55:36 +0200448 unsigned flags; /* virtual service flags */
449 unsigned timeout; /* persistent timeout in sec */
450 u32 netmask; /* persistent netmask */
451};
452
453
454struct ip_vs_dest_user_kern {
455 /* destination server address */
456 union nf_inet_addr addr;
457 u16 port;
458
459 /* real server options */
460 unsigned conn_flags; /* connection flags */
461 int weight; /* destination weight */
462
463 /* thresholds for active connections */
464 u32 u_threshold; /* upper threshold */
465 u32 l_threshold; /* lower threshold */
466};
467
468
469/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * The information about the virtual service offered to the net
471 * and the forwarding entries
472 */
473struct ip_vs_service {
474 struct list_head s_list; /* for normal service table */
475 struct list_head f_list; /* for fwmark-based service table */
476 atomic_t refcnt; /* reference counter */
477 atomic_t usecnt; /* use counter */
478
Julius Volze7ade462008-09-02 15:55:33 +0200479 u16 af; /* address family */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 __u16 protocol; /* which protocol (TCP/UDP) */
Julius Volze7ade462008-09-02 15:55:33 +0200481 union nf_inet_addr addr; /* IP address for virtual service */
Al Viro014d7302006-09-28 14:29:52 -0700482 __be16 port; /* port number for the service */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 __u32 fwmark; /* firewall mark of the service */
484 unsigned flags; /* service status flags */
485 unsigned timeout; /* persistent timeout in ticks */
Al Viro014d7302006-09-28 14:29:52 -0700486 __be32 netmask; /* grouping granularity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 struct list_head destinations; /* real server d-linked list */
489 __u32 num_dests; /* number of servers */
490 struct ip_vs_stats stats; /* statistics for the service */
491 struct ip_vs_app *inc; /* bind conns to this app inc */
492
493 /* for scheduling */
494 struct ip_vs_scheduler *scheduler; /* bound scheduler object */
495 rwlock_t sched_lock; /* lock sched_data */
496 void *sched_data; /* scheduler application data */
Simon Horman85999282010-08-22 21:37:53 +0900497
498 /* alternate persistence engine */
499 struct ip_vs_pe *pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500};
501
502
503/*
504 * The real server destination forwarding entry
505 * with ip address, port number, and so on.
506 */
507struct ip_vs_dest {
508 struct list_head n_list; /* for the dests in the service */
509 struct list_head d_list; /* for table with all the dests */
510
Julius Volze7ade462008-09-02 15:55:33 +0200511 u16 af; /* address family */
512 union nf_inet_addr addr; /* IP address of the server */
Al Viro014d7302006-09-28 14:29:52 -0700513 __be16 port; /* port number of the server */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 volatile unsigned flags; /* dest status flags */
515 atomic_t conn_flags; /* flags to copy to conn */
516 atomic_t weight; /* server weight */
517
518 atomic_t refcnt; /* reference counter */
519 struct ip_vs_stats stats; /* statistics */
520
521 /* connection counters and thresholds */
522 atomic_t activeconns; /* active connections */
523 atomic_t inactconns; /* inactive connections */
524 atomic_t persistconns; /* persistent connections */
525 __u32 u_threshold; /* upper threshold */
526 __u32 l_threshold; /* lower threshold */
527
528 /* for destination cache */
529 spinlock_t dst_lock; /* lock of dst_cache */
530 struct dst_entry *dst_cache; /* destination cache entry */
531 u32 dst_rtos; /* RT_TOS(tos) for dst */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200532 u32 dst_cookie;
533#ifdef CONFIG_IP_VS_IPV6
534 struct in6_addr dst_saddr;
535#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* for virtual service */
538 struct ip_vs_service *svc; /* service it belongs to */
539 __u16 protocol; /* which protocol (TCP/UDP) */
Julius Volze7ade462008-09-02 15:55:33 +0200540 union nf_inet_addr vaddr; /* virtual IP address */
Al Viro014d7302006-09-28 14:29:52 -0700541 __be16 vport; /* virtual port number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 __u32 vfwmark; /* firewall mark of service */
543};
544
545
546/*
547 * The scheduler object
548 */
549struct ip_vs_scheduler {
550 struct list_head n_list; /* d-linked list head */
551 char *name; /* scheduler name */
552 atomic_t refcnt; /* reference counter */
553 struct module *module; /* THIS_MODULE/NULL */
554
555 /* scheduler initializing service */
556 int (*init_service)(struct ip_vs_service *svc);
557 /* scheduling service finish */
558 int (*done_service)(struct ip_vs_service *svc);
559 /* scheduler updating service */
560 int (*update_service)(struct ip_vs_service *svc);
561
562 /* selecting a server from the given service */
563 struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
564 const struct sk_buff *skb);
565};
566
Simon Horman85999282010-08-22 21:37:53 +0900567/* The persistence engine object */
568struct ip_vs_pe {
569 struct list_head n_list; /* d-linked list head */
570 char *name; /* scheduler name */
571 atomic_t refcnt; /* reference counter */
572 struct module *module; /* THIS_MODULE/NULL */
573
574 /* get the connection template, if any */
575 int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
576 bool (*ct_match)(const struct ip_vs_conn_param *p,
577 struct ip_vs_conn *ct);
578 u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
579 bool inverse);
Simon Hormana3c918a2010-08-22 21:37:53 +0900580 int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
Simon Horman85999282010-08-22 21:37:53 +0900581};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583/*
584 * The application module object (a.k.a. app incarnation)
585 */
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000586struct ip_vs_app {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct list_head a_list; /* member in app list */
588 int type; /* IP_VS_APP_TYPE_xxx */
589 char *name; /* application module name */
590 __u16 protocol;
591 struct module *module; /* THIS_MODULE/NULL */
592 struct list_head incs_list; /* list of incarnations */
593
594 /* members for application incarnations */
595 struct list_head p_list; /* member in proto app list */
596 struct ip_vs_app *app; /* its real application */
Al Viro014d7302006-09-28 14:29:52 -0700597 __be16 port; /* port number in net order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 atomic_t usecnt; /* usage counter */
599
Julian Anastasov8b27b102010-10-17 16:17:20 +0300600 /*
601 * output hook: Process packet in inout direction, diff set for TCP.
602 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
603 * 2=Mangled but checksum was not updated
604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700606 struct sk_buff *, int *diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Julian Anastasov8b27b102010-10-17 16:17:20 +0300608 /*
609 * input hook: Process packet in outin direction, diff set for TCP.
610 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
611 * 2=Mangled but checksum was not updated
612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700614 struct sk_buff *, int *diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* ip_vs_app initializer */
617 int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
618
619 /* ip_vs_app finish */
620 int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
621
622
623 /* not used now */
624 int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
625 struct ip_vs_protocol *);
626
627 void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
628
629 int * timeout_table;
630 int * timeouts;
631 int timeouts_size;
632
633 int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
634 int *verdict, struct ip_vs_conn **cpp);
635
636 struct ip_vs_conn *
637 (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
638 const struct iphdr *iph, unsigned int proto_off,
639 int inverse);
640
641 struct ip_vs_conn *
642 (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
643 const struct iphdr *iph, unsigned int proto_off,
644 int inverse);
645
646 int (*state_transition)(struct ip_vs_conn *cp, int direction,
647 const struct sk_buff *skb,
648 struct ip_vs_app *app);
649
650 void (*timeout_change)(struct ip_vs_app *app, int flags);
651};
652
653
654/*
655 * IPVS core functions
656 * (from ip_vs_core.c)
657 */
658extern const char *ip_vs_proto_name(unsigned proto);
659extern void ip_vs_init_hash_table(struct list_head *table, int rows);
Sven Wegenerafdd6142008-08-10 09:18:01 +0000660#define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662#define IP_VS_APP_TYPE_FTP 1
663
664/*
665 * ip_vs_conn handling functions
666 * (from ip_vs_conn.c)
667 */
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669enum {
670 IP_VS_DIR_INPUT = 0,
671 IP_VS_DIR_OUTPUT,
672 IP_VS_DIR_INPUT_ONLY,
673 IP_VS_DIR_LAST,
674};
675
Simon Hormanf11017e2010-08-22 21:37:52 +0900676static inline void ip_vs_conn_fill_param(int af, int protocol,
677 const union nf_inet_addr *caddr,
678 __be16 cport,
679 const union nf_inet_addr *vaddr,
680 __be16 vport,
681 struct ip_vs_conn_param *p)
682{
683 p->af = af;
684 p->protocol = protocol;
685 p->caddr = caddr;
686 p->cport = cport;
687 p->vaddr = vaddr;
688 p->vport = vport;
Simon Horman85999282010-08-22 21:37:53 +0900689 p->pe = NULL;
690 p->pe_data = NULL;
Simon Hormanf11017e2010-08-22 21:37:52 +0900691}
Julius Volz28364a52008-09-02 15:55:43 +0200692
Simon Hormanf11017e2010-08-22 21:37:52 +0900693struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
694struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
Julius Volz28364a52008-09-02 15:55:43 +0200695
Simon Horman5c0d2372010-08-02 17:12:44 +0200696struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
697 struct ip_vs_protocol *pp,
698 const struct ip_vs_iphdr *iph,
699 unsigned int proto_off,
700 int inverse);
701
Simon Hormanf11017e2010-08-22 21:37:52 +0900702struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Simon Horman5c0d2372010-08-02 17:12:44 +0200704struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
705 struct ip_vs_protocol *pp,
706 const struct ip_vs_iphdr *iph,
707 unsigned int proto_off,
708 int inverse);
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710/* put back the conn without restarting its timer */
711static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
712{
713 atomic_dec(&cp->refcnt);
714}
715extern void ip_vs_conn_put(struct ip_vs_conn *cp);
Al Viro014d7302006-09-28 14:29:52 -0700716extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Simon Hormanf11017e2010-08-22 21:37:52 +0900718struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p,
719 const union nf_inet_addr *daddr,
720 __be16 dport, unsigned flags,
721 struct ip_vs_dest *dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
723
724extern const char * ip_vs_state_name(__u16 proto, int state);
725
726extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
727extern int ip_vs_check_template(struct ip_vs_conn *ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728extern void ip_vs_random_dropentry(void);
729extern int ip_vs_conn_init(void);
730extern void ip_vs_conn_cleanup(void);
731
732static inline void ip_vs_control_del(struct ip_vs_conn *cp)
733{
734 struct ip_vs_conn *ctl_cp = cp->control;
735 if (!ctl_cp) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200736 IP_VS_ERR_BUF("request control DEL for uncontrolled: "
737 "%s:%d to %s:%d\n",
738 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
739 ntohs(cp->cport),
740 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
741 ntohs(cp->vport));
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return;
744 }
745
Julius Volzcfc78c52008-09-02 15:55:53 +0200746 IP_VS_DBG_BUF(7, "DELeting control for: "
747 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
748 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
749 ntohs(cp->cport),
750 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
751 ntohs(ctl_cp->cport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 cp->control = NULL;
754 if (atomic_read(&ctl_cp->n_control) == 0) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200755 IP_VS_ERR_BUF("BUG control DEL with n=0 : "
756 "%s:%d to %s:%d\n",
757 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
758 ntohs(cp->cport),
759 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
760 ntohs(cp->vport));
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return;
763 }
764 atomic_dec(&ctl_cp->n_control);
765}
766
767static inline void
768ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
769{
770 if (cp->control) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200771 IP_VS_ERR_BUF("request control ADD for already controlled: "
772 "%s:%d to %s:%d\n",
773 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
774 ntohs(cp->cport),
775 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
776 ntohs(cp->vport));
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 ip_vs_control_del(cp);
779 }
780
Julius Volzcfc78c52008-09-02 15:55:53 +0200781 IP_VS_DBG_BUF(7, "ADDing control for: "
782 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
783 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
784 ntohs(cp->cport),
785 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
786 ntohs(ctl_cp->cport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 cp->control = ctl_cp;
789 atomic_inc(&ctl_cp->n_control);
790}
791
792
793/*
794 * IPVS application functions
795 * (from ip_vs_app.c)
796 */
797#define IP_VS_APP_MAX_PORTS 8
798extern int register_ip_vs_app(struct ip_vs_app *app);
799extern void unregister_ip_vs_app(struct ip_vs_app *app);
800extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
801extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
802extern int
803register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
804extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
805extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
806
Herbert Xu3db05fe2007-10-15 00:53:15 -0700807extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
808extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809extern int ip_vs_app_init(void);
810extern void ip_vs_app_cleanup(void);
811
Simon Horman8be67a62010-08-22 21:37:54 +0900812void ip_vs_bind_pe(struct ip_vs_service *svc, struct ip_vs_pe *pe);
813void ip_vs_unbind_pe(struct ip_vs_service *svc);
814int register_ip_vs_pe(struct ip_vs_pe *pe);
815int unregister_ip_vs_pe(struct ip_vs_pe *pe);
816extern struct ip_vs_pe *ip_vs_pe_get(const char *name);
817extern void ip_vs_pe_put(struct ip_vs_pe *pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819/*
820 * IPVS protocol functions (from ip_vs_proto.c)
821 */
822extern int ip_vs_protocol_init(void);
823extern void ip_vs_protocol_cleanup(void);
824extern void ip_vs_protocol_timeout_change(int flags);
825extern int *ip_vs_create_timeout_table(int *table, int size);
826extern int
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700827ip_vs_set_state_timeout(int *table, int num, const char *const *names,
828 const char *name, int to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829extern void
830ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
831 int offset, const char *msg);
832
833extern struct ip_vs_protocol ip_vs_protocol_tcp;
834extern struct ip_vs_protocol ip_vs_protocol_udp;
835extern struct ip_vs_protocol ip_vs_protocol_icmp;
836extern struct ip_vs_protocol ip_vs_protocol_esp;
837extern struct ip_vs_protocol ip_vs_protocol_ah;
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100838extern struct ip_vs_protocol ip_vs_protocol_sctp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840/*
841 * Registering/unregistering scheduler functions
842 * (from ip_vs_sched.c)
843 */
844extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
845extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
846extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
847 struct ip_vs_scheduler *scheduler);
848extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
849extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
850extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
851extern struct ip_vs_conn *
Simon Horman85999282010-08-22 21:37:53 +0900852ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
854 struct ip_vs_protocol *pp);
855
856
857/*
858 * IPVS control data and functions (from ip_vs_ctl.c)
859 */
860extern int sysctl_ip_vs_cache_bypass;
861extern int sysctl_ip_vs_expire_nodest_conn;
862extern int sysctl_ip_vs_expire_quiescent_template;
863extern int sysctl_ip_vs_sync_threshold[2];
864extern int sysctl_ip_vs_nat_icmp_send;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200865extern int sysctl_ip_vs_conntrack;
Julian Anastasov8a803042010-09-21 17:38:57 +0200866extern int sysctl_ip_vs_snat_reroute;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867extern struct ip_vs_stats ip_vs_stats;
Sven Wegener5587da52008-08-10 18:24:40 +0000868extern const struct ctl_path net_vs_ctl_path[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870extern struct ip_vs_service *
Julius Volz3c2e0502008-09-02 15:55:38 +0200871ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
872 const union nf_inet_addr *vaddr, __be16 vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874static inline void ip_vs_service_put(struct ip_vs_service *svc)
875{
876 atomic_dec(&svc->usecnt);
877}
878
879extern struct ip_vs_dest *
Julius Volz7937df12008-09-02 15:55:48 +0200880ip_vs_lookup_real_service(int af, __u16 protocol,
881 const union nf_inet_addr *daddr, __be16 dport);
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883extern int ip_vs_use_count_inc(void);
884extern void ip_vs_use_count_dec(void);
885extern int ip_vs_control_init(void);
886extern void ip_vs_control_cleanup(void);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800887extern struct ip_vs_dest *
Julius Volz7937df12008-09-02 15:55:48 +0200888ip_vs_find_dest(int af, const union nf_inet_addr *daddr, __be16 dport,
889 const union nf_inet_addr *vaddr, __be16 vport, __u16 protocol);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800890extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892
893/*
894 * IPVS sync daemon data and function prototypes
895 * (from ip_vs_sync.c)
896 */
897extern volatile int ip_vs_sync_state;
898extern volatile int ip_vs_master_syncid;
899extern volatile int ip_vs_backup_syncid;
900extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
901extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
902extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
903extern int stop_sync_thread(int state);
904extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
905
906
907/*
908 * IPVS rate estimator prototypes (from ip_vs_est.c)
909 */
Sven Wegenera919cf42008-08-14 00:47:16 +0200910extern int ip_vs_estimator_init(void);
911extern void ip_vs_estimator_cleanup(void);
Sven Wegener3a14a3132008-08-10 18:24:41 +0000912extern void ip_vs_new_estimator(struct ip_vs_stats *stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
914extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
915
916/*
917 * Various IPVS packet transmitters (from ip_vs_xmit.c)
918 */
919extern int ip_vs_null_xmit
920(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
921extern int ip_vs_bypass_xmit
922(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
923extern int ip_vs_nat_xmit
924(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
925extern int ip_vs_tunnel_xmit
926(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
927extern int ip_vs_dr_xmit
928(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
929extern int ip_vs_icmp_xmit
930(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
931extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
932
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200933#ifdef CONFIG_IP_VS_IPV6
934extern int ip_vs_bypass_xmit_v6
935(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
936extern int ip_vs_nat_xmit_v6
937(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
938extern int ip_vs_tunnel_xmit_v6
939(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
940extern int ip_vs_dr_xmit_v6
941(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
942extern int ip_vs_icmp_xmit_v6
943(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
944 int offset);
945#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947/*
948 * This is a simple mechanism to ignore packets when
949 * we are loaded. Just set ip_vs_drop_rate to 'n' and
950 * we start to drop 1/rate of the packets
951 */
952extern int ip_vs_drop_rate;
953extern int ip_vs_drop_counter;
954
955static __inline__ int ip_vs_todrop(void)
956{
957 if (!ip_vs_drop_rate) return 0;
958 if (--ip_vs_drop_counter > 0) return 0;
959 ip_vs_drop_counter = ip_vs_drop_rate;
960 return 1;
961}
962
963/*
964 * ip_vs_fwd_tag returns the forwarding tag of the connection
965 */
966#define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
967
Adrian Bunk732db652005-09-01 17:40:26 -0700968static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 char fwd;
971
972 switch (IP_VS_FWD_METHOD(cp)) {
973 case IP_VS_CONN_F_MASQ:
974 fwd = 'M'; break;
975 case IP_VS_CONN_F_LOCALNODE:
976 fwd = 'L'; break;
977 case IP_VS_CONN_F_TUNNEL:
978 fwd = 'T'; break;
979 case IP_VS_CONN_F_DROUTE:
980 fwd = 'R'; break;
981 case IP_VS_CONN_F_BYPASS:
982 fwd = 'B'; break;
983 default:
984 fwd = '?'; break;
985 }
986 return fwd;
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200990 struct ip_vs_conn *cp, int dir);
991
992#ifdef CONFIG_IP_VS_IPV6
993extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
994 struct ip_vs_conn *cp, int dir);
995#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Al Virob1550f22006-11-14 21:37:50 -0800997extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Al Virof9214b22006-11-16 02:41:18 -0800999static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Al Virof9214b22006-11-16 02:41:18 -08001001 __be32 diff[2] = { ~old, new };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Joe Perches07f07572008-11-19 15:44:53 -08001003 return csum_partial(diff, sizeof(diff), oldsum);
Al Virof9214b22006-11-16 02:41:18 -08001004}
1005
Julius Volz0bbdd422008-09-02 15:55:42 +02001006#ifdef CONFIG_IP_VS_IPV6
1007static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
1008 __wsum oldsum)
1009{
1010 __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
1011 new[3], new[2], new[1], new[0] };
1012
Joe Perches07f07572008-11-19 15:44:53 -08001013 return csum_partial(diff, sizeof(diff), oldsum);
Julius Volz0bbdd422008-09-02 15:55:42 +02001014}
1015#endif
1016
Al Virof9214b22006-11-16 02:41:18 -08001017static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
1018{
1019 __be16 diff[2] = { ~old, new };
1020
Joe Perches07f07572008-11-19 15:44:53 -08001021 return csum_partial(diff, sizeof(diff), oldsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Julian Anastasovcf356d62010-10-17 16:21:07 +03001024/*
1025 * Forget current conntrack (unconfirmed) and attach notrack entry
1026 */
1027static inline void ip_vs_notrack(struct sk_buff *skb)
1028{
1029#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1030 enum ip_conntrack_info ctinfo;
1031 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1032
1033 if (!ct || !nf_ct_is_untracked(ct)) {
1034 nf_reset(skb);
1035 skb->nfct = &nf_ct_untracked_get()->ct_general;
1036 skb->nfctinfo = IP_CT_NEW;
1037 nf_conntrack_get(skb->nfct);
1038 }
1039#endif
1040}
1041
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001042#ifdef CONFIG_IP_VS_NFCT
1043/*
1044 * Netfilter connection tracking
1045 * (from ip_vs_nfct.c)
1046 */
1047static inline int ip_vs_conntrack_enabled(void)
1048{
1049 return sysctl_ip_vs_conntrack;
1050}
1051
Julian Anastasov6523ce12010-09-05 18:02:29 +00001052extern void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
1053 int outin);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001054extern int ip_vs_confirm_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp);
1055extern void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
1056 struct ip_vs_conn *cp, u_int8_t proto,
1057 const __be16 port, int from_rs);
1058extern void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
1059
1060#else
1061
1062static inline int ip_vs_conntrack_enabled(void)
1063{
1064 return 0;
1065}
1066
1067static inline void ip_vs_update_conntrack(struct sk_buff *skb,
1068 struct ip_vs_conn *cp, int outin)
1069{
1070}
1071
1072static inline int ip_vs_confirm_conntrack(struct sk_buff *skb,
1073 struct ip_vs_conn *cp)
1074{
1075 return NF_ACCEPT;
1076}
1077
1078static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
1079{
1080}
1081/* CONFIG_IP_VS_NFCT */
1082#endif
Julian Anastasov6523ce12010-09-05 18:02:29 +00001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084#endif /* __KERNEL__ */
1085
Julius Volzbc4768e2008-07-31 20:45:24 -07001086#endif /* _NET_IP_VS_H */