ip_vs.h 47 KB

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