xref: /freebsd/sbin/ipf/ipsend/snit.c (revision 7c43148a974877188a930e4078a164f83da8e652)
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 
42 #define	CHUNKSIZE	8192
43 #define BUFSPACE	(4*CHUNKSIZE)
44 
45 /*
46  * Be careful to only include those defined in the flags option for the
47  * interface are included in the header size.
48  */
49 #define BUFHDR_SIZE  (sizeof(struct nit_bufhdr))
50 #define NIT_HDRSIZE  (BUFHDR_SIZE)
51 
52 static	int	timeout;
53 
54 
55 int
56 initdevice(char *device, int tout)
57 {
58 	struct	strioctl si;
59 	struct	timeval to;
60 	struct	ifreq ifr;
61 	int	fd;
62 
63 	if ((fd = open("/dev/nit", O_RDWR)) < 0)
64 	    {
65 		perror("/dev/nit");
66 		exit(-1);
67 	    }
68 
69 	/*
70 	 * arrange to get messages from the NIT STREAM and use NIT_BUF option
71 	 */
72 	ioctl(fd, I_SRDOPT, (char*)RMSGD);
73 	ioctl(fd, I_PUSH, "nbuf");
74 
75 	/*
76 	 * set the timeout
77 	 */
78 	timeout = tout;
79 	si.ic_timout = 1;
80 	to.tv_sec = 1;
81 	to.tv_usec = 0;
82 	si.ic_cmd = NIOCSTIME;
83 	si.ic_len = sizeof(to);
84 	si.ic_dp = (char*)&to;
85 	if (ioctl(fd, I_STR, (char*)&si) == -1)
86 	    {
87 		perror("ioctl: NIT timeout");
88 		exit(-1);
89 	    }
90 
91 	/*
92 	 * request the interface
93 	 */
94 	strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
95 	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' ';
96 	si.ic_cmd = NIOCBIND;
97 	si.ic_len = sizeof(ifr);
98 	si.ic_dp = (char*)&ifr;
99 	if (ioctl(fd, I_STR, (char*)&si) == -1)
100 	    {
101 		perror(ifr.ifr_name);
102 		exit(1);
103 	    }
104 	return (fd);
105 }
106 
107 
108 /*
109  * output an IP packet onto a fd opened for /dev/nit
110  */
111 int
112 sendip(int fd, char *pkt, int len)
113 	int	fd, len;
114 	char	*pkt;
115 {
116 	struct	sockaddr sk, *sa = &sk;
117 	struct	strbuf	cbuf, *cp = &cbuf, dbuf, *dp = &dbuf;
118 
119 	/*
120 	 * For ethernet, need at least 802.3 header and IP header.
121 	 */
122 	if (len < (sizeof(sa->sa_data) + sizeof(struct ip)))
123 		return (-1);
124 	/*
125 	 * to avoid any output processing for IP, say we're not.
126 	 */
127 	sa->sa_family = AF_UNSPEC;
128 	bcopy(pkt, sa->sa_data, sizeof(sa->sa_data));
129 	pkt += sizeof(sa->sa_data);
130 	len -= sizeof(sa->sa_data);
131 
132 	/*
133 	 * construct NIT STREAMS messages, first control then data.
134 	 */
135 	cp->len = sizeof(*sa);
136 	cp->maxlen = sizeof(*sa);
137 	cp->buf = (char *)sa;
138 
139 	dp->buf = pkt;
140 	dp->len = len;
141 	dp->maxlen = dp->len;
142 
143 	if (putmsg(fd, cp, dp, 0) == -1)
144 	    {
145 		perror("putmsg");
146 		return (-1);
147 	    }
148 
149 	if (ioctl(fd, I_FLUSH, FLUSHW) == -1)
150 	    {
151 		perror("I_FLUSH");
152 		return (-1);
153 	    }
154 	return (len);
155 }
156