nat: TCP state tracking based on RFC 7857/RFC 6146
[vpp.git] / src / plugins / nat / nat44-ed / nat44_ed_format.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 NAT formatting
18  */
19
20 #include <nat/nat44-ed/nat44_ed.h>
21 #include <nat/nat44-ed/nat44_ed_inlines.h>
22
23 u8 *
24 format_nat_addr_and_port_alloc_alg (u8 * s, va_list * args)
25 {
26   u32 i = va_arg (*args, u32);
27   u8 *t = 0;
28
29   switch (i)
30     {
31 #define _(v, N, s) case NAT_ADDR_AND_PORT_ALLOC_ALG_##N: t = (u8 *) s; break;
32       foreach_nat_addr_and_port_alloc_alg
33 #undef _
34     default:
35       s = format (s, "unknown");
36       return s;
37     }
38   s = format (s, "%s", t);
39   return s;
40 }
41
42 u8 *
43 format_snat_session (u8 * s, va_list * args)
44 {
45   snat_main_t *sm = va_arg (*args, snat_main_t *);
46   snat_main_per_thread_data_t *tsm =
47     va_arg (*args, snat_main_per_thread_data_t *);
48   snat_session_t *sess = va_arg (*args, snat_session_t *);
49   f64 now = va_arg (*args, f64);
50
51   if (nat44_ed_is_unk_proto (sess->proto))
52     {
53       s = format (s, "  i2o %U proto %u fib %u\n",
54                   format_ip4_address, &sess->in2out.addr,
55                   sess->in2out.port, sess->in2out.fib_index);
56       s =
57         format (s, "    o2i %U proto %u fib %u\n", format_ip4_address,
58                 &sess->out2in.addr, sess->out2in.port, sess->out2in.fib_index);
59     }
60   else
61     {
62       s = format (s, "  i2o %U proto %U port %d fib %d\n", format_ip4_address,
63                   &sess->in2out.addr, format_ip_protocol, sess->proto,
64                   clib_net_to_host_u16 (sess->in2out.port),
65                   sess->in2out.fib_index);
66       s = format (s, "    o2i %U proto %U port %d fib %d\n",
67                   format_ip4_address, &sess->out2in.addr, format_ip_protocol,
68                   sess->proto, clib_net_to_host_u16 (sess->out2in.port),
69                   sess->out2in.fib_index);
70     }
71   if (nat44_ed_is_twice_nat_session (sess))
72     {
73       s = format (s, "       external host o2i %U:%d i2o %U:%d\n",
74                   format_ip4_address, &sess->ext_host_addr,
75                   clib_net_to_host_u16 (sess->ext_host_port),
76                   format_ip4_address, &sess->ext_host_nat_addr,
77                   clib_net_to_host_u16 (sess->ext_host_nat_port));
78     }
79       else
80         {
81           if (sess->ext_host_addr.as_u32)
82             s = format (s, "       external host %U:%u\n",
83                         format_ip4_address, &sess->ext_host_addr,
84                         clib_net_to_host_u16 (sess->ext_host_port));
85         }
86       s = format (s, "       i2o flow: %U\n", format_nat_6t_flow, &sess->i2o);
87       s = format (s, "       o2i flow: %U\n", format_nat_6t_flow, &sess->o2i);
88   s = format (s, "       index %llu\n", sess - tsm->sessions);
89   s = format (s, "       last heard %.2f\n", sess->last_heard);
90   s = format (s, "       timeout in %.2f\n",
91               nat44_session_get_timeout (sm, sess) - (now - sess->last_heard));
92   s = format (s, "       total pkts %d, total bytes %lld\n", sess->total_pkts,
93               sess->total_bytes);
94   if (nat44_ed_is_session_static (sess))
95     s = format (s, "       static translation\n");
96   else
97     s = format (s, "       dynamic translation\n");
98   if (na44_ed_is_fwd_bypass_session (sess))
99     s = format (s, "       forwarding-bypass\n");
100   if (nat44_ed_is_lb_session (sess))
101     s = format (s, "       load-balancing\n");
102   if (nat44_ed_is_twice_nat_session (sess))
103     s = format (s, "       twice-nat\n");
104   return s;
105 }
106
107 u8 *
108 format_snat_static_mapping (u8 * s, va_list * args)
109 {
110   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
111   nat44_lb_addr_port_t *local;
112
113   if (is_sm_identity_nat (m->flags))
114     {
115       if (is_sm_addr_only (m->flags))
116         s = format (s, "identity mapping %U",
117                     format_ip4_address, &m->local_addr);
118       else
119         s = format (s, "identity mapping %U %U:%d", format_ip_protocol,
120                     m->proto, format_ip4_address, &m->local_addr,
121                     clib_net_to_host_u16 (m->local_port));
122
123       pool_foreach (local, m->locals)
124        {
125         s = format (s, " vrf %d", local->vrf_id);
126       }
127
128       return s;
129     }
130
131   if (is_sm_addr_only (m->flags))
132     s =
133       format (s, "local %U external %U vrf %d %s %s", format_ip4_address,
134               &m->local_addr, format_ip4_address, &m->external_addr, m->vrf_id,
135               is_sm_twice_nat (m->flags) ?
136                 "twice-nat" :
137                 is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
138               is_sm_out2in_only (m->flags) ? "out2in-only" : "");
139   else
140     {
141       if (is_sm_lb (m->flags))
142         {
143           s =
144             format (s, "%U external %U:%d %s %s", format_ip_protocol, m->proto,
145                     format_ip4_address, &m->external_addr,
146                     clib_net_to_host_u16 (m->external_port),
147                     is_sm_twice_nat (m->flags) ?
148                       "twice-nat" :
149                       is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
150                     is_sm_out2in_only (m->flags) ? "out2in-only" : "");
151
152           pool_foreach (local, m->locals)
153             {
154               s = format (s, "\n  local %U:%d vrf %d probability %d\%",
155                           format_ip4_address, &local->addr,
156                           clib_net_to_host_u16 (local->port), local->vrf_id,
157                           local->probability);
158             }
159         }
160       else
161         s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
162                     format_ip_protocol, m->proto, format_ip4_address,
163                     &m->local_addr, clib_net_to_host_u16 (m->local_port),
164                     format_ip4_address, &m->external_addr,
165                     clib_net_to_host_u16 (m->external_port), m->vrf_id,
166                     is_sm_twice_nat (m->flags) ?
167                       "twice-nat" :
168                       is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
169                     is_sm_out2in_only (m->flags) ? "out2in-only" : "");
170     }
171   return s;
172 }
173
174 u8 *
175 format_snat_static_map_to_resolve (u8 * s, va_list * args)
176 {
177   snat_static_mapping_resolve_t *m =
178     va_arg (*args, snat_static_mapping_resolve_t *);
179   vnet_main_t *vnm = vnet_get_main ();
180
181   if (is_sm_addr_only (m->flags))
182     s = format (s, "local %U external %U vrf %d",
183                 format_ip4_address, &m->l_addr,
184                 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
185   else
186     s = format (s, "%U local %U:%d external %U:%d vrf %d", format_ip_protocol,
187                 m->proto, format_ip4_address, &m->l_addr,
188                 clib_net_to_host_u16 (m->l_port), format_vnet_sw_if_index_name,
189                 vnm, m->sw_if_index, clib_net_to_host_u16 (m->e_port),
190                 m->vrf_id);
191
192   return s;
193 }
194
195 /*
196  * fd.io coding-style-patch-verification: ON
197  *
198  * Local Variables:
199  * eval: (c-set-style "gnu")
200  * End:
201  */