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