1 /* $OpenBSD: bpf.c,v 1.20 2007/01/08 02:51:13 krw Exp $ */
2 /* $DragonFly: src/sbin/dhclient/bpf.c,v 1.2 2008/11/05 14:08:41 sephe Exp $ */
4 /* BPF socket interface code, originally contributed by Archie Cobbs. */
7 * Copyright (c) 1995, 1996, 1998, 1999
8 * The Internet Software Consortium. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of The Internet Software Consortium nor the names
20 * of its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * This software has been written for the Internet Software Consortium
38 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
39 * Enterprises. To learn more about the Internet Software Consortium,
40 * see ``http://www.vix.com/isc''. To learn more about Vixie
41 * Enterprises, see ``http://www.vix.com''.
44 #include <sys/ioctl.h>
48 #include <netinet/if_ether.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <netinet/udp.h>
55 #define BPF_FORMAT "/dev/bpf%d"
58 * Called by get_interface_list for each interface that's discovered.
59 * Opens a packet filter for each interface and adds it to the select
68 /* Open a BPF device */
70 snprintf(filename
, sizeof(filename
), BPF_FORMAT
, b
);
71 sock
= open(filename
, O_RDWR
, 0);
76 error("Can't find free bpf: %m");
81 /* Set the BPF device to point at this interface. */
82 if (ioctl(sock
, BIOCSETIF
, ifi
->ifp
) < 0)
83 error("Can't attach interface %s to bpf device %s: %m",
90 if_register_send(void)
95 * If we're using the bpf API for sending and receiving, we
96 * don't need to register this interface twice.
98 ifi
->wfdesc
= ifi
->rfdesc
;
101 * Use raw socket for unicast send.
103 if ((sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_UDP
)) == -1)
104 error("socket(SOCK_RAW): %m");
105 if (setsockopt(sock
, IPPROTO_IP
, IP_HDRINCL
, &on
,
107 error("setsockopt(IP_HDRINCL): %m");
112 * Packet filter program...
114 * XXX: Changes to the filter program may require changes to the
115 * constant offsets used in if_register_send to patch the BPF program!
117 struct bpf_insn dhcp_bpf_filter
[] = {
118 /* Make sure this is an IP packet... */
119 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, 12),
120 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, ETHERTYPE_IP
, 0, 8),
122 /* Make sure it's a UDP packet... */
123 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
, 23),
124 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, IPPROTO_UDP
, 0, 6),
126 /* Make sure this isn't a fragment... */
127 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, 20),
128 BPF_JUMP(BPF_JMP
+ BPF_JSET
+ BPF_K
, 0x1fff, 4, 0),
130 /* Get the IP header length... */
131 BPF_STMT(BPF_LDX
+ BPF_B
+ BPF_MSH
, 14),
133 /* Make sure it's to the right port... */
134 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_IND
, 16),
135 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 67, 0, 1), /* patch */
137 /* If we passed all the tests, ask for the whole packet. */
138 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1),
140 /* Otherwise, drop it. */
141 BPF_STMT(BPF_RET
+BPF_K
, 0),
144 int dhcp_bpf_filter_len
= sizeof(dhcp_bpf_filter
) / sizeof(struct bpf_insn
);
147 * Packet write filter program:
148 * 'ip and udp and src port bootps and dst port (bootps or bootpc)'
150 struct bpf_insn dhcp_bpf_wfilter
[] = {
151 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_IND
, 14),
152 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, (IPVERSION
<< 4) + 5, 0, 12),
154 /* Make sure this is an IP packet... */
155 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, 12),
156 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, ETHERTYPE_IP
, 0, 10),
158 /* Make sure it's a UDP packet... */
159 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
, 23),
160 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, IPPROTO_UDP
, 0, 8),
162 /* Make sure this isn't a fragment... */
163 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, 20),
164 BPF_JUMP(BPF_JMP
+ BPF_JSET
+ BPF_K
, 0x1fff, 6, 0), /* patched */
166 /* Get the IP header length... */
167 BPF_STMT(BPF_LDX
+ BPF_B
+ BPF_MSH
, 14),
169 /* Make sure it's from the right port... */
170 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_IND
, 14),
171 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 68, 0, 3),
173 /* Make sure it is to the right ports ... */
174 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_IND
, 16),
175 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 67, 0, 1),
177 /* If we passed all the tests, ask for the whole packet. */
178 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1),
180 /* Otherwise, drop it. */
181 BPF_STMT(BPF_RET
+BPF_K
, 0),
184 int dhcp_bpf_wfilter_len
= sizeof(dhcp_bpf_wfilter
) / sizeof(struct bpf_insn
);
187 if_register_receive(void)
189 struct bpf_version v
;
190 struct bpf_program p
;
193 /* Open a BPF device and hang it on this interface... */
194 ifi
->rfdesc
= if_register_bpf();
196 /* Make sure the BPF version is in range... */
197 if (ioctl(ifi
->rfdesc
, BIOCVERSION
, &v
) < 0)
198 error("Can't get BPF version: %m");
200 if (v
.bv_major
!= BPF_MAJOR_VERSION
||
201 v
.bv_minor
< BPF_MINOR_VERSION
)
202 error("Kernel BPF version out of range - recompile dhcpd!");
205 * Set immediate mode so that reads return as soon as a packet
206 * comes in, rather than waiting for the input buffer to fill
209 if (ioctl(ifi
->rfdesc
, BIOCIMMEDIATE
, &flag
) < 0)
210 error("Can't set immediate mode on bpf device: %m");
212 /*if (ioctl(ifi->rfdesc, BIOCSFILDROP, &flag) < 0)
213 error("Can't set filter-drop mode on bpf device: %m");*/
215 /* Get the required BPF buffer length from the kernel. */
216 if (ioctl(ifi
->rfdesc
, BIOCGBLEN
, &sz
) < 0)
217 error("Can't get bpf buffer length: %m");
219 ifi
->rbuf
= malloc(ifi
->rbuf_max
);
221 error("Can't allocate %lu bytes for bpf input buffer.",
222 (unsigned long)ifi
->rbuf_max
);
223 ifi
->rbuf_offset
= 0;
226 /* Set up the bpf filter program structure. */
227 p
.bf_len
= dhcp_bpf_filter_len
;
228 p
.bf_insns
= dhcp_bpf_filter
;
230 /* Patch the server port into the BPF program...
232 * XXX: changes to filter program may require changes to the
233 * insn number(s) used below!
235 dhcp_bpf_filter
[8].k
= LOCAL_PORT
;
237 if (ioctl(ifi
->rfdesc
, BIOCSETF
, &p
) < 0)
238 error("Can't install packet filter program: %m");
240 /* Set up the bpf write filter program structure. */
241 p
.bf_len
= dhcp_bpf_wfilter_len
;
242 p
.bf_insns
= dhcp_bpf_wfilter
;
244 if (dhcp_bpf_wfilter
[7].k
== 0x1fff)
245 dhcp_bpf_wfilter
[7].k
= htons(IP_MF
|IP_OFFMASK
);
247 if (ioctl(ifi
->rfdesc
, BIOCSETWF
, &p
) < 0)
248 error("Can't install write filter program: %m");
250 if (ioctl(ifi
->rfdesc
, BIOCLOCK
, NULL
) < 0)
251 error("Cannot lock bpf");
255 send_packet(struct in_addr from
, struct sockaddr_in
*to
,
256 struct hardware
*hto
)
259 unsigned char buf
[256];
260 struct iovec iov
[IOVCNT
];
262 int result
, bufp
= 0;
264 if (to
->sin_addr
.s_addr
== INADDR_BROADCAST
) {
265 assemble_hw_header(buf
, &bufp
, hto
);
268 assemble_udp_ip_header(buf
, &bufp
, from
.s_addr
,
269 to
->sin_addr
.s_addr
, to
->sin_port
,
270 (unsigned char *)&client
->packet
,
271 client
->packet_length
);
273 iov
[0].iov_base
= (char *)buf
;
274 iov
[0].iov_len
= bufp
;
275 iov
[1].iov_base
= (char *)&client
->packet
;
276 iov
[1].iov_len
= client
->packet_length
;
278 if (to
->sin_addr
.s_addr
== INADDR_BROADCAST
) {
279 result
= writev(ifi
->wfdesc
, iov
, IOVCNT
);
281 struct ip
*ip
= (struct ip
*)buf
;
284 * DragonFly's raw socket expects ip_len/ip_off
285 * in host byte order.
287 ip
->ip_len
= ntohs(ip
->ip_len
);
288 ip
->ip_off
= ntohs(ip
->ip_off
);
290 memset(&msg
, 0, sizeof(msg
));
291 msg
.msg_name
= (struct sockaddr
*)to
;
292 msg
.msg_namelen
= sizeof(*to
);
294 msg
.msg_iovlen
= IOVCNT
;
295 result
= sendmsg(ifi
->ufdesc
, &msg
, 0);
299 warning("send_packet: %m");
304 receive_packet(struct sockaddr_in
*from
, struct hardware
*hfrom
)
306 int length
= 0, offset
= 0;
310 * All this complexity is because BPF doesn't guarantee that
311 * only one packet will be returned at a time. We're getting
312 * what we deserve, though - this is a terrible abuse of the BPF
316 /* Process packets until we get one we can return or until we've
317 * done a read and gotten nothing we can return...
320 /* If the buffer is empty, fill it. */
321 if (ifi
->rbuf_offset
== ifi
->rbuf_len
) {
322 length
= read(ifi
->rfdesc
, ifi
->rbuf
, ifi
->rbuf_max
);
325 ifi
->rbuf_offset
= 0;
326 ifi
->rbuf_len
= BPF_WORDALIGN(length
);
330 * If there isn't room for a whole bpf header, something
331 * went wrong, but we'll ignore it and hope it goes
334 if (ifi
->rbuf_len
- ifi
->rbuf_offset
< sizeof(hdr
)) {
335 ifi
->rbuf_offset
= ifi
->rbuf_len
;
339 /* Copy out a bpf header... */
340 memcpy(&hdr
, &ifi
->rbuf
[ifi
->rbuf_offset
], sizeof(hdr
));
343 * If the bpf header plus data doesn't fit in what's
344 * left of the buffer, stick head in sand yet again...
346 if (ifi
->rbuf_offset
+ hdr
.bh_hdrlen
+ hdr
.bh_caplen
>
348 ifi
->rbuf_offset
= ifi
->rbuf_len
;
353 * If the captured data wasn't the whole packet, or if
354 * the packet won't fit in the input buffer, all we can
357 if (hdr
.bh_caplen
!= hdr
.bh_datalen
) {
358 ifi
->rbuf_offset
= BPF_WORDALIGN(
359 ifi
->rbuf_offset
+ hdr
.bh_hdrlen
+
364 /* Skip over the BPF header... */
365 ifi
->rbuf_offset
+= hdr
.bh_hdrlen
;
367 /* Decode the physical header... */
368 offset
= decode_hw_header(ifi
->rbuf
, ifi
->rbuf_offset
, hfrom
);
371 * If a physical layer checksum failed (dunno of any
372 * physical layer that supports this, but WTH), skip
376 ifi
->rbuf_offset
= BPF_WORDALIGN(
377 ifi
->rbuf_offset
+ hdr
.bh_caplen
);
380 ifi
->rbuf_offset
+= offset
;
381 hdr
.bh_caplen
-= offset
;
383 /* Decode the IP and UDP headers... */
384 offset
= decode_udp_ip_header(ifi
->rbuf
,
385 ifi
->rbuf_offset
, from
, NULL
, hdr
.bh_caplen
);
387 /* If the IP or UDP checksum was bad, skip the packet... */
389 ifi
->rbuf_offset
= BPF_WORDALIGN(
390 ifi
->rbuf_offset
+ hdr
.bh_caplen
);
393 ifi
->rbuf_offset
+= offset
;
394 hdr
.bh_caplen
-= offset
;
397 * If there's not enough room to stash the packet data,
398 * we have to skip it (this shouldn't happen in real
401 if (hdr
.bh_caplen
> sizeof(client
->packet
)) {
402 ifi
->rbuf_offset
= BPF_WORDALIGN(
403 ifi
->rbuf_offset
+ hdr
.bh_caplen
);
407 /* Copy out the data in the packet... */
408 memset(&client
->packet
, DHO_END
, sizeof(client
->packet
));
409 memcpy(&client
->packet
, ifi
->rbuf
+ ifi
->rbuf_offset
,
411 ifi
->rbuf_offset
= BPF_WORDALIGN(ifi
->rbuf_offset
+
413 return (hdr
.bh_caplen
);