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();
33 struct nl_cache *nl_cli_route_alloc_cache(
struct nl_sock *sk,
int flags)
35 struct nl_cache *cache;
39 nl_cli_fatal(err,
"Unable to allocate route cache: %s\n",
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)
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)
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)
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()))
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",
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)
203 lval = strtoul(arg, &endptr, 0);
205 if ((table = rtnl_route_str2table(arg)) < 0)
212 rtnl_route_set_table(route, table);
215 void nl_cli_route_parse_prio(
struct rtnl_route *route,
char *arg)
220 lval = strtoul(arg, &endptr, 0);
222 nl_cli_fatal(EINVAL,
"Invalid priority value, not numeric");
223 rtnl_route_set_priority(route, lval);
226 void nl_cli_route_parse_scope(
struct rtnl_route *route,
char *arg)
230 if ((ival = rtnl_str2scope(arg)) < 0)
231 nl_cli_fatal(EINVAL,
"Unknown routing scope \"%s\"", arg);
233 rtnl_route_set_scope(route, ival);
236 void nl_cli_route_parse_protocol(
struct rtnl_route *route,
char *arg)
242 lval = strtoul(arg, &endptr, 0);
244 if ((proto = rtnl_route_str2proto(arg)) < 0)
246 "Unknown routing protocol name \"%s\"",
253 rtnl_route_set_protocol(route, proto);
256 void nl_cli_route_parse_type(
struct rtnl_route *route,
char *arg)
260 if ((ival = nl_str2rtntype(arg)) < 0)
261 nl_cli_fatal(EINVAL,
"Unknown routing type \"%s\"", arg);
263 if ((ival = rtnl_route_set_type(route, ival)) < 0)
268 void nl_cli_route_parse_iif(
struct rtnl_route *route,
char *arg,
struct nl_cache *link_cache)
273 nl_cli_fatal(ENOENT,
"Link \"%s\" does not exist", arg);
275 rtnl_route_set_iif(route, ival);