13#include "nl-default.h"
15#include <netlink/netlink.h>
16#include <netlink/cache.h>
17#include <netlink/utils.h>
18#include <netlink/data.h>
19#include <netlink/route/rtnl.h>
20#include <netlink/route/route.h>
21#include <netlink/route/link.h>
24#include "nl-priv-dynamic-core/nl-core.h"
25#include "nl-priv-dynamic-core/cache-api.h"
26#include "nl-aux-route/nl-route.h"
28static struct nl_cache_ops rtnl_route_ops;
30static int route_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
31 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
33 struct rtnl_route *route;
36 if ((err = rtnl_route_parse(nlh, &route)) < 0)
39 err = pp->pp_cb((
struct nl_object *) route, pp);
41 rtnl_route_put(route);
45static int route_request_update(
struct nl_cache *c,
struct nl_sock *h)
48 .rtm_family = c->c_iarg1,
52 rhdr.rtm_flags |= RTM_F_CLONED;
54 return nl_send_simple(h, RTM_GETROUTE, NLM_F_DUMP, &rhdr,
sizeof(rhdr));
81 struct nl_cache **result)
83 struct nl_cache *cache;
89 cache->c_iarg1 = family;
90 cache->c_iarg2 = flags;
108static int build_route_msg(
struct rtnl_route *tmpl,
int cmd,
int flags,
109 struct nl_msg **result)
117 if ((err = rtnl_route_build_msg(msg, tmpl)) < 0) {
126int rtnl_route_build_add_request(
struct rtnl_route *tmpl,
int flags,
127 struct nl_msg **result)
129 return build_route_msg(tmpl, RTM_NEWROUTE, NLM_F_CREATE | flags,
133int rtnl_route_lookup(
struct nl_sock *sk,
struct nl_addr *dst,
134 struct rtnl_route **result)
136 _nl_auto_nl_msg
struct nl_msg *msg = NULL;
137 _nl_auto_rtnl_route
struct rtnl_route *tmpl = NULL;
138 struct nl_object *obj;
141 tmpl = rtnl_route_alloc();
142 rtnl_route_set_dst(tmpl, dst);
143 err = build_route_msg(tmpl, RTM_GETROUTE, 0, &msg);
151 if ((err =
nl_pickup(sk, route_msg_parser, &obj)) < 0)
154 *result = (
struct rtnl_route *)obj;
159int rtnl_route_add(
struct nl_sock *sk,
struct rtnl_route *route,
int flags)
164 if ((err = rtnl_route_build_add_request(route, flags, &msg)) < 0)
172 return wait_for_ack(sk);
175int rtnl_route_build_del_request(
struct rtnl_route *tmpl,
int flags,
176 struct nl_msg **result)
178 return build_route_msg(tmpl, RTM_DELROUTE, flags, result);
181int rtnl_route_delete(
struct nl_sock *sk,
struct rtnl_route *route,
int flags)
186 if ((err = rtnl_route_build_del_request(route, flags, &msg)) < 0)
194 return wait_for_ack(sk);
199static struct nl_af_group route_groups[] = {
200 { AF_INET, RTNLGRP_IPV4_ROUTE },
201 { AF_INET6, RTNLGRP_IPV6_ROUTE },
202 { AF_MPLS, RTNLGRP_MPLS_ROUTE },
203 { AF_DECnet, RTNLGRP_DECnet_ROUTE },
204 { END_OF_GROUP_LIST },
207static struct nl_cache_ops rtnl_route_ops = {
208 .co_name =
"route/route",
209 .co_hdrsize =
sizeof(
struct rtmsg),
211 { RTM_NEWROUTE, NL_ACT_NEW,
"new" },
212 { RTM_DELROUTE, NL_ACT_DEL,
"del" },
213 { RTM_GETROUTE, NL_ACT_GET,
"get" },
214 END_OF_MSGTYPES_LIST,
216 .co_protocol = NETLINK_ROUTE,
217 .co_groups = route_groups,
218 .co_request_update = route_request_update,
219 .co_msg_parser = route_msg_parser,
220 .co_obj_ops = &route_obj_ops,
223static void _nl_init route_init(
void)
228static void _nl_exit route_exit(
void)
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
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.
#define ROUTE_CACHE_CONTENT
When passed to rtnl_route_alloc_cache() the cache will correspond to the contents of the routing cach...
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message.
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
int nl_pickup(struct nl_sock *sk, int(*parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *), struct nl_object **result)
Pickup netlink answer, parse is and return object.
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.