ip_vs.h 47 KB

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