-/* $OpenBSD: bpf.c,v 1.48 2017/04/18 13:44:03 krw Exp $ */
+/* $OpenBSD: bpf.c,v 1.49 2017/04/18 13:59:09 krw Exp $ */
/* BPF socket interface code, originally contributed by Archie Cobbs. */
ifi->rbuf_offset += hdr.bh_hdrlen;
/* Decode the physical header. */
- offset = decode_hw_header(ifi->rbuf, ifi->rbuf_offset, hfrom);
+ offset = decode_hw_header(ifi->rbuf + ifi->rbuf_offset,
+ hdr.bh_caplen, hfrom);
/*
* If a physical layer checksum failed (dunno of any
hdr.bh_caplen -= offset;
/* Decode the IP and UDP headers. */
- offset = decode_udp_ip_header(ifi->rbuf,
- ifi->rbuf_offset, from, hdr.bh_caplen);
+ offset = decode_udp_ip_header(ifi->rbuf + ifi->rbuf_offset,
+ hdr.bh_caplen, from);
/* If the IP or UDP checksum was bad, skip the packet. */
if (offset < 0) {
-/* $OpenBSD: dhcpd.h,v 1.170 2017/04/11 10:40:14 krw Exp $ */
+/* $OpenBSD: dhcpd.h,v 1.171 2017/04/18 13:59:09 krw Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
/* packet.c */
void assemble_eh_header(struct interface_info *, struct ether_header *);
-ssize_t decode_hw_header(unsigned char *, int, struct ether_addr *);
-ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *,
- u_int32_t);
+ssize_t decode_hw_header(unsigned char *, u_int32_t, struct ether_addr *);
+ssize_t decode_udp_ip_header(unsigned char *, u_int32_t, struct sockaddr_in *);
u_int32_t checksum(unsigned char *, u_int32_t, u_int32_t);
u_int32_t wrapsum(u_int32_t);
-/* $OpenBSD: packet.c,v 1.34 2017/04/08 20:16:04 krw Exp $ */
+/* $OpenBSD: packet.c,v 1.35 2017/04/18 13:59:09 krw Exp $ */
/* Packet assembly code, originally contributed by Archie Cobbs. */
}
ssize_t
-decode_hw_header(unsigned char *buf, int bufix, struct ether_addr *from)
+decode_hw_header(unsigned char *buf, u_int32_t buflen, struct ether_addr *from)
{
struct ether_header eh;
- memcpy(&eh, buf + bufix, ETHER_HDR_LEN);
+ if (buflen < sizeof(eh))
+ return (-1);
+
+ memcpy(&eh, buf, sizeof(eh));
memcpy(from->ether_addr_octet, eh.ether_shost, ETHER_ADDR_LEN);
}
ssize_t
-decode_udp_ip_header(unsigned char *buf, int bufix, struct sockaddr_in *from,
- u_int32_t buflen)
+decode_udp_ip_header(unsigned char *buf, u_int32_t buflen,
+ struct sockaddr_in *from)
{
struct ip *ip;
struct udphdr *udp;
/* Assure that an entire IP header is within the buffer. */
if (sizeof(*ip) > buflen)
return (-1);
- ip_len = (buf[bufix] & 0xf) << 2;
+ ip_len = (*buf & 0xf) << 2;
if (ip_len > buflen)
return (-1);
- ip = (struct ip *)(buf + bufix);
+ ip = (struct ip *)(buf);
ip_packets_seen++;
/* Check the IP header checksum - it should be zero. */
- if (wrapsum(checksum(buf + bufix, ip_len, 0)) != 0) {
+ if (wrapsum(checksum((unsigned char *)ip, ip_len, 0)) != 0) {
ip_packets_bad_checksum++;
if (ip_packets_seen > 4 && ip_packets_bad_checksum != 0 &&
(ip_packets_seen / ip_packets_bad_checksum) < 2) {
/* Assure that the UDP header is within the buffer. */
if (ip_len + sizeof(*udp) > buflen)
return (-1);
- udp = (struct udphdr *)(buf + bufix + ip_len);
+ udp = (struct udphdr *)(buf + ip_len);
udp_packets_seen++;
/* Assure that the entire UDP packet is within the buffer. */
if (ip_len + ntohs(udp->uh_ulen) > buflen)
return (-1);
- data = buf + bufix + ip_len + sizeof(*udp);
+ data = buf + ip_len + sizeof(*udp);
/*
* Compute UDP checksums, including the ``pseudo-header'', the
*/
udp_packets_length_checked++;
len = ntohs(udp->uh_ulen) - sizeof(*udp);
- if ((len < 0) || (len + data > buf + bufix + buflen)) {
+ if ((len < 0) || (len + data > buf + buflen)) {
udp_packets_length_overflow++;
if (udp_packets_length_checked > 4 &&
udp_packets_length_overflow != 0 &&
return (-1);
}
#ifdef DEBUG
- if (len + data != buf + bufix + buflen)
+ if (len + data != buf + buflen)
log_debug("accepting packet with data after udp payload.");
#endif /* DEBUG */
-/* $OpenBSD: bpf.c,v 1.17 2017/04/18 13:44:03 krw Exp $ */
+/* $OpenBSD: bpf.c,v 1.18 2017/04/18 13:59:09 krw Exp $ */
/* BPF socket interface code, originally contributed by Archie Cobbs. */
interface->rbuf_offset += hdr.bh_hdrlen;
/* Decode the physical header... */
- offset = decode_hw_header(interface,
- interface->rbuf, interface->rbuf_offset, hfrom);
+ offset = decode_hw_header(interface->rbuf +
+ interface->rbuf_offset, hdr.bh_caplen, hfrom);
/*
* If a physical layer checksum failed (dunno of any
hdr.bh_caplen -= offset;
/* Decode the IP and UDP headers... */
- offset = decode_udp_ip_header(interface, interface->rbuf,
- interface->rbuf_offset, from, hdr.bh_caplen);
+ offset = decode_udp_ip_header(interface->rbuf +
+ interface->rbuf_offset, hdr.bh_caplen, from);
/* If the IP or UDP checksum was bad, skip the packet... */
if (offset < 0) {
-/* $OpenBSD: dhcpd.h,v 1.62 2017/04/17 18:31:08 krw Exp $ */
+/* $OpenBSD: dhcpd.h,v 1.63 2017/04/18 13:59:09 krw Exp $ */
/*
* Copyright (c) 1995, 1996, 1997, 1998, 1999
int *, struct hardware *);
void assemble_udp_ip_header(struct interface_info *, unsigned char *,
int *, u_int32_t, u_int32_t, unsigned int, unsigned char *, int);
-ssize_t decode_hw_header(struct interface_info *, unsigned char *,
- int, struct hardware *);
-ssize_t decode_udp_ip_header(struct interface_info *, unsigned char *,
- int, struct sockaddr_in *, int);
+ssize_t decode_hw_header(unsigned char *, u_int32_t, struct hardware *);
+ssize_t decode_udp_ip_header(unsigned char *, u_int32_t, struct sockaddr_in *);
u_int32_t checksum(unsigned char *, u_int32_t, u_int32_t);
u_int32_t wrapsum(u_int32_t);
-/* $OpenBSD: packet.c,v 1.13 2017/04/17 18:31:08 krw Exp $ */
+/* $OpenBSD: packet.c,v 1.14 2017/04/18 13:59:09 krw Exp $ */
/* Packet assembly code, originally contributed by Archie Cobbs. */
}
ssize_t
-decode_hw_header(struct interface_info *interface, unsigned char *buf,
- int bufix, struct hardware *from)
+decode_hw_header(unsigned char *buf, u_int32_t buflen, struct hardware *from)
{
struct ether_header eh;
- memcpy(&eh, buf + bufix, ETHER_HDR_LEN);
+ if (buflen < sizeof(eh))
+ return (-1);
+
+ memcpy(&eh, buf, sizeof(eh));
memcpy(from->haddr, eh.ether_shost, sizeof(eh.ether_shost));
from->htype = ARPHRD_ETHER;
}
ssize_t
-decode_udp_ip_header(struct interface_info *interface, unsigned char *buf,
- int bufix, struct sockaddr_in *from, int buflen)
+decode_udp_ip_header(unsigned char *buf, u_int32_t buflen,
+ struct sockaddr_in *from)
{
struct ip *ip;
struct udphdr *udp;
/* Assure that an entire IP header is within the buffer. */
if (sizeof(*ip) > buflen)
return (-1);
- ip_len = (buf[bufix] & 0xf) << 2;
+ ip_len = (*buf & 0xf) << 2;
if (ip_len > buflen)
return (-1);
- ip = (struct ip *)(buf + bufix);
+ ip = (struct ip *)buf;
ip_packets_seen++;
/* Check the IP header checksum - it should be zero. */
- if (wrapsum(checksum(buf + bufix, ip_len, 0)) != 0) {
+ if (wrapsum(checksum((unsigned char *)ip, ip_len, 0)) != 0) {
ip_packets_bad_checksum++;
if (ip_packets_seen > 4 && ip_packets_bad_checksum != 0 &&
(ip_packets_seen / ip_packets_bad_checksum) < 2) {
/* Assure that the UDP header is within the buffer. */
if (ip_len + sizeof(*udp) > buflen)
return (-1);
- udp = (struct udphdr *)(buf + bufix + ip_len);
+ udp = (struct udphdr *)(buf + ip_len);
udp_packets_seen++;
/* Assure that the entire UDP packet is within the buffer. */
if (ip_len + ntohs(udp->uh_ulen) > buflen)
return (-1);
- data = buf + bufix + ip_len + sizeof(*udp);
+ data = buf + ip_len + sizeof(*udp);
/*
* Compute UDP checksums, including the ``pseudo-header'', the
*/
udp_packets_length_checked++;
len = ntohs(udp->uh_ulen) - sizeof(*udp);
- if ((len < 0) || (len + data > buf + bufix + buflen)) {
+ if ((len < 0) || (len + data > buf + buflen)) {
udp_packets_length_overflow++;
if (udp_packets_length_checked > 4 &&
udp_packets_length_overflow != 0 &&
}
return (-1);
}
- if (len + data != buf + bufix + buflen)
+ if (len + data != buf + buflen)
log_debug("accepting packet with data after udp payload.");
usum = udp->uh_sum;