NAT44: nat44_del_session and nat44_user_session_details API update (VPP-1271)
[vpp.git] / src / plugins / nat / nat.h
1
2 /*
3  * nat.h - NAT plugin definitions
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef __included_nat_h__
19 #define __included_nat_h__
20
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ip/icmp46_packet.h>
25 #include <vnet/api_errno.h>
26 #include <vppinfra/bihash_8_8.h>
27 #include <vppinfra/bihash_16_8.h>
28 #include <vppinfra/dlist.h>
29 #include <vppinfra/error.h>
30 #include <vlibapi/api.h>
31
32
33 #define SNAT_UDP_TIMEOUT 300
34 #define SNAT_UDP_TIMEOUT_MIN 120
35 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
36 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
37 #define SNAT_TCP_INCOMING_SYN 6
38 #define SNAT_ICMP_TIMEOUT 60
39
40 #define SNAT_FLAG_HAIRPINNING (1 << 0)
41
42 /* Key */
43 typedef struct {
44   union
45   {
46     struct
47     {
48       ip4_address_t addr;
49       u16 port;
50       u16 protocol:3,
51         fib_index:13;
52     };
53     u64 as_u64;
54   };
55 } snat_session_key_t;
56
57 typedef struct {
58   union
59   {
60     struct
61     {
62       ip4_address_t l_addr;
63       ip4_address_t r_addr;
64       u32 proto:8,
65           fib_index:24;
66       u16 l_port;
67       u16 r_port;
68     };
69     u64 as_u64[2];
70   };
71 } nat_ed_ses_key_t;
72
73 typedef struct {
74   union
75   {
76     struct
77     {
78       ip4_address_t ext_host_addr;
79       u16 ext_host_port;
80       u16 out_port;
81     };
82     u64 as_u64;
83   };
84 } snat_det_out_key_t;
85
86 typedef struct {
87   union
88   {
89     struct
90     {
91       ip4_address_t addr;
92       u32 fib_index;
93     };
94     u64 as_u64;
95   };
96 } snat_user_key_t;
97
98
99 #define foreach_snat_protocol \
100   _(UDP, 0, udp, "udp")       \
101   _(TCP, 1, tcp, "tcp")       \
102   _(ICMP, 2, icmp, "icmp")
103
104 typedef enum {
105 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
106   foreach_snat_protocol
107 #undef _
108 } snat_protocol_t;
109
110
111 #define foreach_snat_session_state          \
112   _(0, UNKNOWN, "unknown")                 \
113   _(1, UDP_ACTIVE, "udp-active")           \
114   _(2, TCP_SYN_SENT, "tcp-syn-sent")       \
115   _(3, TCP_ESTABLISHED, "tcp-established") \
116   _(4, TCP_FIN_WAIT, "tcp-fin-wait")       \
117   _(5, TCP_CLOSE_WAIT, "tcp-close-wait")   \
118   _(6, TCP_CLOSING, "tcp-closing")         \
119   _(7, TCP_LAST_ACK, "tcp-last-ack")       \
120   _(8, TCP_CLOSED, "tcp-closed")           \
121   _(9, ICMP_ACTIVE, "icmp-active")
122
123 typedef enum {
124 #define _(v, N, s) SNAT_SESSION_##N = v,
125   foreach_snat_session_state
126 #undef _
127 } snat_session_state_t;
128
129 #define NAT44_SES_I2O_FIN 1
130 #define NAT44_SES_O2I_FIN 2
131 #define NAT44_SES_I2O_FIN_ACK 4
132 #define NAT44_SES_O2I_FIN_ACK 8
133
134 #define nat44_is_ses_closed(s) (s->state == 0xf)
135
136 #define SNAT_SESSION_FLAG_STATIC_MAPPING       1
137 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO        2
138 #define SNAT_SESSION_FLAG_LOAD_BALANCING       4
139 #define SNAT_SESSION_FLAG_TWICE_NAT            8
140 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT   16
141 #define SNAT_SESSION_FLAG_FWD_BYPASS           32
142
143 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
144 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
145
146 typedef CLIB_PACKED(struct {
147   snat_session_key_t out2in;    /* 0-15 */
148
149   snat_session_key_t in2out;    /* 16-31 */
150
151   u32 flags;                    /* 32-35 */
152
153   /* per-user translations */
154   u32 per_user_index;           /* 36-39 */
155
156   u32 per_user_list_head_index; /* 40-43 */
157
158   /* Last heard timer */
159   f64 last_heard;               /* 44-51 */
160
161   u64 total_bytes;              /* 52-59 */
162
163   u32 total_pkts;               /* 60-63 */
164
165   /* Outside address */
166   u32 outside_address_index;    /* 64-67 */
167
168   /* External host address and port */
169   ip4_address_t ext_host_addr;  /* 68-71 */
170   u16 ext_host_port;            /* 72-73 */
171
172   /* External hos address and port after translation */
173   ip4_address_t ext_host_nat_addr; /* 74-77 */
174   u16 ext_host_nat_port;           /* 78-79 */
175
176   /* TCP session state */
177   u8 state;
178   u32 i2o_fin_seq;
179   u32 o2i_fin_seq;
180 }) snat_session_t;
181
182
183 typedef struct {
184   ip4_address_t addr;
185   u32 fib_index;
186   u32 sessions_per_user_list_head_index;
187   u32 nsessions;
188   u32 nstaticsessions;
189 } snat_user_t;
190
191 typedef struct {
192   ip4_address_t addr;
193   u32 fib_index;
194 #define _(N, i, n, s) \
195   u16 busy_##n##_ports; \
196   u16 * busy_##n##_ports_per_thread; \
197   uword * busy_##n##_port_bitmap;
198   foreach_snat_protocol
199 #undef _
200 } snat_address_t;
201
202 typedef struct {
203   u16 in_port;
204   snat_det_out_key_t out;
205   u8 state;
206   u32 expire;
207 } snat_det_session_t;
208
209 typedef struct {
210   ip4_address_t in_addr;
211   u8 in_plen;
212   ip4_address_t out_addr;
213   u8 out_plen;
214   u32 sharing_ratio;
215   u16 ports_per_host;
216   u32 ses_num;
217   /* vector of sessions */
218   snat_det_session_t * sessions;
219 } snat_det_map_t;
220
221 typedef struct {
222   ip4_address_t addr;
223   u16 port;
224   u8 probability;
225   u8 prefix;
226 } nat44_lb_addr_port_t;
227
228 typedef enum {
229   TWICE_NAT_DISABLED,
230   TWICE_NAT,
231   TWICE_NAT_SELF,
232 } twice_nat_type_t;
233
234 typedef struct {
235   ip4_address_t local_addr;
236   ip4_address_t external_addr;
237   u16 local_port;
238   u16 external_port;
239   u8 addr_only;
240   twice_nat_type_t twice_nat;
241   u8 out2in_only;
242   u32 vrf_id;
243   u32 fib_index;
244   snat_protocol_t proto;
245   u32 worker_index;
246   u8 *tag;
247   nat44_lb_addr_port_t *locals;
248 } snat_static_mapping_t;
249
250 typedef struct {
251   u32 sw_if_index;
252   u8 flags;
253 } snat_interface_t;
254
255 typedef struct {
256   ip4_address_t l_addr;
257   u16 l_port;
258   u16 e_port;
259   u32 sw_if_index;
260   u32 vrf_id;
261   snat_protocol_t proto;
262   int addr_only;
263   int twice_nat;
264   int is_add;
265   u8 *tag;
266 } snat_static_map_resolve_t;
267
268 typedef struct {
269   /* Main lookup tables */
270   clib_bihash_8_8_t out2in;
271   clib_bihash_8_8_t in2out;
272
273   /* Find-a-user => src address lookup */
274   clib_bihash_8_8_t user_hash;
275
276   /* User pool */
277   snat_user_t * users;
278
279   /* Session pool */
280   snat_session_t * sessions;
281
282   /* Pool of doubly-linked list elements */
283   dlist_elt_t * list_pool;
284
285   u32 snat_thread_index;
286 } snat_main_per_thread_data_t;
287
288 struct snat_main_s;
289
290 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
291                                         vlib_node_runtime_t *node,
292                                         u32 thread_index,
293                                         vlib_buffer_t *b0,
294                                         ip4_header_t *ip0,
295                                         u8 *p_proto,
296                                         snat_session_key_t *p_value,
297                                         u8 *p_dont_translate,
298                                         void *d,
299                                         void *e);
300
301 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
302
303 typedef int nat_alloc_out_addr_and_port_function_t (snat_address_t * addresses,
304                                                     u32 fib_index,
305                                                     u32 thread_index,
306                                                     snat_session_key_t * k,
307                                                     u32 * address_indexp,
308                                                     u16 port_per_thread,
309                                                     u32 snat_thread_index);
310
311 typedef struct snat_main_s {
312   /* Endpoint address dependent sessions lookup tables */
313   clib_bihash_16_8_t out2in_ed;
314   clib_bihash_16_8_t in2out_ed;
315
316   snat_icmp_match_function_t * icmp_match_in2out_cb;
317   snat_icmp_match_function_t * icmp_match_out2in_cb;
318
319   u32 num_workers;
320   u32 first_worker_index;
321   u32 next_worker;
322   u32 * workers;
323   snat_get_worker_function_t * worker_in2out_cb;
324   snat_get_worker_function_t * worker_out2in_cb;
325   u16 port_per_thread;
326   u32 num_snat_thread;
327
328   /* Per thread data */
329   snat_main_per_thread_data_t * per_thread_data;
330
331   /* Find a static mapping by local */
332   clib_bihash_8_8_t static_mapping_by_local;
333
334   /* Find a static mapping by external */
335   clib_bihash_8_8_t static_mapping_by_external;
336
337   /* Static mapping pool */
338   snat_static_mapping_t * static_mappings;
339
340   /* Interface pool */
341   snat_interface_t * interfaces;
342   snat_interface_t * output_feature_interfaces;
343
344   /* Vector of outside addresses */
345   snat_address_t * addresses;
346   nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
347   u8 psid_offset;
348   u8 psid_length;
349   u16 psid;
350
351   /* Vector of twice NAT addresses for extenal hosts */
352   snat_address_t * twice_nat_addresses;
353
354   /* sw_if_indices whose intfc addresses should be auto-added */
355   u32 * auto_add_sw_if_indices;
356   u32 * auto_add_sw_if_indices_twice_nat;
357
358   /* vector of interface address static mappings to resolve. */
359   snat_static_map_resolve_t *to_resolve;
360
361   /* Randomize port allocation order */
362   u32 random_seed;
363
364   /* Worker handoff index */
365   u32 fq_in2out_index;
366   u32 fq_in2out_output_index;
367   u32 fq_out2in_index;
368
369   /* in2out and out2in node index */
370   u32 in2out_node_index;
371   u32 in2out_output_node_index;
372   u32 out2in_node_index;
373
374   /* Deterministic NAT */
375   snat_det_map_t * det_maps;
376
377   /* If forwarding is enabled */
378   u8 forwarding_enabled;
379
380   /* Config parameters */
381   u8 static_mapping_only;
382   u8 static_mapping_connection_tracking;
383   u8 deterministic;
384   u8 out2in_dpo;
385   u32 translation_buckets;
386   u32 translation_memory_size;
387   u32 max_translations;
388   u32 user_buckets;
389   u32 user_memory_size;
390   u32 max_translations_per_user;
391   u32 outside_vrf_id;
392   u32 outside_fib_index;
393   u32 inside_vrf_id;
394   u32 inside_fib_index;
395
396   /* values of various timeouts */
397   u32 udp_timeout;
398   u32 tcp_established_timeout;
399   u32 tcp_transitory_timeout;
400   u32 icmp_timeout;
401
402   /* API message ID base */
403   u16 msg_id_base;
404
405   /* convenience */
406   vlib_main_t * vlib_main;
407   vnet_main_t * vnet_main;
408   ip4_main_t * ip4_main;
409   ip_lookup_main_t * ip4_lookup_main;
410   api_main_t * api_main;
411 } snat_main_t;
412
413 extern snat_main_t snat_main;
414 extern vlib_node_registration_t snat_in2out_node;
415 extern vlib_node_registration_t snat_in2out_output_node;
416 extern vlib_node_registration_t snat_out2in_node;
417 extern vlib_node_registration_t snat_in2out_fast_node;
418 extern vlib_node_registration_t snat_out2in_fast_node;
419 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
420 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
421 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
422 extern vlib_node_registration_t snat_det_in2out_node;
423 extern vlib_node_registration_t snat_det_out2in_node;
424 extern vlib_node_registration_t snat_hairpin_dst_node;
425 extern vlib_node_registration_t snat_hairpin_src_node;
426
427 void snat_free_outside_address_and_port (snat_address_t * addresses,
428                                          u32 thread_index,
429                                          snat_session_key_t * k,
430                                          u32 address_index);
431
432 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
433                                          u32 fib_index,
434                                          u32 thread_index,
435                                          snat_session_key_t * k,
436                                          u32 * address_indexp,
437                                          u16 port_per_thread,
438                                          u32 snat_thread_index);
439
440 int snat_static_mapping_match (snat_main_t * sm,
441                                snat_session_key_t match,
442                                snat_session_key_t * mapping,
443                                u8 by_external,
444                                u8 *is_addr_only,
445                                twice_nat_type_t *twice_nat,
446                                u8 *lb);
447
448 void snat_add_del_addr_to_fib (ip4_address_t * addr,
449                                u8 p_len,
450                                u32 sw_if_index,
451                                int is_add);
452
453 format_function_t format_snat_user;
454 format_function_t format_snat_static_mapping;
455 format_function_t format_snat_static_map_to_resolve;
456 format_function_t format_det_map_ses;
457
458 typedef struct {
459   u32 cached_sw_if_index;
460   u32 cached_ip4_address;
461 } snat_runtime_t;
462
463 /** \brief Check if SNAT session is created from static mapping.
464     @param s SNAT session
465     @return 1 if SNAT session is created from static mapping otherwise 0
466 */
467 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
468
469 /** \brief Check if SNAT session for unknown protocol.
470     @param s SNAT session
471     @return 1 if SNAT session for unknown protocol otherwise 0
472 */
473 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
474
475 /** \brief Check if NAT session is twice NAT.
476     @param s NAT session
477     @return 1 if NAT session is twice NAT
478 */
479 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
480
481 /** \brief Check if NAT session is load-balancing.
482     @param s NAT session
483     @return 1 if NAT session is load-balancing
484 */
485 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
486
487 /** \brief Check if NAT session is forwarding bypass.
488     @param s NAT session
489     @return 1 if NAT session is load-balancing
490 */
491 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
492
493 /** \brief Check if NAT session is endpoint dependent.
494     @param s NAT session
495     @return 1 if NAT session is endpoint dependent
496 */
497 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
498
499 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
500 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
501
502 /*
503  * Why is this here? Because we don't need to touch this layer to
504  * simply reply to an icmp. We need to change id to a unique
505  * value to NAT an echo request/reply.
506  */
507
508 typedef struct {
509   u16 identifier;
510   u16 sequence;
511 } icmp_echo_header_t;
512
513 always_inline u32
514 ip_proto_to_snat_proto (u8 ip_proto)
515 {
516   u32 snat_proto = ~0;
517
518   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
519   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
520   snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
521   snat_proto = (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
522
523   return snat_proto;
524 }
525
526 always_inline u8
527 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
528 {
529   u8 ip_proto = ~0;
530
531   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
532   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
533   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
534
535   return ip_proto;
536 }
537
538 typedef struct {
539   u16 src_port, dst_port;
540 } tcp_udp_header_t;
541
542 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
543                            u32 thread_index, vlib_buffer_t *b0,
544                            ip4_header_t *ip0, u8 *p_proto,
545                            snat_session_key_t *p_value,
546                            u8 *p_dont_translate, void *d, void *e);
547 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
548                            u32 thread_index, vlib_buffer_t *b0,
549                            ip4_header_t *ip0, u8 *p_proto,
550                            snat_session_key_t *p_value,
551                            u8 *p_dont_translate, void *d, void *e);
552 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
553                           u32 thread_index, vlib_buffer_t *b0,
554                           ip4_header_t *ip0, u8 *p_proto,
555                           snat_session_key_t *p_value,
556                           u8 *p_dont_translate, void *d, void *e);
557 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
558                            u32 thread_index, vlib_buffer_t *b0,
559                            ip4_header_t *ip0, u8 *p_proto,
560                            snat_session_key_t *p_value,
561                            u8 *p_dont_translate, void *d, void *e);
562 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
563                            u32 thread_index, vlib_buffer_t *b0,
564                            ip4_header_t *ip0, u8 *p_proto,
565                            snat_session_key_t *p_value,
566                            u8 *p_dont_translate, void *d, void *e);
567 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
568                           u32 thread_index, vlib_buffer_t *b0,
569                           ip4_header_t *ip0, u8 *p_proto,
570                           snat_session_key_t *p_value,
571                           u8 *p_dont_translate, void *d, void *e);
572 void increment_v4_address(ip4_address_t * a);
573 void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
574                       u8 twice_nat);
575 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
576                      u8 twice_nat);
577 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
578 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
579                             u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
580                             u32 sw_if_index, snat_protocol_t proto, int is_add,
581                             twice_nat_type_t twice_nat, u8 out2in_only,
582                             u8 *tag);
583 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
584 int snat_set_workers (uword * bitmap);
585 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
586 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
587                                           int is_del);
588 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del,
589                                u8 twice_nat);
590 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
591 u8 * format_snat_protocol(u8 * s, va_list * args);
592 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
593                                      snat_protocol_t proto, u32 vrf_id,
594                                      nat44_lb_addr_port_t *locals, u8 is_add,
595                                      twice_nat_type_t twice_nat, u8 out2in_only,
596                                      u8 *tag);
597 int nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
598                        snat_protocol_t proto, u32 vrf_id, int is_in);
599 int nat44_del_ed_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
600                           ip4_address_t *eh_addr, u16 eh_port, u8 proto,
601                           u32 vrf_id, int is_in);
602 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
603                             u32 thread_index);
604 snat_user_t * nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr,
605                                       u32 fib_index, u32 thread_index);
606 snat_session_t * nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u,
607                                                u32 thread_index);
608 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
609                                        u16 psid_length);
610 void nat_set_alloc_addr_and_port_default (void);
611
612 static_always_inline u8
613 icmp_is_error_message (icmp46_header_t * icmp)
614 {
615   switch(icmp->type)
616     {
617     case ICMP4_destination_unreachable:
618     case ICMP4_time_exceeded:
619     case ICMP4_parameter_problem:
620     case ICMP4_source_quench:
621     case ICMP4_redirect:
622     case ICMP4_alternate_host_address:
623       return 1;
624     }
625   return 0;
626 }
627
628 static_always_inline u8
629 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
630                   u32 ip4_addr)
631 {
632   snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
633   ip4_address_t * first_int_addr;
634
635   if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
636     {
637       first_int_addr =
638         ip4_interface_first_address (sm->ip4_main, sw_if_index0,
639                                      0 /* just want the address */);
640       rt->cached_sw_if_index = sw_if_index0;
641       if (first_int_addr)
642         rt->cached_ip4_address = first_int_addr->as_u32;
643       else
644         rt->cached_ip4_address = 0;
645     }
646
647   if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
648     return 1;
649   else
650     return 0;
651 }
652
653 always_inline u8
654 maximum_sessions_exceeded (snat_main_t *sm, u32 thread_index)
655 {
656   if (pool_elts (sm->per_thread_data[thread_index].sessions) >= sm->max_translations)
657     return 1;
658
659   return 0;
660 }
661
662 static_always_inline void
663 nat_send_all_to_node(vlib_main_t *vm, u32 *bi_vector,
664                      vlib_node_runtime_t *node, vlib_error_t *error, u32 next)
665 {
666   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
667
668   from = bi_vector;
669   n_left_from = vec_len(bi_vector);
670   next_index = node->cached_next_index;
671   while (n_left_from > 0) {
672     vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
673     while (n_left_from > 0 && n_left_to_next > 0) {
674       u32 bi0 = to_next[0] = from[0];
675       from += 1;
676       n_left_from -= 1;
677       to_next += 1;
678       n_left_to_next -= 1;
679       vlib_buffer_t *p0 = vlib_get_buffer(vm, bi0);
680       p0->error = *error;
681       vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
682                                       n_left_to_next, bi0, next);
683     }
684     vlib_put_next_frame(vm, node, next_index, n_left_to_next);
685   }
686 }
687
688 always_inline void
689 user_session_increment(snat_main_t *sm, snat_user_t *u, u8 is_static)
690 {
691   if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
692     {
693       if (is_static)
694         u->nstaticsessions++;
695       else
696         u->nsessions++;
697     }
698 }
699
700 always_inline void
701 nat44_delete_session(snat_main_t * sm, snat_session_t * ses, u32 thread_index)
702 {
703   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
704                                                        thread_index);
705   clib_bihash_kv_8_8_t kv, value;
706   snat_user_key_t u_key;
707   snat_user_t *u;
708   u_key.addr = ses->in2out.addr;
709   u_key.fib_index = ses->in2out.fib_index;
710   kv.key = u_key.as_u64;
711   if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
712     {
713       u = pool_elt_at_index (tsm->users, value.value);
714       if (snat_is_session_static(ses))
715         u->nstaticsessions--;
716       else
717         u->nsessions--;
718     }
719   clib_dlist_remove (tsm->list_pool, ses->per_user_index);
720   pool_put_index (tsm->list_pool, ses->per_user_index);
721   pool_put (tsm->sessions, ses);
722 }
723
724 /** \brief Set TCP session state.
725     @return 1 if session was closed, otherwise 0
726 */
727 always_inline int
728 nat44_set_tcp_session_state_i2o(snat_main_t * sm, snat_session_t * ses,
729                                 tcp_header_t * tcp, u32 thread_index)
730 {
731   if (tcp->flags & TCP_FLAG_FIN)
732     {
733       ses->i2o_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
734       ses->state |= NAT44_SES_I2O_FIN;
735     }
736   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
737     {
738       if (clib_net_to_host_u32 (tcp->ack_number) > ses->o2i_fin_seq)
739         ses->state |= NAT44_SES_O2I_FIN_ACK;
740     }
741   if (nat44_is_ses_closed (ses))
742     {
743       nat_free_session_data (sm, ses, thread_index);
744       nat44_delete_session (sm, ses, thread_index);
745       return 1;
746     }
747   return 0;
748 }
749
750 always_inline int
751 nat44_set_tcp_session_state_o2i(snat_main_t * sm, snat_session_t * ses,
752                                 tcp_header_t * tcp, u32 thread_index)
753 {
754   if (tcp->flags & TCP_FLAG_FIN)
755     {
756       ses->o2i_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
757       ses->state |= NAT44_SES_O2I_FIN;
758     }
759   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
760     {
761       if (clib_net_to_host_u32 (tcp->ack_number) > ses->i2o_fin_seq)
762         ses->state |= NAT44_SES_I2O_FIN_ACK;
763     }
764   if (nat44_is_ses_closed (ses))
765     {
766       nat_free_session_data (sm, ses, thread_index);
767       nat44_delete_session (sm, ses, thread_index);
768       return 1;
769     }
770   return 0;
771 }
772
773 #endif /* __included_snat_h__ */