ip_vs.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. /*
  306. * IP_VS structure allocated for each dynamically scheduled connection
  307. */
  308. struct ip_vs_conn {
  309. struct list_head c_list; /* hashed list heads */
  310. /* Protocol, addresses and port numbers */
  311. u16 af; /* address family */
  312. union nf_inet_addr caddr; /* client address */
  313. union nf_inet_addr vaddr; /* virtual address */
  314. union nf_inet_addr daddr; /* destination address */
  315. volatile __u32 flags; /* status flags */
  316. __be16 cport;
  317. __be16 vport;
  318. __be16 dport;
  319. __u16 protocol; /* Which protocol (TCP/UDP) */
  320. /* counter and timer */
  321. atomic_t refcnt; /* reference count */
  322. struct timer_list timer; /* Expiration timer */
  323. volatile unsigned long timeout; /* timeout */
  324. /* Flags and state transition */
  325. spinlock_t lock; /* lock for state transition */
  326. volatile __u16 state; /* state info */
  327. volatile __u16 old_state; /* old state, to be used for
  328. * state transition triggerd
  329. * synchronization
  330. */
  331. /* Control members */
  332. struct ip_vs_conn *control; /* Master control connection */
  333. atomic_t n_control; /* Number of controlled ones */
  334. struct ip_vs_dest *dest; /* real server */
  335. atomic_t in_pkts; /* incoming packet counter */
  336. /* packet transmitter for different forwarding methods. If it
  337. mangles the packet, it must return NF_DROP or better NF_STOLEN,
  338. otherwise this must be changed to a sk_buff **.
  339. */
  340. int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
  341. struct ip_vs_protocol *pp);
  342. /* Note: we can group the following members into a structure,
  343. in order to save more space, and the following members are
  344. only used in VS/NAT anyway */
  345. struct ip_vs_app *app; /* bound ip_vs_app object */
  346. void *app_data; /* Application private data */
  347. struct ip_vs_seq in_seq; /* incoming seq. struct */
  348. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  349. };
  350. /*
  351. * Extended internal versions of struct ip_vs_service_user and
  352. * ip_vs_dest_user for IPv6 support.
  353. *
  354. * We need these to conveniently pass around service and destination
  355. * options, but unfortunately, we also need to keep the old definitions to
  356. * maintain userspace backwards compatibility for the setsockopt interface.
  357. */
  358. struct ip_vs_service_user_kern {
  359. /* virtual service addresses */
  360. u16 af;
  361. u16 protocol;
  362. union nf_inet_addr addr; /* virtual ip address */
  363. u16 port;
  364. u32 fwmark; /* firwall mark of service */
  365. /* virtual service options */
  366. char *sched_name;
  367. unsigned flags; /* virtual service flags */
  368. unsigned timeout; /* persistent timeout in sec */
  369. u32 netmask; /* persistent netmask */
  370. };
  371. struct ip_vs_dest_user_kern {
  372. /* destination server address */
  373. union nf_inet_addr addr;
  374. u16 port;
  375. /* real server options */
  376. unsigned conn_flags; /* connection flags */
  377. int weight; /* destination weight */
  378. /* thresholds for active connections */
  379. u32 u_threshold; /* upper threshold */
  380. u32 l_threshold; /* lower threshold */
  381. };
  382. /*
  383. * The information about the virtual service offered to the net
  384. * and the forwarding entries
  385. */
  386. struct ip_vs_service {
  387. struct list_head s_list; /* for normal service table */
  388. struct list_head f_list; /* for fwmark-based service table */
  389. atomic_t refcnt; /* reference counter */
  390. atomic_t usecnt; /* use counter */
  391. u16 af; /* address family */
  392. __u16 protocol; /* which protocol (TCP/UDP) */
  393. union nf_inet_addr addr; /* IP address for virtual service */
  394. __be16 port; /* port number for the service */
  395. __u32 fwmark; /* firewall mark of the service */
  396. unsigned flags; /* service status flags */
  397. unsigned timeout; /* persistent timeout in ticks */
  398. __be32 netmask; /* grouping granularity */
  399. struct list_head destinations; /* real server d-linked list */
  400. __u32 num_dests; /* number of servers */
  401. struct ip_vs_stats stats; /* statistics for the service */
  402. struct ip_vs_app *inc; /* bind conns to this app inc */
  403. /* for scheduling */
  404. struct ip_vs_scheduler *scheduler; /* bound scheduler object */
  405. rwlock_t sched_lock; /* lock sched_data */
  406. void *sched_data; /* scheduler application data */
  407. };
  408. /*
  409. * The real server destination forwarding entry
  410. * with ip address, port number, and so on.
  411. */
  412. struct ip_vs_dest {
  413. struct list_head n_list; /* for the dests in the service */
  414. struct list_head d_list; /* for table with all the dests */
  415. u16 af; /* address family */
  416. union nf_inet_addr addr; /* IP address of the server */
  417. __be16 port; /* port number of the server */
  418. volatile unsigned flags; /* dest status flags */
  419. atomic_t conn_flags; /* flags to copy to conn */
  420. atomic_t weight; /* server weight */
  421. atomic_t refcnt; /* reference counter */
  422. struct ip_vs_stats stats; /* statistics */
  423. /* connection counters and thresholds */
  424. atomic_t activeconns; /* active connections */
  425. atomic_t inactconns; /* inactive connections */
  426. atomic_t persistconns; /* persistent connections */
  427. __u32 u_threshold; /* upper threshold */
  428. __u32 l_threshold; /* lower threshold */
  429. /* for destination cache */
  430. spinlock_t dst_lock; /* lock of dst_cache */
  431. struct dst_entry *dst_cache; /* destination cache entry */
  432. u32 dst_rtos; /* RT_TOS(tos) for dst */
  433. /* for virtual service */
  434. struct ip_vs_service *svc; /* service it belongs to */
  435. __u16 protocol; /* which protocol (TCP/UDP) */
  436. union nf_inet_addr vaddr; /* virtual IP address */
  437. __be16 vport; /* virtual port number */
  438. __u32 vfwmark; /* firewall mark of service */
  439. };
  440. /*
  441. * The scheduler object
  442. */
  443. struct ip_vs_scheduler {
  444. struct list_head n_list; /* d-linked list head */
  445. char *name; /* scheduler name */
  446. atomic_t refcnt; /* reference counter */
  447. struct module *module; /* THIS_MODULE/NULL */
  448. /* scheduler initializing service */
  449. int (*init_service)(struct ip_vs_service *svc);
  450. /* scheduling service finish */
  451. int (*done_service)(struct ip_vs_service *svc);
  452. /* scheduler updating service */
  453. int (*update_service)(struct ip_vs_service *svc);
  454. /* selecting a server from the given service */
  455. struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
  456. const struct sk_buff *skb);
  457. };
  458. /*
  459. * The application module object (a.k.a. app incarnation)
  460. */
  461. struct ip_vs_app {
  462. struct list_head a_list; /* member in app list */
  463. int type; /* IP_VS_APP_TYPE_xxx */
  464. char *name; /* application module name */
  465. __u16 protocol;
  466. struct module *module; /* THIS_MODULE/NULL */
  467. struct list_head incs_list; /* list of incarnations */
  468. /* members for application incarnations */
  469. struct list_head p_list; /* member in proto app list */
  470. struct ip_vs_app *app; /* its real application */
  471. __be16 port; /* port number in net order */
  472. atomic_t usecnt; /* usage counter */
  473. /* output hook: return false if can't linearize. diff set for TCP. */
  474. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  475. struct sk_buff *, int *diff);
  476. /* input hook: return false if can't linearize. diff set for TCP. */
  477. int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
  478. struct sk_buff *, int *diff);
  479. /* ip_vs_app initializer */
  480. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  481. /* ip_vs_app finish */
  482. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  483. /* not used now */
  484. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  485. struct ip_vs_protocol *);
  486. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  487. int * timeout_table;
  488. int * timeouts;
  489. int timeouts_size;
  490. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  491. int *verdict, struct ip_vs_conn **cpp);
  492. struct ip_vs_conn *
  493. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  494. const struct iphdr *iph, unsigned int proto_off,
  495. int inverse);
  496. struct ip_vs_conn *
  497. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  498. const struct iphdr *iph, unsigned int proto_off,
  499. int inverse);
  500. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  501. const struct sk_buff *skb,
  502. struct ip_vs_app *app);
  503. void (*timeout_change)(struct ip_vs_app *app, int flags);
  504. };
  505. /*
  506. * IPVS core functions
  507. * (from ip_vs_core.c)
  508. */
  509. extern const char *ip_vs_proto_name(unsigned proto);
  510. extern void ip_vs_init_hash_table(struct list_head *table, int rows);
  511. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
  512. #define IP_VS_APP_TYPE_FTP 1
  513. /*
  514. * ip_vs_conn handling functions
  515. * (from ip_vs_conn.c)
  516. */
  517. enum {
  518. IP_VS_DIR_INPUT = 0,
  519. IP_VS_DIR_OUTPUT,
  520. IP_VS_DIR_INPUT_ONLY,
  521. IP_VS_DIR_LAST,
  522. };
  523. extern struct ip_vs_conn *ip_vs_conn_in_get
  524. (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
  525. const union nf_inet_addr *d_addr, __be16 d_port);
  526. extern struct ip_vs_conn *ip_vs_ct_in_get
  527. (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
  528. const union nf_inet_addr *d_addr, __be16 d_port);
  529. struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
  530. struct ip_vs_protocol *pp,
  531. const struct ip_vs_iphdr *iph,
  532. unsigned int proto_off,
  533. int inverse);
  534. extern struct ip_vs_conn *ip_vs_conn_out_get
  535. (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
  536. const union nf_inet_addr *d_addr, __be16 d_port);
  537. struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
  538. struct ip_vs_protocol *pp,
  539. const struct ip_vs_iphdr *iph,
  540. unsigned int proto_off,
  541. int inverse);
  542. /* put back the conn without restarting its timer */
  543. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  544. {
  545. atomic_dec(&cp->refcnt);
  546. }
  547. extern void ip_vs_conn_put(struct ip_vs_conn *cp);
  548. extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  549. extern struct ip_vs_conn *
  550. ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
  551. const union nf_inet_addr *vaddr, __be16 vport,
  552. const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
  553. struct ip_vs_dest *dest);
  554. extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  555. extern const char * ip_vs_state_name(__u16 proto, int state);
  556. extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
  557. extern int ip_vs_check_template(struct ip_vs_conn *ct);
  558. extern void ip_vs_random_dropentry(void);
  559. extern int ip_vs_conn_init(void);
  560. extern void ip_vs_conn_cleanup(void);
  561. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  562. {
  563. struct ip_vs_conn *ctl_cp = cp->control;
  564. if (!ctl_cp) {
  565. IP_VS_ERR_BUF("request control DEL for uncontrolled: "
  566. "%s:%d to %s:%d\n",
  567. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  568. ntohs(cp->cport),
  569. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  570. ntohs(cp->vport));
  571. return;
  572. }
  573. IP_VS_DBG_BUF(7, "DELeting control for: "
  574. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  575. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  576. ntohs(cp->cport),
  577. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  578. ntohs(ctl_cp->cport));
  579. cp->control = NULL;
  580. if (atomic_read(&ctl_cp->n_control) == 0) {
  581. IP_VS_ERR_BUF("BUG control DEL with n=0 : "
  582. "%s:%d to %s:%d\n",
  583. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  584. ntohs(cp->cport),
  585. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  586. ntohs(cp->vport));
  587. return;
  588. }
  589. atomic_dec(&ctl_cp->n_control);
  590. }
  591. static inline void
  592. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  593. {
  594. if (cp->control) {
  595. IP_VS_ERR_BUF("request control ADD for already controlled: "
  596. "%s:%d to %s:%d\n",
  597. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  598. ntohs(cp->cport),
  599. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  600. ntohs(cp->vport));
  601. ip_vs_control_del(cp);
  602. }
  603. IP_VS_DBG_BUF(7, "ADDing control for: "
  604. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  605. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  606. ntohs(cp->cport),
  607. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  608. ntohs(ctl_cp->cport));
  609. cp->control = ctl_cp;
  610. atomic_inc(&ctl_cp->n_control);
  611. }
  612. /*
  613. * IPVS application functions
  614. * (from ip_vs_app.c)
  615. */
  616. #define IP_VS_APP_MAX_PORTS 8
  617. extern int register_ip_vs_app(struct ip_vs_app *app);
  618. extern void unregister_ip_vs_app(struct ip_vs_app *app);
  619. extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  620. extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
  621. extern int
  622. register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
  623. extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
  624. extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
  625. extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
  626. extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
  627. extern int ip_vs_app_init(void);
  628. extern void ip_vs_app_cleanup(void);
  629. /*
  630. * IPVS protocol functions (from ip_vs_proto.c)
  631. */
  632. extern int ip_vs_protocol_init(void);
  633. extern void ip_vs_protocol_cleanup(void);
  634. extern void ip_vs_protocol_timeout_change(int flags);
  635. extern int *ip_vs_create_timeout_table(int *table, int size);
  636. extern int
  637. ip_vs_set_state_timeout(int *table, int num, const char *const *names,
  638. const char *name, int to);
  639. extern void
  640. ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
  641. int offset, const char *msg);
  642. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  643. extern struct ip_vs_protocol ip_vs_protocol_udp;
  644. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  645. extern struct ip_vs_protocol ip_vs_protocol_esp;
  646. extern struct ip_vs_protocol ip_vs_protocol_ah;
  647. extern struct ip_vs_protocol ip_vs_protocol_sctp;
  648. /*
  649. * Registering/unregistering scheduler functions
  650. * (from ip_vs_sched.c)
  651. */
  652. extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  653. extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  654. extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  655. struct ip_vs_scheduler *scheduler);
  656. extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
  657. extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  658. extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  659. extern struct ip_vs_conn *
  660. ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb);
  661. extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  662. struct ip_vs_protocol *pp);
  663. /*
  664. * IPVS control data and functions (from ip_vs_ctl.c)
  665. */
  666. extern int sysctl_ip_vs_cache_bypass;
  667. extern int sysctl_ip_vs_expire_nodest_conn;
  668. extern int sysctl_ip_vs_expire_quiescent_template;
  669. extern int sysctl_ip_vs_sync_threshold[2];
  670. extern int sysctl_ip_vs_nat_icmp_send;
  671. extern int sysctl_ip_vs_conntrack;
  672. extern int sysctl_ip_vs_snat_reroute;
  673. extern struct ip_vs_stats ip_vs_stats;
  674. extern const struct ctl_path net_vs_ctl_path[];
  675. extern struct ip_vs_service *
  676. ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
  677. const union nf_inet_addr *vaddr, __be16 vport);
  678. static inline void ip_vs_service_put(struct ip_vs_service *svc)
  679. {
  680. atomic_dec(&svc->usecnt);
  681. }
  682. extern struct ip_vs_dest *
  683. ip_vs_lookup_real_service(int af, __u16 protocol,
  684. const union nf_inet_addr *daddr, __be16 dport);
  685. extern int ip_vs_use_count_inc(void);
  686. extern void ip_vs_use_count_dec(void);
  687. extern int ip_vs_control_init(void);
  688. extern void ip_vs_control_cleanup(void);
  689. extern struct ip_vs_dest *
  690. ip_vs_find_dest(int af, const union nf_inet_addr *daddr, __be16 dport,
  691. const union nf_inet_addr *vaddr, __be16 vport, __u16 protocol);
  692. extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  693. /*
  694. * IPVS sync daemon data and function prototypes
  695. * (from ip_vs_sync.c)
  696. */
  697. extern volatile int ip_vs_sync_state;
  698. extern volatile int ip_vs_master_syncid;
  699. extern volatile int ip_vs_backup_syncid;
  700. extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  701. extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  702. extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
  703. extern int stop_sync_thread(int state);
  704. extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
  705. /*
  706. * IPVS rate estimator prototypes (from ip_vs_est.c)
  707. */
  708. extern int ip_vs_estimator_init(void);
  709. extern void ip_vs_estimator_cleanup(void);
  710. extern void ip_vs_new_estimator(struct ip_vs_stats *stats);
  711. extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
  712. extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  713. /*
  714. * Various IPVS packet transmitters (from ip_vs_xmit.c)
  715. */
  716. extern int ip_vs_null_xmit
  717. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  718. extern int ip_vs_bypass_xmit
  719. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  720. extern int ip_vs_nat_xmit
  721. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  722. extern int ip_vs_tunnel_xmit
  723. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  724. extern int ip_vs_dr_xmit
  725. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  726. extern int ip_vs_icmp_xmit
  727. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
  728. extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
  729. #ifdef CONFIG_IP_VS_IPV6
  730. extern int ip_vs_bypass_xmit_v6
  731. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  732. extern int ip_vs_nat_xmit_v6
  733. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  734. extern int ip_vs_tunnel_xmit_v6
  735. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  736. extern int ip_vs_dr_xmit_v6
  737. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  738. extern int ip_vs_icmp_xmit_v6
  739. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
  740. int offset);
  741. #endif
  742. /*
  743. * This is a simple mechanism to ignore packets when
  744. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  745. * we start to drop 1/rate of the packets
  746. */
  747. extern int ip_vs_drop_rate;
  748. extern int ip_vs_drop_counter;
  749. static __inline__ int ip_vs_todrop(void)
  750. {
  751. if (!ip_vs_drop_rate) return 0;
  752. if (--ip_vs_drop_counter > 0) return 0;
  753. ip_vs_drop_counter = ip_vs_drop_rate;
  754. return 1;
  755. }
  756. /*
  757. * ip_vs_fwd_tag returns the forwarding tag of the connection
  758. */
  759. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  760. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  761. {
  762. char fwd;
  763. switch (IP_VS_FWD_METHOD(cp)) {
  764. case IP_VS_CONN_F_MASQ:
  765. fwd = 'M'; break;
  766. case IP_VS_CONN_F_LOCALNODE:
  767. fwd = 'L'; break;
  768. case IP_VS_CONN_F_TUNNEL:
  769. fwd = 'T'; break;
  770. case IP_VS_CONN_F_DROUTE:
  771. fwd = 'R'; break;
  772. case IP_VS_CONN_F_BYPASS:
  773. fwd = 'B'; break;
  774. default:
  775. fwd = '?'; break;
  776. }
  777. return fwd;
  778. }
  779. extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  780. struct ip_vs_conn *cp, int dir);
  781. #ifdef CONFIG_IP_VS_IPV6
  782. extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  783. struct ip_vs_conn *cp, int dir);
  784. #endif
  785. extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  786. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  787. {
  788. __be32 diff[2] = { ~old, new };
  789. return csum_partial(diff, sizeof(diff), oldsum);
  790. }
  791. #ifdef CONFIG_IP_VS_IPV6
  792. static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
  793. __wsum oldsum)
  794. {
  795. __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
  796. new[3], new[2], new[1], new[0] };
  797. return csum_partial(diff, sizeof(diff), oldsum);
  798. }
  799. #endif
  800. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  801. {
  802. __be16 diff[2] = { ~old, new };
  803. return csum_partial(diff, sizeof(diff), oldsum);
  804. }
  805. #ifdef CONFIG_IP_VS_NFCT
  806. /*
  807. * Netfilter connection tracking
  808. * (from ip_vs_nfct.c)
  809. */
  810. static inline int ip_vs_conntrack_enabled(void)
  811. {
  812. return sysctl_ip_vs_conntrack;
  813. }
  814. extern void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
  815. int outin);
  816. extern int ip_vs_confirm_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp);
  817. extern void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
  818. struct ip_vs_conn *cp, u_int8_t proto,
  819. const __be16 port, int from_rs);
  820. extern void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
  821. #else
  822. static inline int ip_vs_conntrack_enabled(void)
  823. {
  824. return 0;
  825. }
  826. static inline void ip_vs_update_conntrack(struct sk_buff *skb,
  827. struct ip_vs_conn *cp, int outin)
  828. {
  829. }
  830. static inline int ip_vs_confirm_conntrack(struct sk_buff *skb,
  831. struct ip_vs_conn *cp)
  832. {
  833. return NF_ACCEPT;
  834. }
  835. static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
  836. {
  837. }
  838. /* CONFIG_IP_VS_NFCT */
  839. #endif
  840. #endif /* __KERNEL__ */
  841. #endif /* _NET_IP_VS_H */