ip_vs.h 27 KB

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