ip_vs.h 46 KB

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