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