ip_vs.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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 && dev_net(skb->dev)))
  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 && sock_net(skb->sk)))
  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 && sock_net(skb->sk)))
  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 && dev_net(skb->dev)))
  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. __u64 conns; /* connections scheduled */
  335. __u64 inpkts; /* incoming packets */
  336. __u64 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 cnt;
  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. u64 last_conns;
  351. u64 last_inpkts;
  352. u64 last_outpkts;
  353. u64 cps;
  354. u64 inpps;
  355. u64 outpps;
  356. u64 inbps;
  357. u64 outbps;
  358. };
  359. /*
  360. * IPVS statistics object, 64-bit kernel version of struct ip_vs_stats_user
  361. */
  362. struct ip_vs_kstats {
  363. u64 conns; /* connections scheduled */
  364. u64 inpkts; /* incoming packets */
  365. u64 outpkts; /* outgoing packets */
  366. u64 inbytes; /* incoming bytes */
  367. u64 outbytes; /* outgoing bytes */
  368. u64 cps; /* current connection rate */
  369. u64 inpps; /* current in packet rate */
  370. u64 outpps; /* current out packet rate */
  371. u64 inbps; /* current in byte rate */
  372. u64 outbps; /* current out byte rate */
  373. };
  374. struct ip_vs_stats {
  375. struct ip_vs_kstats kstats; /* kernel statistics */
  376. struct ip_vs_estimator est; /* estimator */
  377. struct ip_vs_cpu_stats __percpu *cpustats; /* per cpu counters */
  378. spinlock_t lock; /* spin lock */
  379. struct ip_vs_kstats kstats0; /* reset values */
  380. };
  381. struct dst_entry;
  382. struct iphdr;
  383. struct ip_vs_conn;
  384. struct ip_vs_app;
  385. struct sk_buff;
  386. struct ip_vs_proto_data;
  387. struct ip_vs_protocol {
  388. struct ip_vs_protocol *next;
  389. char *name;
  390. u16 protocol;
  391. u16 num_states;
  392. int dont_defrag;
  393. void (*init)(struct ip_vs_protocol *pp);
  394. void (*exit)(struct ip_vs_protocol *pp);
  395. int (*init_netns)(struct net *net, struct ip_vs_proto_data *pd);
  396. void (*exit_netns)(struct net *net, struct ip_vs_proto_data *pd);
  397. int (*conn_schedule)(int af, struct sk_buff *skb,
  398. struct ip_vs_proto_data *pd,
  399. int *verdict, struct ip_vs_conn **cpp,
  400. struct ip_vs_iphdr *iph);
  401. struct ip_vs_conn *
  402. (*conn_in_get)(int af,
  403. const struct sk_buff *skb,
  404. const struct ip_vs_iphdr *iph,
  405. int inverse);
  406. struct ip_vs_conn *
  407. (*conn_out_get)(int af,
  408. const struct sk_buff *skb,
  409. const struct ip_vs_iphdr *iph,
  410. int inverse);
  411. int (*snat_handler)(struct sk_buff *skb, struct ip_vs_protocol *pp,
  412. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph);
  413. int (*dnat_handler)(struct sk_buff *skb, struct ip_vs_protocol *pp,
  414. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph);
  415. int (*csum_check)(int af, struct sk_buff *skb,
  416. struct ip_vs_protocol *pp);
  417. const char *(*state_name)(int state);
  418. void (*state_transition)(struct ip_vs_conn *cp, int direction,
  419. const struct sk_buff *skb,
  420. struct ip_vs_proto_data *pd);
  421. int (*register_app)(struct net *net, struct ip_vs_app *inc);
  422. void (*unregister_app)(struct net *net, struct ip_vs_app *inc);
  423. int (*app_conn_bind)(struct ip_vs_conn *cp);
  424. void (*debug_packet)(int af, struct ip_vs_protocol *pp,
  425. const struct sk_buff *skb,
  426. int offset,
  427. const char *msg);
  428. void (*timeout_change)(struct ip_vs_proto_data *pd, int flags);
  429. };
  430. /* protocol data per netns */
  431. struct ip_vs_proto_data {
  432. struct ip_vs_proto_data *next;
  433. struct ip_vs_protocol *pp;
  434. int *timeout_table; /* protocol timeout table */
  435. atomic_t appcnt; /* counter of proto app incs. */
  436. struct tcp_states_t *tcp_state_table;
  437. };
  438. struct ip_vs_protocol *ip_vs_proto_get(unsigned short proto);
  439. struct ip_vs_proto_data *ip_vs_proto_data_get(struct net *net,
  440. unsigned short proto);
  441. struct ip_vs_conn_param {
  442. struct net *net;
  443. const union nf_inet_addr *caddr;
  444. const union nf_inet_addr *vaddr;
  445. __be16 cport;
  446. __be16 vport;
  447. __u16 protocol;
  448. u16 af;
  449. const struct ip_vs_pe *pe;
  450. char *pe_data;
  451. __u8 pe_data_len;
  452. };
  453. /* IP_VS structure allocated for each dynamically scheduled connection */
  454. struct ip_vs_conn {
  455. struct hlist_node c_list; /* hashed list heads */
  456. /* Protocol, addresses and port numbers */
  457. __be16 cport;
  458. __be16 dport;
  459. __be16 vport;
  460. u16 af; /* address family */
  461. union nf_inet_addr caddr; /* client address */
  462. union nf_inet_addr vaddr; /* virtual address */
  463. union nf_inet_addr daddr; /* destination address */
  464. volatile __u32 flags; /* status flags */
  465. __u16 protocol; /* Which protocol (TCP/UDP) */
  466. __u16 daf; /* Address family of the dest */
  467. #ifdef CONFIG_NET_NS
  468. struct net *net; /* Name space */
  469. #endif
  470. /* counter and timer */
  471. atomic_t refcnt; /* reference count */
  472. struct timer_list timer; /* Expiration timer */
  473. volatile unsigned long timeout; /* timeout */
  474. /* Flags and state transition */
  475. spinlock_t lock; /* lock for state transition */
  476. volatile __u16 state; /* state info */
  477. volatile __u16 old_state; /* old state, to be used for
  478. * state transition triggerd
  479. * synchronization
  480. */
  481. __u32 fwmark; /* Fire wall mark from skb */
  482. unsigned long sync_endtime; /* jiffies + sent_retries */
  483. /* Control members */
  484. struct ip_vs_conn *control; /* Master control connection */
  485. atomic_t n_control; /* Number of controlled ones */
  486. struct ip_vs_dest *dest; /* real server */
  487. atomic_t in_pkts; /* incoming packet counter */
  488. /* Packet transmitter for different forwarding methods. If it
  489. * mangles the packet, it must return NF_DROP or better NF_STOLEN,
  490. * otherwise this must be changed to a sk_buff **.
  491. * NF_ACCEPT can be returned when destination is local.
  492. */
  493. int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
  494. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  495. /* Note: we can group the following members into a structure,
  496. * in order to save more space, and the following members are
  497. * only used in VS/NAT anyway
  498. */
  499. struct ip_vs_app *app; /* bound ip_vs_app object */
  500. void *app_data; /* Application private data */
  501. struct ip_vs_seq in_seq; /* incoming seq. struct */
  502. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  503. const struct ip_vs_pe *pe;
  504. char *pe_data;
  505. __u8 pe_data_len;
  506. struct rcu_head rcu_head;
  507. };
  508. /* To save some memory in conn table when name space is disabled. */
  509. static inline struct net *ip_vs_conn_net(const struct ip_vs_conn *cp)
  510. {
  511. #ifdef CONFIG_NET_NS
  512. return cp->net;
  513. #else
  514. return &init_net;
  515. #endif
  516. }
  517. static inline void ip_vs_conn_net_set(struct ip_vs_conn *cp, struct net *net)
  518. {
  519. #ifdef CONFIG_NET_NS
  520. cp->net = net;
  521. #endif
  522. }
  523. static inline int ip_vs_conn_net_eq(const struct ip_vs_conn *cp,
  524. struct net *net)
  525. {
  526. #ifdef CONFIG_NET_NS
  527. return cp->net == net;
  528. #else
  529. return 1;
  530. #endif
  531. }
  532. /* Extended internal versions of struct ip_vs_service_user and ip_vs_dest_user
  533. * for IPv6 support.
  534. *
  535. * We need these to conveniently pass around service and destination
  536. * options, but unfortunately, we also need to keep the old definitions to
  537. * maintain userspace backwards compatibility for the setsockopt interface.
  538. */
  539. struct ip_vs_service_user_kern {
  540. /* virtual service addresses */
  541. u16 af;
  542. u16 protocol;
  543. union nf_inet_addr addr; /* virtual ip address */
  544. __be16 port;
  545. u32 fwmark; /* firwall mark of service */
  546. /* virtual service options */
  547. char *sched_name;
  548. char *pe_name;
  549. unsigned int flags; /* virtual service flags */
  550. unsigned int timeout; /* persistent timeout in sec */
  551. __be32 netmask; /* persistent netmask or plen */
  552. };
  553. struct ip_vs_dest_user_kern {
  554. /* destination server address */
  555. union nf_inet_addr addr;
  556. __be16 port;
  557. /* real server options */
  558. unsigned int conn_flags; /* connection flags */
  559. int weight; /* destination weight */
  560. /* thresholds for active connections */
  561. u32 u_threshold; /* upper threshold */
  562. u32 l_threshold; /* lower threshold */
  563. /* Address family of addr */
  564. u16 af;
  565. };
  566. /*
  567. * The information about the virtual service offered to the net and the
  568. * forwarding entries.
  569. */
  570. struct ip_vs_service {
  571. struct hlist_node s_list; /* for normal service table */
  572. struct hlist_node f_list; /* for fwmark-based service table */
  573. atomic_t refcnt; /* reference counter */
  574. u16 af; /* address family */
  575. __u16 protocol; /* which protocol (TCP/UDP) */
  576. union nf_inet_addr addr; /* IP address for virtual service */
  577. __be16 port; /* port number for the service */
  578. __u32 fwmark; /* firewall mark of the service */
  579. unsigned int flags; /* service status flags */
  580. unsigned int timeout; /* persistent timeout in ticks */
  581. __be32 netmask; /* grouping granularity, mask/plen */
  582. struct net *net;
  583. struct list_head destinations; /* real server d-linked list */
  584. __u32 num_dests; /* number of servers */
  585. struct ip_vs_stats stats; /* statistics for the service */
  586. /* for scheduling */
  587. struct ip_vs_scheduler __rcu *scheduler; /* bound scheduler object */
  588. spinlock_t sched_lock; /* lock sched_data */
  589. void *sched_data; /* scheduler application data */
  590. /* alternate persistence engine */
  591. struct ip_vs_pe __rcu *pe;
  592. struct rcu_head rcu_head;
  593. };
  594. /* Information for cached dst */
  595. struct ip_vs_dest_dst {
  596. struct dst_entry *dst_cache; /* destination cache entry */
  597. u32 dst_cookie;
  598. union nf_inet_addr dst_saddr;
  599. struct rcu_head rcu_head;
  600. };
  601. /* The real server destination forwarding entry with ip address, port number,
  602. * and so on.
  603. */
  604. struct ip_vs_dest {
  605. struct list_head n_list; /* for the dests in the service */
  606. struct hlist_node d_list; /* for table with all the dests */
  607. u16 af; /* address family */
  608. __be16 port; /* port number of the server */
  609. union nf_inet_addr addr; /* IP address of the server */
  610. volatile unsigned int flags; /* dest status flags */
  611. atomic_t conn_flags; /* flags to copy to conn */
  612. atomic_t weight; /* server weight */
  613. atomic_t refcnt; /* reference counter */
  614. struct ip_vs_stats stats; /* statistics */
  615. unsigned long idle_start; /* start time, jiffies */
  616. /* connection counters and thresholds */
  617. atomic_t activeconns; /* active connections */
  618. atomic_t inactconns; /* inactive connections */
  619. atomic_t persistconns; /* persistent connections */
  620. __u32 u_threshold; /* upper threshold */
  621. __u32 l_threshold; /* lower threshold */
  622. /* for destination cache */
  623. spinlock_t dst_lock; /* lock of dst_cache */
  624. struct ip_vs_dest_dst __rcu *dest_dst; /* cached dst info */
  625. /* for virtual service */
  626. struct ip_vs_service __rcu *svc; /* service it belongs to */
  627. __u16 protocol; /* which protocol (TCP/UDP) */
  628. __be16 vport; /* virtual port number */
  629. union nf_inet_addr vaddr; /* virtual IP address */
  630. __u32 vfwmark; /* firewall mark of service */
  631. struct list_head t_list; /* in dest_trash */
  632. unsigned int in_rs_table:1; /* we are in rs_table */
  633. };
  634. /* The scheduler object */
  635. struct ip_vs_scheduler {
  636. struct list_head n_list; /* d-linked list head */
  637. char *name; /* scheduler name */
  638. atomic_t refcnt; /* reference counter */
  639. struct module *module; /* THIS_MODULE/NULL */
  640. /* scheduler initializing service */
  641. int (*init_service)(struct ip_vs_service *svc);
  642. /* scheduling service finish */
  643. void (*done_service)(struct ip_vs_service *svc);
  644. /* dest is linked */
  645. int (*add_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
  646. /* dest is unlinked */
  647. int (*del_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
  648. /* dest is updated */
  649. int (*upd_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
  650. /* selecting a server from the given service */
  651. struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
  652. const struct sk_buff *skb,
  653. struct ip_vs_iphdr *iph);
  654. };
  655. /* The persistence engine object */
  656. struct ip_vs_pe {
  657. struct list_head n_list; /* d-linked list head */
  658. char *name; /* scheduler name */
  659. atomic_t refcnt; /* reference counter */
  660. struct module *module; /* THIS_MODULE/NULL */
  661. /* get the connection template, if any */
  662. int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
  663. bool (*ct_match)(const struct ip_vs_conn_param *p,
  664. struct ip_vs_conn *ct);
  665. u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
  666. bool inverse);
  667. int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
  668. };
  669. /* The application module object (a.k.a. app incarnation) */
  670. struct ip_vs_app {
  671. struct list_head a_list; /* member in app list */
  672. int type; /* IP_VS_APP_TYPE_xxx */
  673. char *name; /* application module name */
  674. __u16 protocol;
  675. struct module *module; /* THIS_MODULE/NULL */
  676. struct list_head incs_list; /* list of incarnations */
  677. /* members for application incarnations */
  678. struct list_head p_list; /* member in proto app list */
  679. struct ip_vs_app *app; /* its real application */
  680. __be16 port; /* port number in net order */
  681. atomic_t usecnt; /* usage counter */
  682. struct rcu_head rcu_head;
  683. /* output hook: Process packet in inout direction, diff set for TCP.
  684. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  685. * 2=Mangled but checksum was not updated
  686. */
  687. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  688. struct sk_buff *, int *diff);
  689. /* input hook: Process packet in outin direction, diff set for TCP.
  690. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  691. * 2=Mangled but checksum was not updated
  692. */
  693. int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
  694. struct sk_buff *, int *diff);
  695. /* ip_vs_app initializer */
  696. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  697. /* ip_vs_app finish */
  698. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  699. /* not used now */
  700. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  701. struct ip_vs_protocol *);
  702. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  703. int * timeout_table;
  704. int * timeouts;
  705. int timeouts_size;
  706. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  707. int *verdict, struct ip_vs_conn **cpp);
  708. struct ip_vs_conn *
  709. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  710. const struct iphdr *iph, int inverse);
  711. struct ip_vs_conn *
  712. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  713. const struct iphdr *iph, int inverse);
  714. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  715. const struct sk_buff *skb,
  716. struct ip_vs_app *app);
  717. void (*timeout_change)(struct ip_vs_app *app, int flags);
  718. };
  719. struct ipvs_master_sync_state {
  720. struct list_head sync_queue;
  721. struct ip_vs_sync_buff *sync_buff;
  722. unsigned long sync_queue_len;
  723. unsigned int sync_queue_delay;
  724. struct task_struct *master_thread;
  725. struct delayed_work master_wakeup_work;
  726. struct netns_ipvs *ipvs;
  727. };
  728. /* How much time to keep dests in trash */
  729. #define IP_VS_DEST_TRASH_PERIOD (120 * HZ)
  730. /* IPVS in network namespace */
  731. struct netns_ipvs {
  732. int gen; /* Generation */
  733. int enable; /* enable like nf_hooks do */
  734. /* Hash table: for real service lookups */
  735. #define IP_VS_RTAB_BITS 4
  736. #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
  737. #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
  738. struct hlist_head rs_table[IP_VS_RTAB_SIZE];
  739. /* ip_vs_app */
  740. struct list_head app_list;
  741. /* ip_vs_proto */
  742. #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
  743. struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE];
  744. /* ip_vs_proto_tcp */
  745. #ifdef CONFIG_IP_VS_PROTO_TCP
  746. #define TCP_APP_TAB_BITS 4
  747. #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
  748. #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
  749. struct list_head tcp_apps[TCP_APP_TAB_SIZE];
  750. #endif
  751. /* ip_vs_proto_udp */
  752. #ifdef CONFIG_IP_VS_PROTO_UDP
  753. #define UDP_APP_TAB_BITS 4
  754. #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
  755. #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
  756. struct list_head udp_apps[UDP_APP_TAB_SIZE];
  757. #endif
  758. /* ip_vs_proto_sctp */
  759. #ifdef CONFIG_IP_VS_PROTO_SCTP
  760. #define SCTP_APP_TAB_BITS 4
  761. #define SCTP_APP_TAB_SIZE (1 << SCTP_APP_TAB_BITS)
  762. #define SCTP_APP_TAB_MASK (SCTP_APP_TAB_SIZE - 1)
  763. /* Hash table for SCTP application incarnations */
  764. struct list_head sctp_apps[SCTP_APP_TAB_SIZE];
  765. #endif
  766. /* ip_vs_conn */
  767. atomic_t conn_count; /* connection counter */
  768. /* ip_vs_ctl */
  769. struct ip_vs_stats tot_stats; /* Statistics & est. */
  770. int num_services; /* no of virtual services */
  771. /* Trash for destinations */
  772. struct list_head dest_trash;
  773. spinlock_t dest_trash_lock;
  774. struct timer_list dest_trash_timer; /* expiration timer */
  775. /* Service counters */
  776. atomic_t ftpsvc_counter;
  777. atomic_t nullsvc_counter;
  778. #ifdef CONFIG_SYSCTL
  779. /* 1/rate drop and drop-entry variables */
  780. struct delayed_work defense_work; /* Work handler */
  781. int drop_rate;
  782. int drop_counter;
  783. atomic_t dropentry;
  784. /* locks in ctl.c */
  785. spinlock_t dropentry_lock; /* drop entry handling */
  786. spinlock_t droppacket_lock; /* drop packet handling */
  787. spinlock_t securetcp_lock; /* state and timeout tables */
  788. /* sys-ctl struct */
  789. struct ctl_table_header *sysctl_hdr;
  790. struct ctl_table *sysctl_tbl;
  791. #endif
  792. /* sysctl variables */
  793. int sysctl_amemthresh;
  794. int sysctl_am_droprate;
  795. int sysctl_drop_entry;
  796. int sysctl_drop_packet;
  797. int sysctl_secure_tcp;
  798. #ifdef CONFIG_IP_VS_NFCT
  799. int sysctl_conntrack;
  800. #endif
  801. int sysctl_snat_reroute;
  802. int sysctl_sync_ver;
  803. int sysctl_sync_ports;
  804. int sysctl_sync_persist_mode;
  805. unsigned long sysctl_sync_qlen_max;
  806. int sysctl_sync_sock_size;
  807. int sysctl_cache_bypass;
  808. int sysctl_expire_nodest_conn;
  809. int sysctl_sloppy_tcp;
  810. int sysctl_sloppy_sctp;
  811. int sysctl_expire_quiescent_template;
  812. int sysctl_sync_threshold[2];
  813. unsigned int sysctl_sync_refresh_period;
  814. int sysctl_sync_retries;
  815. int sysctl_nat_icmp_send;
  816. int sysctl_pmtu_disc;
  817. int sysctl_backup_only;
  818. int sysctl_conn_reuse_mode;
  819. /* ip_vs_lblc */
  820. int sysctl_lblc_expiration;
  821. struct ctl_table_header *lblc_ctl_header;
  822. struct ctl_table *lblc_ctl_table;
  823. /* ip_vs_lblcr */
  824. int sysctl_lblcr_expiration;
  825. struct ctl_table_header *lblcr_ctl_header;
  826. struct ctl_table *lblcr_ctl_table;
  827. /* ip_vs_est */
  828. struct list_head est_list; /* estimator list */
  829. spinlock_t est_lock;
  830. struct timer_list est_timer; /* Estimation timer */
  831. /* ip_vs_sync */
  832. spinlock_t sync_lock;
  833. struct ipvs_master_sync_state *ms;
  834. spinlock_t sync_buff_lock;
  835. struct task_struct **backup_threads;
  836. int threads_mask;
  837. int send_mesg_maxlen;
  838. int recv_mesg_maxlen;
  839. volatile int sync_state;
  840. volatile int master_syncid;
  841. volatile int backup_syncid;
  842. struct mutex sync_mutex;
  843. /* multicast interface name */
  844. char master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  845. char backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  846. /* net name space ptr */
  847. struct net *net; /* Needed by timer routines */
  848. /* Number of heterogeneous destinations, needed becaus heterogeneous
  849. * are not supported when synchronization is enabled.
  850. */
  851. unsigned int mixed_address_family_dests;
  852. };
  853. #define DEFAULT_SYNC_THRESHOLD 3
  854. #define DEFAULT_SYNC_PERIOD 50
  855. #define DEFAULT_SYNC_VER 1
  856. #define DEFAULT_SLOPPY_TCP 0
  857. #define DEFAULT_SLOPPY_SCTP 0
  858. #define DEFAULT_SYNC_REFRESH_PERIOD (0U * HZ)
  859. #define DEFAULT_SYNC_RETRIES 0
  860. #define IPVS_SYNC_WAKEUP_RATE 8
  861. #define IPVS_SYNC_QLEN_MAX (IPVS_SYNC_WAKEUP_RATE * 4)
  862. #define IPVS_SYNC_SEND_DELAY (HZ / 50)
  863. #define IPVS_SYNC_CHECK_PERIOD HZ
  864. #define IPVS_SYNC_FLUSH_TIME (HZ * 2)
  865. #define IPVS_SYNC_PORTS_MAX (1 << 6)
  866. #ifdef CONFIG_SYSCTL
  867. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  868. {
  869. return ipvs->sysctl_sync_threshold[0];
  870. }
  871. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  872. {
  873. return ACCESS_ONCE(ipvs->sysctl_sync_threshold[1]);
  874. }
  875. static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
  876. {
  877. return ACCESS_ONCE(ipvs->sysctl_sync_refresh_period);
  878. }
  879. static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
  880. {
  881. return ipvs->sysctl_sync_retries;
  882. }
  883. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  884. {
  885. return ipvs->sysctl_sync_ver;
  886. }
  887. static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
  888. {
  889. return ipvs->sysctl_sloppy_tcp;
  890. }
  891. static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
  892. {
  893. return ipvs->sysctl_sloppy_sctp;
  894. }
  895. static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
  896. {
  897. return ACCESS_ONCE(ipvs->sysctl_sync_ports);
  898. }
  899. static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
  900. {
  901. return ipvs->sysctl_sync_persist_mode;
  902. }
  903. static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
  904. {
  905. return ipvs->sysctl_sync_qlen_max;
  906. }
  907. static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
  908. {
  909. return ipvs->sysctl_sync_sock_size;
  910. }
  911. static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
  912. {
  913. return ipvs->sysctl_pmtu_disc;
  914. }
  915. static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
  916. {
  917. return ipvs->sync_state & IP_VS_STATE_BACKUP &&
  918. ipvs->sysctl_backup_only;
  919. }
  920. static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
  921. {
  922. return ipvs->sysctl_conn_reuse_mode;
  923. }
  924. #else
  925. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  926. {
  927. return DEFAULT_SYNC_THRESHOLD;
  928. }
  929. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  930. {
  931. return DEFAULT_SYNC_PERIOD;
  932. }
  933. static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
  934. {
  935. return DEFAULT_SYNC_REFRESH_PERIOD;
  936. }
  937. static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
  938. {
  939. return DEFAULT_SYNC_RETRIES & 3;
  940. }
  941. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  942. {
  943. return DEFAULT_SYNC_VER;
  944. }
  945. static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
  946. {
  947. return DEFAULT_SLOPPY_TCP;
  948. }
  949. static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
  950. {
  951. return DEFAULT_SLOPPY_SCTP;
  952. }
  953. static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
  954. {
  955. return 1;
  956. }
  957. static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
  958. {
  959. return 0;
  960. }
  961. static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
  962. {
  963. return IPVS_SYNC_QLEN_MAX;
  964. }
  965. static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
  966. {
  967. return 0;
  968. }
  969. static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
  970. {
  971. return 1;
  972. }
  973. static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
  974. {
  975. return 0;
  976. }
  977. static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
  978. {
  979. return 1;
  980. }
  981. #endif
  982. /* IPVS core functions
  983. * (from ip_vs_core.c)
  984. */
  985. const char *ip_vs_proto_name(unsigned int proto);
  986. void ip_vs_init_hash_table(struct list_head *table, int rows);
  987. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
  988. #define IP_VS_APP_TYPE_FTP 1
  989. /* ip_vs_conn handling functions
  990. * (from ip_vs_conn.c)
  991. */
  992. enum {
  993. IP_VS_DIR_INPUT = 0,
  994. IP_VS_DIR_OUTPUT,
  995. IP_VS_DIR_INPUT_ONLY,
  996. IP_VS_DIR_LAST,
  997. };
  998. static inline void ip_vs_conn_fill_param(struct net *net, int af, int protocol,
  999. const union nf_inet_addr *caddr,
  1000. __be16 cport,
  1001. const union nf_inet_addr *vaddr,
  1002. __be16 vport,
  1003. struct ip_vs_conn_param *p)
  1004. {
  1005. p->net = net;
  1006. p->af = af;
  1007. p->protocol = protocol;
  1008. p->caddr = caddr;
  1009. p->cport = cport;
  1010. p->vaddr = vaddr;
  1011. p->vport = vport;
  1012. p->pe = NULL;
  1013. p->pe_data = NULL;
  1014. }
  1015. struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
  1016. struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
  1017. struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
  1018. const struct ip_vs_iphdr *iph,
  1019. int inverse);
  1020. struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
  1021. struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
  1022. const struct ip_vs_iphdr *iph,
  1023. int inverse);
  1024. /* Get reference to gain full access to conn.
  1025. * By default, RCU read-side critical sections have access only to
  1026. * conn fields and its PE data, see ip_vs_conn_rcu_free() for reference.
  1027. */
  1028. static inline bool __ip_vs_conn_get(struct ip_vs_conn *cp)
  1029. {
  1030. return atomic_inc_not_zero(&cp->refcnt);
  1031. }
  1032. /* put back the conn without restarting its timer */
  1033. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  1034. {
  1035. smp_mb__before_atomic();
  1036. atomic_dec(&cp->refcnt);
  1037. }
  1038. void ip_vs_conn_put(struct ip_vs_conn *cp);
  1039. void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  1040. struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
  1041. const union nf_inet_addr *daddr,
  1042. __be16 dport, unsigned int flags,
  1043. struct ip_vs_dest *dest, __u32 fwmark);
  1044. void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  1045. const char *ip_vs_state_name(__u16 proto, int state);
  1046. void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp);
  1047. int ip_vs_check_template(struct ip_vs_conn *ct);
  1048. void ip_vs_random_dropentry(struct net *net);
  1049. int ip_vs_conn_init(void);
  1050. void ip_vs_conn_cleanup(void);
  1051. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  1052. {
  1053. struct ip_vs_conn *ctl_cp = cp->control;
  1054. if (!ctl_cp) {
  1055. IP_VS_ERR_BUF("request control DEL for uncontrolled: "
  1056. "%s:%d to %s:%d\n",
  1057. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1058. ntohs(cp->cport),
  1059. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1060. ntohs(cp->vport));
  1061. return;
  1062. }
  1063. IP_VS_DBG_BUF(7, "DELeting control for: "
  1064. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  1065. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1066. ntohs(cp->cport),
  1067. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  1068. ntohs(ctl_cp->cport));
  1069. cp->control = NULL;
  1070. if (atomic_read(&ctl_cp->n_control) == 0) {
  1071. IP_VS_ERR_BUF("BUG control DEL with n=0 : "
  1072. "%s:%d to %s:%d\n",
  1073. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1074. ntohs(cp->cport),
  1075. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1076. ntohs(cp->vport));
  1077. return;
  1078. }
  1079. atomic_dec(&ctl_cp->n_control);
  1080. }
  1081. static inline void
  1082. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  1083. {
  1084. if (cp->control) {
  1085. IP_VS_ERR_BUF("request control ADD for already controlled: "
  1086. "%s:%d to %s:%d\n",
  1087. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1088. ntohs(cp->cport),
  1089. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1090. ntohs(cp->vport));
  1091. ip_vs_control_del(cp);
  1092. }
  1093. IP_VS_DBG_BUF(7, "ADDing control for: "
  1094. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  1095. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1096. ntohs(cp->cport),
  1097. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  1098. ntohs(ctl_cp->cport));
  1099. cp->control = ctl_cp;
  1100. atomic_inc(&ctl_cp->n_control);
  1101. }
  1102. /* IPVS netns init & cleanup functions */
  1103. int ip_vs_estimator_net_init(struct net *net);
  1104. int ip_vs_control_net_init(struct net *net);
  1105. int ip_vs_protocol_net_init(struct net *net);
  1106. int ip_vs_app_net_init(struct net *net);
  1107. int ip_vs_conn_net_init(struct net *net);
  1108. int ip_vs_sync_net_init(struct net *net);
  1109. void ip_vs_conn_net_cleanup(struct net *net);
  1110. void ip_vs_app_net_cleanup(struct net *net);
  1111. void ip_vs_protocol_net_cleanup(struct net *net);
  1112. void ip_vs_control_net_cleanup(struct net *net);
  1113. void ip_vs_estimator_net_cleanup(struct net *net);
  1114. void ip_vs_sync_net_cleanup(struct net *net);
  1115. void ip_vs_service_net_cleanup(struct net *net);
  1116. /* IPVS application functions
  1117. * (from ip_vs_app.c)
  1118. */
  1119. #define IP_VS_APP_MAX_PORTS 8
  1120. struct ip_vs_app *register_ip_vs_app(struct net *net, struct ip_vs_app *app);
  1121. void unregister_ip_vs_app(struct net *net, struct ip_vs_app *app);
  1122. int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1123. void ip_vs_unbind_app(struct ip_vs_conn *cp);
  1124. int register_ip_vs_app_inc(struct net *net, struct ip_vs_app *app, __u16 proto,
  1125. __u16 port);
  1126. int ip_vs_app_inc_get(struct ip_vs_app *inc);
  1127. void ip_vs_app_inc_put(struct ip_vs_app *inc);
  1128. int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
  1129. int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
  1130. int register_ip_vs_pe(struct ip_vs_pe *pe);
  1131. int unregister_ip_vs_pe(struct ip_vs_pe *pe);
  1132. struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
  1133. struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
  1134. /* Use a #define to avoid all of module.h just for these trivial ops */
  1135. #define ip_vs_pe_get(pe) \
  1136. if (pe && pe->module) \
  1137. __module_get(pe->module);
  1138. #define ip_vs_pe_put(pe) \
  1139. if (pe && pe->module) \
  1140. module_put(pe->module);
  1141. /* IPVS protocol functions (from ip_vs_proto.c) */
  1142. int ip_vs_protocol_init(void);
  1143. void ip_vs_protocol_cleanup(void);
  1144. void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
  1145. int *ip_vs_create_timeout_table(int *table, int size);
  1146. int ip_vs_set_state_timeout(int *table, int num, const char *const *names,
  1147. const char *name, int to);
  1148. void ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
  1149. const struct sk_buff *skb, int offset,
  1150. const char *msg);
  1151. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  1152. extern struct ip_vs_protocol ip_vs_protocol_udp;
  1153. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  1154. extern struct ip_vs_protocol ip_vs_protocol_esp;
  1155. extern struct ip_vs_protocol ip_vs_protocol_ah;
  1156. extern struct ip_vs_protocol ip_vs_protocol_sctp;
  1157. /* Registering/unregistering scheduler functions
  1158. * (from ip_vs_sched.c)
  1159. */
  1160. int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1161. int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1162. int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  1163. struct ip_vs_scheduler *scheduler);
  1164. void ip_vs_unbind_scheduler(struct ip_vs_service *svc,
  1165. struct ip_vs_scheduler *sched);
  1166. struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  1167. void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  1168. struct ip_vs_conn *
  1169. ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
  1170. struct ip_vs_proto_data *pd, int *ignored,
  1171. struct ip_vs_iphdr *iph);
  1172. int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  1173. struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph);
  1174. void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
  1175. /* IPVS control data and functions (from ip_vs_ctl.c) */
  1176. extern struct ip_vs_stats ip_vs_stats;
  1177. extern int sysctl_ip_vs_sync_ver;
  1178. struct ip_vs_service *
  1179. ip_vs_service_find(struct net *net, int af, __u32 fwmark, __u16 protocol,
  1180. const union nf_inet_addr *vaddr, __be16 vport);
  1181. bool ip_vs_has_real_service(struct net *net, int af, __u16 protocol,
  1182. const union nf_inet_addr *daddr, __be16 dport);
  1183. int ip_vs_use_count_inc(void);
  1184. void ip_vs_use_count_dec(void);
  1185. int ip_vs_register_nl_ioctl(void);
  1186. void ip_vs_unregister_nl_ioctl(void);
  1187. int ip_vs_control_init(void);
  1188. void ip_vs_control_cleanup(void);
  1189. struct ip_vs_dest *
  1190. ip_vs_find_dest(struct net *net, int svc_af, int dest_af,
  1191. const union nf_inet_addr *daddr, __be16 dport,
  1192. const union nf_inet_addr *vaddr, __be16 vport,
  1193. __u16 protocol, __u32 fwmark, __u32 flags);
  1194. void ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  1195. static inline void ip_vs_dest_hold(struct ip_vs_dest *dest)
  1196. {
  1197. atomic_inc(&dest->refcnt);
  1198. }
  1199. static inline void ip_vs_dest_put(struct ip_vs_dest *dest)
  1200. {
  1201. smp_mb__before_atomic();
  1202. atomic_dec(&dest->refcnt);
  1203. }
  1204. static inline void ip_vs_dest_put_and_free(struct ip_vs_dest *dest)
  1205. {
  1206. if (atomic_dec_return(&dest->refcnt) < 0)
  1207. kfree(dest);
  1208. }
  1209. /* IPVS sync daemon data and function prototypes
  1210. * (from ip_vs_sync.c)
  1211. */
  1212. int start_sync_thread(struct net *net, int state, char *mcast_ifn, __u8 syncid);
  1213. int stop_sync_thread(struct net *net, int state);
  1214. void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp, int pkts);
  1215. /* IPVS rate estimator prototypes (from ip_vs_est.c) */
  1216. void ip_vs_start_estimator(struct net *net, struct ip_vs_stats *stats);
  1217. void ip_vs_stop_estimator(struct net *net, struct ip_vs_stats *stats);
  1218. void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  1219. void ip_vs_read_estimator(struct ip_vs_kstats *dst, struct ip_vs_stats *stats);
  1220. /* Various IPVS packet transmitters (from ip_vs_xmit.c) */
  1221. int ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1222. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1223. int ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1224. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1225. int ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1226. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1227. int ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1228. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1229. int ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1230. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1231. int ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1232. struct ip_vs_protocol *pp, int offset,
  1233. unsigned int hooknum, struct ip_vs_iphdr *iph);
  1234. void ip_vs_dest_dst_rcu_free(struct rcu_head *head);
  1235. #ifdef CONFIG_IP_VS_IPV6
  1236. int ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1237. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1238. int ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1239. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1240. int ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1241. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1242. int ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1243. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1244. int ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1245. struct ip_vs_protocol *pp, int offset,
  1246. unsigned int hooknum, struct ip_vs_iphdr *iph);
  1247. #endif
  1248. #ifdef CONFIG_SYSCTL
  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. /* ip_vs_fwd_tag returns the forwarding tag of the connection */
  1266. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  1267. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  1268. {
  1269. char fwd;
  1270. switch (IP_VS_FWD_METHOD(cp)) {
  1271. case IP_VS_CONN_F_MASQ:
  1272. fwd = 'M'; break;
  1273. case IP_VS_CONN_F_LOCALNODE:
  1274. fwd = 'L'; break;
  1275. case IP_VS_CONN_F_TUNNEL:
  1276. fwd = 'T'; break;
  1277. case IP_VS_CONN_F_DROUTE:
  1278. fwd = 'R'; break;
  1279. case IP_VS_CONN_F_BYPASS:
  1280. fwd = 'B'; break;
  1281. default:
  1282. fwd = '?'; break;
  1283. }
  1284. return fwd;
  1285. }
  1286. void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1287. struct ip_vs_conn *cp, int dir);
  1288. #ifdef CONFIG_IP_VS_IPV6
  1289. void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1290. struct ip_vs_conn *cp, int dir);
  1291. #endif
  1292. __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  1293. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  1294. {
  1295. __be32 diff[2] = { ~old, new };
  1296. return csum_partial(diff, sizeof(diff), oldsum);
  1297. }
  1298. #ifdef CONFIG_IP_VS_IPV6
  1299. static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
  1300. __wsum oldsum)
  1301. {
  1302. __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
  1303. new[3], new[2], new[1], new[0] };
  1304. return csum_partial(diff, sizeof(diff), oldsum);
  1305. }
  1306. #endif
  1307. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  1308. {
  1309. __be16 diff[2] = { ~old, new };
  1310. return csum_partial(diff, sizeof(diff), oldsum);
  1311. }
  1312. /* Forget current conntrack (unconfirmed) and attach notrack entry */
  1313. static inline void ip_vs_notrack(struct sk_buff *skb)
  1314. {
  1315. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1316. enum ip_conntrack_info ctinfo;
  1317. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  1318. if (!ct || !nf_ct_is_untracked(ct)) {
  1319. nf_conntrack_put(skb->nfct);
  1320. skb->nfct = &nf_ct_untracked_get()->ct_general;
  1321. skb->nfctinfo = IP_CT_NEW;
  1322. nf_conntrack_get(skb->nfct);
  1323. }
  1324. #endif
  1325. }
  1326. #ifdef CONFIG_IP_VS_NFCT
  1327. /* Netfilter connection tracking
  1328. * (from ip_vs_nfct.c)
  1329. */
  1330. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1331. {
  1332. #ifdef CONFIG_SYSCTL
  1333. return ipvs->sysctl_conntrack;
  1334. #else
  1335. return 0;
  1336. #endif
  1337. }
  1338. void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
  1339. int outin);
  1340. int ip_vs_confirm_conntrack(struct sk_buff *skb);
  1341. void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
  1342. struct ip_vs_conn *cp, u_int8_t proto,
  1343. const __be16 port, int from_rs);
  1344. void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
  1345. #else
  1346. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1347. {
  1348. return 0;
  1349. }
  1350. static inline void ip_vs_update_conntrack(struct sk_buff *skb,
  1351. struct ip_vs_conn *cp, int outin)
  1352. {
  1353. }
  1354. static inline int ip_vs_confirm_conntrack(struct sk_buff *skb)
  1355. {
  1356. return NF_ACCEPT;
  1357. }
  1358. static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
  1359. {
  1360. }
  1361. #endif /* CONFIG_IP_VS_NFCT */
  1362. static inline int
  1363. ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
  1364. {
  1365. /* We think the overhead of processing active connections is 256
  1366. * times higher than that of inactive connections in average. (This
  1367. * 256 times might not be accurate, we will change it later) We
  1368. * use the following formula to estimate the overhead now:
  1369. * dest->activeconns*256 + dest->inactconns
  1370. */
  1371. return (atomic_read(&dest->activeconns) << 8) +
  1372. atomic_read(&dest->inactconns);
  1373. }
  1374. #endif /* _NET_IP_VS_H */