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 <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>
22 
23 static struct nl_cache_ops rtnl_route_ops;
24 
route_msg_parser(struct nl_cache_ops * ops,struct sockaddr_nl * who,struct nlmsghdr * nlh,struct nl_parser_param * pp)25 static int route_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
26 			    struct nlmsghdr *nlh, struct nl_parser_param *pp)
27 {
28 	struct rtnl_route *route;
29 	int err;
30 
31 	if ((err = rtnl_route_parse(nlh, &route)) < 0)
32 		return err;
33 
34 	err = pp->pp_cb((struct nl_object *) route, pp);
35 
36 	rtnl_route_put(route);
37 	return err;
38 }
39 
route_request_update(struct nl_cache * c,struct nl_sock * h)40 static int route_request_update(struct nl_cache *c, struct nl_sock *h)
41 {
42 	struct rtmsg rhdr = {
43 		.rtm_family = c->c_iarg1,
44 	};
45 
46 	if (c->c_iarg2 & ROUTE_CACHE_CONTENT)
47 		rhdr.rtm_flags |= RTM_F_CLONED;
48 
49 	return nl_send_simple(h, RTM_GETROUTE, NLM_F_DUMP, &rhdr, sizeof(rhdr));
50 }
51 
52 /**
53  * @name Cache Management
54  * @{
55  */
56 
57 /**
58  * Build a route cache holding all routes currently configured in the kernel
59  * @arg sk		Netlink socket.
60  * @arg family		Address family of routes to cover or AF_UNSPEC
61  * @arg flags		Flags
62  * @arg result		Result pointer
63  *
64  * Allocates a new cache, initializes it properly and updates it to
65  * contain all routes currently configured in the kernel.
66  *
67  * Valid flags:
68  *   * ROUTE_CACHE_CONTENT - Cache will contain contents of routing cache
69  *                           instead of actual routes.
70  *
71  * @note The caller is responsible for destroying and freeing the
72  *       cache after using it.
73  * @return 0 on success or a negative error code.
74  */
rtnl_route_alloc_cache(struct nl_sock * sk,int family,int flags,struct nl_cache ** result)75 int rtnl_route_alloc_cache(struct nl_sock *sk, int family, int flags,
76 			   struct nl_cache **result)
77 {
78 	struct nl_cache *cache;
79 	int err;
80 
81 	if (!(cache = nl_cache_alloc(&rtnl_route_ops)))
82 		return -NLE_NOMEM;
83 
84 	cache->c_iarg1 = family;
85 	cache->c_iarg2 = flags;
86 
87 	if (sk && (err = nl_cache_refill(sk, cache)) < 0) {
88 		free(cache);
89 		return err;
90 	}
91 
92 	*result = cache;
93 	return 0;
94 }
95 
96 /** @} */
97 
98 /**
99  * @name Route Addition
100  * @{
101  */
102 
build_route_msg(struct rtnl_route * tmpl,int cmd,int flags,struct nl_msg ** result)103 static int build_route_msg(struct rtnl_route *tmpl, int cmd, int flags,
104 			   struct nl_msg **result)
105 {
106 	struct nl_msg *msg;
107 	int err;
108 
109 	if (!(msg = nlmsg_alloc_simple(cmd, flags)))
110 		return -NLE_NOMEM;
111 
112 	if ((err = rtnl_route_build_msg(msg, tmpl)) < 0) {
113 		nlmsg_free(msg);
114 		return err;
115 	}
116 
117 	*result = msg;
118 	return 0;
119 }
120 
rtnl_route_build_add_request(struct rtnl_route * tmpl,int flags,struct nl_msg ** result)121 int rtnl_route_build_add_request(struct rtnl_route *tmpl, int flags,
122 				 struct nl_msg **result)
123 {
124 	return build_route_msg(tmpl, RTM_NEWROUTE, NLM_F_CREATE | flags,
125 			       result);
126 }
127 
rtnl_route_lookup(struct nl_sock * sk,struct nl_addr * dst,struct rtnl_route ** result)128 int rtnl_route_lookup(struct nl_sock *sk, struct nl_addr *dst,
129                       struct rtnl_route **result)
130 {
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;
134 	int err;
135 
136 	tmpl = rtnl_route_alloc();
137 	rtnl_route_set_dst(tmpl, dst);
138 	err = build_route_msg(tmpl, RTM_GETROUTE, 0, &msg);
139 	if (err < 0)
140 		return err;
141 
142 	err = nl_send_auto(sk, msg);
143 	if (err < 0)
144 		return err;
145 
146 	if ((err = nl_pickup(sk, route_msg_parser, &obj)) < 0)
147 		return err;
148 
149 	*result = (struct rtnl_route *)obj;
150 	wait_for_ack(sk);
151 	return 0;
152 }
153 
rtnl_route_add(struct nl_sock * sk,struct rtnl_route * route,int flags)154 int rtnl_route_add(struct nl_sock *sk, struct rtnl_route *route, int flags)
155 {
156 	struct nl_msg *msg;
157 	int err;
158 
159 	if ((err = rtnl_route_build_add_request(route, flags, &msg)) < 0)
160 		return err;
161 
162 	err = nl_send_auto_complete(sk, msg);
163 	nlmsg_free(msg);
164 	if (err < 0)
165 		return err;
166 
167 	return wait_for_ack(sk);
168 }
169 
rtnl_route_build_del_request(struct rtnl_route * tmpl,int flags,struct nl_msg ** result)170 int rtnl_route_build_del_request(struct rtnl_route *tmpl, int flags,
171 				 struct nl_msg **result)
172 {
173 	return build_route_msg(tmpl, RTM_DELROUTE, flags, result);
174 }
175 
rtnl_route_delete(struct nl_sock * sk,struct rtnl_route * route,int flags)176 int rtnl_route_delete(struct nl_sock *sk, struct rtnl_route *route, int flags)
177 {
178 	struct nl_msg *msg;
179 	int err;
180 
181 	if ((err = rtnl_route_build_del_request(route, flags, &msg)) < 0)
182 		return err;
183 
184 	err = nl_send_auto_complete(sk, msg);
185 	nlmsg_free(msg);
186 	if (err < 0)
187 		return err;
188 
189 	return wait_for_ack(sk);
190 }
191 
192 /** @} */
193 
194 static 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 },
200 };
201 
202 static struct nl_cache_ops rtnl_route_ops = {
203 	.co_name		= "route/route",
204 	.co_hdrsize		= sizeof(struct rtmsg),
205 	.co_msgtypes		= {
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,
210 				  },
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,
216 };
217 
route_init(void)218 static void __init route_init(void)
219 {
220 	nl_cache_mngt_register(&rtnl_route_ops);
221 }
222 
route_exit(void)223 static void __exit route_exit(void)
224 {
225 	nl_cache_mngt_unregister(&rtnl_route_ops);
226 }
227 
228 /** @} */
229