ip_vs.h 47 KB

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