ipft_tx.c revision 153881
1/* $FreeBSD: head/contrib/ipfilter/lib/ipft_tx.c 153881 2005-12-30 11:52:26Z guido $ */ 2 3/* 4 * Copyright (C) 1995-2001 by Darren Reed. 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 * 8 * $Id: ipft_tx.c,v 1.15.2.6 2005/12/04 10:07:22 darrenr Exp $ 9 */ 10#if !defined(lint) 11static const char sccsid[] = "@(#)ipft_tx.c 1.7 6/5/96 (C) 1993 Darren Reed"; 12static const char rcsid[] = "@(#)$Id: ipft_tx.c,v 1.15.2.6 2005/12/04 10:07:22 darrenr Exp $"; 13#endif 14 15#include <ctype.h> 16 17#include "ipf.h" 18#include "ipt.h" 19 20#ifndef linux 21#include <netinet/ip_var.h> 22#endif 23#include <netinet/tcpip.h> 24 25 26extern int opts; 27 28static char *tx_proto = ""; 29 30static int text_open __P((char *)), text_close __P((void)); 31static int text_readip __P((char *, int, char **, int *)); 32static int parseline __P((char *, ip_t *, char **, int *)); 33 34static char myflagset[] = "FSRPAUEC"; 35static u_char myflags[] = { TH_FIN, TH_SYN, TH_RST, TH_PUSH, 36 TH_ACK, TH_URG, TH_ECN, TH_CWR }; 37 38struct ipread iptext = { text_open, text_close, text_readip, R_DO_CKSUM }; 39static FILE *tfp = NULL; 40static int tfd = -1; 41 42static u_32_t tx_hostnum __P((char *, int *)); 43static u_short tx_portnum __P((char *)); 44 45 46/* 47 * returns an ip address as a long var as a result of either a DNS lookup or 48 * straight inet_addr() call 49 */ 50static u_32_t tx_hostnum(host, resolved) 51char *host; 52int *resolved; 53{ 54 u_32_t ipa; 55 56 *resolved = 0; 57 if (!strcasecmp("any", host)) 58 return 0L; 59 if (ISDIGIT(*host)) 60 return inet_addr(host); 61 62 if (gethost(host, &ipa) == -1) { 63 *resolved = -1; 64 fprintf(stderr, "can't resolve hostname: %s\n", host); 65 return 0; 66 } 67 return ipa; 68} 69 70 71/* 72 * find the port number given by the name, either from getservbyname() or 73 * straight atoi() 74 */ 75static u_short tx_portnum(name) 76char *name; 77{ 78 struct servent *sp, *sp2; 79 u_short p1 = 0; 80 81 if (ISDIGIT(*name)) 82 return (u_short)atoi(name); 83 if (!tx_proto) 84 tx_proto = "tcp/udp"; 85 if (strcasecmp(tx_proto, "tcp/udp")) { 86 sp = getservbyname(name, tx_proto); 87 if (sp) 88 return ntohs(sp->s_port); 89 (void) fprintf(stderr, "unknown service \"%s\".\n", name); 90 return 0; 91 } 92 sp = getservbyname(name, "tcp"); 93 if (sp) 94 p1 = sp->s_port; 95 sp2 = getservbyname(name, "udp"); 96 if (!sp || !sp2) { 97 (void) fprintf(stderr, "unknown tcp/udp service \"%s\".\n", 98 name); 99 return 0; 100 } 101 if (p1 != sp2->s_port) { 102 (void) fprintf(stderr, "%s %d/tcp is a different port to ", 103 name, p1); 104 (void) fprintf(stderr, "%s %d/udp\n", name, sp->s_port); 105 return 0; 106 } 107 return ntohs(p1); 108} 109 110 111char *tx_icmptypes[] = { 112 "echorep", (char *)NULL, (char *)NULL, "unreach", "squench", 113 "redir", (char *)NULL, (char *)NULL, "echo", "routerad", 114 "routersol", "timex", "paramprob", "timest", "timestrep", 115 "inforeq", "inforep", "maskreq", "maskrep", "END" 116}; 117 118static int text_open(fname) 119char *fname; 120{ 121 if (tfp && tfd != -1) { 122 rewind(tfp); 123 return tfd; 124 } 125 126 if (!strcmp(fname, "-")) { 127 tfd = 0; 128 tfp = stdin; 129 } else { 130 tfd = open(fname, O_RDONLY); 131 if (tfd != -1) 132 tfp = fdopen(tfd, "r"); 133 } 134 return tfd; 135} 136 137 138static int text_close() 139{ 140 int cfd = tfd; 141 142 tfd = -1; 143 return close(cfd); 144} 145 146 147static int text_readip(buf, cnt, ifn, dir) 148char *buf, **ifn; 149int cnt, *dir; 150{ 151 register char *s; 152 char line[513]; 153 154 *ifn = NULL; 155 while (fgets(line, sizeof(line)-1, tfp)) { 156 if ((s = strchr(line, '\n'))) 157 *s = '\0'; 158 if ((s = strchr(line, '\r'))) 159 *s = '\0'; 160 if ((s = strchr(line, '#'))) 161 *s = '\0'; 162 if (!*line) 163 continue; 164 if ((opts & OPT_DEBUG) != 0) 165 printf("input: %s\n", line); 166 *ifn = NULL; 167 *dir = 0; 168 if (!parseline(line, (ip_t *)buf, ifn, dir)) 169#if 0 170 return sizeof(ip_t) + sizeof(tcphdr_t); 171#else 172 return sizeof(ip_t); 173#endif 174 } 175 if (feof(tfp)) 176 return 0; 177 return -1; 178} 179 180static int parseline(line, ip, ifn, out) 181char *line; 182ip_t *ip; 183char **ifn; 184int *out; 185{ 186 tcphdr_t th, *tcp = &th; 187 struct icmp icmp, *ic = &icmp; 188 char *cps[20], **cpp, c, ipopts[68]; 189 int i, r; 190 191 if (*ifn) 192 free(*ifn); 193 bzero((char *)ip, MAX(sizeof(*tcp), sizeof(*ic)) + sizeof(*ip)); 194 bzero((char *)tcp, sizeof(*tcp)); 195 bzero((char *)ic, sizeof(*ic)); 196 bzero(ipopts, sizeof(ipopts)); 197 IP_HL_A(ip, sizeof(*ip) >> 2); 198 IP_V_A(ip, IPVERSION); 199 for (i = 0, cps[0] = strtok(line, " \b\t\r\n"); cps[i] && i < 19; ) 200 cps[++i] = strtok(NULL, " \b\t\r\n"); 201 202 cpp = cps; 203 if (!*cpp) 204 return 1; 205 206 c = **cpp; 207 if (!ISALPHA(c) || (TOLOWER(c) != 'o' && TOLOWER(c) != 'i')) { 208 fprintf(stderr, "bad direction \"%s\"\n", *cpp); 209 return 1; 210 } 211 *out = (TOLOWER(c) == 'o') ? 1 : 0; 212 cpp++; 213 if (!*cpp) 214 return 1; 215 216 if (!strcasecmp(*cpp, "on")) { 217 cpp++; 218 if (!*cpp) 219 return 1; 220 *ifn = strdup(*cpp++); 221 if (!*cpp) 222 return 1; 223 } 224 225 c = **cpp; 226 ip->ip_len = sizeof(ip_t); 227 if (!strcasecmp(*cpp, "tcp") || !strcasecmp(*cpp, "udp") || 228 !strcasecmp(*cpp, "icmp")) { 229 if (c == 't') { 230 ip->ip_p = IPPROTO_TCP; 231 ip->ip_len += sizeof(struct tcphdr); 232 tx_proto = "tcp"; 233 } else if (c == 'u') { 234 ip->ip_p = IPPROTO_UDP; 235 ip->ip_len += sizeof(struct udphdr); 236 tx_proto = "udp"; 237 } else { 238 ip->ip_p = IPPROTO_ICMP; 239 ip->ip_len += ICMPERR_IPICMPHLEN; 240 tx_proto = "icmp"; 241 } 242 cpp++; 243 } else if (ISDIGIT(**cpp) && !index(*cpp, '.')) { 244 ip->ip_p = atoi(*cpp); 245 cpp++; 246 } else 247 ip->ip_p = IPPROTO_IP; 248 249 if (!*cpp) 250 return 1; 251 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) { 252 char *last; 253 254 last = strchr(*cpp, ','); 255 if (!last) { 256 fprintf(stderr, "tcp/udp with no source port\n"); 257 return 1; 258 } 259 *last++ = '\0'; 260 tcp->th_sport = htons(tx_portnum(last)); 261 if (ip->ip_p == IPPROTO_TCP) { 262 tcp->th_win = htons(4096); 263 TCP_OFF_A(tcp, sizeof(*tcp) >> 2); 264 } 265 } 266 ip->ip_src.s_addr = tx_hostnum(*cpp, &r); 267 cpp++; 268 if (!*cpp) 269 return 1; 270 271 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) { 272 char *last; 273 274 last = strchr(*cpp, ','); 275 if (!last) { 276 fprintf(stderr, "tcp/udp with no destination port\n"); 277 return 1; 278 } 279 *last++ = '\0'; 280 tcp->th_dport = htons(tx_portnum(last)); 281 } 282 ip->ip_dst.s_addr = tx_hostnum(*cpp, &r); 283 cpp++; 284 if (*cpp && ip->ip_p == IPPROTO_TCP) { 285 char *s, *t; 286 287 tcp->th_flags = 0; 288 for (s = *cpp; *s; s++) 289 if ((t = strchr(myflagset, *s))) 290 tcp->th_flags |= myflags[t - myflagset]; 291 if (tcp->th_flags) 292 cpp++; 293 if (tcp->th_flags == 0) 294 abort(); 295 if (tcp->th_flags & TH_URG) 296 tcp->th_urp = htons(1); 297 } else if (*cpp && ip->ip_p == IPPROTO_ICMP) { 298 extern char *tx_icmptypes[]; 299 char **s, *t; 300 int i; 301 302 t = strchr(*cpp, ','); 303 if (t != NULL) 304 *t = '\0'; 305 306 for (s = tx_icmptypes, i = 0; !*s || strcmp(*s, "END"); 307 s++, i++) { 308 if (*s && !strcasecmp(*cpp, *s)) { 309 ic->icmp_type = i; 310 if (t != NULL) 311 ic->icmp_code = atoi(t + 1); 312 cpp++; 313 break; 314 } 315 } 316 if (t != NULL) 317 *t = ','; 318 } 319 320 if (*cpp && !strcasecmp(*cpp, "opt")) { 321 u_long olen; 322 323 cpp++; 324 olen = buildopts(*cpp, ipopts, (IP_HL(ip) - 5) << 2); 325 if (olen) { 326 bcopy(ipopts, (char *)(ip + 1), olen); 327 IP_HL_A(ip, IP_HL(ip) + (olen >> 2)); 328 } 329 } 330 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) 331 bcopy((char *)tcp, ((char *)ip) + (IP_HL(ip) << 2), 332 sizeof(*tcp)); 333 else if (ip->ip_p == IPPROTO_ICMP) 334 bcopy((char *)ic, ((char *)ip) + (IP_HL(ip) << 2), 335 sizeof(*ic)); 336 ip->ip_len = htons(ip->ip_len); 337 return 0; 338} 339