ip_vs.h 45 KB

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