2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 #include <sys/socket.h>
28 #include <arpa/inet.h>
30 #include <linux/types.h>
31 #include <linux/netlink.h>
32 #include <linux/rtnetlink.h>
34 #include <linux/if_arp.h>
36 #include <libdaemon/dlog.h>
38 #include <avahi-common/llist.h>
39 #include <avahi-common/malloc.h>
42 #include <linux/if_addr.h>
43 #define IFLA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
47 #include <linux/if_addr.h>
48 #define IFA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
54 static int ifindex = -1;
56 typedef struct Address Address;
60 AVAHI_LLIST_FIELDS(Address, addresses);
63 AVAHI_LLIST_HEAD(Address, addresses) = NULL;
65 int iface_init(int i) {
66 struct sockaddr_nl addr;
69 if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
70 daemon_log(LOG_ERR, "socket(PF_NETLINK): %s", strerror(errno));
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();
79 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
80 daemon_log(LOG_ERR, "bind(): %s", strerror(errno));
84 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
85 daemon_log(LOG_ERR, "SO_PASSCRED: %s", strerror(errno));
102 static int process_nlmsg(struct nlmsghdr *n) {
105 if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
106 /* A link appeared or was removed */
108 struct ifinfomsg *ifi;
111 if (ifi->ifi_family != AF_UNSPEC || (int) ifi->ifi_index != ifindex)
114 if (n->nlmsg_type == RTM_DELLINK) {
115 daemon_log(LOG_ERR, "Interface vanished.");
119 assert(n->nlmsg_type == RTM_NEWLINK);
121 if ((ifi->ifi_flags & IFF_LOOPBACK) ||
122 (ifi->ifi_flags & IFF_NOARP) ||
123 ifi->ifi_type != ARPHRD_ETHER) {
124 daemon_log(LOG_ERR, "Interface not suitable.");
128 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
130 /* An address was added or removed */
132 struct rtattr *a = NULL;
133 struct ifaddrmsg *ifa;
135 uint32_t address = 0;
140 if (ifa->ifa_family != AF_INET || (int) ifa->ifa_index != ifindex)
143 l = NLMSG_PAYLOAD(n, sizeof(*ifa));
146 while(RTA_OK(a, l)) {
148 switch(a->rta_type) {
151 assert(RTA_PAYLOAD(a) == 4);
152 memcpy(&address, RTA_DATA(a), sizeof(uint32_t));
159 if (!address || is_ll_address(address))
162 for (i = addresses; i; i = i->addresses_next)
163 if (i->address == address)
166 if (n->nlmsg_type == RTM_DELADDR && i) {
167 AVAHI_LLIST_REMOVE(Address, addresses, addresses, i);
169 } if (n->nlmsg_type == RTM_NEWADDR && !i) {
170 i = avahi_new(Address, 1);
171 i->address = address;
172 AVAHI_LLIST_PREPEND(Address, addresses, addresses, i);
179 static int process_response(int wait_for_done, unsigned seq) {
185 char replybuf[8*1024];
186 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
187 struct msghdr msghdr;
188 struct cmsghdr *cmsghdr;
191 struct nlmsghdr *p = (struct nlmsghdr *) replybuf;
193 memset(&iov, 0, sizeof(iov));
194 iov.iov_base = replybuf;
195 iov.iov_len = sizeof(replybuf);
197 memset(&msghdr, 0, sizeof(msghdr));
198 msghdr.msg_name = (void*) NULL;
199 msghdr.msg_namelen = 0;
200 msghdr.msg_iov = &iov;
201 msghdr.msg_iovlen = 1;
202 msghdr.msg_control = cred_msg;
203 msghdr.msg_controllen = sizeof(cred_msg);
204 msghdr.msg_flags = 0;
206 if ((r = recvmsg(fd, &msghdr, 0)) < 0) {
207 daemon_log(LOG_ERR, "recvmsg() failed: %s", strerror(errno));
211 if (!(cmsghdr = CMSG_FIRSTHDR(&msghdr)) || cmsghdr->cmsg_type != SCM_CREDENTIALS) {
212 daemon_log(LOG_WARNING, "No sender credentials received, ignoring data.");
216 ucred = (struct ucred*) CMSG_DATA(cmsghdr);
223 for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
225 if (!NLMSG_OK(p, bytes) || bytes < sizeof(struct nlmsghdr) || bytes < p->nlmsg_len) {
226 daemon_log(LOG_ERR, "Netlink packet too small.");
230 if (p->nlmsg_type == NLMSG_DONE && wait_for_done && p->nlmsg_seq == seq && (pid_t) p->nlmsg_pid == getpid())
233 if (p->nlmsg_type == NLMSG_ERROR) {
234 struct nlmsgerr *e = (struct nlmsgerr *) NLMSG_DATA (p);
237 daemon_log(LOG_ERR, "Netlink error: %s", strerror(-e->error));
242 if (process_nlmsg(p) < 0)
245 } while (wait_for_done);
250 int iface_get_initial_state(State *state) {
252 struct ifinfomsg *ifi;
253 struct ifaddrmsg *ifa;
260 memset(&req, 0, sizeof(req));
261 n = (struct nlmsghdr*) req;
262 n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
263 n->nlmsg_type = RTM_GETLINK;
265 n->nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP;
269 ifi->ifi_family = AF_UNSPEC;
270 ifi->ifi_change = -1;
272 if (send(fd, n, n->nlmsg_len, 0) < 0) {
273 daemon_log(LOG_ERR, "send(): %s", strerror(errno));
277 if (process_response(1, 0) < 0)
280 n->nlmsg_type = RTM_GETADDR;
281 n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifa));
282 n->nlmsg_seq = ++seq;
285 ifa->ifa_family = AF_INET;
286 ifa->ifa_index = ifindex;
288 if (send(fd, n, n->nlmsg_len, 0) < 0) {
289 daemon_log(LOG_ERR, "send(): %s", strerror(errno));
293 if (process_response(1, seq) < 0)
296 *state = addresses ? STATE_SLEEPING : STATE_START;
301 int iface_process(Event *event) {
307 if (process_response(0, 0) < 0)
311 *event = EVENT_ROUTABLE_ADDR_UNCONFIGURED;
312 else if (!b && addresses)
313 *event = EVENT_ROUTABLE_ADDR_CONFIGURED;
318 void iface_done(void) {
326 while ((a = addresses)) {
327 AVAHI_LLIST_REMOVE(Address, addresses, addresses, a);