ip_vs.h 45 KB

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