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