ip_vs.h 46 KB

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