ip_vs.h 30 KB

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