]> git.ipfire.org Git - thirdparty/lldpd.git/blob - tests/check_lldp.c
Add LLDP send tests.
[thirdparty/lldpd.git] / tests / check_lldp.c
1 #define _GNU_SOURCE 1
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <time.h>
9 #include <check.h>
10 #include "../src/lldpd.h"
11
12 int dump = -1;
13 char *filename = NULL;
14 struct packet {
15 TAILQ_ENTRY(packet) next;
16 int size;
17 char data[];
18 };
19 TAILQ_HEAD(, packet) pkts;
20 char *buffer[] = { NULL };
21 char macaddress[ETH_ALEN] = { 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad };
22 struct lldpd_hardware hardware;
23 struct lldpd_chassis chassis;
24
25 /* See:
26 * http://wiki.wireshark.org/Development/LibpcapFileFormat
27 */
28 struct pcap_hdr {
29 u_int32_t magic_number; /* magic number */
30 u_int16_t version_major; /* major version number */
31 u_int16_t version_minor; /* minor version number */
32 u_int32_t thiszone; /* GMT to local correction */
33 u_int32_t sigfigs; /* accuracy of timestamps */
34 u_int32_t snaplen; /* max length of captured packets, in octets */
35 u_int32_t network; /* data link type */
36 };
37 struct pcaprec_hdr {
38 u_int32_t ts_sec; /* timestamp seconds */
39 u_int32_t ts_usec; /* timestamp microseconds */
40 u_int32_t incl_len; /* number of octets of packet saved in file */
41 u_int32_t orig_len; /* actual length of packet */
42 };
43
44 int
45 pcap_send(struct lldpd *cfg, struct lldpd_hardware *hardware,
46 char *buffer, size_t size)
47 {
48 struct pcaprec_hdr hdr;
49 struct packet *pkt;
50 int n;
51
52 /* Write pcap record header */
53 hdr.ts_sec = time(NULL);
54 hdr.ts_usec = 0;
55 hdr.incl_len = hdr.orig_len = size;
56 n = write(dump, &hdr, sizeof(hdr));
57 fail_unless(n != -1, "unable to write pcap record header to %s", filename);
58
59 /* Write data */
60 n = write(dump, buffer, size);
61 fail_unless(n != -1, "unable to write pcap data to %s", filename);
62
63 /* Append to list of packets */
64 pkt = (struct packet *)malloc(size + sizeof(TAILQ_HEAD(,packet)) + sizeof(int));
65 fail_unless(pkt != NULL);
66 memcpy(pkt->data, buffer, size);
67 pkt->size = size;
68 TAILQ_INSERT_TAIL(&pkts, pkt, next);
69 return 0;
70 }
71
72 struct lldpd_ops fake_ops = {
73 .send = pcap_send,
74 .recv = NULL, /* Won't be used */
75 .cleanup = NULL, /* Won't be used */
76 };
77
78
79 void
80 setup()
81 {
82 static int serial = 0;
83 struct pcap_hdr hdr;
84 int n;
85 /* Prepare packet buffer */
86 TAILQ_INIT(&pkts);
87 /* Open a new dump file */
88 n = asprintf(&filename, "lldp_send_%04d.pcap", serial++);
89 fail_unless(n != -1, "unable to compute filename");
90 dump = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
91 fail_unless(dump != -1);
92 /* Write a PCAP header */
93 hdr.magic_number = 0xa1b2c3d4;
94 hdr.version_major = 2;
95 hdr.version_minor = 4;
96 hdr.thiszone = 0;
97 hdr.sigfigs = 0;
98 hdr.snaplen = 65535;
99 hdr.network = 1;
100 n = write(dump, &hdr, sizeof(hdr));
101 fail_unless(n != -1, "unable to write pcap header to %s", filename);
102 /* Prepare hardware */
103 memset(&hardware, 0, sizeof(struct lldpd_hardware));
104 TAILQ_INIT(&hardware.h_rports);
105 TAILQ_INIT(&hardware.h_lport.p_vlans);
106 hardware.h_mtu = 1500;
107 hardware.h_ifindex = 1;
108 strcpy(hardware.h_ifname, "test");
109 memcpy(hardware.h_lladdr, macaddress, ETH_ALEN);
110 hardware.h_ops = &fake_ops;
111 /* Prepare chassis */
112 memset(&chassis, 0, sizeof(struct lldpd_chassis));
113 hardware.h_lport.p_chassis = &chassis;
114 chassis.c_ttl = 180;
115 }
116
117 void
118 teardown()
119 {
120 struct packet *npkt, *pkt;
121 for (pkt = TAILQ_FIRST(&pkts);
122 pkt != NULL;
123 pkt = npkt) {
124 npkt = TAILQ_NEXT(pkt, next);
125 TAILQ_REMOVE(&pkts, pkt, next);
126 free(pkt);
127 }
128 if (dump != -1) {
129 close(dump);
130 dump = -1;
131 }
132 if (filename) {
133 free(filename);
134 filename = NULL;
135 }
136 }
137
138 START_TEST (test_send_basic)
139 {
140 int n;
141 /* Packet we should build:
142 Ethernet II, Src: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad), Dst: LLDP_Multicast (01:80:c2:00:00:0e)
143 Destination: LLDP_Multicast (01:80:c2:00:00:0e)
144 Source: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
145 Type: 802.1 Link Layer Discovery Protocol (LLDP) (0x88cc)
146 Link Layer Discovery Protocol
147 Chassis Subtype = MAC address
148 0000 001. .... .... = TLV Type: Chassis Id (1)
149 .... ...0 0000 0111 = TLV Length: 7
150 Chassis Id Subtype: MAC address (4)
151 Chassis Id: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
152 Port Subtype = Interface name
153 0000 010. .... .... = TLV Type: Port Id (2)
154 .... ...0 0001 0001 = TLV Length: 17
155 Port Id Subtype: Interface name (5)
156 Port Id: FastEthernet 1/5
157 Time To Live = 180 sec
158 0000 011. .... .... = TLV Type: Time to Live (3)
159 .... ...0 0000 0010 = TLV Length: 2
160 Seconds: 180
161 System Name = First chassis
162 0000 101. .... .... = TLV Type: System Name (5)
163 .... ...0 0000 1101 = TLV Length: 13
164 System Name = First chassis
165 System Description = Chassis description
166 0000 110. .... .... = TLV Type: System Description (6)
167 .... ...0 0001 0011 = TLV Length: 19
168 System Description = Chassis description
169 Capabilities
170 0000 111. .... .... = TLV Type: System Capabilities (7)
171 .... ...0 0000 0100 = TLV Length: 4
172 Capabilities: 0x0010
173 .... .... ...1 .... = Router
174 Enabled Capabilities: 0x0010
175 .... .... ...1 .... = Router
176 Port Description = Fake port description
177 0000 100. .... .... = TLV Type: Port Description (4)
178 .... ...0 0001 0101 = TLV Length: 21
179 Port Description: Fake port description
180 IEEE 802.3 - Link Aggregation
181 1111 111. .... .... = TLV Type: Organization Specific (127)
182 .... ...0 0000 1001 = TLV Length: 9
183 Organization Unique Code: IEEE 802.3 (0x00120f)
184 IEEE 802.3 Subtype: Link Aggregation (0x03)
185 Aggregation Status: 0x01
186 .... ...1 = Aggregation Capability: Yes
187 .... ..0. = Aggregation Status: Not Enabled
188 Aggregated Port Id: 0
189 IEEE 802.3 - MAC/PHY Configuration/Status
190 1111 111. .... .... = TLV Type: Organization Specific (127)
191 .... ...0 0000 1001 = TLV Length: 9
192 Organization Unique Code: IEEE 802.3 (0x00120f)
193 IEEE 802.3 Subtype: MAC/PHY Configuration/Status (0x01)
194 Auto-Negotiation Support/Status: 0x00
195 .... ...0 = Auto-Negotiation: Not Supported
196 .... ..0. = Auto-Negotiation: Not Enabled
197 PMD Auto-Negotiation Advertised Capability: 0x0000
198 Operational MAU Type: Unknown (0x0000)
199 IEEE 802.3 - Maximum Frame Size
200 1111 111. .... .... = TLV Type: Organization Specific (127)
201 .... ...0 0000 0110 = TLV Length: 6
202 Organization Unique Code: IEEE 802.3 (0x00120f)
203 IEEE 802.3 Subtype: Maximum Frame Size (0x04)
204 Maximum Frame Size: 1516
205 End of LLDPDU
206 0000 000. .... .... = TLV Type: End of LLDPDU (0)
207 .... ...0 0000 0000 = TLV Length: 0
208 */
209 char pkt1[] = {
210 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e, 0x5e, 0x10,
211 0x8e, 0xe7, 0x84, 0xad, 0x88, 0xcc, 0x02, 0x07,
212 0x04, 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad, 0x04,
213 0x11, 0x05, 0x46, 0x61, 0x73, 0x74, 0x45, 0x74,
214 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x31,
215 0x2f, 0x35, 0x06, 0x02, 0x00, 0xb4, 0x0a, 0x0d,
216 0x46, 0x69, 0x72, 0x73, 0x74, 0x20, 0x63, 0x68,
217 0x61, 0x73, 0x73, 0x69, 0x73, 0x0c, 0x13, 0x43,
218 0x68, 0x61, 0x73, 0x73, 0x69, 0x73, 0x20, 0x64,
219 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69,
220 0x6f, 0x6e, 0x0e, 0x04, 0x00, 0x10, 0x00, 0x10,
221 0x08, 0x15, 0x46, 0x61, 0x6b, 0x65, 0x20, 0x70,
222 0x6f, 0x72, 0x74, 0x20, 0x64, 0x65, 0x73, 0x63,
223 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0xfe,
224 0x09, 0x00, 0x12, 0x0f, 0x03, 0x01, 0x00, 0x00,
225 0x00, 0x00, 0xfe, 0x09, 0x00, 0x12, 0x0f, 0x01,
226 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x06, 0x00,
227 0x12, 0x0f, 0x04, 0x05, 0xec, 0x00, 0x00 };
228 struct packet *pkt;
229
230 /* Populate port and chassis */
231 hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME;
232 hardware.h_lport.p_id = "FastEthernet 1/5";
233 hardware.h_lport.p_id_len = strlen(hardware.h_lport.p_id);
234 hardware.h_lport.p_descr = "Fake port description";
235 hardware.h_lport.p_mfs = 1516;
236 chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
237 chassis.c_id = macaddress;
238 chassis.c_id_len = ETH_ALEN;
239 chassis.c_name = "First chassis";
240 chassis.c_descr = "Chassis description";
241 chassis.c_cap_available = chassis.c_cap_enabled = LLDP_CAP_ROUTER;
242
243 /* Build packet */
244 n = lldp_send(NULL, &hardware);
245 fail_unless(n == 0, "unable to build packet");
246 fail_unless(!TAILQ_EMPTY(&pkts));
247 pkt = TAILQ_FIRST(&pkts);
248 ck_assert_int_eq(pkt->size, sizeof(pkt1));
249 fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0);
250 fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than one packet sent");
251 }
252 END_TEST
253
254 START_TEST (test_send_vlan)
255 {
256 int n;
257 struct lldpd_vlan vlan1, vlan2, vlan3;
258 /* Packet we should build:
259 Ethernet II, Src: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad), Dst: LLDP_Multicast (01:80:c2:00:00:0e)
260 Destination: LLDP_Multicast (01:80:c2:00:00:0e)
261 Address: LLDP_Multicast (01:80:c2:00:00:0e)
262 Source: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
263 Address: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
264 he factory default)
265 Type: 802.1 Link Layer Discovery Protocol (LLDP) (0x88cc)
266 Link Layer Discovery Protocol
267 Chassis Subtype = Locally assigned
268 0000 001. .... .... = TLV Type: Chassis Id (1)
269 .... ...0 0000 1101 = TLV Length: 13
270 Chassis Id Subtype: Locally assigned (7)
271 Chassis Id: Chassis name
272 Port Subtype = MAC address
273 0000 010. .... .... = TLV Type: Port Id (2)
274 .... ...0 0000 0111 = TLV Length: 7
275 Port Id Subtype: MAC address (3)
276 Port Id: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
277 Time To Live = 180 sec
278 0000 011. .... .... = TLV Type: Time to Live (3)
279 .... ...0 0000 0010 = TLV Length: 2
280 Seconds: 180
281 System Name = Second chassis
282 0000 101. .... .... = TLV Type: System Name (5)
283 .... ...0 0000 1110 = TLV Length: 14
284 System Name = Second chassis
285 System Description = Chassis description
286 0000 110. .... .... = TLV Type: System Description (6)
287 .... ...0 0001 0011 = TLV Length: 19
288 System Description = Chassis description
289 Capabilities
290 0000 111. .... .... = TLV Type: System Capabilities (7)
291 .... ...0 0000 0100 = TLV Length: 4
292 Capabilities: 0x0014
293 .... .... .... .1.. = Bridge
294 .... .... ...1 .... = Router
295 Enabled Capabilities: 0x0010
296 .... .... ...1 .... = Router
297 Port Description = Fake port description
298 0000 100. .... .... = TLV Type: Port Description (4)
299 .... ...0 0001 0101 = TLV Length: 21
300 Port Description: Fake port description
301 IEEE 802.1 - VLAN Name
302 1111 111. .... .... = TLV Type: Organization Specific (127)
303 .... ...0 0001 0001 = TLV Length: 17
304 Organization Unique Code: IEEE 802.1 (0x0080c2)
305 IEEE 802.1 Subtype: VLAN Name (0x03)
306 VLAN Identifier: 157 (0x009D)
307 VLAN Name Length: 10
308 VLAN Name: First VLAN
309 IEEE 802.1 - VLAN Name
310 1111 111. .... .... = TLV Type: Organization Specific (127)
311 .... ...0 0001 0010 = TLV Length: 18
312 Organization Unique Code: IEEE 802.1 (0x0080c2)
313 IEEE 802.1 Subtype: VLAN Name (0x03)
314 VLAN Identifier: 1247 (0x04DF)
315 VLAN Name Length: 11
316 VLAN Name: Second VLAN
317 IEEE 802.1 - VLAN Name
318 1111 111. .... .... = TLV Type: Organization Specific (127)
319 .... ...0 0001 0001 = TLV Length: 17
320 Organization Unique Code: IEEE 802.1 (0x0080c2)
321 IEEE 802.1 Subtype: VLAN Name (0x03)
322 VLAN Identifier: 741 (0x02E5)
323 VLAN Name Length: 10
324 VLAN Name: Third VLAN
325 IEEE 802.3 - Link Aggregation
326 1111 111. .... .... = TLV Type: Organization Specific (127)
327 .... ...0 0000 1001 = TLV Length: 9
328 Organization Unique Code: IEEE 802.3 (0x00120f)
329 IEEE 802.3 Subtype: Link Aggregation (0x03)
330 Aggregation Status: 0x01
331 .... ...1 = Aggregation Capability: Yes
332 .... ..0. = Aggregation Status: Not Enabled
333 Aggregated Port Id: 0
334 IEEE 802.3 - MAC/PHY Configuration/Status
335 1111 111. .... .... = TLV Type: Organization Specific (127)
336 .... ...0 0000 1001 = TLV Length: 9
337 Organization Unique Code: IEEE 802.3 (0x00120f)
338 IEEE 802.3 Subtype: MAC/PHY Configuration/Status (0x01)
339 Auto-Negotiation Support/Status: 0x00
340 .... ...0 = Auto-Negotiation: Not Supported
341 .... ..0. = Auto-Negotiation: Not Enabled
342 PMD Auto-Negotiation Advertised Capability: 0x0000
343 Operational MAU Type: Unknown (0x0000)
344 IEEE 802.3 - Maximum Frame Size
345 1111 111. .... .... = TLV Type: Organization Specific (127)
346 .... ...0 0000 0110 = TLV Length: 6
347 Organization Unique Code: IEEE 802.3 (0x00120f)
348 IEEE 802.3 Subtype: Maximum Frame Size (0x04)
349 Maximum Frame Size: 1516
350 End of LLDPDU
351 0000 000. .... .... = TLV Type: End of LLDPDU (0)
352 .... ...0 0000 0000 = TLV Length: 0
353 */
354 char pkt1[] = {
355 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e, 0x5e, 0x10,
356 0x8e, 0xe7, 0x84, 0xad, 0x88, 0xcc, 0x02, 0x0d,
357 0x07, 0x43, 0x68, 0x61, 0x73, 0x73, 0x69, 0x73,
358 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x04, 0x07, 0x03,
359 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad, 0x06, 0x02,
360 0x00, 0xb4, 0x0a, 0x0e, 0x53, 0x65, 0x63, 0x6f,
361 0x6e, 0x64, 0x20, 0x63, 0x68, 0x61, 0x73, 0x73,
362 0x69, 0x73, 0x0c, 0x13, 0x43, 0x68, 0x61, 0x73,
363 0x73, 0x69, 0x73, 0x20, 0x64, 0x65, 0x73, 0x63,
364 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x0e,
365 0x04, 0x00, 0x14, 0x00, 0x10, 0x08, 0x15, 0x46,
366 0x61, 0x6b, 0x65, 0x20, 0x70, 0x6f, 0x72, 0x74,
367 0x20, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70,
368 0x74, 0x69, 0x6f, 0x6e, 0xfe, 0x11, 0x00, 0x80,
369 0xc2, 0x03, 0x00, 0x9d, 0x0a, 0x46, 0x69, 0x72,
370 0x73, 0x74, 0x20, 0x56, 0x4c, 0x41, 0x4e, 0xfe,
371 0x12, 0x00, 0x80, 0xc2, 0x03, 0x04, 0xdf, 0x0b,
372 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x56,
373 0x4c, 0x41, 0x4e, 0xfe, 0x11, 0x00, 0x80, 0xc2,
374 0x03, 0x02, 0xe5, 0x0a, 0x54, 0x68, 0x69, 0x72,
375 0x64, 0x20, 0x56, 0x4c, 0x41, 0x4e, 0xfe, 0x09,
376 0x00, 0x12, 0x0f, 0x03, 0x01, 0x00, 0x00, 0x00,
377 0x00, 0xfe, 0x09, 0x00, 0x12, 0x0f, 0x01, 0x00,
378 0x00, 0x00, 0x00, 0x00, 0xfe, 0x06, 0x00, 0x12,
379 0x0f, 0x04, 0x05, 0xec, 0x00, 0x00 };
380 struct packet *pkt;
381
382 /* Populate port and chassis */
383 hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_LLADDR;
384 hardware.h_lport.p_id = macaddress;
385 hardware.h_lport.p_id_len = ETH_ALEN;
386 hardware.h_lport.p_descr = "Fake port description";
387 hardware.h_lport.p_mfs = 1516;
388 chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LOCAL;
389 chassis.c_id = "Chassis name";
390 chassis.c_id_len = strlen(chassis.c_id);
391 chassis.c_name = "Second chassis";
392 chassis.c_descr = "Chassis description";
393 chassis.c_cap_available = LLDP_CAP_ROUTER | LLDP_CAP_BRIDGE;
394 chassis.c_cap_enabled = LLDP_CAP_ROUTER;
395 vlan1.v_name = "First VLAN"; vlan1.v_vid = 157;
396 vlan2.v_name = "Second VLAN"; vlan2.v_vid = 1247;
397 vlan3.v_name = "Third VLAN"; vlan3.v_vid = 741;
398 TAILQ_INSERT_TAIL(&hardware.h_lport.p_vlans, &vlan1, v_entries);
399 TAILQ_INSERT_TAIL(&hardware.h_lport.p_vlans, &vlan2, v_entries);
400 TAILQ_INSERT_TAIL(&hardware.h_lport.p_vlans, &vlan3, v_entries);
401
402 /* Build packet */
403 n = lldp_send(NULL, &hardware);
404 fail_unless(n == 0, "unable to build packet");
405 fail_unless(!TAILQ_EMPTY(&pkts));
406 pkt = TAILQ_FIRST(&pkts);
407 ck_assert_int_eq(pkt->size, sizeof(pkt1));
408 fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0);
409 fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than one packet sent");
410 }
411 END_TEST
412
413 START_TEST (test_send_med)
414 {
415 int n;
416 char loc[] = {0x28, 0x02, 0x55, 0x53, 0x01, 0x02, 0x43, 0x41, 0x03,
417 0x09, 0x52, 0x6f, 0x73, 0x65, 0x76, 0x69, 0x6c,
418 0x6c, 0x65, 0x06, 0x09, 0x46, 0x6f, 0x6f, 0x74,
419 0x68, 0x69, 0x6c, 0x6c, 0x73, 0x13, 0x04, 0x38,
420 0x30, 0x30, 0x30, 0x1a, 0x03, 0x52, 0x33, 0x4c};
421
422 /* Packet we should build:
423 Ethernet II, Src: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad), Dst: LLDP_Multicast (01:80:c2:00:00:0e)
424 Destination: LLDP_Multicast (01:80:c2:00:00:0e)
425 Address: LLDP_Multicast (01:80:c2:00:00:0e)
426 Source: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
427 Address: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
428 he factory default)
429 Type: 802.1 Link Layer Discovery Protocol (LLDP) (0x88cc)
430 Link Layer Discovery Protocol
431 Chassis Subtype = Locally assigned
432 0000 001. .... .... = TLV Type: Chassis Id (1)
433 .... ...0 0000 1101 = TLV Length: 13
434 Chassis Id Subtype: Locally assigned (7)
435 Chassis Id: Chassis name
436 Port Subtype = MAC address
437 0000 010. .... .... = TLV Type: Port Id (2)
438 .... ...0 0000 0111 = TLV Length: 7
439 Port Id Subtype: MAC address (3)
440 Port Id: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
441 Time To Live = 180 sec
442 0000 011. .... .... = TLV Type: Time to Live (3)
443 .... ...0 0000 0010 = TLV Length: 2
444 Seconds: 180
445 System Name = Third chassis
446 0000 101. .... .... = TLV Type: System Name (5)
447 .... ...0 0000 1101 = TLV Length: 13
448 System Name = Third chassis
449 System Description = Chassis description
450 0000 110. .... .... = TLV Type: System Description (6)
451 .... ...0 0001 0011 = TLV Length: 19
452 System Description = Chassis description
453 Capabilities
454 0000 111. .... .... = TLV Type: System Capabilities (7)
455 .... ...0 0000 0100 = TLV Length: 4
456 Capabilities: 0x0014
457 .... .... .... .1.. = Bridge
458 .... .... ...1 .... = Router
459 Enabled Capabilities: 0x0010
460 .... .... ...1 .... = Router
461 Port Description = Fake port description
462 0000 100. .... .... = TLV Type: Port Description (4)
463 .... ...0 0001 0101 = TLV Length: 21
464 Port Description: Fake port description
465 IEEE 802.3 - Link Aggregation
466 1111 111. .... .... = TLV Type: Organization Specific (127)
467 .... ...0 0000 1001 = TLV Length: 9
468 Organization Unique Code: IEEE 802.3 (0x00120f)
469 IEEE 802.3 Subtype: Link Aggregation (0x03)
470 Aggregation Status: 0x01
471 .... ...1 = Aggregation Capability: Yes
472 .... ..0. = Aggregation Status: Not Enabled
473 Aggregated Port Id: 0
474 IEEE 802.3 - MAC/PHY Configuration/Status
475 1111 111. .... .... = TLV Type: Organization Specific (127)
476 .... ...0 0000 1001 = TLV Length: 9
477 Organization Unique Code: IEEE 802.3 (0x00120f)
478 IEEE 802.3 Subtype: MAC/PHY Configuration/Status (0x01)
479 Auto-Negotiation Support/Status: 0x00
480 .... ...0 = Auto-Negotiation: Not Supported
481 .... ..0. = Auto-Negotiation: Not Enabled
482 PMD Auto-Negotiation Advertised Capability: 0x0000
483 Operational MAU Type: Unknown (0x0000)
484 IEEE 802.3 - Maximum Frame Size
485 1111 111. .... .... = TLV Type: Organization Specific (127)
486 .... ...0 0000 0110 = TLV Length: 6
487 Organization Unique Code: IEEE 802.3 (0x00120f)
488 IEEE 802.3 Subtype: Maximum Frame Size (0x04)
489 Maximum Frame Size: 1516
490 TIA - Media Capabilities
491 1111 111. .... .... = TLV Type: Organization Specific (127)
492 .... ...0 0000 0111 = TLV Length: 7
493 Organization Unique Code: TIA (0x0012bb)
494 Media Subtype: Media Capabilities (0x01)
495 Capabilities: 0x002f
496 .... .... .... ...1 = LLDP-MED Capabilities
497 .... .... .... ..1. = Network Policy
498 .... .... .... .1.. = Location Identification
499 .... .... .... 1... = Extended Power via MDI-PSE
500 .... .... ..1. .... = Inventory
501 Class Type: Endpoint Class III
502 TIA - Inventory - Hardware Revision
503 1111 111. .... .... = TLV Type: Organization Specific (127)
504 .... ...0 0001 0010 = TLV Length: 18
505 Organization Unique Code: TIA (0x0012bb)
506 Media Subtype: Inventory - Hardware Revision (0x05)
507 Hardware Revision: hardware rev 5
508 TIA - Inventory - Firmware Revision
509 1111 111. .... .... = TLV Type: Organization Specific (127)
510 .... ...0 0000 1000 = TLV Length: 8
511 Organization Unique Code: TIA (0x0012bb)
512 Media Subtype: Inventory - Firmware Revision (0x06)
513 Firmware Revision: 47b5
514 TIA - Inventory - Software Revision
515 1111 111. .... .... = TLV Type: Organization Specific (127)
516 .... ...0 0000 1100 = TLV Length: 12
517 Organization Unique Code: TIA (0x0012bb)
518 Media Subtype: Inventory - Software Revision (0x07)
519 Software Revision: 2.6.22b5
520 TIA - Inventory - Serial Number
521 1111 111. .... .... = TLV Type: Organization Specific (127)
522 .... ...0 0000 1100 = TLV Length: 12
523 Organization Unique Code: TIA (0x0012bb)
524 Media Subtype: Inventory - Serial Number (0x08)
525 Serial Number: SN 47842
526 TIA - Location Identification
527 1111 111. .... .... = TLV Type: Organization Specific (127)
528 .... ...0 0010 1110 = TLV Length: 46
529 Organization Unique Code: TIA (0x0012bb)
530 Media Subtype: Location Identification (0x03)
531 Location Data Format: Civic Address LCI (2)
532 LCI Length: 40
533 What: Location of the client (2)
534 Country: US
535 CA Type: National subdivisions (province, state, etc) (1)
536 CA Length: 2
537 CA Value: CA
538 CA Type: City, township (3)
539 CA Length: 9
540 CA Value: Roseville
541 CA Type: Street (6)
542 CA Length: 9
543 CA Value: Foothills
544 CA Type: House number (19)
545 CA Length: 4
546 CA Value: 8000
547 CA Type: Unit (26)
548 CA Length: 3
549 CA Value: R3L
550 End of LLDPDU
551 0000 000. .... .... = TLV Type: End of LLDPDU (0)
552 .... ...0 0000 0000 = TLV Length: 0
553 */
554 char pkt1[] = {
555 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e, 0x5e, 0x10,
556 0x8e, 0xe7, 0x84, 0xad, 0x88, 0xcc, 0x02, 0x0d,
557 0x07, 0x43, 0x68, 0x61, 0x73, 0x73, 0x69, 0x73,
558 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x04, 0x07, 0x03,
559 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad, 0x06, 0x02,
560 0x00, 0xb4, 0x0a, 0x0d, 0x54, 0x68, 0x69, 0x72,
561 0x64, 0x20, 0x63, 0x68, 0x61, 0x73, 0x73, 0x69,
562 0x73, 0x0c, 0x13, 0x43, 0x68, 0x61, 0x73, 0x73,
563 0x69, 0x73, 0x20, 0x64, 0x65, 0x73, 0x63, 0x72,
564 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x0e, 0x04,
565 0x00, 0x14, 0x00, 0x10, 0x08, 0x15, 0x46, 0x61,
566 0x6b, 0x65, 0x20, 0x70, 0x6f, 0x72, 0x74, 0x20,
567 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74,
568 0x69, 0x6f, 0x6e, 0xfe, 0x09, 0x00, 0x12, 0x0f,
569 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x09,
570 0x00, 0x12, 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00,
571 0x00, 0xfe, 0x06, 0x00, 0x12, 0x0f, 0x04, 0x05,
572 0xec, 0xfe, 0x07, 0x00, 0x12, 0xbb, 0x01, 0x00,
573 0x2f, 0x03, 0xfe, 0x12, 0x00, 0x12, 0xbb, 0x05,
574 0x68, 0x61, 0x72, 0x64, 0x77, 0x61, 0x72, 0x65,
575 0x20, 0x72, 0x65, 0x76, 0x20, 0x35, 0xfe, 0x08,
576 0x00, 0x12, 0xbb, 0x06, 0x34, 0x37, 0x62, 0x35,
577 0xfe, 0x0c, 0x00, 0x12, 0xbb, 0x07, 0x32, 0x2e,
578 0x36, 0x2e, 0x32, 0x32, 0x62, 0x35, 0xfe, 0x0c,
579 0x00, 0x12, 0xbb, 0x08, 0x53, 0x4e, 0x20, 0x34,
580 0x37, 0x38, 0x34, 0x32, 0xfe, 0x2e, 0x00, 0x12,
581 0xbb, 0x03, 0x02, 0x28, 0x02, 0x55, 0x53, 0x01,
582 0x02, 0x43, 0x41, 0x03, 0x09, 0x52, 0x6f, 0x73,
583 0x65, 0x76, 0x69, 0x6c, 0x6c, 0x65, 0x06, 0x09,
584 0x46, 0x6f, 0x6f, 0x74, 0x68, 0x69, 0x6c, 0x6c,
585 0x73, 0x13, 0x04, 0x38, 0x30, 0x30, 0x30, 0x1a,
586 0x03, 0x52, 0x33, 0x4c, 0x00, 0x00 };
587 struct packet *pkt;
588
589 /* Populate port and chassis */
590 hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_LLADDR;
591 hardware.h_lport.p_id = macaddress;
592 hardware.h_lport.p_id_len = ETH_ALEN;
593 hardware.h_lport.p_descr = "Fake port description";
594 hardware.h_lport.p_mfs = 1516;
595 chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LOCAL;
596 chassis.c_id = "Chassis name";
597 chassis.c_id_len = strlen(chassis.c_id);
598 chassis.c_name = "Third chassis";
599 chassis.c_descr = "Chassis description";
600 chassis.c_cap_available = LLDP_CAP_ROUTER | LLDP_CAP_BRIDGE;
601 chassis.c_cap_enabled = LLDP_CAP_ROUTER;
602 chassis.c_med_cap_available = LLDPMED_CAP_CAP | LLDPMED_CAP_POLICY |
603 LLDPMED_CAP_LOCATION | LLDPMED_CAP_MDI_PSE |
604 LLDPMED_CAP_IV;
605 chassis.c_med_type = LLDPMED_CLASS_III;
606 chassis.c_med_hw = "hardware rev 5";
607 chassis.c_med_fw = "47b5";
608 chassis.c_med_sw = "2.6.22b5";
609 chassis.c_med_sn = "SN 47842";
610 hardware.h_lport.p_med_cap_enabled = chassis.c_med_cap_available;
611 hardware.h_lport.p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].format =
612 LLDPMED_LOCFORMAT_CIVIC;
613 hardware.h_lport.p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].data_len =
614 loc[0] + 1; /* +1 is because of the size */
615 hardware.h_lport.p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].data = loc;
616 /* The following is ignored */
617 hardware.h_lport.p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].type =
618 LLDPMED_APPTYPE_SOFTPHONEVOICE;
619 hardware.h_lport.p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].tagged =
620 1;
621 hardware.h_lport.p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].vid =
622 51;
623 hardware.h_lport.p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].priority =
624 6;
625 hardware.h_lport.p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].dscp =
626 46;
627 hardware.h_lport.p_med_pow_devicetype = LLDPMED_POW_TYPE_PSE;
628 hardware.h_lport.p_med_pow_source = LLDPMED_POW_SOURCE_PRIMARY;
629 hardware.h_lport.p_med_pow_priority = LLDPMED_POW_PRIO_HIGH;
630 hardware.h_lport.p_med_pow_val = 65;
631
632 /* Build packet */
633 n = lldp_send(NULL, &hardware);
634 fail_unless(n == 0, "unable to build packet");
635 fail_unless(!TAILQ_EMPTY(&pkts));
636 pkt = TAILQ_FIRST(&pkts);
637 ck_assert_int_eq(pkt->size, sizeof(pkt1));
638 fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0);
639 fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than one packet sent");
640 }
641 END_TEST
642
643 START_TEST (test_send_dot3)
644 {
645 int n;
646 /* Packet we should build:
647 Ethernet II, Src: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad), Dst: LLDP_Multicast (01:80:c2:00:00:0e)
648 Destination: LLDP_Multicast (01:80:c2:00:00:0e)
649 Address: LLDP_Multicast (01:80:c2:00:00:0e)
650 Source: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
651 Address: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
652 he factory default)
653 Type: 802.1 Link Layer Discovery Protocol (LLDP) (0x88cc)
654 Link Layer Discovery Protocol
655 Chassis Subtype = MAC address
656 0000 001. .... .... = TLV Type: Chassis Id (1)
657 .... ...0 0000 0111 = TLV Length: 7
658 Chassis Id Subtype: MAC address (4)
659 Chassis Id: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
660 Port Subtype = Interface name
661 0000 010. .... .... = TLV Type: Port Id (2)
662 .... ...0 0001 0001 = TLV Length: 17
663 Port Id Subtype: Interface name (5)
664 Port Id: FastEthernet 1/5
665 Time To Live = 180 sec
666 0000 011. .... .... = TLV Type: Time to Live (3)
667 .... ...0 0000 0010 = TLV Length: 2
668 Seconds: 180
669 System Name = Fourth chassis
670 0000 101. .... .... = TLV Type: System Name (5)
671 .... ...0 0000 1110 = TLV Length: 14
672 System Name = Fourth chassis
673 System Description = Long chassis description
674 0000 110. .... .... = TLV Type: System Description (6)
675 .... ...0 0001 1000 = TLV Length: 24
676 System Description = Long chassis description
677 Capabilities
678 0000 111. .... .... = TLV Type: System Capabilities (7)
679 .... ...0 0000 0100 = TLV Length: 4
680 Capabilities: 0x0018
681 .... .... .... 1... = WLAN access point
682 .... .... ...1 .... = Router
683 Enabled Capabilities: 0x0018
684 .... .... .... 1... = WLAN access point
685 .... .... ...1 .... = Router
686 Port Description = Fake port description
687 0000 100. .... .... = TLV Type: Port Description (4)
688 .... ...0 0001 0101 = TLV Length: 21
689 Port Description: Fake port description
690 IEEE 802.3 - Link Aggregation
691 1111 111. .... .... = TLV Type: Organization Specific (127)
692 .... ...0 0000 1001 = TLV Length: 9
693 Organization Unique Code: IEEE 802.3 (0x00120f)
694 IEEE 802.3 Subtype: Link Aggregation (0x03)
695 Aggregation Status: 0x03
696 .... ...1 = Aggregation Capability: Yes
697 .... ..1. = Aggregation Status: Enabled
698 Aggregated Port Id: 5
699 IEEE 802.3 - MAC/PHY Configuration/Status
700 1111 111. .... .... = TLV Type: Organization Specific (127)
701 .... ...0 0000 1001 = TLV Length: 9
702 Organization Unique Code: IEEE 802.3 (0x00120f)
703 IEEE 802.3 Subtype: MAC/PHY Configuration/Status (0x01)
704 Auto-Negotiation Support/Status: 0x03
705 .... ...1 = Auto-Negotiation: Supported
706 .... ..1. = Auto-Negotiation: Enabled
707 PMD Auto-Negotiation Advertised Capability: 0x6C00
708 .... .1.. .... .... = 100BASE-TX (full duplex mode)
709 .... 1... .... .... = 100BASE-TX (half duplex mode)
710 ..1. .... .... .... = 10BASE-T (full duplex mode)
711 .1.. .... .... .... = 10BASE-T (half duplex mode)
712 Operational MAU Type: 100BaseTXFD - 2 pair category 5 UTP, full duplex mode (0x0010)
713 IEEE 802.3 - Maximum Frame Size
714 1111 111. .... .... = TLV Type: Organization Specific (127)
715 .... ...0 0000 0110 = TLV Length: 6
716 Organization Unique Code: IEEE 802.3 (0x00120f)
717 IEEE 802.3 Subtype: Maximum Frame Size (0x04)
718 Maximum Frame Size: 1516
719 End of LLDPDU
720 0000 000. .... .... = TLV Type: End of LLDPDU (0)
721 .... ...0 0000 0000 = TLV Length: 0
722 */
723 char pkt1[] = {
724 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e, 0x5e, 0x10,
725 0x8e, 0xe7, 0x84, 0xad, 0x88, 0xcc, 0x02, 0x07,
726 0x04, 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad, 0x04,
727 0x11, 0x05, 0x46, 0x61, 0x73, 0x74, 0x45, 0x74,
728 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x31,
729 0x2f, 0x35, 0x06, 0x02, 0x00, 0xb4, 0x0a, 0x0e,
730 0x46, 0x6f, 0x75, 0x72, 0x74, 0x68, 0x20, 0x63,
731 0x68, 0x61, 0x73, 0x73, 0x69, 0x73, 0x0c, 0x18,
732 0x4c, 0x6f, 0x6e, 0x67, 0x20, 0x63, 0x68, 0x61,
733 0x73, 0x73, 0x69, 0x73, 0x20, 0x64, 0x65, 0x73,
734 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e,
735 0x0e, 0x04, 0x00, 0x18, 0x00, 0x18, 0x08, 0x15,
736 0x46, 0x61, 0x6b, 0x65, 0x20, 0x70, 0x6f, 0x72,
737 0x74, 0x20, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69,
738 0x70, 0x74, 0x69, 0x6f, 0x6e, 0xfe, 0x09, 0x00,
739 0x12, 0x0f, 0x03, 0x03, 0x00, 0x00, 0x00, 0x05,
740 0xfe, 0x09, 0x00, 0x12, 0x0f, 0x01, 0x03, 0x6c,
741 0x00, 0x00, 0x10, 0xfe, 0x06, 0x00, 0x12, 0x0f,
742 0x04, 0x05, 0xec, 0x00, 0x00 };
743 struct packet *pkt;
744
745 /* Populate port and chassis */
746 hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME;
747 hardware.h_lport.p_id = "FastEthernet 1/5";
748 hardware.h_lport.p_id_len = strlen(hardware.h_lport.p_id);
749 hardware.h_lport.p_descr = "Fake port description";
750 hardware.h_lport.p_mfs = 1516;
751 hardware.h_lport.p_aggregid = 5;
752 hardware.h_lport.p_autoneg_support = 1;
753 hardware.h_lport.p_autoneg_enabled = 1;
754 hardware.h_lport.p_autoneg_advertised = LLDP_DOT3_LINK_AUTONEG_10BASE_T |
755 LLDP_DOT3_LINK_AUTONEG_10BASET_FD | LLDP_DOT3_LINK_AUTONEG_100BASE_TX |
756 LLDP_DOT3_LINK_AUTONEG_100BASE_TXFD;
757 hardware.h_lport.p_mau_type = LLDP_DOT3_MAU_100BASETXFD;
758 chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
759 chassis.c_id = macaddress;
760 chassis.c_id_len = ETH_ALEN;
761 chassis.c_name = "Fourth chassis";
762 chassis.c_descr = "Long chassis description";
763 chassis.c_cap_available = chassis.c_cap_enabled = LLDP_CAP_ROUTER | LLDP_CAP_WLAN;
764
765 /* Build packet */
766 n = lldp_send(NULL, &hardware);
767 fail_unless(n == 0, "unable to build packet");
768 fail_unless(!TAILQ_EMPTY(&pkts));
769 pkt = TAILQ_FIRST(&pkts);
770 ck_assert_int_eq(pkt->size, sizeof(pkt1));
771 fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0);
772 fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than one packet sent");
773 }
774 END_TEST
775
776 Suite *
777 lldp_suite(void)
778 {
779 Suite *s = suite_create("LLDP");
780
781 /* Send tests are first run without knowing the result. The
782 result is then checked with:
783 tshark -V -T text -r tests/lldp_send_0000.pcap
784
785 If the result is correct, then, we get the packet as C
786 bytes using wireshark export to C arrays (tshark seems not
787 be able to do this).
788 */
789
790 TCase *tc_send = tcase_create("Send LLDP packets");
791 tcase_add_checked_fixture(tc_send, setup, teardown);
792 tcase_add_test(tc_send, test_send_basic);
793 tcase_add_test(tc_send, test_send_vlan);
794 tcase_add_test(tc_send, test_send_med);
795 tcase_add_test(tc_send, test_send_dot3);
796 suite_add_tcase(s, tc_send);
797
798 TCase *tc_receive = tcase_create("Receive LLDP packets");
799 suite_add_tcase(s, tc_receive);
800
801 return s;
802 }
803
804 int
805 main()
806 {
807 int number_failed;
808 Suite *s = lldp_suite ();
809 SRunner *sr = srunner_create (s);
810 srunner_set_fork_status (sr, CK_NOFORK); /* Can't fork because
811 we need to write
812 files */
813 srunner_run_all (sr, CK_ENV);
814 number_failed = srunner_ntests_failed (sr);
815 srunner_free (sr);
816 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
817 }