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