ip_vs.h 28 KB

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