retnetlink: Add rtnetlink_destroy function.
[sliver-openvswitch.git] / lib / rtnetlink-route.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "rtnetlink-route.h"
20
21 #include <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <linux/rtnetlink.h>
24 #include <net/if.h>
25
26 #include "netlink.h"
27 #include "ofpbuf.h"
28 #include "rtnetlink.h"
29
30 static struct rtnetlink *rtn = NULL;
31 static struct rtnetlink_route_change rtn_change;
32
33 /* Parses a rtnetlink message 'buf' into 'change'.  If 'buf' is unparseable,
34  * leaves 'change' untouched and returns false.  Otherwise, populates 'change'
35  * and returns true. */
36 bool
37 rtnetlink_route_parse(struct ofpbuf *buf,
38                       struct rtnetlink_route_change *change)
39 {
40     bool parsed;
41
42     static const struct nl_policy policy[] = {
43         [RTA_DST] = { .type = NL_A_U32, .optional = true  },
44         [RTA_OIF] = { .type = NL_A_U32, .optional = false },
45     };
46
47     static struct nlattr *attrs[ARRAY_SIZE(policy)];
48
49     parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
50                              policy, attrs, ARRAY_SIZE(policy));
51
52     if (parsed) {
53         const struct nlmsghdr *nlmsg;
54         const struct rtmsg *rtm;
55
56         nlmsg = buf->data;
57         rtm = (const struct rtmsg *) ((const char *) buf->data + NLMSG_HDRLEN);
58
59         if (rtm->rtm_family != AF_INET) {
60             return false;
61         }
62
63         change->nlmsg_type  = nlmsg->nlmsg_type;
64         change->rtm_dst_len = rtm->rtm_dst_len;
65         change->rta_oif     = nl_attr_get_u32(attrs[RTA_OIF]);
66         change->rta_dst     = (attrs[RTA_DST]
67                                ? ntohl(nl_attr_get_be32(attrs[RTA_DST]))
68                                : 0);
69     }
70
71     return parsed;
72 }
73
74 /* Registers 'cb' to be called with auxiliary data 'aux' with route change
75  * notifications.  The notifier is stored in 'notifier', which callers must
76  * not modify or free.
77  *
78  * Returns 0 if successful, otherwise a positive errno value. */
79 int
80 rtnetlink_route_notifier_register(struct rtnetlink_notifier *notifier,
81                                  rtnetlink_route_notify_func *cb, void *aux)
82 {
83     rtnetlink_parse_func *pf = (rtnetlink_parse_func *) rtnetlink_route_parse;
84     rtnetlink_notify_func *nf = (rtnetlink_notify_func *) cb;
85
86     if (!rtn) {
87         rtn = rtnetlink_create(RTNLGRP_IPV4_ROUTE, pf, &rtn_change);
88     }
89
90     return rtnetlink_notifier_register(rtn, notifier, nf, aux);
91 }
92
93 /* Cancels notification on 'notifier', which must have previously been
94  * registered with rtnetlink_route_notifier_register(). */
95 void
96 rtnetlink_route_notifier_unregister(struct rtnetlink_notifier *notifier)
97 {
98     rtnetlink_notifier_unregister(rtn, notifier);
99 }
100
101 /* Calls all of the registered notifiers, passing along any as-yet-unreported
102  * address change events. */
103 void
104 rtnetlink_route_notifier_run(void)
105 {
106     if (rtn) {
107         rtnetlink_notifier_run(rtn);
108     }
109 }
110
111 /* Causes poll_block() to wake up when address change notifications are ready.
112  */
113 void
114 rtnetlink_route_notifier_wait(void)
115 {
116     if (rtn) {
117         rtnetlink_notifier_wait(rtn);
118     }
119 }