summaryrefslogtreecommitdiffstats
path: root/src/plugins/ikev2/ikev2_format.c
blob: 2b77d7e5945a62e25b71ad99111c5be61a2f32d3 (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
/*
 * Copyright (c) 2015 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 <vnet/vnet.h>
#include <vnet/api_errno.h>
#include <vnet/ip/ip.h>
#include <vnet/interface.h>

#include <vnet/ipsec/ipsec.h>
#include <plugins/ikev2/ikev2.h>
#include <plugins/ikev2/ikev2_priv.h>

u8 *
format_ikev2_sa_transform (u8 * s, va_list * args)
{
  ikev2_sa_transform_t *tr = va_arg (*args, ikev2_sa_transform_t *);

  if (!tr)
    return s;

  if (tr->type >= IKEV2_TRANSFORM_NUM_TYPES)
    return s;

  s = format (s, "%U:", format_ikev2_transform_type, tr->type);

  switch (tr->type)
    {
    case IKEV2_TRANSFORM_TYPE_ENCR:
      s = format (s, "%U", format_ikev2_transform_encr_type, tr->encr_type);
      break;
    case IKEV2_TRANSFORM_TYPE_PRF:
      s = format (s, "%U", format_ikev2_transform_prf_type, tr->prf_type);
      break;
    case IKEV2_TRANSFORM_TYPE_INTEG:
      s = format (s, "%U", format_ikev2_transform_integ_type, tr->integ_type);
      break;
    case IKEV2_TRANSFORM_TYPE_DH:
      s = format (s, "%U", format_ikev2_transform_dh_type, tr->dh_type);
      break;
    case IKEV2_TRANSFORM_TYPE_ESN:
      s = format (s, "%U", format_ikev2_transform_esn_type, tr->esn_type);
      break;
    default:
      break;
    }

  if (tr->type == IKEV2_TRANSFORM_TYPE_ENCR &&
      tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
    s = format (s, "-%u", tr->key_len * 8);
  else if (vec_len (tr->attrs) == 4 && tr->attrs[0] == 0x80
	   && tr->attrs[1] == 0x0e)
    s = format (s, "-%u", tr->attrs[2] * 256 + tr->attrs[3]);
  else if (vec_len (tr->attrs))
    s = format (s, "(unknown attr %U)", format_hex_bytes,
		tr->attrs, vec_len (tr->attrs));

  return s;
}

#define MACRO_FORMAT(lc)                                \
u8 * format_ikev2_##lc (u8 * s, va_list * args)         \
{                                                       \
  u32 i = va_arg (*args, u32);                          \
  char * t = 0;                                         \
  switch (i) {                                          \
        foreach_ikev2_##lc                              \
      default:                                          \
        return format (s, "unknown (%u)", i);           \
    }                                                   \
  s = format (s, "%s", t);                              \
  return s;                                             \
}

#define MACRO_UNFORMAT(lc)                              \
uword                                                   \
unformat_ikev2_##lc (unformat_input_t * input,          \
                     va_list * args)                    \
{                                                       \
  u32 * r = va_arg (*args, u32 *);                      \
  if (0) ;                                              \
  foreach_ikev2_##lc                                    \
  else                                                  \
    return 0;                                           \
  return 1;                                             \
}

#define _(v,f,str) case IKEV2_AUTH_METHOD_##f: t = str; break;
MACRO_FORMAT (auth_method)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_AUTH_METHOD_##f;
  MACRO_UNFORMAT (auth_method)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_TRANSFORM_TYPE_##f;
  MACRO_UNFORMAT (transform_type)
#undef _
#define _(v,f) case IKEV2_NOTIFY_MSG_##f: t = #f; break;
  MACRO_FORMAT (notify_msg_type)
#undef _
#define _(v,f,str) case IKEV2_ID_TYPE_##f: t = str; break;
  MACRO_FORMAT (id_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_ID_TYPE_##f;
  MACRO_UNFORMAT (id_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_ENCR_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_encr_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_TRANSFORM_ENCR_TYPE_##f;
  MACRO_UNFORMAT (transform_encr_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_PRF_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_prf_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_TRANSFORM_PRF_TYPE_##f;
  MACRO_UNFORMAT (transform_prf_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_INTEG_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_integ_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_TRANSFORM_INTEG_TYPE_##f;
  MACRO_UNFORMAT (transform_integ_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_DH_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_dh_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_TRANSFORM_DH_TYPE_##f;
  MACRO_UNFORMAT (transform_dh_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_ESN_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_esn_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_TRANSFORM_ESN_TYPE_##f;
  MACRO_UNFORMAT (transform_esn_type)
#undef _
/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
s="o">.description = description def __iter__(self): for x in range(0, self.n_iters): yield x self.testcase.sleep(self.sleep_sec) class Conn(L4_Conn): def apply_acls(self, reflect_side, acl_side): pkts = [] pkts.append(self.pkt(0)) pkts.append(self.pkt(1)) pkt = pkts[reflect_side] r = [] r.append(pkt.to_acl_rule(2, wildcard_sport=True)) r.append(self.wildcard_rule(0)) res = self.testcase.vapi.acl_add_replace(0xffffffff, r) self.testcase.assert_equal(res.retval, 0, "error adding ACL") reflect_acl_index = res.acl_index r = [] r.append(self.wildcard_rule(0)) res = self.testcase.vapi.acl_add_replace(0xffffffff, r) self.testcase.assert_equal(res.retval, 0, "error adding deny ACL") deny_acl_index = res.acl_index if reflect_side == acl_side: self.testcase.vapi.acl_interface_set_acl_list( self.ifs[acl_side].sw_if_index, 1, [reflect_acl_index, deny_acl_index]) self.testcase.vapi.acl_interface_set_acl_list( self.ifs[1-acl_side].sw_if_index, 0, []) else: self.testcase.vapi.acl_interface_set_acl_list( self.ifs[acl_side].sw_if_index, 1, [deny_acl_index, reflect_acl_index]) self.testcase.vapi.acl_interface_set_acl_list( self.ifs[1-acl_side].sw_if_index, 0, []) def wildcard_rule(self, is_permit): any_addr = ["0.0.0.0", "::"] rule_family = self.address_family is_ip6 = 1 if rule_family == AF_INET6 else 0 new_rule = { 'is_permit': is_permit, 'is_ipv6': is_ip6, 'src_ip_addr': inet_pton(rule_family, any_addr[is_ip6]), 'src_ip_prefix_len': 0, 'dst_ip_addr': inet_pton(rule_family, any_addr[is_ip6]), 'dst_ip_prefix_len': 0, 'srcport_or_icmptype_first': 0, 'srcport_or_icmptype_last': 65535, 'dstport_or_icmpcode_first': 0, 'dstport_or_icmpcode_last': 65535, 'proto': 0, } return new_rule @unittest.skipUnless(running_extended_tests, "part of extended tests") class ACLPluginConnTestCase(VppTestCase): """ ACL plugin connection-oriented extended testcases """ @classmethod def setUpClass(cls): super(ACLPluginConnTestCase, cls).setUpClass() # create pg0 and pg1 cls.create_pg_interfaces(range(2)) cmd = "set acl-plugin session table event-trace 1" cls.logger.info(cls.vapi.cli(cmd)) for i in cls.pg_interfaces: i.admin_up() i.config_ip4() i.config_ip6() i.resolve_arp() i.resolve_ndp() @classmethod def tearDownClass(cls): super(ACLPluginConnTestCase, cls).tearDownClass() def tearDown(self): """Run standard test teardown and log various show commands """ super(ACLPluginConnTestCase, self).tearDown() def show_commands_at_teardown(self): self.logger.info(self.vapi.cli("show ip arp")) self.logger.info(self.vapi.cli("show ip6 neighbors")) self.logger.info(self.vapi.cli("show acl-plugin sessions")) self.logger.info(self.vapi.cli("show acl-plugin acl")) self.logger.info(self.vapi.cli("show acl-plugin interface")) self.logger.info(self.vapi.cli("show acl-plugin tables")) self.logger.info(self.vapi.cli("show event-logger all")) def run_basic_conn_test(self, af, acl_side): """ Basic conn timeout test """ conn1 = Conn(self, self.pg0, self.pg1, af, UDP, 42001, 4242) conn1.apply_acls(0, acl_side) conn1.send_through(0) # the return packets should pass conn1.send_through(1) # send some packets on conn1, ensure it doesn't go away for i in IterateWithSleep(self, 20, "Keep conn active", 0.3): conn1.send_through(1) # allow the conn to time out for i in IterateWithSleep(self, 30, "Wait for timeout", 0.1): pass # now try to send a packet on the reflected side try: p2 = conn1.send_through(1).command() except: # If we asserted while waiting, it's good. # the conn should have timed out. p2 = None self.assert_equal(p2, None, "packet on long-idle conn") def run_active_conn_test(self, af, acl_side): """ Idle connection behind active connection test """ base = 10000 + 1000*acl_side conn1 = Conn(self, self.pg0, self.pg1, af, UDP, base + 1, 2323) conn2 = Conn(self, self.pg0, self.pg1, af, UDP, base + 2, 2323) conn3 = Conn(self, self.pg0, self.pg1, af, UDP, base + 3, 2323) conn1.apply_acls(0, acl_side) conn1.send(0) conn1.recv(1) # create and check that the conn2/3 work self.sleep(0.1) conn2.send_pingpong(0) self.sleep(0.1) conn3.send_pingpong(0) # send some packets on conn1, keep conn2/3 idle for i in IterateWithSleep(self, 20, "Keep conn active", 0.2): conn1.send_through(1) try: p2 = conn2.send_through(1).command() except: # If we asserted while waiting, it's good. # the conn should have timed out. p2 = None # We should have not received the packet on a long-idle # connection, because it should have timed out # If it didn't - it is a problem self.assert_equal(p2, None, "packet on long-idle conn") def run_clear_conn_test(self, af, acl_side): """ Clear the connections via CLI """ conn1 = Conn(self, self.pg0, self.pg1, af, UDP, 42001, 4242) conn1.apply_acls(0, acl_side) conn1.send_through(0) # the return packets should pass conn1.send_through(1) # send some packets on conn1, ensure it doesn't go away for i in IterateWithSleep(self, 20, "Keep conn active", 0.3): conn1.send_through(1) # clear all connections self.vapi.ppcli("clear acl-plugin sessions") # now try to send a packet on the reflected side try: p2 = conn1.send_through(1).command() except: # If we asserted while waiting, it's good. # the conn should have timed out. p2 = None self.assert_equal(p2, None, "packet on supposedly deleted conn") def run_tcp_transient_setup_conn_test(self, af, acl_side): conn1 = Conn(self, self.pg0, self.pg1, af, TCP, 53001, 5151) conn1.apply_acls(0, acl_side) conn1.send_through(0, 'S') # the return packets should pass conn1.send_through(1, 'SA') # allow the conn to time out for i in IterateWithSleep(self, 30, "Wait for timeout", 0.1): pass # ensure conn times out try: p2 = conn1.send_through(1).command() except: # If we asserted while waiting, it's good. # the conn should have timed out. p2 = None self.assert_equal(p2, None, "packet on supposedly deleted conn") def run_tcp_established_conn_test(self, af, acl_side): conn1 = Conn(self, self.pg0, self.pg1, af, TCP, 53002, 5052) conn1.apply_acls(0, acl_side) conn1.send_through(0, 'S') # the return packets should pass conn1.send_through(1, 'SA') # complete the threeway handshake # (NB: sequence numbers not tracked, so not set!) conn1.send_through(0, 'A') # allow the conn to time out if it's in embryonic timer for i in IterateWithSleep(self, 30, "Wait for transient timeout", 0.1): pass # Try to send the packet from the "forbidden" side - it must pass conn1.send_through(1, 'A') # ensure conn times out for real for i in IterateWithSleep(self, 130, "Wait for timeout", 0.1): pass try: p2 = conn1.send_through(1).command() except: # If we asserted while waiting, it's good. # the conn should have timed out. p2 = None self.assert_equal(p2, None, "packet on supposedly deleted conn") def run_tcp_transient_teardown_conn_test(self, af, acl_side): conn1 = Conn(self, self.pg0, self.pg1, af, TCP, 53002, 5052) conn1.apply_acls(0, acl_side) conn1.send_through(0, 'S') # the return packets should pass conn1.send_through(1, 'SA') # complete the threeway handshake # (NB: sequence numbers not tracked, so not set!) conn1.send_through(0, 'A') # allow the conn to time out if it's in embryonic timer for i in IterateWithSleep(self, 30, "Wait for transient timeout", 0.1): pass # Try to send the packet from the "forbidden" side - it must pass conn1.send_through(1, 'A') # Send the FIN to bounce the session out of established conn1.send_through(1, 'FA') # If conn landed on transient timer it will time out here for i in IterateWithSleep(self, 30, "Wait for transient timeout", 0.1): pass # Now it should have timed out already try: p2 = conn1.send_through(1).command() except: # If we asserted while waiting, it's good. # the conn should have timed out. p2 = None self.assert_equal(p2, None, "packet on supposedly deleted conn") def test_0000_conn_prepare_test(self): """ Prepare the settings """ self.vapi.ppcli("set acl-plugin session timeout udp idle 1") def test_0001_basic_conn_test(self): """ IPv4: Basic conn timeout test reflect on ingress """ self.run_basic_conn_test(AF_INET, 0) def test_0002_basic_conn_test(self): """ IPv4: Basic conn timeout test reflect on egress """ self.run_basic_conn_test(AF_INET, 1) def test_0005_clear_conn_test(self): """ IPv4: reflect egress, clear conn """ self.run_clear_conn_test(AF_INET, 1) def test_0006_clear_conn_test(self): """ IPv4: reflect ingress, clear conn """ self.run_clear_conn_test(AF_INET, 0) def test_0011_active_conn_test(self): """ IPv4: Idle conn behind active conn, reflect on ingress """ self.run_active_conn_test(AF_INET, 0) def test_0012_active_conn_test(self): """ IPv4: Idle conn behind active conn, reflect on egress """ self.run_active_conn_test(AF_INET, 1) def test_1001_basic_conn_test(self): """ IPv6: Basic conn timeout test reflect on ingress """ self.run_basic_conn_test(AF_INET6, 0) def test_1002_basic_conn_test(self): """ IPv6: Basic conn timeout test reflect on egress """ self.run_basic_conn_test(AF_INET6, 1) def test_1005_clear_conn_test(self): """ IPv6: reflect egress, clear conn """ self.run_clear_conn_test(AF_INET6, 1) def test_1006_clear_conn_test(self): """ IPv6: reflect ingress, clear conn """ self.run_clear_conn_test(AF_INET6, 0) def test_1011_active_conn_test(self): """ IPv6: Idle conn behind active conn, reflect on ingress """ self.run_active_conn_test(AF_INET6, 0) def test_1012_active_conn_test(self): """ IPv6: Idle conn behind active conn, reflect on egress """ self.run_active_conn_test(AF_INET6, 1) def test_2000_prepare_for_tcp_test(self): """ Prepare for TCP session tests """ # ensure the session hangs on if it gets treated as UDP self.vapi.ppcli("set acl-plugin session timeout udp idle 200") # let the TCP connection time out at 5 seconds self.vapi.ppcli("set acl-plugin session timeout tcp idle 10") self.vapi.ppcli("set acl-plugin session timeout tcp transient 1") def test_2001_tcp_transient_conn_test(self): """ IPv4: transient TCP session (incomplete 3WHS), ref. on ingress """ self.run_tcp_transient_setup_conn_test(AF_INET, 0) def test_2002_tcp_transient_conn_test(self): """ IPv4: transient TCP session (incomplete 3WHS), ref. on egress """ self.run_tcp_transient_setup_conn_test(AF_INET, 1) def test_2003_tcp_transient_conn_test(self): """ IPv4: established TCP session (complete 3WHS), ref. on ingress """ self.run_tcp_established_conn_test(AF_INET, 0) def test_2004_tcp_transient_conn_test(self): """ IPv4: established TCP session (complete 3WHS), ref. on egress """ self.run_tcp_established_conn_test(AF_INET, 1) def test_2005_tcp_transient_teardown_conn_test(self): """ IPv4: transient TCP session (3WHS,ACK,FINACK), ref. on ingress """ self.run_tcp_transient_teardown_conn_test(AF_INET, 0) def test_2006_tcp_transient_teardown_conn_test(self): """ IPv4: transient TCP session (3WHS,ACK,FINACK), ref. on egress """ self.run_tcp_transient_teardown_conn_test(AF_INET, 1) def test_3001_tcp_transient_conn_test(self): """ IPv6: transient TCP session (incomplete 3WHS), ref. on ingress """ self.run_tcp_transient_setup_conn_test(AF_INET6, 0) def test_3002_tcp_transient_conn_test(self): """ IPv6: transient TCP session (incomplete 3WHS), ref. on egress """ self.run_tcp_transient_setup_conn_test(AF_INET6, 1) def test_3003_tcp_transient_conn_test(self): """ IPv6: established TCP session (complete 3WHS), ref. on ingress """ self.run_tcp_established_conn_test(AF_INET6, 0) def test_3004_tcp_transient_conn_test(self): """ IPv6: established TCP session (complete 3WHS), ref. on egress """ self.run_tcp_established_conn_test(AF_INET6, 1) def test_3005_tcp_transient_teardown_conn_test(self): """ IPv6: transient TCP session (3WHS,ACK,FINACK), ref. on ingress """ self.run_tcp_transient_teardown_conn_test(AF_INET6, 0) def test_3006_tcp_transient_teardown_conn_test(self): """ IPv6: transient TCP session (3WHS,ACK,FINACK), ref. on egress """ self.run_tcp_transient_teardown_conn_test(AF_INET6, 1)