| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/if_ether.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <sys/socket.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "sd-event.h" |
| 9 |
#include "sd-json.h" |
| 10 |
#include "sd-lldp-rx.h" |
| 11 |
|
| 12 |
#include "fd-util.h" |
| 13 |
#include "lldp-neighbor.h" |
| 14 |
#include "lldp-network.h" |
| 15 |
#include "tests.h" |
| 16 |
|
| 17 |
#define TEST_LLDP_PORT "em1" |
| 18 |
#define TEST_LLDP_TYPE_SYSTEM_NAME "systemd-lldp" |
| 19 |
#define TEST_LLDP_TYPE_SYSTEM_DESC "systemd-lldp-desc" |
| 20 |
|
| 21 |
static int test_fd[2] = EBADF_PAIR; |
| 22 |
static int lldp_rx_handler_calls; |
| 23 |
|
| 24 |
int lldp_network_bind_raw_socket(int ifindex) { |
| 25 |
if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0) |
| 26 |
return -errno; |
| 27 |
|
| 28 |
return test_fd[0]; |
| 29 |
} |
| 30 |
|
| 31 |
static void lldp_rx_handler(sd_lldp_rx *lldp_rx, sd_lldp_rx_event_t event, sd_lldp_neighbor *n, void *userdata) { |
| 32 |
lldp_rx_handler_calls++; |
| 33 |
} |
| 34 |
|
| 35 |
static int start_lldp_rx(sd_lldp_rx **lldp_rx, sd_event *e, sd_lldp_rx_callback_t cb, void *cb_data) { |
| 36 |
int r; |
| 37 |
|
| 38 |
assert(lldp_rx); |
| 39 |
|
| 40 |
r = sd_lldp_rx_new(lldp_rx); |
| 41 |
if (r < 0) |
| 42 |
return r; |
| 43 |
|
| 44 |
r = sd_lldp_rx_set_ifindex(*lldp_rx, 42); |
| 45 |
if (r < 0) |
| 46 |
return r; |
| 47 |
|
| 48 |
r = sd_lldp_rx_set_callback(*lldp_rx, cb, cb_data); |
| 49 |
if (r < 0) |
| 50 |
return r; |
| 51 |
|
| 52 |
r = sd_lldp_rx_attach_event(*lldp_rx, e, 0); |
| 53 |
if (r < 0) |
| 54 |
return r; |
| 55 |
|
| 56 |
r = sd_lldp_rx_start(*lldp_rx); |
| 57 |
if (r < 0) |
| 58 |
return r; |
| 59 |
|
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
static int stop_lldp_rx(sd_lldp_rx *lldp_rx) { |
| 64 |
int r; |
| 65 |
|
| 66 |
r = sd_lldp_rx_stop(lldp_rx); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
|
| 70 |
r = sd_lldp_rx_detach_event(lldp_rx); |
| 71 |
if (r < 0) |
| 72 |
return r; |
| 73 |
|
| 74 |
sd_lldp_rx_unref(lldp_rx); |
| 75 |
safe_close(test_fd[1]); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static void test_receive_basic_packet(sd_event *e) { |
| 81 |
|
| 82 |
static const uint8_t frame[] = { |
| 83 |
/* Ethernet header */ |
| 84 |
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */ |
| 85 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */ |
| 86 |
0x88, 0xcc, /* Ethertype */ |
| 87 |
/* LLDP mandatory TLVs */ |
| 88 |
0x02, 0x07, 0x04, 0x00, 0x01, 0x02, /* Chassis: MAC, 00:01:02:03:04:05 */ |
| 89 |
0x03, 0x04, 0x05, |
| 90 |
0x04, 0x04, 0x05, 0x31, 0x2f, 0x33, /* Port: interface name, "1/3" */ |
| 91 |
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */ |
| 92 |
/* LLDP optional TLVs */ |
| 93 |
0x08, 0x04, 0x50, 0x6f, 0x72, 0x74, /* Port Description: "Port" */ |
| 94 |
0x0a, 0x03, 0x53, 0x59, 0x53, /* System Name: "SYS" */ |
| 95 |
0x0c, 0x04, 0x66, 0x6f, 0x6f, 0x00, /* System Description: "foo" (NULL-terminated) */ |
| 96 |
0x00, 0x00 /* End Of LLDPDU */ |
| 97 |
}; |
| 98 |
|
| 99 |
sd_lldp_rx *lldp_rx; |
| 100 |
sd_lldp_neighbor **neighbors; |
| 101 |
uint8_t type; |
| 102 |
const void *data; |
| 103 |
uint16_t ttl; |
| 104 |
size_t length; |
| 105 |
const char *str; |
| 106 |
|
| 107 |
lldp_rx_handler_calls = 0; |
| 108 |
assert_se(start_lldp_rx(&lldp_rx, e, lldp_rx_handler, NULL) == 0); |
| 109 |
|
| 110 |
assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame)); |
| 111 |
sd_event_run(e, 0); |
| 112 |
assert_se(lldp_rx_handler_calls == 1); |
| 113 |
assert_se(sd_lldp_rx_get_neighbors(lldp_rx, &neighbors) == 1); |
| 114 |
|
| 115 |
assert_se(sd_lldp_neighbor_get_chassis_id(neighbors[0], &type, &data, &length) == 0); |
| 116 |
assert_se(type == SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS); |
| 117 |
assert_se(length == ETH_ALEN); |
| 118 |
assert_se(!memcmp(data, "\x00\x01\x02\x03\x04\x05", ETH_ALEN)); |
| 119 |
|
| 120 |
assert_se(sd_lldp_neighbor_get_port_id(neighbors[0], &type, &data, &length) == 0); |
| 121 |
assert_se(type == SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME); |
| 122 |
assert_se(length == 3); |
| 123 |
assert_se(!memcmp(data, "1/3", 3)); |
| 124 |
|
| 125 |
assert_se(sd_lldp_neighbor_get_port_description(neighbors[0], &str) == 0); |
| 126 |
assert_se(streq(str, "Port")); |
| 127 |
|
| 128 |
assert_se(sd_lldp_neighbor_get_system_name(neighbors[0], &str) == 0); |
| 129 |
assert_se(streq(str, "SYS")); |
| 130 |
|
| 131 |
assert_se(sd_lldp_neighbor_get_system_description(neighbors[0], &str) == 0); |
| 132 |
assert_se(streq(str, "foo")); |
| 133 |
|
| 134 |
assert_se(sd_lldp_neighbor_get_ttl(neighbors[0], &ttl) == 0); |
| 135 |
assert_se(ttl == 120); |
| 136 |
|
| 137 |
sd_lldp_neighbor_unref(neighbors[0]); |
| 138 |
free(neighbors); |
| 139 |
|
| 140 |
assert_se(stop_lldp_rx(lldp_rx) == 0); |
| 141 |
} |
| 142 |
|
| 143 |
static void test_receive_incomplete_packet(sd_event *e) { |
| 144 |
sd_lldp_rx *lldp_rx; |
| 145 |
sd_lldp_neighbor **neighbors; |
| 146 |
static const uint8_t frame[] = { |
| 147 |
/* Ethernet header */ |
| 148 |
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */ |
| 149 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */ |
| 150 |
0x88, 0xcc, /* Ethertype */ |
| 151 |
/* LLDP mandatory TLVs */ |
| 152 |
0x02, 0x07, 0x04, 0x00, 0x01, 0x02, /* Chassis: MAC, 00:01:02:03:04:05 */ |
| 153 |
0x03, 0x04, 0x05, |
| 154 |
0x04, 0x04, 0x05, 0x31, 0x2f, 0x33, /* Port: interface name, "1/3" */ |
| 155 |
/* Missing TTL */ |
| 156 |
0x00, 0x00 /* End Of LLDPDU */ |
| 157 |
}; |
| 158 |
|
| 159 |
lldp_rx_handler_calls = 0; |
| 160 |
assert_se(start_lldp_rx(&lldp_rx, e, lldp_rx_handler, NULL) == 0); |
| 161 |
|
| 162 |
assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame)); |
| 163 |
sd_event_run(e, 0); |
| 164 |
assert_se(lldp_rx_handler_calls == 0); |
| 165 |
assert_se(sd_lldp_rx_get_neighbors(lldp_rx, &neighbors) == 0); |
| 166 |
|
| 167 |
assert_se(stop_lldp_rx(lldp_rx) == 0); |
| 168 |
} |
| 169 |
|
| 170 |
static void test_receive_oui_packet(sd_event *e) { |
| 171 |
sd_lldp_rx *lldp_rx; |
| 172 |
sd_lldp_neighbor **neighbors; |
| 173 |
static const uint8_t frame[] = { |
| 174 |
/* Ethernet header */ |
| 175 |
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */ |
| 176 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */ |
| 177 |
0x88, 0xcc, /* Ethertype */ |
| 178 |
/* LLDP mandatory TLVs */ |
| 179 |
0x02, 0x07, 0x04, 0x00, 0x01, 0x02, /* Chassis: MAC, 00:01:02:03:04:05 */ |
| 180 |
0x03, 0x04, 0x05, |
| 181 |
0x04, 0x04, 0x05, 0x31, 0x2f, 0x33, /* Port TLV: interface name, "1/3" */ |
| 182 |
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */ |
| 183 |
/* LLDP optional TLVs */ |
| 184 |
0xfe, 0x06, 0x00, 0x80, 0xc2, 0x01, /* Port VLAN ID: 0x1234 */ |
| 185 |
0x12, 0x34, |
| 186 |
0xfe, 0x07, 0x00, 0x80, 0xc2, 0x02, /* Port and protocol: flag 1, PPVID 0x7788 */ |
| 187 |
0x01, 0x77, 0x88, |
| 188 |
0xfe, 0x0d, 0x00, 0x80, 0xc2, 0x03, /* VLAN Name: ID 0x1234, name "Vlan51" */ |
| 189 |
0x12, 0x34, 0x06, 0x56, 0x6c, 0x61, |
| 190 |
0x6e, 0x35, 0x31, |
| 191 |
0xfe, 0x06, 0x00, 0x80, 0xc2, 0x06, /* Management VID: 0x0102 */ |
| 192 |
0x01, 0x02, |
| 193 |
0xfe, 0x09, 0x00, 0x80, 0xc2, 0x07, /* Link aggregation: status 1, ID 0x00140012 */ |
| 194 |
0x01, 0x00, 0x14, 0x00, 0x12, |
| 195 |
0xfe, 0x07, 0x00, 0x12, 0x0f, 0x02, /* 802.3 Power via MDI: PSE, MDI enabled */ |
| 196 |
0x07, 0x01, 0x00, |
| 197 |
0x00, 0x00 /* End of LLDPDU */ |
| 198 |
}; |
| 199 |
|
| 200 |
lldp_rx_handler_calls = 0; |
| 201 |
assert_se(start_lldp_rx(&lldp_rx, e, lldp_rx_handler, NULL) == 0); |
| 202 |
|
| 203 |
assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame)); |
| 204 |
sd_event_run(e, 0); |
| 205 |
assert_se(lldp_rx_handler_calls == 1); |
| 206 |
assert_se(sd_lldp_rx_get_neighbors(lldp_rx, &neighbors) == 1); |
| 207 |
|
| 208 |
assert_se(sd_lldp_neighbor_tlv_rewind(neighbors[0]) >= 0); |
| 209 |
assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_CHASSIS_ID) > 0); |
| 210 |
assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0); |
| 211 |
assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_PORT_ID) > 0); |
| 212 |
assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0); |
| 213 |
assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_TTL) > 0); |
| 214 |
assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0); |
| 215 |
assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_PORT_VLAN_ID) > 0); |
| 216 |
assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0); |
| 217 |
assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_PORT_PROTOCOL_VLAN_ID) > 0); |
| 218 |
assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0); |
| 219 |
assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_VLAN_NAME) > 0); |
| 220 |
assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0); |
| 221 |
assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_MANAGEMENT_VID) > 0); |
| 222 |
assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0); |
| 223 |
assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_LINK_AGGREGATION) > 0); |
| 224 |
assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0); |
| 225 |
assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_3, SD_LLDP_OUI_802_3_SUBTYPE_POWER_VIA_MDI) > 0); |
| 226 |
assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0); |
| 227 |
assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_END) > 0); |
| 228 |
assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) == 0); |
| 229 |
|
| 230 |
sd_lldp_neighbor_unref(neighbors[0]); |
| 231 |
free(neighbors); |
| 232 |
|
| 233 |
assert_se(stop_lldp_rx(lldp_rx) == 0); |
| 234 |
} |
| 235 |
|
| 236 |
static void test_multiple_neighbors_sorted(sd_event *e) { |
| 237 |
|
| 238 |
static const uint8_t frame1[] = { |
| 239 |
/* Ethernet header */ |
| 240 |
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */ |
| 241 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */ |
| 242 |
0x88, 0xcc, /* Ethertype */ |
| 243 |
/* LLDP mandatory TLVs */ |
| 244 |
0x02, 0x04, 0x01, '1', '/', '2', /* Chassis component: "1/2" */ |
| 245 |
0x04, 0x04, 0x02, '2', '/', '3', /* Port component: "2/3" */ |
| 246 |
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */ |
| 247 |
0x00, 0x00 /* End Of LLDPDU */ |
| 248 |
}; |
| 249 |
static const uint8_t frame2[] = { |
| 250 |
/* Ethernet header */ |
| 251 |
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */ |
| 252 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */ |
| 253 |
0x88, 0xcc, /* Ethertype */ |
| 254 |
/* LLDP mandatory TLVs */ |
| 255 |
0x02, 0x04, 0x01, '2', '/', '1', /* Chassis component: "2/1" */ |
| 256 |
0x04, 0x04, 0x02, '1', '/', '3', /* Port component: "1/3" */ |
| 257 |
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */ |
| 258 |
0x00, 0x00 /* End Of LLDPDU */ |
| 259 |
}; |
| 260 |
static const uint8_t frame3[] = { |
| 261 |
/* Ethernet header */ |
| 262 |
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */ |
| 263 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */ |
| 264 |
0x88, 0xcc, /* Ethertype */ |
| 265 |
/* LLDP mandatory TLVs */ |
| 266 |
0x02, 0x05, 0x01, '2', '/', '1', '0', /* Chassis component: "2/10" */ |
| 267 |
0x04, 0x04, 0x02, '1', '/', '0', /* Port component: "1/0" */ |
| 268 |
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */ |
| 269 |
0x00, 0x00 /* End Of LLDPDU */ |
| 270 |
}; |
| 271 |
static const uint8_t frame4[] = { |
| 272 |
/* Ethernet header */ |
| 273 |
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */ |
| 274 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */ |
| 275 |
0x88, 0xcc, /* Ethertype */ |
| 276 |
/* LLDP mandatory TLVs */ |
| 277 |
0x02, 0x05, 0x01, '2', '/', '1', '9', /* Chassis component: "2/19" */ |
| 278 |
0x04, 0x04, 0x02, '1', '/', '0', /* Port component: "1/0" */ |
| 279 |
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */ |
| 280 |
0x00, 0x00 /* End Of LLDPDU */ |
| 281 |
}; |
| 282 |
static const uint8_t frame5[] = { |
| 283 |
/* Ethernet header */ |
| 284 |
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */ |
| 285 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */ |
| 286 |
0x88, 0xcc, /* Ethertype */ |
| 287 |
/* LLDP mandatory TLVs */ |
| 288 |
0x02, 0x04, 0x01, '1', '/', '2', /* Chassis component: "1/2" */ |
| 289 |
0x04, 0x05, 0x02, '2', '/', '1', '0', /* Port component: "2/10" */ |
| 290 |
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */ |
| 291 |
0x00, 0x00 /* End Of LLDPDU */ |
| 292 |
}; |
| 293 |
static const uint8_t frame6[] = { |
| 294 |
/* Ethernet header */ |
| 295 |
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */ |
| 296 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */ |
| 297 |
0x88, 0xcc, /* Ethertype */ |
| 298 |
/* LLDP mandatory TLVs */ |
| 299 |
0x02, 0x04, 0x01, '1', '/', '2', /* Chassis component: "1/2" */ |
| 300 |
0x04, 0x05, 0x02, '2', '/', '3', '9', /* Port component: "2/10" */ |
| 301 |
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */ |
| 302 |
0x00, 0x00 /* End Of LLDPDU */ |
| 303 |
}; |
| 304 |
static const char* expected[] = { |
| 305 |
/* ordered pairs of Chassis+Port */ |
| 306 |
"1/2", "2/10", |
| 307 |
"1/2", "2/3", |
| 308 |
"1/2", "2/39", |
| 309 |
"2/1", "1/3", |
| 310 |
"2/10", "1/0", |
| 311 |
"2/19", "1/0", |
| 312 |
}; |
| 313 |
|
| 314 |
sd_lldp_rx *lldp_rx; |
| 315 |
sd_lldp_neighbor **neighbors; |
| 316 |
int i; |
| 317 |
uint8_t type; |
| 318 |
const void *data; |
| 319 |
size_t length, expected_length; |
| 320 |
uint16_t ttl; |
| 321 |
|
| 322 |
lldp_rx_handler_calls = 0; |
| 323 |
assert_se(start_lldp_rx(&lldp_rx, e, lldp_rx_handler, NULL) == 0); |
| 324 |
|
| 325 |
assert_se(write(test_fd[1], frame1, sizeof(frame1)) == sizeof(frame1)); |
| 326 |
sd_event_run(e, 0); |
| 327 |
assert_se(write(test_fd[1], frame2, sizeof(frame2)) == sizeof(frame2)); |
| 328 |
sd_event_run(e, 0); |
| 329 |
assert_se(write(test_fd[1], frame3, sizeof(frame3)) == sizeof(frame3)); |
| 330 |
sd_event_run(e, 0); |
| 331 |
assert_se(write(test_fd[1], frame4, sizeof(frame4)) == sizeof(frame4)); |
| 332 |
sd_event_run(e, 0); |
| 333 |
assert_se(write(test_fd[1], frame5, sizeof(frame5)) == sizeof(frame5)); |
| 334 |
sd_event_run(e, 0); |
| 335 |
assert_se(write(test_fd[1], frame6, sizeof(frame6)) == sizeof(frame6)); |
| 336 |
sd_event_run(e, 0); |
| 337 |
assert_se(lldp_rx_handler_calls == 6); |
| 338 |
|
| 339 |
assert_se(sd_lldp_rx_get_neighbors(lldp_rx, &neighbors) == 6); |
| 340 |
|
| 341 |
for (i = 0; i < 6; i++) { |
| 342 |
assert_se(sd_lldp_neighbor_get_chassis_id(neighbors[i], &type, &data, &length) == 0); |
| 343 |
assert_se(type == SD_LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT); |
| 344 |
expected_length = strlen(expected[2 * i]); |
| 345 |
assert_se(length == expected_length); |
| 346 |
assert_se(memcmp(data, expected[2 * i], expected_length) == 0); |
| 347 |
|
| 348 |
assert_se(sd_lldp_neighbor_get_port_id(neighbors[i], &type, &data, &length) == 0); |
| 349 |
assert_se(type == SD_LLDP_PORT_SUBTYPE_PORT_COMPONENT); |
| 350 |
expected_length = strlen(expected[2 * i + 1]); |
| 351 |
assert_se(length == expected_length); |
| 352 |
assert_se(memcmp(data, expected[2 * i + 1], expected_length) == 0); |
| 353 |
|
| 354 |
assert_se(sd_lldp_neighbor_get_ttl(neighbors[i], &ttl) == 0); |
| 355 |
assert_se(ttl == 120); |
| 356 |
} |
| 357 |
|
| 358 |
for (i = 0; i < 6; i++) |
| 359 |
sd_lldp_neighbor_unref(neighbors[i]); |
| 360 |
free(neighbors); |
| 361 |
|
| 362 |
assert_se(stop_lldp_rx(lldp_rx) == 0); |
| 363 |
} |
| 364 |
|
| 365 |
static void test_receive_oui_vlanid_packet(sd_event *e) { |
| 366 |
sd_lldp_rx *lldp_rx; |
| 367 |
sd_lldp_neighbor **neighbors; |
| 368 |
sd_json_variant *v; |
| 369 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *neighbor_json = NULL; |
| 370 |
static const uint8_t frame[] = { |
| 371 |
/* Ethernet header */ |
| 372 |
0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */ |
| 373 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */ |
| 374 |
0x88, 0xcc, /* Ethertype */ |
| 375 |
/* LLDP mandatory TLVs */ |
| 376 |
0x02, 0x07, 0x04, 0x00, 0x01, 0x02, /* Chassis: MAC, 00:01:02:03:04:05 */ |
| 377 |
0x03, 0x04, 0x05, |
| 378 |
0x04, 0x04, 0x05, 0x31, 0x2f, 0x33, /* Port TLV: interface name, "1/3" */ |
| 379 |
0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */ |
| 380 |
/* LLDP optional TLVs */ |
| 381 |
0xfe, 0x06, 0x00, 0x80, 0xc2, 0x01, /* Port VLAN ID: 0x1234 */ |
| 382 |
0x12, 0x34, |
| 383 |
0xfe, 0x07, 0x00, 0x80, 0xc2, 0x02, /* Port and protocol: flag 1, PPVID 0x7788 */ |
| 384 |
0x01, 0x77, 0x88, |
| 385 |
0xfe, 0x0d, 0x00, 0x80, 0xc2, 0x03, /* VLAN Name: ID 0x1234, name "Vlan51" */ |
| 386 |
0x12, 0x34, 0x06, 0x56, 0x6c, 0x61, |
| 387 |
0x6e, 0x35, 0x31, |
| 388 |
0xfe, 0x06, 0x00, 0x80, 0xc2, 0x06, /* Management VID: 0x0102 */ |
| 389 |
0x01, 0x02, |
| 390 |
0xfe, 0x09, 0x00, 0x80, 0xc2, 0x07, /* Link aggregation: status 1, ID 0x00140012 */ |
| 391 |
0x01, 0x00, 0x14, 0x00, 0x12, |
| 392 |
0xfe, 0x07, 0x00, 0x12, 0x0f, 0x02, /* 802.3 Power via MDI: PSE, MDI enabled */ |
| 393 |
0x07, 0x01, 0x00, |
| 394 |
0x00, 0x00 /* End of LLDPDU */ |
| 395 |
}; |
| 396 |
uint16_t vlanid; |
| 397 |
|
| 398 |
lldp_rx_handler_calls = 0; |
| 399 |
ASSERT_OK(start_lldp_rx(&lldp_rx, e, lldp_rx_handler, NULL)); |
| 400 |
|
| 401 |
ASSERT_OK_EQ_ERRNO(write(test_fd[1], frame, sizeof(frame)), (ssize_t)sizeof(frame)); |
| 402 |
ASSERT_OK(sd_event_run(e, 0)); |
| 403 |
ASSERT_EQ(lldp_rx_handler_calls, 1); |
| 404 |
ASSERT_OK_EQ(sd_lldp_rx_get_neighbors(lldp_rx, &neighbors), 1); |
| 405 |
|
| 406 |
ASSERT_OK(sd_lldp_neighbor_get_port_vlan_id(neighbors[0], &vlanid)); |
| 407 |
ASSERT_EQ(vlanid, 0x1234); |
| 408 |
|
| 409 |
ASSERT_OK(lldp_neighbor_build_json(neighbors[0], &neighbor_json)); |
| 410 |
ASSERT_NOT_NULL(v = sd_json_variant_by_key(neighbor_json, "VlanID")); |
| 411 |
ASSERT_EQ(sd_json_variant_type(v), SD_JSON_VARIANT_UNSIGNED); |
| 412 |
ASSERT_EQ(sd_json_variant_unsigned(v), UINT64_C(0x1234)); |
| 413 |
|
| 414 |
sd_lldp_neighbor_unref(neighbors[0]); |
| 415 |
free(neighbors); |
| 416 |
|
| 417 |
ASSERT_OK(stop_lldp_rx(lldp_rx)); |
| 418 |
} |
| 419 |
|
| 420 |
int main(int argc, char *argv[]) { |
| 421 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 422 |
|
| 423 |
test_setup_logging(LOG_DEBUG); |
| 424 |
|
| 425 |
/* LLDP reception tests */ |
| 426 |
assert_se(sd_event_new(&e) == 0); |
| 427 |
test_receive_basic_packet(e); |
| 428 |
test_receive_incomplete_packet(e); |
| 429 |
test_receive_oui_packet(e); |
| 430 |
test_multiple_neighbors_sorted(e); |
| 431 |
test_receive_oui_vlanid_packet(e); |
| 432 |
|
| 433 |
return 0; |
| 434 |
} |