Loading...
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | /* * Copyright (c) 2016 Intel Corporation. * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr.h> #include <ztest.h> #include <net/net_core.h> #define NET_LOG_ENABLED 1 #define NET_SYS_LOG_LEVEL 4 #include "net_private.h" #include <net/net_ip.h> #include <net/net_pkt.h> #include <ieee802154_frame.h> #include <ipv6.h> struct ieee802154_pkt_test { char *name; struct in6_addr src; struct in6_addr dst; u8_t *pkt; u8_t length; struct { struct ieee802154_fcf_seq *fc_seq; struct ieee802154_address_field *dst_addr; struct ieee802154_address_field *src_addr; } mhr_check; }; u8_t ns_pkt[] = { 0x41, 0xd8, 0x3e, 0xcd, 0xab, 0xff, 0xff, 0xc2, 0xa3, 0x9e, 0x00, 0x00, 0x4b, 0x12, 0x00, 0x7b, 0x09, 0x3a, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x01, 0xff, 0x00, 0x00, 0x01, 0x87, 0x00, 0x2e, 0xad, 0x00, 0x00, 0x00, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x00, 0x12, 0x4b, 0x00, 0x00, 0x9e, 0xa3, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3d, 0x74 }; struct ieee802154_pkt_test test_ns_pkt = { .name = "NS frame", .src = { { { 0x20, 0x01, 0xdb, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } } }, .dst = { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x00, 0x00, 0x01 } } }, .pkt = ns_pkt, .length = sizeof(ns_pkt), .mhr_check.fc_seq = (struct ieee802154_fcf_seq *)ns_pkt, .mhr_check.dst_addr = (struct ieee802154_address_field *)(ns_pkt + 3), .mhr_check.src_addr = (struct ieee802154_address_field *)(ns_pkt + 7), }; u8_t ack_pkt[] = { 0x02, 0x10, 0x16 }; struct ieee802154_pkt_test test_ack_pkt = { .name = "ACK frame", .pkt = ack_pkt, .length = sizeof(ack_pkt), .mhr_check.fc_seq = (struct ieee802154_fcf_seq *)ack_pkt, .mhr_check.dst_addr = NULL, .mhr_check.src_addr = NULL, }; u8_t beacon_pkt[] = { 0x00, 0xd0, 0x11, 0xcd, 0xab, 0xc2, 0xa3, 0x9e, 0x00, 0x00, 0x4b, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; struct ieee802154_pkt_test test_beacon_pkt = { .name = "Empty beacon frame", .pkt = beacon_pkt, .length = sizeof(beacon_pkt), .mhr_check.fc_seq = (struct ieee802154_fcf_seq *)beacon_pkt, .mhr_check.dst_addr = NULL, .mhr_check.src_addr = (struct ieee802154_address_field *) (beacon_pkt + 3), }; u8_t sec_data_pkt[] = { 0x49, 0xd8, 0x03, 0xcd, 0xab, 0xff, 0xff, 0x02, 0x6d, 0xbb, 0xa7, 0x00, 0x4b, 0x12, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x8e, 0x49, 0xa7, 0xe2, 0x00, 0x67, 0xd4, 0x00, 0x42, 0x52, 0x6f, 0x01, 0x02, 0x00, 0x12, 0x4b, 0x00, 0xa7, 0xbb, 0x6d, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x7f, 0x91, 0xcf, 0x73, 0xf0 }; struct ieee802154_pkt_test test_sec_data_pkt = { .name = "Secured data frame", .pkt = sec_data_pkt, .length = sizeof(sec_data_pkt), .mhr_check.fc_seq = (struct ieee802154_fcf_seq *)sec_data_pkt, .mhr_check.dst_addr = (struct ieee802154_address_field *)(sec_data_pkt + 3), .mhr_check.src_addr = (struct ieee802154_address_field *)(sec_data_pkt + 7), }; struct net_pkt *current_pkt; struct net_if *iface; K_SEM_DEFINE(driver_lock, 0, UINT_MAX); static void pkt_hexdump(u8_t *pkt, u8_t length) { int i; printk(" -> Packet content:\n"); for (i = 0; i < length;) { int j; printk("\t"); for (j = 0; j < 10 && i < length; j++, i++) { printk("%02x ", *pkt); pkt++; } printk("\n"); } } static void ieee_addr_hexdump(u8_t *addr, u8_t length) { int i; printk(" -> IEEE 802.15.4 Address: "); for (i = 0; i < length-1; i++) { printk("%02x:", *addr); addr++; } printk("%02x\n", *addr); } static bool test_packet_parsing(struct ieee802154_pkt_test *t) { struct ieee802154_mpdu mpdu; NET_INFO("- Parsing packet 0x%p of frame %s\n", t->pkt, t->name); if (!ieee802154_validate_frame(t->pkt, t->length, &mpdu)) { NET_ERR("*** Could not validate frame %s\n", t->name); return false; } if (mpdu.mhr.fs != t->mhr_check.fc_seq || mpdu.mhr.dst_addr != t->mhr_check.dst_addr || mpdu.mhr.src_addr != t->mhr_check.src_addr) { NET_INFO("d: %p vs %p -- s: %p vs %p\n", mpdu.mhr.dst_addr, t->mhr_check.dst_addr, mpdu.mhr.src_addr, t->mhr_check.src_addr); NET_ERR("*** Wrong MPDU information on frame %s\n", t->name); return false; } return true; } static bool test_ns_sending(struct ieee802154_pkt_test *t) { struct ieee802154_mpdu mpdu; NET_INFO("- Sending NS packet\n"); if (net_ipv6_send_ns(iface, NULL, &t->src, &t->dst, &t->dst, false)) { NET_ERR("*** Could not create IPv6 NS packet\n"); return false; } k_sem_take(&driver_lock, 10); if (!current_pkt->frags) { NET_ERR("*** Could not send IPv6 NS packet\n"); return false; } pkt_hexdump(net_pkt_ll(current_pkt), net_pkt_get_len(current_pkt)); if (!ieee802154_validate_frame(net_pkt_ll(current_pkt), net_pkt_get_len(current_pkt), &mpdu)) { NET_ERR("*** Sent packet is not valid\n"); net_pkt_unref(current_pkt); return false; } net_pkt_frag_unref(current_pkt->frags); current_pkt->frags = NULL; return true; } static bool test_ack_reply(struct ieee802154_pkt_test *t) { static u8_t data_pkt[] = { 0x61, 0xdc, 0x16, 0xcd, 0xab, 0x26, 0x11, 0x32, 0x00, 0x00, 0x4b, 0x12, 0x00, 0x26, 0x18, 0x32, 0x00, 0x00, 0x4b, 0x12, 0x00, 0x7b, 0x00, 0x3a, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x87, 0x00, 0x8b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x16, 0xf0, 0x02, 0xff, 0x16, 0xf0, 0x12, 0xff, 0x16, 0xf0, 0x32, 0xff, 0x16, 0xf0, 0x00, 0xff, 0x16, 0xf0, 0x00, 0xff, 0x16 }; struct ieee802154_mpdu mpdu; struct net_pkt *pkt; struct net_buf *frag; NET_INFO("- Sending ACK reply to a data packet\n"); pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER); memcpy(frag->data, data_pkt, sizeof(data_pkt)); frag->len = sizeof(data_pkt); net_pkt_frag_add(pkt, frag); net_recv_data(iface, pkt); k_sem_take(&driver_lock, 20); /* an ACK packet should be in current_pkt */ if (!current_pkt->frags) { NET_ERR("*** No ACK reply sent\n"); return false; } pkt_hexdump(net_pkt_ll(current_pkt), net_pkt_get_len(current_pkt)); if (!ieee802154_validate_frame(net_pkt_ll(current_pkt), net_pkt_get_len(current_pkt), &mpdu)) { NET_ERR("*** ACK Reply is invalid\n"); return false; } if (memcmp(mpdu.mhr.fs, t->mhr_check.fc_seq, sizeof(struct ieee802154_fcf_seq))) { NET_ERR("*** ACK Reply does not compare\n"); return false; } net_pkt_frag_unref(current_pkt->frags); current_pkt->frags = NULL; return true; } static bool initialize_test_environment(void) { struct device *dev; k_sem_reset(&driver_lock); current_pkt = net_pkt_get_reserve_rx(0, K_FOREVER); if (!current_pkt) { NET_ERR("*** No buffer to allocate\n"); return false; } dev = device_get_binding("fake_ieee802154"); if (!dev) { NET_ERR("*** Could not get fake device\n"); return false; } iface = net_if_lookup_by_dev(dev); if (!iface) { NET_ERR("*** Could not get fake iface\n"); return false; } NET_INFO("Fake IEEE 802.15.4 network interface ready\n"); ieee_addr_hexdump(iface->link_addr.addr, 8); return true; } static void init_test(void) { bool ret; ret = initialize_test_environment(); zassert_true(ret, "Test initialization"); } static void parsing_ns_pkt(void) { bool ret; ret = test_packet_parsing(&test_ns_pkt); zassert_true(ret, "NS parsed"); } static void sending_ns_pkt(void) { bool ret; ret = test_ns_sending(&test_ns_pkt); zassert_true(ret, "NS sent"); } static void parsing_ack_pkt(void) { bool ret; ret = test_packet_parsing(&test_ack_pkt); zassert_true(ret, "ACK parsed"); } static void replying_ack_pkt(void) { bool ret; ret = test_ack_reply(&test_ack_pkt); zassert_true(ret, "ACK replied"); } static void parsing_beacon_pkt(void) { bool ret; ret = test_packet_parsing(&test_beacon_pkt); zassert_true(ret, "Beacon parsed"); } static void parsing_sec_data_pkt(void) { bool ret; ret = test_packet_parsing(&test_sec_data_pkt); zassert_true(ret, "Secured data frame parsed"); } void test_main(void) { ztest_test_suite(ieee802154_l2, ztest_unit_test(init_test), ztest_unit_test(parsing_ns_pkt), ztest_unit_test(sending_ns_pkt), ztest_unit_test(parsing_ack_pkt), ztest_unit_test(replying_ack_pkt), ztest_unit_test(parsing_beacon_pkt), ztest_unit_test(parsing_sec_data_pkt) ); ztest_run_test_suite(ieee802154_l2); } |