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