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