]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/lldpd.c
Lower log level for problem when getting bridge info
[thirdparty/lldpd.git] / src / lldpd.c
CommitLineData
43c02e7b
VB
1/*
2 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "lldpd.h"
18
19#include <stdio.h>
20#include <unistd.h>
21#include <errno.h>
22#include <signal.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <fnmatch.h>
26#include <time.h>
a2993d83 27#include <libgen.h>
43c02e7b
VB
28#include <sys/utsname.h>
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/select.h>
32#include <sys/time.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
43c02e7b
VB
35#include <ifaddrs.h>
36#include <net/if_arp.h>
37#include <linux/filter.h>
38#include <linux/if_vlan.h>
4de7bd54 39#include <linux/if_packet.h>
43c02e7b 40#include <linux/sockios.h>
43c02e7b
VB
41
42#ifdef USE_SNMP
43#include <net-snmp/net-snmp-config.h>
44#include <net-snmp/net-snmp-includes.h>
45#include <net-snmp/agent/net-snmp-agent-includes.h>
46#include <net-snmp/agent/snmp_vars.h>
47#endif /* USE_SNMP */
48
8888d191 49static void usage(void);
43c02e7b 50
8888d191
VB
51static int lldpd_iface_init(struct lldpd *, struct lldpd_hardware *);
52static int lldpd_iface_init_vlan(struct lldpd *, struct lldpd_vif *);
53static void lldpd_iface_init_mtu(struct lldpd *, struct lldpd_hardware *);
54static int lldpd_iface_close(struct lldpd *, struct lldpd_hardware *);
55static void lldpd_iface_multicast(struct lldpd *, const char *, int);
43c02e7b
VB
56
57/* "ether proto 0x88cc and ether dst 01:80:c2:00:00:0e" */
58#define LLDPD_FILTER_LLDP_F \
59 { 0x28, 0, 0, 0x0000000c }, \
60 { 0x15, 0, 5, 0x000088cc }, \
61 { 0x20, 0, 0, 0x00000002 }, \
62 { 0x15, 0, 3, 0xc200000e }, \
63 { 0x28, 0, 0, 0x00000000 }, \
64 { 0x15, 0, 1, 0x00000180 }, \
65 { 0x6, 0, 0, 0x0000ffff }, \
66 { 0x6, 0, 0, 0x00000000 },
8888d191 67static struct sock_filter lldpd_filter_lldp_f[] = { LLDPD_FILTER_LLDP_F };
4bad1937 68#ifdef ENABLE_FDP
031118c4
VB
69/* "ether dst 01:e0:52:cc:cc:cc" */
70#define LLDPD_FILTER_FDP_F \
71 { 0x20, 0, 0, 0x00000002 }, \
72 { 0x15, 0, 3, 0x52cccccc }, \
73 { 0x28, 0, 0, 0x00000000 }, \
74 { 0x15, 0, 1, 0x000001e0 }, \
75 { 0x6, 0, 0, 0x0000ffff }, \
76 { 0x6, 0, 0, 0x00000000 },
8888d191 77static struct sock_filter lldpd_filter_fdp_f[] = { LLDPD_FILTER_FDP_F };
4bad1937
VB
78#endif /* ENABLE_FDP */
79#ifdef ENABLE_CDP
348f6df6 80/* "ether dst 01:00:0c:cc:cc:cc" */
43c02e7b 81#define LLDPD_FILTER_CDP_F \
348f6df6
VB
82 { 0x20, 0, 0, 0x00000002 }, \
83 { 0x15, 0, 3, 0x0ccccccc }, \
84 { 0x28, 0, 0, 0x00000000 }, \
85 { 0x15, 0, 1, 0x00000100 }, \
86 { 0x6, 0, 0, 0x0000ffff }, \
87 { 0x6, 0, 0, 0x00000000 },
8888d191 88static struct sock_filter lldpd_filter_cdp_f[] = { LLDPD_FILTER_CDP_F };
4bad1937
VB
89#endif /* ENABLE_CDP */
90#ifdef ENABLE_SONMP
43c02e7b
VB
91/* "ether dst 01:00:81:00:01:00" */
92#define LLDPD_FILTER_SONMP_F \
93 { 0x20, 0, 0, 0x00000002 }, \
94 { 0x15, 0, 3, 0x81000100 }, \
95 { 0x28, 0, 0, 0x00000000 }, \
96 { 0x15, 0, 1, 0x00000100 }, \
97 { 0x6, 0, 0, 0x0000ffff }, \
98 { 0x6, 0, 0, 0x00000000 },
8888d191 99static struct sock_filter lldpd_filter_sonmp_f[] = { LLDPD_FILTER_SONMP_F };
4bad1937
VB
100#endif /* ENABLE_SONMP */
101#ifdef ENABLE_EDP
43c02e7b
VB
102/* "ether dst 00:e0:2b:00:00:00" */
103#define LLDPD_FILTER_EDP_F \
104 { 0x20, 0, 0, 0x00000002 }, \
105 { 0x15, 0, 3, 0x2b000000 }, \
106 { 0x28, 0, 0, 0x00000000 }, \
107 { 0x15, 0, 1, 0x000000e0 }, \
108 { 0x6, 0, 0, 0x0000ffff }, \
109 { 0x6, 0, 0, 0x00000000 },
8888d191 110static struct sock_filter lldpd_filter_edp_f[] = { LLDPD_FILTER_EDP_F };
d2012fb3 111#endif /* ENABLE_EDP */
43c02e7b
VB
112#define LLDPD_FILTER_ANY_F \
113 { 0x28, 0, 0, 0x0000000c }, \
114 { 0x15, 0, 4, 0x000088cc }, \
115 { 0x20, 0, 0, 0x00000002 }, \
116 { 0x15, 0, 2, 0xc200000e }, \
117 { 0x28, 0, 0, 0x00000000 }, \
031118c4 118 { 0x15, 11, 12, 0x00000180 }, \
43c02e7b
VB
119 { 0x20, 0, 0, 0x00000002 }, \
120 { 0x15, 0, 2, 0x2b000000 }, \
121 { 0x28, 0, 0, 0x00000000 }, \
031118c4 122 { 0x15, 7, 8, 0x000000e0 }, \
43c02e7b 123 { 0x15, 1, 0, 0x0ccccccc }, \
031118c4
VB
124 { 0x15, 0, 2, 0x81000100 }, \
125 { 0x28, 0, 0, 0x00000000 }, \
126 { 0x15, 3, 4, 0x00000100 }, \
127 { 0x15, 0, 3, 0x52cccccc }, \
43c02e7b 128 { 0x28, 0, 0, 0x00000000 }, \
031118c4 129 { 0x15, 0, 1, 0x000001e0 }, \
43c02e7b
VB
130 { 0x6, 0, 0, 0x0000ffff }, \
131 { 0x6, 0, 0, 0x00000000 },
8888d191 132static struct sock_filter lldpd_filter_any_f[] = { LLDPD_FILTER_ANY_F };
43c02e7b 133
8888d191 134static struct protocol protos[] =
43c02e7b
VB
135{
136 { LLDPD_MODE_LLDP, 1, "LLDP", ' ', lldp_send, lldp_decode, NULL,
137 LLDP_MULTICAST_ADDR, lldpd_filter_lldp_f, sizeof(lldpd_filter_lldp_f) },
4bad1937 138#ifdef ENABLE_CDP
43c02e7b
VB
139 { LLDPD_MODE_CDPV1, 0, "CDPv1", 'c', cdpv1_send, cdp_decode, cdpv1_guess,
140 CDP_MULTICAST_ADDR, lldpd_filter_cdp_f, sizeof(lldpd_filter_cdp_f) },
141 { LLDPD_MODE_CDPV2, 0, "CDPv2", 'c', cdpv2_send, cdp_decode, cdpv2_guess,
142 CDP_MULTICAST_ADDR, lldpd_filter_cdp_f, sizeof(lldpd_filter_cdp_f) },
4bad1937
VB
143#endif
144#ifdef ENABLE_SONMP
43c02e7b
VB
145 { LLDPD_MODE_SONMP, 0, "SONMP", 's', sonmp_send, sonmp_decode, NULL,
146 SONMP_MULTICAST_ADDR, lldpd_filter_sonmp_f, sizeof(lldpd_filter_sonmp_f) },
4bad1937
VB
147#endif
148#ifdef ENABLE_EDP
43c02e7b
VB
149 { LLDPD_MODE_EDP, 0, "EDP", 'e', edp_send, edp_decode, NULL,
150 EDP_MULTICAST_ADDR, lldpd_filter_edp_f, sizeof(lldpd_filter_edp_f) },
4bad1937
VB
151#endif
152#ifdef ENABLE_FDP
031118c4
VB
153 { LLDPD_MODE_FDP, 0, "FDP", 'f', fdp_send, cdp_decode, NULL,
154 FDP_MULTICAST_ADDR, lldpd_filter_fdp_f, sizeof(lldpd_filter_fdp_f) },
4bad1937 155#endif
43c02e7b
VB
156 { 0, 0, "any", ' ', NULL, NULL, NULL,
157 {0,0,0,0,0,0}, lldpd_filter_any_f, sizeof(lldpd_filter_any_f) }
158};
159
8888d191 160static int lldpd_iface_switchto(struct lldpd *, short int,
43c02e7b 161 struct lldpd_hardware *);
8888d191 162static
43c02e7b 163struct lldpd_hardware *lldpd_port_add(struct lldpd *, struct ifaddrs *);
8888d191
VB
164static void lldpd_loop(struct lldpd *);
165static void lldpd_shutdown(int);
166static void lldpd_exit();
167static void lldpd_send_all(struct lldpd *);
168static void lldpd_recv_all(struct lldpd *);
169static int lldpd_guess_type(struct lldpd *, char *, int);
170static void lldpd_decode(struct lldpd *, char *, int,
43c02e7b 171 struct lldpd_hardware *, int);
89840df0 172#ifdef ENABLE_LLDPMED
8888d191 173static void lldpd_med(struct lldpd_chassis *);
89840df0 174#endif
43c02e7b 175
8888d191 176static char **saved_argv;
43c02e7b 177
8888d191 178static void
43c02e7b
VB
179usage(void)
180{
181 extern const char *__progname;
e809a587
VB
182 fprintf(stderr, "usage: %s [options]\n", __progname);
183 fprintf(stderr, "see manual page lldpd(8) for more information\n");
43c02e7b
VB
184 exit(1);
185}
186
8888d191 187static void
50a89ca7 188lldpd_iface_init_mtu(struct lldpd *global, struct lldpd_hardware *hardware)
43c02e7b 189{
43c02e7b 190 struct ifreq ifr;
43c02e7b
VB
191
192 /* get MTU */
193 memset(&ifr, 0, sizeof(ifr));
194 strlcpy(ifr.ifr_name, hardware->h_ifname, sizeof(ifr.ifr_name));
195 if (ioctl(global->g_sock, SIOCGIFMTU, (char*)&ifr) == -1) {
196 LLOG_WARN("unable to get MTU of %s, using 1500", hardware->h_ifname);
197 hardware->h_mtu = 1500;
198 } else
548109b2 199 hardware->h_mtu = hardware->h_lport.p_mfs = ifr.ifr_mtu;
50a89ca7
VB
200}
201
8888d191 202static int
50a89ca7
VB
203lldpd_iface_init_vlan(struct lldpd *global, struct lldpd_vif *vif)
204{
205 int status;
206 short int filter;
207
208 lldpd_iface_init_mtu(global, (struct lldpd_hardware*)vif);
a7502371 209 status = priv_iface_init((struct lldpd_hardware*)vif, -1);
50a89ca7
VB
210 if (status != 0)
211 return status;
212
213 if (global->g_multi)
214 filter = LLDPD_MODE_ANY;
215 else
216 filter = LLDPD_MODE_LLDP;
217
218 if (lldpd_iface_switchto(global, filter,
219 (struct lldpd_hardware*)vif) == -1) {
220 LLOG_WARNX("unable to apply filter");
221 return ENETDOWN;
222 }
223
224 lldpd_iface_multicast(global, vif->vif_ifname, 0);
225
226 LLOG_DEBUG("vlan interface %s initialized (fd=%d)", vif->vif_ifname,
227 vif->vif_raw);
228 return 0;
229}
230
8888d191 231static int
50a89ca7
VB
232lldpd_iface_init(struct lldpd *global, struct lldpd_hardware *hardware)
233{
50a89ca7
VB
234 int master; /* Bond device */
235 char if_bond[IFNAMSIZ];
50a89ca7
VB
236 int status;
237 short int filter;
238
239 lldpd_iface_init_mtu(global, hardware);
a7502371 240 status = priv_iface_init(hardware, -1);
50a89ca7
VB
241 if (status != 0)
242 return status;
243
43c02e7b
VB
244 if ((master = iface_is_enslaved(global, hardware->h_ifname)) != -1) {
245 /* With bonding device, we need to listen on the bond ! */
246 if (if_indextoname(master, if_bond) == NULL) {
247 LLOG_WARN("unable to get index for interface %d (master of %s)",
248 master, hardware->h_ifname);
249 return ENETDOWN;
250 }
251 hardware->h_raw_real = hardware->h_raw;
252 hardware->h_master = master;
253 hardware->h_raw = -1;
a7502371
VB
254 status = priv_iface_init(hardware, master);
255 if (status != 0) {
256 close(hardware->h_raw_real);
257 if (hardware->h_raw != -1)
258 close(hardware->h_raw);
259 return status;
260 }
43c02e7b
VB
261 }
262
263 if (global->g_multi)
264 filter = LLDPD_MODE_ANY;
265 else
266 filter = LLDPD_MODE_LLDP;
267 if (lldpd_iface_switchto(global, filter, hardware) == -1) {
268 LLOG_WARNX("unable to apply filter");
269 return ENETDOWN;
270 }
271
272 if (master != -1)
273 lldpd_iface_multicast(global, if_bond, 0);
274 lldpd_iface_multicast(global, hardware->h_ifname, 0);
275
276 LLOG_DEBUG("interface %s initialized (fd=%d)", hardware->h_ifname,
277 hardware->h_raw);
278 return 0;
279}
280
8888d191 281static void
43c02e7b
VB
282lldpd_iface_multicast(struct lldpd *global, const char *name, int remove)
283{
a7502371 284 int i, rc;
509861fe 285
43c02e7b
VB
286 for (i=0; global->g_protocols[i].mode != 0; i++) {
287 if (!global->g_protocols[i].enabled) continue;
a7502371
VB
288 if ((rc = priv_iface_multicast(name,
289 global->g_protocols[i].mac, !remove)) != 0) {
290 errno = rc;
291 if (errno != ENOENT)
292 LLOG_INFO("unable to %s %s address to multicast filter for %s",
293 (remove)?"delete":"add",
294 global->g_protocols[i].name,
295 name);
43c02e7b
VB
296 }
297 }
298}
299
8888d191 300static int
43c02e7b
VB
301lldpd_iface_close(struct lldpd *global, struct lldpd_hardware *hardware)
302{
303 char listen[IFNAMSIZ];
304
305 close(hardware->h_raw);
306 hardware->h_raw = -1;
307
308 if (hardware->h_raw_real > 0) {
309 if (if_indextoname(hardware->h_master, listen) == NULL) {
310 LLOG_WARN("unable to get index for interface %d",
311 hardware->h_master);
312 strlcpy(listen, hardware->h_ifname, sizeof(listen));
313 }
314 close(hardware->h_raw_real);
315 lldpd_iface_multicast(global, listen, 1);
316 }
317 strlcpy(listen, hardware->h_ifname, sizeof(listen));
318 lldpd_iface_multicast(global, listen, 1);
319
320 hardware->h_raw_real = -1;
321 return 0;
322}
323
8888d191 324static int
43c02e7b
VB
325lldpd_iface_switchto(struct lldpd *cfg, short int filter, struct lldpd_hardware *hardware)
326{
327 struct sock_fprog prog;
328 int i;
329
330 memset(&prog, 0, sizeof(prog));
331 for (i=0; cfg->g_protocols[i].mode != 0; i++) {
332 if (!cfg->g_protocols[i].enabled) continue;
333 if (cfg->g_protocols[i].mode == filter)
334 break;
335 }
336 prog.filter = cfg->g_protocols[i].filter;
337 prog.len = cfg->g_protocols[i].filterlen / sizeof(struct sock_filter);
338 if (setsockopt(hardware->h_raw, SOL_SOCKET, SO_ATTACH_FILTER,
339 &prog, sizeof(prog)) < 0) {
340 LLOG_WARN("unable to change filter for %s", hardware->h_ifname);
341 return -1;
342 }
343 if ((hardware->h_raw_real > 0) &&
344 (setsockopt(hardware->h_raw_real, SOL_SOCKET, SO_ATTACH_FILTER,
345 &prog, sizeof(prog)) < 0)) {
346 LLOG_WARN("unable to change filter for real device %s", hardware->h_ifname);
347 return -1;
348 }
349 return 0;
350}
351
a1347cd8 352#ifdef ENABLE_DOT1
43c02e7b
VB
353void
354lldpd_vlan_cleanup(struct lldpd_port *port)
355{
356 struct lldpd_vlan *vlan, *vlan_next;
357 for (vlan = TAILQ_FIRST(&port->p_vlans);
358 vlan != NULL;
359 vlan = vlan_next) {
360 free(vlan->v_name);
361 vlan_next = TAILQ_NEXT(vlan, v_entries);
362 TAILQ_REMOVE(&port->p_vlans, vlan, v_entries);
363 free(vlan);
364 }
365}
a1347cd8 366#endif
43c02e7b
VB
367
368void
369lldpd_port_cleanup(struct lldpd_port *port)
370{
740593ff
VB
371#ifdef ENABLE_LLDPMED
372 int i;
373 for (i=0; i < LLDPMED_LOCFORMAT_LAST; i++)
374 free(port->p_med_location[i].data);
375#endif
a1347cd8 376#ifdef ENABLE_DOT1
43c02e7b 377 lldpd_vlan_cleanup(port);
a1347cd8 378#endif
43c02e7b
VB
379 free(port->p_id);
380 free(port->p_descr);
381 free(port);
382}
383
384void
385lldpd_chassis_cleanup(struct lldpd_chassis *chassis)
386{
89840df0
VB
387#ifdef ENABLE_LLDPMED
388 free(chassis->c_med_hw);
389 free(chassis->c_med_fw);
390 free(chassis->c_med_sn);
391 free(chassis->c_med_manuf);
392 free(chassis->c_med_model);
393 free(chassis->c_med_asset);
394#endif
43c02e7b
VB
395 free(chassis->c_id);
396 free(chassis->c_name);
397 free(chassis->c_descr);
398 free(chassis);
399}
400
401void
402lldpd_remote_cleanup(struct lldpd *cfg, struct lldpd_hardware *hardware, int reset)
403{
404 if (hardware->h_rport != NULL) {
405 lldpd_port_cleanup(hardware->h_rport);
406 hardware->h_rport = NULL;
407 }
408 if (hardware->h_rchassis != NULL) {
409 lldpd_chassis_cleanup(hardware->h_rchassis);
410 hardware->h_rchassis = NULL;
411 }
412 hardware->h_rlastchange = hardware->h_rlastupdate = 0;
413 free(hardware->h_rlastframe);
414 hardware->h_rlastframe = NULL;
415 if (reset && cfg->g_multi) {
416 hardware->h_mode = LLDPD_MODE_ANY;
417 memset(hardware->h_proto_macs, 0, ETH_ALEN*(cfg->g_multi+1));
418 hardware->h_start_probe = 0;
419 lldpd_iface_switchto(cfg, LLDPD_MODE_ANY, hardware);
420 }
421}
422
d9be8ea0
VB
423void
424lldpd_hardware_cleanup(struct lldpd_hardware *hardware)
425{
426#ifdef ENABLE_DOT1
427 lldpd_vlan_cleanup(&hardware->h_lport);
428#endif
429 free(hardware->h_proto_macs);
430 free(hardware->h_llastframe);
431 free(hardware);
432}
433
43c02e7b
VB
434void
435lldpd_cleanup(struct lldpd *cfg)
436{
437 struct lldpd_hardware *hardware, *hardware_next;
50a89ca7 438 struct lldpd_vif *vif, *vif_next;
43c02e7b
VB
439
440 for (hardware = TAILQ_FIRST(&cfg->g_hardware); hardware != NULL;
441 hardware = hardware_next) {
442 hardware_next = TAILQ_NEXT(hardware, h_entries);
443 if (hardware->h_flags == 0) {
444 TAILQ_REMOVE(&cfg->g_hardware, hardware, h_entries);
445 lldpd_iface_close(cfg, hardware);
43c02e7b 446 lldpd_remote_cleanup(cfg, hardware, 1);
d9be8ea0 447 lldpd_hardware_cleanup(hardware);
43c02e7b
VB
448 } else if (hardware->h_rchassis != NULL) {
449 if (time(NULL) - hardware->h_rlastupdate >
450 hardware->h_rchassis->c_ttl) {
451 lldpd_remote_cleanup(cfg, hardware, 1);
452 hardware->h_rx_ageout_cnt++;
453 }
454 }
455 }
50a89ca7
VB
456 for (vif = TAILQ_FIRST(&cfg->g_vif); vif != NULL;
457 vif = vif_next) {
458 vif_next = TAILQ_NEXT(vif, vif_entries);
459 if (vif->vif_flags == 0) {
460 TAILQ_REMOVE(&cfg->g_vif, vif, vif_entries);
461 lldpd_iface_close(cfg, (struct lldpd_hardware*)vif);
462 free(vif);
463 }
464 }
465}
466
8888d191 467static struct lldpd_vif *
50a89ca7
VB
468lldpd_port_add_vlan(struct lldpd *cfg, struct ifaddrs *ifa)
469{
470 struct lldpd_vif *vif;
471 struct lldpd_hardware *hardware;
472 struct vlan_ioctl_args ifv;
473
474 TAILQ_FOREACH(vif, &cfg->g_vif, vif_entries) {
475 if (strcmp(vif->vif_ifname, ifa->ifa_name) == 0)
476 break;
477 }
478
479 if (vif == NULL) {
480 if ((vif = (struct lldpd_vif *)
481 calloc(1, sizeof(struct lldpd_vif))) == NULL)
482 return NULL;
483 vif->vif_raw = -1;
484 vif->vif_raw_real = -1;
485 }
486 strlcpy(vif->vif_ifname, ifa->ifa_name, sizeof(vif->vif_ifname));
487 vif->vif_flags = ifa->ifa_flags;
488
489 if (vif->vif_raw == -1) {
490
491 if (lldpd_iface_init_vlan(cfg, vif) != 0) {
492 free(vif);
493 return NULL;
494 }
b5562b23 495
50a89ca7
VB
496 /* Find the real interface */
497 vif->vif_real = NULL;
498 TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
499 memset(&ifv, 0, sizeof(ifv));
500 ifv.cmd = GET_VLAN_REALDEV_NAME_CMD;
501 strlcpy(ifv.device1, ifa->ifa_name, sizeof(ifv.device1));
502 if ((ioctl(cfg->g_sock, SIOCGIFVLAN, &ifv) >= 0) &&
503 (strncmp(hardware->h_ifname,
504 ifv.u.device2,
505 sizeof(ifv.u.device2)) == 0))
506 vif->vif_real = hardware;
507 }
508 if (vif->vif_real == NULL) {
509 LLOG_WARNX("unable to find real interface for %s",
510 ifa->ifa_name);
511 free(vif);
512 return NULL;
513 }
514
515 TAILQ_INSERT_TAIL(&cfg->g_vif, vif, vif_entries);
516 }
517
518 return vif;
43c02e7b
VB
519}
520
8888d191 521static struct lldpd_hardware *
43c02e7b
VB
522lldpd_port_add(struct lldpd *cfg, struct ifaddrs *ifa)
523{
a1347cd8 524#if defined (ENABLE_DOT1) || defined (ENABLE_DOT3)
43c02e7b 525 struct ifaddrs *oifap, *oifa;
a1347cd8 526#endif
43c02e7b
VB
527 struct lldpd_hardware *hardware;
528 struct lldpd_port *port;
a1347cd8 529#ifdef ENABLE_DOT1
43c02e7b 530 struct lldpd_vlan *vlan;
43c02e7b 531 struct vlan_ioctl_args ifv;
a1347cd8
VB
532#endif
533#ifdef ENABLE_DOT3
43c02e7b 534 struct ethtool_cmd ethc;
a1347cd8 535#endif
43c02e7b
VB
536 u_int8_t *lladdr;
537
538 TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
539 if (strcmp(hardware->h_ifname, ifa->ifa_name) == 0)
540 break;
541 }
542
543 if (hardware == NULL) {
544 if ((hardware = (struct lldpd_hardware *)
545 calloc(1, sizeof(struct lldpd_hardware))) == NULL)
546 return (NULL);
547 hardware->h_raw = -1;
548 hardware->h_raw_real = -1;
549 hardware->h_start_probe = 0;
550 hardware->h_proto_macs = (u_int8_t*)calloc(cfg->g_multi+1, ETH_ALEN);
740593ff
VB
551#ifdef ENABLE_LLDPMED
552 hardware->h_lport.p_med_cap_enabled = LLDPMED_CAP_CAP;
553 if (!cfg->g_noinventory)
554 hardware->h_lport.p_med_cap_enabled |= LLDPMED_CAP_IV;
555#endif
a1347cd8 556#ifdef ENABLE_DOT1
43c02e7b
VB
557 TAILQ_INIT(&hardware->h_lport.p_vlans);
558 } else {
559 lldpd_vlan_cleanup(&hardware->h_lport);
a1347cd8 560#endif
43c02e7b
VB
561 }
562
563 port = &hardware->h_lport;
564 hardware->h_flags = ifa->ifa_flags;
565
566 strlcpy(hardware->h_ifname, ifa->ifa_name, sizeof(hardware->h_ifname));
567 lladdr = (u_int8_t*)(((struct sockaddr_ll *)ifa->ifa_addr)->sll_addr);
568 memcpy(&hardware->h_lladdr, lladdr, sizeof(hardware->h_lladdr));
1d291522 569 iface_get_permanent_mac(cfg, hardware);
43c02e7b 570 port->p_id_subtype = LLDP_PORTID_SUBTYPE_LLADDR;
4395f9c1
VB
571 if ((port->p_id = calloc(1, sizeof(hardware->h_lladdr))) == NULL)
572 fatal(NULL);
573 memcpy(port->p_id, hardware->h_lladdr, sizeof(hardware->h_lladdr));
43c02e7b
VB
574 port->p_id_len = sizeof(hardware->h_lladdr);
575 port->p_descr = hardware->h_ifname;
576
577 if (cfg->g_lchassis.c_id == NULL) {
578 /* Use the first port's l2 addr as the chassis ID */
579 if ((cfg->g_lchassis.c_id = malloc(sizeof(hardware->h_lladdr))) == NULL)
580 fatal(NULL);
581 cfg->g_lchassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
582 cfg->g_lchassis.c_id_len = sizeof(hardware->h_lladdr);
583 memcpy(cfg->g_lchassis.c_id,
584 hardware->h_lladdr, sizeof(hardware->h_lladdr));
585 }
586
587 /* Get VLANS and aggregation status */
a1347cd8 588#if defined (ENABLE_DOT3) || defined (ENABLE_DOT1)
43c02e7b
VB
589 if (getifaddrs(&oifap) != 0)
590 fatal("lldpd_port_add: failed to get interface list");
591 for (oifa = oifap; oifa != NULL; oifa = oifa->ifa_next) {
a1347cd8 592#ifdef ENABLE_DOT1
43c02e7b
VB
593 /* Check if we already have checked this one */
594 int skip = 0;
595 TAILQ_FOREACH(vlan, &port->p_vlans, v_entries) {
596 if (strcmp(vlan->v_name, oifa->ifa_name) == 0)
597 skip = 1;
598 }
599 if (skip) continue;
a1347cd8 600#endif
43c02e7b
VB
601
602 /* Aggregation check */
a1347cd8 603#ifdef ENABLE_DOT3
beeaefa3 604 if (iface_is_bond_slave(cfg, hardware->h_ifname, oifa->ifa_name, NULL))
43c02e7b 605 port->p_aggregid = if_nametoindex(oifa->ifa_name);
a1347cd8
VB
606#endif
607
608#ifdef ENABLE_DOT1
43c02e7b
VB
609 /* VLAN check */
610 memset(&ifv, 0, sizeof(ifv));
611 ifv.cmd = GET_VLAN_REALDEV_NAME_CMD;
612 strlcpy(ifv.device1, oifa->ifa_name, sizeof(ifv.device1));
613 if ((ioctl(cfg->g_sock, SIOCGIFVLAN, &ifv) >= 0) &&
beeaefa3 614 ((iface_is_bond_slave(cfg, hardware->h_ifname, ifv.u.device2, NULL)) ||
43c02e7b
VB
615 (strncmp(hardware->h_ifname, ifv.u.device2, sizeof(ifv.u.device2)) == 0))) {
616 if ((vlan = (struct lldpd_vlan *)
617 calloc(1, sizeof(struct lldpd_vlan))) == NULL)
618 continue;
249644a4 619 if ((vlan->v_name = strdup(oifa->ifa_name)) == NULL) {
43c02e7b
VB
620 free(vlan);
621 continue;
622 }
623 memset(&ifv, 0, sizeof(ifv));
624 ifv.cmd = GET_VLAN_VID_CMD;
625 strlcpy(ifv.device1, oifa->ifa_name, sizeof(ifv.device1));
626 if (ioctl(cfg->g_sock, SIOCGIFVLAN, &ifv) < 0) {
627 /* Dunno what happened */
628 free(vlan->v_name);
629 free(vlan);
630 } else {
631 vlan->v_vid = ifv.u.VID;
632 TAILQ_INSERT_TAIL(&port->p_vlans, vlan, v_entries);
633 }
634 }
a1347cd8 635#endif
43c02e7b
VB
636 }
637 freeifaddrs(oifap);
a1347cd8 638#endif
43c02e7b 639
a1347cd8 640#ifdef ENABLE_DOT3
43c02e7b 641 /* MAC/PHY */
4afe659e 642 if (priv_ethtool(hardware->h_ifname, &ethc) == 0) {
43c02e7b
VB
643 int j;
644 int advertised_ethtool_to_rfc3636[][2] = {
645 {ADVERTISED_10baseT_Half, LLDP_DOT3_LINK_AUTONEG_10BASE_T},
646 {ADVERTISED_10baseT_Full, LLDP_DOT3_LINK_AUTONEG_10BASET_FD},
647 {ADVERTISED_100baseT_Half, LLDP_DOT3_LINK_AUTONEG_100BASE_TX},
648 {ADVERTISED_100baseT_Full, LLDP_DOT3_LINK_AUTONEG_100BASE_TXFD},
649 {ADVERTISED_1000baseT_Half, LLDP_DOT3_LINK_AUTONEG_1000BASE_T},
650 {ADVERTISED_1000baseT_Full, LLDP_DOT3_LINK_AUTONEG_1000BASE_TFD},
651 {ADVERTISED_10000baseT_Full, LLDP_DOT3_LINK_AUTONEG_OTHER},
652 {ADVERTISED_Pause, LLDP_DOT3_LINK_AUTONEG_FDX_PAUSE},
653 {ADVERTISED_Asym_Pause, LLDP_DOT3_LINK_AUTONEG_FDX_APAUSE},
654 {ADVERTISED_2500baseX_Full, LLDP_DOT3_LINK_AUTONEG_OTHER},
655 {0,0}};
656
657 port->p_autoneg_support = (ethc.supported & SUPPORTED_Autoneg) ? 1 : 0;
658 port->p_autoneg_enabled = (ethc.autoneg == AUTONEG_DISABLE) ? 0 : 1;
659 for (j=0; advertised_ethtool_to_rfc3636[j][0]; j++) {
660 if (ethc.advertising & advertised_ethtool_to_rfc3636[j][0])
661 port->p_autoneg_advertised |= advertised_ethtool_to_rfc3636[j][1];
662 }
663 switch (ethc.speed) {
664 case SPEED_10:
665 port->p_mau_type = (ethc.duplex == DUPLEX_FULL) ? \
666 LLDP_DOT3_MAU_10BASETFD : LLDP_DOT3_MAU_10BASETHD;
667 if (ethc.port == PORT_BNC) port->p_mau_type = LLDP_DOT3_MAU_10BASE2;
668 if (ethc.port == PORT_FIBRE)
669 port->p_mau_type = (ethc.duplex == DUPLEX_FULL) ? \
670 LLDP_DOT3_MAU_10BASEFLDF : LLDP_DOT3_MAU_10BASEFLHD;
671 break;
672 case SPEED_100:
673 port->p_mau_type = (ethc.duplex == DUPLEX_FULL) ? \
674 LLDP_DOT3_MAU_100BASETXFD : LLDP_DOT3_MAU_100BASETXHD;
675 if (ethc.port == PORT_BNC)
676 port->p_mau_type = (ethc.duplex == DUPLEX_FULL) ? \
677 LLDP_DOT3_MAU_100BASET2DF : LLDP_DOT3_MAU_100BASET2HD;
678 if (ethc.port == PORT_FIBRE)
679 port->p_mau_type = (ethc.duplex == DUPLEX_FULL) ? \
680 LLDP_DOT3_MAU_100BASEFXFD : LLDP_DOT3_MAU_100BASEFXHD;
681 break;
682 case SPEED_1000:
683 port->p_mau_type = (ethc.duplex == DUPLEX_FULL) ? \
684 LLDP_DOT3_MAU_1000BASETFD : LLDP_DOT3_MAU_1000BASETHD;
685 if (ethc.port == PORT_FIBRE)
686 port->p_mau_type = (ethc.duplex == DUPLEX_FULL) ? \
687 LLDP_DOT3_MAU_1000BASEXFD : LLDP_DOT3_MAU_1000BASEXHD;
688 break;
689 case SPEED_10000:
690 port->p_mau_type = (ethc.port == PORT_FIBRE) ? \
691 LLDP_DOT3_MAU_10GIGBASEX : LLDP_DOT3_MAU_10GIGBASER;
692 break;
693 }
694 if (ethc.port == PORT_AUI) port->p_mau_type = LLDP_DOT3_MAU_AUI;
b5562b23 695 } else
28aca6a0 696 LLOG_DEBUG("unable to get eth info for %s", hardware->h_ifname);
a1347cd8 697#endif
43c02e7b
VB
698
699 if (!INTERFACE_OPENED(hardware)) {
700
701 if (lldpd_iface_init(cfg, hardware) != 0) {
b5562b23 702 LLOG_WARN("unable to initialize %s", hardware->h_ifname);
d9be8ea0 703 lldpd_hardware_cleanup(hardware);
43c02e7b
VB
704 return (NULL);
705 }
706
707 TAILQ_INSERT_TAIL(&cfg->g_hardware, hardware, h_entries);
708 }
709
710 return (hardware);
711}
712
8888d191 713static int
43c02e7b
VB
714lldpd_guess_type(struct lldpd *cfg, char *frame, int s)
715{
716 int i;
717 if (s < ETH_ALEN)
718 return -1;
719 for (i=0; cfg->g_protocols[i].mode != 0; i++) {
720 if (!cfg->g_protocols[i].enabled)
721 continue;
722 if (cfg->g_protocols[i].guess == NULL) {
723 if (memcmp(frame, cfg->g_protocols[i].mac, ETH_ALEN) == 0)
724 return cfg->g_protocols[i].mode;
725 } else {
726 if (cfg->g_protocols[i].guess(frame, s))
727 return cfg->g_protocols[i].mode;
728 }
729 }
730 return -1;
731}
732
8888d191 733static void
43c02e7b
VB
734lldpd_decode(struct lldpd *cfg, char *frame, int s,
735 struct lldpd_hardware *hardware, int bond)
736{
737 int result = 0, i, j, candidatetonull;
738 u_int8_t nullmac[ETH_ALEN] = {0,0,0,0,0,0};
739 u_int8_t broadcastmac[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
740 struct lldpd_chassis *chassis;
741 struct lldpd_port *port;
742 struct lldpd_hardware *ohardware, *firstnull = NULL, *older = NULL;
743 int guess = LLDPD_MODE_LLDP;
744
50a89ca7
VB
745 /* Discard VLAN frames */
746 if ((s >= sizeof(struct ieee8023)) &&
747 (((struct ieee8023*)frame)->size == htons(ETHERTYPE_VLAN)))
748 return;
749
43c02e7b
VB
750 if ((hardware->h_rlastframe != NULL) &&
751 (hardware->h_rlastframe->size == s) &&
752 (memcmp(hardware->h_rlastframe->frame, frame, s) == 0)) {
753 /* Already received the same frame */
754 hardware->h_rlastupdate = time(NULL);
755 return;
756 }
757
758 if (cfg->g_multi) {
759 if (hardware->h_mode == LLDPD_MODE_ANY)
760 guess = lldpd_guess_type(cfg, frame, s);
761 else
762 guess = hardware->h_mode;
763 for (i=0; cfg->g_protocols[i].mode != 0; i++) {
764 if (!cfg->g_protocols[i].enabled)
765 continue;
766 if (cfg->g_protocols[i].mode == guess) {
767 if ((result = cfg->g_protocols[i].decode(cfg, frame,
768 s, hardware, &chassis, &port)) == -1)
769 return;
770 break;
771 }
772 }
773 if (cfg->g_protocols[i].mode == 0) {
774 LLOG_INFO("unable to guess frame type");
775 return;
776 }
777 } else if (cfg->g_protocols[0].decode(cfg, frame, s, hardware,
778 &chassis, &port) == -1)
779 /* Nothing has been received */
780 return;
781
782 if (bond) {
783 /* Eh, wait ! The frame we just received was for a bonding
784 * device. We need to attach it to a real device. What is the
785 * best candidate? Drum rolling... */
786 TAILQ_FOREACH(ohardware, &cfg->g_hardware, h_entries) {
787 if (ohardware->h_master == hardware->h_master) {
788 /* Same bond */
789 if (ohardware->h_rchassis == NULL) {
790 candidatetonull = 1;
791 if (cfg->g_multi &&
792 (ohardware->h_mode == LLDPD_MODE_ANY)) {
793 for (i=j=0;
794 cfg->g_protocols[i].mode != 0;
795 i++) {
796 if (!cfg->g_protocols[i].enabled)
797 continue;
798 if ((cfg->g_protocols[i].mode == guess) &&
799 (memcmp(frame + ETH_ALEN,
800 ohardware->h_proto_macs + ETH_ALEN*j,
801 ETH_ALEN) == 0)) {
802 hardware = ohardware;
803 bond = 0;
804 break;
805 }
806 j++;
807 }
808 if (!bond) break;
809 if (firstnull != NULL) {
810 for (i=j=0;
811 cfg->g_protocols[i].mode != 0;
812 i++) {
813 if (!cfg->g_protocols[i].enabled)
814 continue;
815 if ((cfg->g_protocols[i].mode == guess) &&
816 (memcmp(nullmac,
817 ohardware->h_proto_macs +
818 ETH_ALEN*j,
819 ETH_ALEN) != 0)) {
820 /* We need to
821 * find a better
822 * candidate */
823 candidatetonull = 0;
824 break;
825 }
826 j++;
827 }
828 }
829 }
830 /* Ok, this is the first candidate if we
831 * don't find a matching chassis/port */
832 if (candidatetonull) firstnull = ohardware;
833 continue;
834 }
835 if ((older == NULL) ||
836 (older->h_rlastupdate > ohardware->h_rlastupdate))
837 /* If there is no matching chassis/port
838 * and no free hardware, we will use
839 * this one. */
840 older = ohardware;
841 if ((chassis->c_id_subtype !=
842 ohardware->h_rchassis->c_id_subtype) ||
843 (chassis->c_id_len != ohardware->h_rchassis->c_id_len) ||
844 (memcmp(chassis->c_id, ohardware->h_rchassis->c_id,
845 chassis->c_id_len) != 0) ||
846 (port->p_id_subtype != ohardware->h_rport->p_id_subtype) ||
847 (port->p_id_len != ohardware->h_rport->p_id_len) ||
848 (memcmp(port->p_id, ohardware->h_rport->p_id,
849 port->p_id_len) != 0))
850 continue;
851 /* We got a match! */
852 hardware = ohardware; /* We switch hardware */
853 bond = 0;
854 break;
855 }
856 }
857 if (bond) {
858 /* No match found */
859 if (firstnull != NULL)
860 hardware = firstnull;
861 else hardware = older;
862 }
863 }
864
865 if (cfg->g_multi &&
866 (hardware->h_mode == LLDPD_MODE_ANY)) {
867 u_int8_t *mac;
868 char *modename;
869 int filter;
870
871 for (i=j=0; cfg->g_protocols[i].mode != 0; i++) {
872 if (!cfg->g_protocols[i].enabled)
873 continue;
874 if (cfg->g_protocols[i].mode == guess) {
875 mac = hardware->h_proto_macs + ETH_ALEN*j;
876 modename = cfg->g_protocols[i].name;
877 filter = cfg->g_protocols[i].mode;
878 break;
879 }
880 j++;
881 }
882 if (cfg->g_protocols[i].mode == 0) {
883 LLOG_WARNX("should not be there");
884 goto cleanup;
885 }
886
887 if (hardware->h_start_probe == 0)
888 hardware->h_start_probe = time(NULL) - 1;
889 /* Handle switching respecting probe time */
890 if ((memcmp(mac, frame + ETH_ALEN, ETH_ALEN) == 0) &&
891 ((time(NULL) - hardware->h_start_probe) > cfg->g_probe_time) &&
892 /* Don't switch to this protocol if not LLDP and LLDP is
893 * a valid candidate */
894 ((filter == LLDPD_MODE_LLDP) ||
895 (memcmp(hardware->h_proto_macs,
896 broadcastmac, ETH_ALEN) == 0) ||
897 (memcmp(hardware->h_proto_macs,
898 nullmac, ETH_ALEN) == 0))) {
899 LLOG_INFO("switching to %s on port %s", modename,
900 hardware->h_ifname);
901 hardware->h_mode = guess;
902 lldpd_iface_switchto(cfg, filter, hardware);
903 } else {
904 /* Wait twice probe time to be able to receive packets of all kind */
905 if ((time(NULL) - hardware->h_start_probe) > cfg->g_probe_time * 2) {
906 LLOG_DEBUG("probe expired on %s, retry", hardware->h_ifname);
907 hardware->h_start_probe = 0;
908 memset(hardware->h_proto_macs, 0, ETH_ALEN*(cfg->g_multi+1));
909 goto cleanup;
910 }
911 if (memcmp(mac, broadcastmac, ETH_ALEN) == 0)
912 goto cleanup;
913 LLOG_INFO("received a %s frame on %s but wait for %d sec",
914 modename, hardware->h_ifname, cfg->g_probe_time - time(NULL) +
915 hardware->h_start_probe);
916 if (memcmp(mac, frame + ETH_ALEN, ETH_ALEN) == 0)
917 goto cleanup;
918 if (memcmp(mac, nullmac, ETH_ALEN) == 0) {
919 memcpy(mac, frame + ETH_ALEN, ETH_ALEN);
920 goto cleanup;
921 }
922 LLOG_INFO("several MAC for %s on %s, discarding %s for this interface",
923 modename, hardware->h_ifname, modename);
924 memcpy(mac, broadcastmac, ETH_ALEN);
925 goto cleanup;
926 }
927 }
928
929 result = 0;
930 if ((hardware->h_rchassis == NULL) ||
931 (chassis->c_id_subtype != hardware->h_rchassis->c_id_subtype) ||
932 (chassis->c_id_len != hardware->h_rchassis->c_id_len) ||
933 (memcmp(chassis->c_id, hardware->h_rchassis->c_id,
934 chassis->c_id_len) != 0))
935 result = 1;
936
937 /* We have our new frame */
938 lldpd_remote_cleanup(cfg, hardware, 0);
939 hardware->h_rport = port;
940 hardware->h_rchassis = chassis;
941 hardware->h_rlastchange = hardware->h_rlastupdate = time(NULL);
942
943 /* We remember this frame */
944 free(hardware->h_rlastframe);
945 if ((hardware->h_rlastframe = (struct lldpd_frame *)malloc(s +
946 sizeof(int))) != NULL) {
947 hardware->h_rlastframe->size = s;
948 memcpy(hardware->h_rlastframe->frame, frame, s);
949 }
950
951 if (result) {
952 /* This is a new remote system */
953 LLOG_DEBUG("we discovered a new remote system on %s",
954 hardware->h_ifname);
955 /* Do we already know this remote system? */
956 TAILQ_FOREACH(ohardware, &cfg->g_hardware, h_entries) {
957 if ((ohardware->h_ifname != hardware->h_ifname) &&
958 (ohardware->h_rchassis != NULL) &&
959 (ohardware->h_rchassis->c_id_subtype ==
960 chassis->c_id_subtype) &&
961 (ohardware->h_rchassis->c_id_len ==
962 chassis->c_id_len) &&
963 (memcmp(ohardware->h_rchassis->c_id,
964 chassis->c_id, chassis->c_id_len) == 0)) {
965 LLOG_DEBUG("but it was already on %s",
966 ohardware->h_ifname);
967 hardware->h_rid = ohardware->h_rid;
968 return;
969 }
970 }
971 hardware->h_rid = ++cfg->g_lastrid;
972 }
973 return;
974
975cleanup:
976 lldpd_chassis_cleanup(chassis);
977 lldpd_port_cleanup(port);
978 return;
979}
980
8888d191 981static void
43c02e7b
VB
982lldpd_recv_all(struct lldpd *cfg)
983{
984 struct lldpd_hardware *hardware;
50a89ca7 985 struct lldpd_vif *vif;
43c02e7b
VB
986 struct lldpd_client *client, *client_next;
987 fd_set rfds;
988 struct timeval tv;
989 struct sockaddr_ll from;
990 socklen_t fromlen;
991 int onreal;
992#ifdef USE_SNMP
993 int fakeblock = 0;
994 struct timeval *tvp = &tv;
995#endif
996 int rc, nfds, n, bond;
997 char *buffer;
998
999 do {
1000 tv.tv_sec = cfg->g_delay - (time(NULL) - cfg->g_lastsent);
1001 if (tv.tv_sec < 0)
1002 tv.tv_sec = LLDPD_TX_DELAY;
1003 if (tv.tv_sec >= cfg->g_delay)
1004 tv.tv_sec = cfg->g_delay;
1005 tv.tv_usec = 0;
1006
1007 FD_ZERO(&rfds);
1008 nfds = -1;
1009
1010 TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
1011 /* Ignore if interface is down */
e70b76f9
VB
1012 if (((hardware->h_flags & IFF_UP) == 0) ||
1013 ((hardware->h_flags & IFF_RUNNING) == 0))
43c02e7b
VB
1014 continue;
1015 FD_SET(hardware->h_raw, &rfds);
1016 if (nfds < hardware->h_raw)
1017 nfds = hardware->h_raw;
1018 /* Listen to real interface too. In 2.6.27, we can
1019 * receive packets if this is the slave interface. */
1020 if (hardware->h_raw_real > 0) {
1021 FD_SET(hardware->h_raw_real, &rfds);
1022 if (nfds < hardware->h_raw_real)
1023 nfds = hardware->h_raw_real;
1024 }
1025 }
50a89ca7 1026 TAILQ_FOREACH(vif, &cfg->g_vif, vif_entries) {
e70b76f9
VB
1027 if (((vif->vif_flags & IFF_UP) == 0) ||
1028 ((vif->vif_flags & IFF_RUNNING) == 0))
50a89ca7
VB
1029 continue;
1030 FD_SET(vif->vif_raw, &rfds);
1031 if (nfds < vif->vif_raw)
1032 nfds = vif->vif_raw;
1033 }
43c02e7b
VB
1034 TAILQ_FOREACH(client, &cfg->g_clients, next) {
1035 FD_SET(client->fd, &rfds);
1036 if (nfds < client->fd)
1037 nfds = client->fd;
1038 }
1039 FD_SET(cfg->g_ctl, &rfds);
1040 if (nfds < cfg->g_ctl)
1041 nfds = cfg->g_ctl;
1042
1043#ifdef USE_SNMP
1044 if (cfg->g_snmp)
1045 snmp_select_info(&nfds, &rfds, tvp, &fakeblock);
1046#endif /* USE_SNMP */
1047 if (nfds == -1) {
1048 sleep(cfg->g_delay);
1049 return;
1050 }
1051
1052 rc = select(nfds + 1, &rfds, NULL, NULL, &tv);
1053 if (rc == -1) {
1054 if (errno == EINTR)
1055 continue;
1056 LLOG_WARN("failure on select");
1057 break;
1058 }
1059#ifdef USE_SNMP
1060 if (cfg->g_snmp) {
1061 if (rc > 0)
1062 snmp_read(&rfds);
1063 else if (rc == 0)
1064 snmp_timeout();
1065 }
1066#endif /* USE_SNMP */
50a89ca7
VB
1067 TAILQ_FOREACH(vif, &cfg->g_vif, vif_entries) {
1068 if (!FD_ISSET(vif->vif_raw, &rfds))
1069 continue;
1070 if ((buffer = (char *)malloc(
1071 vif->vif_mtu)) == NULL) {
1072 LLOG_WARN("failed to alloc reception buffer");
1073 continue;
1074 }
1075 fromlen = sizeof(from);
1076 if ((n = recvfrom(vif->vif_raw,
1077 buffer,
1078 vif->vif_mtu, 0,
1079 (struct sockaddr *)&from,
1080 &fromlen)) == -1) {
1081 LLOG_WARN("error while receiving frame on vlan %s",
1082 vif->vif_ifname);
1083 vif->vif_real->h_rx_discarded_cnt++;
1084 free(buffer);
1085 continue;
1086 }
1087 if (from.sll_pkttype == PACKET_OUTGOING) {
1088 free(buffer);
1089 continue;
1090 }
1091 if (!((cfg->g_multi) &&
1092 (vif->vif_real->h_mode != LLDPD_MODE_ANY) &&
1093 (lldpd_guess_type(cfg, buffer, n) !=
1094 vif->vif_real->h_mode))) {
1095 vif->vif_real->h_rx_cnt++;
1096 lldpd_decode(cfg, buffer, n, vif->vif_real, 0);
1097 }
1098
1099 free(buffer);
1100 }
43c02e7b
VB
1101 TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
1102 /* We could have received something on _real_
1103 * interface. However, even in this case, this could be
1104 * just an outgoing packet. We will try to handle both
1105 * cases, but maybe not in the same select. */
1106 onreal = ((hardware->h_raw_real > 0) &&
1107 (FD_ISSET(hardware->h_raw_real, &rfds)));
1108 if (onreal || (FD_ISSET(hardware->h_raw, &rfds))) {
1109 if ((buffer = (char *)malloc(
1110 hardware->h_mtu)) == NULL) {
1111 LLOG_WARN("failed to alloc reception buffer");
1112 continue;
1113 }
1114 fromlen = sizeof(from);
1115 if ((n = recvfrom(
1116 onreal?hardware->h_raw_real:hardware->h_raw,
1117 buffer,
1118 hardware->h_mtu, 0,
1119 (struct sockaddr *)&from,
1120 &fromlen)) == -1) {
1121 LLOG_WARN("error while receiving frame on %s",
1122 hardware->h_ifname);
1123 hardware->h_rx_discarded_cnt++;
1124 free(buffer);
1125 continue;
1126 }
1127 if (from.sll_pkttype == PACKET_OUTGOING) {
1128 free(buffer);
1129 continue;
1130 }
1131 bond = 0;
1132 /* If received on real interface, we act like if
1133 * this is not a bond! */
1134 if (!onreal && (hardware->h_raw_real > 0)) {
1135 /* Bonding. Is it for the correct
1136 * physical interface ? */
1137 if (from.sll_ifindex == hardware->h_master) {
1138 /* It seems that we don't know from
1139 which physical interface it comes
1140 (kernel < 2.6.24 ?) */
1141 bond = 1;
1142 } else if (from.sll_ifindex !=
1143 if_nametoindex(hardware->h_ifname)) {
1144 free(buffer);
1145 continue;
1146 }
1147 }
1148 hardware->h_rx_cnt++;
1149 lldpd_decode(cfg, buffer, n, hardware, bond);
1150 free(buffer);
1151 }
1152
1153 }
1154 if (FD_ISSET(cfg->g_ctl, &rfds)) {
1155 if (ctl_accept(cfg, cfg->g_ctl) == -1)
1156 LLOG_WARN("unable to accept new client");
1157 }
1158 for (client = TAILQ_FIRST(&cfg->g_clients);
1159 client != NULL;
1160 client = client_next) {
1161 client_next = TAILQ_NEXT(client, next);
1162 if (FD_ISSET(client->fd, &rfds)) {
1163 /* Got a message */
1164 if ((buffer = (char *)malloc(MAX_HMSGSIZE)) ==
1165 NULL) {
1166 LLOG_WARN("failed to alloc reception buffer");
1167 continue;
1168 }
1169 if ((n = recv(client->fd, buffer,
1170 MAX_HMSGSIZE, 0)) == -1) {
1171 LLOG_WARN("error while receiving message");
1172 free(buffer);
1173 continue;
1174 }
1175 if (n > 0)
a552a72e 1176 client_handle_client(cfg, client, buffer, n);
43c02e7b
VB
1177 else
1178 ctl_close(cfg, client->fd); /* Will use TAILQ_REMOVE ! */
1179 free(buffer);
1180 }
1181 }
1182
1183#ifdef USE_SNMP
1184 if (cfg->g_snmp) {
1185 run_alarms();
1186 netsnmp_check_outstanding_agent_requests();
1187 }
1188#endif /* USE_SNMP */
1189 } while ((rc != 0) || (time(NULL) - cfg->g_lastsent < cfg->g_delay));
1190}
1191
8888d191 1192static void
43c02e7b
VB
1193lldpd_send_all(struct lldpd *cfg)
1194{
1195 struct lldpd_hardware *hardware;
f7db0dd8
VB
1196 u_int8_t saved_lladdr[ETHER_ADDR_LEN];
1197 int i, altermac;
1198
43c02e7b
VB
1199 cfg->g_lastsent = time(NULL);
1200 TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
1201 /* Ignore if interface is down */
e70b76f9
VB
1202 if (((hardware->h_flags & IFF_UP) == 0) ||
1203 ((hardware->h_flags & IFF_RUNNING) == 0))
43c02e7b
VB
1204 continue;
1205
a26fc61c 1206 /* When sending on inactive slaves, just send using a 0:0:0:0:0:0 address */
f7db0dd8 1207 altermac = 0;
beeaefa3
VB
1208 if ((hardware->h_raw_real > 0) &&
1209 (!iface_is_slave_active(cfg, hardware->h_master,
1210 hardware->h_ifname))) {
f7db0dd8
VB
1211 altermac = 1;
1212 memcpy(saved_lladdr, hardware->h_lladdr, ETHER_ADDR_LEN);
a26fc61c 1213 memset(hardware->h_lladdr, 0, ETHER_ADDR_LEN);
beeaefa3 1214 }
43c02e7b
VB
1215
1216 for (i=0; cfg->g_protocols[i].mode != 0; i++) {
1217 if (!cfg->g_protocols[i].enabled)
1218 continue;
1219 if ((hardware->h_mode == cfg->g_protocols[i].mode) ||
1220 (cfg->g_protocols[i].mode == LLDPD_MODE_LLDP))
1221 cfg->g_protocols[i].send(cfg, &cfg->g_lchassis, hardware);
1222 }
f7db0dd8
VB
1223 /* Restore MAC if needed */
1224 if (altermac)
1225 memcpy(hardware->h_lladdr, saved_lladdr, ETHER_ADDR_LEN);
43c02e7b
VB
1226 }
1227}
1228
89840df0 1229#ifdef ENABLE_LLDPMED
8888d191 1230static void
89840df0
VB
1231lldpd_med(struct lldpd_chassis *chassis)
1232{
1233 free(chassis->c_med_hw);
1234 free(chassis->c_med_fw);
1235 free(chassis->c_med_sn);
1236 free(chassis->c_med_manuf);
1237 free(chassis->c_med_model);
1238 free(chassis->c_med_asset);
1239 chassis->c_med_hw = dmi_hw();
1240 chassis->c_med_fw = dmi_fw();
1241 chassis->c_med_sn = dmi_sn();
1242 chassis->c_med_manuf = dmi_manuf();
1243 chassis->c_med_model = dmi_model();
1244 chassis->c_med_asset = dmi_asset();
1245}
1246#endif
1247
8888d191 1248static void
43c02e7b
VB
1249lldpd_loop(struct lldpd *cfg)
1250{
1251 struct ifaddrs *ifap, *ifa;
1252 struct sockaddr_ll *sdl;
1253 struct lldpd_hardware *hardware;
50a89ca7 1254 struct lldpd_vif *vif;
43c02e7b
VB
1255 int f;
1256 char status;
1257 struct utsname *un;
b5562b23 1258 char *hp;
43c02e7b
VB
1259
1260 /* Set system name and description */
1261 if ((un = (struct utsname*)malloc(sizeof(struct utsname))) == NULL)
1262 fatal(NULL);
1263 if (uname(un) != 0)
1264 fatal("failed to get system information");
b5562b23 1265 if ((hp = priv_gethostbyname()) == NULL)
43c02e7b
VB
1266 fatal("failed to get system name");
1267 free(cfg->g_lchassis.c_name);
1268 free(cfg->g_lchassis.c_descr);
249644a4
VB
1269 if ((cfg->g_lchassis.c_name = strdup(hp)) == NULL)
1270 fatal(NULL);
43c02e7b
VB
1271 if (asprintf(&cfg->g_lchassis.c_descr, "%s %s %s %s",
1272 un->sysname, un->release, un->version, un->machine) == -1)
1273 fatal("failed to set system description");
43c02e7b
VB
1274
1275 /* Check forwarding */
1276 cfg->g_lchassis.c_cap_enabled = 0;
b5562b23 1277 if ((f = priv_open("/proc/sys/net/ipv4/ip_forward")) >= 0) {
4afe659e 1278 if ((read(f, &status, 1) == 1) && (status == '1')) {
43c02e7b 1279 cfg->g_lchassis.c_cap_enabled = LLDP_CAP_ROUTER;
4afe659e 1280 }
43c02e7b
VB
1281 close(f);
1282 }
89840df0
VB
1283#ifdef ENABLE_LLDPMED
1284 if (cfg->g_lchassis.c_cap_available & LLDP_CAP_TELEPHONE)
1285 cfg->g_lchassis.c_cap_enabled |= LLDP_CAP_TELEPHONE;
1286 lldpd_med(&cfg->g_lchassis);
1287 free(cfg->g_lchassis.c_med_sw);
1288 cfg->g_lchassis.c_med_sw = strdup(un->release);
1289#endif
1290 free(un);
43c02e7b
VB
1291
1292 TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries)
1293 hardware->h_flags = 0;
50a89ca7
VB
1294 TAILQ_FOREACH(vif, &cfg->g_vif, vif_entries)
1295 vif->vif_flags = 0;
43c02e7b
VB
1296
1297 if (getifaddrs(&ifap) != 0)
1298 fatal("lldpd_loop: failed to get interface list");
1299
1300 cfg->g_lchassis.c_mgmt.s_addr = INADDR_ANY;
1301 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
1302 if (cfg->g_lchassis.c_mgmt.s_addr == INADDR_ANY)
1303 /* Get management address, if available */
1304 if ((ifa->ifa_addr != NULL) &&
1305 (ifa->ifa_addr->sa_family == AF_INET)) {
1306 struct sockaddr_in *sa;
1307 sa = (struct sockaddr_in *)ifa->ifa_addr;
1308 if ((ntohl(*(u_int32_t*)&sa->sin_addr) != INADDR_LOOPBACK) &&
1309 (cfg->g_mgmt_pattern == NULL)) {
1310 memcpy(&cfg->g_lchassis.c_mgmt,
1311 &sa->sin_addr,
1312 sizeof(struct in_addr));
1313 cfg->g_lchassis.c_mgmt_if = if_nametoindex(ifa->ifa_name);
1314 }
1315 else if (cfg->g_mgmt_pattern != NULL) {
1316 char *ip;
1317 ip = inet_ntoa(sa->sin_addr);
1318 if (fnmatch(cfg->g_mgmt_pattern,
1319 ip, 0) == 0) {
1320 memcpy(&cfg->g_lchassis.c_mgmt,
1321 &sa->sin_addr,
1322 sizeof(struct in_addr));
1323 cfg->g_lchassis.c_mgmt_if =
1324 if_nametoindex(ifa->ifa_name);
1325 }
1326 }
1327 }
1328
a5e5458f
VB
1329 if (ifa->ifa_addr == NULL ||
1330 ifa->ifa_addr->sa_family != PF_PACKET)
1331 continue;
1332
99f85c00
VB
1333 sdl = (struct sockaddr_ll *)ifa->ifa_addr;
1334 if (sdl->sll_hatype != ARPHRD_ETHER || !sdl->sll_halen)
1335 continue;
1336
43c02e7b
VB
1337 if (iface_is_bridge(cfg, ifa->ifa_name)) {
1338 cfg->g_lchassis.c_cap_enabled |= LLDP_CAP_BRIDGE;
1339 continue;
1340 }
1341
50a89ca7 1342 if ((iface_is_vlan(cfg, ifa->ifa_name)) ||
43c02e7b
VB
1343 (iface_is_bond(cfg, ifa->ifa_name)))
1344 continue;
1345
43c02e7b
VB
1346 if (!(ifa->ifa_flags & IFF_MULTICAST))
1347 continue;
1348
43c02e7b
VB
1349 if (iface_is_wireless(cfg, ifa->ifa_name))
1350 cfg->g_lchassis.c_cap_enabled |= LLDP_CAP_WLAN;
1351
43c02e7b
VB
1352 if (lldpd_port_add(cfg, ifa) == NULL)
1353 LLOG_WARNX("failed to allocate port %s, skip it",
1354 ifa->ifa_name);
1355 }
1356
50a89ca7
VB
1357 /* Handle VLAN */
1358 if (cfg->g_listen_vlans) {
1359 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
1360 if ((iface_is_vlan(cfg, ifa->ifa_name)) &&
1361 (lldpd_port_add_vlan(cfg, ifa) == NULL)) {
1362 LLOG_WARNX("unable to allocate vlan %s, skip it",
1363 ifa->ifa_name);
1364 }
1365 }
1366 }
1367
43c02e7b
VB
1368 freeifaddrs(ifap);
1369
1370 lldpd_cleanup(cfg);
1371
1372 lldpd_send_all(cfg);
1373 lldpd_recv_all(cfg);
1374}
1375
8888d191 1376static void
43c02e7b
VB
1377lldpd_shutdown(int sig)
1378{
1379 LLOG_INFO("signal received, exiting");
1380 exit(0);
1381}
1382
1383/* For signal handling */
8888d191 1384static struct lldpd *gcfg = NULL;
43c02e7b 1385
8888d191 1386static void
43c02e7b
VB
1387lldpd_exit()
1388{
1389 struct lldpd_hardware *hardware;
50a89ca7 1390 struct lldpd_vif *vif;
b5562b23
VB
1391 close(gcfg->g_ctl);
1392 priv_ctl_cleanup();
43c02e7b
VB
1393 TAILQ_FOREACH(hardware, &gcfg->g_hardware, h_entries) {
1394 if (INTERFACE_OPENED(hardware))
1395 lldpd_iface_close(gcfg, hardware);
1396 }
50a89ca7
VB
1397 TAILQ_FOREACH(vif, &gcfg->g_vif, vif_entries) {
1398 if (vif->vif_raw != -1)
1399 lldpd_iface_close(gcfg, (struct lldpd_hardware*)vif);
1400 }
43c02e7b
VB
1401#ifdef USE_SNMP
1402 if (gcfg->g_snmp)
1403 agent_shutdown();
1404#endif /* USE_SNMP */
1405}
1406
1407int
1408main(int argc, char *argv[])
1409{
1410 struct lldpd *cfg;
e809a587
VB
1411 int ch, debug = 0;
1412#ifdef USE_SNMP
1413 int snmp = 0;
1414#endif
43c02e7b 1415 char *mgmtp = NULL;
740593ff 1416 char *popt, opts[] = "vdxm:p:M:i@ ";
766f32b3 1417 int probe = 0, i, found, vlan = 0;
89840df0 1418#ifdef ENABLE_LLDPMED
e809a587 1419 int lldpmed = 0, noinventory = 0;
89840df0 1420#endif
43c02e7b
VB
1421
1422 saved_argv = argv;
1423
1424 /*
1425 * Get and parse command line options
1426 */
1427 popt = index(opts, '@');
1428 for (i=0; protos[i].mode != 0; i++) {
1429 if (protos[i].enabled == 1) continue;
1430 *(popt++) = protos[i].arg;
1431 }
1432 *popt = '\0';
1433 while ((ch = getopt(argc, argv, opts)) != -1) {
1434 switch (ch) {
766f32b3
VB
1435 case 'v':
1436 vlan = 1;
1437 break;
43c02e7b
VB
1438 case 'd':
1439 debug++;
1440 break;
1441 case 'm':
1442 mgmtp = optarg;
1443 break;
e809a587 1444#ifdef ENABLE_LLDPMED
115ff55c 1445 case 'M':
89840df0 1446 lldpmed = atoi(optarg);
e809a587
VB
1447 if ((lldpmed < 1) || (lldpmed > 4)) {
1448 fprintf(stderr, "-M requires an argument between 1 and 4\n");
89840df0 1449 usage();
e809a587 1450 }
89840df0 1451 break;
e809a587 1452 case 'i':
e809a587 1453 noinventory = 1;
115ff55c 1454 break;
e809a587 1455#else
115ff55c
VB
1456 case 'M':
1457 case 'i':
115ff55c 1458 case 'P':
e809a587
VB
1459 fprintf(stderr, "LLDP-MED support is not built-in\n");
1460 usage();
e809a587 1461 break;
115ff55c 1462#endif
43c02e7b
VB
1463 case 'p':
1464 probe = atoi(optarg);
1465 break;
1466 case 'x':
e809a587 1467#ifdef USE_SNMP
43c02e7b 1468 snmp = 1;
e809a587
VB
1469#else
1470 fprintf(stderr, "SNMP support is not built-in\n");
1471 usage();
1472#endif
43c02e7b
VB
1473 break;
1474 default:
1475 found = 0;
1476 for (i=0; protos[i].mode != 0; i++) {
1477 if (protos[i].enabled) continue;
1478 if (ch == protos[i].arg) {
1479 protos[i].enabled = 1;
1480 found = 1;
1481 }
1482 }
1483 if (!found)
1484 usage();
1485 }
1486 }
115ff55c 1487
43c02e7b 1488 log_init(debug);
a2993d83 1489
eac2f38a
VB
1490 if (!debug) {
1491 int pid;
1492 char *spid;
1493 if (daemon(0, 0) != 0)
1494 fatal("failed to detach daemon");
1495 if ((pid = open(LLDPD_PID_FILE,
0aa5f676 1496 O_TRUNC | O_CREAT | O_WRONLY, 0644)) == -1)
eac2f38a
VB
1497 fatal("unable to open pid file " LLDPD_PID_FILE);
1498 if (asprintf(&spid, "%d\n", getpid()) == -1)
1499 fatal("unable to create pid file " LLDPD_PID_FILE);
1500 if (write(pid, spid, strlen(spid)) == -1)
1501 fatal("unable to write pid file " LLDPD_PID_FILE);
1502 free(spid);
1503 close(pid);
1504 }
1505
a2993d83 1506 priv_init(PRIVSEP_CHROOT);
43c02e7b
VB
1507
1508 if (probe == 0) probe = LLDPD_TTL;
1509
1510 if ((cfg = (struct lldpd *)
1511 calloc(1, sizeof(struct lldpd))) == NULL)
1512 fatal(NULL);
1513
766f32b3
VB
1514 cfg->g_mgmt_pattern = mgmtp;
1515 cfg->g_listen_vlans = vlan;
43c02e7b
VB
1516
1517 /* Get ioctl socket */
1518 if ((cfg->g_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1519 fatal("failed to get ioctl socket");
1520 cfg->g_delay = LLDPD_TX_DELAY;
1521
1522 /* Set system capabilities */
1523 cfg->g_lchassis.c_cap_available = LLDP_CAP_BRIDGE | LLDP_CAP_WLAN |
1524 LLDP_CAP_ROUTER;
89840df0
VB
1525#ifdef ENABLE_LLDPMED
1526 if (lldpmed > 0) {
1527 if (lldpmed == LLDPMED_CLASS_III)
1528 cfg->g_lchassis.c_cap_available |= LLDP_CAP_TELEPHONE;
1529 cfg->g_lchassis.c_med_type = lldpmed;
40ecae87 1530 cfg->g_lchassis.c_med_cap_available = LLDPMED_CAP_CAP |
115ff55c 1531 LLDPMED_CAP_IV | LLDPMED_CAP_LOCATION;
740593ff 1532 cfg->g_noinventory = noinventory;
89840df0
VB
1533 }
1534#endif
43c02e7b
VB
1535
1536 /* Set TTL */
1537 cfg->g_lchassis.c_ttl = LLDPD_TTL;
1538
1539 cfg->g_protocols = protos;
1540 cfg->g_probe_time = probe;
1541 for (i=0; protos[i].mode != 0; i++)
1542 if (protos[i].enabled) {
1543 cfg->g_multi++;
1544 LLOG_INFO("protocol %s enabled", protos[i].name);
1545 } else
1546 LLOG_INFO("protocol %s disabled", protos[i].name);
1547 cfg->g_multi--;
1548
1549 TAILQ_INIT(&cfg->g_hardware);
50a89ca7 1550 TAILQ_INIT(&cfg->g_vif);
43c02e7b
VB
1551
1552#ifdef USE_SNMP
1553 if (snmp) {
1554 cfg->g_snmp = 1;
1555 agent_init(cfg, debug);
1556 }
1557#endif /* USE_SNMP */
1558
1559 /* Create socket */
b5562b23
VB
1560 if ((cfg->g_ctl = priv_ctl_create(cfg)) == -1)
1561 fatalx("unable to create control socket " LLDPD_CTL_SOCKET);
1562 TAILQ_INIT(&cfg->g_clients);
43c02e7b 1563
43c02e7b
VB
1564 gcfg = cfg;
1565 if (atexit(lldpd_exit) != 0) {
b5562b23
VB
1566 close(cfg->g_ctl);
1567 priv_ctl_cleanup();
43c02e7b
VB
1568 fatal("unable to set exit function");
1569 }
43c02e7b
VB
1570
1571 /* Signal handling */
b5562b23 1572 signal(SIGHUP, lldpd_shutdown);
43c02e7b
VB
1573 signal(SIGINT, lldpd_shutdown);
1574 signal(SIGTERM, lldpd_shutdown);
1575
1576 for (;;)
1577 lldpd_loop(cfg);
1578
1579 return (0);
1580}