2 * lib/route/route.c Routes
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
14 * @defgroup route Routing
19 #include <netlink-local.h>
20 #include <netlink/netlink.h>
21 #include <netlink/cache.h>
22 #include <netlink/utils.h>
23 #include <netlink/data.h>
24 #include <netlink/route/rtnl.h>
25 #include <netlink/route/route.h>
26 #include <netlink/route/link.h>
28 static struct nl_cache_ops rtnl_route_ops;
30 static 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);
45 static int route_request_update(struct nl_cache *c, struct nl_sock *h)
48 .rtm_family = c->c_iarg1,
51 if (c->c_iarg2 & ROUTE_CACHE_CONTENT)
52 rhdr.rtm_flags |= RTM_F_CLONED;
54 return nl_send_simple(h, RTM_GETROUTE, NLM_F_DUMP, &rhdr, sizeof(rhdr));
58 * @name Cache Management
63 * Build a route cache holding all routes currently configured in the kernel
64 * @arg sk Netlink socket.
65 * @arg family Address family of routes to cover or AF_UNSPEC
68 * Allocates a new cache, initializes it properly and updates it to
69 * contain all routes currently configured in the kernel.
71 * @note The caller is responsible for destroying and freeing the
72 * cache after using it.
73 * @return The cache or NULL if an error has occured.
75 int rtnl_route_alloc_cache(struct nl_sock *sk, int family, int flags,
76 struct nl_cache **result)
78 struct nl_cache *cache;
81 if (!(cache = nl_cache_alloc(&rtnl_route_ops)))
84 cache->c_iarg1 = family;
85 cache->c_iarg2 = flags;
87 if (sk && (err = nl_cache_refill(sk, cache)) < 0) {
99 * @name Route Addition
103 static int build_route_msg(struct rtnl_route *tmpl, int cmd, int flags,
104 struct nl_msg **result)
109 if (!(msg = nlmsg_alloc_simple(cmd, flags)))
112 if ((err = rtnl_route_build_msg(msg, tmpl)) < 0) {
121 int 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,
128 int rtnl_route_add(struct nl_sock *sk, struct rtnl_route *route, int flags)
133 if ((err = rtnl_route_build_add_request(route, flags, &msg)) < 0)
136 err = nl_send_auto_complete(sk, msg);
141 return wait_for_ack(sk);
144 int rtnl_route_build_del_request(struct rtnl_route *tmpl, int flags,
145 struct nl_msg **result)
147 return build_route_msg(tmpl, RTM_DELROUTE, flags, result);
150 int rtnl_route_delete(struct nl_sock *sk, struct rtnl_route *route, int flags)
155 if ((err = rtnl_route_build_del_request(route, flags, &msg)) < 0)
158 err = nl_send_auto_complete(sk, msg);
163 return wait_for_ack(sk);
168 static struct nl_af_group route_groups[] = {
169 { AF_INET, RTNLGRP_IPV4_ROUTE },
170 { AF_INET6, RTNLGRP_IPV6_ROUTE },
171 { AF_DECnet, RTNLGRP_DECnet_ROUTE },
172 { END_OF_GROUP_LIST },
175 static struct nl_cache_ops rtnl_route_ops = {
176 .co_name = "route/route",
177 .co_hdrsize = sizeof(struct rtmsg),
179 { RTM_NEWROUTE, NL_ACT_NEW, "new" },
180 { RTM_DELROUTE, NL_ACT_DEL, "del" },
181 { RTM_GETROUTE, NL_ACT_GET, "get" },
182 END_OF_MSGTYPES_LIST,
184 .co_protocol = NETLINK_ROUTE,
185 .co_groups = route_groups,
186 .co_request_update = route_request_update,
187 .co_msg_parser = route_msg_parser,
188 .co_obj_ops = &route_obj_ops,
191 static void __init route_init(void)
193 nl_cache_mngt_register(&rtnl_route_ops);
196 static void __exit route_exit(void)
198 nl_cache_mngt_unregister(&rtnl_route_ops);