1*7b08a90aSnatano /* $OpenBSD: bpf.c,v 1.19 2017/04/19 05:36:12 natano Exp $ */
248be18b4Shenning
348be18b4Shenning /* BPF socket interface code, originally contributed by Archie Cobbs. */
448be18b4Shenning
548be18b4Shenning /*
648be18b4Shenning * Copyright (c) 1995, 1996, 1998, 1999
748be18b4Shenning * The Internet Software Consortium. All rights reserved.
848be18b4Shenning *
948be18b4Shenning * Redistribution and use in source and binary forms, with or without
1048be18b4Shenning * modification, are permitted provided that the following conditions
1148be18b4Shenning * are met:
1248be18b4Shenning *
1348be18b4Shenning * 1. Redistributions of source code must retain the above copyright
1448be18b4Shenning * notice, this list of conditions and the following disclaimer.
1548be18b4Shenning * 2. Redistributions in binary form must reproduce the above copyright
1648be18b4Shenning * notice, this list of conditions and the following disclaimer in the
1748be18b4Shenning * documentation and/or other materials provided with the distribution.
1848be18b4Shenning * 3. Neither the name of The Internet Software Consortium nor the names
1948be18b4Shenning * of its contributors may be used to endorse or promote products derived
2048be18b4Shenning * from this software without specific prior written permission.
2148be18b4Shenning *
2248be18b4Shenning * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
2348be18b4Shenning * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2448be18b4Shenning * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2548be18b4Shenning * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2648be18b4Shenning * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
2748be18b4Shenning * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2848be18b4Shenning * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2948be18b4Shenning * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
3048be18b4Shenning * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3148be18b4Shenning * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3248be18b4Shenning * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3348be18b4Shenning * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3448be18b4Shenning * SUCH DAMAGE.
3548be18b4Shenning *
3648be18b4Shenning * This software has been written for the Internet Software Consortium
3748be18b4Shenning * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
3848be18b4Shenning * Enterprises. To learn more about the Internet Software Consortium,
3948be18b4Shenning * see ``http://www.vix.com/isc''. To learn more about Vixie
4048be18b4Shenning * Enterprises, see ``http://www.vix.com''.
4148be18b4Shenning */
4248be18b4Shenning
43f70ef60cSkrw #include <sys/types.h>
4448be18b4Shenning #include <sys/ioctl.h>
45f70ef60cSkrw #include <sys/socket.h>
4648be18b4Shenning
4748be18b4Shenning #include <net/bpf.h>
48f70ef60cSkrw #include <net/if.h>
494be048dcSreyk
50f70ef60cSkrw #include <netinet/in.h>
5148be18b4Shenning #include <netinet/if_ether.h>
5248be18b4Shenning
53f70ef60cSkrw #include <errno.h>
54f70ef60cSkrw #include <fcntl.h>
55f70ef60cSkrw #include <stdio.h>
56f70ef60cSkrw #include <stdlib.h>
57f70ef60cSkrw #include <string.h>
58f70ef60cSkrw #include <unistd.h>
59f70ef60cSkrw
60f70ef60cSkrw #include "dhcp.h"
61f70ef60cSkrw #include "dhcpd.h"
62986dbb4cSkrw #include "log.h"
63f70ef60cSkrw
6448be18b4Shenning /*
6548be18b4Shenning * Called by get_interface_list for each interface that's discovered.
6648be18b4Shenning * Opens a packet filter for each interface and adds it to the select
6748be18b4Shenning * mask.
6848be18b4Shenning */
6948be18b4Shenning int
if_register_bpf(struct interface_info * info)7048be18b4Shenning if_register_bpf(struct interface_info *info)
7148be18b4Shenning {
722abf9a0dSnatano int sock;
7348be18b4Shenning
742abf9a0dSnatano /* Open the BPF device */
75*7b08a90aSnatano if ((sock = open("/dev/bpf", O_RDWR)) == -1)
76d8cc3220Skrw fatal("Can't open bpf device");
7748be18b4Shenning
7848be18b4Shenning /* Set the BPF device to point at this interface. */
79a29cd94bSrzalamena if (ioctl(sock, BIOCSETIF, &info->ifr) == -1)
80d8cc3220Skrw fatal("Can't attach interface %s to bpf device", info->name);
8148be18b4Shenning
8248be18b4Shenning return (sock);
8348be18b4Shenning }
8448be18b4Shenning
8548be18b4Shenning void
if_register_send(struct interface_info * info)8648be18b4Shenning if_register_send(struct interface_info *info)
8748be18b4Shenning {
8848be18b4Shenning /*
8948be18b4Shenning * If we're using the bpf API for sending and receiving, we
9048be18b4Shenning * don't need to register this interface twice.
9148be18b4Shenning */
9248be18b4Shenning info->wfdesc = info->rfdesc;
9348be18b4Shenning }
9448be18b4Shenning
9548be18b4Shenning /*
96e4b49721Srzalamena * Packet filter program: 'ip and udp and dst port CLIENT_PORT'
97e4b49721Srzalamena */
98e4b49721Srzalamena struct bpf_insn dhcp_bpf_sfilter[] = {
99e4b49721Srzalamena /* Make sure this is an IP packet... */
100e4b49721Srzalamena BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
101e4b49721Srzalamena BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
102e4b49721Srzalamena
103e4b49721Srzalamena /* Make sure it's a UDP packet... */
104e4b49721Srzalamena BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
105e4b49721Srzalamena BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
106e4b49721Srzalamena
107e4b49721Srzalamena /* Make sure this isn't a fragment... */
108e4b49721Srzalamena BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
109e4b49721Srzalamena BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
110e4b49721Srzalamena
111e4b49721Srzalamena /* Get the IP header length... */
112e4b49721Srzalamena BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
113e4b49721Srzalamena
114e4b49721Srzalamena /* Make sure it's to the right port... */
115e4b49721Srzalamena BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
116e4b49721Srzalamena BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, CLIENT_PORT, 0, 1),
117e4b49721Srzalamena
118e4b49721Srzalamena /* If we passed all the tests, ask for the whole packet. */
119e4b49721Srzalamena BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
120e4b49721Srzalamena
121e4b49721Srzalamena /* Otherwise, drop it. */
122e4b49721Srzalamena BPF_STMT(BPF_RET+BPF_K, 0),
123e4b49721Srzalamena };
124e4b49721Srzalamena
125e4b49721Srzalamena int dhcp_bpf_sfilter_len = sizeof(dhcp_bpf_sfilter) / sizeof(struct bpf_insn);
126e4b49721Srzalamena
127e4b49721Srzalamena /*
12865520a46Scanacar * Packet filter program: 'ip and udp and dst port SERVER_PORT'
12948be18b4Shenning */
13048be18b4Shenning struct bpf_insn dhcp_bpf_filter[] = {
13148be18b4Shenning /* Make sure this is an IP packet... */
13248be18b4Shenning BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
13348be18b4Shenning BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
13448be18b4Shenning
13548be18b4Shenning /* Make sure it's a UDP packet... */
13648be18b4Shenning BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
13748be18b4Shenning BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
13848be18b4Shenning
13948be18b4Shenning /* Make sure this isn't a fragment... */
14048be18b4Shenning BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
14148be18b4Shenning BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
14248be18b4Shenning
14348be18b4Shenning /* Get the IP header length... */
14448be18b4Shenning BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
14548be18b4Shenning
14648be18b4Shenning /* Make sure it's to the right port... */
14748be18b4Shenning BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
14865520a46Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1),
14948be18b4Shenning
15048be18b4Shenning /* If we passed all the tests, ask for the whole packet. */
15148be18b4Shenning BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
15248be18b4Shenning
15348be18b4Shenning /* Otherwise, drop it. */
15448be18b4Shenning BPF_STMT(BPF_RET+BPF_K, 0),
15548be18b4Shenning };
15648be18b4Shenning
15748be18b4Shenning int dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn);
15848be18b4Shenning
1594be048dcSreyk /*
1604be048dcSreyk * Packet filter program: encapsulated 'ip and udp and dst port SERVER_PORT'
1614be048dcSreyk */
1624be048dcSreyk struct bpf_insn dhcp_bpf_efilter[] = {
1634be048dcSreyk /* Make sure this is an encapsulated AF_INET packet... */
1644be048dcSreyk BPF_STMT(BPF_LD + BPF_W + BPF_ABS, 0),
1654be048dcSreyk BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, AF_INET << 24, 0, 10),
1664be048dcSreyk
1674be048dcSreyk /* Make sure it's an IPIP packet... */
1684be048dcSreyk BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 21),
1694be048dcSreyk BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_IPIP, 0, 8),
1704be048dcSreyk
1714be048dcSreyk /* Make sure it's an encapsulated UDP packet... */
1724be048dcSreyk BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 41),
1734be048dcSreyk BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
1744be048dcSreyk
1754be048dcSreyk /* Make sure this isn't a fragment... */
1764be048dcSreyk BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 38),
1774be048dcSreyk BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
1784be048dcSreyk
1794be048dcSreyk /* Get the IP header length... */
1804be048dcSreyk BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 32),
1814be048dcSreyk
1824be048dcSreyk /* Make sure it's to the right port... */
1834be048dcSreyk BPF_STMT(BPF_LD + BPF_H + BPF_IND, 34),
1844be048dcSreyk BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1),
1854be048dcSreyk
1864be048dcSreyk /* If we passed all the tests, ask for the whole packet. */
1874be048dcSreyk BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
1884be048dcSreyk
1894be048dcSreyk /* Otherwise, drop it. */
1904be048dcSreyk BPF_STMT(BPF_RET+BPF_K, 0),
1914be048dcSreyk };
1924be048dcSreyk
1934be048dcSreyk int dhcp_bpf_efilter_len = sizeof(dhcp_bpf_efilter) / sizeof(struct bpf_insn);
1949018249bScanacar
1959018249bScanacar /*
196e4b49721Srzalamena * Packet write filter program: 'ip and udp and src port CLIENT_PORT'
197e4b49721Srzalamena */
198e4b49721Srzalamena struct bpf_insn dhcp_bpf_swfilter[] = {
199e4b49721Srzalamena /* Make sure this is an IP packet... */
200e4b49721Srzalamena BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
201e4b49721Srzalamena BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
202e4b49721Srzalamena
203e4b49721Srzalamena /* Make sure it's a UDP packet... */
204e4b49721Srzalamena BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
205e4b49721Srzalamena BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
206e4b49721Srzalamena
207e4b49721Srzalamena /* Make sure this isn't a fragment... */
208e4b49721Srzalamena BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
209e4b49721Srzalamena BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
210e4b49721Srzalamena
211e4b49721Srzalamena /* Get the IP header length... */
212e4b49721Srzalamena BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
213e4b49721Srzalamena
214e4b49721Srzalamena /* Make sure it's from the right port... */
215e4b49721Srzalamena BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
216e4b49721Srzalamena BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, CLIENT_PORT, 0, 1),
217e4b49721Srzalamena
218e4b49721Srzalamena /* If we passed all the tests, ask for the whole packet. */
219e4b49721Srzalamena BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
220e4b49721Srzalamena
221e4b49721Srzalamena /* Otherwise, drop it. */
222e4b49721Srzalamena BPF_STMT(BPF_RET+BPF_K, 0),
223e4b49721Srzalamena };
224e4b49721Srzalamena
2254971ba0eSkrw int dhcp_bpf_swfilter_len = sizeof(dhcp_bpf_swfilter) /
2264971ba0eSkrw sizeof(struct bpf_insn);
227e4b49721Srzalamena
228e4b49721Srzalamena /*
2299018249bScanacar * Packet write filter program: 'ip and udp and src port SERVER_PORT'
2309018249bScanacar */
2319018249bScanacar struct bpf_insn dhcp_bpf_wfilter[] = {
2329018249bScanacar /* Make sure this is an IP packet... */
2339018249bScanacar BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
2349018249bScanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
2359018249bScanacar
2369018249bScanacar /* Make sure it's a UDP packet... */
2379018249bScanacar BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
2389018249bScanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
2399018249bScanacar
2409018249bScanacar /* Make sure this isn't a fragment... */
2419018249bScanacar BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
2429018249bScanacar BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
2439018249bScanacar
2449018249bScanacar /* Get the IP header length... */
2459018249bScanacar BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
2469018249bScanacar
2479018249bScanacar /* Make sure it's from the right port... */
2489018249bScanacar BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
2499018249bScanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1),
2509018249bScanacar
2519018249bScanacar /* If we passed all the tests, ask for the whole packet. */
2529018249bScanacar BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
2539018249bScanacar
2549018249bScanacar /* Otherwise, drop it. */
2559018249bScanacar BPF_STMT(BPF_RET+BPF_K, 0),
2569018249bScanacar };
2579018249bScanacar
2589018249bScanacar int dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn);
2599018249bScanacar
26048be18b4Shenning void
if_register_receive(struct interface_info * info,int isserver)261e4b49721Srzalamena if_register_receive(struct interface_info *info, int isserver)
26248be18b4Shenning {
26348be18b4Shenning struct bpf_version v;
26448be18b4Shenning struct bpf_program p;
2659018249bScanacar int flag = 1, sz, cmplt = 0;
26648be18b4Shenning
26748be18b4Shenning /* Open a BPF device and hang it on this interface... */
26848be18b4Shenning info->rfdesc = if_register_bpf(info);
26948be18b4Shenning
27048be18b4Shenning /* Make sure the BPF version is in range... */
27123ccc430Sclaudio if (ioctl(info->rfdesc, BIOCVERSION, &v) == -1)
272d8cc3220Skrw fatal("Can't get BPF version");
27348be18b4Shenning
27448be18b4Shenning if (v.bv_major != BPF_MAJOR_VERSION ||
27548be18b4Shenning v.bv_minor < BPF_MINOR_VERSION)
276986dbb4cSkrw fatalx("Kernel BPF version out of range - recompile dhcpd!");
27748be18b4Shenning
27848be18b4Shenning /*
27948be18b4Shenning * Set immediate mode so that reads return as soon as a packet
28048be18b4Shenning * comes in, rather than waiting for the input buffer to fill
28148be18b4Shenning * with packets.
28248be18b4Shenning */
28323ccc430Sclaudio if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) == -1)
284d8cc3220Skrw fatal("Can't set immediate mode on bpf device");
28548be18b4Shenning
2869018249bScanacar /* make sure kernel fills in the source ethernet address */
28723ccc430Sclaudio if (ioctl(info->rfdesc, BIOCSHDRCMPLT, &cmplt) == -1)
288d8cc3220Skrw fatal("Can't set header complete flag on bpf device");
2899018249bScanacar
29048be18b4Shenning /* Get the required BPF buffer length from the kernel. */
29123ccc430Sclaudio if (ioctl(info->rfdesc, BIOCGBLEN, &sz) == -1)
292d8cc3220Skrw fatal("Can't get bpf buffer length");
29348be18b4Shenning info->rbuf_max = sz;
29448be18b4Shenning info->rbuf = malloc(info->rbuf_max);
29548be18b4Shenning if (!info->rbuf)
296986dbb4cSkrw fatalx("Can't allocate %lu bytes for bpf input buffer.",
29748be18b4Shenning (unsigned long)info->rbuf_max);
29848be18b4Shenning info->rbuf_offset = 0;
29948be18b4Shenning info->rbuf_len = 0;
30048be18b4Shenning
30148be18b4Shenning /* Set up the bpf filter program structure. */
302e4b49721Srzalamena if (isserver) {
303e4b49721Srzalamena p.bf_len = dhcp_bpf_sfilter_len;
304e4b49721Srzalamena p.bf_insns = dhcp_bpf_sfilter;
305e4b49721Srzalamena } else if (info->hw_address.htype == HTYPE_IPSEC_TUNNEL) {
3064be048dcSreyk p.bf_len = dhcp_bpf_efilter_len;
3074be048dcSreyk p.bf_insns = dhcp_bpf_efilter;
3084be048dcSreyk } else {
30948be18b4Shenning p.bf_len = dhcp_bpf_filter_len;
31048be18b4Shenning p.bf_insns = dhcp_bpf_filter;
3114be048dcSreyk }
31223ccc430Sclaudio if (ioctl(info->rfdesc, BIOCSETF, &p) == -1)
313d8cc3220Skrw fatal("Can't install packet filter program");
3149018249bScanacar
3159018249bScanacar /* Set up the bpf write filter program structure. */
316e4b49721Srzalamena if (isserver) {
317e4b49721Srzalamena p.bf_len = dhcp_bpf_swfilter_len;
318e4b49721Srzalamena p.bf_insns = dhcp_bpf_swfilter;
319e4b49721Srzalamena } else {
3209018249bScanacar p.bf_len = dhcp_bpf_wfilter_len;
3219018249bScanacar p.bf_insns = dhcp_bpf_wfilter;
322e4b49721Srzalamena }
3239018249bScanacar
32423ccc430Sclaudio if (ioctl(info->rfdesc, BIOCSETWF, &p) == -1)
325d8cc3220Skrw fatal("Can't install write filter program");
3269018249bScanacar
3279018249bScanacar /* make sure these settings cannot be changed after dropping privs */
32823ccc430Sclaudio if (ioctl(info->rfdesc, BIOCLOCK) == -1)
329d8cc3220Skrw fatal("Failed to lock bpf descriptor");
33048be18b4Shenning }
33148be18b4Shenning
33248be18b4Shenning ssize_t
send_packet(struct interface_info * interface,struct dhcp_packet * raw,size_t len,struct packet_ctx * pc)333cb13f214Sderaadt send_packet(struct interface_info *interface,
334fa3d4f89Srzalamena struct dhcp_packet *raw, size_t len, struct packet_ctx *pc)
33548be18b4Shenning {
33648be18b4Shenning unsigned char buf[256];
33748be18b4Shenning struct iovec iov[2];
33822c60a6bSreyk ssize_t bufp;
33922c60a6bSreyk int result;
34022c60a6bSreyk
34122c60a6bSreyk result = -1;
34248be18b4Shenning
3434be048dcSreyk if (interface->hw_address.htype == HTYPE_IPSEC_TUNNEL) {
344fa3d4f89Srzalamena socklen_t slen = pc->pc_dst.ss_len;
3454be048dcSreyk result = sendto(server_fd, raw, len, 0,
346fa3d4f89Srzalamena (struct sockaddr *)&pc->pc_dst, slen);
3474be048dcSreyk goto done;
3484be048dcSreyk }
3494be048dcSreyk
35048be18b4Shenning /* Assemble the headers... */
35122c60a6bSreyk if ((bufp = assemble_hw_header(buf, sizeof(buf), 0, pc,
35222c60a6bSreyk interface->hw_address.htype)) == -1)
35322c60a6bSreyk goto done;
35422c60a6bSreyk if ((bufp = assemble_udp_ip_header(buf, sizeof(buf), bufp, pc,
35522c60a6bSreyk (unsigned char *)raw, len)) == -1)
35622c60a6bSreyk goto done;
35748be18b4Shenning
35848be18b4Shenning /* Fire it off */
35948be18b4Shenning iov[0].iov_base = (char *)buf;
36048be18b4Shenning iov[0].iov_len = bufp;
36148be18b4Shenning iov[1].iov_base = (char *)raw;
36248be18b4Shenning iov[1].iov_len = len;
36348be18b4Shenning
36448be18b4Shenning result = writev(interface->wfdesc, iov, 2);
3654be048dcSreyk done:
36623ccc430Sclaudio if (result == -1)
367d8cc3220Skrw log_warn("send_packet");
36848be18b4Shenning return (result);
36948be18b4Shenning }
37048be18b4Shenning
37148be18b4Shenning ssize_t
receive_packet(struct interface_info * interface,unsigned char * buf,size_t len,struct packet_ctx * pc)37248be18b4Shenning receive_packet(struct interface_info *interface, unsigned char *buf,
373fa3d4f89Srzalamena size_t len, struct packet_ctx *pc)
37448be18b4Shenning {
37522c60a6bSreyk int length = 0;
37622c60a6bSreyk ssize_t offset = 0;
37748be18b4Shenning struct bpf_hdr hdr;
37848be18b4Shenning
37948be18b4Shenning /*
38048be18b4Shenning * All this complexity is because BPF doesn't guarantee that
38148be18b4Shenning * only one packet will be returned at a time. We're getting
38248be18b4Shenning * what we deserve, though - this is a terrible abuse of the BPF
38348be18b4Shenning * interface. Sigh.
38448be18b4Shenning */
38548be18b4Shenning
38648be18b4Shenning /* Process packets until we get one we can return or until we've
38748be18b4Shenning * done a read and gotten nothing we can return...
38848be18b4Shenning */
38948be18b4Shenning do {
39048be18b4Shenning /* If the buffer is empty, fill it. */
39148be18b4Shenning if (interface->rbuf_offset == interface->rbuf_len) {
39248be18b4Shenning length = read(interface->rfdesc, interface->rbuf,
39348be18b4Shenning interface->rbuf_max);
39448be18b4Shenning if (length <= 0)
39548be18b4Shenning return (length);
39648be18b4Shenning interface->rbuf_offset = 0;
39748be18b4Shenning interface->rbuf_len = length;
39848be18b4Shenning }
39948be18b4Shenning
40048be18b4Shenning /*
40148be18b4Shenning * If there isn't room for a whole bpf header, something
40248be18b4Shenning * went wrong, but we'll ignore it and hope it goes
40348be18b4Shenning * away... XXX
40448be18b4Shenning */
40548be18b4Shenning if (interface->rbuf_len - interface->rbuf_offset <
40648be18b4Shenning sizeof(hdr)) {
40748be18b4Shenning interface->rbuf_offset = interface->rbuf_len;
40848be18b4Shenning continue;
40948be18b4Shenning }
41048be18b4Shenning
41148be18b4Shenning /* Copy out a bpf header... */
41248be18b4Shenning memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
41348be18b4Shenning sizeof(hdr));
41448be18b4Shenning
41548be18b4Shenning /*
41648be18b4Shenning * If the bpf header plus data doesn't fit in what's
41748be18b4Shenning * left of the buffer, stick head in sand yet again...
41848be18b4Shenning */
41948be18b4Shenning if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
42048be18b4Shenning interface->rbuf_len) {
42148be18b4Shenning interface->rbuf_offset = interface->rbuf_len;
42248be18b4Shenning continue;
42348be18b4Shenning }
42448be18b4Shenning
42548be18b4Shenning /*
42648be18b4Shenning * If the captured data wasn't the whole packet, or if
42748be18b4Shenning * the packet won't fit in the input buffer, all we can
42848be18b4Shenning * do is drop it.
42948be18b4Shenning */
43048be18b4Shenning if (hdr.bh_caplen != hdr.bh_datalen) {
4314971ba0eSkrw interface->rbuf_offset += hdr.bh_hdrlen =
4324971ba0eSkrw hdr.bh_caplen;
43348be18b4Shenning continue;
43448be18b4Shenning }
43548be18b4Shenning
43648be18b4Shenning /* Skip over the BPF header... */
43748be18b4Shenning interface->rbuf_offset += hdr.bh_hdrlen;
43848be18b4Shenning
43948be18b4Shenning /* Decode the physical header... */
44022c60a6bSreyk offset = decode_hw_header(interface->rbuf,
44122c60a6bSreyk interface->rbuf_len, interface->rbuf_offset, pc,
44222c60a6bSreyk interface->hw_address.htype);
44348be18b4Shenning
44448be18b4Shenning /*
44522c60a6bSreyk * If decoding or a physical layer checksum failed
44622c60a6bSreyk * (dunno of any physical layer that supports this, but WTH),
44722c60a6bSreyk * skip this packet.
44848be18b4Shenning */
44948be18b4Shenning if (offset < 0) {
45048be18b4Shenning interface->rbuf_offset += hdr.bh_caplen;
45148be18b4Shenning continue;
45248be18b4Shenning }
45348be18b4Shenning
45448be18b4Shenning /* Decode the IP and UDP headers... */
45522c60a6bSreyk offset = decode_udp_ip_header(interface->rbuf,
45622c60a6bSreyk interface->rbuf_len, offset, pc);
45748be18b4Shenning
45848be18b4Shenning /* If the IP or UDP checksum was bad, skip the packet... */
45948be18b4Shenning if (offset < 0) {
46048be18b4Shenning interface->rbuf_offset += hdr.bh_caplen;
46148be18b4Shenning continue;
46248be18b4Shenning }
46322c60a6bSreyk
46422c60a6bSreyk hdr.bh_caplen -= offset - interface->rbuf_offset;
46522c60a6bSreyk interface->rbuf_offset = offset;
46648be18b4Shenning
46748be18b4Shenning /*
46848be18b4Shenning * If there's not enough room to stash the packet data,
46948be18b4Shenning * we have to skip it (this shouldn't happen in real
47048be18b4Shenning * life, though).
47148be18b4Shenning */
47248be18b4Shenning if (hdr.bh_caplen > len) {
47348be18b4Shenning interface->rbuf_offset += hdr.bh_caplen;
47448be18b4Shenning continue;
47548be18b4Shenning }
47648be18b4Shenning
47748be18b4Shenning /* Copy out the data in the packet... */
47848be18b4Shenning memcpy(buf, interface->rbuf + interface->rbuf_offset,
47948be18b4Shenning hdr.bh_caplen);
48048be18b4Shenning interface->rbuf_offset += hdr.bh_caplen;
48148be18b4Shenning return (hdr.bh_caplen);
48248be18b4Shenning } while (!length);
48348be18b4Shenning return (0);
48448be18b4Shenning }
485