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)
146 static char *
const tokens[] = {
153 struct rtnl_nexthop *nh;
155 struct nl_addr *addr;
159 if (!(nh = rtnl_route_nh_alloc()))
162 while (*subopts !=
'\0') {
163 int ret = getsubopt(&subopts, tokens, &arg);
165 nl_cli_fatal(EINVAL,
"Unknown nexthop token \"%s\"", arg);
168 nl_cli_fatal(EINVAL,
"Missing argument to option \"%s\"\n",
176 rtnl_route_nh_set_ifindex(nh, ival);
180 if (rtnl_route_get_family(route) == AF_MPLS) {
181 addr = nl_cli_addr_parse(arg, 0);
182 rtnl_route_nh_set_via(nh, addr);
184 addr = nl_cli_addr_parse(arg,rtnl_route_get_family(route));
185 rtnl_route_nh_set_gateway(nh, addr);
191 addr = nl_cli_addr_parse(arg,
192 rtnl_route_get_family(route));
193 rtnl_route_nh_set_newdst(nh, addr);
198 lval = strtoul(arg, &endptr, 0);
201 "Invalid weight \"%s\", not numeric",
203 rtnl_route_nh_set_weight(nh, lval);
208 rtnl_route_add_nexthop(route, nh);
211 void nl_cli_route_parse_table(
struct rtnl_route *route,
char *arg)
217 lval = strtoul(arg, &endptr, 0);
219 if ((table = rtnl_route_str2table(arg)) < 0)
226 rtnl_route_set_table(route, table);
229 void nl_cli_route_parse_prio(
struct rtnl_route *route,
char *arg)
234 lval = strtoul(arg, &endptr, 0);
236 nl_cli_fatal(EINVAL,
"Invalid priority value, not numeric");
237 rtnl_route_set_priority(route, lval);
240 void nl_cli_route_parse_scope(
struct rtnl_route *route,
char *arg)
244 if ((ival = rtnl_str2scope(arg)) < 0)
245 nl_cli_fatal(EINVAL,
"Unknown routing scope \"%s\"", arg);
247 rtnl_route_set_scope(route, ival);
250 void nl_cli_route_parse_protocol(
struct rtnl_route *route,
char *arg)
256 lval = strtoul(arg, &endptr, 0);
258 if ((proto = rtnl_route_str2proto(arg)) < 0)
260 "Unknown routing protocol name \"%s\"",
267 rtnl_route_set_protocol(route, proto);
270 void nl_cli_route_parse_type(
struct rtnl_route *route,
char *arg)
274 if ((ival = nl_str2rtntype(arg)) < 0)
275 nl_cli_fatal(EINVAL,
"Unknown routing type \"%s\"", arg);
277 if ((ival = rtnl_route_set_type(route, ival)) < 0)
282 void nl_cli_route_parse_iif(
struct rtnl_route *route,
char *arg,
struct nl_cache *link_cache)
287 nl_cli_fatal(ENOENT,
"Link \"%s\" does not exist", arg);
289 rtnl_route_set_iif(route, ival);
void nl_cache_mngt_provide(struct nl_cache *cache)
Provide a cache for global use.
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
int rtnl_route_alloc_cache(struct nl_sock *sk, int family, int flags, struct nl_cache **result)
Build a route cache holding all routes currently configured in the kernel.
int rtnl_link_name2i(struct nl_cache *cache, const char *name)
Translate link name to corresponding interface index.