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