]> git.meshlink.io Git - catta/blob - avahi-autoipd/iface-linux.c
set the interface to IFF_UP before using it. handle POLLERR on the packet socket...
[catta] / avahi-autoipd / iface-linux.c
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 #include "iface.h"
44
45 static int fd = -1;
46 static int ifindex = -1;
47
48 typedef struct Address Address;
49
50 struct Address {
51     uint32_t address;
52     AVAHI_LLIST_FIELDS(Address, addresses);
53 };
54
55 AVAHI_LLIST_HEAD(Address, addresses) = NULL;
56
57 int iface_init(int i) {
58     struct sockaddr_nl addr;
59     
60     if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
61         daemon_log(LOG_ERR, "socket(PF_NETLINK): %s", strerror(errno));
62         goto fail;
63     }
64
65     memset(&addr, 0, sizeof(addr));
66     addr.nl_family = AF_NETLINK;
67     addr.nl_groups =  RTMGRP_LINK|RTMGRP_IPV4_IFADDR;
68     addr.nl_pid = getpid();
69
70     if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
71         daemon_log(LOG_ERR, "bind(): %s", strerror(errno));
72         goto fail;
73     }
74
75     ifindex = i;
76     
77     return fd;
78     
79 fail:
80     if (fd >= 0) {
81         close(fd);
82         fd = -1;
83     }
84     
85     return -1;
86 }
87
88 static int process_nlmsg(struct nlmsghdr *n) {
89     assert(n);
90
91     if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
92         /* A link appeared or was removed */
93
94         struct ifinfomsg *ifi;
95         ifi = NLMSG_DATA(n);
96
97         if (ifi->ifi_family != AF_UNSPEC || ifi->ifi_index != ifindex)
98             return 0;
99         
100         if (n->nlmsg_type == RTM_DELLINK) {
101             daemon_log(LOG_ERR, "Interface vanished.");
102             return -1;
103         }
104
105         assert(n->nlmsg_type == RTM_NEWLINK);
106         
107         if ((ifi->ifi_flags & IFF_LOOPBACK) ||
108             (ifi->ifi_flags & IFF_NOARP) ||
109             ifi->ifi_type != ARPHRD_ETHER) {
110             daemon_log(LOG_ERR, "Interface not suitable.");
111             return -1;
112         }
113
114     } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
115
116         /* An address was added or removed */
117
118         struct rtattr *a = NULL;
119         struct ifaddrmsg *ifa;
120         int l;
121         uint32_t address = 0;
122         Address *i;
123         
124         ifa = NLMSG_DATA(n);
125
126         if (ifa->ifa_family != AF_INET || ifa->ifa_index != ifindex)
127             return 0;
128
129         l = NLMSG_PAYLOAD(n, sizeof(*ifa));
130         a = IFLA_RTA(ifa);
131
132         while(RTA_OK(a, l)) {
133
134             switch(a->rta_type) {
135                 case IFA_LOCAL:
136                 case IFA_ADDRESS:
137                     assert(RTA_PAYLOAD(a) == 4);
138                     memcpy(&address, RTA_DATA(a), sizeof(uint32_t));
139                     break;
140             }
141             
142             a = RTA_NEXT(a, l);
143         }
144
145         if (!address || is_ll_address(address))
146             return 0;
147
148         for (i = addresses; i; i = i->addresses_next)
149             if (i->address == address)
150                 break;
151
152         if (n->nlmsg_type == RTM_DELADDR && i) {
153             AVAHI_LLIST_REMOVE(Address, addresses, addresses, i);
154             avahi_free(i);
155         } if (n->nlmsg_type == RTM_NEWADDR && !i) {
156             i = avahi_new(Address, 1);
157             i->address = address;
158             AVAHI_LLIST_PREPEND(Address, addresses, addresses, i);
159         }
160     }
161
162     return 0;
163 }
164
165 static int process_response(int wait_for_done, unsigned seq) {
166     assert(fd >= 0);
167     
168     do {
169         size_t bytes;
170         ssize_t r;
171         char replybuf[2048];
172         struct nlmsghdr *p = (struct nlmsghdr *) replybuf;
173
174         if ((r = recv(fd, replybuf, sizeof(replybuf), 0)) < 0) {
175             daemon_log(LOG_ERR, "recv() failed: %s", strerror(errno));
176             return -1;
177         }
178
179         bytes = (size_t) r;
180         
181         for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
182
183             if (!NLMSG_OK(p, bytes) || bytes < sizeof(struct nlmsghdr) || bytes < p->nlmsg_len) {
184                 daemon_log(LOG_ERR, "Netlink packet too small.");
185                 return -1;
186             }
187
188             if (p->nlmsg_type == NLMSG_DONE && wait_for_done && p->nlmsg_seq == seq && (pid_t) p->nlmsg_pid == getpid())
189                 return 0;
190
191             if (p->nlmsg_type == NLMSG_ERROR) {
192                 struct nlmsgerr *e = (struct nlmsgerr *) NLMSG_DATA (p);
193
194                 if (e->error) {
195                     daemon_log(LOG_ERR, "Netlink error: %s", strerror(-e->error));
196                     return -1;
197                 }
198             }
199
200             if (process_nlmsg(p) < 0)
201                 return -1;
202         }
203     } while (wait_for_done);
204
205     return 0;
206 }
207
208 int iface_get_initial_state(State *state) {
209     struct nlmsghdr *n;
210     struct ifinfomsg *ifi;
211     struct ifaddrmsg *ifa;
212     uint8_t req[1024];
213     int seq = 0;
214
215     assert(fd >= 0);
216     assert(state);
217     
218     memset(&req, 0, sizeof(req));
219     n = (struct nlmsghdr*) req;
220     n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
221     n->nlmsg_type = RTM_GETLINK;
222     n->nlmsg_seq = seq;
223     n->nlmsg_flags = NLM_F_MATCH|NLM_F_REQUEST|NLM_F_ACK;
224     n->nlmsg_pid = 0;
225
226     ifi = NLMSG_DATA(n);
227     ifi->ifi_family = AF_UNSPEC;
228     ifi->ifi_change = -1;
229
230     if (send(fd, n, n->nlmsg_len, 0) < 0) {
231         daemon_log(LOG_ERR, "send(): %s", strerror(errno));
232         return -1;
233     }
234
235     if (process_response(1, 0) < 0)
236         return -1;
237     
238     n->nlmsg_type = RTM_GETADDR;
239     n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifa));
240     n->nlmsg_seq = ++seq;
241
242     ifa = NLMSG_DATA(n);
243     ifa->ifa_family = AF_INET;
244     ifa->ifa_index = ifindex;
245     
246     if (send(fd, n, n->nlmsg_len, 0) < 0) {
247         daemon_log(LOG_ERR, "send(): %s", strerror(errno));
248         return -1;
249     }
250
251     if (process_response(1, seq) < 0)
252         return -1;
253
254     *state = addresses ? STATE_SLEEPING : STATE_START;
255     
256     return 0;
257 }
258
259 int iface_process(Event *event) {
260     int b;
261     assert(fd >= 0);
262
263     b = !!addresses;
264     
265     if (process_response(0, 0) < 0)
266         return -1;
267
268     if (b && !addresses)
269         *event = EVENT_ROUTABLE_ADDR_UNCONFIGURED;
270     else if (!b && addresses)
271         *event = EVENT_ROUTABLE_ADDR_CONFIGURED;
272     
273     return 0;
274 }
275
276 void iface_done(void) {
277     Address *a;
278     
279     if (fd >= 0) {
280         close(fd);
281         fd = -1;
282     }
283
284     while ((a = addresses)) {
285         AVAHI_LLIST_REMOVE(Address, addresses, addresses, a);
286         avahi_free(a);
287     }
288 }
289
290