ip_vs.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /*
  2. * IP Virtual Server
  3. * data structure and functionality definitions
  4. */
  5. #ifndef _NET_IP_VS_H
  6. #define _NET_IP_VS_H
  7. #include <linux/ip_vs.h> /* definitions shared with userland */
  8. #include <asm/types.h> /* for __uXX types */
  9. #include <linux/sysctl.h> /* for ctl_path */
  10. #include <linux/list.h> /* for struct list_head */
  11. #include <linux/spinlock.h> /* for struct rwlock_t */
  12. #include <linux/atomic.h> /* for struct atomic_t */
  13. #include <linux/compiler.h>
  14. #include <linux/timer.h>
  15. #include <net/checksum.h>
  16. #include <linux/netfilter.h> /* for union nf_inet_addr */
  17. #include <linux/ip.h>
  18. #include <linux/ipv6.h> /* for struct ipv6hdr */
  19. #include <net/ipv6.h> /* for ipv6_addr_copy */
  20. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  21. #include <net/netfilter/nf_conntrack.h>
  22. #endif
  23. #include <net/net_namespace.h> /* Netw namespace */
  24. /*
  25. * Generic access of ipvs struct
  26. */
  27. static inline struct netns_ipvs *net_ipvs(struct net* net)
  28. {
  29. return net->ipvs;
  30. }
  31. /*
  32. * Get net ptr from skb in traffic cases
  33. * use skb_sknet when call is from userland (ioctl or netlink)
  34. */
  35. static inline struct net *skb_net(const struct sk_buff *skb)
  36. {
  37. #ifdef CONFIG_NET_NS
  38. #ifdef CONFIG_IP_VS_DEBUG
  39. /*
  40. * This is used for debug only.
  41. * Start with the most likely hit
  42. * End with BUG
  43. */
  44. if (likely(skb->dev && skb->dev->nd_net))
  45. return dev_net(skb->dev);
  46. if (skb_dst(skb) && skb_dst(skb)->dev)
  47. return dev_net(skb_dst(skb)->dev);
  48. WARN(skb->sk, "Maybe skb_sknet should be used in %s() at line:%d\n",
  49. __func__, __LINE__);
  50. if (likely(skb->sk && skb->sk->sk_net))
  51. return sock_net(skb->sk);
  52. pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
  53. __func__, __LINE__);
  54. BUG();
  55. #else
  56. return dev_net(skb->dev ? : skb_dst(skb)->dev);
  57. #endif
  58. #else
  59. return &init_net;
  60. #endif
  61. }
  62. static inline struct net *skb_sknet(const struct sk_buff *skb)
  63. {
  64. #ifdef CONFIG_NET_NS
  65. #ifdef CONFIG_IP_VS_DEBUG
  66. /* Start with the most likely hit */
  67. if (likely(skb->sk && skb->sk->sk_net))
  68. return sock_net(skb->sk);
  69. WARN(skb->dev, "Maybe skb_net should be used instead in %s() line:%d\n",
  70. __func__, __LINE__);
  71. if (likely(skb->dev && skb->dev->nd_net))
  72. return dev_net(skb->dev);
  73. pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
  74. __func__, __LINE__);
  75. BUG();
  76. #else
  77. return sock_net(skb->sk);
  78. #endif
  79. #else
  80. return &init_net;
  81. #endif
  82. }
  83. /*
  84. * This one needed for single_open_net since net is stored directly in
  85. * private not as a struct i.e. seq_file_net can't be used.
  86. */
  87. static inline struct net *seq_file_single_net(struct seq_file *seq)
  88. {
  89. #ifdef CONFIG_NET_NS
  90. return (struct net *)seq->private;
  91. #else
  92. return &init_net;
  93. #endif
  94. }
  95. /* Connections' size value needed by ip_vs_ctl.c */
  96. extern int ip_vs_conn_tab_size;
  97. struct ip_vs_iphdr {
  98. int len;
  99. __u8 protocol;
  100. union nf_inet_addr saddr;
  101. union nf_inet_addr daddr;
  102. };
  103. static inline void
  104. ip_vs_fill_iphdr(int af, const void *nh, struct ip_vs_iphdr *iphdr)
  105. {
  106. #ifdef CONFIG_IP_VS_IPV6
  107. if (af == AF_INET6) {
  108. const struct ipv6hdr *iph = nh;
  109. iphdr->len = sizeof(struct ipv6hdr);
  110. iphdr->protocol = iph->nexthdr;
  111. ipv6_addr_copy(&iphdr->saddr.in6, &iph->saddr);
  112. ipv6_addr_copy(&iphdr->daddr.in6, &iph->daddr);
  113. } else
  114. #endif
  115. {
  116. const struct iphdr *iph = nh;
  117. iphdr->len = iph->ihl * 4;
  118. iphdr->protocol = iph->protocol;
  119. iphdr->saddr.ip = iph->saddr;
  120. iphdr->daddr.ip = iph->daddr;
  121. }
  122. }
  123. static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
  124. const union nf_inet_addr *src)
  125. {
  126. #ifdef CONFIG_IP_VS_IPV6
  127. if (af == AF_INET6)
  128. ipv6_addr_copy(&dst->in6, &src->in6);
  129. else
  130. #endif
  131. dst->ip = src->ip;
  132. }
  133. static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
  134. const union nf_inet_addr *b)
  135. {
  136. #ifdef CONFIG_IP_VS_IPV6
  137. if (af == AF_INET6)
  138. return ipv6_addr_equal(&a->in6, &b->in6);
  139. #endif
  140. return a->ip == b->ip;
  141. }
  142. #ifdef CONFIG_IP_VS_DEBUG
  143. #include <linux/net.h>
  144. extern int ip_vs_get_debug_level(void);
  145. static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
  146. const union nf_inet_addr *addr,
  147. int *idx)
  148. {
  149. int len;
  150. #ifdef CONFIG_IP_VS_IPV6
  151. if (af == AF_INET6)
  152. len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6]",
  153. &addr->in6) + 1;
  154. else
  155. #endif
  156. len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
  157. &addr->ip) + 1;
  158. *idx += len;
  159. BUG_ON(*idx > buf_len + 1);
  160. return &buf[*idx - len];
  161. }
  162. #define IP_VS_DBG_BUF(level, msg, ...) \
  163. do { \
  164. char ip_vs_dbg_buf[160]; \
  165. int ip_vs_dbg_idx = 0; \
  166. if (level <= ip_vs_get_debug_level()) \
  167. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  168. } while (0)
  169. #define IP_VS_ERR_BUF(msg...) \
  170. do { \
  171. char ip_vs_dbg_buf[160]; \
  172. int ip_vs_dbg_idx = 0; \
  173. pr_err(msg); \
  174. } while (0)
  175. /* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
  176. #define IP_VS_DBG_ADDR(af, addr) \
  177. ip_vs_dbg_addr(af, ip_vs_dbg_buf, \
  178. sizeof(ip_vs_dbg_buf), addr, \
  179. &ip_vs_dbg_idx)
  180. #define IP_VS_DBG(level, msg, ...) \
  181. do { \
  182. if (level <= ip_vs_get_debug_level()) \
  183. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  184. } while (0)
  185. #define IP_VS_DBG_RL(msg, ...) \
  186. do { \
  187. if (net_ratelimit()) \
  188. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  189. } while (0)
  190. #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) \
  191. do { \
  192. if (level <= ip_vs_get_debug_level()) \
  193. pp->debug_packet(af, pp, skb, ofs, msg); \
  194. } while (0)
  195. #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) \
  196. do { \
  197. if (level <= ip_vs_get_debug_level() && \
  198. net_ratelimit()) \
  199. pp->debug_packet(af, pp, skb, ofs, msg); \
  200. } while (0)
  201. #else /* NO DEBUGGING at ALL */
  202. #define IP_VS_DBG_BUF(level, msg...) do {} while (0)
  203. #define IP_VS_ERR_BUF(msg...) do {} while (0)
  204. #define IP_VS_DBG(level, msg...) do {} while (0)
  205. #define IP_VS_DBG_RL(msg...) do {} while (0)
  206. #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) do {} while (0)
  207. #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) do {} while (0)
  208. #endif
  209. #define IP_VS_BUG() BUG()
  210. #define IP_VS_ERR_RL(msg, ...) \
  211. do { \
  212. if (net_ratelimit()) \
  213. pr_err(msg, ##__VA_ARGS__); \
  214. } while (0)
  215. #ifdef CONFIG_IP_VS_DEBUG
  216. #define EnterFunction(level) \
  217. do { \
  218. if (level <= ip_vs_get_debug_level()) \
  219. printk(KERN_DEBUG \
  220. pr_fmt("Enter: %s, %s line %i\n"), \
  221. __func__, __FILE__, __LINE__); \
  222. } while (0)
  223. #define LeaveFunction(level) \
  224. do { \
  225. if (level <= ip_vs_get_debug_level()) \
  226. printk(KERN_DEBUG \
  227. pr_fmt("Leave: %s, %s line %i\n"), \
  228. __func__, __FILE__, __LINE__); \
  229. } while (0)
  230. #else
  231. #define EnterFunction(level) do {} while (0)
  232. #define LeaveFunction(level) do {} while (0)
  233. #endif
  234. #define IP_VS_WAIT_WHILE(expr) while (expr) { cpu_relax(); }
  235. /*
  236. * The port number of FTP service (in network order).
  237. */
  238. #define FTPPORT cpu_to_be16(21)
  239. #define FTPDATA cpu_to_be16(20)
  240. /*
  241. * TCP State Values
  242. */
  243. enum {
  244. IP_VS_TCP_S_NONE = 0,
  245. IP_VS_TCP_S_ESTABLISHED,
  246. IP_VS_TCP_S_SYN_SENT,
  247. IP_VS_TCP_S_SYN_RECV,
  248. IP_VS_TCP_S_FIN_WAIT,
  249. IP_VS_TCP_S_TIME_WAIT,
  250. IP_VS_TCP_S_CLOSE,
  251. IP_VS_TCP_S_CLOSE_WAIT,
  252. IP_VS_TCP_S_LAST_ACK,
  253. IP_VS_TCP_S_LISTEN,
  254. IP_VS_TCP_S_SYNACK,
  255. IP_VS_TCP_S_LAST
  256. };
  257. /*
  258. * UDP State Values
  259. */
  260. enum {
  261. IP_VS_UDP_S_NORMAL,
  262. IP_VS_UDP_S_LAST,
  263. };
  264. /*
  265. * ICMP State Values
  266. */
  267. enum {
  268. IP_VS_ICMP_S_NORMAL,
  269. IP_VS_ICMP_S_LAST,
  270. };
  271. /*
  272. * SCTP State Values
  273. */
  274. enum ip_vs_sctp_states {
  275. IP_VS_SCTP_S_NONE,
  276. IP_VS_SCTP_S_INIT_CLI,
  277. IP_VS_SCTP_S_INIT_SER,
  278. IP_VS_SCTP_S_INIT_ACK_CLI,
  279. IP_VS_SCTP_S_INIT_ACK_SER,
  280. IP_VS_SCTP_S_ECHO_CLI,
  281. IP_VS_SCTP_S_ECHO_SER,
  282. IP_VS_SCTP_S_ESTABLISHED,
  283. IP_VS_SCTP_S_SHUT_CLI,
  284. IP_VS_SCTP_S_SHUT_SER,
  285. IP_VS_SCTP_S_SHUT_ACK_CLI,
  286. IP_VS_SCTP_S_SHUT_ACK_SER,
  287. IP_VS_SCTP_S_CLOSED,
  288. IP_VS_SCTP_S_LAST
  289. };
  290. /*
  291. * Delta sequence info structure
  292. * Each ip_vs_conn has 2 (output AND input seq. changes).
  293. * Only used in the VS/NAT.
  294. */
  295. struct ip_vs_seq {
  296. __u32 init_seq; /* Add delta from this seq */
  297. __u32 delta; /* Delta in sequence numbers */
  298. __u32 previous_delta; /* Delta in sequence numbers
  299. before last resized pkt */
  300. };
  301. /*
  302. * counters per cpu
  303. */
  304. struct ip_vs_counters {
  305. __u32 conns; /* connections scheduled */
  306. __u32 inpkts; /* incoming packets */
  307. __u32 outpkts; /* outgoing packets */
  308. __u64 inbytes; /* incoming bytes */
  309. __u64 outbytes; /* outgoing bytes */
  310. };
  311. /*
  312. * Stats per cpu
  313. */
  314. struct ip_vs_cpu_stats {
  315. struct ip_vs_counters ustats;
  316. struct u64_stats_sync syncp;
  317. };
  318. /*
  319. * IPVS statistics objects
  320. */
  321. struct ip_vs_estimator {
  322. struct list_head list;
  323. u64 last_inbytes;
  324. u64 last_outbytes;
  325. u32 last_conns;
  326. u32 last_inpkts;
  327. u32 last_outpkts;
  328. u32 cps;
  329. u32 inpps;
  330. u32 outpps;
  331. u32 inbps;
  332. u32 outbps;
  333. };
  334. struct ip_vs_stats {
  335. struct ip_vs_stats_user ustats; /* statistics */
  336. struct ip_vs_estimator est; /* estimator */
  337. struct ip_vs_cpu_stats *cpustats; /* per cpu counters */
  338. spinlock_t lock; /* spin lock */
  339. struct ip_vs_stats_user ustats0; /* reset values */
  340. };
  341. struct dst_entry;
  342. struct iphdr;
  343. struct ip_vs_conn;
  344. struct ip_vs_app;
  345. struct sk_buff;
  346. struct ip_vs_proto_data;
  347. struct ip_vs_protocol {
  348. struct ip_vs_protocol *next;
  349. char *name;
  350. u16 protocol;
  351. u16 num_states;
  352. int dont_defrag;
  353. void (*init)(struct ip_vs_protocol *pp);
  354. void (*exit)(struct ip_vs_protocol *pp);
  355. void (*init_netns)(struct net *net, struct ip_vs_proto_data *pd);
  356. void (*exit_netns)(struct net *net, struct ip_vs_proto_data *pd);
  357. int (*conn_schedule)(int af, struct sk_buff *skb,
  358. struct ip_vs_proto_data *pd,
  359. int *verdict, struct ip_vs_conn **cpp);
  360. struct ip_vs_conn *
  361. (*conn_in_get)(int af,
  362. const struct sk_buff *skb,
  363. const struct ip_vs_iphdr *iph,
  364. unsigned int proto_off,
  365. int inverse);
  366. struct ip_vs_conn *
  367. (*conn_out_get)(int af,
  368. const struct sk_buff *skb,
  369. const struct ip_vs_iphdr *iph,
  370. unsigned int proto_off,
  371. int inverse);
  372. int (*snat_handler)(struct sk_buff *skb,
  373. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  374. int (*dnat_handler)(struct sk_buff *skb,
  375. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  376. int (*csum_check)(int af, struct sk_buff *skb,
  377. struct ip_vs_protocol *pp);
  378. const char *(*state_name)(int state);
  379. void (*state_transition)(struct ip_vs_conn *cp, int direction,
  380. const struct sk_buff *skb,
  381. struct ip_vs_proto_data *pd);
  382. int (*register_app)(struct net *net, struct ip_vs_app *inc);
  383. void (*unregister_app)(struct net *net, struct ip_vs_app *inc);
  384. int (*app_conn_bind)(struct ip_vs_conn *cp);
  385. void (*debug_packet)(int af, struct ip_vs_protocol *pp,
  386. const struct sk_buff *skb,
  387. int offset,
  388. const char *msg);
  389. void (*timeout_change)(struct ip_vs_proto_data *pd, int flags);
  390. };
  391. /*
  392. * protocol data per netns
  393. */
  394. struct ip_vs_proto_data {
  395. struct ip_vs_proto_data *next;
  396. struct ip_vs_protocol *pp;
  397. int *timeout_table; /* protocol timeout table */
  398. atomic_t appcnt; /* counter of proto app incs. */
  399. struct tcp_states_t *tcp_state_table;
  400. };
  401. extern struct ip_vs_protocol *ip_vs_proto_get(unsigned short proto);
  402. extern struct ip_vs_proto_data *ip_vs_proto_data_get(struct net *net,
  403. unsigned short proto);
  404. struct ip_vs_conn_param {
  405. struct net *net;
  406. const union nf_inet_addr *caddr;
  407. const union nf_inet_addr *vaddr;
  408. __be16 cport;
  409. __be16 vport;
  410. __u16 protocol;
  411. u16 af;
  412. const struct ip_vs_pe *pe;
  413. char *pe_data;
  414. __u8 pe_data_len;
  415. };
  416. /*
  417. * IP_VS structure allocated for each dynamically scheduled connection
  418. */
  419. struct ip_vs_conn {
  420. struct hlist_node c_list; /* hashed list heads */
  421. #ifdef CONFIG_NET_NS
  422. struct net *net; /* Name space */
  423. #endif
  424. /* Protocol, addresses and port numbers */
  425. u16 af; /* address family */
  426. __be16 cport;
  427. __be16 vport;
  428. __be16 dport;
  429. __u32 fwmark; /* Fire wall mark from skb */
  430. union nf_inet_addr caddr; /* client address */
  431. union nf_inet_addr vaddr; /* virtual address */
  432. union nf_inet_addr daddr; /* destination address */
  433. volatile __u32 flags; /* status flags */
  434. __u16 protocol; /* Which protocol (TCP/UDP) */
  435. /* counter and timer */
  436. atomic_t refcnt; /* reference count */
  437. struct timer_list timer; /* Expiration timer */
  438. volatile unsigned long timeout; /* timeout */
  439. /* Flags and state transition */
  440. spinlock_t lock; /* lock for state transition */
  441. volatile __u16 state; /* state info */
  442. volatile __u16 old_state; /* old state, to be used for
  443. * state transition triggerd
  444. * synchronization
  445. */
  446. /* Control members */
  447. struct ip_vs_conn *control; /* Master control connection */
  448. atomic_t n_control; /* Number of controlled ones */
  449. struct ip_vs_dest *dest; /* real server */
  450. atomic_t in_pkts; /* incoming packet counter */
  451. /* packet transmitter for different forwarding methods. If it
  452. mangles the packet, it must return NF_DROP or better NF_STOLEN,
  453. otherwise this must be changed to a sk_buff **.
  454. NF_ACCEPT can be returned when destination is local.
  455. */
  456. int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
  457. struct ip_vs_protocol *pp);
  458. /* Note: we can group the following members into a structure,
  459. in order to save more space, and the following members are
  460. only used in VS/NAT anyway */
  461. struct ip_vs_app *app; /* bound ip_vs_app object */
  462. void *app_data; /* Application private data */
  463. struct ip_vs_seq in_seq; /* incoming seq. struct */
  464. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  465. const struct ip_vs_pe *pe;
  466. char *pe_data;
  467. __u8 pe_data_len;
  468. };
  469. /*
  470. * To save some memory in conn table when name space is disabled.
  471. */
  472. static inline struct net *ip_vs_conn_net(const struct ip_vs_conn *cp)
  473. {
  474. #ifdef CONFIG_NET_NS
  475. return cp->net;
  476. #else
  477. return &init_net;
  478. #endif
  479. }
  480. static inline void ip_vs_conn_net_set(struct ip_vs_conn *cp, struct net *net)
  481. {
  482. #ifdef CONFIG_NET_NS
  483. cp->net = net;
  484. #endif
  485. }
  486. static inline int ip_vs_conn_net_eq(const struct ip_vs_conn *cp,
  487. struct net *net)
  488. {
  489. #ifdef CONFIG_NET_NS
  490. return cp->net == net;
  491. #else
  492. return 1;
  493. #endif
  494. }
  495. /*
  496. * Extended internal versions of struct ip_vs_service_user and
  497. * ip_vs_dest_user for IPv6 support.
  498. *
  499. * We need these to conveniently pass around service and destination
  500. * options, but unfortunately, we also need to keep the old definitions to
  501. * maintain userspace backwards compatibility for the setsockopt interface.
  502. */
  503. struct ip_vs_service_user_kern {
  504. /* virtual service addresses */
  505. u16 af;
  506. u16 protocol;
  507. union nf_inet_addr addr; /* virtual ip address */
  508. u16 port;
  509. u32 fwmark; /* firwall mark of service */
  510. /* virtual service options */
  511. char *sched_name;
  512. char *pe_name;
  513. unsigned flags; /* virtual service flags */
  514. unsigned timeout; /* persistent timeout in sec */
  515. u32 netmask; /* persistent netmask */
  516. };
  517. struct ip_vs_dest_user_kern {
  518. /* destination server address */
  519. union nf_inet_addr addr;
  520. u16 port;
  521. /* real server options */
  522. unsigned conn_flags; /* connection flags */
  523. int weight; /* destination weight */
  524. /* thresholds for active connections */
  525. u32 u_threshold; /* upper threshold */
  526. u32 l_threshold; /* lower threshold */
  527. };
  528. /*
  529. * The information about the virtual service offered to the net
  530. * and the forwarding entries
  531. */
  532. struct ip_vs_service {
  533. struct list_head s_list; /* for normal service table */
  534. struct list_head f_list; /* for fwmark-based service table */
  535. atomic_t refcnt; /* reference counter */
  536. atomic_t usecnt; /* use counter */
  537. u16 af; /* address family */
  538. __u16 protocol; /* which protocol (TCP/UDP) */
  539. union nf_inet_addr addr; /* IP address for virtual service */
  540. __be16 port; /* port number for the service */
  541. __u32 fwmark; /* firewall mark of the service */
  542. unsigned flags; /* service status flags */
  543. unsigned timeout; /* persistent timeout in ticks */
  544. __be32 netmask; /* grouping granularity */
  545. struct net *net;
  546. struct list_head destinations; /* real server d-linked list */
  547. __u32 num_dests; /* number of servers */
  548. struct ip_vs_stats stats; /* statistics for the service */
  549. struct ip_vs_app *inc; /* bind conns to this app inc */
  550. /* for scheduling */
  551. struct ip_vs_scheduler *scheduler; /* bound scheduler object */
  552. rwlock_t sched_lock; /* lock sched_data */
  553. void *sched_data; /* scheduler application data */
  554. /* alternate persistence engine */
  555. struct ip_vs_pe *pe;
  556. };
  557. /*
  558. * The real server destination forwarding entry
  559. * with ip address, port number, and so on.
  560. */
  561. struct ip_vs_dest {
  562. struct list_head n_list; /* for the dests in the service */
  563. struct list_head d_list; /* for table with all the dests */
  564. u16 af; /* address family */
  565. __be16 port; /* port number of the server */
  566. union nf_inet_addr addr; /* IP address of the server */
  567. volatile unsigned flags; /* dest status flags */
  568. atomic_t conn_flags; /* flags to copy to conn */
  569. atomic_t weight; /* server weight */
  570. atomic_t refcnt; /* reference counter */
  571. struct ip_vs_stats stats; /* statistics */
  572. /* connection counters and thresholds */
  573. atomic_t activeconns; /* active connections */
  574. atomic_t inactconns; /* inactive connections */
  575. atomic_t persistconns; /* persistent connections */
  576. __u32 u_threshold; /* upper threshold */
  577. __u32 l_threshold; /* lower threshold */
  578. /* for destination cache */
  579. spinlock_t dst_lock; /* lock of dst_cache */
  580. struct dst_entry *dst_cache; /* destination cache entry */
  581. u32 dst_rtos; /* RT_TOS(tos) for dst */
  582. u32 dst_cookie;
  583. union nf_inet_addr dst_saddr;
  584. /* for virtual service */
  585. struct ip_vs_service *svc; /* service it belongs to */
  586. __u16 protocol; /* which protocol (TCP/UDP) */
  587. __be16 vport; /* virtual port number */
  588. union nf_inet_addr vaddr; /* virtual IP address */
  589. __u32 vfwmark; /* firewall mark of service */
  590. };
  591. /*
  592. * The scheduler object
  593. */
  594. struct ip_vs_scheduler {
  595. struct list_head n_list; /* d-linked list head */
  596. char *name; /* scheduler name */
  597. atomic_t refcnt; /* reference counter */
  598. struct module *module; /* THIS_MODULE/NULL */
  599. /* scheduler initializing service */
  600. int (*init_service)(struct ip_vs_service *svc);
  601. /* scheduling service finish */
  602. int (*done_service)(struct ip_vs_service *svc);
  603. /* scheduler updating service */
  604. int (*update_service)(struct ip_vs_service *svc);
  605. /* selecting a server from the given service */
  606. struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
  607. const struct sk_buff *skb);
  608. };
  609. /* The persistence engine object */
  610. struct ip_vs_pe {
  611. struct list_head n_list; /* d-linked list head */
  612. char *name; /* scheduler name */
  613. atomic_t refcnt; /* reference counter */
  614. struct module *module; /* THIS_MODULE/NULL */
  615. /* get the connection template, if any */
  616. int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
  617. bool (*ct_match)(const struct ip_vs_conn_param *p,
  618. struct ip_vs_conn *ct);
  619. u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
  620. bool inverse);
  621. int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
  622. };
  623. /*
  624. * The application module object (a.k.a. app incarnation)
  625. */
  626. struct ip_vs_app {
  627. struct list_head a_list; /* member in app list */
  628. int type; /* IP_VS_APP_TYPE_xxx */
  629. char *name; /* application module name */
  630. __u16 protocol;
  631. struct module *module; /* THIS_MODULE/NULL */
  632. struct list_head incs_list; /* list of incarnations */
  633. /* members for application incarnations */
  634. struct list_head p_list; /* member in proto app list */
  635. struct ip_vs_app *app; /* its real application */
  636. __be16 port; /* port number in net order */
  637. atomic_t usecnt; /* usage counter */
  638. /*
  639. * output hook: Process packet in inout direction, diff set for TCP.
  640. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  641. * 2=Mangled but checksum was not updated
  642. */
  643. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  644. struct sk_buff *, int *diff);
  645. /*
  646. * input hook: Process packet in outin direction, diff set for TCP.
  647. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  648. * 2=Mangled but checksum was not updated
  649. */
  650. int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
  651. struct sk_buff *, int *diff);
  652. /* ip_vs_app initializer */
  653. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  654. /* ip_vs_app finish */
  655. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  656. /* not used now */
  657. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  658. struct ip_vs_protocol *);
  659. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  660. int * timeout_table;
  661. int * timeouts;
  662. int timeouts_size;
  663. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  664. int *verdict, struct ip_vs_conn **cpp);
  665. struct ip_vs_conn *
  666. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  667. const struct iphdr *iph, unsigned int proto_off,
  668. int inverse);
  669. struct ip_vs_conn *
  670. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  671. const struct iphdr *iph, unsigned int proto_off,
  672. int inverse);
  673. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  674. const struct sk_buff *skb,
  675. struct ip_vs_app *app);
  676. void (*timeout_change)(struct ip_vs_app *app, int flags);
  677. };
  678. /* IPVS in network namespace */
  679. struct netns_ipvs {
  680. int gen; /* Generation */
  681. int enable; /* enable like nf_hooks do */
  682. /*
  683. * Hash table: for real service lookups
  684. */
  685. #define IP_VS_RTAB_BITS 4
  686. #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
  687. #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
  688. struct list_head rs_table[IP_VS_RTAB_SIZE];
  689. /* ip_vs_app */
  690. struct list_head app_list;
  691. /* ip_vs_ftp */
  692. struct ip_vs_app *ftp_app;
  693. /* ip_vs_proto */
  694. #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
  695. struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE];
  696. /* ip_vs_proto_tcp */
  697. #ifdef CONFIG_IP_VS_PROTO_TCP
  698. #define TCP_APP_TAB_BITS 4
  699. #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
  700. #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
  701. struct list_head tcp_apps[TCP_APP_TAB_SIZE];
  702. spinlock_t tcp_app_lock;
  703. #endif
  704. /* ip_vs_proto_udp */
  705. #ifdef CONFIG_IP_VS_PROTO_UDP
  706. #define UDP_APP_TAB_BITS 4
  707. #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
  708. #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
  709. struct list_head udp_apps[UDP_APP_TAB_SIZE];
  710. spinlock_t udp_app_lock;
  711. #endif
  712. /* ip_vs_proto_sctp */
  713. #ifdef CONFIG_IP_VS_PROTO_SCTP
  714. #define SCTP_APP_TAB_BITS 4
  715. #define SCTP_APP_TAB_SIZE (1 << SCTP_APP_TAB_BITS)
  716. #define SCTP_APP_TAB_MASK (SCTP_APP_TAB_SIZE - 1)
  717. /* Hash table for SCTP application incarnations */
  718. struct list_head sctp_apps[SCTP_APP_TAB_SIZE];
  719. spinlock_t sctp_app_lock;
  720. #endif
  721. /* ip_vs_conn */
  722. atomic_t conn_count; /* connection counter */
  723. /* ip_vs_ctl */
  724. struct ip_vs_stats tot_stats; /* Statistics & est. */
  725. int num_services; /* no of virtual services */
  726. rwlock_t rs_lock; /* real services table */
  727. /* Trash for destinations */
  728. struct list_head dest_trash;
  729. /* Service counters */
  730. atomic_t ftpsvc_counter;
  731. atomic_t nullsvc_counter;
  732. #ifdef CONFIG_SYSCTL
  733. /* 1/rate drop and drop-entry variables */
  734. struct delayed_work defense_work; /* Work handler */
  735. int drop_rate;
  736. int drop_counter;
  737. atomic_t dropentry;
  738. /* locks in ctl.c */
  739. spinlock_t dropentry_lock; /* drop entry handling */
  740. spinlock_t droppacket_lock; /* drop packet handling */
  741. spinlock_t securetcp_lock; /* state and timeout tables */
  742. /* sys-ctl struct */
  743. struct ctl_table_header *sysctl_hdr;
  744. struct ctl_table *sysctl_tbl;
  745. #endif
  746. /* sysctl variables */
  747. int sysctl_amemthresh;
  748. int sysctl_am_droprate;
  749. int sysctl_drop_entry;
  750. int sysctl_drop_packet;
  751. int sysctl_secure_tcp;
  752. #ifdef CONFIG_IP_VS_NFCT
  753. int sysctl_conntrack;
  754. #endif
  755. int sysctl_snat_reroute;
  756. int sysctl_sync_ver;
  757. int sysctl_cache_bypass;
  758. int sysctl_expire_nodest_conn;
  759. int sysctl_expire_quiescent_template;
  760. int sysctl_sync_threshold[2];
  761. int sysctl_nat_icmp_send;
  762. /* ip_vs_lblc */
  763. int sysctl_lblc_expiration;
  764. struct ctl_table_header *lblc_ctl_header;
  765. struct ctl_table *lblc_ctl_table;
  766. /* ip_vs_lblcr */
  767. int sysctl_lblcr_expiration;
  768. struct ctl_table_header *lblcr_ctl_header;
  769. struct ctl_table *lblcr_ctl_table;
  770. /* ip_vs_est */
  771. struct list_head est_list; /* estimator list */
  772. spinlock_t est_lock;
  773. struct timer_list est_timer; /* Estimation timer */
  774. /* ip_vs_sync */
  775. struct list_head sync_queue;
  776. spinlock_t sync_lock;
  777. struct ip_vs_sync_buff *sync_buff;
  778. spinlock_t sync_buff_lock;
  779. struct sockaddr_in sync_mcast_addr;
  780. struct task_struct *master_thread;
  781. struct task_struct *backup_thread;
  782. int send_mesg_maxlen;
  783. int recv_mesg_maxlen;
  784. volatile int sync_state;
  785. volatile int master_syncid;
  786. volatile int backup_syncid;
  787. struct mutex sync_mutex;
  788. /* multicast interface name */
  789. char master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  790. char backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  791. /* net name space ptr */
  792. struct net *net; /* Needed by timer routines */
  793. };
  794. #define DEFAULT_SYNC_THRESHOLD 3
  795. #define DEFAULT_SYNC_PERIOD 50
  796. #define DEFAULT_SYNC_VER 1
  797. #ifdef CONFIG_SYSCTL
  798. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  799. {
  800. return ipvs->sysctl_sync_threshold[0];
  801. }
  802. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  803. {
  804. return ipvs->sysctl_sync_threshold[1];
  805. }
  806. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  807. {
  808. return ipvs->sysctl_sync_ver;
  809. }
  810. #else
  811. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  812. {
  813. return DEFAULT_SYNC_THRESHOLD;
  814. }
  815. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  816. {
  817. return DEFAULT_SYNC_PERIOD;
  818. }
  819. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  820. {
  821. return DEFAULT_SYNC_VER;
  822. }
  823. #endif
  824. /*
  825. * IPVS core functions
  826. * (from ip_vs_core.c)
  827. */
  828. extern const char *ip_vs_proto_name(unsigned proto);
  829. extern void ip_vs_init_hash_table(struct list_head *table, int rows);
  830. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
  831. #define IP_VS_APP_TYPE_FTP 1
  832. /*
  833. * ip_vs_conn handling functions
  834. * (from ip_vs_conn.c)
  835. */
  836. enum {
  837. IP_VS_DIR_INPUT = 0,
  838. IP_VS_DIR_OUTPUT,
  839. IP_VS_DIR_INPUT_ONLY,
  840. IP_VS_DIR_LAST,
  841. };
  842. static inline void ip_vs_conn_fill_param(struct net *net, int af, int protocol,
  843. const union nf_inet_addr *caddr,
  844. __be16 cport,
  845. const union nf_inet_addr *vaddr,
  846. __be16 vport,
  847. struct ip_vs_conn_param *p)
  848. {
  849. p->net = net;
  850. p->af = af;
  851. p->protocol = protocol;
  852. p->caddr = caddr;
  853. p->cport = cport;
  854. p->vaddr = vaddr;
  855. p->vport = vport;
  856. p->pe = NULL;
  857. p->pe_data = NULL;
  858. }
  859. struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
  860. struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
  861. struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
  862. const struct ip_vs_iphdr *iph,
  863. unsigned int proto_off,
  864. int inverse);
  865. struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
  866. struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
  867. const struct ip_vs_iphdr *iph,
  868. unsigned int proto_off,
  869. int inverse);
  870. /* put back the conn without restarting its timer */
  871. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  872. {
  873. atomic_dec(&cp->refcnt);
  874. }
  875. extern void ip_vs_conn_put(struct ip_vs_conn *cp);
  876. extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  877. struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p,
  878. const union nf_inet_addr *daddr,
  879. __be16 dport, unsigned flags,
  880. struct ip_vs_dest *dest, __u32 fwmark);
  881. extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  882. extern const char * ip_vs_state_name(__u16 proto, int state);
  883. extern void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp);
  884. extern int ip_vs_check_template(struct ip_vs_conn *ct);
  885. extern void ip_vs_random_dropentry(struct net *net);
  886. extern int ip_vs_conn_init(void);
  887. extern void ip_vs_conn_cleanup(void);
  888. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  889. {
  890. struct ip_vs_conn *ctl_cp = cp->control;
  891. if (!ctl_cp) {
  892. IP_VS_ERR_BUF("request control DEL for uncontrolled: "
  893. "%s:%d to %s:%d\n",
  894. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  895. ntohs(cp->cport),
  896. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  897. ntohs(cp->vport));
  898. return;
  899. }
  900. IP_VS_DBG_BUF(7, "DELeting control for: "
  901. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  902. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  903. ntohs(cp->cport),
  904. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  905. ntohs(ctl_cp->cport));
  906. cp->control = NULL;
  907. if (atomic_read(&ctl_cp->n_control) == 0) {
  908. IP_VS_ERR_BUF("BUG control DEL with n=0 : "
  909. "%s:%d to %s:%d\n",
  910. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  911. ntohs(cp->cport),
  912. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  913. ntohs(cp->vport));
  914. return;
  915. }
  916. atomic_dec(&ctl_cp->n_control);
  917. }
  918. static inline void
  919. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  920. {
  921. if (cp->control) {
  922. IP_VS_ERR_BUF("request control ADD for already controlled: "
  923. "%s:%d to %s:%d\n",
  924. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  925. ntohs(cp->cport),
  926. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  927. ntohs(cp->vport));
  928. ip_vs_control_del(cp);
  929. }
  930. IP_VS_DBG_BUF(7, "ADDing control for: "
  931. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  932. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  933. ntohs(cp->cport),
  934. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  935. ntohs(ctl_cp->cport));
  936. cp->control = ctl_cp;
  937. atomic_inc(&ctl_cp->n_control);
  938. }
  939. /*
  940. * IPVS netns init & cleanup functions
  941. */
  942. extern int ip_vs_estimator_net_init(struct net *net);
  943. extern int ip_vs_control_net_init(struct net *net);
  944. extern int ip_vs_protocol_net_init(struct net *net);
  945. extern int ip_vs_app_net_init(struct net *net);
  946. extern int ip_vs_conn_net_init(struct net *net);
  947. extern int ip_vs_sync_net_init(struct net *net);
  948. extern void ip_vs_conn_net_cleanup(struct net *net);
  949. extern void ip_vs_app_net_cleanup(struct net *net);
  950. extern void ip_vs_protocol_net_cleanup(struct net *net);
  951. extern void ip_vs_control_net_cleanup(struct net *net);
  952. extern void ip_vs_estimator_net_cleanup(struct net *net);
  953. extern void ip_vs_sync_net_cleanup(struct net *net);
  954. extern void ip_vs_service_net_cleanup(struct net *net);
  955. /*
  956. * IPVS application functions
  957. * (from ip_vs_app.c)
  958. */
  959. #define IP_VS_APP_MAX_PORTS 8
  960. extern int register_ip_vs_app(struct net *net, struct ip_vs_app *app);
  961. extern void unregister_ip_vs_app(struct net *net, struct ip_vs_app *app);
  962. extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  963. extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
  964. extern int register_ip_vs_app_inc(struct net *net, struct ip_vs_app *app,
  965. __u16 proto, __u16 port);
  966. extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
  967. extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
  968. extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
  969. extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
  970. void ip_vs_bind_pe(struct ip_vs_service *svc, struct ip_vs_pe *pe);
  971. void ip_vs_unbind_pe(struct ip_vs_service *svc);
  972. int register_ip_vs_pe(struct ip_vs_pe *pe);
  973. int unregister_ip_vs_pe(struct ip_vs_pe *pe);
  974. struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
  975. struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
  976. /*
  977. * Use a #define to avoid all of module.h just for these trivial ops
  978. */
  979. #define ip_vs_pe_get(pe) \
  980. if (pe && pe->module) \
  981. __module_get(pe->module);
  982. #define ip_vs_pe_put(pe) \
  983. if (pe && pe->module) \
  984. module_put(pe->module);
  985. /*
  986. * IPVS protocol functions (from ip_vs_proto.c)
  987. */
  988. extern int ip_vs_protocol_init(void);
  989. extern void ip_vs_protocol_cleanup(void);
  990. extern void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
  991. extern int *ip_vs_create_timeout_table(int *table, int size);
  992. extern int
  993. ip_vs_set_state_timeout(int *table, int num, const char *const *names,
  994. const char *name, int to);
  995. extern void
  996. ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
  997. const struct sk_buff *skb,
  998. int offset, const char *msg);
  999. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  1000. extern struct ip_vs_protocol ip_vs_protocol_udp;
  1001. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  1002. extern struct ip_vs_protocol ip_vs_protocol_esp;
  1003. extern struct ip_vs_protocol ip_vs_protocol_ah;
  1004. extern struct ip_vs_protocol ip_vs_protocol_sctp;
  1005. /*
  1006. * Registering/unregistering scheduler functions
  1007. * (from ip_vs_sched.c)
  1008. */
  1009. extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1010. extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1011. extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  1012. struct ip_vs_scheduler *scheduler);
  1013. extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
  1014. extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  1015. extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  1016. extern struct ip_vs_conn *
  1017. ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
  1018. struct ip_vs_proto_data *pd, int *ignored);
  1019. extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  1020. struct ip_vs_proto_data *pd);
  1021. extern void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
  1022. /*
  1023. * IPVS control data and functions (from ip_vs_ctl.c)
  1024. */
  1025. extern struct ip_vs_stats ip_vs_stats;
  1026. extern const struct ctl_path net_vs_ctl_path[];
  1027. extern int sysctl_ip_vs_sync_ver;
  1028. extern void ip_vs_sync_switch_mode(struct net *net, int mode);
  1029. extern struct ip_vs_service *
  1030. ip_vs_service_get(struct net *net, int af, __u32 fwmark, __u16 protocol,
  1031. const union nf_inet_addr *vaddr, __be16 vport);
  1032. static inline void ip_vs_service_put(struct ip_vs_service *svc)
  1033. {
  1034. atomic_dec(&svc->usecnt);
  1035. }
  1036. extern struct ip_vs_dest *
  1037. ip_vs_lookup_real_service(struct net *net, int af, __u16 protocol,
  1038. const union nf_inet_addr *daddr, __be16 dport);
  1039. extern int ip_vs_use_count_inc(void);
  1040. extern void ip_vs_use_count_dec(void);
  1041. extern int ip_vs_control_init(void);
  1042. extern void ip_vs_control_cleanup(void);
  1043. extern struct ip_vs_dest *
  1044. ip_vs_find_dest(struct net *net, int af, const union nf_inet_addr *daddr,
  1045. __be16 dport, const union nf_inet_addr *vaddr, __be16 vport,
  1046. __u16 protocol, __u32 fwmark);
  1047. extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  1048. /*
  1049. * IPVS sync daemon data and function prototypes
  1050. * (from ip_vs_sync.c)
  1051. */
  1052. extern int start_sync_thread(struct net *net, int state, char *mcast_ifn,
  1053. __u8 syncid);
  1054. extern int stop_sync_thread(struct net *net, int state);
  1055. extern void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp);
  1056. /*
  1057. * IPVS rate estimator prototypes (from ip_vs_est.c)
  1058. */
  1059. extern void ip_vs_start_estimator(struct net *net, struct ip_vs_stats *stats);
  1060. extern void ip_vs_stop_estimator(struct net *net, struct ip_vs_stats *stats);
  1061. extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  1062. extern void ip_vs_read_estimator(struct ip_vs_stats_user *dst,
  1063. struct ip_vs_stats *stats);
  1064. /*
  1065. * Various IPVS packet transmitters (from ip_vs_xmit.c)
  1066. */
  1067. extern int ip_vs_null_xmit
  1068. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1069. extern int ip_vs_bypass_xmit
  1070. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1071. extern int ip_vs_nat_xmit
  1072. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1073. extern int ip_vs_tunnel_xmit
  1074. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1075. extern int ip_vs_dr_xmit
  1076. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1077. extern int ip_vs_icmp_xmit
  1078. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
  1079. int offset, unsigned int hooknum);
  1080. extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
  1081. #ifdef CONFIG_IP_VS_IPV6
  1082. extern int ip_vs_bypass_xmit_v6
  1083. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1084. extern int ip_vs_nat_xmit_v6
  1085. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1086. extern int ip_vs_tunnel_xmit_v6
  1087. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1088. extern int ip_vs_dr_xmit_v6
  1089. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1090. extern int ip_vs_icmp_xmit_v6
  1091. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
  1092. int offset, unsigned int hooknum);
  1093. #endif
  1094. #ifdef CONFIG_SYSCTL
  1095. /*
  1096. * This is a simple mechanism to ignore packets when
  1097. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  1098. * we start to drop 1/rate of the packets
  1099. */
  1100. static inline int ip_vs_todrop(struct netns_ipvs *ipvs)
  1101. {
  1102. if (!ipvs->drop_rate)
  1103. return 0;
  1104. if (--ipvs->drop_counter > 0)
  1105. return 0;
  1106. ipvs->drop_counter = ipvs->drop_rate;
  1107. return 1;
  1108. }
  1109. #else
  1110. static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; }
  1111. #endif
  1112. /*
  1113. * ip_vs_fwd_tag returns the forwarding tag of the connection
  1114. */
  1115. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  1116. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  1117. {
  1118. char fwd;
  1119. switch (IP_VS_FWD_METHOD(cp)) {
  1120. case IP_VS_CONN_F_MASQ:
  1121. fwd = 'M'; break;
  1122. case IP_VS_CONN_F_LOCALNODE:
  1123. fwd = 'L'; break;
  1124. case IP_VS_CONN_F_TUNNEL:
  1125. fwd = 'T'; break;
  1126. case IP_VS_CONN_F_DROUTE:
  1127. fwd = 'R'; break;
  1128. case IP_VS_CONN_F_BYPASS:
  1129. fwd = 'B'; break;
  1130. default:
  1131. fwd = '?'; break;
  1132. }
  1133. return fwd;
  1134. }
  1135. extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1136. struct ip_vs_conn *cp, int dir);
  1137. #ifdef CONFIG_IP_VS_IPV6
  1138. extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1139. struct ip_vs_conn *cp, int dir);
  1140. #endif
  1141. extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  1142. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  1143. {
  1144. __be32 diff[2] = { ~old, new };
  1145. return csum_partial(diff, sizeof(diff), oldsum);
  1146. }
  1147. #ifdef CONFIG_IP_VS_IPV6
  1148. static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
  1149. __wsum oldsum)
  1150. {
  1151. __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
  1152. new[3], new[2], new[1], new[0] };
  1153. return csum_partial(diff, sizeof(diff), oldsum);
  1154. }
  1155. #endif
  1156. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  1157. {
  1158. __be16 diff[2] = { ~old, new };
  1159. return csum_partial(diff, sizeof(diff), oldsum);
  1160. }
  1161. /*
  1162. * Forget current conntrack (unconfirmed) and attach notrack entry
  1163. */
  1164. static inline void ip_vs_notrack(struct sk_buff *skb)
  1165. {
  1166. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1167. enum ip_conntrack_info ctinfo;
  1168. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  1169. if (!ct || !nf_ct_is_untracked(ct)) {
  1170. nf_reset(skb);
  1171. skb->nfct = &nf_ct_untracked_get()->ct_general;
  1172. skb->nfctinfo = IP_CT_NEW;
  1173. nf_conntrack_get(skb->nfct);
  1174. }
  1175. #endif
  1176. }
  1177. #ifdef CONFIG_IP_VS_NFCT
  1178. /*
  1179. * Netfilter connection tracking
  1180. * (from ip_vs_nfct.c)
  1181. */
  1182. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1183. {
  1184. #ifdef CONFIG_SYSCTL
  1185. return ipvs->sysctl_conntrack;
  1186. #else
  1187. return 0;
  1188. #endif
  1189. }
  1190. extern void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
  1191. int outin);
  1192. extern int ip_vs_confirm_conntrack(struct sk_buff *skb);
  1193. extern void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
  1194. struct ip_vs_conn *cp, u_int8_t proto,
  1195. const __be16 port, int from_rs);
  1196. extern void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
  1197. #else
  1198. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1199. {
  1200. return 0;
  1201. }
  1202. static inline void ip_vs_update_conntrack(struct sk_buff *skb,
  1203. struct ip_vs_conn *cp, int outin)
  1204. {
  1205. }
  1206. static inline int ip_vs_confirm_conntrack(struct sk_buff *skb)
  1207. {
  1208. return NF_ACCEPT;
  1209. }
  1210. static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
  1211. {
  1212. }
  1213. /* CONFIG_IP_VS_NFCT */
  1214. #endif
  1215. static inline unsigned int
  1216. ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
  1217. {
  1218. /*
  1219. * We think the overhead of processing active connections is 256
  1220. * times higher than that of inactive connections in average. (This
  1221. * 256 times might not be accurate, we will change it later) We
  1222. * use the following formula to estimate the overhead now:
  1223. * dest->activeconns*256 + dest->inactconns
  1224. */
  1225. return (atomic_read(&dest->activeconns) << 8) +
  1226. atomic_read(&dest->inactconns);
  1227. }
  1228. #endif /* _NET_IP_VS_H */