]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/net/ip_vs.h
ipvs: Fix faulty IPv6 extension header handling in IPVS
[mirror_ubuntu-bionic-kernel.git] / include / net / ip_vs.h
CommitLineData
1da177e4
LT
1/*
2 * IP Virtual Server
3 * data structure and functionality definitions
4 */
5
bc4768eb
JV
6#ifndef _NET_IP_VS_H
7#define _NET_IP_VS_H
1da177e4 8
bc4768eb 9#include <linux/ip_vs.h> /* definitions shared with userland */
1da177e4 10
bc4768eb
JV
11#include <asm/types.h> /* for __uXX types */
12
1da177e4
LT
13#include <linux/list.h> /* for struct list_head */
14#include <linux/spinlock.h> /* for struct rwlock_t */
60063497 15#include <linux/atomic.h> /* for struct atomic_t */
1da177e4 16#include <linux/compiler.h>
14c85021 17#include <linux/timer.h>
187f1882 18#include <linux/bug.h>
1da177e4 19
14c85021 20#include <net/checksum.h>
e7ade46a 21#include <linux/netfilter.h> /* for union nf_inet_addr */
1668e010 22#include <linux/ip.h>
e7ade46a 23#include <linux/ipv6.h> /* for struct ipv6hdr */
4e3fd7a0 24#include <net/ipv6.h>
63dca2c0
JDB
25#if IS_ENABLED(CONFIG_IPV6)
26#include <linux/netfilter_ipv6/ip6_tables.h>
27#endif
a638e514 28#if IS_ENABLED(CONFIG_NF_CONNTRACK)
f4bc17cd
JA
29#include <net/netfilter/nf_conntrack.h>
30#endif
61b1ab45
HS
31#include <net/net_namespace.h> /* Netw namespace */
32
33/*
34 * Generic access of ipvs struct
35 */
36static inline struct netns_ipvs *net_ipvs(struct net* net)
37{
38 return net->ipvs;
39}
fc723250
HS
40/*
41 * Get net ptr from skb in traffic cases
42 * use skb_sknet when call is from userland (ioctl or netlink)
43 */
a0840e2e 44static inline struct net *skb_net(const struct sk_buff *skb)
fc723250
HS
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);
a09d1977 55 if (skb_dst(skb) && skb_dst(skb)->dev)
fc723250
HS
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
a0840e2e 72static inline struct net *skb_sknet(const struct sk_buff *skb)
fc723250
HS
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}
b17fc996
HS
93/*
94 * This one needed for single_open_net since net is stored directly in
25985edc 95 * private not as a struct i.e. seq_file_net can't be used.
b17fc996
HS
96 */
97static inline struct net *seq_file_single_net(struct seq_file *seq)
98{
99#ifdef CONFIG_NET_NS
100 return (struct net *)seq->private;
101#else
102 return &init_net;
103#endif
104}
6f7edb48
CB
105
106/* Connections' size value needed by ip_vs_ctl.c */
107extern int ip_vs_conn_tab_size;
108
64aae3cb 109struct ip_vs_iphdr {
63dca2c0
JDB
110 __u32 len; /* IPv4 simply where L4 starts
111 IPv6 where L4 Transport Header starts */
112 __u16 fragoffs; /* IPv6 fragment offset, 0 if first frag (or not frag)*/
113 __s16 protocol;
114 __s32 flags;
64aae3cb
JV
115 union nf_inet_addr saddr;
116 union nf_inet_addr daddr;
117};
118
119static inline void
63dca2c0
JDB
120ip_vs_fill_ip4hdr(const void *nh, struct ip_vs_iphdr *iphdr)
121{
122 const struct iphdr *iph = nh;
123
124 iphdr->len = iph->ihl * 4;
125 iphdr->fragoffs = 0;
126 iphdr->protocol = iph->protocol;
127 iphdr->saddr.ip = iph->saddr;
128 iphdr->daddr.ip = iph->daddr;
129}
130
131/* This function handles filling *ip_vs_iphdr, both for IPv4 and IPv6.
132 * IPv6 requires some extra work, as finding proper header position,
133 * depend on the IPv6 extension headers.
134 */
135static inline void
136ip_vs_fill_iph_skb(int af, const struct sk_buff *skb, struct ip_vs_iphdr *iphdr)
64aae3cb
JV
137{
138#ifdef CONFIG_IP_VS_IPV6
139 if (af == AF_INET6) {
63dca2c0
JDB
140 const struct ipv6hdr *iph =
141 (struct ipv6hdr *)skb_network_header(skb);
4e3fd7a0
AD
142 iphdr->saddr.in6 = iph->saddr;
143 iphdr->daddr.in6 = iph->daddr;
63dca2c0
JDB
144 /* ipv6_find_hdr() updates len, flags */
145 iphdr->len = 0;
146 iphdr->flags = 0;
147 iphdr->protocol = ipv6_find_hdr(skb, &iphdr->len, -1,
148 &iphdr->fragoffs,
149 &iphdr->flags);
64aae3cb
JV
150 } else
151#endif
152 {
63dca2c0
JDB
153 const struct iphdr *iph =
154 (struct iphdr *)skb_network_header(skb);
155 iphdr->len = iph->ihl * 4;
156 iphdr->fragoffs = 0;
157 iphdr->protocol = iph->protocol;
158 iphdr->saddr.ip = iph->saddr;
159 iphdr->daddr.ip = iph->daddr;
160 }
161}
162
163/* This function is a faster version of ip_vs_fill_iph_skb().
164 * Where we only populate {s,d}addr (and avoid calling ipv6_find_hdr()).
165 * This is used by the some of the ip_vs_*_schedule() functions.
166 * (Mostly done to avoid ABI breakage of external schedulers)
167 */
168static inline void
169ip_vs_fill_iph_addr_only(int af, const struct sk_buff *skb,
170 struct ip_vs_iphdr *iphdr)
171{
172#ifdef CONFIG_IP_VS_IPV6
173 if (af == AF_INET6) {
174 const struct ipv6hdr *iph =
175 (struct ipv6hdr *)skb_network_header(skb);
176 iphdr->saddr.in6 = iph->saddr;
177 iphdr->daddr.in6 = iph->daddr;
178 } else {
179#endif
180 const struct iphdr *iph =
181 (struct iphdr *)skb_network_header(skb);
64aae3cb
JV
182 iphdr->saddr.ip = iph->saddr;
183 iphdr->daddr.ip = iph->daddr;
184 }
185}
186
187static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
188 const union nf_inet_addr *src)
189{
190#ifdef CONFIG_IP_VS_IPV6
191 if (af == AF_INET6)
4e3fd7a0 192 dst->in6 = src->in6;
64aae3cb
JV
193 else
194#endif
195 dst->ip = src->ip;
196}
197
198static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
199 const union nf_inet_addr *b)
200{
201#ifdef CONFIG_IP_VS_IPV6
202 if (af == AF_INET6)
203 return ipv6_addr_equal(&a->in6, &b->in6);
204#endif
205 return a->ip == b->ip;
206}
207
1da177e4 208#ifdef CONFIG_IP_VS_DEBUG
14c85021
ACM
209#include <linux/net.h>
210
1da177e4 211extern int ip_vs_get_debug_level(void);
c842a3ad
JV
212
213static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
214 const union nf_inet_addr *addr,
215 int *idx)
216{
217 int len;
218#ifdef CONFIG_IP_VS_IPV6
219 if (af == AF_INET6)
120b9c14 220 len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6c]",
0c6ce78a 221 &addr->in6) + 1;
c842a3ad
JV
222 else
223#endif
3685f25d
HH
224 len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
225 &addr->ip) + 1;
c842a3ad
JV
226
227 *idx += len;
228 BUG_ON(*idx > buf_len + 1);
229 return &buf[*idx - len];
230}
231
9aada7ac
HE
232#define IP_VS_DBG_BUF(level, msg, ...) \
233 do { \
234 char ip_vs_dbg_buf[160]; \
235 int ip_vs_dbg_idx = 0; \
236 if (level <= ip_vs_get_debug_level()) \
237 printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
238 } while (0)
239#define IP_VS_ERR_BUF(msg...) \
240 do { \
241 char ip_vs_dbg_buf[160]; \
242 int ip_vs_dbg_idx = 0; \
243 pr_err(msg); \
244 } while (0)
c842a3ad
JV
245
246/* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
9aada7ac
HE
247#define IP_VS_DBG_ADDR(af, addr) \
248 ip_vs_dbg_addr(af, ip_vs_dbg_buf, \
249 sizeof(ip_vs_dbg_buf), addr, \
250 &ip_vs_dbg_idx)
251
252#define IP_VS_DBG(level, msg, ...) \
253 do { \
254 if (level <= ip_vs_get_debug_level()) \
255 printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
256 } while (0)
257#define IP_VS_DBG_RL(msg, ...) \
258 do { \
259 if (net_ratelimit()) \
260 printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
261 } while (0)
0d79641a 262#define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) \
9aada7ac
HE
263 do { \
264 if (level <= ip_vs_get_debug_level()) \
0d79641a 265 pp->debug_packet(af, pp, skb, ofs, msg); \
9aada7ac 266 } while (0)
0d79641a 267#define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) \
9aada7ac
HE
268 do { \
269 if (level <= ip_vs_get_debug_level() && \
270 net_ratelimit()) \
0d79641a 271 pp->debug_packet(af, pp, skb, ofs, msg); \
9aada7ac 272 } while (0)
1da177e4 273#else /* NO DEBUGGING at ALL */
c842a3ad
JV
274#define IP_VS_DBG_BUF(level, msg...) do {} while (0)
275#define IP_VS_ERR_BUF(msg...) do {} while (0)
1da177e4
LT
276#define IP_VS_DBG(level, msg...) do {} while (0)
277#define IP_VS_DBG_RL(msg...) do {} while (0)
0d79641a
JA
278#define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) do {} while (0)
279#define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) do {} while (0)
1da177e4
LT
280#endif
281
282#define IP_VS_BUG() BUG()
1e3e238e 283#define IP_VS_ERR_RL(msg, ...) \
9aada7ac
HE
284 do { \
285 if (net_ratelimit()) \
1e3e238e 286 pr_err(msg, ##__VA_ARGS__); \
9aada7ac 287 } while (0)
1da177e4
LT
288
289#ifdef CONFIG_IP_VS_DEBUG
290#define EnterFunction(level) \
9aada7ac
HE
291 do { \
292 if (level <= ip_vs_get_debug_level()) \
293 printk(KERN_DEBUG \
294 pr_fmt("Enter: %s, %s line %i\n"), \
295 __func__, __FILE__, __LINE__); \
296 } while (0)
297#define LeaveFunction(level) \
298 do { \
299 if (level <= ip_vs_get_debug_level()) \
300 printk(KERN_DEBUG \
301 pr_fmt("Leave: %s, %s line %i\n"), \
302 __func__, __FILE__, __LINE__); \
303 } while (0)
1da177e4
LT
304#else
305#define EnterFunction(level) do {} while (0)
306#define LeaveFunction(level) do {} while (0)
307#endif
308
309#define IP_VS_WAIT_WHILE(expr) while (expr) { cpu_relax(); }
310
311
312/*
313 * The port number of FTP service (in network order).
314 */
f3a7c66b
HH
315#define FTPPORT cpu_to_be16(21)
316#define FTPDATA cpu_to_be16(20)
1da177e4 317
1da177e4
LT
318/*
319 * TCP State Values
320 */
321enum {
322 IP_VS_TCP_S_NONE = 0,
323 IP_VS_TCP_S_ESTABLISHED,
324 IP_VS_TCP_S_SYN_SENT,
325 IP_VS_TCP_S_SYN_RECV,
326 IP_VS_TCP_S_FIN_WAIT,
327 IP_VS_TCP_S_TIME_WAIT,
328 IP_VS_TCP_S_CLOSE,
329 IP_VS_TCP_S_CLOSE_WAIT,
330 IP_VS_TCP_S_LAST_ACK,
331 IP_VS_TCP_S_LISTEN,
332 IP_VS_TCP_S_SYNACK,
333 IP_VS_TCP_S_LAST
334};
335
336/*
337 * UDP State Values
338 */
339enum {
340 IP_VS_UDP_S_NORMAL,
341 IP_VS_UDP_S_LAST,
342};
343
344/*
345 * ICMP State Values
346 */
347enum {
348 IP_VS_ICMP_S_NORMAL,
349 IP_VS_ICMP_S_LAST,
350};
351
2906f66a
VMR
352/*
353 * SCTP State Values
354 */
355enum ip_vs_sctp_states {
356 IP_VS_SCTP_S_NONE,
357 IP_VS_SCTP_S_INIT_CLI,
358 IP_VS_SCTP_S_INIT_SER,
359 IP_VS_SCTP_S_INIT_ACK_CLI,
360 IP_VS_SCTP_S_INIT_ACK_SER,
361 IP_VS_SCTP_S_ECHO_CLI,
362 IP_VS_SCTP_S_ECHO_SER,
363 IP_VS_SCTP_S_ESTABLISHED,
364 IP_VS_SCTP_S_SHUT_CLI,
365 IP_VS_SCTP_S_SHUT_SER,
366 IP_VS_SCTP_S_SHUT_ACK_CLI,
367 IP_VS_SCTP_S_SHUT_ACK_SER,
368 IP_VS_SCTP_S_CLOSED,
369 IP_VS_SCTP_S_LAST
370};
371
1da177e4
LT
372/*
373 * Delta sequence info structure
374 * Each ip_vs_conn has 2 (output AND input seq. changes).
375 * Only used in the VS/NAT.
376 */
377struct ip_vs_seq {
378 __u32 init_seq; /* Add delta from this seq */
379 __u32 delta; /* Delta in sequence numbers */
380 __u32 previous_delta; /* Delta in sequence numbers
381 before last resized pkt */
382};
383
b17fc996
HS
384/*
385 * counters per cpu
386 */
387struct ip_vs_counters {
388 __u32 conns; /* connections scheduled */
389 __u32 inpkts; /* incoming packets */
390 __u32 outpkts; /* outgoing packets */
391 __u64 inbytes; /* incoming bytes */
392 __u64 outbytes; /* outgoing bytes */
393};
394/*
395 * Stats per cpu
396 */
397struct ip_vs_cpu_stats {
398 struct ip_vs_counters ustats;
399 struct u64_stats_sync syncp;
400};
1da177e4
LT
401
402/*
3a14a313 403 * IPVS statistics objects
1da177e4 404 */
3a14a313
SW
405struct ip_vs_estimator {
406 struct list_head list;
407
408 u64 last_inbytes;
409 u64 last_outbytes;
410 u32 last_conns;
411 u32 last_inpkts;
412 u32 last_outpkts;
413
414 u32 cps;
415 u32 inpps;
416 u32 outpps;
417 u32 inbps;
418 u32 outbps;
419};
420
fd2c3ef7 421struct ip_vs_stats {
b17fc996 422 struct ip_vs_stats_user ustats; /* statistics */
e9c0ce23 423 struct ip_vs_estimator est; /* estimator */
b17fc996
HS
424 struct ip_vs_cpu_stats *cpustats; /* per cpu counters */
425 spinlock_t lock; /* spin lock */
55a3d4e1 426 struct ip_vs_stats_user ustats0; /* reset values */
1da177e4
LT
427};
428
14c85021
ACM
429struct dst_entry;
430struct iphdr;
1da177e4
LT
431struct ip_vs_conn;
432struct ip_vs_app;
14c85021 433struct sk_buff;
252c6410 434struct ip_vs_proto_data;
1da177e4
LT
435
436struct ip_vs_protocol {
437 struct ip_vs_protocol *next;
438 char *name;
2ad17def
JA
439 u16 protocol;
440 u16 num_states;
1da177e4 441 int dont_defrag;
1da177e4
LT
442
443 void (*init)(struct ip_vs_protocol *pp);
444
445 void (*exit)(struct ip_vs_protocol *pp);
446
582b8e3e 447 int (*init_netns)(struct net *net, struct ip_vs_proto_data *pd);
252c6410
HS
448
449 void (*exit_netns)(struct net *net, struct ip_vs_proto_data *pd);
450
51ef348b 451 int (*conn_schedule)(int af, struct sk_buff *skb,
9330419d 452 struct ip_vs_proto_data *pd,
1da177e4
LT
453 int *verdict, struct ip_vs_conn **cpp);
454
455 struct ip_vs_conn *
51ef348b
JV
456 (*conn_in_get)(int af,
457 const struct sk_buff *skb,
51ef348b 458 const struct ip_vs_iphdr *iph,
1da177e4
LT
459 unsigned int proto_off,
460 int inverse);
461
462 struct ip_vs_conn *
51ef348b
JV
463 (*conn_out_get)(int af,
464 const struct sk_buff *skb,
51ef348b 465 const struct ip_vs_iphdr *iph,
1da177e4
LT
466 unsigned int proto_off,
467 int inverse);
468
3db05fea 469 int (*snat_handler)(struct sk_buff *skb,
1da177e4
LT
470 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
471
3db05fea 472 int (*dnat_handler)(struct sk_buff *skb,
1da177e4
LT
473 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
474
51ef348b
JV
475 int (*csum_check)(int af, struct sk_buff *skb,
476 struct ip_vs_protocol *pp);
1da177e4
LT
477
478 const char *(*state_name)(int state);
479
4a516f11
SH
480 void (*state_transition)(struct ip_vs_conn *cp, int direction,
481 const struct sk_buff *skb,
482 struct ip_vs_proto_data *pd);
1da177e4 483
ab8a5e84 484 int (*register_app)(struct net *net, struct ip_vs_app *inc);
1da177e4 485
ab8a5e84 486 void (*unregister_app)(struct net *net, struct ip_vs_app *inc);
1da177e4
LT
487
488 int (*app_conn_bind)(struct ip_vs_conn *cp);
489
0d79641a 490 void (*debug_packet)(int af, struct ip_vs_protocol *pp,
1da177e4
LT
491 const struct sk_buff *skb,
492 int offset,
493 const char *msg);
494
9330419d 495 void (*timeout_change)(struct ip_vs_proto_data *pd, int flags);
1da177e4
LT
496};
497
252c6410
HS
498/*
499 * protocol data per netns
500 */
501struct ip_vs_proto_data {
502 struct ip_vs_proto_data *next;
503 struct ip_vs_protocol *pp;
504 int *timeout_table; /* protocol timeout table */
505 atomic_t appcnt; /* counter of proto app incs. */
506 struct tcp_states_t *tcp_state_table;
507};
508
509extern struct ip_vs_protocol *ip_vs_proto_get(unsigned short proto);
510extern struct ip_vs_proto_data *ip_vs_proto_data_get(struct net *net,
511 unsigned short proto);
1da177e4 512
f11017ec 513struct ip_vs_conn_param {
6e67e586 514 struct net *net;
f11017ec
SH
515 const union nf_inet_addr *caddr;
516 const union nf_inet_addr *vaddr;
517 __be16 cport;
518 __be16 vport;
519 __u16 protocol;
520 u16 af;
85999283
SH
521
522 const struct ip_vs_pe *pe;
523 char *pe_data;
524 __u8 pe_data_len;
f11017ec
SH
525};
526
1da177e4
LT
527/*
528 * IP_VS structure allocated for each dynamically scheduled connection
529 */
530struct ip_vs_conn {
731109e7 531 struct hlist_node c_list; /* hashed list heads */
6e67e586
HS
532#ifdef CONFIG_NET_NS
533 struct net *net; /* Name space */
534#endif
1da177e4 535 /* Protocol, addresses and port numbers */
6e67e586
HS
536 u16 af; /* address family */
537 __be16 cport;
538 __be16 vport;
539 __be16 dport;
540 __u32 fwmark; /* Fire wall mark from skb */
541 union nf_inet_addr caddr; /* client address */
542 union nf_inet_addr vaddr; /* virtual address */
543 union nf_inet_addr daddr; /* destination address */
544 volatile __u32 flags; /* status flags */
1da177e4
LT
545 __u16 protocol; /* Which protocol (TCP/UDP) */
546
547 /* counter and timer */
548 atomic_t refcnt; /* reference count */
549 struct timer_list timer; /* Expiration timer */
550 volatile unsigned long timeout; /* timeout */
551
552 /* Flags and state transition */
553 spinlock_t lock; /* lock for state transition */
1da177e4 554 volatile __u16 state; /* state info */
efac5276
RB
555 volatile __u16 old_state; /* old state, to be used for
556 * state transition triggerd
557 * synchronization
558 */
749c42b6 559 unsigned long sync_endtime; /* jiffies + sent_retries */
1da177e4
LT
560
561 /* Control members */
562 struct ip_vs_conn *control; /* Master control connection */
563 atomic_t n_control; /* Number of controlled ones */
564 struct ip_vs_dest *dest; /* real server */
565 atomic_t in_pkts; /* incoming packet counter */
566
567 /* packet transmitter for different forwarding methods. If it
568 mangles the packet, it must return NF_DROP or better NF_STOLEN,
569 otherwise this must be changed to a sk_buff **.
fc604767 570 NF_ACCEPT can be returned when destination is local.
1da177e4
LT
571 */
572 int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
573 struct ip_vs_protocol *pp);
574
575 /* Note: we can group the following members into a structure,
576 in order to save more space, and the following members are
577 only used in VS/NAT anyway */
578 struct ip_vs_app *app; /* bound ip_vs_app object */
579 void *app_data; /* Application private data */
580 struct ip_vs_seq in_seq; /* incoming seq. struct */
581 struct ip_vs_seq out_seq; /* outgoing seq. struct */
85999283 582
e9e5eee8 583 const struct ip_vs_pe *pe;
85999283
SH
584 char *pe_data;
585 __u8 pe_data_len;
1da177e4
LT
586};
587
6e67e586
HS
588/*
589 * To save some memory in conn table when name space is disabled.
590 */
591static inline struct net *ip_vs_conn_net(const struct ip_vs_conn *cp)
592{
593#ifdef CONFIG_NET_NS
594 return cp->net;
595#else
596 return &init_net;
597#endif
598}
599static inline void ip_vs_conn_net_set(struct ip_vs_conn *cp, struct net *net)
600{
601#ifdef CONFIG_NET_NS
602 cp->net = net;
603#endif
604}
605
606static inline int ip_vs_conn_net_eq(const struct ip_vs_conn *cp,
607 struct net *net)
608{
609#ifdef CONFIG_NET_NS
610 return cp->net == net;
611#else
612 return 1;
613#endif
614}
1da177e4 615
c860c6b1
JV
616/*
617 * Extended internal versions of struct ip_vs_service_user and
618 * ip_vs_dest_user for IPv6 support.
619 *
620 * We need these to conveniently pass around service and destination
621 * options, but unfortunately, we also need to keep the old definitions to
622 * maintain userspace backwards compatibility for the setsockopt interface.
623 */
624struct ip_vs_service_user_kern {
625 /* virtual service addresses */
626 u16 af;
627 u16 protocol;
628 union nf_inet_addr addr; /* virtual ip address */
629 u16 port;
630 u32 fwmark; /* firwall mark of service */
631
632 /* virtual service options */
633 char *sched_name;
0d1e71b0 634 char *pe_name;
95c96174
ED
635 unsigned int flags; /* virtual service flags */
636 unsigned int timeout; /* persistent timeout in sec */
c860c6b1
JV
637 u32 netmask; /* persistent netmask */
638};
639
640
641struct ip_vs_dest_user_kern {
642 /* destination server address */
643 union nf_inet_addr addr;
644 u16 port;
645
646 /* real server options */
95c96174 647 unsigned int conn_flags; /* connection flags */
c860c6b1
JV
648 int weight; /* destination weight */
649
650 /* thresholds for active connections */
651 u32 u_threshold; /* upper threshold */
652 u32 l_threshold; /* lower threshold */
653};
654
655
1da177e4
LT
656/*
657 * The information about the virtual service offered to the net
658 * and the forwarding entries
659 */
660struct ip_vs_service {
661 struct list_head s_list; /* for normal service table */
662 struct list_head f_list; /* for fwmark-based service table */
663 atomic_t refcnt; /* reference counter */
664 atomic_t usecnt; /* use counter */
665
e7ade46a 666 u16 af; /* address family */
1da177e4 667 __u16 protocol; /* which protocol (TCP/UDP) */
e7ade46a 668 union nf_inet_addr addr; /* IP address for virtual service */
014d730d 669 __be16 port; /* port number for the service */
1da177e4 670 __u32 fwmark; /* firewall mark of the service */
95c96174
ED
671 unsigned int flags; /* service status flags */
672 unsigned int timeout; /* persistent timeout in ticks */
014d730d 673 __be32 netmask; /* grouping granularity */
fc723250 674 struct net *net;
1da177e4
LT
675
676 struct list_head destinations; /* real server d-linked list */
677 __u32 num_dests; /* number of servers */
678 struct ip_vs_stats stats; /* statistics for the service */
679 struct ip_vs_app *inc; /* bind conns to this app inc */
680
681 /* for scheduling */
682 struct ip_vs_scheduler *scheduler; /* bound scheduler object */
683 rwlock_t sched_lock; /* lock sched_data */
684 void *sched_data; /* scheduler application data */
85999283
SH
685
686 /* alternate persistence engine */
687 struct ip_vs_pe *pe;
1da177e4
LT
688};
689
690
691/*
692 * The real server destination forwarding entry
693 * with ip address, port number, and so on.
694 */
695struct ip_vs_dest {
696 struct list_head n_list; /* for the dests in the service */
697 struct list_head d_list; /* for table with all the dests */
698
e7ade46a 699 u16 af; /* address family */
014d730d 700 __be16 port; /* port number of the server */
f2431e6e 701 union nf_inet_addr addr; /* IP address of the server */
95c96174 702 volatile unsigned int flags; /* dest status flags */
1da177e4
LT
703 atomic_t conn_flags; /* flags to copy to conn */
704 atomic_t weight; /* server weight */
705
706 atomic_t refcnt; /* reference counter */
707 struct ip_vs_stats stats; /* statistics */
708
709 /* connection counters and thresholds */
710 atomic_t activeconns; /* active connections */
711 atomic_t inactconns; /* inactive connections */
712 atomic_t persistconns; /* persistent connections */
713 __u32 u_threshold; /* upper threshold */
714 __u32 l_threshold; /* lower threshold */
715
716 /* for destination cache */
717 spinlock_t dst_lock; /* lock of dst_cache */
718 struct dst_entry *dst_cache; /* destination cache entry */
719 u32 dst_rtos; /* RT_TOS(tos) for dst */
714f095f 720 u32 dst_cookie;
c92f5ca2 721 union nf_inet_addr dst_saddr;
1da177e4
LT
722
723 /* for virtual service */
724 struct ip_vs_service *svc; /* service it belongs to */
725 __u16 protocol; /* which protocol (TCP/UDP) */
014d730d 726 __be16 vport; /* virtual port number */
f2431e6e 727 union nf_inet_addr vaddr; /* virtual IP address */
1da177e4
LT
728 __u32 vfwmark; /* firewall mark of service */
729};
730
731
732/*
733 * The scheduler object
734 */
735struct ip_vs_scheduler {
736 struct list_head n_list; /* d-linked list head */
737 char *name; /* scheduler name */
738 atomic_t refcnt; /* reference counter */
739 struct module *module; /* THIS_MODULE/NULL */
740
741 /* scheduler initializing service */
742 int (*init_service)(struct ip_vs_service *svc);
743 /* scheduling service finish */
744 int (*done_service)(struct ip_vs_service *svc);
745 /* scheduler updating service */
746 int (*update_service)(struct ip_vs_service *svc);
747
748 /* selecting a server from the given service */
749 struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
750 const struct sk_buff *skb);
751};
752
85999283
SH
753/* The persistence engine object */
754struct ip_vs_pe {
755 struct list_head n_list; /* d-linked list head */
756 char *name; /* scheduler name */
757 atomic_t refcnt; /* reference counter */
758 struct module *module; /* THIS_MODULE/NULL */
759
760 /* get the connection template, if any */
761 int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
762 bool (*ct_match)(const struct ip_vs_conn_param *p,
763 struct ip_vs_conn *ct);
764 u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
765 bool inverse);
a3c918ac 766 int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
85999283 767};
1da177e4
LT
768
769/*
770 * The application module object (a.k.a. app incarnation)
771 */
fd2c3ef7 772struct ip_vs_app {
1da177e4
LT
773 struct list_head a_list; /* member in app list */
774 int type; /* IP_VS_APP_TYPE_xxx */
775 char *name; /* application module name */
776 __u16 protocol;
777 struct module *module; /* THIS_MODULE/NULL */
778 struct list_head incs_list; /* list of incarnations */
779
780 /* members for application incarnations */
781 struct list_head p_list; /* member in proto app list */
782 struct ip_vs_app *app; /* its real application */
014d730d 783 __be16 port; /* port number in net order */
1da177e4
LT
784 atomic_t usecnt; /* usage counter */
785
8b27b10f
JA
786 /*
787 * output hook: Process packet in inout direction, diff set for TCP.
788 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
789 * 2=Mangled but checksum was not updated
790 */
1da177e4 791 int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
3db05fea 792 struct sk_buff *, int *diff);
1da177e4 793
8b27b10f
JA
794 /*
795 * input hook: Process packet in outin direction, diff set for TCP.
796 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
797 * 2=Mangled but checksum was not updated
798 */
1da177e4 799 int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
3db05fea 800 struct sk_buff *, int *diff);
1da177e4
LT
801
802 /* ip_vs_app initializer */
803 int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
804
805 /* ip_vs_app finish */
806 int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
807
808
809 /* not used now */
810 int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
811 struct ip_vs_protocol *);
812
813 void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
814
815 int * timeout_table;
816 int * timeouts;
817 int timeouts_size;
818
819 int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
820 int *verdict, struct ip_vs_conn **cpp);
821
822 struct ip_vs_conn *
823 (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
824 const struct iphdr *iph, unsigned int proto_off,
825 int inverse);
826
827 struct ip_vs_conn *
828 (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
829 const struct iphdr *iph, unsigned int proto_off,
830 int inverse);
831
832 int (*state_transition)(struct ip_vs_conn *cp, int direction,
833 const struct sk_buff *skb,
834 struct ip_vs_app *app);
835
836 void (*timeout_change)(struct ip_vs_app *app, int flags);
837};
838
f73181c8
PNA
839struct ipvs_master_sync_state {
840 struct list_head sync_queue;
841 struct ip_vs_sync_buff *sync_buff;
842 int sync_queue_len;
843 unsigned int sync_queue_delay;
844 struct task_struct *master_thread;
845 struct delayed_work master_wakeup_work;
846 struct netns_ipvs *ipvs;
847};
848
2553d064
JA
849/* IPVS in network namespace */
850struct netns_ipvs {
851 int gen; /* Generation */
7a4f0761 852 int enable; /* enable like nf_hooks do */
2553d064
JA
853 /*
854 * Hash table: for real service lookups
855 */
856 #define IP_VS_RTAB_BITS 4
857 #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
858 #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
859
860 struct list_head rs_table[IP_VS_RTAB_SIZE];
861 /* ip_vs_app */
862 struct list_head app_list;
2553d064
JA
863 /* ip_vs_proto */
864 #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
865 struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE];
866 /* ip_vs_proto_tcp */
867#ifdef CONFIG_IP_VS_PROTO_TCP
868 #define TCP_APP_TAB_BITS 4
869 #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
870 #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
871 struct list_head tcp_apps[TCP_APP_TAB_SIZE];
872 spinlock_t tcp_app_lock;
873#endif
874 /* ip_vs_proto_udp */
875#ifdef CONFIG_IP_VS_PROTO_UDP
876 #define UDP_APP_TAB_BITS 4
877 #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
878 #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
879 struct list_head udp_apps[UDP_APP_TAB_SIZE];
880 spinlock_t udp_app_lock;
881#endif
882 /* ip_vs_proto_sctp */
883#ifdef CONFIG_IP_VS_PROTO_SCTP
884 #define SCTP_APP_TAB_BITS 4
885 #define SCTP_APP_TAB_SIZE (1 << SCTP_APP_TAB_BITS)
886 #define SCTP_APP_TAB_MASK (SCTP_APP_TAB_SIZE - 1)
887 /* Hash table for SCTP application incarnations */
888 struct list_head sctp_apps[SCTP_APP_TAB_SIZE];
889 spinlock_t sctp_app_lock;
890#endif
891 /* ip_vs_conn */
892 atomic_t conn_count; /* connection counter */
893
894 /* ip_vs_ctl */
2a0751af 895 struct ip_vs_stats tot_stats; /* Statistics & est. */
2553d064
JA
896
897 int num_services; /* no of virtual services */
f2247fbd
SH
898
899 rwlock_t rs_lock; /* real services table */
f2247fbd
SH
900 /* Trash for destinations */
901 struct list_head dest_trash;
902 /* Service counters */
903 atomic_t ftpsvc_counter;
904 atomic_t nullsvc_counter;
905
906#ifdef CONFIG_SYSCTL
2553d064
JA
907 /* 1/rate drop and drop-entry variables */
908 struct delayed_work defense_work; /* Work handler */
909 int drop_rate;
910 int drop_counter;
911 atomic_t dropentry;
912 /* locks in ctl.c */
913 spinlock_t dropentry_lock; /* drop entry handling */
914 spinlock_t droppacket_lock; /* drop packet handling */
915 spinlock_t securetcp_lock; /* state and timeout tables */
2553d064
JA
916
917 /* sys-ctl struct */
918 struct ctl_table_header *sysctl_hdr;
919 struct ctl_table *sysctl_tbl;
f2247fbd
SH
920#endif
921
2553d064
JA
922 /* sysctl variables */
923 int sysctl_amemthresh;
924 int sysctl_am_droprate;
925 int sysctl_drop_entry;
926 int sysctl_drop_packet;
927 int sysctl_secure_tcp;
928#ifdef CONFIG_IP_VS_NFCT
929 int sysctl_conntrack;
930#endif
931 int sysctl_snat_reroute;
932 int sysctl_sync_ver;
f73181c8 933 int sysctl_sync_ports;
1c003b15
PNA
934 int sysctl_sync_qlen_max;
935 int sysctl_sync_sock_size;
2553d064
JA
936 int sysctl_cache_bypass;
937 int sysctl_expire_nodest_conn;
938 int sysctl_expire_quiescent_template;
939 int sysctl_sync_threshold[2];
749c42b6
JA
940 unsigned int sysctl_sync_refresh_period;
941 int sysctl_sync_retries;
2553d064 942 int sysctl_nat_icmp_send;
3654e611 943 int sysctl_pmtu_disc;
2553d064
JA
944
945 /* ip_vs_lblc */
946 int sysctl_lblc_expiration;
947 struct ctl_table_header *lblc_ctl_header;
948 struct ctl_table *lblc_ctl_table;
949 /* ip_vs_lblcr */
950 int sysctl_lblcr_expiration;
951 struct ctl_table_header *lblcr_ctl_header;
952 struct ctl_table *lblcr_ctl_table;
953 /* ip_vs_est */
954 struct list_head est_list; /* estimator list */
955 spinlock_t est_lock;
956 struct timer_list est_timer; /* Estimation timer */
957 /* ip_vs_sync */
2553d064 958 spinlock_t sync_lock;
f73181c8 959 struct ipvs_master_sync_state *ms;
2553d064 960 spinlock_t sync_buff_lock;
f73181c8
PNA
961 struct task_struct **backup_threads;
962 int threads_mask;
2553d064
JA
963 int send_mesg_maxlen;
964 int recv_mesg_maxlen;
965 volatile int sync_state;
966 volatile int master_syncid;
967 volatile int backup_syncid;
ae1d48b2 968 struct mutex sync_mutex;
2553d064
JA
969 /* multicast interface name */
970 char master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
971 char backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
972 /* net name space ptr */
973 struct net *net; /* Needed by timer routines */
974};
1da177e4 975
59e0350e
SH
976#define DEFAULT_SYNC_THRESHOLD 3
977#define DEFAULT_SYNC_PERIOD 50
7532e8d4 978#define DEFAULT_SYNC_VER 1
749c42b6
JA
979#define DEFAULT_SYNC_REFRESH_PERIOD (0U * HZ)
980#define DEFAULT_SYNC_RETRIES 0
1c003b15
PNA
981#define IPVS_SYNC_WAKEUP_RATE 8
982#define IPVS_SYNC_QLEN_MAX (IPVS_SYNC_WAKEUP_RATE * 4)
983#define IPVS_SYNC_SEND_DELAY (HZ / 50)
984#define IPVS_SYNC_CHECK_PERIOD HZ
749c42b6 985#define IPVS_SYNC_FLUSH_TIME (HZ * 2)
f73181c8 986#define IPVS_SYNC_PORTS_MAX (1 << 6)
59e0350e
SH
987
988#ifdef CONFIG_SYSCTL
989
990static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
991{
992 return ipvs->sysctl_sync_threshold[0];
993}
994
995static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
996{
749c42b6
JA
997 return ACCESS_ONCE(ipvs->sysctl_sync_threshold[1]);
998}
999
1000static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
1001{
1002 return ACCESS_ONCE(ipvs->sysctl_sync_refresh_period);
1003}
1004
1005static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
1006{
1007 return ipvs->sysctl_sync_retries;
59e0350e
SH
1008}
1009
7532e8d4
SH
1010static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
1011{
1012 return ipvs->sysctl_sync_ver;
1013}
1014
f73181c8
PNA
1015static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
1016{
1017 return ACCESS_ONCE(ipvs->sysctl_sync_ports);
1018}
1019
1c003b15
PNA
1020static inline int sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
1021{
1022 return ipvs->sysctl_sync_qlen_max;
1023}
1024
1025static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
1026{
1027 return ipvs->sysctl_sync_sock_size;
1028}
1029
3654e611
JA
1030static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
1031{
1032 return ipvs->sysctl_pmtu_disc;
1033}
1034
59e0350e
SH
1035#else
1036
1037static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
1038{
1039 return DEFAULT_SYNC_THRESHOLD;
1040}
1041
1042static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
1043{
1044 return DEFAULT_SYNC_PERIOD;
1045}
1046
749c42b6
JA
1047static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
1048{
1049 return DEFAULT_SYNC_REFRESH_PERIOD;
1050}
1051
1052static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
1053{
1054 return DEFAULT_SYNC_RETRIES & 3;
1055}
1056
7532e8d4
SH
1057static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
1058{
1059 return DEFAULT_SYNC_VER;
1060}
1061
f73181c8
PNA
1062static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
1063{
1064 return 1;
1065}
1066
1c003b15
PNA
1067static inline int sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
1068{
1069 return IPVS_SYNC_QLEN_MAX;
1070}
1071
1072static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
1073{
1074 return 0;
1075}
1076
3654e611
JA
1077static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
1078{
1079 return 1;
1080}
1081
59e0350e
SH
1082#endif
1083
1da177e4
LT
1084/*
1085 * IPVS core functions
1086 * (from ip_vs_core.c)
1087 */
95c96174 1088extern const char *ip_vs_proto_name(unsigned int proto);
1da177e4 1089extern void ip_vs_init_hash_table(struct list_head *table, int rows);
afdd6140 1090#define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
1da177e4 1091
1da177e4
LT
1092#define IP_VS_APP_TYPE_FTP 1
1093
1094/*
1095 * ip_vs_conn handling functions
1096 * (from ip_vs_conn.c)
1097 */
1098
1da177e4
LT
1099enum {
1100 IP_VS_DIR_INPUT = 0,
1101 IP_VS_DIR_OUTPUT,
1102 IP_VS_DIR_INPUT_ONLY,
1103 IP_VS_DIR_LAST,
1104};
1105
6e67e586 1106static inline void ip_vs_conn_fill_param(struct net *net, int af, int protocol,
f11017ec
SH
1107 const union nf_inet_addr *caddr,
1108 __be16 cport,
1109 const union nf_inet_addr *vaddr,
1110 __be16 vport,
1111 struct ip_vs_conn_param *p)
1112{
6e67e586 1113 p->net = net;
f11017ec
SH
1114 p->af = af;
1115 p->protocol = protocol;
1116 p->caddr = caddr;
1117 p->cport = cport;
1118 p->vaddr = vaddr;
1119 p->vport = vport;
85999283
SH
1120 p->pe = NULL;
1121 p->pe_data = NULL;
f11017ec 1122}
28364a59 1123
f11017ec
SH
1124struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
1125struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
28364a59 1126
5c0d2374 1127struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
5c0d2374
SH
1128 const struct ip_vs_iphdr *iph,
1129 unsigned int proto_off,
1130 int inverse);
1131
f11017ec 1132struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
1da177e4 1133
5c0d2374 1134struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
5c0d2374
SH
1135 const struct ip_vs_iphdr *iph,
1136 unsigned int proto_off,
1137 int inverse);
1138
1da177e4
LT
1139/* put back the conn without restarting its timer */
1140static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
1141{
1142 atomic_dec(&cp->refcnt);
1143}
1144extern void ip_vs_conn_put(struct ip_vs_conn *cp);
014d730d 1145extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
1da177e4 1146
f11017ec
SH
1147struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p,
1148 const union nf_inet_addr *daddr,
95c96174 1149 __be16 dport, unsigned int flags,
0e051e68 1150 struct ip_vs_dest *dest, __u32 fwmark);
1da177e4
LT
1151extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
1152
1153extern const char * ip_vs_state_name(__u16 proto, int state);
1154
4a85b96c 1155extern void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp);
1da177e4 1156extern int ip_vs_check_template(struct ip_vs_conn *ct);
f6340ee0 1157extern void ip_vs_random_dropentry(struct net *net);
1da177e4
LT
1158extern int ip_vs_conn_init(void);
1159extern void ip_vs_conn_cleanup(void);
1160
1161static inline void ip_vs_control_del(struct ip_vs_conn *cp)
1162{
1163 struct ip_vs_conn *ctl_cp = cp->control;
1164 if (!ctl_cp) {
cfc78c5a
JV
1165 IP_VS_ERR_BUF("request control DEL for uncontrolled: "
1166 "%s:%d to %s:%d\n",
1167 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1168 ntohs(cp->cport),
1169 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
1170 ntohs(cp->vport));
1171
1da177e4
LT
1172 return;
1173 }
1174
cfc78c5a
JV
1175 IP_VS_DBG_BUF(7, "DELeting control for: "
1176 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
1177 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1178 ntohs(cp->cport),
1179 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
1180 ntohs(ctl_cp->cport));
1da177e4
LT
1181
1182 cp->control = NULL;
1183 if (atomic_read(&ctl_cp->n_control) == 0) {
cfc78c5a
JV
1184 IP_VS_ERR_BUF("BUG control DEL with n=0 : "
1185 "%s:%d to %s:%d\n",
1186 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1187 ntohs(cp->cport),
1188 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
1189 ntohs(cp->vport));
1190
1da177e4
LT
1191 return;
1192 }
1193 atomic_dec(&ctl_cp->n_control);
1194}
1195
1196static inline void
1197ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
1198{
1199 if (cp->control) {
cfc78c5a
JV
1200 IP_VS_ERR_BUF("request control ADD for already controlled: "
1201 "%s:%d to %s:%d\n",
1202 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1203 ntohs(cp->cport),
1204 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
1205 ntohs(cp->vport));
1206
1da177e4
LT
1207 ip_vs_control_del(cp);
1208 }
1209
cfc78c5a
JV
1210 IP_VS_DBG_BUF(7, "ADDing control for: "
1211 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
1212 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1213 ntohs(cp->cport),
1214 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
1215 ntohs(ctl_cp->cport));
1da177e4
LT
1216
1217 cp->control = ctl_cp;
1218 atomic_inc(&ctl_cp->n_control);
1219}
1220
7a4f0761
HS
1221/*
1222 * IPVS netns init & cleanup functions
1223 */
503cf15a
HS
1224extern int ip_vs_estimator_net_init(struct net *net);
1225extern int ip_vs_control_net_init(struct net *net);
1226extern int ip_vs_protocol_net_init(struct net *net);
1227extern int ip_vs_app_net_init(struct net *net);
1228extern int ip_vs_conn_net_init(struct net *net);
1229extern int ip_vs_sync_net_init(struct net *net);
1230extern void ip_vs_conn_net_cleanup(struct net *net);
1231extern void ip_vs_app_net_cleanup(struct net *net);
1232extern void ip_vs_protocol_net_cleanup(struct net *net);
1233extern void ip_vs_control_net_cleanup(struct net *net);
1234extern void ip_vs_estimator_net_cleanup(struct net *net);
1235extern void ip_vs_sync_net_cleanup(struct net *net);
1236extern void ip_vs_service_net_cleanup(struct net *net);
1da177e4
LT
1237
1238/*
1239 * IPVS application functions
1240 * (from ip_vs_app.c)
1241 */
1242#define IP_VS_APP_MAX_PORTS 8
be97fdb5
JA
1243extern struct ip_vs_app *register_ip_vs_app(struct net *net,
1244 struct ip_vs_app *app);
ab8a5e84 1245extern void unregister_ip_vs_app(struct net *net, struct ip_vs_app *app);
1da177e4
LT
1246extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1247extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
ab8a5e84
HS
1248extern int register_ip_vs_app_inc(struct net *net, struct ip_vs_app *app,
1249 __u16 proto, __u16 port);
1da177e4
LT
1250extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
1251extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
1252
3db05fea
HX
1253extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
1254extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
1da177e4 1255
8be67a66
SH
1256void ip_vs_bind_pe(struct ip_vs_service *svc, struct ip_vs_pe *pe);
1257void ip_vs_unbind_pe(struct ip_vs_service *svc);
1258int register_ip_vs_pe(struct ip_vs_pe *pe);
1259int unregister_ip_vs_pe(struct ip_vs_pe *pe);
e9e5eee8 1260struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
fe5e7a1e 1261struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
e9e5eee8 1262
69e7dae4
PG
1263/*
1264 * Use a #define to avoid all of module.h just for these trivial ops
1265 */
1266#define ip_vs_pe_get(pe) \
1267 if (pe && pe->module) \
e9e5eee8 1268 __module_get(pe->module);
e9e5eee8 1269
69e7dae4
PG
1270#define ip_vs_pe_put(pe) \
1271 if (pe && pe->module) \
e9e5eee8 1272 module_put(pe->module);
1da177e4
LT
1273
1274/*
1275 * IPVS protocol functions (from ip_vs_proto.c)
1276 */
1277extern int ip_vs_protocol_init(void);
1278extern void ip_vs_protocol_cleanup(void);
9330419d 1279extern void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
1da177e4
LT
1280extern int *ip_vs_create_timeout_table(int *table, int size);
1281extern int
36cbd3dc
JE
1282ip_vs_set_state_timeout(int *table, int num, const char *const *names,
1283 const char *name, int to);
1da177e4 1284extern void
0d79641a
JA
1285ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
1286 const struct sk_buff *skb,
1da177e4
LT
1287 int offset, const char *msg);
1288
1289extern struct ip_vs_protocol ip_vs_protocol_tcp;
1290extern struct ip_vs_protocol ip_vs_protocol_udp;
1291extern struct ip_vs_protocol ip_vs_protocol_icmp;
1292extern struct ip_vs_protocol ip_vs_protocol_esp;
1293extern struct ip_vs_protocol ip_vs_protocol_ah;
2906f66a 1294extern struct ip_vs_protocol ip_vs_protocol_sctp;
1da177e4
LT
1295
1296/*
1297 * Registering/unregistering scheduler functions
1298 * (from ip_vs_sched.c)
1299 */
1300extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
1301extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
1302extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
1303 struct ip_vs_scheduler *scheduler);
1304extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
1305extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
1306extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
1307extern struct ip_vs_conn *
190ecd27 1308ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
9330419d 1309 struct ip_vs_proto_data *pd, int *ignored);
1da177e4 1310extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
9330419d 1311 struct ip_vs_proto_data *pd);
1da177e4 1312
41ac51ee
PS
1313extern void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
1314
1da177e4
LT
1315
1316/*
1317 * IPVS control data and functions (from ip_vs_ctl.c)
1318 */
1da177e4 1319extern struct ip_vs_stats ip_vs_stats;
b880c1f0 1320extern int sysctl_ip_vs_sync_ver;
1da177e4
LT
1321
1322extern struct ip_vs_service *
fc723250 1323ip_vs_service_get(struct net *net, int af, __u32 fwmark, __u16 protocol,
3c2e0505 1324 const union nf_inet_addr *vaddr, __be16 vport);
1da177e4
LT
1325
1326static inline void ip_vs_service_put(struct ip_vs_service *svc)
1327{
1328 atomic_dec(&svc->usecnt);
1329}
1330
1331extern struct ip_vs_dest *
fc723250 1332ip_vs_lookup_real_service(struct net *net, int af, __u16 protocol,
7937df15
JV
1333 const union nf_inet_addr *daddr, __be16 dport);
1334
1da177e4
LT
1335extern int ip_vs_use_count_inc(void);
1336extern void ip_vs_use_count_dec(void);
8537de8a
HS
1337extern int ip_vs_register_nl_ioctl(void);
1338extern void ip_vs_unregister_nl_ioctl(void);
1da177e4
LT
1339extern int ip_vs_control_init(void);
1340extern void ip_vs_control_cleanup(void);
1e356f9c 1341extern struct ip_vs_dest *
fc723250
HS
1342ip_vs_find_dest(struct net *net, int af, const union nf_inet_addr *daddr,
1343 __be16 dport, const union nf_inet_addr *vaddr, __be16 vport,
52793dbe 1344 __u16 protocol, __u32 fwmark, __u32 flags);
1e356f9c 1345extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
1da177e4
LT
1346
1347
1348/*
1349 * IPVS sync daemon data and function prototypes
1350 * (from ip_vs_sync.c)
1351 */
f131315f
HS
1352extern int start_sync_thread(struct net *net, int state, char *mcast_ifn,
1353 __u8 syncid);
1354extern int stop_sync_thread(struct net *net, int state);
749c42b6 1355extern void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp, int pkts);
1da177e4
LT
1356
1357
1358/*
1359 * IPVS rate estimator prototypes (from ip_vs_est.c)
1360 */
6ef757f9
JA
1361extern void ip_vs_start_estimator(struct net *net, struct ip_vs_stats *stats);
1362extern void ip_vs_stop_estimator(struct net *net, struct ip_vs_stats *stats);
1da177e4 1363extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
ea9f22cc
JA
1364extern void ip_vs_read_estimator(struct ip_vs_stats_user *dst,
1365 struct ip_vs_stats *stats);
1da177e4
LT
1366
1367/*
1368 * Various IPVS packet transmitters (from ip_vs_xmit.c)
1369 */
1370extern int ip_vs_null_xmit
1371(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1372extern int ip_vs_bypass_xmit
1373(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1374extern int ip_vs_nat_xmit
1375(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1376extern int ip_vs_tunnel_xmit
1377(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1378extern int ip_vs_dr_xmit
1379(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1380extern int ip_vs_icmp_xmit
c92f5ca2
JA
1381(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
1382 int offset, unsigned int hooknum);
1da177e4
LT
1383extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
1384
b3cdd2a7
JV
1385#ifdef CONFIG_IP_VS_IPV6
1386extern int ip_vs_bypass_xmit_v6
1387(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1388extern int ip_vs_nat_xmit_v6
1389(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1390extern int ip_vs_tunnel_xmit_v6
1391(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1392extern int ip_vs_dr_xmit_v6
1393(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1394extern int ip_vs_icmp_xmit_v6
1395(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
c92f5ca2 1396 int offset, unsigned int hooknum);
b3cdd2a7 1397#endif
1da177e4 1398
3a1bbf18 1399#ifdef CONFIG_SYSCTL
1da177e4
LT
1400/*
1401 * This is a simple mechanism to ignore packets when
1402 * we are loaded. Just set ip_vs_drop_rate to 'n' and
1403 * we start to drop 1/rate of the packets
1404 */
1da177e4 1405
a0840e2e 1406static inline int ip_vs_todrop(struct netns_ipvs *ipvs)
1da177e4 1407{
a0840e2e
HS
1408 if (!ipvs->drop_rate)
1409 return 0;
1410 if (--ipvs->drop_counter > 0)
1411 return 0;
1412 ipvs->drop_counter = ipvs->drop_rate;
1da177e4
LT
1413 return 1;
1414}
3a1bbf18
SH
1415#else
1416static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; }
1417#endif
1da177e4
LT
1418
1419/*
1420 * ip_vs_fwd_tag returns the forwarding tag of the connection
1421 */
1422#define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
1423
732db659 1424static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
1da177e4
LT
1425{
1426 char fwd;
1427
1428 switch (IP_VS_FWD_METHOD(cp)) {
1429 case IP_VS_CONN_F_MASQ:
1430 fwd = 'M'; break;
1431 case IP_VS_CONN_F_LOCALNODE:
1432 fwd = 'L'; break;
1433 case IP_VS_CONN_F_TUNNEL:
1434 fwd = 'T'; break;
1435 case IP_VS_CONN_F_DROUTE:
1436 fwd = 'R'; break;
1437 case IP_VS_CONN_F_BYPASS:
1438 fwd = 'B'; break;
1439 default:
1440 fwd = '?'; break;
1441 }
1442 return fwd;
1443}
1444
1da177e4 1445extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
b3cdd2a7
JV
1446 struct ip_vs_conn *cp, int dir);
1447
1448#ifdef CONFIG_IP_VS_IPV6
1449extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
1450 struct ip_vs_conn *cp, int dir);
1451#endif
1da177e4 1452
b1550f22 1453extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
1da177e4 1454
f9214b26 1455static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
1da177e4 1456{
f9214b26 1457 __be32 diff[2] = { ~old, new };
1da177e4 1458
07f0757a 1459 return csum_partial(diff, sizeof(diff), oldsum);
f9214b26
AV
1460}
1461
0bbdd42b
JV
1462#ifdef CONFIG_IP_VS_IPV6
1463static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
1464 __wsum oldsum)
1465{
1466 __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
1467 new[3], new[2], new[1], new[0] };
1468
07f0757a 1469 return csum_partial(diff, sizeof(diff), oldsum);
0bbdd42b
JV
1470}
1471#endif
1472
f9214b26
AV
1473static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
1474{
1475 __be16 diff[2] = { ~old, new };
1476
07f0757a 1477 return csum_partial(diff, sizeof(diff), oldsum);
1da177e4
LT
1478}
1479
cf356d69
JA
1480/*
1481 * Forget current conntrack (unconfirmed) and attach notrack entry
1482 */
1483static inline void ip_vs_notrack(struct sk_buff *skb)
1484{
1485#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1486 enum ip_conntrack_info ctinfo;
06b69390 1487 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
cf356d69
JA
1488
1489 if (!ct || !nf_ct_is_untracked(ct)) {
9e33ce45 1490 nf_conntrack_put(skb->nfct);
cf356d69
JA
1491 skb->nfct = &nf_ct_untracked_get()->ct_general;
1492 skb->nfctinfo = IP_CT_NEW;
1493 nf_conntrack_get(skb->nfct);
1494 }
1495#endif
1496}
1497
f4bc17cd
JA
1498#ifdef CONFIG_IP_VS_NFCT
1499/*
1500 * Netfilter connection tracking
1501 * (from ip_vs_nfct.c)
1502 */
a0840e2e 1503static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
f4bc17cd 1504{
a4e2f5a7 1505#ifdef CONFIG_SYSCTL
a0840e2e 1506 return ipvs->sysctl_conntrack;
a4e2f5a7
SH
1507#else
1508 return 0;
1509#endif
f4bc17cd
JA
1510}
1511
6523ce15
JA
1512extern void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
1513 int outin);
3c2de2ae 1514extern int ip_vs_confirm_conntrack(struct sk_buff *skb);
f4bc17cd
JA
1515extern void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
1516 struct ip_vs_conn *cp, u_int8_t proto,
1517 const __be16 port, int from_rs);
1518extern void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
1519
1520#else
1521
a0840e2e 1522static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
f4bc17cd
JA
1523{
1524 return 0;
1525}
1526
1527static inline void ip_vs_update_conntrack(struct sk_buff *skb,
1528 struct ip_vs_conn *cp, int outin)
1529{
1530}
1531
e23ebf0f 1532static inline int ip_vs_confirm_conntrack(struct sk_buff *skb)
f4bc17cd
JA
1533{
1534 return NF_ACCEPT;
1535}
1536
1537static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
1538{
1539}
1540/* CONFIG_IP_VS_NFCT */
1541#endif
6523ce15 1542
b552f7e3
CG
1543static inline unsigned int
1544ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
1545{
1546 /*
1547 * We think the overhead of processing active connections is 256
1548 * times higher than that of inactive connections in average. (This
1549 * 256 times might not be accurate, we will change it later) We
1550 * use the following formula to estimate the overhead now:
1551 * dest->activeconns*256 + dest->inactconns
1552 */
1553 return (atomic_read(&dest->activeconns) << 8) +
1554 atomic_read(&dest->inactconns);
1555}
1556
bc4768eb 1557#endif /* _NET_IP_VS_H */