20 #include <netlink/cli/utils.h> 21 #include <netlink/cli/route.h> 23 struct rtnl_route *nl_cli_route_alloc(
void)
25 struct rtnl_route *route;
27 route = rtnl_route_alloc();
34 struct nl_cache *nl_cli_route_alloc_cache(
struct nl_sock *sk,
int flags)
36 struct nl_cache *cache;
40 nl_cli_fatal(err,
"Unable to allocate route cache: %s\n",
48 void nl_cli_route_parse_family(
struct rtnl_route *route,
char *arg)
52 if ((family = nl_str2af(arg)) != AF_UNSPEC)
53 rtnl_route_set_family(route, family);
56 void nl_cli_route_parse_dst(
struct rtnl_route *route,
char *arg)
61 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
62 if ((err = rtnl_route_set_dst(route, addr)) < 0)
63 nl_cli_fatal(err,
"Unable to set destination address: %s",
69 void nl_cli_route_parse_src(
struct rtnl_route *route,
char *arg)
74 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
75 if ((err = rtnl_route_set_src(route, addr)) < 0)
82 void nl_cli_route_parse_pref_src(
struct rtnl_route *route,
char *arg)
87 addr = nl_cli_addr_parse(arg, rtnl_route_get_family(route));
88 if ((err = rtnl_route_set_pref_src(route, addr)) < 0)
89 nl_cli_fatal(err,
"Unable to set preferred source address: %s",
95 void nl_cli_route_parse_metric(
struct rtnl_route *route,
char *subopts)
98 static char *
const tokens[] = {
117 while (*subopts !=
'\0') {
118 int ret = getsubopt(&subopts, tokens, &arg);
120 nl_cli_fatal(EINVAL,
"Unknown metric token \"%s\"", arg);
123 nl_cli_fatal(EINVAL,
"Invalid metric \"%s\"", tokens[ret]);
126 nl_cli_fatal(EINVAL,
"Metric \"%s\", no value given", tokens[ret]);
128 lval = strtoul(arg, &endptr, 0);
130 nl_cli_fatal(EINVAL,
"Metric \"%s\", value not numeric", tokens[ret]);
132 if ((ret = rtnl_route_set_metric(route, ret, lval)) < 0)
138 void nl_cli_route_parse_nexthop(
struct rtnl_route *route,
char *subopts,
139 struct nl_cache *link_cache)
147 static char *
const tokens[] = {
154 struct rtnl_nexthop *nh;
156 struct nl_addr *addr;
160 if (!(nh = rtnl_route_nh_alloc()))
163 while (*subopts !=
'\0') {
164 int ret = getsubopt(&subopts, tokens, &arg);
166 nl_cli_fatal(EINVAL,
"Unknown nexthop token \"%s\"", arg);
169 nl_cli_fatal(EINVAL,
"Missing argument to option \"%s\"\n",
177 rtnl_route_nh_set_ifindex(nh, ival);
181 if (rtnl_route_get_family(route) == AF_MPLS) {
182 addr = nl_cli_addr_parse(arg, 0);
183 rtnl_route_nh_set_via(nh, addr);
185 addr = nl_cli_addr_parse(arg,rtnl_route_get_family(route));
186 rtnl_route_nh_set_gateway(nh, addr);
192 addr = nl_cli_addr_parse(arg,
193 rtnl_route_get_family(route));
194 rtnl_route_nh_set_newdst(nh, addr);
199 lval = strtoul(arg, &endptr, 0);
202 "Invalid weight \"%s\", not numeric",
204 rtnl_route_nh_set_weight(nh, lval);
209 rtnl_route_add_nexthop(route, nh);
212 void nl_cli_route_parse_table(
struct rtnl_route *route,
char *arg)
218 lval = strtoul(arg, &endptr, 0);
220 if ((table = rtnl_route_str2table(arg)) < 0)
227 rtnl_route_set_table(route, table);
230 void nl_cli_route_parse_prio(
struct rtnl_route *route,
char *arg)
235 lval = strtoul(arg, &endptr, 0);
237 nl_cli_fatal(EINVAL,
"Invalid priority value, not numeric");
238 rtnl_route_set_priority(route, lval);
241 void nl_cli_route_parse_scope(
struct rtnl_route *route,
char *arg)
245 if ((ival = rtnl_str2scope(arg)) < 0)
246 nl_cli_fatal(EINVAL,
"Unknown routing scope \"%s\"", arg);
248 rtnl_route_set_scope(route, ival);
251 void nl_cli_route_parse_protocol(
struct rtnl_route *route,
char *arg)
257 lval = strtoul(arg, &endptr, 0);
259 if ((proto = rtnl_route_str2proto(arg)) < 0)
261 "Unknown routing protocol name \"%s\"",
268 rtnl_route_set_protocol(route, proto);
271 void nl_cli_route_parse_type(
struct rtnl_route *route,
char *arg)
275 if ((ival = nl_str2rtntype(arg)) < 0)
276 nl_cli_fatal(EINVAL,
"Unknown routing type \"%s\"", arg);
278 if ((ival = rtnl_route_set_type(route, ival)) < 0)
283 void nl_cli_route_parse_iif(
struct rtnl_route *route,
char *arg,
struct nl_cache *link_cache)
288 nl_cli_fatal(ENOENT,
"Link \"%s\" does not exist", arg);
290 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.