4 This file is part of avahi.
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.
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.
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
26 #include <sys/socket.h>
30 #include <arpa/inet.h>
32 #include <linux/types.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
36 #include <linux/if_arp.h>
38 #include <libdaemon/dlog.h>
40 #include <avahi-common/llist.h>
41 #include <avahi-common/malloc.h>
46 static int ifindex = -1;
48 typedef struct Address Address;
52 AVAHI_LLIST_FIELDS(Address, addresses);
55 AVAHI_LLIST_HEAD(Address, addresses) = NULL;
57 int iface_init(int i) {
58 struct sockaddr_nl addr;
60 if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
61 daemon_log(LOG_ERR, "socket(PF_NETLINK): %s", strerror(errno));
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();
70 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
71 daemon_log(LOG_ERR, "bind(): %s", strerror(errno));
88 static int process_nlmsg(struct nlmsghdr *n) {
91 if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
92 /* A link appeared or was removed */
94 struct ifinfomsg *ifi;
97 if (ifi->ifi_family != AF_UNSPEC || ifi->ifi_index != ifindex)
100 if (n->nlmsg_type == RTM_DELLINK) {
101 daemon_log(LOG_ERR, "Interface vanished.");
105 assert(n->nlmsg_type == RTM_NEWLINK);
107 if ((ifi->ifi_flags & IFF_LOOPBACK) ||
108 (ifi->ifi_flags & IFF_NOARP) ||
109 ifi->ifi_type != ARPHRD_ETHER) {
110 daemon_log(LOG_ERR, "Interface not suitable.");
114 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
116 /* An address was added or removed */
118 struct rtattr *a = NULL;
119 struct ifaddrmsg *ifa;
121 uint32_t address = 0;
126 if (ifa->ifa_family != AF_INET || ifa->ifa_index != ifindex)
129 l = NLMSG_PAYLOAD(n, sizeof(*ifa));
132 while(RTA_OK(a, l)) {
134 switch(a->rta_type) {
137 assert(RTA_PAYLOAD(a) == 4);
138 memcpy(&address, RTA_DATA(a), sizeof(uint32_t));
145 if (!address || is_ll_address(address))
148 for (i = addresses; i; i = i->addresses_next)
149 if (i->address == address)
152 if (n->nlmsg_type == RTM_DELADDR && i) {
153 AVAHI_LLIST_REMOVE(Address, addresses, addresses, i);
155 } if (n->nlmsg_type == RTM_NEWADDR && !i) {
156 i = avahi_new(Address, 1);
157 i->address = address;
158 AVAHI_LLIST_PREPEND(Address, addresses, addresses, i);
165 static int process_response(int wait_for_done, unsigned seq) {
172 struct nlmsghdr *p = (struct nlmsghdr *) replybuf;
174 if ((r = recv(fd, replybuf, sizeof(replybuf), 0)) < 0) {
175 daemon_log(LOG_ERR, "recv() failed: %s", strerror(errno));
181 for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
183 if (!NLMSG_OK(p, bytes) || bytes < sizeof(struct nlmsghdr) || bytes < p->nlmsg_len) {
184 daemon_log(LOG_ERR, "Netlink packet too small.");
188 if (p->nlmsg_type == NLMSG_DONE && wait_for_done && p->nlmsg_seq == seq && (pid_t) p->nlmsg_pid == getpid())
191 if (p->nlmsg_type == NLMSG_ERROR) {
192 struct nlmsgerr *e = (struct nlmsgerr *) NLMSG_DATA (p);
195 daemon_log(LOG_ERR, "Netlink error: %s", strerror(-e->error));
200 if (process_nlmsg(p) < 0)
203 } while (wait_for_done);
208 int iface_get_initial_state(State *state) {
210 struct ifinfomsg *ifi;
211 struct ifaddrmsg *ifa;
218 memset(&req, 0, sizeof(req));
219 n = (struct nlmsghdr*) req;
220 n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
221 n->nlmsg_type = RTM_GETLINK;
223 n->nlmsg_flags = NLM_F_MATCH|NLM_F_REQUEST|NLM_F_ACK;
227 ifi->ifi_family = AF_UNSPEC;
228 ifi->ifi_change = -1;
230 if (send(fd, n, n->nlmsg_len, 0) < 0) {
231 daemon_log(LOG_ERR, "send(): %s", strerror(errno));
235 if (process_response(1, 0) < 0)
238 n->nlmsg_type = RTM_GETADDR;
239 n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifa));
240 n->nlmsg_seq = ++seq;
243 ifa->ifa_family = AF_INET;
244 ifa->ifa_index = ifindex;
246 if (send(fd, n, n->nlmsg_len, 0) < 0) {
247 daemon_log(LOG_ERR, "send(): %s", strerror(errno));
251 if (process_response(1, seq) < 0)
254 *state = addresses ? STATE_SLEEPING : STATE_START;
259 int iface_process(Event *event) {
265 if (process_response(0, 0) < 0)
269 *event = EVENT_ROUTABLE_ADDR_UNCONFIGURED;
270 else if (!b && addresses)
271 *event = EVENT_ROUTABLE_ADDR_CONFIGURED;
276 void iface_done(void) {
284 while ((a = addresses)) {
285 AVAHI_LLIST_REMOVE(Address, addresses, addresses, a);