2 * Copyright (c) 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "rtnetlink-route.h"
21 #include <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <linux/rtnetlink.h>
28 #include "rtnetlink.h"
30 static struct rtnetlink *rtn = NULL;
31 static struct rtnetlink_route_change rtn_change;
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. */
37 rtnetlink_route_parse(struct ofpbuf *buf,
38 struct rtnetlink_route_change *change)
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 },
47 static struct nlattr *attrs[ARRAY_SIZE(policy)];
49 parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
50 policy, attrs, ARRAY_SIZE(policy));
53 const struct nlmsghdr *nlmsg;
54 const struct rtmsg *rtm;
57 rtm = (const struct rtmsg *) ((const char *) buf->data + NLMSG_HDRLEN);
59 if (rtm->rtm_family != AF_INET) {
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]))
74 /* Registers 'cb' to be called with auxiliary data 'aux' with route change
75 * notifications. The notifier is stored in 'notifier', which callers must
78 * Returns 0 if successful, otherwise a positive errno value. */
80 rtnetlink_route_notifier_register(struct rtnetlink_notifier *notifier,
81 rtnetlink_route_notify_func *cb, void *aux)
83 rtnetlink_parse_func *pf = (rtnetlink_parse_func *) rtnetlink_route_parse;
84 rtnetlink_notify_func *nf = (rtnetlink_notify_func *) cb;
87 rtn = rtnetlink_create(RTNLGRP_IPV4_ROUTE, pf, &rtn_change);
90 return rtnetlink_notifier_register(rtn, notifier, nf, aux);
93 /* Cancels notification on 'notifier', which must have previously been
94 * registered with rtnetlink_route_notifier_register(). */
96 rtnetlink_route_notifier_unregister(struct rtnetlink_notifier *notifier)
98 rtnetlink_notifier_unregister(rtn, notifier);
101 /* Calls all of the registered notifiers, passing along any as-yet-unreported
102 * address change events. */
104 rtnetlink_route_notifier_run(void)
107 rtnetlink_notifier_run(rtn);
111 /* Causes poll_block() to wake up when address change notifications are ready.
114 rtnetlink_route_notifier_wait(void)
117 rtnetlink_notifier_wait(rtn);