xref: /freebsd/sbin/ipf/ipsend/sockraw.c (revision efeb8bffe34422937c7f8df836afb5b817366d16)
141edb306SCy Schubert /*	$FreeBSD$	*/
241edb306SCy Schubert 
341edb306SCy Schubert /*
441edb306SCy Schubert  * (C)opyright 2000 Darren Reed.
541edb306SCy Schubert  *
641edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
741edb306SCy Schubert  *
841edb306SCy Schubert  * WARNING: Attempting to use this .c file on HP-UX 11.00 will cause the
941edb306SCy Schubert  *          system to crash.
1041edb306SCy Schubert  */
1141edb306SCy Schubert #include <sys/param.h>
1241edb306SCy Schubert #include <sys/types.h>
1341edb306SCy Schubert #include <sys/socket.h>
1441edb306SCy Schubert #include <sys/ioctl.h>
1541edb306SCy Schubert 
1641edb306SCy Schubert #include <net/if.h>
1741edb306SCy Schubert #include <netinet/in.h>
1841edb306SCy Schubert #include <netinet/in_systm.h>
1941edb306SCy Schubert #include <netinet/ip.h>
2041edb306SCy Schubert #include <netinet/if_ether.h>
2141edb306SCy Schubert #include <netinet/ip_var.h>
2241edb306SCy Schubert #include <netinet/udp.h>
2341edb306SCy Schubert #include <netinet/udp_var.h>
2441edb306SCy Schubert #include <netinet/tcp.h>
2541edb306SCy Schubert #include <stdio.h>
2641edb306SCy Schubert #include <string.h>
2741edb306SCy Schubert #include <unistd.h>
2841edb306SCy Schubert #include <stdlib.h>
2941edb306SCy Schubert #include <errno.h>
3041edb306SCy Schubert #include "ipsend.h"
3141edb306SCy Schubert 
3241edb306SCy Schubert #if !defined(lint) && defined(LIBC_SCCS)
3341edb306SCy Schubert static	char	sirix[] = "@(#)sirix.c	1.0 10/9/97 (C)1997 Marc Boucher";
3441edb306SCy Schubert #endif
3541edb306SCy Schubert 
3641edb306SCy Schubert 
37*efeb8bffSCy Schubert int
38*efeb8bffSCy Schubert initdevice(char *device, int tout)
3941edb306SCy Schubert {
4041edb306SCy Schubert 	struct sockaddr s;
4141edb306SCy Schubert 	struct ifreq ifr;
4241edb306SCy Schubert 	int fd;
4341edb306SCy Schubert 
4441edb306SCy Schubert 	memset(&ifr, 0, sizeof(ifr));
4541edb306SCy Schubert 	strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name);
4641edb306SCy Schubert 
4741edb306SCy Schubert 	if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
4841edb306SCy Schubert 	    {
4941edb306SCy Schubert 		perror("socket(AF_INET, SOCK_RAW, IPPROTO_RAW)");
5041edb306SCy Schubert 		return -1;
5141edb306SCy Schubert 	    }
5241edb306SCy Schubert 
5341edb306SCy Schubert 	if (ioctl(fd, SIOCGIFADDR, &ifr) == -1)
5441edb306SCy Schubert 	    {
5541edb306SCy Schubert 		perror("ioctl SIOCGIFADDR");
5641edb306SCy Schubert 		return -1;
5741edb306SCy Schubert 	    }
5841edb306SCy Schubert 
5941edb306SCy Schubert 	bzero((char *)&s, sizeof(s));
6041edb306SCy Schubert 	s.sa_family = AF_INET;
6141edb306SCy Schubert 	bcopy(&ifr.ifr_addr, s.sa_data, 4);
6241edb306SCy Schubert 	if (bind(fd, &s, sizeof(s)) == -1)
6341edb306SCy Schubert 		perror("bind");
6441edb306SCy Schubert 	return fd;
6541edb306SCy Schubert }
6641edb306SCy Schubert 
6741edb306SCy Schubert 
6841edb306SCy Schubert /*
6941edb306SCy Schubert  * output an IP packet
7041edb306SCy Schubert  */
7141edb306SCy Schubert int	sendip(int fd, char *pkt, int len)
7241edb306SCy Schubert {
7341edb306SCy Schubert 	struct ether_header *eh;
7441edb306SCy Schubert 	struct sockaddr_in sin;
7541edb306SCy Schubert 
7641edb306SCy Schubert 	eh = (struct ether_header *)pkt;
7741edb306SCy Schubert 	bzero((char *)&sin, sizeof(sin));
7841edb306SCy Schubert 	sin.sin_family = AF_INET;
7941edb306SCy Schubert 	pkt += 14;
8041edb306SCy Schubert 	len -= 14;
8141edb306SCy Schubert 	bcopy(pkt + 12, (char *)&sin.sin_addr, 4);
8241edb306SCy Schubert 
8341edb306SCy Schubert 	if (sendto(fd, pkt, len, 0, &sin, sizeof(sin)) == -1)
8441edb306SCy Schubert 	    {
8541edb306SCy Schubert 		perror("send");
8641edb306SCy Schubert 		return -1;
8741edb306SCy Schubert 	    }
8841edb306SCy Schubert 
8941edb306SCy Schubert 	return len;
9041edb306SCy Schubert }
91