19 #include <netlink-local.h>
20 #include <netlink/netlink.h>
21 #include <netlink/attr.h>
22 #include <netlink/utils.h>
23 #include <netlink/object.h>
24 #include <netlink/route/rtnl.h>
25 #include <netlink/route/route.h>
26 #include <netlink/fib_lookup/request.h>
27 #include <netlink/fib_lookup/lookup.h>
34 struct fib_result_nl {
38 unsigned char fl_scope;
39 unsigned char tb_id_in;
42 unsigned char prefixlen;
50 static void result_free_data(
struct nl_object *obj)
52 struct flnl_result *res = nl_object_priv(obj);
54 if (res && res->fr_req)
58 static int result_clone(
struct nl_object *_dst,
struct nl_object *_src)
60 struct flnl_result *dst = nl_object_priv(_dst);
61 struct flnl_result *src = nl_object_priv(_src);
64 if (!(dst->fr_req = (
struct flnl_request *)
66 return nl_get_errno();
74 struct flnl_result *res;
75 struct fib_result_nl *fr;
79 res = flnl_result_alloc();
85 res->fr_req = flnl_request_alloc();
93 err = flnl_request_set_addr(res->fr_req, addr);
98 flnl_request_set_fwmark(res->fr_req, fr->fl_fwmark);
99 flnl_request_set_tos(res->fr_req, fr->fl_tos);
100 flnl_request_set_scope(res->fr_req, fr->fl_scope);
101 flnl_request_set_table(res->fr_req, fr->tb_id_in);
103 res->fr_table_id = fr->tb_id;
104 res->fr_prefixlen = fr->prefixlen;
105 res->fr_nh_sel = fr->nh_sel;
106 res->fr_type = fr->type;
107 res->fr_scope = fr->scope;
108 res->fr_error = fr->err;
110 err = pp->pp_cb((
struct nl_object *) res, pp);
120 flnl_result_put(res);
124 static int result_dump_brief(
struct nl_object *obj,
struct nl_dump_params *p)
126 struct flnl_result *res = (
struct flnl_result *) obj;
130 dp_dump(p,
"table %s prefixlen %u next-hop-selector %u\n",
131 rtnl_route_table2str(res->fr_table_id, buf,
sizeof(buf)),
132 res->fr_prefixlen, res->fr_nh_sel);
133 dp_dump_line(p, line++,
"type %s ",
134 nl_rtntype2str(res->fr_type, buf,
sizeof(buf)));
135 dp_dump(p,
"scope %s error %s (%d)\n",
136 rtnl_scope2str(res->fr_scope, buf,
sizeof(buf)),
137 strerror_r(-res->fr_error, buf,
sizeof(buf)), res->fr_error);
142 static int result_dump_full(
struct nl_object *obj,
struct nl_dump_params *p)
144 return result_dump_brief(obj, p);
147 static int result_compare(
struct nl_object *_a,
struct nl_object *_b,
148 uint32_t attrs,
int flags)
158 struct flnl_result *flnl_result_alloc(
void)
163 void flnl_result_put(
struct flnl_result *res)
215 struct nl_addr *addr;
217 int tos, scope, table;
218 struct fib_result_nl fr = {0};
220 fwmark = flnl_request_get_fwmark(req);
221 tos = flnl_request_get_tos(req);
222 scope = flnl_request_get_scope(req);
223 table = flnl_request_get_table(req);
225 fr.fl_fwmark = fwmark != UINT_LEAST64_MAX ? fwmark : 0;
226 fr.fl_tos = tos >= 0 ? tos : 0;
227 fr.fl_scope = scope >= 0 ? scope : RT_SCOPE_UNIVERSE;
228 fr.tb_id_in = table >= 0 ? table : RT_TABLE_UNSPEC;
230 addr = flnl_request_get_addr(req);
232 nl_error(EINVAL,
"Request must specify the address");
242 if (
nlmsg_append(msg, &fr,
sizeof(fr), NLMSG_ALIGNTO) < 0)
263 int flnl_lookup(
struct nl_handle *handle,
struct flnl_request *req,
264 struct nl_cache *cache)
271 return nl_errno(ENOMEM);
288 int flnl_result_get_table_id(
struct flnl_result *res)
290 return res->fr_table_id;
293 int flnl_result_get_prefixlen(
struct flnl_result *res)
295 return res->fr_prefixlen;
298 int flnl_result_get_nexthop_sel(
struct flnl_result *res)
300 return res->fr_nh_sel;
303 int flnl_result_get_type(
struct flnl_result *res)
308 int flnl_result_get_scope(
struct flnl_result *res)
310 return res->fr_scope;
313 int flnl_result_get_error(
struct flnl_result *res)
315 return res->fr_error;
321 .
oo_name =
"fib_lookup/result",
322 .oo_size =
sizeof(
struct flnl_result),
323 .oo_free_data = result_free_data,
324 .oo_clone = result_clone,
327 .oo_compare = result_compare,
331 .co_name =
"fib_lookup/fib_lookup",
332 .co_hdrsize =
sizeof(
struct fib_result_nl),
334 { 0, NL_ACT_UNSPEC,
"any" },
335 END_OF_MSGTYPES_LIST,
337 .co_protocol = NETLINK_FIB_LOOKUP,
338 .co_msg_parser = result_msg_parser,
339 .co_obj_ops = &result_obj_ops,
342 static void __init fib_lookup_init(
void)
347 static void __exit fib_lookup_exit(
void)
Dump object in a brief one-liner.
void * nlmsg_data(const struct nlmsghdr *nlh)
head of message payload
uint16_t nlmsg_type
Message type (content type)
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
void nlmsg_free(struct nl_msg *n)
Free a netlink message.
int nl_cache_pickup(struct nl_handle *handle, struct nl_cache *cache)
Pickup a netlink dump response and put it into a cache.
Stop parsing altogether and discard remaining messages.
int flnl_lookup(struct nl_handle *handle, struct flnl_request *req, struct nl_cache *cache)
Perform FIB Lookup.
int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
Send netlink message and check & extend header values as needed.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
void * nl_addr_get_binary_addr(struct nl_addr *addr)
Get binary address of abstract address object.
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
struct nl_cache * flnl_result_alloc_cache(void)
Allocate lookup result cache.
struct nl_msg * flnl_lookup_build_request(struct flnl_request *req, int flags)
Builds a netlink request message to do a lookup.
struct nl_object * nl_object_clone(struct nl_object *obj)
Allocate a new object and copy all data from an existing object.
char * oo_name
Unique name of object type.
Dump all attributes but no statistics.
struct nl_addr * nl_addr_build(int family, void *buf, size_t size)
Allocate new abstract address object based on a binary address.
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate an empty cache.