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