-/* $OpenBSD: bpf.c,v 1.50 2017/04/19 05:36:12 natano Exp $ */
+/* $OpenBSD: bpf.c,v 1.51 2017/06/13 15:49:32 krw Exp $ */
/* BPF socket interface code, originally contributed by Archie Cobbs. */
ssize_t
send_packet(struct interface_info *ifi, struct in_addr from, struct in_addr to)
{
- struct client_state *client = ifi->client;
struct sockaddr_in dest;
struct ether_header eh;
struct ip ip;
struct udphdr udp;
struct iovec iov[4];
struct msghdr msg;
- unsigned char *data;
+ struct dhcp_packet *packet = &ifi->client->sent_packet;
ssize_t result;
- int iovcnt = 0, len;
+ int iovcnt = 0, len = ifi->client->sent_packet_length;
memset(&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
iovcnt++;
}
- data = (unsigned char *)&client->bootrequest_packet;
- len = client->bootrequest_packet_length;
-
ip.ip_v = 4;
ip.ip_hl = 5;
ip.ip_tos = IPTOS_LOWDELAY;
udp.uh_ulen = htons(sizeof(udp) + len);
udp.uh_sum = 0;
udp.uh_sum = wrapsum(checksum((unsigned char *)&udp, sizeof(udp),
- checksum(data, len, checksum((unsigned char *)&ip.ip_src,
+ checksum((unsigned char *)packet, len,
+ checksum((unsigned char *)&ip.ip_src,
2 * sizeof(ip.ip_src),
IPPROTO_UDP + (u_int32_t)ntohs(udp.uh_ulen)))));
iov[iovcnt].iov_base = &udp;
iov[iovcnt].iov_len = sizeof(udp);
iovcnt++;
- iov[iovcnt].iov_base = data;
+ iov[iovcnt].iov_base = packet;
iov[iovcnt].iov_len = len;
iovcnt++;
receive_packet(struct interface_info *ifi, struct sockaddr_in *from,
struct ether_addr *hfrom)
{
- struct client_state *client = ifi->client;
+ struct dhcp_packet *packet = &ifi->client->recv_packet;
int length = 0, offset = 0;
struct bpf_hdr hdr;
* we have to skip it (this shouldn't happen in real
* life, though).
*/
- if (hdr.bh_caplen > sizeof(client->packet)) {
+ if (hdr.bh_caplen > sizeof(*packet)) {
ifi->rbuf_offset = BPF_WORDALIGN(
ifi->rbuf_offset + hdr.bh_caplen);
continue;
}
/* Copy out the data in the packet. */
- memset(&client->packet, DHO_END, sizeof(client->packet));
- memcpy(&client->packet, ifi->rbuf + ifi->rbuf_offset,
- hdr.bh_caplen);
+ memset(packet, DHO_END, sizeof(*packet));
+ memcpy(packet, ifi->rbuf + ifi->rbuf_offset, hdr.bh_caplen);
ifi->rbuf_offset = BPF_WORDALIGN(ifi->rbuf_offset +
hdr.bh_caplen);
return (hdr.bh_caplen);
-/* $OpenBSD: dhclient.c,v 1.418 2017/05/26 14:57:41 krw Exp $ */
+/* $OpenBSD: dhclient.c,v 1.419 2017/06/13 15:49:32 krw Exp $ */
/*
* Copyright 2004 Henning Brauer <henning@openbsd.org>
struct option_data *options, char *info)
{
struct client_state *client = ifi->client;
+ struct dhcp_packet *packet = &client->recv_packet;
struct client_lease *lease, *lp;
time_t stop_selecting;
/* If we've already seen this lease, don't record it again. */
TAILQ_FOREACH(lp, &client->offered_leases, next) {
- if (!memcmp(&lp->address.s_addr, &client->packet.yiaddr,
+ if (!memcmp(&lp->address.s_addr, &packet->yiaddr,
sizeof(in_addr_t))) {
#ifdef DEBUG
log_debug("Duplicate %s.", info);
struct option_data *options)
{
struct client_state *client = ifi->client;
+ struct dhcp_packet *packet = &client->recv_packet;
char ifname[IF_NAMESIZE];
struct client_lease *lease;
char *pretty, *buf;
* If this lease is trying to sell us an address we are already
* using, blow it off.
*/
- lease->address.s_addr = client->packet.yiaddr.s_addr;
+ lease->address.s_addr = packet->yiaddr.s_addr;
memset(ifname, 0, sizeof(ifname));
if (addressinuse(ifi, lease->address, ifname) &&
strncmp(ifname, ifi->name, IF_NAMESIZE) != 0) {
}
/* Save the siaddr (a.k.a. next-server) info. */
- lease->next_server.s_addr = client->packet.siaddr.s_addr;
+ lease->next_server.s_addr = packet->siaddr.s_addr;
/* If the server name was filled out, copy it. */
if ((!lease->options[DHO_DHCP_OPTION_OVERLOAD].len ||
!(lease->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 2)) &&
- client->packet.sname[0]) {
+ packet->sname[0]) {
lease->server_name = malloc(DHCP_SNAME_LEN + 1);
if (!lease->server_name) {
log_warnx("dhcpoffer: no memory for server name.");
lease->is_invalid = 1;
}
- memcpy(lease->server_name, client->packet.sname,
- DHCP_SNAME_LEN);
+ memcpy(lease->server_name, packet->sname, DHCP_SNAME_LEN);
lease->server_name[DHCP_SNAME_LEN] = '\0';
if (!res_hnok(lease->server_name)) {
log_warnx("Bogus server name %s", lease->server_name);
/* Ditto for the filename. */
if ((!lease->options[DHO_DHCP_OPTION_OVERLOAD].len ||
!(lease->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 1)) &&
- client->packet.file[0]) {
+ packet->file[0]) {
/* Don't count on the NUL terminator. */
lease->filename = malloc(DHCP_FILE_LEN + 1);
if (!lease->filename) {
log_warnx("dhcpoffer: no memory for filename.");
lease->is_invalid = 1;
}
- memcpy(lease->filename, client->packet.file, DHCP_FILE_LEN);
+ memcpy(lease->filename, packet->file, DHCP_FILE_LEN);
lease->filename[DHCP_FILE_LEN] = '\0';
}
return lease;
{
struct interface_info *ifi = xifi;
struct client_state *client = ifi->client;
+ struct dhcp_packet *packet = &client->sent_packet;
time_t cur_time;
ssize_t rslt;
int interval;
/* Record the number of seconds since we started sending. */
if (interval < UINT16_MAX)
- client->bootrequest_packet.secs = htons(interval);
+ packet->secs = htons(interval);
else
- client->bootrequest_packet.secs = htons(UINT16_MAX);
- client->secs = client->bootrequest_packet.secs;
+ packet->secs = htons(UINT16_MAX);
+ client->secs = packet->secs;
log_info("DHCPDISCOVER on %s - interval %lld", ifi->name,
(long long)client->interval);
{
struct interface_info *ifi = xifi;
struct client_state *client = ifi->client;
+ struct dhcp_packet *packet = &client->sent_packet;
struct sockaddr_in destination;
struct in_addr from;
time_t cur_time;
/* Record the number of seconds since we started sending. */
if (client->state == S_REQUESTING)
- client->bootrequest_packet.secs = client->secs;
+ packet->secs = client->secs;
else {
if (interval < UINT16_MAX)
- client->bootrequest_packet.secs = htons(interval);
+ packet->secs = htons(interval);
else
- client->bootrequest_packet.secs = htons(UINT16_MAX);
+ packet->secs = htons(UINT16_MAX);
}
log_info("DHCPREQUEST on %s to %s", ifi->name,
{
struct client_state *client = ifi->client;
struct option_data options[256];
- struct dhcp_packet *packet = &client->bootrequest_packet;
+ struct dhcp_packet *packet = &client->sent_packet;
unsigned char discover = DHCPDISCOVER;
int i;
i = cons_options(ifi, options);
if (i == -1 || packet->options[i] != DHO_END)
fatalx("options do not fit in DHCPDISCOVER packet.");
- client->bootrequest_packet_length = DHCP_FIXED_NON_UDP+i+1;
- if (client->bootrequest_packet_length < BOOTP_MIN_LEN)
- client->bootrequest_packet_length = BOOTP_MIN_LEN;
+ client->sent_packet_length = DHCP_FIXED_NON_UDP+i+1;
+ if (client->sent_packet_length < BOOTP_MIN_LEN)
+ client->sent_packet_length = BOOTP_MIN_LEN;
packet->op = BOOTREQUEST;
packet->htype = HTYPE_ETHER ;
{
struct client_state *client = ifi->client;
struct option_data options[256];
- struct dhcp_packet *packet = &client->bootrequest_packet;
+ struct dhcp_packet *packet = &client->sent_packet;
unsigned char request = DHCPREQUEST;
int i;
i = cons_options(ifi, options);
if (i == -1 || packet->options[i] != DHO_END)
fatalx("options do not fit in DHCPREQUEST packet.");
- client->bootrequest_packet_length = DHCP_FIXED_NON_UDP+i+1;
- if (client->bootrequest_packet_length < BOOTP_MIN_LEN)
- client->bootrequest_packet_length = BOOTP_MIN_LEN;
+ client->sent_packet_length = DHCP_FIXED_NON_UDP+i+1;
+ if (client->sent_packet_length < BOOTP_MIN_LEN)
+ client->sent_packet_length = BOOTP_MIN_LEN;
packet->op = BOOTREQUEST;
packet->htype = HTYPE_ETHER ;
{
struct client_state *client = ifi->client;
struct option_data options[256];
- struct dhcp_packet *packet = &client->bootrequest_packet;
+ struct dhcp_packet *packet = &client->sent_packet;
unsigned char decline = DHCPDECLINE;
int i;
i = cons_options(ifi, options);
if (i == -1 || packet->options[i] != DHO_END)
fatalx("options do not fit in DHCPDECLINE packet.");
- client->bootrequest_packet_length = DHCP_FIXED_NON_UDP+i+1;
- if (client->bootrequest_packet_length < BOOTP_MIN_LEN)
- client->bootrequest_packet_length = BOOTP_MIN_LEN;
+ client->sent_packet_length = DHCP_FIXED_NON_UDP+i+1;
+ if (client->sent_packet_length < BOOTP_MIN_LEN)
+ client->sent_packet_length = BOOTP_MIN_LEN;
packet->op = BOOTREQUEST;
packet->htype = HTYPE_ETHER ;