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;
52 static void sighandler(_unused
int signal
)
60 openlog("odhcpd", LOG_PERROR
| LOG_PID
, LOG_DAEMON
);
61 setlogmask(LOG_UPTO(LOG_WARNING
));
65 syslog(LOG_ERR
, "Must be run as root!");
69 ioctl_sock
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
71 if ((rtnl_socket
= odhcpd_open_rtnl()) < 0) {
72 syslog(LOG_ERR
, "Unable to open socket: %s", strerror(errno
));
76 if ((urandom_fd
= open("/dev/urandom", O_RDONLY
| O_CLOEXEC
)) < 0)
79 signal(SIGUSR1
, SIG_IGN
);
80 signal(SIGINT
, sighandler
);
81 signal(SIGTERM
, sighandler
);
99 int odhcpd_open_rtnl(void)
101 int sock
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, NETLINK_ROUTE
);
103 // Connect to the kernel netlink interface
104 struct sockaddr_nl nl
= {.nl_family
= AF_NETLINK
};
105 if (connect(sock
, (struct sockaddr
*)&nl
, sizeof(nl
))) {
106 syslog(LOG_ERR
, "Failed to connect to kernel rtnetlink: %s",
115 // Read IPv6 MTU for interface
116 int odhcpd_get_interface_mtu(const char *ifname
)
119 const char *sysctl_pattern
= "/proc/sys/net/ipv6/conf/%s/mtu";
120 snprintf(buf
, sizeof(buf
), sysctl_pattern
, ifname
);
122 int fd
= open(buf
, O_RDONLY
);
123 ssize_t len
= read(fd
, buf
, sizeof(buf
) - 1);
136 // Read IPv6 MAC for interface
137 int odhcpd_get_mac(const struct interface
*iface
, uint8_t mac
[6])
140 memset(&ifr
, 0, sizeof(ifr
));
141 strncpy(ifr
.ifr_name
, iface
->ifname
, sizeof(ifr
.ifr_name
));
142 if (ioctl(ioctl_sock
, SIOCGIFHWADDR
, &ifr
) < 0)
144 memcpy(mac
, ifr
.ifr_hwaddr
.sa_data
, 6);
149 // Forwards a packet on a specific interface
150 ssize_t
odhcpd_send(int socket
, struct sockaddr_in6
*dest
,
151 struct iovec
*iov
, size_t iov_len
,
152 const struct interface
*iface
)
155 uint8_t cmsg_buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))] = {0};
156 struct msghdr msg
= {(void*)dest
, sizeof(*dest
), iov
, iov_len
,
157 cmsg_buf
, sizeof(cmsg_buf
), 0};
159 // Set control data (define destination interface)
160 struct cmsghdr
*chdr
= CMSG_FIRSTHDR(&msg
);
161 chdr
->cmsg_level
= IPPROTO_IPV6
;
162 chdr
->cmsg_type
= IPV6_PKTINFO
;
163 chdr
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
164 struct in6_pktinfo
*pktinfo
= (struct in6_pktinfo
*)CMSG_DATA(chdr
);
165 pktinfo
->ipi6_ifindex
= iface
->ifindex
;
167 // Also set scope ID if link-local
168 if (IN6_IS_ADDR_LINKLOCAL(&dest
->sin6_addr
)
169 || IN6_IS_ADDR_MC_LINKLOCAL(&dest
->sin6_addr
))
170 dest
->sin6_scope_id
= iface
->ifindex
;
172 // IPV6_PKTINFO doesn't really work for IPv6-raw sockets (bug?)
173 if (dest
->sin6_port
== 0) {
174 msg
.msg_control
= NULL
;
175 msg
.msg_controllen
= 0;
178 char ipbuf
[INET6_ADDRSTRLEN
];
179 inet_ntop(AF_INET6
, &dest
->sin6_addr
, ipbuf
, sizeof(ipbuf
));
181 ssize_t sent
= sendmsg(socket
, &msg
, MSG_DONTWAIT
);
183 syslog(LOG_NOTICE
, "Failed to send to %s%%%s (%s)",
184 ipbuf
, iface
->ifname
, strerror(errno
));
186 syslog(LOG_DEBUG
, "Sent %li bytes to %s%%%s",
187 (long)sent
, ipbuf
, iface
->ifname
);
192 // Detect an IPV6-address currently assigned to the given interface
193 ssize_t
odhcpd_get_interface_addresses(int ifindex
,
194 struct odhcpd_ipaddr
*addrs
, size_t cnt
)
198 struct ifaddrmsg ifa
;
199 } req
= {{sizeof(req
), RTM_GETADDR
, NLM_F_REQUEST
| NLM_F_DUMP
,
200 ++rtnl_seq
, 0}, {AF_INET6
, 0, 0, 0, ifindex
}};
201 if (send(rtnl_socket
, &req
, sizeof(req
), 0) < (ssize_t
)sizeof(req
))
205 ssize_t len
= 0, ret
= 0;
207 for (struct nlmsghdr
*nhm
= NULL
; ; nhm
= NLMSG_NEXT(nhm
, len
)) {
208 while (len
< 0 || !NLMSG_OK(nhm
, (size_t)len
)) {
209 len
= recv(rtnl_socket
, buf
, sizeof(buf
), 0);
210 nhm
= (struct nlmsghdr
*)buf
;
211 if (len
< 0 || !NLMSG_OK(nhm
, (size_t)len
)) {
219 if (nhm
->nlmsg_type
!= RTM_NEWADDR
)
222 // Skip address but keep clearing socket buffer
223 if (ret
>= (ssize_t
)cnt
)
226 struct ifaddrmsg
*ifa
= NLMSG_DATA(nhm
);
227 if (ifa
->ifa_scope
!= RT_SCOPE_UNIVERSE
||
228 (ifindex
&& ifa
->ifa_index
!= (unsigned)ifindex
))
231 struct rtattr
*rta
= (struct rtattr
*)&ifa
[1];
232 size_t alen
= NLMSG_PAYLOAD(nhm
, sizeof(*ifa
));
233 memset(&addrs
[ret
], 0, sizeof(addrs
[ret
]));
234 addrs
[ret
].prefix
= ifa
->ifa_prefixlen
;
236 while (RTA_OK(rta
, alen
)) {
237 if (rta
->rta_type
== IFA_ADDRESS
) {
238 memcpy(&addrs
[ret
].addr
, RTA_DATA(rta
),
239 sizeof(struct in6_addr
));
240 } else if (rta
->rta_type
== IFA_CACHEINFO
) {
241 struct ifa_cacheinfo
*ifc
= RTA_DATA(rta
);
242 addrs
[ret
].preferred
= ifc
->ifa_prefered
;
243 addrs
[ret
].valid
= ifc
->ifa_valid
;
246 rta
= RTA_NEXT(rta
, alen
);
249 if (ifa
->ifa_flags
& IFA_F_DEPRECATED
)
250 addrs
[ret
].preferred
= 0;
252 addrs
[ret
].has_class
= false;
253 addrs
[ret
].class = 0;
255 struct interface
*iface
= odhcpd_get_interface_by_index(ifindex
);
257 addrs
[ret
].has_class
= ubus_get_class(iface
->ifname
,
258 &addrs
[ret
].addr
, &addrs
[ret
].class);
267 struct interface
* odhcpd_get_interface_by_index(int ifindex
)
269 struct interface
*iface
;
270 list_for_each_entry(iface
, &interfaces
, head
)
271 if (iface
->ifindex
== ifindex
)
278 struct interface
* odhcpd_get_interface_by_name(const char *name
)
280 struct interface
*iface
;
281 list_for_each_entry(iface
, &interfaces
, head
)
282 if (!strcmp(iface
->ifname
, name
))
289 struct interface
* odhcpd_get_master_interface(void)
291 struct interface
*iface
;
292 list_for_each_entry(iface
, &interfaces
, head
)
300 // Convenience function to receive and do basic validation of packets
301 static void odhcpd_receive_packets(struct uloop_fd
*u
, _unused
unsigned int events
)
303 struct odhcpd_event
*e
= container_of(u
, struct odhcpd_event
, uloop
);
305 uint8_t data_buf
[RELAYD_BUFFER_SIZE
], cmsg_buf
[128];
307 struct sockaddr_in6 in6
;
308 struct sockaddr_in in
;
309 struct sockaddr_ll ll
;
310 struct sockaddr_nl nl
;
314 struct iovec iov
= {data_buf
, sizeof(data_buf
)};
315 struct msghdr msg
= {&addr
, sizeof(addr
), &iov
, 1,
316 cmsg_buf
, sizeof(cmsg_buf
), 0};
318 ssize_t len
= recvmsg(u
->fd
, &msg
, MSG_DONTWAIT
);
327 // Extract destination interface
331 struct in6_pktinfo
*pktinfo
;
332 struct in_pktinfo
*pkt4info
;
333 for (struct cmsghdr
*ch
= CMSG_FIRSTHDR(&msg
); ch
!= NULL
; ch
= CMSG_NXTHDR(&msg
, ch
)) {
334 if (ch
->cmsg_level
== IPPROTO_IPV6
&&
335 ch
->cmsg_type
== IPV6_PKTINFO
) {
336 pktinfo
= (struct in6_pktinfo
*)CMSG_DATA(ch
);
337 destiface
= pktinfo
->ipi6_ifindex
;
338 dest
= &pktinfo
->ipi6_addr
;
339 } else if (ch
->cmsg_level
== IPPROTO_IP
&&
340 ch
->cmsg_type
== IP_PKTINFO
) {
341 pkt4info
= (struct in_pktinfo
*)CMSG_DATA(ch
);
342 destiface
= pkt4info
->ipi_ifindex
;
343 dest
= &pkt4info
->ipi_addr
;
344 } else if (ch
->cmsg_level
== IPPROTO_IPV6
&&
345 ch
->cmsg_type
== IPV6_HOPLIMIT
) {
346 hlim
= (int*)CMSG_DATA(ch
);
350 // Check hoplimit if received
351 if (hlim
&& *hlim
!= 255)
354 // Detect interface for packet sockets
355 if (addr
.ll
.sll_family
== AF_PACKET
)
356 destiface
= addr
.ll
.sll_ifindex
;
358 struct interface
*iface
=
359 odhcpd_get_interface_by_index(destiface
);
361 if (!iface
&& addr
.nl
.nl_family
!= AF_NETLINK
)
364 char ipbuf
[INET6_ADDRSTRLEN
] = "kernel";
365 if (addr
.ll
.sll_family
== AF_PACKET
&&
366 len
>= (ssize_t
)sizeof(struct ip6_hdr
))
367 inet_ntop(AF_INET6
, &data_buf
[8], ipbuf
, sizeof(ipbuf
));
368 else if (addr
.in6
.sin6_family
== AF_INET6
)
369 inet_ntop(AF_INET6
, &addr
.in6
.sin6_addr
, ipbuf
, sizeof(ipbuf
));
370 else if (addr
.in
.sin_family
== AF_INET
)
371 inet_ntop(AF_INET
, &addr
.in
.sin_addr
, ipbuf
, sizeof(ipbuf
));
373 syslog(LOG_DEBUG
, "--");
374 syslog(LOG_DEBUG
, "Received %li Bytes from %s%%%s", (long)len
,
375 ipbuf
, (iface
) ? iface
->ifname
: "netlink");
377 e
->handle_dgram(&addr
, data_buf
, len
, iface
, dest
);
381 // Register events for the multiplexer
382 int odhcpd_register(struct odhcpd_event
*event
)
384 event
->uloop
.cb
= odhcpd_receive_packets
;
385 return uloop_fd_add(&event
->uloop
, ULOOP_READ
);
388 void odhcpd_process(struct odhcpd_event
*event
)
390 odhcpd_receive_packets(&event
->uloop
, 0);
393 void odhcpd_urandom(void *data
, size_t len
)
395 read(urandom_fd
, data
, len
);
399 time_t odhcpd_time(void)
402 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &ts
);
407 static const char hexdigits
[] = "0123456789abcdef";
408 static const int8_t hexvals
[] = {
409 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
410 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
411 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
412 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
413 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
414 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
415 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
416 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
419 ssize_t
odhcpd_unhexlify(uint8_t *dst
, size_t len
, const char *src
)
422 for (c
= 0; c
< len
&& src
[0] && src
[1]; ++c
) {
423 int8_t x
= (int8_t)*src
++;
424 int8_t y
= (int8_t)*src
++;
425 if (x
< 0 || (x
= hexvals
[x
]) < 0
426 || y
< 0 || (y
= hexvals
[y
]) < 0)
429 while (((int8_t)*src
) < 0 ||
430 (*src
&& hexvals
[(uint8_t)*src
] < 0))
438 void odhcpd_hexlify(char *dst
, const uint8_t *src
, size_t len
)
440 for (size_t i
= 0; i
< len
; ++i
) {
441 *dst
++ = hexdigits
[src
[i
] >> 4];
442 *dst
++ = hexdigits
[src
[i
] & 0x0f];
448 int odhcpd_bmemcmp(const void *av
, const void *bv
, size_t bits
)
450 const uint8_t *a
= av
, *b
= bv
;
451 size_t bytes
= bits
/ 8;
454 int res
= memcmp(a
, b
, bytes
);
455 if (res
== 0 && bits
> 0)
456 res
= (a
[bytes
] >> (8 - bits
)) - (b
[bytes
] >> (8 - bits
));
462 void odhcpd_bmemcpy(void *av
, const void *bv
, size_t bits
)
465 const uint8_t *b
= bv
;
467 size_t bytes
= bits
/ 8;
472 uint8_t mask
= (1 << (8 - bits
)) - 1;
473 a
[bytes
] = (a
[bytes
] & mask
) | ((~mask
) & b
[bytes
]);