1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1988, 1992 The University of Utah and the Center 5 * for Software Science (CSS). 6 * Copyright (c) 1992, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * the Center for Software Science of the University of Utah Computer 11 * Science Department. CSS requests users of this software to return 12 * to css-dist@cs.utah.edu any improvements that they make and grant 13 * CSS redistribution rights. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 3. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * from: @(#)bpf.c 8.1 (Berkeley) 6/4/93 40 * 41 * From: Utah Hdr: bpf.c 3.1 92/07/06 42 * Author: Jeff Forys, University of Utah CSS 43 */ 44 45 #ifndef lint 46 #if 0 47 static const char sccsid[] = "@(#)bpf.c 8.1 (Berkeley) 6/4/93"; 48 #endif 49 static const char rcsid[] = 50 "$FreeBSD$"; 51 #endif /* not lint */ 52 53 #include <sys/param.h> 54 #include <sys/ioctl.h> 55 #include <sys/socket.h> 56 #include <sys/time.h> 57 58 #include <net/if.h> 59 #include <net/bpf.h> 60 61 #include <ctype.h> 62 #include <errno.h> 63 #include <fcntl.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <syslog.h> 68 #include <unistd.h> 69 #include "defs.h" 70 #include "pathnames.h" 71 72 static int BpfFd = -1; 73 static unsigned BpfLen = 0; 74 static u_int8_t *BpfPkt = NULL; 75 76 /* 77 ** BpfOpen -- Open and initialize a BPF device. 78 ** 79 ** Parameters: 80 ** None. 81 ** 82 ** Returns: 83 ** File descriptor of opened BPF device (for select() etc). 84 ** 85 ** Side Effects: 86 ** If an error is encountered, the program terminates here. 87 */ 88 int 89 BpfOpen(void) 90 { 91 struct ifreq ifr; 92 char bpfdev[32]; 93 int n = 0; 94 95 /* 96 * Open the first available BPF device. 97 */ 98 do { 99 (void) sprintf(bpfdev, _PATH_BPF, n++); 100 BpfFd = open(bpfdev, O_RDWR); 101 } while (BpfFd < 0 && (errno == EBUSY || errno == EPERM)); 102 103 if (BpfFd < 0) { 104 syslog(LOG_ERR, "bpf: no available devices: %m"); 105 Exit(0); 106 } 107 108 /* 109 * Set interface name for bpf device, get data link layer 110 * type and make sure it's type Ethernet. 111 */ 112 (void) strncpy(ifr.ifr_name, IntfName, sizeof(ifr.ifr_name)); 113 if (ioctl(BpfFd, BIOCSETIF, (caddr_t)&ifr) < 0) { 114 syslog(LOG_ERR, "bpf: ioctl(BIOCSETIF,%s): %m", IntfName); 115 Exit(0); 116 } 117 118 /* 119 * Make sure we are dealing with an Ethernet device. 120 */ 121 if (ioctl(BpfFd, BIOCGDLT, (caddr_t)&n) < 0) { 122 syslog(LOG_ERR, "bpf: ioctl(BIOCGDLT): %m"); 123 Exit(0); 124 } 125 if (n != DLT_EN10MB) { 126 syslog(LOG_ERR,"bpf: %s: data-link type %d unsupported", 127 IntfName, n); 128 Exit(0); 129 } 130 131 /* 132 * On read(), return packets immediately (do not buffer them). 133 */ 134 n = 1; 135 if (ioctl(BpfFd, BIOCIMMEDIATE, (caddr_t)&n) < 0) { 136 syslog(LOG_ERR, "bpf: ioctl(BIOCIMMEDIATE): %m"); 137 Exit(0); 138 } 139 140 /* 141 * Try to enable the chip/driver's multicast address filter to 142 * grab our RMP address. If this fails, try promiscuous mode. 143 * If this fails, there's no way we are going to get any RMP 144 * packets so just exit here. 145 */ 146 #ifdef MSG_EOR 147 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2; 148 #endif 149 ifr.ifr_addr.sa_family = AF_UNSPEC; 150 memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0], RMP_ADDRLEN); 151 if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) { 152 syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m"); 153 Exit(0); 154 } 155 156 /* 157 * Ask BPF how much buffer space it requires and allocate one. 158 */ 159 if (ioctl(BpfFd, BIOCGBLEN, (caddr_t)&BpfLen) < 0) { 160 syslog(LOG_ERR, "bpf: ioctl(BIOCGBLEN): %m"); 161 Exit(0); 162 } 163 if (BpfPkt == NULL) 164 BpfPkt = (u_int8_t *)malloc(BpfLen); 165 166 if (BpfPkt == NULL) { 167 syslog(LOG_ERR, "bpf: out of memory (%u bytes for bpfpkt)", 168 BpfLen); 169 Exit(0); 170 } 171 172 /* 173 * Write a little program to snarf RMP Boot packets and stuff 174 * it down BPF's throat (i.e. set up the packet filter). 175 */ 176 { 177 #define RMP ((struct rmp_packet *)0) 178 static struct bpf_insn bpf_insn[] = { 179 { BPF_LD|BPF_B|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dsap }, 180 { BPF_JMP|BPF_JEQ|BPF_K, 0, 5, IEEE_DSAP_HP }, 181 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.cntrl }, 182 { BPF_JMP|BPF_JEQ|BPF_K, 0, 3, IEEE_CNTL_HP }, 183 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dxsap }, 184 { BPF_JMP|BPF_JEQ|BPF_K, 0, 1, HPEXT_DXSAP }, 185 { BPF_RET|BPF_K, 0, 0, RMP_MAX_PACKET }, 186 { BPF_RET|BPF_K, 0, 0, 0x0 } 187 }; 188 #undef RMP 189 static struct bpf_program bpf_pgm = { 190 sizeof(bpf_insn)/sizeof(bpf_insn[0]), bpf_insn 191 }; 192 193 if (ioctl(BpfFd, BIOCSETF, (caddr_t)&bpf_pgm) < 0) { 194 syslog(LOG_ERR, "bpf: ioctl(BIOCSETF): %m"); 195 Exit(0); 196 } 197 } 198 199 return(BpfFd); 200 } 201 202 /* 203 ** BPF GetIntfName -- Return the name of a network interface attached to 204 ** the system, or 0 if none can be found. The interface 205 ** must be configured up; the lowest unit number is 206 ** preferred; loopback is ignored. 207 ** 208 ** Parameters: 209 ** errmsg - if no network interface found, *errmsg explains why. 210 ** 211 ** Returns: 212 ** A (static) pointer to interface name, or NULL on error. 213 ** 214 ** Side Effects: 215 ** None. 216 */ 217 char * 218 BpfGetIntfName(char **errmsg) 219 { 220 struct ifreq ibuf[8], *ifrp, *ifend, *mp; 221 struct ifconf ifc; 222 int fd; 223 int minunit, n; 224 char *cp; 225 static char device[sizeof(ifrp->ifr_name)]; 226 static char errbuf[128] = "No Error!"; 227 228 if (errmsg != NULL) 229 *errmsg = errbuf; 230 231 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 232 (void) strcpy(errbuf, "bpf: socket: %m"); 233 return(NULL); 234 } 235 ifc.ifc_len = sizeof ibuf; 236 ifc.ifc_buf = (caddr_t)ibuf; 237 238 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 || 239 ifc.ifc_len < sizeof(struct ifreq)) { 240 (void) strcpy(errbuf, "bpf: ioctl(SIOCGIFCONF): %m"); 241 return(NULL); 242 } 243 ifrp = ibuf; 244 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len); 245 246 mp = NULL; 247 minunit = 666; 248 for (; ifrp < ifend; ++ifrp) { 249 if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) { 250 (void) strcpy(errbuf, "bpf: ioctl(SIOCGIFFLAGS): %m"); 251 return(NULL); 252 } 253 254 /* 255 * If interface is down or this is the loopback interface, 256 * ignore it. 257 */ 258 if ((ifrp->ifr_flags & IFF_UP) == 0 || 259 #ifdef IFF_LOOPBACK 260 (ifrp->ifr_flags & IFF_LOOPBACK)) 261 #else 262 (strcmp(ifrp->ifr_name, "lo0") == 0)) 263 #endif 264 continue; 265 266 for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp) 267 ; 268 n = atoi(cp); 269 if (n < minunit) { 270 minunit = n; 271 mp = ifrp; 272 } 273 } 274 275 (void) close(fd); 276 if (mp == NULL) { 277 (void) strcpy(errbuf, "bpf: no interfaces found"); 278 return(NULL); 279 } 280 281 (void) strcpy(device, mp->ifr_name); 282 return(device); 283 } 284 285 /* 286 ** BpfRead -- Read packets from a BPF device and fill in `rconn'. 287 ** 288 ** Parameters: 289 ** rconn - filled in with next packet. 290 ** doread - is True if we can issue a read() syscall. 291 ** 292 ** Returns: 293 ** True if `rconn' contains a new packet, False otherwise. 294 ** 295 ** Side Effects: 296 ** None. 297 */ 298 int 299 BpfRead(RMPCONN *rconn, int doread) 300 { 301 int datlen, caplen, hdrlen; 302 static u_int8_t *bp = NULL, *ep = NULL; 303 int cc; 304 305 /* 306 * The read() may block, or it may return one or more packets. 307 * We let the caller decide whether or not we can issue a read(). 308 */ 309 if (doread) { 310 if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) { 311 syslog(LOG_ERR, "bpf: read: %m"); 312 return(0); 313 } else { 314 bp = BpfPkt; 315 ep = BpfPkt + cc; 316 } 317 } 318 319 #define bhp ((struct bpf_hdr *)bp) 320 /* 321 * If there is a new packet in the buffer, stuff it into `rconn' 322 * and return a success indication. 323 */ 324 if (bp < ep) { 325 datlen = bhp->bh_datalen; 326 caplen = bhp->bh_caplen; 327 hdrlen = bhp->bh_hdrlen; 328 329 if (caplen != datlen) 330 syslog(LOG_ERR, 331 "bpf: short packet dropped (%d of %d bytes)", 332 caplen, datlen); 333 else if (caplen > sizeof(struct rmp_packet)) 334 syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)", 335 caplen); 336 else { 337 rconn->rmplen = caplen; 338 memmove((char *)&rconn->tstamp, (char *)&bhp->bh_tstamp, 339 sizeof(struct timeval)); 340 memmove((char *)&rconn->rmp, (char *)bp + hdrlen, caplen); 341 } 342 bp += BPF_WORDALIGN(caplen + hdrlen); 343 return(1); 344 } 345 #undef bhp 346 347 return(0); 348 } 349 350 /* 351 ** BpfWrite -- Write packet to BPF device. 352 ** 353 ** Parameters: 354 ** rconn - packet to send. 355 ** 356 ** Returns: 357 ** True if write succeeded, False otherwise. 358 ** 359 ** Side Effects: 360 ** None. 361 */ 362 int 363 BpfWrite(RMPCONN *rconn) 364 { 365 if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) { 366 syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn)); 367 return(0); 368 } 369 370 return(1); 371 } 372 373 /* 374 ** BpfClose -- Close a BPF device. 375 ** 376 ** Parameters: 377 ** None. 378 ** 379 ** Returns: 380 ** Nothing. 381 ** 382 ** Side Effects: 383 ** None. 384 */ 385 void 386 BpfClose(void) 387 { 388 struct ifreq ifr; 389 390 if (BpfPkt != NULL) { 391 free((char *)BpfPkt); 392 BpfPkt = NULL; 393 } 394 395 if (BpfFd == -1) 396 return; 397 398 #ifdef MSG_EOR 399 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2; 400 #endif 401 ifr.ifr_addr.sa_family = AF_UNSPEC; 402 memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0], RMP_ADDRLEN); 403 if (ioctl(BpfFd, SIOCDELMULTI, (caddr_t)&ifr) < 0) 404 (void) ioctl(BpfFd, BIOCPROMISC, (caddr_t)0); 405 406 (void) close(BpfFd); 407 BpfFd = -1; 408 } 409