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