ip_vs.h 46 KB

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