aboutsummaryrefslogtreecommitdiffstats
path: root/test/packetdrill/net_utils.c
blob: 1b59d6452a4f9f86ae6b8a4d4971c41dc55d377a (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
/*
 * 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)
 *
 * Implementation for various network utilities.
 */

#include "net_utils.h"

#include <stdlib.h>
#include <net/if.h>
#include <unistd.h>

#include "logging.h"

static void verbose_system(const char *command)
{
	int result;

	DEBUGP("running: '%s'\n", command);
	result = system(command);
	DEBUGP("result: %d\n", result);
	if (result != 0)
		DEBUGP("error executing command '%s'\n", command);
}

/* Configure a local IPv4 address and netmask for the device */
static void net_add_ipv4_address(const char *dev_name,
				 const struct ip_address *ip,
				 int prefix_len)
{
	char *command = NULL;
	char ip_string[ADDR_STR_LEN];

	ip_to_string(ip, ip_string);

#ifdef linux
	asprintf(&command, "ip addr add %s/%d dev %s > /dev/null 2>&1",
		 ip_string, prefix_len, dev_name);
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
	asprintf(&command, "/sbin/ifconfig %s %s/%d alias",
		 dev_name, ip_string, prefix_len);
#endif /* defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) */

	verbose_system(command);
	free(command);
}

/* Configure a local IPv6 address and prefix length for the device */
static void net_add_ipv6_address(const char *dev_name,
				 const struct ip_address *ip,
				 int prefix_len)
{
	char *command = NULL;
	char ip_string[ADDR_STR_LEN];

	ip_to_string(ip, ip_string);

#ifdef linux

	asprintf(&command, "ip addr add %s/%d dev %s > /dev/null 2>&1",
		 ip_string, prefix_len, dev_name);
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)

	asprintf(&command, "/sbin/ifconfig %s inet6 %s/%d",
		 dev_name, ip_string, prefix_len);
#endif /* defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) */

	verbose_system(command);
	free(command);

	/* Wait for IPv6 duplicate address detection to converge,
	 * so that this address no longer shows as "tentative".
	 * e.g. "ip addr show" shows:
	 * inet6 fd3d:fa7b:d17d::36/48 scope global tentative
	 */
#ifdef linux
	if (!strstr(dev_name, "tun"))
		sleep(2);
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
	sleep(3);
#endif
}

void net_add_dev_address(const char *dev_name,
			 const struct ip_address *ip,
			 int prefix_len)
{
	switch (ip->address_family) {
	case AF_INET:
		net_add_ipv4_address(dev_name, ip, prefix_len);
		break;
	case AF_INET6:
		net_add_ipv6_address(dev_name, ip, prefix_len);
		break;
	default:
		assert(!"bad family");
		break;
	}
}

void net_del_dev_address(const char *dev_name,
			 const struct ip_address *ip,
			 int prefix_len)
{
	char *command = NULL;
	char ip_string[ADDR_STR_LEN];

	ip_to_string(ip, ip_string);

#ifdef linux
	asprintf(&command, "ip addr del %s/%d dev %s > /dev/null 2>&1",
		 ip_string, prefix_len, dev_name);
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
	asprintf(&command, "/sbin/ifconfig %s %s %s/%d -alias",
		 dev_name,
		 ip->address_family ==  AF_INET6 ? "inet6" : "",
		 ip_string, prefix_len);
#endif /* defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) */

	verbose_system(command);
	free(command);
}

/* In general we want to avoid configuring a new IP address on an
 * interface, because we do not want to pay the latency penaly
 * (e.g. it takes about one second for IPv6 duplicate address
 * detection). So if we find the IP configured the correct local
 * network device, then we're done, and we short-circuit and return
 * immediately. Otherwise remove the address from the current device
 * and add it on the newly-requested device.
 */
void net_setup_dev_address(const char *dev_name,
			   const struct ip_address *ip,
			   int prefix_len)
{
	char cur_dev_name[IFNAMSIZ];

	bool found = get_ip_device(ip, cur_dev_name);

	DEBUGP("net_setup_dev_address: found: %d\n", found);

	if (found && strcmp(cur_dev_name, dev_name) == 0) {
		DEBUGP("net_setup_dev_address: found on correct device\n");
		return;
	}

	if (found)
		net_del_dev_address(cur_dev_name, ip, prefix_len);
	net_add_dev_address(dev_name, ip, prefix_len);
}