libnl  1.1.4
Modules

Modules

 Request
 

Allocation/Freeing

struct flnl_result * flnl_result_alloc (void)
 
void flnl_result_put (struct flnl_result *res)
 

Cache Management

struct nl_cache * flnl_result_alloc_cache (void)
 Allocate lookup result cache. More...
 

Lookup

struct nl_msg * flnl_lookup_build_request (struct flnl_request *req, int flags)
 Builds a netlink request message to do a lookup. More...
 
int flnl_lookup (struct nl_handle *handle, struct flnl_request *req, struct nl_cache *cache)
 Perform FIB Lookup. More...
 

Attribute Access

int flnl_result_get_table_id (struct flnl_result *res)
 
int flnl_result_get_prefixlen (struct flnl_result *res)
 
int flnl_result_get_nexthop_sel (struct flnl_result *res)
 
int flnl_result_get_type (struct flnl_result *res)
 
int flnl_result_get_scope (struct flnl_result *res)
 
int flnl_result_get_error (struct flnl_result *res)
 

Detailed Description

Function Documentation

◆ flnl_result_alloc_cache()

struct nl_cache* flnl_result_alloc_cache ( void  )

Allocates a new lookup result cache and initializes it properly.

Note
Free the memory after usage using nl_cache_destroy_and_free().
Returns
Newly allocated cache or NULL if an error occured.

Definition at line 183 of file lookup.c.

References nl_cache_alloc().

184 {
185  return nl_cache_alloc(&fib_lookup_ops);
186 }
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate an empty cache.
Definition: cache.c:170

◆ flnl_lookup_build_request()

struct nl_msg* flnl_lookup_build_request ( struct flnl_request *  req,
int  flags 
)
Parameters
reqRequested match.
flagsadditional netlink message flags

Builds a new netlink message requesting a change of link attributes. The netlink message header isn't fully equipped with all relevant fields and must be sent out via nl_send_auto_complete() or supplemented as needed. old must point to a link currently configured in the kernel and tmpl must contain the attributes to be changed set via rtnl_link_set_* functions.

Returns
New netlink message
Note
Not all attributes can be changed, see Changeable Attributes for more details.

Definition at line 212 of file lookup.c.

Referenced by flnl_lookup().

213 {
214  struct nl_msg *msg;
215  struct nl_addr *addr;
216  uint64_t fwmark;
217  int tos, scope, table;
218  struct fib_result_nl fr = {0};
219 
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);
224 
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;
229 
230  addr = flnl_request_get_addr(req);
231  if (!addr) {
232  nl_error(EINVAL, "Request must specify the address");
233  return NULL;
234  }
235 
236  fr.fl_addr = *(uint32_t *) nl_addr_get_binary_addr(addr);
237 
238  msg = nlmsg_alloc_simple(0, flags);
239  if (!msg)
240  goto errout;
241 
242  if (nlmsg_append(msg, &fr, sizeof(fr), NLMSG_ALIGNTO) < 0)
243  goto errout;
244 
245  return msg;
246 
247 errout:
248  nlmsg_free(msg);
249  return NULL;
250 }
void nlmsg_free(struct nl_msg *n)
Free a netlink message.
Definition: msg.c:656
void * nl_addr_get_binary_addr(struct nl_addr *addr)
Get binary address of abstract address object.
Definition: addr.c:756
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition: msg.c:549
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition: msg.c:448

◆ flnl_lookup()

int flnl_lookup ( struct nl_handle *  handle,
struct flnl_request *  req,
struct nl_cache *  cache 
)
Parameters
handleNetlink handle.
reqLookup request object.
cacheCache for result.

Builds a netlink message to request a FIB lookup, waits for the reply and adds the result to the specified cache.

Returns
0 on success or a negative error code.

Definition at line 263 of file lookup.c.

References flnl_lookup_build_request(), nl_cache_pickup(), nl_send_auto_complete(), and nlmsg_free().

265 {
266  struct nl_msg *msg;
267  int err;
268 
269  msg = flnl_lookup_build_request(req, 0);
270  if (!msg)
271  return nl_errno(ENOMEM);
272 
273  err = nl_send_auto_complete(handle, msg);
274  nlmsg_free(msg);
275  if (err < 0)
276  return err;
277 
278  return nl_cache_pickup(handle, cache);
279 }
void nlmsg_free(struct nl_msg *n)
Free a netlink message.
Definition: msg.c:656
int nl_cache_pickup(struct nl_handle *handle, struct nl_cache *cache)
Pickup a netlink dump response and put it into a cache.
Definition: cache.c:505
int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
Send netlink message and check & extend header values as needed.
Definition: nl.c:373
struct nl_msg * flnl_lookup_build_request(struct flnl_request *req, int flags)
Builds a netlink request message to do a lookup.
Definition: lookup.c:212