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