xref: /freebsd/sbin/ipf/ipsend/sdlpi.c (revision 41edb306f05651fcaf6c74f9e3557f59f80292e1)
1*41edb306SCy Schubert /*	$FreeBSD$	*/
2*41edb306SCy Schubert 
3*41edb306SCy Schubert /*
4*41edb306SCy Schubert  * (C)opyright 1992-1998 Darren Reed. (from tcplog)
5*41edb306SCy Schubert  *
6*41edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
7*41edb306SCy Schubert  *
8*41edb306SCy Schubert  */
9*41edb306SCy Schubert 
10*41edb306SCy Schubert #include <stdio.h>
11*41edb306SCy Schubert #include <netdb.h>
12*41edb306SCy Schubert #include <ctype.h>
13*41edb306SCy Schubert #include <fcntl.h>
14*41edb306SCy Schubert #include <signal.h>
15*41edb306SCy Schubert #include <errno.h>
16*41edb306SCy Schubert #include <sys/types.h>
17*41edb306SCy Schubert #include <sys/time.h>
18*41edb306SCy Schubert #include <sys/timeb.h>
19*41edb306SCy Schubert #include <sys/socket.h>
20*41edb306SCy Schubert #include <sys/file.h>
21*41edb306SCy Schubert #include <sys/ioctl.h>
22*41edb306SCy Schubert #include <sys/stropts.h>
23*41edb306SCy Schubert 
24*41edb306SCy Schubert #ifdef sun
25*41edb306SCy Schubert # include <sys/pfmod.h>
26*41edb306SCy Schubert # include <sys/bufmod.h>
27*41edb306SCy Schubert #endif
28*41edb306SCy Schubert # include <sys/dlpi.h>
29*41edb306SCy Schubert 
30*41edb306SCy Schubert #include <net/if.h>
31*41edb306SCy Schubert #include <netinet/in.h>
32*41edb306SCy Schubert #include <netinet/in_systm.h>
33*41edb306SCy Schubert #include <netinet/ip.h>
34*41edb306SCy Schubert #include <netinet/if_ether.h>
35*41edb306SCy Schubert #include <netinet/ip_var.h>
36*41edb306SCy Schubert #include <netinet/udp.h>
37*41edb306SCy Schubert #include <netinet/udp_var.h>
38*41edb306SCy Schubert #include <netinet/tcp.h>
39*41edb306SCy Schubert 
40*41edb306SCy Schubert #include "ipsend.h"
41*41edb306SCy Schubert 
42*41edb306SCy Schubert #if !defined(lint)
43*41edb306SCy Schubert static const char sccsid[] = "@(#)sdlpi.c	1.3 10/30/95 (C)1995 Darren Reed";
44*41edb306SCy Schubert static const char rcsid[] = "@(#)$Id$";
45*41edb306SCy Schubert #endif
46*41edb306SCy Schubert 
47*41edb306SCy Schubert #define	CHUNKSIZE	8192
48*41edb306SCy Schubert #define BUFSPACE	(4*CHUNKSIZE)
49*41edb306SCy Schubert 
50*41edb306SCy Schubert 
51*41edb306SCy Schubert /*
52*41edb306SCy Schubert  * Be careful to only include those defined in the flags option for the
53*41edb306SCy Schubert  * interface are included in the header size.
54*41edb306SCy Schubert  */
55*41edb306SCy Schubert int	initdevice(device, tout)
56*41edb306SCy Schubert 	char	*device;
57*41edb306SCy Schubert 	int	tout;
58*41edb306SCy Schubert {
59*41edb306SCy Schubert 	char	devname[16], *s, buf[256];
60*41edb306SCy Schubert 	int	i, fd;
61*41edb306SCy Schubert 
62*41edb306SCy Schubert 	(void) strcpy(devname, "/dev/");
63*41edb306SCy Schubert 	(void) strncat(devname, device, sizeof(devname) - strlen(devname));
64*41edb306SCy Schubert 
65*41edb306SCy Schubert 	s = devname + 5;
66*41edb306SCy Schubert 	while (*s && !ISDIGIT(*s))
67*41edb306SCy Schubert 		s++;
68*41edb306SCy Schubert 	if (!*s)
69*41edb306SCy Schubert 	    {
70*41edb306SCy Schubert 		fprintf(stderr, "bad device name %s\n", devname);
71*41edb306SCy Schubert 		exit(-1);
72*41edb306SCy Schubert 	    }
73*41edb306SCy Schubert 	i = atoi(s);
74*41edb306SCy Schubert 	*s = '\0';
75*41edb306SCy Schubert 	/*
76*41edb306SCy Schubert 	 * For writing
77*41edb306SCy Schubert 	 */
78*41edb306SCy Schubert 	if ((fd = open(devname, O_RDWR)) < 0)
79*41edb306SCy Schubert 	    {
80*41edb306SCy Schubert 		fprintf(stderr, "O_RDWR(1) ");
81*41edb306SCy Schubert 		perror(devname);
82*41edb306SCy Schubert 		exit(-1);
83*41edb306SCy Schubert 	    }
84*41edb306SCy Schubert 
85*41edb306SCy Schubert 	if (dlattachreq(fd, i) == -1)
86*41edb306SCy Schubert 	    {
87*41edb306SCy Schubert 		fprintf(stderr, "dlattachreq: DLPI error\n");
88*41edb306SCy Schubert 		exit(-1);
89*41edb306SCy Schubert 	    }
90*41edb306SCy Schubert 	else if (dlokack(fd, buf) == -1)
91*41edb306SCy Schubert 	    {
92*41edb306SCy Schubert 		fprintf(stderr, "dlokack(attach): DLPI error\n");
93*41edb306SCy Schubert 		exit(-1);
94*41edb306SCy Schubert 	    }
95*41edb306SCy Schubert #ifdef DL_HP_RAWDLS
96*41edb306SCy Schubert 	if (dlpromisconreq(fd, DL_PROMISC_SAP) < 0)
97*41edb306SCy Schubert 	    {
98*41edb306SCy Schubert 		fprintf(stderr, "dlpromisconreq: DL_PROMISC_PHYS error\n");
99*41edb306SCy Schubert 		exit(-1);
100*41edb306SCy Schubert 	    }
101*41edb306SCy Schubert 	else if (dlokack(fd, buf) < 0)
102*41edb306SCy Schubert 	    {
103*41edb306SCy Schubert 		fprintf(stderr, "dlokack(promisc): DLPI error\n");
104*41edb306SCy Schubert 		exit(-1);
105*41edb306SCy Schubert 	    }
106*41edb306SCy Schubert 	/* 22 is INSAP as per the HP-UX DLPI Programmer's Guide */
107*41edb306SCy Schubert 
108*41edb306SCy Schubert 	dlbindreq(fd, 22, 1, DL_HP_RAWDLS, 0, 0);
109*41edb306SCy Schubert #else
110*41edb306SCy Schubert 	dlbindreq(fd, ETHERTYPE_IP, 0, DL_CLDLS, 0, 0);
111*41edb306SCy Schubert #endif
112*41edb306SCy Schubert 	dlbindack(fd, buf);
113*41edb306SCy Schubert 	/*
114*41edb306SCy Schubert 	 * write full headers
115*41edb306SCy Schubert 	 */
116*41edb306SCy Schubert #ifdef DLIOCRAW /* we require RAW DLPI mode, which is a Sun extension */
117*41edb306SCy Schubert 	if (strioctl(fd, DLIOCRAW, -1, 0, NULL) == -1)
118*41edb306SCy Schubert 	    {
119*41edb306SCy Schubert 		fprintf(stderr, "DLIOCRAW error\n");
120*41edb306SCy Schubert 		exit(-1);
121*41edb306SCy Schubert 	    }
122*41edb306SCy Schubert #endif
123*41edb306SCy Schubert 	return fd;
124*41edb306SCy Schubert }
125*41edb306SCy Schubert 
126*41edb306SCy Schubert 
127*41edb306SCy Schubert /*
128*41edb306SCy Schubert  * output an IP packet onto a fd opened for /dev/nit
129*41edb306SCy Schubert  */
130*41edb306SCy Schubert int	sendip(fd, pkt, len)
131*41edb306SCy Schubert 	int	fd, len;
132*41edb306SCy Schubert 	char	*pkt;
133*41edb306SCy Schubert {
134*41edb306SCy Schubert 	struct strbuf dbuf, *dp = &dbuf, *cp = NULL;
135*41edb306SCy Schubert 	int pri = 0;
136*41edb306SCy Schubert #ifdef DL_HP_RAWDLS
137*41edb306SCy Schubert 	struct strbuf cbuf;
138*41edb306SCy Schubert 	dl_hp_rawdata_req_t raw;
139*41edb306SCy Schubert 
140*41edb306SCy Schubert 	cp = &cbuf;
141*41edb306SCy Schubert 	raw.dl_primitive = DL_HP_RAWDATA_REQ;
142*41edb306SCy Schubert 	cp->len = sizeof(raw);
143*41edb306SCy Schubert 	cp->buf = (char *)&raw;
144*41edb306SCy Schubert 	cp->maxlen = cp->len;
145*41edb306SCy Schubert 	pri = MSG_HIPRI;
146*41edb306SCy Schubert #endif
147*41edb306SCy Schubert 	/*
148*41edb306SCy Schubert 	 * construct NIT STREAMS messages, first control then data.
149*41edb306SCy Schubert 	 */
150*41edb306SCy Schubert 	dp->buf = pkt;
151*41edb306SCy Schubert 	dp->len = len;
152*41edb306SCy Schubert 	dp->maxlen = dp->len;
153*41edb306SCy Schubert 
154*41edb306SCy Schubert 	if (putmsg(fd, cp, dp, pri) == -1)
155*41edb306SCy Schubert 	    {
156*41edb306SCy Schubert 		perror("putmsg");
157*41edb306SCy Schubert 		return -1;
158*41edb306SCy Schubert 	    }
159*41edb306SCy Schubert 	if (ioctl(fd, I_FLUSH, FLUSHW) == -1)
160*41edb306SCy Schubert 	    {
161*41edb306SCy Schubert 		perror("I_FLUSHW");
162*41edb306SCy Schubert 		return -1;
163*41edb306SCy Schubert 	    }
164*41edb306SCy Schubert 	return len;
165*41edb306SCy Schubert }
166*41edb306SCy Schubert 
167