]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/test-lldp.c
47aebc5756ad3621384f7e92bf2661c45ebe2f7b
[thirdparty/systemd.git] / src / libsystemd-network / test-lldp.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright (C) 2014 Tom Gundersen
6 Copyright (C) 2014 Susant Sahani
7 ***/
8
9 #include <arpa/inet.h>
10 #include <errno.h>
11 #include <net/ethernet.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include "sd-event.h"
17 #include "sd-lldp.h"
18
19 #include "alloc-util.h"
20 #include "fd-util.h"
21 #include "lldp-network.h"
22 #include "macro.h"
23 #include "string-util.h"
24
25 #define TEST_LLDP_PORT "em1"
26 #define TEST_LLDP_TYPE_SYSTEM_NAME "systemd-lldp"
27 #define TEST_LLDP_TYPE_SYSTEM_DESC "systemd-lldp-desc"
28
29 static int test_fd[2] = { -1, -1 };
30 static int lldp_handler_calls;
31
32 int lldp_network_bind_raw_socket(int ifindex) {
33 if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, test_fd) < 0)
34 return -errno;
35
36 return test_fd[0];
37 }
38
39 static void lldp_handler(sd_lldp *lldp, sd_lldp_event event, sd_lldp_neighbor *n, void *userdata) {
40 lldp_handler_calls++;
41 }
42
43 static int start_lldp(sd_lldp **lldp, sd_event *e, sd_lldp_callback_t cb, void *cb_data) {
44 int r;
45
46 r = sd_lldp_new(lldp);
47 if (r < 0)
48 return r;
49
50 r = sd_lldp_set_ifindex(*lldp, 42);
51 if (r < 0)
52 return r;
53
54 r = sd_lldp_set_callback(*lldp, cb, cb_data);
55 if (r < 0)
56 return r;
57
58 r = sd_lldp_attach_event(*lldp, e, 0);
59 if (r < 0)
60 return r;
61
62 r = sd_lldp_start(*lldp);
63 if (r < 0)
64 return r;
65
66 return 0;
67 }
68
69 static int stop_lldp(sd_lldp *lldp) {
70 int r;
71
72 r = sd_lldp_stop(lldp);
73 if (r < 0)
74 return r;
75
76 r = sd_lldp_detach_event(lldp);
77 if (r < 0)
78 return r;
79
80 sd_lldp_unref(lldp);
81 safe_close(test_fd[1]);
82
83 return 0;
84 }
85
86 static void test_receive_basic_packet(sd_event *e) {
87
88 static const uint8_t frame[] = {
89 /* Ethernet header */
90 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */
91 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */
92 0x88, 0xcc, /* Ethertype */
93 /* LLDP mandatory TLVs */
94 0x02, 0x07, 0x04, 0x00, 0x01, 0x02, /* Chassis: MAC, 00:01:02:03:04:05 */
95 0x03, 0x04, 0x05,
96 0x04, 0x04, 0x05, 0x31, 0x2f, 0x33, /* Port: interface name, "1/3" */
97 0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */
98 /* LLDP optional TLVs */
99 0x08, 0x04, 0x50, 0x6f, 0x72, 0x74, /* Port Description: "Port" */
100 0x0a, 0x03, 0x53, 0x59, 0x53, /* System Name: "SYS" */
101 0x0c, 0x04, 0x66, 0x6f, 0x6f, 0x00, /* System Description: "foo" (NULL-terminated) */
102 0x00, 0x00 /* End Of LLDPDU */
103 };
104
105 sd_lldp *lldp;
106 sd_lldp_neighbor **neighbors;
107 uint8_t type;
108 const void *data;
109 uint16_t ttl;
110 size_t length;
111 const char *str;
112
113 lldp_handler_calls = 0;
114 assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);
115
116 assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
117 sd_event_run(e, 0);
118 assert_se(lldp_handler_calls == 1);
119 assert_se(sd_lldp_get_neighbors(lldp, &neighbors) == 1);
120
121 assert_se(sd_lldp_neighbor_get_chassis_id(neighbors[0], &type, &data, &length) == 0);
122 assert_se(type == SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS);
123 assert_se(length == ETH_ALEN);
124 assert_se(!memcmp(data, "\x00\x01\x02\x03\x04\x05", ETH_ALEN));
125
126 assert_se(sd_lldp_neighbor_get_port_id(neighbors[0], &type, &data, &length) == 0);
127 assert_se(type == SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME);
128 assert_se(length == 3);
129 assert_se(!memcmp(data, "1/3", 3));
130
131 assert_se(sd_lldp_neighbor_get_port_description(neighbors[0], &str) == 0);
132 assert_se(streq(str, "Port"));
133
134 assert_se(sd_lldp_neighbor_get_system_name(neighbors[0], &str) == 0);
135 assert_se(streq(str, "SYS"));
136
137 assert_se(sd_lldp_neighbor_get_system_description(neighbors[0], &str) == 0);
138 assert_se(streq(str, "foo"));
139
140 assert_se(sd_lldp_neighbor_get_ttl(neighbors[0], &ttl) == 0);
141 assert_se(ttl == 120);
142
143 sd_lldp_neighbor_unref(neighbors[0]);
144 free(neighbors);
145
146 assert_se(stop_lldp(lldp) == 0);
147 }
148
149 static void test_receive_incomplete_packet(sd_event *e) {
150 sd_lldp *lldp;
151 sd_lldp_neighbor **neighbors;
152 uint8_t frame[] = {
153 /* Ethernet header */
154 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */
155 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */
156 0x88, 0xcc, /* Ethertype */
157 /* LLDP mandatory TLVs */
158 0x02, 0x07, 0x04, 0x00, 0x01, 0x02, /* Chassis: MAC, 00:01:02:03:04:05 */
159 0x03, 0x04, 0x05,
160 0x04, 0x04, 0x05, 0x31, 0x2f, 0x33, /* Port: interface name, "1/3" */
161 /* Missing TTL */
162 0x00, 0x00 /* End Of LLDPDU */
163 };
164
165 lldp_handler_calls = 0;
166 assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);
167
168 assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
169 sd_event_run(e, 0);
170 assert_se(lldp_handler_calls == 0);
171 assert_se(sd_lldp_get_neighbors(lldp, &neighbors) == 0);
172
173 assert_se(stop_lldp(lldp) == 0);
174 }
175
176 static void test_receive_oui_packet(sd_event *e) {
177 sd_lldp *lldp;
178 sd_lldp_neighbor **neighbors;
179 uint8_t frame[] = {
180 /* Ethernet header */
181 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03, /* Destination MAC */
182 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* Source MAC */
183 0x88, 0xcc, /* Ethertype */
184 /* LLDP mandatory TLVs */
185 0x02, 0x07, 0x04, 0x00, 0x01, 0x02, /* Chassis: MAC, 00:01:02:03:04:05 */
186 0x03, 0x04, 0x05,
187 0x04, 0x04, 0x05, 0x31, 0x2f, 0x33, /* Port TLV: interface name, "1/3" */
188 0x06, 0x02, 0x00, 0x78, /* TTL: 120 seconds */
189 /* LLDP optional TLVs */
190 0xfe, 0x06, 0x00, 0x80, 0xc2, 0x01, /* Port VLAN ID: 0x1234 */
191 0x12, 0x34,
192 0xfe, 0x07, 0x00, 0x80, 0xc2, 0x02, /* Port and protocol: flag 1, PPVID 0x7788 */
193 0x01, 0x77, 0x88,
194 0xfe, 0x0d, 0x00, 0x80, 0xc2, 0x03, /* VLAN Name: ID 0x1234, name "Vlan51" */
195 0x12, 0x34, 0x06, 0x56, 0x6c, 0x61,
196 0x6e, 0x35, 0x31,
197 0xfe, 0x06, 0x00, 0x80, 0xc2, 0x06, /* Management VID: 0x0102 */
198 0x01, 0x02,
199 0xfe, 0x09, 0x00, 0x80, 0xc2, 0x07, /* Link aggregation: status 1, ID 0x00140012 */
200 0x01, 0x00, 0x14, 0x00, 0x12,
201 0x00, 0x00 /* End of LLDPDU */
202 };
203
204 lldp_handler_calls = 0;
205 assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);
206
207 assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
208 sd_event_run(e, 0);
209 assert_se(lldp_handler_calls == 1);
210 assert_se(sd_lldp_get_neighbors(lldp, &neighbors) == 1);
211
212 assert_se(sd_lldp_neighbor_tlv_rewind(neighbors[0]) >= 0);
213 assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_CHASSIS_ID) > 0);
214 assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
215 assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_PORT_ID) > 0);
216 assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
217 assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_TTL) > 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_PORT_VLAN_ID) > 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_PORT_PROTOCOL_VLAN_ID) > 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_VLAN_NAME) > 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_1, SD_LLDP_OUI_802_1_SUBTYPE_MANAGEMENT_VID) > 0);
226 assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
227 assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_LINK_AGGREGATION) > 0);
228 assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
229 assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_END) > 0);
230 assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) == 0);
231
232 sd_lldp_neighbor_unref(neighbors[0]);
233 free(neighbors);
234
235 assert_se(stop_lldp(lldp) == 0);
236 }
237
238 int main(int argc, char *argv[]) {
239 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
240
241 log_set_max_level(LOG_DEBUG);
242
243 /* LLDP reception tests */
244 assert_se(sd_event_new(&e) == 0);
245 test_receive_basic_packet(e);
246 test_receive_incomplete_packet(e);
247 test_receive_oui_packet(e);
248
249 return 0;
250 }