nat: fix coverity warning
[vpp.git] / src / plugins / nat / nat44_hairpinning.c
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  * @file
17  * @brief NAT44 hairpinning
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <nat/nat.h>
24 #include <nat/nat_inlines.h>
25
26 typedef enum
27 {
28   SNAT_HAIRPIN_SRC_NEXT_DROP,
29   SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT,
30   SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH,
31   SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT,
32   SNAT_HAIRPIN_SRC_N_NEXT,
33 } snat_hairpin_src_next_t;
34
35 typedef enum
36 {
37   NAT_HAIRPIN_NEXT_LOOKUP,
38   NAT_HAIRPIN_NEXT_DROP,
39   NAT_HAIRPIN_N_NEXT,
40 } nat_hairpin_next_t;
41
42 #define foreach_nat44_hairpin_error                       \
43 _(PROCESSED, "NAT44 hairpinning packets processed")
44
45 typedef enum
46 {
47 #define _(sym,str) NAT44_HAIRPIN_ERROR_##sym,
48   foreach_nat44_hairpin_error
49 #undef _
50     NAT44_HAIRPIN_N_ERROR,
51 } nat44_hairpin_error_t;
52
53 static char *nat44_hairpin_error_strings[] = {
54 #define _(sym,string) string,
55   foreach_nat44_hairpin_error
56 #undef _
57 };
58
59 typedef struct
60 {
61   ip4_address_t addr;
62   u16 port;
63   u32 fib_index;
64   u32 session_index;
65 } nat_hairpin_trace_t;
66
67 static u8 *
68 format_nat_hairpin_trace (u8 * s, va_list * args)
69 {
70   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
71   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
72   nat_hairpin_trace_t *t = va_arg (*args, nat_hairpin_trace_t *);
73
74   s =
75     format (s, "new dst addr %U port %u fib-index %u", format_ip4_address,
76             &t->addr, clib_net_to_host_u16 (t->port), t->fib_index);
77   if (~0 == t->session_index)
78     {
79       s = format (s, " is-static-mapping");
80     }
81   else
82     {
83       s = format (s, " session-index %u", t->session_index);
84     }
85
86   return s;
87 }
88
89 extern vnet_feature_arc_registration_t vnet_feat_arc_ip4_local;
90
91 static_always_inline int
92 is_hairpinning (snat_main_t * sm, ip4_address_t * dst_addr)
93 {
94   snat_address_t *ap;
95   clib_bihash_kv_8_8_t kv, value;
96   snat_session_key_t m_key;
97
98   /* *INDENT-OFF* */
99   vec_foreach (ap, sm->addresses)
100     {
101       if (ap->addr.as_u32 == dst_addr->as_u32)
102         return 1;
103     }
104   /* *INDENT-ON* */
105
106   m_key.addr.as_u32 = dst_addr->as_u32;
107   m_key.fib_index = 0;
108   m_key.port = 0;
109   m_key.protocol = 0;
110   kv.key = m_key.as_u64;
111   if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
112     return 1;
113
114   return 0;
115 }
116
117 #ifndef CLIB_MARCH_VARIANT
118 int
119 snat_hairpinning (vlib_main_t * vm, vlib_node_runtime_t * node,
120                   snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
121                   udp_header_t * udp0, tcp_header_t * tcp0, u32 proto0,
122                   int is_ed, int do_trace)
123 {
124   snat_session_key_t key0, sm0;
125   snat_session_t *s0 = NULL;
126   clib_bihash_kv_8_8_t kv0, value0;
127   ip_csum_t sum0;
128   u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si = ~0;
129   u16 new_dst_port0 = ~0, old_dst_port0;
130   int rv;
131
132   key0.addr = ip0->dst_address;
133   key0.port = udp0->dst_port;
134   key0.protocol = proto0;
135   key0.fib_index = sm->outside_fib_index;
136   kv0.key = key0.as_u64;
137
138   /* Check if destination is static mappings */
139   if (!snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
140     {
141       new_dst_addr0 = sm0.addr.as_u32;
142       new_dst_port0 = sm0.port;
143       vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
144     }
145   /* or active session */
146   else
147     {
148       if (sm->num_workers > 1)
149         ti =
150           (clib_net_to_host_u16 (udp0->dst_port) -
151            1024) / sm->port_per_thread;
152       else
153         ti = sm->num_workers;
154
155       if (is_ed)
156         {
157           clib_bihash_kv_16_8_t ed_kv, ed_value;
158           make_ed_kv (&ip0->dst_address, &ip0->src_address,
159                       ip0->protocol, sm->outside_fib_index, udp0->dst_port,
160                       udp0->src_port, ~0, ~0, &ed_kv);
161           rv = clib_bihash_search_16_8 (&sm->out2in_ed, &ed_kv, &ed_value);
162           ASSERT (ti == ed_value_get_thread_index (&ed_value));
163           si = ed_value_get_session_index (&ed_value);
164         }
165       else
166         {
167           rv = clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
168                                        &value0);
169           si = value0.value;
170         }
171       if (rv)
172         {
173           rv = 0;
174           goto trace;
175         }
176
177       s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
178       new_dst_addr0 = s0->in2out.addr.as_u32;
179       new_dst_port0 = s0->in2out.port;
180       vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
181     }
182
183   /* Destination is behind the same NAT, use internal address and port */
184   if (new_dst_addr0)
185     {
186       old_dst_addr0 = ip0->dst_address.as_u32;
187       ip0->dst_address.as_u32 = new_dst_addr0;
188       sum0 = ip0->checksum;
189       sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
190                              ip4_header_t, dst_address);
191       ip0->checksum = ip_csum_fold (sum0);
192
193       old_dst_port0 = tcp0->dst;
194       if (PREDICT_TRUE (new_dst_port0 != old_dst_port0))
195         {
196           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
197             {
198               tcp0->dst = new_dst_port0;
199               sum0 = tcp0->checksum;
200               sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
201                                      ip4_header_t, dst_address);
202               sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
203                                      ip4_header_t /* cheat */ , length);
204               tcp0->checksum = ip_csum_fold (sum0);
205             }
206           else
207             {
208               udp0->dst_port = new_dst_port0;
209               udp0->checksum = 0;
210             }
211         }
212       else
213         {
214           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
215             {
216               sum0 = tcp0->checksum;
217               sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
218                                      ip4_header_t, dst_address);
219               tcp0->checksum = ip_csum_fold (sum0);
220             }
221         }
222       rv = 1;
223       goto trace;
224     }
225   rv = 0;
226 trace:
227   if (do_trace && PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
228                                  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
229     {
230       nat_hairpin_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
231       t->addr.as_u32 = new_dst_addr0;
232       t->port = new_dst_port0;
233       t->fib_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
234       if (s0)
235         {
236           t->session_index = si;
237         }
238       else
239         {
240           t->session_index = ~0;
241         }
242     }
243   return rv;
244 }
245 #endif
246
247 #ifndef CLIB_MARCH_VARIANT
248 u32
249 snat_icmp_hairpinning (snat_main_t * sm,
250                        vlib_buffer_t * b0,
251                        ip4_header_t * ip0, icmp46_header_t * icmp0, int is_ed)
252 {
253   snat_session_key_t key0;
254   clib_bihash_kv_8_8_t kv0, value0;
255   u32 old_dst_addr0, new_dst_addr0;
256   u32 old_addr0, new_addr0;
257   u16 old_port0, new_port0;
258   u16 old_checksum0, new_checksum0;
259   u32 si, ti = 0;
260   ip_csum_t sum0;
261   snat_session_t *s0;
262   snat_static_mapping_t *m0;
263
264   if (icmp_type_is_error_message
265       (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
266     {
267       ip4_header_t *inner_ip0 = 0;
268       tcp_udp_header_t *l4_header = 0;
269
270       inner_ip0 = (ip4_header_t *) ((icmp_echo_header_t *) (icmp0 + 1) + 1);
271       l4_header = ip4_next_header (inner_ip0);
272       u32 protocol = ip_proto_to_nat_proto (inner_ip0->protocol);
273
274       if (protocol != NAT_PROTOCOL_TCP && protocol != NAT_PROTOCOL_UDP)
275         return 1;
276
277       if (is_ed)
278         {
279           clib_bihash_kv_16_8_t ed_kv, ed_value;
280           make_ed_kv (&ip0->dst_address, &ip0->src_address,
281                       inner_ip0->protocol, sm->outside_fib_index,
282                       l4_header->src_port, l4_header->dst_port, ~0, ~0,
283                       &ed_kv);
284           if (clib_bihash_search_16_8 (&sm->out2in_ed, &ed_kv, &ed_value))
285             return 1;
286           ASSERT (ti == ed_value_get_thread_index (&ed_value));
287           si = ed_value_get_session_index (&ed_value);
288         }
289       else
290         {
291           key0.addr = ip0->dst_address;
292           key0.port = l4_header->src_port;
293           key0.protocol = protocol;
294           key0.fib_index = sm->outside_fib_index;
295           kv0.key = key0.as_u64;
296           if (clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
297                                       &value0))
298             return 1;
299           si = value0.value;
300         }
301       s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
302       new_dst_addr0 = s0->in2out.addr.as_u32;
303       vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
304
305       /* update inner source IP address */
306       old_addr0 = inner_ip0->src_address.as_u32;
307       inner_ip0->src_address.as_u32 = new_dst_addr0;
308       new_addr0 = inner_ip0->src_address.as_u32;
309       sum0 = icmp0->checksum;
310       sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
311                              src_address);
312       icmp0->checksum = ip_csum_fold (sum0);
313
314       /* update inner IP header checksum */
315       old_checksum0 = inner_ip0->checksum;
316       sum0 = inner_ip0->checksum;
317       sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
318                              src_address);
319       inner_ip0->checksum = ip_csum_fold (sum0);
320       new_checksum0 = inner_ip0->checksum;
321       sum0 = icmp0->checksum;
322       sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
323                              checksum);
324       icmp0->checksum = ip_csum_fold (sum0);
325
326       /* update inner source port */
327       old_port0 = l4_header->src_port;
328       l4_header->src_port = s0->in2out.port;
329       new_port0 = l4_header->src_port;
330       sum0 = icmp0->checksum;
331       sum0 = ip_csum_update (sum0, old_port0, new_port0, tcp_udp_header_t,
332                              src_port);
333       icmp0->checksum = ip_csum_fold (sum0);
334     }
335   else
336     {
337       key0.addr = ip0->dst_address;
338       key0.port = 0;
339       key0.protocol = 0;
340       key0.fib_index = sm->outside_fib_index;
341       kv0.key = key0.as_u64;
342
343       if (clib_bihash_search_8_8
344           (&sm->static_mapping_by_external, &kv0, &value0))
345         {
346           if (!is_ed)
347             {
348               icmp_echo_header_t *echo0 = (icmp_echo_header_t *) (icmp0 + 1);
349               u16 icmp_id0 = echo0->identifier;
350               key0.addr = ip0->dst_address;
351               key0.port = icmp_id0;
352               key0.protocol = NAT_PROTOCOL_ICMP;
353               key0.fib_index = sm->outside_fib_index;
354               kv0.key = key0.as_u64;
355               if (sm->num_workers > 1)
356                 ti =
357                   (clib_net_to_host_u16 (icmp_id0) -
358                    1024) / sm->port_per_thread;
359               else
360                 ti = sm->num_workers;
361               int rv =
362                 clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
363                                         &value0);
364               if (!rv)
365                 {
366                   si = value0.value;
367                   s0 =
368                     pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
369                   new_dst_addr0 = s0->in2out.addr.as_u32;
370                   vnet_buffer (b0)->sw_if_index[VLIB_TX] =
371                     s0->in2out.fib_index;
372                   echo0->identifier = s0->in2out.port;
373                   sum0 = icmp0->checksum;
374                   sum0 = ip_csum_update (sum0, icmp_id0, s0->in2out.port,
375                                          icmp_echo_header_t, identifier);
376                   icmp0->checksum = ip_csum_fold (sum0);
377                   goto change_addr;
378                 }
379             }
380
381           return 1;
382         }
383
384       m0 = pool_elt_at_index (sm->static_mappings, value0.value);
385
386       new_dst_addr0 = m0->local_addr.as_u32;
387       if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
388         vnet_buffer (b0)->sw_if_index[VLIB_TX] = m0->fib_index;
389     }
390 change_addr:
391   /* Destination is behind the same NAT, use internal address and port */
392   if (new_dst_addr0)
393     {
394       old_dst_addr0 = ip0->dst_address.as_u32;
395       ip0->dst_address.as_u32 = new_dst_addr0;
396       sum0 = ip0->checksum;
397       sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
398                              ip4_header_t, dst_address);
399       ip0->checksum = ip_csum_fold (sum0);
400     }
401   return 0;
402 }
403 #endif
404
405 #ifndef CLIB_MARCH_VARIANT
406 void
407 nat_hairpinning_sm_unknown_proto (snat_main_t * sm,
408                                   vlib_buffer_t * b, ip4_header_t * ip)
409 {
410   clib_bihash_kv_8_8_t kv, value;
411   snat_static_mapping_t *m;
412   u32 old_addr, new_addr;
413   ip_csum_t sum;
414
415   make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
416   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
417     return;
418
419   m = pool_elt_at_index (sm->static_mappings, value.value);
420
421   old_addr = ip->dst_address.as_u32;
422   new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
423   sum = ip->checksum;
424   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
425   ip->checksum = ip_csum_fold (sum);
426
427   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
428     vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
429 }
430 #endif
431
432 #ifndef CLIB_MARCH_VARIANT
433 void
434 nat44_ed_hairpinning_unknown_proto (snat_main_t * sm,
435                                     vlib_buffer_t * b, ip4_header_t * ip)
436 {
437   u32 old_addr, new_addr = 0, ti = 0;
438   clib_bihash_kv_8_8_t kv, value;
439   clib_bihash_kv_16_8_t s_kv, s_value;
440   snat_static_mapping_t *m;
441   ip_csum_t sum;
442   snat_session_t *s;
443
444   if (sm->num_workers > 1)
445     ti = sm->worker_out2in_cb (b, ip, sm->outside_fib_index, 0);
446   else
447     ti = sm->num_workers;
448
449   old_addr = ip->dst_address.as_u32;
450   make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol,
451               sm->outside_fib_index, 0, 0, ~0, ~0, &s_kv);
452   if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
453     {
454       make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
455       if (clib_bihash_search_8_8
456           (&sm->static_mapping_by_external, &kv, &value))
457         return;
458
459       m = pool_elt_at_index (sm->static_mappings, value.value);
460       if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
461         vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
462       new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
463     }
464   else
465     {
466       ASSERT (ti == ed_value_get_thread_index (&s_value));
467       s =
468         pool_elt_at_index (sm->per_thread_data[ti].sessions,
469                            ed_value_get_session_index (&s_value));
470       if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
471         vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
472       new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
473     }
474   sum = ip->checksum;
475   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
476   ip->checksum = ip_csum_fold (sum);
477 }
478 #endif
479
480 static inline uword
481 nat44_hairpinning_fn_inline (vlib_main_t * vm,
482                              vlib_node_runtime_t * node,
483                              vlib_frame_t * frame, int is_ed)
484 {
485   u32 n_left_from, *from, *to_next, stats_node_index;
486   nat_hairpin_next_t next_index;
487   u32 pkts_processed = 0;
488   snat_main_t *sm = &snat_main;
489   vnet_feature_main_t *fm = &feature_main;
490   u8 arc_index = vnet_feat_arc_ip4_local.feature_arc_index;
491   vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc_index];
492
493   stats_node_index = is_ed ? sm->ed_hairpinning_node_index :
494     sm->hairpinning_node_index;
495   from = vlib_frame_vector_args (frame);
496   n_left_from = frame->n_vectors;
497   next_index = node->cached_next_index;
498
499   while (n_left_from > 0)
500     {
501       u32 n_left_to_next;
502
503       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
504
505       while (n_left_from > 0 && n_left_to_next > 0)
506         {
507           u32 bi0;
508           vlib_buffer_t *b0;
509           u32 next0;
510           ip4_header_t *ip0;
511           u32 proto0;
512           udp_header_t *udp0;
513           tcp_header_t *tcp0;
514
515           /* speculatively enqueue b0 to the current next frame */
516           bi0 = from[0];
517           to_next[0] = bi0;
518           from += 1;
519           to_next += 1;
520           n_left_from -= 1;
521           n_left_to_next -= 1;
522
523           b0 = vlib_get_buffer (vm, bi0);
524           ip0 = vlib_buffer_get_current (b0);
525           udp0 = ip4_next_header (ip0);
526           tcp0 = (tcp_header_t *) udp0;
527
528           proto0 = ip_proto_to_nat_proto (ip0->protocol);
529
530           vnet_get_config_data (&cm->config_main, &b0->current_config_index,
531                                 &next0, 0);
532
533           if (snat_hairpinning
534               (vm, node, sm, b0, ip0, udp0, tcp0, proto0, is_ed,
535                1 /* do_trace */ ))
536             next0 = NAT_HAIRPIN_NEXT_LOOKUP;
537
538           pkts_processed += next0 != NAT_HAIRPIN_NEXT_DROP;
539
540           /* verify speculative enqueue, maybe switch current next frame */
541           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
542                                            to_next, n_left_to_next,
543                                            bi0, next0);
544         }
545
546       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
547     }
548
549   vlib_node_increment_counter (vm, stats_node_index,
550                                NAT44_HAIRPIN_ERROR_PROCESSED, pkts_processed);
551   return frame->n_vectors;
552 }
553
554 VLIB_NODE_FN (nat44_hairpinning_node) (vlib_main_t * vm,
555                                        vlib_node_runtime_t * node,
556                                        vlib_frame_t * frame)
557 {
558   return nat44_hairpinning_fn_inline (vm, node, frame, 0);
559 }
560
561 /* *INDENT-OFF* */
562 VLIB_REGISTER_NODE (nat44_hairpinning_node) = {
563   .name = "nat44-hairpinning",
564   .vector_size = sizeof (u32),
565   .type = VLIB_NODE_TYPE_INTERNAL,
566   .n_errors = ARRAY_LEN(nat44_hairpin_error_strings),
567   .format_trace = format_nat_hairpin_trace,
568   .error_strings = nat44_hairpin_error_strings,
569   .n_next_nodes = NAT_HAIRPIN_N_NEXT,
570   .next_nodes = {
571     [NAT_HAIRPIN_NEXT_DROP] = "error-drop",
572     [NAT_HAIRPIN_NEXT_LOOKUP] = "ip4-lookup",
573   },
574 };
575 /* *INDENT-ON* */
576
577 VLIB_NODE_FN (nat44_ed_hairpinning_node) (vlib_main_t * vm,
578                                           vlib_node_runtime_t * node,
579                                           vlib_frame_t * frame)
580 {
581   return nat44_hairpinning_fn_inline (vm, node, frame, 1);
582 }
583
584 /* *INDENT-OFF* */
585 VLIB_REGISTER_NODE (nat44_ed_hairpinning_node) = {
586   .name = "nat44-ed-hairpinning",
587   .vector_size = sizeof (u32),
588   .type = VLIB_NODE_TYPE_INTERNAL,
589   .n_errors = ARRAY_LEN(nat44_hairpin_error_strings),
590   .error_strings = nat44_hairpin_error_strings,
591   .format_trace = format_nat_hairpin_trace,
592   .n_next_nodes = NAT_HAIRPIN_N_NEXT,
593   .next_nodes = {
594     [NAT_HAIRPIN_NEXT_DROP] = "error-drop",
595     [NAT_HAIRPIN_NEXT_LOOKUP] = "ip4-lookup",
596   },
597 };
598 /* *INDENT-ON* */
599
600 static inline uword
601 snat_hairpin_dst_fn_inline (vlib_main_t * vm,
602                             vlib_node_runtime_t * node,
603                             vlib_frame_t * frame, int is_ed)
604 {
605   u32 n_left_from, *from, *to_next, stats_node_index;
606   nat_hairpin_next_t next_index;
607   u32 pkts_processed = 0;
608   snat_main_t *sm = &snat_main;
609
610   stats_node_index = is_ed ? sm->ed_hairpin_dst_node_index :
611     sm->hairpin_dst_node_index;
612
613   from = vlib_frame_vector_args (frame);
614   n_left_from = frame->n_vectors;
615   next_index = node->cached_next_index;
616
617   while (n_left_from > 0)
618     {
619       u32 n_left_to_next;
620
621       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
622
623       while (n_left_from > 0 && n_left_to_next > 0)
624         {
625           u32 bi0;
626           vlib_buffer_t *b0;
627           u32 next0;
628           ip4_header_t *ip0;
629           u32 proto0;
630
631           /* speculatively enqueue b0 to the current next frame */
632           bi0 = from[0];
633           to_next[0] = bi0;
634           from += 1;
635           to_next += 1;
636           n_left_from -= 1;
637           n_left_to_next -= 1;
638
639           b0 = vlib_get_buffer (vm, bi0);
640           next0 = NAT_HAIRPIN_NEXT_LOOKUP;
641           ip0 = vlib_buffer_get_current (b0);
642
643           proto0 = ip_proto_to_nat_proto (ip0->protocol);
644
645           vnet_buffer (b0)->snat.flags = 0;
646           if (PREDICT_FALSE (is_hairpinning (sm, &ip0->dst_address)))
647             {
648               if (proto0 == NAT_PROTOCOL_TCP || proto0 == NAT_PROTOCOL_UDP)
649                 {
650                   udp_header_t *udp0 = ip4_next_header (ip0);
651                   tcp_header_t *tcp0 = (tcp_header_t *) udp0;
652
653                   snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0,
654                                     is_ed, 1 /* do_trace */ );
655                 }
656               else if (proto0 == NAT_PROTOCOL_ICMP)
657                 {
658                   icmp46_header_t *icmp0 = ip4_next_header (ip0);
659
660                   snat_icmp_hairpinning (sm, b0, ip0, icmp0, is_ed);
661                 }
662               else
663                 {
664                   if (is_ed)
665                     nat44_ed_hairpinning_unknown_proto (sm, b0, ip0);
666                   else
667                     nat_hairpinning_sm_unknown_proto (sm, b0, ip0);
668                 }
669
670               vnet_buffer (b0)->snat.flags = SNAT_FLAG_HAIRPINNING;
671             }
672
673           pkts_processed += next0 != NAT_HAIRPIN_NEXT_DROP;
674
675           /* verify speculative enqueue, maybe switch current next frame */
676           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
677                                            to_next, n_left_to_next,
678                                            bi0, next0);
679         }
680
681       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
682     }
683
684   vlib_node_increment_counter (vm, stats_node_index,
685                                NAT44_HAIRPIN_ERROR_PROCESSED, pkts_processed);
686   return frame->n_vectors;
687 }
688
689 VLIB_NODE_FN (snat_hairpin_dst_node) (vlib_main_t * vm,
690                                       vlib_node_runtime_t * node,
691                                       vlib_frame_t * frame)
692 {
693   return snat_hairpin_dst_fn_inline (vm, node, frame, 0);
694 }
695
696 /* *INDENT-OFF* */
697 VLIB_REGISTER_NODE (snat_hairpin_dst_node) = {
698   .name = "nat44-hairpin-dst",
699   .vector_size = sizeof (u32),
700   .type = VLIB_NODE_TYPE_INTERNAL,
701   .n_errors = ARRAY_LEN(nat44_hairpin_error_strings),
702   .format_trace = format_nat_hairpin_trace,
703   .error_strings = nat44_hairpin_error_strings,
704   .n_next_nodes = NAT_HAIRPIN_N_NEXT,
705   .next_nodes = {
706     [NAT_HAIRPIN_NEXT_DROP] = "error-drop",
707     [NAT_HAIRPIN_NEXT_LOOKUP] = "ip4-lookup",
708   },
709 };
710 /* *INDENT-ON* */
711
712 VLIB_NODE_FN (nat44_ed_hairpin_dst_node) (vlib_main_t * vm,
713                                           vlib_node_runtime_t * node,
714                                           vlib_frame_t * frame)
715 {
716   return snat_hairpin_dst_fn_inline (vm, node, frame, 1);
717 }
718
719 /* *INDENT-OFF* */
720 VLIB_REGISTER_NODE (nat44_ed_hairpin_dst_node) = {
721   .name = "nat44-ed-hairpin-dst",
722   .vector_size = sizeof (u32),
723   .type = VLIB_NODE_TYPE_INTERNAL,
724   .n_errors = ARRAY_LEN(nat44_hairpin_error_strings),
725   .format_trace = format_nat_hairpin_trace,
726   .error_strings = nat44_hairpin_error_strings,
727   .n_next_nodes = NAT_HAIRPIN_N_NEXT,
728   .next_nodes = {
729     [NAT_HAIRPIN_NEXT_DROP] = "error-drop",
730     [NAT_HAIRPIN_NEXT_LOOKUP] = "ip4-lookup",
731   },
732 };
733 /* *INDENT-ON* */
734
735 static inline uword
736 snat_hairpin_src_fn_inline (vlib_main_t * vm,
737                             vlib_node_runtime_t * node,
738                             vlib_frame_t * frame, int is_ed)
739 {
740   u32 n_left_from, *from, *to_next, stats_node_index;
741   snat_hairpin_src_next_t next_index;
742   u32 pkts_processed = 0;
743   snat_main_t *sm = &snat_main;
744
745   stats_node_index = is_ed ? sm->ed_hairpin_src_node_index :
746     sm->hairpin_src_node_index;
747
748   from = vlib_frame_vector_args (frame);
749   n_left_from = frame->n_vectors;
750   next_index = node->cached_next_index;
751
752   while (n_left_from > 0)
753     {
754       u32 n_left_to_next;
755
756       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
757
758       while (n_left_from > 0 && n_left_to_next > 0)
759         {
760           u32 bi0;
761           vlib_buffer_t *b0;
762           u32 next0;
763           snat_interface_t *i;
764           u32 sw_if_index0;
765
766           /* speculatively enqueue b0 to the current next frame */
767           bi0 = from[0];
768           to_next[0] = bi0;
769           from += 1;
770           to_next += 1;
771           n_left_from -= 1;
772           n_left_to_next -= 1;
773
774           b0 = vlib_get_buffer (vm, bi0);
775           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
776           vnet_feature_next (&next0, b0);
777
778           /* *INDENT-OFF* */
779           pool_foreach (i, sm->output_feature_interfaces,
780           ({
781             /* Only packets from NAT inside interface */
782             if ((nat_interface_is_inside(i)) && (sw_if_index0 == i->sw_if_index))
783               {
784                 if (PREDICT_FALSE ((vnet_buffer (b0)->snat.flags) &
785                                     SNAT_FLAG_HAIRPINNING))
786                   {
787                     if (PREDICT_TRUE (sm->num_workers > 1))
788                       next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH;
789                     else
790                       next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT;
791                   }
792                 break;
793               }
794           }));
795           /* *INDENT-ON* */
796
797           pkts_processed += next0 != SNAT_HAIRPIN_SRC_NEXT_DROP;
798
799           /* verify speculative enqueue, maybe switch current next frame */
800           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
801                                            to_next, n_left_to_next,
802                                            bi0, next0);
803         }
804
805       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
806     }
807
808   vlib_node_increment_counter (vm, stats_node_index,
809                                NAT44_HAIRPIN_ERROR_PROCESSED, pkts_processed);
810   return frame->n_vectors;
811 }
812
813 VLIB_NODE_FN (snat_hairpin_src_node) (vlib_main_t * vm,
814                                       vlib_node_runtime_t * node,
815                                       vlib_frame_t * frame)
816 {
817   return snat_hairpin_src_fn_inline (vm, node, frame, 0);
818 }
819
820 /* *INDENT-OFF* */
821 VLIB_REGISTER_NODE (snat_hairpin_src_node) = {
822   .name = "nat44-hairpin-src",
823   .vector_size = sizeof (u32),
824   .type = VLIB_NODE_TYPE_INTERNAL,
825   .n_errors = ARRAY_LEN(nat44_hairpin_error_strings),
826   .error_strings = nat44_hairpin_error_strings,
827   .n_next_nodes = SNAT_HAIRPIN_SRC_N_NEXT,
828   .next_nodes = {
829      [SNAT_HAIRPIN_SRC_NEXT_DROP] = "error-drop",
830      [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT] = "nat44-in2out-output",
831      [SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT] = "interface-output",
832      [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH] = "nat44-in2out-output-worker-handoff",
833   },
834 };
835 /* *INDENT-ON* */
836
837 VLIB_NODE_FN (nat44_ed_hairpin_src_node) (vlib_main_t * vm,
838                                           vlib_node_runtime_t * node,
839                                           vlib_frame_t * frame)
840 {
841   return snat_hairpin_src_fn_inline (vm, node, frame, 1);
842 }
843
844 /* *INDENT-OFF* */
845 VLIB_REGISTER_NODE (nat44_ed_hairpin_src_node) = {
846   .name = "nat44-ed-hairpin-src",
847   .vector_size = sizeof (u32),
848   .type = VLIB_NODE_TYPE_INTERNAL,
849   .n_errors = ARRAY_LEN(nat44_hairpin_error_strings),
850   .error_strings = nat44_hairpin_error_strings,
851   .n_next_nodes = SNAT_HAIRPIN_SRC_N_NEXT,
852   .next_nodes = {
853      [SNAT_HAIRPIN_SRC_NEXT_DROP] = "error-drop",
854      [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT] = "nat44-ed-in2out-output",
855      [SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT] = "interface-output",
856      [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH] = "nat44-in2out-output-worker-handoff",
857   },
858 };
859 /* *INDENT-ON* */
860
861 /*
862  * fd.io coding-style-patch-verification: ON
863  *
864  * Local Variables:
865  * eval: (c-set-style "gnu")
866  * End:
867  */