etc/protocols - sync with NetBSD-8
[minix.git] / external / bsd / dhcp / dist / common / bpf.c
blob7e760352766f7acd6faa0201594eac69136bd542
1 /* $NetBSD: bpf.c,v 1.3 2014/07/12 12:09:37 spz Exp $ */
2 /* bpf.c
4 BPF socket interface code, originally contributed by Archie Cobbs. */
6 /*
7 * Copyright (c) 2009,2012-2014 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
9 * Copyright (c) 1996-2003 by Internet Software Consortium
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Internet Systems Consortium, Inc.
24 * 950 Charter Street
25 * Redwood City, CA 94063
26 * <info@isc.org>
27 * https://www.isc.org/
29 * This software was contributed to Internet Systems Consortium
30 * by Archie Cobbs.
32 * Patches for FDDI support on Digital Unix were written by Bill
33 * Stapleton, and maintained for a while by Mike Meredith before he
34 * managed to get me to integrate them.
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: bpf.c,v 1.3 2014/07/12 12:09:37 spz Exp $");
40 #include "dhcpd.h"
41 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
42 || defined (USE_LPF_RECEIVE)
43 # if defined (USE_LPF_RECEIVE)
44 # include <asm/types.h>
45 # include <linux/filter.h>
46 # define bpf_insn sock_filter /* Linux: dare to be gratuitously different. */
47 # else
48 # include <sys/ioctl.h>
49 # include <sys/uio.h>
50 # include <net/bpf.h>
51 # if defined (NEED_OSF_PFILT_HACKS)
52 # include <net/pfilt.h>
53 # endif
54 # endif
56 #include <sys/param.h>
57 #include <netinet/in_systm.h>
58 #include "includes/netinet/ip.h"
59 #include "includes/netinet/udp.h"
60 #include "includes/netinet/if_ether.h"
61 #endif
63 #if defined(USE_BPF_SEND) || defined(USE_BPF_RECEIVE) || defined(USE_BPF_HWADDR)
64 #include <net/if_types.h>
65 #include <ifaddrs.h>
66 #endif
68 #include <errno.h>
70 /* Reinitializes the specified interface after an address change. This
71 is not required for packet-filter APIs. */
73 #ifdef USE_BPF_SEND
74 void if_reinitialize_send (info)
75 struct interface_info *info;
78 #endif
80 #ifdef USE_BPF_RECEIVE
81 void if_reinitialize_receive (info)
82 struct interface_info *info;
85 #endif
87 /* Called by get_interface_list for each interface that's discovered.
88 Opens a packet filter for each interface and adds it to the select
89 mask. */
91 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
92 int if_register_bpf (info)
93 struct interface_info *info;
95 int sock;
96 char filename[50];
97 int b;
99 /* Open a BPF device */
100 for (b = 0; 1; b++) {
101 /* %Audit% 31 bytes max. %2004.06.17,Safe% */
102 sprintf(filename, BPF_FORMAT, b);
103 sock = open (filename, O_RDWR, 0);
104 if (sock < 0) {
105 if (errno == EBUSY) {
106 continue;
107 } else {
108 if (!b)
109 log_fatal ("No bpf devices.%s%s%s",
110 " Please read the README",
111 " section for your operating",
112 " system.");
113 log_fatal ("Can't find free bpf: %m");
115 } else {
116 break;
120 /* Set the BPF device to point at this interface. */
121 if (ioctl (sock, BIOCSETIF, info -> ifp) < 0)
122 log_fatal ("Can't attach interface %s to bpf device %s: %m",
123 info -> name, filename);
125 get_hw_addr(info->name, &info->hw_address);
127 return sock;
129 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
131 #ifdef USE_BPF_SEND
132 void if_register_send (info)
133 struct interface_info *info;
135 /* If we're using the bpf API for sending and receiving,
136 we don't need to register this interface twice. */
137 #ifndef USE_BPF_RECEIVE
138 info -> wfdesc = if_register_bpf (info, interface);
139 #else
140 info -> wfdesc = info -> rfdesc;
141 #endif
142 if (!quiet_interface_discovery)
143 log_info ("Sending on BPF/%s/%s%s%s",
144 info -> name,
145 print_hw_addr (info -> hw_address.hbuf [0],
146 info -> hw_address.hlen - 1,
147 &info -> hw_address.hbuf [1]),
148 (info -> shared_network ? "/" : ""),
149 (info -> shared_network ?
150 info -> shared_network -> name : ""));
153 void if_deregister_send (info)
154 struct interface_info *info;
156 /* If we're using the bpf API for sending and receiving,
157 we don't need to register this interface twice. */
158 #ifndef USE_BPF_RECEIVE
159 close (info -> wfdesc);
160 #endif
161 info -> wfdesc = -1;
163 if (!quiet_interface_discovery)
164 log_info ("Disabling output on BPF/%s/%s%s%s",
165 info -> name,
166 print_hw_addr (info -> hw_address.hbuf [0],
167 info -> hw_address.hlen - 1,
168 &info -> hw_address.hbuf [1]),
169 (info -> shared_network ? "/" : ""),
170 (info -> shared_network ?
171 info -> shared_network -> name : ""));
173 #endif /* USE_BPF_SEND */
175 #if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
176 /* Packet filter program...
177 XXX Changes to the filter program may require changes to the constant
178 offsets used in if_register_send to patch the BPF program! XXX */
180 struct bpf_insn dhcp_bpf_filter [] = {
181 /* Make sure this is an IP packet... */
182 BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12),
183 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
185 /* Make sure it's a UDP packet... */
186 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23),
187 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
189 /* Make sure this isn't a fragment... */
190 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
191 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
193 /* Get the IP header length... */
194 BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14),
196 /* Make sure it's to the right port... */
197 BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16),
198 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */
200 /* If we passed all the tests, ask for the whole packet. */
201 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
203 /* Otherwise, drop it. */
204 BPF_STMT(BPF_RET+BPF_K, 0),
207 #if defined (DEC_FDDI)
208 struct bpf_insn *bpf_fddi_filter;
209 #endif
211 int dhcp_bpf_filter_len = sizeof dhcp_bpf_filter / sizeof (struct bpf_insn);
212 #if defined (HAVE_TR_SUPPORT)
213 struct bpf_insn dhcp_bpf_tr_filter [] = {
214 /* accept all token ring packets due to variable length header */
215 /* if we want to get clever, insert the program here */
217 /* If we passed all the tests, ask for the whole packet. */
218 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
220 /* Otherwise, drop it. */
221 BPF_STMT(BPF_RET+BPF_K, 0),
224 int dhcp_bpf_tr_filter_len = (sizeof dhcp_bpf_tr_filter /
225 sizeof (struct bpf_insn));
226 #endif /* HAVE_TR_SUPPORT */
227 #endif /* USE_LPF_RECEIVE || USE_BPF_RECEIVE */
229 #if defined (USE_BPF_RECEIVE)
230 void if_register_receive (info)
231 struct interface_info *info;
233 int flag = 1;
234 struct bpf_version v;
235 struct bpf_program p;
236 #ifdef NEED_OSF_PFILT_HACKS
237 u_int32_t bits;
238 #endif
239 #ifdef DEC_FDDI
240 int link_layer;
241 #endif /* DEC_FDDI */
243 /* Open a BPF device and hang it on this interface... */
244 info -> rfdesc = if_register_bpf (info);
246 /* Make sure the BPF version is in range... */
247 if (ioctl (info -> rfdesc, BIOCVERSION, &v) < 0)
248 log_fatal ("Can't get BPF version: %m");
250 if (v.bv_major != BPF_MAJOR_VERSION ||
251 v.bv_minor < BPF_MINOR_VERSION)
252 log_fatal ("BPF version mismatch - recompile DHCP!");
254 /* Set immediate mode so that reads return as soon as a packet
255 comes in, rather than waiting for the input buffer to fill with
256 packets. */
257 if (ioctl (info -> rfdesc, BIOCIMMEDIATE, &flag) < 0)
258 log_fatal ("Can't set immediate mode on bpf device: %m");
260 #ifdef NEED_OSF_PFILT_HACKS
261 /* Allow the copyall flag to be set... */
262 if (ioctl(info -> rfdesc, EIOCALLOWCOPYALL, &flag) < 0)
263 log_fatal ("Can't set ALLOWCOPYALL: %m");
265 /* Clear all the packet filter mode bits first... */
266 bits = 0;
267 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
268 log_fatal ("Can't clear pfilt bits: %m");
270 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
271 bits = ENBATCH | ENCOPYALL | ENBPFHDR;
272 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
273 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
274 #endif
275 /* Get the required BPF buffer length from the kernel. */
276 if (ioctl (info -> rfdesc, BIOCGBLEN, &info -> rbuf_max) < 0)
277 log_fatal ("Can't get bpf buffer length: %m");
278 info -> rbuf = dmalloc (info -> rbuf_max, MDL);
279 if (!info -> rbuf)
280 log_fatal ("Can't allocate %ld bytes for bpf input buffer.",
281 (long)(info -> rbuf_max));
282 info -> rbuf_offset = 0;
283 info -> rbuf_len = 0;
285 /* Set up the bpf filter program structure. */
286 p.bf_len = dhcp_bpf_filter_len;
288 #ifdef DEC_FDDI
289 /* See if this is an FDDI interface, flag it for later. */
290 if (ioctl(info -> rfdesc, BIOCGDLT, &link_layer) >= 0 &&
291 link_layer == DLT_FDDI) {
292 if (!bpf_fddi_filter) {
293 bpf_fddi_filter = dmalloc (sizeof bpf_fddi_filter,
294 MDL);
295 if (!bpf_fddi_filter)
296 log_fatal ("No memory for FDDI filter.");
297 memcpy (bpf_fddi_filter,
298 dhcp_bpf_filter, sizeof dhcp_bpf_filter);
299 /* Patch the BPF program to account for the difference
300 in length between ethernet headers (14), FDDI and
301 802.2 headers (16 +8=24, +10).
302 XXX changes to filter program may require changes to
303 XXX the insn number(s) used below! */
304 bpf_fddi_filter[0].k += 10;
305 bpf_fddi_filter[2].k += 10;
306 bpf_fddi_filter[4].k += 10;
307 bpf_fddi_filter[6].k += 10;
308 bpf_fddi_filter[7].k += 10;
310 p.bf_insns = bpf_fddi_filter;
311 } else
312 #endif /* DEC_FDDI */
313 p.bf_insns = dhcp_bpf_filter;
315 /* Patch the server port into the BPF program...
316 XXX changes to filter program may require changes
317 to the insn number(s) used below! XXX */
318 dhcp_bpf_filter [8].k = ntohs (local_port);
320 if (ioctl (info -> rfdesc, BIOCSETF, &p) < 0)
321 log_fatal ("Can't install packet filter program: %m");
322 if (!quiet_interface_discovery)
323 log_info ("Listening on BPF/%s/%s%s%s",
324 info -> name,
325 print_hw_addr (info -> hw_address.hbuf [0],
326 info -> hw_address.hlen - 1,
327 &info -> hw_address.hbuf [1]),
328 (info -> shared_network ? "/" : ""),
329 (info -> shared_network ?
330 info -> shared_network -> name : ""));
333 void if_deregister_receive (info)
334 struct interface_info *info;
336 close (info -> rfdesc);
337 info -> rfdesc = -1;
339 if (!quiet_interface_discovery)
340 log_info ("Disabling input on BPF/%s/%s%s%s",
341 info -> name,
342 print_hw_addr (info -> hw_address.hbuf [0],
343 info -> hw_address.hlen - 1,
344 &info -> hw_address.hbuf [1]),
345 (info -> shared_network ? "/" : ""),
346 (info -> shared_network ?
347 info -> shared_network -> name : ""));
349 #endif /* USE_BPF_RECEIVE */
351 #ifdef USE_BPF_SEND
352 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
353 struct interface_info *interface;
354 struct packet *packet;
355 struct dhcp_packet *raw;
356 size_t len;
357 struct in_addr from;
358 struct sockaddr_in *to;
359 struct hardware *hto;
361 unsigned hbufp = 0, ibufp = 0;
362 double hw [4];
363 double ip [32];
364 struct iovec iov [3];
365 int result;
367 if (!strcmp (interface -> name, "fallback"))
368 return send_fallback (interface, packet, raw,
369 len, from, to, hto);
371 if (hto == NULL && interface->anycast_mac_addr.hlen)
372 hto = &interface->anycast_mac_addr;
374 /* Assemble the headers... */
375 assemble_hw_header (interface, (unsigned char *)hw, &hbufp, hto);
376 assemble_udp_ip_header (interface,
377 (unsigned char *)ip, &ibufp, from.s_addr,
378 to -> sin_addr.s_addr, to -> sin_port,
379 (unsigned char *)raw, len);
381 /* Fire it off */
382 iov [0].iov_base = ((char *)hw);
383 iov [0].iov_len = hbufp;
384 iov [1].iov_base = ((char *)ip);
385 iov [1].iov_len = ibufp;
386 iov [2].iov_base = (char *)raw;
387 iov [2].iov_len = len;
389 result = writev(interface -> wfdesc, iov, 3);
390 if (result < 0)
391 log_error ("send_packet: %m");
392 return result;
394 #endif /* USE_BPF_SEND */
396 #ifdef USE_BPF_RECEIVE
397 ssize_t receive_packet (interface, buf, len, from, hfrom)
398 struct interface_info *interface;
399 unsigned char *buf;
400 size_t len;
401 struct sockaddr_in *from;
402 struct hardware *hfrom;
404 int length = 0;
405 int offset = 0;
406 struct bpf_hdr hdr;
407 unsigned paylen;
409 /* All this complexity is because BPF doesn't guarantee
410 that only one packet will be returned at a time. We're
411 getting what we deserve, though - this is a terrible abuse
412 of the BPF interface. Sigh. */
414 /* Process packets until we get one we can return or until we've
415 done a read and gotten nothing we can return... */
417 /* If the buffer is empty, fill it. */
418 if (interface->rbuf_offset >= interface->rbuf_len) {
419 length = read(interface->rfdesc, interface->rbuf,
420 (size_t)interface->rbuf_max);
421 if (length <= 0) {
422 #ifdef __FreeBSD__
423 if (errno == ENXIO) {
424 #else
425 if (errno == EIO) {
426 #endif
427 dhcp_interface_remove
428 ((omapi_object_t *)interface, NULL);
430 return (length);
432 interface->rbuf_offset = 0;
433 interface->rbuf_len = BPF_WORDALIGN(length);
436 do {
437 /* If there isn't room for a whole bpf header, something went
438 wrong, but we'll ignore it and hope it goes away... XXX */
439 if (interface->rbuf_len -
440 interface->rbuf_offset < sizeof hdr) {
441 interface->rbuf_offset = interface->rbuf_len;
442 continue;
445 /* Copy out a bpf header... */
446 memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
447 sizeof hdr);
449 /* If the bpf header plus data doesn't fit in what's left
450 of the buffer, stick head in sand yet again... */
451 if (interface->rbuf_offset +
452 hdr.bh_hdrlen + hdr.bh_caplen > interface->rbuf_len) {
453 interface->rbuf_offset = interface->rbuf_len;
454 continue;
457 /* If the captured data wasn't the whole packet, or if
458 the packet won't fit in the input buffer, all we
459 can do is drop it. */
460 if (hdr.bh_caplen != hdr.bh_datalen) {
461 interface->rbuf_offset =
462 BPF_WORDALIGN(interface->rbuf_offset +
463 hdr.bh_hdrlen + hdr.bh_caplen);
464 continue;
467 /* Skip over the BPF header... */
468 interface->rbuf_offset += hdr.bh_hdrlen;
470 /* Decode the physical header... */
471 offset = decode_hw_header(interface, interface->rbuf,
472 interface->rbuf_offset, hfrom);
474 /* If a physical layer checksum failed (dunno of any
475 physical layer that supports this, but WTH), skip this
476 packet. */
477 if (offset < 0) {
478 interface->rbuf_offset =
479 BPF_WORDALIGN(interface->rbuf_offset +
480 hdr.bh_caplen);
481 continue;
483 interface->rbuf_offset += offset;
484 hdr.bh_caplen -= offset;
486 /* Decode the IP and UDP headers... */
487 offset = decode_udp_ip_header(interface, interface->rbuf,
488 interface->rbuf_offset,
489 from, hdr.bh_caplen, &paylen);
491 /* If the IP or UDP checksum was bad, skip the packet... */
492 if (offset < 0) {
493 interface->rbuf_offset =
494 BPF_WORDALIGN(interface->rbuf_offset +
495 hdr.bh_caplen);
496 continue;
498 interface->rbuf_offset = interface->rbuf_offset + offset;
499 hdr.bh_caplen -= offset;
501 /* If there's not enough room to stash the packet data,
502 we have to skip it (this shouldn't happen in real
503 life, though). */
504 if (hdr.bh_caplen > len) {
505 interface->rbuf_offset =
506 BPF_WORDALIGN(interface->rbuf_offset +
507 hdr.bh_caplen);
508 continue;
511 /* Copy out the data in the packet... */
512 memcpy(buf, interface->rbuf + interface->rbuf_offset, paylen);
513 interface->rbuf_offset =
514 BPF_WORDALIGN(interface->rbuf_offset + hdr.bh_caplen);
515 return paylen;
516 } while (interface->rbuf_offset < interface->rbuf_len);
518 return (0);
521 int can_unicast_without_arp (ip)
522 struct interface_info *ip;
524 return 1;
527 int can_receive_unicast_unconfigured (ip)
528 struct interface_info *ip;
530 return 1;
533 int supports_multiple_interfaces (ip)
534 struct interface_info *ip;
536 return 1;
539 void maybe_setup_fallback ()
541 isc_result_t status;
542 struct interface_info *fbi = (struct interface_info *)0;
543 if (setup_fallback (&fbi, MDL)) {
544 if_register_fallback (fbi);
545 status = omapi_register_io_object ((omapi_object_t *)fbi,
546 if_readsocket, 0,
547 fallback_discard, 0, 0);
548 if (status != ISC_R_SUCCESS)
549 log_fatal ("Can't register I/O handle for %s: %s",
550 fbi -> name, isc_result_totext (status));
551 interface_dereference (&fbi, MDL);
555 #endif
557 #if defined(USE_BPF_RECEIVE) || defined(USE_BPF_HWADDR)
558 static int
559 lladdr_active(int s, const char *name, const struct ifaddrs *ifa)
561 if (ifa->ifa_addr->sa_family != AF_LINK)
562 return 0;
563 if (strcmp(ifa->ifa_name, name) != 0)
564 return 0;
566 #ifdef SIOCGLIFADDR
568 struct if_laddrreq iflr;
569 const struct sockaddr_dl *sdl;
571 sdl = satocsdl(ifa->ifa_addr);
572 memset(&iflr, 0, sizeof(iflr));
574 strlcpy(iflr.iflr_name, ifa->ifa_name, sizeof(iflr.iflr_name));
575 memcpy(&iflr.addr, ifa->ifa_addr, MIN(ifa->ifa_addr->sa_len,
576 sizeof(iflr.addr)));
577 iflr.flags = IFLR_PREFIX;
578 iflr.prefixlen = sdl->sdl_alen * NBBY;
580 if (ioctl(s, SIOCGLIFADDR, &iflr) == -1) {
581 log_fatal("ioctl(SIOCGLIFADDR): %m");
584 if ((iflr.flags & IFLR_ACTIVE) == 0)
585 return 0;
587 #endif
588 return 1;
591 void
592 get_hw_addr(const char *name, struct hardware *hw) {
593 struct ifaddrs *ifa;
594 struct ifaddrs *p;
595 struct sockaddr_dl *sa;
596 int s;
598 if ((s = socket(AF_LINK, SOCK_DGRAM, 0)) == -1) {
599 log_fatal("socket AF_LINK: %m");
602 if (getifaddrs(&ifa) != 0) {
603 log_fatal("Error getting interface information; %m");
607 * Loop through our interfaces finding a match.
609 sa = NULL;
610 for (p = ifa; p != NULL; p = p->ifa_next) {
611 if (lladdr_active(s, name, p)) {
612 sa = (struct sockaddr_dl *)p->ifa_addr;
613 break;
616 if (sa == NULL) {
617 log_fatal("No interface called '%s'", name);
619 close(s);
622 * Pull out the appropriate information.
624 switch (sa->sdl_type) {
625 case IFT_ETHER:
626 hw->hlen = sa->sdl_alen + 1;
627 hw->hbuf[0] = HTYPE_ETHER;
628 memcpy(&hw->hbuf[1], LLADDR(sa), sa->sdl_alen);
629 break;
630 case IFT_ISO88023:
631 case IFT_ISO88024: /* "token ring" */
632 case IFT_ISO88025:
633 case IFT_ISO88026:
634 hw->hlen = sa->sdl_alen + 1;
635 hw->hbuf[0] = HTYPE_IEEE802;
636 memcpy(&hw->hbuf[1], LLADDR(sa), sa->sdl_alen);
637 break;
638 #ifdef IFT_FDDI
639 case IFT_FDDI:
640 hw->hlen = sa->sdl_alen + 1;
641 hw->hbuf[0] = HTYPE_FDDI;
642 memcpy(&hw->hbuf[1], LLADDR(sa), sa->sdl_alen);
643 break;
644 #endif /* IFT_FDDI */
645 default:
646 log_fatal("Unsupported device type %d for \"%s\"",
647 sa->sdl_type, name);
650 freeifaddrs(ifa);
652 #endif