libnl 3.9.0
route.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @ingroup rtnl
8 * @defgroup route Routing
9 * @brief
10 * @{
11 */
12
13#include "nl-default.h"
14
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>
22
23#include "nl-route.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"
27
28static struct nl_cache_ops rtnl_route_ops;
29
30static int route_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
31 struct nlmsghdr *nlh, struct nl_parser_param *pp)
32{
33 struct rtnl_route *route;
34 int err;
35
36 if ((err = rtnl_route_parse(nlh, &route)) < 0)
37 return err;
38
39 err = pp->pp_cb((struct nl_object *) route, pp);
40
41 rtnl_route_put(route);
42 return err;
43}
44
45static int route_request_update(struct nl_cache *c, struct nl_sock *h)
46{
47 struct rtmsg rhdr = {
48 .rtm_family = c->c_iarg1,
49 };
50
51 if (c->c_iarg2 & ROUTE_CACHE_CONTENT)
52 rhdr.rtm_flags |= RTM_F_CLONED;
53
54 return nl_send_simple(h, RTM_GETROUTE, NLM_F_DUMP, &rhdr, sizeof(rhdr));
55}
56
57/**
58 * @name Cache Management
59 * @{
60 */
61
62/**
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
66 * @arg flags Flags
67 * @arg result Result pointer
68 *
69 * Allocates a new cache, initializes it properly and updates it to
70 * contain all routes currently configured in the kernel.
71 *
72 * Valid flags:
73 * * ROUTE_CACHE_CONTENT - Cache will contain contents of routing cache
74 * instead of actual routes.
75 *
76 * @note The caller is responsible for destroying and freeing the
77 * cache after using it.
78 * @return 0 on success or a negative error code.
79 */
80int rtnl_route_alloc_cache(struct nl_sock *sk, int family, int flags,
81 struct nl_cache **result)
82{
83 struct nl_cache *cache;
84 int err;
85
86 if (!(cache = nl_cache_alloc(&rtnl_route_ops)))
87 return -NLE_NOMEM;
88
89 cache->c_iarg1 = family;
90 cache->c_iarg2 = flags;
91
92 if (sk && (err = nl_cache_refill(sk, cache)) < 0) {
93 free(cache);
94 return err;
95 }
96
97 *result = cache;
98 return 0;
99}
100
101/** @} */
102
103/**
104 * @name Route Addition
105 * @{
106 */
107
108static int build_route_msg(struct rtnl_route *tmpl, int cmd, int flags,
109 struct nl_msg **result)
110{
111 struct nl_msg *msg;
112 int err;
113
114 if (!(msg = nlmsg_alloc_simple(cmd, flags)))
115 return -NLE_NOMEM;
116
117 if ((err = rtnl_route_build_msg(msg, tmpl)) < 0) {
118 nlmsg_free(msg);
119 return err;
120 }
121
122 *result = msg;
123 return 0;
124}
125
126int rtnl_route_build_add_request(struct rtnl_route *tmpl, int flags,
127 struct nl_msg **result)
128{
129 return build_route_msg(tmpl, RTM_NEWROUTE, NLM_F_CREATE | flags,
130 result);
131}
132
133int rtnl_route_lookup(struct nl_sock *sk, struct nl_addr *dst,
134 struct rtnl_route **result)
135{
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;
139 int err;
140
141 tmpl = rtnl_route_alloc();
142 rtnl_route_set_dst(tmpl, dst);
143 err = build_route_msg(tmpl, RTM_GETROUTE, 0, &msg);
144 if (err < 0)
145 return err;
146
147 err = nl_send_auto(sk, msg);
148 if (err < 0)
149 return err;
150
151 if ((err = nl_pickup(sk, route_msg_parser, &obj)) < 0)
152 return err;
153
154 *result = (struct rtnl_route *)obj;
155 wait_for_ack(sk);
156 return 0;
157}
158
159int rtnl_route_add(struct nl_sock *sk, struct rtnl_route *route, int flags)
160{
161 struct nl_msg *msg;
162 int err;
163
164 if ((err = rtnl_route_build_add_request(route, flags, &msg)) < 0)
165 return err;
166
167 err = nl_send_auto_complete(sk, msg);
168 nlmsg_free(msg);
169 if (err < 0)
170 return err;
171
172 return wait_for_ack(sk);
173}
174
175int rtnl_route_build_del_request(struct rtnl_route *tmpl, int flags,
176 struct nl_msg **result)
177{
178 return build_route_msg(tmpl, RTM_DELROUTE, flags, result);
179}
180
181int rtnl_route_delete(struct nl_sock *sk, struct rtnl_route *route, int flags)
182{
183 struct nl_msg *msg;
184 int err;
185
186 if ((err = rtnl_route_build_del_request(route, flags, &msg)) < 0)
187 return err;
188
189 err = nl_send_auto_complete(sk, msg);
190 nlmsg_free(msg);
191 if (err < 0)
192 return err;
193
194 return wait_for_ack(sk);
195}
196
197/** @} */
198
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 },
205};
206
207static struct nl_cache_ops rtnl_route_ops = {
208 .co_name = "route/route",
209 .co_hdrsize = sizeof(struct rtmsg),
210 .co_msgtypes = {
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,
215 },
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,
221};
222
223static void _nl_init route_init(void)
224{
225 nl_cache_mngt_register(&rtnl_route_ops);
226}
227
228static void _nl_exit route_exit(void)
229{
230 nl_cache_mngt_unregister(&rtnl_route_ops);
231}
232
233/** @} */
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Definition cache_mngt.c:287
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
Definition cache_mngt.c:252
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
Definition cache.c:1041
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.
Definition cache.c:184
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition msg.c:349
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition msg.c:566
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.
Definition route.c:80
#define ROUTE_CACHE_CONTENT
When passed to rtnl_route_alloc_cache() the cache will correspond to the contents of the routing cach...
Definition route.h:27
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message.
Definition nl.c:515
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition nl.c:1246
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.
Definition nl.c:1177
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
Definition nl.c:579