summaryrefslogtreecommitdiffstats
path: root/src/lib/route.c
blob: cd3e89783b33f2f558649b369e2ee0485a20f1c7 (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
/*
 * src/lib/route.c     CLI 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-2009 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @ingroup cli
 * @defgroup cli_route Routing
 *
 * @{
 */

#include <netlink/cli/utils.h>
#include <netlink/cli/route.h>

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

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

	return route;
}

struct nl_cache *nl_cli_route_alloc_cache(struct nl_sock *sk, int flags)
{
	struct nl_cache *cache;
	int err;

	if ((err = rtnl_route_alloc_cache(sk, AF_UNSPEC, flags, &cache)) < 0)
		nl_cli_fatal(err, "Unable to allocate route cache: %s\n",
			     nl_geterror(err));

	nl_cache_mngt_provide(cache);

	return cache;
}

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

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

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

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

	nl_addr_put(addr);
}

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

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

	nl_addr_put(addr);
}

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

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

	nl_addr_put(addr);
}

void nl_cli_route_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)
			nl_cli_fatal(EINVAL, "Unknown metric token \"%s\"", arg);

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

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

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

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

void nl_cli_route_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()))
		nl_cli_fatal(ENOMEM, "Out of memory");

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

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

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

			rtnl_route_nh_set_ifindex(nh, ival);
			break;

		case NH_VIA:
			addr = nl_cli_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)
				nl_cli_fatal(EINVAL,
					     "Invalid weight \"%s\", not numeric",
					     arg);
			rtnl_route_nh_set_weight(nh, lval);
			break;
		}
	}

	rtnl_route_add_nexthop(route, nh);
}

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

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

	rtnl_route_set_table(route, table);
}

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

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

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

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

	rtnl_route_set_scope(route, ival);
}

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

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

	rtnl_route_set_protocol(route, proto);
}

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

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

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

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

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

	rtnl_route_set_iif(route, ival);
}

/** @} */