1 /*
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>
17 #include <string.h>
18
19 #define LOG_TAG "NetlinkEvent"
20 #include <cutils/log.h>
21
22 #include <sysutils/NetlinkEvent.h>
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <linux/if.h>
27 #include <linux/netfilter/nfnetlink.h>
28 #include <linux/netfilter_ipv4/ipt_ULOG.h>
29 /* From kernel's net/netfilter/xt_quota2.c */
30 const int QLOG_NL_EVENT = 112;
31
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
34
35 const int NetlinkEvent::NlActionUnknown = 0;
36 const int NetlinkEvent::NlActionAdd = 1;
37 const int NetlinkEvent::NlActionRemove = 2;
38 const int NetlinkEvent::NlActionChange = 3;
39 const int NetlinkEvent::NlActionLinkUp = 4;
40 const int NetlinkEvent::NlActionLinkDown = 5;
41
NetlinkEvent()42 NetlinkEvent::NetlinkEvent() {
43 mAction = NlActionUnknown;
44 memset(mParams, 0, sizeof(mParams));
45 mPath = NULL;
46 mSubsystem = NULL;
47 }
48
~NetlinkEvent()49 NetlinkEvent::~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
dump()62 void NetlinkEvent::dump() {
63 int i;
64
65 for (i = 0; i < NL_PARAMS_MAX; i++) {
66 if (!mParams[i])
67 break;
68 SLOGD("NL param '%s'\n", mParams[i]);
69 }
70 }
71
72 /*
73 * Parse an binary message from a NETLINK_ROUTE netlink socket.
74 */
parseBinaryNetlinkMessage(char * buffer,int size)75 bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) {
76 size_t sz = size;
77 const struct nlmsghdr *nh = (struct nlmsghdr *) buffer;
78
79 while (NLMSG_OK(nh, sz) && (nh->nlmsg_type != NLMSG_DONE)) {
80
81 if (nh->nlmsg_type == RTM_NEWLINK) {
82 int len = nh->nlmsg_len - sizeof(*nh);
83 struct ifinfomsg *ifi;
84
85 if (sizeof(*ifi) > (size_t) len) {
86 SLOGE("Got a short RTM_NEWLINK message\n");
87 continue;
88 }
89
90 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;
125 SLOGD("QLOG prefix=%s dev=%s\n", pm->prefix, devname);
126 asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix);
127 asprintf(&mParams[1], "INTERFACE=%s", devname);
128 mSubsystem = strdup("qlog");
129 mAction = NlActionChange;
130
131 } else {
132 SLOGD("Unexpected netlink message. type=0x%x\n", nh->nlmsg_type);
133 }
134 nh = NLMSG_NEXT(nh, size);
135 }
136
137 return true;
138 }
139
140 /* If the string between 'str' and 'end' begins with 'prefixlen' characters
141 * from the 'prefix' array, then return 'str + prefixlen', otherwise return
142 * NULL.
143 */
144 static const char*
has_prefix(const char * str,const char * end,const char * prefix,size_t prefixlen)145 has_prefix(const char* str, const char* end, const char* prefix, size_t prefixlen)
146 {
147 if ((end-str) >= (ptrdiff_t)prefixlen && !memcmp(str, prefix, prefixlen))
148 return str + prefixlen;
149 else
150 return NULL;
151 }
152
153 /* Same as strlen(x) for constant string literals ONLY */
154 #define CONST_STRLEN(x) (sizeof(x)-1)
155
156 /* Convenience macro to call has_prefix with a constant string literal */
157 #define HAS_CONST_PREFIX(str,end,prefix) has_prefix((str),(end),prefix,CONST_STRLEN(prefix))
158
159
160 /*
161 * Parse an ASCII-formatted message from a NETLINK_KOBJECT_UEVENT
162 * netlink socket.
163 */
parseAsciiNetlinkMessage(char * buffer,int size)164 bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) {
165 const char *s = buffer;
166 const char *end;
167 int param_idx = 0;
168 int i;
169 int first = 1;
170
171 if (size == 0)
172 return false;
173
174 /* Ensure the buffer is zero-terminated, the code below depends on this */
175 buffer[size-1] = '\0';
176
177 end = s + size;
178 while (s < end) {
179 if (first) {
180 const char *p;
181 /* buffer is 0-terminated, no need to check p < end */
182 for (p = s; *p != '@'; p++) {
183 if (!*p) { /* no '@', should not happen */
184 return false;
185 }
186 }
187 mPath = strdup(p+1);
188 first = 0;
189 } else {
190 const char* a;
191 if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) {
192 if (!strcmp(a, "add"))
193 mAction = NlActionAdd;
194 else if (!strcmp(a, "remove"))
195 mAction = NlActionRemove;
196 else if (!strcmp(a, "change"))
197 mAction = NlActionChange;
198 } else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != NULL) {
199 mSeq = atoi(a);
200 } else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != NULL) {
201 mSubsystem = strdup(a);
202 } else if (param_idx < NL_PARAMS_MAX) {
203 mParams[param_idx++] = strdup(s);
204 }
205 }
206 s += strlen(s) + 1;
207 }
208 return true;
209 }
210
decode(char * buffer,int size,int format)211 bool NetlinkEvent::decode(char *buffer, int size, int format) {
212 if (format == NetlinkListener::NETLINK_FORMAT_BINARY) {
213 return parseBinaryNetlinkMessage(buffer, size);
214 } else {
215 return parseAsciiNetlinkMessage(buffer, size);
216 }
217 }
218
findParam(const char * paramName)219 const char *NetlinkEvent::findParam(const char *paramName) {
220 size_t len = strlen(paramName);
221 for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != NULL; ++i) {
222 const char *ptr = mParams[i] + len;
223 if (!strncmp(mParams[i], paramName, len) && *ptr == '=')
224 return ++ptr;
225 }
226
227 SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName);
228 return NULL;
229 }
230