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 <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 <net/nit.h> 22 #include <sys/fcntlcom.h> 23 #include <sys/dir.h> 24 #include <net/nit_if.h> 25 #include <net/nit_pf.h> 26 #include <net/nit_buf.h> 27 #include <net/packetfilt.h> 28 #include <sys/stropts.h> 29 30 #include <net/if.h> 31 #include <netinet/in.h> 32 #include <netinet/in_systm.h> 33 #include <netinet/ip.h> 34 #include <netinet/if_ether.h> 35 #include <netinet/ip_var.h> 36 #include <netinet/udp.h> 37 #include <netinet/udp_var.h> 38 #include <netinet/tcp.h> 39 40 #include "ipsend.h" 41 42 #if !defined(lint) 43 static const char sccsid[] = "@(#)snit.c 1.5 1/11/96 (C)1995 Darren Reed"; 44 static const char rcsid[] = "@(#)$Id$"; 45 #endif 46 47 #define CHUNKSIZE 8192 48 #define BUFSPACE (4*CHUNKSIZE) 49 50 /* 51 * Be careful to only include those defined in the flags option for the 52 * interface are included in the header size. 53 */ 54 #define BUFHDR_SIZE (sizeof(struct nit_bufhdr)) 55 #define NIT_HDRSIZE (BUFHDR_SIZE) 56 57 static int timeout; 58 59 60 int initdevice(device, tout) 61 char *device; 62 int tout; 63 { 64 struct strioctl si; 65 struct timeval to; 66 struct ifreq ifr; 67 int fd; 68 69 if ((fd = open("/dev/nit", O_RDWR)) < 0) 70 { 71 perror("/dev/nit"); 72 exit(-1); 73 } 74 75 /* 76 * arrange to get messages from the NIT STREAM and use NIT_BUF option 77 */ 78 ioctl(fd, I_SRDOPT, (char*)RMSGD); 79 ioctl(fd, I_PUSH, "nbuf"); 80 81 /* 82 * set the timeout 83 */ 84 timeout = tout; 85 si.ic_timout = 1; 86 to.tv_sec = 1; 87 to.tv_usec = 0; 88 si.ic_cmd = NIOCSTIME; 89 si.ic_len = sizeof(to); 90 si.ic_dp = (char*)&to; 91 if (ioctl(fd, I_STR, (char*)&si) == -1) 92 { 93 perror("ioctl: NIT timeout"); 94 exit(-1); 95 } 96 97 /* 98 * request the interface 99 */ 100 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 101 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' '; 102 si.ic_cmd = NIOCBIND; 103 si.ic_len = sizeof(ifr); 104 si.ic_dp = (char*)𝔦 105 if (ioctl(fd, I_STR, (char*)&si) == -1) 106 { 107 perror(ifr.ifr_name); 108 exit(1); 109 } 110 return fd; 111 } 112 113 114 /* 115 * output an IP packet onto a fd opened for /dev/nit 116 */ 117 int sendip(fd, pkt, len) 118 int fd, len; 119 char *pkt; 120 { 121 struct sockaddr sk, *sa = &sk; 122 struct strbuf cbuf, *cp = &cbuf, dbuf, *dp = &dbuf; 123 124 /* 125 * For ethernet, need at least 802.3 header and IP header. 126 */ 127 if (len < (sizeof(sa->sa_data) + sizeof(struct ip))) 128 return -1; 129 /* 130 * to avoid any output processing for IP, say we're not. 131 */ 132 sa->sa_family = AF_UNSPEC; 133 bcopy(pkt, sa->sa_data, sizeof(sa->sa_data)); 134 pkt += sizeof(sa->sa_data); 135 len -= sizeof(sa->sa_data); 136 137 /* 138 * construct NIT STREAMS messages, first control then data. 139 */ 140 cp->len = sizeof(*sa); 141 cp->maxlen = sizeof(*sa); 142 cp->buf = (char *)sa; 143 144 dp->buf = pkt; 145 dp->len = len; 146 dp->maxlen = dp->len; 147 148 if (putmsg(fd, cp, dp, 0) == -1) 149 { 150 perror("putmsg"); 151 return -1; 152 } 153 154 if (ioctl(fd, I_FLUSH, FLUSHW) == -1) 155 { 156 perror("I_FLUSH"); 157 return -1; 158 } 159 return len; 160 } 161