summaryrefslogtreecommitdiffstats
path: root/src/daemon/interfaces.c
blob: c1179d9c5cee5755daa6589a001fe610fa229785 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/* -*- mode: c; c-file-style: "openbsd" -*- */
/*
 * Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "lldpd.h"
#include "trace.h"

#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <arpa/inet.h>

static int
lldpd_af(int af)
{
	switch (af) {
	case LLDPD_AF_IPV4:
		return AF_INET;
	case LLDPD_AF_IPV6:
		return AF_INET6;
	case LLDPD_AF_LAST:
		return AF_MAX;
	default:
		return AF_UNSPEC;
	}
}

/* Generic ethernet interface initialization */
/**
 * Enable multicast on the given interface.
 */
void
interfaces_setup_multicast(struct lldpd *cfg, const char *name, int remove)
{
	int rc;
	size_t i, j;
	const u_int8_t *mac;
	const u_int8_t zero[ETHER_ADDR_LEN] = {};

	for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
		if (!cfg->g_protocols[i].enabled) continue;
		for (j = 0; j < sizeof(cfg->g_protocols[0].mac) /
			 sizeof(cfg->g_protocols[0].mac[0]);
		     j++) {
			mac = cfg->g_protocols[i].mac[j];
			if (memcmp(mac, zero, ETHER_ADDR_LEN) == 0) break;
			if ((rc = priv_iface_multicast(name, mac, !remove)) != 0) {
				errno = rc;
				if (errno != ENOENT)
					log_debug("interfaces",
					    "unable to %s %s address to multicast filter for %s (%s)",
					    (remove) ? "delete" : "add",
					    cfg->g_protocols[i].name, name,
					    strerror(rc));
			}
		}
	}
}

/**
 * Free an interface.
 *
 * @param iff interface to be freed
 */
void
interfaces_free_device(struct interfaces_device *iff)
{
	if (!iff) return;
	free(iff->name);
	free(iff->alias);
	free(iff->address);
	free(iff->driver);
	free(iff);
}

/**
 * Free a list of interfaces.
 *
 * @param ifs list of interfaces to be freed
 */
void
interfaces_free_devices(struct interfaces_device_list *ifs)
{
	struct interfaces_device *iff, *iff_next;
	if (!ifs) return;
	for (iff = TAILQ_FIRST(ifs); iff != NULL; iff = iff_next) {
		iff_next = TAILQ_NEXT(iff, next);
		interfaces_free_device(iff);
	}
	free(ifs);
}

/**
 * Free one address
 *
 * @param ifaddr Address to be freed
 */
void
interfaces_free_address(struct interfaces_address *ifaddr)
{
	free(ifaddr);
}

/**
 * Free a list of addresses.
 *
 * @param ifaddrs list of addresses
 */
void
interfaces_free_addresses(struct interfaces_address_list *ifaddrs)
{
	struct interfaces_address *ifa, *ifa_next;
	if (!ifaddrs) return;
	for (ifa = TAILQ_FIRST(ifaddrs); ifa != NULL; ifa = ifa_next) {
		ifa_next = TAILQ_NEXT(ifa, next);
		interfaces_free_address(ifa);
	}
	free(ifaddrs);
}

/**
 * Find the appropriate interface from the name.
 *
 * @param interfaces List of available interfaces
 * @param device     Name of the device we search for
 * @return The interface or NULL if not found
 */
struct interfaces_device *
interfaces_nametointerface(struct interfaces_device_list *interfaces,
    const char *device)
{
	struct interfaces_device *iface;
	TAILQ_FOREACH (iface, interfaces, next) {
		if (!strncmp(iface->name, device, IFNAMSIZ)) return iface;
	}
	log_debug("interfaces", "cannot get interface for index %s", device);
	return NULL;
}

/**
 * Find the appropriate interface from the index.
 *
 * @param interfaces List of available interfaces
 * @param index      Index of the device we search for
 * @return The interface or NULL if not found
 */
struct interfaces_device *
interfaces_indextointerface(struct interfaces_device_list *interfaces, int index)
{
	struct interfaces_device *iface;
	TAILQ_FOREACH (iface, interfaces, next) {
		if (iface->index == index) return iface;
	}
	log_debug("interfaces", "cannot get interface for index %d", index);
	return NULL;
}

void
interfaces_helper_allowlist(struct lldpd *cfg,
    struct interfaces_device_list *interfaces)
{
	struct interfaces_device *iface;

	if (!cfg->g_config.c_iface_pattern) return;

	TAILQ_FOREACH (iface, interfaces, next) {
		int m = pattern_match(iface->name, cfg->g_config.c_iface_pattern, 0);
		switch (m) {
		case PATTERN_MATCH_DENIED:
			log_debug("interfaces", "deny %s", iface->name);
			iface->ignore = 1;
			continue;
		case PATTERN_MATCH_ALLOWED_EXACT:
			log_debug("interfaces",
			    "allow %s (consider it as a physical interface)",
			    iface->name);
			iface->type |= IFACE_PHYSICAL_T;
			continue;
		}
	}
}

#ifdef ENABLE_DOT1
static void
iface_append_vlan(struct lldpd *cfg, struct interfaces_device *vlan,
    struct interfaces_device *lower)
{
	struct lldpd_hardware *hardware =
	    lldpd_get_hardware(cfg, lower->name, lower->index);
	struct lldpd_port *port;
	struct lldpd_vlan *v;
	char *name = NULL;
	uint16_t vlan_id;

	if (hardware == NULL) {
		log_debug("interfaces", "cannot find real interface %s for VLAN %s",
		    lower->name, vlan->name);
		return;
	}
	port = &hardware->h_lport;

	for (int i = 0; (i < VLAN_BITMAP_LEN); i++) {
		if (vlan->vlan_bmap[i] == 0) continue;
		for (unsigned bit = 0; bit < 32; bit++) {
			uint32_t mask = 1L << bit;
			if (!(vlan->vlan_bmap[i] & mask)) continue;
			vlan_id = (i * 32) + bit;
			if (asprintf(&name, "vlan%d", vlan_id) == -1) return;

			/* Check if the VLAN is already here. */
			TAILQ_FOREACH (v, &port->p_vlans, v_entries)
				if (strncmp(name, v->v_name, IFNAMSIZ) == 0) {
					free(name);
					return;
				}

			if ((v = (struct lldpd_vlan *)calloc(1,
				 sizeof(struct lldpd_vlan))) == NULL) {
				free(name);
				return;
			}
			v->v_name = name;
			v->v_vid = vlan_id;
			if (vlan->pvid) port->p_pvid = vlan->pvid;
			log_debug("interfaces", "append VLAN %s for %s", v->v_name,
			    hardware->h_ifname);
			TAILQ_INSERT_TAIL(&port->p_vlans, v, v_entries);
		}
	}
}

/**
 * Append VLAN to the lowest possible interface.
 *
 * @param vlan  The VLAN interface (used to get VLAN ID).
 * @param upper The upper interface we are currently examining.
 * @param depth Depth of the stack (avoid infinite recursion)
 *
 * Initially, upper == vlan. This function will be called recursively.
 */
static void
iface_append_vlan_to_lower(struct lldpd *cfg, struct interfaces_device_list *interfaces,
    struct interfaces_device *vlan, struct interfaces_device *upper, int depth)
{
	if (depth > 5) {
		log_warnx("interfaces",
		    "BUG: maximum depth reached when applying VLAN %s (loop?)",
		    vlan->name);
		return;
	}
	depth++;
	struct interfaces_device *lower;
	log_debug("interfaces",
	    "looking to apply VLAN %s to physical interface behind %s", vlan->name,
	    upper->name);

	/* Some bridges managed VLAN internally, skip them. */
	if (upper->type & IFACE_BRIDGE_VLAN_T) {
		log_debug("interfaces",
		    "VLAN %s ignored for VLAN-aware bridge interface %s", vlan->name,
		    upper->name);
		return;
	}

	/* Easy: check if we have a lower interface. */
	if (upper->lower) {
		log_debug("interfaces", "VLAN %s on lower interface %s", vlan->name,
		    upper->name);
		iface_append_vlan_to_lower(cfg, interfaces, vlan, upper->lower, depth);
		return;
	}

	/* Other easy case, we have a physical interface. */
	if (upper->type & IFACE_PHYSICAL_T) {
		log_debug("interfaces", "VLAN %s on physical interface %s", vlan->name,
		    upper->name);
		iface_append_vlan(cfg, vlan, upper);
		return;
	}

	/* We can now search for interfaces that have our interface as an upper
	 * interface. */
	TAILQ_FOREACH (lower, interfaces, next) {
		if (lower->upper != upper) continue;
		log_debug("interfaces", "VLAN %s on lower interface %s", vlan->name,
		    upper->name);
		iface_append_vlan_to_lower(cfg, interfaces, vlan, lower, depth);
	}
}

void
interfaces_helper_vlan(struct lldpd *cfg, struct interfaces_device_list *interfaces)
{
	struct interfaces_device *iface;

	TAILQ_FOREACH (iface, interfaces, next) {
		if (!(iface->type & IFACE_VLAN_T) && bitmap_isempty(iface->vlan_bmap))
			continue;

		/* We need to find the physical interfaces of this
		   vlan, through bonds and bridges. */
		log_debug("interfaces",
		    "search physical interface for VLAN interface %s", iface->name);
		iface_append_vlan_to_lower(cfg, interfaces, iface, iface, 0);
	}
}
#endif

/* Fill out chassis ID if not already done. Only physical interfaces are
 * considered. */
void
interfaces_helper_chassis(struct lldpd *cfg, struct interfaces_device_list *interfaces)
{
	struct interfaces_device *iface;
	struct lldpd_hardware *hardware;
	char *name = NULL;
	static u_int8_t zero_mac[] = { 0, 0, 0, 0, 0, 0 };

	if (!cfg->g_config.c_cap_override) {
		LOCAL_CHASSIS(cfg)->c_cap_enabled &=
		    ~(LLDP_CAP_BRIDGE | LLDP_CAP_WLAN | LLDP_CAP_STATION);
		TAILQ_FOREACH (iface, interfaces, next) {
			if (iface->type & IFACE_BRIDGE_T)
				LOCAL_CHASSIS(cfg)->c_cap_enabled |= LLDP_CAP_BRIDGE;
			if (iface->type & IFACE_WIRELESS_T)
				LOCAL_CHASSIS(cfg)->c_cap_enabled |= LLDP_CAP_WLAN;
		}
		if ((LOCAL_CHASSIS(cfg)->c_cap_available & LLDP_CAP_STATION) &&
		    (LOCAL_CHASSIS(cfg)->c_cap_enabled == 0))
			LOCAL_CHASSIS(cfg)->c_cap_enabled = LLDP_CAP_STATION;
	}

	/* Do not modify the chassis if it's already set to a MAC address or if
	 * it's set to a local address equal to the user-provided
	 * configuration. */
	if ((LOCAL_CHASSIS(cfg)->c_id != NULL &&
		LOCAL_CHASSIS(cfg)->c_id_subtype == LLDP_CHASSISID_SUBTYPE_LLADDR) ||
	    cfg->g_config.c_cid_string != NULL)
		return; /* We already have one */

	TAILQ_FOREACH (iface, interfaces, next) {
		if (!(iface->type & IFACE_PHYSICAL_T)) continue;
		if (cfg->g_config.c_cid_pattern &&
		    !pattern_match(iface->name, cfg->g_config.c_cid_pattern, 0))
			continue;

		if ((hardware = lldpd_get_hardware(cfg, iface->name, iface->index)) ==
		    NULL)
			/* That's odd. Let's skip. */
			continue;
		if (memcmp(hardware->h_lladdr, zero_mac, ETHER_ADDR_LEN) == 0)
			/* All-zero MAC address */
			continue;

		name = malloc(ETHER_ADDR_LEN);
		if (!name) {
			log_warn("interfaces", "not enough memory for chassis ID");
			return;
		}
		free(LOCAL_CHASSIS(cfg)->c_id);
		memcpy(name, hardware->h_lladdr, ETHER_ADDR_LEN);
		LOCAL_CHASSIS(cfg)->c_id = name;
		LOCAL_CHASSIS(cfg)->c_id_len = ETHER_ADDR_LEN;
		LOCAL_CHASSIS(cfg)->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
		return;
	}
}

#undef IN_IS_ADDR_LOOPBACK
#define IN_IS_ADDR_LOOPBACK(a) ((a)->s_addr == htonl(INADDR_LOOPBACK))
#undef IN_IS_ADDR_ANY
#define IN_IS_ADDR_ANY(a) ((a)->s_addr == htonl(INADDR_ANY))
#undef IN_IS_ADDR_LINKLOCAL
#define IN_IS_ADDR_LINKLOCAL(a) (((a)->s_addr & htonl(0xffff0000)) == htonl(0xa9fe0000))
#undef IN_IS_ADDR_GLOBAL
#define IN_IS_ADDR_GLOBAL(a) \
  (!IN_IS_ADDR_LOOPBACK(a) && !IN_IS_ADDR_ANY(a) && !IN_IS_ADDR_LINKLOCAL(a))
#undef IN6_IS_ADDR_GLOBAL
#define IN6_IS_ADDR_GLOBAL(a) (!IN6_IS_ADDR_LOOPBACK(a) && !IN6_IS_ADDR_LINKLOCAL(a))

/* Add management addresses for the given family. We only take one of each
   address family, unless a pattern is provided and is not all negative. For
   example !*:*,!10.* will only deny addresses. We will pick the first IPv4
   address not matching 10.*.
*/
static int
interfaces_helper_mgmt_for_af(struct lldpd *cfg, int af,
    struct interfaces_address_list *addrs, struct interfaces_device_list *interfaces,
    int global, int allnegative)
{
	struct interfaces_address *addr;
	struct interfaces_device *device;
	struct lldpd_mgmt *mgmt;
	char addrstrbuf[INET6_ADDRSTRLEN];
	int found = 0;
	union lldpd_address in_addr;
	size_t in_addr_size;

	TAILQ_FOREACH (addr, addrs, next) {
		if (addr->address.ss_family != lldpd_af(af)) continue;

		switch (af) {
		case LLDPD_AF_IPV4:
			in_addr_size = sizeof(struct in_addr);
			memcpy(&in_addr,
			    &((struct sockaddr_in *)&addr->address)->sin_addr,
			    in_addr_size);
			if (global) {
				if (!IN_IS_ADDR_GLOBAL(&in_addr.inet)) continue;
			} else {
				if (!IN_IS_ADDR_LINKLOCAL(&in_addr.inet)) continue;
			}
			break;
		case LLDPD_AF_IPV6:
			in_addr_size = sizeof(struct in6_addr);
			memcpy(&in_addr,
			    &((struct sockaddr_in6 *)&addr->address)->sin6_addr,
			    in_addr_size);
			if (global) {
				if (!IN6_IS_ADDR_GLOBAL(&in_addr.inet6)) continue;
			} else {
				if (!IN6_IS_ADDR_LINKLOCAL(&in_addr.inet6)) continue;
			}
			break;
		default:
			assert(0);
			continue;
		}
		if (inet_ntop(lldpd_af(af), &in_addr, addrstrbuf, sizeof(addrstrbuf)) ==
		    NULL) {
			log_warn("interfaces",
			    "unable to convert IP address to a string");
			continue;
		}
		if (cfg->g_config.c_mgmt_pattern == NULL ||
		    /* Match on IP address */
		    pattern_match(addrstrbuf, cfg->g_config.c_mgmt_pattern,
			allnegative) ||
		    /* Match on interface name */
		    ((device = interfaces_indextointerface(interfaces, addr->index)) &&
			pattern_match(device->name, cfg->g_config.c_mgmt_pattern,
			    allnegative))) {
			mgmt =
			    lldpd_alloc_mgmt(af, &in_addr, in_addr_size, addr->index);
			if (mgmt == NULL) {
				assert(errno == ENOMEM); /* anything else is a bug */
				log_warn("interfaces", "out of memory error");
				return found;
			}
			log_debug("interfaces", "add management address %s",
			    addrstrbuf);
			TAILQ_INSERT_TAIL(&LOCAL_CHASSIS(cfg)->c_mgmt, mgmt, m_entries);
			found = 1;

			/* Don't take additional address if the pattern is all negative.
			 */
			if (allnegative) break;
		}
	}
	return found;
}

/* Find a management address in all available interfaces, even those that were
   already handled. This is a special interface handler because it does not
   really handle interface related information (management address is attached
   to the local chassis). */
void
interfaces_helper_mgmt(struct lldpd *cfg, struct interfaces_address_list *addrs,
    struct interfaces_device_list *interfaces)
{
	int allnegative = 0;
	int af;
	const char *pattern = cfg->g_config.c_mgmt_pattern;

	lldpd_chassis_mgmt_cleanup(LOCAL_CHASSIS(cfg));
	if (!cfg->g_config.c_mgmt_advertise) return;

	/* Is the pattern provided an actual IP address? */
	if (pattern && strpbrk(pattern, "!,*?") == NULL) {
		unsigned char addr[sizeof(struct in6_addr)];
		size_t addr_size;
		struct lldpd_mgmt *mgmt;
		struct interfaces_address *ifaddr;

		for (af = LLDPD_AF_UNSPEC + 1; af != LLDPD_AF_LAST; af++) {
			switch (af) {
			case LLDPD_AF_IPV4:
				addr_size = sizeof(struct in_addr);
				break;
			case LLDPD_AF_IPV6:
				addr_size = sizeof(struct in6_addr);
				break;
			default:
				assert(0);
			}
			if (inet_pton(lldpd_af(af), pattern, addr) == 1) break;
		}
		if (af != LLDPD_AF_LAST) {
			/* Try to get the index if possible. */
			TAILQ_FOREACH (ifaddr, addrs, next) {
				if (ifaddr->address.ss_family != lldpd_af(af)) continue;
				if (LLDPD_AF_IPV4 == af) {
					struct sockaddr_in *sa_sin;
					sa_sin = (struct sockaddr_in *)&ifaddr->address;
					if (0 ==
					    memcmp(addr, &(sa_sin->sin_addr),
						addr_size))
						break;
				} else if (LLDPD_AF_IPV6 == af) {
					if (0 ==
					    memcmp(addr,
						&((struct sockaddr_in6 *)&ifaddr
							->address)
						     ->sin6_addr,
						addr_size))
						break;
				}
			}

			mgmt = lldpd_alloc_mgmt(af, addr, addr_size,
			    ifaddr ? ifaddr->index : 0);
			if (mgmt == NULL) {
				log_warn("interfaces", "out of memory error");
				return;
			}
			log_debug("interfaces", "add exact management address %s",
			    pattern);
			TAILQ_INSERT_TAIL(&LOCAL_CHASSIS(cfg)->c_mgmt, mgmt, m_entries);
			return;
		}
		/* else: could be an interface name */
	}

	/* Is the pattern provided all negative? */
	if (pattern == NULL)
		allnegative = 1;
	else if (pattern[0] == '!') {
		/* If each comma is followed by '!', its an all
		   negative pattern */
		const char *sep = pattern;
		while ((sep = strchr(sep, ',')) && (*(++sep) == '!'))
			;
		if (sep == NULL) allnegative = 1;
	}

	/* Find management addresses */
	for (af = LLDPD_AF_UNSPEC + 1; af != LLDPD_AF_LAST; af++) {
		(void)(interfaces_helper_mgmt_for_af(cfg, af, addrs, interfaces, 1,
			   allnegative) ||
		    interfaces_helper_mgmt_for_af(cfg, af, addrs, interfaces, 0,
			allnegative));
	}
}

/* Fill up port name and description */
void
interfaces_helper_port_name_desc(struct lldpd *cfg, struct lldpd_hardware *hardware,
    struct interfaces_device *iface)
{
	struct lldpd_port *port = &hardware->h_lport;

	/* We need to set the portid to what the client configured.
	   This can be done from the CLI.
	*/
	int has_alias = (iface->alias != NULL && strlen(iface->alias) != 0 &&
	    strncmp("lldpd: ", iface->alias, 7));
	int portid_type = cfg->g_config.c_lldp_portid_type;
	if (portid_type == LLDP_PORTID_SUBTYPE_IFNAME ||
	    (portid_type == LLDP_PORTID_SUBTYPE_UNKNOWN && has_alias) ||
	    (port->p_id_subtype == LLDP_PORTID_SUBTYPE_LOCAL && has_alias)) {
		if (port->p_id_subtype != LLDP_PORTID_SUBTYPE_LOCAL) {
			log_debug("interfaces", "use ifname for %s",
			    hardware->h_ifname);
			port->p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME;
			port->p_id_len = strlen(hardware->h_ifname);
			free(port->p_id);
			if ((port->p_id = calloc(1, port->p_id_len)) == NULL)
				fatal("interfaces", NULL);
			memcpy(port->p_id, hardware->h_ifname, port->p_id_len);
		}

		if (port->p_descr_force == 0) {
			/* use the actual alias in the port description */
			log_debug("interfaces", "using alias in description for %s",
			    hardware->h_ifname);
			free(port->p_descr);
			if (has_alias) {
				port->p_descr = strdup(iface->alias);
			} else {
				/* We don't have anything else to put here and for CDP
				 * with need something non-NULL */
				port->p_descr = strdup(hardware->h_ifname);
			}
		}
	} else {
		if (port->p_id_subtype != LLDP_PORTID_SUBTYPE_LOCAL) {
			log_debug("interfaces", "use MAC address for %s",
			    hardware->h_ifname);
			port->p_id_subtype = LLDP_PORTID_SUBTYPE_LLADDR;
			free(port->p_id);
			if ((port->p_id = calloc(1, ETHER_ADDR_LEN)) == NULL)
				fatal("interfaces", NULL);
			memcpy(port->p_id, hardware->h_lladdr, ETHER_ADDR_LEN);
			port->p_id_len = ETHER_ADDR_LEN;
		}

		if (port->p_descr_force == 0) {
			/* use the ifname in the port description until alias is set */
			log_debug("interfaces", "using ifname in description for %s",
			    hardware->h_ifname);
			free(port->p_descr);
			port->p_descr = strdup(hardware->h_ifname);
		}
	}
}

void
interfaces_helper_add_hardware(struct lldpd *cfg, struct lldpd_hardware *hardware)
{
	TRACE(LLDPD_INTERFACES_NEW(hardware->h_ifname));
	TAILQ_INSERT_TAIL(&cfg->g_hardware, hardware, h_entries);
}

void
interfaces_helper_physical(struct lldpd *cfg, struct interfaces_device_list *interfaces,
    struct lldpd_ops *ops, int (*init)(struct lldpd *, struct lldpd_hardware *))
{
	struct interfaces_device *iface;
	struct lldpd_hardware *hardware;
	int created;

	TAILQ_FOREACH (iface, interfaces, next) {
		if (!(iface->type & IFACE_PHYSICAL_T)) continue;
		if (iface->ignore) continue;

		log_debug("interfaces", "%s is an acceptable ethernet device",
		    iface->name);
		created = 0;
		if ((hardware = lldpd_get_hardware(cfg, iface->name, iface->index)) ==
		    NULL) {
			if ((hardware = lldpd_alloc_hardware(cfg, iface->name,
				 iface->index)) == NULL) {
				log_warnx("interfaces",
				    "Unable to allocate space for %s", iface->name);
				continue;
			}
			created = 1;
		}
		if (hardware->h_flags) continue;
		if (hardware->h_ops != ops || hardware->h_ifindex_changed) {
			if (!created) {
				log_debug("interfaces",
				    "interface %s is converted from another type of interface",
				    hardware->h_ifname);
				if (hardware->h_ops && hardware->h_ops->cleanup) {
					hardware->h_ops->cleanup(cfg, hardware);
					levent_hardware_release(hardware);
					levent_hardware_init(hardware);
				}
			}
			if (init(cfg, hardware) != 0) {
				log_warnx("interfaces", "unable to initialize %s",
				    hardware->h_ifname);
				lldpd_hardware_cleanup(cfg, hardware);
				continue;
			}
			hardware->h_ops = ops;
			hardware->h_mangle =
			    (iface->upper && iface->upper->type & IFACE_BOND_T);
		}
		if (created)
			interfaces_helper_add_hardware(cfg, hardware);
		else
			lldpd_port_cleanup(&hardware->h_lport, 0);

		hardware->h_flags = iface->flags; /* Should be non-zero */
		iface->ignore = 1;		  /* Future handlers
						     don't have to
						     care about this
						     interface. */

		/* Get local address */
		memcpy(&hardware->h_lladdr, iface->address, ETHER_ADDR_LEN);

		/* Fill information about port */
		interfaces_helper_port_name_desc(cfg, hardware, iface);

		/* Fill additional info */
		hardware->h_mtu = iface->mtu ? iface->mtu : 1500;

#ifdef ENABLE_DOT3
		if (iface->upper && iface->upper->type & IFACE_BOND_T)
			hardware->h_lport.p_aggregid = iface->upper->index;
		else
			hardware->h_lport.p_aggregid = 0;
#endif
	}
}

void
interfaces_helper_promisc(struct lldpd *cfg, struct lldpd_hardware *hardware)
{
	if (!cfg->g_config.c_promisc) return;
	if (priv_iface_promisc(hardware->h_ifname) != 0) {
		log_warnx("interfaces", "unable to enable promiscuous mode for %s",
		    hardware->h_ifname);
	}
}

/**
 * Send the packet using the hardware function. Optionnaly mangle the MAC address.
 *
 * With bonds, we have duplicate MAC address on different physical
 * interfaces. We need to alter the source MAC address when we send on an
 * inactive slave. The `h_mangle` flah is used to know if we need to do
 * something like that.
 */
int
interfaces_send_helper(struct lldpd *cfg, struct lldpd_hardware *hardware, char *buffer,
    size_t size)
{
	if (size < 2 * ETHER_ADDR_LEN) {
		log_warnx("interfaces", "packet to send on %s is too small!",
		    hardware->h_ifname);
		return 0;
	}
	if (hardware->h_mangle) {
#define MAC_UL_ADMINISTERED_BIT_MASK 0x02
		char *src_mac = buffer + ETHER_ADDR_LEN;
		char arbitrary[] = { 0x00, 0x60, 0x08, 0x69, 0x97, 0xef };

		switch (cfg->g_config.c_bond_slave_src_mac_type) {
		case LLDP_BOND_SLAVE_SRC_MAC_TYPE_LOCALLY_ADMINISTERED:
			if (!(*src_mac & MAC_UL_ADMINISTERED_BIT_MASK)) {
				*src_mac |= MAC_UL_ADMINISTERED_BIT_MASK;
				break;
			}
			/* Fallback to fixed value */
			memcpy(src_mac, arbitrary, ETHER_ADDR_LEN);
			break;
		case LLDP_BOND_SLAVE_SRC_MAC_TYPE_FIXED:
			memcpy(src_mac, arbitrary, ETHER_ADDR_LEN);
			break;
		case LLDP_BOND_SLAVE_SRC_MAC_TYPE_ZERO:
			memset(src_mac, 0, ETHER_ADDR_LEN);
			break;
		}
	}
	return hardware->h_ops->send(cfg, hardware, buffer, size);
}