ip_vs.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /*
  2. * IP Virtual Server
  3. * data structure and functionality definitions
  4. */
  5. #ifndef _NET_IP_VS_H
  6. #define _NET_IP_VS_H
  7. #include <linux/ip_vs.h> /* definitions shared with userland */
  8. /* old ipvsadm versions still include this file directly */
  9. #ifdef __KERNEL__
  10. #include <asm/types.h> /* for __uXX types */
  11. #include <linux/sysctl.h> /* for ctl_path */
  12. #include <linux/list.h> /* for struct list_head */
  13. #include <linux/spinlock.h> /* for struct rwlock_t */
  14. #include <asm/atomic.h> /* for struct atomic_t */
  15. #include <linux/compiler.h>
  16. #include <linux/timer.h>
  17. #include <net/checksum.h>
  18. #include <linux/netfilter.h> /* for union nf_inet_addr */
  19. #include <linux/ip.h>
  20. #include <linux/ipv6.h> /* for struct ipv6hdr */
  21. #include <net/ipv6.h> /* for ipv6_addr_copy */
  22. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  23. #include <net/netfilter/nf_conntrack.h>
  24. #endif
  25. #include <net/net_namespace.h> /* Netw namespace */
  26. /*
  27. * Generic access of ipvs struct
  28. */
  29. static inline struct netns_ipvs *net_ipvs(struct net* net)
  30. {
  31. return net->ipvs;
  32. }
  33. /*
  34. * Get net ptr from skb in traffic cases
  35. * use skb_sknet when call is from userland (ioctl or netlink)
  36. */
  37. static inline struct net *skb_net(const struct sk_buff *skb)
  38. {
  39. #ifdef CONFIG_NET_NS
  40. #ifdef CONFIG_IP_VS_DEBUG
  41. /*
  42. * This is used for debug only.
  43. * Start with the most likely hit
  44. * End with BUG
  45. */
  46. if (likely(skb->dev && skb->dev->nd_net))
  47. return dev_net(skb->dev);
  48. if (skb_dst(skb)->dev)
  49. return dev_net(skb_dst(skb)->dev);
  50. WARN(skb->sk, "Maybe skb_sknet should be used in %s() at line:%d\n",
  51. __func__, __LINE__);
  52. if (likely(skb->sk && skb->sk->sk_net))
  53. return sock_net(skb->sk);
  54. pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
  55. __func__, __LINE__);
  56. BUG();
  57. #else
  58. return dev_net(skb->dev ? : skb_dst(skb)->dev);
  59. #endif
  60. #else
  61. return &init_net;
  62. #endif
  63. }
  64. static inline struct net *skb_sknet(const struct sk_buff *skb)
  65. {
  66. #ifdef CONFIG_NET_NS
  67. #ifdef CONFIG_IP_VS_DEBUG
  68. /* Start with the most likely hit */
  69. if (likely(skb->sk && skb->sk->sk_net))
  70. return sock_net(skb->sk);
  71. WARN(skb->dev, "Maybe skb_net should be used instead in %s() line:%d\n",
  72. __func__, __LINE__);
  73. if (likely(skb->dev && skb->dev->nd_net))
  74. return dev_net(skb->dev);
  75. pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
  76. __func__, __LINE__);
  77. BUG();
  78. #else
  79. return sock_net(skb->sk);
  80. #endif
  81. #else
  82. return &init_net;
  83. #endif
  84. }
  85. /*
  86. * This one needed for single_open_net since net is stored directly in
  87. * private not as a struct i.e. seq_file_net cant be used.
  88. */
  89. static inline struct net *seq_file_single_net(struct seq_file *seq)
  90. {
  91. #ifdef CONFIG_NET_NS
  92. return (struct net *)seq->private;
  93. #else
  94. return &init_net;
  95. #endif
  96. }
  97. /* Connections' size value needed by ip_vs_ctl.c */
  98. extern int ip_vs_conn_tab_size;
  99. struct ip_vs_iphdr {
  100. int len;
  101. __u8 protocol;
  102. union nf_inet_addr saddr;
  103. union nf_inet_addr daddr;
  104. };
  105. static inline void
  106. ip_vs_fill_iphdr(int af, const void *nh, struct ip_vs_iphdr *iphdr)
  107. {
  108. #ifdef CONFIG_IP_VS_IPV6
  109. if (af == AF_INET6) {
  110. const struct ipv6hdr *iph = nh;
  111. iphdr->len = sizeof(struct ipv6hdr);
  112. iphdr->protocol = iph->nexthdr;
  113. ipv6_addr_copy(&iphdr->saddr.in6, &iph->saddr);
  114. ipv6_addr_copy(&iphdr->daddr.in6, &iph->daddr);
  115. } else
  116. #endif
  117. {
  118. const struct iphdr *iph = nh;
  119. iphdr->len = iph->ihl * 4;
  120. iphdr->protocol = iph->protocol;
  121. iphdr->saddr.ip = iph->saddr;
  122. iphdr->daddr.ip = iph->daddr;
  123. }
  124. }
  125. static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
  126. const union nf_inet_addr *src)
  127. {
  128. #ifdef CONFIG_IP_VS_IPV6
  129. if (af == AF_INET6)
  130. ipv6_addr_copy(&dst->in6, &src->in6);
  131. else
  132. #endif
  133. dst->ip = src->ip;
  134. }
  135. static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
  136. const union nf_inet_addr *b)
  137. {
  138. #ifdef CONFIG_IP_VS_IPV6
  139. if (af == AF_INET6)
  140. return ipv6_addr_equal(&a->in6, &b->in6);
  141. #endif
  142. return a->ip == b->ip;
  143. }
  144. #ifdef CONFIG_IP_VS_DEBUG
  145. #include <linux/net.h>
  146. extern int ip_vs_get_debug_level(void);
  147. static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
  148. const union nf_inet_addr *addr,
  149. int *idx)
  150. {
  151. int len;
  152. #ifdef CONFIG_IP_VS_IPV6
  153. if (af == AF_INET6)
  154. len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6]",
  155. &addr->in6) + 1;
  156. else
  157. #endif
  158. len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
  159. &addr->ip) + 1;
  160. *idx += len;
  161. BUG_ON(*idx > buf_len + 1);
  162. return &buf[*idx - len];
  163. }
  164. #define IP_VS_DBG_BUF(level, msg, ...) \
  165. do { \
  166. char ip_vs_dbg_buf[160]; \
  167. int ip_vs_dbg_idx = 0; \
  168. if (level <= ip_vs_get_debug_level()) \
  169. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  170. } while (0)
  171. #define IP_VS_ERR_BUF(msg...) \
  172. do { \
  173. char ip_vs_dbg_buf[160]; \
  174. int ip_vs_dbg_idx = 0; \
  175. pr_err(msg); \
  176. } while (0)
  177. /* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
  178. #define IP_VS_DBG_ADDR(af, addr) \
  179. ip_vs_dbg_addr(af, ip_vs_dbg_buf, \
  180. sizeof(ip_vs_dbg_buf), addr, \
  181. &ip_vs_dbg_idx)
  182. #define IP_VS_DBG(level, msg, ...) \
  183. do { \
  184. if (level <= ip_vs_get_debug_level()) \
  185. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  186. } while (0)
  187. #define IP_VS_DBG_RL(msg, ...) \
  188. do { \
  189. if (net_ratelimit()) \
  190. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  191. } while (0)
  192. #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) \
  193. do { \
  194. if (level <= ip_vs_get_debug_level()) \
  195. pp->debug_packet(af, pp, skb, ofs, msg); \
  196. } while (0)
  197. #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) \
  198. do { \
  199. if (level <= ip_vs_get_debug_level() && \
  200. net_ratelimit()) \
  201. pp->debug_packet(af, pp, skb, ofs, msg); \
  202. } while (0)
  203. #else /* NO DEBUGGING at ALL */
  204. #define IP_VS_DBG_BUF(level, msg...) do {} while (0)
  205. #define IP_VS_ERR_BUF(msg...) do {} while (0)
  206. #define IP_VS_DBG(level, msg...) do {} while (0)
  207. #define IP_VS_DBG_RL(msg...) do {} while (0)
  208. #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) do {} while (0)
  209. #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) do {} while (0)
  210. #endif
  211. #define IP_VS_BUG() BUG()
  212. #define IP_VS_ERR_RL(msg, ...) \
  213. do { \
  214. if (net_ratelimit()) \
  215. pr_err(msg, ##__VA_ARGS__); \
  216. } while (0)
  217. #ifdef CONFIG_IP_VS_DEBUG
  218. #define EnterFunction(level) \
  219. do { \
  220. if (level <= ip_vs_get_debug_level()) \
  221. printk(KERN_DEBUG \
  222. pr_fmt("Enter: %s, %s line %i\n"), \
  223. __func__, __FILE__, __LINE__); \
  224. } while (0)
  225. #define LeaveFunction(level) \
  226. do { \
  227. if (level <= ip_vs_get_debug_level()) \
  228. printk(KERN_DEBUG \
  229. pr_fmt("Leave: %s, %s line %i\n"), \
  230. __func__, __FILE__, __LINE__); \
  231. } while (0)
  232. #else
  233. #define EnterFunction(level) do {} while (0)
  234. #define LeaveFunction(level) do {} while (0)
  235. #endif
  236. #define IP_VS_WAIT_WHILE(expr) while (expr) { cpu_relax(); }
  237. /*
  238. * The port number of FTP service (in network order).
  239. */
  240. #define FTPPORT cpu_to_be16(21)
  241. #define FTPDATA cpu_to_be16(20)
  242. /*
  243. * TCP State Values
  244. */
  245. enum {
  246. IP_VS_TCP_S_NONE = 0,
  247. IP_VS_TCP_S_ESTABLISHED,
  248. IP_VS_TCP_S_SYN_SENT,
  249. IP_VS_TCP_S_SYN_RECV,
  250. IP_VS_TCP_S_FIN_WAIT,
  251. IP_VS_TCP_S_TIME_WAIT,
  252. IP_VS_TCP_S_CLOSE,
  253. IP_VS_TCP_S_CLOSE_WAIT,
  254. IP_VS_TCP_S_LAST_ACK,
  255. IP_VS_TCP_S_LISTEN,
  256. IP_VS_TCP_S_SYNACK,
  257. IP_VS_TCP_S_LAST
  258. };
  259. /*
  260. * UDP State Values
  261. */
  262. enum {
  263. IP_VS_UDP_S_NORMAL,
  264. IP_VS_UDP_S_LAST,
  265. };
  266. /*
  267. * ICMP State Values
  268. */
  269. enum {
  270. IP_VS_ICMP_S_NORMAL,
  271. IP_VS_ICMP_S_LAST,
  272. };
  273. /*
  274. * SCTP State Values
  275. */
  276. enum ip_vs_sctp_states {
  277. IP_VS_SCTP_S_NONE,
  278. IP_VS_SCTP_S_INIT_CLI,
  279. IP_VS_SCTP_S_INIT_SER,
  280. IP_VS_SCTP_S_INIT_ACK_CLI,
  281. IP_VS_SCTP_S_INIT_ACK_SER,
  282. IP_VS_SCTP_S_ECHO_CLI,
  283. IP_VS_SCTP_S_ECHO_SER,
  284. IP_VS_SCTP_S_ESTABLISHED,
  285. IP_VS_SCTP_S_SHUT_CLI,
  286. IP_VS_SCTP_S_SHUT_SER,
  287. IP_VS_SCTP_S_SHUT_ACK_CLI,
  288. IP_VS_SCTP_S_SHUT_ACK_SER,
  289. IP_VS_SCTP_S_CLOSED,
  290. IP_VS_SCTP_S_LAST
  291. };
  292. /*
  293. * Delta sequence info structure
  294. * Each ip_vs_conn has 2 (output AND input seq. changes).
  295. * Only used in the VS/NAT.
  296. */
  297. struct ip_vs_seq {
  298. __u32 init_seq; /* Add delta from this seq */
  299. __u32 delta; /* Delta in sequence numbers */
  300. __u32 previous_delta; /* Delta in sequence numbers
  301. before last resized pkt */
  302. };
  303. /*
  304. * counters per cpu
  305. */
  306. struct ip_vs_counters {
  307. __u32 conns; /* connections scheduled */
  308. __u32 inpkts; /* incoming packets */
  309. __u32 outpkts; /* outgoing packets */
  310. __u64 inbytes; /* incoming bytes */
  311. __u64 outbytes; /* outgoing bytes */
  312. };
  313. /*
  314. * Stats per cpu
  315. */
  316. struct ip_vs_cpu_stats {
  317. struct ip_vs_counters ustats;
  318. struct u64_stats_sync syncp;
  319. };
  320. /*
  321. * IPVS statistics objects
  322. */
  323. struct ip_vs_estimator {
  324. struct list_head list;
  325. u64 last_inbytes;
  326. u64 last_outbytes;
  327. u32 last_conns;
  328. u32 last_inpkts;
  329. u32 last_outpkts;
  330. u32 cps;
  331. u32 inpps;
  332. u32 outpps;
  333. u32 inbps;
  334. u32 outbps;
  335. };
  336. struct ip_vs_stats {
  337. struct ip_vs_stats_user ustats; /* statistics */
  338. struct ip_vs_estimator est; /* estimator */
  339. struct ip_vs_cpu_stats *cpustats; /* per cpu counters */
  340. spinlock_t lock; /* spin lock */
  341. struct ip_vs_stats_user ustats0; /* reset values */
  342. };
  343. struct dst_entry;
  344. struct iphdr;
  345. struct ip_vs_conn;
  346. struct ip_vs_app;
  347. struct sk_buff;
  348. struct ip_vs_proto_data;
  349. struct ip_vs_protocol {
  350. struct ip_vs_protocol *next;
  351. char *name;
  352. u16 protocol;
  353. u16 num_states;
  354. int dont_defrag;
  355. void (*init)(struct ip_vs_protocol *pp);
  356. void (*exit)(struct ip_vs_protocol *pp);
  357. void (*init_netns)(struct net *net, struct ip_vs_proto_data *pd);
  358. void (*exit_netns)(struct net *net, struct ip_vs_proto_data *pd);
  359. int (*conn_schedule)(int af, struct sk_buff *skb,
  360. struct ip_vs_proto_data *pd,
  361. int *verdict, struct ip_vs_conn **cpp);
  362. struct ip_vs_conn *
  363. (*conn_in_get)(int af,
  364. const struct sk_buff *skb,
  365. const struct ip_vs_iphdr *iph,
  366. unsigned int proto_off,
  367. int inverse);
  368. struct ip_vs_conn *
  369. (*conn_out_get)(int af,
  370. const struct sk_buff *skb,
  371. const struct ip_vs_iphdr *iph,
  372. unsigned int proto_off,
  373. int inverse);
  374. int (*snat_handler)(struct sk_buff *skb,
  375. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  376. int (*dnat_handler)(struct sk_buff *skb,
  377. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  378. int (*csum_check)(int af, struct sk_buff *skb,
  379. struct ip_vs_protocol *pp);
  380. const char *(*state_name)(int state);
  381. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  382. const struct sk_buff *skb,
  383. struct ip_vs_proto_data *pd);
  384. int (*register_app)(struct net *net, struct ip_vs_app *inc);
  385. void (*unregister_app)(struct net *net, struct ip_vs_app *inc);
  386. int (*app_conn_bind)(struct ip_vs_conn *cp);
  387. void (*debug_packet)(int af, struct ip_vs_protocol *pp,
  388. const struct sk_buff *skb,
  389. int offset,
  390. const char *msg);
  391. void (*timeout_change)(struct ip_vs_proto_data *pd, int flags);
  392. };
  393. /*
  394. * protocol data per netns
  395. */
  396. struct ip_vs_proto_data {
  397. struct ip_vs_proto_data *next;
  398. struct ip_vs_protocol *pp;
  399. int *timeout_table; /* protocol timeout table */
  400. atomic_t appcnt; /* counter of proto app incs. */
  401. struct tcp_states_t *tcp_state_table;
  402. };
  403. extern struct ip_vs_protocol *ip_vs_proto_get(unsigned short proto);
  404. extern struct ip_vs_proto_data *ip_vs_proto_data_get(struct net *net,
  405. unsigned short proto);
  406. struct ip_vs_conn_param {
  407. struct net *net;
  408. const union nf_inet_addr *caddr;
  409. const union nf_inet_addr *vaddr;
  410. __be16 cport;
  411. __be16 vport;
  412. __u16 protocol;
  413. u16 af;
  414. const struct ip_vs_pe *pe;
  415. char *pe_data;
  416. __u8 pe_data_len;
  417. };
  418. /*
  419. * IP_VS structure allocated for each dynamically scheduled connection
  420. */
  421. struct ip_vs_conn {
  422. struct hlist_node c_list; /* hashed list heads */
  423. #ifdef CONFIG_NET_NS
  424. struct net *net; /* Name space */
  425. #endif
  426. /* Protocol, addresses and port numbers */
  427. u16 af; /* address family */
  428. __be16 cport;
  429. __be16 vport;
  430. __be16 dport;
  431. __u32 fwmark; /* Fire wall mark from skb */
  432. union nf_inet_addr caddr; /* client address */
  433. union nf_inet_addr vaddr; /* virtual address */
  434. union nf_inet_addr daddr; /* destination address */
  435. volatile __u32 flags; /* status flags */
  436. __u16 protocol; /* Which protocol (TCP/UDP) */
  437. /* counter and timer */
  438. atomic_t refcnt; /* reference count */
  439. struct timer_list timer; /* Expiration timer */
  440. volatile unsigned long timeout; /* timeout */
  441. /* Flags and state transition */
  442. spinlock_t lock; /* lock for state transition */
  443. volatile __u16 state; /* state info */
  444. volatile __u16 old_state; /* old state, to be used for
  445. * state transition triggerd
  446. * synchronization
  447. */
  448. /* Control members */
  449. struct ip_vs_conn *control; /* Master control connection */
  450. atomic_t n_control; /* Number of controlled ones */
  451. struct ip_vs_dest *dest; /* real server */
  452. atomic_t in_pkts; /* incoming packet counter */
  453. /* packet transmitter for different forwarding methods. If it
  454. mangles the packet, it must return NF_DROP or better NF_STOLEN,
  455. otherwise this must be changed to a sk_buff **.
  456. NF_ACCEPT can be returned when destination is local.
  457. */
  458. int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
  459. struct ip_vs_protocol *pp);
  460. /* Note: we can group the following members into a structure,
  461. in order to save more space, and the following members are
  462. only used in VS/NAT anyway */
  463. struct ip_vs_app *app; /* bound ip_vs_app object */
  464. void *app_data; /* Application private data */
  465. struct ip_vs_seq in_seq; /* incoming seq. struct */
  466. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  467. const struct ip_vs_pe *pe;
  468. char *pe_data;
  469. __u8 pe_data_len;
  470. };
  471. /*
  472. * To save some memory in conn table when name space is disabled.
  473. */
  474. static inline struct net *ip_vs_conn_net(const struct ip_vs_conn *cp)
  475. {
  476. #ifdef CONFIG_NET_NS
  477. return cp->net;
  478. #else
  479. return &init_net;
  480. #endif
  481. }
  482. static inline void ip_vs_conn_net_set(struct ip_vs_conn *cp, struct net *net)
  483. {
  484. #ifdef CONFIG_NET_NS
  485. cp->net = net;
  486. #endif
  487. }
  488. static inline int ip_vs_conn_net_eq(const struct ip_vs_conn *cp,
  489. struct net *net)
  490. {
  491. #ifdef CONFIG_NET_NS
  492. return cp->net == net;
  493. #else
  494. return 1;
  495. #endif
  496. }
  497. /*
  498. * Extended internal versions of struct ip_vs_service_user and
  499. * ip_vs_dest_user for IPv6 support.
  500. *
  501. * We need these to conveniently pass around service and destination
  502. * options, but unfortunately, we also need to keep the old definitions to
  503. * maintain userspace backwards compatibility for the setsockopt interface.
  504. */
  505. struct ip_vs_service_user_kern {
  506. /* virtual service addresses */
  507. u16 af;
  508. u16 protocol;
  509. union nf_inet_addr addr; /* virtual ip address */
  510. u16 port;
  511. u32 fwmark; /* firwall mark of service */
  512. /* virtual service options */
  513. char *sched_name;
  514. char *pe_name;
  515. unsigned flags; /* virtual service flags */
  516. unsigned timeout; /* persistent timeout in sec */
  517. u32 netmask; /* persistent netmask */
  518. };
  519. struct ip_vs_dest_user_kern {
  520. /* destination server address */
  521. union nf_inet_addr addr;
  522. u16 port;
  523. /* real server options */
  524. unsigned conn_flags; /* connection flags */
  525. int weight; /* destination weight */
  526. /* thresholds for active connections */
  527. u32 u_threshold; /* upper threshold */
  528. u32 l_threshold; /* lower threshold */
  529. };
  530. /*
  531. * The information about the virtual service offered to the net
  532. * and the forwarding entries
  533. */
  534. struct ip_vs_service {
  535. struct list_head s_list; /* for normal service table */
  536. struct list_head f_list; /* for fwmark-based service table */
  537. atomic_t refcnt; /* reference counter */
  538. atomic_t usecnt; /* use counter */
  539. u16 af; /* address family */
  540. __u16 protocol; /* which protocol (TCP/UDP) */
  541. union nf_inet_addr addr; /* IP address for virtual service */
  542. __be16 port; /* port number for the service */
  543. __u32 fwmark; /* firewall mark of the service */
  544. unsigned flags; /* service status flags */
  545. unsigned timeout; /* persistent timeout in ticks */
  546. __be32 netmask; /* grouping granularity */
  547. struct net *net;
  548. struct list_head destinations; /* real server d-linked list */
  549. __u32 num_dests; /* number of servers */
  550. struct ip_vs_stats stats; /* statistics for the service */
  551. struct ip_vs_app *inc; /* bind conns to this app inc */
  552. /* for scheduling */
  553. struct ip_vs_scheduler *scheduler; /* bound scheduler object */
  554. rwlock_t sched_lock; /* lock sched_data */
  555. void *sched_data; /* scheduler application data */
  556. /* alternate persistence engine */
  557. struct ip_vs_pe *pe;
  558. };
  559. /*
  560. * The real server destination forwarding entry
  561. * with ip address, port number, and so on.
  562. */
  563. struct ip_vs_dest {
  564. struct list_head n_list; /* for the dests in the service */
  565. struct list_head d_list; /* for table with all the dests */
  566. u16 af; /* address family */
  567. __be16 port; /* port number of the server */
  568. union nf_inet_addr addr; /* IP address of the server */
  569. volatile unsigned flags; /* dest status flags */
  570. atomic_t conn_flags; /* flags to copy to conn */
  571. atomic_t weight; /* server weight */
  572. atomic_t refcnt; /* reference counter */
  573. struct ip_vs_stats stats; /* statistics */
  574. /* connection counters and thresholds */
  575. atomic_t activeconns; /* active connections */
  576. atomic_t inactconns; /* inactive connections */
  577. atomic_t persistconns; /* persistent connections */
  578. __u32 u_threshold; /* upper threshold */
  579. __u32 l_threshold; /* lower threshold */
  580. /* for destination cache */
  581. spinlock_t dst_lock; /* lock of dst_cache */
  582. struct dst_entry *dst_cache; /* destination cache entry */
  583. u32 dst_rtos; /* RT_TOS(tos) for dst */
  584. u32 dst_cookie;
  585. #ifdef CONFIG_IP_VS_IPV6
  586. struct in6_addr dst_saddr;
  587. #endif
  588. /* for virtual service */
  589. struct ip_vs_service *svc; /* service it belongs to */
  590. __u16 protocol; /* which protocol (TCP/UDP) */
  591. __be16 vport; /* virtual port number */
  592. union nf_inet_addr vaddr; /* virtual IP address */
  593. __u32 vfwmark; /* firewall mark of service */
  594. };
  595. /*
  596. * The scheduler object
  597. */
  598. struct ip_vs_scheduler {
  599. struct list_head n_list; /* d-linked list head */
  600. char *name; /* scheduler name */
  601. atomic_t refcnt; /* reference counter */
  602. struct module *module; /* THIS_MODULE/NULL */
  603. /* scheduler initializing service */
  604. int (*init_service)(struct ip_vs_service *svc);
  605. /* scheduling service finish */
  606. int (*done_service)(struct ip_vs_service *svc);
  607. /* scheduler updating service */
  608. int (*update_service)(struct ip_vs_service *svc);
  609. /* selecting a server from the given service */
  610. struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
  611. const struct sk_buff *skb);
  612. };
  613. /* The persistence engine object */
  614. struct ip_vs_pe {
  615. struct list_head n_list; /* d-linked list head */
  616. char *name; /* scheduler name */
  617. atomic_t refcnt; /* reference counter */
  618. struct module *module; /* THIS_MODULE/NULL */
  619. /* get the connection template, if any */
  620. int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
  621. bool (*ct_match)(const struct ip_vs_conn_param *p,
  622. struct ip_vs_conn *ct);
  623. u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
  624. bool inverse);
  625. int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
  626. };
  627. /*
  628. * The application module object (a.k.a. app incarnation)
  629. */
  630. struct ip_vs_app {
  631. struct list_head a_list; /* member in app list */
  632. int type; /* IP_VS_APP_TYPE_xxx */
  633. char *name; /* application module name */
  634. __u16 protocol;
  635. struct module *module; /* THIS_MODULE/NULL */
  636. struct list_head incs_list; /* list of incarnations */
  637. /* members for application incarnations */
  638. struct list_head p_list; /* member in proto app list */
  639. struct ip_vs_app *app; /* its real application */
  640. __be16 port; /* port number in net order */
  641. atomic_t usecnt; /* usage counter */
  642. /*
  643. * output hook: Process packet in inout direction, diff set for TCP.
  644. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  645. * 2=Mangled but checksum was not updated
  646. */
  647. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  648. struct sk_buff *, int *diff);
  649. /*
  650. * input hook: Process packet in outin direction, diff set for TCP.
  651. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  652. * 2=Mangled but checksum was not updated
  653. */
  654. int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
  655. struct sk_buff *, int *diff);
  656. /* ip_vs_app initializer */
  657. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  658. /* ip_vs_app finish */
  659. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  660. /* not used now */
  661. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  662. struct ip_vs_protocol *);
  663. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  664. int * timeout_table;
  665. int * timeouts;
  666. int timeouts_size;
  667. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  668. int *verdict, struct ip_vs_conn **cpp);
  669. struct ip_vs_conn *
  670. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  671. const struct iphdr *iph, unsigned int proto_off,
  672. int inverse);
  673. struct ip_vs_conn *
  674. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  675. const struct iphdr *iph, unsigned int proto_off,
  676. int inverse);
  677. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  678. const struct sk_buff *skb,
  679. struct ip_vs_app *app);
  680. void (*timeout_change)(struct ip_vs_app *app, int flags);
  681. };
  682. /* IPVS in network namespace */
  683. struct netns_ipvs {
  684. int gen; /* Generation */
  685. /*
  686. * Hash table: for real service lookups
  687. */
  688. #define IP_VS_RTAB_BITS 4
  689. #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
  690. #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
  691. struct list_head rs_table[IP_VS_RTAB_SIZE];
  692. /* ip_vs_app */
  693. struct list_head app_list;
  694. struct mutex app_mutex;
  695. struct lock_class_key app_key; /* mutex debuging */
  696. /* ip_vs_proto */
  697. #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
  698. struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE];
  699. /* ip_vs_proto_tcp */
  700. #ifdef CONFIG_IP_VS_PROTO_TCP
  701. #define TCP_APP_TAB_BITS 4
  702. #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
  703. #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
  704. struct list_head tcp_apps[TCP_APP_TAB_SIZE];
  705. spinlock_t tcp_app_lock;
  706. #endif
  707. /* ip_vs_proto_udp */
  708. #ifdef CONFIG_IP_VS_PROTO_UDP
  709. #define UDP_APP_TAB_BITS 4
  710. #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
  711. #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
  712. struct list_head udp_apps[UDP_APP_TAB_SIZE];
  713. spinlock_t udp_app_lock;
  714. #endif
  715. /* ip_vs_proto_sctp */
  716. #ifdef CONFIG_IP_VS_PROTO_SCTP
  717. #define SCTP_APP_TAB_BITS 4
  718. #define SCTP_APP_TAB_SIZE (1 << SCTP_APP_TAB_BITS)
  719. #define SCTP_APP_TAB_MASK (SCTP_APP_TAB_SIZE - 1)
  720. /* Hash table for SCTP application incarnations */
  721. struct list_head sctp_apps[SCTP_APP_TAB_SIZE];
  722. spinlock_t sctp_app_lock;
  723. #endif
  724. /* ip_vs_conn */
  725. atomic_t conn_count; /* connection counter */
  726. /* ip_vs_ctl */
  727. struct ip_vs_stats tot_stats; /* Statistics & est. */
  728. int num_services; /* no of virtual services */
  729. rwlock_t rs_lock; /* real services table */
  730. /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
  731. struct lock_class_key ctl_key; /* ctl_mutex debuging */
  732. /* Trash for destinations */
  733. struct list_head dest_trash;
  734. /* Service counters */
  735. atomic_t ftpsvc_counter;
  736. atomic_t nullsvc_counter;
  737. #ifdef CONFIG_SYSCTL
  738. /* 1/rate drop and drop-entry variables */
  739. struct delayed_work defense_work; /* Work handler */
  740. int drop_rate;
  741. int drop_counter;
  742. atomic_t dropentry;
  743. /* locks in ctl.c */
  744. spinlock_t dropentry_lock; /* drop entry handling */
  745. spinlock_t droppacket_lock; /* drop packet handling */
  746. spinlock_t securetcp_lock; /* state and timeout tables */
  747. /* sys-ctl struct */
  748. struct ctl_table_header *sysctl_hdr;
  749. struct ctl_table *sysctl_tbl;
  750. #endif
  751. /* sysctl variables */
  752. int sysctl_amemthresh;
  753. int sysctl_am_droprate;
  754. int sysctl_drop_entry;
  755. int sysctl_drop_packet;
  756. int sysctl_secure_tcp;
  757. #ifdef CONFIG_IP_VS_NFCT
  758. int sysctl_conntrack;
  759. #endif
  760. int sysctl_snat_reroute;
  761. int sysctl_sync_ver;
  762. int sysctl_cache_bypass;
  763. int sysctl_expire_nodest_conn;
  764. int sysctl_expire_quiescent_template;
  765. int sysctl_sync_threshold[2];
  766. int sysctl_nat_icmp_send;
  767. /* ip_vs_lblc */
  768. int sysctl_lblc_expiration;
  769. struct ctl_table_header *lblc_ctl_header;
  770. struct ctl_table *lblc_ctl_table;
  771. /* ip_vs_lblcr */
  772. int sysctl_lblcr_expiration;
  773. struct ctl_table_header *lblcr_ctl_header;
  774. struct ctl_table *lblcr_ctl_table;
  775. /* ip_vs_est */
  776. struct list_head est_list; /* estimator list */
  777. spinlock_t est_lock;
  778. struct timer_list est_timer; /* Estimation timer */
  779. /* ip_vs_sync */
  780. struct list_head sync_queue;
  781. spinlock_t sync_lock;
  782. struct ip_vs_sync_buff *sync_buff;
  783. spinlock_t sync_buff_lock;
  784. struct sockaddr_in sync_mcast_addr;
  785. struct task_struct *master_thread;
  786. struct task_struct *backup_thread;
  787. int send_mesg_maxlen;
  788. int recv_mesg_maxlen;
  789. volatile int sync_state;
  790. volatile int master_syncid;
  791. volatile int backup_syncid;
  792. /* multicast interface name */
  793. char master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  794. char backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  795. /* net name space ptr */
  796. struct net *net; /* Needed by timer routines */
  797. };
  798. #define DEFAULT_SYNC_THRESHOLD 3
  799. #define DEFAULT_SYNC_PERIOD 50
  800. #define DEFAULT_SYNC_VER 1
  801. #ifdef CONFIG_SYSCTL
  802. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  803. {
  804. return ipvs->sysctl_sync_threshold[0];
  805. }
  806. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  807. {
  808. return ipvs->sysctl_sync_threshold[1];
  809. }
  810. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  811. {
  812. return ipvs->sysctl_sync_ver;
  813. }
  814. #else
  815. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  816. {
  817. return DEFAULT_SYNC_THRESHOLD;
  818. }
  819. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  820. {
  821. return DEFAULT_SYNC_PERIOD;
  822. }
  823. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  824. {
  825. return DEFAULT_SYNC_VER;
  826. }
  827. #endif
  828. /*
  829. * IPVS core functions
  830. * (from ip_vs_core.c)
  831. */
  832. extern const char *ip_vs_proto_name(unsigned proto);
  833. extern void ip_vs_init_hash_table(struct list_head *table, int rows);
  834. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
  835. #define IP_VS_APP_TYPE_FTP 1
  836. /*
  837. * ip_vs_conn handling functions
  838. * (from ip_vs_conn.c)
  839. */
  840. enum {
  841. IP_VS_DIR_INPUT = 0,
  842. IP_VS_DIR_OUTPUT,
  843. IP_VS_DIR_INPUT_ONLY,
  844. IP_VS_DIR_LAST,
  845. };
  846. static inline void ip_vs_conn_fill_param(struct net *net, int af, int protocol,
  847. const union nf_inet_addr *caddr,
  848. __be16 cport,
  849. const union nf_inet_addr *vaddr,
  850. __be16 vport,
  851. struct ip_vs_conn_param *p)
  852. {
  853. p->net = net;
  854. p->af = af;
  855. p->protocol = protocol;
  856. p->caddr = caddr;
  857. p->cport = cport;
  858. p->vaddr = vaddr;
  859. p->vport = vport;
  860. p->pe = NULL;
  861. p->pe_data = NULL;
  862. }
  863. struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
  864. struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
  865. struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
  866. const struct ip_vs_iphdr *iph,
  867. unsigned int proto_off,
  868. int inverse);
  869. struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
  870. struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
  871. const struct ip_vs_iphdr *iph,
  872. unsigned int proto_off,
  873. int inverse);
  874. /* put back the conn without restarting its timer */
  875. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  876. {
  877. atomic_dec(&cp->refcnt);
  878. }
  879. extern void ip_vs_conn_put(struct ip_vs_conn *cp);
  880. extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  881. struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p,
  882. const union nf_inet_addr *daddr,
  883. __be16 dport, unsigned flags,
  884. struct ip_vs_dest *dest, __u32 fwmark);
  885. extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  886. extern const char * ip_vs_state_name(__u16 proto, int state);
  887. extern void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp);
  888. extern int ip_vs_check_template(struct ip_vs_conn *ct);
  889. extern void ip_vs_random_dropentry(struct net *net);
  890. extern int ip_vs_conn_init(void);
  891. extern void ip_vs_conn_cleanup(void);
  892. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  893. {
  894. struct ip_vs_conn *ctl_cp = cp->control;
  895. if (!ctl_cp) {
  896. IP_VS_ERR_BUF("request control DEL for uncontrolled: "
  897. "%s:%d to %s:%d\n",
  898. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  899. ntohs(cp->cport),
  900. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  901. ntohs(cp->vport));
  902. return;
  903. }
  904. IP_VS_DBG_BUF(7, "DELeting control for: "
  905. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  906. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  907. ntohs(cp->cport),
  908. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  909. ntohs(ctl_cp->cport));
  910. cp->control = NULL;
  911. if (atomic_read(&ctl_cp->n_control) == 0) {
  912. IP_VS_ERR_BUF("BUG control DEL with n=0 : "
  913. "%s:%d to %s:%d\n",
  914. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  915. ntohs(cp->cport),
  916. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  917. ntohs(cp->vport));
  918. return;
  919. }
  920. atomic_dec(&ctl_cp->n_control);
  921. }
  922. static inline void
  923. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  924. {
  925. if (cp->control) {
  926. IP_VS_ERR_BUF("request control ADD for already controlled: "
  927. "%s:%d to %s:%d\n",
  928. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  929. ntohs(cp->cport),
  930. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  931. ntohs(cp->vport));
  932. ip_vs_control_del(cp);
  933. }
  934. IP_VS_DBG_BUF(7, "ADDing control for: "
  935. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  936. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  937. ntohs(cp->cport),
  938. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  939. ntohs(ctl_cp->cport));
  940. cp->control = ctl_cp;
  941. atomic_inc(&ctl_cp->n_control);
  942. }
  943. /*
  944. * IPVS application functions
  945. * (from ip_vs_app.c)
  946. */
  947. #define IP_VS_APP_MAX_PORTS 8
  948. extern int register_ip_vs_app(struct net *net, struct ip_vs_app *app);
  949. extern void unregister_ip_vs_app(struct net *net, struct ip_vs_app *app);
  950. extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  951. extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
  952. extern int register_ip_vs_app_inc(struct net *net, struct ip_vs_app *app,
  953. __u16 proto, __u16 port);
  954. extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
  955. extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
  956. extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
  957. extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
  958. extern int ip_vs_app_init(void);
  959. extern void ip_vs_app_cleanup(void);
  960. void ip_vs_bind_pe(struct ip_vs_service *svc, struct ip_vs_pe *pe);
  961. void ip_vs_unbind_pe(struct ip_vs_service *svc);
  962. int register_ip_vs_pe(struct ip_vs_pe *pe);
  963. int unregister_ip_vs_pe(struct ip_vs_pe *pe);
  964. struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
  965. struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
  966. static inline void ip_vs_pe_get(const struct ip_vs_pe *pe)
  967. {
  968. if (pe && pe->module)
  969. __module_get(pe->module);
  970. }
  971. static inline void ip_vs_pe_put(const struct ip_vs_pe *pe)
  972. {
  973. if (pe && pe->module)
  974. module_put(pe->module);
  975. }
  976. /*
  977. * IPVS protocol functions (from ip_vs_proto.c)
  978. */
  979. extern int ip_vs_protocol_init(void);
  980. extern void ip_vs_protocol_cleanup(void);
  981. extern void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
  982. extern int *ip_vs_create_timeout_table(int *table, int size);
  983. extern int
  984. ip_vs_set_state_timeout(int *table, int num, const char *const *names,
  985. const char *name, int to);
  986. extern void
  987. ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
  988. const struct sk_buff *skb,
  989. int offset, const char *msg);
  990. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  991. extern struct ip_vs_protocol ip_vs_protocol_udp;
  992. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  993. extern struct ip_vs_protocol ip_vs_protocol_esp;
  994. extern struct ip_vs_protocol ip_vs_protocol_ah;
  995. extern struct ip_vs_protocol ip_vs_protocol_sctp;
  996. /*
  997. * Registering/unregistering scheduler functions
  998. * (from ip_vs_sched.c)
  999. */
  1000. extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1001. extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1002. extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  1003. struct ip_vs_scheduler *scheduler);
  1004. extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
  1005. extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  1006. extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  1007. extern struct ip_vs_conn *
  1008. ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
  1009. struct ip_vs_proto_data *pd, int *ignored);
  1010. extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  1011. struct ip_vs_proto_data *pd);
  1012. extern void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
  1013. /*
  1014. * IPVS control data and functions (from ip_vs_ctl.c)
  1015. */
  1016. extern struct ip_vs_stats ip_vs_stats;
  1017. extern const struct ctl_path net_vs_ctl_path[];
  1018. extern int sysctl_ip_vs_sync_ver;
  1019. extern void ip_vs_sync_switch_mode(struct net *net, int mode);
  1020. extern struct ip_vs_service *
  1021. ip_vs_service_get(struct net *net, int af, __u32 fwmark, __u16 protocol,
  1022. const union nf_inet_addr *vaddr, __be16 vport);
  1023. static inline void ip_vs_service_put(struct ip_vs_service *svc)
  1024. {
  1025. atomic_dec(&svc->usecnt);
  1026. }
  1027. extern struct ip_vs_dest *
  1028. ip_vs_lookup_real_service(struct net *net, int af, __u16 protocol,
  1029. const union nf_inet_addr *daddr, __be16 dport);
  1030. extern int ip_vs_use_count_inc(void);
  1031. extern void ip_vs_use_count_dec(void);
  1032. extern int ip_vs_control_init(void);
  1033. extern void ip_vs_control_cleanup(void);
  1034. extern struct ip_vs_dest *
  1035. ip_vs_find_dest(struct net *net, int af, const union nf_inet_addr *daddr,
  1036. __be16 dport, const union nf_inet_addr *vaddr, __be16 vport,
  1037. __u16 protocol, __u32 fwmark);
  1038. extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  1039. /*
  1040. * IPVS sync daemon data and function prototypes
  1041. * (from ip_vs_sync.c)
  1042. */
  1043. extern int start_sync_thread(struct net *net, int state, char *mcast_ifn,
  1044. __u8 syncid);
  1045. extern int stop_sync_thread(struct net *net, int state);
  1046. extern void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp);
  1047. extern int ip_vs_sync_init(void);
  1048. extern void ip_vs_sync_cleanup(void);
  1049. /*
  1050. * IPVS rate estimator prototypes (from ip_vs_est.c)
  1051. */
  1052. extern int ip_vs_estimator_init(void);
  1053. extern void ip_vs_estimator_cleanup(void);
  1054. extern void ip_vs_start_estimator(struct net *net, struct ip_vs_stats *stats);
  1055. extern void ip_vs_stop_estimator(struct net *net, struct ip_vs_stats *stats);
  1056. extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  1057. extern void ip_vs_read_estimator(struct ip_vs_stats_user *dst,
  1058. struct ip_vs_stats *stats);
  1059. /*
  1060. * Various IPVS packet transmitters (from ip_vs_xmit.c)
  1061. */
  1062. extern int ip_vs_null_xmit
  1063. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1064. extern int ip_vs_bypass_xmit
  1065. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1066. extern int ip_vs_nat_xmit
  1067. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1068. extern int ip_vs_tunnel_xmit
  1069. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1070. extern int ip_vs_dr_xmit
  1071. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1072. extern int ip_vs_icmp_xmit
  1073. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
  1074. extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
  1075. #ifdef CONFIG_IP_VS_IPV6
  1076. extern int ip_vs_bypass_xmit_v6
  1077. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1078. extern int ip_vs_nat_xmit_v6
  1079. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1080. extern int ip_vs_tunnel_xmit_v6
  1081. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1082. extern int ip_vs_dr_xmit_v6
  1083. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1084. extern int ip_vs_icmp_xmit_v6
  1085. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
  1086. int offset);
  1087. #endif
  1088. #ifdef CONFIG_SYSCTL
  1089. /*
  1090. * This is a simple mechanism to ignore packets when
  1091. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  1092. * we start to drop 1/rate of the packets
  1093. */
  1094. static inline int ip_vs_todrop(struct netns_ipvs *ipvs)
  1095. {
  1096. if (!ipvs->drop_rate)
  1097. return 0;
  1098. if (--ipvs->drop_counter > 0)
  1099. return 0;
  1100. ipvs->drop_counter = ipvs->drop_rate;
  1101. return 1;
  1102. }
  1103. #else
  1104. static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; }
  1105. #endif
  1106. /*
  1107. * ip_vs_fwd_tag returns the forwarding tag of the connection
  1108. */
  1109. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  1110. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  1111. {
  1112. char fwd;
  1113. switch (IP_VS_FWD_METHOD(cp)) {
  1114. case IP_VS_CONN_F_MASQ:
  1115. fwd = 'M'; break;
  1116. case IP_VS_CONN_F_LOCALNODE:
  1117. fwd = 'L'; break;
  1118. case IP_VS_CONN_F_TUNNEL:
  1119. fwd = 'T'; break;
  1120. case IP_VS_CONN_F_DROUTE:
  1121. fwd = 'R'; break;
  1122. case IP_VS_CONN_F_BYPASS:
  1123. fwd = 'B'; break;
  1124. default:
  1125. fwd = '?'; break;
  1126. }
  1127. return fwd;
  1128. }
  1129. extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1130. struct ip_vs_conn *cp, int dir);
  1131. #ifdef CONFIG_IP_VS_IPV6
  1132. extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1133. struct ip_vs_conn *cp, int dir);
  1134. #endif
  1135. extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  1136. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  1137. {
  1138. __be32 diff[2] = { ~old, new };
  1139. return csum_partial(diff, sizeof(diff), oldsum);
  1140. }
  1141. #ifdef CONFIG_IP_VS_IPV6
  1142. static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
  1143. __wsum oldsum)
  1144. {
  1145. __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
  1146. new[3], new[2], new[1], new[0] };
  1147. return csum_partial(diff, sizeof(diff), oldsum);
  1148. }
  1149. #endif
  1150. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  1151. {
  1152. __be16 diff[2] = { ~old, new };
  1153. return csum_partial(diff, sizeof(diff), oldsum);
  1154. }
  1155. /*
  1156. * Forget current conntrack (unconfirmed) and attach notrack entry
  1157. */
  1158. static inline void ip_vs_notrack(struct sk_buff *skb)
  1159. {
  1160. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1161. enum ip_conntrack_info ctinfo;
  1162. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  1163. if (!ct || !nf_ct_is_untracked(ct)) {
  1164. nf_reset(skb);
  1165. skb->nfct = &nf_ct_untracked_get()->ct_general;
  1166. skb->nfctinfo = IP_CT_NEW;
  1167. nf_conntrack_get(skb->nfct);
  1168. }
  1169. #endif
  1170. }
  1171. #ifdef CONFIG_IP_VS_NFCT
  1172. /*
  1173. * Netfilter connection tracking
  1174. * (from ip_vs_nfct.c)
  1175. */
  1176. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1177. {
  1178. #ifdef CONFIG_SYSCTL
  1179. return ipvs->sysctl_conntrack;
  1180. #else
  1181. return 0;
  1182. #endif
  1183. }
  1184. extern void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
  1185. int outin);
  1186. extern int ip_vs_confirm_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp);
  1187. extern void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
  1188. struct ip_vs_conn *cp, u_int8_t proto,
  1189. const __be16 port, int from_rs);
  1190. extern void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
  1191. #else
  1192. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1193. {
  1194. return 0;
  1195. }
  1196. static inline void ip_vs_update_conntrack(struct sk_buff *skb,
  1197. struct ip_vs_conn *cp, int outin)
  1198. {
  1199. }
  1200. static inline int ip_vs_confirm_conntrack(struct sk_buff *skb,
  1201. struct ip_vs_conn *cp)
  1202. {
  1203. return NF_ACCEPT;
  1204. }
  1205. static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
  1206. {
  1207. }
  1208. /* CONFIG_IP_VS_NFCT */
  1209. #endif
  1210. static inline unsigned int
  1211. ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
  1212. {
  1213. /*
  1214. * We think the overhead of processing active connections is 256
  1215. * times higher than that of inactive connections in average. (This
  1216. * 256 times might not be accurate, we will change it later) We
  1217. * use the following formula to estimate the overhead now:
  1218. * dest->activeconns*256 + dest->inactconns
  1219. */
  1220. return (atomic_read(&dest->activeconns) << 8) +
  1221. atomic_read(&dest->inactconns);
  1222. }
  1223. #endif /* __KERNEL__ */
  1224. #endif /* _NET_IP_VS_H */