9eb8d9153909b4c1821f59a89e55beed7e91f07f
[vpp.git] / src / plugins / nat / nat64 / nat64.h
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __included_nat64_h__
16 #define __included_nat64_h__
17
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ip/icmp46_packet.h>
22 #include <vnet/api_errno.h>
23 #include <vnet/fib/fib_source.h>
24 #include <vppinfra/dlist.h>
25 #include <vppinfra/error.h>
26 #include <vlibapi/api.h>
27 #include <vlib/log.h>
28 #include <vnet/fib/fib_table.h>
29 #include <vnet/fib/ip4_fib.h>
30 #include <vnet/ip/reass/ip4_sv_reass.h>
31
32 #include <nat/lib/lib.h>
33 #include <nat/lib/inlines.h>
34 #include <nat/lib/nat_inlines.h>
35
36 #include <nat/nat64/nat64_db.h>
37
38 typedef struct
39 {
40   u16 identifier;
41   u16 sequence;
42 } icmp_echo_header_t;
43
44 typedef struct
45 {
46   u16 src_port, dst_port;
47 } tcp_udp_header_t;
48
49 #define foreach_nat64_tcp_ses_state            \
50   _(0, CLOSED, "closed")                       \
51   _(1, V4_INIT, "v4-init")                     \
52   _(2, V6_INIT, "v6-init")                     \
53   _(3, ESTABLISHED, "established")             \
54   _(4, V4_FIN_RCV, "v4-fin-rcv")               \
55   _(5, V6_FIN_RCV, "v6-fin-rcv")               \
56   _(6, V6_FIN_V4_FIN_RCV, "v6-fin-v4-fin-rcv") \
57   _(7, TRANS, "trans")
58
59 typedef enum
60 {
61 #define _(v, N, s) NAT64_TCP_STATE_##N = v,
62   foreach_nat64_tcp_ses_state
63 #undef _
64 } nat64_tcp_ses_state_t;
65
66 typedef enum
67 {
68   NAT64_CLEANER_RESCHEDULE = 1,
69 } nat64_cleaner_process_event_e;
70
71 typedef struct
72 {
73   ip6_address_t prefix;
74   u8 plen;
75   u32 vrf_id;
76   u32 fib_index;
77 } nat64_prefix_t;
78
79 typedef struct
80 {
81   ip6_address_t in_addr;
82   u16 in_port;
83   ip4_address_t out_addr;
84   u16 out_port;
85   u32 fib_index;
86   u32 thread_index;
87   u8 proto;
88   u8 is_add;
89   u8 done;
90 } nat64_static_bib_to_update_t;
91
92 typedef struct
93 {
94   ip4_address_t addr;
95   u32 fib_index;
96 #define _(N, i, n, s) \
97   u16 busy_##n##_ports; \
98   u16 * busy_##n##_ports_per_thread; \
99   u32 busy_##n##_port_refcounts[65535];
100   foreach_nat_protocol
101 #undef _
102 } nat64_address_t;
103
104 typedef struct
105 {
106   u32 sw_if_index;
107   u8 flags;
108 } nat64_interface_t;
109
110 typedef struct
111 {
112   u32 enabled;
113
114   nat64_config_t config;
115
116   /* API message ID base */
117   u16 msg_id_base;
118
119   /* log class */
120   vlib_log_class_t log_class;
121
122   /** Interface pool */
123   nat64_interface_t *interfaces;
124
125   /** Address pool vector */
126   nat64_address_t *addr_pool;
127
128   /** sw_if_indices whose interface addresses should be auto-added */
129   u32 *auto_add_sw_if_indices;
130
131   /** Pref64 vector */
132   nat64_prefix_t *pref64;
133
134   /** BIB and session DB per thread */
135   nat64_db_t *db;
136
137   /** Worker handoff */
138   u32 fq_in2out_index;
139   u32 fq_out2in_index;
140
141   /** Pool of static BIB entries to be added/deleted in worker threads */
142   nat64_static_bib_to_update_t *static_bibs;
143
144   /** config parameters */
145   u32 bib_buckets;
146   uword bib_memory_size;
147   u32 st_buckets;
148   uword st_memory_size;
149
150   /** values of various timeouts */
151   u32 udp_timeout;
152   u32 icmp_timeout;
153   u32 tcp_trans_timeout;
154   u32 tcp_est_timeout;
155
156   /* Total count of interfaces enabled */
157   u32 total_enabled_count;
158
159   /* Expire walk process node index */
160   u32 expire_walk_node_index;
161
162   /* Expire worker walk process node index */
163   u32 expire_worker_walk_node_index;
164
165   /* counters/gauges */
166   vlib_simple_counter_main_t total_bibs;
167   vlib_simple_counter_main_t total_sessions;
168
169   /** node index **/
170   u32 error_node_index;
171
172   u32 in2out_node_index;
173   u32 in2out_slowpath_node_index;
174
175   u32 out2in_node_index;
176
177 #define _(x) vlib_simple_counter_main_t x;
178   struct
179   {
180     struct
181     {
182       foreach_nat_counter;
183     } in2out;
184
185     struct
186     {
187       foreach_nat_counter;
188     } out2in;
189   } counters;
190 #undef _
191
192   /* convenience */
193   ip4_main_t *ip4_main;
194
195   /* required */
196   vnet_main_t *vnet_main;
197
198   /* Randomize port allocation order */
199   u32 random_seed;
200
201   /* TCP MSS clamping */
202   u16 mss_clamping;
203
204   fib_source_t fib_src_hi;
205   fib_source_t fib_src_low;
206
207   /* Thread settings */
208   u32 num_workers;
209   u32 first_worker_index;
210   u32 *workers;
211   u16 port_per_thread;
212
213 } nat64_main_t;
214
215 extern nat64_main_t nat64_main;
216 extern vlib_node_registration_t nat64_in2out_node;
217 extern vlib_node_registration_t nat64_out2in_node;
218
219 /**
220  * @brief Add/delete address to NAT64 pool.
221  *
222  * @param thread_index Thread index used by ipfix nat logging (not address per thread).
223  * @param addr   IPv4 address.
224  * @param vrf_id VRF id of tenant, ~0 means independent of VRF.
225  * @param is_add 1 if add, 0 if delete.
226  *
227  * @returns 0 on success, non-zero value otherwise.
228  */
229 int nat64_add_del_pool_addr (u32 thread_index,
230                              ip4_address_t * addr, u32 vrf_id, u8 is_add);
231
232 /**
233  * @brief Call back function when walking addresses in NAT64 pool, non-zero
234  * return value stop walk.
235  */
236 typedef int (*nat64_pool_addr_walk_fn_t) (nat64_address_t * addr, void *ctx);
237
238 /**
239  * @brief Walk NAT64 pool.
240  *
241  * @param fn The function to invoke on each entry visited.
242  * @param ctx A context passed in the visit function.
243  */
244 void nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx);
245
246 /**
247  * @brief NAT64 pool address from specific (DHCP addressed) interface.
248  *
249  * @param sw_if_index Index of the interface.
250  * @param is_add      1 if add, 0 if delete.
251  *
252  * @returns 0 on success, non-zero value otherwise.
253  */
254 int nat64_add_interface_address (u32 sw_if_index, int is_add);
255
256 /**
257  * @brief Enable/disable NAT64 feature on the interface.
258  *
259  * @param sw_if_index Index of the interface.
260  * @param is_inside   1 if inside, 0 if outside.
261  * @param is_add      1 if add, 0 if delete.
262  *
263  * @returns 0 on success, non-zero value otherwise.
264  */
265 int nat64_interface_add_del (u32 sw_if_index, u8 is_inside, u8 is_add);
266
267 /**
268  * @brief Call back function when walking interfaces with NAT64 feature,
269  * non-zero return value stop walk.
270  */
271 typedef int (*nat64_interface_walk_fn_t) (nat64_interface_t * i, void *ctx);
272
273 /**
274  * @brief Walk NAT64 interfaces.
275  *
276  * @param fn The function to invoke on each entry visited.
277  * @param ctx A context passed in the visit function.
278  */
279 void nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx);
280
281 /**
282  * @brief Initialize NAT64.
283  *
284  * @param vm vlib main.
285  *
286  * @return error code.
287  */
288 clib_error_t *nat64_init (vlib_main_t * vm);
289
290 /**
291  * @brief Add/delete static NAT64 BIB entry.
292  *
293  * @param in_addr  Inside IPv6 address.
294  * @param out_addr Outside IPv4 address.
295  * @param in_port  Inside port number.
296  * @param out_port Outside port number.
297  * @param proto    L4 protocol.
298  * @param vrf_id   VRF id of tenant.
299  * @param is_add   1 if add, 0 if delete.
300  *
301  * @returns 0 on success, non-zero value otherwise.
302  */
303 int nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
304                                     ip4_address_t * out_addr, u16 in_port,
305                                     u16 out_port, u8 proto, u32 vrf_id,
306                                     u8 is_add);
307
308 /**
309  * @brief Alloce IPv4 address and port pair from NAT64 pool.
310  *
311  * @param fib_index    FIB index of tenant.
312  * @param proto        L4 protocol.
313  * @param addr         Allocated IPv4 address.
314  * @param port         Allocated port number.
315  * @param thread_index Thread index.
316  *
317  * @returns 0 on success, non-zero value otherwise.
318  */
319 int nat64_alloc_out_addr_and_port (u32 fib_index, nat_protocol_t proto,
320                                    ip4_address_t * addr, u16 * port,
321                                    u32 thread_index);
322
323 /**
324  * @brief Set UDP session timeout.
325  *
326  * @param timeout Timeout value in seconds (if 0 reset to default value 300sec).
327  *
328  * @returns 0 on success, non-zero value otherwise.
329  */
330 int nat64_set_udp_timeout (u32 timeout);
331
332 /**
333  * @brief Get UDP session timeout.
334  *
335  * @returns UDP session timeout in seconds.
336  */
337 u32 nat64_get_udp_timeout (void);
338
339 /**
340  * @brief Set ICMP session timeout.
341  *
342  * @param timeout Timeout value in seconds (if 0 reset to default value 60sec).
343  *
344  * @returns 0 on success, non-zero value otherwise.
345  */
346 int nat64_set_icmp_timeout (u32 timeout);
347
348 /**
349  * @brief Get ICMP session timeout.
350  *
351  * @returns ICMP session timeout in seconds.
352  */
353 u32 nat64_get_icmp_timeout (void);
354
355 /**
356  * @brief Set TCP session timeouts.
357  *
358  * @param trans Transitory timeout in seconds (if 0 reset to default value 240sec).
359  * @param est Established timeout in seconds (if 0 reset to default value 7440sec).
360  *
361  * @returns 0 on success, non-zero value otherwise.
362  */
363 int nat64_set_tcp_timeouts (u32 trans, u32 est);
364
365 /**
366  * @brief Get TCP transitory timeout.
367  *
368  * @returns TCP transitory timeout in seconds.
369  */
370 u32 nat64_get_tcp_trans_timeout (void);
371
372 /**
373  * @brief Get TCP established timeout.
374  *
375  * @returns TCP established timeout in seconds.
376  */
377 u32 nat64_get_tcp_est_timeout (void);
378
379 /**
380  * @brief Reset NAT64 session timeout.
381  *
382  * @param ste Session table entry.
383  * @param vm VLIB main.
384  **/
385 void nat64_session_reset_timeout (nat64_db_st_entry_t * ste,
386                                   vlib_main_t * vm);
387
388 /**
389  * @brief Set NAT64 TCP session state.
390  *
391  * @param ste Session table entry.
392  * @param tcp TCP header.
393  * @param is_ip6 1 if IPv6 packet, 0 if IPv4.
394  */
395 void nat64_tcp_session_set_state (nat64_db_st_entry_t * ste,
396                                   tcp_header_t * tcp, u8 is_ip6);
397
398 /**
399  * @brief Add/delete NAT64 prefix.
400  *
401  * @param prefix NAT64 prefix.
402  * @param plen Prefix length.
403  * @param vrf_id VRF id of tenant.
404  * @param is_add 1 if add, 0 if delete.
405  *
406  * @returns 0 on success, non-zero value otherwise.
407  */
408 int nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id,
409                           u8 is_add);
410
411 /**
412  * @brief Call back function when walking addresses in NAT64 prefixes, non-zero
413  * return value stop walk.
414  */
415 typedef int (*nat64_prefix_walk_fn_t) (nat64_prefix_t * pref64, void *ctx);
416
417 /**
418  * @brief Walk NAT64 prefixes.
419  *
420  * @param fn The function to invoke on each entry visited.
421  * @param ctx A context passed in the visit function.
422  */
423 void nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx);
424
425 /**
426  * Compose IPv4-embedded IPv6 addresses.
427  * @param ip6 IPv4-embedded IPv6 addresses.
428  * @param ip4 IPv4 address.
429  * @param fib_index Tenant FIB index.
430  */
431 void nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4,
432                         u32 fib_index);
433
434 /**
435  * Extract IPv4 address from the IPv4-embedded IPv6 addresses.
436  *
437  * @param ip6 IPv4-embedded IPv6 addresses.
438  * @param ip4 IPv4 address.
439  * @param fib_index Tenant FIB index.
440  */
441 void nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4,
442                         u32 fib_index);
443
444 /**
445  * @brief Set NAT64 hash tables configuration.
446  *
447  * @param bib_buckets Number of BIB hash buckets.
448  * @param bib_memory_size Memory size of BIB hash.
449  * @param st_buckets Number of session table hash buckets.
450  * @param st_memory_size Memory size of session table hash.
451  */
452 void nat64_set_hash (u32 bib_buckets, uword bib_memory_size, u32 st_buckets,
453                      uword st_memory_size);
454
455 /**
456  * @brief Get worker thread index for NAT64 in2out.
457  *
458  * @param addr IPv6 src address.
459  *
460  * @returns worker thread index.
461  */
462 u32 nat64_get_worker_in2out (ip6_address_t * addr);
463
464 /**
465  * @brief Get worker thread index for NAT64 out2in.
466  *
467  * @param ip IPv4 header.
468  *
469  * @returns worker thread index.
470  */
471 u32 nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip);
472
473 /* NAT64 interface flags */
474 #define NAT64_INTERFACE_FLAG_IS_INSIDE 1
475 #define NAT64_INTERFACE_FLAG_IS_OUTSIDE 2
476
477 /** \brief Check if NAT64 interface is inside.
478     @param i NAT64 interface
479     @return 1 if inside interface
480 */
481 #define nat64_interface_is_inside(i) i->flags & NAT64_INTERFACE_FLAG_IS_INSIDE
482
483 /** \brief Check if NAT64 interface is outside.
484     @param i NAT64 interface
485     @return 1 if outside interface
486 */
487 #define nat64_interface_is_outside(i) i->flags & NAT64_INTERFACE_FLAG_IS_OUTSIDE
488
489 static_always_inline u8
490 plugin_enabled ()
491 {
492   nat64_main_t *nm = &nat64_main;
493   return nm->enabled;
494 }
495
496 void
497 nat64_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
498                            int is_add);
499
500 int nat64_plugin_enable (nat64_config_t c);
501 int nat64_plugin_disable ();
502 void nat64_reset_timeouts ();
503
504 format_function_t format_nat_protocol;
505 unformat_function_t unformat_nat_protocol;
506
507 /* logging */
508 #define nat64_log_err(...) \
509   vlib_log(VLIB_LOG_LEVEL_ERR, nat64_main.log_class, __VA_ARGS__)
510 #define nat64_log_warn(...) \
511   vlib_log(VLIB_LOG_LEVEL_WARNING, nat64_main.log_class, __VA_ARGS__)
512 #define nat64_log_notice(...) \
513   vlib_log(VLIB_LOG_LEVEL_NOTICE, nat64_main.log_class, __VA_ARGS__)
514 #define nat64_log_info(...) \
515   vlib_log(VLIB_LOG_LEVEL_INFO, nat64_main.log_class, __VA_ARGS__)
516 #define nat64_log_debug(...)\
517   vlib_log(VLIB_LOG_LEVEL_DEBUG, nat64_main.log_class, __VA_ARGS__)
518
519 clib_error_t *nat64_api_hookup (vlib_main_t * vm);
520
521 #endif /* __included_nat64_h__ */
522
523 /*
524  * fd.io coding-style-patch-verification: ON
525  *
526  * Local Variables:
527  * eval: (c-set-style "gnu")
528  * End:
529  */