db5b295f9386a09ae5a6425c50ddf97087621677
[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_session (snat_main_t * sm, snat_session_t * ses,
145                       u32 thread_index)
146 {
147   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
148                                                        thread_index);
149   clib_bihash_kv_8_8_t kv, value;
150   snat_user_key_t u_key;
151   snat_user_t *u;
152
153   nat_log_debug ("session deleted %U", format_snat_session, tsm, ses);
154   u_key.addr = ses->in2out.addr;
155   u_key.fib_index = ses->in2out.fib_index;
156   kv.key = u_key.as_u64;
157   if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
158     {
159       u = pool_elt_at_index (tsm->users, value.value);
160       if (snat_is_session_static (ses))
161         u->nstaticsessions--;
162       else
163         u->nsessions--;
164     }
165   clib_dlist_remove (tsm->list_pool, ses->per_user_index);
166   pool_put_index (tsm->list_pool, ses->per_user_index);
167   pool_put (tsm->sessions, ses);
168 }
169
170 /** \brief Set TCP session state.
171     @return 1 if session was closed, otherwise 0
172 */
173 always_inline int
174 nat44_set_tcp_session_state_i2o (snat_main_t * sm, snat_session_t * ses,
175                                  tcp_header_t * tcp, u32 thread_index)
176 {
177   if (tcp->flags & TCP_FLAG_FIN)
178     {
179       ses->i2o_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
180       ses->state |= NAT44_SES_I2O_FIN;
181     }
182   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
183     {
184       if (clib_net_to_host_u32 (tcp->ack_number) > ses->o2i_fin_seq)
185         ses->state |= NAT44_SES_O2I_FIN_ACK;
186     }
187   if (nat44_is_ses_closed (ses))
188     {
189       nat_log_debug ("TCP close connection %U", format_snat_session,
190                      &sm->per_thread_data[thread_index], ses);
191       nat_free_session_data (sm, ses, thread_index);
192       nat44_delete_session (sm, ses, thread_index);
193       return 1;
194     }
195   return 0;
196 }
197
198 always_inline int
199 nat44_set_tcp_session_state_o2i (snat_main_t * sm, snat_session_t * ses,
200                                  tcp_header_t * tcp, u32 thread_index)
201 {
202   if (tcp->flags & TCP_FLAG_FIN)
203     {
204       ses->o2i_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
205       ses->state |= NAT44_SES_O2I_FIN;
206     }
207   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
208     {
209       if (clib_net_to_host_u32 (tcp->ack_number) > ses->i2o_fin_seq)
210         ses->state |= NAT44_SES_I2O_FIN_ACK;
211     }
212   if (nat44_is_ses_closed (ses))
213     {
214       nat_log_debug ("TCP close connection %U", format_snat_session,
215                      &sm->per_thread_data[thread_index], ses);
216       nat_free_session_data (sm, ses, thread_index);
217       nat44_delete_session (sm, ses, thread_index);
218       return 1;
219     }
220   return 0;
221 }
222
223 always_inline void
224 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes)
225 {
226   s->last_heard = now;
227   s->total_pkts++;
228   s->total_bytes += bytes;
229 }
230
231 /** \brief Per-user LRU list maintenance */
232 always_inline void
233 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
234                           u32 thread_index)
235 {
236   clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
237                      s->per_user_index);
238   clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
239                       s->per_user_list_head_index, s->per_user_index);
240 }
241
242 #endif /* __included_nat_inlines_h__ */
243
244 /*
245  * fd.io coding-style-patch-verification: ON
246  *
247  * Local Variables:
248  * eval: (c-set-style "gnu")
249  * End:
250  */