xref: /freebsd/sbin/ipf/ipsend/sdlpi.c (revision efeb8bffe34422937c7f8df836afb5b817366d16)
141edb306SCy Schubert /*	$FreeBSD$	*/
241edb306SCy Schubert 
341edb306SCy Schubert /*
441edb306SCy Schubert  * (C)opyright 1992-1998 Darren Reed. (from tcplog)
541edb306SCy Schubert  *
641edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
741edb306SCy Schubert  *
841edb306SCy Schubert  */
941edb306SCy Schubert 
1041edb306SCy Schubert #include <stdio.h>
1141edb306SCy Schubert #include <netdb.h>
1241edb306SCy Schubert #include <ctype.h>
1341edb306SCy Schubert #include <fcntl.h>
1441edb306SCy Schubert #include <signal.h>
1541edb306SCy Schubert #include <errno.h>
1641edb306SCy Schubert #include <sys/types.h>
1741edb306SCy Schubert #include <sys/time.h>
1841edb306SCy Schubert #include <sys/timeb.h>
1941edb306SCy Schubert #include <sys/socket.h>
2041edb306SCy Schubert #include <sys/file.h>
2141edb306SCy Schubert #include <sys/ioctl.h>
2241edb306SCy Schubert #include <sys/stropts.h>
2341edb306SCy Schubert 
2441edb306SCy Schubert #ifdef sun
2541edb306SCy Schubert # include <sys/pfmod.h>
2641edb306SCy Schubert # include <sys/bufmod.h>
2741edb306SCy Schubert #endif
2841edb306SCy Schubert # include <sys/dlpi.h>
2941edb306SCy Schubert 
3041edb306SCy Schubert #include <net/if.h>
3141edb306SCy Schubert #include <netinet/in.h>
3241edb306SCy Schubert #include <netinet/in_systm.h>
3341edb306SCy Schubert #include <netinet/ip.h>
3441edb306SCy Schubert #include <netinet/if_ether.h>
3541edb306SCy Schubert #include <netinet/ip_var.h>
3641edb306SCy Schubert #include <netinet/udp.h>
3741edb306SCy Schubert #include <netinet/udp_var.h>
3841edb306SCy Schubert #include <netinet/tcp.h>
3941edb306SCy Schubert 
4041edb306SCy Schubert #include "ipsend.h"
4141edb306SCy Schubert 
4241edb306SCy Schubert #if !defined(lint)
4341edb306SCy Schubert static const char sccsid[] = "@(#)sdlpi.c	1.3 10/30/95 (C)1995 Darren Reed";
4441edb306SCy Schubert static const char rcsid[] = "@(#)$Id$";
4541edb306SCy Schubert #endif
4641edb306SCy Schubert 
4741edb306SCy Schubert #define	CHUNKSIZE	8192
4841edb306SCy Schubert #define BUFSPACE	(4*CHUNKSIZE)
4941edb306SCy Schubert 
5041edb306SCy Schubert 
5141edb306SCy Schubert /*
5241edb306SCy Schubert  * Be careful to only include those defined in the flags option for the
5341edb306SCy Schubert  * interface are included in the header size.
5441edb306SCy Schubert  */
55*efeb8bffSCy Schubert int
56*efeb8bffSCy Schubert initdevice(char *device, int tout)
5741edb306SCy Schubert {
5841edb306SCy Schubert 	char	devname[16], *s, buf[256];
5941edb306SCy Schubert 	int	i, fd;
6041edb306SCy Schubert 
6141edb306SCy Schubert 	(void) strcpy(devname, "/dev/");
6241edb306SCy Schubert 	(void) strncat(devname, device, sizeof(devname) - strlen(devname));
6341edb306SCy Schubert 
6441edb306SCy Schubert 	s = devname + 5;
6541edb306SCy Schubert 	while (*s && !ISDIGIT(*s))
6641edb306SCy Schubert 		s++;
6741edb306SCy Schubert 	if (!*s)
6841edb306SCy Schubert 	    {
6941edb306SCy Schubert 		fprintf(stderr, "bad device name %s\n", devname);
7041edb306SCy Schubert 		exit(-1);
7141edb306SCy Schubert 	    }
7241edb306SCy Schubert 	i = atoi(s);
7341edb306SCy Schubert 	*s = '\0';
7441edb306SCy Schubert 	/*
7541edb306SCy Schubert 	 * For writing
7641edb306SCy Schubert 	 */
7741edb306SCy Schubert 	if ((fd = open(devname, O_RDWR)) < 0)
7841edb306SCy Schubert 	    {
7941edb306SCy Schubert 		fprintf(stderr, "O_RDWR(1) ");
8041edb306SCy Schubert 		perror(devname);
8141edb306SCy Schubert 		exit(-1);
8241edb306SCy Schubert 	    }
8341edb306SCy Schubert 
8441edb306SCy Schubert 	if (dlattachreq(fd, i) == -1)
8541edb306SCy Schubert 	    {
8641edb306SCy Schubert 		fprintf(stderr, "dlattachreq: DLPI error\n");
8741edb306SCy Schubert 		exit(-1);
8841edb306SCy Schubert 	    }
8941edb306SCy Schubert 	else if (dlokack(fd, buf) == -1)
9041edb306SCy Schubert 	    {
9141edb306SCy Schubert 		fprintf(stderr, "dlokack(attach): DLPI error\n");
9241edb306SCy Schubert 		exit(-1);
9341edb306SCy Schubert 	    }
9441edb306SCy Schubert #ifdef DL_HP_RAWDLS
9541edb306SCy Schubert 	if (dlpromisconreq(fd, DL_PROMISC_SAP) < 0)
9641edb306SCy Schubert 	    {
9741edb306SCy Schubert 		fprintf(stderr, "dlpromisconreq: DL_PROMISC_PHYS error\n");
9841edb306SCy Schubert 		exit(-1);
9941edb306SCy Schubert 	    }
10041edb306SCy Schubert 	else if (dlokack(fd, buf) < 0)
10141edb306SCy Schubert 	    {
10241edb306SCy Schubert 		fprintf(stderr, "dlokack(promisc): DLPI error\n");
10341edb306SCy Schubert 		exit(-1);
10441edb306SCy Schubert 	    }
10541edb306SCy Schubert 	/* 22 is INSAP as per the HP-UX DLPI Programmer's Guide */
10641edb306SCy Schubert 
10741edb306SCy Schubert 	dlbindreq(fd, 22, 1, DL_HP_RAWDLS, 0, 0);
10841edb306SCy Schubert #else
10941edb306SCy Schubert 	dlbindreq(fd, ETHERTYPE_IP, 0, DL_CLDLS, 0, 0);
11041edb306SCy Schubert #endif
11141edb306SCy Schubert 	dlbindack(fd, buf);
11241edb306SCy Schubert 	/*
11341edb306SCy Schubert 	 * write full headers
11441edb306SCy Schubert 	 */
11541edb306SCy Schubert #ifdef DLIOCRAW /* we require RAW DLPI mode, which is a Sun extension */
11641edb306SCy Schubert 	if (strioctl(fd, DLIOCRAW, -1, 0, NULL) == -1)
11741edb306SCy Schubert 	    {
11841edb306SCy Schubert 		fprintf(stderr, "DLIOCRAW error\n");
11941edb306SCy Schubert 		exit(-1);
12041edb306SCy Schubert 	    }
12141edb306SCy Schubert #endif
12241edb306SCy Schubert 	return fd;
12341edb306SCy Schubert }
12441edb306SCy Schubert 
12541edb306SCy Schubert 
12641edb306SCy Schubert /*
12741edb306SCy Schubert  * output an IP packet onto a fd opened for /dev/nit
12841edb306SCy Schubert  */
129*efeb8bffSCy Schubert int
130*efeb8bffSCy Schubert sendip(int fd, char *pkt, int len)
13141edb306SCy Schubert 	int	fd, len;
13241edb306SCy Schubert 	char	*pkt;
13341edb306SCy Schubert {
13441edb306SCy Schubert 	struct strbuf dbuf, *dp = &dbuf, *cp = NULL;
13541edb306SCy Schubert 	int pri = 0;
13641edb306SCy Schubert #ifdef DL_HP_RAWDLS
13741edb306SCy Schubert 	struct strbuf cbuf;
13841edb306SCy Schubert 	dl_hp_rawdata_req_t raw;
13941edb306SCy Schubert 
14041edb306SCy Schubert 	cp = &cbuf;
14141edb306SCy Schubert 	raw.dl_primitive = DL_HP_RAWDATA_REQ;
14241edb306SCy Schubert 	cp->len = sizeof(raw);
14341edb306SCy Schubert 	cp->buf = (char *)&raw;
14441edb306SCy Schubert 	cp->maxlen = cp->len;
14541edb306SCy Schubert 	pri = MSG_HIPRI;
14641edb306SCy Schubert #endif
14741edb306SCy Schubert 	/*
14841edb306SCy Schubert 	 * construct NIT STREAMS messages, first control then data.
14941edb306SCy Schubert 	 */
15041edb306SCy Schubert 	dp->buf = pkt;
15141edb306SCy Schubert 	dp->len = len;
15241edb306SCy Schubert 	dp->maxlen = dp->len;
15341edb306SCy Schubert 
15441edb306SCy Schubert 	if (putmsg(fd, cp, dp, pri) == -1)
15541edb306SCy Schubert 	    {
15641edb306SCy Schubert 		perror("putmsg");
15741edb306SCy Schubert 		return -1;
15841edb306SCy Schubert 	    }
15941edb306SCy Schubert 	if (ioctl(fd, I_FLUSH, FLUSHW) == -1)
16041edb306SCy Schubert 	    {
16141edb306SCy Schubert 		perror("I_FLUSHW");
16241edb306SCy Schubert 		return -1;
16341edb306SCy Schubert 	    }
16441edb306SCy Schubert 	return len;
16541edb306SCy Schubert }
16641edb306SCy Schubert 
167