Home | History | Annotate | Line # | Download | only in config
ndbootd-bpf.c revision 1.1.1.2
      1 /* ndbootd-bpf.c - the Sun Network Disk (nd) daemon BPF component: */
      2 
      3 /*
      4  * Copyright (c) 2001 Matthew Fredette.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *   1. Redistributions of source code must retain the above copyright
     10  *      notice, this list of conditions and the following disclaimer.
     11  *   2. Redistributions in binary form must reproduce the above copyright
     12  *      notice, this list of conditions and the following disclaimer in the
     13  *      documentation and/or other materials provided with the distribution.
     14  *   3. All advertising materials mentioning features or use of this software
     15  *      must display the following acknowledgement:
     16  *        This product includes software developed by Matthew Fredette.
     17  *   4. The name of Matthew Fredette may not be used to endorse or promote
     18  *      products derived from this software without specific prior written
     19  *      permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     22  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     24  */
     25 
     26 /* <<Header: /data/home/fredette/project/THE-WEIGHT-CVS/ndbootd/config/ndbootd-bpf.c,v 1.4 2001/05/23 02:35:49 fredette Exp >> */
     27 
     28 /*
     29  * <<Log: ndbootd-bpf.c,v >>
     30  * Revision 1.4  2001/05/23 02:35:49  fredette
     31  * Changed many debugging printfs to compile quietly on the
     32  * alpha.  Patch from Andrew Brown <atatat (at) atatdot.net>.
     33  *
     34  * Revision 1.3  2001/05/22 13:13:24  fredette
     35  * Ran indent(1) with NetBSD's KNF-approximating profile.
     36  *
     37  * Revision 1.2  2001/05/09 20:50:46  fredette
     38  * Removed an unnecessary comment.
     39  *
     40  * Revision 1.1  2001/01/29 15:12:13  fredette
     41  * Added.
     42  *
     43  */
     44 
     45 static const char _ndbootd_bpf_c_rcsid[] = "<<Id: ndbootd-bpf.c,v 1.4 2001/05/23 02:35:49 fredette Exp >>";
     46 
     47 /* includes: */
     48 #include <net/bpf.h>
     49 
     50 /* structures: */
     51 struct _ndbootd_interface_bpf {
     52 
     53 	/* the size of the packet buffer for the interface: */
     54 	size_t _ndbootd_interface_bpf_buffer_size;
     55 
     56 	/* the packet buffer for the interface: */
     57 	char *_ndbootd_interface_bpf_buffer;
     58 
     59 	/* the next offset within the packet buffer, and the end of the data
     60 	 * in the packet buffer: */
     61 	size_t _ndbootd_interface_bpf_buffer_offset;
     62 	size_t _ndbootd_interface_bpf_buffer_end;
     63 };
     64 
     65 /* the BPF program to capture ND packets: */
     66 static struct bpf_insn ndboot_bpf_filter[] = {
     67 
     68 	/* drop this packet if its ethertype isn't ETHERTYPE_IP: */
     69 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, NDBOOTD_OFFSETOF(struct ether_header, ether_type)),
     70 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 9),
     71 
     72 	/* drop this packet if its IP protocol isn't IPPROTO_ND: */
     73 	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, sizeof(struct ether_header) + NDBOOTD_OFFSETOF(struct ip, ip_p)),
     74 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_ND, 0, 7),
     75 
     76 	/* drop this packet if it's a fragment: */
     77 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, sizeof(struct ether_header) + NDBOOTD_OFFSETOF(struct ip, ip_off)),
     78 	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x3fff, 5, 0),
     79 
     80 	/* drop this packet if it is carrying data (we only want requests,
     81 	 * which have no data): */
     82 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, sizeof(struct ether_header) + NDBOOTD_OFFSETOF(struct ip, ip_len)),
     83 	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, sizeof(struct ether_header)),
     84 	BPF_STMT(BPF_ALU + BPF_SUB + BPF_X, 0),
     85 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, sizeof(struct ndboot_packet), 0, 1),
     86 
     87 	/* accept this packet: */
     88 	BPF_STMT(BPF_RET + BPF_K, (u_int) -1),
     89 
     90 	/* drop this packet: */
     91 	BPF_STMT(BPF_RET + BPF_K, 0),
     92 };
     93 
     94 /* this opens a raw socket using BPF. */
     95 int
     96 ndbootd_raw_open(struct ndbootd_interface * interface)
     97 {
     98 	int network_fd;
     99 #define DEV_BPF_FORMAT "/dev/bpf%d"
    100 	char dev_bpf_filename[sizeof(DEV_BPF_FORMAT) + (sizeof(int) * 3) + 1];
    101 	int minor;
    102 	int saved_errno;
    103 	u_int bpf_opt;
    104 	struct bpf_version version;
    105 	u_int packet_buffer_size;
    106 	struct bpf_program program;
    107 	struct _ndbootd_interface_bpf *interface_bpf;
    108 
    109 	/* loop trying to open a /dev/bpf device: */
    110 	for (minor = 0;; minor++) {
    111 
    112 		/* form the name of the next device to try, then try opening
    113 		 * it. if we succeed, we're done: */
    114 		sprintf(dev_bpf_filename, DEV_BPF_FORMAT, minor);
    115 		_NDBOOTD_DEBUG((fp, "bpf: trying %s", dev_bpf_filename));
    116 		if ((network_fd = open(dev_bpf_filename, O_RDWR)) >= 0) {
    117 			_NDBOOTD_DEBUG((fp, "bpf: opened %s", dev_bpf_filename));
    118 			break;
    119 		}
    120 		/* we failed to open this device.  if this device was simply
    121 		 * busy, loop: */
    122 		_NDBOOTD_DEBUG((fp, "bpf: failed to open %s: %s", dev_bpf_filename, strerror(errno)));
    123 		if (errno == EBUSY) {
    124 			continue;
    125 		}
    126 		/* otherwise, we have failed: */
    127 		return (-1);
    128 	}
    129 
    130 	/* this macro helps in closing the BPF socket on error: */
    131 #define _NDBOOTD_RAW_OPEN_ERROR(x) saved_errno = errno; x; errno = saved_errno
    132 
    133 	/* check the BPF version: */
    134 	if (ioctl(network_fd, BIOCVERSION, &version) < 0) {
    135 		_NDBOOTD_DEBUG((fp, "bpf: failed to get the BPF version on %s: %s",
    136 			dev_bpf_filename, strerror(errno)));
    137 		_NDBOOTD_RAW_OPEN_ERROR(close(network_fd));
    138 		return (-1);
    139 	}
    140 	if (version.bv_major != BPF_MAJOR_VERSION
    141 	    || version.bv_minor < BPF_MINOR_VERSION) {
    142 		_NDBOOTD_DEBUG((fp, "bpf: kernel BPF version is %d.%d, my BPF version is %d.%d",
    143 			version.bv_major, version.bv_minor,
    144 			BPF_MAJOR_VERSION, BPF_MINOR_VERSION));
    145 		close(network_fd);
    146 		errno = ENXIO;
    147 		return (-1);
    148 	}
    149 	/* put the BPF device into immediate mode: */
    150 	bpf_opt = TRUE;
    151 	if (ioctl(network_fd, BIOCIMMEDIATE, &bpf_opt) < 0) {
    152 		_NDBOOTD_DEBUG((fp, "bpf: failed to put %s into immediate mode: %s",
    153 			dev_bpf_filename, strerror(errno)));
    154 		_NDBOOTD_RAW_OPEN_ERROR(close(network_fd));
    155 		return (-1);
    156 	}
    157 	/* tell the BPF device we're providing complete Ethernet headers: */
    158 	bpf_opt = TRUE;
    159 	if (ioctl(network_fd, BIOCSHDRCMPLT, &bpf_opt) < 0) {
    160 		_NDBOOTD_DEBUG((fp, "bpf: failed to put %s into complete-headers mode: %s",
    161 			dev_bpf_filename, strerror(errno)));
    162 		_NDBOOTD_RAW_OPEN_ERROR(close(network_fd));
    163 		return (-1);
    164 	}
    165 	/* point the BPF device at the interface we're using: */
    166 	if (ioctl(network_fd, BIOCSETIF, interface->ndbootd_interface_ifreq) < 0) {
    167 		_NDBOOTD_DEBUG((fp, "bpf: failed to point BPF socket at %s: %s",
    168 			interface->ndbootd_interface_ifreq->ifr_name, strerror(errno)));
    169 		saved_errno = errno;
    170 		close(network_fd);
    171 		errno = saved_errno;
    172 		return (-1);
    173 	}
    174 	/* set the filter on the BPF device: */
    175 	program.bf_len = sizeof(ndboot_bpf_filter) / sizeof(ndboot_bpf_filter[0]);
    176 	program.bf_insns = ndboot_bpf_filter;
    177 	if (ioctl(network_fd, BIOCSETF, &program) < 0) {
    178 		_NDBOOTD_DEBUG((fp, "bpf: failed to set the filter on %s: %s",
    179 			dev_bpf_filename, strerror(errno)));
    180 		_NDBOOTD_RAW_OPEN_ERROR(close(network_fd));
    181 		return (-1);
    182 	}
    183 	/* get the BPF read buffer size: */
    184 	if (ioctl(network_fd, BIOCGBLEN, &packet_buffer_size) < 0) {
    185 		_NDBOOTD_DEBUG((fp, "bpf: failed to read the buffer size for %s: %s",
    186 			dev_bpf_filename, strerror(errno)));
    187 		_NDBOOTD_RAW_OPEN_ERROR(close(network_fd));
    188 		return (-1);
    189 	}
    190 	_NDBOOTD_DEBUG((fp, "bpf: buffer size for %s is %u",
    191 		dev_bpf_filename, packet_buffer_size));
    192 
    193 	/* allocate our private interface information and we're done: */
    194 	interface->ndbootd_interface_fd = network_fd;
    195 	interface_bpf = ndbootd_new0(struct _ndbootd_interface_bpf, 1);
    196 	interface_bpf->_ndbootd_interface_bpf_buffer_size = packet_buffer_size;
    197 	interface_bpf->_ndbootd_interface_bpf_buffer = ndbootd_new(char, packet_buffer_size);
    198 	interface->_ndbootd_interface_raw_private = interface_bpf;
    199 	return (0);
    200 #undef _NDBOOTD_RAW_OPEN_ERROR
    201 }
    202 
    203 /* this reads a raw packet: */
    204 int
    205 ndbootd_raw_read(struct ndbootd_interface * interface, void *packet_buffer, size_t packet_buffer_size)
    206 {
    207 	struct _ndbootd_interface_bpf *interface_bpf;
    208 	ssize_t buffer_end;
    209 	struct bpf_hdr the_bpf_header;
    210 	fd_set fdset_read;
    211 
    212 	/* recover our state: */
    213 	interface_bpf = (struct _ndbootd_interface_bpf *) interface->_ndbootd_interface_raw_private;
    214 
    215 	/* loop until we have something to return: */
    216 	for (;;) {
    217 
    218 		/* if the buffer is empty, fill it: */
    219 		if (interface_bpf->_ndbootd_interface_bpf_buffer_offset
    220 		    >= interface_bpf->_ndbootd_interface_bpf_buffer_end) {
    221 
    222 			/* select on the BPF socket: */
    223 			_NDBOOTD_DEBUG((fp, "bpf: calling select"));
    224 			FD_ZERO(&fdset_read);
    225 			FD_SET(interface->ndbootd_interface_fd, &fdset_read);
    226 			switch (select(interface->ndbootd_interface_fd + 1, &fdset_read, NULL, NULL, NULL)) {
    227 			case 0:
    228 				_NDBOOTD_DEBUG((fp, "bpf: select returned zero"));
    229 				continue;
    230 			case 1:
    231 				break;
    232 			default:
    233 				if (errno == EINTR) {
    234 					_NDBOOTD_DEBUG((fp, "bpf: select got EINTR"));
    235 					continue;
    236 				}
    237 				_NDBOOTD_DEBUG((fp, "bpf: select failed: %s", strerror(errno)));
    238 				return (-1);
    239 			}
    240 			assert(FD_ISSET(interface->ndbootd_interface_fd, &fdset_read));
    241 
    242 			/* read the BPF socket: */
    243 			_NDBOOTD_DEBUG((fp, "bpf: calling read"));
    244 			buffer_end = read(interface->ndbootd_interface_fd,
    245 			    interface_bpf->_ndbootd_interface_bpf_buffer,
    246 			    interface_bpf->_ndbootd_interface_bpf_buffer_size);
    247 			if (buffer_end <= 0) {
    248 				_NDBOOTD_DEBUG((fp, "bpf: failed to read packets: %s", strerror(errno)));
    249 				return (-1);
    250 			}
    251 			_NDBOOTD_DEBUG((fp, "bpf: read %ld bytes of packets", (long) buffer_end));
    252 			interface_bpf->_ndbootd_interface_bpf_buffer_offset = 0;
    253 			interface_bpf->_ndbootd_interface_bpf_buffer_end = buffer_end;
    254 		}
    255 		/* if there's not enough for a BPF header, flush the buffer: */
    256 		if ((interface_bpf->_ndbootd_interface_bpf_buffer_offset
    257 			+ sizeof(the_bpf_header))
    258 		    > interface_bpf->_ndbootd_interface_bpf_buffer_end) {
    259 			_NDBOOTD_DEBUG((fp, "bpf: flushed garbage BPF header bytes"));
    260 			interface_bpf->_ndbootd_interface_bpf_buffer_end = 0;
    261 			continue;
    262 		}
    263 		/* get the BPF header and check it: */
    264 		memcpy(&the_bpf_header,
    265 		    interface_bpf->_ndbootd_interface_bpf_buffer
    266 		    + interface_bpf->_ndbootd_interface_bpf_buffer_offset,
    267 		    sizeof(the_bpf_header));
    268 		interface_bpf->_ndbootd_interface_bpf_buffer_offset += the_bpf_header.bh_hdrlen;
    269 
    270 		/* if we're missing some part of the packet: */
    271 		if (the_bpf_header.bh_caplen != the_bpf_header.bh_datalen
    272 		    || ((interface_bpf->_ndbootd_interface_bpf_buffer_offset + the_bpf_header.bh_datalen)
    273 			> interface_bpf->_ndbootd_interface_bpf_buffer_end)) {
    274 			_NDBOOTD_DEBUG((fp, "bpf: flushed truncated BPF packet"));
    275 			interface_bpf->_ndbootd_interface_bpf_buffer_offset += the_bpf_header.bh_datalen;
    276 			continue;
    277 		}
    278 		/* silently ignore packets that don't even have Ethernet
    279 		 * headers, and those packets that we transmitted: */
    280 		if (the_bpf_header.bh_datalen < sizeof(struct ether_header)
    281 		    || !memcmp(((struct ether_header *)
    282 			    (interface_bpf->_ndbootd_interface_bpf_buffer
    283 				+ interface_bpf->_ndbootd_interface_bpf_buffer_offset))->ether_shost,
    284 			interface->ndbootd_interface_ether,
    285 			ETHER_ADDR_LEN)) {
    286 			/* silently ignore packets from us: */
    287 			interface_bpf->_ndbootd_interface_bpf_buffer_offset += the_bpf_header.bh_datalen;
    288 			continue;
    289 		}
    290 		/* if the caller hasn't provided a large enough buffer: */
    291 		if (packet_buffer_size < the_bpf_header.bh_datalen) {
    292 			errno = EIO;
    293 			interface_bpf->_ndbootd_interface_bpf_buffer_offset += the_bpf_header.bh_datalen;
    294 			return (-1);
    295 		}
    296 		/* return this captured packet to the user: */
    297 		memcpy(packet_buffer,
    298 		    interface_bpf->_ndbootd_interface_bpf_buffer
    299 		    + interface_bpf->_ndbootd_interface_bpf_buffer_offset,
    300 		    the_bpf_header.bh_datalen);
    301 		interface_bpf->_ndbootd_interface_bpf_buffer_offset += the_bpf_header.bh_datalen;
    302 		return (the_bpf_header.bh_datalen);
    303 	}
    304 	/* NOTREACHED */
    305 }
    306 
    307 /* this writes a raw packet: */
    308 int
    309 ndbootd_raw_write(struct ndbootd_interface * interface, void *packet_buffer, size_t packet_buffer_size)
    310 {
    311 	return (write(interface->ndbootd_interface_fd, packet_buffer, packet_buffer_size));
    312 }
    313