summaryrefslogtreecommitdiffstats
path: root/test/test_ipsec_tun_if_esp.py
blob: ac96cfb9cc4aa2226e6741d167b74cadf4841ed4 (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
import unittest
import socket
import copy
from scapy.layers.ipsec import ESP
from framework import VppTestRunner
from template_ipsec import TemplateIpsec, IpsecTun4Tests, IpsecTun6Tests, \
    IpsecTun4, IpsecTun6,  IpsecTcpTests,  config_tun_params
from vpp_ipsec_tun_interface import VppIpsecTunInterface
from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto


class TemplateIpsec4TunIfEsp(TemplateIpsec):
    """ IPsec tunnel interface tests """

    encryption_type = ESP

    def setUp(self):
        super(TemplateIpsec4TunIfEsp, self).setUp()

        self.tun_if = self.pg0

        p = self.ipv4_params
        tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi,
                                      p.scapy_tun_spi, p.crypt_algo_vpp_id,
                                      p.crypt_key, p.crypt_key,
                                      p.auth_algo_vpp_id, p.auth_key,
                                      p.auth_key)
        tun_if.add_vpp_config()
        tun_if.admin_up()
        tun_if.config_ip4()

        VppIpRoute(self,  p.remote_tun_if_host, 32,
                   [VppRoutePath(tun_if.remote_ip4,
                                 0xffffffff)]).add_vpp_config()

    def tearDown(self):
        if not self.vpp_dead:
            self.vapi.cli("show hardware")
        super(TemplateIpsec4TunIfEsp, self).tearDown()


class TestIpsec4TunIfEsp1(TemplateIpsec4TunIfEsp, IpsecTun4Tests):
    """ Ipsec ESP - TUN tests """
    tun4_encrypt_node_name = "esp4-encrypt"
    tun4_decrypt_node_name = "esp4-decrypt"


class TestIpsec4TunIfEsp2(TemplateIpsec4TunIfEsp, IpsecTcpTests):
    """ Ipsec ESP - TCP tests """
    pass


class TemplateIpsec6TunIfEsp(TemplateIpsec):
    """ IPsec tunnel interface tests """

    encryption_type = ESP

    def setUp(self):
        super(TemplateIpsec6TunIfEsp, self).setUp()

        self.tun_if = self.pg0

        p = self.ipv6_params
        tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi,
                                      p.scapy_tun_spi, p.crypt_algo_vpp_id,
                                      p.crypt_key, p.crypt_key,
                                      p.auth_algo_vpp_id, p.auth_key,
                                      p.auth_key, is_ip6=True)
        tun_if.add_vpp_config()
        tun_if.admin_up()
        tun_if.config_ip6()

        VppIpRoute(self,  p.remote_tun_if_host, 128,
                   [VppRoutePath(tun_if.remote_ip6,
                                 0xffffffff,
                                 proto=DpoProto.DPO_PROTO_IP6)],
                   is_ip6=1).add_vpp_config()

    def tearDown(self):
        if not self.vpp_dead:
            self.vapi.cli("show hardware")
        super(TemplateIpsec6TunIfEsp, self).tearDown()


class TestIpsec6TunIfEsp1(TemplateIpsec6TunIfEsp, IpsecTun6Tests):
    """ Ipsec ESP - TUN tests """
    tun6_encrypt_node_name = "esp6-encrypt"
    tun6_decrypt_node_name = "esp6-decrypt"


class TestIpsec4MultiTunIfEsp(TemplateIpsec, IpsecTun4):
    """ IPsec IPv4 Multi Tunnel interface """

    encryption_type = ESP
    tun4_encrypt_node_name = "esp4-encrypt"
    tun4_decrypt_node_name = "esp4-decrypt"

    def setUp(self):
        super(TestIpsec4MultiTunIfEsp, self).setUp()

        self.tun_if = self.pg0

        self.multi_params = []

        for ii in range(10):
            p = copy.copy(self.ipv4_params)

            p.remote_tun_if_host = "1.1.1.%d" % (ii + 1)
            p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii
            p.scapy_tun_spi = p.scapy_tun_spi + ii
            p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii
            p.vpp_tun_spi = p.vpp_tun_spi + ii

            p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii
            p.scapy_tra_spi = p.scapy_tra_spi + ii
            p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii
            p.vpp_tra_spi = p.vpp_tra_spi + ii

            config_tun_params(p, self.encryption_type, self.tun_if)
            self.multi_params.append(p)

            p.tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi,
                                            p.scapy_tun_spi,
                                            p.crypt_algo_vpp_id,
                                            p.crypt_key, p.crypt_key,
                                            p.auth_algo_vpp_id, p.auth_key,
                                            p.auth_key)
            p.tun_if.add_vpp_config()
            p.tun_if.admin_up()
            p.tun_if.config_ip4()

            VppIpRoute(self, p.remote_tun_if_host, 32,
                       [VppRoutePath(p.tun_if.remote_ip4,
                                     0xffffffff)]).add_vpp_config()

    def tearDown(self):
        if not self.vpp_dead:
            self.vapi.cli("show hardware")
        super(TestIpsec4MultiTunIfEsp, self).tearDown()

    def test_tun_44(self):
        """Multiple IPSEC tunnel interfaces """
        for p in self.multi_params:
            self.verify_tun_44(p, count=127)
            c = p.tun_if.get_rx_stats()
            self.assertEqual(c['packets'], 127)
            c = p.tun_if.get_tx_stats()
            self.assertEqual(c['packets'], 127)


class TestIpsec6MultiTunIfEsp(TemplateIpsec, IpsecTun6):
    """ IPsec IPv6 Muitli Tunnel interface """

    encryption_type = ESP
    tun6_encrypt_node_name = "esp6-encrypt"
    tun6_decrypt_node_name = "esp6-decrypt"

    def setUp(self):
        super(TestIpsec6MultiTunIfEsp, self).setUp()

        self.tun_if = self.pg0

        self.multi_params = []

        for ii in range(10):
            p = copy.copy(self.ipv6_params)

            p.remote_tun_if_host = "1111::%d" % (ii + 1)
            p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii
            p.scapy_tun_spi = p.scapy_tun_spi + ii
            p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii
            p.vpp_tun_spi = p.vpp_tun_spi + ii

            p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii
            p.scapy_tra_spi = p.scapy_tra_spi + ii
            p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii
            p.vpp_tra_spi = p.vpp_tra_spi + ii

            config_tun_params(p, self.encryption_type, self.tun_if)
            self.multi_params.append(p)

            p.tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi,
                                            p.scapy_tun_spi,
                                            p.crypt_algo_vpp_id,
                                            p.crypt_key, p.crypt_key,
                                            p.auth_algo_vpp_id, p.auth_key,
                                            p.auth_key, is_ip6=True)
            p.tun_if.add_vpp_config()
            p.tun_if.admin_up()
            p.tun_if.config_ip6()

            VppIpRoute(self, p.remote_tun_if_host, 128,
                       [VppRoutePath(p.tun_if.remote_ip6,
                                     0xffffffff,
                                     proto=DpoProto.DPO_PROTO_IP6)],
                       is_ip6=1).add_vpp_config()

    def tearDown(self):
        if not self.vpp_dead:
            self.vapi.cli("show hardware")
        super(TestIpsec6MultiTunIfEsp, self).tearDown()

    def test_tun_66(self):
        """Multiple IPSEC tunnel interfaces """
        for p in self.multi_params:
            self.verify_tun_66(p, count=127)
            c = p.tun_if.get_rx_stats()
            self.assertEqual(c['packets'], 127)
            c = p.tun_if.get_tx_stats()
            self.assertEqual(c['packets'], 127)


if __name__ == '__main__':
    unittest.main(testRunner=VppTestRunner)
ass="n">proposals - p + 1 < vec_len (proposals) ? 2 : 0; prop->protocol_id = p->protocol_id; prop->proposal_num = p->proposal_num; prop->spi_size = spi_size; prop->num_transforms = vec_len (p->transforms); if (spi_size) prop->spi[0] = clib_host_to_net_u32 (p->spi); vec_foreach (t, p->transforms) { vec_add2 (tr_data, tmp, sizeof (*tr) + vec_len (t->attrs)); tr = (ike_sa_transform_data_t *) tmp; tr->last_or_more = ((t - p->transforms) + 1 < vec_len (p->transforms)) ? 3 : 0; tr->transform_type = t->type; tr->transform_id = clib_host_to_net_u16 (t->transform_id); tr->transform_len = clib_host_to_net_u16 (sizeof (*tr) + vec_len (t->attrs)); if (vec_len (t->attrs) > 0) clib_memcpy_fast (tr->attributes, t->attrs, vec_len (t->attrs)); } prop->proposal_len = clib_host_to_net_u16 (vec_len (tr_data) + vec_len (pr_data)); ikev2_payload_add_data (c, pr_data); ikev2_payload_add_data (c, tr_data); vec_free (pr_data); vec_free (tr_data); } } void ikev2_payload_add_ke (ikev2_payload_chain_t * c, u16 dh_group, u8 * dh_data) { ike_ke_payload_header_t *ke; ke = (ike_ke_payload_header_t *) ikev2_payload_add_hdr (c, IKEV2_PAYLOAD_KE, sizeof (*ke)); ke->dh_group = clib_host_to_net_u16 (dh_group); ikev2_payload_add_data (c, dh_data); } void ikev2_payload_add_nonce (ikev2_payload_chain_t * c, u8 * nonce) { ikev2_payload_add_hdr (c, IKEV2_PAYLOAD_NONCE, sizeof (ike_payload_header_t)); ikev2_payload_add_data (c, nonce); } void ikev2_payload_add_id (ikev2_payload_chain_t * c, ikev2_id_t * id, u8 type) { ike_id_payload_header_t *idp; idp = (ike_id_payload_header_t *) ikev2_payload_add_hdr (c, type, sizeof (*idp)); idp->id_type = id->type; ikev2_payload_add_data (c, id->data); } void ikev2_payload_add_delete (ikev2_payload_chain_t * c, ikev2_delete_t * d) { ike_delete_payload_header_t *dp; u16 num_of_spi = vec_len (d); ikev2_delete_t *d2; dp = (ike_delete_payload_header_t *) ikev2_payload_add_hdr (c, IKEV2_PAYLOAD_DELETE, sizeof (*dp)); if (d[0].protocol_id == IKEV2_PROTOCOL_IKE) { dp->protocol_id = 1; } else { dp->protocol_id = d[0].protocol_id; dp->spi_size = 4; dp->num_of_spi = clib_host_to_net_u16 (num_of_spi); vec_foreach (d2, d) { u8 *data = vec_new (u8, 4); u32 spi = clib_host_to_net_u32 (d2->spi); clib_memcpy (data, &spi, 4); ikev2_payload_add_data (c, data); vec_free (data); } } } void ikev2_payload_add_auth (ikev2_payload_chain_t * c, ikev2_auth_t * auth) { ike_auth_payload_header_t *ap; ap = (ike_auth_payload_header_t *) ikev2_payload_add_hdr (c, IKEV2_PAYLOAD_AUTH, sizeof (*ap)); ap->auth_method = auth->method; ikev2_payload_add_data (c, auth->data); } static void ikev2_payload_add_ts_entry (u8 ** data, ikev2_ts_t * ts) { u8 * tmp; ikev2_ts_payload_entry_t *entry; int len = sizeof (*entry); if (ts->ts_type == TS_IPV4_ADDR_RANGE) len += sizeof (ikev2_ip4_addr_pair_t); else len += sizeof (ikev2_ip6_addr_pair_t); vec_add2 (data[0], tmp, len); entry = (ikev2_ts_payload_entry_t *) tmp; entry->ts_type = ts->ts_type; entry->protocol_id = ts->protocol_id; entry->selector_len = clib_host_to_net_u16 (len); entry->start_port = clib_host_to_net_u16 (ts->start_port); entry->end_port = clib_host_to_net_u16 (ts->end_port); if (ts->ts_type == TS_IPV4_ADDR_RANGE) { ikev2_ip4_addr_pair_t *pair = (ikev2_ip4_addr_pair_t*) entry->addr_pair; ip_address_copy_addr (&pair->start_addr, &ts->start_addr); ip_address_copy_addr (&pair->end_addr, &ts->end_addr); } else { ikev2_ip6_addr_pair_t *pair = (ikev2_ip6_addr_pair_t*) entry->addr_pair; ip_address_copy_addr (&pair->start_addr, &ts->start_addr); ip_address_copy_addr (&pair->end_addr, &ts->end_addr); } } void ikev2_payload_add_ts (ikev2_payload_chain_t * c, ikev2_ts_t * ts, u8 type) { ike_ts_payload_header_t *tsh; ikev2_ts_t *ts2; u8 *data = 0; tsh = (ike_ts_payload_header_t *) ikev2_payload_add_hdr (c, type, sizeof (*tsh)); tsh->num_ts = vec_len (ts); vec_foreach (ts2, ts) { ASSERT (ts2->ts_type == TS_IPV4_ADDR_RANGE || ts2->ts_type == TS_IPV6_ADDR_RANGE); ikev2_payload_add_ts_entry (&data, ts2); } ikev2_payload_add_data (c, data); vec_free (data); } void ikev2_payload_chain_add_padding (ikev2_payload_chain_t * c, int bs) { u8 *tmp __attribute__ ((unused)); u8 pad_len = (vec_len (c->data) / bs + 1) * bs - vec_len (c->data); vec_add2 (c->data, tmp, pad_len); c->data[vec_len (c->data) - 1] = pad_len - 1; } ikev2_sa_proposal_t * ikev2_parse_sa_payload (ike_payload_header_t * ikep, u32 rlen) { ikev2_sa_proposal_t *v = 0; ikev2_sa_proposal_t *proposal; ikev2_sa_transform_t *transform; u32 plen = clib_net_to_host_u16 (ikep->length); ike_sa_proposal_data_t *sap; int proposal_ptr = 0; if (sizeof (*ikep) > rlen) return 0; rlen -= sizeof (*ikep); do { if (proposal_ptr + sizeof (*sap) > rlen) goto data_corrupted; sap = (ike_sa_proposal_data_t *) & ikep->payload[proposal_ptr]; int i, transform_ptr; /* IKE proposal should not have SPI */ if (sap->protocol_id == IKEV2_PROTOCOL_IKE && sap->spi_size != 0) goto data_corrupted; /* IKE proposal should not have SPI */ if (sap->protocol_id == IKEV2_PROTOCOL_ESP && sap->spi_size != 4) goto data_corrupted; transform_ptr = proposal_ptr + sizeof (*sap) + sap->spi_size; if (transform_ptr > rlen) goto data_corrupted; vec_add2 (v, proposal, 1); proposal->proposal_num = sap->proposal_num; proposal->protocol_id = sap->protocol_id; if (sap->spi_size == 4) { proposal->spi = clib_net_to_host_u32 (sap->spi[0]); } for (i = 0; i < sap->num_transforms; i++) { ike_sa_transform_data_t *tr = (ike_sa_transform_data_t *) & ikep->payload[transform_ptr]; if (transform_ptr + sizeof (*tr) > rlen) goto data_corrupted; u16 tlen = clib_net_to_host_u16 (tr->transform_len); if (tlen < sizeof (*tr)) goto data_corrupted; vec_add2 (proposal->transforms, transform, 1); transform->type = tr->transform_type; transform->transform_id = clib_net_to_host_u16 (tr->transform_id); if (transform_ptr + tlen > rlen) goto data_corrupted; if (tlen > sizeof (*tr)) vec_add (transform->attrs, tr->attributes, tlen - sizeof (*tr)); transform_ptr += tlen; } proposal_ptr += clib_net_to_host_u16 (sap->proposal_len); } while (proposal_ptr < (plen - sizeof (*ikep)) && sap->last_or_more == 2); /* data validation */ if (proposal_ptr != (plen - sizeof (*ikep)) || sap->last_or_more) goto data_corrupted; return v; data_corrupted: ikev2_elog_detail ("SA payload data corrupted"); ikev2_sa_free_proposal_vector (&v); return 0; } ikev2_ts_t * ikev2_parse_ts_payload (ike_payload_header_t * ikep, u32 rlen) { ike_ts_payload_header_t *tsp = (ike_ts_payload_header_t *) ikep; ikev2_ts_t *r = 0, *ts; ikev2_ip4_addr_pair_t *pair4; ikev2_ip6_addr_pair_t *pair6; int p = 0, n_left; ikev2_ts_payload_entry_t *pe; if (sizeof (*tsp) > rlen) return 0; rlen -= sizeof (*tsp); n_left = tsp->num_ts; while (n_left && p + sizeof (*pe) < rlen) { pe = (ikev2_ts_payload_entry_t *) (((u8 *)tsp->ts) + p); p += sizeof (*pe); if (pe->ts_type != TS_IPV4_ADDR_RANGE && pe->ts_type != TS_IPV6_ADDR_RANGE) { ikev2_elog_uint (IKEV2_LOG_ERROR, "unsupported TS type received (%u)", pe->ts_type); return 0; } vec_add2 (r, ts, 1); ts->ts_type = pe->ts_type; ts->protocol_id = pe->protocol_id; ts->start_port = pe->start_port; ts->end_port = pe->end_port; if (pe->ts_type == TS_IPV4_ADDR_RANGE) { pair4 = (ikev2_ip4_addr_pair_t*) pe->addr_pair; ip_address_set (&ts->start_addr, &pair4->start_addr, AF_IP4); ip_address_set (&ts->end_addr, &pair4->end_addr, AF_IP4); p += sizeof (*pair4); } else { pair6 = (ikev2_ip6_addr_pair_t*) pe->addr_pair; ip_address_set (&ts->start_addr, &pair6->start_addr, AF_IP6); ip_address_set (&ts->end_addr, &pair6->end_addr, AF_IP6); p += sizeof (*pair6); } n_left--; } if (n_left) return 0; return r; } ikev2_notify_t * ikev2_parse_notify_payload (ike_payload_header_t * ikep, u32 rlen) { ike_notify_payload_header_t *n = (ike_notify_payload_header_t *) ikep; u32 plen = clib_net_to_host_u16 (n->length); ikev2_notify_t *r = 0; u32 spi; if (sizeof (*n) > rlen) return 0; r = vec_new (ikev2_notify_t, 1); r->msg_type = clib_net_to_host_u16 (n->msg_type); r->protocol_id = n->protocol_id; if (n->spi_size == 4) { if (sizeof (spi) + sizeof (*n) > rlen) goto cleanup; clib_memcpy (&spi, n->payload, n->spi_size); r->spi = clib_net_to_host_u32 (spi); } else if (n->spi_size == 0) { r->spi = 0; } else { clib_warning ("invalid SPI Size %d", n->spi_size); goto cleanup; } if (plen > (sizeof (*n) + n->spi_size)) { if (plen <= sizeof (*n) + n->spi_size) goto cleanup; u32 data_len = plen - sizeof (*n) - n->spi_size; vec_add (r->data, n->payload + n->spi_size, data_len); } return r; cleanup: vec_free (r); return 0; } void ikev2_parse_vendor_payload (ike_payload_header_t * ikep) { u32 plen = clib_net_to_host_u16 (ikep->length); ikev2_elog_uint (IKEV2_LOG_DEBUG, "vendor payload skipped, len %d", plen); } ikev2_delete_t * ikev2_parse_delete_payload (ike_payload_header_t * ikep, u32 rlen) { ike_delete_payload_header_t * d = (ike_delete_payload_header_t *) ikep; ikev2_delete_t *r = 0, *del; u16 i, num_of_spi; if (rlen < sizeof (*d)) return 0; num_of_spi = clib_net_to_host_u16 (d->num_of_spi); if (d->protocol_id == IKEV2_PROTOCOL_IKE) { r = vec_new (ikev2_delete_t, 1); r->protocol_id = 1; } else { if (sizeof (*d) + num_of_spi * sizeof (u32) > rlen) return 0; for (i = 0; i < num_of_spi; i++) { vec_add2 (r, del, 1); del->protocol_id = d->protocol_id; del->spi = clib_net_to_host_u32 (d->spi[i]); } } return r; } u8 * ikev2_find_ike_notify_payload (ike_header_t * ike, u32 msg_type) { int p = 0; ike_notify_payload_header_t *n; ike_payload_header_t *ikep; u32 payload = ike->nextpayload; while (payload != IKEV2_PAYLOAD_NONE) { ikep = (ike_payload_header_t *) & ike->payload[p]; if (payload == IKEV2_PAYLOAD_NOTIFY) { n = (ike_notify_payload_header_t *)ikep; if (n->msg_type == clib_net_to_host_u16 (msg_type)) return n->payload; } u16 plen = clib_net_to_host_u16 (ikep->length); payload = ikep->nextpayload; p += plen; } return 0; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */