ip_vs.h 47 KB

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