ip_vs.h 45 KB

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