ip_vs.h 48 KB

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