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