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