ip_vs.h 27 KB

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