Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 National Instruments |
| 4 | * |
| 5 | * (C) Copyright 2015 |
| 6 | * Joe Hershberger <joe.hershberger@ni.com> |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 11 | #include <env.h> |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 12 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | #include <net.h> |
Viacheslav Mitrofanov | f96b109 | 2022-12-02 12:18:09 +0300 | [diff] [blame] | 16 | #include <net6.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 17 | #include <asm/eth.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 18 | #include <dm/test.h> |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 19 | #include <dm/device-internal.h> |
| 20 | #include <dm/uclass-internal.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 21 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 22 | #include <test/ut.h> |
Ehsan Mohandesi | 63c76c0 | 2023-04-21 17:08:23 -0700 | [diff] [blame] | 23 | #include <ndisc.h> |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 24 | |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 25 | #define DM_TEST_ETH_NUM 4 |
| 26 | |
Viacheslav Mitrofanov | f96b109 | 2022-12-02 12:18:09 +0300 | [diff] [blame] | 27 | #if IS_ENABLED(CONFIG_IPV6) |
| 28 | static int dm_test_string_to_ip6(struct unit_test_state *uts) |
| 29 | { |
| 30 | char *str; |
| 31 | struct test_ip6_pair { |
| 32 | char *string_addr; |
| 33 | struct in6_addr ip6_addr; |
| 34 | }; |
| 35 | |
| 36 | struct in6_addr ip6 = {0}; |
| 37 | |
| 38 | /* Correct statements */ |
| 39 | struct test_ip6_pair test_suite[] = { |
| 40 | {"2001:db8::0:1234:1", {.s6_addr32[0] = 0xb80d0120, |
| 41 | .s6_addr32[1] = 0x00000000, |
| 42 | .s6_addr32[2] = 0x00000000, |
| 43 | .s6_addr32[3] = 0x01003412}}, |
| 44 | {"2001:0db8:0000:0000:0000:0000:1234:0001", |
| 45 | {.s6_addr32[0] = 0xb80d0120, |
| 46 | .s6_addr32[1] = 0x00000000, |
| 47 | .s6_addr32[2] = 0x00000000, |
| 48 | .s6_addr32[3] = 0x01003412}}, |
| 49 | {"::1", {.s6_addr32[0] = 0x00000000, |
| 50 | .s6_addr32[1] = 0x00000000, |
| 51 | .s6_addr32[2] = 0x00000000, |
| 52 | .s6_addr32[3] = 0x01000000}}, |
| 53 | {"::ffff:192.168.1.1", {.s6_addr32[0] = 0x00000000, |
| 54 | .s6_addr32[1] = 0x00000000, |
| 55 | .s6_addr32[2] = 0xffff0000, |
| 56 | .s6_addr32[3] = 0x0101a8c0}}, |
| 57 | }; |
| 58 | |
| 59 | for (int i = 0; i < ARRAY_SIZE(test_suite); ++i) { |
| 60 | ut_assertok(string_to_ip6(test_suite[i].string_addr, |
| 61 | strlen(test_suite[i].string_addr), &ip6)); |
| 62 | ut_asserteq_mem(&ip6, &test_suite[i].ip6_addr, |
| 63 | sizeof(struct in6_addr)); |
| 64 | } |
| 65 | |
| 66 | /* Incorrect statements */ |
| 67 | str = "hello:world"; |
| 68 | ut_assertok(!string_to_ip6(str, strlen(str), &ip6)); |
| 69 | str = "2001:db8::0::0"; |
| 70 | ut_assertok(!string_to_ip6(str, strlen(str), &ip6)); |
| 71 | str = "2001:db8:192.168.1.1::1"; |
| 72 | ut_assertok(!string_to_ip6(str, strlen(str), &ip6)); |
| 73 | str = "192.168.1.1"; |
| 74 | ut_assertok(!string_to_ip6(str, strlen(str), &ip6)); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | DM_TEST(dm_test_string_to_ip6, 0); |
Viacheslav Mitrofanov | 224f563 | 2022-12-02 12:18:10 +0300 | [diff] [blame] | 79 | |
| 80 | static int dm_test_csum_ipv6_magic(struct unit_test_state *uts) |
| 81 | { |
| 82 | unsigned short csum = 0xbeef; |
| 83 | /* Predefined correct parameters */ |
| 84 | unsigned short correct_csum = 0xd8ac; |
| 85 | struct in6_addr saddr = {.s6_addr32[0] = 0x000080fe, |
| 86 | .s6_addr32[1] = 0x00000000, |
| 87 | .s6_addr32[2] = 0xffe9f242, |
| 88 | .s6_addr32[3] = 0xe8f66dfe}; |
| 89 | struct in6_addr daddr = {.s6_addr32[0] = 0x000080fe, |
| 90 | .s6_addr32[1] = 0x00000000, |
| 91 | .s6_addr32[2] = 0xffd5b372, |
| 92 | .s6_addr32[3] = 0x3ef692fe}; |
| 93 | u16 len = 1460; |
| 94 | unsigned short proto = 17; |
| 95 | unsigned int head_csum = 0x91f0; |
| 96 | |
| 97 | csum = csum_ipv6_magic(&saddr, &daddr, len, proto, head_csum); |
| 98 | ut_asserteq(csum, correct_csum); |
| 99 | |
| 100 | /* Broke a parameter */ |
| 101 | proto--; |
| 102 | csum = csum_ipv6_magic(&saddr, &daddr, len, proto, head_csum); |
| 103 | ut_assert(csum != correct_csum); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | DM_TEST(dm_test_csum_ipv6_magic, 0); |
Viacheslav Mitrofanov | ac6a9ce | 2022-12-02 12:18:11 +0300 | [diff] [blame] | 108 | |
| 109 | static int dm_test_ip6_addr_in_subnet(struct unit_test_state *uts) |
| 110 | { |
| 111 | struct in6_addr our = {.s6_addr32[0] = 0x000080fe, |
| 112 | .s6_addr32[1] = 0x00000000, |
| 113 | .s6_addr32[2] = 0xffe9f242, |
| 114 | .s6_addr32[3] = 0xe8f66dfe}; |
| 115 | struct in6_addr neigh1 = {.s6_addr32[0] = 0x000080fe, |
| 116 | .s6_addr32[1] = 0x00000000, |
| 117 | .s6_addr32[2] = 0xffd5b372, |
| 118 | .s6_addr32[3] = 0x3ef692fe}; |
| 119 | struct in6_addr neigh2 = {.s6_addr32[0] = 0x60480120, |
| 120 | .s6_addr32[1] = 0x00006048, |
| 121 | .s6_addr32[2] = 0x00000000, |
| 122 | .s6_addr32[3] = 0x00008888}; |
| 123 | |
| 124 | /* in */ |
| 125 | ut_assert(ip6_addr_in_subnet(&our, &neigh1, 64)); |
| 126 | /* outside */ |
| 127 | ut_assert(!ip6_addr_in_subnet(&our, &neigh2, 64)); |
| 128 | ut_assert(!ip6_addr_in_subnet(&our, &neigh1, 128)); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | DM_TEST(dm_test_ip6_addr_in_subnet, 0); |
Viacheslav Mitrofanov | 3b673c0 | 2022-12-02 12:18:12 +0300 | [diff] [blame] | 133 | |
| 134 | static int dm_test_ip6_make_snma(struct unit_test_state *uts) |
| 135 | { |
| 136 | struct in6_addr mult = {0}; |
| 137 | struct in6_addr correct_addr = { |
| 138 | .s6_addr32[0] = 0x000002ff, |
| 139 | .s6_addr32[1] = 0x00000000, |
| 140 | .s6_addr32[2] = 0x01000000, |
| 141 | .s6_addr32[3] = 0xe8f66dff}; |
| 142 | struct in6_addr addr = { .s6_addr32[0] = 0x000080fe, |
| 143 | .s6_addr32[1] = 0x00000000, |
| 144 | .s6_addr32[2] = 0xffe9f242, |
| 145 | .s6_addr32[3] = 0xe8f66dfe}; |
| 146 | |
| 147 | ip6_make_snma(&mult, &addr); |
| 148 | ut_asserteq_mem(&mult, &correct_addr, sizeof(struct in6_addr)); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | DM_TEST(dm_test_ip6_make_snma, 0); |
Viacheslav Mitrofanov | 5c8e0a6 | 2022-12-02 12:18:13 +0300 | [diff] [blame] | 153 | |
| 154 | static int dm_test_ip6_make_lladdr(struct unit_test_state *uts) |
| 155 | { |
| 156 | struct in6_addr generated_lladdr = {0}; |
| 157 | struct in6_addr correct_lladdr = { |
| 158 | .s6_addr32[0] = 0x000080fe, |
| 159 | .s6_addr32[1] = 0x00000000, |
| 160 | .s6_addr32[2] = 0xffabf33a, |
| 161 | .s6_addr32[3] = 0xfbb352fe}; |
| 162 | const unsigned char mac[6] = {0x38, 0xf3, 0xab, 0x52, 0xb3, 0xfb}; |
| 163 | |
| 164 | ip6_make_lladdr(&generated_lladdr, mac); |
| 165 | ut_asserteq_mem(&generated_lladdr, &correct_lladdr, |
| 166 | sizeof(struct in6_addr)); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | DM_TEST(dm_test_ip6_make_lladdr, UT_TESTF_SCAN_FDT); |
Viacheslav Mitrofanov | f96b109 | 2022-12-02 12:18:09 +0300 | [diff] [blame] | 171 | #endif |
| 172 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 173 | static int dm_test_eth(struct unit_test_state *uts) |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 174 | { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 175 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 176 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 177 | env_set("ethact", "eth@10002000"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 178 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 179 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 180 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 181 | env_set("ethact", "eth@10003000"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 182 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 183 | ut_asserteq_str("eth@10003000", env_get("ethact")); |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 184 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 185 | env_set("ethact", "eth@10004000"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 186 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 187 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 188 | |
| 189 | return 0; |
| 190 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 191 | DM_TEST(dm_test_eth, UT_TESTF_SCAN_FDT); |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 192 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 193 | static int dm_test_eth_alias(struct unit_test_state *uts) |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 194 | { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 195 | net_ping_ip = string_to_ip("1.1.2.2"); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 196 | env_set("ethact", "eth0"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 197 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 198 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 199 | |
Michael Walle | 7c41a22 | 2020-06-02 01:47:09 +0200 | [diff] [blame] | 200 | env_set("ethact", "eth6"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 201 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 202 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 203 | |
Claudiu Manoil | d9eaa92 | 2021-03-14 20:14:57 +0800 | [diff] [blame] | 204 | /* Expected to fail since eth1 is not defined in the device tree */ |
| 205 | env_set("ethact", "eth1"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 206 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 207 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 208 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 209 | env_set("ethact", "eth5"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 210 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 211 | ut_asserteq_str("eth@10003000", env_get("ethact")); |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 215 | DM_TEST(dm_test_eth_alias, UT_TESTF_SCAN_FDT); |
Joe Hershberger | aa52edd | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 216 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 217 | static int dm_test_eth_prime(struct unit_test_state *uts) |
Joe Hershberger | aa52edd | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 218 | { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 219 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | aa52edd | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 220 | |
| 221 | /* Expected to be "eth@10003000" because of ethprime variable */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 222 | env_set("ethact", NULL); |
| 223 | env_set("ethprime", "eth5"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 224 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 225 | ut_asserteq_str("eth@10003000", env_get("ethact")); |
Joe Hershberger | aa52edd | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 226 | |
| 227 | /* Expected to be "eth@10002000" because it is first */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 228 | env_set("ethact", NULL); |
| 229 | env_set("ethprime", NULL); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 230 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 231 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | aa52edd | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 232 | |
| 233 | return 0; |
| 234 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 235 | DM_TEST(dm_test_eth_prime, UT_TESTF_SCAN_FDT); |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 236 | |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 237 | /** |
| 238 | * This test case is trying to test the following scenario: |
| 239 | * - All ethernet devices are not probed |
| 240 | * - "ethaddr" for all ethernet devices are not set |
| 241 | * - "ethact" is set to a valid ethernet device name |
| 242 | * |
| 243 | * With Sandbox default test configuration, all ethernet devices are |
| 244 | * probed after power-up, so we have to manually create such scenario: |
| 245 | * - Remove all ethernet devices |
| 246 | * - Remove all "ethaddr" environment variables |
| 247 | * - Set "ethact" to the first ethernet device |
| 248 | * |
| 249 | * Do a ping test to see if anything goes wrong. |
| 250 | */ |
| 251 | static int dm_test_eth_act(struct unit_test_state *uts) |
| 252 | { |
| 253 | struct udevice *dev[DM_TEST_ETH_NUM]; |
| 254 | const char *ethname[DM_TEST_ETH_NUM] = {"eth@10002000", "eth@10003000", |
| 255 | "sbe5", "eth@10004000"}; |
| 256 | const char *addrname[DM_TEST_ETH_NUM] = {"ethaddr", "eth5addr", |
Michael Walle | 7c41a22 | 2020-06-02 01:47:09 +0200 | [diff] [blame] | 257 | "eth3addr", "eth6addr"}; |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 258 | char ethaddr[DM_TEST_ETH_NUM][18]; |
| 259 | int i; |
| 260 | |
Tom Rini | 0ac17d8 | 2017-09-26 14:08:30 -0400 | [diff] [blame] | 261 | memset(ethaddr, '\0', sizeof(ethaddr)); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 262 | net_ping_ip = string_to_ip("1.1.2.2"); |
| 263 | |
| 264 | /* Prepare the test scenario */ |
| 265 | for (i = 0; i < DM_TEST_ETH_NUM; i++) { |
Sean Anderson | 85e8fc8 | 2023-10-28 18:57:27 -0400 | [diff] [blame] | 266 | char *addr; |
| 267 | |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 268 | ut_assertok(uclass_find_device_by_name(UCLASS_ETH, |
| 269 | ethname[i], &dev[i])); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 270 | ut_assertok(device_remove(dev[i], DM_REMOVE_NORMAL)); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 271 | |
| 272 | /* Invalidate MAC address */ |
Sean Anderson | 85e8fc8 | 2023-10-28 18:57:27 -0400 | [diff] [blame] | 273 | addr = env_get(addrname[i]); |
| 274 | ut_assertnonnull(addr); |
| 275 | strncpy(ethaddr[i], addr, 17); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 276 | /* Must disable access protection for ethaddr before clearing */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 277 | env_set(".flags", addrname[i]); |
| 278 | env_set(addrname[i], NULL); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* Set ethact to "eth@10002000" */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 282 | env_set("ethact", ethname[0]); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 283 | |
| 284 | /* Segment fault might happen if something is wrong */ |
| 285 | ut_asserteq(-ENODEV, net_loop(PING)); |
| 286 | |
| 287 | for (i = 0; i < DM_TEST_ETH_NUM; i++) { |
| 288 | /* Restore the env */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 289 | env_set(".flags", addrname[i]); |
| 290 | env_set(addrname[i], ethaddr[i]); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 291 | |
| 292 | /* Probe the device again */ |
| 293 | ut_assertok(device_probe(dev[i])); |
| 294 | } |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 295 | env_set(".flags", NULL); |
| 296 | env_set("ethact", NULL); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 300 | DM_TEST(dm_test_eth_act, UT_TESTF_SCAN_FDT); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 301 | |
Sean Anderson | 86f5cc7 | 2022-05-05 13:11:32 -0400 | [diff] [blame] | 302 | /* Ensure that all addresses are loaded properly */ |
| 303 | static int dm_test_ethaddr(struct unit_test_state *uts) |
| 304 | { |
| 305 | static const char *const addr[] = { |
| 306 | "02:00:11:22:33:44", |
| 307 | "02:00:11:22:33:48", /* dsa slave */ |
| 308 | "02:00:11:22:33:45", |
| 309 | "02:00:11:22:33:48", /* dsa master */ |
| 310 | "02:00:11:22:33:46", |
| 311 | "02:00:11:22:33:47", |
| 312 | "02:00:11:22:33:48", /* dsa slave */ |
| 313 | "02:00:11:22:33:49", |
| 314 | }; |
| 315 | int i; |
| 316 | |
| 317 | for (i = 0; i < ARRAY_SIZE(addr); i++) { |
| 318 | char addrname[10]; |
Sean Anderson | 85e8fc8 | 2023-10-28 18:57:27 -0400 | [diff] [blame] | 319 | char *env_addr; |
Sean Anderson | 86f5cc7 | 2022-05-05 13:11:32 -0400 | [diff] [blame] | 320 | |
| 321 | if (i) |
| 322 | snprintf(addrname, sizeof(addrname), "eth%daddr", i + 1); |
| 323 | else |
| 324 | strcpy(addrname, "ethaddr"); |
Sean Anderson | 85e8fc8 | 2023-10-28 18:57:27 -0400 | [diff] [blame] | 325 | |
| 326 | env_addr = env_get(addrname); |
| 327 | ut_assertnonnull(env_addr); |
| 328 | ut_asserteq_str(addr[i], env_addr); |
Sean Anderson | 86f5cc7 | 2022-05-05 13:11:32 -0400 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | DM_TEST(dm_test_ethaddr, UT_TESTF_SCAN_FDT); |
| 334 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 335 | /* The asserts include a return on fail; cleanup in the caller */ |
| 336 | static int _dm_test_eth_rotate1(struct unit_test_state *uts) |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 337 | { |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 338 | /* Make sure that the default is to rotate to the next interface */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 339 | env_set("ethact", "eth@10004000"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 340 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 341 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 342 | |
| 343 | /* If ethrotate is no, then we should fail on a bad MAC */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 344 | env_set("ethact", "eth@10004000"); |
| 345 | env_set("ethrotate", "no"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 346 | ut_asserteq(-EINVAL, net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 347 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 348 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 349 | return 0; |
| 350 | } |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 351 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 352 | static int _dm_test_eth_rotate2(struct unit_test_state *uts) |
| 353 | { |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 354 | /* Make sure we can skip invalid devices */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 355 | env_set("ethact", "eth@10004000"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 356 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 357 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 358 | |
Bin Meng | 04a11cb | 2015-08-27 22:25:53 -0700 | [diff] [blame] | 359 | /* Make sure we can handle device name which is not eth# */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 360 | env_set("ethact", "sbe5"); |
Bin Meng | 04a11cb | 2015-08-27 22:25:53 -0700 | [diff] [blame] | 361 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 362 | ut_asserteq_str("sbe5", env_get("ethact")); |
Bin Meng | 04a11cb | 2015-08-27 22:25:53 -0700 | [diff] [blame] | 363 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static int dm_test_eth_rotate(struct unit_test_state *uts) |
| 368 | { |
| 369 | char ethaddr[18]; |
| 370 | int retval; |
| 371 | |
| 372 | /* Set target IP to mock ping */ |
| 373 | net_ping_ip = string_to_ip("1.1.2.2"); |
| 374 | |
| 375 | /* Invalidate eth1's MAC address */ |
Tom Rini | 0ac17d8 | 2017-09-26 14:08:30 -0400 | [diff] [blame] | 376 | memset(ethaddr, '\0', sizeof(ethaddr)); |
Michael Walle | 7c41a22 | 2020-06-02 01:47:09 +0200 | [diff] [blame] | 377 | strncpy(ethaddr, env_get("eth6addr"), 17); |
| 378 | /* Must disable access protection for eth6addr before clearing */ |
| 379 | env_set(".flags", "eth6addr"); |
| 380 | env_set("eth6addr", NULL); |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 381 | |
| 382 | retval = _dm_test_eth_rotate1(uts); |
| 383 | |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 384 | /* Restore the env */ |
Michael Walle | 7c41a22 | 2020-06-02 01:47:09 +0200 | [diff] [blame] | 385 | env_set("eth6addr", ethaddr); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 386 | env_set("ethrotate", NULL); |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 387 | |
| 388 | if (!retval) { |
| 389 | /* Invalidate eth0's MAC address */ |
Tom Rini | 0ac17d8 | 2017-09-26 14:08:30 -0400 | [diff] [blame] | 390 | strncpy(ethaddr, env_get("ethaddr"), 17); |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 391 | /* Must disable access protection for ethaddr before clearing */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 392 | env_set(".flags", "ethaddr"); |
| 393 | env_set("ethaddr", NULL); |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 394 | |
| 395 | retval = _dm_test_eth_rotate2(uts); |
| 396 | |
| 397 | /* Restore the env */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 398 | env_set("ethaddr", ethaddr); |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 399 | } |
| 400 | /* Restore the env */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 401 | env_set(".flags", NULL); |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 402 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 403 | return retval; |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 404 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 405 | DM_TEST(dm_test_eth_rotate, UT_TESTF_SCAN_FDT); |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 406 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 407 | /* The asserts include a return on fail; cleanup in the caller */ |
| 408 | static int _dm_test_net_retry(struct unit_test_state *uts) |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 409 | { |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 410 | /* |
| 411 | * eth1 is disabled and netretry is yes, so the ping should succeed and |
| 412 | * the active device should be eth0 |
| 413 | */ |
| 414 | sandbox_eth_disable_response(1, true); |
Claudiu Manoil | d9eaa92 | 2021-03-14 20:14:57 +0800 | [diff] [blame] | 415 | env_set("ethact", "lan1"); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 416 | env_set("netretry", "yes"); |
Joe Hershberger | 6901f43 | 2015-04-21 13:57:20 -0500 | [diff] [blame] | 417 | sandbox_eth_skip_timeout(); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 418 | ut_assertok(net_loop(PING)); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 419 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * eth1 is disabled and netretry is no, so the ping should fail and the |
| 423 | * active device should be eth1 |
| 424 | */ |
Claudiu Manoil | d9eaa92 | 2021-03-14 20:14:57 +0800 | [diff] [blame] | 425 | env_set("ethact", "lan1"); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 426 | env_set("netretry", "no"); |
Joe Hershberger | 6901f43 | 2015-04-21 13:57:20 -0500 | [diff] [blame] | 427 | sandbox_eth_skip_timeout(); |
Thomas RIENOESSL | 2dee419 | 2018-11-21 15:56:07 +0100 | [diff] [blame] | 428 | ut_asserteq(-ENONET, net_loop(PING)); |
Claudiu Manoil | d9eaa92 | 2021-03-14 20:14:57 +0800 | [diff] [blame] | 429 | ut_asserteq_str("lan1", env_get("ethact")); |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 430 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | static int dm_test_net_retry(struct unit_test_state *uts) |
| 435 | { |
| 436 | int retval; |
| 437 | |
| 438 | net_ping_ip = string_to_ip("1.1.2.2"); |
| 439 | |
| 440 | retval = _dm_test_net_retry(uts); |
| 441 | |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 442 | /* Restore the env */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 443 | env_set("netretry", NULL); |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 444 | sandbox_eth_disable_response(1, false); |
| 445 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 446 | return retval; |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 447 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 448 | DM_TEST(dm_test_net_retry, UT_TESTF_SCAN_FDT); |
Joe Hershberger | 54418d0 | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 449 | |
| 450 | static int sb_check_arp_reply(struct udevice *dev, void *packet, |
| 451 | unsigned int len) |
| 452 | { |
| 453 | struct eth_sandbox_priv *priv = dev_get_priv(dev); |
| 454 | struct ethernet_hdr *eth = packet; |
| 455 | struct arp_hdr *arp; |
| 456 | /* Used by all of the ut_assert macros */ |
| 457 | struct unit_test_state *uts = priv->priv; |
| 458 | |
| 459 | if (ntohs(eth->et_protlen) != PROT_ARP) |
| 460 | return 0; |
| 461 | |
| 462 | arp = packet + ETHER_HDR_SIZE; |
| 463 | |
| 464 | if (ntohs(arp->ar_op) != ARPOP_REPLY) |
| 465 | return 0; |
| 466 | |
| 467 | /* This test would be worthless if we are not waiting */ |
| 468 | ut_assert(arp_is_waiting()); |
| 469 | |
| 470 | /* Validate response */ |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 471 | ut_asserteq_mem(eth->et_src, net_ethaddr, ARP_HLEN); |
| 472 | ut_asserteq_mem(eth->et_dest, priv->fake_host_hwaddr, ARP_HLEN); |
Joe Hershberger | 54418d0 | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 473 | ut_assert(eth->et_protlen == htons(PROT_ARP)); |
| 474 | |
| 475 | ut_assert(arp->ar_hrd == htons(ARP_ETHER)); |
| 476 | ut_assert(arp->ar_pro == htons(PROT_IP)); |
| 477 | ut_assert(arp->ar_hln == ARP_HLEN); |
| 478 | ut_assert(arp->ar_pln == ARP_PLEN); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 479 | ut_asserteq_mem(&arp->ar_sha, net_ethaddr, ARP_HLEN); |
Joe Hershberger | 54418d0 | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 480 | ut_assert(net_read_ip(&arp->ar_spa).s_addr == net_ip.s_addr); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 481 | ut_asserteq_mem(&arp->ar_tha, priv->fake_host_hwaddr, ARP_HLEN); |
Joe Hershberger | 54418d0 | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 482 | ut_assert(net_read_ip(&arp->ar_tpa).s_addr == |
| 483 | string_to_ip("1.1.2.4").s_addr); |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static int sb_with_async_arp_handler(struct udevice *dev, void *packet, |
| 489 | unsigned int len) |
| 490 | { |
| 491 | struct eth_sandbox_priv *priv = dev_get_priv(dev); |
| 492 | struct ethernet_hdr *eth = packet; |
| 493 | struct arp_hdr *arp = packet + ETHER_HDR_SIZE; |
| 494 | int ret; |
| 495 | |
| 496 | /* |
| 497 | * If we are about to generate a reply to ARP, first inject a request |
| 498 | * from another host |
| 499 | */ |
| 500 | if (ntohs(eth->et_protlen) == PROT_ARP && |
| 501 | ntohs(arp->ar_op) == ARPOP_REQUEST) { |
| 502 | /* Make sure sandbox_eth_recv_arp_req() knows who is asking */ |
| 503 | priv->fake_host_ipaddr = string_to_ip("1.1.2.4"); |
| 504 | |
| 505 | ret = sandbox_eth_recv_arp_req(dev); |
| 506 | if (ret) |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | sandbox_eth_arp_req_to_reply(dev, packet, len); |
| 511 | sandbox_eth_ping_req_to_reply(dev, packet, len); |
| 512 | |
| 513 | return sb_check_arp_reply(dev, packet, len); |
| 514 | } |
| 515 | |
| 516 | static int dm_test_eth_async_arp_reply(struct unit_test_state *uts) |
| 517 | { |
| 518 | net_ping_ip = string_to_ip("1.1.2.2"); |
| 519 | |
| 520 | sandbox_eth_set_tx_handler(0, sb_with_async_arp_handler); |
| 521 | /* Used by all of the ut_assert macros in the tx_handler */ |
| 522 | sandbox_eth_set_priv(0, uts); |
Joe Hershberger | 54418d0 | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 523 | |
| 524 | env_set("ethact", "eth@10002000"); |
Joe Hershberger | e79a518 | 2018-09-26 16:49:02 -0500 | [diff] [blame] | 525 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 54418d0 | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 526 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
| 527 | |
| 528 | sandbox_eth_set_tx_handler(0, NULL); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 533 | DM_TEST(dm_test_eth_async_arp_reply, UT_TESTF_SCAN_FDT); |
Joe Hershberger | 9716dc1 | 2018-09-26 16:49:01 -0500 | [diff] [blame] | 534 | |
| 535 | static int sb_check_ping_reply(struct udevice *dev, void *packet, |
| 536 | unsigned int len) |
| 537 | { |
| 538 | struct eth_sandbox_priv *priv = dev_get_priv(dev); |
| 539 | struct ethernet_hdr *eth = packet; |
| 540 | struct ip_udp_hdr *ip; |
| 541 | struct icmp_hdr *icmp; |
| 542 | /* Used by all of the ut_assert macros */ |
| 543 | struct unit_test_state *uts = priv->priv; |
| 544 | |
| 545 | if (ntohs(eth->et_protlen) != PROT_IP) |
| 546 | return 0; |
| 547 | |
| 548 | ip = packet + ETHER_HDR_SIZE; |
| 549 | |
| 550 | if (ip->ip_p != IPPROTO_ICMP) |
| 551 | return 0; |
| 552 | |
| 553 | icmp = (struct icmp_hdr *)&ip->udp_src; |
| 554 | |
| 555 | if (icmp->type != ICMP_ECHO_REPLY) |
| 556 | return 0; |
| 557 | |
| 558 | /* This test would be worthless if we are not waiting */ |
| 559 | ut_assert(arp_is_waiting()); |
| 560 | |
| 561 | /* Validate response */ |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 562 | ut_asserteq_mem(eth->et_src, net_ethaddr, ARP_HLEN); |
| 563 | ut_asserteq_mem(eth->et_dest, priv->fake_host_hwaddr, ARP_HLEN); |
Joe Hershberger | 9716dc1 | 2018-09-26 16:49:01 -0500 | [diff] [blame] | 564 | ut_assert(eth->et_protlen == htons(PROT_IP)); |
| 565 | |
| 566 | ut_assert(net_read_ip(&ip->ip_src).s_addr == net_ip.s_addr); |
| 567 | ut_assert(net_read_ip(&ip->ip_dst).s_addr == |
| 568 | string_to_ip("1.1.2.4").s_addr); |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static int sb_with_async_ping_handler(struct udevice *dev, void *packet, |
| 574 | unsigned int len) |
| 575 | { |
| 576 | struct eth_sandbox_priv *priv = dev_get_priv(dev); |
| 577 | struct ethernet_hdr *eth = packet; |
| 578 | struct arp_hdr *arp = packet + ETHER_HDR_SIZE; |
| 579 | int ret; |
| 580 | |
| 581 | /* |
| 582 | * If we are about to generate a reply to ARP, first inject a request |
| 583 | * from another host |
| 584 | */ |
| 585 | if (ntohs(eth->et_protlen) == PROT_ARP && |
| 586 | ntohs(arp->ar_op) == ARPOP_REQUEST) { |
| 587 | /* Make sure sandbox_eth_recv_arp_req() knows who is asking */ |
| 588 | priv->fake_host_ipaddr = string_to_ip("1.1.2.4"); |
| 589 | |
| 590 | ret = sandbox_eth_recv_ping_req(dev); |
| 591 | if (ret) |
| 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | sandbox_eth_arp_req_to_reply(dev, packet, len); |
| 596 | sandbox_eth_ping_req_to_reply(dev, packet, len); |
| 597 | |
| 598 | return sb_check_ping_reply(dev, packet, len); |
| 599 | } |
| 600 | |
| 601 | static int dm_test_eth_async_ping_reply(struct unit_test_state *uts) |
| 602 | { |
| 603 | net_ping_ip = string_to_ip("1.1.2.2"); |
| 604 | |
| 605 | sandbox_eth_set_tx_handler(0, sb_with_async_ping_handler); |
| 606 | /* Used by all of the ut_assert macros in the tx_handler */ |
| 607 | sandbox_eth_set_priv(0, uts); |
Joe Hershberger | 9716dc1 | 2018-09-26 16:49:01 -0500 | [diff] [blame] | 608 | |
| 609 | env_set("ethact", "eth@10002000"); |
Joe Hershberger | e79a518 | 2018-09-26 16:49:02 -0500 | [diff] [blame] | 610 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 9716dc1 | 2018-09-26 16:49:01 -0500 | [diff] [blame] | 611 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
| 612 | |
| 613 | sandbox_eth_set_tx_handler(0, NULL); |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 618 | DM_TEST(dm_test_eth_async_ping_reply, UT_TESTF_SCAN_FDT); |
Ehsan Mohandesi | 63c76c0 | 2023-04-21 17:08:23 -0700 | [diff] [blame] | 619 | |
| 620 | #if IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY) |
| 621 | |
| 622 | static u8 ip6_ra_buf[] = {0x60, 0xf, 0xc5, 0x4a, 0x0, 0x38, 0x3a, 0xff, 0xfe, |
| 623 | 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x85, 0xe6, |
| 624 | 0x29, 0x77, 0xcb, 0xc8, 0x53, 0xff, 0x2, 0x0, 0x0, |
| 625 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 626 | 0x1, 0x86, 0x0, 0xdc, 0x90, 0x40, 0x80, 0x15, 0x18, |
| 627 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x4, |
| 628 | 0x40, 0xc0, 0x0, 0x0, 0x37, 0xdc, 0x0, 0x0, 0x37, |
| 629 | 0x78, 0x0, 0x0, 0x0, 0x0, 0x20, 0x1, 0xca, 0xfe, 0xca, |
| 630 | 0xfe, 0xca, 0xfe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 631 | 0x0, 0x1, 0x1, 0x0, 0x15, 0x5d, 0xe2, 0x8a, 0x2}; |
| 632 | |
| 633 | static int dm_test_validate_ra(struct unit_test_state *uts) |
| 634 | { |
| 635 | struct ip6_hdr *ip6 = (struct ip6_hdr *)ip6_ra_buf; |
| 636 | struct icmp6hdr *icmp = (struct icmp6hdr *)(ip6 + 1); |
| 637 | __be16 temp = 0; |
| 638 | |
| 639 | ut_assert(validate_ra(ip6) == true); |
| 640 | |
| 641 | temp = ip6->payload_len; |
| 642 | ip6->payload_len = 15; |
| 643 | ut_assert(validate_ra(ip6) == false); |
| 644 | ip6->payload_len = temp; |
| 645 | |
| 646 | temp = ip6->saddr.s6_addr16[0]; |
| 647 | ip6->saddr.s6_addr16[0] = 0x2001; |
| 648 | ut_assert(validate_ra(ip6) == false); |
| 649 | ip6->saddr.s6_addr16[0] = temp; |
| 650 | |
| 651 | temp = ip6->hop_limit; |
| 652 | ip6->hop_limit = 15; |
| 653 | ut_assert(validate_ra(ip6) == false); |
| 654 | ip6->hop_limit = temp; |
| 655 | |
| 656 | temp = icmp->icmp6_code; |
| 657 | icmp->icmp6_code = 15; |
| 658 | ut_assert(validate_ra(ip6) == false); |
| 659 | icmp->icmp6_code = temp; |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | DM_TEST(dm_test_validate_ra, 0); |
| 665 | |
| 666 | static int dm_test_process_ra(struct unit_test_state *uts) |
| 667 | { |
| 668 | int len = sizeof(ip6_ra_buf); |
| 669 | struct ip6_hdr *ip6 = (struct ip6_hdr *)ip6_ra_buf; |
| 670 | struct icmp6hdr *icmp = (struct icmp6hdr *)(ip6 + 1); |
| 671 | struct ra_msg *msg = (struct ra_msg *)icmp; |
| 672 | unsigned char *option = msg->opt; |
| 673 | struct icmp6_ra_prefix_info *prefix = |
| 674 | (struct icmp6_ra_prefix_info *)option; |
| 675 | __be16 temp = 0; |
| 676 | unsigned char option_len = option[1]; |
| 677 | |
| 678 | ut_assert(process_ra(ip6, len) == 0); |
| 679 | |
| 680 | temp = icmp->icmp6_rt_lifetime; |
| 681 | icmp->icmp6_rt_lifetime = 0; |
| 682 | ut_assert(process_ra(ip6, len) != 0); |
| 683 | icmp->icmp6_rt_lifetime = temp; |
| 684 | |
| 685 | ut_assert(process_ra(ip6, 0) != 0); |
| 686 | |
| 687 | option[1] = 0; |
| 688 | ut_assert(process_ra(ip6, len) != 0); |
| 689 | option[1] = option_len; |
| 690 | |
| 691 | prefix->on_link = false; |
| 692 | ut_assert(process_ra(ip6, len) != 0); |
| 693 | prefix->on_link = true; |
| 694 | |
| 695 | temp = prefix->prefix.s6_addr16[0]; |
| 696 | prefix->prefix.s6_addr16[0] = 0x80fe; |
| 697 | ut_assert(process_ra(ip6, len) != 0); |
| 698 | prefix->prefix.s6_addr16[0] = temp; |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | DM_TEST(dm_test_process_ra, 0); |
| 704 | |
| 705 | #endif |