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