ip_vs.h 29 KB

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