ip_vs.h 42 KB

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