ip_vs.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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. };
  628. /* The application module object (a.k.a. app incarnation) */
  629. struct ip_vs_app {
  630. struct list_head a_list; /* member in app list */
  631. int type; /* IP_VS_APP_TYPE_xxx */
  632. char *name; /* application module name */
  633. __u16 protocol;
  634. struct module *module; /* THIS_MODULE/NULL */
  635. struct list_head incs_list; /* list of incarnations */
  636. /* members for application incarnations */
  637. struct list_head p_list; /* member in proto app list */
  638. struct ip_vs_app *app; /* its real application */
  639. __be16 port; /* port number in net order */
  640. atomic_t usecnt; /* usage counter */
  641. struct rcu_head rcu_head;
  642. /* output hook: Process packet in inout direction, diff set for TCP.
  643. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  644. * 2=Mangled but checksum was not updated
  645. */
  646. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  647. struct sk_buff *, int *diff);
  648. /* input hook: Process packet in outin 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_in)(struct ip_vs_app *, struct ip_vs_conn *,
  653. struct sk_buff *, int *diff);
  654. /* ip_vs_app initializer */
  655. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  656. /* ip_vs_app finish */
  657. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  658. /* not used now */
  659. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  660. struct ip_vs_protocol *);
  661. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  662. int * timeout_table;
  663. int * timeouts;
  664. int timeouts_size;
  665. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  666. int *verdict, struct ip_vs_conn **cpp);
  667. struct ip_vs_conn *
  668. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  669. const struct iphdr *iph, int inverse);
  670. struct ip_vs_conn *
  671. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  672. const struct iphdr *iph, int inverse);
  673. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  674. const struct sk_buff *skb,
  675. struct ip_vs_app *app);
  676. void (*timeout_change)(struct ip_vs_app *app, int flags);
  677. };
  678. struct ipvs_master_sync_state {
  679. struct list_head sync_queue;
  680. struct ip_vs_sync_buff *sync_buff;
  681. unsigned long sync_queue_len;
  682. unsigned int sync_queue_delay;
  683. struct task_struct *master_thread;
  684. struct delayed_work master_wakeup_work;
  685. struct netns_ipvs *ipvs;
  686. };
  687. /* How much time to keep dests in trash */
  688. #define IP_VS_DEST_TRASH_PERIOD (120 * HZ)
  689. struct ipvs_sync_daemon_cfg {
  690. union nf_inet_addr mcast_group;
  691. int syncid;
  692. u16 sync_maxlen;
  693. u16 mcast_port;
  694. u8 mcast_af;
  695. u8 mcast_ttl;
  696. /* multicast interface name */
  697. char mcast_ifn[IP_VS_IFNAME_MAXLEN];
  698. };
  699. /* IPVS in network namespace */
  700. struct netns_ipvs {
  701. int gen; /* Generation */
  702. int enable; /* enable like nf_hooks do */
  703. /* Hash table: for real service lookups */
  704. #define IP_VS_RTAB_BITS 4
  705. #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
  706. #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
  707. struct hlist_head rs_table[IP_VS_RTAB_SIZE];
  708. /* ip_vs_app */
  709. struct list_head app_list;
  710. /* ip_vs_proto */
  711. #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
  712. struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE];
  713. /* ip_vs_proto_tcp */
  714. #ifdef CONFIG_IP_VS_PROTO_TCP
  715. #define TCP_APP_TAB_BITS 4
  716. #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
  717. #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
  718. struct list_head tcp_apps[TCP_APP_TAB_SIZE];
  719. #endif
  720. /* ip_vs_proto_udp */
  721. #ifdef CONFIG_IP_VS_PROTO_UDP
  722. #define UDP_APP_TAB_BITS 4
  723. #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
  724. #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
  725. struct list_head udp_apps[UDP_APP_TAB_SIZE];
  726. #endif
  727. /* ip_vs_proto_sctp */
  728. #ifdef CONFIG_IP_VS_PROTO_SCTP
  729. #define SCTP_APP_TAB_BITS 4
  730. #define SCTP_APP_TAB_SIZE (1 << SCTP_APP_TAB_BITS)
  731. #define SCTP_APP_TAB_MASK (SCTP_APP_TAB_SIZE - 1)
  732. /* Hash table for SCTP application incarnations */
  733. struct list_head sctp_apps[SCTP_APP_TAB_SIZE];
  734. #endif
  735. /* ip_vs_conn */
  736. atomic_t conn_count; /* connection counter */
  737. /* ip_vs_ctl */
  738. struct ip_vs_stats tot_stats; /* Statistics & est. */
  739. int num_services; /* no of virtual services */
  740. /* Trash for destinations */
  741. struct list_head dest_trash;
  742. spinlock_t dest_trash_lock;
  743. struct timer_list dest_trash_timer; /* expiration timer */
  744. /* Service counters */
  745. atomic_t ftpsvc_counter;
  746. atomic_t nullsvc_counter;
  747. #ifdef CONFIG_SYSCTL
  748. /* 1/rate drop and drop-entry variables */
  749. struct delayed_work defense_work; /* Work handler */
  750. int drop_rate;
  751. int drop_counter;
  752. atomic_t dropentry;
  753. /* locks in ctl.c */
  754. spinlock_t dropentry_lock; /* drop entry handling */
  755. spinlock_t droppacket_lock; /* drop packet handling */
  756. spinlock_t securetcp_lock; /* state and timeout tables */
  757. /* sys-ctl struct */
  758. struct ctl_table_header *sysctl_hdr;
  759. struct ctl_table *sysctl_tbl;
  760. #endif
  761. /* sysctl variables */
  762. int sysctl_amemthresh;
  763. int sysctl_am_droprate;
  764. int sysctl_drop_entry;
  765. int sysctl_drop_packet;
  766. int sysctl_secure_tcp;
  767. #ifdef CONFIG_IP_VS_NFCT
  768. int sysctl_conntrack;
  769. #endif
  770. int sysctl_snat_reroute;
  771. int sysctl_sync_ver;
  772. int sysctl_sync_ports;
  773. int sysctl_sync_persist_mode;
  774. unsigned long sysctl_sync_qlen_max;
  775. int sysctl_sync_sock_size;
  776. int sysctl_cache_bypass;
  777. int sysctl_expire_nodest_conn;
  778. int sysctl_sloppy_tcp;
  779. int sysctl_sloppy_sctp;
  780. int sysctl_expire_quiescent_template;
  781. int sysctl_sync_threshold[2];
  782. unsigned int sysctl_sync_refresh_period;
  783. int sysctl_sync_retries;
  784. int sysctl_nat_icmp_send;
  785. int sysctl_pmtu_disc;
  786. int sysctl_backup_only;
  787. int sysctl_conn_reuse_mode;
  788. int sysctl_schedule_icmp;
  789. int sysctl_ignore_tunneled;
  790. /* ip_vs_lblc */
  791. int sysctl_lblc_expiration;
  792. struct ctl_table_header *lblc_ctl_header;
  793. struct ctl_table *lblc_ctl_table;
  794. /* ip_vs_lblcr */
  795. int sysctl_lblcr_expiration;
  796. struct ctl_table_header *lblcr_ctl_header;
  797. struct ctl_table *lblcr_ctl_table;
  798. /* ip_vs_est */
  799. struct list_head est_list; /* estimator list */
  800. spinlock_t est_lock;
  801. struct timer_list est_timer; /* Estimation timer */
  802. /* ip_vs_sync */
  803. spinlock_t sync_lock;
  804. struct ipvs_master_sync_state *ms;
  805. spinlock_t sync_buff_lock;
  806. struct task_struct **backup_threads;
  807. int threads_mask;
  808. volatile int sync_state;
  809. struct mutex sync_mutex;
  810. struct ipvs_sync_daemon_cfg mcfg; /* Master Configuration */
  811. struct ipvs_sync_daemon_cfg bcfg; /* Backup Configuration */
  812. /* net name space ptr */
  813. struct net *net; /* Needed by timer routines */
  814. /* Number of heterogeneous destinations, needed becaus heterogeneous
  815. * are not supported when synchronization is enabled.
  816. */
  817. unsigned int mixed_address_family_dests;
  818. };
  819. #define DEFAULT_SYNC_THRESHOLD 3
  820. #define DEFAULT_SYNC_PERIOD 50
  821. #define DEFAULT_SYNC_VER 1
  822. #define DEFAULT_SLOPPY_TCP 0
  823. #define DEFAULT_SLOPPY_SCTP 0
  824. #define DEFAULT_SYNC_REFRESH_PERIOD (0U * HZ)
  825. #define DEFAULT_SYNC_RETRIES 0
  826. #define IPVS_SYNC_WAKEUP_RATE 8
  827. #define IPVS_SYNC_QLEN_MAX (IPVS_SYNC_WAKEUP_RATE * 4)
  828. #define IPVS_SYNC_SEND_DELAY (HZ / 50)
  829. #define IPVS_SYNC_CHECK_PERIOD HZ
  830. #define IPVS_SYNC_FLUSH_TIME (HZ * 2)
  831. #define IPVS_SYNC_PORTS_MAX (1 << 6)
  832. #ifdef CONFIG_SYSCTL
  833. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  834. {
  835. return ipvs->sysctl_sync_threshold[0];
  836. }
  837. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  838. {
  839. return ACCESS_ONCE(ipvs->sysctl_sync_threshold[1]);
  840. }
  841. static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
  842. {
  843. return ACCESS_ONCE(ipvs->sysctl_sync_refresh_period);
  844. }
  845. static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
  846. {
  847. return ipvs->sysctl_sync_retries;
  848. }
  849. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  850. {
  851. return ipvs->sysctl_sync_ver;
  852. }
  853. static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
  854. {
  855. return ipvs->sysctl_sloppy_tcp;
  856. }
  857. static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
  858. {
  859. return ipvs->sysctl_sloppy_sctp;
  860. }
  861. static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
  862. {
  863. return ACCESS_ONCE(ipvs->sysctl_sync_ports);
  864. }
  865. static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
  866. {
  867. return ipvs->sysctl_sync_persist_mode;
  868. }
  869. static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
  870. {
  871. return ipvs->sysctl_sync_qlen_max;
  872. }
  873. static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
  874. {
  875. return ipvs->sysctl_sync_sock_size;
  876. }
  877. static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
  878. {
  879. return ipvs->sysctl_pmtu_disc;
  880. }
  881. static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
  882. {
  883. return ipvs->sync_state & IP_VS_STATE_BACKUP &&
  884. ipvs->sysctl_backup_only;
  885. }
  886. static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
  887. {
  888. return ipvs->sysctl_conn_reuse_mode;
  889. }
  890. static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs)
  891. {
  892. return ipvs->sysctl_schedule_icmp;
  893. }
  894. static inline int sysctl_ignore_tunneled(struct netns_ipvs *ipvs)
  895. {
  896. return ipvs->sysctl_ignore_tunneled;
  897. }
  898. static inline int sysctl_cache_bypass(struct netns_ipvs *ipvs)
  899. {
  900. return ipvs->sysctl_cache_bypass;
  901. }
  902. #else
  903. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  904. {
  905. return DEFAULT_SYNC_THRESHOLD;
  906. }
  907. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  908. {
  909. return DEFAULT_SYNC_PERIOD;
  910. }
  911. static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
  912. {
  913. return DEFAULT_SYNC_REFRESH_PERIOD;
  914. }
  915. static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
  916. {
  917. return DEFAULT_SYNC_RETRIES & 3;
  918. }
  919. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  920. {
  921. return DEFAULT_SYNC_VER;
  922. }
  923. static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
  924. {
  925. return DEFAULT_SLOPPY_TCP;
  926. }
  927. static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
  928. {
  929. return DEFAULT_SLOPPY_SCTP;
  930. }
  931. static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
  932. {
  933. return 1;
  934. }
  935. static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
  936. {
  937. return 0;
  938. }
  939. static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
  940. {
  941. return IPVS_SYNC_QLEN_MAX;
  942. }
  943. static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
  944. {
  945. return 0;
  946. }
  947. static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
  948. {
  949. return 1;
  950. }
  951. static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
  952. {
  953. return 0;
  954. }
  955. static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
  956. {
  957. return 1;
  958. }
  959. static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs)
  960. {
  961. return 0;
  962. }
  963. static inline int sysctl_ignore_tunneled(struct netns_ipvs *ipvs)
  964. {
  965. return 0;
  966. }
  967. static inline int sysctl_cache_bypass(struct netns_ipvs *ipvs)
  968. {
  969. return 0;
  970. }
  971. #endif
  972. /* IPVS core functions
  973. * (from ip_vs_core.c)
  974. */
  975. const char *ip_vs_proto_name(unsigned int proto);
  976. void ip_vs_init_hash_table(struct list_head *table, int rows);
  977. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
  978. #define IP_VS_APP_TYPE_FTP 1
  979. /* ip_vs_conn handling functions
  980. * (from ip_vs_conn.c)
  981. */
  982. enum {
  983. IP_VS_DIR_INPUT = 0,
  984. IP_VS_DIR_OUTPUT,
  985. IP_VS_DIR_INPUT_ONLY,
  986. IP_VS_DIR_LAST,
  987. };
  988. static inline void ip_vs_conn_fill_param(struct netns_ipvs *ipvs, int af, int protocol,
  989. const union nf_inet_addr *caddr,
  990. __be16 cport,
  991. const union nf_inet_addr *vaddr,
  992. __be16 vport,
  993. struct ip_vs_conn_param *p)
  994. {
  995. p->ipvs = ipvs;
  996. p->af = af;
  997. p->protocol = protocol;
  998. p->caddr = caddr;
  999. p->cport = cport;
  1000. p->vaddr = vaddr;
  1001. p->vport = vport;
  1002. p->pe = NULL;
  1003. p->pe_data = NULL;
  1004. }
  1005. struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
  1006. struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
  1007. struct ip_vs_conn * ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
  1008. const struct sk_buff *skb,
  1009. const struct ip_vs_iphdr *iph);
  1010. struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
  1011. struct ip_vs_conn * ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
  1012. const struct sk_buff *skb,
  1013. const struct ip_vs_iphdr *iph);
  1014. /* Get reference to gain full access to conn.
  1015. * By default, RCU read-side critical sections have access only to
  1016. * conn fields and its PE data, see ip_vs_conn_rcu_free() for reference.
  1017. */
  1018. static inline bool __ip_vs_conn_get(struct ip_vs_conn *cp)
  1019. {
  1020. return atomic_inc_not_zero(&cp->refcnt);
  1021. }
  1022. /* put back the conn without restarting its timer */
  1023. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  1024. {
  1025. smp_mb__before_atomic();
  1026. atomic_dec(&cp->refcnt);
  1027. }
  1028. void ip_vs_conn_put(struct ip_vs_conn *cp);
  1029. void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  1030. struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
  1031. const union nf_inet_addr *daddr,
  1032. __be16 dport, unsigned int flags,
  1033. struct ip_vs_dest *dest, __u32 fwmark);
  1034. void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  1035. const char *ip_vs_state_name(__u16 proto, int state);
  1036. void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
  1037. int ip_vs_check_template(struct ip_vs_conn *ct);
  1038. void ip_vs_random_dropentry(struct netns_ipvs *ipvs);
  1039. int ip_vs_conn_init(void);
  1040. void ip_vs_conn_cleanup(void);
  1041. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  1042. {
  1043. struct ip_vs_conn *ctl_cp = cp->control;
  1044. if (!ctl_cp) {
  1045. IP_VS_ERR_BUF("request control DEL for uncontrolled: "
  1046. "%s:%d to %s:%d\n",
  1047. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1048. ntohs(cp->cport),
  1049. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1050. ntohs(cp->vport));
  1051. return;
  1052. }
  1053. IP_VS_DBG_BUF(7, "DELeting control for: "
  1054. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  1055. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1056. ntohs(cp->cport),
  1057. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  1058. ntohs(ctl_cp->cport));
  1059. cp->control = NULL;
  1060. if (atomic_read(&ctl_cp->n_control) == 0) {
  1061. IP_VS_ERR_BUF("BUG control DEL with n=0 : "
  1062. "%s:%d to %s:%d\n",
  1063. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1064. ntohs(cp->cport),
  1065. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1066. ntohs(cp->vport));
  1067. return;
  1068. }
  1069. atomic_dec(&ctl_cp->n_control);
  1070. }
  1071. static inline void
  1072. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  1073. {
  1074. if (cp->control) {
  1075. IP_VS_ERR_BUF("request control ADD for already controlled: "
  1076. "%s:%d to %s:%d\n",
  1077. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1078. ntohs(cp->cport),
  1079. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1080. ntohs(cp->vport));
  1081. ip_vs_control_del(cp);
  1082. }
  1083. IP_VS_DBG_BUF(7, "ADDing control for: "
  1084. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  1085. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1086. ntohs(cp->cport),
  1087. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  1088. ntohs(ctl_cp->cport));
  1089. cp->control = ctl_cp;
  1090. atomic_inc(&ctl_cp->n_control);
  1091. }
  1092. /* IPVS netns init & cleanup functions */
  1093. int ip_vs_estimator_net_init(struct netns_ipvs *ipvs);
  1094. int ip_vs_control_net_init(struct netns_ipvs *ipvs);
  1095. int ip_vs_protocol_net_init(struct netns_ipvs *ipvs);
  1096. int ip_vs_app_net_init(struct netns_ipvs *ipvs);
  1097. int ip_vs_conn_net_init(struct netns_ipvs *ipvs);
  1098. int ip_vs_sync_net_init(struct netns_ipvs *ipvs);
  1099. void ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs);
  1100. void ip_vs_app_net_cleanup(struct netns_ipvs *ipvs);
  1101. void ip_vs_protocol_net_cleanup(struct netns_ipvs *ipvs);
  1102. void ip_vs_control_net_cleanup(struct netns_ipvs *ipvs);
  1103. void ip_vs_estimator_net_cleanup(struct netns_ipvs *ipvs);
  1104. void ip_vs_sync_net_cleanup(struct netns_ipvs *ipvs);
  1105. void ip_vs_service_net_cleanup(struct netns_ipvs *ipvs);
  1106. /* IPVS application functions
  1107. * (from ip_vs_app.c)
  1108. */
  1109. #define IP_VS_APP_MAX_PORTS 8
  1110. struct ip_vs_app *register_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app);
  1111. void unregister_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app);
  1112. int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1113. void ip_vs_unbind_app(struct ip_vs_conn *cp);
  1114. int register_ip_vs_app_inc(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
  1115. __u16 port);
  1116. int ip_vs_app_inc_get(struct ip_vs_app *inc);
  1117. void ip_vs_app_inc_put(struct ip_vs_app *inc);
  1118. int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
  1119. int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
  1120. int register_ip_vs_pe(struct ip_vs_pe *pe);
  1121. int unregister_ip_vs_pe(struct ip_vs_pe *pe);
  1122. struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
  1123. struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
  1124. /* Use a #define to avoid all of module.h just for these trivial ops */
  1125. #define ip_vs_pe_get(pe) \
  1126. if (pe && pe->module) \
  1127. __module_get(pe->module);
  1128. #define ip_vs_pe_put(pe) \
  1129. if (pe && pe->module) \
  1130. module_put(pe->module);
  1131. /* IPVS protocol functions (from ip_vs_proto.c) */
  1132. int ip_vs_protocol_init(void);
  1133. void ip_vs_protocol_cleanup(void);
  1134. void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
  1135. int *ip_vs_create_timeout_table(int *table, int size);
  1136. int ip_vs_set_state_timeout(int *table, int num, const char *const *names,
  1137. const char *name, int to);
  1138. void ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
  1139. const struct sk_buff *skb, int offset,
  1140. const char *msg);
  1141. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  1142. extern struct ip_vs_protocol ip_vs_protocol_udp;
  1143. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  1144. extern struct ip_vs_protocol ip_vs_protocol_esp;
  1145. extern struct ip_vs_protocol ip_vs_protocol_ah;
  1146. extern struct ip_vs_protocol ip_vs_protocol_sctp;
  1147. /* Registering/unregistering scheduler functions
  1148. * (from ip_vs_sched.c)
  1149. */
  1150. int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1151. int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1152. int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  1153. struct ip_vs_scheduler *scheduler);
  1154. void ip_vs_unbind_scheduler(struct ip_vs_service *svc,
  1155. struct ip_vs_scheduler *sched);
  1156. struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  1157. void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  1158. struct ip_vs_conn *
  1159. ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
  1160. struct ip_vs_proto_data *pd, int *ignored,
  1161. struct ip_vs_iphdr *iph);
  1162. int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  1163. struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph);
  1164. void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
  1165. /* IPVS control data and functions (from ip_vs_ctl.c) */
  1166. extern struct ip_vs_stats ip_vs_stats;
  1167. extern int sysctl_ip_vs_sync_ver;
  1168. struct ip_vs_service *
  1169. ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
  1170. const union nf_inet_addr *vaddr, __be16 vport);
  1171. bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
  1172. const union nf_inet_addr *daddr, __be16 dport);
  1173. int ip_vs_use_count_inc(void);
  1174. void ip_vs_use_count_dec(void);
  1175. int ip_vs_register_nl_ioctl(void);
  1176. void ip_vs_unregister_nl_ioctl(void);
  1177. int ip_vs_control_init(void);
  1178. void ip_vs_control_cleanup(void);
  1179. struct ip_vs_dest *
  1180. ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
  1181. const union nf_inet_addr *daddr, __be16 dport,
  1182. const union nf_inet_addr *vaddr, __be16 vport,
  1183. __u16 protocol, __u32 fwmark, __u32 flags);
  1184. void ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  1185. static inline void ip_vs_dest_hold(struct ip_vs_dest *dest)
  1186. {
  1187. atomic_inc(&dest->refcnt);
  1188. }
  1189. static inline void ip_vs_dest_put(struct ip_vs_dest *dest)
  1190. {
  1191. smp_mb__before_atomic();
  1192. atomic_dec(&dest->refcnt);
  1193. }
  1194. static inline void ip_vs_dest_put_and_free(struct ip_vs_dest *dest)
  1195. {
  1196. if (atomic_dec_return(&dest->refcnt) < 0)
  1197. kfree(dest);
  1198. }
  1199. /* IPVS sync daemon data and function prototypes
  1200. * (from ip_vs_sync.c)
  1201. */
  1202. int start_sync_thread(struct netns_ipvs *ipvs, struct ipvs_sync_daemon_cfg *cfg,
  1203. int state);
  1204. int stop_sync_thread(struct netns_ipvs *ipvs, int state);
  1205. void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts);
  1206. /* IPVS rate estimator prototypes (from ip_vs_est.c) */
  1207. void ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
  1208. void ip_vs_stop_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
  1209. void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  1210. void ip_vs_read_estimator(struct ip_vs_kstats *dst, struct ip_vs_stats *stats);
  1211. /* Various IPVS packet transmitters (from ip_vs_xmit.c) */
  1212. int ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1213. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1214. int ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1215. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1216. int ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1217. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1218. int ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1219. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1220. int ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1221. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1222. int ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1223. struct ip_vs_protocol *pp, int offset,
  1224. unsigned int hooknum, struct ip_vs_iphdr *iph);
  1225. void ip_vs_dest_dst_rcu_free(struct rcu_head *head);
  1226. #ifdef CONFIG_IP_VS_IPV6
  1227. int ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1228. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1229. int ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1230. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1231. int ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1232. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1233. int ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1234. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1235. int ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1236. struct ip_vs_protocol *pp, int offset,
  1237. unsigned int hooknum, struct ip_vs_iphdr *iph);
  1238. #endif
  1239. #ifdef CONFIG_SYSCTL
  1240. /* This is a simple mechanism to ignore packets when
  1241. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  1242. * we start to drop 1/rate of the packets
  1243. */
  1244. static inline int ip_vs_todrop(struct netns_ipvs *ipvs)
  1245. {
  1246. if (!ipvs->drop_rate)
  1247. return 0;
  1248. if (--ipvs->drop_counter > 0)
  1249. return 0;
  1250. ipvs->drop_counter = ipvs->drop_rate;
  1251. return 1;
  1252. }
  1253. #else
  1254. static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; }
  1255. #endif
  1256. /* ip_vs_fwd_tag returns the forwarding tag of the connection */
  1257. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  1258. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  1259. {
  1260. char fwd;
  1261. switch (IP_VS_FWD_METHOD(cp)) {
  1262. case IP_VS_CONN_F_MASQ:
  1263. fwd = 'M'; break;
  1264. case IP_VS_CONN_F_LOCALNODE:
  1265. fwd = 'L'; break;
  1266. case IP_VS_CONN_F_TUNNEL:
  1267. fwd = 'T'; break;
  1268. case IP_VS_CONN_F_DROUTE:
  1269. fwd = 'R'; break;
  1270. case IP_VS_CONN_F_BYPASS:
  1271. fwd = 'B'; break;
  1272. default:
  1273. fwd = '?'; break;
  1274. }
  1275. return fwd;
  1276. }
  1277. void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1278. struct ip_vs_conn *cp, int dir);
  1279. #ifdef CONFIG_IP_VS_IPV6
  1280. void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1281. struct ip_vs_conn *cp, int dir);
  1282. #endif
  1283. __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  1284. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  1285. {
  1286. __be32 diff[2] = { ~old, new };
  1287. return csum_partial(diff, sizeof(diff), oldsum);
  1288. }
  1289. #ifdef CONFIG_IP_VS_IPV6
  1290. static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
  1291. __wsum oldsum)
  1292. {
  1293. __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
  1294. new[3], new[2], new[1], new[0] };
  1295. return csum_partial(diff, sizeof(diff), oldsum);
  1296. }
  1297. #endif
  1298. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  1299. {
  1300. __be16 diff[2] = { ~old, new };
  1301. return csum_partial(diff, sizeof(diff), oldsum);
  1302. }
  1303. /* Forget current conntrack (unconfirmed) and attach notrack entry */
  1304. static inline void ip_vs_notrack(struct sk_buff *skb)
  1305. {
  1306. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1307. enum ip_conntrack_info ctinfo;
  1308. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  1309. if (!ct || !nf_ct_is_untracked(ct)) {
  1310. nf_conntrack_put(skb->nfct);
  1311. skb->nfct = &nf_ct_untracked_get()->ct_general;
  1312. skb->nfctinfo = IP_CT_NEW;
  1313. nf_conntrack_get(skb->nfct);
  1314. }
  1315. #endif
  1316. }
  1317. #ifdef CONFIG_IP_VS_NFCT
  1318. /* Netfilter connection tracking
  1319. * (from ip_vs_nfct.c)
  1320. */
  1321. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1322. {
  1323. #ifdef CONFIG_SYSCTL
  1324. return ipvs->sysctl_conntrack;
  1325. #else
  1326. return 0;
  1327. #endif
  1328. }
  1329. void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
  1330. int outin);
  1331. int ip_vs_confirm_conntrack(struct sk_buff *skb);
  1332. void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
  1333. struct ip_vs_conn *cp, u_int8_t proto,
  1334. const __be16 port, int from_rs);
  1335. void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
  1336. #else
  1337. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1338. {
  1339. return 0;
  1340. }
  1341. static inline void ip_vs_update_conntrack(struct sk_buff *skb,
  1342. struct ip_vs_conn *cp, int outin)
  1343. {
  1344. }
  1345. static inline int ip_vs_confirm_conntrack(struct sk_buff *skb)
  1346. {
  1347. return NF_ACCEPT;
  1348. }
  1349. static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
  1350. {
  1351. }
  1352. #endif /* CONFIG_IP_VS_NFCT */
  1353. /* Really using conntrack? */
  1354. static inline bool ip_vs_conn_uses_conntrack(struct ip_vs_conn *cp,
  1355. struct sk_buff *skb)
  1356. {
  1357. #ifdef CONFIG_IP_VS_NFCT
  1358. enum ip_conntrack_info ctinfo;
  1359. struct nf_conn *ct;
  1360. if (!(cp->flags & IP_VS_CONN_F_NFCT))
  1361. return false;
  1362. ct = nf_ct_get(skb, &ctinfo);
  1363. if (ct && !nf_ct_is_untracked(ct))
  1364. return true;
  1365. #endif
  1366. return false;
  1367. }
  1368. static inline int
  1369. ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
  1370. {
  1371. /* We think the overhead of processing active connections is 256
  1372. * times higher than that of inactive connections in average. (This
  1373. * 256 times might not be accurate, we will change it later) We
  1374. * use the following formula to estimate the overhead now:
  1375. * dest->activeconns*256 + dest->inactconns
  1376. */
  1377. return (atomic_read(&dest->activeconns) << 8) +
  1378. atomic_read(&dest->inactconns);
  1379. }
  1380. #endif /* _NET_IP_VS_H */