]> git.meshlink.io Git - catta/blob - avahi-autoipd/iface-linux.c
add netlink support
[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_flags & IFF_UP) ||
110             ifi->ifi_type != ARPHRD_ETHER) {
111             daemon_log(LOG_ERR, "Interface not suitable.");
112             return -1;
113         }
114
115     } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
116
117         /* An address was added or removed */
118
119         struct rtattr *a = NULL;
120         struct ifaddrmsg *ifa;
121         int l;
122         uint32_t address = 0;
123         Address *i;
124         char buf[32];
125         
126         ifa = NLMSG_DATA(n);
127
128         if (ifa->ifa_family != AF_INET || ifa->ifa_index != ifindex)
129             return 0;
130
131         l = NLMSG_PAYLOAD(n, sizeof(*ifa));
132         a = IFLA_RTA(ifa);
133
134         while(RTA_OK(a, l)) {
135
136             switch(a->rta_type) {
137                 case IFA_LOCAL:
138                 case IFA_ADDRESS:
139                     assert(RTA_PAYLOAD(a) == 4);
140                     memcpy(&address, RTA_DATA(a), sizeof(uint32_t));
141                     break;
142             }
143             
144             a = RTA_NEXT(a, l);
145         }
146
147         daemon_log(LOG_INFO, "%s", inet_ntop(AF_INET, &address, buf, sizeof(buf)));
148
149         if (!address || is_ll_address(address))
150             return 0;
151
152         for (i = addresses; i; i = i->addresses_next)
153             if (i->address == address)
154                 break;
155
156         if (n->nlmsg_type == RTM_DELADDR && i) {
157             AVAHI_LLIST_REMOVE(Address, addresses, addresses, i);
158             avahi_free(i);
159         } if (n->nlmsg_type == RTM_NEWADDR && !i) {
160             i = avahi_new(Address, 1);
161             i->address = address;
162             AVAHI_LLIST_PREPEND(Address, addresses, addresses, i);
163         }
164     }
165
166     return 0;
167 }
168
169 static int process_response(int wait_for_done, unsigned seq) {
170     assert(fd >= 0);
171     
172     do {
173         size_t bytes;
174         ssize_t r;
175         char replybuf[2048];
176         struct nlmsghdr *p = (struct nlmsghdr *) replybuf;
177
178         if ((r = recv(fd, replybuf, sizeof(replybuf), 0)) < 0) {
179             daemon_log(LOG_ERR, "recv() failed: %s", strerror(errno));
180             return -1;
181         }
182
183         bytes = (size_t) r;
184         
185         for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
186
187             if (!NLMSG_OK(p, bytes) || bytes < sizeof(struct nlmsghdr) || bytes < p->nlmsg_len) {
188                 daemon_log(LOG_ERR, "Netlink packet too small.");
189                 return -1;
190             }
191
192             if (p->nlmsg_type == NLMSG_DONE && wait_for_done && p->nlmsg_seq == seq)
193                 return 0;
194
195             if (p->nlmsg_type == NLMSG_ERROR) {
196                 struct nlmsgerr *e = (struct nlmsgerr *) NLMSG_DATA (p);
197
198                 if (e->error) {
199                     daemon_log(LOG_ERR, "Netlink error: %s", strerror(-e->error));
200                     return -1;
201                 }
202             }
203
204             if ((pid_t) p->nlmsg_pid != getpid())
205                 continue;
206
207             if (process_nlmsg(p) < 0)
208                 return -1;
209         }
210     } while (wait_for_done);
211
212     return 0;
213 }
214
215 int iface_get_initial_state(State *state) {
216     struct nlmsghdr *n;
217     struct ifinfomsg *ifi;
218     struct ifaddrmsg *ifa;
219     uint8_t req[1024];
220     int seq = 0;
221
222     assert(fd >= 0);
223     assert(state);
224     
225     memset(&req, 0, sizeof(req));
226     n = (struct nlmsghdr*) req;
227     n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
228     n->nlmsg_type = RTM_GETLINK;
229     n->nlmsg_seq = seq;
230     n->nlmsg_flags = NLM_F_MATCH|NLM_F_REQUEST|NLM_F_ACK;
231     n->nlmsg_pid = 0;
232
233     ifi = NLMSG_DATA(n);
234     ifi->ifi_family = AF_UNSPEC;
235     ifi->ifi_change = -1;
236
237     if (send(fd, n, n->nlmsg_len, 0) < 0) {
238         daemon_log(LOG_ERR, "send(): %s", strerror(errno));
239         return -1;
240     }
241
242     if (process_response(1, 0) < 0)
243         return -1;
244     
245     n->nlmsg_type = RTM_GETADDR;
246     n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifa));
247     n->nlmsg_seq = ++seq;
248
249     ifa = NLMSG_DATA(n);
250     ifa->ifa_family = AF_INET;
251     ifa->ifa_index = ifindex;
252     
253     if (send(fd, n, n->nlmsg_len, 0) < 0) {
254         daemon_log(LOG_ERR, "send(): %s", strerror(errno));
255         return -1;
256     }
257
258     if (process_response(1, seq) < 0)
259         return -1;
260
261     *state = addresses ? STATE_SLEEPING : STATE_START;
262     
263     return 0;
264 }
265
266 int iface_process(Event *event) {
267     int b;
268     assert(fd >= 0);
269
270     b = !!addresses;
271     
272     if (process_response(0, 0) < 0)
273         return -1;
274
275     if (b && !!addresses)
276         *event = EVENT_ROUTABLE_ADDR_UNCONFIGURED;
277     else if (!b && addresses)
278         *event = EVENT_ROUTABLE_ADDR_CONFIGURED;
279     
280     return 0;
281 }
282
283 void iface_done(void) {
284     Address *a;
285     
286     if (fd >= 0) {
287         close(fd);
288         fd = -1;
289     }
290
291     while ((a = addresses)) {
292         AVAHI_LLIST_REMOVE(Address, addresses, addresses, a);
293         avahi_free(a);
294     }
295 }
296
297