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