1 /* $OpenBSD: pf.c,v 1.4 2000/02/20 17:45:33 bitblt 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 #ifndef LINT 37 static char rcsid[] = "$OpenBSD: pf.c,v 1.4 2000/02/20 17:45:33 bitblt Exp $"; 38 #endif 39 40 #include <stdio.h> 41 #include <unistd.h> 42 #include <sys/types.h> 43 #include <sys/time.h> 44 #include <sys/ioctl.h> 45 #include <sys/file.h> 46 #include <sys/socket.h> 47 #include <sys/uio.h> 48 #include <net/if.h> 49 50 #include <net/bpf.h> 51 #include <sys/errno.h> 52 53 #include <netinet/in.h> 54 #include <netinet/if_ether.h> 55 56 #include <netdb.h> 57 #include <ctype.h> 58 #include <string.h> 59 60 #include <syslog.h> 61 #include <varargs.h> 62 63 #include "common/mopdef.h" 64 65 /* 66 * Variables 67 */ 68 69 extern int errno; 70 extern int promisc; 71 72 /* 73 * Return information to device.c how to open device. 74 * In this case the driver can handle both Ethernet type II and 75 * IEEE 802.3 frames (SNAP) in a single pfOpen. 76 */ 77 78 int 79 pfTrans(interface) 80 char *interface; 81 { 82 return TRANS_ETHER+TRANS_8023+TRANS_AND; 83 } 84 85 /* 86 * Open and initialize packet filter. 87 */ 88 89 int 90 pfInit(interface, mode, protocol, typ) 91 char *interface; 92 u_short protocol; 93 int typ, mode; 94 { 95 int fd; 96 int n = 0; 97 char device[sizeof "/dev/bpf000"]; 98 struct ifreq ifr; 99 u_int dlt; 100 int immediate; 101 102 static struct bpf_insn insns[] = { 103 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12), 104 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 4, 0), 105 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20), 106 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 0, 3), 107 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 14), 108 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xaaaa, 0, 1), 109 BPF_STMT(BPF_RET | BPF_K, 1520), 110 BPF_STMT(BPF_RET | BPF_K, 0), 111 }; 112 static struct bpf_program filter = { 113 sizeof insns / sizeof(insns[0]), 114 insns 115 }; 116 117 /* Go through all the minors and find one that isn't in use. */ 118 do { 119 (void) sprintf(device, "/dev/bpf%d", n++); 120 fd = open(device, mode); 121 } while (fd < 0 && errno == EBUSY); 122 123 if (fd < 0) { 124 syslog(LOG_ERR,"pfInit: open bpf %m"); 125 return(-1); 126 } 127 128 /* Set immediate mode so packets are processed as they arrive. */ 129 immediate = 1; 130 if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) { 131 syslog(LOG_ERR,"pfInit: BIOCIMMEDIATE: %m"); 132 return(-1); 133 } 134 (void) strncpy(ifr.ifr_name, interface, sizeof ifr.ifr_name); 135 if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) { 136 syslog(LOG_ERR,"pfInit: BIOCSETIF: %m"); 137 return(-1); 138 } 139 /* Check that the data link layer is an Ethernet; this code won't work 140 * with anything else. */ 141 if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) { 142 syslog(LOG_ERR,"pfInit: BIOCGDLT: %m"); 143 return(-1); 144 } 145 if (dlt != DLT_EN10MB) { 146 syslog(LOG_ERR,"pfInit: %s is not ethernet", device); 147 return(-1); 148 } 149 if (promisc) { 150 /* Set promiscuous mode. */ 151 if (ioctl(fd, BIOCPROMISC, (caddr_t)0) < 0) { 152 syslog(LOG_ERR,"pfInit: BIOCPROMISC: %m"); 153 return(-1); 154 } 155 } 156 /* Set filter program. */ 157 insns[1].k = protocol; 158 insns[3].k = protocol; 159 160 if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) { 161 syslog(LOG_ERR,"pfInit: BIOCSETF: %m"); 162 return(-1); 163 } 164 return(fd); 165 } 166 167 /* 168 * Add a Multicast address to the interface 169 */ 170 171 int 172 pfAddMulti(s, interface, addr) 173 int s; 174 char *interface, *addr; 175 { 176 struct ifreq ifr; 177 int fd; 178 179 strncpy(ifr.ifr_name, interface,sizeof(ifr.ifr_name) - 1); 180 ifr.ifr_name[sizeof(ifr.ifr_name)] = 0; 181 182 ifr.ifr_addr.sa_family = AF_UNSPEC; 183 bcopy(addr, ifr.ifr_addr.sa_data, 6); 184 185 /* 186 * open a socket, temporarily, to use for SIOC* ioctls 187 * 188 */ 189 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 190 syslog(LOG_ERR, "pfAddMulti: socket: %m"); 191 return(-1); 192 } 193 if (ioctl(fd, SIOCADDMULTI, (caddr_t)&ifr) < 0) { 194 syslog(LOG_ERR, "pfAddMulti: SIOCADDMULTI: %m"); 195 close(fd); 196 return(-1); 197 } 198 close(fd); 199 200 return(0); 201 } 202 203 /* 204 * Delete a Multicast address from the interface 205 */ 206 207 int 208 pfDelMulti(s, interface, addr) 209 int s; 210 char *interface, *addr; 211 { 212 struct ifreq ifr; 213 int fd; 214 215 strncpy(ifr.ifr_name, interface, sizeof (ifr.ifr_name) - 1); 216 ifr.ifr_name[sizeof(ifr.ifr_name)] = 0; 217 218 ifr.ifr_addr.sa_family = AF_UNSPEC; 219 bcopy(addr, ifr.ifr_addr.sa_data, 6); 220 221 /* 222 * open a socket, temporarily, to use for SIOC* ioctls 223 * 224 */ 225 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 226 syslog(LOG_ERR, "pfDelMulti: socket: %m"); 227 return(-1); 228 } 229 if (ioctl(fd, SIOCDELMULTI, (caddr_t)&ifr) < 0) { 230 syslog(LOG_ERR, "pfAddMulti: SIOCDELMULTI: %m"); 231 close(fd); 232 return(-1); 233 } 234 close(fd); 235 236 return(0); 237 } 238 239 /* 240 * read a packet 241 */ 242 243 int 244 pfRead(fd, buf, len) 245 int fd, len; 246 u_char *buf; 247 { 248 return(read(fd, buf, len)); 249 } 250 251 /* 252 * write a packet 253 */ 254 255 int 256 pfWrite(fd, buf, len, trans) 257 int fd, len, trans; 258 u_char *buf; 259 { 260 261 struct iovec iov[2]; 262 263 switch (trans) { 264 case TRANS_8023: 265 iov[0].iov_base = (caddr_t)buf; 266 iov[0].iov_len = 22; 267 iov[1].iov_base = (caddr_t)buf+22; 268 iov[1].iov_len = len-22; 269 break; 270 default: 271 iov[0].iov_base = (caddr_t)buf; 272 iov[0].iov_len = 14; 273 iov[1].iov_base = (caddr_t)buf+14; 274 iov[1].iov_len = len-14; 275 break; 276 } 277 278 if (writev(fd, iov, 2) == len) 279 return(len); 280 281 return(-1); 282 } 283 284