2 * Copyright (C) 1995-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 #if defined(__sgi) && (IRIX > 602)
7 # include <sys/ptimers.h>
13 #include <sys/types.h>
14 #if !defined(__SVR4) && !defined(__svr4__)
17 #include <sys/byteorder.h>
19 #include <sys/param.h>
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <netinet/in.h>
27 #include <netinet/in_systm.h>
29 #include <netinet/ip_var.h>
31 #include <netinet/ip.h>
32 #include <netinet/udp.h>
33 #include <netinet/tcp.h>
34 #include <netinet/ip_icmp.h>
35 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
40 #include "ip_compat.h"
41 #include <netinet/tcpip.h>
46 static const char sccsid
[] = "@(#)ipft_tx.c 1.7 6/5/96 (C) 1993 Darren Reed";
47 static const char rcsid
[] = "@(#)$Id: ipft_tx.c,v 2.3.2.8 2002/12/06 11:40:26 darrenr Exp $";
52 static char *tx_proto
= "";
54 static int text_open
__P((char *)), text_close
__P((void));
55 static int text_readip
__P((char *, int, char **, int *));
56 static int parseline
__P((char *, ip_t
*, char **, int *));
58 static char _tcp_flagset
[] = "FSRPAUEC";
59 static u_char _tcp_flags
[] = { TH_FIN
, TH_SYN
, TH_RST
, TH_PUSH
,
60 TH_ACK
, TH_URG
, TH_ECN
, TH_CWR
};
62 struct ipread iptext
= { text_open
, text_close
, text_readip
};
63 static FILE *tfp
= NULL
;
66 static u_32_t tx_hostnum
__P((char *, int *));
67 static u_short tx_portnum
__P((char *));
71 * returns an ip address as a long var as a result of either a DNS lookup or
72 * straight inet_addr() call
74 static u_32_t
tx_hostnum(host
, resolved
)
82 if (!strcasecmp("any",host
))
85 return inet_addr(host
);
87 if (!(hp
= gethostbyname(host
))) {
88 if (!(np
= getnetbyname(host
))) {
90 fprintf(stderr
, "can't resolve hostname: %s\n", host
);
93 return htonl(np
->n_net
);
95 return *(u_32_t
*)hp
->h_addr
;
100 * find the port number given by the name, either from getservbyname() or
103 static u_short
tx_portnum(name
)
106 struct servent
*sp
, *sp2
;
110 return (u_short
)atoi(name
);
112 tx_proto
= "tcp/udp";
113 if (strcasecmp(tx_proto
, "tcp/udp")) {
114 sp
= getservbyname(name
, tx_proto
);
116 return ntohs(sp
->s_port
);
117 (void) fprintf(stderr
, "unknown service \"%s\".\n", name
);
120 sp
= getservbyname(name
, "tcp");
123 sp2
= getservbyname(name
, "udp");
125 (void) fprintf(stderr
, "unknown tcp/udp service \"%s\".\n",
129 if (p1
!= sp2
->s_port
) {
130 (void) fprintf(stderr
, "%s %d/tcp is a different port to ",
132 (void) fprintf(stderr
, "%s %d/udp\n", name
, sp
->s_port
);
139 char *tx_icmptypes
[] = {
140 "echorep", (char *)NULL
, (char *)NULL
, "unreach", "squench",
141 "redir", (char *)NULL
, (char *)NULL
, "echo", "routerad",
142 "routersol", "timex", "paramprob", "timest", "timestrep",
143 "inforeq", "inforep", "maskreq", "maskrep", "END"
146 static int text_open(fname
)
149 if (tfp
&& tfd
!= -1) {
154 if (!strcmp(fname
, "-")) {
158 tfd
= open(fname
, O_RDONLY
);
160 tfp
= fdopen(tfd
, "r");
166 static int text_close()
175 static int text_readip(buf
, cnt
, ifn
, dir
)
183 while (fgets(line
, sizeof(line
)-1, tfp
)) {
184 if ((s
= index(line
, '\n')))
186 if ((s
= index(line
, '\r')))
188 if ((s
= index(line
, '#')))
192 if (!(opts
& OPT_BRIEF
))
193 printf("input: %s\n", line
);
196 if (!parseline(line
, (ip_t
*)buf
, ifn
, dir
))
198 return sizeof(ip_t
) + sizeof(tcphdr_t
);
206 static int parseline(line
, ip
, ifn
, out
)
212 tcphdr_t th
, *tcp
= &th
;
213 struct icmp icmp
, *ic
= &icmp
;
214 char *cps
[20], **cpp
, c
, ipopts
[68];
219 bzero((char *)ip
, MAX(sizeof(*tcp
), sizeof(*ic
)) + sizeof(*ip
));
220 bzero((char *)tcp
, sizeof(*tcp
));
221 bzero((char *)ic
, sizeof(*ic
));
222 bzero(ipopts
, sizeof(ipopts
));
223 ip
->ip_hl
= sizeof(*ip
) >> 2;
224 ip
->ip_v
= IPVERSION
;
225 for (i
= 0, cps
[0] = strtok(line
, " \b\t\r\n"); cps
[i
] && (i
< 19); )
226 cps
[++i
] = strtok(NULL
, " \b\t\r\n");
233 if (!isalpha(c
) || (tolower(c
) != 'o' && tolower(c
) != 'i')) {
234 fprintf(stderr
, "bad direction \"%s\"\n", *cpp
);
237 *out
= (tolower(c
) == 'o') ? 1 : 0;
242 if (!strcasecmp(*cpp
, "on")) {
246 *ifn
= strdup(*cpp
++);
252 ip
->ip_len
= sizeof(ip_t
);
253 if (!strcasecmp(*cpp
, "tcp") || !strcasecmp(*cpp
, "udp") ||
254 !strcasecmp(*cpp
, "icmp")) {
256 ip
->ip_p
= IPPROTO_TCP
;
257 ip
->ip_len
+= sizeof(struct tcphdr
);
259 } else if (c
== 'u') {
260 ip
->ip_p
= IPPROTO_UDP
;
261 ip
->ip_len
+= sizeof(struct udphdr
);
264 ip
->ip_p
= IPPROTO_ICMP
;
265 ip
->ip_len
+= ICMPERR_IPICMPHLEN
;
269 } else if (isdigit(**cpp
) && !index(*cpp
, '.')) {
270 ip
->ip_p
= atoi(*cpp
);
273 ip
->ip_p
= IPPROTO_IP
;
277 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_UDP
) {
280 last
= index(*cpp
, ',');
282 fprintf(stderr
, "tcp/udp with no source port\n");
286 tcp
->th_sport
= htons(tx_portnum(last
));
288 ip
->ip_src
.s_addr
= tx_hostnum(*cpp
, &r
);
293 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_UDP
) {
296 last
= index(*cpp
, ',');
298 fprintf(stderr
, "tcp/udp with no destination port\n");
302 tcp
->th_dport
= htons(tx_portnum(last
));
304 ip
->ip_dst
.s_addr
= tx_hostnum(*cpp
, &r
);
306 if (*cpp
&& ip
->ip_p
== IPPROTO_TCP
) {
307 extern char _tcp_flagset
[];
308 extern u_char _tcp_flags
[];
311 for (s
= *cpp
; *s
; s
++)
312 if ((t
= index(_tcp_flagset
, *s
)))
313 tcp
->th_flags
|= _tcp_flags
[t
- _tcp_flagset
];
316 assert(tcp
->th_flags
!= 0);
317 tcp
->th_win
= htons(4096);
318 tcp
->th_off
= sizeof(*tcp
) >> 2;
319 } else if (*cpp
&& ip
->ip_p
== IPPROTO_ICMP
) {
320 extern char *tx_icmptypes
[];
324 for (s
= tx_icmptypes
, i
= 0; !*s
|| strcmp(*s
, "END");
326 if (*s
&& !strncasecmp(*cpp
, *s
, strlen(*s
))) {
328 if ((t
= index(*cpp
, ',')))
329 ic
->icmp_code
= atoi(t
+1);
335 if (*cpp
&& !strcasecmp(*cpp
, "opt")) {
339 olen
= buildopts(*cpp
, ipopts
, (ip
->ip_hl
- 5) << 2);
341 bcopy(ipopts
, (char *)(ip
+ 1), olen
);
342 ip
->ip_hl
+= olen
>> 2;
345 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_UDP
)
346 bcopy((char *)tcp
, ((char *)ip
) + (ip
->ip_hl
<< 2),
348 else if (ip
->ip_p
== IPPROTO_ICMP
)
349 bcopy((char *)ic
, ((char *)ip
) + (ip
->ip_hl
<< 2),
351 ip
->ip_len
= htons(ip
->ip_len
);