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