Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / stacks / AROSTCP / dhcp / common / bpf.c
blob4f0e5aa73f9c66cb946fb93bbbf32be93ec9718f
1 /* bpf.c
3 BPF socket interface code, originally contributed by Archie Cobbs. */
5 /*
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996-2003 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
22 * 950 Charter Street
23 * Redwood City, CA 94063
24 * <info@isc.org>
25 * http://www.isc.org/
27 * This software was contributed to Internet Systems Consortium
28 * by Archie Cobbs.
30 * Patches for FDDI support on Digital Unix were written by Bill
31 * Stapleton, and maintained for a while by Mike Meredith before he
32 * managed to get me to integrate them.
35 #if 0
36 static char copyright[] =
37 "$Id$ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
38 #endif
40 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
41 || defined (USE_LPF_RECEIVE)
42 # if defined (USE_LPF_RECEIVE)
43 # include "dhcpd.h"
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 /* This one's for BSD based OSes (dunno if it breaks any other OSes) */
49 # define USE_SOCKET_FALLBACK
50 # include "dhcpd.h"
51 # include <sys/ioctl.h>
52 # include <sys/uio.h>
53 # include "/usr/include/net/bpf.h"
54 # if defined (NEED_OSF_PFILT_HACKS)
55 # include </usr/include/net/pfilt.h>
56 # endif
57 # endif
59 #include <netinet/in_systm.h>
60 #include "includes/netinet/ip.h"
61 #include "includes/netinet/udp.h"
62 #include "includes/netinet/if_ether.h"
63 #endif
65 /* Reinitializes the specified interface after an address change. This
66 is not required for packet-filter APIs. */
68 #ifdef USE_BPF_SEND
69 void if_reinitialize_send (info)
70 struct interface_info *info;
73 #endif
75 #ifdef USE_BPF_RECEIVE
76 void if_reinitialize_receive (info)
77 struct interface_info *info;
80 #endif
82 /* Called by get_interface_list for each interface that's discovered.
83 Opens a packet filter for each interface and adds it to the select
84 mask. */
86 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
87 int if_register_bpf (info)
88 struct interface_info *info;
90 int sock;
91 char filename[50];
92 int b;
94 /* Open a BPF device */
95 for (b = 0; 1; b++) {
96 /* %Audit% 31 bytes max. %2004.06.17,Safe% */
97 sprintf(filename, BPF_FORMAT, b);
98 sock = open (filename, O_RDWR, 0);
99 if (sock < 0) {
100 if (errno == EBUSY) {
101 continue;
102 } else {
103 if (!b)
104 log_fatal ("No bpf devices.%s%s%s",
105 " Please read the README",
106 " section for your operating",
107 " system.");
108 log_fatal ("Can't find free bpf: %m");
110 } else {
111 break;
115 /* Set the BPF device to point at this interface. */
116 if (ioctl (sock, BIOCSETIF, info -> ifp) < 0)
117 log_fatal ("Can't attach interface %s to bpf device %s: %m",
118 info -> name, filename);
120 return sock;
122 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
124 #ifdef USE_BPF_SEND
125 void if_register_send (info)
126 struct interface_info *info;
128 /* If we're using the bpf API for sending and receiving,
129 we don't need to register this interface twice. */
130 #ifndef USE_BPF_RECEIVE
131 info -> wfdesc = if_register_bpf (info, interface);
132 #else
133 info -> wfdesc = info -> rfdesc;
134 #endif
135 if (!quiet_interface_discovery)
136 log_info ("Sending on BPF/%s/%s%s%s",
137 info -> name,
138 print_hw_addr (info -> hw_address.hbuf [0],
139 info -> hw_address.hlen - 1,
140 &info -> hw_address.hbuf [1]),
141 (info -> shared_network ? "/" : ""),
142 (info -> shared_network ?
143 info -> shared_network -> name : ""));
146 void if_deregister_send (info)
147 struct interface_info *info;
149 /* If we're using the bpf API for sending and receiving,
150 we don't need to register this interface twice. */
151 #ifndef USE_BPF_RECEIVE
152 close (info -> wfdesc);
153 #endif
154 info -> wfdesc = -1;
156 if (!quiet_interface_discovery)
157 log_info ("Disabling output on BPF/%s/%s%s%s",
158 info -> name,
159 print_hw_addr (info -> hw_address.hbuf [0],
160 info -> hw_address.hlen - 1,
161 &info -> hw_address.hbuf [1]),
162 (info -> shared_network ? "/" : ""),
163 (info -> shared_network ?
164 info -> shared_network -> name : ""));
166 #endif /* USE_BPF_SEND */
168 #if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
169 /* Packet filter program...
170 XXX Changes to the filter program may require changes to the constant
171 offsets used in if_register_send to patch the BPF program! XXX */
173 struct bpf_insn dhcp_bpf_filter [] = {
174 /* Make sure this is an IP packet... */
175 BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12),
176 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
178 /* Make sure it's a UDP packet... */
179 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23),
180 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
182 /* Make sure this isn't a fragment... */
183 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
184 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
186 /* Get the IP header length... */
187 BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14),
189 /* Make sure it's to the right port... */
190 BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16),
191 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */
193 /* If we passed all the tests, ask for the whole packet. */
194 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
196 /* Otherwise, drop it. */
197 BPF_STMT(BPF_RET+BPF_K, 0),
200 #if defined (DEC_FDDI)
201 struct bpf_insn *bpf_fddi_filter;
202 #endif
204 int dhcp_bpf_filter_len = sizeof dhcp_bpf_filter / sizeof (struct bpf_insn);
205 #if defined (HAVE_TR_SUPPORT)
206 struct bpf_insn dhcp_bpf_tr_filter [] = {
207 /* accept all token ring packets due to variable length header */
208 /* if we want to get clever, insert the program here */
210 /* If we passed all the tests, ask for the whole packet. */
211 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
213 /* Otherwise, drop it. */
214 BPF_STMT(BPF_RET+BPF_K, 0),
217 int dhcp_bpf_tr_filter_len = (sizeof dhcp_bpf_tr_filter /
218 sizeof (struct bpf_insn));
219 #endif /* HAVE_TR_SUPPORT */
220 #endif /* USE_LPF_RECEIVE || USE_BPF_RECEIVE */
222 #if defined (USE_BPF_RECEIVE)
223 void if_register_receive (info)
224 struct interface_info *info;
226 int flag = 1;
227 struct bpf_version v;
228 u_int32_t addr;
229 struct bpf_program p;
230 u_int32_t bits;
231 #ifdef DEC_FDDI
232 int link_layer;
233 #endif /* DEC_FDDI */
235 /* Open a BPF device and hang it on this interface... */
236 info -> rfdesc = if_register_bpf (info);
238 /* Make sure the BPF version is in range... */
239 if (ioctl (info -> rfdesc, BIOCVERSION, &v) < 0)
240 log_fatal ("Can't get BPF version: %m");
242 if (v.bv_major != BPF_MAJOR_VERSION ||
243 v.bv_minor < BPF_MINOR_VERSION)
244 log_fatal ("BPF version mismatch - recompile DHCP!");
246 /* Set immediate mode so that reads return as soon as a packet
247 comes in, rather than waiting for the input buffer to fill with
248 packets. */
249 if (ioctl (info -> rfdesc, BIOCIMMEDIATE, &flag) < 0)
250 log_fatal ("Can't set immediate mode on bpf device: %m");
252 #ifdef NEED_OSF_PFILT_HACKS
253 /* Allow the copyall flag to be set... */
254 if (ioctl(info -> rfdesc, EIOCALLOWCOPYALL, &flag) < 0)
255 log_fatal ("Can't set ALLOWCOPYALL: %m");
257 /* Clear all the packet filter mode bits first... */
258 bits = 0;
259 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
260 log_fatal ("Can't clear pfilt bits: %m");
262 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
263 bits = ENBATCH | ENCOPYALL | ENBPFHDR;
264 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
265 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
266 #endif
267 /* Get the required BPF buffer length from the kernel. */
268 if (ioctl (info -> rfdesc, BIOCGBLEN, &info -> rbuf_max) < 0)
269 log_fatal ("Can't get bpf buffer length: %m");
270 info -> rbuf = dmalloc (info -> rbuf_max, MDL);
271 if (!info -> rbuf)
272 log_fatal ("Can't allocate %ld bytes for bpf input buffer.",
273 (long)(info -> rbuf_max));
274 info -> rbuf_offset = 0;
275 info -> rbuf_len = 0;
277 /* Set up the bpf filter program structure. */
278 p.bf_len = dhcp_bpf_filter_len;
280 #ifdef DEC_FDDI
281 /* See if this is an FDDI interface, flag it for later. */
282 if (ioctl(info -> rfdesc, BIOCGDLT, &link_layer) >= 0 &&
283 link_layer == DLT_FDDI) {
284 if (!bpf_fddi_filter) {
285 bpf_fddi_filter = dmalloc (sizeof bpf_fddi_filter,
286 MDL);
287 if (!bpf_fddi_filter)
288 log_fatal ("No memory for FDDI filter.");
289 memcpy (bpf_fddi_filter,
290 dhcp_bpf_filter, sizeof dhcp_bpf_filter);
291 /* Patch the BPF program to account for the difference
292 in length between ethernet headers (14), FDDI and
293 802.2 headers (16 +8=24, +10).
294 XXX changes to filter program may require changes to
295 XXX the insn number(s) used below! */
296 bpf_fddi_filter[0].k += 10;
297 bpf_fddi_filter[2].k += 10;
298 bpf_fddi_filter[4].k += 10;
299 bpf_fddi_filter[6].k += 10;
300 bpf_fddi_filter[7].k += 10;
302 p.bf_insns = bpf_fddi_filter;
303 } else
304 #endif /* DEC_FDDI */
305 p.bf_insns = dhcp_bpf_filter;
307 /* Patch the server port into the BPF program...
308 XXX changes to filter program may require changes
309 to the insn number(s) used below! XXX */
310 dhcp_bpf_filter [8].k = ntohs (local_port);
312 if (ioctl (info -> rfdesc, BIOCSETF, &p) < 0)
313 log_fatal ("Can't install packet filter program: %m");
314 if (!quiet_interface_discovery)
315 log_info ("Listening on BPF/%s/%s%s%s",
316 info -> name,
317 print_hw_addr (info -> hw_address.hbuf [0],
318 info -> hw_address.hlen - 1,
319 &info -> hw_address.hbuf [1]),
320 (info -> shared_network ? "/" : ""),
321 (info -> shared_network ?
322 info -> shared_network -> name : ""));
325 void if_deregister_receive (info)
326 struct interface_info *info;
328 close (info -> rfdesc);
329 info -> rfdesc = -1;
331 if (!quiet_interface_discovery)
332 log_info ("Disabling input on BPF/%s/%s%s%s",
333 info -> name,
334 print_hw_addr (info -> hw_address.hbuf [0],
335 info -> hw_address.hlen - 1,
336 &info -> hw_address.hbuf [1]),
337 (info -> shared_network ? "/" : ""),
338 (info -> shared_network ?
339 info -> shared_network -> name : ""));
341 #endif /* USE_BPF_RECEIVE */
343 #ifdef USE_BPF_SEND
344 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
345 struct interface_info *interface;
346 struct packet *packet;
347 struct dhcp_packet *raw;
348 size_t len;
349 struct in_addr from;
350 struct sockaddr_in *to;
351 struct hardware *hto;
353 unsigned hbufp = 0, ibufp = 0;
354 double hw [4];
355 double ip [32];
356 struct iovec iov [3];
357 int result;
358 int fudge;
360 if (!strcmp (interface -> name, "fallback"))
361 return send_fallback (interface, packet, raw,
362 len, from, to, hto);
364 /* Assemble the headers... */
365 assemble_hw_header (interface, (unsigned char *)hw, &hbufp, hto);
366 assemble_udp_ip_header (interface,
367 (unsigned char *)ip, &ibufp, from.s_addr,
368 to -> sin_addr.s_addr, to -> sin_port,
369 (unsigned char *)raw, len);
371 /* Fire it off */
372 iov [0].iov_base = ((char *)hw);
373 iov [0].iov_len = hbufp;
374 iov [1].iov_base = ((char *)ip);
375 iov [1].iov_len = ibufp;
376 iov [2].iov_base = (char *)raw;
377 iov [2].iov_len = len;
379 result = writev(interface -> wfdesc, iov, 3);
380 if (result < 0)
381 log_error ("send_packet: %m");
382 return result;
384 #endif /* USE_BPF_SEND */
386 #ifdef USE_BPF_RECEIVE
387 ssize_t receive_packet (interface, buf, len, from, hfrom)
388 struct interface_info *interface;
389 unsigned char *buf;
390 size_t len;
391 struct sockaddr_in *from;
392 struct hardware *hfrom;
394 int length = 0;
395 int offset = 0;
396 struct bpf_hdr hdr;
398 /* All this complexity is because BPF doesn't guarantee
399 that only one packet will be returned at a time. We're
400 getting what we deserve, though - this is a terrible abuse
401 of the BPF interface. Sigh. */
403 /* Process packets until we get one we can return or until we've
404 done a read and gotten nothing we can return... */
406 do {
407 /* If the buffer is empty, fill it. */
408 if (interface -> rbuf_offset == interface -> rbuf_len) {
409 length = read (interface -> rfdesc,
410 interface -> rbuf,
411 (size_t)interface -> rbuf_max);
412 if (length <= 0) {
413 #ifdef __FreeBSD__
414 if (errno == ENXIO) {
415 #else
416 if (errno == EIO) {
417 #endif
418 dhcp_interface_remove
419 ((omapi_object_t *)interface,
420 (omapi_object_t *)0);
422 return length;
424 interface -> rbuf_offset = 0;
425 interface -> rbuf_len = BPF_WORDALIGN (length);
428 /* If there isn't room for a whole bpf header, something went
429 wrong, but we'll ignore it and hope it goes away... XXX */
430 if (interface -> rbuf_len -
431 interface -> rbuf_offset < sizeof hdr) {
432 interface -> rbuf_offset = interface -> rbuf_len;
433 continue;
436 /* Copy out a bpf header... */
437 memcpy (&hdr, &interface -> rbuf [interface -> rbuf_offset],
438 sizeof hdr);
440 /* If the bpf header plus data doesn't fit in what's left
441 of the buffer, stick head in sand yet again... */
442 if (interface -> rbuf_offset +
443 hdr.bh_hdrlen + hdr.bh_caplen > interface -> rbuf_len) {
444 interface -> rbuf_offset = interface -> rbuf_len;
445 continue;
448 /* If the captured data wasn't the whole packet, or if
449 the packet won't fit in the input buffer, all we
450 can do is drop it. */
451 if (hdr.bh_caplen != hdr.bh_datalen) {
452 interface -> rbuf_offset =
453 BPF_WORDALIGN (interface -> rbuf_offset +
454 hdr.bh_hdrlen + hdr.bh_caplen);
455 continue;
458 /* Skip over the BPF header... */
459 interface -> rbuf_offset += hdr.bh_hdrlen;
461 /* Decode the physical header... */
462 offset = decode_hw_header (interface,
463 interface -> rbuf,
464 interface -> rbuf_offset,
465 hfrom);
467 /* If a physical layer checksum failed (dunno of any
468 physical layer that supports this, but WTH), skip this
469 packet. */
470 if (offset < 0) {
471 interface -> rbuf_offset =
472 BPF_WORDALIGN (interface -> rbuf_offset +
473 hdr.bh_caplen);
474 continue;
476 interface -> rbuf_offset += offset;
477 hdr.bh_caplen -= offset;
479 /* Decode the IP and UDP headers... */
480 offset = decode_udp_ip_header (interface,
481 interface -> rbuf,
482 interface -> rbuf_offset,
483 from,
484 hdr.bh_caplen);
486 /* If the IP or UDP checksum was bad, skip the packet... */
487 if (offset < 0) {
488 interface -> rbuf_offset =
489 BPF_WORDALIGN (interface -> rbuf_offset +
490 hdr.bh_caplen);
491 continue;
493 interface -> rbuf_offset = interface -> rbuf_offset + offset;
494 hdr.bh_caplen -= offset;
496 /* If there's not enough room to stash the packet data,
497 we have to skip it (this shouldn't happen in real
498 life, though). */
499 if (hdr.bh_caplen > len) {
500 interface -> rbuf_offset =
501 BPF_WORDALIGN (interface -> rbuf_offset +
502 hdr.bh_caplen);
503 continue;
506 /* Copy out the data in the packet... */
507 memcpy (buf, interface -> rbuf + interface -> rbuf_offset,
508 hdr.bh_caplen);
509 interface -> rbuf_offset =
510 BPF_WORDALIGN (interface -> rbuf_offset +
511 hdr.bh_caplen);
512 return hdr.bh_caplen;
513 } while (!length);
514 return 0;
517 int can_unicast_without_arp (ip)
518 struct interface_info *ip;
520 return 1;
523 int can_receive_unicast_unconfigured (ip)
524 struct interface_info *ip;
526 return 1;
529 int supports_multiple_interfaces (ip)
530 struct interface_info *ip;
532 return 1;
535 void maybe_setup_fallback ()
537 isc_result_t status;
538 struct interface_info *fbi = (struct interface_info *)0;
539 if (setup_fallback (&fbi, MDL)) {
540 if_register_fallback (fbi);
541 status = omapi_register_io_object ((omapi_object_t *)fbi,
542 if_readsocket, 0,
543 fallback_discard, 0, 0);
544 if (status != ISC_R_SUCCESS)
545 log_fatal ("Can't register I/O handle for %s: %s",
546 fbi -> name, isc_result_totext (status));
547 interface_dereference (&fbi, MDL);
550 #endif