ip_vs.h 50 KB

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