3 * $Id: inet.c,v 1.7 2004/04/08 01:36:25 mdw Exp $
5 * Protocol specific definitions for IPv4 sockets
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
39 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <mLib/alloc.h>
48 #include <mLib/dstr.h>
49 #include <mLib/fdflags.h>
50 #include <mLib/report.h>
64 /*----- Data structures ---------------------------------------------------*/
66 typedef struct inet_addrx {
68 struct sockaddr_in sin;
71 typedef struct inet_opts {
76 typedef struct inet_srcopts {
82 typedef struct inet_targopts {
87 #define ADDRF_PRIVCONN 16u
89 static inet_srcopts inet_globalsrc =
90 { { { 0 }, { INADDR_ANY } }, 0, &inet_globalsrc.acl };
91 static inet_targopts inet_globaltarg =
92 { { { 0 }, { INADDR_ANY } } };
94 /*----- Protocol operations -----------------------------------------------*/
98 static addr *inet_read(scanner *sc, unsigned type)
100 inet_addrx *ia = xmalloc(sizeof(*ia));
102 ia->a.ops = &inet_ops;
103 ia->a.sz = sizeof(struct sockaddr_in);
104 memset(&ia->sin, 0, sizeof(ia->sin));
105 ia->sin.sin_family = AF_INET;
107 /* --- Read the host address part --- */
111 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "port") == 0)
113 ia->sin.sin_addr.s_addr = htonl(INADDR_ANY);
118 conf_name(sc, '.', &d);
119 if ((h = gethostbyname(d.buf)) == 0)
120 error(sc, "couldn't resolve Internet address `%s'", d.buf);
121 memcpy(&ia->sin.sin_addr, h->h_addr, sizeof(struct in_addr));
128 /* --- Read the port number --- */
133 if (sc->t != CTOK_WORD)
134 error(sc, "parse error, TCP port expected");
135 if (isdigit((unsigned char)sc->d.buf[0]))
136 ia->sin.sin_port = htons(atoi(sc->d.buf));
137 else if ((s = getservbyname(sc->d.buf, "tcp")) == 0)
138 error(sc, "unknown tcp service `%s'", sc->d.buf);
140 ia->sin.sin_port = s->s_port;
147 /* --- @destroy@ --- */
149 static void inet_destroy(addr *a)
151 inet_addrx *ia = (inet_addrx *)a;
155 /* --- @print@ --- */
157 static void inet_print(addr *a, unsigned type, dstr *d)
159 inet_addrx *ia = (inet_addrx *)a;
162 dstr_putf(d, "inet:%u", (unsigned)ntohs(ia->sin.sin_port));
165 dstr_putf(d, "inet:%s:%u",
166 inet_ntoa(ia->sin.sin_addr),
167 (unsigned)ntohs(ia->sin.sin_port));
172 /* --- @initopts@ --- */
174 static addr_opts *inet_initsrcopts(void)
176 inet_srcopts *io = CREATE(inet_srcopts);
177 *io = inet_globalsrc;
179 io->acltail = &io->acl;
183 static addr_opts *inet_inittargopts(void)
185 inet_targopts *io = CREATE(inet_targopts);
186 *io = inet_globaltarg;
191 /* --- @option@ --- */
193 static void addropt(scanner *sc, inet_opts *io)
201 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "any") == 0)
202 io->bind.s_addr = INADDR_ANY;
204 conf_name(sc, '.', &d);
205 if ((h = gethostbyname(d.buf)) == 0)
206 error(sc, "couldn't resolve address `%s'", d.buf);
207 memcpy(&io->bind, h->h_addr, sizeof(struct in_addr));
211 static int srcopt(scanner *sc, addr_opts *ao)
213 inet_srcopts *io = (inet_srcopts *)ao;
216 CONF_BEGIN(sc, "source", "Internet socket source")
218 /* --- Initialization --- */
221 if (!inet_globalsrc.acltail)
222 inet_globalsrc.acltail = &inet_globalsrc.acl;
223 io = &inet_globalsrc;
226 /* --- Source address configuration --- */
228 if (strcmp(sc->d.buf, "addr") == 0) {
229 addropt(sc, &io->io);
233 /* --- Access control limitations --- */
235 if ((strcmp(sc->d.buf, "allow") == 0 && (act = ACL_ALLOW, 1)) ||
236 (strcmp(sc->d.buf, "deny") == 0 && (act = ACL_DENY, 1))) {
242 /* --- Find out what's going on --- */
245 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "from") == 0)
248 if (sc->t == CTOK_WORD && (strcmp(sc->d.buf, "priv") == 0 ||
249 strcmp(sc->d.buf, "priv-port") == 0)) {
250 acl_addpriv(&io->acltail, act);
253 if (sc->t == CTOK_WORD && strcmp(sc->d.buf, "host") == 0)
256 /* --- Find the host or network address --- */
258 conf_name(sc, '.', &d);
259 #ifdef HAVE_GETNETBYNAME
260 if ((n = getnetbyname(d.buf)) != 0)
261 a.s_addr = htonl(n->n_net);
264 if ((h = gethostbyname(d.buf)) == 0)
265 error(sc, "couldn't resolve address `%s'", d.buf);
267 memcpy(&a, h->h_addr, sizeof(struct in_addr));
269 /* --- Find the netmask, if any --- */
276 conf_name(sc, '.', &d);
277 if (strchr(d.buf, '.') == 0) {
282 m.s_addr = htonl((~0ul << (32 - n)) & 0xffffffff);
284 #ifdef HAVE_INET_ATON
285 if (!inet_aton(d.buf, &m))
286 error(sc, "bad netmask `%s'", d.buf);
288 m.s_addr = inet_addr(d.buf);
294 /* --- Add the access control entry --- */
296 acl_addhost(&io->acltail, act, a, m);
301 /* --- Anything unrecognized --- */
306 static int targopt(scanner *sc, addr_opts *ao)
308 inet_targopts *io = (inet_targopts *)ao;
310 CONF_BEGIN(sc, "dest", "Internet socket target");
311 if (strcmp(sc->d.buf, "addr") == 0) {
312 addropt(sc, &io->io);
315 if (strcmp(sc->d.buf, "priv") == 0 ||
316 strcmp(sc->d.buf, "priv-port") == 0) {
318 if (sc->t == '=') token(sc);
319 if (conf_enum(sc, "no,yes", ENUM_ABBREV, "privileged connection status"))
320 io->io.ao.f |= ADDRF_PRIVCONN;
322 io->io.ao.f &= ~ADDRF_PRIVCONN;
328 static int inet_option(scanner *sc, addr_opts *ao, unsigned type)
330 CONF_BEGIN(sc, "inet", "Internet socket");
331 if (type != ADDR_DEST && srcopt(sc, ao))
333 if (type != ADDR_SRC && targopt(sc, ao))
338 /* --- @confirm@ --- */
340 static void inet_confirm(addr *a, unsigned type, addr_opts *ao)
342 inet_addrx *ia = (inet_addrx *)a;
346 inet_targopts *io = (inet_targopts *)ao;
347 if ((io->io.ao.f & ADDRF_PRIVCONN) &&
348 (io->ipriv = privconn_adddest(ia->sin.sin_addr,
349 ia->sin.sin_port)) < 0)
350 die(1, "couldn't add privileged connection target (too late)");
355 /* --- @freeopts@ --- */
357 static void inet_freesrcopts(addr_opts *ao)
359 inet_srcopts *io = (inet_srcopts *)ao;
364 static void inet_freetargopts(addr_opts *ao)
366 inet_targopts *io = (inet_targopts *)ao;
372 static int inet_bind(addr *a, addr_opts *ao)
374 inet_addrx *ia = (inet_addrx *)a;
375 inet_srcopts *io = (inet_srcopts *)ao;
376 struct sockaddr_in sin;
380 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
382 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
383 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
385 sin.sin_addr = io->io.bind;
386 if (bind(fd, (struct sockaddr *)&sin, sizeof(ia->sin)))
396 /* --- @accept@ --- */
398 static reffd *inet_accept(int fd, addr_opts *ao, const char *desc)
400 inet_srcopts *io = (inet_srcopts *)ao;
403 size_t lsinsz = sizeof(q.lsin), rsinsz = sizeof(q.rsin);
406 /* --- Accept the new connection --- */
408 if ((nfd = accept(fd, (struct sockaddr *)&q.rsin, &rsinsz)) < 0)
410 if (getsockname(nfd, (struct sockaddr *)&q.lsin, &lsinsz)) {
415 q.r = reffd_init(nfd);
417 /* --- Find out whether this connection is allowed --- */
419 if (!acl_check(io->acl, q.rsin.sin_addr, ntohs(q.rsin.sin_port), &act))
420 acl_check(inet_globalsrc.acl, q.rsin.sin_addr,
421 ntohs(q.rsin.sin_port), &act);
422 if (act != ACL_ALLOW) {
424 if (!(io->io.ao.f & ADDRF_NOLOG))
430 /* --- Everything seems to be OK --- */
433 if (!(io->io.ao.f & ADDRF_NOLOG))
438 /* --- @connect@ --- */
440 static int inet_connect(addr *a, addr_opts *ao, conn *c, endpt *e)
442 inet_addrx *ia = (inet_addrx *)a;
443 inet_targopts *io = (inet_targopts *)ao;
446 if (io->ipriv >= 0) {
447 return (privconn_connect(c, sel, io->ipriv, io->io.bind,
448 starget_connected, e));
450 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
452 if (io->io.bind.s_addr != INADDR_ANY) {
453 struct sockaddr_in sin;
454 memset(&sin, 0, sizeof(sin));
455 sin.sin_family = AF_INET;
456 sin.sin_addr = io->io.bind;
458 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
461 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
462 return (conn_init(c, sel, fd, (struct sockaddr *)&ia->sin, sizeof(ia->sin),
463 starget_connected, e));
470 /* --- Ops table --- */
472 addr_ops inet_ops = {
474 inet_read, inet_destroy, inet_print,
475 inet_initsrcopts, inet_option, inet_confirm, inet_freesrcopts,
476 inet_bind, 0, inet_accept,
477 inet_inittargopts, inet_freetargopts,
481 /*----- That's all, folks -------------------------------------------------*/