Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / isc-dhcp / common / bpf.c
1 /* bpf.c
2
3    BPF socket interface code, originally contributed by Archie Cobbs. */
4
5 /*
6  * Copyright (c) 1996-2002 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.48.2.3 2002/11/17 02:26:56 dhankins Exp $ Copyright (c) 1995-2002 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 #if defined (DEC_FDDI)
214 struct bpf_insn *bpf_fddi_filter;
215 #endif
216
217 int dhcp_bpf_filter_len = sizeof dhcp_bpf_filter / sizeof (struct bpf_insn);
218 #if defined (HAVE_TR_SUPPORT)
219 struct bpf_insn dhcp_bpf_tr_filter [] = {
220         /* accept all token ring packets due to variable length header */
221         /* if we want to get clever, insert the program here */
222
223         /* If we passed all the tests, ask for the whole packet. */
224         BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
225
226         /* Otherwise, drop it. */
227         BPF_STMT(BPF_RET+BPF_K, 0),
228 };
229
230 int dhcp_bpf_tr_filter_len = (sizeof dhcp_bpf_tr_filter /
231                               sizeof (struct bpf_insn));
232 #endif /* HAVE_TR_SUPPORT */
233 #endif /* USE_LPF_RECEIVE || USE_BPF_RECEIVE */
234
235 #if defined (USE_BPF_RECEIVE)
236 void if_register_receive (info)
237         struct interface_info *info;
238 {
239         int flag = 1;
240         struct bpf_version v;
241         u_int32_t addr;
242         struct bpf_program p;
243         u_int32_t bits;
244 #ifdef DEC_FDDI
245         int link_layer;
246 #endif /* DEC_FDDI */
247
248         /* Open a BPF device and hang it on this interface... */
249         info -> rfdesc = if_register_bpf (info);
250
251         /* Make sure the BPF version is in range... */
252         if (ioctl (info -> rfdesc, BIOCVERSION, &v) < 0)
253                 log_fatal ("Can't get BPF version: %m");
254
255         if (v.bv_major != BPF_MAJOR_VERSION ||
256             v.bv_minor < BPF_MINOR_VERSION)
257                 log_fatal ("BPF version mismatch - recompile DHCP!");
258
259         /* Set immediate mode so that reads return as soon as a packet
260            comes in, rather than waiting for the input buffer to fill with
261            packets. */
262         if (ioctl (info -> rfdesc, BIOCIMMEDIATE, &flag) < 0)
263                 log_fatal ("Can't set immediate mode on bpf device: %m");
264
265 #ifdef NEED_OSF_PFILT_HACKS
266         /* Allow the copyall flag to be set... */
267         if (ioctl(info -> rfdesc, EIOCALLOWCOPYALL, &flag) < 0)
268                 log_fatal ("Can't set ALLOWCOPYALL: %m");
269
270         /* Clear all the packet filter mode bits first... */
271         bits = 0;
272         if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
273                 log_fatal ("Can't clear pfilt bits: %m");
274
275         /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
276         bits = ENBATCH | ENCOPYALL | ENBPFHDR;
277         if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
278                 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
279 #endif
280         /* Get the required BPF buffer length from the kernel. */
281         if (ioctl (info -> rfdesc, BIOCGBLEN, &info -> rbuf_max) < 0)
282                 log_fatal ("Can't get bpf buffer length: %m");
283         info -> rbuf = dmalloc (info -> rbuf_max, MDL);
284         if (!info -> rbuf)
285                 log_fatal ("Can't allocate %ld bytes for bpf input buffer.",
286                            (long)(info -> rbuf_max));
287         info -> rbuf_offset = 0;
288         info -> rbuf_len = 0;
289
290         /* Set up the bpf filter program structure. */
291         p.bf_len = dhcp_bpf_filter_len;
292
293 #ifdef DEC_FDDI
294         /* See if this is an FDDI interface, flag it for later. */
295         if (ioctl(info -> rfdesc, BIOCGDLT, &link_layer) >= 0 &&
296             link_layer == DLT_FDDI) {
297                 if (!bpf_fddi_filter) {
298                         bpf_fddi_filter = dmalloc (sizeof bpf_fddi_filter,
299                                                     MDL);
300                         if (!bpf_fddi_filter)
301                                 log_fatal ("No memory for FDDI filter.");
302                         memcpy (bpf_fddi_filter,
303                                 dhcp_bpf_filter, sizeof dhcp_bpf_filter);
304                         /* Patch the BPF program to account for the difference
305                            in length between ethernet headers (14), FDDI and
306                            802.2 headers (16 +8=24, +10).
307                            XXX changes to filter program may require changes to
308                            XXX the insn number(s) used below! */
309                         bpf_fddi_filter[0].k += 10;
310                         bpf_fddi_filter[2].k += 10;
311                         bpf_fddi_filter[4].k += 10;
312                         bpf_fddi_filter[6].k += 10;
313                         bpf_fddi_filter[7].k += 10;
314                 }
315                 p.bf_insns = bpf_fddi_filter;
316         } else
317 #endif /* DEC_FDDI */
318         p.bf_insns = dhcp_bpf_filter;
319
320         /* Patch the server port into the BPF  program...
321            XXX changes to filter program may require changes
322            to the insn number(s) used below! XXX */
323         dhcp_bpf_filter [8].k = ntohs (local_port);
324
325         if (ioctl (info -> rfdesc, BIOCSETF, &p) < 0)
326                 log_fatal ("Can't install packet filter program: %m");
327         if (!quiet_interface_discovery)
328                 log_info ("Listening on BPF/%s/%s%s%s",
329                       info -> name,
330                       print_hw_addr (info -> hw_address.hbuf [0],
331                                      info -> hw_address.hlen - 1,
332                                      &info -> hw_address.hbuf [1]),
333                       (info -> shared_network ? "/" : ""),
334                       (info -> shared_network ?
335                        info -> shared_network -> name : ""));
336 }
337
338 void if_deregister_receive (info)
339         struct interface_info *info;
340 {
341         close (info -> rfdesc);
342         info -> rfdesc = -1;
343
344         if (!quiet_interface_discovery)
345                 log_info ("Disabling input on BPF/%s/%s%s%s",
346                       info -> name,
347                       print_hw_addr (info -> hw_address.hbuf [0],
348                                      info -> hw_address.hlen - 1,
349                                      &info -> hw_address.hbuf [1]),
350                       (info -> shared_network ? "/" : ""),
351                       (info -> shared_network ?
352                        info -> shared_network -> name : ""));
353 }
354 #endif /* USE_BPF_RECEIVE */
355
356 #ifdef USE_BPF_SEND
357 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
358         struct interface_info *interface;
359         struct packet *packet;
360         struct dhcp_packet *raw;
361         size_t len;
362         struct in_addr from;
363         struct sockaddr_in *to;
364         struct hardware *hto;
365 {
366         unsigned hbufp = 0, ibufp = 0;
367         double hw [4];
368         double ip [32];
369         struct iovec iov [3];
370         int result;
371         int fudge;
372
373         if (!strcmp (interface -> name, "fallback"))
374                 return send_fallback (interface, packet, raw,
375                                       len, from, to, hto);
376
377         /* Assemble the headers... */
378         assemble_hw_header (interface, (unsigned char *)hw, &hbufp, hto);
379         assemble_udp_ip_header (interface,
380                                 (unsigned char *)ip, &ibufp, from.s_addr,
381                                 to -> sin_addr.s_addr, to -> sin_port,
382                                 (unsigned char *)raw, len);
383
384         /* Fire it off */
385         iov [0].iov_base = ((char *)hw);
386         iov [0].iov_len = hbufp;
387         iov [1].iov_base = ((char *)ip);
388         iov [1].iov_len = ibufp;
389         iov [2].iov_base = (char *)raw;
390         iov [2].iov_len = len;
391
392         result = writev(interface -> wfdesc, iov, 3);
393         if (result < 0)
394                 log_error ("send_packet: %m");
395         return result;
396 }
397 #endif /* USE_BPF_SEND */
398
399 #ifdef USE_BPF_RECEIVE
400 ssize_t receive_packet (interface, buf, len, from, hfrom)
401         struct interface_info *interface;
402         unsigned char *buf;
403         size_t len;
404         struct sockaddr_in *from;
405         struct hardware *hfrom;
406 {
407         int length = 0;
408         int offset = 0;
409         struct bpf_hdr hdr;
410
411         /* All this complexity is because BPF doesn't guarantee
412            that only one packet will be returned at a time.   We're
413            getting what we deserve, though - this is a terrible abuse
414            of the BPF interface.   Sigh. */
415
416         /* Process packets until we get one we can return or until we've
417            done a read and gotten nothing we can return... */
418
419         do {
420                 /* If the buffer is empty, fill it. */
421                 if (interface -> rbuf_offset == interface -> rbuf_len) {
422                         length = read (interface -> rfdesc,
423                                        interface -> rbuf,
424                                        (size_t)interface -> rbuf_max);
425                         if (length <= 0) {
426 #ifdef __FreeBSD__
427                                 if (errno == ENXIO) {
428 #else
429                                 if (errno == EIO) {
430 #endif
431                                         dhcp_interface_remove
432                                                 ((omapi_object_t *)interface,
433                                                  (omapi_object_t *)0);
434                                 }
435                                 return length;
436                         }
437                         interface -> rbuf_offset = 0;
438                         interface -> rbuf_len = BPF_WORDALIGN (length);
439                 }
440
441                 /* If there isn't room for a whole bpf header, something went
442                    wrong, but we'll ignore it and hope it goes away... XXX */
443                 if (interface -> rbuf_len -
444                     interface -> rbuf_offset < sizeof hdr) {
445                         interface -> rbuf_offset = interface -> rbuf_len;
446                         continue;
447                 }
448
449                 /* Copy out a bpf header... */
450                 memcpy (&hdr, &interface -> rbuf [interface -> rbuf_offset],
451                         sizeof hdr);
452
453                 /* If the bpf header plus data doesn't fit in what's left
454                    of the buffer, stick head in sand yet again... */
455                 if (interface -> rbuf_offset +
456                     hdr.bh_hdrlen + hdr.bh_caplen > interface -> rbuf_len) {
457                         interface -> rbuf_offset = interface -> rbuf_len;
458                         continue;
459                 }
460
461                 /* If the captured data wasn't the whole packet, or if
462                    the packet won't fit in the input buffer, all we
463                    can do is drop it. */
464                 if (hdr.bh_caplen != hdr.bh_datalen) {
465                         interface -> rbuf_offset =
466                                 BPF_WORDALIGN (interface -> rbuf_offset +
467                                                hdr.bh_hdrlen + hdr.bh_caplen);
468                         continue;
469                 }
470
471                 /* Skip over the BPF header... */
472                 interface -> rbuf_offset += hdr.bh_hdrlen;
473
474                 /* Decode the physical header... */
475                 offset = decode_hw_header (interface,
476                                            interface -> rbuf,
477                                            interface -> rbuf_offset,
478                                            hfrom);
479
480                 /* If a physical layer checksum failed (dunno of any
481                    physical layer that supports this, but WTH), skip this
482                    packet. */
483                 if (offset < 0) {
484                         interface -> rbuf_offset = 
485                                 BPF_WORDALIGN (interface -> rbuf_offset +
486                                                hdr.bh_caplen);
487                         continue;
488                 }
489                 interface -> rbuf_offset += offset;
490                 hdr.bh_caplen -= offset;
491
492                 /* Decode the IP and UDP headers... */
493                 offset = decode_udp_ip_header (interface,
494                                                interface -> rbuf,
495                                                interface -> rbuf_offset,
496                                                from,
497                                                (unsigned char *)0,
498                                                hdr.bh_caplen);
499
500                 /* If the IP or UDP checksum was bad, skip the packet... */
501                 if (offset < 0) {
502                         interface -> rbuf_offset = 
503                                 BPF_WORDALIGN (interface -> rbuf_offset +
504                                                hdr.bh_caplen);
505                         continue;
506                 }
507                 interface -> rbuf_offset = interface -> rbuf_offset + offset;
508                 hdr.bh_caplen -= offset;
509
510                 /* If there's not enough room to stash the packet data,
511                    we have to skip it (this shouldn't happen in real
512                    life, though). */
513                 if (hdr.bh_caplen > len) {
514                         interface -> rbuf_offset =
515                                 BPF_WORDALIGN (interface -> rbuf_offset +
516                                                hdr.bh_caplen);
517                         continue;
518                 }
519
520                 /* Copy out the data in the packet... */
521                 memcpy (buf, interface -> rbuf + interface -> rbuf_offset,
522                         hdr.bh_caplen);
523                 interface -> rbuf_offset =
524                         BPF_WORDALIGN (interface -> rbuf_offset +
525                                        hdr.bh_caplen);
526                 return hdr.bh_caplen;
527         } while (!length);
528         return 0;
529 }
530
531 int can_unicast_without_arp (ip)
532         struct interface_info *ip;
533 {
534         return 1;
535 }
536
537 int can_receive_unicast_unconfigured (ip)
538         struct interface_info *ip;
539 {
540         return 1;
541 }
542
543 int supports_multiple_interfaces (ip)
544         struct interface_info *ip;
545 {
546         return 1;
547 }
548
549 void maybe_setup_fallback ()
550 {
551         isc_result_t status;
552         struct interface_info *fbi = (struct interface_info *)0;
553         if (setup_fallback (&fbi, MDL)) {
554                 if_register_fallback (fbi);
555                 status = omapi_register_io_object ((omapi_object_t *)fbi,
556                                                    if_readsocket, 0,
557                                                    fallback_discard, 0, 0);
558                 if (status != ISC_R_SUCCESS)
559                         log_fatal ("Can't register I/O handle for %s: %s",
560                                    fbi -> name, isc_result_totext (status));
561                 interface_dereference (&fbi, MDL);
562         }
563 }
564 #endif