NAT44: add support for session timeout (VPP-1272)
[vpp.git] / src / plugins / nat / nat_inlines.h
1 /*
2  * Copyright (c) 2018 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 /**
16  * @brief The NAT inline functions
17  */
18
19 #ifndef __included_nat_inlines_h__
20 #define __included_nat_inlines_h__
21
22 #include <nat/nat.h>
23
24 always_inline u32
25 ip_proto_to_snat_proto (u8 ip_proto)
26 {
27   u32 snat_proto = ~0;
28
29   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
30   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
31   snat_proto =
32     (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
33   snat_proto =
34     (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
35
36   return snat_proto;
37 }
38
39 always_inline u8
40 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
41 {
42   u8 ip_proto = ~0;
43
44   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
45   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
46   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
47
48   return ip_proto;
49 }
50
51 static_always_inline u8
52 icmp_is_error_message (icmp46_header_t * icmp)
53 {
54   switch (icmp->type)
55     {
56     case ICMP4_destination_unreachable:
57     case ICMP4_time_exceeded:
58     case ICMP4_parameter_problem:
59     case ICMP4_source_quench:
60     case ICMP4_redirect:
61     case ICMP4_alternate_host_address:
62       return 1;
63     }
64   return 0;
65 }
66
67 always_inline u8
68 is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node,
69                    u32 sw_if_index0, u32 ip4_addr)
70 {
71   snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
72   ip4_address_t *first_int_addr;
73
74   if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
75     {
76       first_int_addr =
77         ip4_interface_first_address (sm->ip4_main, sw_if_index0,
78                                      0 /* just want the address */ );
79       rt->cached_sw_if_index = sw_if_index0;
80       if (first_int_addr)
81         rt->cached_ip4_address = first_int_addr->as_u32;
82       else
83         rt->cached_ip4_address = 0;
84     }
85
86   if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
87     return 1;
88   else
89     return 0;
90 }
91
92 always_inline u8
93 maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
94 {
95   if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
96       sm->max_translations)
97     return 1;
98
99   return 0;
100 }
101
102 always_inline void
103 nat_send_all_to_node (vlib_main_t * vm, u32 * bi_vector,
104                       vlib_node_runtime_t * node, vlib_error_t * error,
105                       u32 next)
106 {
107   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
108
109   from = bi_vector;
110   n_left_from = vec_len (bi_vector);
111   next_index = node->cached_next_index;
112   while (n_left_from > 0)
113     {
114       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
115       while (n_left_from > 0 && n_left_to_next > 0)
116         {
117           u32 bi0 = to_next[0] = from[0];
118           from += 1;
119           n_left_from -= 1;
120           to_next += 1;
121           n_left_to_next -= 1;
122           vlib_buffer_t *p0 = vlib_get_buffer (vm, bi0);
123           p0->error = *error;
124           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
125                                            n_left_to_next, bi0, next);
126         }
127       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
128     }
129 }
130
131 always_inline void
132 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
133 {
134   if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
135     {
136       if (is_static)
137         u->nstaticsessions++;
138       else
139         u->nsessions++;
140     }
141 }
142
143 always_inline void
144 nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u,
145                                    u32 thread_index)
146 {
147   clib_bihash_kv_8_8_t kv;
148   snat_user_key_t u_key;
149   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
150                                                        thread_index);
151
152   if (u->nstaticsessions == 0 && u->nsessions == 0)
153     {
154       u_key.addr.as_u32 = u->addr.as_u32;
155       u_key.fib_index = u->fib_index;
156       kv.key = u_key.as_u64;
157       pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index);
158       pool_put (tsm->users, u);
159       clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
160     }
161 }
162
163 always_inline void
164 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
165                       u32 thread_index)
166 {
167   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
168                                                        thread_index);
169   clib_bihash_kv_8_8_t kv, value;
170   snat_user_key_t u_key;
171   snat_user_t *u;
172
173   nat_log_debug ("session deleted %U", format_snat_session, tsm, ses);
174
175   clib_dlist_remove (tsm->list_pool, ses->per_user_index);
176   pool_put_index (tsm->list_pool, ses->per_user_index);
177   pool_put (tsm->sessions, ses);
178
179   u_key.addr = ses->in2out.addr;
180   u_key.fib_index = ses->in2out.fib_index;
181   kv.key = u_key.as_u64;
182   if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
183     {
184       u = pool_elt_at_index (tsm->users, value.value);
185       if (snat_is_session_static (ses))
186         u->nstaticsessions--;
187       else
188         u->nsessions--;
189
190       nat44_delete_user_with_no_session (sm, u, thread_index);
191     }
192 }
193
194 /** \brief Set TCP session state.
195     @return 1 if session was closed, otherwise 0
196 */
197 always_inline int
198 nat44_set_tcp_session_state_i2o (snat_main_t * sm, snat_session_t * ses,
199                                  tcp_header_t * tcp, u32 thread_index)
200 {
201   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
202       (ses->state & NAT44_SES_O2I_SYN))
203     ses->state = 0;
204   if (tcp->flags & TCP_FLAG_SYN)
205     ses->state |= NAT44_SES_I2O_SYN;
206   if (tcp->flags & TCP_FLAG_FIN)
207     {
208       ses->i2o_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
209       ses->state |= NAT44_SES_I2O_FIN;
210     }
211   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
212     {
213       if (clib_net_to_host_u32 (tcp->ack_number) > ses->o2i_fin_seq)
214         ses->state |= NAT44_SES_O2I_FIN_ACK;
215     }
216   if (nat44_is_ses_closed (ses))
217     {
218       nat_log_debug ("TCP close connection %U", format_snat_session,
219                      &sm->per_thread_data[thread_index], ses);
220       nat_free_session_data (sm, ses, thread_index);
221       nat44_delete_session (sm, ses, thread_index);
222       return 1;
223     }
224   return 0;
225 }
226
227 always_inline int
228 nat44_set_tcp_session_state_o2i (snat_main_t * sm, snat_session_t * ses,
229                                  tcp_header_t * tcp, u32 thread_index)
230 {
231   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
232       (ses->state & NAT44_SES_O2I_SYN))
233     ses->state = 0;
234   if (tcp->flags & TCP_FLAG_SYN)
235     ses->state |= NAT44_SES_O2I_SYN;
236   if (tcp->flags & TCP_FLAG_FIN)
237     {
238       ses->o2i_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
239       ses->state |= NAT44_SES_O2I_FIN;
240     }
241   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
242     {
243       if (clib_net_to_host_u32 (tcp->ack_number) > ses->i2o_fin_seq)
244         ses->state |= NAT44_SES_I2O_FIN_ACK;
245     }
246   if (nat44_is_ses_closed (ses))
247     {
248       nat_log_debug ("TCP close connection %U", format_snat_session,
249                      &sm->per_thread_data[thread_index], ses);
250       nat_free_session_data (sm, ses, thread_index);
251       nat44_delete_session (sm, ses, thread_index);
252       return 1;
253     }
254   return 0;
255 }
256
257 always_inline u32
258 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
259 {
260   switch (s->in2out.protocol)
261     {
262     case SNAT_PROTOCOL_ICMP:
263       return sm->icmp_timeout;
264     case SNAT_PROTOCOL_UDP:
265       return sm->udp_timeout;
266     case SNAT_PROTOCOL_TCP:
267       {
268         if (s->state)
269           return sm->tcp_transitory_timeout;
270         else
271           return sm->tcp_established_timeout;
272       }
273     default:
274       return sm->udp_timeout;
275     }
276
277   return 0;
278 }
279
280 always_inline void
281 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes)
282 {
283   s->last_heard = now;
284   s->total_pkts++;
285   s->total_bytes += bytes;
286 }
287
288 /** \brief Per-user LRU list maintenance */
289 always_inline void
290 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
291                           u32 thread_index)
292 {
293   clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
294                      s->per_user_index);
295   clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
296                       s->per_user_list_head_index, s->per_user_index);
297 }
298
299 always_inline void
300 make_ed_kv (clib_bihash_kv_16_8_t * kv, ip4_address_t * l_addr,
301             ip4_address_t * r_addr, u8 proto, u32 fib_index, u16 l_port,
302             u16 r_port)
303 {
304   nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
305
306   key->l_addr.as_u32 = l_addr->as_u32;
307   key->r_addr.as_u32 = r_addr->as_u32;
308   key->fib_index = fib_index;
309   key->proto = proto;
310   key->l_port = l_port;
311   key->r_port = r_port;
312
313   kv->value = ~0ULL;
314 }
315
316 always_inline void
317 make_sm_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t * addr, u8 proto,
318             u32 fib_index, u16 port)
319 {
320   snat_session_key_t key;
321
322   key.addr.as_u32 = addr->as_u32;
323   key.port = port;
324   key.protocol = proto;
325   key.fib_index = fib_index;
326
327   kv->key = key.as_u64;
328   kv->value = ~0ULL;
329 }
330
331 #endif /* __included_nat_inlines_h__ */
332
333 /*
334  * fd.io coding-style-patch-verification: ON
335  *
336  * Local Variables:
337  * eval: (c-set-style "gnu")
338  * End:
339  */