summaryrefslogtreecommitdiff
path: root/lib/rtnetlink.c
blob: d0c1ee7c5e0503a047da69d76c05413ebcaef98f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (c) 2009, 2010, 2013, 2015 Nicira, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

#include "rtnetlink.h"

#include <sys/socket.h>
#include <linux/rtnetlink.h>
#include <net/if.h>

#include "netlink.h"
#include "netlink-notifier.h"
#include "ofpbuf.h"

static struct nln *nln = NULL;
static struct rtnetlink_change rtn_change;

/* Returns true if the given netlink msg type corresponds to RTNLGRP_LINK. */
bool
rtnetlink_type_is_rtnlgrp_link(uint16_t type)
{
    return type == RTM_NEWLINK || type == RTM_DELLINK;
}

/* Returns true if the given netlink msg type corresponds to
 * RTNLGRP_IPV4_IFADDR or RTNLGRP_IPV6_IFADDR. */
bool
rtnetlink_type_is_rtnlgrp_addr(uint16_t type)
{
    return type == RTM_NEWADDR || type == RTM_DELADDR;
}

/* Parses a rtnetlink message 'buf' into 'change'.  If 'buf' is unparseable,
 * leaves 'change' untouched and returns false.  Otherwise, populates 'change'
 * and returns true. */
bool
rtnetlink_parse(struct ofpbuf *buf, struct rtnetlink_change *change)
{
    const struct nlmsghdr *nlmsg = buf->data;
    bool parsed = false;

    if (rtnetlink_type_is_rtnlgrp_link(nlmsg->nlmsg_type)) {
        /* Policy for RTNLGRP_LINK messages.
         *
         * There are *many* more fields in these messages, but currently we
         * only care about these fields. */
        static const struct nl_policy policy[] = {
            [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
            [IFLA_MASTER] = { .type = NL_A_U32,    .optional = true },
            [IFLA_MTU]    = { .type = NL_A_U32,    .optional = true },
            [IFLA_ADDRESS] = { .type = NL_A_UNSPEC, .optional = true },
        };

        struct nlattr *attrs[ARRAY_SIZE(policy)];

        parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
                                 policy, attrs, ARRAY_SIZE(policy));

        if (parsed) {
            const struct ifinfomsg *ifinfo;

            ifinfo = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *ifinfo);

            change->nlmsg_type     = nlmsg->nlmsg_type;
            change->if_index       = ifinfo->ifi_index;
            change->ifname         = nl_attr_get_string(attrs[IFLA_IFNAME]);
            change->ifi_flags      = ifinfo->ifi_flags;
            change->master_ifindex = (attrs[IFLA_MASTER]
                                      ? nl_attr_get_u32(attrs[IFLA_MASTER])
                                      : 0);
            change->mtu            = (attrs[IFLA_MTU]
                                      ? nl_attr_get_u32(attrs[IFLA_MTU])
                                      : 0);

            if (attrs[IFLA_ADDRESS] &&
                nl_attr_get_size(attrs[IFLA_ADDRESS]) == ETH_ADDR_LEN) {
                memcpy(&change->mac, nl_attr_get(attrs[IFLA_ADDRESS]),
                       ETH_ADDR_LEN);
            } else {
                memset(&change->mac, 0, ETH_ADDR_LEN);
            }
        }
    } else if (rtnetlink_type_is_rtnlgrp_addr(nlmsg->nlmsg_type)) {
        /* Policy for RTNLGRP_IPV4_IFADDR/RTNLGRP_IPV6_IFADDR messages.
         *
         * There are *many* more fields in these messages, but currently we
         * only care about these fields. */
        static const struct nl_policy policy[] = {
            [IFA_LABEL] = { .type = NL_A_STRING, .optional = false },
        };

        struct nlattr *attrs[ARRAY_SIZE(policy)];

        parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifaddrmsg),
                                 policy, attrs, ARRAY_SIZE(policy));

        if (parsed) {
            const struct ifaddrmsg *ifaddr;

            ifaddr = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *ifaddr);

            change->nlmsg_type     = nlmsg->nlmsg_type;
            change->if_index       = ifaddr->ifa_index;
            change->ifname         = nl_attr_get_string(attrs[IFA_LABEL]);
        }
    }

    return parsed;
}

static bool
rtnetlink_parse_cb(struct ofpbuf *buf, void *change)
{
    return rtnetlink_parse(buf, change);
}

/* Registers 'cb' to be called with auxiliary data 'aux' with network device
 * change notifications.  The notifier is stored in 'notifier', which the
 * caller must not modify or free.
 *
 * This is probably not the function that you want.  You should probably be
 * using dpif_port_poll() or netdev_change_seq(), which unlike this function
 * are not Linux-specific.
 *
 * xxx Joins more multicast groups when needed.
 *
 * Returns an initialized nln_notifier if successful, NULL otherwise. */
struct nln_notifier *
rtnetlink_notifier_create(rtnetlink_notify_func *cb, void *aux)
{
    if (!nln) {
        nln = nln_create(NETLINK_ROUTE, RTNLGRP_LINK, rtnetlink_parse_cb,
                         &rtn_change);
    }

    return nln_notifier_create(nln, (nln_notify_func *) cb, aux);
}

/* Destroys 'notifier', which must have previously been created with
 * rtnetlink_notifier_register(). */
void
rtnetlink_notifier_destroy(struct nln_notifier *notifier)
{
    nln_notifier_destroy(notifier);
}

/* Calls all of the registered notifiers, passing along any as-yet-unreported
 * netdev change events. */
void
rtnetlink_run(void)
{
    if (nln) {
        nln_run(nln);
    }
}

/* Causes poll_block() to wake up when network device change notifications are
 * ready. */
void
rtnetlink_wait(void)
{
    if (nln) {
        nln_wait(nln);
    }
}