xref: /freebsd/sbin/ipf/ipsend/sockraw.c (revision 41edb306f05651fcaf6c74f9e3557f59f80292e1)
1*41edb306SCy Schubert /*	$FreeBSD$	*/
2*41edb306SCy Schubert 
3*41edb306SCy Schubert /*
4*41edb306SCy Schubert  * (C)opyright 2000 Darren Reed.
5*41edb306SCy Schubert  *
6*41edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
7*41edb306SCy Schubert  *
8*41edb306SCy Schubert  * WARNING: Attempting to use this .c file on HP-UX 11.00 will cause the
9*41edb306SCy Schubert  *          system to crash.
10*41edb306SCy Schubert  */
11*41edb306SCy Schubert #include <sys/param.h>
12*41edb306SCy Schubert #include <sys/types.h>
13*41edb306SCy Schubert #include <sys/socket.h>
14*41edb306SCy Schubert #include <sys/ioctl.h>
15*41edb306SCy Schubert 
16*41edb306SCy Schubert #include <net/if.h>
17*41edb306SCy Schubert #include <netinet/in.h>
18*41edb306SCy Schubert #include <netinet/in_systm.h>
19*41edb306SCy Schubert #include <netinet/ip.h>
20*41edb306SCy Schubert #include <netinet/if_ether.h>
21*41edb306SCy Schubert #include <netinet/ip_var.h>
22*41edb306SCy Schubert #include <netinet/udp.h>
23*41edb306SCy Schubert #include <netinet/udp_var.h>
24*41edb306SCy Schubert #include <netinet/tcp.h>
25*41edb306SCy Schubert #include <stdio.h>
26*41edb306SCy Schubert #include <string.h>
27*41edb306SCy Schubert #include <unistd.h>
28*41edb306SCy Schubert #include <stdlib.h>
29*41edb306SCy Schubert #include <errno.h>
30*41edb306SCy Schubert #include "ipsend.h"
31*41edb306SCy Schubert 
32*41edb306SCy Schubert #if !defined(lint) && defined(LIBC_SCCS)
33*41edb306SCy Schubert static	char	sirix[] = "@(#)sirix.c	1.0 10/9/97 (C)1997 Marc Boucher";
34*41edb306SCy Schubert #endif
35*41edb306SCy Schubert 
36*41edb306SCy Schubert 
37*41edb306SCy Schubert int	initdevice(char *device, int tout)
38*41edb306SCy Schubert {
39*41edb306SCy Schubert 	struct sockaddr s;
40*41edb306SCy Schubert 	struct ifreq ifr;
41*41edb306SCy Schubert 	int fd;
42*41edb306SCy Schubert 
43*41edb306SCy Schubert 	memset(&ifr, 0, sizeof(ifr));
44*41edb306SCy Schubert 	strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name);
45*41edb306SCy Schubert 
46*41edb306SCy Schubert 	if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
47*41edb306SCy Schubert 	    {
48*41edb306SCy Schubert 		perror("socket(AF_INET, SOCK_RAW, IPPROTO_RAW)");
49*41edb306SCy Schubert 		return -1;
50*41edb306SCy Schubert 	    }
51*41edb306SCy Schubert 
52*41edb306SCy Schubert 	if (ioctl(fd, SIOCGIFADDR, &ifr) == -1)
53*41edb306SCy Schubert 	    {
54*41edb306SCy Schubert 		perror("ioctl SIOCGIFADDR");
55*41edb306SCy Schubert 		return -1;
56*41edb306SCy Schubert 	    }
57*41edb306SCy Schubert 
58*41edb306SCy Schubert 	bzero((char *)&s, sizeof(s));
59*41edb306SCy Schubert 	s.sa_family = AF_INET;
60*41edb306SCy Schubert 	bcopy(&ifr.ifr_addr, s.sa_data, 4);
61*41edb306SCy Schubert 	if (bind(fd, &s, sizeof(s)) == -1)
62*41edb306SCy Schubert 		perror("bind");
63*41edb306SCy Schubert 	return fd;
64*41edb306SCy Schubert }
65*41edb306SCy Schubert 
66*41edb306SCy Schubert 
67*41edb306SCy Schubert /*
68*41edb306SCy Schubert  * output an IP packet
69*41edb306SCy Schubert  */
70*41edb306SCy Schubert int	sendip(int fd, char *pkt, int len)
71*41edb306SCy Schubert {
72*41edb306SCy Schubert 	struct ether_header *eh;
73*41edb306SCy Schubert 	struct sockaddr_in sin;
74*41edb306SCy Schubert 
75*41edb306SCy Schubert 	eh = (struct ether_header *)pkt;
76*41edb306SCy Schubert 	bzero((char *)&sin, sizeof(sin));
77*41edb306SCy Schubert 	sin.sin_family = AF_INET;
78*41edb306SCy Schubert 	pkt += 14;
79*41edb306SCy Schubert 	len -= 14;
80*41edb306SCy Schubert 	bcopy(pkt + 12, (char *)&sin.sin_addr, 4);
81*41edb306SCy Schubert 
82*41edb306SCy Schubert 	if (sendto(fd, pkt, len, 0, &sin, sizeof(sin)) == -1)
83*41edb306SCy Schubert 	    {
84*41edb306SCy Schubert 		perror("send");
85*41edb306SCy Schubert 		return -1;
86*41edb306SCy Schubert 	    }
87*41edb306SCy Schubert 
88*41edb306SCy Schubert 	return len;
89*41edb306SCy Schubert }
90