The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=7d8b5bd3f15ba99343e2d93c53ac73cc4bc550f0
commit 7d8b5bd3f15ba99343e2d93c53ac73cc4bc550f0 Author: Gleb Smirnoff <[email protected]> AuthorDate: 2026-12-04 18:01:57 +0000 Commit: Gleb Smirnoff <[email protected]> CommitDate: 2026-12-04 18:01:57 +0000 bootpd(8): add minimal RFC1542 support and improve multihoming Support the broadcast flag from RFC1542. With this flag a BOOTP client requests the server to send initial reply as a broadcast, all-ones both on the IP and on Ethernet layers. The EDK2 Firmware for bhyve from the sysutils/edk2-bhyve sets the broadcast flag and is not able to boot if server doesn't support it. This change makes edk2-bhyve with stock bootpd(8) bootable. Improve multihoming. With multiple interfaces in the system bootpd does a bad job sending replies. Set IP_RECVIF on the socket and upon receive find the interface we received the packet and then its primary IP address & broadcast. Use the broadcast address later with IP_ONESBCAST socket option if client requested broadcast reply. Use the primary IP address as server address in case if sa= is missing in the bootptab(5). Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D62411 --- libexec/bootpd/Makefile | 2 +- libexec/bootpd/README | 2 - libexec/bootpd/bootp.h | 1 + libexec/bootpd/bootpd.c | 162 +++++++++++++++++++++++++++++++--------------- libexec/bootpd/getif.c | 142 ---------------------------------------- libexec/bootpd/getif.h | 3 - libexec/bootpd/trygetif.c | 72 --------------------- 7 files changed, 111 insertions(+), 273 deletions(-) diff --git a/libexec/bootpd/Makefile b/libexec/bootpd/Makefile index 7d07cac1cc6c..b7eead32700b 100644 --- a/libexec/bootpd/Makefile +++ b/libexec/bootpd/Makefile @@ -9,7 +9,7 @@ WARNS?= 2 SUBDIR= bootpgw tools SRCS= bootpd.c dovend.c readfile.c hash.c dumptab.c \ - lookup.c getif.c hwaddr.c report.c tzone.c rtmsg.c + lookup.c hwaddr.c report.c tzone.c rtmsg.c MAN= bootptab.5 bootpd.8 MLINKS= bootpd.8 bootpgw.8 diff --git a/libexec/bootpd/README b/libexec/bootpd/README index 0901b2578598..6f09bc3f390c 100644 --- a/libexec/bootpd/README +++ b/libexec/bootpd/README @@ -113,8 +113,6 @@ dovend.h header for above dumptab.c Implements debugging dump for bootpd getether.c For bootptest (not used yet) getether.h header for above -getif.c Get network interface info. -getif.h header for above hash.c The hash table module hash.h header for above hwaddr.c Hardware address support diff --git a/libexec/bootpd/bootp.h b/libexec/bootpd/bootp.h index 343efb459327..2cc558f25276 100644 --- a/libexec/bootpd/bootp.h +++ b/libexec/bootpd/bootp.h @@ -47,6 +47,7 @@ struct bootp { u_int32 bp_xid; /* transaction ID */ unsigned short bp_secs; /* seconds since boot began */ unsigned short bp_flags; /* RFC1532 broadcast, etc. */ +#define BP_FLAG_BROADCAST 0x8000 struct in_addr bp_ciaddr; /* client IP address */ struct in_addr bp_yiaddr; /* 'your' IP address */ struct in_addr bp_siaddr; /* server IP address */ diff --git a/libexec/bootpd/bootpd.c b/libexec/bootpd/bootpd.c index 31e1967884ad..3cf2566d86a4 100644 --- a/libexec/bootpd/bootpd.c +++ b/libexec/bootpd/bootpd.c @@ -47,6 +47,7 @@ SOFTWARE. #include <sys/utsname.h> #include <net/if.h> +#include <net/if_dl.h> #include <netinet/in.h> #include <arpa/inet.h> /* inet_ntoa */ @@ -65,6 +66,8 @@ SOFTWARE. #include <syslog.h> #include <assert.h> #include <inttypes.h> +#include <ifaddrs.h> +#include <stdbool.h> #ifdef NO_SETSID # include <fcntl.h> /* for O_RDONLY, etc */ @@ -75,7 +78,6 @@ SOFTWARE. #include "hwaddr.h" #include "bootpd.h" #include "dovend.h" -#include "getif.h" #include "readfile.h" #include "report.h" #include "tzone.h" @@ -119,9 +121,9 @@ u_short bootps_port, bootpc_port; */ struct sockaddr_in bind_addr; /* Listening */ -struct sockaddr_in recv_addr; /* Packet source */ -struct sockaddr_in send_addr; /* destination */ - +struct sockaddr_in recv_addr; /* address of receive interface */ +struct sockaddr_in bcast_addr; /* broadcast address of receive interface */ +u_short ifindex; /* index of receive interface */ /* * option defaults @@ -138,8 +140,9 @@ int arpmod = TRUE; /* modify the ARP table */ * General */ -int s; /* Socket file descriptor */ -char *pktbuf; /* Receive packet buffer */ +static int s; /* Socket file descriptor */ +static char pktbuf[MAX_MSG_SIZE]; /* Receive packet buffer */ + int pktlen; char *progname; char *chdir_path; @@ -174,7 +177,7 @@ main(int argc, char **argv) struct servent *servp; struct hostent *hep; char *stmp; - socklen_t ba_len, ra_len; + socklen_t ba_len; int n; int nfound; fd_set readfds; @@ -200,12 +203,6 @@ main(int argc, char **argv) /* Debugging for compilers with struct padding. */ assert(sizeof(struct bootp) == BP_MINPKTSZ); - /* Get space for receiving packets and composing replies. */ - pktbuf = malloc(MAX_MSG_SIZE); - if (!pktbuf) { - report(LOG_ERR, "malloc failed"); - exit(1); - } bp = (struct bootp *) pktbuf; /* @@ -494,12 +491,40 @@ main(int argc, char **argv) } #endif /* SA_NOCLDSTOP */ + if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &(int){1}, + sizeof(int)) != 0) { + report(LOG_ERR, "setsockopt(SO_BROADCAST): %s", + get_network_errmsg()); + exit(1); + } + if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &(int){1}, + sizeof(int)) != 0) { + report(LOG_ERR, "setsockopt(IP_RECVIF): %s", + get_network_errmsg()); + exit(1); + } + /* * Process incoming requests. */ FD_ZERO(&readfds); for (;;) { + struct iovec iov = { + .iov_base = pktbuf, + .iov_len = sizeof(pktbuf) + }; + char cbuf[CMSG_SPACE(sizeof(struct sockaddr_dl))]; + struct sockaddr_in from; + struct msghdr msg = { + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_name = &from, + .msg_namelen = sizeof(from), + .msg_control = cbuf, + .msg_controllen = sizeof(cbuf) + }; struct timeval tv; + struct ifaddrs *ifa0, *ifa; FD_SET(s, &readfds); if (timeout) @@ -531,15 +556,45 @@ main(int argc, char **argv) (intmax_t)actualtimeout.tv_sec / 60); exit(0); } - ra_len = sizeof(recv_addr); - n = recvfrom(s, pktbuf, MAX_MSG_SIZE, 0, - (struct sockaddr *) &recv_addr, &ra_len); + n = recvmsg(s, &msg, 0); if (n <= 0) { continue; } + /* + * Find primary address of the receive interface, to be used + * later with sendto(2). + */ + ifindex = 0; + for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; + cmsg = CMSG_NXTHDR(&msg, cmsg)) + if (cmsg->cmsg_level == IPPROTO_IP && + cmsg->cmsg_type == IP_RECVIF) + ifindex = ((struct sockaddr_dl *) + CMSG_DATA(cmsg))->sdl_index; + if (ifindex == 0) { + report(LOG_ERR, "failed to find IP_RECVIF"); + continue; + } + recv_addr.sin_addr.s_addr = 0; + bcast_addr.sin_addr.s_addr = 0; + getifaddrs(&ifa0); + for (ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr->sa_family != AF_INET || + if_nametoindex(ifa->ifa_name) != ifindex || + (ifa->ifa_flags & IFF_BROADCAST) == 0) + continue; + recv_addr = *(struct sockaddr_in *)ifa->ifa_addr; + if (ifa->ifa_broadaddr != NULL) + bcast_addr = *(struct sockaddr_in *) + ifa->ifa_broadaddr; + } + freeifaddrs(ifa0); if (debug > 1) { - report(LOG_INFO, "recvd pkt from IP addr %s", - inet_ntoa(recv_addr.sin_addr)); + char name[IFNAMSIZ]; + + report(LOG_NOTICE, "recvd pkt from IP addr %s on %s", + inet_ntoa(from.sin_addr), + if_indextoname(ifindex, name)); } if (n < sizeof(struct bootp)) { if (debug) { @@ -1009,23 +1064,19 @@ handle_reply(void) PRIVATE void sendreply(int forward, int32 dst_override) { + struct sockaddr_in send_addr; struct bootp *bp = (struct bootp *) pktbuf; struct in_addr dst; u_short port = bootpc_port; unsigned char *ha; int len, haf; - - /* - * XXX - Should honor bp_flags "broadcast" bit here. - * Temporary workaround: use the :ra=ADDR: option to - * set the reply address to the broadcast address. - */ + bool broadcast = false; /* * If the destination address was specified explicitly * (i.e. the broadcast address for HP compatibility) * then send the response to that address. Otherwise, - * act in accordance with RFC951: + * act in accordance with RFC951 and RFC1542: * If the client IP address is specified, use that * else if gateway IP address is specified, use that * else make a temporary arp cache entry for the client's @@ -1046,6 +1097,19 @@ sendreply(int forward, int32 dst_override) report(LOG_INFO, "sending reply to gateway %s", inet_ntoa(dst)); } + } else if (ntohs(bp->bp_flags) & BP_FLAG_BROADCAST) { + broadcast = true; + if (setsockopt(s, IPPROTO_IP, IP_ONESBCAST, &(int){1}, + sizeof(int)) != 0) + report(LOG_ERR, "setsockopt(IP_ONESBCAST): %s", + get_network_errmsg()); + dst = bcast_addr.sin_addr; + if (debug > 1) { + char name[IFNAMSIZ]; + + report(LOG_INFO, "sending reply to broadcast at %s", + if_indextoname(ifindex, name)); + } } else { dst = bp->bp_yiaddr; ha = bp->bp_chaddr; @@ -1067,48 +1131,40 @@ sendreply(int forward, int32 dst_override) if ((forward == 0) && (bp->bp_siaddr.s_addr == 0)) { - struct ifreq *ifr; - struct in_addr siaddr; /* - * If we are originating this reply, we - * need to find our own interface address to - * put in the bp_siaddr field of the reply. - * If this server is multi-homed, pick the - * 'best' interface (the one on the same net - * as the client). Of course, the client may + * If we are originating this reply, we need to find our own + * interface address to put in the bp_siaddr field of the reply. + * Use primary address of interface packet was received on. + * Of course, the client may * be on the other side of a BOOTP gateway... */ - ifr = getif(s, &dst); - if (ifr) { - struct sockaddr_in *sip; - sip = (struct sockaddr_in *) &(ifr->ifr_addr); - siaddr = sip->sin_addr; - } else { + if (recv_addr.sin_addr.s_addr != 0) + bp->bp_siaddr = recv_addr.sin_addr; + else /* Just use my "official" IP address. */ - siaddr = my_ip_addr; - } - + bp->bp_siaddr = my_ip_addr; /* XXX - No need to set bp_giaddr here. */ - - /* Finally, set the server address field. */ - bp->bp_siaddr = siaddr; } + + bp->bp_flags = 0; + /* Set up socket address for send. */ send_addr.sin_family = AF_INET; send_addr.sin_port = htons(port); send_addr.sin_addr = dst; /* Send reply with same size packet as request used. */ - if (sendto(s, pktbuf, pktlen, 0, - (struct sockaddr *) &send_addr, - sizeof(send_addr)) < 0) - { - report(LOG_ERR, "sendto: %s", get_network_errmsg()); + if (sendto(s, pktbuf, pktlen, 0, (struct sockaddr *) &send_addr, + sizeof(send_addr)) < 0) { + report(LOG_ERR, "sendto(%s%s): %s", inet_ntoa(dst), + broadcast ? " IP_ONESBCAST" : "", get_network_errmsg()); } -} /* sendreply */ - + if (broadcast && setsockopt(s, IPPROTO_IP, IP_ONESBCAST, &(int){0}, + sizeof(int)) != 0) + report(LOG_ERR, "setsockopt(~IP_ONESBCAST): %s", + get_network_errmsg()); +} -/* nmatch() - now in getif.c */ /* setarp() - now in hwaddr.c */ diff --git a/libexec/bootpd/getif.c b/libexec/bootpd/getif.c deleted file mode 100644 index 6d2ca48eaf42..000000000000 --- a/libexec/bootpd/getif.c +++ /dev/null @@ -1,142 +0,0 @@ -/* - * getif.c : get an interface structure - */ - -#include <sys/types.h> -#include <sys/socket.h> -#include <sys/ioctl.h> - -#if defined(SUNOS) || defined(SVR4) -#include <sys/sockio.h> -#endif -#ifdef SVR4 -#include <sys/stropts.h> -#endif - -#include <sys/time.h> /* for struct timeval in net/if.h */ -#include <net/if.h> /* for struct ifreq */ -#include <netinet/in.h> - -#ifndef NO_UNISTD -#include <unistd.h> -#endif -#include <syslog.h> -#include <errno.h> -#include <assert.h> - -#include "getif.h" -#include "report.h" - -#ifdef __bsdi__ -#define BSD 43 -#endif - -static struct ifreq ifreq[10]; /* Holds interface configuration */ -static struct ifconf ifconf; /* points to ifreq */ - -static int nmatch(u_char *ca, u_char *cb); - -/* Return a pointer to the interface struct for the passed address. */ -struct ifreq * -getif(int s, struct in_addr *addrp) -{ - int maxmatch; - int len, m, incr; - struct ifreq *ifrq, *ifrmax; - struct sockaddr_in *sip; - char *p; - - /* If no address was supplied, just return NULL. */ - if (!addrp) - return (struct ifreq *) 0; - - /* Get the interface config if not done already. */ - if (ifconf.ifc_len == 0) { -#ifdef SVR4 - /* - * SysVr4 returns garbage if you do this the obvious way! - * This one took a while to figure out... -gwr - */ - struct strioctl ioc; - ioc.ic_cmd = SIOCGIFCONF; - ioc.ic_timout = 0; - ioc.ic_len = sizeof(ifreq); - ioc.ic_dp = (char *) ifreq; - m = ioctl(s, I_STR, (char *) &ioc); - ifconf.ifc_len = ioc.ic_len; - ifconf.ifc_req = ifreq; -#else /* SVR4 */ - ifconf.ifc_len = sizeof(ifreq); - ifconf.ifc_req = ifreq; - m = ioctl(s, SIOCGIFCONF, (caddr_t) & ifconf); -#endif /* SVR4 */ - if ((m < 0) || (ifconf.ifc_len <= 0)) { - report(LOG_ERR, "ioctl SIOCGIFCONF"); - return (struct ifreq *) 0; - } - } - maxmatch = 7; /* this many bits or less... */ - ifrmax = (struct ifreq *) 0;/* ... is not a valid match */ - p = (char *) ifreq; - len = ifconf.ifc_len; - while (len > 0) { - ifrq = (struct ifreq *) p; - sip = (struct sockaddr_in *) &ifrq->ifr_addr; - m = nmatch((u_char *)addrp, (u_char *)&(sip->sin_addr)); - if (m > maxmatch) { - maxmatch = m; - ifrmax = ifrq; - } -#ifndef IFNAMSIZ - /* BSD not defined or earlier than 4.3 */ - incr = sizeof(*ifrq); -#else - incr = ifrq->ifr_addr.sa_len + IFNAMSIZ; -#endif - - p += incr; - len -= incr; - } - - return ifrmax; -} - -/* - * Return the number of leading bits matching in the - * internet addresses supplied. - */ -static int -nmatch(u_char *ca, u_char *cb) -{ - u_int m = 0; /* count of matching bits */ - u_int n = 4; /* bytes left, then bitmask */ - - /* Count matching bytes. */ - while (n && (*ca == *cb)) { - ca++; - cb++; - m += 8; - n--; - } - /* Now count matching bits. */ - if (n) { - n = 0x80; - while (n && ((*ca & n) == (*cb & n))) { - m++; - n >>= 1; - } - } - return (m); -} - -/* - * Local Variables: - * tab-width: 4 - * c-indent-level: 4 - * c-argdecl-indent: 4 - * c-continued-statement-offset: 4 - * c-continued-brace-offset: -4 - * c-label-offset: -4 - * c-brace-offset: 0 - * End: - */ diff --git a/libexec/bootpd/getif.h b/libexec/bootpd/getif.h deleted file mode 100644 index 3199b6c151dc..000000000000 --- a/libexec/bootpd/getif.h +++ /dev/null @@ -1,3 +0,0 @@ -/* getif.h */ - -extern struct ifreq *getif(int, struct in_addr *); diff --git a/libexec/bootpd/trygetif.c b/libexec/bootpd/trygetif.c deleted file mode 100644 index 8c78ee34247a..000000000000 --- a/libexec/bootpd/trygetif.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * trygetif.c - test program for getif.c - */ - -#include <sys/types.h> -#include <sys/socket.h> - -#if defined(SUNOS) || defined(SVR4) -#include <sys/sockio.h> -#endif - -#ifdef _AIX32 -#include <sys/time.h> /* for struct timeval in net/if.h */ -#endif -#include <net/if.h> /* for struct ifreq */ -#include <netinet/in.h> -#include <arpa/inet.h> /* inet_ntoa */ - -#include <netdb.h> -#include <stdio.h> -#include <ctype.h> -#include <errno.h> - -#include "getif.h" - -int debug = 0; -char *progname; - -void -main(argc, argv) - int argc; - char **argv; -{ - struct hostent *hep; - struct sockaddr_in *sip; /* Interface address */ - struct ifreq *ifr; - struct in_addr dst_addr; - struct in_addr *dap; - int s; - - progname = argv[0]; /* for report */ - - dap = NULL; - if (argc > 1) { - dap = &dst_addr; - if (isdigit(argv[1][0])) - dst_addr.s_addr = inet_addr(argv[1]); - else { - hep = gethostbyname(argv[1]); - if (!hep) { - printf("gethostbyname(%s)\n", argv[1]); - exit(1); - } - memcpy(&dst_addr, hep->h_addr, sizeof(dst_addr)); - } - } - s = socket(AF_INET, SOCK_DGRAM, 0); - if (s < 0) { - perror("socket open"); - exit(1); - } - ifr = getif(s, dap); - if (!ifr) { - printf("no interface for address\n"); - exit(1); - } - printf("Intf-name:%s\n", ifr->ifr_name); - sip = (struct sockaddr_in *) &(ifr->ifr_addr); - printf("Intf-addr:%s\n", inet_ntoa(sip->sin_addr)); - - exit(0); -}
