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