aboutsummaryrefslogtreecommitdiffstats
path: root/test/packetdrill/link_layer.c
blob: d6e85a4890e944241611e63fb9641723982d23f0 (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
/*
 * Copyright 2013 Google Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
/*
 * Author: ncardwell@google.com (Neal Cardwell)
 *
 * Link-layer utilities.
 */

#include "link_layer.h"

#include <stdlib.h>
#include <unistd.h>

#include "logging.h"

#ifdef linux

#include <net/if.h>
#include <sys/ioctl.h>

#include "wrap.h"

void get_hw_address(const char *name, struct ether_addr *hw_address,
			enum ip_version_t ip_version)
{
	u8 *m = NULL;
	struct ifreq ifr;
	int fd;

	DEBUGP("get_hw_address for device %s\n", name);

	fd = wrap_socket(ip_version, SOCK_DGRAM);

	/* Discover the index of the interface. */
	snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name);
	if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0)
		die_perror("ioctl SIOCGIFINDEX");

	/* Get hardware address for the interface. */
	if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0)
		die_perror("ioctl SIOCGIFHWADDR");

	m = (u8 *)&ifr.ifr_addr.sa_data;
	DEBUGP("%s HWaddr: %02x:%02x:%02x:%02x:%02x:%02x\n",
	       name, m[0], m[1], m[2], m[3], m[4], m[5]);
	memcpy(hw_address, m, sizeof(*hw_address));

	if (close(fd))
		die_perror("close");
}

#else

#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#include <net/if_types.h>
#include <net/if_dl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#endif /* defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) */

void get_hw_address(const char *name, struct ether_addr *hw_address)
{
	struct ifaddrs *ifaddrs_list, *ifaddr;

	DEBUGP("get_hw_address for device %s\n", name);

	if (getifaddrs(&ifaddrs_list) < 0)
		die_perror("getifaddrs");

	for (ifaddr = ifaddrs_list; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
		if (strcmp(name, ifaddr->ifa_name) == 0 &&
		    ifaddr->ifa_addr->sa_family == AF_LINK) {
			struct sockaddr_dl *sdl;
			sdl = (struct sockaddr_dl *)ifaddr->ifa_addr;
			if (sdl->sdl_type == IFT_ETHER) {
				memcpy(hw_address, LLADDR(sdl),
				       sizeof(*hw_address));
				freeifaddrs(ifaddrs_list);
				return;
			}
		}
	}

	die("unable to find hw address for %s\n", name);
}

#endif