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