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