ip_vs.h 31 KB

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