ip_vs.h 47 KB

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