2 * src/lib/route.c CLI Route Helpers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch>
14 * @defgroup cli_route Routing
19 #include <netlink/cli/utils.h>
20 #include <netlink/cli/route.h>
22 struct rtnl_route *nl_cli_route_alloc(void)
24 struct rtnl_route *route;
26 route = rtnl_route_alloc();
28 nl_cli_fatal(ENOMEM, "Unable to allocate route object");
33 struct nl_cache *nl_cli_route_alloc_cache(struct nl_sock *sk, int flags)
35 struct nl_cache *cache;
38 if ((err = rtnl_route_alloc_cache(sk, AF_UNSPEC, flags, &cache)) < 0)
39 nl_cli_fatal(err, "Unable to allocate route cache: %s\n",
42 nl_cache_mngt_provide(cache);
47 void nl_cli_route_parse_family(struct rtnl_route *route, char *arg)
51 if ((family = nl_str2af(arg)) != AF_UNSPEC)
52 rtnl_route_set_family(route, family);
55 void nl_cli_route_parse_dst(struct rtnl_route *route, char *arg)
60 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
61 if ((err = rtnl_route_set_dst(route, addr)) < 0)
62 nl_cli_fatal(err, "Unable to set destination address: %s",
68 void nl_cli_route_parse_src(struct rtnl_route *route, char *arg)
73 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
74 if ((err = rtnl_route_set_src(route, addr)) < 0)
75 nl_cli_fatal(err, "Unable to set source address: %s",
81 void nl_cli_route_parse_pref_src(struct rtnl_route *route, char *arg)
86 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
87 if ((err = rtnl_route_set_pref_src(route, addr)) < 0)
88 nl_cli_fatal(err, "Unable to set preferred source address: %s",
94 void nl_cli_route_parse_metric(struct rtnl_route *route, char *subopts)
96 /* strict equal order to RTAX_* */
97 static char *const tokens[] = {
116 while (*subopts != '\0') {
117 int ret = getsubopt(&subopts, tokens, &arg);
119 nl_cli_fatal(EINVAL, "Unknown metric token \"%s\"", arg);
122 nl_cli_fatal(EINVAL, "Invalid metric \"%s\"", tokens[ret]);
125 nl_cli_fatal(EINVAL, "Metric \"%s\", no value given", tokens[ret]);
127 lval = strtoul(arg, &endptr, 0);
129 nl_cli_fatal(EINVAL, "Metric \"%s\", value not numeric", tokens[ret]);
131 if ((ret = rtnl_route_set_metric(route, ret, lval)) < 0)
132 nl_cli_fatal(ret, "Unable to set metric: %s",
137 void nl_cli_route_parse_nexthop(struct rtnl_route *route, char *subopts,
138 struct nl_cache *link_cache)
145 static char *const tokens[] = {
151 struct rtnl_nexthop *nh;
153 struct nl_addr *addr;
157 if (!(nh = rtnl_route_nh_alloc()))
158 nl_cli_fatal(ENOMEM, "Out of memory");
160 while (*subopts != '\0') {
161 int ret = getsubopt(&subopts, tokens, &arg);
163 nl_cli_fatal(EINVAL, "Unknown nexthop token \"%s\"", arg);
166 nl_cli_fatal(EINVAL, "Missing argument to option \"%s\"\n",
171 if (!(ival = rtnl_link_name2i(link_cache, arg)))
172 nl_cli_fatal(ENOENT,"Link \"%s\" does not exist", arg);
174 rtnl_route_nh_set_ifindex(nh, ival);
178 addr = nl_cli_addr_parse(arg,rtnl_route_get_family(route));
179 rtnl_route_nh_set_gateway(nh, addr);
184 lval = strtoul(arg, &endptr, 0);
187 "Invalid weight \"%s\", not numeric",
189 rtnl_route_nh_set_weight(nh, lval);
194 rtnl_route_add_nexthop(route, nh);
197 void nl_cli_route_parse_table(struct rtnl_route *route, char *arg)
202 lval = strtoul(arg, &endptr, 0);
204 if ((lval = rtnl_route_str2table(arg)) < 0)
205 nl_cli_fatal(EINVAL, "Unknown table name \"%s\"", arg);
208 rtnl_route_set_table(route, lval);
211 void nl_cli_route_parse_prio(struct rtnl_route *route, char *arg)
216 lval = strtoul(arg, &endptr, 0);
218 nl_cli_fatal(EINVAL, "Invalid priority value, not numeric");
219 rtnl_route_set_priority(route, lval);
222 void nl_cli_route_parse_scope(struct rtnl_route *route, char *arg)
226 if ((ival = rtnl_str2scope(arg)) < 0)
227 nl_cli_fatal(EINVAL, "Unknown routing scope \"%s\"", arg);
229 rtnl_route_set_scope(route, ival);
232 void nl_cli_route_parse_protocol(struct rtnl_route *route, char *arg)
237 lval = strtoul(arg, &endptr, 0);
239 if ((lval = rtnl_route_str2proto(arg)) < 0)
241 "Unknown routing protocol name \"%s\"",
245 rtnl_route_set_protocol(route, lval);
248 void nl_cli_route_parse_type(struct rtnl_route *route, char *arg)
252 if ((ival = nl_str2rtntype(arg)) < 0)
253 nl_cli_fatal(EINVAL, "Unknown routing type \"%s\"", arg);
255 if ((ival = rtnl_route_set_type(route, ival)) < 0)
256 nl_cli_fatal(ival, "Unable to set routing type: %s",
260 void nl_cli_route_parse_iif(struct rtnl_route *route, char *arg, struct nl_cache *link_cache)
264 if (!(ival = rtnl_link_name2i(link_cache, arg)))
265 nl_cli_fatal(ENOENT, "Link \"%s\" does not exist", arg);
267 rtnl_route_set_iif(route, ival);