]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/lib/ipft_tx.c
This commit was generated by cvs2svn to compensate for changes in r163976,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / lib / ipft_tx.c
1 /*      $FreeBSD$       */
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.7 2005/12/18 14:53:39 darrenr Exp $
9  */
10 #if !defined(lint)
11 static const char sccsid[] = "@(#)ipft_tx.c     1.7 6/5/96 (C) 1993 Darren Reed";
12 static const char rcsid[] = "@(#)$Id: ipft_tx.c,v 1.15.2.7 2005/12/18 14:53:39 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
26 extern  int     opts;
27
28 static  char    *tx_proto = "";
29
30 static  int     text_open __P((char *)), text_close __P((void));
31 static  int     text_readip __P((char *, int, char **, int *));
32 static  int     parseline __P((char *, ip_t *, char **, int *));
33
34 static  char    myflagset[] = "FSRPAUEC";
35 static  u_char  myflags[] = { TH_FIN, TH_SYN, TH_RST, TH_PUSH,
36                                 TH_ACK, TH_URG, TH_ECN, TH_CWR };
37
38 struct  ipread  iptext = { text_open, text_close, text_readip, R_DO_CKSUM };
39 static  FILE    *tfp = NULL;
40 static  int     tfd = -1;
41
42 static  u_32_t  tx_hostnum __P((char *, int *));
43 static  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  */
50 static  u_32_t  tx_hostnum(host, resolved)
51 char    *host;
52 int     *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  */
75 static  u_short tx_portnum(name)
76 char    *name;
77 {
78         struct  servent *sp;
79
80         if (ISDIGIT(*name))
81                 return (u_short)atoi(name);
82         sp = getservbyname(name, tx_proto);
83         if (sp)
84                 return ntohs(sp->s_port);
85         (void) fprintf(stderr, "unknown service \"%s\".\n", name);
86         return 0;
87 }
88
89
90 char    *tx_icmptypes[] = {
91         "echorep", (char *)NULL, (char *)NULL, "unreach", "squench",
92         "redir", (char *)NULL, (char *)NULL, "echo", "routerad",
93         "routersol", "timex", "paramprob", "timest", "timestrep",
94         "inforeq", "inforep", "maskreq", "maskrep", "END"
95 };
96
97 static  int     text_open(fname)
98 char    *fname;
99 {
100         if (tfp && tfd != -1) {
101                 rewind(tfp);
102                 return tfd;
103         }
104
105         if (!strcmp(fname, "-")) {
106                 tfd = 0;
107                 tfp = stdin;
108         } else {
109                 tfd = open(fname, O_RDONLY);
110                 if (tfd != -1)
111                         tfp = fdopen(tfd, "r");
112         }
113         return tfd;
114 }
115
116
117 static  int     text_close()
118 {
119         int     cfd = tfd;
120
121         tfd = -1;
122         return close(cfd);
123 }
124
125
126 static  int     text_readip(buf, cnt, ifn, dir)
127 char    *buf, **ifn;
128 int     cnt, *dir;
129 {
130         register char *s;
131         char    line[513];
132
133         *ifn = NULL;
134         while (fgets(line, sizeof(line)-1, tfp)) {
135                 if ((s = strchr(line, '\n')))
136                         *s = '\0';
137                 if ((s = strchr(line, '\r')))
138                         *s = '\0';
139                 if ((s = strchr(line, '#')))
140                         *s = '\0';
141                 if (!*line)
142                         continue;
143                 if ((opts & OPT_DEBUG) != 0)
144                         printf("input: %s\n", line);
145                 *ifn = NULL;
146                 *dir = 0;
147                 if (!parseline(line, (ip_t *)buf, ifn, dir))
148 #if 0
149                         return sizeof(ip_t) + sizeof(tcphdr_t);
150 #else
151                         return sizeof(ip_t);
152 #endif
153         }
154         if (feof(tfp))
155                 return 0;
156         return -1;
157 }
158
159 static  int     parseline(line, ip, ifn, out)
160 char    *line;
161 ip_t    *ip;
162 char    **ifn;
163 int     *out;
164 {
165         tcphdr_t        th, *tcp = &th;
166         struct  icmp    icmp, *ic = &icmp;
167         char    *cps[20], **cpp, c, ipopts[68];
168         int     i, r;
169
170         if (*ifn)
171                 free(*ifn);
172         bzero((char *)ip, MAX(sizeof(*tcp), sizeof(*ic)) + sizeof(*ip));
173         bzero((char *)tcp, sizeof(*tcp));
174         bzero((char *)ic, sizeof(*ic));
175         bzero(ipopts, sizeof(ipopts));
176         IP_HL_A(ip, sizeof(*ip) >> 2);
177         IP_V_A(ip, IPVERSION);
178         for (i = 0, cps[0] = strtok(line, " \b\t\r\n"); cps[i] && i < 19; )
179                 cps[++i] = strtok(NULL, " \b\t\r\n");
180
181         cpp = cps;
182         if (!*cpp)
183                 return 1;
184
185         c = **cpp;
186         if (!ISALPHA(c) || (TOLOWER(c) != 'o' && TOLOWER(c) != 'i')) {
187                 fprintf(stderr, "bad direction \"%s\"\n", *cpp);
188                 return 1;
189         }
190         *out = (TOLOWER(c) == 'o') ? 1 : 0;
191         cpp++;
192         if (!*cpp)
193                 return 1;
194
195         if (!strcasecmp(*cpp, "on")) {
196                 cpp++;
197                 if (!*cpp)
198                         return 1;
199                 *ifn = strdup(*cpp++);
200                 if (!*cpp)
201                         return 1;
202         }
203
204         c = **cpp;
205         ip->ip_len = sizeof(ip_t);
206         if (!strcasecmp(*cpp, "tcp") || !strcasecmp(*cpp, "udp") ||
207             !strcasecmp(*cpp, "icmp")) {
208                 if (c == 't') {
209                         ip->ip_p = IPPROTO_TCP;
210                         ip->ip_len += sizeof(struct tcphdr);
211                         tx_proto = "tcp";
212                 } else if (c == 'u') {
213                         ip->ip_p = IPPROTO_UDP;
214                         ip->ip_len += sizeof(struct udphdr);
215                         tx_proto = "udp";
216                 } else {
217                         ip->ip_p = IPPROTO_ICMP;
218                         ip->ip_len += ICMPERR_IPICMPHLEN;
219                         tx_proto = "icmp";
220                 }
221                 cpp++;
222         } else if (ISDIGIT(**cpp) && !index(*cpp, '.')) {
223                 ip->ip_p = atoi(*cpp);
224                 cpp++;
225         } else
226                 ip->ip_p = IPPROTO_IP;
227
228         if (!*cpp)
229                 return 1;
230         if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) {
231                 char    *last;
232
233                 last = strchr(*cpp, ',');
234                 if (!last) {
235                         fprintf(stderr, "tcp/udp with no source port\n");
236                         return 1;
237                 }
238                 *last++ = '\0';
239                 tcp->th_sport = htons(tx_portnum(last));
240                 if (ip->ip_p == IPPROTO_TCP) {
241                         tcp->th_win = htons(4096);
242                         TCP_OFF_A(tcp, sizeof(*tcp) >> 2);
243                 }
244         }
245         ip->ip_src.s_addr = tx_hostnum(*cpp, &r);
246         cpp++;
247         if (!*cpp)
248                 return 1;
249
250         if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) {
251                 char    *last;
252
253                 last = strchr(*cpp, ',');
254                 if (!last) {
255                         fprintf(stderr, "tcp/udp with no destination port\n");
256                         return 1;
257                 }
258                 *last++ = '\0';
259                 tcp->th_dport = htons(tx_portnum(last));
260         }
261         ip->ip_dst.s_addr = tx_hostnum(*cpp, &r);
262         cpp++;
263         if (*cpp && ip->ip_p == IPPROTO_TCP) {
264                 char    *s, *t;
265
266                 tcp->th_flags = 0;
267                 for (s = *cpp; *s; s++)
268                         if ((t  = strchr(myflagset, *s)))
269                                 tcp->th_flags |= myflags[t - myflagset];
270                 if (tcp->th_flags)
271                         cpp++;
272                 if (tcp->th_flags == 0)
273                         abort();
274                 if (tcp->th_flags & TH_URG)
275                         tcp->th_urp = htons(1);
276         } else if (*cpp && ip->ip_p == IPPROTO_ICMP) {
277                 extern  char    *tx_icmptypes[];
278                 char    **s, *t;
279                 int     i;
280
281                 t = strchr(*cpp, ',');
282                 if (t != NULL)
283                         *t = '\0';
284
285                 for (s = tx_icmptypes, i = 0; !*s || strcmp(*s, "END");
286                      s++, i++) {
287                         if (*s && !strcasecmp(*cpp, *s)) {
288                                 ic->icmp_type = i;
289                                 if (t != NULL)
290                                         ic->icmp_code = atoi(t + 1);
291                                 cpp++;
292                                 break;
293                         }
294                 }
295                 if (t != NULL)
296                         *t = ',';
297         }
298
299         if (*cpp && !strcasecmp(*cpp, "opt")) {
300                 u_long  olen;
301
302                 cpp++;
303                 olen = buildopts(*cpp, ipopts, (IP_HL(ip) - 5) << 2);
304                 if (olen) {
305                         bcopy(ipopts, (char *)(ip + 1), olen);
306                         IP_HL_A(ip, IP_HL(ip) + (olen >> 2));
307                 }
308         }
309         if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
310                 bcopy((char *)tcp, ((char *)ip) + (IP_HL(ip) << 2),
311                         sizeof(*tcp));
312         else if (ip->ip_p == IPPROTO_ICMP)
313                 bcopy((char *)ic, ((char *)ip) + (IP_HL(ip) << 2),
314                         sizeof(*ic));
315         ip->ip_len = htons(ip->ip_len);
316         return 0;
317 }