14 MaxWeight = 0x7fffffff,
19 EtherType = 2*Eaddrlen,
20 EtherHdr = EtherType+2,
27 /* worst case: UDPv6 over 6in4 over PPPoE */
28 DefPMTU = 1500-8-Ip4Hdr-Ip6Hdr-UdpHdr-4-AESbsize-MAClen,
55 typedef struct Snet Snet;
56 typedef struct Edge Edge;
57 typedef struct Ciph Ciph;
58 typedef struct Host Host;
59 typedef struct Conn Conn;
65 Snet *next; /* next subnet on owner */
66 uchar mask[IPaddrlen];
78 Edge *next; /* next edge on src */
79 Edge *rev; /* reverse drection edge */
92 void (*crypt)(uchar*, int, AESstate*);
94 uchar key[AESkeylen+AESbsize];
119 uvlong ooo; /* out of order replay window */
144 char buf[MaxPacket+16];
162 Conn *bcast = (void*)-1;
166 char *outside = "/net";
167 char *inside = "/net";
171 uchar localip[IPaddrlen];
172 uchar localmask[IPaddrlen];
175 void delsubnet(Snet*);
176 void netrecalc(void);
178 void procsetname(char *fmt, ...);
179 int consend(Conn *c, char *fmt, ...);
180 void routepkt(Host *s, uchar *p, int n);
181 void needkey(Host *from);
182 void clearkey(Host *from);
184 #pragma varargck argpos procsetname 1
185 #pragma varargck argpos consend 2
190 void *v = malloc(len);
192 sysfatal("malloc: %r");
193 setmalloctag(v, getcallerpc(&len));
198 erealloc(void *v, ulong len)
200 if((v = realloc(v, len)) == nil && len != 0)
201 sysfatal("realloc: %r");
202 setrealloctag(v, getcallerpc(&v));
208 if((s = strdup(s)) == nil)
209 sysfatal("strdup: %r");
210 setmalloctag(s, getcallerpc(&s));
215 procsetname(char *fmt, ...)
221 snprint(buf, sizeof buf, "#p/%d/args", getpid());
222 if((fd = open(buf, OWRITE)) < 0)
225 n = vsnprint(buf, sizeof buf, fmt, arg);
232 fd2dir(int fd, char *dir, int len)
237 if(fd2path(fd, dir, len) < 0)
239 p = strrchr(dir, '/');
240 if(p == nil || p == dir)
247 dir2ipport(char *dir, uchar ip[IPaddrlen])
252 if((nci = getnetconninfo(dir, -1)) == nil)
254 if(parseip(ip, nci->rsys) != -1)
255 port = atoi(nci->rserv);
256 freenetconninfo(nci);
265 if(fd < 0 || fd2dir(fd, buf, sizeof(buf)-5) == nil)
268 if((fd = open(buf, OWRITE)) >= 0){
279 assert(lconn == nil);
283 assert(lconn == nil);
295 assert(lconn == nil);
301 edgecmp(Edge *a, Edge *b)
305 if((c = a->deleted - b->deleted) != 0)
307 return a->weight - b->weight;
310 edgepcmp(void *a, void *b)
312 return edgecmp(*(Edge**)a, *(Edge**)b);
316 subnetcmp(Snet *a, Snet *b)
320 if((c = a->deleted - b->deleted) != 0)
322 if((c = memcmp(b->mask, a->mask, IPaddrlen)) != 0)
324 if((c = memcmp(a->ip, b->ip, IPaddrlen)) != 0)
326 return a->weight - b->weight;
329 subnetpcmp(void *a, void *b)
331 return subnetcmp(*(Snet**)a, *(Snet**)b);
335 lookupnet(uchar ip[IPaddrlen])
341 for(i=0; i<nsnet; i++){
343 maskip(ip, t->mask, x);
344 if(memcmp(x, t->ip, IPaddrlen) == 0)
351 reportsubnet(Conn *c, Snet *t)
353 if(c == nil || !(t->deleted || t->owner->connected))
358 if(t->deleted != t->reported)
360 t->reported = !t->deleted;
361 for(x = myhost->link; x != nil; x = x->next)
362 if(x->dst->conn != lconn && x->dst->from == myhost)
363 reportsubnet(x->dst->conn, t);
366 if(t->owner == c->host)
369 consend(c, "%d %x %s %I/%d#%d", DEL_SUBNET, rand(),
370 t->owner->name, t->ip, t->prefixlen, t->weight);
372 consend(c, "%d %x %s %I/%d#%d", ADD_SUBNET, rand(), t->owner->name,
373 t->ip, t->prefixlen, t->weight);
376 reportedge(Conn *c, Edge *e)
378 if(c == nil || !(e->deleted || e->src->connected && e->dst->connected))
383 if(e->deleted != e->reported)
385 e->reported = !e->deleted;
386 for(x = myhost->link; x != nil; x = x->next)
387 if(x->dst->conn != lconn && x->dst->from == myhost)
388 reportedge(x->dst->conn, e);
391 if(e->src == c->host)
394 if(e->dst == c->host)
396 consend(c, "%d %x %s %s", DEL_EDGE, rand(),
397 e->src->name, e->dst->name);
399 consend(c, "%d %x %s %s %s %d %x %d", ADD_EDGE, rand(),
400 e->src->name, e->dst->name,
401 e->dst->addr, e->dst->port, e->dst->options, e->weight);
415 qsort(edges, nedges, sizeof(edges[0]), edgepcmp);
416 while(nedges > 0 && edges[nedges-1]->deleted){
417 reportedge(bcast, edges[--nedges]);
421 for(h = hosts; h != nil; h = h->next) h->from = nil;
423 myhost->from = myhost;
424 myhost->connected = 1;
427 for(i=0; i<nedges; i++){
429 if(e->src->from == nil || (h = e->dst)->from != nil)
431 memmove(h->ip, e->ip, IPaddrlen);
433 h->options = e->options;
435 if(h->connected == 0){
437 for(t = h->snet; t != nil; t = t->next)
444 for(h = hosts; h != nil; h = h->next){
445 if(h->from == nil && h->connected){
448 while(h->link != nil) {
449 deledge(h->link->rev);
452 while(h->snet != nil) delsubnet(h->snet);
456 qsort(snet, nsnet, sizeof(snet[0]), subnetpcmp);
457 for(i = nsnet-1; i >= 0; i--){
458 reportsubnet(bcast, snet[i]);
459 if(snet[i]->deleted){
460 assert(i == nsnet-1);
467 qsort(edges, nedges, sizeof(edges[0]), edgepcmp);
468 for(i = nedges-1; i >= 0; i--){
469 reportedge(bcast, edges[i]);
470 if(edges[i]->deleted){
471 assert(i == nedges-1);
480 getsubnet(Host *h, char *s, int new)
482 uchar ip[IPaddrlen], mask[IPaddrlen];
485 int i, prefixlen, weight;
487 if(parseip(ip, s) == -1)
492 if((p = strchr(s, '/')) != nil){
493 if((e = strchr(p+1, '#')) != nil)
495 prefixlen = atoi(p+1) + (isv4(ip)!=0)*(128-32);
501 memset(mask, 0, IPaddrlen);
502 for(i=0; i<prefixlen; i++)
503 mask[i/8] |= 0x80>>(i%8);
504 maskip(ip, mask, ip);
506 for(t = h->snet; t != nil; t = t->next)
507 if(memcmp(t->ip, ip, IPaddrlen) == 0
508 && memcmp(t->mask, mask, IPaddrlen) == 0){
517 t = emalloc(sizeof(Snet));
518 memmove(t->ip, ip, IPaddrlen);
519 memmove(t->mask, mask, IPaddrlen);
520 t->prefixlen = prefixlen - (isv4(ip)!=0)*(128-32);
525 if((nsnet % 16) == 0)
526 snet = erealloc(snet, sizeof(snet[0])*(nsnet+16));
536 if(t == nil || t->deleted)
538 for(tp = &t->owner->snet; *tp != nil; tp = &(*tp)->next){
549 getedge(Host *src, Host *dst, int new)
553 for(e = src->link; e != nil; e = e->next)
558 e = emalloc(sizeof(Edge));
559 e->weight = MaxWeight;
564 if((e->rev = getedge(dst, src, 0)) != nil)
566 if((nedges % 16) == 0)
567 edges = erealloc(edges, sizeof(edges[0])*(nedges+16));
577 if(e == nil || e->deleted)
580 if(e->rev->rev != nil){
581 assert(e->rev->rev == e);
586 for(ep = &e->src->link; *ep != nil; ep = &((*ep)->next))
593 e->weight = MaxWeight;
595 memset(e->ip, 0, IPaddrlen);
600 gethost(char *name, int new)
602 char buf[8*1024], *s, *e, *x;
606 if(*name == 0 || *name == '.' || strchr(name, '/') != nil)
609 for(h = hosts; h != nil; h = h->next)
610 if(strcmp(h->name, name) == 0)
612 snprint(buf, sizeof(buf), "%s/hosts/%s", confdir, name);
613 if((fd = open(buf, OREAD)) < 0){
618 n = read(fd, buf, sizeof(buf)-1);
624 h = emalloc(sizeof(Host));
625 h->name = estrdup(name);
626 h->addr = estrdup(name);
629 h->options = OptClampMss;
634 if((s = (char*)decodePEM(buf, "RSA PUBLIC KEY", &n, nil)) == nil)
636 h->rsapub = asn1toRSApub((uchar*)s, n);
640 for(s = buf; s != nil; s = e){
643 if((e = strchr(s, '\n')) != nil)
645 if((x = strchr(s, '=')) == nil)
648 if((n = tokenize(s, f, nelem(f))) != 2)
650 if(cistrcmp(f[0], "Address") == 0){
652 h->addr = estrdup(f[1]);
655 if(cistrcmp(f[0], "IndirectData") == 0){
656 h->options |= OptIndirect*(cistrcmp(f[1], "yes") == 0);
659 if(cistrcmp(f[0], "TCPonly") == 0){
660 h->options |= OptTcpOnly*(cistrcmp(f[1], "yes") == 0);
663 if(cistrcmp(f[0], "ClampMSS") == 0){
664 h->options &= ~(OptClampMss*(cistrcmp(f[1], "no") == 0));
667 if(cistrcmp(f[0], "PMTU") == 0){
668 h->pmtu = atoi(f[1]);
669 if(h->pmtu > MaxPacket)
671 else if(h->pmtu < 512)
675 if(cistrcmp(f[0], "Port") == 0){
676 h->port = atoi(f[1]);
679 if(cistrcmp(f[0], "Subnet") == 0){
681 getsubnet(h, f[1], 1);
685 if(myhost == nil && h->snet == nil){
686 snprint(buf, sizeof(buf), "%I/%d", localip, isv4(localip) ? 32 : 128);
687 getsubnet(h, buf, 1);
689 parseip(h->ip, h->addr);
696 findhost(uchar ip[IPaddrlen], int port)
701 for(h = hosts; h != nil; h = h->next){
702 if(memcmp(ip, h->ip, IPaddrlen) == 0
703 && (port == -1 || port == h->port))
718 if(myhost->rsapub == nil){
719 werrstr("no RSA public key");
722 if((afd = open("/mnt/factotum/rpc", ORDWR)) < 0)
724 if((rpc = auth_allocrpc(afd)) == nil){
729 s = smprint("proto=rsa service=tinc role=client host=%q", myhost->name);
730 r = auth_rpc(rpc, "start", s, strlen(s));
735 werrstr("no key found");
736 while(auth_rpc(rpc, "read", nil, 0) == ARok){
738 if(strtomp(s, &s, 16, m) == nil)
740 if(mpcmp(m, myhost->rsapub->n) != 0)
752 putrsarpc(AuthRpc *rpc)
767 memmove(c->buf, c->rp, c->wp - c->rp);
768 c->wp -= (c->rp - c->buf);
771 if((n = read(c->fd, c->wp, &c->buf[sizeof(c->buf)] - c->wp)) <= 0)
773 if(c->cin->crypt != nil)
774 (*c->cin->crypt)((uchar*)c->wp, n, c->cin->cs);
779 conwrite(Conn *c, char *s, int n)
781 if(c->cout->crypt != nil)
782 (*c->cout->crypt)((uchar*)s, n, c->cout->cs);
783 if(write(c->fd, s, n) != n)
789 conrecv(Conn *c, char **f, int nf)
794 if(c->wp > c->rp && (e = memchr(s = c->rp, '\n', c->wp - c->rp)) != nil){
797 if(debug) fprint(2, "<-%s %s\n", c->host != nil ? c->host->name : "???", s);
798 return tokenize(s, f, nf);
800 } while(conread(c) > 0);
804 consend(Conn *c, char *fmt, ...)
814 n = vsnprint(buf, sizeof(buf)-2, fmt, a);
821 if(debug) fprint(2, "->%s %s", c->host != nil ? c->host->name : "???", buf);
822 n = conwrite(c, buf, n);
831 uchar buf[4+MaxPacket+AESbsize+MAClen], mac[SHA2_256dlen];
836 if((n = read(h->udpfd, buf, sizeof(buf))) <= 0)
839 if(h->cin->crypt == nil || (n -= MAClen) < AESbsize){
843 hmac_sha2_256(buf, n, h->cin->key, sizeof(h->cin->key), mac, nil);
844 if(tsmemcmp(mac, buf+n, MAClen) != 0){
848 memmove(cs, h->cin->cs, sizeof(cs));
849 (*h->cin->crypt)(buf, n, cs);
856 if((o = (int)(seq - h->cin->seq)) > 0){
858 h->ooo = o < 64 ? h->ooo<<o | 1ULL : 0ULL;
861 if(o >= 64 || (h->ooo & 1ULL<<o) != 0){
868 if((n -= buf[n-1]) < 4+EtherHdr)
870 routepkt(h, buf+4, n-4);
874 sendudp(Host *h, uchar *p, int n)
876 uchar buf[4+MaxPacket+AESbsize+SHA2_256dlen];
881 if(h->udpfd < 0 || n > MaxPacket || n > h->pmtu)
884 if(h->cout->crypt == nil){
890 seq = ++h->cout->seq;
896 memmove(buf+4, p, n), n += 4;
897 pad = AESbsize - ((uint)n % AESbsize);
898 memset(buf+n, pad, pad), n += pad;
899 memmove(cs, h->cout->cs, sizeof(cs));
900 (*h->cout->crypt)(buf, n, cs);
901 hmac_sha2_256(buf, n, h->cout->key, sizeof(h->cout->key), buf+n, nil);
904 if(write(h->udpfd, buf, n) != n)
906 if((seq & 0xFFFFF) == 0) needkey(h);
911 sendtcp(Host *h, uchar *p, int n)
917 if((c = h->conn) == nil)
919 m = snprint(buf, sizeof(buf), "17 %d\n", n);
921 if(conwrite(c, buf, m) < 0
922 || conwrite(c, (char*)p, n) < 0)
931 forward(Host *s, Host *d, uchar *p, int n)
935 while(d != s && d != myhost){
936 if(sendudp(d, p, n) == 0)
938 if(sendtcp(d, p, n) == 0)
945 updatebyte(int csum, uchar *b, uchar *p, int v)
952 if(((p - b) & 1) == 0){
958 while(v = csum >> 16)
959 csum = (csum & 0xFFFF) + v;
964 clampmss(Host *d, uchar *p, int n, int o)
966 int oldmss, newmss, csum;
969 if(n <= TcpHdr || (p[13]&2) == 0 || (d->options & OptClampMss) == 0)
971 if((e = p+(p[12]>>4)*4) > p+n)
973 for(h = p+TcpHdr; h+4 <= e && h[1] > 0; h += h[1])
974 if(h[0] == 2 && h[1] == 4)
978 oldmss = h[2]<<8 | h[3];
979 newmss = myhost->pmtu;
982 newmss -= o + TcpHdr;
985 if(debug) fprint(2, "clamping tcp mss %d -> %d for %s\n", oldmss, newmss, d->name);
986 csum = (p[16]<<8 | p[17]) ^ 0xFFFF;
987 csum = updatebyte(csum, p, h+2, newmss>>8);
988 csum = updatebyte(csum, p, h+3, newmss);
995 routepkt(Host *s, uchar *p, int n)
997 uchar src[IPaddrlen], dst[IPaddrlen];
1004 switch(p[EtherType+0]<<8 | p[EtherType+1]){
1007 case 0x8100: /* VLAN */
1008 memmove(p+4, p, 2*Eaddrlen);
1011 case 0x0800: /* IP */
1014 switch(p[EtherHdr] & 0xF0){
1018 o = EtherHdr+(p[EtherHdr] & 15)*4;
1019 if(n < EtherHdr+Ip4Hdr || n < o)
1021 type = p[EtherHdr+9];
1022 v4tov6(src, p+EtherHdr+12);
1023 v4tov6(dst, p+EtherHdr+16);
1026 o = EtherHdr+Ip6Hdr;
1029 type = p[EtherHdr+6];
1030 memmove(src, p+EtherHdr+8, 16);
1031 memmove(dst, p+EtherHdr+24, 16);
1035 if((t = lookupnet(dst)) != nil){
1036 if(type == 6) /* TCP */
1037 clampmss(t->owner, p+o, n-o, o);
1038 if(t->owner == myhost)
1039 write(ipdfd, p+EtherHdr, n-EtherHdr);
1041 forward(s, t->owner, p, n);
1047 updateweight(Edge *e, int ms)
1049 e->weight = (e->weight + ms) / 2;
1064 c->pingtime = nsec();
1065 if(consend(c, "%d %s 17", ID, myhost->name) < 0)
1067 n = conrecv(c, f, nelem(f));
1068 if(n != 3 || atoi(f[0]) != ID || atoi(f[2]) != 17)
1070 if((c->host = gethost(f[1], 0)) == nil
1071 || c->host == myhost || c->host->rsapub == nil)
1074 n1 = (mpsignif(c->host->rsapub->n)+7)/8;
1075 if(n1 < AESkeylen+AESbsize || n1 > sizeof(b))
1077 n2 = (mpsignif(myhost->rsapub->n)+7)/8;
1078 if(n2 < AESkeylen+AESbsize || n2 > sizeof(b))
1081 m = mpnrand(c->host->rsapub->n, genrandom, nil);
1083 setupAESstate(c->cout->cs, b+n1-AESkeylen, AESkeylen, b+n1-AESkeylen-AESbsize);
1084 rsaencrypt(c->host->rsapub, m, m);
1087 if(consend(c, "%d %d %d 0 0 %.*H", META_KEY, EVP_AES256CFB, EVP_SHA256, n1, b) < 0)
1089 c->cout->crypt = aesCFBencrypt;
1091 n = conrecv(c, f, nelem(f));
1092 if(n != 6 || atoi(f[0]) != META_KEY || strlen(f[5]) != 2*n2)
1094 if(atoi(f[1]) != EVP_AES256CFB || atoi(f[2]) != EVP_SHA256){
1095 fprint(2, "%s uses unknown cipher/digest agorithms: %s %s\n",
1096 c->host->name, f[1], f[2]);
1100 if((rpc = getrsarpc()) == nil
1101 || auth_rpc(rpc, "write", f[5], strlen(f[5])) != ARok
1102 || auth_rpc(rpc, "read", nil, 0) != ARok){
1107 m = strtomp(rpc->arg, nil, 16, nil);
1111 setupAESstate(c->cin->cs, b+n2-AESkeylen, AESkeylen, b+n2-AESkeylen-AESbsize);
1112 c->cin->crypt = aesCFBdecrypt;
1114 h = mpnrand(c->host->rsapub->n, genrandom, nil);
1116 if(consend(c, "%d %.*H", CHALLENGE, n1, b) < 0){
1120 sha2_256(b, n1, b, nil);
1121 betomp(b, SHA2_256dlen, h);
1123 n = conrecv(c, f, nelem(f));
1124 if(n != 2 || atoi(f[0]) != CHALLENGE){
1128 m = strtomp(f[1], nil, 16, nil);
1131 sha2_256(b, n2, b, nil);
1132 if(consend(c, "%d %.*H", CHAL_REPLY, SHA2_256dlen, b) < 0){
1136 n = conrecv(c, f, nelem(f));
1137 if(n != 2 || atoi(f[0]) != CHAL_REPLY){
1141 m = strtomp(f[1], nil, 16, nil);
1147 ms = (nsec() - c->pingtime)/1000000LL;
1148 if(consend(c, "%d %d %d %x", ACK, myhost->port, ms, myhost->options) < 0)
1150 n = conrecv(c, f, nelem(f));
1151 if(n != 4 || atoi(f[0]) != ACK)
1155 e = getedge(myhost, c->host, 1);
1156 memmove(e->ip, c->ip, IPaddrlen);
1157 e->port = atoi(f[1]);
1158 e->weight = atoi(f[2]);
1159 e->options = strtol(f[3], nil, 16);
1160 updateweight(e, ms);
1172 while(to != nil && to != myhost){
1186 genrandom(to->cin->key, sizeof(to->cin->key));
1187 setupAESstate(to->cin->cs, to->cin->key, AESkeylen, to->cin->key+AESkeylen);
1188 to->cin->crypt = aesCBCdecrypt;
1191 consend(nearcon(to), "%d %s %s %.*H %d %d %d %d", ANS_KEY,
1192 myhost->name, to->name,
1193 sizeof(to->cin->key), to->cin->key,
1194 EVP_AES256CBC, EVP_SHA256, MAClen, 0);
1199 consend(nearcon(from), "%d %s %s", REQ_KEY, myhost->name, from->name);
1202 recvkey(Host *from, char **f)
1204 uchar key[sizeof(from->cout->key)];
1207 if(atoi(f[1]) != EVP_AES256CBC || atoi(f[2]) != EVP_SHA256
1208 || atoi(f[3]) != MAClen || atoi(f[4]) != 0){
1209 fprint(2, "%s key uses unknown parameters: %s %s %s %s\n",
1210 from->name, f[1], f[2], f[3], f[4]);
1213 if(strlen(f[0]) != sizeof(key)*2)
1215 if((m = strtomp(f[0], nil, 16, nil)) == nil)
1217 mptober(m, key, sizeof(key));
1220 if(tsmemcmp(key, from->cout->key, sizeof(key)) == 0)
1222 from->cout->seq = 0;
1223 memmove(from->cout->key, key, sizeof(key));
1224 setupAESstate(from->cout->cs, from->cout->key, AESkeylen, from->cout->key+AESkeylen);
1225 from->cout->crypt = aesCBCencrypt;
1228 memset(key, 0, sizeof(key));
1231 clearkey(Host *from)
1234 from->cout->crypt = nil;
1235 from->cout->seq = 0;
1236 memset(from->cout->cs, 0, sizeof(from->cout->cs));
1237 genrandom(from->cout->key, sizeof(from->cout->key));
1251 for(i=0; i<nsnet; i++)
1252 reportsubnet(c, snet[i]);
1253 for(i=0; i<nedges; i++)
1254 reportedge(c, edges[i]);
1258 while((n = conrecv(c, f, nelem(f))) > 0){
1261 if(consend(c, "%d %x", PONG, rand()) < 0)
1266 if(c->pingtime != 0){
1267 if((e = getedge(myhost, c->host, 0)) != nil)
1268 updateweight(e, (nsec() - c->pingtime) / 1000000LL);
1274 if(n != 4 || (h = gethost(f[2], 1)) == nil || h == myhost)
1277 getsubnet(h, f[3], 1);
1281 if(n != 4 || (h = gethost(f[2], 0)) == nil || h == myhost)
1284 if((t = getsubnet(h, f[3], 0)) != nil)
1289 if(n != 8 || (h = gethost(f[2], 1)) == nil || h == myhost
1290 || (r = gethost(f[3], 1)) == nil)
1293 if((e = getedge(h, r, 1)) != nil){
1294 if(parseip(e->ip, f[4]) == -1)
1295 memmove(e->ip, r->ip, IPaddrlen);
1296 e->port = atoi(f[5]);
1297 e->weight = atoi(f[7]);
1298 e->options = strtol(f[6], nil, 16);
1303 if(n != 4 || (h = gethost(f[2], 0)) == nil || h == myhost
1304 || (r = gethost(f[3], 1)) == nil)
1307 if((e = getedge(h, r, 0)) != nil)
1312 if(n != 3 || (h = gethost(f[2], 0)) == nil || h == myhost)
1316 for(e = myhost->link; e != nil; e = e->next)
1317 if(e->dst->conn != c && e->dst->from == myhost)
1318 consend(e->dst->conn, "%s %s %s", f[0], f[1], f[2]);
1322 if(n != 3 || (h = gethost(f[1], 0)) == nil || h == myhost
1323 || (r = gethost(f[2], 0)) == nil)
1327 consend(nearcon(r), "%s %s %s", f[0], f[1], f[2]);
1333 if(n != 8 || (h = gethost(f[1], 0)) == nil || h == myhost
1334 || (r = gethost(f[2], 0)) == nil)
1338 consend(nearcon(r), "%s %s %s %s %s %s %s %s",
1339 f[0], f[1], f[2], f[3],
1340 f[4], f[5], f[6], f[7]);
1349 if(n < 0 || n > MaxPacket)
1351 while((c->wp - c->rp) < n && conread(c) > 0)
1353 if(c->wp - c->rp < n)
1355 routepkt(c->host, (uchar*)c->rp, n);
1368 c = emalloc(sizeof(Conn));
1371 c->rp = c->wp = c->buf;
1372 c->port = dir2ipport(fd2dir(fd, dir, sizeof(dir)), c->ip);
1373 procsetname("tcpclient %s %s %I!%d", myhost->name,
1374 dir, c->ip, c->port);
1375 if(metaauth(c) == 0){
1376 procsetname("tcpclient %s %s %I!%d %s", myhost->name,
1377 dir, c->ip, c->port, c->host->name);
1381 if(c->host != nil && c->host->conn == c){
1382 c->host->conn = nil;
1383 if(c->edge != nil && c->edge->dst == c->host){
1384 deledge(c->edge->rev);
1387 hangupfd(c->host->udpfd);
1390 memset(c, 0, sizeof(*c));
1398 uchar ip[IPaddrlen];
1402 if((h = findhost(ip, dir2ipport(fd2dir(fd, dir, sizeof(dir)), ip))) != nil
1404 procsetname("udpclient %s %s %I!%d %s", myhost->name,
1405 dir, h->ip, h->port, h->name);
1409 } while(recvudp(h) == 0);
1417 dialer(char *proto, char *host, int rport, int lport)
1419 char addr[40], local[16];
1422 snprint(local, sizeof(local), "%d", lport);
1423 snprint(addr, sizeof(addr), "%s/%s!%s!%d", outside, proto, host, rport);
1424 procsetname("dialer %s %s", myhost->name, addr);
1427 if((dfd = dial(addr, lport ? local : nil, nil, nil)) >= 0){
1428 switch(rfork(RFPROC|RFMEM)){
1443 listener(char *proto, int port, int nprocs)
1445 char addr[40], adir[40], ldir[40];
1446 int acfd, lcfd, dfd;
1448 snprint(addr, sizeof(addr), "%s/%s!*!%d", outside, proto, port);
1449 procsetname("listener %s %s", myhost->name, addr);
1451 if((acfd = announce(addr, adir)) < 0)
1453 while((lcfd = listen(adir, ldir)) >= 0){
1454 if((dfd = accept(lcfd, ldir)) >= 0)
1455 switch(rfork(RFPROC|RFMEM)){
1457 if(nprocs > 1 || waitpid() < 0) nprocs--;
1476 procsetname("pingpong %s", myhost->name);
1478 sleep(15*1000 + (rand() % 3000));
1480 for(e = myhost->link; e != nil; e = e->next){
1481 if((c = e->dst->conn) != nil){
1482 if(c->pingtime != 0){
1486 c->pingtime = nsec();
1487 consend(c, "%d %x", PING, rand());
1500 snprint(buf, sizeof buf, "%s/ipifc/clone", inside);
1501 if((ipcfd = open(buf, ORDWR)) < 0)
1502 sysfatal("can't open ip interface: %r");
1503 if((n = read(ipcfd, buf, sizeof buf - 1)) <= 0)
1504 sysfatal("can't read interface number: %r");
1507 snprint(buf, sizeof buf, "%s/ipifc/%d/data", inside, ipifn);
1508 if((ipdfd = open(buf, ORDWR)) < 0)
1509 sysfatal("can't open ip data: %r");
1510 fprint(ipcfd, "bind pkt");
1511 fprint(ipcfd, "mtu %d", myhost->pmtu-EtherHdr);
1512 fprint(ipcfd, "add %I %M", localip, localmask);
1518 uchar buf[MaxPacket];
1521 procsetname("ip2tunnel %s %s %I %M", myhost->name,
1522 fd2dir(ipdfd, (char*)buf, sizeof(buf)),
1523 localip, localmask);
1524 while((n = read(ipdfd, buf+EtherHdr, sizeof buf-EtherHdr)) > 0){
1525 memset(buf, 0, 2*Eaddrlen);
1526 buf[EtherType+0] = 0x08;
1527 buf[EtherType+1] = 0x00;
1528 routepkt(myhost, buf, n+EtherHdr);
1533 catch(void*, char *msg)
1535 if(strcmp(msg, "alarm") == 0 || strcmp(msg, "interrupt") == 0)
1542 postnote(PNGROUP, getpid(), "shutdown");
1548 fprint(2, "%s [-d] [-p maxprocs] [-x inside] [-o outside] [-c confdir] [-n myname] "
1549 "localip localmask [host...]\n", argv0);
1554 main(int argc, char *argv[])
1562 fmtinstall('I', eipfmt);
1563 fmtinstall('M', eipfmt);
1564 fmtinstall('H', encodefmt);
1571 if((maxprocs = atoi(EARGF(usage()))) < 1)
1572 sysfatal("bad number of procs");
1575 confdir = EARGF(usage());
1578 myname = EARGF(usage());
1581 outside = inside = EARGF(usage());
1584 outside = EARGF(usage());
1592 if(parseip(localip, argv[0]) == -1)
1593 sysfatal("bad local ip: %s", argv[0]);
1594 if(parseipmask(localmask, argv[1]) == -1)
1595 sysfatal("bad local mask: %s", argv[1]);
1596 argv += 2, argc -= 2;
1601 if((myhost = gethost(myname, 0)) == nil)
1602 sysfatal("can't get my host: %r");
1603 if((rpc = getrsarpc()) == nil)
1604 sysfatal("can't find my key in factotum: %r");
1607 for(i = 0; i < argc; i++){
1608 if((h = gethost(argv[i], 0)) == nil)
1609 sysfatal("unknown host: %s", *argv);
1611 sysfatal("will not connect to myself");
1612 if(h->rsapub == nil)
1613 sysfatal("no RSA public key for: %s", h->name);
1616 if((t = lookupnet(localip)) == nil)
1617 sysfatal("no subnet found for local ip %I", localip);
1618 if(t->owner != myhost)
1619 sysfatal("local ip %I belongs to host %s subnet %I/%d",
1620 localip, t->owner->name, t->ip, t->prefixlen);
1624 switch(rfork(RFPROC|RFFDG|RFREND|RFNOTEG)){
1626 sysfatal("can't fork: %r");
1632 fprint(ipcfd, "unbind");
1637 if(rfork(RFPROC|RFMEM) == 0){
1638 tcpclient(listener("tcp", myhost->port, maxprocs));
1641 if(rfork(RFPROC|RFMEM) == 0){
1642 udpclient(listener("udp", myhost->port, maxprocs));
1645 for(i = 0; i < argc; i++){
1646 if((h = gethost(argv[i], 0)) == nil)
1648 if(rfork(RFPROC|RFMEM) == 0){
1649 tcpclient(dialer("tcp", h->addr, h->port, myhost->port));
1652 if(rfork(RFPROC|RFMEM) == 0){
1653 udpclient(dialer("udp", h->addr, h->port, myhost->port));
1657 if(rfork(RFPROC|RFMEM) == 0){