ip_vs.h 35 KB

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