ip_vs.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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/sysctl.h> /* for ctl_path */
  10. #include <linux/list.h> /* for struct list_head */
  11. #include <linux/spinlock.h> /* for struct rwlock_t */
  12. #include <asm/atomic.h> /* for struct atomic_t */
  13. #include <linux/compiler.h>
  14. #include <linux/timer.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> /* for ipv6_addr_copy */
  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. ipv6_addr_copy(&iphdr->saddr.in6, &iph->saddr);
  112. ipv6_addr_copy(&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. ipv6_addr_copy(&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. void (*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. int (*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. /* Control members */
  447. struct ip_vs_conn *control; /* Master control connection */
  448. atomic_t n_control; /* Number of controlled ones */
  449. struct ip_vs_dest *dest; /* real server */
  450. atomic_t in_pkts; /* incoming packet counter */
  451. /* packet transmitter for different forwarding methods. If it
  452. mangles the packet, it must return NF_DROP or better NF_STOLEN,
  453. otherwise this must be changed to a sk_buff **.
  454. NF_ACCEPT can be returned when destination is local.
  455. */
  456. int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
  457. struct ip_vs_protocol *pp);
  458. /* Note: we can group the following members into a structure,
  459. in order to save more space, and the following members are
  460. only used in VS/NAT anyway */
  461. struct ip_vs_app *app; /* bound ip_vs_app object */
  462. void *app_data; /* Application private data */
  463. struct ip_vs_seq in_seq; /* incoming seq. struct */
  464. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  465. const struct ip_vs_pe *pe;
  466. char *pe_data;
  467. __u8 pe_data_len;
  468. };
  469. /*
  470. * To save some memory in conn table when name space is disabled.
  471. */
  472. static inline struct net *ip_vs_conn_net(const struct ip_vs_conn *cp)
  473. {
  474. #ifdef CONFIG_NET_NS
  475. return cp->net;
  476. #else
  477. return &init_net;
  478. #endif
  479. }
  480. static inline void ip_vs_conn_net_set(struct ip_vs_conn *cp, struct net *net)
  481. {
  482. #ifdef CONFIG_NET_NS
  483. cp->net = net;
  484. #endif
  485. }
  486. static inline int ip_vs_conn_net_eq(const struct ip_vs_conn *cp,
  487. struct net *net)
  488. {
  489. #ifdef CONFIG_NET_NS
  490. return cp->net == net;
  491. #else
  492. return 1;
  493. #endif
  494. }
  495. /*
  496. * Extended internal versions of struct ip_vs_service_user and
  497. * ip_vs_dest_user for IPv6 support.
  498. *
  499. * We need these to conveniently pass around service and destination
  500. * options, but unfortunately, we also need to keep the old definitions to
  501. * maintain userspace backwards compatibility for the setsockopt interface.
  502. */
  503. struct ip_vs_service_user_kern {
  504. /* virtual service addresses */
  505. u16 af;
  506. u16 protocol;
  507. union nf_inet_addr addr; /* virtual ip address */
  508. u16 port;
  509. u32 fwmark; /* firwall mark of service */
  510. /* virtual service options */
  511. char *sched_name;
  512. char *pe_name;
  513. unsigned flags; /* virtual service flags */
  514. unsigned timeout; /* persistent timeout in sec */
  515. u32 netmask; /* persistent netmask */
  516. };
  517. struct ip_vs_dest_user_kern {
  518. /* destination server address */
  519. union nf_inet_addr addr;
  520. u16 port;
  521. /* real server options */
  522. unsigned conn_flags; /* connection flags */
  523. int weight; /* destination weight */
  524. /* thresholds for active connections */
  525. u32 u_threshold; /* upper threshold */
  526. u32 l_threshold; /* lower threshold */
  527. };
  528. /*
  529. * The information about the virtual service offered to the net
  530. * and the forwarding entries
  531. */
  532. struct ip_vs_service {
  533. struct list_head s_list; /* for normal service table */
  534. struct list_head f_list; /* for fwmark-based service table */
  535. atomic_t refcnt; /* reference counter */
  536. atomic_t usecnt; /* use counter */
  537. u16 af; /* address family */
  538. __u16 protocol; /* which protocol (TCP/UDP) */
  539. union nf_inet_addr addr; /* IP address for virtual service */
  540. __be16 port; /* port number for the service */
  541. __u32 fwmark; /* firewall mark of the service */
  542. unsigned flags; /* service status flags */
  543. unsigned timeout; /* persistent timeout in ticks */
  544. __be32 netmask; /* grouping granularity */
  545. struct net *net;
  546. struct list_head destinations; /* real server d-linked list */
  547. __u32 num_dests; /* number of servers */
  548. struct ip_vs_stats stats; /* statistics for the service */
  549. struct ip_vs_app *inc; /* bind conns to this app inc */
  550. /* for scheduling */
  551. struct ip_vs_scheduler *scheduler; /* bound scheduler object */
  552. rwlock_t sched_lock; /* lock sched_data */
  553. void *sched_data; /* scheduler application data */
  554. /* alternate persistence engine */
  555. struct ip_vs_pe *pe;
  556. };
  557. /*
  558. * The real server destination forwarding entry
  559. * with ip address, port number, and so on.
  560. */
  561. struct ip_vs_dest {
  562. struct list_head n_list; /* for the dests in the service */
  563. struct list_head d_list; /* for table with all the dests */
  564. u16 af; /* address family */
  565. __be16 port; /* port number of the server */
  566. union nf_inet_addr addr; /* IP address of the server */
  567. volatile unsigned flags; /* dest status flags */
  568. atomic_t conn_flags; /* flags to copy to conn */
  569. atomic_t weight; /* server weight */
  570. atomic_t refcnt; /* reference counter */
  571. struct ip_vs_stats stats; /* statistics */
  572. /* connection counters and thresholds */
  573. atomic_t activeconns; /* active connections */
  574. atomic_t inactconns; /* inactive connections */
  575. atomic_t persistconns; /* persistent connections */
  576. __u32 u_threshold; /* upper threshold */
  577. __u32 l_threshold; /* lower threshold */
  578. /* for destination cache */
  579. spinlock_t dst_lock; /* lock of dst_cache */
  580. struct dst_entry *dst_cache; /* destination cache entry */
  581. u32 dst_rtos; /* RT_TOS(tos) for dst */
  582. u32 dst_cookie;
  583. union nf_inet_addr dst_saddr;
  584. /* for virtual service */
  585. struct ip_vs_service *svc; /* service it belongs to */
  586. __u16 protocol; /* which protocol (TCP/UDP) */
  587. __be16 vport; /* virtual port number */
  588. union nf_inet_addr vaddr; /* virtual IP address */
  589. __u32 vfwmark; /* firewall mark of service */
  590. };
  591. /*
  592. * The scheduler object
  593. */
  594. struct ip_vs_scheduler {
  595. struct list_head n_list; /* d-linked list head */
  596. char *name; /* scheduler name */
  597. atomic_t refcnt; /* reference counter */
  598. struct module *module; /* THIS_MODULE/NULL */
  599. /* scheduler initializing service */
  600. int (*init_service)(struct ip_vs_service *svc);
  601. /* scheduling service finish */
  602. int (*done_service)(struct ip_vs_service *svc);
  603. /* scheduler updating service */
  604. int (*update_service)(struct ip_vs_service *svc);
  605. /* selecting a server from the given service */
  606. struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
  607. const struct sk_buff *skb);
  608. };
  609. /* The persistence engine object */
  610. struct ip_vs_pe {
  611. struct list_head n_list; /* d-linked list head */
  612. char *name; /* scheduler name */
  613. atomic_t refcnt; /* reference counter */
  614. struct module *module; /* THIS_MODULE/NULL */
  615. /* get the connection template, if any */
  616. int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
  617. bool (*ct_match)(const struct ip_vs_conn_param *p,
  618. struct ip_vs_conn *ct);
  619. u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
  620. bool inverse);
  621. int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
  622. };
  623. /*
  624. * The application module object (a.k.a. app incarnation)
  625. */
  626. struct ip_vs_app {
  627. struct list_head a_list; /* member in app list */
  628. int type; /* IP_VS_APP_TYPE_xxx */
  629. char *name; /* application module name */
  630. __u16 protocol;
  631. struct module *module; /* THIS_MODULE/NULL */
  632. struct list_head incs_list; /* list of incarnations */
  633. /* members for application incarnations */
  634. struct list_head p_list; /* member in proto app list */
  635. struct ip_vs_app *app; /* its real application */
  636. __be16 port; /* port number in net order */
  637. atomic_t usecnt; /* usage counter */
  638. /*
  639. * output hook: Process packet in inout direction, diff set for TCP.
  640. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  641. * 2=Mangled but checksum was not updated
  642. */
  643. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  644. struct sk_buff *, int *diff);
  645. /*
  646. * input hook: Process packet in outin direction, diff set for TCP.
  647. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  648. * 2=Mangled but checksum was not updated
  649. */
  650. int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
  651. struct sk_buff *, int *diff);
  652. /* ip_vs_app initializer */
  653. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  654. /* ip_vs_app finish */
  655. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  656. /* not used now */
  657. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  658. struct ip_vs_protocol *);
  659. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  660. int * timeout_table;
  661. int * timeouts;
  662. int timeouts_size;
  663. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  664. int *verdict, struct ip_vs_conn **cpp);
  665. struct ip_vs_conn *
  666. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  667. const struct iphdr *iph, unsigned int proto_off,
  668. int inverse);
  669. struct ip_vs_conn *
  670. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  671. const struct iphdr *iph, unsigned int proto_off,
  672. int inverse);
  673. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  674. const struct sk_buff *skb,
  675. struct ip_vs_app *app);
  676. void (*timeout_change)(struct ip_vs_app *app, int flags);
  677. };
  678. /* IPVS in network namespace */
  679. struct netns_ipvs {
  680. int gen; /* Generation */
  681. int enable; /* enable like nf_hooks do */
  682. /*
  683. * Hash table: for real service lookups
  684. */
  685. #define IP_VS_RTAB_BITS 4
  686. #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
  687. #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
  688. struct list_head rs_table[IP_VS_RTAB_SIZE];
  689. /* ip_vs_app */
  690. struct list_head app_list;
  691. /* ip_vs_ftp */
  692. struct ip_vs_app *ftp_app;
  693. /* ip_vs_proto */
  694. #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
  695. struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE];
  696. /* ip_vs_proto_tcp */
  697. #ifdef CONFIG_IP_VS_PROTO_TCP
  698. #define TCP_APP_TAB_BITS 4
  699. #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
  700. #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
  701. struct list_head tcp_apps[TCP_APP_TAB_SIZE];
  702. spinlock_t tcp_app_lock;
  703. #endif
  704. /* ip_vs_proto_udp */
  705. #ifdef CONFIG_IP_VS_PROTO_UDP
  706. #define UDP_APP_TAB_BITS 4
  707. #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
  708. #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
  709. struct list_head udp_apps[UDP_APP_TAB_SIZE];
  710. spinlock_t udp_app_lock;
  711. #endif
  712. /* ip_vs_proto_sctp */
  713. #ifdef CONFIG_IP_VS_PROTO_SCTP
  714. #define SCTP_APP_TAB_BITS 4
  715. #define SCTP_APP_TAB_SIZE (1 << SCTP_APP_TAB_BITS)
  716. #define SCTP_APP_TAB_MASK (SCTP_APP_TAB_SIZE - 1)
  717. /* Hash table for SCTP application incarnations */
  718. struct list_head sctp_apps[SCTP_APP_TAB_SIZE];
  719. spinlock_t sctp_app_lock;
  720. #endif
  721. /* ip_vs_conn */
  722. atomic_t conn_count; /* connection counter */
  723. /* ip_vs_ctl */
  724. struct ip_vs_stats tot_stats; /* Statistics & est. */
  725. int num_services; /* no of virtual services */
  726. rwlock_t rs_lock; /* real services table */
  727. /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
  728. struct lock_class_key ctl_key; /* ctl_mutex debuging */
  729. /* Trash for destinations */
  730. struct list_head dest_trash;
  731. /* Service counters */
  732. atomic_t ftpsvc_counter;
  733. atomic_t nullsvc_counter;
  734. #ifdef CONFIG_SYSCTL
  735. /* 1/rate drop and drop-entry variables */
  736. struct delayed_work defense_work; /* Work handler */
  737. int drop_rate;
  738. int drop_counter;
  739. atomic_t dropentry;
  740. /* locks in ctl.c */
  741. spinlock_t dropentry_lock; /* drop entry handling */
  742. spinlock_t droppacket_lock; /* drop packet handling */
  743. spinlock_t securetcp_lock; /* state and timeout tables */
  744. /* sys-ctl struct */
  745. struct ctl_table_header *sysctl_hdr;
  746. struct ctl_table *sysctl_tbl;
  747. #endif
  748. /* sysctl variables */
  749. int sysctl_amemthresh;
  750. int sysctl_am_droprate;
  751. int sysctl_drop_entry;
  752. int sysctl_drop_packet;
  753. int sysctl_secure_tcp;
  754. #ifdef CONFIG_IP_VS_NFCT
  755. int sysctl_conntrack;
  756. #endif
  757. int sysctl_snat_reroute;
  758. int sysctl_sync_ver;
  759. int sysctl_cache_bypass;
  760. int sysctl_expire_nodest_conn;
  761. int sysctl_expire_quiescent_template;
  762. int sysctl_sync_threshold[2];
  763. int sysctl_nat_icmp_send;
  764. /* ip_vs_lblc */
  765. int sysctl_lblc_expiration;
  766. struct ctl_table_header *lblc_ctl_header;
  767. struct ctl_table *lblc_ctl_table;
  768. /* ip_vs_lblcr */
  769. int sysctl_lblcr_expiration;
  770. struct ctl_table_header *lblcr_ctl_header;
  771. struct ctl_table *lblcr_ctl_table;
  772. /* ip_vs_est */
  773. struct list_head est_list; /* estimator list */
  774. spinlock_t est_lock;
  775. struct timer_list est_timer; /* Estimation timer */
  776. /* ip_vs_sync */
  777. struct list_head sync_queue;
  778. spinlock_t sync_lock;
  779. struct ip_vs_sync_buff *sync_buff;
  780. spinlock_t sync_buff_lock;
  781. struct sockaddr_in sync_mcast_addr;
  782. struct task_struct *master_thread;
  783. struct task_struct *backup_thread;
  784. int send_mesg_maxlen;
  785. int recv_mesg_maxlen;
  786. volatile int sync_state;
  787. volatile int master_syncid;
  788. volatile int backup_syncid;
  789. /* multicast interface name */
  790. char master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  791. char backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  792. /* net name space ptr */
  793. struct net *net; /* Needed by timer routines */
  794. };
  795. #define DEFAULT_SYNC_THRESHOLD 3
  796. #define DEFAULT_SYNC_PERIOD 50
  797. #define DEFAULT_SYNC_VER 1
  798. #ifdef CONFIG_SYSCTL
  799. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  800. {
  801. return ipvs->sysctl_sync_threshold[0];
  802. }
  803. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  804. {
  805. return ipvs->sysctl_sync_threshold[1];
  806. }
  807. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  808. {
  809. return ipvs->sysctl_sync_ver;
  810. }
  811. #else
  812. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  813. {
  814. return DEFAULT_SYNC_THRESHOLD;
  815. }
  816. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  817. {
  818. return DEFAULT_SYNC_PERIOD;
  819. }
  820. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  821. {
  822. return DEFAULT_SYNC_VER;
  823. }
  824. #endif
  825. /*
  826. * IPVS core functions
  827. * (from ip_vs_core.c)
  828. */
  829. extern const char *ip_vs_proto_name(unsigned proto);
  830. extern void ip_vs_init_hash_table(struct list_head *table, int rows);
  831. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
  832. #define IP_VS_APP_TYPE_FTP 1
  833. /*
  834. * ip_vs_conn handling functions
  835. * (from ip_vs_conn.c)
  836. */
  837. enum {
  838. IP_VS_DIR_INPUT = 0,
  839. IP_VS_DIR_OUTPUT,
  840. IP_VS_DIR_INPUT_ONLY,
  841. IP_VS_DIR_LAST,
  842. };
  843. static inline void ip_vs_conn_fill_param(struct net *net, int af, int protocol,
  844. const union nf_inet_addr *caddr,
  845. __be16 cport,
  846. const union nf_inet_addr *vaddr,
  847. __be16 vport,
  848. struct ip_vs_conn_param *p)
  849. {
  850. p->net = net;
  851. p->af = af;
  852. p->protocol = protocol;
  853. p->caddr = caddr;
  854. p->cport = cport;
  855. p->vaddr = vaddr;
  856. p->vport = vport;
  857. p->pe = NULL;
  858. p->pe_data = NULL;
  859. }
  860. struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
  861. struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
  862. struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
  863. const struct ip_vs_iphdr *iph,
  864. unsigned int proto_off,
  865. int inverse);
  866. struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
  867. struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
  868. const struct ip_vs_iphdr *iph,
  869. unsigned int proto_off,
  870. int inverse);
  871. /* put back the conn without restarting its timer */
  872. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  873. {
  874. atomic_dec(&cp->refcnt);
  875. }
  876. extern void ip_vs_conn_put(struct ip_vs_conn *cp);
  877. extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  878. struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p,
  879. const union nf_inet_addr *daddr,
  880. __be16 dport, unsigned flags,
  881. struct ip_vs_dest *dest, __u32 fwmark);
  882. extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  883. extern const char * ip_vs_state_name(__u16 proto, int state);
  884. extern void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp);
  885. extern int ip_vs_check_template(struct ip_vs_conn *ct);
  886. extern void ip_vs_random_dropentry(struct net *net);
  887. extern int ip_vs_conn_init(void);
  888. extern void ip_vs_conn_cleanup(void);
  889. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  890. {
  891. struct ip_vs_conn *ctl_cp = cp->control;
  892. if (!ctl_cp) {
  893. IP_VS_ERR_BUF("request control DEL for uncontrolled: "
  894. "%s:%d to %s:%d\n",
  895. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  896. ntohs(cp->cport),
  897. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  898. ntohs(cp->vport));
  899. return;
  900. }
  901. IP_VS_DBG_BUF(7, "DELeting control for: "
  902. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  903. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  904. ntohs(cp->cport),
  905. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  906. ntohs(ctl_cp->cport));
  907. cp->control = NULL;
  908. if (atomic_read(&ctl_cp->n_control) == 0) {
  909. IP_VS_ERR_BUF("BUG control DEL with n=0 : "
  910. "%s:%d to %s:%d\n",
  911. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  912. ntohs(cp->cport),
  913. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  914. ntohs(cp->vport));
  915. return;
  916. }
  917. atomic_dec(&ctl_cp->n_control);
  918. }
  919. static inline void
  920. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  921. {
  922. if (cp->control) {
  923. IP_VS_ERR_BUF("request control ADD for already controlled: "
  924. "%s:%d to %s:%d\n",
  925. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  926. ntohs(cp->cport),
  927. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  928. ntohs(cp->vport));
  929. ip_vs_control_del(cp);
  930. }
  931. IP_VS_DBG_BUF(7, "ADDing control for: "
  932. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  933. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  934. ntohs(cp->cport),
  935. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  936. ntohs(ctl_cp->cport));
  937. cp->control = ctl_cp;
  938. atomic_inc(&ctl_cp->n_control);
  939. }
  940. /*
  941. * IPVS netns init & cleanup functions
  942. */
  943. extern int __ip_vs_estimator_init(struct net *net);
  944. extern int __ip_vs_control_init(struct net *net);
  945. extern int __ip_vs_protocol_init(struct net *net);
  946. extern int __ip_vs_app_init(struct net *net);
  947. extern int __ip_vs_conn_init(struct net *net);
  948. extern int __ip_vs_sync_init(struct net *net);
  949. extern void __ip_vs_conn_cleanup(struct net *net);
  950. extern void __ip_vs_app_cleanup(struct net *net);
  951. extern void __ip_vs_protocol_cleanup(struct net *net);
  952. extern void __ip_vs_control_cleanup(struct net *net);
  953. extern void __ip_vs_estimator_cleanup(struct net *net);
  954. extern void __ip_vs_sync_cleanup(struct net *net);
  955. extern void __ip_vs_service_cleanup(struct net *net);
  956. /*
  957. * IPVS application functions
  958. * (from ip_vs_app.c)
  959. */
  960. #define IP_VS_APP_MAX_PORTS 8
  961. extern int register_ip_vs_app(struct net *net, struct ip_vs_app *app);
  962. extern void unregister_ip_vs_app(struct net *net, struct ip_vs_app *app);
  963. extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  964. extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
  965. extern int register_ip_vs_app_inc(struct net *net, struct ip_vs_app *app,
  966. __u16 proto, __u16 port);
  967. extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
  968. extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
  969. extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
  970. extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
  971. extern int ip_vs_app_init(void);
  972. extern void ip_vs_app_cleanup(void);
  973. void ip_vs_bind_pe(struct ip_vs_service *svc, struct ip_vs_pe *pe);
  974. void ip_vs_unbind_pe(struct ip_vs_service *svc);
  975. int register_ip_vs_pe(struct ip_vs_pe *pe);
  976. int unregister_ip_vs_pe(struct ip_vs_pe *pe);
  977. struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
  978. struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
  979. static inline void ip_vs_pe_get(const struct ip_vs_pe *pe)
  980. {
  981. if (pe && pe->module)
  982. __module_get(pe->module);
  983. }
  984. static inline void ip_vs_pe_put(const struct ip_vs_pe *pe)
  985. {
  986. if (pe && pe->module)
  987. module_put(pe->module);
  988. }
  989. /*
  990. * IPVS protocol functions (from ip_vs_proto.c)
  991. */
  992. extern int ip_vs_protocol_init(void);
  993. extern void ip_vs_protocol_cleanup(void);
  994. extern void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
  995. extern int *ip_vs_create_timeout_table(int *table, int size);
  996. extern int
  997. ip_vs_set_state_timeout(int *table, int num, const char *const *names,
  998. const char *name, int to);
  999. extern void
  1000. ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
  1001. const struct sk_buff *skb,
  1002. int offset, const char *msg);
  1003. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  1004. extern struct ip_vs_protocol ip_vs_protocol_udp;
  1005. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  1006. extern struct ip_vs_protocol ip_vs_protocol_esp;
  1007. extern struct ip_vs_protocol ip_vs_protocol_ah;
  1008. extern struct ip_vs_protocol ip_vs_protocol_sctp;
  1009. /*
  1010. * Registering/unregistering scheduler functions
  1011. * (from ip_vs_sched.c)
  1012. */
  1013. extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1014. extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1015. extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  1016. struct ip_vs_scheduler *scheduler);
  1017. extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
  1018. extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  1019. extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  1020. extern struct ip_vs_conn *
  1021. ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
  1022. struct ip_vs_proto_data *pd, int *ignored);
  1023. extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  1024. struct ip_vs_proto_data *pd);
  1025. extern void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
  1026. /*
  1027. * IPVS control data and functions (from ip_vs_ctl.c)
  1028. */
  1029. extern struct ip_vs_stats ip_vs_stats;
  1030. extern const struct ctl_path net_vs_ctl_path[];
  1031. extern int sysctl_ip_vs_sync_ver;
  1032. extern void ip_vs_sync_switch_mode(struct net *net, int mode);
  1033. extern struct ip_vs_service *
  1034. ip_vs_service_get(struct net *net, int af, __u32 fwmark, __u16 protocol,
  1035. const union nf_inet_addr *vaddr, __be16 vport);
  1036. static inline void ip_vs_service_put(struct ip_vs_service *svc)
  1037. {
  1038. atomic_dec(&svc->usecnt);
  1039. }
  1040. extern struct ip_vs_dest *
  1041. ip_vs_lookup_real_service(struct net *net, int af, __u16 protocol,
  1042. const union nf_inet_addr *daddr, __be16 dport);
  1043. extern int ip_vs_use_count_inc(void);
  1044. extern void ip_vs_use_count_dec(void);
  1045. extern int ip_vs_control_init(void);
  1046. extern void ip_vs_control_cleanup(void);
  1047. extern struct ip_vs_dest *
  1048. ip_vs_find_dest(struct net *net, int af, const union nf_inet_addr *daddr,
  1049. __be16 dport, const union nf_inet_addr *vaddr, __be16 vport,
  1050. __u16 protocol, __u32 fwmark);
  1051. extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  1052. /*
  1053. * IPVS sync daemon data and function prototypes
  1054. * (from ip_vs_sync.c)
  1055. */
  1056. extern int start_sync_thread(struct net *net, int state, char *mcast_ifn,
  1057. __u8 syncid);
  1058. extern int stop_sync_thread(struct net *net, int state);
  1059. extern void ip_vs_sync_conn(struct net *net, struct ip_vs_conn *cp);
  1060. extern int ip_vs_sync_init(void);
  1061. extern void ip_vs_sync_cleanup(void);
  1062. /*
  1063. * IPVS rate estimator prototypes (from ip_vs_est.c)
  1064. */
  1065. extern int ip_vs_estimator_init(void);
  1066. extern void ip_vs_estimator_cleanup(void);
  1067. extern void ip_vs_start_estimator(struct net *net, struct ip_vs_stats *stats);
  1068. extern void ip_vs_stop_estimator(struct net *net, struct ip_vs_stats *stats);
  1069. extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  1070. extern void ip_vs_read_estimator(struct ip_vs_stats_user *dst,
  1071. struct ip_vs_stats *stats);
  1072. /*
  1073. * Various IPVS packet transmitters (from ip_vs_xmit.c)
  1074. */
  1075. extern int ip_vs_null_xmit
  1076. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1077. extern int ip_vs_bypass_xmit
  1078. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1079. extern int ip_vs_nat_xmit
  1080. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1081. extern int ip_vs_tunnel_xmit
  1082. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1083. extern int ip_vs_dr_xmit
  1084. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1085. extern int ip_vs_icmp_xmit
  1086. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
  1087. int offset, unsigned int hooknum);
  1088. extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
  1089. #ifdef CONFIG_IP_VS_IPV6
  1090. extern int ip_vs_bypass_xmit_v6
  1091. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1092. extern int ip_vs_nat_xmit_v6
  1093. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1094. extern int ip_vs_tunnel_xmit_v6
  1095. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1096. extern int ip_vs_dr_xmit_v6
  1097. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1098. extern int ip_vs_icmp_xmit_v6
  1099. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
  1100. int offset, unsigned int hooknum);
  1101. #endif
  1102. #ifdef CONFIG_SYSCTL
  1103. /*
  1104. * This is a simple mechanism to ignore packets when
  1105. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  1106. * we start to drop 1/rate of the packets
  1107. */
  1108. static inline int ip_vs_todrop(struct netns_ipvs *ipvs)
  1109. {
  1110. if (!ipvs->drop_rate)
  1111. return 0;
  1112. if (--ipvs->drop_counter > 0)
  1113. return 0;
  1114. ipvs->drop_counter = ipvs->drop_rate;
  1115. return 1;
  1116. }
  1117. #else
  1118. static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; }
  1119. #endif
  1120. /*
  1121. * ip_vs_fwd_tag returns the forwarding tag of the connection
  1122. */
  1123. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  1124. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  1125. {
  1126. char fwd;
  1127. switch (IP_VS_FWD_METHOD(cp)) {
  1128. case IP_VS_CONN_F_MASQ:
  1129. fwd = 'M'; break;
  1130. case IP_VS_CONN_F_LOCALNODE:
  1131. fwd = 'L'; break;
  1132. case IP_VS_CONN_F_TUNNEL:
  1133. fwd = 'T'; break;
  1134. case IP_VS_CONN_F_DROUTE:
  1135. fwd = 'R'; break;
  1136. case IP_VS_CONN_F_BYPASS:
  1137. fwd = 'B'; break;
  1138. default:
  1139. fwd = '?'; break;
  1140. }
  1141. return fwd;
  1142. }
  1143. extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1144. struct ip_vs_conn *cp, int dir);
  1145. #ifdef CONFIG_IP_VS_IPV6
  1146. extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1147. struct ip_vs_conn *cp, int dir);
  1148. #endif
  1149. extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  1150. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  1151. {
  1152. __be32 diff[2] = { ~old, new };
  1153. return csum_partial(diff, sizeof(diff), oldsum);
  1154. }
  1155. #ifdef CONFIG_IP_VS_IPV6
  1156. static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
  1157. __wsum oldsum)
  1158. {
  1159. __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
  1160. new[3], new[2], new[1], new[0] };
  1161. return csum_partial(diff, sizeof(diff), oldsum);
  1162. }
  1163. #endif
  1164. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  1165. {
  1166. __be16 diff[2] = { ~old, new };
  1167. return csum_partial(diff, sizeof(diff), oldsum);
  1168. }
  1169. /*
  1170. * Forget current conntrack (unconfirmed) and attach notrack entry
  1171. */
  1172. static inline void ip_vs_notrack(struct sk_buff *skb)
  1173. {
  1174. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1175. enum ip_conntrack_info ctinfo;
  1176. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  1177. if (!ct || !nf_ct_is_untracked(ct)) {
  1178. nf_reset(skb);
  1179. skb->nfct = &nf_ct_untracked_get()->ct_general;
  1180. skb->nfctinfo = IP_CT_NEW;
  1181. nf_conntrack_get(skb->nfct);
  1182. }
  1183. #endif
  1184. }
  1185. #ifdef CONFIG_IP_VS_NFCT
  1186. /*
  1187. * Netfilter connection tracking
  1188. * (from ip_vs_nfct.c)
  1189. */
  1190. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1191. {
  1192. #ifdef CONFIG_SYSCTL
  1193. return ipvs->sysctl_conntrack;
  1194. #else
  1195. return 0;
  1196. #endif
  1197. }
  1198. extern void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
  1199. int outin);
  1200. extern int ip_vs_confirm_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp);
  1201. extern void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
  1202. struct ip_vs_conn *cp, u_int8_t proto,
  1203. const __be16 port, int from_rs);
  1204. extern void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
  1205. #else
  1206. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1207. {
  1208. return 0;
  1209. }
  1210. static inline void ip_vs_update_conntrack(struct sk_buff *skb,
  1211. struct ip_vs_conn *cp, int outin)
  1212. {
  1213. }
  1214. static inline int ip_vs_confirm_conntrack(struct sk_buff *skb,
  1215. struct ip_vs_conn *cp)
  1216. {
  1217. return NF_ACCEPT;
  1218. }
  1219. static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
  1220. {
  1221. }
  1222. /* CONFIG_IP_VS_NFCT */
  1223. #endif
  1224. static inline unsigned int
  1225. ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
  1226. {
  1227. /*
  1228. * We think the overhead of processing active connections is 256
  1229. * times higher than that of inactive connections in average. (This
  1230. * 256 times might not be accurate, we will change it later) We
  1231. * use the following formula to estimate the overhead now:
  1232. * dest->activeconns*256 + dest->inactconns
  1233. */
  1234. return (atomic_read(&dest->activeconns) << 8) +
  1235. atomic_read(&dest->inactconns);
  1236. }
  1237. #endif /* _NET_IP_VS_H */