13#include <netlink-private/netlink.h>
14#include <netlink-private/nl-auto.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>
23static struct nl_cache_ops rtnl_route_ops;
25static int route_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
26 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
28 struct rtnl_route *route;
31 if ((err = rtnl_route_parse(nlh, &route)) < 0)
34 err = pp->pp_cb((
struct nl_object *) route, pp);
36 rtnl_route_put(route);
40static int route_request_update(
struct nl_cache *c,
struct nl_sock *h)
43 .rtm_family = c->c_iarg1,
47 rhdr.rtm_flags |= RTM_F_CLONED;
49 return nl_send_simple(h, RTM_GETROUTE, NLM_F_DUMP, &rhdr,
sizeof(rhdr));
76 struct nl_cache **result)
78 struct nl_cache *cache;
84 cache->c_iarg1 = family;
85 cache->c_iarg2 = flags;
103static int build_route_msg(
struct rtnl_route *tmpl,
int cmd,
int flags,
104 struct nl_msg **result)
112 if ((err = rtnl_route_build_msg(msg, tmpl)) < 0) {
121int rtnl_route_build_add_request(
struct rtnl_route *tmpl,
int flags,
122 struct nl_msg **result)
124 return build_route_msg(tmpl, RTM_NEWROUTE, NLM_F_CREATE | flags,
128int rtnl_route_lookup(
struct nl_sock *sk,
struct nl_addr *dst,
129 struct rtnl_route **result)
131 _nl_auto_nl_msg
struct nl_msg *msg = NULL;
132 _nl_auto_rtnl_route
struct rtnl_route *tmpl = NULL;
133 struct nl_object *obj;
136 tmpl = rtnl_route_alloc();
137 rtnl_route_set_dst(tmpl, dst);
138 err = build_route_msg(tmpl, RTM_GETROUTE, 0, &msg);
146 if ((err =
nl_pickup(sk, route_msg_parser, &obj)) < 0)
149 *result = (
struct rtnl_route *)obj;
154int rtnl_route_add(
struct nl_sock *sk,
struct rtnl_route *route,
int flags)
159 if ((err = rtnl_route_build_add_request(route, flags, &msg)) < 0)
167 return wait_for_ack(sk);
170int rtnl_route_build_del_request(
struct rtnl_route *tmpl,
int flags,
171 struct nl_msg **result)
173 return build_route_msg(tmpl, RTM_DELROUTE, flags, result);
176int rtnl_route_delete(
struct nl_sock *sk,
struct rtnl_route *route,
int flags)
181 if ((err = rtnl_route_build_del_request(route, flags, &msg)) < 0)
189 return wait_for_ack(sk);
194static struct nl_af_group route_groups[] = {
195 { AF_INET, RTNLGRP_IPV4_ROUTE },
196 { AF_INET6, RTNLGRP_IPV6_ROUTE },
197 { AF_MPLS, RTNLGRP_MPLS_ROUTE },
198 { AF_DECnet, RTNLGRP_DECnet_ROUTE },
199 { END_OF_GROUP_LIST },
202static struct nl_cache_ops rtnl_route_ops = {
203 .co_name =
"route/route",
204 .co_hdrsize =
sizeof(
struct rtmsg),
206 { RTM_NEWROUTE, NL_ACT_NEW,
"new" },
207 { RTM_DELROUTE, NL_ACT_DEL,
"del" },
208 { RTM_GETROUTE, NL_ACT_GET,
"get" },
209 END_OF_MSGTYPES_LIST,
211 .co_protocol = NETLINK_ROUTE,
212 .co_groups = route_groups,
213 .co_request_update = route_request_update,
214 .co_msg_parser = route_msg_parser,
215 .co_obj_ops = &route_obj_ops,
218static void __init route_init(
void)
223static void __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.