summaryrefslogtreecommitdiffstats
path: root/src/device-common.c
blob: 9e7605e5205b1fe9950230e54e6fe38809517a8b (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
#include "device-common.h"
#include "netlink.h"
#include "util.h"

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <arpa/inet.h>

#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>

static unsigned long strtoul_safe(const char *str) {
	char *endptr;

	errno = 0;
	unsigned long val = strtoul(str, &endptr, 10);
	if (endptr == str || *endptr)
		errno = EINVAL;

	return val;
}

static bool parse_address(ipaddr_t *addr, const char *str) {
	if (inet_pton(AF_INET, str, &addr->addr4)) {
		addr->af = AF_INET;
		return true;
	}

	if (inet_pton(AF_INET6, str, &addr->addr6)) {
		addr->af = AF_INET6;
		return true;
	}

	return false;
}

static bool parse_prefix(ipaddr_prefix_t *prefix, const char *str, bool allow_host) {
	const char *slash = strrchr(str, '/');
	if (!slash)
		return false;

	size_t len = slash - str;
	char buf[len+1];
	memcpy(buf, str, len);
	buf[len] = 0;

	if (!parse_address(&prefix->addr, buf))
		return false;

	long plen = strtoul_safe(slash + 1);
	if (errno)
		return false;

	switch (prefix->addr.af) {
	case AF_INET:
		if (plen > 32)
			return false;
		break;

	case AF_INET6:
		if (plen > 128)
			return false;
		break;

	default:
		assert(false);
		__builtin_unreachable();
	}

	prefix->plen = plen;

	// TODO: Implement allow_host

	return true;
}

static bool process_section_device(device_common_t *device, struct json_object *section) {
	return true;
}

static bool process_section_static(device_common_t *device, struct json_object *section) {
	struct json_object *addresses = neco_json_get_value(section, "addresses", json_type_array);
	if (addresses) {
		for (size_t i = 0; i < json_object_array_length(addresses); i++) {
			struct json_object *address = json_object_array_get_idx(addresses, i);
			if (!json_object_is_type(address, json_type_string)) {
				fprintf(stderr, "interface: static: invalid address entry of type %s\n", json_type_to_name(json_object_get_type(address)));
				continue;
			}

			ipaddr_prefix_t p;
			if (!parse_prefix(&p, json_object_get_string(address), true)) {
				fprintf(stderr, "interface: static: unable to parse Address %s\n", json_object_get_string(address));
				break;
			}

			if (!VECTOR_ADD(device->addrs, p)) {
				fprintf(stderr, "interface: static: adding address failed\n");
				return false;
			}
		}
	}

	return true;
}

bool device_common_process_config(device_common_t *device, struct json_object *config) {
	struct json_object *sec_device = neco_json_get_value(config, "device", json_type_object);
	if (sec_device) {
		if (!process_section_device(device, sec_device))
			return false;
	}

	struct json_object *sec_static = neco_json_get_value(config, "static", json_type_object);
	if (sec_static) {
		if (!process_section_static(device, sec_static))
			return false;
	}

	return true;
}

static bool device_common_set_link_flags(unsigned ifindex, unsigned change, unsigned flags) {
	char buf[MNL_SOCKET_BUFFER_SIZE];

	struct mnl_socket *nl = nl_socket();

	struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
	nlh->nlmsg_type	= RTM_SETLINK;
	nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
	int seq = nlh->nlmsg_seq = nl_seq();

	struct ifinfomsg *ifi = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifi));
	ifi->ifi_index = ifindex;
	ifi->ifi_family = AF_UNSPEC;

	ifi->ifi_change = change;
	ifi->ifi_flags = flags;

	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
		perror("mnl_socket_sendto");
		return false;
	}

	int ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
	if (ret == -1) {
		perror("mnl_socket_recvfrom");
		return false;
	}

	ret = mnl_cb_run(buf, ret, seq, mnl_socket_get_portid(nl), NULL, NULL);
	if (ret == -1) {
		perror("mnl_cb_run");
		return false;
	}

	return true;
}

static bool device_common_set_link_state(unsigned ifindex, bool up) {
	return device_common_set_link_flags(ifindex, IFF_UP, up ? IFF_UP : 0);
}

static bool device_common_set_ipaddr(unsigned ifindex, ipaddr_prefix_t *addr, bool add) {
	char buf[MNL_SOCKET_BUFFER_SIZE];

	struct mnl_socket *nl = nl_socket();

	struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
	nlh->nlmsg_type	= add ? RTM_NEWADDR : RTM_DELADDR;
	nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
	if (add)
		nlh->nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;

	int seq = nlh->nlmsg_seq = nl_seq();

	struct ifaddrmsg *ifa = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifa));
	ifa->ifa_index = ifindex;
	ifa->ifa_family = addr->addr.af;

	ifa->ifa_prefixlen = addr->plen;

	switch (addr->addr.af) {
	case AF_INET:
		mnl_attr_put(nlh, IFA_LOCAL, 4, &addr->addr.addr4);
		break;

	case AF_INET6:
		mnl_attr_put(nlh, IFA_LOCAL, 16, &addr->addr.addr6);
		break;

	default:
		errno = EINVAL;
		return false;
	}

	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
		perror("mnl_socket_sendto");
		return false;
	}

	int ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
	if (ret == -1) {
		perror("mnl_socket_recvfrom");
		return false;
	}

	ret = mnl_cb_run(buf, ret, seq, mnl_socket_get_portid(nl), NULL, NULL);
	if (ret == -1) {
		perror("mnl_cb_run");
		return false;
	}

	return true;
}
void device_common_init(device_common_t *device, int ifindex) {
	device_common_set_link_state(ifindex, true);

	for (size_t i = 0; i < VECTOR_LEN(device->addrs); i++)
		device_common_set_ipaddr(ifindex, &VECTOR_INDEX(device->addrs, i), true);
}

void device_common_release(device_common_t *device, int ifindex) {
	for (size_t i = 0; i < VECTOR_LEN(device->addrs); i++)
		device_common_set_ipaddr(ifindex, &VECTOR_INDEX(device->addrs, i), false);

	device_common_set_link_state(ifindex, false);
}