xref: /freebsd/sbin/ipf/ipsend/snit.c (revision 1323ec57)
1 /*	$FreeBSD$	*/
2 
3 /*
4  * (C)opyright 1992-1998 Darren Reed. (from tcplog)
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  */
9 
10 #include <stdio.h>
11 #include <netdb.h>
12 #include <ctype.h>
13 #include <signal.h>
14 #include <errno.h>
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <sys/timeb.h>
18 #include <sys/socket.h>
19 #include <sys/file.h>
20 #include <sys/ioctl.h>
21 #include <net/nit.h>
22 #include <sys/fcntlcom.h>
23 #include <sys/dir.h>
24 #include <net/nit_if.h>
25 #include <net/nit_pf.h>
26 #include <net/nit_buf.h>
27 #include <net/packetfilt.h>
28 #include <sys/stropts.h>
29 
30 #include <net/if.h>
31 #include <netinet/in.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/ip.h>
34 #include <netinet/if_ether.h>
35 #include <netinet/ip_var.h>
36 #include <netinet/udp.h>
37 #include <netinet/udp_var.h>
38 #include <netinet/tcp.h>
39 
40 #include "ipsend.h"
41 
42 #if !defined(lint)
43 static const char sccsid[] = "@(#)snit.c	1.5 1/11/96 (C)1995 Darren Reed";
44 static const char rcsid[] = "@(#)$Id$";
45 #endif
46 
47 #define	CHUNKSIZE	8192
48 #define BUFSPACE	(4*CHUNKSIZE)
49 
50 /*
51  * Be careful to only include those defined in the flags option for the
52  * interface are included in the header size.
53  */
54 #define BUFHDR_SIZE  (sizeof(struct nit_bufhdr))
55 #define NIT_HDRSIZE  (BUFHDR_SIZE)
56 
57 static	int	timeout;
58 
59 
60 int
61 initdevice(char *device, int tout)
62 {
63 	struct	strioctl si;
64 	struct	timeval to;
65 	struct	ifreq ifr;
66 	int	fd;
67 
68 	if ((fd = open("/dev/nit", O_RDWR)) < 0)
69 	    {
70 		perror("/dev/nit");
71 		exit(-1);
72 	    }
73 
74 	/*
75 	 * arrange to get messages from the NIT STREAM and use NIT_BUF option
76 	 */
77 	ioctl(fd, I_SRDOPT, (char*)RMSGD);
78 	ioctl(fd, I_PUSH, "nbuf");
79 
80 	/*
81 	 * set the timeout
82 	 */
83 	timeout = tout;
84 	si.ic_timout = 1;
85 	to.tv_sec = 1;
86 	to.tv_usec = 0;
87 	si.ic_cmd = NIOCSTIME;
88 	si.ic_len = sizeof(to);
89 	si.ic_dp = (char*)&to;
90 	if (ioctl(fd, I_STR, (char*)&si) == -1)
91 	    {
92 		perror("ioctl: NIT timeout");
93 		exit(-1);
94 	    }
95 
96 	/*
97 	 * request the interface
98 	 */
99 	strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
100 	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' ';
101 	si.ic_cmd = NIOCBIND;
102 	si.ic_len = sizeof(ifr);
103 	si.ic_dp = (char*)&ifr;
104 	if (ioctl(fd, I_STR, (char*)&si) == -1)
105 	    {
106 		perror(ifr.ifr_name);
107 		exit(1);
108 	    }
109 	return (fd);
110 }
111 
112 
113 /*
114  * output an IP packet onto a fd opened for /dev/nit
115  */
116 int
117 sendip(int fd, char *pkt, int len)
118 	int	fd, len;
119 	char	*pkt;
120 {
121 	struct	sockaddr sk, *sa = &sk;
122 	struct	strbuf	cbuf, *cp = &cbuf, dbuf, *dp = &dbuf;
123 
124 	/*
125 	 * For ethernet, need at least 802.3 header and IP header.
126 	 */
127 	if (len < (sizeof(sa->sa_data) + sizeof(struct ip)))
128 		return (-1);
129 	/*
130 	 * to avoid any output processing for IP, say we're not.
131 	 */
132 	sa->sa_family = AF_UNSPEC;
133 	bcopy(pkt, sa->sa_data, sizeof(sa->sa_data));
134 	pkt += sizeof(sa->sa_data);
135 	len -= sizeof(sa->sa_data);
136 
137 	/*
138 	 * construct NIT STREAMS messages, first control then data.
139 	 */
140 	cp->len = sizeof(*sa);
141 	cp->maxlen = sizeof(*sa);
142 	cp->buf = (char *)sa;
143 
144 	dp->buf = pkt;
145 	dp->len = len;
146 	dp->maxlen = dp->len;
147 
148 	if (putmsg(fd, cp, dp, 0) == -1)
149 	    {
150 		perror("putmsg");
151 		return (-1);
152 	    }
153 
154 	if (ioctl(fd, I_FLUSH, FLUSHW) == -1)
155 	    {
156 		perror("I_FLUSH");
157 		return (-1);
158 	    }
159 	return (len);
160 }
161