nat: handoff next node feature fix
[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 #include <nat/nat_ha.h>
25
26 static inline uword
27 nat_pre_node_fn_inline (vlib_main_t * vm,
28                         vlib_node_runtime_t * node,
29                         vlib_frame_t * frame, u32 def_next)
30 {
31   u32 n_left_from, *from, *to_next;
32   u16 next_index;
33
34   from = vlib_frame_vector_args (frame);
35   n_left_from = frame->n_vectors;
36   next_index = node->cached_next_index;
37
38   while (n_left_from > 0)
39     {
40       u32 n_left_to_next;
41
42       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
43
44       while (n_left_from >= 4 && n_left_to_next >= 2)
45         {
46           u32 next0, next1;
47           u32 arc_next0, arc_next1;
48           u32 bi0, bi1;
49           vlib_buffer_t *b0, *b1;
50
51           /* Prefetch next iteration. */
52           {
53             vlib_buffer_t *p2, *p3;
54
55             p2 = vlib_get_buffer (vm, from[2]);
56             p3 = vlib_get_buffer (vm, from[3]);
57
58             vlib_prefetch_buffer_header (p2, LOAD);
59             vlib_prefetch_buffer_header (p3, LOAD);
60
61             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
62             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
63           }
64
65           /* speculatively enqueue b0 and b1 to the current next frame */
66           to_next[0] = bi0 = from[0];
67           to_next[1] = bi1 = from[1];
68           from += 2;
69           to_next += 2;
70           n_left_from -= 2;
71           n_left_to_next -= 2;
72
73           b0 = vlib_get_buffer (vm, bi0);
74           b1 = vlib_get_buffer (vm, bi1);
75
76           next0 = def_next;
77           next1 = def_next;
78
79           vnet_feature_next (&arc_next0, b0);
80           vnet_feature_next (&arc_next1, b1);
81
82           vnet_buffer2 (b0)->nat.arc_next = arc_next0;
83           vnet_buffer2 (b1)->nat.arc_next = arc_next1;
84
85           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
86             {
87               if (b0->flags & VLIB_BUFFER_IS_TRACED)
88                 {
89                   nat_pre_trace_t *t =
90                     vlib_add_trace (vm, node, b0, sizeof (*t));
91                   t->next_index = next0;
92                   t->arc_next_index = arc_next0;
93                 }
94               if (b1->flags & VLIB_BUFFER_IS_TRACED)
95                 {
96                   nat_pre_trace_t *t =
97                     vlib_add_trace (vm, node, b0, sizeof (*t));
98                   t->next_index = next1;
99                   t->arc_next_index = arc_next1;
100                 }
101             }
102
103           /* verify speculative enqueues, maybe switch current next frame */
104           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
105                                            to_next, n_left_to_next,
106                                            bi0, bi1, next0, next1);
107         }
108
109       while (n_left_from > 0 && n_left_to_next > 0)
110         {
111           u32 next0;
112           u32 arc_next0;
113           u32 bi0;
114           vlib_buffer_t *b0;
115
116           /* speculatively enqueue b0 to the current next frame */
117           bi0 = from[0];
118           to_next[0] = bi0;
119           from += 1;
120           to_next += 1;
121           n_left_from -= 1;
122           n_left_to_next -= 1;
123
124           b0 = vlib_get_buffer (vm, bi0);
125           next0 = def_next;
126           vnet_feature_next (&arc_next0, b0);
127           vnet_buffer2 (b0)->nat.arc_next = arc_next0;
128
129           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
130                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
131             {
132               nat_pre_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
133               t->next_index = next0;
134               t->arc_next_index = arc_next0;
135             }
136
137           /* verify speculative enqueue, maybe switch current next frame */
138           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
139                                            to_next, n_left_to_next,
140                                            bi0, next0);
141         }
142
143       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
144     }
145
146   return frame->n_vectors;
147 }
148
149 always_inline u32
150 ip_proto_to_snat_proto (u8 ip_proto)
151 {
152   u32 snat_proto = ~0;
153
154   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
155   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
156   snat_proto =
157     (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
158   snat_proto =
159     (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
160
161   return snat_proto;
162 }
163
164 always_inline u8
165 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
166 {
167   u8 ip_proto = ~0;
168
169   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
170   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
171   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
172
173   return ip_proto;
174 }
175
176 static_always_inline u8
177 icmp_type_is_error_message (u8 icmp_type)
178 {
179   switch (icmp_type)
180     {
181     case ICMP4_destination_unreachable:
182     case ICMP4_time_exceeded:
183     case ICMP4_parameter_problem:
184     case ICMP4_source_quench:
185     case ICMP4_redirect:
186     case ICMP4_alternate_host_address:
187       return 1;
188     }
189   return 0;
190 }
191
192 always_inline u8
193 is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node,
194                    u32 sw_if_index0, u32 ip4_addr)
195 {
196   snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
197   ip4_address_t *first_int_addr;
198
199   if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
200     {
201       first_int_addr =
202         ip4_interface_first_address (sm->ip4_main, sw_if_index0,
203                                      0 /* just want the address */ );
204       rt->cached_sw_if_index = sw_if_index0;
205       if (first_int_addr)
206         rt->cached_ip4_address = first_int_addr->as_u32;
207       else
208         rt->cached_ip4_address = 0;
209     }
210
211   if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
212     return 1;
213   else
214     return 0;
215 }
216
217 always_inline u8
218 maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
219 {
220   if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
221       sm->max_translations)
222     return 1;
223
224   return 0;
225 }
226
227 always_inline void
228 nat_send_all_to_node (vlib_main_t * vm, u32 * bi_vector,
229                       vlib_node_runtime_t * node, vlib_error_t * error,
230                       u32 next)
231 {
232   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
233
234   from = bi_vector;
235   n_left_from = vec_len (bi_vector);
236   next_index = node->cached_next_index;
237   while (n_left_from > 0)
238     {
239       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
240       while (n_left_from > 0 && n_left_to_next > 0)
241         {
242           u32 bi0 = to_next[0] = from[0];
243           from += 1;
244           n_left_from -= 1;
245           to_next += 1;
246           n_left_to_next -= 1;
247           vlib_buffer_t *p0 = vlib_get_buffer (vm, bi0);
248           if (error)
249             p0->error = *error;
250           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
251                                            n_left_to_next, bi0, next);
252         }
253       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
254     }
255 }
256
257 always_inline void
258 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
259 {
260   if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
261     {
262       if (is_static)
263         u->nstaticsessions++;
264       else
265         u->nsessions++;
266     }
267 }
268
269 always_inline void
270 nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u,
271                                    u32 thread_index)
272 {
273   clib_bihash_kv_8_8_t kv;
274   snat_user_key_t u_key;
275   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
276                                                        thread_index);
277
278   if (u->nstaticsessions == 0 && u->nsessions == 0)
279     {
280       u_key.addr.as_u32 = u->addr.as_u32;
281       u_key.fib_index = u->fib_index;
282       kv.key = u_key.as_u64;
283       pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index);
284       pool_put (tsm->users, u);
285       clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
286       vlib_set_simple_counter (&sm->total_users, thread_index, 0,
287                                pool_elts (tsm->users));
288     }
289 }
290
291 always_inline void
292 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
293                       u32 thread_index)
294 {
295   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
296                                                        thread_index);
297   clib_bihash_kv_8_8_t kv, value;
298   snat_user_t *u;
299   const snat_user_key_t u_key = {
300     .addr = ses->in2out.addr,
301     .fib_index = ses->in2out.fib_index
302   };
303   const u8 u_static = snat_is_session_static (ses);
304
305   clib_dlist_remove (tsm->list_pool, ses->per_user_index);
306   pool_put_index (tsm->list_pool, ses->per_user_index);
307   clib_dlist_remove (tsm->global_lru_pool, ses->global_lru_index);
308   pool_put_index (tsm->global_lru_pool, ses->global_lru_index);
309   pool_put (tsm->sessions, ses);
310   vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
311                            pool_elts (tsm->sessions));
312
313   kv.key = u_key.as_u64;
314   if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
315     {
316       u = pool_elt_at_index (tsm->users, value.value);
317       if (u_static)
318         u->nstaticsessions--;
319       else
320         u->nsessions--;
321
322       nat44_delete_user_with_no_session (sm, u, thread_index);
323     }
324 }
325
326 always_inline void
327 nat44_ed_delete_session (snat_main_t * sm, snat_session_t * ses,
328                          u32 thread_index, int global_lru_delete
329                          /* delete from global LRU list */ )
330 {
331   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
332                                                        thread_index);
333
334   if (global_lru_delete)
335     {
336       clib_dlist_remove (tsm->global_lru_pool, ses->global_lru_index);
337     }
338   pool_put_index (tsm->global_lru_pool, ses->global_lru_index);
339   pool_put (tsm->sessions, ses);
340   vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
341                            pool_elts (tsm->sessions));
342
343 }
344
345 /** \brief Set TCP session state.
346     @return 1 if session was closed, otherwise 0
347 */
348 always_inline int
349 nat44_set_tcp_session_state_i2o (snat_main_t * sm, f64 now,
350                                  snat_session_t * ses, vlib_buffer_t * b,
351                                  u32 thread_index)
352 {
353   u8 tcp_flags = vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags;
354   u32 tcp_ack_number = vnet_buffer (b)->ip.reass.tcp_ack_number;
355   u32 tcp_seq_number = vnet_buffer (b)->ip.reass.tcp_seq_number;
356   if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
357     ses->state = NAT44_SES_RST;
358   if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
359     ses->state = 0;
360   if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
361       (ses->state & NAT44_SES_O2I_SYN))
362     ses->state = 0;
363   if (tcp_flags & TCP_FLAG_SYN)
364     ses->state |= NAT44_SES_I2O_SYN;
365   if (tcp_flags & TCP_FLAG_FIN)
366     {
367       ses->i2o_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
368       ses->state |= NAT44_SES_I2O_FIN;
369     }
370   if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
371     {
372       if (clib_net_to_host_u32 (tcp_ack_number) > ses->o2i_fin_seq)
373         {
374           ses->state |= NAT44_SES_O2I_FIN_ACK;
375           if (nat44_is_ses_closed (ses))
376             {                   // if session is now closed, save the timestamp
377               ses->tcp_close_timestamp = now + sm->tcp_transitory_timeout;
378             }
379         }
380     }
381   return 0;
382 }
383
384 always_inline int
385 nat44_set_tcp_session_state_o2i (snat_main_t * sm, f64 now,
386                                  snat_session_t * ses, u8 tcp_flags,
387                                  u32 tcp_ack_number, u32 tcp_seq_number,
388                                  u32 thread_index)
389 {
390   if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
391     ses->state = NAT44_SES_RST;
392   if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
393     ses->state = 0;
394   if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
395       (ses->state & NAT44_SES_O2I_SYN))
396     ses->state = 0;
397   if (tcp_flags & TCP_FLAG_SYN)
398     ses->state |= NAT44_SES_O2I_SYN;
399   if (tcp_flags & TCP_FLAG_FIN)
400     {
401       ses->o2i_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
402       ses->state |= NAT44_SES_O2I_FIN;
403     }
404   if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
405     {
406       if (clib_net_to_host_u32 (tcp_ack_number) > ses->i2o_fin_seq)
407         ses->state |= NAT44_SES_I2O_FIN_ACK;
408       if (nat44_is_ses_closed (ses))
409         {                       // if session is now closed, save the timestamp
410           ses->tcp_close_timestamp = now + sm->tcp_transitory_timeout;
411         }
412     }
413   return 0;
414 }
415
416 always_inline u32
417 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
418 {
419   switch (s->in2out.protocol)
420     {
421     case SNAT_PROTOCOL_ICMP:
422       return sm->icmp_timeout;
423     case SNAT_PROTOCOL_UDP:
424       return sm->udp_timeout;
425     case SNAT_PROTOCOL_TCP:
426       {
427         if (s->state)
428           return sm->tcp_transitory_timeout;
429         else
430           return sm->tcp_established_timeout;
431       }
432     default:
433       return sm->udp_timeout;
434     }
435
436   return 0;
437 }
438
439 always_inline void
440 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes,
441                                u32 thread_index)
442 {
443   s->last_heard = now;
444   s->total_pkts++;
445   s->total_bytes += bytes;
446   nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
447                s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
448                s->total_pkts, s->total_bytes, thread_index,
449                &s->ha_last_refreshed, now);
450 }
451
452 /** \brief Per-user LRU list maintenance */
453 always_inline void
454 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
455                           u32 thread_index)
456 {
457   /* don't update too often - timeout is in a magnitude of seconds anyway */
458   if (s->last_heard > s->last_lru_update + 1)
459     {
460       if (!sm->endpoint_dependent)
461         {
462           clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
463                              s->per_user_index);
464           clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
465                               s->per_user_list_head_index, s->per_user_index);
466         }
467
468       clib_dlist_remove (sm->per_thread_data[thread_index].global_lru_pool,
469                          s->global_lru_index);
470       clib_dlist_addtail (sm->per_thread_data[thread_index].global_lru_pool,
471                           sm->
472                           per_thread_data[thread_index].global_lru_head_index,
473                           s->global_lru_index);
474       s->last_lru_update = s->last_heard;
475     }
476 }
477
478 always_inline void
479 make_ed_kv (ip4_address_t * l_addr, ip4_address_t * r_addr, u8 proto,
480             u32 fib_index, u16 l_port, u16 r_port, u64 value,
481             clib_bihash_kv_16_8_t * kv)
482 {
483   kv->key[0] = (u64) r_addr->as_u32 << 32 | l_addr->as_u32;
484   kv->key[1] =
485     (u64) r_port << 48 | (u64) l_port << 32 | fib_index << 8 | proto;
486   kv->value = value;
487 }
488
489 always_inline void
490 split_ed_kv (clib_bihash_kv_16_8_t * kv,
491              ip4_address_t * l_addr, ip4_address_t * r_addr, u8 * proto,
492              u32 * fib_index, u16 * l_port, u16 * r_port)
493 {
494   if (l_addr)
495     {
496       l_addr->as_u32 = kv->key[0] & (u32) ~ 0;
497     }
498   if (r_addr)
499     {
500       r_addr->as_u32 = kv->key[0] >> 32;
501     }
502   if (r_port)
503     {
504       *r_port = kv->key[1] >> 48;
505     }
506   if (l_port)
507     {
508       *l_port = (kv->key[1] >> 32) & (u16) ~ 0;
509     }
510   if (fib_index)
511     {
512       *fib_index = (kv->key[1] >> 8) & ((1 << 24) - 1);
513     }
514   if (proto)
515     {
516       *proto = kv->key[1] & (u8) ~ 0;
517     }
518 }
519
520 always_inline void
521 make_sm_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t * addr, u8 proto,
522             u32 fib_index, u16 port)
523 {
524   kv->key = (u64) fib_index << 51 | (u64) proto << 48 | (u64) port << 32 |
525     addr->as_u32;
526
527   kv->value = ~0ULL;
528 }
529
530 static_always_inline int
531 get_icmp_i2o_ed_key (vlib_buffer_t * b, ip4_header_t * ip0, u32 rx_fib_index,
532                      u64 value, u8 * snat_proto, u16 * l_port, u16 * r_port,
533                      clib_bihash_kv_16_8_t * kv)
534 {
535   u8 proto;
536   u16 _l_port, _r_port;
537   ip4_address_t *l_addr, *r_addr;
538
539   icmp46_header_t *icmp0;
540   icmp_echo_header_t *echo0, *inner_echo0 = 0;
541   ip4_header_t *inner_ip0 = 0;
542   void *l4_header = 0;
543   icmp46_header_t *inner_icmp0;
544
545   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
546   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
547
548   if (!icmp_type_is_error_message
549       (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
550     {
551       proto = IP_PROTOCOL_ICMP;
552       l_addr = &ip0->src_address;
553       r_addr = &ip0->dst_address;
554       _l_port = vnet_buffer (b)->ip.reass.l4_src_port;  // TODO should this be src or dst?
555       _r_port = 0;
556     }
557   else
558     {
559       inner_ip0 = (ip4_header_t *) (echo0 + 1);
560       l4_header = ip4_next_header (inner_ip0);
561       proto = inner_ip0->protocol;
562       r_addr = &inner_ip0->src_address;
563       l_addr = &inner_ip0->dst_address;
564       switch (ip_proto_to_snat_proto (inner_ip0->protocol))
565         {
566         case SNAT_PROTOCOL_ICMP:
567           inner_icmp0 = (icmp46_header_t *) l4_header;
568           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
569           _r_port = 0;
570           _l_port = inner_echo0->identifier;
571           break;
572         case SNAT_PROTOCOL_UDP:
573         case SNAT_PROTOCOL_TCP:
574           _l_port = ((tcp_udp_header_t *) l4_header)->dst_port;
575           _r_port = ((tcp_udp_header_t *) l4_header)->src_port;
576           break;
577         default:
578           return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
579         }
580     }
581   make_ed_kv (l_addr, r_addr, proto, rx_fib_index, _l_port, _r_port, value,
582               kv);
583   if (snat_proto)
584     {
585       *snat_proto = ip_proto_to_snat_proto (proto);
586     }
587   if (l_port)
588     {
589       *l_port = _l_port;
590     }
591   if (r_port)
592     {
593       *r_port = _r_port;
594     }
595   return 0;
596 }
597
598
599 static_always_inline int
600 get_icmp_o2i_ed_key (vlib_buffer_t * b, ip4_header_t * ip0, u32 rx_fib_index,
601                      u64 value, u8 * snat_proto, u16 * l_port, u16 * r_port,
602                      clib_bihash_kv_16_8_t * kv)
603 {
604   icmp46_header_t *icmp0;
605   u8 proto;
606   ip4_address_t *l_addr, *r_addr;
607   u16 _l_port, _r_port;
608   icmp_echo_header_t *echo0, *inner_echo0 = 0;
609   ip4_header_t *inner_ip0;
610   void *l4_header = 0;
611   icmp46_header_t *inner_icmp0;
612
613   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
614   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
615
616   if (!icmp_type_is_error_message
617       (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
618     {
619       proto = IP_PROTOCOL_ICMP;
620       l_addr = &ip0->dst_address;
621       r_addr = &ip0->src_address;
622       _l_port = vnet_buffer (b)->ip.reass.l4_src_port;  // TODO should this be src or dst?
623       _r_port = 0;
624     }
625   else
626     {
627       inner_ip0 = (ip4_header_t *) (echo0 + 1);
628       l4_header = ip4_next_header (inner_ip0);
629       proto = inner_ip0->protocol;
630       l_addr = &inner_ip0->src_address;
631       r_addr = &inner_ip0->dst_address;
632       switch (ip_proto_to_snat_proto (inner_ip0->protocol))
633         {
634         case SNAT_PROTOCOL_ICMP:
635           inner_icmp0 = (icmp46_header_t *) l4_header;
636           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
637           _l_port = inner_echo0->identifier;
638           _r_port = 0;
639           break;
640         case SNAT_PROTOCOL_UDP:
641         case SNAT_PROTOCOL_TCP:
642           _l_port = ((tcp_udp_header_t *) l4_header)->src_port;
643           _r_port = ((tcp_udp_header_t *) l4_header)->dst_port;
644           break;
645         default:
646           return -1;
647         }
648     }
649   make_ed_kv (l_addr, r_addr, proto, rx_fib_index, _l_port, _r_port, value,
650               kv);
651   if (snat_proto)
652     {
653       *snat_proto = ip_proto_to_snat_proto (proto);
654     }
655   if (l_port)
656     {
657       *l_port = _l_port;
658     }
659   if (r_port)
660     {
661       *r_port = _r_port;
662     }
663   return 0;
664 }
665
666 always_inline void
667 mss_clamping (snat_main_t * sm, tcp_header_t * tcp, ip_csum_t * sum)
668 {
669   u8 *data;
670   u8 opt_len, opts_len, kind;
671   u16 mss;
672
673   if (!(sm->mss_clamping && tcp_syn (tcp)))
674     return;
675
676   opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
677   data = (u8 *) (tcp + 1);
678   for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
679     {
680       kind = data[0];
681
682       if (kind == TCP_OPTION_EOL)
683         break;
684       else if (kind == TCP_OPTION_NOOP)
685         {
686           opt_len = 1;
687           continue;
688         }
689       else
690         {
691           if (opts_len < 2)
692             return;
693           opt_len = data[1];
694
695           if (opt_len < 2 || opt_len > opts_len)
696             return;
697         }
698
699       if (kind == TCP_OPTION_MSS)
700         {
701           mss = *(u16 *) (data + 2);
702           if (clib_net_to_host_u16 (mss) > sm->mss_clamping)
703             {
704               *sum =
705                 ip_csum_update (*sum, mss, sm->mss_value_net, ip4_header_t,
706                                 length);
707               clib_memcpy_fast (data + 2, &sm->mss_value_net, 2);
708             }
709           return;
710         }
711     }
712 }
713
714 /**
715  * @brief Check if packet should be translated
716  *
717  * Packets aimed at outside interface and external address with active session
718  * should be translated.
719  *
720  * @param sm            NAT main
721  * @param rt            NAT runtime data
722  * @param sw_if_index0  index of the inside interface
723  * @param ip0           IPv4 header
724  * @param proto0        NAT protocol
725  * @param rx_fib_index0 RX FIB index
726  *
727  * @returns 0 if packet should be translated otherwise 1
728  */
729 static inline int
730 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t * node,
731                          u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
732                          u32 rx_fib_index0)
733 {
734   if (sm->out2in_dpo)
735     return 0;
736
737   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
738   nat_outside_fib_t *outside_fib;
739   fib_prefix_t pfx = {
740     .fp_proto = FIB_PROTOCOL_IP4,
741     .fp_len = 32,
742     .fp_addr = {
743                 .ip4.as_u32 = ip0->dst_address.as_u32,
744                 }
745     ,
746   };
747
748   /* Don't NAT packet aimed at the intfc address */
749   if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
750                                         ip0->dst_address.as_u32)))
751     return 1;
752
753   fei = fib_table_lookup (rx_fib_index0, &pfx);
754   if (FIB_NODE_INDEX_INVALID != fei)
755     {
756       u32 sw_if_index = fib_entry_get_resolving_interface (fei);
757       if (sw_if_index == ~0)
758         {
759           vec_foreach (outside_fib, sm->outside_fibs)
760           {
761             fei = fib_table_lookup (outside_fib->fib_index, &pfx);
762             if (FIB_NODE_INDEX_INVALID != fei)
763               {
764                 sw_if_index = fib_entry_get_resolving_interface (fei);
765                 if (sw_if_index != ~0)
766                   break;
767               }
768           }
769         }
770       if (sw_if_index == ~0)
771         return 1;
772
773       snat_interface_t *i;
774       /* *INDENT-OFF* */
775       pool_foreach (i, sm->interfaces, ({
776         /* NAT packet aimed at outside interface */
777         if ((nat_interface_is_outside (i)) && (sw_if_index == i->sw_if_index))
778           return 0;
779       }));
780       /* *INDENT-ON* */
781     }
782
783   return 1;
784 }
785
786 static inline void
787 increment_v4_address (ip4_address_t * a)
788 {
789   u32 v;
790
791   v = clib_net_to_host_u32 (a->as_u32) + 1;
792   a->as_u32 = clib_host_to_net_u32 (v);
793 }
794
795 #endif /* __included_nat_inlines_h__ */
796
797 /*
798  * fd.io coding-style-patch-verification: ON
799  *
800  * Local Variables:
801  * eval: (c-set-style "gnu")
802  * End:
803  */