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