4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
11 static const char sccsid[] = "@(#)ipft_tx.c 1.7 6/5/96 (C) 1993 Darren Reed";
12 static const char rcsid[] = "@(#)$Id$";
22 static char *tx_proto = "";
24 static int text_open __P((char *)), text_close __P((void));
25 static int text_readip __P((mb_t *, char **, int *));
26 static int parseline __P((char *, ip_t *, char **, int *));
28 static char myflagset[] = "FSRPAUEC";
29 static u_char myflags[] = { TH_FIN, TH_SYN, TH_RST, TH_PUSH,
30 TH_ACK, TH_URG, TH_ECN, TH_CWR };
32 struct ipread iptext = { text_open, text_close, text_readip, R_DO_CKSUM };
33 static FILE *tfp = NULL;
36 static u_32_t tx_hostnum __P((char *, int *));
37 static u_short tx_portnum __P((char *));
40 int parseipv6 __P((char **, ip6_t *, char **, int *));
44 * returns an ip address as a long var as a result of either a DNS lookup or
45 * straight inet_addr() call
47 static u_32_t tx_hostnum(host, resolved)
54 if (!strcasecmp("any", host))
57 return inet_addr(host);
59 if (gethost(AF_INET, host, &ipa) == -1) {
61 fprintf(stderr, "can't resolve hostname: %s\n", host);
64 return ipa.in4.s_addr;
69 * find the port number given by the name, either from getservbyname() or
72 static u_short tx_portnum(name)
78 return (u_short)atoi(name);
79 sp = getservbyname(name, tx_proto);
81 return ntohs(sp->s_port);
82 (void) fprintf(stderr, "unknown service \"%s\".\n", name);
87 static int text_open(fname)
90 if (tfp && tfd != -1) {
95 if (!strcmp(fname, "-")) {
99 tfd = open(fname, O_RDONLY);
101 tfp = fdopen(tfd, "r");
107 static int text_close()
116 static int text_readip(mb, ifn, dir)
127 buf = (char *)mb->mb_buf;
128 cnt = sizeof(mb->mb_buf);
131 while (fgets(line, sizeof(line)-1, tfp)) {
132 if ((s = strchr(line, '\n')))
134 if ((s = strchr(line, '\r')))
136 if ((s = strchr(line, '#')))
140 if ((opts & OPT_DEBUG) != 0)
141 printf("input: %s\n", line);
144 if (!parseline(line, (ip_t *)buf, ifn, dir)) {
148 mb->mb_len = ntohs(((ip6_t *)ip)->ip6_plen) +
154 mb->mb_len = ntohs(ip->ip_len);
164 static int parseline(line, ip, ifn, out)
170 tcphdr_t th, *tcp = &th;
171 struct icmp icmp, *ic = &icmp;
172 char *cps[20], **cpp, c, ipopts[68];
177 bzero((char *)ip, MAX(sizeof(*tcp), sizeof(*ic)) + sizeof(*ip));
178 bzero((char *)tcp, sizeof(*tcp));
179 bzero((char *)ic, sizeof(*ic));
180 bzero(ipopts, sizeof(ipopts));
181 IP_HL_A(ip, sizeof(*ip) >> 2);
182 IP_V_A(ip, IPVERSION);
184 for (i = 0, cps[0] = strtok(line, " \b\t\r\n"); cps[i] && i < 19; )
185 cps[++i] = strtok(NULL, " \b\t\r\n");
192 if (!ISALPHA(c) || (TOLOWER(c) != 'o' && TOLOWER(c) != 'i')) {
193 fprintf(stderr, "bad direction \"%s\"\n", *cpp);
198 if (!strcasecmp(*cpp, "out6") || !strcasecmp(*cpp, "in6")) {
199 return parseipv6(cpp, (ip6_t *)ip, ifn, out);
203 *out = (TOLOWER(c) == 'o') ? 1 : 0;
208 if (!strcasecmp(*cpp, "on")) {
212 *ifn = strdup(*cpp++);
218 ip->ip_len = sizeof(ip_t);
219 if (!strcasecmp(*cpp, "tcp") || !strcasecmp(*cpp, "udp") ||
220 !strcasecmp(*cpp, "icmp")) {
222 ip->ip_p = IPPROTO_TCP;
223 ip->ip_len += sizeof(struct tcphdr);
225 } else if (c == 'u') {
226 ip->ip_p = IPPROTO_UDP;
227 ip->ip_len += sizeof(struct udphdr);
230 ip->ip_p = IPPROTO_ICMP;
231 ip->ip_len += ICMPERR_IPICMPHLEN;
235 } else if (ISDIGIT(**cpp) && !index(*cpp, '.')) {
236 ip->ip_p = atoi(*cpp);
239 ip->ip_p = IPPROTO_IP;
243 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) {
246 last = strchr(*cpp, ',');
248 fprintf(stderr, "tcp/udp with no source port\n");
252 tcp->th_sport = htons(tx_portnum(last));
253 if (ip->ip_p == IPPROTO_TCP) {
254 tcp->th_win = htons(4096);
255 TCP_OFF_A(tcp, sizeof(*tcp) >> 2);
258 ip->ip_src.s_addr = tx_hostnum(*cpp, &r);
263 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) {
266 last = strchr(*cpp, ',');
268 fprintf(stderr, "tcp/udp with no destination port\n");
272 tcp->th_dport = htons(tx_portnum(last));
274 ip->ip_dst.s_addr = tx_hostnum(*cpp, &r);
276 if (ip->ip_p == IPPROTO_TCP) {
281 for (s = *cpp; *s; s++)
282 if ((t = strchr(myflagset, *s)))
283 tcp->th_flags |= myflags[t-myflagset];
288 if (tcp->th_flags & TH_URG)
289 tcp->th_urp = htons(1);
291 if (*cpp && !strncasecmp(*cpp, "seq=", 4)) {
292 tcp->th_seq = htonl(atoi(*cpp + 4));
296 if (*cpp && !strncasecmp(*cpp, "ack=", 4)) {
297 tcp->th_ack = htonl(atoi(*cpp + 4));
300 } else if (*cpp && ip->ip_p == IPPROTO_ICMP) {
303 t = strchr(*cpp, ',');
307 ic->icmp_type = geticmptype(AF_INET, *cpp);
309 ic->icmp_code = atoi(t + 1);
312 if (ic->icmp_type == ICMP_ECHO ||
313 ic->icmp_type == ICMP_ECHOREPLY)
314 ic->icmp_id = htons(getpid());
319 if (*cpp && !strcasecmp(*cpp, "opt")) {
323 olen = buildopts(*cpp, ipopts, (IP_HL(ip) - 5) << 2);
325 bcopy(ipopts, (char *)(ip + 1), olen);
326 IP_HL_A(ip, IP_HL(ip) + (olen >> 2));
330 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
331 bcopy((char *)tcp, ((char *)ip) + (IP_HL(ip) << 2),
333 else if (ip->ip_p == IPPROTO_ICMP)
334 bcopy((char *)ic, ((char *)ip) + (IP_HL(ip) << 2),
336 ip->ip_len = htons(ip->ip_len);
342 int parseipv6(cpp, ip6, ifn, out)
348 tcphdr_t th, *tcp = &th;
349 struct icmp6_hdr icmp, *ic6 = &icmp;
351 bzero((char *)ip6, MAX(sizeof(*tcp), sizeof(*ic6)) + sizeof(*ip6));
352 bzero((char *)tcp, sizeof(*tcp));
353 bzero((char *)ic6, sizeof(*ic6));
356 *out = (**cpp == 'o') ? 1 : 0;
361 if (!strcasecmp(*cpp, "on")) {
365 *ifn = strdup(*cpp++);
370 if (!strcasecmp(*cpp, "tcp")) {
371 ip6->ip6_nxt = IPPROTO_TCP;
374 } else if (!strcasecmp(*cpp, "udp")) {
375 ip6->ip6_nxt = IPPROTO_UDP;
378 } else if (!strcasecmp(*cpp, "icmpv6")) {
379 ip6->ip6_nxt = IPPROTO_ICMPV6;
382 } else if (ISDIGIT(**cpp) && !index(*cpp, ':')) {
383 ip6->ip6_nxt = atoi(*cpp);
386 ip6->ip6_nxt = IPPROTO_IPV6;
391 switch (ip6->ip6_nxt)
394 ip6->ip6_plen = sizeof(struct tcphdr);
397 ip6->ip6_plen = sizeof(struct udphdr);
399 case IPPROTO_ICMPV6 :
400 ip6->ip6_plen = ICMP6ERR_IPICMPHLEN;
406 if (ip6->ip6_nxt == IPPROTO_TCP || ip6->ip6_nxt == IPPROTO_UDP) {
409 last = strchr(*cpp, ',');
411 fprintf(stderr, "tcp/udp with no source port\n");
415 tcp->th_sport = htons(tx_portnum(last));
416 if (ip6->ip6_nxt == IPPROTO_TCP) {
417 tcp->th_win = htons(4096);
418 TCP_OFF_A(tcp, sizeof(*tcp) >> 2);
422 if (inet_pton(AF_INET6, *cpp, &ip6->ip6_src) != 1) {
423 fprintf(stderr, "cannot parse source address '%s'\n", *cpp);
431 if (ip6->ip6_nxt == IPPROTO_TCP || ip6->ip6_nxt == IPPROTO_UDP) {
434 last = strchr(*cpp, ',');
436 fprintf(stderr, "tcp/udp with no destination port\n");
440 tcp->th_dport = htons(tx_portnum(last));
443 if (inet_pton(AF_INET6, *cpp, &ip6->ip6_dst) != 1) {
444 fprintf(stderr, "cannot parse destination address '%s'\n",
450 if (ip6->ip6_nxt == IPPROTO_TCP) {
455 for (s = *cpp; *s; s++)
456 if ((t = strchr(myflagset, *s)))
457 tcp->th_flags |= myflags[t-myflagset];
462 if (tcp->th_flags & TH_URG)
463 tcp->th_urp = htons(1);
465 if (*cpp && !strncasecmp(*cpp, "seq=", 4)) {
466 tcp->th_seq = htonl(atoi(*cpp + 4));
470 if (*cpp && !strncasecmp(*cpp, "ack=", 4)) {
471 tcp->th_ack = htonl(atoi(*cpp + 4));
474 } else if (*cpp && ip6->ip6_nxt == IPPROTO_ICMPV6) {
477 t = strchr(*cpp, ',');
481 ic6->icmp6_type = geticmptype(AF_INET6, *cpp);
483 ic6->icmp6_code = atoi(t + 1);
485 if (ic6->icmp6_type == ICMP6_ECHO_REQUEST ||
486 ic6->icmp6_type == ICMP6_ECHO_REPLY)
487 ic6->icmp6_id = htons(getpid());
493 if (ip6->ip6_nxt == IPPROTO_TCP || ip6->ip6_nxt == IPPROTO_UDP) {
494 bcopy((char *)tcp, (char *)ip6 + sizeof(*ip6),
496 } else if (ip6->ip6_nxt == IPPROTO_ICMPV6) {
497 bcopy((char *)ic6, (char *)ip6 + sizeof(*ip6),
502 * Because a length of 0 == jumbo gram...
504 if (ip6->ip6_plen == 0) {
507 ip6->ip6_plen = htons(ip6->ip6_plen);