ip_vs.h 46 KB

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