libnl  3.2.7
route.c
1 /*
2  * lib/route/route.c Routes
3  *
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
7  * of the License.
8  *
9  * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup rtnl
14  * @defgroup route Routing
15  * @brief
16  * @{
17  */
18 
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>
27 
28 static struct nl_cache_ops rtnl_route_ops;
29 
30 static 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 
45 static 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  * @note The caller is responsible for destroying and freeing the
73  * cache after using it.
74  * @return 0 on success or a negative error code.
75  */
76 int rtnl_route_alloc_cache(struct nl_sock *sk, int family, int flags,
77  struct nl_cache **result)
78 {
79  struct nl_cache *cache;
80  int err;
81 
82  if (!(cache = nl_cache_alloc(&rtnl_route_ops)))
83  return -NLE_NOMEM;
84 
85  cache->c_iarg1 = family;
86  cache->c_iarg2 = flags;
87 
88  if (sk && (err = nl_cache_refill(sk, cache)) < 0) {
89  free(cache);
90  return err;
91  }
92 
93  *result = cache;
94  return 0;
95 }
96 
97 /** @} */
98 
99 /**
100  * @name Route Addition
101  * @{
102  */
103 
104 static int build_route_msg(struct rtnl_route *tmpl, int cmd, int flags,
105  struct nl_msg **result)
106 {
107  struct nl_msg *msg;
108  int err;
109 
110  if (!(msg = nlmsg_alloc_simple(cmd, flags)))
111  return -NLE_NOMEM;
112 
113  if ((err = rtnl_route_build_msg(msg, tmpl)) < 0) {
114  nlmsg_free(msg);
115  return err;
116  }
117 
118  *result = msg;
119  return 0;
120 }
121 
122 int rtnl_route_build_add_request(struct rtnl_route *tmpl, int flags,
123  struct nl_msg **result)
124 {
125  return build_route_msg(tmpl, RTM_NEWROUTE, NLM_F_CREATE | flags,
126  result);
127 }
128 
129 int rtnl_route_add(struct nl_sock *sk, struct rtnl_route *route, int flags)
130 {
131  struct nl_msg *msg;
132  int err;
133 
134  if ((err = rtnl_route_build_add_request(route, flags, &msg)) < 0)
135  return err;
136 
137  err = nl_send_auto_complete(sk, msg);
138  nlmsg_free(msg);
139  if (err < 0)
140  return err;
141 
142  return wait_for_ack(sk);
143 }
144 
145 int rtnl_route_build_del_request(struct rtnl_route *tmpl, int flags,
146  struct nl_msg **result)
147 {
148  return build_route_msg(tmpl, RTM_DELROUTE, flags, result);
149 }
150 
151 int rtnl_route_delete(struct nl_sock *sk, struct rtnl_route *route, int flags)
152 {
153  struct nl_msg *msg;
154  int err;
155 
156  if ((err = rtnl_route_build_del_request(route, flags, &msg)) < 0)
157  return err;
158 
159  err = nl_send_auto_complete(sk, msg);
160  nlmsg_free(msg);
161  if (err < 0)
162  return err;
163 
164  return wait_for_ack(sk);
165 }
166 
167 /** @} */
168 
169 static struct nl_af_group route_groups[] = {
170  { AF_INET, RTNLGRP_IPV4_ROUTE },
171  { AF_INET6, RTNLGRP_IPV6_ROUTE },
172  { AF_DECnet, RTNLGRP_DECnet_ROUTE },
173  { END_OF_GROUP_LIST },
174 };
175 
176 static struct nl_cache_ops rtnl_route_ops = {
177  .co_name = "route/route",
178  .co_hdrsize = sizeof(struct rtmsg),
179  .co_msgtypes = {
180  { RTM_NEWROUTE, NL_ACT_NEW, "new" },
181  { RTM_DELROUTE, NL_ACT_DEL, "del" },
182  { RTM_GETROUTE, NL_ACT_GET, "get" },
183  END_OF_MSGTYPES_LIST,
184  },
185  .co_protocol = NETLINK_ROUTE,
186  .co_groups = route_groups,
187  .co_request_update = route_request_update,
188  .co_msg_parser = route_msg_parser,
189  .co_obj_ops = &route_obj_ops,
190 };
191 
192 static void __init route_init(void)
193 {
194  nl_cache_mngt_register(&rtnl_route_ops);
195 }
196 
197 static void __exit route_exit(void)
198 {
199  nl_cache_mngt_unregister(&rtnl_route_ops);
200 }
201 
202 /** @} */