2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
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.
28 #include <arpa/inet.h>
30 #include <netinet/ip6.h>
31 #include <netpacket/packet.h>
32 #include <linux/rtnetlink.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <sys/epoll.h>
37 #include <sys/types.h>
39 #include <sys/syscall.h>
41 #include <libubox/uloop.h>
46 static int ioctl_sock
;
47 static int rtnl_socket
= -1;
48 static int rtnl_seq
= 0;
49 static int urandom_fd
= -1;
54 openlog("odhcpd", LOG_PERROR
| LOG_PID
, LOG_DAEMON
);
58 syslog(LOG_ERR
, "Must be run as root!");
62 ioctl_sock
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
64 if ((rtnl_socket
= odhcpd_open_rtnl()) < 0) {
65 syslog(LOG_ERR
, "Unable to open socket: %s", strerror(errno
));
69 if ((urandom_fd
= open("/dev/urandom", O_RDONLY
| O_CLOEXEC
)) < 0)
72 signal(SIGUSR1
, SIG_IGN
);
93 int odhcpd_open_rtnl(void)
95 int sock
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, NETLINK_ROUTE
);
97 // Connect to the kernel netlink interface
98 struct sockaddr_nl nl
= {.nl_family
= AF_NETLINK
};
99 if (connect(sock
, (struct sockaddr
*)&nl
, sizeof(nl
))) {
100 syslog(LOG_ERR
, "Failed to connect to kernel rtnetlink: %s",
109 // Read IPv6 MTU for interface
110 int odhcpd_get_interface_mtu(const char *ifname
)
113 const char *sysctl_pattern
= "/proc/sys/net/ipv6/conf/%s/mtu";
114 snprintf(buf
, sizeof(buf
), sysctl_pattern
, ifname
);
116 int fd
= open(buf
, O_RDONLY
);
117 ssize_t len
= read(fd
, buf
, sizeof(buf
) - 1);
130 // Read IPv6 MAC for interface
131 int odhcpd_get_mac(const struct interface
*iface
, uint8_t mac
[6])
134 strncpy(ifr
.ifr_name
, iface
->ifname
, sizeof(ifr
.ifr_name
));
135 if (ioctl(ioctl_sock
, SIOCGIFHWADDR
, &ifr
) < 0)
137 memcpy(mac
, ifr
.ifr_hwaddr
.sa_data
, 6);
142 // Forwards a packet on a specific interface
143 ssize_t
odhcpd_send(int socket
, struct sockaddr_in6
*dest
,
144 struct iovec
*iov
, size_t iov_len
,
145 const struct interface
*iface
)
148 uint8_t cmsg_buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))] = {0};
149 struct msghdr msg
= {(void*)dest
, sizeof(*dest
), iov
, iov_len
,
150 cmsg_buf
, sizeof(cmsg_buf
), 0};
152 // Set control data (define destination interface)
153 struct cmsghdr
*chdr
= CMSG_FIRSTHDR(&msg
);
154 chdr
->cmsg_level
= IPPROTO_IPV6
;
155 chdr
->cmsg_type
= IPV6_PKTINFO
;
156 chdr
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
157 struct in6_pktinfo
*pktinfo
= (struct in6_pktinfo
*)CMSG_DATA(chdr
);
158 pktinfo
->ipi6_ifindex
= iface
->ifindex
;
160 // Also set scope ID if link-local
161 if (IN6_IS_ADDR_LINKLOCAL(&dest
->sin6_addr
)
162 || IN6_IS_ADDR_MC_LINKLOCAL(&dest
->sin6_addr
))
163 dest
->sin6_scope_id
= iface
->ifindex
;
165 // IPV6_PKTINFO doesn't really work for IPv6-raw sockets (bug?)
166 if (dest
->sin6_port
== 0) {
167 msg
.msg_control
= NULL
;
168 msg
.msg_controllen
= 0;
171 char ipbuf
[INET6_ADDRSTRLEN
];
172 inet_ntop(AF_INET6
, &dest
->sin6_addr
, ipbuf
, sizeof(ipbuf
));
174 ssize_t sent
= sendmsg(socket
, &msg
, MSG_DONTWAIT
);
176 syslog(LOG_WARNING
, "Failed to send to %s%%%s (%s)",
177 ipbuf
, iface
->ifname
, strerror(errno
));
179 syslog(LOG_NOTICE
, "Sent %li bytes to %s%%%s",
180 (long)sent
, ipbuf
, iface
->ifname
);
185 // Detect an IPV6-address currently assigned to the given interface
186 ssize_t
odhcpd_get_interface_addresses(int ifindex
,
187 struct odhcpd_ipaddr
*addrs
, size_t cnt
)
191 struct ifaddrmsg ifa
;
192 } req
= {{sizeof(req
), RTM_GETADDR
, NLM_F_REQUEST
| NLM_F_DUMP
,
193 ++rtnl_seq
, 0}, {AF_INET6
, 0, 0, 0, ifindex
}};
194 if (send(rtnl_socket
, &req
, sizeof(req
), 0) < (ssize_t
)sizeof(req
))
198 ssize_t len
= 0, ret
= 0;
200 for (struct nlmsghdr
*nhm
= NULL
; ; nhm
= NLMSG_NEXT(nhm
, len
)) {
201 while (len
< 0 || !NLMSG_OK(nhm
, (size_t)len
)) {
202 len
= recv(rtnl_socket
, buf
, sizeof(buf
), 0);
203 nhm
= (struct nlmsghdr
*)buf
;
204 if (len
< 0 || !NLMSG_OK(nhm
, (size_t)len
)) {
212 if (nhm
->nlmsg_type
!= RTM_NEWADDR
)
215 // Skip address but keep clearing socket buffer
216 if (ret
>= (ssize_t
)cnt
)
219 struct ifaddrmsg
*ifa
= NLMSG_DATA(nhm
);
220 if (ifa
->ifa_scope
!= RT_SCOPE_UNIVERSE
||
221 ifa
->ifa_index
!= (unsigned)ifindex
)
224 struct rtattr
*rta
= (struct rtattr
*)&ifa
[1];
225 size_t alen
= NLMSG_PAYLOAD(nhm
, sizeof(*ifa
));
226 memset(&addrs
[ret
], 0, sizeof(addrs
[ret
]));
227 addrs
[ret
].prefix
= ifa
->ifa_prefixlen
;
229 while (RTA_OK(rta
, alen
)) {
230 if (rta
->rta_type
== IFA_ADDRESS
) {
231 memcpy(&addrs
[ret
].addr
, RTA_DATA(rta
),
232 sizeof(struct in6_addr
));
233 } else if (rta
->rta_type
== IFA_CACHEINFO
) {
234 struct ifa_cacheinfo
*ifc
= RTA_DATA(rta
);
235 addrs
[ret
].preferred
= ifc
->ifa_prefered
;
236 addrs
[ret
].valid
= ifc
->ifa_valid
;
239 rta
= RTA_NEXT(rta
, alen
);
242 if (ifa
->ifa_flags
& IFA_F_DEPRECATED
)
243 addrs
[ret
].preferred
= 0;
252 struct interface
* odhcpd_get_interface_by_index(int ifindex
)
254 struct interface
*iface
;
255 list_for_each_entry(iface
, &interfaces
, head
)
256 if (iface
->ifindex
== ifindex
)
263 struct interface
* odhcpd_get_interface_by_name(const char *name
)
265 struct interface
*iface
;
266 list_for_each_entry(iface
, &interfaces
, head
)
267 if (!strcmp(iface
->ifname
, name
))
274 struct interface
* odhcpd_get_master_interface(void)
276 struct interface
*iface
;
277 list_for_each_entry(iface
, &interfaces
, head
)
285 // Convenience function to receive and do basic validation of packets
286 static void odhcpd_receive_packets(struct uloop_fd
*u
, _unused
unsigned int events
)
288 struct odhcpd_event
*e
= container_of(u
, struct odhcpd_event
, uloop
);
290 uint8_t data_buf
[RELAYD_BUFFER_SIZE
], cmsg_buf
[128];
292 struct sockaddr_in6 in6
;
293 struct sockaddr_in in
;
294 struct sockaddr_ll ll
;
295 struct sockaddr_nl nl
;
299 struct iovec iov
= {data_buf
, sizeof(data_buf
)};
300 struct msghdr msg
= {&addr
, sizeof(addr
), &iov
, 1,
301 cmsg_buf
, sizeof(cmsg_buf
), 0};
303 ssize_t len
= recvmsg(u
->fd
, &msg
, MSG_DONTWAIT
);
312 // Extract destination interface
315 struct in6_pktinfo
*pktinfo
;
316 struct in_pktinfo
*pkt4info
;
317 for (struct cmsghdr
*ch
= CMSG_FIRSTHDR(&msg
); ch
!= NULL
&&
318 destiface
== 0; ch
= CMSG_NXTHDR(&msg
, ch
)) {
319 if (ch
->cmsg_level
== IPPROTO_IPV6
&&
320 ch
->cmsg_type
== IPV6_PKTINFO
) {
321 pktinfo
= (struct in6_pktinfo
*)CMSG_DATA(ch
);
322 destiface
= pktinfo
->ipi6_ifindex
;
323 } else if (ch
->cmsg_level
== IPPROTO_IP
&&
324 ch
->cmsg_type
== IP_PKTINFO
) {
325 pkt4info
= (struct in_pktinfo
*)CMSG_DATA(ch
);
326 destiface
= pkt4info
->ipi_ifindex
;
327 } else if (ch
->cmsg_level
== IPPROTO_IPV6
&&
328 ch
->cmsg_type
== IPV6_HOPLIMIT
) {
329 hlim
= (int*)CMSG_DATA(ch
);
333 // Check hoplimit if received
334 if (hlim
&& *hlim
!= 255)
337 // Detect interface for packet sockets
338 if (addr
.ll
.sll_family
== AF_PACKET
)
339 destiface
= addr
.ll
.sll_ifindex
;
341 struct interface
*iface
=
342 odhcpd_get_interface_by_index(destiface
);
344 if (!iface
&& addr
.nl
.nl_family
!= AF_NETLINK
)
347 char ipbuf
[INET6_ADDRSTRLEN
] = "kernel";
348 if (addr
.ll
.sll_family
== AF_PACKET
&&
349 len
>= (ssize_t
)sizeof(struct ip6_hdr
))
350 inet_ntop(AF_INET6
, &data_buf
[8], ipbuf
, sizeof(ipbuf
));
351 else if (addr
.in6
.sin6_family
== AF_INET6
)
352 inet_ntop(AF_INET6
, &addr
.in6
.sin6_addr
, ipbuf
, sizeof(ipbuf
));
353 else if (addr
.in
.sin_family
== AF_INET
)
354 inet_ntop(AF_INET
, &addr
.in
.sin_addr
, ipbuf
, sizeof(ipbuf
));
356 syslog(LOG_NOTICE
, "--");
357 syslog(LOG_NOTICE
, "Received %li Bytes from %s%%%s", (long)len
,
358 ipbuf
, (iface
) ? iface
->ifname
: "netlink");
360 e
->handle_dgram(&addr
, data_buf
, len
, iface
);
364 // Register events for the multiplexer
365 int odhcpd_register(struct odhcpd_event
*event
)
367 event
->uloop
.cb
= odhcpd_receive_packets
;
368 return uloop_fd_add(&event
->uloop
, ULOOP_READ
);
371 void odhcpd_urandom(void *data
, size_t len
)
373 read(urandom_fd
, data
, len
);
377 time_t odhcpd_time(void)
380 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &ts
);
385 static const char hexdigits
[] = "0123456789abcdef";
386 static const int8_t hexvals
[] = {
387 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
388 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
389 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
390 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
391 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
392 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
393 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
394 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
397 ssize_t
odhcpd_unhexlify(uint8_t *dst
, size_t len
, const char *src
)
400 for (c
= 0; c
< len
&& src
[0] && src
[1]; ++c
) {
401 int8_t x
= (int8_t)*src
++;
402 int8_t y
= (int8_t)*src
++;
403 if (x
< 0 || (x
= hexvals
[x
]) < 0
404 || y
< 0 || (y
= hexvals
[y
]) < 0)
407 while (((int8_t)*src
) < 0 ||
408 (*src
&& hexvals
[(uint8_t)*src
] < 0))
416 void odhcpd_hexlify(char *dst
, const uint8_t *src
, size_t len
)
418 for (size_t i
= 0; i
< len
; ++i
) {
419 *dst
++ = hexdigits
[src
[i
] >> 4];
420 *dst
++ = hexdigits
[src
[i
] & 0x0f];