ip_vs.h 43 KB

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