ip_vs.h 27 KB

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