blob: aae2ae7318771b8f4cf1343d04a5bacc47f782cb [file] [log] [blame]
San Mehat168415b2009-05-06 11:14:21 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
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#include <stdlib.h>
San Mehat3d407292009-05-07 08:49:30 -070017#include <string.h>
San Mehat168415b2009-05-06 11:14:21 -070018
19#define LOG_TAG "NetlinkEvent"
20#include <cutils/log.h>
21
22#include <sysutils/NetlinkEvent.h>
23
Mike J. Chenec16b9d2011-06-23 14:55:28 -070024#include <sys/types.h>
25#include <sys/socket.h>
Lorenzo Colitti381f70f2013-08-02 05:58:37 +090026#include <netinet/in.h>
27#include <arpa/inet.h>
28#include <net/if.h>
29
Mike J. Chenec16b9d2011-06-23 14:55:28 -070030#include <linux/if.h>
JP Abgralle6f80142011-07-14 16:46:32 -070031#include <linux/netfilter/nfnetlink.h>
32#include <linux/netfilter_ipv4/ipt_ULOG.h>
33/* From kernel's net/netfilter/xt_quota2.c */
34const int QLOG_NL_EVENT = 112;
35
36#include <linux/netlink.h>
37#include <linux/rtnetlink.h>
Mike J. Chenec16b9d2011-06-23 14:55:28 -070038
San Mehat168415b2009-05-06 11:14:21 -070039const int NetlinkEvent::NlActionUnknown = 0;
40const int NetlinkEvent::NlActionAdd = 1;
41const int NetlinkEvent::NlActionRemove = 2;
42const int NetlinkEvent::NlActionChange = 3;
Mike J. Chenec16b9d2011-06-23 14:55:28 -070043const int NetlinkEvent::NlActionLinkUp = 4;
44const int NetlinkEvent::NlActionLinkDown = 5;
Lorenzo Colitti526b8382013-09-03 00:25:14 +090045const int NetlinkEvent::NlActionAddressUpdated = 6;
46const int NetlinkEvent::NlActionAddressRemoved = 7;
San Mehat168415b2009-05-06 11:14:21 -070047
48NetlinkEvent::NetlinkEvent() {
49 mAction = NlActionUnknown;
San Mehatebfe3db2009-10-10 17:35:13 -070050 memset(mParams, 0, sizeof(mParams));
51 mPath = NULL;
52 mSubsystem = NULL;
San Mehat168415b2009-05-06 11:14:21 -070053}
54
55NetlinkEvent::~NetlinkEvent() {
56 int i;
57 if (mPath)
58 free(mPath);
59 if (mSubsystem)
60 free(mSubsystem);
61 for (i = 0; i < NL_PARAMS_MAX; i++) {
62 if (!mParams[i])
63 break;
64 free(mParams[i]);
65 }
66}
67
San Mehatd6744132009-12-24 07:17:09 -080068void NetlinkEvent::dump() {
69 int i;
70
71 for (i = 0; i < NL_PARAMS_MAX; i++) {
72 if (!mParams[i])
73 break;
San Mehat7e8529a2010-03-25 09:31:42 -070074 SLOGD("NL param '%s'\n", mParams[i]);
San Mehatd6744132009-12-24 07:17:09 -080075 }
76}
77
Mike J. Chenec16b9d2011-06-23 14:55:28 -070078/*
Lorenzo Colitti381f70f2013-08-02 05:58:37 +090079 * Decode a RTM_NEWADDR or RTM_DELADDR message.
80 */
81bool NetlinkEvent::parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr,
82 int rtasize) {
Lorenzo Colitti96834562013-08-17 03:40:31 +090083 struct rtattr *rta;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +090084 struct ifa_cacheinfo *cacheinfo = NULL;
85 char addrstr[INET6_ADDRSTRLEN] = "";
86
87 // Sanity check.
88 if (type != RTM_NEWADDR && type != RTM_DELADDR) {
89 SLOGE("parseIfAddrMessage on incorrect message type 0x%x\n", type);
90 return false;
91 }
92
93 // For log messages.
94 const char *msgtype = (type == RTM_NEWADDR) ? "RTM_NEWADDR" : "RTM_DELADDR";
95
Lorenzo Colitti96834562013-08-17 03:40:31 +090096 for (rta = IFA_RTA(ifaddr); RTA_OK(rta, rtasize);
97 rta = RTA_NEXT(rta, rtasize)) {
Lorenzo Colitti381f70f2013-08-02 05:58:37 +090098 if (rta->rta_type == IFA_ADDRESS) {
99 // Only look at the first address, because we only support notifying
100 // one change at a time.
101 if (*addrstr != '\0') {
102 SLOGE("Multiple IFA_ADDRESSes in %s, ignoring\n", msgtype);
103 continue;
104 }
105
106 // Convert the IP address to a string.
107 if (ifaddr->ifa_family == AF_INET) {
108 struct in_addr *addr4 = (struct in_addr *) RTA_DATA(rta);
109 if (RTA_PAYLOAD(rta) < sizeof(*addr4)) {
110 SLOGE("Short IPv4 address (%d bytes) in %s",
111 RTA_PAYLOAD(rta), msgtype);
112 continue;
113 }
114 inet_ntop(AF_INET, addr4, addrstr, sizeof(addrstr));
115 } else if (ifaddr->ifa_family == AF_INET6) {
116 struct in6_addr *addr6 = (struct in6_addr *) RTA_DATA(rta);
117 if (RTA_PAYLOAD(rta) < sizeof(*addr6)) {
118 SLOGE("Short IPv6 address (%d bytes) in %s",
119 RTA_PAYLOAD(rta), msgtype);
120 continue;
121 }
122 inet_ntop(AF_INET6, addr6, addrstr, sizeof(addrstr));
123 } else {
124 SLOGE("Unknown address family %d\n", ifaddr->ifa_family);
125 continue;
126 }
127
128 // Find the interface name.
129 char ifname[IFNAMSIZ + 1];
130 if (!if_indextoname(ifaddr->ifa_index, ifname)) {
131 SLOGE("Unknown ifindex %d in %s", ifaddr->ifa_index, msgtype);
132 return false;
133 }
134
135 // Fill in interface information.
Lorenzo Colitti526b8382013-09-03 00:25:14 +0900136 mAction = (type == RTM_NEWADDR) ? NlActionAddressUpdated :
137 NlActionAddressRemoved;
138 mSubsystem = strdup("net");
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900139 asprintf(&mParams[0], "ADDRESS=%s/%d", addrstr,
140 ifaddr->ifa_prefixlen);
Lorenzo Colitti526b8382013-09-03 00:25:14 +0900141 asprintf(&mParams[1], "INTERFACE=%s", ifname);
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900142 asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags);
143 asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope);
144 } else if (rta->rta_type == IFA_CACHEINFO) {
145 // Address lifetime information.
146 if (cacheinfo) {
147 // We only support one address.
148 SLOGE("Multiple IFA_CACHEINFOs in %s, ignoring\n", msgtype);
149 continue;
150 }
151
152 if (RTA_PAYLOAD(rta) < sizeof(*cacheinfo)) {
153 SLOGE("Short IFA_CACHEINFO (%d vs. %d bytes) in %s",
154 RTA_PAYLOAD(rta), sizeof(cacheinfo), msgtype);
155 continue;
156 }
157
158 cacheinfo = (struct ifa_cacheinfo *) RTA_DATA(rta);
159 asprintf(&mParams[4], "PREFERRED=%u", cacheinfo->ifa_prefered);
160 asprintf(&mParams[5], "VALID=%u", cacheinfo->ifa_valid);
161 asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp);
162 asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp);
163 }
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900164 }
165
166 if (addrstr[0] == '\0') {
167 SLOGE("No IFA_ADDRESS in %s\n", msgtype);
168 return false;
169 }
170
171 return true;
172}
173
174/*
JP Abgrallb982bce2012-04-26 23:52:58 -0700175 * Parse an binary message from a NETLINK_ROUTE netlink socket.
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700176 */
177bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) {
Lorenzo Colitti96834562013-08-17 03:40:31 +0900178 const struct nlmsghdr *nh;
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700179
Lorenzo Colitti96834562013-08-17 03:40:31 +0900180 for (nh = (struct nlmsghdr *) buffer;
181 NLMSG_OK(nh, size) && (nh->nlmsg_type != NLMSG_DONE);
182 nh = NLMSG_NEXT(nh, size)) {
JP Abgralle6f80142011-07-14 16:46:32 -0700183
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700184 if (nh->nlmsg_type == RTM_NEWLINK) {
185 int len = nh->nlmsg_len - sizeof(*nh);
186 struct ifinfomsg *ifi;
187
JP Abgralle6f80142011-07-14 16:46:32 -0700188 if (sizeof(*ifi) > (size_t) len) {
189 SLOGE("Got a short RTM_NEWLINK message\n");
190 continue;
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700191 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700192
JP Abgralle6f80142011-07-14 16:46:32 -0700193 ifi = (ifinfomsg *)NLMSG_DATA(nh);
194 if ((ifi->ifi_flags & IFF_LOOPBACK) != 0) {
195 continue;
196 }
197
198 struct rtattr *rta = (struct rtattr *)
199 ((char *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
200 len = NLMSG_PAYLOAD(nh, sizeof(*ifi));
201
202 while(RTA_OK(rta, len)) {
203 switch(rta->rta_type) {
204 case IFLA_IFNAME:
205 char buffer[16 + IFNAMSIZ];
206 snprintf(buffer, sizeof(buffer), "INTERFACE=%s",
207 (char *) RTA_DATA(rta));
208 mParams[0] = strdup(buffer);
209 mAction = (ifi->ifi_flags & IFF_LOWER_UP) ?
210 NlActionLinkUp : NlActionLinkDown;
Lorenzo Colitti526b8382013-09-03 00:25:14 +0900211 mSubsystem = strdup("net");
JP Abgralle6f80142011-07-14 16:46:32 -0700212 break;
213 }
214
215 rta = RTA_NEXT(rta, len);
216 }
217
218 } else if (nh->nlmsg_type == QLOG_NL_EVENT) {
219 char *devname;
220 ulog_packet_msg_t *pm;
221 size_t len = nh->nlmsg_len - sizeof(*nh);
222 if (sizeof(*pm) > len) {
223 SLOGE("Got a short QLOG message\n");
224 continue;
225 }
226 pm = (ulog_packet_msg_t *)NLMSG_DATA(nh);
227 devname = pm->indev_name[0] ? pm->indev_name : pm->outdev_name;
JP Abgralle6f80142011-07-14 16:46:32 -0700228 asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix);
229 asprintf(&mParams[1], "INTERFACE=%s", devname);
230 mSubsystem = strdup("qlog");
231 mAction = NlActionChange;
232
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900233 } else if (nh->nlmsg_type == RTM_NEWADDR ||
234 nh->nlmsg_type == RTM_DELADDR) {
235 int len = nh->nlmsg_len - sizeof(*nh);
236 struct ifaddrmsg *ifa;
237
238 if (sizeof(*ifa) > (size_t) len) {
239 SLOGE("Got a short RTM_xxxADDR message\n");
240 continue;
241 }
242
243 ifa = (ifaddrmsg *)NLMSG_DATA(nh);
244 size_t rtasize = IFA_PAYLOAD(nh);
245 if (!parseIfAddrMessage(nh->nlmsg_type, ifa, rtasize)) {
246 continue;
247 }
JP Abgralle6f80142011-07-14 16:46:32 -0700248 } else {
249 SLOGD("Unexpected netlink message. type=0x%x\n", nh->nlmsg_type);
250 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700251 }
252
253 return true;
254}
255
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100256/* If the string between 'str' and 'end' begins with 'prefixlen' characters
257 * from the 'prefix' array, then return 'str + prefixlen', otherwise return
258 * NULL.
259 */
260static const char*
261has_prefix(const char* str, const char* end, const char* prefix, size_t prefixlen)
262{
263 if ((end-str) >= (ptrdiff_t)prefixlen && !memcmp(str, prefix, prefixlen))
264 return str + prefixlen;
265 else
266 return NULL;
267}
268
269/* Same as strlen(x) for constant string literals ONLY */
270#define CONST_STRLEN(x) (sizeof(x)-1)
271
272/* Convenience macro to call has_prefix with a constant string literal */
273#define HAS_CONST_PREFIX(str,end,prefix) has_prefix((str),(end),prefix,CONST_STRLEN(prefix))
274
275
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700276/*
277 * Parse an ASCII-formatted message from a NETLINK_KOBJECT_UEVENT
278 * netlink socket.
279 */
280bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) {
Mike J. Chen17260b12011-06-23 15:00:30 -0700281 const char *s = buffer;
282 const char *end;
San Mehat168415b2009-05-06 11:14:21 -0700283 int param_idx = 0;
284 int i;
285 int first = 1;
286
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100287 if (size == 0)
288 return false;
289
290 /* Ensure the buffer is zero-terminated, the code below depends on this */
291 buffer[size-1] = '\0';
292
San Mehat168415b2009-05-06 11:14:21 -0700293 end = s + size;
294 while (s < end) {
295 if (first) {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100296 const char *p;
297 /* buffer is 0-terminated, no need to check p < end */
298 for (p = s; *p != '@'; p++) {
299 if (!*p) { /* no '@', should not happen */
300 return false;
301 }
302 }
303 mPath = strdup(p+1);
San Mehat168415b2009-05-06 11:14:21 -0700304 first = 0;
305 } else {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100306 const char* a;
307 if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) {
San Mehat168415b2009-05-06 11:14:21 -0700308 if (!strcmp(a, "add"))
309 mAction = NlActionAdd;
310 else if (!strcmp(a, "remove"))
311 mAction = NlActionRemove;
312 else if (!strcmp(a, "change"))
313 mAction = NlActionChange;
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100314 } else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != NULL) {
315 mSeq = atoi(a);
316 } else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != NULL) {
317 mSubsystem = strdup(a);
318 } else if (param_idx < NL_PARAMS_MAX) {
San Mehat168415b2009-05-06 11:14:21 -0700319 mParams[param_idx++] = strdup(s);
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100320 }
San Mehat168415b2009-05-06 11:14:21 -0700321 }
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100322 s += strlen(s) + 1;
San Mehat168415b2009-05-06 11:14:21 -0700323 }
324 return true;
325}
326
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700327bool NetlinkEvent::decode(char *buffer, int size, int format) {
Mike J. Chen17260b12011-06-23 15:00:30 -0700328 if (format == NetlinkListener::NETLINK_FORMAT_BINARY) {
329 return parseBinaryNetlinkMessage(buffer, size);
330 } else {
331 return parseAsciiNetlinkMessage(buffer, size);
332 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700333}
334
San Mehat168415b2009-05-06 11:14:21 -0700335const char *NetlinkEvent::findParam(const char *paramName) {
Chih-Wei Huang80ec37a2010-07-14 14:00:41 +0800336 size_t len = strlen(paramName);
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100337 for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != NULL; ++i) {
Chih-Wei Huang80ec37a2010-07-14 14:00:41 +0800338 const char *ptr = mParams[i] + len;
339 if (!strncmp(mParams[i], paramName, len) && *ptr == '=')
340 return ++ptr;
San Mehat168415b2009-05-06 11:14:21 -0700341 }
342
San Mehat7e8529a2010-03-25 09:31:42 -0700343 SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName);
San Mehat168415b2009-05-06 11:14:21 -0700344 return NULL;
345}