]> git.ipfire.org Git - thirdparty/dhcp.git/blob - common/bpf.c
- Do what people expect when they don't declare the subnet to which an
[thirdparty/dhcp.git] / common / bpf.c
1 /* bpf.c
2
3 BPF socket interface code, originally contributed by Archie Cobbs. */
4
5 /*
6 * Copyright (c) 1996-2000 Internet Software Consortium.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * This software was contributed to the Internet Software Consortium
37 * by Archie Cobbs, and is now maintained by Ted Lemon in cooperation
38 * with Nominum, Inc. To learn more about the Internet Software
39 * Consortium, see ``http://www.isc.org/''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''. To learn more about
41 * Nominum, Inc., see ``http://www.nominum.com''.
42 *
43 * Patches for FDDI support on Digital Unix were written by Bill
44 * Stapleton, and maintained for a while by Mike Meredith before he
45 * managed to get me to integrate them.
46 */
47
48 #ifndef lint
49 static char copyright[] =
50 "$Id: bpf.c,v 1.42 2000/09/01 23:03:31 mellon Exp $ Copyright (c) 1995-2000 The Internet Software Consortium. All rights reserved.\n";
51 #endif /* not lint */
52
53 #include "dhcpd.h"
54 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
55 || defined (USE_LPF_RECEIVE)
56 # if defined (USE_LPF_RECEIVE)
57 # include <asm/types.h>
58 # include <linux/filter.h>
59 # define bpf_insn sock_filter /* Linux: dare to be gratuitously different. */
60 # else
61 # include <sys/ioctl.h>
62 # include <sys/uio.h>
63 # include <net/bpf.h>
64 # if defined (NEED_OSF_PFILT_HACKS)
65 # include <net/pfilt.h>
66 # endif
67 # endif
68
69 #include <netinet/in_systm.h>
70 #include "includes/netinet/ip.h"
71 #include "includes/netinet/udp.h"
72 #include "includes/netinet/if_ether.h"
73 #endif
74
75 /* Reinitializes the specified interface after an address change. This
76 is not required for packet-filter APIs. */
77
78 #ifdef USE_BPF_SEND
79 void if_reinitialize_send (info)
80 struct interface_info *info;
81 {
82 }
83 #endif
84
85 #ifdef USE_BPF_RECEIVE
86 void if_reinitialize_receive (info)
87 struct interface_info *info;
88 {
89 }
90 #endif
91
92 /* Called by get_interface_list for each interface that's discovered.
93 Opens a packet filter for each interface and adds it to the select
94 mask. */
95
96 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
97 int if_register_bpf (info)
98 struct interface_info *info;
99 {
100 int sock;
101 char filename[50];
102 int b;
103
104 /* Open a BPF device */
105 for (b = 0; 1; b++) {
106 #ifndef NO_SNPRINTF
107 snprintf(filename, sizeof(filename), BPF_FORMAT, b);
108 #else
109 sprintf(filename, BPF_FORMAT, b);
110 #endif
111 sock = open (filename, O_RDWR, 0);
112 if (sock < 0) {
113 if (errno == EBUSY) {
114 continue;
115 } else {
116 if (!b)
117 log_fatal ("No bpf devices.%s%s%s",
118 " Please read the README",
119 " section for your operating",
120 " system.");
121 log_fatal ("Can't find free bpf: %m");
122 }
123 } else {
124 break;
125 }
126 }
127
128 /* Set the BPF device to point at this interface. */
129 if (ioctl (sock, BIOCSETIF, info -> ifp) < 0)
130 log_fatal ("Can't attach interface %s to bpf device %s: %m",
131 info -> name, filename);
132
133 return sock;
134 }
135 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
136
137 #ifdef USE_BPF_SEND
138 void if_register_send (info)
139 struct interface_info *info;
140 {
141 /* If we're using the bpf API for sending and receiving,
142 we don't need to register this interface twice. */
143 #ifndef USE_BPF_RECEIVE
144 info -> wfdesc = if_register_bpf (info, interface);
145 #else
146 info -> wfdesc = info -> rfdesc;
147 #endif
148 if (!quiet_interface_discovery)
149 log_info ("Sending on BPF/%s/%s%s%s",
150 info -> name,
151 print_hw_addr (info -> hw_address.hbuf [0],
152 info -> hw_address.hlen - 1,
153 &info -> hw_address.hbuf [1]),
154 (info -> shared_network ? "/" : ""),
155 (info -> shared_network ?
156 info -> shared_network -> name : ""));
157 }
158
159 void if_deregister_send (info)
160 struct interface_info *info;
161 {
162 /* If we're using the bpf API for sending and receiving,
163 we don't need to register this interface twice. */
164 #ifndef USE_BPF_RECEIVE
165 close (info -> wfdesc);
166 #endif
167 info -> wfdesc = -1;
168
169 if (!quiet_interface_discovery)
170 log_info ("Disabling output on BPF/%s/%s%s%s",
171 info -> name,
172 print_hw_addr (info -> hw_address.hbuf [0],
173 info -> hw_address.hlen - 1,
174 &info -> hw_address.hbuf [1]),
175 (info -> shared_network ? "/" : ""),
176 (info -> shared_network ?
177 info -> shared_network -> name : ""));
178 }
179 #endif /* USE_BPF_SEND */
180
181 #if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
182 /* Packet filter program...
183 XXX Changes to the filter program may require changes to the constant
184 offsets used in if_register_send to patch the BPF program! XXX */
185
186 struct bpf_insn dhcp_bpf_filter [] = {
187 /* Make sure this is an IP packet... */
188 BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12),
189 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
190
191 /* Make sure it's a UDP packet... */
192 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23),
193 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
194
195 /* Make sure this isn't a fragment... */
196 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
197 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
198
199 /* Get the IP header length... */
200 BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14),
201
202 /* Make sure it's to the right port... */
203 BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16),
204 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */
205
206 /* If we passed all the tests, ask for the whole packet. */
207 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
208
209 /* Otherwise, drop it. */
210 BPF_STMT(BPF_RET+BPF_K, 0),
211 };
212
213 struct bpf_insn *bpf_fddi_filter;
214
215 int dhcp_bpf_filter_len = sizeof dhcp_bpf_filter / sizeof (struct bpf_insn);
216 struct bpf_insn dhcp_bpf_tr_filter [] = {
217 /* accept all token ring packets due to variable length header */
218 /* if we want to get clever, insert the program here */
219
220 /* If we passed all the tests, ask for the whole packet. */
221 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
222
223 /* Otherwise, drop it. */
224 BPF_STMT(BPF_RET+BPF_K, 0),
225 };
226
227 int dhcp_bpf_tr_filter_len = (sizeof dhcp_bpf_tr_filter /
228 sizeof (struct bpf_insn));
229 #endif
230
231 #if defined (USE_BPF_RECEIVE)
232 void if_register_receive (info)
233 struct interface_info *info;
234 {
235 int flag = 1;
236 struct bpf_version v;
237 u_int32_t addr;
238 struct bpf_program p;
239 u_int32_t bits;
240 #ifdef DEC_FDDI
241 int link_layer;
242 #endif /* DEC_FDDI */
243
244 /* Open a BPF device and hang it on this interface... */
245 info -> rfdesc = if_register_bpf (info);
246
247 /* Make sure the BPF version is in range... */
248 if (ioctl (info -> rfdesc, BIOCVERSION, &v) < 0)
249 log_fatal ("Can't get BPF version: %m");
250
251 if (v.bv_major != BPF_MAJOR_VERSION ||
252 v.bv_minor < BPF_MINOR_VERSION)
253 log_fatal ("Kernel BPF version out of range - recompile dhcpd!");
254
255 /* Set immediate mode so that reads return as soon as a packet
256 comes in, rather than waiting for the input buffer to fill with
257 packets. */
258 if (ioctl (info -> rfdesc, BIOCIMMEDIATE, &flag) < 0)
259 log_fatal ("Can't set immediate mode on bpf device: %m");
260
261 #ifdef NEED_OSF_PFILT_HACKS
262 /* Allow the copyall flag to be set... */
263 if (ioctl(info -> rfdesc, EIOCALLOWCOPYALL, &flag) < 0)
264 log_fatal ("Can't set ALLOWCOPYALL: %m");
265
266 /* Clear all the packet filter mode bits first... */
267 bits = 0;
268 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
269 log_fatal ("Can't clear pfilt bits: %m");
270
271 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
272 bits = ENBATCH | ENCOPYALL | ENBPFHDR;
273 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
274 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
275 #endif
276 /* Get the required BPF buffer length from the kernel. */
277 if (ioctl (info -> rfdesc, BIOCGBLEN, &info -> rbuf_max) < 0)
278 log_fatal ("Can't get bpf buffer length: %m");
279 info -> rbuf = dmalloc (info -> rbuf_max, MDL);
280 if (!info -> rbuf)
281 log_fatal ("Can't allocate %d bytes for bpf input buffer.",
282 info -> rbuf_max);
283 info -> rbuf_offset = 0;
284 info -> rbuf_len = 0;
285
286 /* Set up the bpf filter program structure. */
287 p.bf_len = dhcp_bpf_filter_len;
288
289 #ifdef DEC_FDDI
290 /* See if this is an FDDI interface, flag it for later. */
291 if (ioctl(info -> rfdesc, BIOCGDLT, &link_layer) >= 0 &&
292 link_layer == DLT_FDDI) {
293 if (!bpf_fddi_filter) {
294 bpf_fddi_filter = dmalloc (sizeof bpf_fddi_filter,
295 MDL);
296 if (!bpf_fddi_filter)
297 log_fatal ("No memory for FDDI filter.");
298 memcpy (bpf_fddi_filter,
299 dhcp_bpf_filter, sizeof dhcp_bpf_filter);
300 /* Patch the BPF program to account for the difference
301 in length between ethernet headers (14), FDDI and
302 802.2 headers (16 +8=24, +10).
303 XXX changes to filter program may require changes to
304 XXX the insn number(s) used below! */
305 bpf_fddi_filter[0].k += 10;
306 bpf_fddi_filter[2].k += 10;
307 bpf_fddi_filter[4].k += 10;
308 bpf_fddi_filter[6].k += 10;
309 bpf_fddi_filter[7].k += 10;
310 }
311 p.bf_insns = bpf_fddi_filter;
312 } else
313 #endif /* DEC_FDDI */
314 p.bf_insns = dhcp_bpf_filter;
315
316 /* Patch the server port into the BPF program...
317 XXX changes to filter program may require changes
318 to the insn number(s) used below! XXX */
319 dhcp_bpf_filter [8].k = ntohs (local_port);
320
321 if (ioctl (info -> rfdesc, BIOCSETF, &p) < 0)
322 log_fatal ("Can't install packet filter program: %m");
323 if (!quiet_interface_discovery)
324 log_info ("Listening on BPF/%s/%s%s%s",
325 info -> name,
326 print_hw_addr (info -> hw_address.hbuf [0],
327 info -> hw_address.hlen - 1,
328 &info -> hw_address.hbuf [1]),
329 (info -> shared_network ? "/" : ""),
330 (info -> shared_network ?
331 info -> shared_network -> name : ""));
332 }
333
334 void if_deregister_receive (info)
335 struct interface_info *info;
336 {
337 close (info -> rfdesc);
338 info -> rfdesc = -1;
339
340 if (!quiet_interface_discovery)
341 log_info ("Disabling input on BPF/%s/%s%s%s",
342 info -> name,
343 print_hw_addr (info -> hw_address.hbuf [0],
344 info -> hw_address.hlen - 1,
345 &info -> hw_address.hbuf [1]),
346 (info -> shared_network ? "/" : ""),
347 (info -> shared_network ?
348 info -> shared_network -> name : ""));
349 }
350 #endif /* USE_BPF_RECEIVE */
351
352 #ifdef USE_BPF_SEND
353 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
354 struct interface_info *interface;
355 struct packet *packet;
356 struct dhcp_packet *raw;
357 size_t len;
358 struct in_addr from;
359 struct sockaddr_in *to;
360 struct hardware *hto;
361 {
362 unsigned hbufp = 0, ibufp = 0;
363 double hw [4];
364 double ip [32];
365 struct iovec iov [3];
366 int result;
367 int fudge;
368
369 if (!strcmp (interface -> name, "fallback"))
370 return send_fallback (interface, packet, raw,
371 len, from, to, hto);
372
373 /* Assemble the headers... */
374 assemble_hw_header (interface, (unsigned char *)hw, &hbufp, hto);
375 assemble_udp_ip_header (interface,
376 (unsigned char *)ip, &ibufp, from.s_addr,
377 to -> sin_addr.s_addr, to -> sin_port,
378 (unsigned char *)raw, len);
379
380 /* Fire it off */
381 iov [0].iov_base = ((char *)hw);
382 iov [0].iov_len = hbufp;
383 iov [1].iov_base = ((char *)ip);
384 iov [1].iov_len = ibufp;
385 iov [2].iov_base = (char *)raw;
386 iov [2].iov_len = len;
387
388 result = writev(interface -> wfdesc, iov, 3);
389 if (result < 0)
390 log_error ("send_packet: %m");
391 return result;
392 }
393 #endif /* USE_BPF_SEND */
394
395 #ifdef USE_BPF_RECEIVE
396 ssize_t receive_packet (interface, buf, len, from, hfrom)
397 struct interface_info *interface;
398 unsigned char *buf;
399 size_t len;
400 struct sockaddr_in *from;
401 struct hardware *hfrom;
402 {
403 int length = 0;
404 int offset = 0;
405 struct bpf_hdr hdr;
406
407 /* All this complexity is because BPF doesn't guarantee
408 that only one packet will be returned at a time. We're
409 getting what we deserve, though - this is a terrible abuse
410 of the BPF interface. Sigh. */
411
412 /* Process packets until we get one we can return or until we've
413 done a read and gotten nothing we can return... */
414
415 do {
416 /* If the buffer is empty, fill it. */
417 if (interface -> rbuf_offset == interface -> rbuf_len) {
418 length = read (interface -> rfdesc,
419 interface -> rbuf,
420 interface -> rbuf_max);
421 if (length <= 0)
422 return length;
423 interface -> rbuf_offset = 0;
424 interface -> rbuf_len = length;
425 }
426
427 /* If there isn't room for a whole bpf header, something went
428 wrong, but we'll ignore it and hope it goes away... XXX */
429 if (interface -> rbuf_len -
430 interface -> rbuf_offset < sizeof hdr) {
431 interface -> rbuf_offset = interface -> rbuf_len;
432 continue;
433 }
434
435 /* Copy out a bpf header... */
436 memcpy (&hdr, &interface -> rbuf [interface -> rbuf_offset],
437 sizeof hdr);
438
439 /* If the bpf header plus data doesn't fit in what's left
440 of the buffer, stick head in sand yet again... */
441 if (interface -> rbuf_offset +
442 hdr.bh_hdrlen + hdr.bh_caplen > interface -> rbuf_len) {
443 interface -> rbuf_offset = interface -> rbuf_len;
444 continue;
445 }
446
447 /* If the captured data wasn't the whole packet, or if
448 the packet won't fit in the input buffer, all we
449 can do is drop it. */
450 if (hdr.bh_caplen != hdr.bh_datalen) {
451 interface -> rbuf_offset +=
452 hdr.bh_hdrlen = hdr.bh_caplen;
453 continue;
454 }
455
456 /* Skip over the BPF header... */
457 interface -> rbuf_offset += hdr.bh_hdrlen;
458
459 /* Decode the physical header... */
460 offset = decode_hw_header (interface,
461 interface -> rbuf,
462 interface -> rbuf_offset,
463 hfrom);
464
465 /* If a physical layer checksum failed (dunno of any
466 physical layer that supports this, but WTH), skip this
467 packet. */
468 if (offset < 0) {
469 interface -> rbuf_offset += hdr.bh_caplen;
470 continue;
471 }
472 interface -> rbuf_offset += offset;
473 hdr.bh_caplen -= offset;
474
475 /* Decode the IP and UDP headers... */
476 offset = decode_udp_ip_header (interface,
477 interface -> rbuf,
478 interface -> rbuf_offset,
479 from,
480 (unsigned char *)0,
481 hdr.bh_caplen);
482
483 /* If the IP or UDP checksum was bad, skip the packet... */
484 if (offset < 0) {
485 interface -> rbuf_offset += hdr.bh_caplen;
486 continue;
487 }
488 interface -> rbuf_offset += offset;
489 hdr.bh_caplen -= offset;
490
491 /* If there's not enough room to stash the packet data,
492 we have to skip it (this shouldn't happen in real
493 life, though). */
494 if (hdr.bh_caplen > len) {
495 interface -> rbuf_offset += hdr.bh_caplen;
496 continue;
497 }
498
499 /* Copy out the data in the packet... */
500 memcpy (buf, interface -> rbuf + interface -> rbuf_offset,
501 hdr.bh_caplen);
502 interface -> rbuf_offset += hdr.bh_caplen;
503 return hdr.bh_caplen;
504 } while (!length);
505 return 0;
506 }
507
508 int can_unicast_without_arp (ip)
509 struct interface_info *ip;
510 {
511 return 1;
512 }
513
514 int can_receive_unicast_unconfigured (ip)
515 struct interface_info *ip;
516 {
517 return 1;
518 }
519
520 int supports_multiple_interfaces (ip)
521 struct interface_info *ip;
522 {
523 return 1;
524 }
525
526 void maybe_setup_fallback ()
527 {
528 isc_result_t status;
529 struct interface_info *fbi = (struct interface_info *)0;
530 if (setup_fallback (&fbi, MDL)) {
531 if_register_fallback (fbi);
532 status = omapi_register_io_object ((omapi_object_t *)fbi,
533 if_readsocket, 0,
534 fallback_discard, 0, 0);
535 if (status != ISC_R_SUCCESS)
536 log_fatal ("Can't register I/O handle for %s: %s",
537 fbi -> name, isc_result_totext (status));
538 interface_dereference (&fbi, MDL);
539 }
540 }
541 #endif