]> git.ipfire.org Git - thirdparty/dhcp.git/blob - common/bpf.c
[master] Correct size for buffer allocation
[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) 2009,2012-2014,2016 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 1996-2003 by Internet Software Consortium
9 *
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 *
22 * Internet Systems Consortium, Inc.
23 * 950 Charter Street
24 * Redwood City, CA 94063
25 * <info@isc.org>
26 * https://www.isc.org/
27 *
28 * This software was contributed to Internet Systems Consortium
29 * by Archie Cobbs.
30 *
31 * Patches for FDDI support on Digital Unix were written by Bill
32 * Stapleton, and maintained for a while by Mike Meredith before he
33 * managed to get me to integrate them.
34 */
35
36 #include "dhcpd.h"
37 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
38 || defined (USE_LPF_RECEIVE)
39 # if defined (USE_LPF_RECEIVE)
40 # include <asm/types.h>
41 # include <linux/filter.h>
42 # define bpf_insn sock_filter /* Linux: dare to be gratuitously different. */
43 # else
44 # include <sys/ioctl.h>
45 # include <sys/uio.h>
46 # include <net/bpf.h>
47 # if defined (NEED_OSF_PFILT_HACKS)
48 # include <net/pfilt.h>
49 # endif
50 # endif
51
52 #include <netinet/in_systm.h>
53 #include "includes/netinet/ip.h"
54 #include "includes/netinet/udp.h"
55 #include "includes/netinet/if_ether.h"
56 #endif
57
58 #if defined(USE_BPF_SEND) || defined(USE_BPF_RECEIVE) || defined(USE_BPF_HWADDR)
59 #include <net/if_types.h>
60 #include <ifaddrs.h>
61 #endif
62
63 #include <errno.h>
64
65 /* Reinitializes the specified interface after an address change. This
66 is not required for packet-filter APIs. */
67
68 #ifdef USE_BPF_SEND
69 void if_reinitialize_send (info)
70 struct interface_info *info;
71 {
72 }
73 #endif
74
75 #ifdef USE_BPF_RECEIVE
76 void if_reinitialize_receive (info)
77 struct interface_info *info;
78 {
79 }
80 #endif
81
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. */
85
86 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
87 int if_register_bpf (info)
88 struct interface_info *info;
89 {
90 int sock;
91 char filename[50];
92 int b;
93
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");
109 }
110 } else {
111 break;
112 }
113 }
114
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);
119
120 get_hw_addr(info->name, &info->hw_address);
121
122 return sock;
123 }
124 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
125
126 #ifdef USE_BPF_SEND
127 void if_register_send (info)
128 struct interface_info *info;
129 {
130 /* If we're using the bpf API for sending and receiving,
131 we don't need to register this interface twice. */
132 #ifndef USE_BPF_RECEIVE
133 info -> wfdesc = if_register_bpf (info, interface);
134 #else
135 info -> wfdesc = info -> rfdesc;
136 #endif
137 if (!quiet_interface_discovery)
138 log_info ("Sending on BPF/%s/%s%s%s",
139 info -> name,
140 print_hw_addr (info -> hw_address.hbuf [0],
141 info -> hw_address.hlen - 1,
142 &info -> hw_address.hbuf [1]),
143 (info -> shared_network ? "/" : ""),
144 (info -> shared_network ?
145 info -> shared_network -> name : ""));
146 }
147
148 void if_deregister_send (info)
149 struct interface_info *info;
150 {
151 /* If we're using the bpf API for sending and receiving,
152 we don't need to register this interface twice. */
153 #ifndef USE_BPF_RECEIVE
154 close (info -> wfdesc);
155 #endif
156 info -> wfdesc = -1;
157
158 if (!quiet_interface_discovery)
159 log_info ("Disabling output on BPF/%s/%s%s%s",
160 info -> name,
161 print_hw_addr (info -> hw_address.hbuf [0],
162 info -> hw_address.hlen - 1,
163 &info -> hw_address.hbuf [1]),
164 (info -> shared_network ? "/" : ""),
165 (info -> shared_network ?
166 info -> shared_network -> name : ""));
167 }
168 #endif /* USE_BPF_SEND */
169
170 #if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
171 /* Packet filter program...
172 XXX Changes to the filter program may require changes to the constant
173 offsets used in if_register_send to patch the BPF program! XXX */
174
175 struct bpf_insn dhcp_bpf_filter [] = {
176 /* Make sure this is an IP packet... */
177 BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12),
178 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
179
180 /* Make sure it's a UDP packet... */
181 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23),
182 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
183
184 /* Make sure this isn't a fragment... */
185 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
186 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
187
188 /* Get the IP header length... */
189 BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14),
190
191 /* Make sure it's to the right port... */
192 BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16),
193 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */
194
195 /* If we passed all the tests, ask for the whole packet. */
196 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
197
198 /* Otherwise, drop it. */
199 BPF_STMT(BPF_RET+BPF_K, 0),
200 };
201
202 #if defined (DEC_FDDI)
203 struct bpf_insn *bpf_fddi_filter = NULL;
204 #endif
205
206 int dhcp_bpf_filter_len = sizeof dhcp_bpf_filter / sizeof (struct bpf_insn);
207 #if defined (HAVE_TR_SUPPORT)
208 struct bpf_insn dhcp_bpf_tr_filter [] = {
209 /* accept all token ring packets due to variable length header */
210 /* if we want to get clever, insert the program here */
211
212 /* If we passed all the tests, ask for the whole packet. */
213 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
214
215 /* Otherwise, drop it. */
216 BPF_STMT(BPF_RET+BPF_K, 0),
217 };
218
219 int dhcp_bpf_tr_filter_len = (sizeof dhcp_bpf_tr_filter /
220 sizeof (struct bpf_insn));
221 #endif /* HAVE_TR_SUPPORT */
222 #endif /* USE_LPF_RECEIVE || USE_BPF_RECEIVE */
223
224 #if defined (USE_BPF_RECEIVE)
225 void if_register_receive (info)
226 struct interface_info *info;
227 {
228 int flag = 1;
229 struct bpf_version v;
230 struct bpf_program p;
231 #ifdef NEED_OSF_PFILT_HACKS
232 u_int32_t bits;
233 #endif
234 #ifdef DEC_FDDI
235 int link_layer;
236 #endif /* DEC_FDDI */
237
238 /* Open a BPF device and hang it on this interface... */
239 info -> rfdesc = if_register_bpf (info);
240
241 /* Make sure the BPF version is in range... */
242 if (ioctl (info -> rfdesc, BIOCVERSION, &v) < 0)
243 log_fatal ("Can't get BPF version: %m");
244
245 if (v.bv_major != BPF_MAJOR_VERSION ||
246 v.bv_minor < BPF_MINOR_VERSION)
247 log_fatal ("BPF version mismatch - recompile DHCP!");
248
249 /* Set immediate mode so that reads return as soon as a packet
250 comes in, rather than waiting for the input buffer to fill with
251 packets. */
252 if (ioctl (info -> rfdesc, BIOCIMMEDIATE, &flag) < 0)
253 log_fatal ("Can't set immediate mode on bpf device: %m");
254
255 #ifdef NEED_OSF_PFILT_HACKS
256 /* Allow the copyall flag to be set... */
257 if (ioctl(info -> rfdesc, EIOCALLOWCOPYALL, &flag) < 0)
258 log_fatal ("Can't set ALLOWCOPYALL: %m");
259
260 /* Clear all the packet filter mode bits first... */
261 bits = 0;
262 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
263 log_fatal ("Can't clear pfilt bits: %m");
264
265 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
266 bits = ENBATCH | ENCOPYALL | ENBPFHDR;
267 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
268 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
269 #endif
270 /* Get the required BPF buffer length from the kernel. */
271 if (ioctl (info -> rfdesc, BIOCGBLEN, &info -> rbuf_max) < 0)
272 log_fatal ("Can't get bpf buffer length: %m");
273 info -> rbuf = dmalloc (info -> rbuf_max, MDL);
274 if (!info -> rbuf)
275 log_fatal ("Can't allocate %ld bytes for bpf input buffer.",
276 (long)(info -> rbuf_max));
277 info -> rbuf_offset = 0;
278 info -> rbuf_len = 0;
279
280 /* Set up the bpf filter program structure. */
281 p.bf_len = dhcp_bpf_filter_len;
282
283 #ifdef DEC_FDDI
284 /* See if this is an FDDI interface, flag it for later. */
285 if (ioctl(info -> rfdesc, BIOCGDLT, &link_layer) >= 0 &&
286 link_layer == DLT_FDDI) {
287 if (!bpf_fddi_filter) {
288 bpf_fddi_filter = dmalloc (sizeof dhcp_bpf_filter,
289 MDL);
290 if (!bpf_fddi_filter)
291 log_fatal ("No memory for FDDI filter.");
292 memcpy (bpf_fddi_filter,
293 dhcp_bpf_filter, sizeof dhcp_bpf_filter);
294 /* Patch the BPF program to account for the difference
295 in length between ethernet headers (14), FDDI and
296 802.2 headers (16 +8=24, +10).
297 XXX changes to filter program may require changes to
298 XXX the insn number(s) used below! */
299 bpf_fddi_filter[0].k += 10;
300 bpf_fddi_filter[2].k += 10;
301 bpf_fddi_filter[4].k += 10;
302 bpf_fddi_filter[6].k += 10;
303 bpf_fddi_filter[7].k += 10;
304 }
305 p.bf_insns = bpf_fddi_filter;
306 } else
307 #endif /* DEC_FDDI */
308 p.bf_insns = dhcp_bpf_filter;
309
310 /* Patch the server port into the BPF program...
311 XXX changes to filter program may require changes
312 to the insn number(s) used below! XXX */
313 dhcp_bpf_filter [8].k = ntohs (local_port);
314
315 if (ioctl (info -> rfdesc, BIOCSETF, &p) < 0)
316 log_fatal ("Can't install packet filter program: %m");
317 if (!quiet_interface_discovery)
318 log_info ("Listening on BPF/%s/%s%s%s",
319 info -> name,
320 print_hw_addr (info -> hw_address.hbuf [0],
321 info -> hw_address.hlen - 1,
322 &info -> hw_address.hbuf [1]),
323 (info -> shared_network ? "/" : ""),
324 (info -> shared_network ?
325 info -> shared_network -> name : ""));
326 }
327
328 void if_deregister_receive (info)
329 struct interface_info *info;
330 {
331 close (info -> rfdesc);
332 info -> rfdesc = -1;
333
334 if (!quiet_interface_discovery)
335 log_info ("Disabling input on BPF/%s/%s%s%s",
336 info -> name,
337 print_hw_addr (info -> hw_address.hbuf [0],
338 info -> hw_address.hlen - 1,
339 &info -> hw_address.hbuf [1]),
340 (info -> shared_network ? "/" : ""),
341 (info -> shared_network ?
342 info -> shared_network -> name : ""));
343 }
344 #endif /* USE_BPF_RECEIVE */
345
346 #ifdef USE_BPF_SEND
347 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
348 struct interface_info *interface;
349 struct packet *packet;
350 struct dhcp_packet *raw;
351 size_t len;
352 struct in_addr from;
353 struct sockaddr_in *to;
354 struct hardware *hto;
355 {
356 unsigned hbufp = 0, ibufp = 0;
357 double hw [4];
358 double ip [32];
359 struct iovec iov [3];
360 int result;
361
362 if (!strcmp (interface -> name, "fallback"))
363 return send_fallback (interface, packet, raw,
364 len, from, to, hto);
365
366 if (hto == NULL && interface->anycast_mac_addr.hlen)
367 hto = &interface->anycast_mac_addr;
368
369 /* Assemble the headers... */
370 assemble_hw_header (interface, (unsigned char *)hw, &hbufp, hto);
371 assemble_udp_ip_header (interface,
372 (unsigned char *)ip, &ibufp, from.s_addr,
373 to -> sin_addr.s_addr, to -> sin_port,
374 (unsigned char *)raw, len);
375
376 /* Fire it off */
377 iov [0].iov_base = ((char *)hw);
378 iov [0].iov_len = hbufp;
379 iov [1].iov_base = ((char *)ip);
380 iov [1].iov_len = ibufp;
381 iov [2].iov_base = (char *)raw;
382 iov [2].iov_len = len;
383
384 result = writev(interface -> wfdesc, iov, 3);
385 if (result < 0)
386 log_error ("send_packet: %m");
387 return result;
388 }
389 #endif /* USE_BPF_SEND */
390
391 #ifdef USE_BPF_RECEIVE
392 ssize_t receive_packet (interface, buf, len, from, hfrom)
393 struct interface_info *interface;
394 unsigned char *buf;
395 size_t len;
396 struct sockaddr_in *from;
397 struct hardware *hfrom;
398 {
399 int length = 0;
400 int offset = 0;
401 struct bpf_hdr hdr;
402 unsigned paylen;
403
404 /* All this complexity is because BPF doesn't guarantee
405 that only one packet will be returned at a time. We're
406 getting what we deserve, though - this is a terrible abuse
407 of the BPF interface. Sigh. */
408
409 /* Process packets until we get one we can return or until we've
410 done a read and gotten nothing we can return... */
411
412 /* If the buffer is empty, fill it. */
413 if (interface->rbuf_offset >= interface->rbuf_len) {
414 length = read(interface->rfdesc, interface->rbuf,
415 (size_t)interface->rbuf_max);
416 if (length <= 0) {
417 #ifdef __FreeBSD__
418 if (errno == ENXIO) {
419 #else
420 if (errno == EIO) {
421 #endif
422 dhcp_interface_remove
423 ((omapi_object_t *)interface, NULL);
424 }
425 return (length);
426 }
427 interface->rbuf_offset = 0;
428 interface->rbuf_len = BPF_WORDALIGN(length);
429 }
430
431 do {
432 /* If there isn't room for a whole bpf header, something went
433 wrong, but we'll ignore it and hope it goes away... XXX */
434 if (interface->rbuf_len -
435 interface->rbuf_offset < sizeof hdr) {
436 interface->rbuf_offset = interface->rbuf_len;
437 continue;
438 }
439
440 /* Copy out a bpf header... */
441 memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
442 sizeof hdr);
443
444 /* If the bpf header plus data doesn't fit in what's left
445 of the buffer, stick head in sand yet again... */
446 if (interface->rbuf_offset +
447 hdr.bh_hdrlen + hdr.bh_caplen > interface->rbuf_len) {
448 interface->rbuf_offset = interface->rbuf_len;
449 continue;
450 }
451
452 /* If the captured data wasn't the whole packet, or if
453 the packet won't fit in the input buffer, all we
454 can do is drop it. */
455 if (hdr.bh_caplen != hdr.bh_datalen) {
456 interface->rbuf_offset =
457 BPF_WORDALIGN(interface->rbuf_offset +
458 hdr.bh_hdrlen + hdr.bh_caplen);
459 continue;
460 }
461
462 /* Skip over the BPF header... */
463 interface->rbuf_offset += hdr.bh_hdrlen;
464
465 /* Decode the physical header... */
466 offset = decode_hw_header(interface, interface->rbuf,
467 interface->rbuf_offset, hfrom);
468
469 /* If a physical layer checksum failed (dunno of any
470 physical layer that supports this, but WTH), skip this
471 packet. */
472 if (offset < 0) {
473 interface->rbuf_offset =
474 BPF_WORDALIGN(interface->rbuf_offset +
475 hdr.bh_caplen);
476 continue;
477 }
478 interface->rbuf_offset += offset;
479 hdr.bh_caplen -= offset;
480
481 /* Decode the IP and UDP headers... */
482 offset = decode_udp_ip_header(interface, interface->rbuf,
483 interface->rbuf_offset,
484 from, hdr.bh_caplen, &paylen, 1);
485
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;
492 }
493 interface->rbuf_offset = interface->rbuf_offset + offset;
494 hdr.bh_caplen -= offset;
495
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;
504 }
505
506 /* Copy out the data in the packet... */
507 memcpy(buf, interface->rbuf + interface->rbuf_offset, paylen);
508 interface->rbuf_offset =
509 BPF_WORDALIGN(interface->rbuf_offset + hdr.bh_caplen);
510 return paylen;
511 } while (interface->rbuf_offset < interface->rbuf_len);
512
513 return (0);
514 }
515
516 int can_unicast_without_arp (ip)
517 struct interface_info *ip;
518 {
519 return 1;
520 }
521
522 int can_receive_unicast_unconfigured (ip)
523 struct interface_info *ip;
524 {
525 return 1;
526 }
527
528 int supports_multiple_interfaces (ip)
529 struct interface_info *ip;
530 {
531 return 1;
532 }
533
534 void maybe_setup_fallback ()
535 {
536 isc_result_t status;
537 struct interface_info *fbi = (struct interface_info *)0;
538 if (setup_fallback (&fbi, MDL)) {
539 if_register_fallback (fbi);
540 status = omapi_register_io_object ((omapi_object_t *)fbi,
541 if_readsocket, 0,
542 fallback_discard, 0, 0);
543 if (status != ISC_R_SUCCESS)
544 log_fatal ("Can't register I/O handle for %s: %s",
545 fbi -> name, isc_result_totext (status));
546 interface_dereference (&fbi, MDL);
547 }
548 }
549 #endif
550
551 #if defined(USE_BPF_RECEIVE) || defined(USE_BPF_HWADDR)
552 void
553 get_hw_addr(const char *name, struct hardware *hw) {
554 struct ifaddrs *ifa;
555 struct ifaddrs *p;
556 struct sockaddr_dl *sa;
557
558 if (getifaddrs(&ifa) != 0) {
559 log_fatal("Error getting interface information; %m");
560 }
561
562 /*
563 * Loop through our interfaces finding a match.
564 */
565 sa = NULL;
566 for (p=ifa; (p != NULL) && (sa == NULL); p = p->ifa_next) {
567 if ((p->ifa_addr->sa_family == AF_LINK) &&
568 !strcmp(p->ifa_name, name)) {
569 sa = (struct sockaddr_dl *)p->ifa_addr;
570 }
571 }
572 if (sa == NULL) {
573 log_fatal("No interface called '%s'", name);
574 }
575
576 /*
577 * Pull out the appropriate information.
578 */
579 switch (sa->sdl_type) {
580 case IFT_ETHER:
581 #if defined (IFT_L2VLAN)
582 case IFT_L2VLAN:
583 #endif
584 hw->hlen = sa->sdl_alen + 1;
585 hw->hbuf[0] = HTYPE_ETHER;
586 memcpy(&hw->hbuf[1], LLADDR(sa), sa->sdl_alen);
587 break;
588 case IFT_ISO88023:
589 case IFT_ISO88024: /* "token ring" */
590 case IFT_ISO88025:
591 case IFT_ISO88026:
592 hw->hlen = sa->sdl_alen + 1;
593 hw->hbuf[0] = HTYPE_IEEE802;
594 memcpy(&hw->hbuf[1], LLADDR(sa), sa->sdl_alen);
595 break;
596 #ifdef IFT_FDDI
597 case IFT_FDDI:
598 hw->hlen = sa->sdl_alen + 1;
599 hw->hbuf[0] = HTYPE_FDDI;
600 memcpy(&hw->hbuf[1], LLADDR(sa), sa->sdl_alen);
601 break;
602 #endif /* IFT_FDDI */
603 default:
604 log_fatal("Unsupported device type %d for \"%s\"",
605 sa->sdl_type, name);
606 }
607
608 freeifaddrs(ifa);
609 }
610 #endif