ip_vs.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. /* old ipvsadm versions still include this file directly */
  9. #ifdef __KERNEL__
  10. #include <asm/types.h> /* for __uXX types */
  11. #include <linux/sysctl.h> /* for ctl_path */
  12. #include <linux/list.h> /* for struct list_head */
  13. #include <linux/spinlock.h> /* for struct rwlock_t */
  14. #include <asm/atomic.h> /* for struct atomic_t */
  15. #include <linux/compiler.h>
  16. #include <linux/timer.h>
  17. #include <net/checksum.h>
  18. #include <linux/netfilter.h> /* for union nf_inet_addr */
  19. #include <linux/ip.h>
  20. #include <linux/ipv6.h> /* for struct ipv6hdr */
  21. #include <net/ipv6.h> /* for ipv6_addr_copy */
  22. #ifdef CONFIG_IP_VS_NFCT
  23. #include <net/netfilter/nf_conntrack.h>
  24. #endif
  25. /* Connections' size value needed by ip_vs_ctl.c */
  26. extern int ip_vs_conn_tab_size;
  27. struct ip_vs_iphdr {
  28. int len;
  29. __u8 protocol;
  30. union nf_inet_addr saddr;
  31. union nf_inet_addr daddr;
  32. };
  33. static inline void
  34. ip_vs_fill_iphdr(int af, const void *nh, struct ip_vs_iphdr *iphdr)
  35. {
  36. #ifdef CONFIG_IP_VS_IPV6
  37. if (af == AF_INET6) {
  38. const struct ipv6hdr *iph = nh;
  39. iphdr->len = sizeof(struct ipv6hdr);
  40. iphdr->protocol = iph->nexthdr;
  41. ipv6_addr_copy(&iphdr->saddr.in6, &iph->saddr);
  42. ipv6_addr_copy(&iphdr->daddr.in6, &iph->daddr);
  43. } else
  44. #endif
  45. {
  46. const struct iphdr *iph = nh;
  47. iphdr->len = iph->ihl * 4;
  48. iphdr->protocol = iph->protocol;
  49. iphdr->saddr.ip = iph->saddr;
  50. iphdr->daddr.ip = iph->daddr;
  51. }
  52. }
  53. static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
  54. const union nf_inet_addr *src)
  55. {
  56. #ifdef CONFIG_IP_VS_IPV6
  57. if (af == AF_INET6)
  58. ipv6_addr_copy(&dst->in6, &src->in6);
  59. else
  60. #endif
  61. dst->ip = src->ip;
  62. }
  63. static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
  64. const union nf_inet_addr *b)
  65. {
  66. #ifdef CONFIG_IP_VS_IPV6
  67. if (af == AF_INET6)
  68. return ipv6_addr_equal(&a->in6, &b->in6);
  69. #endif
  70. return a->ip == b->ip;
  71. }
  72. #ifdef CONFIG_IP_VS_DEBUG
  73. #include <linux/net.h>
  74. extern int ip_vs_get_debug_level(void);
  75. static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
  76. const union nf_inet_addr *addr,
  77. int *idx)
  78. {
  79. int len;
  80. #ifdef CONFIG_IP_VS_IPV6
  81. if (af == AF_INET6)
  82. len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6]",
  83. &addr->in6) + 1;
  84. else
  85. #endif
  86. len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
  87. &addr->ip) + 1;
  88. *idx += len;
  89. BUG_ON(*idx > buf_len + 1);
  90. return &buf[*idx - len];
  91. }
  92. #define IP_VS_DBG_BUF(level, msg, ...) \
  93. do { \
  94. char ip_vs_dbg_buf[160]; \
  95. int ip_vs_dbg_idx = 0; \
  96. if (level <= ip_vs_get_debug_level()) \
  97. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  98. } while (0)
  99. #define IP_VS_ERR_BUF(msg...) \
  100. do { \
  101. char ip_vs_dbg_buf[160]; \
  102. int ip_vs_dbg_idx = 0; \
  103. pr_err(msg); \
  104. } while (0)
  105. /* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
  106. #define IP_VS_DBG_ADDR(af, addr) \
  107. ip_vs_dbg_addr(af, ip_vs_dbg_buf, \
  108. sizeof(ip_vs_dbg_buf), addr, \
  109. &ip_vs_dbg_idx)
  110. #define IP_VS_DBG(level, msg, ...) \
  111. do { \
  112. if (level <= ip_vs_get_debug_level()) \
  113. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  114. } while (0)
  115. #define IP_VS_DBG_RL(msg, ...) \
  116. do { \
  117. if (net_ratelimit()) \
  118. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  119. } while (0)
  120. #define IP_VS_DBG_PKT(level, pp, skb, ofs, msg) \
  121. do { \
  122. if (level <= ip_vs_get_debug_level()) \
  123. pp->debug_packet(pp, skb, ofs, msg); \
  124. } while (0)
  125. #define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg) \
  126. do { \
  127. if (level <= ip_vs_get_debug_level() && \
  128. net_ratelimit()) \
  129. pp->debug_packet(pp, skb, ofs, msg); \
  130. } while (0)
  131. #else /* NO DEBUGGING at ALL */
  132. #define IP_VS_DBG_BUF(level, msg...) do {} while (0)
  133. #define IP_VS_ERR_BUF(msg...) do {} while (0)
  134. #define IP_VS_DBG(level, msg...) do {} while (0)
  135. #define IP_VS_DBG_RL(msg...) do {} while (0)
  136. #define IP_VS_DBG_PKT(level, pp, skb, ofs, msg) do {} while (0)
  137. #define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg) do {} while (0)
  138. #endif
  139. #define IP_VS_BUG() BUG()
  140. #define IP_VS_ERR_RL(msg, ...) \
  141. do { \
  142. if (net_ratelimit()) \
  143. pr_err(msg, ##__VA_ARGS__); \
  144. } while (0)
  145. #ifdef CONFIG_IP_VS_DEBUG
  146. #define EnterFunction(level) \
  147. do { \
  148. if (level <= ip_vs_get_debug_level()) \
  149. printk(KERN_DEBUG \
  150. pr_fmt("Enter: %s, %s line %i\n"), \
  151. __func__, __FILE__, __LINE__); \
  152. } while (0)
  153. #define LeaveFunction(level) \
  154. do { \
  155. if (level <= ip_vs_get_debug_level()) \
  156. printk(KERN_DEBUG \
  157. pr_fmt("Leave: %s, %s line %i\n"), \
  158. __func__, __FILE__, __LINE__); \
  159. } while (0)
  160. #else
  161. #define EnterFunction(level) do {} while (0)
  162. #define LeaveFunction(level) do {} while (0)
  163. #endif
  164. #define IP_VS_WAIT_WHILE(expr) while (expr) { cpu_relax(); }
  165. /*
  166. * The port number of FTP service (in network order).
  167. */
  168. #define FTPPORT cpu_to_be16(21)
  169. #define FTPDATA cpu_to_be16(20)
  170. /*
  171. * TCP State Values
  172. */
  173. enum {
  174. IP_VS_TCP_S_NONE = 0,
  175. IP_VS_TCP_S_ESTABLISHED,
  176. IP_VS_TCP_S_SYN_SENT,
  177. IP_VS_TCP_S_SYN_RECV,
  178. IP_VS_TCP_S_FIN_WAIT,
  179. IP_VS_TCP_S_TIME_WAIT,
  180. IP_VS_TCP_S_CLOSE,
  181. IP_VS_TCP_S_CLOSE_WAIT,
  182. IP_VS_TCP_S_LAST_ACK,
  183. IP_VS_TCP_S_LISTEN,
  184. IP_VS_TCP_S_SYNACK,
  185. IP_VS_TCP_S_LAST
  186. };
  187. /*
  188. * UDP State Values
  189. */
  190. enum {
  191. IP_VS_UDP_S_NORMAL,
  192. IP_VS_UDP_S_LAST,
  193. };
  194. /*
  195. * ICMP State Values
  196. */
  197. enum {
  198. IP_VS_ICMP_S_NORMAL,
  199. IP_VS_ICMP_S_LAST,
  200. };
  201. /*
  202. * SCTP State Values
  203. */
  204. enum ip_vs_sctp_states {
  205. IP_VS_SCTP_S_NONE,
  206. IP_VS_SCTP_S_INIT_CLI,
  207. IP_VS_SCTP_S_INIT_SER,
  208. IP_VS_SCTP_S_INIT_ACK_CLI,
  209. IP_VS_SCTP_S_INIT_ACK_SER,
  210. IP_VS_SCTP_S_ECHO_CLI,
  211. IP_VS_SCTP_S_ECHO_SER,
  212. IP_VS_SCTP_S_ESTABLISHED,
  213. IP_VS_SCTP_S_SHUT_CLI,
  214. IP_VS_SCTP_S_SHUT_SER,
  215. IP_VS_SCTP_S_SHUT_ACK_CLI,
  216. IP_VS_SCTP_S_SHUT_ACK_SER,
  217. IP_VS_SCTP_S_CLOSED,
  218. IP_VS_SCTP_S_LAST
  219. };
  220. /*
  221. * Delta sequence info structure
  222. * Each ip_vs_conn has 2 (output AND input seq. changes).
  223. * Only used in the VS/NAT.
  224. */
  225. struct ip_vs_seq {
  226. __u32 init_seq; /* Add delta from this seq */
  227. __u32 delta; /* Delta in sequence numbers */
  228. __u32 previous_delta; /* Delta in sequence numbers
  229. before last resized pkt */
  230. };
  231. /*
  232. * IPVS statistics objects
  233. */
  234. struct ip_vs_estimator {
  235. struct list_head list;
  236. u64 last_inbytes;
  237. u64 last_outbytes;
  238. u32 last_conns;
  239. u32 last_inpkts;
  240. u32 last_outpkts;
  241. u32 cps;
  242. u32 inpps;
  243. u32 outpps;
  244. u32 inbps;
  245. u32 outbps;
  246. };
  247. struct ip_vs_stats {
  248. struct ip_vs_stats_user ustats; /* statistics */
  249. struct ip_vs_estimator est; /* estimator */
  250. spinlock_t lock; /* spin lock */
  251. };
  252. struct dst_entry;
  253. struct iphdr;
  254. struct ip_vs_conn;
  255. struct ip_vs_app;
  256. struct sk_buff;
  257. struct ip_vs_protocol {
  258. struct ip_vs_protocol *next;
  259. char *name;
  260. u16 protocol;
  261. u16 num_states;
  262. int dont_defrag;
  263. atomic_t appcnt; /* counter of proto app incs */
  264. int *timeout_table; /* protocol timeout table */
  265. void (*init)(struct ip_vs_protocol *pp);
  266. void (*exit)(struct ip_vs_protocol *pp);
  267. int (*conn_schedule)(int af, struct sk_buff *skb,
  268. struct ip_vs_protocol *pp,
  269. int *verdict, struct ip_vs_conn **cpp);
  270. struct ip_vs_conn *
  271. (*conn_in_get)(int af,
  272. const struct sk_buff *skb,
  273. struct ip_vs_protocol *pp,
  274. const struct ip_vs_iphdr *iph,
  275. unsigned int proto_off,
  276. int inverse);
  277. struct ip_vs_conn *
  278. (*conn_out_get)(int af,
  279. const struct sk_buff *skb,
  280. struct ip_vs_protocol *pp,
  281. const struct ip_vs_iphdr *iph,
  282. unsigned int proto_off,
  283. int inverse);
  284. int (*snat_handler)(struct sk_buff *skb,
  285. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  286. int (*dnat_handler)(struct sk_buff *skb,
  287. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  288. int (*csum_check)(int af, struct sk_buff *skb,
  289. struct ip_vs_protocol *pp);
  290. const char *(*state_name)(int state);
  291. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  292. const struct sk_buff *skb,
  293. struct ip_vs_protocol *pp);
  294. int (*register_app)(struct ip_vs_app *inc);
  295. void (*unregister_app)(struct ip_vs_app *inc);
  296. int (*app_conn_bind)(struct ip_vs_conn *cp);
  297. void (*debug_packet)(struct ip_vs_protocol *pp,
  298. const struct sk_buff *skb,
  299. int offset,
  300. const char *msg);
  301. void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
  302. int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
  303. };
  304. extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
  305. struct ip_vs_conn_param {
  306. const union nf_inet_addr *caddr;
  307. const union nf_inet_addr *vaddr;
  308. __be16 cport;
  309. __be16 vport;
  310. __u16 protocol;
  311. u16 af;
  312. const struct ip_vs_pe *pe;
  313. char *pe_data;
  314. __u8 pe_data_len;
  315. };
  316. /*
  317. * IP_VS structure allocated for each dynamically scheduled connection
  318. */
  319. struct ip_vs_conn {
  320. struct list_head c_list; /* hashed list heads */
  321. /* Protocol, addresses and port numbers */
  322. u16 af; /* address family */
  323. union nf_inet_addr caddr; /* client address */
  324. union nf_inet_addr vaddr; /* virtual address */
  325. union nf_inet_addr daddr; /* destination address */
  326. volatile __u32 flags; /* status flags */
  327. __be16 cport;
  328. __be16 vport;
  329. __be16 dport;
  330. __u16 protocol; /* Which protocol (TCP/UDP) */
  331. /* counter and timer */
  332. atomic_t refcnt; /* reference count */
  333. struct timer_list timer; /* Expiration timer */
  334. volatile unsigned long timeout; /* timeout */
  335. /* Flags and state transition */
  336. spinlock_t lock; /* lock for state transition */
  337. volatile __u16 state; /* state info */
  338. volatile __u16 old_state; /* old state, to be used for
  339. * state transition triggerd
  340. * synchronization
  341. */
  342. /* Control members */
  343. struct ip_vs_conn *control; /* Master control connection */
  344. atomic_t n_control; /* Number of controlled ones */
  345. struct ip_vs_dest *dest; /* real server */
  346. atomic_t in_pkts; /* incoming packet counter */
  347. /* packet transmitter for different forwarding methods. If it
  348. mangles the packet, it must return NF_DROP or better NF_STOLEN,
  349. otherwise this must be changed to a sk_buff **.
  350. */
  351. int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
  352. struct ip_vs_protocol *pp);
  353. /* Note: we can group the following members into a structure,
  354. in order to save more space, and the following members are
  355. only used in VS/NAT anyway */
  356. struct ip_vs_app *app; /* bound ip_vs_app object */
  357. void *app_data; /* Application private data */
  358. struct ip_vs_seq in_seq; /* incoming seq. struct */
  359. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  360. char *pe_data;
  361. __u8 pe_data_len;
  362. };
  363. /*
  364. * Extended internal versions of struct ip_vs_service_user and
  365. * ip_vs_dest_user for IPv6 support.
  366. *
  367. * We need these to conveniently pass around service and destination
  368. * options, but unfortunately, we also need to keep the old definitions to
  369. * maintain userspace backwards compatibility for the setsockopt interface.
  370. */
  371. struct ip_vs_service_user_kern {
  372. /* virtual service addresses */
  373. u16 af;
  374. u16 protocol;
  375. union nf_inet_addr addr; /* virtual ip address */
  376. u16 port;
  377. u32 fwmark; /* firwall mark of service */
  378. /* virtual service options */
  379. char *sched_name;
  380. char *pe_name;
  381. unsigned flags; /* virtual service flags */
  382. unsigned timeout; /* persistent timeout in sec */
  383. u32 netmask; /* persistent netmask */
  384. };
  385. struct ip_vs_dest_user_kern {
  386. /* destination server address */
  387. union nf_inet_addr addr;
  388. u16 port;
  389. /* real server options */
  390. unsigned conn_flags; /* connection flags */
  391. int weight; /* destination weight */
  392. /* thresholds for active connections */
  393. u32 u_threshold; /* upper threshold */
  394. u32 l_threshold; /* lower threshold */
  395. };
  396. /*
  397. * The information about the virtual service offered to the net
  398. * and the forwarding entries
  399. */
  400. struct ip_vs_service {
  401. struct list_head s_list; /* for normal service table */
  402. struct list_head f_list; /* for fwmark-based service table */
  403. atomic_t refcnt; /* reference counter */
  404. atomic_t usecnt; /* use counter */
  405. u16 af; /* address family */
  406. __u16 protocol; /* which protocol (TCP/UDP) */
  407. union nf_inet_addr addr; /* IP address for virtual service */
  408. __be16 port; /* port number for the service */
  409. __u32 fwmark; /* firewall mark of the service */
  410. unsigned flags; /* service status flags */
  411. unsigned timeout; /* persistent timeout in ticks */
  412. __be32 netmask; /* grouping granularity */
  413. struct list_head destinations; /* real server d-linked list */
  414. __u32 num_dests; /* number of servers */
  415. struct ip_vs_stats stats; /* statistics for the service */
  416. struct ip_vs_app *inc; /* bind conns to this app inc */
  417. /* for scheduling */
  418. struct ip_vs_scheduler *scheduler; /* bound scheduler object */
  419. rwlock_t sched_lock; /* lock sched_data */
  420. void *sched_data; /* scheduler application data */
  421. /* alternate persistence engine */
  422. struct ip_vs_pe *pe;
  423. };
  424. /*
  425. * The real server destination forwarding entry
  426. * with ip address, port number, and so on.
  427. */
  428. struct ip_vs_dest {
  429. struct list_head n_list; /* for the dests in the service */
  430. struct list_head d_list; /* for table with all the dests */
  431. u16 af; /* address family */
  432. union nf_inet_addr addr; /* IP address of the server */
  433. __be16 port; /* port number of the server */
  434. volatile unsigned flags; /* dest status flags */
  435. atomic_t conn_flags; /* flags to copy to conn */
  436. atomic_t weight; /* server weight */
  437. atomic_t refcnt; /* reference counter */
  438. struct ip_vs_stats stats; /* statistics */
  439. /* connection counters and thresholds */
  440. atomic_t activeconns; /* active connections */
  441. atomic_t inactconns; /* inactive connections */
  442. atomic_t persistconns; /* persistent connections */
  443. __u32 u_threshold; /* upper threshold */
  444. __u32 l_threshold; /* lower threshold */
  445. /* for destination cache */
  446. spinlock_t dst_lock; /* lock of dst_cache */
  447. struct dst_entry *dst_cache; /* destination cache entry */
  448. u32 dst_rtos; /* RT_TOS(tos) for dst */
  449. u32 dst_cookie;
  450. #ifdef CONFIG_IP_VS_IPV6
  451. struct in6_addr dst_saddr;
  452. #endif
  453. /* for virtual service */
  454. struct ip_vs_service *svc; /* service it belongs to */
  455. __u16 protocol; /* which protocol (TCP/UDP) */
  456. union nf_inet_addr vaddr; /* virtual IP address */
  457. __be16 vport; /* virtual port number */
  458. __u32 vfwmark; /* firewall mark of service */
  459. };
  460. /*
  461. * The scheduler object
  462. */
  463. struct ip_vs_scheduler {
  464. struct list_head n_list; /* d-linked list head */
  465. char *name; /* scheduler name */
  466. atomic_t refcnt; /* reference counter */
  467. struct module *module; /* THIS_MODULE/NULL */
  468. /* scheduler initializing service */
  469. int (*init_service)(struct ip_vs_service *svc);
  470. /* scheduling service finish */
  471. int (*done_service)(struct ip_vs_service *svc);
  472. /* scheduler updating service */
  473. int (*update_service)(struct ip_vs_service *svc);
  474. /* selecting a server from the given service */
  475. struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
  476. const struct sk_buff *skb);
  477. };
  478. /* The persistence engine object */
  479. struct ip_vs_pe {
  480. struct list_head n_list; /* d-linked list head */
  481. char *name; /* scheduler name */
  482. atomic_t refcnt; /* reference counter */
  483. struct module *module; /* THIS_MODULE/NULL */
  484. /* get the connection template, if any */
  485. int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
  486. bool (*ct_match)(const struct ip_vs_conn_param *p,
  487. struct ip_vs_conn *ct);
  488. u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
  489. bool inverse);
  490. int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
  491. };
  492. /*
  493. * The application module object (a.k.a. app incarnation)
  494. */
  495. struct ip_vs_app {
  496. struct list_head a_list; /* member in app list */
  497. int type; /* IP_VS_APP_TYPE_xxx */
  498. char *name; /* application module name */
  499. __u16 protocol;
  500. struct module *module; /* THIS_MODULE/NULL */
  501. struct list_head incs_list; /* list of incarnations */
  502. /* members for application incarnations */
  503. struct list_head p_list; /* member in proto app list */
  504. struct ip_vs_app *app; /* its real application */
  505. __be16 port; /* port number in net order */
  506. atomic_t usecnt; /* usage counter */
  507. /* output hook: return false if can't linearize. diff set for TCP. */
  508. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  509. struct sk_buff *, int *diff);
  510. /* input hook: return false if can't linearize. diff set for TCP. */
  511. int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
  512. struct sk_buff *, int *diff);
  513. /* ip_vs_app initializer */
  514. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  515. /* ip_vs_app finish */
  516. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  517. /* not used now */
  518. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  519. struct ip_vs_protocol *);
  520. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  521. int * timeout_table;
  522. int * timeouts;
  523. int timeouts_size;
  524. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  525. int *verdict, struct ip_vs_conn **cpp);
  526. struct ip_vs_conn *
  527. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  528. const struct iphdr *iph, unsigned int proto_off,
  529. int inverse);
  530. struct ip_vs_conn *
  531. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  532. const struct iphdr *iph, unsigned int proto_off,
  533. int inverse);
  534. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  535. const struct sk_buff *skb,
  536. struct ip_vs_app *app);
  537. void (*timeout_change)(struct ip_vs_app *app, int flags);
  538. };
  539. /*
  540. * IPVS core functions
  541. * (from ip_vs_core.c)
  542. */
  543. extern const char *ip_vs_proto_name(unsigned proto);
  544. extern void ip_vs_init_hash_table(struct list_head *table, int rows);
  545. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
  546. #define IP_VS_APP_TYPE_FTP 1
  547. /*
  548. * ip_vs_conn handling functions
  549. * (from ip_vs_conn.c)
  550. */
  551. enum {
  552. IP_VS_DIR_INPUT = 0,
  553. IP_VS_DIR_OUTPUT,
  554. IP_VS_DIR_INPUT_ONLY,
  555. IP_VS_DIR_LAST,
  556. };
  557. static inline void ip_vs_conn_fill_param(int af, int protocol,
  558. const union nf_inet_addr *caddr,
  559. __be16 cport,
  560. const union nf_inet_addr *vaddr,
  561. __be16 vport,
  562. struct ip_vs_conn_param *p)
  563. {
  564. p->af = af;
  565. p->protocol = protocol;
  566. p->caddr = caddr;
  567. p->cport = cport;
  568. p->vaddr = vaddr;
  569. p->vport = vport;
  570. p->pe = NULL;
  571. p->pe_data = NULL;
  572. }
  573. struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
  574. struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
  575. struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
  576. struct ip_vs_protocol *pp,
  577. const struct ip_vs_iphdr *iph,
  578. unsigned int proto_off,
  579. int inverse);
  580. struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
  581. struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
  582. struct ip_vs_protocol *pp,
  583. const struct ip_vs_iphdr *iph,
  584. unsigned int proto_off,
  585. int inverse);
  586. /* put back the conn without restarting its timer */
  587. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  588. {
  589. atomic_dec(&cp->refcnt);
  590. }
  591. extern void ip_vs_conn_put(struct ip_vs_conn *cp);
  592. extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  593. struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p,
  594. const union nf_inet_addr *daddr,
  595. __be16 dport, unsigned flags,
  596. struct ip_vs_dest *dest);
  597. extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  598. extern const char * ip_vs_state_name(__u16 proto, int state);
  599. extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
  600. extern int ip_vs_check_template(struct ip_vs_conn *ct);
  601. extern void ip_vs_random_dropentry(void);
  602. extern int ip_vs_conn_init(void);
  603. extern void ip_vs_conn_cleanup(void);
  604. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  605. {
  606. struct ip_vs_conn *ctl_cp = cp->control;
  607. if (!ctl_cp) {
  608. IP_VS_ERR_BUF("request control DEL for uncontrolled: "
  609. "%s:%d to %s:%d\n",
  610. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  611. ntohs(cp->cport),
  612. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  613. ntohs(cp->vport));
  614. return;
  615. }
  616. IP_VS_DBG_BUF(7, "DELeting control for: "
  617. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  618. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  619. ntohs(cp->cport),
  620. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  621. ntohs(ctl_cp->cport));
  622. cp->control = NULL;
  623. if (atomic_read(&ctl_cp->n_control) == 0) {
  624. IP_VS_ERR_BUF("BUG control DEL with n=0 : "
  625. "%s:%d to %s:%d\n",
  626. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  627. ntohs(cp->cport),
  628. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  629. ntohs(cp->vport));
  630. return;
  631. }
  632. atomic_dec(&ctl_cp->n_control);
  633. }
  634. static inline void
  635. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  636. {
  637. if (cp->control) {
  638. IP_VS_ERR_BUF("request control ADD for already controlled: "
  639. "%s:%d to %s:%d\n",
  640. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  641. ntohs(cp->cport),
  642. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  643. ntohs(cp->vport));
  644. ip_vs_control_del(cp);
  645. }
  646. IP_VS_DBG_BUF(7, "ADDing control for: "
  647. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  648. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  649. ntohs(cp->cport),
  650. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  651. ntohs(ctl_cp->cport));
  652. cp->control = ctl_cp;
  653. atomic_inc(&ctl_cp->n_control);
  654. }
  655. /*
  656. * IPVS application functions
  657. * (from ip_vs_app.c)
  658. */
  659. #define IP_VS_APP_MAX_PORTS 8
  660. extern int register_ip_vs_app(struct ip_vs_app *app);
  661. extern void unregister_ip_vs_app(struct ip_vs_app *app);
  662. extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  663. extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
  664. extern int
  665. register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
  666. extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
  667. extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
  668. extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
  669. extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
  670. extern int ip_vs_app_init(void);
  671. extern void ip_vs_app_cleanup(void);
  672. void ip_vs_bind_pe(struct ip_vs_service *svc, struct ip_vs_pe *pe);
  673. void ip_vs_unbind_pe(struct ip_vs_service *svc);
  674. int register_ip_vs_pe(struct ip_vs_pe *pe);
  675. int unregister_ip_vs_pe(struct ip_vs_pe *pe);
  676. extern struct ip_vs_pe *ip_vs_pe_get(const char *name);
  677. extern void ip_vs_pe_put(struct ip_vs_pe *pe);
  678. /*
  679. * IPVS protocol functions (from ip_vs_proto.c)
  680. */
  681. extern int ip_vs_protocol_init(void);
  682. extern void ip_vs_protocol_cleanup(void);
  683. extern void ip_vs_protocol_timeout_change(int flags);
  684. extern int *ip_vs_create_timeout_table(int *table, int size);
  685. extern int
  686. ip_vs_set_state_timeout(int *table, int num, const char *const *names,
  687. const char *name, int to);
  688. extern void
  689. ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
  690. int offset, const char *msg);
  691. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  692. extern struct ip_vs_protocol ip_vs_protocol_udp;
  693. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  694. extern struct ip_vs_protocol ip_vs_protocol_esp;
  695. extern struct ip_vs_protocol ip_vs_protocol_ah;
  696. extern struct ip_vs_protocol ip_vs_protocol_sctp;
  697. /*
  698. * Registering/unregistering scheduler functions
  699. * (from ip_vs_sched.c)
  700. */
  701. extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  702. extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  703. extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  704. struct ip_vs_scheduler *scheduler);
  705. extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
  706. extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  707. extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  708. extern struct ip_vs_conn *
  709. ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb);
  710. extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  711. struct ip_vs_protocol *pp);
  712. /*
  713. * IPVS control data and functions (from ip_vs_ctl.c)
  714. */
  715. extern int sysctl_ip_vs_cache_bypass;
  716. extern int sysctl_ip_vs_expire_nodest_conn;
  717. extern int sysctl_ip_vs_expire_quiescent_template;
  718. extern int sysctl_ip_vs_sync_threshold[2];
  719. extern int sysctl_ip_vs_nat_icmp_send;
  720. extern int sysctl_ip_vs_conntrack;
  721. extern int sysctl_ip_vs_snat_reroute;
  722. extern struct ip_vs_stats ip_vs_stats;
  723. extern const struct ctl_path net_vs_ctl_path[];
  724. extern struct ip_vs_service *
  725. ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
  726. const union nf_inet_addr *vaddr, __be16 vport);
  727. static inline void ip_vs_service_put(struct ip_vs_service *svc)
  728. {
  729. atomic_dec(&svc->usecnt);
  730. }
  731. extern struct ip_vs_dest *
  732. ip_vs_lookup_real_service(int af, __u16 protocol,
  733. const union nf_inet_addr *daddr, __be16 dport);
  734. extern int ip_vs_use_count_inc(void);
  735. extern void ip_vs_use_count_dec(void);
  736. extern int ip_vs_control_init(void);
  737. extern void ip_vs_control_cleanup(void);
  738. extern struct ip_vs_dest *
  739. ip_vs_find_dest(int af, const union nf_inet_addr *daddr, __be16 dport,
  740. const union nf_inet_addr *vaddr, __be16 vport, __u16 protocol);
  741. extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  742. /*
  743. * IPVS sync daemon data and function prototypes
  744. * (from ip_vs_sync.c)
  745. */
  746. extern volatile int ip_vs_sync_state;
  747. extern volatile int ip_vs_master_syncid;
  748. extern volatile int ip_vs_backup_syncid;
  749. extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  750. extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  751. extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
  752. extern int stop_sync_thread(int state);
  753. extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
  754. /*
  755. * IPVS rate estimator prototypes (from ip_vs_est.c)
  756. */
  757. extern int ip_vs_estimator_init(void);
  758. extern void ip_vs_estimator_cleanup(void);
  759. extern void ip_vs_new_estimator(struct ip_vs_stats *stats);
  760. extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
  761. extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  762. /*
  763. * Various IPVS packet transmitters (from ip_vs_xmit.c)
  764. */
  765. extern int ip_vs_null_xmit
  766. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  767. extern int ip_vs_bypass_xmit
  768. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  769. extern int ip_vs_nat_xmit
  770. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  771. extern int ip_vs_tunnel_xmit
  772. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  773. extern int ip_vs_dr_xmit
  774. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  775. extern int ip_vs_icmp_xmit
  776. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
  777. extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
  778. #ifdef CONFIG_IP_VS_IPV6
  779. extern int ip_vs_bypass_xmit_v6
  780. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  781. extern int ip_vs_nat_xmit_v6
  782. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  783. extern int ip_vs_tunnel_xmit_v6
  784. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  785. extern int ip_vs_dr_xmit_v6
  786. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  787. extern int ip_vs_icmp_xmit_v6
  788. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
  789. int offset);
  790. #endif
  791. /*
  792. * This is a simple mechanism to ignore packets when
  793. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  794. * we start to drop 1/rate of the packets
  795. */
  796. extern int ip_vs_drop_rate;
  797. extern int ip_vs_drop_counter;
  798. static __inline__ int ip_vs_todrop(void)
  799. {
  800. if (!ip_vs_drop_rate) return 0;
  801. if (--ip_vs_drop_counter > 0) return 0;
  802. ip_vs_drop_counter = ip_vs_drop_rate;
  803. return 1;
  804. }
  805. /*
  806. * ip_vs_fwd_tag returns the forwarding tag of the connection
  807. */
  808. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  809. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  810. {
  811. char fwd;
  812. switch (IP_VS_FWD_METHOD(cp)) {
  813. case IP_VS_CONN_F_MASQ:
  814. fwd = 'M'; break;
  815. case IP_VS_CONN_F_LOCALNODE:
  816. fwd = 'L'; break;
  817. case IP_VS_CONN_F_TUNNEL:
  818. fwd = 'T'; break;
  819. case IP_VS_CONN_F_DROUTE:
  820. fwd = 'R'; break;
  821. case IP_VS_CONN_F_BYPASS:
  822. fwd = 'B'; break;
  823. default:
  824. fwd = '?'; break;
  825. }
  826. return fwd;
  827. }
  828. extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  829. struct ip_vs_conn *cp, int dir);
  830. #ifdef CONFIG_IP_VS_IPV6
  831. extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  832. struct ip_vs_conn *cp, int dir);
  833. #endif
  834. extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  835. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  836. {
  837. __be32 diff[2] = { ~old, new };
  838. return csum_partial(diff, sizeof(diff), oldsum);
  839. }
  840. #ifdef CONFIG_IP_VS_IPV6
  841. static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
  842. __wsum oldsum)
  843. {
  844. __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
  845. new[3], new[2], new[1], new[0] };
  846. return csum_partial(diff, sizeof(diff), oldsum);
  847. }
  848. #endif
  849. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  850. {
  851. __be16 diff[2] = { ~old, new };
  852. return csum_partial(diff, sizeof(diff), oldsum);
  853. }
  854. #ifdef CONFIG_IP_VS_NFCT
  855. /*
  856. * Netfilter connection tracking
  857. * (from ip_vs_nfct.c)
  858. */
  859. static inline int ip_vs_conntrack_enabled(void)
  860. {
  861. return sysctl_ip_vs_conntrack;
  862. }
  863. extern void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
  864. int outin);
  865. extern int ip_vs_confirm_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp);
  866. extern void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
  867. struct ip_vs_conn *cp, u_int8_t proto,
  868. const __be16 port, int from_rs);
  869. extern void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
  870. #else
  871. static inline int ip_vs_conntrack_enabled(void)
  872. {
  873. return 0;
  874. }
  875. static inline void ip_vs_update_conntrack(struct sk_buff *skb,
  876. struct ip_vs_conn *cp, int outin)
  877. {
  878. }
  879. static inline int ip_vs_confirm_conntrack(struct sk_buff *skb,
  880. struct ip_vs_conn *cp)
  881. {
  882. return NF_ACCEPT;
  883. }
  884. static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
  885. {
  886. }
  887. /* CONFIG_IP_VS_NFCT */
  888. #endif
  889. #endif /* __KERNEL__ */
  890. #endif /* _NET_IP_VS_H */