aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/nat/nat44-ed/nat44_ed_format.c
blob: f643d1c605689b325a5d13fb41758b204fb28266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <nat/nat44-ed/nat44_ed.h>
#include <nat/nat44-ed/nat44_ed_inlines.h>

u8 *
format_ed_session_kvp (u8 *s, va_list *args)
{
  clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);

  u8 proto;
  u16 r_port, l_port;
  ip4_address_t l_addr, r_addr;
  u32 fib_index;

  split_ed_kv (v, &l_addr, &r_addr, &proto, &fib_index, &l_port, &r_port);
  s = format (s,
	      "local %U:%d remote %U:%d proto %U fib %d thread-index %u "
	      "session-index %u",
	      format_ip4_address, &l_addr, clib_net_to_host_u16 (l_port),
	      format_ip4_address, &r_addr, clib_net_to_host_u16 (r_port),
	      format_ip_protocol, proto, fib_index,
	      ed_value_get_thread_index (v), ed_value_get_session_index (v));

  return s;
}

u8 *
format_snat_session (u8 * s, va_list * args)
{
  snat_main_t *sm = va_arg (*args, snat_main_t *);
  snat_main_per_thread_data_t *tsm =
    va_arg (*args, snat_main_per_thread_data_t *);
  snat_session_t *sess = va_arg (*args, snat_session_t *);
  f64 now = va_arg (*args, f64);

  if (nat44_ed_is_unk_proto (sess->proto))
    {
      s = format (s, "  i2o %U proto %u fib %u\n",
		  format_ip4_address, &sess->in2out.addr,
		  sess->in2out.port, sess->in2out.fib_index);
      s =
	format (s, "    o2i %U proto %u fib %u\n", format_ip4_address,
		&sess->out2in.addr, sess->out2in.port, sess->out2in.fib_index);
    }
  else
    {
      s = format (s, "  i2o %U proto %U port %d fib %d\n", format_ip4_address,
		  &sess->in2out.addr, format_ip_protocol, sess->proto,
		  clib_net_to_host_u16 (sess->in2out.port),
		  sess->in2out.fib_index);
      s = format (s, "    o2i %U proto %U port %d fib %d\n",
		  format_ip4_address, &sess->out2in.addr, format_ip_protocol,
		  sess->proto, clib_net_to_host_u16 (sess->out2in.port),
		  sess->out2in.fib_index);
    }
  if (nat44_ed_is_twice_nat_session (sess))
    {
      s = format (s, "       external host o2i %U:%d i2o %U:%d\n",
		  format_ip4_address, &sess->ext_host_addr,
		  clib_net_to_host_u16 (sess->ext_host_port),
		  format_ip4_address, &sess->ext_host_nat_addr,
		  clib_net_to_host_u16 (sess->ext_host_nat_port));
    }
      else
	{
	  if (sess->ext_host_addr.as_u32)
	    s = format (s, "       external host %U:%u\n",
			format_ip4_address, &sess->ext_host_addr,
			clib_net_to_host_u16 (sess->ext_host_port));
	}
      s = format (s, "       i2o flow: %U\n", format_nat_6t_flow, &sess->i2o);
      s = format (s, "       o2i flow: %U\n", format_nat_6t_flow, &sess->o2i);
  s = format (s, "       index %llu\n", sess - tsm->sessions);
  s = format (s, "       last heard %.2f\n", sess->last_heard);
  s = format (s, "       timeout in %.2f\n",
	      nat44_session_get_timeout (sm, sess) - (now - sess->last_heard));
  s = format (s, "       total pkts %d, total bytes %lld\n", sess->total_pkts,
	      sess->total_bytes);
  if (nat44_ed_is_session_static (sess))
    s = format (s, "       static translation\n");
  else
    s = format (s, "       dynamic translation\n");
  if (na44_ed_is_fwd_bypass_session (sess))
    s = format (s, "       forwarding-bypass\n");
  if (nat44_ed_is_lb_session (sess))
    s = format (s, "       load-balancing\n");
  if (nat44_ed_is_twice_nat_session (sess))
    s = format (s, "       twice-nat\n");
  return s;
}

u8 *
format_snat_static_mapping (u8 * s, va_list * args)
{
  snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
  nat44_lb_addr_port_t *local;

  if (is_sm_identity_nat (m->flags))
    {
      if (is_sm_addr_only (m->flags))
	s = format (s, "identity mapping %U",
		    format_ip4_address, &m->local_addr);
      else
	s = format (s, "identity mapping %U %U:%d", format_ip_protocol,
		    m->proto, format_ip4_address, &m->local_addr,
		    clib_net_to_host_u16 (m->local_port));

      pool_foreach (local, m->locals)
       {
        s = format (s, " vrf %d", local->vrf_id);
      }

      return s;
    }

  if (is_sm_addr_only (m->flags))
    s =
      format (s, "local %U external %U vrf %d %s %s", format_ip4_address,
	      &m->local_addr, format_ip4_address, &m->external_addr, m->vrf_id,
	      is_sm_twice_nat (m->flags) ?
		"twice-nat" :
		is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
	      is_sm_out2in_only (m->flags) ? "out2in-only" : "");
  else
    {
      if (is_sm_lb (m->flags))
	{
	  s =
	    format (s, "%U external %U:%d %s %s", format_ip_protocol, m->proto,
		    format_ip4_address, &m->external_addr,
		    clib_net_to_host_u16 (m->external_port),
		    is_sm_twice_nat (m->flags) ?
		      "twice-nat" :
		      is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
		    is_sm_out2in_only (m->flags) ? "out2in-only" : "");

	  pool_foreach (local, m->locals)
	    {
	      s = format (s, "\n  local %U:%d vrf %d probability %d\%",
			  format_ip4_address, &local->addr,
			  clib_net_to_host_u16 (local->port), local->vrf_id,
			  local->probability);
	    }
	}
      else
	s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
		    format_ip_protocol, m->proto, format_ip4_address,
		    &m->local_addr, clib_net_to_host_u16 (m->local_port),
		    format_ip4_address, &m->external_addr,
		    clib_net_to_host_u16 (m->external_port), m->vrf_id,
		    is_sm_twice_nat (m->flags) ?
		      "twice-nat" :
		      is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
		    is_sm_out2in_only (m->flags) ? "out2in-only" : "");
    }
  return s;
}

u8 *
format_snat_static_map_to_resolve (u8 * s, va_list * args)
{
  snat_static_mapping_resolve_t *m =
    va_arg (*args, snat_static_mapping_resolve_t *);
  vnet_main_t *vnm = vnet_get_main ();

  if (is_sm_addr_only (m->flags))
    s = format (s, "local %U external %U vrf %d",
		format_ip4_address, &m->l_addr,
		format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
  else
    s = format (s, "%U local %U:%d external %U:%d vrf %d", format_ip_protocol,
		m->proto, format_ip4_address, &m->l_addr,
		clib_net_to_host_u16 (m->l_port), format_vnet_sw_if_index_name,
		vnm, m->sw_if_index, clib_net_to_host_u16 (m->e_port),
		m->vrf_id);

  return s;
}

u8 *
format_nat_ed_translation_error (u8 *s, va_list *args)
{
  nat_translation_error_e e = va_arg (*args, nat_translation_error_e);

  switch (e)
    {
    case NAT_ED_TRNSL_ERR_SUCCESS:
      s = format (s, "success");
      break;
    case NAT_ED_TRNSL_ERR_TRANSLATION_FAILED:
      s = format (s, "translation-failed");
      break;
    case NAT_ED_TRNSL_ERR_FLOW_MISMATCH:
      s = format (s, "flow-mismatch");
      break;
    case NAT_ED_TRNSL_ERR_PACKET_TRUNCATED:
      s = format (s, "packet-truncated");
      break;
    case NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT:
      s = format (s, "inner-ip-corrupted");
      break;
    case NAT_ED_TRNSL_ERR_INVALID_CSUM:
      s = format (s, "invalid-checksum");
      break;
    }
  return s;
}

u8 *
format_nat_6t_flow (u8 *s, va_list *args)
{
  nat_6t_flow_t *f = va_arg (*args, nat_6t_flow_t *);

  s = format (s, "match: %U ", format_nat_6t, &f->match);
  int r = 0;
  if (f->ops & NAT_FLOW_OP_SADDR_REWRITE)
    {
      s = format (s, "rewrite: saddr %U ", format_ip4_address,
		  f->rewrite.saddr.as_u8);
      r = 1;
    }
  if (f->ops & NAT_FLOW_OP_SPORT_REWRITE)
    {
      if (!r)
	{
	  s = format (s, "rewrite: ");
	  r = 1;
	}
      s = format (s, "sport %u ", clib_net_to_host_u16 (f->rewrite.sport));
    }
  if (f->ops & NAT_FLOW_OP_DADDR_REWRITE)
    {
      if (!r)
	{
	  s = format (s, "rewrite: ");
	  r = 1;
	}
      s = format (s, "daddr %U ", format_ip4_address, f->rewrite.daddr.as_u8);
    }
  if (f->ops & NAT_FLOW_OP_DPORT_REWRITE)
    {
      if (!r)
	{
	  s = format (s, "rewrite: ");
	  r = 1;
	}
      s = format (s, "dport %u ", clib_net_to_host_u16 (f->rewrite.dport));
    }
  if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE)
    {
      if (!r)
	{
	  s = format (s, "rewrite: ");
	  r = 1;
	}
      s = format (s, "icmp-id %u ", clib_net_to_host_u16 (f->rewrite.icmp_id));
    }
  if (f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
    {
      if (!r)
	{
	  s = format (s, "rewrite: ");
	  r = 1;
	}
      s = format (s, "txfib %u ", f->rewrite.fib_index);
    }
  return s;
}

u8 *
format_nat_6t (u8 *s, va_list *args)
{
  nat_6t_t *t = va_arg (*args, nat_6t_t *);

  s = format (s, "saddr %U sport %u daddr %U dport %u proto %U fib_idx %u",
	      format_ip4_address, t->saddr.as_u8,
	      clib_net_to_host_u16 (t->sport), format_ip4_address,
	      t->daddr.as_u8, clib_net_to_host_u16 (t->dport),
	      format_ip_protocol, t->proto, t->fib_index);
  return s;
}

u8 *
format_nat44_ed_tcp_state (u8 *s, va_list *args)
{
  nat44_ed_tcp_state_e e = va_arg (*args, nat44_ed_tcp_state_e);
  switch (e)
    {
    case NAT44_ED_TCP_STATE_CLOSED:
      s = format (s, "closed");
      break;
    case NAT44_ED_TCP_STATE_SYN_I2O:
      s = format (s, "SYN seen in in2out direction");
      break;
    case NAT44_ED_TCP_STATE_SYN_O2I:
      s = format (s, "SYN seen in out2in direction");
      break;
    case NAT44_ED_TCP_STATE_ESTABLISHED:
      s = format (s, "SYN seen in both directions/established");
      break;
    case NAT44_ED_TCP_STATE_FIN_I2O:
      s = format (s, "FIN seen in in2out direction");
      break;
    case NAT44_ED_TCP_STATE_FIN_O2I:
      s = format (s, "FIN seen in out2in direction");
      break;
    case NAT44_ED_TCP_STATE_RST_TRANS:
      s = format (s, "RST seen/transitory timeout");
      break;
    case NAT44_ED_TCP_STATE_FIN_TRANS:
      s = format (s, "FIN seen in both directions/transitory timeout");
      break;
    case NAT44_ED_TCP_STATE_FIN_REOPEN_SYN_O2I:
      s = format (s, "FIN seen in both directions/transitory timeout/session "
		     "reopening in out2in direction");
      break;
    case NAT44_ED_TCP_STATE_FIN_REOPEN_SYN_I2O:
      s = format (s, "FIN seen in both directions/transitory timeout/session "
		     "reopening in in2out direction");
      break;
    case NAT44_ED_TCP_N_STATE:
      s = format (s, "BUG! unexpected N_STATE! BUG!");
      break;
    }
  return s;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */