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