1 /* $OpenBSD: pf.c,v 1.14 2006/04/20 08:52:52 maja 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef lint 32 static const char rcsid[] = 33 "$OpenBSD: pf.c,v 1.14 2006/04/20 08:52:52 maja Exp $"; 34 #endif 35 36 #include <stdio.h> 37 #include <unistd.h> 38 #include <sys/types.h> 39 #include <sys/time.h> 40 #include <sys/ioctl.h> 41 #include <sys/file.h> 42 #include <sys/socket.h> 43 #include <sys/uio.h> 44 #include <net/if.h> 45 46 #include <net/bpf.h> 47 48 #include <netinet/in.h> 49 #include <netinet/if_ether.h> 50 51 #include <netdb.h> 52 #include <ctype.h> 53 #include <string.h> 54 #include <err.h> 55 #include <errno.h> 56 57 #include <syslog.h> 58 59 #include "common/mopdef.h" 60 61 /* 62 * Variables 63 */ 64 65 extern int promisc; 66 67 /* 68 * Return information to device.c how to open device. 69 * In this case the driver can handle both Ethernet type II and 70 * IEEE 802.3 frames (SNAP) in a single pfOpen. 71 */ 72 /* ARGSUSED */ 73 int 74 pfTrans(char *interface) 75 { 76 return (TRANS_ETHER + TRANS_8023 + TRANS_AND); 77 } 78 79 /* 80 * Open and initialize packet filter. 81 */ 82 /* ARGSUSED */ 83 int 84 pfInit(char *interface, int mode, u_short protocol, int typ) 85 { 86 int fd; 87 int n = 0; 88 char device[sizeof "/dev/bpf000"]; 89 struct ifreq ifr; 90 u_int dlt; 91 int immediate; 92 93 static struct bpf_insn insns[] = { 94 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12), 95 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 4, 0), 96 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20), 97 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 0, 3), 98 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 14), 99 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xaaaa, 0, 1), 100 BPF_STMT(BPF_RET | BPF_K, 1520), 101 BPF_STMT(BPF_RET | BPF_K, 0), 102 }; 103 static struct bpf_program filter = { 104 sizeof insns / sizeof(insns[0]), 105 insns 106 }; 107 108 /* Go through all the minors and find one that isn't in use. */ 109 do { 110 snprintf(device, sizeof device, "/dev/bpf%d", n++); 111 fd = open(device, mode); 112 } while (fd < 0 && errno == EBUSY); 113 114 if (fd < 0) { 115 syslog(LOG_ERR,"pfInit: open bpf %m"); 116 return (-1); 117 } 118 119 /* Set immediate mode so packets are processed as they arrive. */ 120 immediate = 1; 121 if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) { 122 syslog(LOG_ERR,"pfInit: BIOCIMMEDIATE: %m"); 123 return (-1); 124 } 125 strncpy(ifr.ifr_name, interface, sizeof ifr.ifr_name); 126 if (ioctl(fd, BIOCSETIF, &ifr) < 0) { 127 syslog(LOG_ERR,"pfInit: BIOCSETIF: %m"); 128 return (-1); 129 } 130 /* Check that the data link layer is an Ethernet; this code won't work 131 * with anything else. */ 132 if (ioctl(fd, BIOCGDLT, &dlt) < 0) { 133 syslog(LOG_ERR,"pfInit: BIOCGDLT: %m"); 134 return (-1); 135 } 136 if (dlt != DLT_EN10MB) { 137 syslog(LOG_ERR,"pfInit: %s is not ethernet", device); 138 return (-1); 139 } 140 if (promisc) 141 /* Set promiscuous mode. */ 142 if (ioctl(fd, BIOCPROMISC, 0) < 0) { 143 syslog(LOG_ERR,"pfInit: BIOCPROMISC: %m"); 144 return (-1); 145 } 146 147 /* Set filter program. */ 148 insns[1].k = protocol; 149 insns[3].k = protocol; 150 151 if (ioctl(fd, BIOCSETF, &filter) < 0) { 152 syslog(LOG_ERR,"pfInit: BIOCSETF: %m"); 153 return (-1); 154 } 155 156 /* XXX set the same write filter (for protocol only) */ 157 if (ioctl(fd, BIOCSETWF, &filter) < 0) { 158 syslog(LOG_ERR,"pfInit: BIOCSETWF: %m"); 159 return (-1); 160 } 161 162 /* Lock the interface to prevent further changes */ 163 if (ioctl(fd, BIOCLOCK) < 0) { 164 syslog(LOG_ERR,"pfInit: BIOCLOCK: %m"); 165 return (-1); 166 } 167 168 return (fd); 169 } 170 171 /* 172 * Add a Multicast address to the interface 173 */ 174 /* ARGSUSED */ 175 int 176 pfAddMulti(int s, char *interface, char *addr) 177 { 178 struct ifreq ifr; 179 int fd; 180 181 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name) - 1); 182 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = 0; 183 184 ifr.ifr_addr.sa_family = AF_UNSPEC; 185 bcopy(addr, ifr.ifr_addr.sa_data, 6); 186 187 /* 188 * open a socket, temporarily, to use for SIOC* ioctls 189 */ 190 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 191 syslog(LOG_ERR, "pfAddMulti: socket: %m"); 192 return (-1); 193 } 194 if (ioctl(fd, SIOCADDMULTI, &ifr) < 0) { 195 syslog(LOG_ERR, "pfAddMulti: SIOCADDMULTI: %m"); 196 close(fd); 197 return (-1); 198 } 199 close(fd); 200 201 return (0); 202 } 203 204 /* 205 * Delete a Multicast address from the interface 206 */ 207 /* ARGSUSED */ 208 int 209 pfDelMulti(int s, char *interface, char *addr) 210 { 211 struct ifreq ifr; 212 int fd; 213 214 strncpy(ifr.ifr_name, interface, sizeof (ifr.ifr_name) - 1); 215 ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0; 216 217 ifr.ifr_addr.sa_family = AF_UNSPEC; 218 bcopy(addr, ifr.ifr_addr.sa_data, 6); 219 220 /* 221 * open a socket, temporarily, to use for SIOC* ioctls 222 * 223 */ 224 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 225 syslog(LOG_ERR, "pfDelMulti: socket: %m"); 226 return (-1); 227 } 228 if (ioctl(fd, SIOCDELMULTI, &ifr) < 0) { 229 syslog(LOG_ERR, "pfAddMulti: SIOCDELMULTI: %m"); 230 close(fd); 231 return (-1); 232 } 233 close(fd); 234 235 return (0); 236 } 237 238 /* 239 * read a packet 240 */ 241 int 242 pfRead(int fd, u_char *buf, int len) 243 { 244 return (read(fd, buf, len)); 245 } 246 247 /* 248 * write a packet 249 */ 250 int 251 pfWrite(int fd, u_char *buf, int len, int trans) 252 { 253 struct iovec iov[2]; 254 255 /* XXX */ 256 switch (trans) { 257 case TRANS_8023: 258 iov[0].iov_base = buf; 259 iov[0].iov_len = 22; 260 iov[1].iov_base = buf + 22; 261 iov[1].iov_len = len - 22; 262 break; 263 default: 264 iov[0].iov_base = buf; 265 iov[0].iov_len = 14; 266 iov[1].iov_base = buf + 14; 267 iov[1].iov_len = len - 14; 268 break; 269 } 270 271 if (writev(fd, iov, 2) == len) 272 return (len); 273 274 return (-1); 275 } 276 277