]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/ipfilter/ipsend/sbpf.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / ipfilter / ipsend / sbpf.c
1 /* $FreeBSD$ */
2 /*
3  * (C)opyright 1995-1998 Darren Reed. (from tcplog)
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  */
8 #include <sys/param.h>
9 #include <sys/types.h>
10 #include <sys/mbuf.h>
11 #include <sys/time.h>
12 #include <sys/socket.h>
13 #include <sys/file.h>
14 #include <sys/ioctl.h>
15 #if BSD < 199103
16 #include <sys/fcntlcom.h>
17 #endif
18 #if (__FreeBSD_version >= 300000)
19 # include <sys/dirent.h>
20 #else
21 # include <sys/dir.h>
22 #endif
23 #include <net/bpf.h>
24
25 #include <net/if.h>
26 #include <netinet/in.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
29 #include <netinet/ip_var.h>
30
31 #include <stdio.h>
32 #include <netdb.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #ifdef __NetBSD__
37 # include <paths.h>
38 #endif
39 #include <ctype.h>
40 #include <signal.h>
41 #include <errno.h>
42
43 #include "ipsend.h"
44
45 #if !defined(lint)
46 static const char sccsid[] = "@(#)sbpf.c        1.3 8/25/95 (C)1995 Darren Reed";
47 static const char rcsid[] = "@(#)$Id: sbpf.c,v 2.5.4.1 2006/03/21 16:32:58 darrenr Exp $";
48 #endif
49
50 /*
51  * the code herein is dervied from libpcap.
52  */
53 static  u_char  *buf = NULL;
54 static  int     bufsize = 0, timeout = 1;
55
56
57 int     initdevice(device, tout)
58 char    *device;
59 int     tout;
60 {
61         struct  bpf_version bv;
62         struct  timeval to;
63         struct  ifreq ifr;
64 #ifdef _PATH_BPF
65         char    *bpfname = _PATH_BPF;
66         int     fd;
67
68         if ((fd = open(bpfname, O_RDWR)) < 0)
69             {
70                 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
71                 return -1;
72             }
73 #else
74         char    bpfname[16];
75         int     fd = 0, i;
76
77         for (i = 0; i < 16; i++)
78             {
79                 (void) sprintf(bpfname, "/dev/bpf%d", i);
80                 if ((fd = open(bpfname, O_RDWR)) >= 0)
81                         break;
82             }
83         if (i == 16)
84             {
85                 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
86                 return -1;
87             }
88 #endif
89
90         if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
91             {
92                 perror("BIOCVERSION");
93                 return -1;
94             }
95         if (bv.bv_major != BPF_MAJOR_VERSION ||
96             bv.bv_minor < BPF_MINOR_VERSION)
97             {
98                 fprintf(stderr, "kernel bpf (v%d.%d) filter out of date:\n",
99                         bv.bv_major, bv.bv_minor);
100                 fprintf(stderr, "current version: %d.%d\n",
101                         BPF_MAJOR_VERSION, BPF_MINOR_VERSION);
102                 return -1;
103             }
104
105         (void) strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
106         if (ioctl(fd, BIOCSETIF, &ifr) == -1)
107             {
108                 fprintf(stderr, "%s(%d):", ifr.ifr_name, fd);
109                 perror("BIOCSETIF");
110                 exit(1);
111             }
112         /*
113          * get kernel buffer size
114          */
115         if (ioctl(fd, BIOCGBLEN, &bufsize) == -1)
116             {
117                 perror("BIOCSBLEN");
118                 exit(-1);
119             }
120         buf = (u_char*)malloc(bufsize);
121         /*
122          * set the timeout
123          */
124         timeout = tout;
125         to.tv_sec = 1;
126         to.tv_usec = 0;
127         if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&to) == -1)
128             {
129                 perror("BIOCSRTIMEOUT");
130                 exit(-1);
131             }
132
133         (void) ioctl(fd, BIOCFLUSH, 0);
134         return fd;
135 }
136
137
138 /*
139  * output an IP packet onto a fd opened for /dev/bpf
140  */
141 int     sendip(fd, pkt, len)
142 int     fd, len;
143 char    *pkt;
144 {                       
145         if (write(fd, pkt, len) == -1)
146             {
147                 perror("send");
148                 return -1;
149             }
150
151         return len;
152 }