ip_vs.h 45 KB

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