X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=userv-utils.git;a=blobdiff_plain;f=ipif%2Fservice.c;h=b9e40e29fcdb3a2f26ccabf02122d49ab230d388;hp=b7706235d6227e6943b0cf4d596aabff1423e16f;hb=44a77f4851d3c819f9b364018a9695f332758a71;hpb=1e9634732a90ffe9bf3aeff4be92b5baa402599f diff --git a/ipif/service.c b/ipif/service.c index b770623..b9e40e2 100644 --- a/ipif/service.c +++ b/ipif/service.c @@ -1,35 +1,114 @@ /* - * userv service (or standalone program) - * for per-user IP subranges. + * userv service (or standalone program) for per-user IP subranges. + * + * When invoked appropriately, it creates a point-to-point network + * interface with specified parameters. It arranges for packets sent out + * via that interface by the kernel to appear on its own stdout in SLIP or + * CSLIP encoding, and packets injected into its own stdin to be given to + * the kernel as if received on that interface. Optionally, additional + * routes can be set up to arrange for traffic for other address ranges to + * be routed through the new interface. + * + * This is the service program, which is invoked as root from userv (or may + * be invoked firectly). + * + * Its arguments are supposed to be, in order, as follows: + * + * The first two arguments are usually supplied by the userv + * configuration. See the file `ipif/ipif' in the source tree, which + * is installed in /etc/userv/services.d/ipif by `make install': * - * This is invoked as root, directly from userv. - * Its arguments are supposed to be, in order: * - * Specifies address ranges and gids which own them. - * -- Indicates that the remaining arguments are user-supplied - * and therefore untrusted. + * + * Specifies address ranges and gids which own them. The default + * configuration supplies /etc/userv/ipif-networks, which is then read + * for a list of entries, one per line. + * + * -- + * Serves to separate the user-supplied and therefore untrusted + * arguments from the trusted first argument. + * + * The remaining arguments are supplied by the (untrusted) caller: + * * ,,, - * As for slattach. Supported protocols are slip, cslip, and - * adaptive. Alternatively, set to `debug' to print debugging - * info. is address of the interface on chiark; - * is the address of the point-to-point peer. + * + * As for slattach. The only supported protocol is slip. + * Alternatively, set to `debug' to print debugging info and + * exit. is address of the interface to be created + * on the local system; is the address of the + * point-to-point peer. They must be actual addresses (not + * hostnames). + * * /,/,... - * List of additional routes to add for this interface. - * May be the empty argument. * - * is either - * ,/[,] - * indicating that that gid may allocate addresses in - * the relevant subspace ( is ignored) - * or #... - * which is a comment - * or / or ./ or ../ - * which refers to a file which contains lines which - * are each - * or * - * which means that anything is permitted - * - * Should be run from userv with no-disconnect-hup. + * List of additional routes to add for this interface. routes will + * be set up on the local system arranging for packets for those + * networks to be sent via the created interface. must be an + * IPv4 address, and mask must be an integer (dotted-quad masks are + * not supported). If no additional routes are to be set up, use `-' + * or supply an empty argument. + * + * Each item - whether a line in a file such as + * /etc/userv/ipif-networks, or the single trusted argument supplied + * on the service program command line - is one of: + * + * / + * ./ + * ../ + * + * Reads a file which contains lines which are each + * items. + * + * ,[=][-|+]/(-|+/...)[,] + * + * Indicates that may allocate addresses in the relevant address + * range ( is ignored). must be numeric. To specify a + * single host address, you must specify a mask of /32. If `=' is + * specified then the specific subrange is only allowed for the local + * endpoint address, but not for remote addresses. + * + * More than one range may be given, with each range prefixed + * by + or -. In this case each address range in the rule will + * scanned in order, and the first range in the rule that matches + * any desired rule will count: if that first matching range is + * prefixed by `+' (or nothing) then the rule applies, if it + * is prefixed by `-' (or nothing matches), the rule does not. + * + * * + * Means that anything is to be permitted. This should not appear in + * /etc/userv/ipif-networks, as that would permit any user on the + * system to create any interfaces with any addresses and routes + * attached. It is provided so that root can usefully invoke the ipif + * service program directly (not via userv), without needing to set up + * permissions in /etc/userv/ipif-networks. + * + * #... + * + * Comment. Blank lines are also ignored. + * + * NB: Permission is granted if _any_ config entry matches the request. + * + * The service program should be run from userv with no-disconnect-hup. + */ +/* + * Copyright (C) 1999-2000,2003 Ian Jackson + * This file is part of ipif, part of userv-utils + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with userv-utils; if not, write to the Free Software + * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ */ #include @@ -40,21 +119,41 @@ #include #include #include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include +#include #define NARGS 4 -#define MAXEXROUTES 5 -#define ATXTLEN 12 +#define MAXEXROUTES 50 +#define ATXTLEN 16 static const unsigned long gidmaxval= (unsigned long)((gid_t)-2); -static const char *const protos_ok[]= { "slip", "cslip", "adaptive", 0 }; +static const char *const protos_ok[]= { "slip", 0 }; +static const int signals[]= { SIGHUP, SIGINT, SIGTERM, 0 }; static const char *configstr, *proto; static unsigned long localaddr, peeraddr, mtu; +static int localpming, peerpming; static int localallow, peerallow, allallow; static int nexroutes; static struct exroute { unsigned long prefix, mask; - int allow; + int allow, pming; char prefixtxt[ATXTLEN], masktxt[ATXTLEN]; } exroutes[MAXEXROUTES]; @@ -67,6 +166,16 @@ static struct pplace { int lineno; } *cpplace; + +static int tunfd; +static char *ifname; + + +static void terminate(int estatus) { + exit(estatus); +} + + static void fatal(const char *fmt, ...) __attribute__((format(printf,1,2))); static void fatal(const char *fmt, ...) { @@ -76,7 +185,7 @@ static void fatal(const char *fmt, ...) { fputs("userv-ipif service: fatal error: ",stderr); vfprintf(stderr, fmt, al); putc('\n',stderr); - exit(8); + terminate(8); } static void sysfatal(const char *fmt, ...) @@ -88,10 +197,10 @@ static void sysfatal(const char *fmt, ...) { e= errno; va_start(al,fmt); - fputs("userv-ipif service: fatal system error",stderr); + fputs("userv-ipif service: fatal system error: ",stderr); vfprintf(stderr, fmt, al); - fprintf(stderr,"%s\n", strerror(e)); - exit(12); + fprintf(stderr,": %s\n", strerror(e)); + terminate(12); } @@ -119,7 +228,7 @@ static void badusage(const char *fmt, ...) { cpp->filename, cpp->lineno); } } - exit(16); + terminate(16); } static char *ip2txt(unsigned long addr, char *buf) { @@ -143,13 +252,13 @@ static unsigned long eat_number(const char **argp, const char *what, char *ep; int endchar; - if (!*argp) { badusage("missing number %s\n",what); } + if (!*argp) { badusage("missing number %s",what); } rv= strtoul(*argp,&ep,0); if ((endchar= *ep)) { - if (!endchars) badusage("junk after number %s\n",what); + if (!endchars) badusage("junk after number %s",what); if (!strchr(endchars,endchar)) badusage("invalid character or delimiter `%c' in or after number, %s:" - " expected %s (or none?)\n", endchar,what,endchars); + " expected %s (or none?)", endchar,what,endchars); *argp= ep+1; } else { *argp= 0; @@ -160,12 +269,17 @@ static unsigned long eat_number(const char **argp, const char *what, return rv; } -static void addrnet_mustdiffer(const char *w1, unsigned long p1, unsigned long m1, - const char *w2, unsigned long p2, unsigned long m2) { +static int addrnet_overlap(unsigned long p1, unsigned long m1, + unsigned long p2, unsigned long m2) { unsigned long mask; mask= m1&m2; - if ((p1 & mask) != (p2 & mask)) return; + return (p1 & mask) == (p2 & mask); +} + +static void addrnet_mustdiffer(const char *w1, unsigned long p1, unsigned long m1, + const char *w2, unsigned long p2, unsigned long m2) { + if (!addrnet_overlap(p1,m1,p2,m2)) return; badusage("%s %08lx/%08lx overlaps/clashes with %s %08lx/%08lx", w1,p1,m1, w2,p2,m2); } @@ -199,8 +313,8 @@ static void eat_prefixmask(const char **argp, const char *what, sprintf(whatbuf,"%s length",what); len= eat_number(argp,whatbuf, 0,32, endchars,endchar_r); - mask= (~0UL << (32-len)); - if (prefix & ~mask) badusage("%s prefix %08lx not fully contained in mask %08lx\n", + mask= len ? (~0UL << (32-len)) : 0UL; + if (prefix & ~mask) badusage("%s prefix %08lx not fully contained in mask %08lx", what,prefix,mask); *prefix_r= prefix; if (mask_r) *mask_r= mask; @@ -211,32 +325,66 @@ static int addrnet_isin(unsigned long prefix, unsigned long mask, unsigned long mprefix, unsigned long mmask) { return !(~mask & mmask) && (prefix & mmask) == mprefix; } + +/* Totally hideous algorithm for parsing the config file lines. + * For each config file line, we first see if its gid applies. If not + * we skip it. Otherwise, we do + * permit_begin + * which sets pming to 1 + * for each range. pming may be 0 if we've determined that + * this line does not apply to . + * permit_range + * which calls permit_range_thing for each + * which checks to see if is inside the relevant + * range (for +) or overlaps it (for -) and updates + * allow and pming. + */ + +static void permit_begin(void) { + int i; + localpming= peerpming= 1; + for (i=0; i= sizeof(ifnamepat)); + strcpy(ifr.ifr_name, ifnamepat); + + tunfd= open("/dev/net/tun", O_RDWR); + if (!tunfd) sysfatal("open /dev/net/tun"); + + r= fcntl(tunfd, F_GETFD); + if (r==-1) sysfatal("fcntl(tunfd,F_GETFD)"); + r= fcntl(tunfd, F_SETFD, r|FD_CLOEXEC); + if (r==-1) sysfatal("fcntl(tunfd,F_SETFD,|FD_CLOEXEC)"); + + r= ioctl(tunfd, TUNSETIFF, (void*)&ifr); + if (r) sysfatal("ioctl TUNSETIFF"); + + /* ifr.ifr_name might not be null-terminated. crazy abi. */ + ifname= malloc(sizeof(ifr.ifr_name)+1); + if (!ifname) sysfatal("malloc for interface name"); + memcpy(ifname, ifr.ifr_name, sizeof(ifr.ifr_name)); + ifname[sizeof(ifr.ifr_name)]= 0; +} + +static void netconfigure(void) { + char mtutxt[100]; + int i; + + if (task("ifconfig")) { + sprintf(mtutxt,"%lu",mtu); + + execlp("ifconfig", "ifconfig", ifname, localtxt, + "netmask","255.255.255.255", "pointopoint",peertxt, "-broadcast", + "mtu",mtutxt, "up", (char*)0); + sysfatal("cannot exec ifconfig"); + } + + for (i=0; i=ip_end) break; + uint8_t c= *ip++; + if (c==SLIP_END) { + rx_packet(output_buf, op-output_buf); + op= output_buf; + eaten= ip - input_buf; + continue; + } + if (c==SLIP_ESC) { + if (ip>=ip_end) { /* rescan this when there's more */ ip--; break; } + c= *ip++; + if (c==SLIP_ESC_END) c=SLIP_END; + else if (c==SLIP_ESC_ESC) c=SLIP_ESC; + else fatal("unexpected byte 0%o after SLIP_ESC",c); + } + if (op == output_buf+mtu) + fatal("SLIP packet exceeds mtu"); + *op++= c; + } + + output_len= op - output_buf; + scanned= ip - input_buf; + + input_waiting -= eaten; + memmove(input_buf, input_buf+eaten, input_waiting); + scanned -= eaten; +} + +static void tx_packet(uint8_t *output_buf, const uint8_t *ip, int inlen) { + /* output_buf is passed as a parameter since it's in copydata's stack frame */ + assert(!output_waiting); + uint8_t *op= output_buf; + + *op++= SLIP_END; + while (inlen-- >0) { + uint8_t c= *ip++; + if (c==SLIP_END) { *op++= SLIP_ESC; *op++= SLIP_ESC_END; } + else if (c==SLIP_ESC) { *op++= SLIP_ESC; *op++= SLIP_ESC_ESC; } + else *op++= c; + } + *op++= SLIP_END; + assert(op <= output_buf + mtu*2+2); + + output_waiting= op - output_buf; +} + +static void copydata(void) __attribute__((noreturn)); +static void copydata(void) { + uint8_t output_buf[mtu*2+2]; + uint8_t input_buf[mtu*2+2]; + uint8_t rx_packet_buf[mtu]; + + int r, i; + + struct pollfd polls[3]; + memset(polls, 0, sizeof(polls)); + + polls[0].fd= 0; polls[0].events= POLLIN; + polls[1].fd= 1; + polls[2].fd= tunfd; + + /* We don't do flow control on input packets; instead, we just throw + * away ones which the kernel doesn't accept. So we always poll for + * those. + * + * Output packets we buffer, so we poll only as appropriate for those. + */ + + /* Start by transmitting one END byte to say we're ready. */ + output_buf[0]= SLIP_END; + output_waiting= 1; + + for (;;) { + if (output_waiting) { + r= write(1, output_buf, output_waiting); + if (r<0) { + if (errno==EINTR) continue; + if (errno!=EAGAIN) + sysfatal("error writing SLIP output (packets being received)"); + } else { + assert(r>0); + output_waiting -= r; + memmove(output_buf, output_buf+r, output_waiting); + } + } + if (output_waiting) { + polls[1].events |= POLLOUT; + polls[2].events &= ~POLLIN; + } else { + polls[1].events &= ~POLLOUT; + polls[2].events |= POLLIN; + } + r= poll(polls,3,-1); + + if (r<0) { + if (errno==EINTR) continue; + sysfatal("poll() failed"); + } + assert(r>0); /* we used an infinite timeout */ + + for (i=0; i0) { + input_waiting += r; + assert(input_waiting <= sizeof(input_buf)); + more_rx_data(input_buf, rx_packet_buf); + } else if (r==0) { + terminate(0); + } else { + if (!(errno==EINTR || errno==EAGAIN)) + sysfatal("error reading input SLIP data (packets to transmit)"); + } + } + + /* We handle what would be (polls[1].events & POLLOUT) above, + * unconditionally. That eliminates the need to poll in the usual case */ + + if (polls[2].events & POLLIN) { + uint8_t packet_buf[mtu]; + r= read(tunfd, packet_buf, mtu); + if (r>0) { + tx_packet(output_buf, packet_buf, r); + } else { + assert(r<0); + if (!(errno==EAGAIN || errno==EWOULDBLOCK)) + sysfatal("error reading packet (being transmitted) from tun"); + } + } + } +} + int main(int argc, const char *const *argv) { parseargs(argc,argv); pconfig(configstr,0); checkpermit(); if (!proto) dumpdebug(); - abort(); + createif(); + netconfigure(); + setnonblock(tunfd); + setnonblock(0); + setnonblock(1); + copydata(); }