~ubuntu-branches/ubuntu/gutsy/dhcp/gutsy-security

« back to all changes in this revision

Viewing changes to common/bpf.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2004-10-30 21:07:22 UTC
  • Revision ID: james.westby@ubuntu.com-20041030210722-p0u4682vtncfxnnn
Tags: upstream-2.0pl5
ImportĀ upstreamĀ versionĀ 2.0pl5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* bpf.c
 
2
 
 
3
   BPF socket interface code, originally contributed by Archie Cobbs. */
 
4
 
 
5
/*
 
6
 * Copyright (c) 1995, 1996, 1998, 1999
 
7
 * The Internet Software Consortium.    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 has been written for the Internet Software Consortium
 
37
 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
 
38
 * Enterprises.  To learn more about the Internet Software Consortium,
 
39
 * see ``http://www.vix.com/isc''.  To learn more about Vixie
 
40
 * Enterprises, see ``http://www.vix.com''.
 
41
 */
 
42
 
 
43
#ifndef lint
 
44
static char copyright[] =
 
45
"$Id: bpf.c,v 1.19.2.10 1999/05/27 17:44:51 mellon Exp $ Copyright (c) 1995, 1996, 1998, 1999 The Internet Software Consortium.  All rights reserved.\n";
 
46
#endif /* not lint */
 
47
 
 
48
#include "dhcpd.h"
 
49
#if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
 
50
                                || defined (USE_LPF_RECEIVE)
 
51
# if defined (USE_LPF_RECEIVE)
 
52
#  include <asm/types.h>
 
53
#  include <linux/filter.h>
 
54
#  define bpf_insn sock_filter /* Linux: dare to be gratuitously different. */
 
55
# else
 
56
#  include <sys/ioctl.h>
 
57
#  include <sys/uio.h>
 
58
 
 
59
#  include <net/bpf.h>
 
60
#  if defined (NEED_OSF_PFILT_HACKS)
 
61
#   include <net/pfilt.h>
 
62
#  endif
 
63
# endif
 
64
 
 
65
#include <netinet/in_systm.h>
 
66
#include "includes/netinet/ip.h"
 
67
#include "includes/netinet/udp.h"
 
68
#include "includes/netinet/if_ether.h"
 
69
#endif
 
70
 
 
71
/* Reinitializes the specified interface after an address change.   This
 
72
   is not required for packet-filter APIs. */
 
73
 
 
74
#ifdef USE_BPF_SEND
 
75
void if_reinitialize_send (info)
 
76
        struct interface_info *info;
 
77
{
 
78
}
 
79
#endif
 
80
 
 
81
#ifdef USE_BPF_RECEIVE
 
82
void if_reinitialize_receive (info)
 
83
        struct interface_info *info;
 
84
{
 
85
}
 
86
#endif
 
87
 
 
88
/* Called by get_interface_list for each interface that's discovered.
 
89
   Opens a packet filter for each interface and adds it to the select
 
90
   mask. */
 
91
 
 
92
#if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
 
93
int if_register_bpf (info)
 
94
        struct interface_info *info;
 
95
{
 
96
        int sock;
 
97
        char filename[50];
 
98
        int b;
 
99
 
 
100
        /* Open a BPF device */
 
101
        for (b = 0; 1; b++) {
 
102
#ifndef NO_SNPRINTF
 
103
                snprintf(filename, sizeof(filename), BPF_FORMAT, b);
 
104
#else
 
105
                sprintf(filename, BPF_FORMAT, b);
 
106
#endif
 
107
                sock = open (filename, O_RDWR, 0);
 
108
                if (sock < 0) {
 
109
                        if (errno == EBUSY) {
 
110
                                continue;
 
111
                        } else {
 
112
                                if (!b)
 
113
                                        error ("No bpf devices.%s%s%s",
 
114
                                               "   Please read the README",
 
115
                                               " section for your operating",
 
116
                                               " system.");
 
117
                                error ("Can't find free bpf: %m");
 
118
                        }
 
119
                } else {
 
120
                        break;
 
121
                }
 
122
        }
 
123
 
 
124
        /* Set the BPF device to point at this interface. */
 
125
        if (ioctl (sock, BIOCSETIF, info -> ifp) < 0)
 
126
                error ("Can't attach interface %s to bpf device %s: %m",
 
127
                       info -> name, filename);
 
128
 
 
129
        return sock;
 
130
}
 
131
#endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
 
132
 
 
133
#ifdef USE_BPF_SEND
 
134
void if_register_send (info)
 
135
        struct interface_info *info;
 
136
{
 
137
        /* If we're using the bpf API for sending and receiving,
 
138
           we don't need to register this interface twice. */
 
139
#ifndef USE_BPF_RECEIVE
 
140
        info -> wfdesc = if_register_bpf (info, interface);
 
141
#else
 
142
        info -> wfdesc = info -> rfdesc;
 
143
#endif
 
144
        if (!quiet_interface_discovery)
 
145
                note ("Sending on   BPF/%s/%s%s%s",
 
146
                      info -> name,
 
147
                      print_hw_addr (info -> hw_address.htype,
 
148
                                     info -> hw_address.hlen,
 
149
                                     info -> hw_address.haddr),
 
150
                      (info -> shared_network ? "/" : ""),
 
151
                      (info -> shared_network ?
 
152
                       info -> shared_network -> name : ""));
 
153
}
 
154
#endif /* USE_BPF_SEND */
 
155
 
 
156
#if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
 
157
/* Packet filter program...
 
158
   XXX Changes to the filter program may require changes to the constant
 
159
   offsets used in if_register_send to patch the BPF program! XXX */
 
160
 
 
161
struct bpf_insn dhcp_bpf_filter [] = {
 
162
        /* Make sure this is an IP packet... */
 
163
        BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12),
 
164
        BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
 
165
 
 
166
        /* Make sure it's a UDP packet... */
 
167
        BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23),
 
168
        BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
 
169
 
 
170
        /* Make sure this isn't a fragment... */
 
171
        BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
 
172
        BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
 
173
 
 
174
        /* Get the IP header length... */
 
175
        BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14),
 
176
 
 
177
        /* Make sure it's to the right port... */
 
178
        BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16),
 
179
        BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1),             /* patch */
 
180
 
 
181
        /* If we passed all the tests, ask for the whole packet. */
 
182
        BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
 
183
 
 
184
        /* Otherwise, drop it. */
 
185
        BPF_STMT(BPF_RET+BPF_K, 0),
 
186
};
 
187
 
 
188
int dhcp_bpf_filter_len = sizeof dhcp_bpf_filter / sizeof (struct bpf_insn);
 
189
 
 
190
struct bpf_insn dhcp_bpf_tr_filter [] = {
 
191
        /* accept all token ring packets due to variable length header */
 
192
        /* if we want to get clever, insert the program here */
 
193
 
 
194
        /* If we passed all the tests, ask for the whole packet. */
 
195
        BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
 
196
 
 
197
        /* Otherwise, drop it. */
 
198
        BPF_STMT(BPF_RET+BPF_K, 0),
 
199
};
 
200
 
 
201
int dhcp_bpf_tr_filter_len = (sizeof dhcp_bpf_tr_filter /
 
202
                              sizeof (struct bpf_insn));
 
203
#endif
 
204
 
 
205
#if defined (USE_BPF_RECEIVE)
 
206
void if_register_receive (info)
 
207
        struct interface_info *info;
 
208
{
 
209
        int flag = 1;
 
210
        struct bpf_version v;
 
211
        u_int32_t addr;
 
212
        struct bpf_program p;
 
213
        u_int32_t bits;
 
214
 
 
215
        /* Open a BPF device and hang it on this interface... */
 
216
        info -> rfdesc = if_register_bpf (info);
 
217
 
 
218
        /* Make sure the BPF version is in range... */
 
219
        if (ioctl (info -> rfdesc, BIOCVERSION, &v) < 0)
 
220
                error ("Can't get BPF version: %m");
 
221
 
 
222
        if (v.bv_major != BPF_MAJOR_VERSION ||
 
223
            v.bv_minor < BPF_MINOR_VERSION)
 
224
                error ("Kernel BPF version out of range - recompile dhcpd!");
 
225
 
 
226
        /* Set immediate mode so that reads return as soon as a packet
 
227
           comes in, rather than waiting for the input buffer to fill with
 
228
           packets. */
 
229
        if (ioctl (info -> rfdesc, BIOCIMMEDIATE, &flag) < 0)
 
230
                error ("Can't set immediate mode on bpf device: %m");
 
231
 
 
232
#ifdef NEED_OSF_PFILT_HACKS
 
233
        /* Allow the copyall flag to be set... */
 
234
        if (ioctl(info -> rfdesc, EIOCALLOWCOPYALL, &flag) < 0)
 
235
                error ("Can't set ALLOWCOPYALL: %m");
 
236
 
 
237
        /* Clear all the packet filter mode bits first... */
 
238
        bits = 0;
 
239
        if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
 
240
                error ("Can't clear pfilt bits: %m");
 
241
 
 
242
        /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
 
243
        bits = ENBATCH | ENCOPYALL | ENBPFHDR;
 
244
        if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
 
245
                error ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
 
246
#endif
 
247
        /* Get the required BPF buffer length from the kernel. */
 
248
        if (ioctl (info -> rfdesc, BIOCGBLEN, &info -> rbuf_max) < 0)
 
249
                error ("Can't get bpf buffer length: %m");
 
250
        info -> rbuf = malloc (info -> rbuf_max);
 
251
        if (!info -> rbuf)
 
252
                error ("Can't allocate %d bytes for bpf input buffer.");
 
253
        info -> rbuf_offset = 0;
 
254
        info -> rbuf_len = 0;
 
255
 
 
256
        /* Set up the bpf filter program structure. */
 
257
        p.bf_len = dhcp_bpf_filter_len;
 
258
        p.bf_insns = dhcp_bpf_filter;
 
259
 
 
260
        /* Patch the server port into the BPF  program...
 
261
           XXX changes to filter program may require changes
 
262
           to the insn number(s) used below! XXX */
 
263
        dhcp_bpf_filter [8].k = ntohs (local_port);
 
264
 
 
265
        if (ioctl (info -> rfdesc, BIOCSETF, &p) < 0)
 
266
                error ("Can't install packet filter program: %m");
 
267
        if (!quiet_interface_discovery)
 
268
                note ("Listening on BPF/%s/%s%s%s",
 
269
                      info -> name,
 
270
                      print_hw_addr (info -> hw_address.htype,
 
271
                                     info -> hw_address.hlen,
 
272
                                     info -> hw_address.haddr),
 
273
                      (info -> shared_network ? "/" : ""),
 
274
                      (info -> shared_network ?
 
275
                       info -> shared_network -> name : ""));
 
276
}
 
277
#endif /* USE_BPF_RECEIVE */
 
278
 
 
279
#ifdef USE_BPF_SEND
 
280
ssize_t send_packet (interface, packet, raw, len, from, to, hto)
 
281
        struct interface_info *interface;
 
282
        struct packet *packet;
 
283
        struct dhcp_packet *raw;
 
284
        size_t len;
 
285
        struct in_addr from;
 
286
        struct sockaddr_in *to;
 
287
        struct hardware *hto;
 
288
{
 
289
        int bufp = 0;
 
290
        unsigned char buf [256];
 
291
        struct iovec iov [2];
 
292
        int result;
 
293
 
 
294
        if (!strcmp (interface -> name, "fallback"))
 
295
                return send_fallback (interface, packet, raw,
 
296
                                      len, from, to, hto);
 
297
 
 
298
        /* Assemble the headers... */
 
299
        assemble_hw_header (interface, buf, &bufp, hto);
 
300
        assemble_udp_ip_header (interface, buf, &bufp, from.s_addr,
 
301
                                to -> sin_addr.s_addr, to -> sin_port,
 
302
                                (unsigned char *)raw, len);
 
303
 
 
304
        /* Fire it off */
 
305
        iov [0].iov_base = (char *)buf;
 
306
        iov [0].iov_len = bufp;
 
307
        iov [1].iov_base = (char *)raw;
 
308
        iov [1].iov_len = len;
 
309
 
 
310
        result = writev(interface -> wfdesc, iov, 2);
 
311
        if (result < 0)
 
312
                warn ("send_packet: %m");
 
313
        return result;
 
314
}
 
315
#endif /* USE_BPF_SEND */
 
316
 
 
317
#ifdef USE_BPF_RECEIVE
 
318
ssize_t receive_packet (interface, buf, len, from, hfrom)
 
319
        struct interface_info *interface;
 
320
        unsigned char *buf;
 
321
        size_t len;
 
322
        struct sockaddr_in *from;
 
323
        struct hardware *hfrom;
 
324
{
 
325
        int length = 0;
 
326
        int offset = 0;
 
327
        struct bpf_hdr hdr;
 
328
 
 
329
        /* All this complexity is because BPF doesn't guarantee
 
330
           that only one packet will be returned at a time.   We're
 
331
           getting what we deserve, though - this is a terrible abuse
 
332
           of the BPF interface.   Sigh. */
 
333
 
 
334
        /* Process packets until we get one we can return or until we've
 
335
           done a read and gotten nothing we can return... */
 
336
 
 
337
        do {
 
338
                /* If the buffer is empty, fill it. */
 
339
                if (interface -> rbuf_offset == interface -> rbuf_len) {
 
340
                        length = read (interface -> rfdesc,
 
341
                                       interface -> rbuf,
 
342
                                       interface -> rbuf_max);
 
343
                        if (length <= 0)
 
344
                                return length;
 
345
                        interface -> rbuf_offset = 0;
 
346
                        interface -> rbuf_len = length;
 
347
                }
 
348
 
 
349
                /* If there isn't room for a whole bpf header, something went
 
350
                   wrong, but we'll ignore it and hope it goes away... XXX */
 
351
                if (interface -> rbuf_len -
 
352
                    interface -> rbuf_offset < sizeof hdr) {
 
353
                        interface -> rbuf_offset = interface -> rbuf_len;
 
354
                        continue;
 
355
                }
 
356
 
 
357
                /* Copy out a bpf header... */
 
358
                memcpy (&hdr, &interface -> rbuf [interface -> rbuf_offset],
 
359
                        sizeof hdr);
 
360
 
 
361
                /* If the bpf header plus data doesn't fit in what's left
 
362
                   of the buffer, stick head in sand yet again... */
 
363
                if (interface -> rbuf_offset +
 
364
                    hdr.bh_hdrlen + hdr.bh_caplen > interface -> rbuf_len) {
 
365
                        interface -> rbuf_offset = interface -> rbuf_len;
 
366
                        continue;
 
367
                }
 
368
 
 
369
                /* If the captured data wasn't the whole packet, or if
 
370
                   the packet won't fit in the input buffer, all we
 
371
                   can do is drop it. */
 
372
                if (hdr.bh_caplen != hdr.bh_datalen) {
 
373
                        interface -> rbuf_offset +=
 
374
                                hdr.bh_hdrlen = hdr.bh_caplen;
 
375
                        continue;
 
376
                }
 
377
 
 
378
                /* Skip over the BPF header... */
 
379
                interface -> rbuf_offset += hdr.bh_hdrlen;
 
380
 
 
381
                /* Decode the physical header... */
 
382
                offset = decode_hw_header (interface,
 
383
                                           interface -> rbuf,
 
384
                                           interface -> rbuf_offset,
 
385
                                           hfrom);
 
386
 
 
387
                /* If a physical layer checksum failed (dunno of any
 
388
                   physical layer that supports this, but WTH), skip this
 
389
                   packet. */
 
390
                if (offset < 0) {
 
391
                        interface -> rbuf_offset += hdr.bh_caplen;
 
392
                        continue;
 
393
                }
 
394
                interface -> rbuf_offset += offset;
 
395
                hdr.bh_caplen -= offset;
 
396
 
 
397
                /* Decode the IP and UDP headers... */
 
398
                offset = decode_udp_ip_header (interface,
 
399
                                               interface -> rbuf,
 
400
                                               interface -> rbuf_offset,
 
401
                                               from,
 
402
                                               (unsigned char *)0,
 
403
                                               hdr.bh_caplen);
 
404
 
 
405
                /* If the IP or UDP checksum was bad, skip the packet... */
 
406
                if (offset < 0) {
 
407
                        interface -> rbuf_offset += hdr.bh_caplen;
 
408
                        continue;
 
409
                }
 
410
                interface -> rbuf_offset += offset;
 
411
                hdr.bh_caplen -= offset;
 
412
 
 
413
                /* If there's not enough room to stash the packet data,
 
414
                   we have to skip it (this shouldn't happen in real
 
415
                   life, though). */
 
416
                if (hdr.bh_caplen > len) {
 
417
                        interface -> rbuf_offset += hdr.bh_caplen;
 
418
                        continue;
 
419
                }
 
420
 
 
421
                /* Copy out the data in the packet... */
 
422
                memcpy (buf, interface -> rbuf + interface -> rbuf_offset,
 
423
                        hdr.bh_caplen);
 
424
                interface -> rbuf_offset += hdr.bh_caplen;
 
425
                return hdr.bh_caplen;
 
426
        } while (!length);
 
427
        return 0;
 
428
}
 
429
 
 
430
int can_unicast_without_arp ()
 
431
{
 
432
        return 1;
 
433
}
 
434
 
 
435
int can_receive_unicast_unconfigured (ip)
 
436
        struct interface_info *ip;
 
437
{
 
438
        return 1;
 
439
}
 
440
 
 
441
void maybe_setup_fallback ()
 
442
{
 
443
        struct interface_info *fbi;
 
444
        fbi = setup_fallback ();
 
445
        if (fbi) {
 
446
                if_register_fallback (fbi);
 
447
                add_protocol ("fallback", fallback_interface -> wfdesc,
 
448
                              fallback_discard, fallback_interface);
 
449
        }
 
450
}
 
451
#endif