1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
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.
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.
26 #include <arpa/inet.h>
33 edp_send(struct lldpd
*global
,
34 struct lldpd_hardware
*hardware
)
36 const u_int8_t mcastaddr
[] = EDP_MULTICAST_ADDR
;
37 const u_int8_t llcorg
[] = LLC_ORG_EXTREME
;
38 struct lldpd_chassis
*chassis
;
40 u_int8_t
*packet
, *pos
, *pos_llc
, *pos_len_eh
, *pos_len_edp
, *pos_edp
, *tlv
, *end
;
43 struct lldpd_vlan
*vlan
;
44 unsigned int state
= 0;
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
};
52 log_debug("edp", "send EDP frame on port %s", hardware
->h_ifname
);
54 chassis
= hardware
->h_lport
.p_chassis
;
58 length
= hardware
->h_mtu
;
59 if ((packet
= (u_int8_t
*)malloc(length
)) == NULL
)
61 memset(packet
, 0, length
);
67 POKE_BYTES(mcastaddr
, sizeof(mcastaddr
)) &&
68 POKE_BYTES(&hardware
->h_lladdr
, sizeof(hardware
->h_lladdr
)) &&
69 POKE_SAVE(pos_len_eh
) && /* We compute the len later */
75 POKE_SAVE(pos_llc
) && /* We need to save our
77 compute ethernet len */
79 POKE_UINT8(0xaa) && POKE_UINT8(0xaa) &&
83 POKE_BYTES(llcorg
, sizeof(llcorg
)) &&
84 POKE_UINT16(LLC_PID_EDP
)))
88 if ((chassis
->c_id_len
!= ETH_ALEN
) ||
89 (chassis
->c_id_subtype
!= LLDP_CHASSISID_SUBTYPE_LLADDR
)) {
90 log_warnx("edp", "local chassis does not use MAC address as chassis ID!?");
95 POKE_SAVE(pos_edp
) && /* Save the start of EDP frame */
96 POKE_UINT8(1) && POKE_UINT8(0) &&
97 POKE_SAVE(pos_len_edp
) && /* We compute the len
100 POKE_UINT32(0) && /* Len + Checksum */
103 POKE_BYTES(chassis
->c_id
, ETH_ALEN
)))
113 POKE_START_EDP_TLV(EDP_TLV_DISPLAY
) &&
114 POKE_BYTES(chassis
->c_name
, strlen(chassis
->c_name
)) &&
115 POKE_UINT8(0) && /* Add a NULL character
123 POKE_START_EDP_TLV(EDP_TLV_INFO
)))
125 /* We try to emulate the slot thing */
126 for (i
=0; deviceslot
[i
] != NULL
; i
++) {
127 if (strncmp(hardware
->h_ifname
, deviceslot
[i
],
128 strlen(deviceslot
[i
])) == 0) {
131 POKE_UINT16(atoi(hardware
->h_ifname
+
132 strlen(deviceslot
[i
])))))
137 /* If we don't find a "slot", we say that the
138 interface is in slot 8 */
139 if (deviceslot
[i
] == NULL
) {
142 POKE_UINT16(hardware
->h_ifindex
)))
146 POKE_UINT16(0) && /* vchassis */
147 POKE_UINT32(0) && POKE_UINT16(0) && /* Reserved */
149 POKE_BYTES(edp_fakeversion
, sizeof(edp_fakeversion
)) &&
150 /* Connections, we say that we won't
151 have more interfaces than this
153 POKE_UINT32(0xffffffff) &&
154 POKE_UINT32(0) && POKE_UINT32(0) && POKE_UINT32(0) &&
161 TAILQ_FOREACH(vlan
, &hardware
->h_lport
.p_vlans
,
165 POKE_START_EDP_TLV(EDP_TLV_VLAN
) &&
166 POKE_UINT8(0) && /* Flags: no IP address */
167 POKE_UINT8(0) && /* Reserved */
168 POKE_UINT16(vlan
->v_vid
) &&
169 POKE_UINT32(0) && /* Reserved */
170 POKE_UINT32(0) && /* IP address */
172 POKE_BYTES(vlan
->v_name
, strlen(vlan
->v_name
)) &&
180 if ((state
== 1) && (v
== 0)) /* No VLAN, no need to send another TLV */
186 POKE_START_EDP_TLV(EDP_TLV_NULL
) &&
191 /* Compute len and checksum */
192 i
= end
- pos_llc
; /* Ethernet length */
193 v
= end
- pos_edp
; /* EDP length */
194 POKE_RESTORE(pos_len_eh
);
195 if (!(POKE_UINT16(i
))) goto toobig
;
196 POKE_RESTORE(pos_len_edp
);
197 if (!(POKE_UINT16(v
))) goto toobig
;
198 checksum
= frame_checksum(pos_edp
, v
, 0);
199 if (!(POKE_UINT16(ntohs(checksum
)))) goto toobig
;
201 if (hardware
->h_ops
->send(global
, hardware
,
202 (char *)packet
, end
- packet
) == -1) {
203 log_warn("edp", "unable to send packet on real device for %s",
215 hardware
->h_tx_cnt
++;
222 #define CHECK_TLV_SIZE(x, name) \
223 do { if (tlv_len < (x)) { \
224 log_warnx("edp", name " EDP TLV too short received on %s", \
225 hardware->h_ifname); \
230 edp_decode(struct lldpd
*cfg
, char *frame
, int s
,
231 struct lldpd_hardware
*hardware
,
232 struct lldpd_chassis
**newchassis
, struct lldpd_port
**newport
)
234 struct lldpd_chassis
*chassis
;
235 struct lldpd_port
*port
;
237 struct lldpd_mgmt
*mgmt
, *mgmt_next
, *m
;
238 struct lldpd_vlan
*lvlan
= NULL
, *lvlan_next
;
240 const unsigned char edpaddr
[] = EDP_MULTICAST_ADDR
;
241 int length
, gotend
= 0, gotvlans
= 0, edp_len
, tlv_len
, tlv_type
;
242 int edp_port
, edp_slot
;
243 u_int8_t
*pos
, *pos_edp
, *tlv
;
246 struct in_addr address
;
247 struct lldpd_port
*oport
;
250 log_debug("edp", "decode EDP frame on port %s",
253 if ((chassis
= calloc(1, sizeof(struct lldpd_chassis
))) == NULL
) {
254 log_warn("edp", "failed to allocate remote chassis");
257 TAILQ_INIT(&chassis
->c_mgmt
);
258 if ((port
= calloc(1, sizeof(struct lldpd_port
))) == NULL
) {
259 log_warn("edp", "failed to allocate remote port");
264 TAILQ_INIT(&port
->p_vlans
);
268 pos
= (u_int8_t
*)frame
;
270 if (length
< 2*ETH_ALEN
+ sizeof(u_int16_t
) + 8 /* LLC */ +
271 10 + ETH_ALEN
/* EDP header */) {
272 log_warnx("edp", "too short EDP frame received on %s", hardware
->h_ifname
);
276 if (PEEK_CMP(edpaddr
, sizeof(edpaddr
)) != 0) {
277 log_info("edp", "frame not targeted at EDP multicast address received on %s",
281 PEEK_DISCARD(ETH_ALEN
); PEEK_DISCARD_UINT16
;
282 PEEK_DISCARD(6); /* LLC: DSAP + SSAP + control + org */
283 if (PEEK_UINT16
!= LLC_PID_EDP
) {
284 log_debug("edp", "incorrect LLC protocol ID received on %s",
289 PEEK_SAVE(pos_edp
); /* Save the start of EDP packet */
290 if (PEEK_UINT8
!= 1) {
291 log_warnx("edp", "incorrect EDP version for frame received on %s",
295 PEEK_DISCARD_UINT8
; /* Reserved */
296 edp_len
= PEEK_UINT16
;
297 PEEK_DISCARD_UINT16
; /* Checksum */
298 PEEK_DISCARD_UINT16
; /* Sequence */
299 if (PEEK_UINT16
!= 0) { /* ID Type = 0 = MAC */
300 log_warnx("edp", "incorrect device id type for frame received on %s",
304 if (edp_len
> length
+ 10) {
305 log_warnx("edp", "incorrect size for EDP frame received on %s",
309 chassis
->c_ttl
= LLDPD_TTL
;
310 chassis
->c_id_subtype
= LLDP_CHASSISID_SUBTYPE_LLADDR
;
311 chassis
->c_id_len
= ETH_ALEN
;
312 if ((chassis
->c_id
= (char *)malloc(ETH_ALEN
)) == NULL
) {
313 log_warn("edp", "unable to allocate memory for chassis ID");
316 PEEK_BYTES(chassis
->c_id
, ETH_ALEN
);
318 /* Let's check checksum */
319 if (frame_checksum(pos_edp
, edp_len
, 0) != 0) {
320 log_warnx("edp", "incorrect EDP checksum for frame received on %s",
325 while (length
&& !gotend
) {
327 log_warnx("edp", "EDP TLV header is too large for "
328 "frame received on %s",
332 if (PEEK_UINT8
!= EDP_TLV_MARKER
) {
333 log_warnx("edp", "incorrect marker starting EDP TLV header for frame "
338 tlv_type
= PEEK_UINT8
;
339 tlv_len
= PEEK_UINT16
- 4;
341 if ((tlv_len
< 0) || (tlv_len
> length
)) {
342 log_debug("edp", "incorrect size in EDP TLV header for frame "
345 /* Some poor old Extreme Summit are quite bogus */
351 CHECK_TLV_SIZE(32, "Info");
352 port
->p_id_subtype
= LLDP_PORTID_SUBTYPE_IFNAME
;
353 edp_slot
= PEEK_UINT16
; edp_port
= PEEK_UINT16
;
354 if (asprintf(&port
->p_id
, "%d/%d",
355 edp_slot
+ 1, edp_port
+ 1) == -1) {
356 log_warn("edp", "unable to allocate memory for "
360 port
->p_id_len
= strlen(port
->p_id
);
361 if (asprintf(&port
->p_descr
, "Slot %d / Port %d",
362 edp_slot
+ 1, edp_port
+ 1) == -1) {
363 log_warn("edp", "unable to allocate memory for "
367 PEEK_DISCARD_UINT16
; /* vchassis */
368 PEEK_DISCARD(6); /* Reserved */
369 PEEK_BYTES(version
, 4);
370 if (asprintf(&chassis
->c_descr
,
371 "EDP enabled device, version %d.%d.%d.%d",
372 version
[0], version
[1],
373 version
[2], version
[3]) == -1) {
374 log_warn("edp", "unable to allocate memory for "
375 "chassis description");
379 case EDP_TLV_DISPLAY
:
380 if ((chassis
->c_name
= (char *)calloc(1, tlv_len
+ 1)) == NULL
) {
381 log_warn("edp", "unable to allocate memory for chassis "
385 /* TLV display contains a lot of garbage */
386 PEEK_BYTES(chassis
->c_name
, tlv_len
);
390 log_warnx("edp", "null tlv with incorrect size in frame "
396 log_debug("edp", "extra data after edp frame on %s",
402 CHECK_TLV_SIZE(12, "VLAN");
403 if ((lvlan
= (struct lldpd_vlan
*)calloc(1,
404 sizeof(struct lldpd_vlan
))) == NULL
) {
405 log_warn("edp", "unable to allocate vlan");
408 PEEK_DISCARD_UINT16
; /* Flags + reserved */
409 lvlan
->v_vid
= PEEK_UINT16
; /* VID */
410 PEEK_DISCARD(4); /* Reserved */
411 PEEK_BYTES(&address
, sizeof(address
));
413 if ((lvlan
->v_name
= (char *)calloc(1,
414 tlv_len
+ 1 - 12)) == NULL
) {
415 log_warn("edp", "unable to allocate vlan name");
419 PEEK_BYTES(lvlan
->v_name
, tlv_len
- 12);
421 if (address
.s_addr
!= INADDR_ANY
) {
422 mgmt
= lldpd_alloc_mgmt(LLDPD_AF_IPV4
, &address
,
423 sizeof(struct in_addr
), 0);
425 assert(errno
== ENOMEM
);
426 log_warn("edp", "Out of memory");
429 TAILQ_INSERT_TAIL(&chassis
->c_mgmt
, mgmt
, m_entries
);
431 TAILQ_INSERT_TAIL(&port
->p_vlans
,
437 log_debug("edp", "unknown EDP TLV type (%d) received on %s",
438 tlv_type
, hardware
->h_ifname
);
439 hardware
->h_rx_unrecognized_cnt
++;
441 PEEK_DISCARD(tlv
+ tlv_len
- pos
);
443 if ((chassis
->c_id
== NULL
) ||
444 (port
->p_id
== NULL
) ||
445 (chassis
->c_name
== NULL
) ||
446 (chassis
->c_descr
== NULL
) ||
447 (port
->p_descr
== NULL
) ||
450 if (gotvlans
&& gotend
) {
451 /* VLAN can be sent in a separate frames. We need to add
452 * those vlans to an existing port */
453 TAILQ_FOREACH(oport
, &hardware
->h_rports
, p_entries
) {
454 if (!((oport
->p_protocol
== LLDPD_MODE_EDP
) &&
455 (oport
->p_chassis
->c_id_subtype
==
456 chassis
->c_id_subtype
) &&
457 (oport
->p_chassis
->c_id_len
== chassis
->c_id_len
) &&
458 (memcmp(oport
->p_chassis
->c_id
, chassis
->c_id
,
459 chassis
->c_id_len
) == 0)))
461 /* We attach the VLANs to the found port */
462 lldpd_vlan_cleanup(oport
);
463 for (lvlan
= TAILQ_FIRST(&port
->p_vlans
);
465 lvlan
= lvlan_next
) {
466 lvlan_next
= TAILQ_NEXT(lvlan
, v_entries
);
467 TAILQ_REMOVE(&port
->p_vlans
, lvlan
, v_entries
);
468 TAILQ_INSERT_TAIL(&oport
->p_vlans
,
471 /* And the IP addresses */
472 for (mgmt
= TAILQ_FIRST(&chassis
->c_mgmt
);
475 mgmt_next
= TAILQ_NEXT(mgmt
, m_entries
);
476 TAILQ_REMOVE(&chassis
->c_mgmt
, mgmt
, m_entries
);
477 /* Don't add an address that already exists! */
478 TAILQ_FOREACH(m
, &chassis
->c_mgmt
, m_entries
)
479 if (m
->m_family
== mgmt
->m_family
&&
480 !memcmp(&m
->m_addr
, &mgmt
->m_addr
,
481 sizeof(m
->m_addr
))) break;
483 TAILQ_INSERT_TAIL(&oport
->p_chassis
->c_mgmt
,
487 /* We discard the remaining frame */
494 log_warnx("edp", "some mandatory tlv are missing for frame received on %s",
498 *newchassis
= chassis
;
503 lldpd_chassis_cleanup(chassis
, 1);
504 lldpd_port_cleanup(port
, 1);
509 #endif /* ENABLE_EDP */