blob: b6da72f4cff4a42088f9ef220d82ed74d9758e0d [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;
San Mehat168415b2009-05-06 11:14:21 -070045
46NetlinkEvent::NetlinkEvent() {
47 mAction = NlActionUnknown;
San Mehatebfe3db2009-10-10 17:35:13 -070048 memset(mParams, 0, sizeof(mParams));
49 mPath = NULL;
50 mSubsystem = NULL;
San Mehat168415b2009-05-06 11:14:21 -070051}
52
53NetlinkEvent::~NetlinkEvent() {
54 int i;
55 if (mPath)
56 free(mPath);
57 if (mSubsystem)
58 free(mSubsystem);
59 for (i = 0; i < NL_PARAMS_MAX; i++) {
60 if (!mParams[i])
61 break;
62 free(mParams[i]);
63 }
64}
65
San Mehatd6744132009-12-24 07:17:09 -080066void NetlinkEvent::dump() {
67 int i;
68
69 for (i = 0; i < NL_PARAMS_MAX; i++) {
70 if (!mParams[i])
71 break;
San Mehat7e8529a2010-03-25 09:31:42 -070072 SLOGD("NL param '%s'\n", mParams[i]);
San Mehatd6744132009-12-24 07:17:09 -080073 }
74}
75
Mike J. Chenec16b9d2011-06-23 14:55:28 -070076/*
Lorenzo Colitti381f70f2013-08-02 05:58:37 +090077 * Decode a RTM_NEWADDR or RTM_DELADDR message.
78 */
79bool NetlinkEvent::parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr,
80 int rtasize) {
81 struct rtattr *rta = IFA_RTA(ifaddr);
82 struct ifa_cacheinfo *cacheinfo = NULL;
83 char addrstr[INET6_ADDRSTRLEN] = "";
84
85 // Sanity check.
86 if (type != RTM_NEWADDR && type != RTM_DELADDR) {
87 SLOGE("parseIfAddrMessage on incorrect message type 0x%x\n", type);
88 return false;
89 }
90
91 // For log messages.
92 const char *msgtype = (type == RTM_NEWADDR) ? "RTM_NEWADDR" : "RTM_DELADDR";
93
94 while(RTA_OK(rta, rtasize)) {
95 if (rta->rta_type == IFA_ADDRESS) {
96 // Only look at the first address, because we only support notifying
97 // one change at a time.
98 if (*addrstr != '\0') {
99 SLOGE("Multiple IFA_ADDRESSes in %s, ignoring\n", msgtype);
100 continue;
101 }
102
103 // Convert the IP address to a string.
104 if (ifaddr->ifa_family == AF_INET) {
105 struct in_addr *addr4 = (struct in_addr *) RTA_DATA(rta);
106 if (RTA_PAYLOAD(rta) < sizeof(*addr4)) {
107 SLOGE("Short IPv4 address (%d bytes) in %s",
108 RTA_PAYLOAD(rta), msgtype);
109 continue;
110 }
111 inet_ntop(AF_INET, addr4, addrstr, sizeof(addrstr));
112 } else if (ifaddr->ifa_family == AF_INET6) {
113 struct in6_addr *addr6 = (struct in6_addr *) RTA_DATA(rta);
114 if (RTA_PAYLOAD(rta) < sizeof(*addr6)) {
115 SLOGE("Short IPv6 address (%d bytes) in %s",
116 RTA_PAYLOAD(rta), msgtype);
117 continue;
118 }
119 inet_ntop(AF_INET6, addr6, addrstr, sizeof(addrstr));
120 } else {
121 SLOGE("Unknown address family %d\n", ifaddr->ifa_family);
122 continue;
123 }
124
125 // Find the interface name.
126 char ifname[IFNAMSIZ + 1];
127 if (!if_indextoname(ifaddr->ifa_index, ifname)) {
128 SLOGE("Unknown ifindex %d in %s", ifaddr->ifa_index, msgtype);
129 return false;
130 }
131
132 // Fill in interface information.
133 mAction = (type == RTM_NEWADDR) ? NlActionAdd : NlActionRemove;
134 mSubsystem = strdup("address");
135 asprintf(&mParams[0], "ADDRESS=%s/%d", addrstr,
136 ifaddr->ifa_prefixlen);
137 asprintf(&mParams[1], "IFACE=%s", ifname);
138 asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags);
139 asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope);
140 } else if (rta->rta_type == IFA_CACHEINFO) {
141 // Address lifetime information.
142 if (cacheinfo) {
143 // We only support one address.
144 SLOGE("Multiple IFA_CACHEINFOs in %s, ignoring\n", msgtype);
145 continue;
146 }
147
148 if (RTA_PAYLOAD(rta) < sizeof(*cacheinfo)) {
149 SLOGE("Short IFA_CACHEINFO (%d vs. %d bytes) in %s",
150 RTA_PAYLOAD(rta), sizeof(cacheinfo), msgtype);
151 continue;
152 }
153
154 cacheinfo = (struct ifa_cacheinfo *) RTA_DATA(rta);
155 asprintf(&mParams[4], "PREFERRED=%u", cacheinfo->ifa_prefered);
156 asprintf(&mParams[5], "VALID=%u", cacheinfo->ifa_valid);
157 asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp);
158 asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp);
159 }
160
161 rta = RTA_NEXT(rta, rtasize);
162 }
163
164 if (addrstr[0] == '\0') {
165 SLOGE("No IFA_ADDRESS in %s\n", msgtype);
166 return false;
167 }
168
169 return true;
170}
171
172/*
JP Abgrallb982bce2012-04-26 23:52:58 -0700173 * Parse an binary message from a NETLINK_ROUTE netlink socket.
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700174 */
175bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) {
176 size_t sz = size;
Mike J. Chen17260b12011-06-23 15:00:30 -0700177 const struct nlmsghdr *nh = (struct nlmsghdr *) buffer;
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700178
179 while (NLMSG_OK(nh, sz) && (nh->nlmsg_type != NLMSG_DONE)) {
JP Abgralle6f80142011-07-14 16:46:32 -0700180
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700181 if (nh->nlmsg_type == RTM_NEWLINK) {
182 int len = nh->nlmsg_len - sizeof(*nh);
183 struct ifinfomsg *ifi;
184
JP Abgralle6f80142011-07-14 16:46:32 -0700185 if (sizeof(*ifi) > (size_t) len) {
186 SLOGE("Got a short RTM_NEWLINK message\n");
187 continue;
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700188 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700189
JP Abgralle6f80142011-07-14 16:46:32 -0700190 ifi = (ifinfomsg *)NLMSG_DATA(nh);
191 if ((ifi->ifi_flags & IFF_LOOPBACK) != 0) {
192 continue;
193 }
194
195 struct rtattr *rta = (struct rtattr *)
196 ((char *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
197 len = NLMSG_PAYLOAD(nh, sizeof(*ifi));
198
199 while(RTA_OK(rta, len)) {
200 switch(rta->rta_type) {
201 case IFLA_IFNAME:
202 char buffer[16 + IFNAMSIZ];
203 snprintf(buffer, sizeof(buffer), "INTERFACE=%s",
204 (char *) RTA_DATA(rta));
205 mParams[0] = strdup(buffer);
206 mAction = (ifi->ifi_flags & IFF_LOWER_UP) ?
207 NlActionLinkUp : NlActionLinkDown;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900208 mSubsystem = strdup("interface");
JP Abgralle6f80142011-07-14 16:46:32 -0700209 break;
210 }
211
212 rta = RTA_NEXT(rta, len);
213 }
214
215 } else if (nh->nlmsg_type == QLOG_NL_EVENT) {
216 char *devname;
217 ulog_packet_msg_t *pm;
218 size_t len = nh->nlmsg_len - sizeof(*nh);
219 if (sizeof(*pm) > len) {
220 SLOGE("Got a short QLOG message\n");
221 continue;
222 }
223 pm = (ulog_packet_msg_t *)NLMSG_DATA(nh);
224 devname = pm->indev_name[0] ? pm->indev_name : pm->outdev_name;
JP Abgralle6f80142011-07-14 16:46:32 -0700225 asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix);
226 asprintf(&mParams[1], "INTERFACE=%s", devname);
227 mSubsystem = strdup("qlog");
228 mAction = NlActionChange;
229
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900230 } else if (nh->nlmsg_type == RTM_NEWADDR ||
231 nh->nlmsg_type == RTM_DELADDR) {
232 int len = nh->nlmsg_len - sizeof(*nh);
233 struct ifaddrmsg *ifa;
234
235 if (sizeof(*ifa) > (size_t) len) {
236 SLOGE("Got a short RTM_xxxADDR message\n");
237 continue;
238 }
239
240 ifa = (ifaddrmsg *)NLMSG_DATA(nh);
241 size_t rtasize = IFA_PAYLOAD(nh);
242 if (!parseIfAddrMessage(nh->nlmsg_type, ifa, rtasize)) {
243 continue;
244 }
JP Abgralle6f80142011-07-14 16:46:32 -0700245 } else {
246 SLOGD("Unexpected netlink message. type=0x%x\n", nh->nlmsg_type);
247 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700248 nh = NLMSG_NEXT(nh, size);
249 }
250
251 return true;
252}
253
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100254/* If the string between 'str' and 'end' begins with 'prefixlen' characters
255 * from the 'prefix' array, then return 'str + prefixlen', otherwise return
256 * NULL.
257 */
258static const char*
259has_prefix(const char* str, const char* end, const char* prefix, size_t prefixlen)
260{
261 if ((end-str) >= (ptrdiff_t)prefixlen && !memcmp(str, prefix, prefixlen))
262 return str + prefixlen;
263 else
264 return NULL;
265}
266
267/* Same as strlen(x) for constant string literals ONLY */
268#define CONST_STRLEN(x) (sizeof(x)-1)
269
270/* Convenience macro to call has_prefix with a constant string literal */
271#define HAS_CONST_PREFIX(str,end,prefix) has_prefix((str),(end),prefix,CONST_STRLEN(prefix))
272
273
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700274/*
275 * Parse an ASCII-formatted message from a NETLINK_KOBJECT_UEVENT
276 * netlink socket.
277 */
278bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) {
Mike J. Chen17260b12011-06-23 15:00:30 -0700279 const char *s = buffer;
280 const char *end;
San Mehat168415b2009-05-06 11:14:21 -0700281 int param_idx = 0;
282 int i;
283 int first = 1;
284
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100285 if (size == 0)
286 return false;
287
288 /* Ensure the buffer is zero-terminated, the code below depends on this */
289 buffer[size-1] = '\0';
290
San Mehat168415b2009-05-06 11:14:21 -0700291 end = s + size;
292 while (s < end) {
293 if (first) {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100294 const char *p;
295 /* buffer is 0-terminated, no need to check p < end */
296 for (p = s; *p != '@'; p++) {
297 if (!*p) { /* no '@', should not happen */
298 return false;
299 }
300 }
301 mPath = strdup(p+1);
San Mehat168415b2009-05-06 11:14:21 -0700302 first = 0;
303 } else {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100304 const char* a;
305 if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) {
San Mehat168415b2009-05-06 11:14:21 -0700306 if (!strcmp(a, "add"))
307 mAction = NlActionAdd;
308 else if (!strcmp(a, "remove"))
309 mAction = NlActionRemove;
310 else if (!strcmp(a, "change"))
311 mAction = NlActionChange;
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100312 } else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != NULL) {
313 mSeq = atoi(a);
314 } else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != NULL) {
315 mSubsystem = strdup(a);
316 } else if (param_idx < NL_PARAMS_MAX) {
San Mehat168415b2009-05-06 11:14:21 -0700317 mParams[param_idx++] = strdup(s);
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100318 }
San Mehat168415b2009-05-06 11:14:21 -0700319 }
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100320 s += strlen(s) + 1;
San Mehat168415b2009-05-06 11:14:21 -0700321 }
322 return true;
323}
324
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700325bool NetlinkEvent::decode(char *buffer, int size, int format) {
Mike J. Chen17260b12011-06-23 15:00:30 -0700326 if (format == NetlinkListener::NETLINK_FORMAT_BINARY) {
327 return parseBinaryNetlinkMessage(buffer, size);
328 } else {
329 return parseAsciiNetlinkMessage(buffer, size);
330 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700331}
332
San Mehat168415b2009-05-06 11:14:21 -0700333const char *NetlinkEvent::findParam(const char *paramName) {
Chih-Wei Huang80ec37a2010-07-14 14:00:41 +0800334 size_t len = strlen(paramName);
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100335 for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != NULL; ++i) {
Chih-Wei Huang80ec37a2010-07-14 14:00:41 +0800336 const char *ptr = mParams[i] + len;
337 if (!strncmp(mParams[i], paramName, len) && *ptr == '=')
338 return ++ptr;
San Mehat168415b2009-05-06 11:14:21 -0700339 }
340
San Mehat7e8529a2010-03-25 09:31:42 -0700341 SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName);
San Mehat168415b2009-05-06 11:14:21 -0700342 return NULL;
343}