1 /* dnsmasq is Copyright (c) 2000-2013 Simon Kelley
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifdef HAVE_LINUX_NETWORK
21 #include <linux/types.h>
22 #include <linux/netlink.h>
23 #include <linux/rtnetlink.h>
25 /* linux 2.6.19 buggers up the headers, patch it up here. */
28 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
30 # include <linux/if_addr.h>
34 # define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
38 static struct iovec iov
;
39 static u32 netlink_pid
;
41 static int nl_async(struct nlmsghdr
*h
);
42 static void nl_newaddress(time_t now
);
44 void netlink_init(void)
46 struct sockaddr_nl addr
;
47 socklen_t slen
= sizeof(addr
);
49 addr
.nl_family
= AF_NETLINK
;
51 addr
.nl_pid
= 0; /* autobind */
52 addr
.nl_groups
= RTMGRP_IPV4_ROUTE
;
53 if (option_bool(OPT_CLEVERBIND
))
54 addr
.nl_groups
|= RTMGRP_IPV4_IFADDR
;
56 addr
.nl_groups
|= RTMGRP_IPV6_ROUTE
;
57 if (option_bool(OPT_CLEVERBIND
))
58 addr
.nl_groups
|= RTMGRP_IPV6_IFADDR
;
61 if (daemon
->doing_ra
|| daemon
->doing_dhcp6
)
62 addr
.nl_groups
|= RTMGRP_IPV6_IFADDR
;
65 /* May not be able to have permission to set multicast groups don't die in that case */
66 if ((daemon
->netlinkfd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
)) != -1)
68 if (bind(daemon
->netlinkfd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1)
71 if (errno
!= EPERM
|| bind(daemon
->netlinkfd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1)
72 daemon
->netlinkfd
= -1;
76 if (daemon
->netlinkfd
== -1 ||
77 getsockname(daemon
->netlinkfd
, (struct sockaddr
*)&addr
, &slen
) == 1)
78 die(_("cannot create netlink socket: %s"), NULL
, EC_MISC
);
80 /* save pid assigned by bind() and retrieved by getsockname() */
81 netlink_pid
= addr
.nl_pid
;
84 iov
.iov_base
= safe_malloc(iov
.iov_len
);
87 static ssize_t
netlink_recv(void)
90 struct sockaddr_nl nladdr
;
95 msg
.msg_control
= NULL
;
96 msg
.msg_controllen
= 0;
97 msg
.msg_name
= &nladdr
;
98 msg
.msg_namelen
= sizeof(nladdr
);
103 while ((rc
= recvmsg(daemon
->netlinkfd
, &msg
, MSG_PEEK
| MSG_TRUNC
)) == -1 && errno
== EINTR
);
105 /* make buffer big enough */
106 if (rc
!= -1 && (msg
.msg_flags
& MSG_TRUNC
))
108 /* Very new Linux kernels return the actual size needed, older ones always return truncated size */
109 if ((size_t)rc
== iov
.iov_len
)
111 if (expand_buf(&iov
, rc
+ 100))
115 expand_buf(&iov
, rc
);
118 /* read it for real */
120 while ((rc
= recvmsg(daemon
->netlinkfd
, &msg
, 0)) == -1 && errno
== EINTR
);
122 /* Make sure this is from the kernel */
123 if (rc
== -1 || nladdr
.nl_pid
== 0)
127 /* discard stuff which is truncated at this point (expand_buf() may fail) */
128 if (msg
.msg_flags
& MSG_TRUNC
)
138 /* family = AF_UNSPEC finds ARP table entries.
139 family = AF_LOCAL finds MAC addresses. */
140 int iface_enumerate(int family
, void *parm
, int (*callback
)())
142 struct sockaddr_nl addr
;
145 static unsigned int seq
= 0;
146 int callback_ok
= 1, newaddr
= 0;
153 addr
.nl_family
= AF_NETLINK
;
156 addr
.nl_pid
= 0; /* address to kernel */
159 if (family
== AF_UNSPEC
)
160 req
.nlh
.nlmsg_type
= RTM_GETNEIGH
;
161 else if (family
== AF_LOCAL
)
162 req
.nlh
.nlmsg_type
= RTM_GETLINK
;
164 req
.nlh
.nlmsg_type
= RTM_GETADDR
;
166 req
.nlh
.nlmsg_len
= sizeof(req
);
167 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
| NLM_F_ACK
;
168 req
.nlh
.nlmsg_pid
= 0;
169 req
.nlh
.nlmsg_seq
= ++seq
;
170 req
.g
.rtgen_family
= family
;
172 /* Don't block in recvfrom if send fails */
173 while((len
= sendto(daemon
->netlinkfd
, (void *)&req
, sizeof(req
), 0,
174 (struct sockaddr
*)&addr
, sizeof(addr
))) == -1 && retry_send());
181 if ((len
= netlink_recv()) == -1)
183 if (errno
== ENOBUFS
)
191 for (h
= (struct nlmsghdr
*)iov
.iov_base
; NLMSG_OK(h
, (size_t)len
); h
= NLMSG_NEXT(h
, len
))
192 if (h
->nlmsg_seq
!= seq
|| h
->nlmsg_pid
!= netlink_pid
|| h
->nlmsg_type
== NLMSG_ERROR
)
194 /* May be multicast arriving async */
198 enumerate_interfaces(1); /* reset */
201 else if (h
->nlmsg_type
== NLMSG_DONE
)
203 /* handle async new interface address arrivals, these have to be done
204 after we complete as we're not re-entrant */
206 nl_newaddress(dnsmasq_time());
210 else if (h
->nlmsg_type
== RTM_NEWADDR
&& family
!= AF_UNSPEC
&& family
!= AF_LOCAL
)
212 struct ifaddrmsg
*ifa
= NLMSG_DATA(h
);
213 struct rtattr
*rta
= IFA_RTA(ifa
);
214 unsigned int len1
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
));
216 if (ifa
->ifa_family
== family
)
218 if (ifa
->ifa_family
== AF_INET
)
220 struct in_addr netmask
, addr
, broadcast
;
223 netmask
.s_addr
= htonl(0xffffffff << (32 - ifa
->ifa_prefixlen
));
225 broadcast
.s_addr
= 0;
227 while (RTA_OK(rta
, len1
))
229 if (rta
->rta_type
== IFA_LOCAL
)
230 addr
= *((struct in_addr
*)(rta
+1));
231 else if (rta
->rta_type
== IFA_BROADCAST
)
232 broadcast
= *((struct in_addr
*)(rta
+1));
233 else if (rta
->rta_type
== IFA_LABEL
)
234 label
= RTA_DATA(rta
);
236 rta
= RTA_NEXT(rta
, len1
);
239 if (addr
.s_addr
&& callback_ok
)
240 if (!((*callback
)(addr
, ifa
->ifa_index
, label
, netmask
, broadcast
, parm
)))
244 else if (ifa
->ifa_family
== AF_INET6
)
246 struct in6_addr
*addrp
= NULL
;
247 u32 valid
= 0, preferred
= 0;
250 while (RTA_OK(rta
, len1
))
252 if (rta
->rta_type
== IFA_ADDRESS
)
253 addrp
= ((struct in6_addr
*)(rta
+1));
254 else if (rta
->rta_type
== IFA_CACHEINFO
)
256 struct ifa_cacheinfo
*ifc
= (struct ifa_cacheinfo
*)(rta
+1);
257 preferred
= ifc
->ifa_prefered
;
258 valid
= ifc
->ifa_valid
;
260 rta
= RTA_NEXT(rta
, len1
);
263 if (ifa
->ifa_flags
& IFA_F_TENTATIVE
)
264 flags
|= IFACE_TENTATIVE
;
266 if (ifa
->ifa_flags
& IFA_F_DEPRECATED
)
267 flags
|= IFACE_DEPRECATED
;
269 if (ifa
->ifa_flags
& IFA_F_PERMANENT
)
270 flags
|= IFACE_PERMANENT
;
272 if (addrp
&& callback_ok
)
273 if (!((*callback
)(addrp
, (int)(ifa
->ifa_prefixlen
), (int)(ifa
->ifa_scope
),
274 (int)(ifa
->ifa_index
), flags
,
275 (int) preferred
, (int)valid
, parm
)))
281 else if (h
->nlmsg_type
== RTM_NEWNEIGH
&& family
== AF_UNSPEC
)
283 struct ndmsg
*neigh
= NLMSG_DATA(h
);
284 struct rtattr
*rta
= NDA_RTA(neigh
);
285 unsigned int len1
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*neigh
));
287 char *inaddr
= NULL
, *mac
= NULL
;
289 while (RTA_OK(rta
, len1
))
291 if (rta
->rta_type
== NDA_DST
)
292 inaddr
= (char *)(rta
+1);
293 else if (rta
->rta_type
== NDA_LLADDR
)
295 maclen
= rta
->rta_len
- sizeof(struct rtattr
);
296 mac
= (char *)(rta
+1);
299 rta
= RTA_NEXT(rta
, len1
);
302 if (inaddr
&& mac
&& callback_ok
)
303 if (!((*callback
)(neigh
->ndm_family
, inaddr
, mac
, maclen
, parm
)))
307 else if (h
->nlmsg_type
== RTM_NEWLINK
&& family
== AF_LOCAL
)
309 struct ifinfomsg
*link
= NLMSG_DATA(h
);
310 struct rtattr
*rta
= IFLA_RTA(link
);
311 unsigned int len1
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*link
));
315 while (RTA_OK(rta
, len1
))
317 if (rta
->rta_type
== IFLA_ADDRESS
)
319 maclen
= rta
->rta_len
- sizeof(struct rtattr
);
320 mac
= (char *)(rta
+1);
323 rta
= RTA_NEXT(rta
, len1
);
326 if (mac
&& callback_ok
&& !((link
->ifi_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
))) &&
327 !((*callback
)((int)link
->ifi_index
, (unsigned int)link
->ifi_type
, mac
, maclen
, parm
)))
334 void netlink_multicast(time_t now
)
338 int flags
, newaddr
= 0;
340 /* don't risk blocking reading netlink messages here. */
341 if ((flags
= fcntl(daemon
->netlinkfd
, F_GETFL
)) == -1 ||
342 fcntl(daemon
->netlinkfd
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
345 if ((len
= netlink_recv()) != -1)
346 for (h
= (struct nlmsghdr
*)iov
.iov_base
; NLMSG_OK(h
, (size_t)len
); h
= NLMSG_NEXT(h
, len
))
350 /* restore non-blocking status */
351 fcntl(daemon
->netlinkfd
, F_SETFL
, flags
);
357 static int nl_async(struct nlmsghdr
*h
)
359 if (h
->nlmsg_type
== NLMSG_ERROR
)
361 struct nlmsgerr
*err
= NLMSG_DATA(h
);
363 my_syslog(LOG_ERR
, _("netlink returns error: %s"), strerror(-(err
->error
)));
366 else if (h
->nlmsg_pid
== 0 && h
->nlmsg_type
== RTM_NEWROUTE
)
368 /* We arrange to receive netlink multicast messages whenever the network route is added.
369 If this happens and we still have a DNS packet in the buffer, we re-send it.
370 This helps on DoD links, where frequently the packet which triggers dialling is
371 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
373 struct rtmsg
*rtm
= NLMSG_DATA(h
);
375 if (rtm
->rtm_type
== RTN_UNICAST
&& rtm
->rtm_scope
== RT_SCOPE_LINK
)
377 /* Force re-reading resolv file right now, for luck. */
378 daemon
->last_resolv
= 0;
380 if (daemon
->srv_save
)
384 if (daemon
->srv_save
->sfd
)
385 fd
= daemon
->srv_save
->sfd
->fd
;
386 else if (daemon
->rfd_save
&& daemon
->rfd_save
->refcount
!= 0)
387 fd
= daemon
->rfd_save
->fd
;
391 while(sendto(fd
, daemon
->packet
, daemon
->packet_len
, 0,
392 &daemon
->srv_save
->addr
.sa
, sa_len(&daemon
->srv_save
->addr
)) == -1 && retry_send());
397 else if (h
->nlmsg_type
== RTM_NEWADDR
|| h
->nlmsg_type
== RTM_DELADDR
)
398 return 1; /* clever bind mode - rescan */
403 static void nl_newaddress(time_t now
)
405 if (option_bool(OPT_CLEVERBIND
) || daemon
->doing_dhcp6
|| daemon
->relay6
|| daemon
->doing_ra
)
406 enumerate_interfaces(0);
408 if (option_bool(OPT_CLEVERBIND
))
409 create_bound_listeners(0);
412 if (daemon
->doing_dhcp6
|| daemon
->relay6
|| daemon
->doing_ra
)
415 if (daemon
->doing_dhcp6
|| daemon
->doing_ra
)
416 dhcp_construct_contexts(now
);
418 if (daemon
->doing_dhcp6
)
419 lease_find_interfaces(now
);