1 /* $NetBSD: pf.c,v 1.9 2004/04/10 17:53:05 darrenr Exp $ */ 2 3 /* 4 * Copyright (c) 1993-95 Mats O Jansson. All rights reserved. 5 * Copyright (c) 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is partly derived from rarpd. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by Mats O Jansson. 21 * 4. The name of the author may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __RCSID("$NetBSD: pf.c,v 1.9 2004/04/10 17:53:05 darrenr Exp $"); 39 #endif 40 41 #include "os.h" 42 43 #include <sys/uio.h> 44 #include <net/bpf.h> 45 46 #include "mopdef.h" 47 #include "pf.h" 48 #include "log.h" 49 50 /* 51 * Variables 52 */ 53 54 extern int promisc; 55 56 /* 57 * Return information to device.c how to open device. 58 * In this case the driver can handle both Ethernet type II and 59 * IEEE 802.3 frames (SNAP) in a single pfOpen. 60 */ 61 62 int 63 pfTrans(interface) 64 char *interface; 65 { 66 return TRANS_ETHER+TRANS_8023+TRANS_AND; 67 } 68 69 /* 70 * Open and initialize packet filter. 71 */ 72 73 int 74 pfInit(interface, mode, protocol, typ) 75 char *interface; 76 u_short protocol; 77 int typ, mode; 78 { 79 int fd; 80 int n = 0; 81 char device[sizeof "/dev/bpf000"]; 82 struct ifreq ifr; 83 u_int dlt; 84 int immediate; 85 u_int bufsize; 86 87 static struct bpf_insn insns[] = { 88 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12), 89 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 4, 0), 90 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20), 91 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 0, 3), 92 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 14), 93 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xaaaa, 0, 1), 94 BPF_STMT(BPF_RET | BPF_K, 1520), 95 BPF_STMT(BPF_RET | BPF_K, 0), 96 }; 97 static struct bpf_program filter = { 98 sizeof insns / sizeof(insns[0]), 99 insns 100 }; 101 102 /* Go through all the minors and find one that isn't in use. */ 103 do { 104 (void) snprintf(device, sizeof(device), "/dev/bpf%d", n++); 105 fd = open(device, mode); 106 } while (fd < 0 && errno == EBUSY); 107 108 if (fd < 0) { 109 mopLogWarn("pfInit: open %s", device); 110 return(-1); 111 } 112 113 /* Set immediate mode so packets are processed as they arrive. */ 114 immediate = 1; 115 if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) { 116 mopLogWarn("pfInit: BIOCIMMEDIATE"); 117 return(-1); 118 } 119 bufsize = 32768; 120 if (ioctl(fd, BIOCSBLEN, &bufsize) < 0) { 121 mopLogWarn("pfInit: BIOCSBLEN(%d)", bufsize); 122 } 123 (void) strncpy(ifr.ifr_name, interface, sizeof ifr.ifr_name); 124 if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) { 125 mopLogWarn("pfInit: BIOCSETIF"); 126 return(-1); 127 } 128 /* Check that the data link layer is an Ethernet; this code won't work 129 * with anything else. */ 130 if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) { 131 mopLogWarn("pfInit: BIOCGDLT"); 132 return(-1); 133 } 134 if (dlt != DLT_EN10MB) { 135 mopLogWarnX("pfInit: %s is not ethernet", device); 136 return(-1); 137 } 138 if (promisc) { 139 /* Set promiscuous mode. */ 140 if (ioctl(fd, BIOCPROMISC, (caddr_t)0) < 0) { 141 mopLogWarn("pfInit: BIOCPROMISC"); 142 return(-1); 143 } 144 } 145 /* Set filter program. */ 146 insns[1].k = protocol; 147 insns[3].k = protocol; 148 149 if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) { 150 mopLogWarn("pfInit: BIOCSETF"); 151 return(-1); 152 } 153 return(fd); 154 } 155 156 /* 157 * Add a Multicast address to the interface 158 */ 159 160 int 161 pfAddMulti(s, interface, addr) 162 int s; 163 char *interface, *addr; 164 { 165 struct ifreq ifr; 166 int fd; 167 168 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); 169 170 ifr.ifr_addr.sa_family = AF_UNSPEC; 171 memmove(ifr.ifr_addr.sa_data, addr, 6); 172 173 /* 174 * open a socket, temporarily, to use for SIOC* ioctls 175 * 176 */ 177 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 178 mopLogWarn("pfAddMulti: socket"); 179 return(-1); 180 } 181 if (ioctl(fd, SIOCADDMULTI, (caddr_t)&ifr) < 0) { 182 mopLogWarn("pfAddMulti: SIOCADDMULTI"); 183 close(fd); 184 return(-1); 185 } 186 close(fd); 187 188 return(0); 189 } 190 191 /* 192 * Delete a Multicast address from the interface 193 */ 194 195 int 196 pfDelMulti(s, interface, addr) 197 int s; 198 char *interface, *addr; 199 { 200 struct ifreq ifr; 201 int fd; 202 203 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); 204 205 ifr.ifr_addr.sa_family = AF_UNSPEC; 206 memmove(ifr.ifr_addr.sa_data, addr, 6); 207 208 /* 209 * open a socket, temporarily, to use for SIOC* ioctls 210 * 211 */ 212 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 213 mopLogWarn("pfDelMulti: socket"); 214 return(-1); 215 } 216 if (ioctl(fd, SIOCDELMULTI, (caddr_t)&ifr) < 0) { 217 mopLogWarn("pfAddMulti: SIOCDELMULTI"); 218 close(fd); 219 return(-1); 220 } 221 close(fd); 222 223 return(0); 224 } 225 226 /* 227 * read a packet 228 */ 229 230 int 231 pfRead(fd, buf, len) 232 int fd, len; 233 u_char *buf; 234 { 235 return(read(fd, buf, len)); 236 } 237 238 /* 239 * write a packet 240 */ 241 242 int 243 pfWrite(fd, buf, len, trans) 244 int fd, len, trans; 245 u_char *buf; 246 { 247 248 struct iovec iov[2]; 249 250 switch (trans) { 251 case TRANS_8023: 252 iov[0].iov_base = (caddr_t)buf; 253 iov[0].iov_len = 22; 254 iov[1].iov_base = (caddr_t)buf+22; 255 iov[1].iov_len = len-22; 256 break; 257 default: 258 iov[0].iov_base = (caddr_t)buf; 259 iov[0].iov_len = 14; 260 iov[1].iov_base = (caddr_t)buf+14; 261 iov[1].iov_len = len-14; 262 break; 263 } 264 265 if (writev(fd, iov, 2) == len) 266 return(len); 267 268 return(-1); 269 } 270 271