summaryrefslogtreecommitdiffstats
path: root/src/route-utils.c
blob: 549e6ff222d2932f71c426f80443def244c46f8a (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
/*
 * src/route-utils.c     Route Helpers
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2008 Thomas Graf <tgraf@suug.ch>
 */

#include "route-utils.h"

struct rtnl_route *nlt_alloc_route(void)
{
	struct rtnl_route *route;

	route = rtnl_route_alloc();
	if (!route)
		fatal(ENOMEM, "Unable to allocate route object");

	return route;
}

void parse_family(struct rtnl_route *route, char *arg)
{
	int family;

	if ((family = nl_str2af(arg)) != AF_UNSPEC)
		rtnl_route_set_family(route, family);
}

void parse_dst(struct rtnl_route *route, char *arg)
{
	struct nl_addr *addr;
	int err;

	addr = nlt_addr_parse(arg, rtnl_route_get_family(route));
	if ((err = rtnl_route_set_dst(route, addr)) < 0)
		fatal(err, "Unable to set destination address: %s",
		      nl_geterror(err));

	nl_addr_put(addr);
}

void parse_src(struct rtnl_route *route, char *arg)
{
	struct nl_addr *addr;
	int err;

	addr = nlt_addr_parse(arg, rtnl_route_get_family(route));
	if ((err = rtnl_route_set_src(route, addr)) < 0)
		fatal(err, "Unable to set source address: %s",
		      nl_geterror(err));

	nl_addr_put(addr);
}

void parse_pref_src(struct rtnl_route *route, char *arg)
{
	struct nl_addr *addr;
	int err;

	addr = nlt_addr_parse(arg, rtnl_route_get_family(route));
	if ((err = rtnl_route_set_pref_src(route, addr)) < 0)
		fatal(err, "Unable to set preferred source address: %s",
		      nl_geterror(err));

	nl_addr_put(addr);
}

void parse_metric(struct rtnl_route *route, char *subopts)
{
	/* strict equal order to RTAX_* */
	static char *const tokens[] = {
		"unspec",
		"lock",
		"mtu",
		"window",
		"rtt",
		"rttvar",
		"sstresh",
		"cwnd",
		"advmss",
		"reordering",
		"hoplimit",
		"initcwnd",
		"features",
		NULL,
	};
	unsigned long lval;
	char *arg, *endptr;

	while (*subopts != '\0') {
		int ret = getsubopt(&subopts, tokens, &arg);
		if (ret == -1)
			fatal(EINVAL, "Unknown metric token \"%s\"", arg);

		if (ret == 0)
			fatal(EINVAL, "Invalid metric \"%s\"", tokens[ret]);

		if (arg == NULL)
			fatal(EINVAL, "Metric \"%s\", no value given", tokens[ret]);

		lval = strtoul(arg, &endptr, 0);
		if (endptr == arg)
			fatal(EINVAL, "Metric \"%s\", value not numeric", tokens[ret]);

		if ((ret = rtnl_route_set_metric(route, ret, lval)) < 0)
			fatal(ret, "Unable to set metric: %s",
			      nl_geterror(ret));
	}
}

void parse_nexthop(struct rtnl_route *route, char *subopts,
		   struct nl_cache *link_cache)
{
	enum {
		NH_DEV,
		NH_VIA,
		NH_WEIGHT,
	};
	static char *const tokens[] = {
		"dev",
		"via",
		"weight",
		NULL,
	};
	struct rtnl_nexthop *nh;
	unsigned long lval;
	struct nl_addr *addr;
	int ival;
	char *arg, *endptr;

	if (!(nh = rtnl_route_nh_alloc()))
		fatal(ENOMEM, "Out of memory");

	while (*subopts != '\0') {
		int ret = getsubopt(&subopts, tokens, &arg);
		if (ret == -1)
			fatal(EINVAL, "Unknown nexthop token \"%s\"", arg);

		if (arg == NULL)
			fatal(EINVAL, "Missing argument to option \"%s\"\n",
				tokens[ret]);

		switch (ret) {
		case NH_DEV:
			if (!(ival = rtnl_link_name2i(link_cache, arg)))
				fatal(ENOENT,"Link \"%s\" does not exist", arg);

			rtnl_route_nh_set_ifindex(nh, ival);
			break;

		case NH_VIA:
			addr = nlt_addr_parse(arg,rtnl_route_get_family(route));
			rtnl_route_nh_set_gateway(nh, addr);
			nl_addr_put(addr);
			break;

		case NH_WEIGHT:
			lval = strtoul(arg, &endptr, 0);
			if (endptr == arg)
				fatal(EINVAL, "Invalid weight \"%s\", not numeric", arg);
			rtnl_route_nh_set_weight(nh, lval);
			break;
		}
	}

	rtnl_route_add_nexthop(route, nh);
}

void parse_table(struct rtnl_route *route, char *arg)
{
	unsigned long lval;
	char *endptr;

	lval = strtoul(arg, &endptr, 0);
	if (endptr == arg) {
		if ((lval = rtnl_route_str2table(arg)) < 0)
			fatal(EINVAL, "Unknown table name \"%s\"", arg);
	}

	rtnl_route_set_table(route, lval);
}

void parse_prio(struct rtnl_route *route, char *arg)
{
	unsigned long lval;
	char *endptr;

	lval = strtoul(arg, &endptr, 0);
	if (endptr == arg)
		fatal(EINVAL, "Invalid priority value, not numeric");
	rtnl_route_set_priority(route, lval);
}

void parse_scope(struct rtnl_route *route, char *arg)
{
	int ival;

	if ((ival = rtnl_str2scope(arg)) < 0)
		fatal(EINVAL, "Unknown routing scope \"%s\"", arg);

	rtnl_route_set_scope(route, ival);
}

void parse_protocol(struct rtnl_route *route, char *arg)
{
	unsigned long lval;
	char *endptr;

	lval = strtoul(arg, &endptr, 0);
	if (endptr == arg) {
		if ((lval = rtnl_route_str2proto(arg)) < 0)
			fatal(EINVAL, "Unknown routing protocol name \"%s\"",
				arg);
	}

	rtnl_route_set_protocol(route, lval);
}

void parse_type(struct rtnl_route *route, char *arg)
{
	int ival;

	if ((ival = nl_str2rtntype(arg)) < 0)
		fatal(EINVAL, "Unknown routing type \"%s\"", arg);

	if ((ival = rtnl_route_set_type(route, ival)) < 0)
		fatal(ival, "Unable to set routing type: %s",
		      nl_geterror(ival));
}

void parse_iif(struct rtnl_route *route, char *arg, struct nl_cache *link_cache)
{
	int ival;

	if (!(ival = rtnl_link_name2i(link_cache, arg)))
		fatal(ENOENT, "Link \"%s\" does not exist", arg);

	rtnl_route_set_iif(route, ival);
}