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