netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / rtnetlink.c
1 /*
2  * Copyright (c) 2009, 2010, 2013, 2015 Nicira, Inc.
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.h"
20
21 #include <sys/socket.h>
22 #include <linux/rtnetlink.h>
23 #include <net/if.h>
24
25 #include "netlink.h"
26 #include "netlink-notifier.h"
27 #include "ofpbuf.h"
28
29 static struct nln *nln = NULL;
30 static struct rtnetlink_change rtn_change;
31
32 /* Returns true if the given netlink msg type corresponds to RTNLGRP_LINK. */
33 bool
34 rtnetlink_type_is_rtnlgrp_link(uint16_t type)
35 {
36     return type == RTM_NEWLINK || type == RTM_DELLINK;
37 }
38
39 /* Returns true if the given netlink msg type corresponds to
40  * RTNLGRP_IPV4_IFADDR or RTNLGRP_IPV6_IFADDR. */
41 bool
42 rtnetlink_type_is_rtnlgrp_addr(uint16_t type)
43 {
44     return type == RTM_NEWADDR || type == RTM_DELADDR;
45 }
46
47 /* Parses a rtnetlink message 'buf' into 'change'.  If 'buf' is unparseable,
48  * leaves 'change' untouched and returns false.  Otherwise, populates 'change'
49  * and returns true. */
50 bool
51 rtnetlink_parse(struct ofpbuf *buf, struct rtnetlink_change *change)
52 {
53     const struct nlmsghdr *nlmsg = buf->data;
54     bool parsed = false;
55
56     if (rtnetlink_type_is_rtnlgrp_link(nlmsg->nlmsg_type)) {
57         /* Policy for RTNLGRP_LINK messages.
58          *
59          * There are *many* more fields in these messages, but currently we
60          * only care about these fields. */
61         static const struct nl_policy policy[] = {
62             [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
63             [IFLA_MASTER] = { .type = NL_A_U32,    .optional = true },
64             [IFLA_MTU]    = { .type = NL_A_U32,    .optional = true },
65             [IFLA_ADDRESS] = { .type = NL_A_UNSPEC, .optional = true },
66         };
67
68         struct nlattr *attrs[ARRAY_SIZE(policy)];
69
70         parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
71                                  policy, attrs, ARRAY_SIZE(policy));
72
73         if (parsed) {
74             const struct ifinfomsg *ifinfo;
75
76             ifinfo = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *ifinfo);
77
78             change->nlmsg_type     = nlmsg->nlmsg_type;
79             change->if_index       = ifinfo->ifi_index;
80             change->ifname         = nl_attr_get_string(attrs[IFLA_IFNAME]);
81             change->ifi_flags      = ifinfo->ifi_flags;
82             change->master_ifindex = (attrs[IFLA_MASTER]
83                                       ? nl_attr_get_u32(attrs[IFLA_MASTER])
84                                       : 0);
85             change->mtu            = (attrs[IFLA_MTU]
86                                       ? nl_attr_get_u32(attrs[IFLA_MTU])
87                                       : 0);
88
89             if (attrs[IFLA_ADDRESS] &&
90                 nl_attr_get_size(attrs[IFLA_ADDRESS]) == ETH_ADDR_LEN) {
91                 memcpy(&change->mac, nl_attr_get(attrs[IFLA_ADDRESS]),
92                        ETH_ADDR_LEN);
93             } else {
94                 memset(&change->mac, 0, ETH_ADDR_LEN);
95             }
96         }
97     } else if (rtnetlink_type_is_rtnlgrp_addr(nlmsg->nlmsg_type)) {
98         /* Policy for RTNLGRP_IPV4_IFADDR/RTNLGRP_IPV6_IFADDR messages.
99          *
100          * There are *many* more fields in these messages, but currently we
101          * only care about these fields. */
102         static const struct nl_policy policy[] = {
103             [IFA_LABEL] = { .type = NL_A_STRING, .optional = false },
104         };
105
106         struct nlattr *attrs[ARRAY_SIZE(policy)];
107
108         parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifaddrmsg),
109                                  policy, attrs, ARRAY_SIZE(policy));
110
111         if (parsed) {
112             const struct ifaddrmsg *ifaddr;
113
114             ifaddr = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *ifaddr);
115
116             change->nlmsg_type     = nlmsg->nlmsg_type;
117             change->if_index       = ifaddr->ifa_index;
118             change->ifname         = nl_attr_get_string(attrs[IFA_LABEL]);
119         }
120     }
121
122     return parsed;
123 }
124
125 static bool
126 rtnetlink_parse_cb(struct ofpbuf *buf, void *change)
127 {
128     return rtnetlink_parse(buf, change);
129 }
130
131 /* Registers 'cb' to be called with auxiliary data 'aux' with network device
132  * change notifications.  The notifier is stored in 'notifier', which the
133  * caller must not modify or free.
134  *
135  * This is probably not the function that you want.  You should probably be
136  * using dpif_port_poll() or netdev_change_seq(), which unlike this function
137  * are not Linux-specific.
138  *
139  * xxx Joins more multicast groups when needed.
140  *
141  * Returns an initialized nln_notifier if successful, NULL otherwise. */
142 struct nln_notifier *
143 rtnetlink_notifier_create(rtnetlink_notify_func *cb, void *aux)
144 {
145     if (!nln) {
146         nln = nln_create(NETLINK_ROUTE, RTNLGRP_LINK, rtnetlink_parse_cb,
147                          &rtn_change);
148     }
149
150     return nln_notifier_create(nln, (nln_notify_func *) cb, aux);
151 }
152
153 /* Destroys 'notifier', which must have previously been created with
154  * rtnetlink_notifier_register(). */
155 void
156 rtnetlink_notifier_destroy(struct nln_notifier *notifier)
157 {
158     nln_notifier_destroy(notifier);
159 }
160
161 /* Calls all of the registered notifiers, passing along any as-yet-unreported
162  * netdev change events. */
163 void
164 rtnetlink_run(void)
165 {
166     if (nln) {
167         nln_run(nln);
168     }
169 }
170
171 /* Causes poll_block() to wake up when network device change notifications are
172  * ready. */
173 void
174 rtnetlink_wait(void)
175 {
176     if (nln) {
177         nln_wait(nln);
178     }
179 }