Remove c-11 memcpy checks from perf-critical code
[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 <vnet/fib/ip4_fib.h>
23 #include <nat/nat.h>
24
25 always_inline u32
26 ip_proto_to_snat_proto (u8 ip_proto)
27 {
28   u32 snat_proto = ~0;
29
30   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
31   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
32   snat_proto =
33     (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
34   snat_proto =
35     (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
36
37   return snat_proto;
38 }
39
40 always_inline u8
41 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
42 {
43   u8 ip_proto = ~0;
44
45   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
46   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
47   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
48
49   return ip_proto;
50 }
51
52 static_always_inline u8
53 icmp_is_error_message (icmp46_header_t * icmp)
54 {
55   switch (icmp->type)
56     {
57     case ICMP4_destination_unreachable:
58     case ICMP4_time_exceeded:
59     case ICMP4_parameter_problem:
60     case ICMP4_source_quench:
61     case ICMP4_redirect:
62     case ICMP4_alternate_host_address:
63       return 1;
64     }
65   return 0;
66 }
67
68 always_inline u8
69 is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node,
70                    u32 sw_if_index0, u32 ip4_addr)
71 {
72   snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
73   ip4_address_t *first_int_addr;
74
75   if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
76     {
77       first_int_addr =
78         ip4_interface_first_address (sm->ip4_main, sw_if_index0,
79                                      0 /* just want the address */ );
80       rt->cached_sw_if_index = sw_if_index0;
81       if (first_int_addr)
82         rt->cached_ip4_address = first_int_addr->as_u32;
83       else
84         rt->cached_ip4_address = 0;
85     }
86
87   if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
88     return 1;
89   else
90     return 0;
91 }
92
93 always_inline u8
94 maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
95 {
96   if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
97       sm->max_translations)
98     return 1;
99
100   return 0;
101 }
102
103 always_inline void
104 nat_send_all_to_node (vlib_main_t * vm, u32 * bi_vector,
105                       vlib_node_runtime_t * node, vlib_error_t * error,
106                       u32 next)
107 {
108   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
109
110   from = bi_vector;
111   n_left_from = vec_len (bi_vector);
112   next_index = node->cached_next_index;
113   while (n_left_from > 0)
114     {
115       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
116       while (n_left_from > 0 && n_left_to_next > 0)
117         {
118           u32 bi0 = to_next[0] = from[0];
119           from += 1;
120           n_left_from -= 1;
121           to_next += 1;
122           n_left_to_next -= 1;
123           vlib_buffer_t *p0 = vlib_get_buffer (vm, bi0);
124           if (error)
125             p0->error = *error;
126           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
127                                            n_left_to_next, bi0, next);
128         }
129       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
130     }
131 }
132
133 always_inline void
134 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
135 {
136   if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
137     {
138       if (is_static)
139         u->nstaticsessions++;
140       else
141         u->nsessions++;
142     }
143 }
144
145 always_inline void
146 nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u,
147                                    u32 thread_index)
148 {
149   clib_bihash_kv_8_8_t kv;
150   snat_user_key_t u_key;
151   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
152                                                        thread_index);
153
154   if (u->nstaticsessions == 0 && u->nsessions == 0)
155     {
156       u_key.addr.as_u32 = u->addr.as_u32;
157       u_key.fib_index = u->fib_index;
158       kv.key = u_key.as_u64;
159       pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index);
160       pool_put (tsm->users, u);
161       clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
162     }
163 }
164
165 always_inline void
166 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
167                       u32 thread_index)
168 {
169   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
170                                                        thread_index);
171   clib_bihash_kv_8_8_t kv, value;
172   snat_user_key_t u_key;
173   snat_user_t *u;
174
175   nat_log_debug ("session deleted %U", format_snat_session, tsm, ses);
176
177   clib_dlist_remove (tsm->list_pool, ses->per_user_index);
178   pool_put_index (tsm->list_pool, ses->per_user_index);
179   pool_put (tsm->sessions, ses);
180
181   u_key.addr = ses->in2out.addr;
182   u_key.fib_index = ses->in2out.fib_index;
183   kv.key = u_key.as_u64;
184   if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
185     {
186       u = pool_elt_at_index (tsm->users, value.value);
187       if (snat_is_session_static (ses))
188         u->nstaticsessions--;
189       else
190         u->nsessions--;
191
192       nat44_delete_user_with_no_session (sm, u, thread_index);
193     }
194 }
195
196 /** \brief Set TCP session state.
197     @return 1 if session was closed, otherwise 0
198 */
199 always_inline int
200 nat44_set_tcp_session_state_i2o (snat_main_t * sm, snat_session_t * ses,
201                                  tcp_header_t * tcp, u32 thread_index)
202 {
203   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
204       (ses->state & NAT44_SES_O2I_SYN))
205     ses->state = 0;
206   if (tcp->flags & TCP_FLAG_SYN)
207     ses->state |= NAT44_SES_I2O_SYN;
208   if (tcp->flags & TCP_FLAG_FIN)
209     {
210       ses->i2o_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
211       ses->state |= NAT44_SES_I2O_FIN;
212     }
213   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
214     {
215       if (clib_net_to_host_u32 (tcp->ack_number) > ses->o2i_fin_seq)
216         ses->state |= NAT44_SES_O2I_FIN_ACK;
217     }
218   if (nat44_is_ses_closed (ses))
219     {
220       nat_log_debug ("TCP close connection %U", format_snat_session,
221                      &sm->per_thread_data[thread_index], ses);
222       nat_free_session_data (sm, ses, thread_index);
223       nat44_delete_session (sm, ses, thread_index);
224       return 1;
225     }
226   return 0;
227 }
228
229 always_inline int
230 nat44_set_tcp_session_state_o2i (snat_main_t * sm, snat_session_t * ses,
231                                  tcp_header_t * tcp, u32 thread_index)
232 {
233   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
234       (ses->state & NAT44_SES_O2I_SYN))
235     ses->state = 0;
236   if (tcp->flags & TCP_FLAG_SYN)
237     ses->state |= NAT44_SES_O2I_SYN;
238   if (tcp->flags & TCP_FLAG_FIN)
239     {
240       ses->o2i_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
241       ses->state |= NAT44_SES_O2I_FIN;
242     }
243   if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
244     {
245       if (clib_net_to_host_u32 (tcp->ack_number) > ses->i2o_fin_seq)
246         ses->state |= NAT44_SES_I2O_FIN_ACK;
247     }
248   if (nat44_is_ses_closed (ses))
249     {
250       nat_log_debug ("TCP close connection %U", format_snat_session,
251                      &sm->per_thread_data[thread_index], ses);
252       nat_free_session_data (sm, ses, thread_index);
253       nat44_delete_session (sm, ses, thread_index);
254       return 1;
255     }
256   return 0;
257 }
258
259 always_inline u32
260 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
261 {
262   switch (s->in2out.protocol)
263     {
264     case SNAT_PROTOCOL_ICMP:
265       return sm->icmp_timeout;
266     case SNAT_PROTOCOL_UDP:
267       return sm->udp_timeout;
268     case SNAT_PROTOCOL_TCP:
269       {
270         if (s->state)
271           return sm->tcp_transitory_timeout;
272         else
273           return sm->tcp_established_timeout;
274       }
275     default:
276       return sm->udp_timeout;
277     }
278
279   return 0;
280 }
281
282 always_inline void
283 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes)
284 {
285   s->last_heard = now;
286   s->total_pkts++;
287   s->total_bytes += bytes;
288 }
289
290 /** \brief Per-user LRU list maintenance */
291 always_inline void
292 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
293                           u32 thread_index)
294 {
295   clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
296                      s->per_user_index);
297   clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
298                       s->per_user_list_head_index, s->per_user_index);
299 }
300
301 always_inline void
302 make_ed_kv (clib_bihash_kv_16_8_t * kv, ip4_address_t * l_addr,
303             ip4_address_t * r_addr, u8 proto, u32 fib_index, u16 l_port,
304             u16 r_port)
305 {
306   nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
307
308   key->l_addr.as_u32 = l_addr->as_u32;
309   key->r_addr.as_u32 = r_addr->as_u32;
310   key->fib_index = fib_index;
311   key->proto = proto;
312   key->l_port = l_port;
313   key->r_port = r_port;
314
315   kv->value = ~0ULL;
316 }
317
318 always_inline void
319 make_sm_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t * addr, u8 proto,
320             u32 fib_index, u16 port)
321 {
322   snat_session_key_t key;
323
324   key.addr.as_u32 = addr->as_u32;
325   key.port = port;
326   key.protocol = proto;
327   key.fib_index = fib_index;
328
329   kv->key = key.as_u64;
330   kv->value = ~0ULL;
331 }
332
333 always_inline void
334 mss_clamping (snat_main_t * sm, tcp_header_t * tcp, ip_csum_t * sum)
335 {
336   u8 *data;
337   u8 opt_len, opts_len, kind;
338   u16 mss;
339
340   if (!(sm->mss_clamping && tcp_syn (tcp)))
341     return;
342
343   opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
344   data = (u8 *) (tcp + 1);
345   for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
346     {
347       kind = data[0];
348
349       if (kind == TCP_OPTION_EOL)
350         break;
351       else if (kind == TCP_OPTION_NOOP)
352         {
353           opt_len = 1;
354           continue;
355         }
356       else
357         {
358           if (opts_len < 2)
359             return;
360           opt_len = data[1];
361
362           if (opt_len < 2 || opt_len > opts_len)
363             return;
364         }
365
366       if (kind == TCP_OPTION_MSS)
367         {
368           mss = *(u16 *) (data + 2);
369           if (clib_net_to_host_u16 (mss) > sm->mss_clamping)
370             {
371               *sum =
372                 ip_csum_update (*sum, mss, sm->mss_value_net, ip4_header_t,
373                                 length);
374               clib_memcpy_fast (data + 2, &sm->mss_value_net, 2);
375             }
376           return;
377         }
378     }
379 }
380
381 /**
382  * @brief Check if packet should be translated
383  *
384  * Packets aimed at outside interface and external address with active session
385  * should be translated.
386  *
387  * @param sm            NAT main
388  * @param rt            NAT runtime data
389  * @param sw_if_index0  index of the inside interface
390  * @param ip0           IPv4 header
391  * @param proto0        NAT protocol
392  * @param rx_fib_index0 RX FIB index
393  *
394  * @returns 0 if packet should be translated otherwise 1
395  */
396 static inline int
397 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t * node,
398                          u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
399                          u32 rx_fib_index0)
400 {
401   if (sm->out2in_dpo)
402     return 0;
403
404   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
405   nat_outside_fib_t *outside_fib;
406   fib_prefix_t pfx = {
407     .fp_proto = FIB_PROTOCOL_IP4,
408     .fp_len = 32,
409     .fp_addr = {
410                 .ip4.as_u32 = ip0->dst_address.as_u32,
411                 }
412     ,
413   };
414
415   /* Don't NAT packet aimed at the intfc address */
416   if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
417                                         ip0->dst_address.as_u32)))
418     return 1;
419
420   fei = fib_table_lookup (rx_fib_index0, &pfx);
421   if (FIB_NODE_INDEX_INVALID != fei)
422     {
423       u32 sw_if_index = fib_entry_get_resolving_interface (fei);
424       if (sw_if_index == ~0)
425         {
426           vec_foreach (outside_fib, sm->outside_fibs)
427           {
428             fei = fib_table_lookup (outside_fib->fib_index, &pfx);
429             if (FIB_NODE_INDEX_INVALID != fei)
430               {
431                 sw_if_index = fib_entry_get_resolving_interface (fei);
432                 if (sw_if_index != ~0)
433                   break;
434               }
435           }
436         }
437       if (sw_if_index == ~0)
438         return 1;
439
440       snat_interface_t *i;
441       pool_foreach (i, sm->interfaces, (
442                                          {
443                                          /* NAT packet aimed at outside interface */
444                                          if ((nat_interface_is_outside (i))
445                                              && (sw_if_index ==
446                                                  i->sw_if_index)) return 0;}
447                     ));
448     }
449
450   return 1;
451 }
452
453 #endif /* __included_nat_inlines_h__ */
454
455 /*
456  * fd.io coding-style-patch-verification: ON
457  *
458  * Local Variables:
459  * eval: (c-set-style "gnu")
460  * End:
461  */