Home | History | Annotate | Download | only in avahi-core
      1 /***
      2   This file is part of avahi.
      3 
      4   avahi is free software; you can redistribute it and/or modify it
      5   under the terms of the GNU Lesser General Public License as
      6   published by the Free Software Foundation; either version 2.1 of the
      7   License, or (at your option) any later version.
      8 
      9   avahi is distributed in the hope that it will be useful, but WITHOUT
     10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
     12   Public License for more details.
     13 
     14   You should have received a copy of the GNU Lesser General Public
     15   License along with avahi; if not, write to the Free Software
     16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
     17   USA.
     18 ***/
     19 
     20 #ifdef HAVE_CONFIG_H
     21 #include <config.h>
     22 #endif
     23 
     24 #include <unistd.h>
     25 #include <errno.h>
     26 #include <string.h>
     27 #include <sys/ioctl.h>
     28 #include <assert.h>
     29 
     30 #include "avahi-common/avahi-malloc.h"
     31 #include "netlink.h"
     32 #include "log.h"
     33 
     34 struct AvahiNetlink {
     35     int fd;
     36     unsigned seq;
     37     AvahiNetlinkCallback callback;
     38     void* userdata;
     39     uint8_t* buffer;
     40     size_t buffer_length;
     41 
     42     const AvahiPoll *poll_api;
     43     AvahiWatch *watch;
     44 };
     45 
     46 int avahi_netlink_work(AvahiNetlink *nl, int block) {
     47     ssize_t bytes;
     48     struct msghdr smsg;
     49     struct cmsghdr *cmsg;
     50     struct ucred *cred;
     51     struct iovec iov;
     52     struct nlmsghdr *p;
     53     char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
     54 
     55     assert(nl);
     56 
     57     iov.iov_base = nl->buffer;
     58     iov.iov_len = nl->buffer_length;
     59 
     60     smsg.msg_name = NULL;
     61     smsg.msg_namelen = 0;
     62     smsg.msg_iov = &iov;
     63     smsg.msg_iovlen = 1;
     64     smsg.msg_control = cred_msg;
     65     smsg.msg_controllen = sizeof(cred_msg);
     66     smsg.msg_flags = (block ? 0 : MSG_DONTWAIT);
     67 
     68     if ((bytes = recvmsg(nl->fd, &smsg, 0)) < 0) {
     69         if (errno == EAGAIN || errno == EINTR)
     70             return 0;
     71 
     72         avahi_log_error(__FILE__": recvmsg() failed: %s", strerror(errno));
     73         return -1;
     74     }
     75 
     76     cmsg = CMSG_FIRSTHDR(&smsg);
     77 
     78     if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
     79         avahi_log_warn("No sender credentials received, ignoring data.");
     80         return -1;
     81     }
     82 
     83     cred = (struct ucred*) CMSG_DATA(cmsg);
     84 
     85     if (cred->uid != 0)
     86         return -1;
     87 
     88     p = (struct nlmsghdr *) nl->buffer;
     89 
     90     assert(nl->callback);
     91 
     92     for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
     93         if (!NLMSG_OK(p, (size_t) bytes)) {
     94             avahi_log_warn(__FILE__": packet truncated");
     95             return -1;
     96         }
     97 
     98         nl->callback(nl, p, nl->userdata);
     99     }
    100 
    101     return 0;
    102 }
    103 
    104 static void socket_event(AvahiWatch *w, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, void *userdata) {
    105     AvahiNetlink *nl = userdata;
    106 
    107     assert(w);
    108     assert(nl);
    109     assert(fd == nl->fd);
    110 
    111     avahi_netlink_work(nl, 0);
    112 }
    113 
    114 AvahiNetlink *avahi_netlink_new(const AvahiPoll *poll_api, uint32_t groups, void (*cb) (AvahiNetlink *nl, struct nlmsghdr *n, void* userdata), void* userdata) {
    115     int fd = -1;
    116     const int on = 1;
    117     struct sockaddr_nl addr;
    118     AvahiNetlink *nl = NULL;
    119 
    120     assert(poll_api);
    121     assert(cb);
    122 
    123     if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
    124         avahi_log_error(__FILE__": socket(PF_NETLINK): %s", strerror(errno));
    125         return NULL;
    126     }
    127 
    128     memset(&addr, 0, sizeof(addr));
    129     addr.nl_family = AF_NETLINK;
    130     addr.nl_groups = groups;
    131     addr.nl_pid = getpid();
    132 
    133     if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    134         avahi_log_error(__FILE__": bind(): %s", strerror(errno));
    135         goto fail;
    136     }
    137 
    138     if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
    139         avahi_log_error(__FILE__": SO_PASSCRED: %s", strerror(errno));
    140         goto fail;
    141     }
    142 
    143     if (!(nl = avahi_new(AvahiNetlink, 1))) {
    144         avahi_log_error(__FILE__": avahi_new() failed.");
    145         goto fail;
    146     }
    147 
    148     nl->poll_api = poll_api;
    149     nl->fd = fd;
    150     nl->seq = 0;
    151     nl->callback = cb;
    152     nl->userdata = userdata;
    153 
    154     if (!(nl->buffer = avahi_new(uint8_t, nl->buffer_length = 64*1024))) {
    155         avahi_log_error(__FILE__": avahi_new() failed.");
    156         goto fail;
    157     }
    158 
    159     if (!(nl->watch = poll_api->watch_new(poll_api, fd, AVAHI_WATCH_IN, socket_event, nl))) {
    160         avahi_log_error(__FILE__": Failed to create watch.");
    161         goto fail;
    162     }
    163 
    164     return nl;
    165 
    166 fail:
    167 
    168     if (fd >= 0)
    169         close(fd);
    170 
    171     if (nl) {
    172         avahi_free(nl->buffer);
    173         avahi_free(nl);
    174     }
    175 
    176     return NULL;
    177 }
    178 
    179 void avahi_netlink_free(AvahiNetlink *nl) {
    180     assert(nl);
    181 
    182     if (nl->watch)
    183         nl->poll_api->watch_free(nl->watch);
    184 
    185     if (nl->fd >= 0)
    186         close(nl->fd);
    187 
    188     avahi_free(nl->buffer);
    189     avahi_free(nl);
    190 }
    191 
    192 int avahi_netlink_send(AvahiNetlink *nl, struct nlmsghdr *m, unsigned *ret_seq) {
    193     assert(nl);
    194     assert(m);
    195 
    196     m->nlmsg_seq = nl->seq++;
    197     m->nlmsg_flags |= NLM_F_ACK;
    198 
    199     if (send(nl->fd, m, m->nlmsg_len, 0) < 0) {
    200         avahi_log_error(__FILE__": send(): %s", strerror(errno));
    201         return -1;
    202     }
    203 
    204     if (ret_seq)
    205         *ret_seq = m->nlmsg_seq;
    206 
    207     return 0;
    208 }
    209