]> git.ipfire.org Git - thirdparty/dhcp.git/blob - common/bpf.c
fd307fc28389b9378ea540e57d2ab5ae047f5694
[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-1999 Internet Software Consortium.
7 * Use is subject to license terms which appear in the file named
8 * ISC-LICENSE that should have accompanied this file when you
9 * received it. If a file named ISC-LICENSE did not accompany this
10 * file, or you are not sure the one you have is correct, you may
11 * obtain an applicable copy of the license at:
12 *
13 * http://www.isc.org/isc-license-1.0.html.
14 *
15 * This file is part of the ISC DHCP distribution. The documentation
16 * associated with this file is listed in the file DOCUMENTATION,
17 * included in the top-level directory of this release.
18 *
19 * Support and other services are available for ISC products - see
20 * http://www.isc.org for more information.
21 */
22
23 #ifndef lint
24 static char copyright[] =
25 "$Id: bpf.c,v 1.34 2000/03/06 19:39:53 mellon Exp $ Copyright (c) 1995, 1996 The Internet Software Consortium. All rights reserved.\n";
26 #endif /* not lint */
27
28 #include "dhcpd.h"
29 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
30 || defined (USE_LPF_RECEIVE)
31 # if defined (USE_LPF_RECEIVE)
32 # include <asm/types.h>
33 # include <linux/filter.h>
34 # define bpf_insn sock_filter /* Linux: dare to be gratuitously different. */
35 # else
36 # include <sys/ioctl.h>
37 # include <sys/uio.h>
38 # include <net/bpf.h>
39 # if defined (NEED_OSF_PFILT_HACKS)
40 # include <net/pfilt.h>
41 # endif
42 # endif
43
44 #include <netinet/in_systm.h>
45 #include "includes/netinet/ip.h"
46 #include "includes/netinet/udp.h"
47 #include "includes/netinet/if_ether.h"
48 #endif
49
50 /* Reinitializes the specified interface after an address change. This
51 is not required for packet-filter APIs. */
52
53 #ifdef USE_BPF_SEND
54 void if_reinitialize_send (info)
55 struct interface_info *info;
56 {
57 }
58 #endif
59
60 #ifdef USE_BPF_RECEIVE
61 void if_reinitialize_receive (info)
62 struct interface_info *info;
63 {
64 }
65 #endif
66
67 /* Called by get_interface_list for each interface that's discovered.
68 Opens a packet filter for each interface and adds it to the select
69 mask. */
70
71 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
72 int if_register_bpf (info)
73 struct interface_info *info;
74 {
75 int sock;
76 char filename[50];
77 int b;
78
79 /* Open a BPF device */
80 for (b = 0; 1; b++) {
81 #ifndef NO_SNPRINTF
82 snprintf(filename, sizeof(filename), BPF_FORMAT, b);
83 #else
84 sprintf(filename, BPF_FORMAT, b);
85 #endif
86 sock = open (filename, O_RDWR, 0);
87 if (sock < 0) {
88 if (errno == EBUSY) {
89 continue;
90 } else {
91 if (!b)
92 log_fatal ("No bpf devices.%s%s%s",
93 " Please read the README",
94 " section for your operating",
95 " system.");
96 log_fatal ("Can't find free bpf: %m");
97 }
98 } else {
99 break;
100 }
101 }
102
103 /* Set the BPF device to point at this interface. */
104 if (ioctl (sock, BIOCSETIF, info -> ifp) < 0)
105 log_fatal ("Can't attach interface %s to bpf device %s: %m",
106 info -> name, filename);
107
108 return sock;
109 }
110 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
111
112 #ifdef USE_BPF_SEND
113 void if_register_send (info)
114 struct interface_info *info;
115 {
116 /* If we're using the bpf API for sending and receiving,
117 we don't need to register this interface twice. */
118 #ifndef USE_BPF_RECEIVE
119 info -> wfdesc = if_register_bpf (info, interface);
120 #else
121 info -> wfdesc = info -> rfdesc;
122 #endif
123 if (!quiet_interface_discovery)
124 log_info ("Sending on BPF/%s/%s%s%s",
125 info -> name,
126 print_hw_addr (info -> hw_address.hbuf [0],
127 info -> hw_address.hlen - 1,
128 &info -> hw_address.hbuf [1]),
129 (info -> shared_network ? "/" : ""),
130 (info -> shared_network ?
131 info -> shared_network -> name : ""));
132 }
133
134 void if_deregister_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 close (info -> wfdesc);
141 #endif
142 info -> wfdesc = -1;
143
144 if (!quiet_interface_discovery)
145 log_info ("Disabling output on BPF/%s/%s%s%s",
146 info -> name,
147 print_hw_addr (info -> hw_address.hbuf [0],
148 info -> hw_address.hlen - 1,
149 &info -> hw_address.hbuf [1]),
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 struct bpf_insn dhcp_bpf_tr_filter [] = {
190 /* accept all token ring packets due to variable length header */
191 /* if we want to get clever, insert the program here */
192
193 /* If we passed all the tests, ask for the whole packet. */
194 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
195
196 /* Otherwise, drop it. */
197 BPF_STMT(BPF_RET+BPF_K, 0),
198 };
199
200 int dhcp_bpf_tr_filter_len = (sizeof dhcp_bpf_tr_filter /
201 sizeof (struct bpf_insn));
202 #endif
203
204 #if defined (USE_BPF_RECEIVE)
205 void if_register_receive (info)
206 struct interface_info *info;
207 {
208 int flag = 1;
209 struct bpf_version v;
210 u_int32_t addr;
211 struct bpf_program p;
212 u_int32_t bits;
213
214 /* Open a BPF device and hang it on this interface... */
215 info -> rfdesc = if_register_bpf (info);
216
217 /* Make sure the BPF version is in range... */
218 if (ioctl (info -> rfdesc, BIOCVERSION, &v) < 0)
219 log_fatal ("Can't get BPF version: %m");
220
221 if (v.bv_major != BPF_MAJOR_VERSION ||
222 v.bv_minor < BPF_MINOR_VERSION)
223 log_fatal ("Kernel BPF version out of range - recompile dhcpd!");
224
225 /* Set immediate mode so that reads return as soon as a packet
226 comes in, rather than waiting for the input buffer to fill with
227 packets. */
228 if (ioctl (info -> rfdesc, BIOCIMMEDIATE, &flag) < 0)
229 log_fatal ("Can't set immediate mode on bpf device: %m");
230
231 #ifdef NEED_OSF_PFILT_HACKS
232 /* Allow the copyall flag to be set... */
233 if (ioctl(info -> rfdesc, EIOCALLOWCOPYALL, &flag) < 0)
234 log_fatal ("Can't set ALLOWCOPYALL: %m");
235
236 /* Clear all the packet filter mode bits first... */
237 bits = 0;
238 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
239 log_fatal ("Can't clear pfilt bits: %m");
240
241 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
242 bits = ENBATCH | ENCOPYALL | ENBPFHDR;
243 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
244 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
245 #endif
246 /* Get the required BPF buffer length from the kernel. */
247 if (ioctl (info -> rfdesc, BIOCGBLEN, &info -> rbuf_max) < 0)
248 log_fatal ("Can't get bpf buffer length: %m");
249 info -> rbuf = dmalloc (info -> rbuf_max, MDL);
250 if (!info -> rbuf)
251 log_fatal ("Can't allocate %d bytes for bpf input buffer.",
252 info -> rbuf_max);
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 log_fatal ("Can't install packet filter program: %m");
267 if (!quiet_interface_discovery)
268 log_info ("Listening on BPF/%s/%s%s%s",
269 info -> name,
270 print_hw_addr (info -> hw_address.hbuf [0],
271 info -> hw_address.hlen - 1,
272 &info -> hw_address.hbuf [1]),
273 (info -> shared_network ? "/" : ""),
274 (info -> shared_network ?
275 info -> shared_network -> name : ""));
276 }
277
278 void if_deregister_receive (info)
279 struct interface_info *info;
280 {
281 close (info -> rfdesc);
282 info -> rfdesc = -1;
283
284 if (!quiet_interface_discovery)
285 log_info ("Disabling input on BPF/%s/%s%s%s",
286 info -> name,
287 print_hw_addr (info -> hw_address.hbuf [0],
288 info -> hw_address.hlen - 1,
289 &info -> hw_address.hbuf [1]),
290 (info -> shared_network ? "/" : ""),
291 (info -> shared_network ?
292 info -> shared_network -> name : ""));
293 }
294 #endif /* USE_BPF_RECEIVE */
295
296 #ifdef USE_BPF_SEND
297 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
298 struct interface_info *interface;
299 struct packet *packet;
300 struct dhcp_packet *raw;
301 size_t len;
302 struct in_addr from;
303 struct sockaddr_in *to;
304 struct hardware *hto;
305 {
306 unsigned bufp = 0;
307 unsigned char buf [256];
308 struct iovec iov [2];
309 int result;
310
311 if (!strcmp (interface -> name, "fallback"))
312 return send_fallback (interface, packet, raw,
313 len, from, to, hto);
314
315 /* Assemble the headers... */
316 assemble_hw_header (interface, buf, &bufp, hto);
317 assemble_udp_ip_header (interface, buf, &bufp, from.s_addr,
318 to -> sin_addr.s_addr, to -> sin_port,
319 (unsigned char *)raw, len);
320
321 /* Fire it off */
322 iov [0].iov_base = (char *)buf;
323 iov [0].iov_len = bufp;
324 iov [1].iov_base = (char *)raw;
325 iov [1].iov_len = len;
326
327 result = writev(interface -> wfdesc, iov, 2);
328 if (result < 0)
329 log_error ("send_packet: %m");
330 return result;
331 }
332 #endif /* USE_BPF_SEND */
333
334 #ifdef USE_BPF_RECEIVE
335 ssize_t receive_packet (interface, buf, len, from, hfrom)
336 struct interface_info *interface;
337 unsigned char *buf;
338 size_t len;
339 struct sockaddr_in *from;
340 struct hardware *hfrom;
341 {
342 int length = 0;
343 int offset = 0;
344 struct bpf_hdr hdr;
345
346 /* All this complexity is because BPF doesn't guarantee
347 that only one packet will be returned at a time. We're
348 getting what we deserve, though - this is a terrible abuse
349 of the BPF interface. Sigh. */
350
351 /* Process packets until we get one we can return or until we've
352 done a read and gotten nothing we can return... */
353
354 do {
355 /* If the buffer is empty, fill it. */
356 if (interface -> rbuf_offset == interface -> rbuf_len) {
357 length = read (interface -> rfdesc,
358 interface -> rbuf,
359 interface -> rbuf_max);
360 if (length <= 0)
361 return length;
362 interface -> rbuf_offset = 0;
363 interface -> rbuf_len = length;
364 }
365
366 /* If there isn't room for a whole bpf header, something went
367 wrong, but we'll ignore it and hope it goes away... XXX */
368 if (interface -> rbuf_len -
369 interface -> rbuf_offset < sizeof hdr) {
370 interface -> rbuf_offset = interface -> rbuf_len;
371 continue;
372 }
373
374 /* Copy out a bpf header... */
375 memcpy (&hdr, &interface -> rbuf [interface -> rbuf_offset],
376 sizeof hdr);
377
378 /* If the bpf header plus data doesn't fit in what's left
379 of the buffer, stick head in sand yet again... */
380 if (interface -> rbuf_offset +
381 hdr.bh_hdrlen + hdr.bh_caplen > interface -> rbuf_len) {
382 interface -> rbuf_offset = interface -> rbuf_len;
383 continue;
384 }
385
386 /* If the captured data wasn't the whole packet, or if
387 the packet won't fit in the input buffer, all we
388 can do is drop it. */
389 if (hdr.bh_caplen != hdr.bh_datalen) {
390 interface -> rbuf_offset +=
391 hdr.bh_hdrlen = hdr.bh_caplen;
392 continue;
393 }
394
395 /* Skip over the BPF header... */
396 interface -> rbuf_offset += hdr.bh_hdrlen;
397
398 /* Decode the physical header... */
399 offset = decode_hw_header (interface,
400 interface -> rbuf,
401 interface -> rbuf_offset,
402 hfrom);
403
404 /* If a physical layer checksum failed (dunno of any
405 physical layer that supports this, but WTH), skip this
406 packet. */
407 if (offset < 0) {
408 interface -> rbuf_offset += hdr.bh_caplen;
409 continue;
410 }
411 interface -> rbuf_offset += offset;
412 hdr.bh_caplen -= offset;
413
414 /* Decode the IP and UDP headers... */
415 offset = decode_udp_ip_header (interface,
416 interface -> rbuf,
417 interface -> rbuf_offset,
418 from,
419 (unsigned char *)0,
420 hdr.bh_caplen);
421
422 /* If the IP or UDP checksum was bad, skip the packet... */
423 if (offset < 0) {
424 interface -> rbuf_offset += hdr.bh_caplen;
425 continue;
426 }
427 interface -> rbuf_offset += offset;
428 hdr.bh_caplen -= offset;
429
430 /* If there's not enough room to stash the packet data,
431 we have to skip it (this shouldn't happen in real
432 life, though). */
433 if (hdr.bh_caplen > len) {
434 interface -> rbuf_offset += hdr.bh_caplen;
435 continue;
436 }
437
438 /* Copy out the data in the packet... */
439 memcpy (buf, interface -> rbuf + interface -> rbuf_offset,
440 hdr.bh_caplen);
441 interface -> rbuf_offset += hdr.bh_caplen;
442 return hdr.bh_caplen;
443 } while (!length);
444 return 0;
445 }
446
447 int can_unicast_without_arp (ip)
448 struct interface_info *ip;
449 {
450 return 1;
451 }
452
453 int can_receive_unicast_unconfigured (ip)
454 struct interface_info *ip;
455 {
456 return 1;
457 }
458
459 void maybe_setup_fallback ()
460 {
461 isc_result_t status;
462 struct interface_info *fbi;
463 fbi = setup_fallback ();
464 if (fbi) {
465 if_register_fallback (fbi);
466 fbi -> refcnt = 1;
467 fbi -> type = dhcp_type_interface;
468 status = omapi_register_io_object ((omapi_object_t *)fbi,
469 if_readsocket, 0,
470 fallback_discard, 0, 0);
471 if (status != ISC_R_SUCCESS)
472 log_fatal ("Can't register I/O handle for %s: %s",
473 fbi -> name, isc_result_totext (status));
474 }
475 }
476 #endif