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: Utah Hdr: bpf.c 3.1 92/07/06 40 * Author: Jeff Forys, University of Utah CSS 41 */ 42 43 #ifndef lint 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/ioctl.h> 48 #include <sys/socket.h> 49 #include <sys/time.h> 50 51 #include <net/if.h> 52 #include <net/bpf.h> 53 54 #include <ctype.h> 55 #include <errno.h> 56 #include <fcntl.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <syslog.h> 61 #include <unistd.h> 62 #include "defs.h" 63 #include "pathnames.h" 64 65 static int BpfFd = -1; 66 static unsigned BpfLen = 0; 67 static u_int8_t *BpfPkt = NULL; 68 69 /* 70 ** BpfOpen -- Open and initialize a BPF device. 71 ** 72 ** Parameters: 73 ** None. 74 ** 75 ** Returns: 76 ** File descriptor of opened BPF device (for select() etc). 77 ** 78 ** Side Effects: 79 ** If an error is encountered, the program terminates here. 80 */ 81 int 82 BpfOpen(void) 83 { 84 struct ifreq ifr; 85 char bpfdev[32]; 86 int n = 0; 87 88 /* 89 * Open the first available BPF device. 90 */ 91 do { 92 (void) sprintf(bpfdev, _PATH_BPF, n++); 93 BpfFd = open(bpfdev, O_RDWR); 94 } while (BpfFd < 0 && (errno == EBUSY || errno == EPERM)); 95 96 if (BpfFd < 0) { 97 syslog(LOG_ERR, "bpf: no available devices: %m"); 98 Exit(0); 99 } 100 101 /* 102 * Set interface name for bpf device, get data link layer 103 * type and make sure it's type Ethernet. 104 */ 105 (void) strncpy(ifr.ifr_name, IntfName, sizeof(ifr.ifr_name)); 106 if (ioctl(BpfFd, BIOCSETIF, (caddr_t)&ifr) < 0) { 107 syslog(LOG_ERR, "bpf: ioctl(BIOCSETIF,%s): %m", IntfName); 108 Exit(0); 109 } 110 111 /* 112 * Make sure we are dealing with an Ethernet device. 113 */ 114 if (ioctl(BpfFd, BIOCGDLT, (caddr_t)&n) < 0) { 115 syslog(LOG_ERR, "bpf: ioctl(BIOCGDLT): %m"); 116 Exit(0); 117 } 118 if (n != DLT_EN10MB) { 119 syslog(LOG_ERR,"bpf: %s: data-link type %d unsupported", 120 IntfName, n); 121 Exit(0); 122 } 123 124 /* 125 * On read(), return packets immediately (do not buffer them). 126 */ 127 n = 1; 128 if (ioctl(BpfFd, BIOCIMMEDIATE, (caddr_t)&n) < 0) { 129 syslog(LOG_ERR, "bpf: ioctl(BIOCIMMEDIATE): %m"); 130 Exit(0); 131 } 132 133 /* 134 * Try to enable the chip/driver's multicast address filter to 135 * grab our RMP address. If this fails, try promiscuous mode. 136 * If this fails, there's no way we are going to get any RMP 137 * packets so just exit here. 138 */ 139 #ifdef MSG_EOR 140 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2; 141 #endif 142 ifr.ifr_addr.sa_family = AF_UNSPEC; 143 memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0], RMP_ADDRLEN); 144 if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) { 145 syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m"); 146 Exit(0); 147 } 148 149 /* 150 * Ask BPF how much buffer space it requires and allocate one. 151 */ 152 if (ioctl(BpfFd, BIOCGBLEN, (caddr_t)&BpfLen) < 0) { 153 syslog(LOG_ERR, "bpf: ioctl(BIOCGBLEN): %m"); 154 Exit(0); 155 } 156 if (BpfPkt == NULL) 157 BpfPkt = (u_int8_t *)malloc(BpfLen); 158 159 if (BpfPkt == NULL) { 160 syslog(LOG_ERR, "bpf: out of memory (%u bytes for bpfpkt)", 161 BpfLen); 162 Exit(0); 163 } 164 165 /* 166 * Write a little program to snarf RMP Boot packets and stuff 167 * it down BPF's throat (i.e. set up the packet filter). 168 */ 169 { 170 #define RMP ((struct rmp_packet *)0) 171 static struct bpf_insn bpf_insn[] = { 172 { BPF_LD|BPF_B|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dsap }, 173 { BPF_JMP|BPF_JEQ|BPF_K, 0, 5, IEEE_DSAP_HP }, 174 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.cntrl }, 175 { BPF_JMP|BPF_JEQ|BPF_K, 0, 3, IEEE_CNTL_HP }, 176 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dxsap }, 177 { BPF_JMP|BPF_JEQ|BPF_K, 0, 1, HPEXT_DXSAP }, 178 { BPF_RET|BPF_K, 0, 0, RMP_MAX_PACKET }, 179 { BPF_RET|BPF_K, 0, 0, 0x0 } 180 }; 181 #undef RMP 182 static struct bpf_program bpf_pgm = { 183 sizeof(bpf_insn)/sizeof(bpf_insn[0]), bpf_insn 184 }; 185 186 if (ioctl(BpfFd, BIOCSETF, (caddr_t)&bpf_pgm) < 0) { 187 syslog(LOG_ERR, "bpf: ioctl(BIOCSETF): %m"); 188 Exit(0); 189 } 190 } 191 192 return(BpfFd); 193 } 194 195 /* 196 ** BPF GetIntfName -- Return the name of a network interface attached to 197 ** the system, or 0 if none can be found. The interface 198 ** must be configured up; the lowest unit number is 199 ** preferred; loopback is ignored. 200 ** 201 ** Parameters: 202 ** errmsg - if no network interface found, *errmsg explains why. 203 ** 204 ** Returns: 205 ** A (static) pointer to interface name, or NULL on error. 206 ** 207 ** Side Effects: 208 ** None. 209 */ 210 char * 211 BpfGetIntfName(char **errmsg) 212 { 213 struct ifreq ibuf[8], *ifrp, *ifend, *mp; 214 struct ifconf ifc; 215 int fd; 216 int minunit, n; 217 char *cp; 218 static char device[sizeof(ifrp->ifr_name)]; 219 static char errbuf[128] = "No Error!"; 220 221 if (errmsg != NULL) 222 *errmsg = errbuf; 223 224 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 225 (void) strcpy(errbuf, "bpf: socket: %m"); 226 return(NULL); 227 } 228 ifc.ifc_len = sizeof ibuf; 229 ifc.ifc_buf = (caddr_t)ibuf; 230 231 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 || 232 ifc.ifc_len < sizeof(struct ifreq)) { 233 (void) strcpy(errbuf, "bpf: ioctl(SIOCGIFCONF): %m"); 234 return(NULL); 235 } 236 ifrp = ibuf; 237 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len); 238 239 mp = NULL; 240 minunit = 666; 241 for (; ifrp < ifend; ++ifrp) { 242 if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) { 243 (void) strcpy(errbuf, "bpf: ioctl(SIOCGIFFLAGS): %m"); 244 return(NULL); 245 } 246 247 /* 248 * If interface is down or this is the loopback interface, 249 * ignore it. 250 */ 251 if ((ifrp->ifr_flags & IFF_UP) == 0 || 252 #ifdef IFF_LOOPBACK 253 (ifrp->ifr_flags & IFF_LOOPBACK)) 254 #else 255 (strcmp(ifrp->ifr_name, "lo0") == 0)) 256 #endif 257 continue; 258 259 for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp) 260 ; 261 n = atoi(cp); 262 if (n < minunit) { 263 minunit = n; 264 mp = ifrp; 265 } 266 } 267 268 (void) close(fd); 269 if (mp == NULL) { 270 (void) strcpy(errbuf, "bpf: no interfaces found"); 271 return(NULL); 272 } 273 274 (void) strcpy(device, mp->ifr_name); 275 return(device); 276 } 277 278 /* 279 ** BpfRead -- Read packets from a BPF device and fill in `rconn'. 280 ** 281 ** Parameters: 282 ** rconn - filled in with next packet. 283 ** doread - is True if we can issue a read() syscall. 284 ** 285 ** Returns: 286 ** True if `rconn' contains a new packet, False otherwise. 287 ** 288 ** Side Effects: 289 ** None. 290 */ 291 int 292 BpfRead(RMPCONN *rconn, int doread) 293 { 294 int datlen, caplen, hdrlen; 295 static u_int8_t *bp = NULL, *ep = NULL; 296 int cc; 297 298 /* 299 * The read() may block, or it may return one or more packets. 300 * We let the caller decide whether or not we can issue a read(). 301 */ 302 if (doread) { 303 if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) { 304 syslog(LOG_ERR, "bpf: read: %m"); 305 return(0); 306 } else { 307 bp = BpfPkt; 308 ep = BpfPkt + cc; 309 } 310 } 311 312 #define bhp ((struct bpf_hdr *)bp) 313 /* 314 * If there is a new packet in the buffer, stuff it into `rconn' 315 * and return a success indication. 316 */ 317 if (bp < ep) { 318 datlen = bhp->bh_datalen; 319 caplen = bhp->bh_caplen; 320 hdrlen = bhp->bh_hdrlen; 321 322 if (caplen != datlen) 323 syslog(LOG_ERR, 324 "bpf: short packet dropped (%d of %d bytes)", 325 caplen, datlen); 326 else if (caplen > sizeof(struct rmp_packet)) 327 syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)", 328 caplen); 329 else { 330 rconn->rmplen = caplen; 331 memmove((char *)&rconn->tstamp, (char *)&bhp->bh_tstamp, 332 sizeof(struct timeval)); 333 memmove((char *)&rconn->rmp, (char *)bp + hdrlen, caplen); 334 } 335 bp += BPF_WORDALIGN(caplen + hdrlen); 336 return(1); 337 } 338 #undef bhp 339 340 return(0); 341 } 342 343 /* 344 ** BpfWrite -- Write packet to BPF device. 345 ** 346 ** Parameters: 347 ** rconn - packet to send. 348 ** 349 ** Returns: 350 ** True if write succeeded, False otherwise. 351 ** 352 ** Side Effects: 353 ** None. 354 */ 355 int 356 BpfWrite(RMPCONN *rconn) 357 { 358 if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) { 359 syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn)); 360 return(0); 361 } 362 363 return(1); 364 } 365 366 /* 367 ** BpfClose -- Close a BPF device. 368 ** 369 ** Parameters: 370 ** None. 371 ** 372 ** Returns: 373 ** Nothing. 374 ** 375 ** Side Effects: 376 ** None. 377 */ 378 void 379 BpfClose(void) 380 { 381 struct ifreq ifr; 382 383 if (BpfPkt != NULL) { 384 free((char *)BpfPkt); 385 BpfPkt = NULL; 386 } 387 388 if (BpfFd == -1) 389 return; 390 391 #ifdef MSG_EOR 392 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2; 393 #endif 394 ifr.ifr_addr.sa_family = AF_UNSPEC; 395 memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0], RMP_ADDRLEN); 396 if (ioctl(BpfFd, SIOCDELMULTI, (caddr_t)&ifr) < 0) 397 (void) ioctl(BpfFd, BIOCPROMISC, (caddr_t)0); 398 399 (void) close(BpfFd); 400 BpfFd = -1; 401 } 402