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