Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | #include <vnet/vnet.h> |
| 17 | #include <vppinfra/error.h> |
| 18 | #include <vnet/lisp-cp/lisp_cp_messages.h> |
| 19 | #include <vnet/lisp-cp/control.h> |
| 20 | #include <vnet/lisp-cp/lisp_msg_serdes.h> |
| 21 | #include <vlibapi/api.h> |
| 22 | #include <vnet/lisp-cp/packets.h> |
| 23 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 24 | #define _assert(e) \ |
| 25 | error = CLIB_ERROR_ASSERT (e); \ |
| 26 | if (error) \ |
| 27 | goto done; |
| 28 | |
| 29 | static void print_chunk(u8 * b, int * offset, int c, char * des) |
| 30 | { |
| 31 | int i, n = offset[0] + c;; |
| 32 | for (i = offset[0]; i < n; i++) |
| 33 | { |
| 34 | printf("0x%02x, ", b[i]); |
| 35 | } |
| 36 | printf(" // %s\n", des); |
| 37 | *offset += c; |
| 38 | } |
| 39 | |
| 40 | void print_map_request(map_request_hdr_t * h) |
| 41 | { |
| 42 | #define pchunk(_count, _desc) \ |
| 43 | print_chunk((u8 *)h, &offset, _count, _desc) |
| 44 | |
| 45 | int offset = 0; |
| 46 | |
| 47 | pchunk(4, "data"); |
| 48 | pchunk(8, "Nonce"); |
| 49 | pchunk(2, "Source-EID-AFI"); |
| 50 | pchunk(4, "Source EID Address"); |
| 51 | pchunk(2, "ITR-RLOC-AFI 1"); |
| 52 | pchunk(4, "ITR-RLOC Address 1"); |
| 53 | pchunk(2, "ITR-RLOC-AFI 2"); |
| 54 | pchunk(16, "ITR-RLOC Address 2"); |
| 55 | pchunk(1, "REC: reserved"); |
| 56 | pchunk(1, "REC: EID mask-len"); |
| 57 | pchunk(2, "REC: EID-prefix-AFI"); |
| 58 | pchunk(4, "REC: EID-prefix"); |
| 59 | printf("\n"); |
| 60 | } |
| 61 | |
| 62 | static clib_error_t * test_lisp_msg_push_ecm () |
| 63 | { |
| 64 | vlib_main_t * vm = vlib_get_main (); |
| 65 | clib_error_t * error = 0; |
| 66 | gid_address_t la, ra; |
| 67 | vlib_buffer_t * b = 0; |
| 68 | u32 buff_len = 900; |
| 69 | int lp = 0x15, rp = 0x14; |
| 70 | |
| 71 | b = clib_mem_alloc (buff_len); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 72 | clib_memset((u8 *)b, 0, buff_len); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 73 | b->current_length = buff_len; |
| 74 | b->current_data = sizeof(udp_header_t) + sizeof(ip4_header_t) + |
| 75 | sizeof(ecm_hdr_t) + 1; |
| 76 | |
| 77 | la.type = GID_ADDR_IP_PREFIX; |
| 78 | la.ippref.addr.ip.v4.as_u32 = 0xa1b2c3d4; |
| 79 | la.ippref.addr.version = IP4; |
| 80 | |
| 81 | ra.type = GID_ADDR_IP_PREFIX; |
| 82 | ra.ippref.addr.ip.v4.as_u32 = 0x90817263; |
| 83 | ra.ippref.addr.version = IP4; |
| 84 | |
| 85 | ecm_hdr_t * lh = lisp_msg_push_ecm (vm, b, lp, rp, &la, &ra); |
| 86 | |
| 87 | u8 expected_ecm_hdr[] = { |
| 88 | 0x80, 0x00, 0x00, 0x00 |
| 89 | }; |
| 90 | _assert(0 == memcmp(expected_ecm_hdr, lh, sizeof(expected_ecm_hdr))); |
| 91 | |
| 92 | ip4_header_t * ih = (ip4_header_t *) (lh + 1); |
| 93 | /* clear ip checksum */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 94 | clib_memset((u8 *)ih + 10, 0, 2); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 95 | |
| 96 | u8 expected_ip4_hdr[] = { |
| 97 | 0x45, /* version; IHL */ |
| 98 | 0x00, /* services */ |
| 99 | 0x03, 0xa0, /* total length */ |
| 100 | 0x00, 0x00, /* identification */ |
| 101 | 0x40, 0x00, /* flags; fragment offset*/ |
| 102 | 0xff, /* TTL */ |
| 103 | 0x11, /* protocol */ |
| 104 | 0x00, 0x00, /* header checksum */ |
| 105 | 0xd4, 0xc3, 0xb2, 0xa1, /* src IP */ |
| 106 | 0x63, 0x72, 0x81, 0x90, /* dst IP */ |
| 107 | }; |
| 108 | _assert(0 == memcmp(ih, expected_ip4_hdr, sizeof(expected_ip4_hdr))); |
| 109 | |
| 110 | udp_header_t * uh = (udp_header_t *) (ih + 1); |
| 111 | /* clear udp checksum */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 112 | clib_memset((u8 *)uh + 6, 0, 2); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 113 | |
| 114 | u8 expected_udp_hdr[] = { |
| 115 | 0x00, 0x15, /* src port */ |
| 116 | 0x00, 0x14, /* dst port */ |
| 117 | 0x03, 0x8c, /* length */ |
| 118 | 0x00, 0x00, /* checksum */ |
| 119 | }; |
| 120 | _assert(0 == memcmp(uh, expected_udp_hdr, sizeof(expected_udp_hdr))); |
| 121 | |
| 122 | done: |
| 123 | clib_mem_free (b); |
| 124 | return error; |
| 125 | } |
| 126 | |
| 127 | static clib_error_t * test_lisp_msg_parse_mapping_record () |
| 128 | { |
| 129 | clib_error_t * error = 0; |
| 130 | locator_t probed; |
| 131 | locator_t * locs = 0; |
| 132 | vlib_buffer_t * b = 0; |
| 133 | gid_address_t eid; |
| 134 | u32 buff_len = 500; |
| 135 | |
| 136 | b = clib_mem_alloc (buff_len); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 137 | clib_memset((u8 *)b, 0, buff_len); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 138 | |
| 139 | u8 map_reply_records[] = { |
| 140 | /* 1. record */ |
| 141 | 0x01, 0x02, 0x03, 0x04, /* record TTL */ |
| 142 | 0x01, /* locator count */ |
| 143 | 0x00, 0x00, 0x00, /* eid-mask-len; ... */ |
| 144 | 0x00, 0x00, /* reserved; map-version num */ |
| 145 | 0x00, 0x01, /* EID-Prefix-AFI */ |
| 146 | 0x33, 0x44, 0x55, 0x66, /* eid-prefix */ |
| 147 | /* loc */ |
| 148 | 0x0a, /* prority */ |
| 149 | 0x0b, /* weight */ |
| 150 | 0x0c, /* m-prority */ |
| 151 | 0x0d, /* m-weight */ |
| 152 | 0x00, 0x00, /* unused flags */ |
| 153 | 0x00, 0x01, /* Loc-AFI */ |
| 154 | 0xaa, 0xbb, 0xcc, 0xdd, /* Loator */ |
| 155 | }; |
| 156 | b->current_length = buff_len; |
| 157 | clib_memcpy(b->data, map_reply_records, sizeof(map_reply_records)); |
| 158 | |
| 159 | lisp_msg_parse_mapping_record (b, &eid, &locs, &probed); |
| 160 | _assert(vec_len (locs) == 1); |
| 161 | _assert(eid.ippref.addr.ip.v4.as_u32 == 0x66554433); |
| 162 | _assert(locs[0].local == 0); |
| 163 | _assert(locs[0].address.ippref.addr.ip.v4.as_u32 == 0xddccbbaa); |
| 164 | _assert(locs[0].address.type == GID_ADDR_IP_PREFIX); |
| 165 | _assert(locs[0].priority == 0xa); |
| 166 | _assert(locs[0].weight == 0xb); |
| 167 | _assert(locs[0].mpriority == 0xc); |
| 168 | _assert(locs[0].mweight == 0xd); |
| 169 | |
| 170 | done: |
| 171 | clib_mem_free (b); |
| 172 | if (locs) |
| 173 | vec_free (locs); |
| 174 | return error; |
| 175 | } |
| 176 | |
| 177 | static map_request_hdr_t * |
| 178 | build_map_request (lisp_cp_main_t * lcm, vlib_buffer_t * b, |
| 179 | gid_address_t * rlocs) |
| 180 | { |
| 181 | gid_address_t _seid, * seid = &_seid; |
| 182 | gid_address_t _deid, * deid = &_deid; |
| 183 | u8 is_smr_invoked = 1; |
| 184 | u8 rloc_probe_set = 0; |
| 185 | u64 nonce = 0; |
| 186 | map_request_hdr_t * h = 0; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 187 | clib_memset (deid, 0, sizeof (deid[0])); |
| 188 | clib_memset (seid, 0, sizeof (seid[0])); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 189 | |
| 190 | gid_address_type (seid) = GID_ADDR_IP_PREFIX; |
| 191 | ip_address_t * ip_addr = &gid_address_ip (seid); |
| 192 | ip_addr_v4 (ip_addr).as_u32 = 0x12345678; |
| 193 | seid->ippref.addr.version = IP4; |
| 194 | |
| 195 | gid_address_type (deid) = GID_ADDR_IP_PREFIX; |
| 196 | ip_address_t * ip_addr2 = &gid_address_ip (deid); |
| 197 | ip_addr_v4 (ip_addr2).as_u32 = 0x9abcdef0; |
| 198 | deid->ippref.addr.version = IP4; |
| 199 | gid_address_ippref_len (deid) = 24; |
| 200 | |
| 201 | h = lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, |
| 202 | is_smr_invoked, rloc_probe_set, &nonce); |
| 203 | vec_free(rlocs); |
| 204 | return h; |
| 205 | } |
| 206 | |
| 207 | static void |
| 208 | generate_rlocs (gid_address_t **rlocs, u32 * count) |
| 209 | { |
| 210 | gid_address_t gid_addr_data, * gid_addr = &gid_addr_data; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 211 | clib_memset (gid_addr, 0, sizeof (gid_addr[0])); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 212 | ip_address_t * addr = &gid_address_ip (gid_addr); |
| 213 | |
| 214 | gid_address_type (gid_addr) = GID_ADDR_IP_PREFIX; |
| 215 | |
| 216 | ip_addr_version (addr) = IP4; |
| 217 | ip_addr_v4 (addr).data_u32 = 0x10203040; |
| 218 | vec_add1 (rlocs[0], gid_addr[0]); |
| 219 | |
| 220 | ip_addr_v6 (addr).as_u32[0] = 0xffeeddcc; |
| 221 | ip_addr_v6 (addr).as_u32[1] = 0xbbaa9988; |
| 222 | ip_addr_v6 (addr).as_u32[2] = 0x77665544; |
| 223 | ip_addr_v6 (addr).as_u32[3] = 0x33221100; |
| 224 | ip_addr_version (addr) = IP6; |
| 225 | vec_add1 (rlocs[0], gid_addr[0]); |
| 226 | } |
| 227 | |
| 228 | static clib_error_t * test_lisp_msg_parse () |
| 229 | { |
| 230 | gid_address_t eid; |
| 231 | lisp_cp_main_t * lcm = vnet_lisp_cp_get_main(); |
| 232 | map_request_hdr_t *h; |
| 233 | gid_address_t gid; |
| 234 | clib_error_t * error = 0; |
| 235 | vlib_buffer_t * b; |
| 236 | gid_address_t * rlocs_decode = 0, * rlocs = 0; |
| 237 | u32 rloc_count_parse = 0; |
| 238 | |
| 239 | u8 * data = clib_mem_alloc(500); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 240 | clib_memset(data, 0, 500); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 241 | b = (vlib_buffer_t *) data; |
| 242 | |
| 243 | generate_rlocs (&rlocs_decode, &rloc_count_parse); |
| 244 | h = build_map_request (lcm, b, rlocs_decode); |
| 245 | |
| 246 | vlib_buffer_pull(b, sizeof(*h)); |
| 247 | u32 len = lisp_msg_parse_addr(b, &gid); |
| 248 | _assert (len == 2 + 4 |
| 249 | /* Source-EID-AFI field lenght + IPv4 address length */); |
| 250 | _assert (gid.ippref.addr.ip.v4.as_u32 == 0x12345678); |
| 251 | _assert (gid.ippref.addr.version == IP4); |
| 252 | |
| 253 | u8 rloc_count = MREQ_ITR_RLOC_COUNT(h) + 1; |
| 254 | lisp_msg_parse_itr_rlocs (b, &rlocs, rloc_count); |
| 255 | |
| 256 | _assert (vec_len (rlocs) == 2); |
| 257 | _assert (rlocs[0].ippref.addr.ip.v4.as_u32 == 0x10203040); |
| 258 | _assert (rlocs[0].ippref.addr.version == IP4); |
| 259 | |
| 260 | _assert (rlocs[1].ippref.addr.ip.v6.as_u32[0] == 0xffeeddcc); |
| 261 | _assert (rlocs[1].ippref.addr.ip.v6.as_u32[1] == 0xbbaa9988); |
| 262 | _assert (rlocs[1].ippref.addr.ip.v6.as_u32[2] == 0x77665544); |
| 263 | _assert (rlocs[1].ippref.addr.ip.v6.as_u32[3] == 0x33221100); |
| 264 | _assert (rlocs[1].ippref.addr.version == IP6); |
| 265 | |
| 266 | lisp_msg_parse_eid_rec (b, &eid); |
| 267 | _assert (eid.ippref.addr.ip.v4.as_u32 == 0x9abcdef0); |
| 268 | _assert (eid.ippref.addr.version == IP4); |
| 269 | _assert (eid.ippref.len == 24); |
| 270 | |
| 271 | done: |
| 272 | clib_mem_free (data); |
| 273 | if (rlocs) |
| 274 | vec_free (rlocs); |
| 275 | return error; |
| 276 | } |
| 277 | |
| 278 | static clib_error_t * test_lisp_msg_put_mreq_with_lcaf () |
| 279 | { |
| 280 | lisp_cp_main_t * lcm = vnet_lisp_cp_get_main (); |
| 281 | clib_error_t * error = 0; |
| 282 | map_request_hdr_t *h = 0; |
| 283 | gid_address_t * rlocs = 0; |
| 284 | |
| 285 | ip_prefix_t ippref; |
| 286 | ip_prefix_version (&ippref) = IP4; |
| 287 | ip4_address_t * ip = &ip_prefix_v4 (&ippref); |
| 288 | ip->as_u32 = 0x11223344; |
| 289 | |
| 290 | gid_address_t g = |
| 291 | { |
| 292 | .type = GID_ADDR_IP_PREFIX, |
| 293 | .ippref = ippref, |
| 294 | .vni = 0x90919293, |
| 295 | .vni_mask = 0x17 |
| 296 | }; |
| 297 | vec_add1 (rlocs, g); |
| 298 | |
| 299 | u8 * data = clib_mem_alloc (500); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 300 | clib_memset (data, 0, 500); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 301 | |
| 302 | h = build_map_request (lcm, (vlib_buffer_t *) data, rlocs); |
| 303 | |
| 304 | /* clear Nonce to simplify comparison */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 305 | clib_memset ((u8 *)h + 4, 0, 8); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 306 | |
| 307 | u8 expected_data[] = |
| 308 | { |
| 309 | 0x10, 0x40, 0x00, 0x01, /* type; flags; IRC; REC count */ |
| 310 | 0x00, 0x00, 0x00, 0x00, |
| 311 | 0x00, 0x00, 0x00, 0x00, /* nonce */ |
| 312 | 0x00, 0x01, /* Source-EID-AFI */ |
| 313 | 0x78, 0x56, 0x34, 0x12, /* Source EID Address */ |
| 314 | |
| 315 | /* RLOCs */ |
| 316 | 0x40, 0x03, /* AFI = LCAF*/ |
| 317 | /* LCAF header*/ |
| 318 | 0x00, 0x00, /* reserved1, flags */ |
| 319 | 0x02, /* type = Instance ID */ |
| 320 | 0x17, /* IID mask-len */ |
| 321 | 0x00, 0x0a, /* lenght */ |
| 322 | 0x90, 0x91, 0x92, 0x93, /* IID / VNI */ |
| 323 | |
| 324 | 0x00, 0x01, /* AFI = ipv4 */ |
| 325 | 0x44, 0x33, 0x22, 0x11, /* ITR-RLOC Address 1 */ |
| 326 | |
| 327 | /* record */ |
| 328 | 0x00, /* reserved */ |
| 329 | 0x18, /* EID mask-len */ |
| 330 | 0x00, 0x01, /* EID-prefix-AFI */ |
| 331 | 0xf0, 0xde, 0xbc, 0x9a, /* EID-prefix */ |
| 332 | }; |
| 333 | |
| 334 | _assert (0 == memcmp (expected_data, (u8 *) h, sizeof (expected_data))); |
| 335 | done: |
| 336 | clib_mem_free (data); |
| 337 | return error; |
| 338 | } |
| 339 | |
| 340 | static clib_error_t * test_lisp_msg_put_mreq () |
| 341 | { |
| 342 | lisp_cp_main_t * lcm = vnet_lisp_cp_get_main(); |
| 343 | clib_error_t * error = 0; |
| 344 | map_request_hdr_t *h; |
| 345 | gid_address_t * rlocs = 0; |
| 346 | u32 rloc_count = 0; |
| 347 | |
| 348 | u8 * data = clib_mem_alloc(500); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 349 | clib_memset(data, 0, 500); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 350 | |
| 351 | generate_rlocs (&rlocs, &rloc_count); |
| 352 | h = build_map_request (lcm, (vlib_buffer_t *) data, rlocs); |
| 353 | |
| 354 | /* clear Nonce to simplify comparison */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 355 | clib_memset((u8 *)h + 4, 0, 8); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 356 | |
| 357 | print_map_request(h); |
| 358 | |
| 359 | u8 expected_data[50] = { |
| 360 | 0x10, 0x40, 0x01, 0x01, /* type; flags; IRC; REC count */ |
| 361 | 0x00, 0x00, 0x00, 0x00, |
| 362 | 0x00, 0x00, 0x00, 0x00, /* nonce */ |
| 363 | 0x00, 0x01, /* Source-EID-AFI */ |
| 364 | 0x78, 0x56, 0x34, 0x12, /* Source EID Address */ |
| 365 | |
| 366 | /* RLOCs */ |
| 367 | 0x00, 0x01, /* ITR-RLOC-AFI 1 */ |
| 368 | 0x40, 0x30, 0x20, 0x10, /* ITR-RLOC Address 1 */ |
| 369 | 0x00, 0x02, /* ITR-RLOC-AFI 2 */ |
| 370 | 0xcc, 0xdd, 0xee, 0xff, |
| 371 | 0x88, 0x99, 0xaa, 0xbb, |
| 372 | 0x44, 0x55, 0x66, 0x77, |
| 373 | 0x00, 0x11, 0x22, 0x33, /* ITR-RLOC Address 2 */ |
| 374 | |
| 375 | /* record */ |
| 376 | 0x00, /* reserved */ |
| 377 | 0x18, /* EID mask-len */ |
| 378 | 0x00, 0x01, /* EID-prefix-AFI */ |
| 379 | 0xf0, 0xde, 0xbc, 0x9a, /* EID-prefix */ |
| 380 | }; |
| 381 | _assert (0 == memcmp (expected_data, (u8 *) h, sizeof (expected_data))); |
| 382 | |
| 383 | done: |
| 384 | clib_mem_free (data); |
| 385 | return error; |
| 386 | } |
| 387 | |
| 388 | /* generate a vector of eid records */ |
| 389 | static mapping_t * |
| 390 | build_test_map_records () |
| 391 | { |
| 392 | mapping_t * records = 0; |
| 393 | |
| 394 | mapping_t r = { |
Filip Tehlar | 68d2e24 | 2017-04-21 12:05:58 +0200 | [diff] [blame] | 395 | .ttl = MAP_REGISTER_DEFAULT_TTL, |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 396 | .eid = { |
| 397 | .type = GID_ADDR_MAC, |
| 398 | .mac = {1, 2, 3, 4, 5, 6}, |
| 399 | .vni = 0x0 |
| 400 | } |
| 401 | }; |
| 402 | |
| 403 | locator_t loc = { |
| 404 | .weight = 1, |
| 405 | .priority = 2, |
| 406 | .local = 1, |
| 407 | .address = { |
| 408 | .type = GID_ADDR_IP_PREFIX, |
| 409 | .ippref = { |
| 410 | .addr = { |
| 411 | .ip.v4.as_u32 = 0x99887766, |
| 412 | .version = IP4 |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | }; |
| 417 | vec_add1 (r.locators, loc); |
| 418 | vec_add1 (records, r); |
| 419 | |
| 420 | return records; |
| 421 | } |
| 422 | |
| 423 | static void |
| 424 | free_test_map_records (mapping_t * maps) |
| 425 | { |
| 426 | mapping_t * map; |
| 427 | vec_foreach (map, maps) |
| 428 | { |
| 429 | vec_free (map->locators); |
| 430 | } |
| 431 | vec_free (maps); |
| 432 | } |
| 433 | |
| 434 | static clib_error_t * |
| 435 | test_lisp_map_register () |
| 436 | { |
| 437 | vlib_buffer_t *b; |
| 438 | clib_error_t * error = 0; |
| 439 | u64 nonce; |
| 440 | u32 msg_len = 0; |
| 441 | mapping_t * records = build_test_map_records (); |
| 442 | |
| 443 | u8 * data = clib_mem_alloc(500); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 444 | clib_memset(data, 0, 500); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 445 | b = (vlib_buffer_t *) data; |
| 446 | |
| 447 | lisp_msg_put_map_register (b, records, 1 /* want map notify */, |
| 448 | 20 /* length of HMAC_SHA_1_96 */, |
| 449 | &nonce, &msg_len); |
| 450 | free_test_map_records (records); |
| 451 | |
| 452 | /* clear Nonce to simplify comparison */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 453 | clib_memset((u8 *)b->data + 4, 0, 8); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 454 | |
| 455 | /* clear authentication data */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 456 | clib_memset ((u8 *)b->data + 16, 0, 20); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 457 | |
| 458 | u8 expected_data[] = { |
| 459 | 0x30, 0x00, 0x01, 0x01, /* type; rsvd; want notify; REC count */ |
| 460 | 0x00, 0x00, 0x00, 0x00, |
| 461 | 0x00, 0x00, 0x00, 0x00, /* nonce */ |
| 462 | 0x00, 0x00, 0x00, 0x00, /* key id, auth data length: |
| 463 | both are zeroes because those are set in another |
| 464 | function (see auth_data_len_by_key_id())*/ |
| 465 | 0x00, 0x00, 0x00, 0x00, |
| 466 | 0x00, 0x00, 0x00, 0x00, |
| 467 | 0x00, 0x00, 0x00, 0x00, |
| 468 | 0x00, 0x00, 0x00, 0x00, |
| 469 | 0x00, 0x00, 0x00, 0x00, /* auth data */ |
| 470 | |
| 471 | /* first record */ |
Filip Tehlar | 68d2e24 | 2017-04-21 12:05:58 +0200 | [diff] [blame] | 472 | 0x00, 0x00, 0x03, 0x84, /* default ttl (15 minues) */ |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 473 | 0x01, 0x00, 0x00, 0x00, /* loc count, eid len, ACT, A */ |
| 474 | 0x00, 0x00, 0x40, 0x05, /* rsvd, map ver num, AFI = MAC */ |
| 475 | 0x01, 0x02, 0x03, 0x04, |
| 476 | 0x05, 0x06, /* MAC EID */ |
| 477 | |
| 478 | /* locator 1 */ |
| 479 | 0x02, 0x01, 0x00, 0x00, /* prio, weight, mprio, mweight */ |
| 480 | 0x00, 0x04, 0x00, 0x01, /* flags, AFI = ipv4 */ |
| 481 | 0x66, 0x77, 0x88, 0x99, /* ipv4 locator address */ |
| 482 | }; |
| 483 | _assert (0 == memcmp (expected_data, b->data, sizeof (expected_data))); |
| 484 | done: |
| 485 | clib_mem_free (data); |
| 486 | return error; |
| 487 | } |
| 488 | |
Filip Tehlar | 816f437 | 2017-04-26 16:09:06 +0200 | [diff] [blame] | 489 | static vlib_buffer_t * |
| 490 | create_buffer (u8 * data, u32 data_len) |
| 491 | { |
| 492 | vlib_buffer_t *b; |
| 493 | |
| 494 | u8 *buf_data = clib_mem_alloc(500); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 495 | clib_memset (buf_data, 0, 500); |
Filip Tehlar | 816f437 | 2017-04-26 16:09:06 +0200 | [diff] [blame] | 496 | b = (vlib_buffer_t *)buf_data; |
| 497 | |
| 498 | u8 * p = vlib_buffer_put_uninit (b, data_len); |
| 499 | clib_memcpy (p, data, data_len); |
| 500 | |
| 501 | return b; |
| 502 | } |
| 503 | |
| 504 | static clib_error_t * |
| 505 | test_lisp_parse_map_reply () |
| 506 | { |
| 507 | clib_error_t * error = 0; |
| 508 | u8 map_reply_data[] = |
| 509 | { |
| 510 | 0x00, 0x00, 0x00, 0x01, /* type; rsvd; mapping count */ |
| 511 | 0x00, 0x00, 0x00, 0x00, |
| 512 | }; |
| 513 | vlib_buffer_t *b = create_buffer (map_reply_data, sizeof (map_reply_data)); |
| 514 | map_records_arg_t *mrecs = parse_map_reply (b); |
| 515 | _assert (0 == mrecs); |
| 516 | clib_mem_free (b); |
| 517 | |
| 518 | u8 map_reply_data2[] = |
| 519 | { |
| 520 | 0x00, 0x00, 0x00, 0x01, /* type; rsvd */ |
| 521 | 0x00, 0x00, 0x00, 0x00, |
| 522 | 0x00, 0x00, 0x00, 0x00, /* nonce */ |
| 523 | |
| 524 | /* 1. record - incomplete */ |
| 525 | 0x01, 0x02, 0x03, 0x04, /* record TTL */ |
| 526 | 0x01, /* locator count */ |
| 527 | }; |
| 528 | b = create_buffer (map_reply_data2, sizeof (map_reply_data2)); |
| 529 | mrecs = parse_map_reply (b); |
| 530 | _assert (0 == mrecs); |
| 531 | done: |
| 532 | clib_mem_free (b); |
| 533 | return error; |
| 534 | } |
| 535 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 536 | static clib_error_t * |
| 537 | test_lisp_parse_lcaf () |
| 538 | { |
| 539 | int i; |
| 540 | clib_error_t * error = 0; |
| 541 | gid_address_t eid; |
| 542 | locator_t * locs = 0; |
| 543 | locator_t probed; |
| 544 | vlib_buffer_t * b = 0; |
| 545 | u32 buff_len = 500; |
| 546 | |
| 547 | b = clib_mem_alloc (buff_len); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 548 | clib_memset ((u8 *)b, 0, buff_len); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 549 | |
| 550 | u8 map_reply_records[] = |
| 551 | { |
| 552 | /* 1. record */ |
| 553 | 0x01, 0x02, 0x03, 0x04, /* record TTL */ |
| 554 | 0x03, /* locator count */ |
| 555 | 0x00, 0x00, 0x00, /* eid-mask-len; ... */ |
| 556 | 0x00, 0x00, /* reserved; map-version num */ |
| 557 | 0x00, 0x01, /* EID-Prefix-AFI */ |
| 558 | 0x33, 0x44, 0x55, 0x66, /* eid-prefix */ |
| 559 | |
| 560 | /* 1st locator */ |
| 561 | 0x0a, /* prority */ |
| 562 | 0x0b, /* weight */ |
| 563 | 0x0c, /* m-prority */ |
| 564 | 0x0d, /* m-weight */ |
| 565 | 0x00, 0x00, /* unused flags */ |
| 566 | 0x40, 0x03, /* Loc-AFI = LCAF*/ |
| 567 | |
| 568 | /* LCAF header*/ |
| 569 | 0x00, 0x00, /* reserved1, flags */ |
| 570 | 0x02, /* type = Instance ID */ |
| 571 | 0x18, /* IID mask-len */ |
| 572 | 0x00, 0x0a, /* lenght */ |
| 573 | /* LCAF Instance ID */ |
| 574 | 0x00, 0x00, 0x00, 0x09, /* iid */ |
| 575 | 0x00, 0x01, /* AFI = ipv4 */ |
| 576 | 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 loator address */ |
| 577 | |
| 578 | /* 2nd locator */ |
| 579 | 0x07, /* prority */ |
| 580 | 0x06, /* weight */ |
| 581 | 0x05, /* m-prority */ |
| 582 | 0x04, /* m-weight */ |
| 583 | 0x00, 0x00, /* unused flags */ |
| 584 | 0x40, 0x03, /* Loc-AFI = LCAF*/ |
| 585 | |
| 586 | /* LCAF header*/ |
| 587 | 0x00, 0x00, /* reserved1, flags */ |
| 588 | 0x02, /* type = Instance ID */ |
| 589 | 0x18, /* IID mask-len */ |
| 590 | 0x00, 0x16, /* iid length + next AFI lenght */ |
| 591 | /* LCAF Instance ID */ |
| 592 | 0x22, 0x44, 0x66, 0x88, /* iid */ |
| 593 | 0x00, 0x02, /* AFI = ipv6 */ |
| 594 | 0xcc, 0xdd, 0xee, 0xff, |
| 595 | 0x88, 0x99, 0xaa, 0xbb, |
| 596 | 0x44, 0x55, 0x66, 0x77, |
| 597 | 0x00, 0x11, 0x22, 0x33, /* ipv6 locator address */ |
| 598 | |
| 599 | /* 3rd locator */ |
| 600 | 0x0a, /* prority */ |
| 601 | 0x0b, /* weight */ |
| 602 | 0x0c, /* m-prority */ |
| 603 | 0x0d, /* m-weight */ |
| 604 | 0x00, 0x00, /* unused flags */ |
| 605 | 0x00, 0x01, /* Loc-AFI */ |
| 606 | 0xaa, 0xbb, 0xcc, 0xdd, /* Loator */ |
| 607 | }; |
| 608 | |
| 609 | b->current_length = buff_len; |
| 610 | memcpy (b->data, map_reply_records, sizeof (map_reply_records)); |
| 611 | |
| 612 | lisp_msg_parse_mapping_record (b, &eid, &locs, &probed); |
| 613 | _assert (vec_len (locs) == 3); |
| 614 | _assert (eid.ippref.addr.ip.v4.as_u32 == 0x66554433); |
| 615 | |
| 616 | /* check 1st locator - an LCAF with ipv4 */ |
| 617 | _assert (locs[0].local == 0); |
| 618 | _assert (locs[0].priority == 0xa); |
| 619 | _assert (locs[0].weight == 0xb); |
| 620 | _assert (locs[0].mpriority == 0xc); |
| 621 | _assert (locs[0].mweight == 0xd); |
| 622 | |
| 623 | _assert (gid_address_type (&locs[0].address) == GID_ADDR_IP_PREFIX); |
| 624 | _assert (gid_address_vni (&locs[0].address) == 0x09); |
| 625 | ip_prefix_t * ip_pref = &gid_address_ippref (&locs[0].address); |
| 626 | _assert (IP4 == ip_prefix_version (ip_pref)); |
| 627 | |
| 628 | /* 2nd locator - LCAF entry with ipv6 address */ |
| 629 | _assert (locs[1].local == 0); |
| 630 | _assert (locs[1].priority == 0x7); |
| 631 | _assert (locs[1].weight == 0x6); |
| 632 | _assert (locs[1].mpriority == 0x5); |
| 633 | _assert (locs[1].mweight == 0x4); |
| 634 | |
| 635 | _assert (gid_address_type (&locs[1].address) == GID_ADDR_IP_PREFIX); |
| 636 | _assert (0x22446688 == gid_address_vni (&locs[1].address)); |
| 637 | ip_pref = &gid_address_ippref (&locs[1].address); |
| 638 | _assert (IP6 == ip_prefix_version (ip_pref)); |
| 639 | |
| 640 | /* 3rd locator - simple ipv4 address */ |
| 641 | _assert (gid_address_type (&locs[2].address) == GID_ADDR_IP_PREFIX); |
| 642 | done: |
| 643 | clib_mem_free (b); |
| 644 | |
| 645 | for (i = 0; i < 3; i++) |
| 646 | locator_free (&locs[i]); |
| 647 | vec_free (locs); |
| 648 | return error; |
| 649 | } |
| 650 | |
| 651 | #define foreach_test_case \ |
| 652 | _(lisp_msg_put_mreq) \ |
| 653 | _(lisp_msg_put_mreq_with_lcaf) \ |
| 654 | _(lisp_msg_push_ecm) \ |
| 655 | _(lisp_msg_parse) \ |
| 656 | _(lisp_msg_parse_mapping_record) \ |
Filip Tehlar | 816f437 | 2017-04-26 16:09:06 +0200 | [diff] [blame] | 657 | _(lisp_parse_map_reply) \ |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 658 | _(lisp_parse_lcaf) \ |
| 659 | _(lisp_map_register) |
| 660 | |
| 661 | int run_tests (void) |
| 662 | { |
| 663 | clib_error_t * error; |
| 664 | |
| 665 | #define _(_test_name) \ |
| 666 | error = test_ ## _test_name (); \ |
| 667 | if (error) \ |
| 668 | { \ |
| 669 | clib_error_report (error); \ |
| 670 | return 0; \ |
| 671 | } |
| 672 | |
| 673 | foreach_test_case |
| 674 | #undef _ |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | int main() |
| 680 | { |
| 681 | return run_tests (); |
| 682 | } |
| 683 | #undef _assert |