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/netlink.h>
33 #include <linux/if_addr.h>
34 #include <linux/neighbour.h>
35 #include <linux/rtnetlink.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
39 #include <sys/epoll.h>
40 #include <sys/types.h>
42 #include <sys/syscall.h>
44 #include <netlink/msg.h>
45 #include <netlink/socket.h>
46 #include <netlink/attr.h>
47 #include <libubox/uloop.h>
52 static int ioctl_sock
;
53 static struct nl_sock
*rtnl_socket
= NULL
;
54 static int urandom_fd
= -1;
57 static void sighandler(_unused
int signal
)
62 static void print_usage(const char *app
)
66 " -h, --help Print this help\n"
67 " -l level Specify log level 0..7 (default %d)\n",
72 int main(int argc
, char **argv
)
74 openlog("odhcpd", LOG_PERROR
| LOG_PID
, LOG_DAEMON
);
76 int log_level
= LOG_INFO
;
77 while ((opt
= getopt(argc
, argv
, "hl:")) != -1) {
83 log_level
= atoi(optarg
);
84 fprintf(stderr
, "Log level set to %d\n", log_level
);
88 setlogmask(LOG_UPTO(log_level
));
92 syslog(LOG_ERR
, "Must be run as root!");
96 ioctl_sock
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
98 if (!(rtnl_socket
= odhcpd_create_nl_socket(NETLINK_ROUTE
))) {
99 syslog(LOG_ERR
, "Unable to open nl socket: %s", strerror(errno
));
103 if ((urandom_fd
= open("/dev/urandom", O_RDONLY
| O_CLOEXEC
)) < 0)
106 signal(SIGUSR1
, SIG_IGN
);
107 signal(SIGINT
, sighandler
);
108 signal(SIGTERM
, sighandler
);
126 struct nl_sock
*odhcpd_create_nl_socket(int protocol
)
128 struct nl_sock
*nl_sock
;
130 nl_sock
= nl_socket_alloc();
134 if (nl_connect(nl_sock
, protocol
) < 0)
141 nl_socket_free(nl_sock
);
147 // Read IPv6 MTU for interface
148 int odhcpd_get_interface_config(const char *ifname
, const char *what
)
151 const char *sysctl_pattern
= "/proc/sys/net/ipv6/conf/%s/%s";
152 snprintf(buf
, sizeof(buf
), sysctl_pattern
, ifname
, what
);
154 int fd
= open(buf
, O_RDONLY
);
155 ssize_t len
= read(fd
, buf
, sizeof(buf
) - 1);
166 // Read IPv6 MAC for interface
167 int odhcpd_get_mac(const struct interface
*iface
, uint8_t mac
[6])
170 memset(&ifr
, 0, sizeof(ifr
));
171 strncpy(ifr
.ifr_name
, iface
->ifname
, sizeof(ifr
.ifr_name
));
172 if (ioctl(ioctl_sock
, SIOCGIFHWADDR
, &ifr
) < 0)
174 memcpy(mac
, ifr
.ifr_hwaddr
.sa_data
, 6);
179 // Forwards a packet on a specific interface
180 ssize_t
odhcpd_send(int socket
, struct sockaddr_in6
*dest
,
181 struct iovec
*iov
, size_t iov_len
,
182 const struct interface
*iface
)
185 uint8_t cmsg_buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))] = {0};
186 struct msghdr msg
= {
187 .msg_name
= (void *) dest
,
188 .msg_namelen
= sizeof(*dest
),
190 .msg_iovlen
= iov_len
,
191 .msg_control
= cmsg_buf
,
192 .msg_controllen
= sizeof(cmsg_buf
),
196 // Set control data (define destination interface)
197 struct cmsghdr
*chdr
= CMSG_FIRSTHDR(&msg
);
198 chdr
->cmsg_level
= IPPROTO_IPV6
;
199 chdr
->cmsg_type
= IPV6_PKTINFO
;
200 chdr
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
201 struct in6_pktinfo
*pktinfo
= (struct in6_pktinfo
*)CMSG_DATA(chdr
);
202 pktinfo
->ipi6_ifindex
= iface
->ifindex
;
204 // Also set scope ID if link-local
205 if (IN6_IS_ADDR_LINKLOCAL(&dest
->sin6_addr
)
206 || IN6_IS_ADDR_MC_LINKLOCAL(&dest
->sin6_addr
))
207 dest
->sin6_scope_id
= iface
->ifindex
;
209 char ipbuf
[INET6_ADDRSTRLEN
];
210 inet_ntop(AF_INET6
, &dest
->sin6_addr
, ipbuf
, sizeof(ipbuf
));
212 ssize_t sent
= sendmsg(socket
, &msg
, MSG_DONTWAIT
);
214 syslog(LOG_NOTICE
, "Failed to send to %s%%%s (%s)",
215 ipbuf
, iface
->ifname
, strerror(errno
));
217 syslog(LOG_DEBUG
, "Sent %li bytes to %s%%%s",
218 (long)sent
, ipbuf
, iface
->ifname
);
224 struct odhcpd_ipaddr
*addrs
;
230 static int cb_valid_handler(struct nl_msg
*msg
, void *arg
)
232 struct addr_info
*ctxt
= (struct addr_info
*)arg
;
233 struct nlmsghdr
*hdr
= nlmsg_hdr(msg
);
234 struct ifaddrmsg
*ifa
;
235 struct nlattr
*nla
[__IFA_MAX
];
237 if (hdr
->nlmsg_type
!= RTM_NEWADDR
|| ctxt
->ret
>= (ssize_t
)ctxt
->addrs_sz
)
240 ifa
= NLMSG_DATA(hdr
);
241 if (ifa
->ifa_scope
!= RT_SCOPE_UNIVERSE
||
242 (ctxt
->ifindex
&& ifa
->ifa_index
!= (unsigned)ctxt
->ifindex
))
245 nlmsg_parse(hdr
, sizeof(*ifa
), nla
, __IFA_MAX
- 1, NULL
);
246 if (!nla
[IFA_ADDRESS
])
249 memset(&ctxt
->addrs
[ctxt
->ret
], 0, sizeof(ctxt
->addrs
[ctxt
->ret
]));
250 ctxt
->addrs
[ctxt
->ret
].prefix
= ifa
->ifa_prefixlen
;
252 nla_memcpy(&ctxt
->addrs
[ctxt
->ret
].addr
, nla
[IFA_ADDRESS
],
253 sizeof(ctxt
->addrs
[ctxt
->ret
].addr
));
255 if (nla
[IFA_CACHEINFO
]) {
256 struct ifa_cacheinfo
*ifc
= nla_data(nla
[IFA_CACHEINFO
]);
258 ctxt
->addrs
[ctxt
->ret
].preferred
= ifc
->ifa_prefered
;
259 ctxt
->addrs
[ctxt
->ret
].valid
= ifc
->ifa_valid
;
262 if (ifa
->ifa_flags
& IFA_F_DEPRECATED
)
263 ctxt
->addrs
[ctxt
->ret
].preferred
= 0;
270 static int cb_finish_handler(_unused
struct nl_msg
*msg
, void *arg
)
272 struct addr_info
*ctxt
= (struct addr_info
*)arg
;
279 static int cb_error_handler(_unused
struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
282 struct addr_info
*ctxt
= (struct addr_info
*)arg
;
285 ctxt
->ret
= err
->error
;
290 // Detect an IPV6-address currently assigned to the given interface
291 ssize_t
odhcpd_get_interface_addresses(int ifindex
,
292 struct odhcpd_ipaddr
*addrs
, size_t cnt
)
295 struct ifaddrmsg ifa
= {
296 .ifa_family
= AF_INET6
,
300 .ifa_index
= ifindex
, };
301 struct nl_cb
*cb
= nl_cb_alloc(NL_CB_DEFAULT
);
302 struct addr_info ctxt
= {
315 msg
= nlmsg_alloc_simple(RTM_GETADDR
, NLM_F_REQUEST
| NLM_F_DUMP
);
322 nlmsg_append(msg
, &ifa
, sizeof(ifa
), 0);
324 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_valid_handler
, &ctxt
);
325 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, cb_finish_handler
, &ctxt
);
326 nl_cb_err(cb
, NL_CB_CUSTOM
, cb_error_handler
, &ctxt
);
328 nl_send_auto_complete(rtnl_socket
, msg
);
329 while (ctxt
.pending
> 0)
330 nl_recvmsgs(rtnl_socket
, cb
);
339 int odhcpd_get_linklocal_interface_address(int ifindex
, struct in6_addr
*lladdr
)
342 struct sockaddr_in6 addr
= {AF_INET6
, 0, 0, ALL_IPV6_ROUTERS
, ifindex
};
343 socklen_t alen
= sizeof(addr
);
344 int sock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
);
346 if (!connect(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)) &&
347 !getsockname(sock
, (struct sockaddr
*)&addr
, &alen
)) {
348 *lladdr
= addr
.sin6_addr
;
357 int odhcpd_setup_route(const struct in6_addr
*addr
, const int prefixlen
,
358 const struct interface
*iface
, const struct in6_addr
*gw
,
359 const uint32_t metric
, const bool add
)
363 .rtm_family
= AF_INET6
,
364 .rtm_dst_len
= prefixlen
,
366 .rtm_table
= RT_TABLE_MAIN
,
367 .rtm_protocol
= (add
? RTPROT_STATIC
: RTPROT_UNSPEC
),
368 .rtm_scope
= (add
? (gw
? RT_SCOPE_UNIVERSE
: RT_SCOPE_LINK
) : RT_SCOPE_NOWHERE
),
369 .rtm_type
= (add
? RTN_UNICAST
: RTN_UNSPEC
),
373 msg
= nlmsg_alloc_simple(add
? RTM_NEWROUTE
: RTM_DELROUTE
,
374 add
? NLM_F_CREATE
| NLM_F_REPLACE
: 0);
378 nlmsg_append(msg
, &rtm
, sizeof(rtm
), 0);
380 nla_put(msg
, RTA_DST
, sizeof(*addr
), addr
);
381 nla_put_u32(msg
, RTA_OIF
, iface
->ifindex
);
382 nla_put_u32(msg
, RTA_PRIORITY
, metric
);
385 nla_put(msg
, RTA_GATEWAY
, sizeof(*gw
), gw
);
387 ret
= nl_send_auto_complete(rtnl_socket
, msg
);
393 return nl_wait_for_ack(rtnl_socket
);
396 int odhcpd_setup_proxy_neigh(const struct in6_addr
*addr
,
397 const struct interface
*iface
, const bool add
)
401 .ndm_family
= AF_INET6
,
402 .ndm_flags
= NTF_PROXY
,
403 .ndm_ifindex
= iface
->ifindex
,
405 int ret
= 0, flags
= NLM_F_REQUEST
;
408 flags
|= NLM_F_REPLACE
| NLM_F_CREATE
;
410 msg
= nlmsg_alloc_simple(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, flags
);
414 nlmsg_append(msg
, &ndm
, sizeof(ndm
), 0);
416 nla_put(msg
, NDA_DST
, sizeof(*addr
), addr
);
418 ret
= nl_send_auto_complete(rtnl_socket
, msg
);
424 return nl_wait_for_ack(rtnl_socket
);
427 struct interface
* odhcpd_get_interface_by_index(int ifindex
)
429 struct interface
*iface
;
430 list_for_each_entry(iface
, &interfaces
, head
)
431 if (iface
->ifindex
== ifindex
)
438 struct interface
* odhcpd_get_interface_by_name(const char *name
)
440 struct interface
*iface
;
441 list_for_each_entry(iface
, &interfaces
, head
)
442 if (!strcmp(iface
->ifname
, name
))
449 struct interface
* odhcpd_get_master_interface(void)
451 struct interface
*iface
;
452 list_for_each_entry(iface
, &interfaces
, head
)
460 // Convenience function to receive and do basic validation of packets
461 static void odhcpd_receive_packets(struct uloop_fd
*u
, _unused
unsigned int events
)
463 struct odhcpd_event
*e
= container_of(u
, struct odhcpd_event
, uloop
);
465 uint8_t data_buf
[RELAYD_BUFFER_SIZE
], cmsg_buf
[128];
467 struct sockaddr_in6 in6
;
468 struct sockaddr_in in
;
469 struct sockaddr_ll ll
;
470 struct sockaddr_nl nl
;
475 socklen_t ret_len
= sizeof(ret
);
476 getsockopt(u
->fd
, SOL_SOCKET
, SO_ERROR
, &ret
, &ret_len
);
479 e
->handle_error(e
, ret
);
488 struct iovec iov
= {data_buf
, sizeof(data_buf
)};
489 struct msghdr msg
= {
490 .msg_name
= (void *) &addr
,
491 .msg_namelen
= sizeof(addr
),
494 .msg_control
= cmsg_buf
,
495 .msg_controllen
= sizeof(cmsg_buf
),
499 ssize_t len
= recvmsg(u
->fd
, &msg
, MSG_DONTWAIT
);
508 // Extract destination interface
512 struct in6_pktinfo
*pktinfo
;
513 struct in_pktinfo
*pkt4info
;
514 for (struct cmsghdr
*ch
= CMSG_FIRSTHDR(&msg
); ch
!= NULL
; ch
= CMSG_NXTHDR(&msg
, ch
)) {
515 if (ch
->cmsg_level
== IPPROTO_IPV6
&&
516 ch
->cmsg_type
== IPV6_PKTINFO
) {
517 pktinfo
= (struct in6_pktinfo
*)CMSG_DATA(ch
);
518 destiface
= pktinfo
->ipi6_ifindex
;
519 dest
= &pktinfo
->ipi6_addr
;
520 } else if (ch
->cmsg_level
== IPPROTO_IP
&&
521 ch
->cmsg_type
== IP_PKTINFO
) {
522 pkt4info
= (struct in_pktinfo
*)CMSG_DATA(ch
);
523 destiface
= pkt4info
->ipi_ifindex
;
524 dest
= &pkt4info
->ipi_addr
;
525 } else if (ch
->cmsg_level
== IPPROTO_IPV6
&&
526 ch
->cmsg_type
== IPV6_HOPLIMIT
) {
527 hlim
= (int*)CMSG_DATA(ch
);
531 // Check hoplimit if received
532 if (hlim
&& *hlim
!= 255)
535 // Detect interface for packet sockets
536 if (addr
.ll
.sll_family
== AF_PACKET
)
537 destiface
= addr
.ll
.sll_ifindex
;
539 struct interface
*iface
=
540 odhcpd_get_interface_by_index(destiface
);
542 if (!iface
&& addr
.nl
.nl_family
!= AF_NETLINK
)
545 char ipbuf
[INET6_ADDRSTRLEN
] = "kernel";
546 if (addr
.ll
.sll_family
== AF_PACKET
&&
547 len
>= (ssize_t
)sizeof(struct ip6_hdr
))
548 inet_ntop(AF_INET6
, &data_buf
[8], ipbuf
, sizeof(ipbuf
));
549 else if (addr
.in6
.sin6_family
== AF_INET6
)
550 inet_ntop(AF_INET6
, &addr
.in6
.sin6_addr
, ipbuf
, sizeof(ipbuf
));
551 else if (addr
.in
.sin_family
== AF_INET
)
552 inet_ntop(AF_INET
, &addr
.in
.sin_addr
, ipbuf
, sizeof(ipbuf
));
554 syslog(LOG_DEBUG
, "Received %li Bytes from %s%%%s", (long)len
,
555 ipbuf
, (iface
) ? iface
->ifname
: "netlink");
557 e
->handle_dgram(&addr
, data_buf
, len
, iface
, dest
);
561 // Register events for the multiplexer
562 int odhcpd_register(struct odhcpd_event
*event
)
564 event
->uloop
.cb
= odhcpd_receive_packets
;
565 return uloop_fd_add(&event
->uloop
, ULOOP_READ
|
566 ((event
->handle_error
) ? ULOOP_ERROR_CB
: 0));
569 void odhcpd_process(struct odhcpd_event
*event
)
571 odhcpd_receive_packets(&event
->uloop
, 0);
574 int odhcpd_urandom(void *data
, size_t len
)
576 return read(urandom_fd
, data
, len
);
580 time_t odhcpd_time(void)
583 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &ts
);
588 static const char hexdigits
[] = "0123456789abcdef";
589 static const int8_t hexvals
[] = {
590 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
591 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
592 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
593 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
594 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
595 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
596 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
597 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
600 ssize_t
odhcpd_unhexlify(uint8_t *dst
, size_t len
, const char *src
)
603 for (c
= 0; c
< len
&& src
[0] && src
[1]; ++c
) {
604 int8_t x
= (int8_t)*src
++;
605 int8_t y
= (int8_t)*src
++;
606 if (x
< 0 || (x
= hexvals
[x
]) < 0
607 || y
< 0 || (y
= hexvals
[y
]) < 0)
610 while (((int8_t)*src
) < 0 ||
611 (*src
&& hexvals
[(uint8_t)*src
] < 0))
619 void odhcpd_hexlify(char *dst
, const uint8_t *src
, size_t len
)
621 for (size_t i
= 0; i
< len
; ++i
) {
622 *dst
++ = hexdigits
[src
[i
] >> 4];
623 *dst
++ = hexdigits
[src
[i
] & 0x0f];
629 int odhcpd_bmemcmp(const void *av
, const void *bv
, size_t bits
)
631 const uint8_t *a
= av
, *b
= bv
;
632 size_t bytes
= bits
/ 8;
635 int res
= memcmp(a
, b
, bytes
);
636 if (res
== 0 && bits
> 0)
637 res
= (a
[bytes
] >> (8 - bits
)) - (b
[bytes
] >> (8 - bits
));
643 void odhcpd_bmemcpy(void *av
, const void *bv
, size_t bits
)
646 const uint8_t *b
= bv
;
648 size_t bytes
= bits
/ 8;
653 uint8_t mask
= (1 << (8 - bits
)) - 1;
654 a
[bytes
] = (a
[bytes
] & mask
) | ((~mask
) & b
[bytes
]);