ip_vs.h 42 KB

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