blob: 4beebb73d007625fee953ffb4ac675f1a80646a0 [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>
Mike J. Chenec16b9d2011-06-23 14:55:28 -070026#include <linux/if.h>
JP Abgralle6f80142011-07-14 16:46:32 -070027#include <linux/netfilter/nfnetlink.h>
28#include <linux/netfilter_ipv4/ipt_ULOG.h>
29/* From kernel's net/netfilter/xt_quota2.c */
30const int QLOG_NL_EVENT = 112;
31
32#include <linux/netlink.h>
33#include <linux/rtnetlink.h>
Mike J. Chenec16b9d2011-06-23 14:55:28 -070034
San Mehat168415b2009-05-06 11:14:21 -070035const int NetlinkEvent::NlActionUnknown = 0;
36const int NetlinkEvent::NlActionAdd = 1;
37const int NetlinkEvent::NlActionRemove = 2;
38const int NetlinkEvent::NlActionChange = 3;
Mike J. Chenec16b9d2011-06-23 14:55:28 -070039const int NetlinkEvent::NlActionLinkUp = 4;
40const int NetlinkEvent::NlActionLinkDown = 5;
San Mehat168415b2009-05-06 11:14:21 -070041
42NetlinkEvent::NetlinkEvent() {
43 mAction = NlActionUnknown;
San Mehatebfe3db2009-10-10 17:35:13 -070044 memset(mParams, 0, sizeof(mParams));
45 mPath = NULL;
46 mSubsystem = NULL;
San Mehat168415b2009-05-06 11:14:21 -070047}
48
49NetlinkEvent::~NetlinkEvent() {
50 int i;
51 if (mPath)
52 free(mPath);
53 if (mSubsystem)
54 free(mSubsystem);
55 for (i = 0; i < NL_PARAMS_MAX; i++) {
56 if (!mParams[i])
57 break;
58 free(mParams[i]);
59 }
60}
61
San Mehatd6744132009-12-24 07:17:09 -080062void NetlinkEvent::dump() {
63 int i;
64
65 for (i = 0; i < NL_PARAMS_MAX; i++) {
66 if (!mParams[i])
67 break;
San Mehat7e8529a2010-03-25 09:31:42 -070068 SLOGD("NL param '%s'\n", mParams[i]);
San Mehatd6744132009-12-24 07:17:09 -080069 }
70}
71
Mike J. Chenec16b9d2011-06-23 14:55:28 -070072/*
JP Abgrallb982bce2012-04-26 23:52:58 -070073 * Parse an binary message from a NETLINK_ROUTE netlink socket.
Mike J. Chenec16b9d2011-06-23 14:55:28 -070074 */
75bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) {
76 size_t sz = size;
Mike J. Chen17260b12011-06-23 15:00:30 -070077 const struct nlmsghdr *nh = (struct nlmsghdr *) buffer;
Mike J. Chenec16b9d2011-06-23 14:55:28 -070078
79 while (NLMSG_OK(nh, sz) && (nh->nlmsg_type != NLMSG_DONE)) {
JP Abgralle6f80142011-07-14 16:46:32 -070080
Mike J. Chenec16b9d2011-06-23 14:55:28 -070081 if (nh->nlmsg_type == RTM_NEWLINK) {
82 int len = nh->nlmsg_len - sizeof(*nh);
83 struct ifinfomsg *ifi;
84
JP Abgralle6f80142011-07-14 16:46:32 -070085 if (sizeof(*ifi) > (size_t) len) {
86 SLOGE("Got a short RTM_NEWLINK message\n");
87 continue;
Mike J. Chenec16b9d2011-06-23 14:55:28 -070088 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -070089
JP Abgralle6f80142011-07-14 16:46:32 -070090 ifi = (ifinfomsg *)NLMSG_DATA(nh);
91 if ((ifi->ifi_flags & IFF_LOOPBACK) != 0) {
92 continue;
93 }
94
95 struct rtattr *rta = (struct rtattr *)
96 ((char *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
97 len = NLMSG_PAYLOAD(nh, sizeof(*ifi));
98
99 while(RTA_OK(rta, len)) {
100 switch(rta->rta_type) {
101 case IFLA_IFNAME:
102 char buffer[16 + IFNAMSIZ];
103 snprintf(buffer, sizeof(buffer), "INTERFACE=%s",
104 (char *) RTA_DATA(rta));
105 mParams[0] = strdup(buffer);
106 mAction = (ifi->ifi_flags & IFF_LOWER_UP) ?
107 NlActionLinkUp : NlActionLinkDown;
108 mSubsystem = strdup("net");
109 break;
110 }
111
112 rta = RTA_NEXT(rta, len);
113 }
114
115 } else if (nh->nlmsg_type == QLOG_NL_EVENT) {
116 char *devname;
117 ulog_packet_msg_t *pm;
118 size_t len = nh->nlmsg_len - sizeof(*nh);
119 if (sizeof(*pm) > len) {
120 SLOGE("Got a short QLOG message\n");
121 continue;
122 }
123 pm = (ulog_packet_msg_t *)NLMSG_DATA(nh);
124 devname = pm->indev_name[0] ? pm->indev_name : pm->outdev_name;
JP Abgralle6f80142011-07-14 16:46:32 -0700125 asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix);
126 asprintf(&mParams[1], "INTERFACE=%s", devname);
127 mSubsystem = strdup("qlog");
128 mAction = NlActionChange;
129
130 } else {
131 SLOGD("Unexpected netlink message. type=0x%x\n", nh->nlmsg_type);
132 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700133 nh = NLMSG_NEXT(nh, size);
134 }
135
136 return true;
137}
138
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100139/* If the string between 'str' and 'end' begins with 'prefixlen' characters
140 * from the 'prefix' array, then return 'str + prefixlen', otherwise return
141 * NULL.
142 */
143static const char*
144has_prefix(const char* str, const char* end, const char* prefix, size_t prefixlen)
145{
146 if ((end-str) >= (ptrdiff_t)prefixlen && !memcmp(str, prefix, prefixlen))
147 return str + prefixlen;
148 else
149 return NULL;
150}
151
152/* Same as strlen(x) for constant string literals ONLY */
153#define CONST_STRLEN(x) (sizeof(x)-1)
154
155/* Convenience macro to call has_prefix with a constant string literal */
156#define HAS_CONST_PREFIX(str,end,prefix) has_prefix((str),(end),prefix,CONST_STRLEN(prefix))
157
158
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700159/*
160 * Parse an ASCII-formatted message from a NETLINK_KOBJECT_UEVENT
161 * netlink socket.
162 */
163bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) {
Mike J. Chen17260b12011-06-23 15:00:30 -0700164 const char *s = buffer;
165 const char *end;
San Mehat168415b2009-05-06 11:14:21 -0700166 int param_idx = 0;
167 int i;
168 int first = 1;
169
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100170 if (size == 0)
171 return false;
172
173 /* Ensure the buffer is zero-terminated, the code below depends on this */
174 buffer[size-1] = '\0';
175
San Mehat168415b2009-05-06 11:14:21 -0700176 end = s + size;
177 while (s < end) {
178 if (first) {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100179 const char *p;
180 /* buffer is 0-terminated, no need to check p < end */
181 for (p = s; *p != '@'; p++) {
182 if (!*p) { /* no '@', should not happen */
183 return false;
184 }
185 }
186 mPath = strdup(p+1);
San Mehat168415b2009-05-06 11:14:21 -0700187 first = 0;
188 } else {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100189 const char* a;
190 if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) {
San Mehat168415b2009-05-06 11:14:21 -0700191 if (!strcmp(a, "add"))
192 mAction = NlActionAdd;
193 else if (!strcmp(a, "remove"))
194 mAction = NlActionRemove;
195 else if (!strcmp(a, "change"))
196 mAction = NlActionChange;
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100197 } else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != NULL) {
198 mSeq = atoi(a);
199 } else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != NULL) {
200 mSubsystem = strdup(a);
201 } else if (param_idx < NL_PARAMS_MAX) {
San Mehat168415b2009-05-06 11:14:21 -0700202 mParams[param_idx++] = strdup(s);
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100203 }
San Mehat168415b2009-05-06 11:14:21 -0700204 }
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100205 s += strlen(s) + 1;
San Mehat168415b2009-05-06 11:14:21 -0700206 }
207 return true;
208}
209
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700210bool NetlinkEvent::decode(char *buffer, int size, int format) {
Mike J. Chen17260b12011-06-23 15:00:30 -0700211 if (format == NetlinkListener::NETLINK_FORMAT_BINARY) {
212 return parseBinaryNetlinkMessage(buffer, size);
213 } else {
214 return parseAsciiNetlinkMessage(buffer, size);
215 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700216}
217
San Mehat168415b2009-05-06 11:14:21 -0700218const char *NetlinkEvent::findParam(const char *paramName) {
Chih-Wei Huang80ec37a2010-07-14 14:00:41 +0800219 size_t len = strlen(paramName);
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100220 for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != NULL; ++i) {
Chih-Wei Huang80ec37a2010-07-14 14:00:41 +0800221 const char *ptr = mParams[i] + len;
222 if (!strncmp(mParams[i], paramName, len) && *ptr == '=')
223 return ++ptr;
San Mehat168415b2009-05-06 11:14:21 -0700224 }
225
San Mehat7e8529a2010-03-25 09:31:42 -0700226 SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName);
San Mehat168415b2009-05-06 11:14:21 -0700227 return NULL;
228}