virtio: enable the interrupt support for uio_pci_generic
[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/nat44-ei/nat44_ei_ha.h>
25
26 always_inline u64
27 calc_nat_key (ip4_address_t addr, u16 port, u32 fib_index, u8 proto)
28 {
29   ASSERT (fib_index <= (1 << 14) - 1);
30   ASSERT (proto <= (1 << 3) - 1);
31   return (u64) addr.as_u32 << 32 | (u64) port << 16 | fib_index << 3 |
32          (proto & 0x7);
33 }
34
35 always_inline void
36 split_nat_key (u64 key, ip4_address_t * addr, u16 * port,
37                u32 * fib_index, nat_protocol_t * proto)
38 {
39   if (addr)
40     {
41       addr->as_u32 = key >> 32;
42     }
43   if (port)
44     {
45       *port = (key >> 16) & (u16) ~ 0;
46     }
47   if (fib_index)
48     {
49       *fib_index = key >> 3 & ((1 << 13) - 1);
50     }
51   if (proto)
52     {
53       *proto = key & 0x7;
54     }
55 }
56
57 always_inline void
58 init_nat_k (clib_bihash_kv_8_8_t * kv, ip4_address_t addr, u16 port,
59             u32 fib_index, nat_protocol_t proto)
60 {
61   kv->key = calc_nat_key (addr, port, fib_index, proto);
62   kv->value = ~0ULL;
63 }
64
65 always_inline void
66 init_nat_kv (clib_bihash_kv_8_8_t *kv, ip4_address_t addr, u16 port,
67              u32 fib_index, nat_protocol_t proto, u32 thread_index,
68              u32 session_index)
69 {
70   init_nat_k (kv, addr, port, fib_index, proto);
71   kv->value = (u64) thread_index << 32 | session_index;
72 }
73
74 always_inline void
75 init_nat_i2o_k (clib_bihash_kv_8_8_t * kv, snat_session_t * s)
76 {
77   return init_nat_k (kv, s->in2out.addr, s->in2out.port, s->in2out.fib_index,
78                      s->nat_proto);
79 }
80
81 always_inline void
82 init_nat_i2o_kv (clib_bihash_kv_8_8_t *kv, snat_session_t *s, u32 thread_index,
83                  u32 session_index)
84 {
85   init_nat_k (kv, s->in2out.addr, s->in2out.port, s->in2out.fib_index,
86               s->nat_proto);
87   kv->value = (u64) thread_index << 32 | session_index;
88 }
89
90 always_inline void
91 init_nat_o2i_k (clib_bihash_kv_8_8_t * kv, snat_session_t * s)
92 {
93   return init_nat_k (kv, s->out2in.addr, s->out2in.port, s->out2in.fib_index,
94                      s->nat_proto);
95 }
96
97 always_inline void
98 init_nat_o2i_kv (clib_bihash_kv_8_8_t *kv, snat_session_t *s, u32 thread_index,
99                  u32 session_index)
100 {
101   init_nat_k (kv, s->out2in.addr, s->out2in.port, s->out2in.fib_index,
102               s->nat_proto);
103   kv->value = (u64) thread_index << 32 | session_index;
104 }
105
106 always_inline u32
107 nat_value_get_thread_index (clib_bihash_kv_8_8_t *value)
108 {
109   return value->value >> 32;
110 }
111
112 always_inline u32
113 nat_value_get_session_index (clib_bihash_kv_8_8_t *value)
114 {
115   return value->value & ~(u32) 0;
116 }
117
118 static inline uword
119 nat_pre_node_fn_inline (vlib_main_t * vm,
120                         vlib_node_runtime_t * node,
121                         vlib_frame_t * frame, u32 def_next)
122 {
123   u32 n_left_from, *from;
124
125   from = vlib_frame_vector_args (frame);
126   n_left_from = frame->n_vectors;
127
128   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
129   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
130   vlib_get_buffers (vm, from, b, n_left_from);
131
132   while (n_left_from >= 2)
133     {
134       u32 next0, next1;
135       u32 arc_next0, arc_next1;
136       vlib_buffer_t *b0, *b1;
137
138       b0 = *b;
139       b++;
140       b1 = *b;
141       b++;
142
143       /* Prefetch next iteration. */
144       if (PREDICT_TRUE (n_left_from >= 4))
145         {
146           vlib_buffer_t *p2, *p3;
147
148           p2 = *b;
149           p3 = *(b + 1);
150
151           vlib_prefetch_buffer_header (p2, LOAD);
152           vlib_prefetch_buffer_header (p3, LOAD);
153
154           CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
155           CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
156         }
157
158       next0 = def_next;
159       next1 = def_next;
160
161       vnet_feature_next (&arc_next0, b0);
162       vnet_feature_next (&arc_next1, b1);
163
164       vnet_buffer2 (b0)->nat.arc_next = arc_next0;
165       vnet_buffer2 (b1)->nat.arc_next = arc_next1;
166
167       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
168         {
169           if (b0->flags & VLIB_BUFFER_IS_TRACED)
170             {
171               nat_pre_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
172               t->next_index = next0;
173               t->arc_next_index = arc_next0;
174             }
175           if (b1->flags & VLIB_BUFFER_IS_TRACED)
176             {
177               nat_pre_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
178               t->next_index = next1;
179               t->arc_next_index = arc_next1;
180             }
181         }
182
183       n_left_from -= 2;
184       next[0] = next0;
185       next[1] = next1;
186       next += 2;
187     }
188
189   while (n_left_from > 0)
190     {
191       u32 next0;
192       u32 arc_next0;
193       vlib_buffer_t *b0;
194
195       b0 = *b;
196       b++;
197
198       next0 = def_next;
199       vnet_feature_next (&arc_next0, b0);
200       vnet_buffer2 (b0)->nat.arc_next = arc_next0;
201
202       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
203                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
204         {
205           nat_pre_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
206           t->next_index = next0;
207           t->arc_next_index = arc_next0;
208         }
209
210       n_left_from--;
211       next[0] = next0;
212       next++;
213     }
214   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
215                                frame->n_vectors);
216
217   return frame->n_vectors;
218 }
219
220 always_inline u8
221 is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node,
222                    u32 sw_if_index0, u32 ip4_addr)
223 {
224   snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
225   ip4_address_t *first_int_addr;
226
227   if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
228     {
229       first_int_addr =
230         ip4_interface_first_address (sm->ip4_main, sw_if_index0,
231                                      0 /* just want the address */ );
232       rt->cached_sw_if_index = sw_if_index0;
233       if (first_int_addr)
234         rt->cached_ip4_address = first_int_addr->as_u32;
235       else
236         rt->cached_ip4_address = 0;
237     }
238
239   if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
240     return 1;
241   else
242     return 0;
243 }
244
245 always_inline void
246 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
247 {
248   if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
249     {
250       if (is_static)
251         u->nstaticsessions++;
252       else
253         u->nsessions++;
254     }
255 }
256
257 always_inline void
258 nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u,
259                                    u32 thread_index)
260 {
261   clib_bihash_kv_8_8_t kv;
262   snat_user_key_t u_key;
263   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
264                                                        thread_index);
265
266   if (u->nstaticsessions == 0 && u->nsessions == 0)
267     {
268       u_key.addr.as_u32 = u->addr.as_u32;
269       u_key.fib_index = u->fib_index;
270       kv.key = u_key.as_u64;
271       pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index);
272       pool_put (tsm->users, u);
273       clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
274       vlib_set_simple_counter (&sm->total_users, thread_index, 0,
275                                pool_elts (tsm->users));
276     }
277 }
278
279 always_inline void
280 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
281                       u32 thread_index)
282 {
283   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
284                                                        thread_index);
285   clib_bihash_kv_8_8_t kv, value;
286   snat_user_t *u;
287   const snat_user_key_t u_key = {
288     .addr = ses->in2out.addr,
289     .fib_index = ses->in2out.fib_index
290   };
291   const u8 u_static = snat_is_session_static (ses);
292
293   clib_dlist_remove (tsm->list_pool, ses->per_user_index);
294   pool_put_index (tsm->list_pool, ses->per_user_index);
295   if (sm->endpoint_dependent)
296     {
297       clib_dlist_remove (tsm->lru_pool, ses->lru_index);
298       pool_put_index (tsm->lru_pool, ses->lru_index);
299     }
300   pool_put (tsm->sessions, ses);
301   vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
302                            pool_elts (tsm->sessions));
303
304   kv.key = u_key.as_u64;
305   if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
306     {
307       u = pool_elt_at_index (tsm->users, value.value);
308       if (u_static)
309         u->nstaticsessions--;
310       else
311         u->nsessions--;
312
313       nat44_delete_user_with_no_session (sm, u, thread_index);
314     }
315 }
316
317 always_inline void
318 nat44_set_tcp_session_state_i2o (snat_main_t * sm, f64 now,
319                                  snat_session_t * ses, vlib_buffer_t * b,
320                                  u32 thread_index)
321 {
322   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
323   u8 tcp_flags = vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags;
324   u32 tcp_ack_number = vnet_buffer (b)->ip.reass.tcp_ack_number;
325   u32 tcp_seq_number = vnet_buffer (b)->ip.reass.tcp_seq_number;
326   if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
327     ses->state = NAT44_SES_RST;
328   if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
329     ses->state = 0;
330   if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
331       (ses->state & NAT44_SES_O2I_SYN))
332     ses->state = 0;
333   if (tcp_flags & TCP_FLAG_SYN)
334     ses->state |= NAT44_SES_I2O_SYN;
335   if (tcp_flags & TCP_FLAG_FIN)
336     {
337       ses->i2o_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
338       ses->state |= NAT44_SES_I2O_FIN;
339     }
340   if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
341     {
342       if (clib_net_to_host_u32 (tcp_ack_number) > ses->o2i_fin_seq)
343         {
344           ses->state |= NAT44_SES_O2I_FIN_ACK;
345           if (nat44_is_ses_closed (ses))
346             {                   // if session is now closed, save the timestamp
347               ses->tcp_closed_timestamp = now + sm->timeouts.tcp.transitory;
348               ses->last_lru_update = now;
349             }
350         }
351     }
352
353   // move the session to proper LRU
354   if (ses->state)
355     {
356       ses->lru_head_index = tsm->tcp_trans_lru_head_index;
357     }
358   else
359     {
360       ses->lru_head_index = tsm->tcp_estab_lru_head_index;
361     }
362   clib_dlist_remove (tsm->lru_pool, ses->lru_index);
363   clib_dlist_addtail (tsm->lru_pool, ses->lru_head_index, ses->lru_index);
364 }
365
366 always_inline void
367 nat44_set_tcp_session_state_o2i (snat_main_t * sm, f64 now,
368                                  snat_session_t * ses, u8 tcp_flags,
369                                  u32 tcp_ack_number, u32 tcp_seq_number,
370                                  u32 thread_index)
371 {
372   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
373   if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
374     ses->state = NAT44_SES_RST;
375   if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
376     ses->state = 0;
377   if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
378       (ses->state & NAT44_SES_O2I_SYN))
379     ses->state = 0;
380   if (tcp_flags & TCP_FLAG_SYN)
381     ses->state |= NAT44_SES_O2I_SYN;
382   if (tcp_flags & TCP_FLAG_FIN)
383     {
384       ses->o2i_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
385       ses->state |= NAT44_SES_O2I_FIN;
386     }
387   if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
388     {
389       if (clib_net_to_host_u32 (tcp_ack_number) > ses->i2o_fin_seq)
390         ses->state |= NAT44_SES_I2O_FIN_ACK;
391       if (nat44_is_ses_closed (ses))
392         {                       // if session is now closed, save the timestamp
393           ses->tcp_closed_timestamp = now + sm->timeouts.tcp.transitory;
394           ses->last_lru_update = now;
395         }
396     }
397   // move the session to proper LRU
398   if (ses->state)
399     {
400       ses->lru_head_index = tsm->tcp_trans_lru_head_index;
401     }
402   else
403     {
404       ses->lru_head_index = tsm->tcp_estab_lru_head_index;
405     }
406   clib_dlist_remove (tsm->lru_pool, ses->lru_index);
407   clib_dlist_addtail (tsm->lru_pool, ses->lru_head_index, ses->lru_index);
408 }
409
410 always_inline u32
411 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
412 {
413   switch (s->nat_proto)
414     {
415     case NAT_PROTOCOL_ICMP:
416       return sm->timeouts.icmp;
417     case NAT_PROTOCOL_UDP:
418       return sm->timeouts.udp;
419     case NAT_PROTOCOL_TCP:
420       {
421         if (s->state)
422           return sm->timeouts.tcp.transitory;
423         else
424           return sm->timeouts.tcp.established;
425       }
426     default:
427       return sm->timeouts.udp;
428     }
429
430   return 0;
431 }
432
433 always_inline void
434 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes,
435                                u32 thread_index)
436 {
437   s->last_heard = now;
438   s->total_pkts++;
439   s->total_bytes += bytes;
440 #if 0
441   nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
442                s->ext_host_port, s->nat_proto, s->out2in.fib_index,
443                s->total_pkts, s->total_bytes, thread_index,
444                &s->ha_last_refreshed, now);
445 #endif
446 }
447
448 /** \brief Per-user LRU list maintenance */
449 always_inline void
450 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
451                           u32 thread_index)
452 {
453   /* don't update too often - timeout is in magnitude of seconds anyway */
454   if (s->last_heard > s->last_lru_update + 1)
455     {
456       if (!sm->endpoint_dependent)
457         {
458           clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
459                              s->per_user_index);
460           clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
461                               s->per_user_list_head_index, s->per_user_index);
462         }
463       else
464         {
465           clib_dlist_remove (sm->per_thread_data[thread_index].lru_pool,
466                              s->lru_index);
467           clib_dlist_addtail (sm->per_thread_data[thread_index].lru_pool,
468                               s->lru_head_index, s->lru_index);
469         }
470       s->last_lru_update = s->last_heard;
471     }
472 }
473
474 always_inline void
475 init_ed_k (clib_bihash_kv_16_8_t * kv, ip4_address_t l_addr, u16 l_port,
476            ip4_address_t r_addr, u16 r_port, u32 fib_index, u8 proto)
477 {
478   kv->key[0] = (u64) r_addr.as_u32 << 32 | l_addr.as_u32;
479   kv->key[1] =
480     (u64) r_port << 48 | (u64) l_port << 32 | fib_index << 8 | proto;
481 }
482
483 always_inline void
484 init_ed_kv (clib_bihash_kv_16_8_t * kv, ip4_address_t l_addr, u16 l_port,
485             ip4_address_t r_addr, u16 r_port, u32 fib_index, u8 proto,
486             u32 thread_index, u32 session_index)
487 {
488   init_ed_k (kv, l_addr, l_port, r_addr, r_port, fib_index, proto);
489   kv->value = (u64) thread_index << 32 | session_index;
490 }
491
492 always_inline u32
493 ed_value_get_thread_index (clib_bihash_kv_16_8_t * value)
494 {
495   return value->value >> 32;
496 }
497
498 always_inline u32
499 ed_value_get_session_index (clib_bihash_kv_16_8_t * value)
500 {
501   return value->value & ~(u32) 0;
502 }
503
504 always_inline void
505 split_ed_kv (clib_bihash_kv_16_8_t * kv,
506              ip4_address_t * l_addr, ip4_address_t * r_addr, u8 * proto,
507              u32 * fib_index, u16 * l_port, u16 * r_port)
508 {
509   if (l_addr)
510     {
511       l_addr->as_u32 = kv->key[0] & (u32) ~ 0;
512     }
513   if (r_addr)
514     {
515       r_addr->as_u32 = kv->key[0] >> 32;
516     }
517   if (r_port)
518     {
519       *r_port = kv->key[1] >> 48;
520     }
521   if (l_port)
522     {
523       *l_port = (kv->key[1] >> 32) & (u16) ~ 0;
524     }
525   if (fib_index)
526     {
527       *fib_index = (kv->key[1] >> 8) & ((1 << 24) - 1);
528     }
529   if (proto)
530     {
531       *proto = kv->key[1] & (u8) ~ 0;
532     }
533 }
534
535 static_always_inline int
536 nat_get_icmp_session_lookup_values (vlib_buffer_t *b, ip4_header_t *ip0,
537                                     ip4_address_t *lookup_saddr,
538                                     u16 *lookup_sport,
539                                     ip4_address_t *lookup_daddr,
540                                     u16 *lookup_dport, u8 *lookup_protocol)
541 {
542   icmp46_header_t *icmp0;
543   icmp_echo_header_t *echo0, *inner_echo0 = 0;
544   ip4_header_t *inner_ip0 = 0;
545   void *l4_header = 0;
546   icmp46_header_t *inner_icmp0;
547
548   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
549   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
550
551   // avoid warning about unused variables in caller by setting to bogus values
552   *lookup_sport = 0;
553   *lookup_dport = 0;
554
555   if (!icmp_type_is_error_message
556       (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
557     {
558       *lookup_protocol = IP_PROTOCOL_ICMP;
559       lookup_saddr->as_u32 = ip0->src_address.as_u32;
560       *lookup_sport = vnet_buffer (b)->ip.reass.l4_src_port;
561       lookup_daddr->as_u32 = ip0->dst_address.as_u32;
562       *lookup_dport = vnet_buffer (b)->ip.reass.l4_dst_port;
563     }
564   else
565     {
566       inner_ip0 = (ip4_header_t *) (echo0 + 1);
567       l4_header = ip4_next_header (inner_ip0);
568       *lookup_protocol = inner_ip0->protocol;
569       lookup_saddr->as_u32 = inner_ip0->dst_address.as_u32;
570       lookup_daddr->as_u32 = inner_ip0->src_address.as_u32;
571       switch (ip_proto_to_nat_proto (inner_ip0->protocol))
572         {
573         case NAT_PROTOCOL_ICMP:
574           inner_icmp0 = (icmp46_header_t *) l4_header;
575           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
576           *lookup_sport = inner_echo0->identifier;
577           *lookup_dport = inner_echo0->identifier;
578           break;
579         case NAT_PROTOCOL_UDP:
580         case NAT_PROTOCOL_TCP:
581           *lookup_sport = ((tcp_udp_header_t *) l4_header)->dst_port;
582           *lookup_dport = ((tcp_udp_header_t *) l4_header)->src_port;
583           break;
584         default:
585           return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
586         }
587     }
588   return 0;
589 }
590
591 /**
592  * @brief Check if packet should be translated
593  *
594  * Packets aimed at outside interface and external address with active session
595  * should be translated.
596  *
597  * @param sm            NAT main
598  * @param rt            NAT runtime data
599  * @param sw_if_index0  index of the inside interface
600  * @param ip0           IPv4 header
601  * @param proto0        NAT protocol
602  * @param rx_fib_index0 RX FIB index
603  *
604  * @returns 0 if packet should be translated otherwise 1
605  */
606 static inline int
607 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t * node,
608                          u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
609                          u32 rx_fib_index0)
610 {
611   if (sm->out2in_dpo)
612     return 0;
613
614   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
615   nat_outside_fib_t *outside_fib;
616   fib_prefix_t pfx = {
617     .fp_proto = FIB_PROTOCOL_IP4,
618     .fp_len = 32,
619     .fp_addr = {
620                 .ip4.as_u32 = ip0->dst_address.as_u32,
621                 }
622     ,
623   };
624
625   /* Don't NAT packet aimed at the intfc address */
626   if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
627                                         ip0->dst_address.as_u32)))
628     return 1;
629
630   fei = fib_table_lookup (rx_fib_index0, &pfx);
631   if (FIB_NODE_INDEX_INVALID != fei)
632     {
633       u32 sw_if_index = fib_entry_get_resolving_interface (fei);
634       if (sw_if_index == ~0)
635         {
636           vec_foreach (outside_fib, sm->outside_fibs)
637           {
638             fei = fib_table_lookup (outside_fib->fib_index, &pfx);
639             if (FIB_NODE_INDEX_INVALID != fei)
640               {
641                 sw_if_index = fib_entry_get_resolving_interface (fei);
642                 if (sw_if_index != ~0)
643                   break;
644               }
645           }
646         }
647       if (sw_if_index == ~0)
648         return 1;
649
650       snat_interface_t *i;
651       /* *INDENT-OFF* */
652       pool_foreach (i, sm->interfaces)  {
653         /* NAT packet aimed at outside interface */
654         if ((nat_interface_is_outside (i)) && (sw_if_index == i->sw_if_index))
655           return 0;
656       }
657       /* *INDENT-ON* */
658     }
659
660   return 1;
661 }
662
663 static_always_inline u16
664 snat_random_port (u16 min, u16 max)
665 {
666   snat_main_t *sm = &snat_main;
667   u32 rwide;
668   u16 r;
669
670   rwide = random_u32 (&sm->random_seed);
671   r = rwide & 0xFFFF;
672   if (r >= min && r <= max)
673     return r;
674
675   return min + (rwide % (max - min + 1));
676 }
677
678 #endif /* __included_nat_inlines_h__ */
679
680 /*
681  * fd.io coding-style-patch-verification: ON
682  *
683  * Local Variables:
684  * eval: (c-set-style "gnu")
685  * End:
686  */