1 /* $NetBSD: pf.c,v 1.8 2003/04/20 00:17:22 christos 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.8 2003/04/20 00:17:22 christos 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 86 static struct bpf_insn insns[] = { 87 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12), 88 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 4, 0), 89 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20), 90 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 0, 3), 91 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 14), 92 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xaaaa, 0, 1), 93 BPF_STMT(BPF_RET | BPF_K, 1520), 94 BPF_STMT(BPF_RET | BPF_K, 0), 95 }; 96 static struct bpf_program filter = { 97 sizeof insns / sizeof(insns[0]), 98 insns 99 }; 100 101 /* Go through all the minors and find one that isn't in use. */ 102 do { 103 (void) snprintf(device, sizeof(device), "/dev/bpf%d", n++); 104 fd = open(device, mode); 105 } while (fd < 0 && errno == EBUSY); 106 107 if (fd < 0) { 108 mopLogWarn("pfInit: open %s", device); 109 return(-1); 110 } 111 112 /* Set immediate mode so packets are processed as they arrive. */ 113 immediate = 1; 114 if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) { 115 mopLogWarn("pfInit: BIOCIMMEDIATE"); 116 return(-1); 117 } 118 (void) strncpy(ifr.ifr_name, interface, sizeof ifr.ifr_name); 119 if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) { 120 mopLogWarn("pfInit: BIOCSETIF"); 121 return(-1); 122 } 123 /* Check that the data link layer is an Ethernet; this code won't work 124 * with anything else. */ 125 if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) { 126 mopLogWarn("pfInit: BIOCGDLT"); 127 return(-1); 128 } 129 if (dlt != DLT_EN10MB) { 130 mopLogWarnX("pfInit: %s is not ethernet", device); 131 return(-1); 132 } 133 if (promisc) { 134 /* Set promiscuous mode. */ 135 if (ioctl(fd, BIOCPROMISC, (caddr_t)0) < 0) { 136 mopLogWarn("pfInit: BIOCPROMISC"); 137 return(-1); 138 } 139 } 140 /* Set filter program. */ 141 insns[1].k = protocol; 142 insns[3].k = protocol; 143 144 if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) { 145 mopLogWarn("pfInit: BIOCSETF"); 146 return(-1); 147 } 148 return(fd); 149 } 150 151 /* 152 * Add a Multicast address to the interface 153 */ 154 155 int 156 pfAddMulti(s, interface, addr) 157 int s; 158 char *interface, *addr; 159 { 160 struct ifreq ifr; 161 int fd; 162 163 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); 164 165 ifr.ifr_addr.sa_family = AF_UNSPEC; 166 memmove(ifr.ifr_addr.sa_data, addr, 6); 167 168 /* 169 * open a socket, temporarily, to use for SIOC* ioctls 170 * 171 */ 172 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 173 mopLogWarn("pfAddMulti: socket"); 174 return(-1); 175 } 176 if (ioctl(fd, SIOCADDMULTI, (caddr_t)&ifr) < 0) { 177 mopLogWarn("pfAddMulti: SIOCADDMULTI"); 178 close(fd); 179 return(-1); 180 } 181 close(fd); 182 183 return(0); 184 } 185 186 /* 187 * Delete a Multicast address from the interface 188 */ 189 190 int 191 pfDelMulti(s, interface, addr) 192 int s; 193 char *interface, *addr; 194 { 195 struct ifreq ifr; 196 int fd; 197 198 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); 199 200 ifr.ifr_addr.sa_family = AF_UNSPEC; 201 memmove(ifr.ifr_addr.sa_data, addr, 6); 202 203 /* 204 * open a socket, temporarily, to use for SIOC* ioctls 205 * 206 */ 207 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 208 mopLogWarn("pfDelMulti: socket"); 209 return(-1); 210 } 211 if (ioctl(fd, SIOCDELMULTI, (caddr_t)&ifr) < 0) { 212 mopLogWarn("pfAddMulti: SIOCDELMULTI"); 213 close(fd); 214 return(-1); 215 } 216 close(fd); 217 218 return(0); 219 } 220 221 /* 222 * read a packet 223 */ 224 225 int 226 pfRead(fd, buf, len) 227 int fd, len; 228 u_char *buf; 229 { 230 return(read(fd, buf, len)); 231 } 232 233 /* 234 * write a packet 235 */ 236 237 int 238 pfWrite(fd, buf, len, trans) 239 int fd, len, trans; 240 u_char *buf; 241 { 242 243 struct iovec iov[2]; 244 245 switch (trans) { 246 case TRANS_8023: 247 iov[0].iov_base = (caddr_t)buf; 248 iov[0].iov_len = 22; 249 iov[1].iov_base = (caddr_t)buf+22; 250 iov[1].iov_len = len-22; 251 break; 252 default: 253 iov[0].iov_base = (caddr_t)buf; 254 iov[0].iov_len = 14; 255 iov[1].iov_base = (caddr_t)buf+14; 256 iov[1].iov_len = len-14; 257 break; 258 } 259 260 if (writev(fd, iov, 2) == len) 261 return(len); 262 263 return(-1); 264 } 265 266