]> git.meshlink.io Git - catta/blob - avahi-core/iface-linux.c
* Some minor cleanups in netlink code
[catta] / avahi-core / 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 <string.h>
27 #include <net/if.h>
28 #include <errno.h>
29 #include <string.h>
30
31 #include <avahi-common/malloc.h>
32
33 #include "log.h"
34 #include "iface.h"
35 #include "iface-linux.h"
36
37 static int netlink_list_items(AvahiNetlink *nl, uint16_t type, unsigned *ret_seq) {
38     struct nlmsghdr *n;
39     struct rtgenmsg *gen;
40     uint8_t req[1024];
41     
42     memset(&req, 0, sizeof(req));
43     n = (struct nlmsghdr*) req;
44     n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
45     n->nlmsg_type = type;
46     n->nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
47     n->nlmsg_pid = 0;
48
49     gen = NLMSG_DATA(n);
50     memset(gen, 0, sizeof(struct rtgenmsg));
51     gen->rtgen_family = AF_UNSPEC;
52
53     return avahi_netlink_send(nl, n, ret_seq);
54 }
55
56 static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, void* userdata) {
57     AvahiInterfaceMonitor *m = userdata;
58     
59     assert(m);
60     assert(n);
61     assert(m->osdep.netlink == nl);
62
63     if (n->nlmsg_type == RTM_NEWLINK) {
64         struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
65         AvahiHwInterface *hw;
66         struct rtattr *a = NULL;
67         size_t l;
68         
69         if (ifinfomsg->ifi_family != AF_UNSPEC)
70             return;
71
72         /* Check whether there already is an AvahiHwInterface object
73          * for this link, so that we can update its data. Note that
74          * Netlink sends us an RTM_NEWLINK not only when a new
75          * interface appears, but when it changes, too */
76
77         if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
78             
79             /* No object found, so let's create a new
80              * one. avahi_hw_interface_new() will call
81              * avahi_interface_new() internally twice for IPv4 and
82              * IPv6, so there is no need for us to do that
83              * ourselves */
84             if (!(hw = avahi_hw_interface_new(m, (AvahiIfIndex) ifinfomsg->ifi_index)))
85                 return; /* OOM */
86
87         /* Check whether the flags of this interface are OK for us */
88         hw->flags_ok =
89             (ifinfomsg->ifi_flags & IFF_UP) &&
90             (!m->server->config.use_iff_running || (ifinfomsg->ifi_flags & IFF_RUNNING)) &&
91             !(ifinfomsg->ifi_flags & IFF_LOOPBACK) &&
92             (ifinfomsg->ifi_flags & IFF_MULTICAST) &&
93             !(ifinfomsg->ifi_flags & IFF_POINTOPOINT);
94
95         /* Handle interface attributes */
96         l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
97         a = IFLA_RTA(ifinfomsg);
98
99         while (RTA_OK(a, l)) {
100             switch(a->rta_type) {
101                 case IFLA_IFNAME:
102
103                     /* Fill in interface name */
104                     avahi_free(hw->name);
105                     hw->name = avahi_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
106                     break;
107
108                 case IFLA_MTU:
109
110                     /* Fill in MTU */
111                     assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
112                     hw->mtu = *((unsigned int*) RTA_DATA(a));
113                     break;
114
115                 case IFLA_ADDRESS:
116
117                     /* Fill in hardware (MAC) address */
118                     hw->mac_address_size = RTA_PAYLOAD(a);
119                     if (hw->mac_address_size > AVAHI_MAX_MAC_ADDRESS)
120                         hw->mac_address_size = AVAHI_MAX_MAC_ADDRESS;
121                     
122                     memcpy(hw->mac_address, RTA_DATA(a), hw->mac_address_size);
123                     break;
124                     
125                 default:
126                     ;
127             }
128
129             a = RTA_NEXT(a, l);
130         }
131
132         /* Check whether this interface is now "relevant" for us. If
133          * it is Avahi will start to announce its records on this
134          * interface and send out queries for subscribed records on
135          * it */
136         avahi_hw_interface_check_relevant(hw);
137
138         /* Update any associated RRs of this interface. (i.e. the
139          * _workstation._tcp record containing the MAC address) */
140         avahi_hw_interface_update_rrs(hw, 0);
141         
142     } else if (n->nlmsg_type == RTM_DELLINK) {
143         struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
144         AvahiHwInterface *hw;
145
146         if (ifinfomsg->ifi_family != AF_UNSPEC)
147             return;
148
149         /* Get a reference to our AvahiHwInterface object of this interface */
150         if (!(hw = avahi_interface_monitor_get_hw_interface(m, (AvahiIfIndex) ifinfomsg->ifi_index)))
151             return;
152
153         /* Free our object */
154         avahi_hw_interface_free(hw, 0);
155         
156     } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
157
158         struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
159         AvahiInterface *i;
160         struct rtattr *a = NULL;
161         size_t l;
162         AvahiAddress raddr;
163         int raddr_valid = 0;
164
165         /* We are only interested in IPv4 and IPv6 */
166         if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
167             return;
168
169         /* Try to get a reference to our AvahiInterface object for the
170          * interface this address is assigned to. If ther is no object
171          * for this interface, we ignore this address. */
172         if (!(i = avahi_interface_monitor_get_interface(m, (AvahiIfIndex) ifaddrmsg->ifa_index, avahi_af_to_proto(ifaddrmsg->ifa_family))))
173             return;
174
175         /* Fill in address family for our new address */
176         raddr.proto = avahi_af_to_proto(ifaddrmsg->ifa_family);
177
178         l = NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg));
179         a = IFA_RTA(ifaddrmsg);
180
181         while (RTA_OK(a, l)) {
182
183             switch(a->rta_type) {
184                 case IFA_ADDRESS:
185                     /* Fill in address data */
186                     
187                     if ((raddr.proto == AVAHI_PROTO_INET6 && RTA_PAYLOAD(a) != 16) ||
188                         (raddr.proto == AVAHI_PROTO_INET && RTA_PAYLOAD(a) != 4))
189                         return;
190
191                     memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
192                     raddr_valid = 1;
193
194                     break;
195
196                 default:
197                     ;
198             }
199             
200             a = RTA_NEXT(a, l);
201         }
202
203         /* If there was no adress attached to this message, let's quit. */
204         if (!raddr_valid)
205             return;
206
207         if (n->nlmsg_type == RTM_NEWADDR) {
208             AvahiInterfaceAddress *addr;
209
210             /* This address is new or has been modified, so let's get an object for it */
211             if (!(addr = avahi_interface_monitor_get_address(m, i, &raddr)))
212
213                 /* Mmm, no object existing yet, so let's create a new one */
214                 if (!(addr = avahi_interface_address_new(m, i, &raddr, ifaddrmsg->ifa_prefixlen)))
215                     return; /* OOM */
216
217             /* Update the scope field for the address */
218             addr->global_scope = ifaddrmsg->ifa_scope == RT_SCOPE_UNIVERSE || ifaddrmsg->ifa_scope == RT_SCOPE_SITE;
219         } else {
220             AvahiInterfaceAddress *addr;
221             assert(n->nlmsg_type == RTM_DELADDR);
222
223             /* Try to get a reference to our AvahiInterfaceAddress object for this address */
224             if (!(addr = avahi_interface_monitor_get_address(m, i, &raddr)))
225                 return;
226
227             /* And free it */
228             avahi_interface_address_free(addr);
229         }
230
231         /* Avahi only considers interfaces with at least one address
232          * attached relevant. Since we migh have added or removed an
233          * address, let's have it check again whether the interface is
234          * now relevant */
235         avahi_interface_check_relevant(i);
236
237         /* Update any associated RRs, like A or AAAA for our new/removed address */
238         avahi_interface_update_rrs(i, 0);
239         
240     } else if (n->nlmsg_type == NLMSG_DONE) {
241
242         /* This wild dump request ended, so let's see what we do next */
243         
244         if (m->osdep.list == LIST_IFACE) {
245
246             /* Mmmm, interfaces have been wild dumped already, so
247              * let's go on with wild dumping the addresses */
248             
249             if (netlink_list_items(m->osdep.netlink, RTM_GETADDR, &m->osdep.query_addr_seq) < 0) {
250                 avahi_log_warn("NETLINK: Failed to list addrs: %s", strerror(errno));
251                 m->osdep.list = LIST_DONE;
252             } else
253
254                 /* Update state information */
255                 m->osdep.list = LIST_ADDR;
256
257         } else
258             /* We're done. Tell avahi_interface_monitor_sync() to finish. */
259             m->osdep.list = LIST_DONE;
260
261         if (m->osdep.list == LIST_DONE) {
262
263             /* Only after this boolean variable has been set, Avahi
264              * will start to announce or browse on all interfaces. It
265              * is originaly set to 0, which means that relevancy
266              * checks and RR updates are disabled during the wild
267              * dumps. */
268             m->list_complete = 1;
269
270             /* So let's check if any interfaces are relevant now */
271             avahi_interface_monitor_check_relevant(m);
272
273             /* And update all RRs attached to any interface */
274             avahi_interface_monitor_update_rrs(m, 0);
275
276             /* Tell the user that the wild dump is complete */
277             avahi_log_info("Network interface enumeration completed.");
278         }
279         
280     } else if (n->nlmsg_type == NLMSG_ERROR &&
281                (n->nlmsg_seq == m->osdep.query_link_seq || n->nlmsg_seq == m->osdep.query_addr_seq)) {
282         struct nlmsgerr *e = NLMSG_DATA (n);
283
284         /* Some kind of error happened. Let's just tell the user and
285          * ignore it otherwise */
286         
287         if (e->error)
288             avahi_log_warn("NETLINK: Failed to browse: %s", strerror(-e->error));
289     }
290 }
291
292 int avahi_interface_monitor_init_osdep(AvahiInterfaceMonitor *m) {
293     assert(m);
294
295     /* Initialize our own data */
296     
297     m->osdep.netlink = NULL;
298     m->osdep.query_addr_seq = m->osdep.query_link_seq = 0;
299
300     /* Create a netlink object for us. It abstracts some things and
301      * makes netlink easier to use. It will attach to the main loop
302      * for us and call netlink_callback() whenever an event
303      * happens. */
304     if (!(m->osdep.netlink = avahi_netlink_new(m->server->poll_api, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, netlink_callback, m)))
305         goto fail;
306
307     /* Start the wild dump for the interfaces */
308     if (netlink_list_items(m->osdep.netlink, RTM_GETLINK, &m->osdep.query_link_seq) < 0)
309         goto fail;
310
311     /* Set the initial state. */
312     m->osdep.list = LIST_IFACE;
313
314     return 0;
315
316 fail:
317
318     if (m->osdep.netlink) {
319         avahi_netlink_free(m->osdep.netlink);
320         m->osdep.netlink = NULL;
321     }
322
323     return -1;
324 }
325
326 void avahi_interface_monitor_free_osdep(AvahiInterfaceMonitor *m) {
327     assert(m);
328
329     if (m->osdep.netlink) {
330         avahi_netlink_free(m->osdep.netlink);
331         m->osdep.netlink = NULL;
332     }
333 }
334
335 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
336     assert(m);
337
338     /* Let's handle netlink events until we are done with wild
339      * dumping */
340     
341     while (m->osdep.list != LIST_DONE) {
342         if (!avahi_netlink_work(m->osdep.netlink, 1) == 0)
343             break;
344     }
345
346     /* At this point Avahi knows about all local interfaces and
347      * addresses in existance. */
348 }