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 <sys/socket.h>
27#include <errno.h>
28#include <string.h>
29#include <unistd.h>
30#include <arpa/inet.h>
31
32#include <linux/types.h>
33#include <linux/netlink.h>
34#include <linux/rtnetlink.h>
35#include <linux/if.h>
36#include <linux/if_arp.h>
37
38#include <libdaemon/dlog.h>
39
40#include <avahi-common/llist.h>
41#include <avahi-common/malloc.h>
42
43#ifndef IFLA_RTA
44#include <linux/if_addr.h>
45#define IFLA_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
46#endif
47
48#ifndef IFA_RTA
49#include <linux/if_addr.h>
50#define IFA_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
51#endif
52
53#include "iface.h"
54
55static int fd = -1;
56static int ifindex = -1;
57
58typedef struct Address Address;
59
60struct Address {
61    uint32_t address;
62    AVAHI_LLIST_FIELDS(Address, addresses);
63};
64
65AVAHI_LLIST_HEAD(Address, addresses) = NULL;
66
67int iface_init(int i) {
68    struct sockaddr_nl addr;
69    int on = 1;
70
71    if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
72        daemon_log(LOG_ERR, "socket(PF_NETLINK): %s", strerror(errno));
73        goto fail;
74    }
75
76    memset(&addr, 0, sizeof(addr));
77    addr.nl_family = AF_NETLINK;
78    addr.nl_groups =  RTMGRP_LINK|RTMGRP_IPV4_IFADDR;
79    addr.nl_pid = getpid();
80
81    if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
82        daemon_log(LOG_ERR, "bind(): %s", strerror(errno));
83        goto fail;
84    }
85
86    if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
87        daemon_log(LOG_ERR, "SO_PASSCRED: %s", strerror(errno));
88        goto fail;
89    }
90
91    ifindex = i;
92
93    return fd;
94
95fail:
96    if (fd >= 0) {
97        close(fd);
98        fd = -1;
99    }
100
101    return -1;
102}
103
104static int process_nlmsg(struct nlmsghdr *n) {
105    assert(n);
106
107    if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
108        /* A link appeared or was removed */
109
110        struct ifinfomsg *ifi;
111        ifi = NLMSG_DATA(n);
112
113        if (ifi->ifi_family != AF_UNSPEC || (int) ifi->ifi_index != ifindex)
114            return 0;
115
116        if (n->nlmsg_type == RTM_DELLINK) {
117            daemon_log(LOG_ERR, "Interface vanished.");
118            return -1;
119        }
120
121        assert(n->nlmsg_type == RTM_NEWLINK);
122
123        if ((ifi->ifi_flags & IFF_LOOPBACK) ||
124            (ifi->ifi_flags & IFF_NOARP) ||
125            ifi->ifi_type != ARPHRD_ETHER) {
126            daemon_log(LOG_ERR, "Interface not suitable.");
127            return -1;
128        }
129
130    } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
131
132        /* An address was added or removed */
133
134        struct rtattr *a = NULL;
135        struct ifaddrmsg *ifa;
136        int l;
137        uint32_t address = 0;
138        Address *i;
139
140        ifa = NLMSG_DATA(n);
141
142        if (ifa->ifa_family != AF_INET || (int) ifa->ifa_index != ifindex)
143            return 0;
144
145        l = NLMSG_PAYLOAD(n, sizeof(*ifa));
146        a = IFLA_RTA(ifa);
147
148        while(RTA_OK(a, l)) {
149
150            switch(a->rta_type) {
151                case IFA_LOCAL:
152                case IFA_ADDRESS:
153                    assert(RTA_PAYLOAD(a) == 4);
154                    memcpy(&address, RTA_DATA(a), sizeof(uint32_t));
155                    break;
156            }
157
158            a = RTA_NEXT(a, l);
159        }
160
161        if (!address || is_ll_address(address))
162            return 0;
163
164        for (i = addresses; i; i = i->addresses_next)
165            if (i->address == address)
166                break;
167
168        if (n->nlmsg_type == RTM_DELADDR && i) {
169            AVAHI_LLIST_REMOVE(Address, addresses, addresses, i);
170            avahi_free(i);
171        } if (n->nlmsg_type == RTM_NEWADDR && !i) {
172            i = avahi_new(Address, 1);
173            i->address = address;
174            AVAHI_LLIST_PREPEND(Address, addresses, addresses, i);
175        }
176    }
177
178    return 0;
179}
180
181static int process_response(int wait_for_done, unsigned seq) {
182    assert(fd >= 0);
183
184    do {
185        size_t bytes;
186        ssize_t r;
187        char replybuf[8*1024];
188        char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
189        struct msghdr msghdr;
190        struct cmsghdr *cmsghdr;
191        struct ucred *ucred;
192        struct iovec iov;
193        struct nlmsghdr *p = (struct nlmsghdr *) replybuf;
194
195        memset(&iov, 0, sizeof(iov));
196        iov.iov_base = replybuf;
197 	iov.iov_len = sizeof(replybuf);
198
199        memset(&msghdr, 0, sizeof(msghdr));
200        msghdr.msg_name = (void*) NULL;
201        msghdr.msg_namelen = 0;
202        msghdr.msg_iov = &iov;
203        msghdr.msg_iovlen = 1;
204        msghdr.msg_control = cred_msg;
205        msghdr.msg_controllen = sizeof(cred_msg);
206 	msghdr.msg_flags = 0;
207
208        if ((r = recvmsg(fd, &msghdr, 0)) < 0) {
209            daemon_log(LOG_ERR, "recvmsg() failed: %s", strerror(errno));
210            return -1;
211        }
212
213        if (!(cmsghdr = CMSG_FIRSTHDR(&msghdr)) || cmsghdr->cmsg_type != SCM_CREDENTIALS) {
214            daemon_log(LOG_WARNING, "No sender credentials received, ignoring data.");
215            return -1;
216        }
217
218        ucred = (struct ucred*) CMSG_DATA(cmsghdr);
219
220        if (ucred->uid != 0)
221            return -1;
222
223        bytes = (size_t) r;
224
225        for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
226
227            if (!NLMSG_OK(p, bytes) || bytes < sizeof(struct nlmsghdr) || bytes < p->nlmsg_len) {
228                daemon_log(LOG_ERR, "Netlink packet too small.");
229                return -1;
230            }
231
232            if (p->nlmsg_type == NLMSG_DONE && wait_for_done && p->nlmsg_seq == seq && (pid_t) p->nlmsg_pid == getpid())
233                return 0;
234
235            if (p->nlmsg_type == NLMSG_ERROR) {
236                struct nlmsgerr *e = (struct nlmsgerr *) NLMSG_DATA (p);
237
238                if (e->error) {
239                    daemon_log(LOG_ERR, "Netlink error: %s", strerror(-e->error));
240                    return -1;
241                }
242            }
243
244            if (process_nlmsg(p) < 0)
245                return -1;
246        }
247    } while (wait_for_done);
248
249    return 0;
250}
251
252int iface_get_initial_state(State *state) {
253    struct nlmsghdr *n;
254    struct ifinfomsg *ifi;
255    struct ifaddrmsg *ifa;
256    uint8_t req[1024];
257    int seq = 0;
258
259    assert(fd >= 0);
260    assert(state);
261
262    memset(&req, 0, sizeof(req));
263    n = (struct nlmsghdr*) req;
264    n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
265    n->nlmsg_type = RTM_GETLINK;
266    n->nlmsg_seq = seq;
267    n->nlmsg_flags = NLM_F_MATCH|NLM_F_REQUEST|NLM_F_ACK;
268    n->nlmsg_pid = 0;
269
270    ifi = NLMSG_DATA(n);
271    ifi->ifi_family = AF_UNSPEC;
272    ifi->ifi_change = -1;
273
274    if (send(fd, n, n->nlmsg_len, 0) < 0) {
275        daemon_log(LOG_ERR, "send(): %s", strerror(errno));
276        return -1;
277    }
278
279    if (process_response(1, 0) < 0)
280        return -1;
281
282    n->nlmsg_type = RTM_GETADDR;
283    n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifa));
284    n->nlmsg_seq = ++seq;
285
286    ifa = NLMSG_DATA(n);
287    ifa->ifa_family = AF_INET;
288    ifa->ifa_index = ifindex;
289
290    if (send(fd, n, n->nlmsg_len, 0) < 0) {
291        daemon_log(LOG_ERR, "send(): %s", strerror(errno));
292        return -1;
293    }
294
295    if (process_response(1, seq) < 0)
296        return -1;
297
298    *state = addresses ? STATE_SLEEPING : STATE_START;
299
300    return 0;
301}
302
303int iface_process(Event *event) {
304    int b;
305    assert(fd >= 0);
306
307    b = !!addresses;
308
309    if (process_response(0, 0) < 0)
310        return -1;
311
312    if (b && !addresses)
313        *event = EVENT_ROUTABLE_ADDR_UNCONFIGURED;
314    else if (!b && addresses)
315        *event = EVENT_ROUTABLE_ADDR_CONFIGURED;
316
317    return 0;
318}
319
320void iface_done(void) {
321    Address *a;
322
323    if (fd >= 0) {
324        close(fd);
325        fd = -1;
326    }
327
328    while ((a = addresses)) {
329        AVAHI_LLIST_REMOVE(Address, addresses, addresses, a);
330        avahi_free(a);
331    }
332}
333
334
335