13#include <netlink/cli/utils.h>
14#include <netlink/cli/route.h>
16struct rtnl_route *nl_cli_route_alloc(
void)
18 struct rtnl_route *route;
20 route = rtnl_route_alloc();
27struct nl_cache *nl_cli_route_alloc_cache(
struct nl_sock *sk,
int flags)
29 struct nl_cache *cache;
33 nl_cli_fatal(err,
"Unable to allocate route cache: %s\n",
41void nl_cli_route_parse_family(
struct rtnl_route *route,
char *arg)
45 if ((family = nl_str2af(arg)) != AF_UNSPEC)
46 rtnl_route_set_family(route, family);
49void nl_cli_route_parse_dst(
struct rtnl_route *route,
char *arg)
54 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
55 if ((err = rtnl_route_set_dst(route, addr)) < 0)
56 nl_cli_fatal(err,
"Unable to set destination address: %s",
62void nl_cli_route_parse_src(
struct rtnl_route *route,
char *arg)
67 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
68 if ((err = rtnl_route_set_src(route, addr)) < 0)
75void nl_cli_route_parse_pref_src(
struct rtnl_route *route,
char *arg)
80 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
81 if ((err = rtnl_route_set_pref_src(route, addr)) < 0)
82 nl_cli_fatal(err,
"Unable to set preferred source address: %s",
88void nl_cli_route_parse_metric(
struct rtnl_route *route,
char *subopts)
91 static char *
const tokens[] = {
110 while (*subopts !=
'\0') {
111 int ret = getsubopt(&subopts, tokens, &arg);
113 nl_cli_fatal(EINVAL,
"Unknown metric token \"%s\"", arg);
116 nl_cli_fatal(EINVAL,
"Invalid metric \"%s\"", tokens[ret]);
119 nl_cli_fatal(EINVAL,
"Metric \"%s\", no value given", tokens[ret]);
121 lval = strtoul(arg, &endptr, 0);
123 nl_cli_fatal(EINVAL,
"Metric \"%s\", value not numeric", tokens[ret]);
125 if ((ret = rtnl_route_set_metric(route, ret, lval)) < 0)
131void nl_cli_route_parse_nexthop(
struct rtnl_route *route,
char *subopts,
132 struct nl_cache *link_cache)
140 static char *
const tokens[] = {
147 struct rtnl_nexthop *nh;
149 struct nl_addr *addr;
153 if (!(nh = rtnl_route_nh_alloc()))
156 while (*subopts !=
'\0') {
157 int ret = getsubopt(&subopts, tokens, &arg);
159 nl_cli_fatal(EINVAL,
"Unknown nexthop token \"%s\"", arg);
162 nl_cli_fatal(EINVAL,
"Missing argument to option \"%s\"\n",
170 rtnl_route_nh_set_ifindex(nh, ival);
174 if (rtnl_route_get_family(route) == AF_MPLS) {
175 addr = nl_cli_addr_parse(arg, 0);
176 rtnl_route_nh_set_via(nh, addr);
178 addr = nl_cli_addr_parse(arg,rtnl_route_get_family(route));
179 rtnl_route_nh_set_gateway(nh, addr);
185 addr = nl_cli_addr_parse(arg,
186 rtnl_route_get_family(route));
187 rtnl_route_nh_set_newdst(nh, addr);
192 lval = strtoul(arg, &endptr, 0);
195 "Invalid weight \"%s\", not numeric",
197 rtnl_route_nh_set_weight(nh, lval);
202 rtnl_route_add_nexthop(route, nh);
205void nl_cli_route_parse_table(
struct rtnl_route *route,
char *arg)
211 lval = strtoul(arg, &endptr, 0);
213 if ((table = rtnl_route_str2table(arg)) < 0)
220 rtnl_route_set_table(route, table);
223void nl_cli_route_parse_prio(
struct rtnl_route *route,
char *arg)
228 lval = strtoul(arg, &endptr, 0);
230 nl_cli_fatal(EINVAL,
"Invalid priority value, not numeric");
231 rtnl_route_set_priority(route, lval);
234void nl_cli_route_parse_scope(
struct rtnl_route *route,
char *arg)
238 if ((ival = rtnl_str2scope(arg)) < 0)
239 nl_cli_fatal(EINVAL,
"Unknown routing scope \"%s\"", arg);
241 rtnl_route_set_scope(route, ival);
244void nl_cli_route_parse_protocol(
struct rtnl_route *route,
char *arg)
250 lval = strtoul(arg, &endptr, 0);
252 if ((proto = rtnl_route_str2proto(arg)) < 0)
254 "Unknown routing protocol name \"%s\"",
261 rtnl_route_set_protocol(route, proto);
264void nl_cli_route_parse_type(
struct rtnl_route *route,
char *arg)
268 if ((ival = nl_str2rtntype(arg)) < 0)
269 nl_cli_fatal(EINVAL,
"Unknown routing type \"%s\"", arg);
271 if ((ival = rtnl_route_set_type(route, ival)) < 0)
276void nl_cli_route_parse_iif(
struct rtnl_route *route,
char *arg,
struct nl_cache *link_cache)
281 nl_cli_fatal(ENOENT,
"Link \"%s\" does not exist", arg);
283 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.