13#include "nl-default.h"
15#include <netlink/cli/utils.h>
16#include <netlink/cli/route.h>
18struct rtnl_route *nl_cli_route_alloc(
void)
20 struct rtnl_route *route;
22 route = rtnl_route_alloc();
29struct nl_cache *nl_cli_route_alloc_cache(
struct nl_sock *sk,
int flags)
31 struct nl_cache *cache;
35 nl_cli_fatal(err,
"Unable to allocate route cache: %s\n",
43void nl_cli_route_parse_family(
struct rtnl_route *route,
char *arg)
47 if ((family = nl_str2af(arg)) != AF_UNSPEC)
48 rtnl_route_set_family(route, family);
51void nl_cli_route_parse_dst(
struct rtnl_route *route,
char *arg)
56 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
57 if ((err = rtnl_route_set_dst(route, addr)) < 0)
58 nl_cli_fatal(err,
"Unable to set destination address: %s",
64void nl_cli_route_parse_src(
struct rtnl_route *route,
char *arg)
69 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
70 if ((err = rtnl_route_set_src(route, addr)) < 0)
77void nl_cli_route_parse_pref_src(
struct rtnl_route *route,
char *arg)
82 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
83 if ((err = rtnl_route_set_pref_src(route, addr)) < 0)
84 nl_cli_fatal(err,
"Unable to set preferred source address: %s",
90void nl_cli_route_parse_metric(
struct rtnl_route *route,
char *subopts)
93 static char *
const tokens[] = {
112 while (*subopts !=
'\0') {
113 int ret = getsubopt(&subopts, tokens, &arg);
115 nl_cli_fatal(EINVAL,
"Unknown metric token \"%s\"", arg);
118 nl_cli_fatal(EINVAL,
"Invalid metric \"%s\"", tokens[ret]);
121 nl_cli_fatal(EINVAL,
"Metric \"%s\", no value given", tokens[ret]);
123 lval = strtoul(arg, &endptr, 0);
125 nl_cli_fatal(EINVAL,
"Metric \"%s\", value not numeric", tokens[ret]);
127 if ((ret = rtnl_route_set_metric(route, ret, lval)) < 0)
133void nl_cli_route_parse_nexthop(
struct rtnl_route *route,
char *subopts,
134 struct nl_cache *link_cache)
142 static char *
const tokens[] = {
151 struct nl_addr *addr;
155 if (!(nh = rtnl_route_nh_alloc()))
158 while (*subopts !=
'\0') {
159 int ret = getsubopt(&subopts, tokens, &arg);
161 nl_cli_fatal(EINVAL,
"Unknown nexthop token \"%s\"", arg);
164 nl_cli_fatal(EINVAL,
"Missing argument to option \"%s\"\n",
172 rtnl_route_nh_set_ifindex(nh, ival);
176 if (rtnl_route_get_family(route) == AF_MPLS) {
177 addr = nl_cli_addr_parse(arg, 0);
178 rtnl_route_nh_set_via(nh, addr);
180 addr = nl_cli_addr_parse(arg,rtnl_route_get_family(route));
181 rtnl_route_nh_set_gateway(nh, addr);
187 addr = nl_cli_addr_parse(arg,
188 rtnl_route_get_family(route));
189 rtnl_route_nh_set_newdst(nh, addr);
194 lval = strtoul(arg, &endptr, 0);
197 "Invalid weight \"%s\", not numeric",
199 rtnl_route_nh_set_weight(nh, lval);
204 rtnl_route_add_nexthop(route, nh);
207void nl_cli_route_parse_table(
struct rtnl_route *route,
char *arg)
213 lval = strtoul(arg, &endptr, 0);
215 if ((table = rtnl_route_str2table(arg)) < 0)
222 rtnl_route_set_table(route, table);
225void nl_cli_route_parse_prio(
struct rtnl_route *route,
char *arg)
230 lval = strtoul(arg, &endptr, 0);
232 nl_cli_fatal(EINVAL,
"Invalid priority value, not numeric");
233 rtnl_route_set_priority(route, lval);
236void nl_cli_route_parse_scope(
struct rtnl_route *route,
char *arg)
240 if ((ival = rtnl_str2scope(arg)) < 0)
241 nl_cli_fatal(EINVAL,
"Unknown routing scope \"%s\"", arg);
243 rtnl_route_set_scope(route, ival);
246void nl_cli_route_parse_protocol(
struct rtnl_route *route,
char *arg)
252 lval = strtoul(arg, &endptr, 0);
254 if ((proto = rtnl_route_str2proto(arg)) < 0)
256 "Unknown routing protocol name \"%s\"",
263 rtnl_route_set_protocol(route, proto);
266void nl_cli_route_parse_type(
struct rtnl_route *route,
char *arg)
270 if ((ival = nl_str2rtntype(arg)) < 0)
271 nl_cli_fatal(EINVAL,
"Unknown routing type \"%s\"", arg);
273 if ((ival = rtnl_route_set_type(route, ival)) < 0)
278void nl_cli_route_parse_iif(
struct rtnl_route *route,
char *arg,
struct nl_cache *link_cache)
283 nl_cli_fatal(ENOENT,
"Link \"%s\" does not exist", arg);
285 rtnl_route_set_iif(route, ival);
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
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.
int rtnl_link_name2i(struct nl_cache *cache, const char *name)
Translate link name to corresponding interface index.
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.