Home | History | Annotate | Download | only in src
      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 
     42 NetlinkEvent::NetlinkEvent() {
     43     mAction = NlActionUnknown;
     44     memset(mParams, 0, sizeof(mParams));
     45     mPath = NULL;
     46     mSubsystem = NULL;
     47 }
     48 
     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 
     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  */
     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             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         }
    133         nh = NLMSG_NEXT(nh, size);
    134     }
    135 
    136     return true;
    137 }
    138 
    139 /* 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  */
    143 static const char*
    144 has_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 
    159 /*
    160  * Parse an ASCII-formatted message from a NETLINK_KOBJECT_UEVENT
    161  * netlink socket.
    162  */
    163 bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) {
    164     const char *s = buffer;
    165     const char *end;
    166     int param_idx = 0;
    167     int i;
    168     int first = 1;
    169 
    170     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 
    176     end = s + size;
    177     while (s < end) {
    178         if (first) {
    179             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);
    187             first = 0;
    188         } else {
    189             const char* a;
    190             if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) {
    191                 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;
    197             } 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) {
    202                 mParams[param_idx++] = strdup(s);
    203             }
    204         }
    205         s += strlen(s) + 1;
    206     }
    207     return true;
    208 }
    209 
    210 bool NetlinkEvent::decode(char *buffer, int size, int format) {
    211     if (format == NetlinkListener::NETLINK_FORMAT_BINARY) {
    212         return parseBinaryNetlinkMessage(buffer, size);
    213     } else {
    214         return parseAsciiNetlinkMessage(buffer, size);
    215     }
    216 }
    217 
    218 const char *NetlinkEvent::findParam(const char *paramName) {
    219     size_t len = strlen(paramName);
    220     for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != NULL; ++i) {
    221         const char *ptr = mParams[i] + len;
    222         if (!strncmp(mParams[i], paramName, len) && *ptr == '=')
    223             return ++ptr;
    224     }
    225 
    226     SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName);
    227     return NULL;
    228 }
    229