ip_vs.h 45 KB

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