ip_vs.h 32 KB

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