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