28
25
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
29
26
#include <net/netfilter/nf_conntrack.h>
28
#include <net/net_namespace.h> /* Netw namespace */
31
* Generic access of ipvs struct
33
static inline struct netns_ipvs *net_ipvs(struct net* net)
38
* Get net ptr from skb in traffic cases
39
* use skb_sknet when call is from userland (ioctl or netlink)
41
static inline struct net *skb_net(const struct sk_buff *skb)
44
#ifdef CONFIG_IP_VS_DEBUG
46
* This is used for debug only.
47
* Start with the most likely hit
50
if (likely(skb->dev && skb->dev->nd_net))
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",
56
if (likely(skb->sk && skb->sk->sk_net))
57
return sock_net(skb->sk);
58
pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
62
return dev_net(skb->dev ? : skb_dst(skb)->dev);
69
static inline struct net *skb_sknet(const struct sk_buff *skb)
72
#ifdef CONFIG_IP_VS_DEBUG
73
/* Start with the most likely hit */
74
if (likely(skb->sk && skb->sk->sk_net))
75
return sock_net(skb->sk);
76
WARN(skb->dev, "Maybe skb_net should be used instead in %s() line:%d\n",
78
if (likely(skb->dev && skb->dev->nd_net))
79
return dev_net(skb->dev);
80
pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
84
return sock_net(skb->sk);
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.
94
static inline struct net *seq_file_single_net(struct seq_file *seq)
97
return (struct net *)seq->private;
32
103
/* Connections' size value needed by ip_vs_ctl.c */
33
104
extern int ip_vs_conn_tab_size;
300
atomic_t appcnt; /* counter of proto app incs */
301
int *timeout_table; /* protocol timeout table */
303
391
void (*init)(struct ip_vs_protocol *pp);
305
393
void (*exit)(struct ip_vs_protocol *pp);
395
void (*init_netns)(struct net *net, struct ip_vs_proto_data *pd);
397
void (*exit_netns)(struct net *net, struct ip_vs_proto_data *pd);
307
399
int (*conn_schedule)(int af, struct sk_buff *skb,
308
struct ip_vs_protocol *pp,
400
struct ip_vs_proto_data *pd,
309
401
int *verdict, struct ip_vs_conn **cpp);
311
403
struct ip_vs_conn *
312
404
(*conn_in_get)(int af,
313
405
const struct sk_buff *skb,
314
struct ip_vs_protocol *pp,
315
406
const struct ip_vs_iphdr *iph,
316
407
unsigned int proto_off,
351
441
const char *msg);
353
void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
355
int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
358
extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
443
void (*timeout_change)(struct ip_vs_proto_data *pd, int flags);
447
* protocol data per netns
449
struct ip_vs_proto_data {
450
struct ip_vs_proto_data *next;
451
struct ip_vs_protocol *pp;
452
int *timeout_table; /* protocol timeout table */
453
atomic_t appcnt; /* counter of proto app incs. */
454
struct tcp_states_t *tcp_state_table;
457
extern struct ip_vs_protocol *ip_vs_proto_get(unsigned short proto);
458
extern struct ip_vs_proto_data *ip_vs_proto_data_get(struct net *net,
459
unsigned short proto);
360
461
struct ip_vs_conn_param {
361
463
const union nf_inet_addr *caddr;
362
464
const union nf_inet_addr *vaddr;
374
476
* IP_VS structure allocated for each dynamically scheduled connection
376
478
struct ip_vs_conn {
377
struct list_head c_list; /* hashed list heads */
479
struct hlist_node c_list; /* hashed list heads */
481
struct net *net; /* Name space */
379
483
/* Protocol, addresses and port numbers */
380
u16 af; /* address family */
381
union nf_inet_addr caddr; /* client address */
382
union nf_inet_addr vaddr; /* virtual address */
383
union nf_inet_addr daddr; /* destination address */
384
volatile __u32 flags; /* status flags */
484
u16 af; /* address family */
488
__u32 fwmark; /* Fire wall mark from skb */
489
union nf_inet_addr caddr; /* client address */
490
union nf_inet_addr vaddr; /* virtual address */
491
union nf_inet_addr daddr; /* destination address */
492
volatile __u32 flags; /* status flags */
388
493
__u16 protocol; /* Which protocol (TCP/UDP) */
390
495
/* counter and timer */
651
783
void (*timeout_change)(struct ip_vs_app *app, int flags);
786
/* IPVS in network namespace */
788
int gen; /* Generation */
789
int enable; /* enable like nf_hooks do */
791
* Hash table: for real service lookups
793
#define IP_VS_RTAB_BITS 4
794
#define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
795
#define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
797
struct list_head rs_table[IP_VS_RTAB_SIZE];
799
struct list_head app_list;
801
struct ip_vs_app *ftp_app;
803
#define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
804
struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE];
805
/* ip_vs_proto_tcp */
806
#ifdef CONFIG_IP_VS_PROTO_TCP
807
#define TCP_APP_TAB_BITS 4
808
#define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
809
#define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
810
struct list_head tcp_apps[TCP_APP_TAB_SIZE];
811
spinlock_t tcp_app_lock;
813
/* ip_vs_proto_udp */
814
#ifdef CONFIG_IP_VS_PROTO_UDP
815
#define UDP_APP_TAB_BITS 4
816
#define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
817
#define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
818
struct list_head udp_apps[UDP_APP_TAB_SIZE];
819
spinlock_t udp_app_lock;
821
/* ip_vs_proto_sctp */
822
#ifdef CONFIG_IP_VS_PROTO_SCTP
823
#define SCTP_APP_TAB_BITS 4
824
#define SCTP_APP_TAB_SIZE (1 << SCTP_APP_TAB_BITS)
825
#define SCTP_APP_TAB_MASK (SCTP_APP_TAB_SIZE - 1)
826
/* Hash table for SCTP application incarnations */
827
struct list_head sctp_apps[SCTP_APP_TAB_SIZE];
828
spinlock_t sctp_app_lock;
831
atomic_t conn_count; /* connection counter */
834
struct ip_vs_stats tot_stats; /* Statistics & est. */
836
int num_services; /* no of virtual services */
838
rwlock_t rs_lock; /* real services table */
839
/* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
840
struct lock_class_key ctl_key; /* ctl_mutex debuging */
841
/* Trash for destinations */
842
struct list_head dest_trash;
843
/* Service counters */
844
atomic_t ftpsvc_counter;
845
atomic_t nullsvc_counter;
848
/* 1/rate drop and drop-entry variables */
849
struct delayed_work defense_work; /* Work handler */
854
spinlock_t dropentry_lock; /* drop entry handling */
855
spinlock_t droppacket_lock; /* drop packet handling */
856
spinlock_t securetcp_lock; /* state and timeout tables */
859
struct ctl_table_header *sysctl_hdr;
860
struct ctl_table *sysctl_tbl;
863
/* sysctl variables */
864
int sysctl_amemthresh;
865
int sysctl_am_droprate;
866
int sysctl_drop_entry;
867
int sysctl_drop_packet;
868
int sysctl_secure_tcp;
869
#ifdef CONFIG_IP_VS_NFCT
870
int sysctl_conntrack;
872
int sysctl_snat_reroute;
874
int sysctl_cache_bypass;
875
int sysctl_expire_nodest_conn;
876
int sysctl_expire_quiescent_template;
877
int sysctl_sync_threshold[2];
878
int sysctl_nat_icmp_send;
881
int sysctl_lblc_expiration;
882
struct ctl_table_header *lblc_ctl_header;
883
struct ctl_table *lblc_ctl_table;
885
int sysctl_lblcr_expiration;
886
struct ctl_table_header *lblcr_ctl_header;
887
struct ctl_table *lblcr_ctl_table;
889
struct list_head est_list; /* estimator list */
891
struct timer_list est_timer; /* Estimation timer */
893
struct list_head sync_queue;
894
spinlock_t sync_lock;
895
struct ip_vs_sync_buff *sync_buff;
896
spinlock_t sync_buff_lock;
897
struct sockaddr_in sync_mcast_addr;
898
struct task_struct *master_thread;
899
struct task_struct *backup_thread;
900
int send_mesg_maxlen;
901
int recv_mesg_maxlen;
902
volatile int sync_state;
903
volatile int master_syncid;
904
volatile int backup_syncid;
905
/* multicast interface name */
906
char master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
907
char backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
908
/* net name space ptr */
909
struct net *net; /* Needed by timer routines */
912
#define DEFAULT_SYNC_THRESHOLD 3
913
#define DEFAULT_SYNC_PERIOD 50
914
#define DEFAULT_SYNC_VER 1
918
static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
920
return ipvs->sysctl_sync_threshold[0];
923
static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
925
return ipvs->sysctl_sync_threshold[1];
928
static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
930
return ipvs->sysctl_sync_ver;
935
static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
937
return DEFAULT_SYNC_THRESHOLD;
940
static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
942
return DEFAULT_SYNC_PERIOD;
945
static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
947
return DEFAULT_SYNC_VER;
656
953
* IPVS core functions
719
1015
struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p,
720
1016
const union nf_inet_addr *daddr,
721
1017
__be16 dport, unsigned flags,
722
struct ip_vs_dest *dest);
1018
struct ip_vs_dest *dest, __u32 fwmark);
723
1019
extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
725
1021
extern const char * ip_vs_state_name(__u16 proto, int state);
727
extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
1023
extern void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp);
728
1024
extern int ip_vs_check_template(struct ip_vs_conn *ct);
729
extern void ip_vs_random_dropentry(void);
1025
extern void ip_vs_random_dropentry(struct net *net);
730
1026
extern int ip_vs_conn_init(void);
731
1027
extern void ip_vs_conn_cleanup(void);
790
1086
atomic_inc(&ctl_cp->n_control);
1090
* IPVS netns init & cleanup functions
1092
extern int __ip_vs_estimator_init(struct net *net);
1093
extern int __ip_vs_control_init(struct net *net);
1094
extern int __ip_vs_protocol_init(struct net *net);
1095
extern int __ip_vs_app_init(struct net *net);
1096
extern int __ip_vs_conn_init(struct net *net);
1097
extern int __ip_vs_sync_init(struct net *net);
1098
extern void __ip_vs_conn_cleanup(struct net *net);
1099
extern void __ip_vs_app_cleanup(struct net *net);
1100
extern void __ip_vs_protocol_cleanup(struct net *net);
1101
extern void __ip_vs_control_cleanup(struct net *net);
1102
extern void __ip_vs_estimator_cleanup(struct net *net);
1103
extern void __ip_vs_sync_cleanup(struct net *net);
1104
extern void __ip_vs_service_cleanup(struct net *net);
795
1107
* IPVS application functions
796
1108
* (from ip_vs_app.c)
798
1110
#define IP_VS_APP_MAX_PORTS 8
799
extern int register_ip_vs_app(struct ip_vs_app *app);
800
extern void unregister_ip_vs_app(struct ip_vs_app *app);
1111
extern int register_ip_vs_app(struct net *net, struct ip_vs_app *app);
1112
extern void unregister_ip_vs_app(struct net *net, struct ip_vs_app *app);
801
1113
extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
802
1114
extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
804
register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
1115
extern int register_ip_vs_app_inc(struct net *net, struct ip_vs_app *app,
1116
__u16 proto, __u16 port);
805
1117
extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
806
1118
extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
814
1126
void ip_vs_unbind_pe(struct ip_vs_service *svc);
815
1127
int register_ip_vs_pe(struct ip_vs_pe *pe);
816
1128
int unregister_ip_vs_pe(struct ip_vs_pe *pe);
817
extern struct ip_vs_pe *ip_vs_pe_get(const char *name);
818
extern void ip_vs_pe_put(struct ip_vs_pe *pe);
1129
struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
1130
struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
1132
static inline void ip_vs_pe_get(const struct ip_vs_pe *pe)
1134
if (pe && pe->module)
1135
__module_get(pe->module);
1138
static inline void ip_vs_pe_put(const struct ip_vs_pe *pe)
1140
if (pe && pe->module)
1141
module_put(pe->module);
821
1145
* IPVS protocol functions (from ip_vs_proto.c)
823
1147
extern int ip_vs_protocol_init(void);
824
1148
extern void ip_vs_protocol_cleanup(void);
825
extern void ip_vs_protocol_timeout_change(int flags);
1149
extern void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
826
1150
extern int *ip_vs_create_timeout_table(int *table, int size);
828
1152
ip_vs_set_state_timeout(int *table, int num, const char *const *names,
852
1176
extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
853
1177
extern struct ip_vs_conn *
854
1178
ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
855
struct ip_vs_protocol *pp, int *ignored);
1179
struct ip_vs_proto_data *pd, int *ignored);
856
1180
extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
857
struct ip_vs_protocol *pp);
1181
struct ip_vs_proto_data *pd);
1183
extern void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
861
1187
* IPVS control data and functions (from ip_vs_ctl.c)
863
extern int sysctl_ip_vs_cache_bypass;
864
extern int sysctl_ip_vs_expire_nodest_conn;
865
extern int sysctl_ip_vs_expire_quiescent_template;
866
extern int sysctl_ip_vs_sync_threshold[2];
867
extern int sysctl_ip_vs_nat_icmp_send;
868
extern int sysctl_ip_vs_conntrack;
869
extern int sysctl_ip_vs_snat_reroute;
870
1189
extern struct ip_vs_stats ip_vs_stats;
871
1190
extern const struct ctl_path net_vs_ctl_path[];
1191
extern int sysctl_ip_vs_sync_ver;
1193
extern void ip_vs_sync_switch_mode(struct net *net, int mode);
873
1194
extern struct ip_vs_service *
874
ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
1195
ip_vs_service_get(struct net *net, int af, __u32 fwmark, __u16 protocol,
875
1196
const union nf_inet_addr *vaddr, __be16 vport);
877
1198
static inline void ip_vs_service_put(struct ip_vs_service *svc)
888
1209
extern int ip_vs_control_init(void);
889
1210
extern void ip_vs_control_cleanup(void);
890
1211
extern struct ip_vs_dest *
891
ip_vs_find_dest(int af, const union nf_inet_addr *daddr, __be16 dport,
892
const union nf_inet_addr *vaddr, __be16 vport, __u16 protocol);
1212
ip_vs_find_dest(struct net *net, int af, const union nf_inet_addr *daddr,
1213
__be16 dport, const union nf_inet_addr *vaddr, __be16 vport,
1214
__u16 protocol, __u32 fwmark);
893
1215
extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
897
1219
* IPVS sync daemon data and function prototypes
898
1220
* (from ip_vs_sync.c)
900
extern volatile int ip_vs_sync_state;
901
extern volatile int ip_vs_master_syncid;
902
extern volatile int ip_vs_backup_syncid;
903
extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
904
extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
905
extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
906
extern int stop_sync_thread(int state);
907
extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
1222
extern int start_sync_thread(struct net *net, int state, char *mcast_ifn,
1224
extern int stop_sync_thread(struct net *net, int state);
1225
extern void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp);
1226
extern int ip_vs_sync_init(void);
1227
extern void ip_vs_sync_cleanup(void);
944
1267
(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
945
1268
extern int ip_vs_icmp_xmit_v6
946
1269
(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
1270
int offset, unsigned int hooknum);
1273
#ifdef CONFIG_SYSCTL
951
1275
* This is a simple mechanism to ignore packets when
952
1276
* we are loaded. Just set ip_vs_drop_rate to 'n' and
953
1277
* we start to drop 1/rate of the packets
955
extern int ip_vs_drop_rate;
956
extern int ip_vs_drop_counter;
958
static __inline__ int ip_vs_todrop(void)
1280
static inline int ip_vs_todrop(struct netns_ipvs *ipvs)
960
if (!ip_vs_drop_rate) return 0;
961
if (--ip_vs_drop_counter > 0) return 0;
962
ip_vs_drop_counter = ip_vs_drop_rate;
1282
if (!ipvs->drop_rate)
1284
if (--ipvs->drop_counter > 0)
1286
ipvs->drop_counter = ipvs->drop_rate;
1290
static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; }
967
1294
* ip_vs_fwd_tag returns the forwarding tag of the connection