Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * resend.c (C) 1995-1998 Darren Reed |
| 3 | * |
Darren Reed | 9e45c8c | 2006-06-15 16:31:54 +0000 | [diff] [blame] | 4 | * See the IPFILTER.LICENCE file for details on licencing. |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 5 | * |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 6 | */ |
| 7 | #if !defined(lint) |
| 8 | static const char sccsid[] = "@(#)resend.c 1.3 1/11/96 (C)1995 Darren Reed"; |
| 9 | static const char rcsid[] = "@(#)$Id$"; |
| 10 | #endif |
Darren Reed | 9e45c8c | 2006-06-15 16:31:54 +0000 | [diff] [blame] | 11 | #include <sys/param.h> |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <sys/time.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <net/if.h> |
| 16 | #include <netinet/in.h> |
| 17 | #include <arpa/inet.h> |
| 18 | #include <netinet/in_systm.h> |
| 19 | #include <netinet/ip.h> |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 20 | #ifndef linux |
| 21 | # include <netinet/ip_var.h> |
| 22 | # include <netinet/if_ether.h> |
| 23 | # if __FreeBSD_version >= 300000 |
| 24 | # include <net/if_var.h> |
| 25 | # endif |
| 26 | #endif |
Darren Reed | 9e45c8c | 2006-06-15 16:31:54 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <netdb.h> |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 32 | #include "ipsend.h" |
| 33 | |
| 34 | extern int opts; |
| 35 | |
Darren Reed | c4af1f3 | 2007-08-20 10:15:33 +0000 | [diff] [blame] | 36 | void dumppacket __P((ip_t *)); |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 37 | |
| 38 | |
Darren Reed | c4af1f3 | 2007-08-20 10:15:33 +0000 | [diff] [blame] | 39 | void dumppacket(ip) |
Martti Kuparinen | 74b6c5c | 2007-10-25 12:55:40 +0000 | [diff] [blame] | 40 | ip_t *ip; |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 41 | { |
| 42 | tcphdr_t *t; |
| 43 | int i, j; |
| 44 | |
Darren Reed | 9e45c8c | 2006-06-15 16:31:54 +0000 | [diff] [blame] | 45 | t = (tcphdr_t *)((char *)ip + (IP_HL(ip) << 2)); |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 46 | if (ip->ip_tos) |
| 47 | printf("tos %#x ", ip->ip_tos); |
| 48 | if (ip->ip_off & 0x3fff) |
| 49 | printf("frag @%#x ", (ip->ip_off & 0x1fff) << 3); |
| 50 | printf("len %d id %d ", ip->ip_len, ip->ip_id); |
| 51 | printf("ttl %d p %d src %s", ip->ip_ttl, ip->ip_p, |
| 52 | inet_ntoa(ip->ip_src)); |
| 53 | if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) |
| 54 | printf(",%d", t->th_sport); |
| 55 | printf(" dst %s", inet_ntoa(ip->ip_dst)); |
| 56 | if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) |
| 57 | printf(",%d", t->th_dport); |
| 58 | if (ip->ip_p == IPPROTO_TCP) { |
| 59 | printf(" seq %lu:%lu flags ", |
| 60 | (u_long)t->th_seq, (u_long)t->th_ack); |
| 61 | for (j = 0, i = 1; i < 256; i *= 2, j++) |
| 62 | if (t->th_flags & i) |
| 63 | printf("%c", "FSRPAU--"[j]); |
| 64 | } |
| 65 | putchar('\n'); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | int ip_resend(dev, mtu, r, gwip, datain) |
Martti Kuparinen | 74b6c5c | 2007-10-25 12:55:40 +0000 | [diff] [blame] | 70 | char *dev; |
| 71 | int mtu; |
| 72 | struct in_addr gwip; |
| 73 | struct ipread *r; |
| 74 | char *datain; |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 75 | { |
| 76 | ether_header_t *eh; |
| 77 | char dhost[6]; |
| 78 | ip_t *ip; |
Darren Reed | 9e45c8c | 2006-06-15 16:31:54 +0000 | [diff] [blame] | 79 | int fd, wfd = initdevice(dev, 5), len, i; |
Darren Reed | 88151e3 | 2007-10-10 09:37:06 +0000 | [diff] [blame] | 80 | mb_t mb; |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 81 | |
Darren Reed | d4718fc | 2006-06-15 17:00:40 +0000 | [diff] [blame] | 82 | if (wfd == -1) |
| 83 | return -1; |
| 84 | |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 85 | if (datain) |
| 86 | fd = (*r->r_open)(datain); |
| 87 | else |
| 88 | fd = (*r->r_open)("-"); |
Darren Reed | 9e45c8c | 2006-06-15 16:31:54 +0000 | [diff] [blame] | 89 | |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 90 | if (fd < 0) |
| 91 | exit(-1); |
| 92 | |
Darren Reed | 88151e3 | 2007-10-10 09:37:06 +0000 | [diff] [blame] | 93 | ip = (struct ip *)mb.mb_buf; |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 94 | eh = (ether_header_t *)malloc(sizeof(*eh)); |
Darren Reed | 9e45c8c | 2006-06-15 16:31:54 +0000 | [diff] [blame] | 95 | if(!eh) |
| 96 | { |
| 97 | perror("malloc failed"); |
| 98 | return -2; |
| 99 | } |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 100 | |
| 101 | bzero((char *)A_A eh->ether_shost, sizeof(eh->ether_shost)); |
| 102 | if (gwip.s_addr && (arp((char *)&gwip, dhost) == -1)) |
| 103 | { |
| 104 | perror("arp"); |
Darren Reed | d4718fc | 2006-06-15 17:00:40 +0000 | [diff] [blame] | 105 | free(eh); |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 106 | return -2; |
| 107 | } |
| 108 | |
Darren Reed | 88151e3 | 2007-10-10 09:37:06 +0000 | [diff] [blame] | 109 | while ((i = (*r->r_readip)(&mb, NULL, NULL)) > 0) |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 110 | { |
| 111 | if (!(opts & OPT_RAW)) { |
| 112 | len = ntohs(ip->ip_len); |
| 113 | eh = (ether_header_t *)realloc((char *)eh, sizeof(*eh) + len); |
| 114 | eh->ether_type = htons((u_short)ETHERTYPE_IP); |
| 115 | if (!gwip.s_addr) { |
| 116 | if (arp((char *)&gwip, |
| 117 | (char *)A_A eh->ether_dhost) == -1) { |
| 118 | perror("arp"); |
| 119 | continue; |
| 120 | } |
| 121 | } else |
| 122 | bcopy(dhost, (char *)A_A eh->ether_dhost, |
| 123 | sizeof(dhost)); |
| 124 | if (!ip->ip_sum) |
| 125 | ip->ip_sum = chksum((u_short *)ip, |
Darren Reed | 9e45c8c | 2006-06-15 16:31:54 +0000 | [diff] [blame] | 126 | IP_HL(ip) << 2); |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 127 | bcopy(ip, (char *)(eh + 1), len); |
| 128 | len += sizeof(*eh); |
Darren Reed | c4af1f3 | 2007-08-20 10:15:33 +0000 | [diff] [blame] | 129 | dumppacket(ip); |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 130 | } else { |
Darren Reed | 88151e3 | 2007-10-10 09:37:06 +0000 | [diff] [blame] | 131 | eh = (ether_header_t *)mb.mb_buf; |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 132 | len = i; |
| 133 | } |
| 134 | |
| 135 | if (sendip(wfd, (char *)eh, len) == -1) |
| 136 | { |
| 137 | perror("send_packet"); |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | (*r->r_close)(); |
Darren Reed | d4718fc | 2006-06-15 17:00:40 +0000 | [diff] [blame] | 142 | free(eh); |
Darren Reed | bd81eb3 | 2006-06-15 16:06:55 +0000 | [diff] [blame] | 143 | return 0; |
| 144 | } |