diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2014-11-23 14:02:22 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2014-11-23 14:02:22 +0000 |
commit | 2c8a6fa2520bbf73b45b575bb02660214cbf9c5b (patch) | |
tree | c82378324566d105bbeb5cc6288625f1d7ff2350 | |
parent | 2083da42359123572c6a4498a3be6fb40ffc2518 (diff) |
Cleanup some struct interface_info fields. Make 'nomedia' a flag. Replace
the two copies of the bpf socket descriptor (rfdesc, wfdesc) with
just one (bfdesc). No need to keep a struct ifreq (ifp) since it's
only used once and can be constructed there and discarded. Nuke
unused 'primary_address' member.
No intentional functional change.
-rw-r--r-- | sbin/dhclient/bpf.c | 34 | ||||
-rw-r--r-- | sbin/dhclient/dhcpd.h | 11 | ||||
-rw-r--r-- | sbin/dhclient/dispatch.c | 29 |
3 files changed, 30 insertions, 44 deletions
diff --git a/sbin/dhclient/bpf.c b/sbin/dhclient/bpf.c index 6ffdfd369dd..770ba1a2b88 100644 --- a/sbin/dhclient/bpf.c +++ b/sbin/dhclient/bpf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bpf.c,v 1.35 2014/11/08 22:08:01 krw Exp $ */ +/* $OpenBSD: bpf.c,v 1.36 2014/11/23 14:02:21 krw Exp $ */ /* BPF socket interface code, originally contributed by Archie Cobbs. */ @@ -61,6 +61,7 @@ int if_register_bpf(void) { char filename[50]; + struct ifreq ifr; int sock, b; /* Open a BPF device */ @@ -78,8 +79,9 @@ if_register_bpf(void) } /* Set the BPF device to point at this interface. */ - if (ioctl(sock, BIOCSETIF, ifi->ifp) < 0) - error("Can't attach interface %s to bpf device %s: %s", + strlcpy(ifr.ifr_name, ifi->name, IFNAMSIZ); + if (ioctl(sock, BIOCSETIF, &ifr) < 0) + error("Can't attach interface %s to %s: %s", ifi->name, filename, strerror(errno)); return (sock); @@ -91,12 +93,6 @@ if_register_send(void) int sock, on = 1; /* - * If we're using the bpf API for sending and receiving, we - * don't need to register this interface twice. - */ - ifi->wfdesc = ifi->rfdesc; - - /* * Use raw socket for unicast send. */ if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1) @@ -194,10 +190,10 @@ if_register_receive(void) int flag = 1, sz; /* Open a BPF device and hang it on this interface. */ - ifi->rfdesc = if_register_bpf(); + ifi->bfdesc = if_register_bpf(); /* Make sure the BPF version is in range. */ - if (ioctl(ifi->rfdesc, BIOCVERSION, &v) < 0) + if (ioctl(ifi->bfdesc, BIOCVERSION, &v) < 0) error("Can't get BPF version: %s", strerror(errno)); if (v.bv_major != BPF_MAJOR_VERSION || @@ -209,16 +205,16 @@ if_register_receive(void) * comes in, rather than waiting for the input buffer to fill * with packets. */ - if (ioctl(ifi->rfdesc, BIOCIMMEDIATE, &flag) < 0) + if (ioctl(ifi->bfdesc, BIOCIMMEDIATE, &flag) < 0) error("Can't set immediate mode on bpf device: %s", strerror(errno)); - if (ioctl(ifi->rfdesc, BIOCSFILDROP, &flag) < 0) + if (ioctl(ifi->bfdesc, BIOCSFILDROP, &flag) < 0) error("Can't set filter-drop mode on bpf device: %s", strerror(errno)); /* Get the required BPF buffer length from the kernel. */ - if (ioctl(ifi->rfdesc, BIOCGBLEN, &sz) < 0) + if (ioctl(ifi->bfdesc, BIOCGBLEN, &sz) < 0) error("Can't get bpf buffer length: %s", strerror(errno)); ifi->rbuf_max = sz; ifi->rbuf = malloc(ifi->rbuf_max); @@ -239,7 +235,7 @@ if_register_receive(void) */ dhcp_bpf_filter[8].k = LOCAL_PORT; - if (ioctl(ifi->rfdesc, BIOCSETF, &p) < 0) + if (ioctl(ifi->bfdesc, BIOCSETF, &p) < 0) error("Can't install packet filter program: %s", strerror(errno)); @@ -250,11 +246,11 @@ if_register_receive(void) if (dhcp_bpf_wfilter[7].k == 0x1fff) dhcp_bpf_wfilter[7].k = htons(IP_MF|IP_OFFMASK); - if (ioctl(ifi->rfdesc, BIOCSETWF, &p) < 0) + if (ioctl(ifi->bfdesc, BIOCSETWF, &p) < 0) error("Can't install write filter program: %s", strerror(errno)); - if (ioctl(ifi->rfdesc, BIOCLOCK, NULL) < 0) + if (ioctl(ifi->bfdesc, BIOCLOCK, NULL) < 0) error("Cannot lock bpf"); } @@ -319,7 +315,7 @@ send_packet(struct in_addr from, struct in_addr to) iovcnt++; if (to.s_addr == INADDR_BROADCAST) { - result = writev(ifi->wfdesc, iov, iovcnt); + result = writev(ifi->bfdesc, iov, iovcnt); } else { memset(&msg, 0, sizeof(msg)); msg.msg_name = (struct sockaddr *)&dest; @@ -353,7 +349,7 @@ receive_packet(struct sockaddr_in *from, struct ether_addr *hfrom) do { /* If the buffer is empty, fill it. */ if (ifi->rbuf_offset == ifi->rbuf_len) { - length = read(ifi->rfdesc, ifi->rbuf, ifi->rbuf_max); + length = read(ifi->bfdesc, ifi->rbuf, ifi->rbuf_max); if (length <= 0) return (length); ifi->rbuf_offset = 0; diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h index 11dfd149dc2..9e5e28460d6 100644 --- a/sbin/dhclient/dhcpd.h +++ b/sbin/dhclient/dhcpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpd.h,v 1.142 2014/11/01 15:49:07 krw Exp $ */ +/* $OpenBSD: dhcpd.h,v 1.143 2014/11/23 14:02:21 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> @@ -171,17 +171,13 @@ struct client_state { struct interface_info { struct ether_addr hw_address; - struct in_addr primary_address; char name[IFNAMSIZ]; - int rfdesc; - int wfdesc; - int ufdesc; /* unicast */ + int bfdesc; /* bpf - reading & broadcast writing*/ + int ufdesc; /* udp - unicast writing */ unsigned char *rbuf; size_t rbuf_max; size_t rbuf_offset; size_t rbuf_len; - struct ifreq *ifp; - int noifmedia; int errors; u_int16_t index; int linkstat; @@ -189,6 +185,7 @@ struct interface_info { int flags; #define IFI_NEW_LLADDR 0x00000001 #define IFI_HUP 0x00000002 +#define IFI_NOMEDIA 0x00000004 }; struct dhcp_timeout { diff --git a/sbin/dhclient/dispatch.c b/sbin/dhclient/dispatch.c index 15b59026462..42f21cf2ae4 100644 --- a/sbin/dhclient/dispatch.c +++ b/sbin/dhclient/dispatch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dispatch.c,v 1.92 2014/11/16 12:12:01 krw Exp $ */ +/* $OpenBSD: dispatch.c,v 1.93 2014/11/23 14:02:21 krw Exp $ */ /* * Copyright 2004 Henning Brauer <henning@openbsd.org> @@ -60,14 +60,14 @@ void discover_interface(void) { struct ifaddrs *ifap, *ifa; - struct ifreq *tif; struct option_data *opt; char *data; - int len; + int found; if (getifaddrs(&ifap) != 0) error("getifaddrs failed"); + found = 0; for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { if ((ifa->ifa_flags & IFF_LOOPBACK) || (ifa->ifa_flags & IFF_POINTOPOINT) || @@ -76,6 +76,7 @@ discover_interface(void) if (strcmp(ifi->name, ifa->ifa_name)) continue; + found = 1; /* * If we have the capability, extract & save link information. @@ -109,17 +110,9 @@ discover_interface(void) } } } - - if (!ifi->ifp) { - len = IFNAMSIZ + sizeof(struct sockaddr_storage); - if ((tif = malloc(len)) == NULL) - error("no space to remember ifp"); - strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ); - ifi->ifp = tif; - } } - if (!ifi->ifp) + if (!found) error("%s: not found", ifi->name); freeifaddrs(ifap); @@ -147,7 +140,7 @@ dispatch(void) quit = INTERNALSIG; continue; } - if (ifi->rfdesc == -1) { + if (ifi->bfdesc == -1) { warning("%s bpf socket gone; exiting", ifi->name); quit = INTERNALSIG; continue; @@ -181,7 +174,7 @@ dispatch(void) * fds[1] == routing socket for incoming RTM messages * fds[2] == imsg socket to privileged process */ - fds[0].fd = ifi->rfdesc; + fds[0].fd = ifi->bfdesc; fds[1].fd = routefd; fds[2].fd = unpriv_ibuf->fd; fds[0].events = fds[1].events = fds[2].events = POLLIN; @@ -201,7 +194,7 @@ dispatch(void) } if ((fds[0].revents & (POLLIN | POLLHUP))) { - if (ifi && ifi->linkstat && ifi->rfdesc != -1) + if (ifi && ifi->linkstat && ifi->bfdesc != -1) got_one(); } if ((fds[1].revents & (POLLIN | POLLHUP))) { @@ -242,7 +235,7 @@ got_one(void) strerror(errno)); ifi->errors++; if ((!interface_status(ifi->name)) || - (ifi->noifmedia && ifi->errors > 20)) { + ((ifi->flags & IFI_NOMEDIA) && ifi->errors > 20)) { /* our interface has gone away. */ error("Interface %s no longer appears valid.", ifi->name); @@ -317,7 +310,7 @@ interface_status(char *ifname) goto inactive; /* Next, check carrier on the interface if possible. */ - if (ifi->noifmedia) + if (ifi->flags & IFI_NOMEDIA) goto active; memset(&ifmr, 0, sizeof(ifmr)); strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name)); @@ -332,7 +325,7 @@ interface_status(char *ifname) strerror(errno)); #endif - ifi->noifmedia = 1; + ifi->flags |= IFI_NOMEDIA; goto active; } if (ifmr.ifm_status & IFM_AVALID) { |