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 chassis
= hardware
->h_lport
.p_chassis
;
56 length
= hardware
->h_mtu
;
57 if ((packet
= (u_int8_t
*)malloc(length
)) == NULL
)
59 memset(packet
, 0, length
);
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 */
73 POKE_SAVE(pos_llc
) && /* We need to save our
75 compute ethernet len */
77 POKE_UINT8(0xaa) && POKE_UINT8(0xaa) &&
81 POKE_BYTES(llcorg
, sizeof(llcorg
)) &&
82 POKE_UINT16(LLC_PID_EDP
)))
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!?");
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
98 POKE_UINT32(0) && /* Len + Checksum */
101 POKE_BYTES(chassis
->c_id
, ETH_ALEN
)))
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
121 POKE_START_EDP_TLV(EDP_TLV_INFO
)))
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) {
129 POKE_UINT16(atoi(hardware
->h_ifname
+
130 strlen(deviceslot
[i
])))))
135 /* If we don't find a "slot", we say that the
136 interface is in slot 8 */
137 if (deviceslot
[i
] == NULL
) {
140 POKE_UINT16(hardware
->h_ifindex
)))
144 POKE_UINT16(0) && /* vchassis */
145 POKE_UINT32(0) && POKE_UINT16(0) && /* Reserved */
147 POKE_BYTES(edp_fakeversion
, sizeof(edp_fakeversion
)) &&
148 /* Connections, we say that we won't
149 have more interfaces than this
151 POKE_UINT32(0xffffffff) &&
152 POKE_UINT32(0) && POKE_UINT32(0) && POKE_UINT32(0) &&
159 TAILQ_FOREACH(vlan
, &hardware
->h_lport
.p_vlans
,
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 */
170 POKE_BYTES(vlan
->v_name
, strlen(vlan
->v_name
)) &&
178 if ((state
== 1) && (v
== 0)) /* No VLAN, no need to send another TLV */
184 POKE_START_EDP_TLV(EDP_TLV_NULL
) &&
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
;
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",
213 hardware
->h_tx_cnt
++;
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); \
228 edp_decode(struct lldpd
*cfg
, char *frame
, int s
,
229 struct lldpd_hardware
*hardware
,
230 struct lldpd_chassis
**newchassis
, struct lldpd_port
**newport
)
232 struct lldpd_chassis
*chassis
;
233 struct lldpd_port
*port
;
235 struct lldpd_mgmt
*mgmt
, *mgmt_next
, *m
;
236 struct lldpd_vlan
*lvlan
= NULL
, *lvlan_next
;
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
;
244 struct in_addr address
;
245 struct lldpd_port
*oport
;
248 if ((chassis
= calloc(1, sizeof(struct lldpd_chassis
))) == NULL
) {
249 LLOG_WARN("failed to allocate remote chassis");
252 TAILQ_INIT(&chassis
->c_mgmt
);
253 if ((port
= calloc(1, sizeof(struct lldpd_port
))) == NULL
) {
254 LLOG_WARN("failed to allocate remote port");
259 TAILQ_INIT(&port
->p_vlans
);
263 pos
= (u_int8_t
*)frame
;
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
);
271 if (PEEK_CMP(edpaddr
, sizeof(edpaddr
)) != 0) {
272 LLOG_INFO("frame not targeted at EDP multicast address received on %s",
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",
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",
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",
299 if (edp_len
> length
+ 10) {
300 LLOG_WARNX("incorrect size for EDP frame received on %s",
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");
311 PEEK_BYTES(chassis
->c_id
, ETH_ALEN
);
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",
320 while (length
&& !gotend
) {
322 LLOG_WARNX("EDP TLV header is too large for "
323 "frame received on %s",
327 if (PEEK_UINT8
!= EDP_TLV_MARKER
) {
328 LLOG_WARNX("incorrect marker starting EDP TLV header for frame "
333 tlv_type
= PEEK_UINT8
;
334 tlv_len
= PEEK_UINT16
- 4;
336 if ((tlv_len
< 0) || (tlv_len
> length
)) {
337 LLOG_DEBUG("incorrect size in EDP TLV header for frame "
340 /* Some poor old Extreme Summit are quite bogus */
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 "
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 "
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");
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 "
380 /* TLV display contains a lot of garbage */
381 PEEK_BYTES(chassis
->c_name
, tlv_len
);
385 LLOG_WARNX("null tlv with incorrect size in frame "
391 LLOG_DEBUG("extra data after edp frame on %s",
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");
403 PEEK_DISCARD_UINT16
; /* Flags + reserved */
404 lvlan
->v_vid
= PEEK_UINT16
; /* VID */
405 PEEK_DISCARD(4); /* Reserved */
406 PEEK_BYTES(&address
, sizeof(address
));
408 if ((lvlan
->v_name
= (char *)calloc(1,
409 tlv_len
+ 1 - 12)) == NULL
) {
410 LLOG_WARN("unable to allocate vlan name");
414 PEEK_BYTES(lvlan
->v_name
, tlv_len
- 12);
416 if (address
.s_addr
!= INADDR_ANY
) {
417 mgmt
= lldpd_alloc_mgmt(LLDPD_AF_IPV4
, &address
,
418 sizeof(struct in_addr
), 0);
420 assert(errno
== ENOMEM
);
421 LLOG_WARN("Out of memory");
424 TAILQ_INSERT_TAIL(&chassis
->c_mgmt
, mgmt
, m_entries
);
426 TAILQ_INSERT_TAIL(&port
->p_vlans
,
432 LLOG_DEBUG("unknown EDP TLV type (%d) received on %s",
433 tlv_type
, hardware
->h_ifname
);
434 hardware
->h_rx_unrecognized_cnt
++;
436 PEEK_DISCARD(tlv
+ tlv_len
- pos
);
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
) ||
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)))
456 /* We attach the VLANs to the found port */
457 lldpd_vlan_cleanup(oport
);
458 for (lvlan
= TAILQ_FIRST(&port
->p_vlans
);
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
,
466 /* And the IP addresses */
467 for (mgmt
= TAILQ_FIRST(&chassis
->c_mgmt
);
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;
478 TAILQ_INSERT_TAIL(&oport
->p_chassis
->c_mgmt
,
482 /* We discard the remaining frame */
489 LLOG_WARNX("some mandatory tlv are missing for frame received on %s",
493 *newchassis
= chassis
;
498 lldpd_chassis_cleanup(chassis
, 1);
499 lldpd_port_cleanup(port
, 1);
504 #endif /* ENABLE_EDP */