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