]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/daemon/edp.c
a3a23b2549c640832a822fd91a04a1442ba82053
[thirdparty/lldpd.git] / src / daemon / edp.c
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include "lldpd.h"
19 #include "frame.h"
20
21 #ifdef ENABLE_EDP
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <arpa/inet.h>
27 #include <fnmatch.h>
28 #include <assert.h>
29
30 static int seq = 0;
31
32 int
33 edp_send(struct lldpd *global,
34 struct lldpd_hardware *hardware)
35 {
36 const u_int8_t mcastaddr[] = EDP_MULTICAST_ADDR;
37 const u_int8_t llcorg[] = LLC_ORG_EXTREME;
38 struct lldpd_chassis *chassis;
39 int length, i, v;
40 u_int8_t *packet, *pos, *pos_llc, *pos_len_eh, *pos_len_edp, *pos_edp, *tlv, *end;
41 u_int16_t checksum;
42 #ifdef ENABLE_DOT1
43 struct lldpd_vlan *vlan;
44 unsigned int state = 0;
45 #endif
46 u_int8_t edp_fakeversion[] = {7, 6, 4, 99};
47 /* Subsequent XXX can be replaced by other values. We place
48 them here to ensure the position of "" to be a bit
49 invariant with version changes. */
50 char *deviceslot[] = { "eth", "veth", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "", NULL };
51
52 chassis = hardware->h_lport.p_chassis;
53 #ifdef ENABLE_DOT1
54 while (state != 2) {
55 #endif
56 length = hardware->h_mtu;
57 if ((packet = (u_int8_t*)malloc(length)) == NULL)
58 return ENOMEM;
59 memset(packet, 0, length);
60 pos = packet;
61 v = 0;
62
63 /* Ethernet header */
64 if (!(
65 POKE_BYTES(mcastaddr, sizeof(mcastaddr)) &&
66 POKE_BYTES(&hardware->h_lladdr, sizeof(hardware->h_lladdr)) &&
67 POKE_SAVE(pos_len_eh) && /* We compute the len later */
68 POKE_UINT16(0)))
69 goto toobig;
70
71 /* LLC */
72 if (!(
73 POKE_SAVE(pos_llc) && /* We need to save our
74 current position to
75 compute ethernet len */
76 /* SSAP and DSAP */
77 POKE_UINT8(0xaa) && POKE_UINT8(0xaa) &&
78 /* Control field */
79 POKE_UINT8(0x03) &&
80 /* ORG */
81 POKE_BYTES(llcorg, sizeof(llcorg)) &&
82 POKE_UINT16(LLC_PID_EDP)))
83 goto toobig;
84
85 /* EDP header */
86 if ((chassis->c_id_len != ETH_ALEN) ||
87 (chassis->c_id_subtype != LLDP_CHASSISID_SUBTYPE_LLADDR)) {
88 LLOG_WARNX("local chassis does not use MAC address as chassis ID!?");
89 free(packet);
90 return EINVAL;
91 }
92 if (!(
93 POKE_SAVE(pos_edp) && /* Save the start of EDP frame */
94 POKE_UINT8(1) && POKE_UINT8(0) &&
95 POKE_SAVE(pos_len_edp) && /* We compute the len
96 and the checksum
97 later */
98 POKE_UINT32(0) && /* Len + Checksum */
99 POKE_UINT16(seq) &&
100 POKE_UINT16(0) &&
101 POKE_BYTES(chassis->c_id, ETH_ALEN)))
102 goto toobig;
103 seq++;
104
105 #ifdef ENABLE_DOT1
106 switch (state) {
107 case 0:
108 #endif
109 /* Display TLV */
110 if (!(
111 POKE_START_EDP_TLV(EDP_TLV_DISPLAY) &&
112 POKE_BYTES(chassis->c_name, strlen(chassis->c_name)) &&
113 POKE_UINT8(0) && /* Add a NULL character
114 for better
115 compatibility */
116 POKE_END_EDP_TLV))
117 goto toobig;
118
119 /* Info TLV */
120 if (!(
121 POKE_START_EDP_TLV(EDP_TLV_INFO)))
122 goto toobig;
123 /* We try to emulate the slot thing */
124 for (i=0; deviceslot[i] != NULL; i++) {
125 if (strncmp(hardware->h_ifname, deviceslot[i],
126 strlen(deviceslot[i])) == 0) {
127 if (!(
128 POKE_UINT16(i) &&
129 POKE_UINT16(atoi(hardware->h_ifname +
130 strlen(deviceslot[i])))))
131 goto toobig;
132 break;
133 }
134 }
135 /* If we don't find a "slot", we say that the
136 interface is in slot 8 */
137 if (deviceslot[i] == NULL) {
138 if (!(
139 POKE_UINT16(8) &&
140 POKE_UINT16(hardware->h_ifindex)))
141 goto toobig;
142 }
143 if (!(
144 POKE_UINT16(0) && /* vchassis */
145 POKE_UINT32(0) && POKE_UINT16(0) && /* Reserved */
146 /* Version */
147 POKE_BYTES(edp_fakeversion, sizeof(edp_fakeversion)) &&
148 /* Connections, we say that we won't
149 have more interfaces than this
150 mask. */
151 POKE_UINT32(0xffffffff) &&
152 POKE_UINT32(0) && POKE_UINT32(0) && POKE_UINT32(0) &&
153 POKE_END_EDP_TLV))
154 goto toobig;
155
156 #ifdef ENABLE_DOT1
157 break;
158 case 1:
159 TAILQ_FOREACH(vlan, &hardware->h_lport.p_vlans,
160 v_entries) {
161 v++;
162 if (!(
163 POKE_START_EDP_TLV(EDP_TLV_VLAN) &&
164 POKE_UINT8(0) && /* Flags: no IP address */
165 POKE_UINT8(0) && /* Reserved */
166 POKE_UINT16(vlan->v_vid) &&
167 POKE_UINT32(0) && /* Reserved */
168 POKE_UINT32(0) && /* IP address */
169 /* VLAN name */
170 POKE_BYTES(vlan->v_name, strlen(vlan->v_name)) &&
171 POKE_UINT8(0) &&
172 POKE_END_EDP_TLV))
173 goto toobig;
174 }
175 break;
176 }
177
178 if ((state == 1) && (v == 0)) /* No VLAN, no need to send another TLV */
179 break;
180 #endif
181
182 /* Null TLV */
183 if (!(
184 POKE_START_EDP_TLV(EDP_TLV_NULL) &&
185 POKE_END_EDP_TLV &&
186 POKE_SAVE(end)))
187 goto toobig;
188
189 /* Compute len and checksum */
190 i = end - pos_llc; /* Ethernet length */
191 v = end - pos_edp; /* EDP length */
192 POKE_RESTORE(pos_len_eh);
193 if (!(POKE_UINT16(i))) goto toobig;
194 POKE_RESTORE(pos_len_edp);
195 if (!(POKE_UINT16(v))) goto toobig;
196 checksum = frame_checksum(pos_edp, v, 0);
197 if (!(POKE_UINT16(ntohs(checksum)))) goto toobig;
198
199 if (hardware->h_ops->send(global, hardware,
200 (char *)packet, end - packet) == -1) {
201 LLOG_WARN("unable to send packet on real device for %s",
202 hardware->h_ifname);
203 free(packet);
204 return ENETDOWN;
205 }
206 free(packet);
207
208 #ifdef ENABLE_DOT1
209 state++;
210 }
211 #endif
212
213 hardware->h_tx_cnt++;
214 return 0;
215 toobig:
216 free(packet);
217 return E2BIG;
218 }
219
220 #define CHECK_TLV_SIZE(x, name) \
221 do { if (tlv_len < (x)) { \
222 LLOG_WARNX(name " EDP TLV too short received on %s",\
223 hardware->h_ifname); \
224 goto malformed; \
225 } } while (0)
226
227 int
228 edp_decode(struct lldpd *cfg, char *frame, int s,
229 struct lldpd_hardware *hardware,
230 struct lldpd_chassis **newchassis, struct lldpd_port **newport)
231 {
232 struct lldpd_chassis *chassis;
233 struct lldpd_port *port;
234 #ifdef ENABLE_DOT1
235 struct lldpd_mgmt *mgmt, *mgmt_next, *m;
236 struct lldpd_vlan *lvlan = NULL, *lvlan_next;
237 #endif
238 const unsigned char edpaddr[] = EDP_MULTICAST_ADDR;
239 int length, gotend = 0, gotvlans = 0, edp_len, tlv_len, tlv_type;
240 int edp_port, edp_slot;
241 u_int8_t *pos, *pos_edp, *tlv;
242 u_int8_t version[4];
243 #ifdef ENABLE_DOT1
244 struct in_addr address;
245 struct lldpd_port *oport;
246 #endif
247
248 if ((chassis = calloc(1, sizeof(struct lldpd_chassis))) == NULL) {
249 LLOG_WARN("failed to allocate remote chassis");
250 return -1;
251 }
252 TAILQ_INIT(&chassis->c_mgmt);
253 if ((port = calloc(1, sizeof(struct lldpd_port))) == NULL) {
254 LLOG_WARN("failed to allocate remote port");
255 free(chassis);
256 return -1;
257 }
258 #ifdef ENABLE_DOT1
259 TAILQ_INIT(&port->p_vlans);
260 #endif
261
262 length = s;
263 pos = (u_int8_t*)frame;
264
265 if (length < 2*ETH_ALEN + sizeof(u_int16_t) + 8 /* LLC */ +
266 10 + ETH_ALEN /* EDP header */) {
267 LLOG_WARNX("too short EDP frame received on %s", hardware->h_ifname);
268 goto malformed;
269 }
270
271 if (PEEK_CMP(edpaddr, sizeof(edpaddr)) != 0) {
272 LLOG_INFO("frame not targeted at EDP multicast address received on %s",
273 hardware->h_ifname);
274 goto malformed;
275 }
276 PEEK_DISCARD(ETH_ALEN); PEEK_DISCARD_UINT16;
277 PEEK_DISCARD(6); /* LLC: DSAP + SSAP + control + org */
278 if (PEEK_UINT16 != LLC_PID_EDP) {
279 LLOG_DEBUG("incorrect LLC protocol ID received on %s",
280 hardware->h_ifname);
281 goto malformed;
282 }
283
284 PEEK_SAVE(pos_edp); /* Save the start of EDP packet */
285 if (PEEK_UINT8 != 1) {
286 LLOG_WARNX("incorrect EDP version for frame received on %s",
287 hardware->h_ifname);
288 goto malformed;
289 }
290 PEEK_DISCARD_UINT8; /* Reserved */
291 edp_len = PEEK_UINT16;
292 PEEK_DISCARD_UINT16; /* Checksum */
293 PEEK_DISCARD_UINT16; /* Sequence */
294 if (PEEK_UINT16 != 0) { /* ID Type = 0 = MAC */
295 LLOG_WARNX("incorrect device id type for frame received on %s",
296 hardware->h_ifname);
297 goto malformed;
298 }
299 if (edp_len > length + 10) {
300 LLOG_WARNX("incorrect size for EDP frame received on %s",
301 hardware->h_ifname);
302 goto malformed;
303 }
304 chassis->c_ttl = LLDPD_TTL;
305 chassis->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
306 chassis->c_id_len = ETH_ALEN;
307 if ((chassis->c_id = (char *)malloc(ETH_ALEN)) == NULL) {
308 LLOG_WARN("unable to allocate memory for chassis ID");
309 goto malformed;
310 }
311 PEEK_BYTES(chassis->c_id, ETH_ALEN);
312
313 /* Let's check checksum */
314 if (frame_checksum(pos_edp, edp_len, 0) != 0) {
315 LLOG_WARNX("incorrect EDP checksum for frame received on %s",
316 hardware->h_ifname);
317 goto malformed;
318 }
319
320 while (length && !gotend) {
321 if (length < 4) {
322 LLOG_WARNX("EDP TLV header is too large for "
323 "frame received on %s",
324 hardware->h_ifname);
325 goto malformed;
326 }
327 if (PEEK_UINT8 != EDP_TLV_MARKER) {
328 LLOG_WARNX("incorrect marker starting EDP TLV header for frame "
329 "received on %s",
330 hardware->h_ifname);
331 goto malformed;
332 }
333 tlv_type = PEEK_UINT8;
334 tlv_len = PEEK_UINT16 - 4;
335 PEEK_SAVE(tlv);
336 if ((tlv_len < 0) || (tlv_len > length)) {
337 LLOG_DEBUG("incorrect size in EDP TLV header for frame "
338 "received on %s",
339 hardware->h_ifname);
340 /* Some poor old Extreme Summit are quite bogus */
341 gotend = 1;
342 break;
343 }
344 switch (tlv_type) {
345 case EDP_TLV_INFO:
346 CHECK_TLV_SIZE(32, "Info");
347 port->p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME;
348 edp_slot = PEEK_UINT16; edp_port = PEEK_UINT16;
349 if (asprintf(&port->p_id, "%d/%d",
350 edp_slot + 1, edp_port + 1) == -1) {
351 LLOG_WARN("unable to allocate memory for "
352 "port ID");
353 goto malformed;
354 }
355 port->p_id_len = strlen(port->p_id);
356 if (asprintf(&port->p_descr, "Slot %d / Port %d",
357 edp_slot + 1, edp_port + 1) == -1) {
358 LLOG_WARN("unable to allocate memory for "
359 "port description");
360 goto malformed;
361 }
362 PEEK_DISCARD_UINT16; /* vchassis */
363 PEEK_DISCARD(6); /* Reserved */
364 PEEK_BYTES(version, 4);
365 if (asprintf(&chassis->c_descr,
366 "EDP enabled device, version %d.%d.%d.%d",
367 version[0], version[1],
368 version[2], version[3]) == -1) {
369 LLOG_WARN("unable to allocate memory for "
370 "chassis description");
371 goto malformed;
372 }
373 break;
374 case EDP_TLV_DISPLAY:
375 if ((chassis->c_name = (char *)calloc(1, tlv_len + 1)) == NULL) {
376 LLOG_WARN("unable to allocate memory for chassis "
377 "name");
378 goto malformed;
379 }
380 /* TLV display contains a lot of garbage */
381 PEEK_BYTES(chassis->c_name, tlv_len);
382 break;
383 case EDP_TLV_NULL:
384 if (tlv_len != 0) {
385 LLOG_WARNX("null tlv with incorrect size in frame "
386 "received on %s",
387 hardware->h_ifname);
388 goto malformed;
389 }
390 if (length)
391 LLOG_DEBUG("extra data after edp frame on %s",
392 hardware->h_ifname);
393 gotend = 1;
394 break;
395 case EDP_TLV_VLAN:
396 #ifdef ENABLE_DOT1
397 CHECK_TLV_SIZE(12, "VLAN");
398 if ((lvlan = (struct lldpd_vlan *)calloc(1,
399 sizeof(struct lldpd_vlan))) == NULL) {
400 LLOG_WARN("unable to allocate vlan");
401 goto malformed;
402 }
403 PEEK_DISCARD_UINT16; /* Flags + reserved */
404 lvlan->v_vid = PEEK_UINT16; /* VID */
405 PEEK_DISCARD(4); /* Reserved */
406 PEEK_BYTES(&address, sizeof(address));
407
408 if ((lvlan->v_name = (char *)calloc(1,
409 tlv_len + 1 - 12)) == NULL) {
410 LLOG_WARN("unable to allocate vlan name");
411 free(lvlan);
412 goto malformed;
413 }
414 PEEK_BYTES(lvlan->v_name, tlv_len - 12);
415
416 if (address.s_addr != INADDR_ANY) {
417 mgmt = lldpd_alloc_mgmt(LLDPD_AF_IPV4, &address,
418 sizeof(struct in_addr), 0);
419 if (mgmt == NULL) {
420 assert(errno == ENOMEM);
421 LLOG_WARN("Out of memory");
422 goto malformed;
423 }
424 TAILQ_INSERT_TAIL(&chassis->c_mgmt, mgmt, m_entries);
425 }
426 TAILQ_INSERT_TAIL(&port->p_vlans,
427 lvlan, v_entries);
428 #endif
429 gotvlans = 1;
430 break;
431 default:
432 LLOG_DEBUG("unknown EDP TLV type (%d) received on %s",
433 tlv_type, hardware->h_ifname);
434 hardware->h_rx_unrecognized_cnt++;
435 }
436 PEEK_DISCARD(tlv + tlv_len - pos);
437 }
438 if ((chassis->c_id == NULL) ||
439 (port->p_id == NULL) ||
440 (chassis->c_name == NULL) ||
441 (chassis->c_descr == NULL) ||
442 (port->p_descr == NULL) ||
443 (gotend == 0)) {
444 #ifdef ENABLE_DOT1
445 if (gotvlans && gotend) {
446 /* VLAN can be sent in a separate frames. We need to add
447 * those vlans to an existing port */
448 TAILQ_FOREACH(oport, &hardware->h_rports, p_entries) {
449 if (!((oport->p_protocol == LLDPD_MODE_EDP) &&
450 (oport->p_chassis->c_id_subtype ==
451 chassis->c_id_subtype) &&
452 (oport->p_chassis->c_id_len == chassis->c_id_len) &&
453 (memcmp(oport->p_chassis->c_id, chassis->c_id,
454 chassis->c_id_len) == 0)))
455 continue;
456 /* We attach the VLANs to the found port */
457 lldpd_vlan_cleanup(oport);
458 for (lvlan = TAILQ_FIRST(&port->p_vlans);
459 lvlan != NULL;
460 lvlan = lvlan_next) {
461 lvlan_next = TAILQ_NEXT(lvlan, v_entries);
462 TAILQ_REMOVE(&port->p_vlans, lvlan, v_entries);
463 TAILQ_INSERT_TAIL(&oport->p_vlans,
464 lvlan, v_entries);
465 }
466 /* And the IP addresses */
467 for (mgmt = TAILQ_FIRST(&chassis->c_mgmt);
468 mgmt != NULL;
469 mgmt = mgmt_next) {
470 mgmt_next = TAILQ_NEXT(mgmt, m_entries);
471 TAILQ_REMOVE(&chassis->c_mgmt, mgmt, m_entries);
472 /* Don't add an address that already exists! */
473 TAILQ_FOREACH(m, &chassis->c_mgmt, m_entries)
474 if (m->m_family == mgmt->m_family &&
475 !memcmp(&m->m_addr, &mgmt->m_addr,
476 sizeof(m->m_addr))) break;
477 if (m == NULL)
478 TAILQ_INSERT_TAIL(&oport->p_chassis->c_mgmt,
479 mgmt, m_entries);
480 }
481 }
482 /* We discard the remaining frame */
483 goto malformed;
484 }
485 #else
486 if (gotvlans)
487 goto malformed;
488 #endif
489 LLOG_WARNX("some mandatory tlv are missing for frame received on %s",
490 hardware->h_ifname);
491 goto malformed;
492 }
493 *newchassis = chassis;
494 *newport = port;
495 return 1;
496
497 malformed:
498 lldpd_chassis_cleanup(chassis, 1);
499 lldpd_port_cleanup(port, 1);
500 free(port);
501 return -1;
502 }
503
504 #endif /* ENABLE_EDP */