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 int consend(Conn *c, char *fmt, ...);
179 #pragma varargck argpos consend 2
180 void routepkt(Host *s, uchar *p, int n);
181 void needkey(Host *from);
182 void clearkey(Host *from);
187 void *v = malloc(len);
189 sysfatal("malloc: %r");
190 setmalloctag(v, getcallerpc(&len));
195 erealloc(void *v, ulong len)
197 if((v = realloc(v, len)) == nil && len != 0)
198 sysfatal("realloc: %r");
199 setrealloctag(v, getcallerpc(&v));
205 if((s = strdup(s)) == nil)
206 sysfatal("strdup: %r");
207 setmalloctag(s, getcallerpc(&s));
212 fd2dir(int fd, char *dir, int len)
217 if(fd2path(fd, dir, len) < 0)
219 p = strrchr(dir, '/');
220 if(p == nil || p == dir)
227 dir2ipport(char *dir, uchar ip[IPaddrlen])
232 if((nci = getnetconninfo(dir, -1)) == nil)
234 if(parseip(ip, nci->rsys) != -1)
235 port = atoi(nci->rserv);
236 freenetconninfo(nci);
245 if(fd < 0 || fd2dir(fd, buf, sizeof(buf)-5) == nil)
248 if((fd = open(buf, OWRITE)) >= 0){
259 assert(lconn == nil);
263 assert(lconn == nil);
275 assert(lconn == nil);
281 edgecmp(Edge *a, Edge *b)
285 if((c = a->deleted - b->deleted) != 0)
287 return a->weight - b->weight;
290 edgepcmp(void *a, void *b)
292 return edgecmp(*(Edge**)a, *(Edge**)b);
296 subnetcmp(Snet *a, Snet *b)
300 if((c = a->deleted - b->deleted) != 0)
302 if((c = memcmp(b->mask, a->mask, IPaddrlen)) != 0)
304 if((c = memcmp(a->ip, b->ip, IPaddrlen)) != 0)
306 return a->weight - b->weight;
309 subnetpcmp(void *a, void *b)
311 return subnetcmp(*(Snet**)a, *(Snet**)b);
315 lookupnet(uchar ip[IPaddrlen])
321 for(i=0; i<nsnet; i++){
323 maskip(ip, t->mask, x);
324 if(memcmp(x, t->ip, IPaddrlen) == 0)
331 reportsubnet(Conn *c, Snet *t)
333 if(c == nil || !(t->deleted || t->owner->connected))
338 if(t->deleted != t->reported)
340 t->reported = !t->deleted;
341 for(x = myhost->link; x != nil; x = x->next)
342 if(x->dst->conn != lconn && x->dst->from == myhost)
343 reportsubnet(x->dst->conn, t);
346 if(t->owner == c->host)
349 consend(c, "%d %x %s %I/%d#%d", DEL_SUBNET, rand(),
350 t->owner->name, t->ip, t->prefixlen, t->weight);
352 consend(c, "%d %x %s %I/%d#%d", ADD_SUBNET, rand(), t->owner->name,
353 t->ip, t->prefixlen, t->weight);
356 reportedge(Conn *c, Edge *e)
358 if(c == nil || !(e->deleted || e->src->connected && e->dst->connected))
363 if(e->deleted != e->reported)
365 e->reported = !e->deleted;
366 for(x = myhost->link; x != nil; x = x->next)
367 if(x->dst->conn != lconn && x->dst->from == myhost)
368 reportedge(x->dst->conn, e);
371 if(e->src == c->host)
374 if(e->dst == c->host)
376 consend(c, "%d %x %s %s", DEL_EDGE, rand(),
377 e->src->name, e->dst->name);
379 consend(c, "%d %x %s %s %s %d %x %d", ADD_EDGE, rand(),
380 e->src->name, e->dst->name,
381 e->dst->addr, e->dst->port, e->dst->options, e->weight);
395 qsort(edges, nedges, sizeof(edges[0]), edgepcmp);
396 while(nedges > 0 && edges[nedges-1]->deleted){
397 reportedge(bcast, edges[--nedges]);
401 for(h = hosts; h != nil; h = h->next) h->from = nil;
403 myhost->from = myhost;
404 myhost->connected = 1;
407 for(i=0; i<nedges; i++){
409 if(e->src->from == nil || (h = e->dst)->from != nil)
411 memmove(h->ip, e->ip, IPaddrlen);
413 h->options = e->options;
415 if(h->connected == 0){
417 for(t = h->snet; t != nil; t = t->next)
424 for(h = hosts; h != nil; h = h->next){
425 if(h->from == nil && h->connected){
428 while(h->link != nil) {
429 deledge(h->link->rev);
432 while(h->snet != nil) delsubnet(h->snet);
436 qsort(snet, nsnet, sizeof(snet[0]), subnetpcmp);
437 for(i = nsnet-1; i >= 0; i--){
438 reportsubnet(bcast, snet[i]);
439 if(snet[i]->deleted){
440 assert(i == nsnet-1);
447 qsort(edges, nedges, sizeof(edges[0]), edgepcmp);
448 for(i = nedges-1; i >= 0; i--){
449 reportedge(bcast, edges[i]);
450 if(edges[i]->deleted){
451 assert(i == nedges-1);
460 getsubnet(Host *h, char *s, int new)
462 uchar ip[IPaddrlen], mask[IPaddrlen];
465 int i, prefixlen, weight;
467 if(parseip(ip, s) == -1)
472 if((p = strchr(s, '/')) != nil){
473 if((e = strchr(p+1, '#')) != nil)
475 prefixlen = atoi(p+1) + (isv4(ip)!=0)*(128-32);
481 memset(mask, 0, IPaddrlen);
482 for(i=0; i<prefixlen; i++)
483 mask[i/8] |= 0x80>>(i%8);
484 maskip(ip, mask, ip);
486 for(t = h->snet; t != nil; t = t->next)
487 if(memcmp(t->ip, ip, IPaddrlen) == 0
488 && memcmp(t->mask, mask, IPaddrlen) == 0){
497 t = emalloc(sizeof(Snet));
498 memmove(t->ip, ip, IPaddrlen);
499 memmove(t->mask, mask, IPaddrlen);
500 t->prefixlen = prefixlen - (isv4(ip)!=0)*(128-32);
505 if((nsnet % 16) == 0)
506 snet = erealloc(snet, sizeof(snet[0])*(nsnet+16));
516 if(t == nil || t->deleted)
518 for(tp = &t->owner->snet; *tp != nil; tp = &(*tp)->next){
529 getedge(Host *src, Host *dst, int new)
533 for(e = src->link; e != nil; e = e->next)
538 e = emalloc(sizeof(Edge));
539 e->weight = MaxWeight;
544 if((e->rev = getedge(dst, src, 0)) != nil)
546 if((nedges % 16) == 0)
547 edges = erealloc(edges, sizeof(edges[0])*(nedges+16));
557 if(e == nil || e->deleted)
560 if(e->rev->rev != nil){
561 assert(e->rev->rev == e);
566 for(ep = &e->src->link; *ep != nil; ep = &((*ep)->next))
573 e->weight = MaxWeight;
575 memset(e->ip, 0, IPaddrlen);
580 gethost(char *name, int new)
582 char buf[8*1024], *s, *e, *x;
586 if(*name == 0 || *name == '.' || strchr(name, '/') != nil)
589 for(h = hosts; h != nil; h = h->next)
590 if(strcmp(h->name, name) == 0)
592 snprint(buf, sizeof(buf), "%s/hosts/%s", confdir, name);
593 if((fd = open(buf, OREAD)) < 0){
598 n = read(fd, buf, sizeof(buf)-1);
604 h = emalloc(sizeof(Host));
605 h->name = estrdup(name);
606 h->addr = estrdup(name);
609 h->options = OptClampMss;
614 if((s = (char*)decodePEM(buf, "RSA PUBLIC KEY", &n, nil)) == nil)
616 h->rsapub = asn1toRSApub((uchar*)s, n);
620 for(s = buf; s != nil; s = e){
623 if((e = strchr(s, '\n')) != nil)
625 if((x = strchr(s, '=')) == nil)
628 if((n = tokenize(s, f, nelem(f))) != 2)
630 if(cistrcmp(f[0], "Address") == 0){
632 h->addr = estrdup(f[1]);
635 if(cistrcmp(f[0], "IndirectData") == 0){
636 h->options |= OptIndirect*(cistrcmp(f[1], "yes") == 0);
639 if(cistrcmp(f[0], "TCPonly") == 0){
640 h->options |= OptTcpOnly*(cistrcmp(f[1], "yes") == 0);
643 if(cistrcmp(f[0], "ClampMSS") == 0){
644 h->options &= ~(OptClampMss*(cistrcmp(f[1], "no") == 0));
647 if(cistrcmp(f[0], "PMTU") == 0){
648 h->pmtu = atoi(f[1]);
649 if(h->pmtu > MaxPacket)
651 else if(h->pmtu < 512)
655 if(cistrcmp(f[0], "Port") == 0){
656 h->port = atoi(f[1]);
659 if(cistrcmp(f[0], "Subnet") == 0){
661 getsubnet(h, f[1], 1);
665 if(myhost == nil && h->snet == nil){
666 snprint(buf, sizeof(buf), "%I/%d", localip, isv4(localip) ? 32 : 128);
667 getsubnet(h, buf, 1);
669 parseip(h->ip, h->addr);
676 findhost(uchar ip[IPaddrlen], int port)
681 for(h = hosts; h != nil; h = h->next){
682 if(memcmp(ip, h->ip, IPaddrlen) == 0
683 && (port == -1 || port == h->port))
698 if(myhost->rsapub == nil){
699 werrstr("no RSA public key");
702 if((afd = open("/mnt/factotum/rpc", ORDWR)) < 0)
704 if((rpc = auth_allocrpc(afd)) == nil){
709 s = smprint("proto=rsa service=tinc role=client host=%q", myhost->name);
710 r = auth_rpc(rpc, "start", s, strlen(s));
715 werrstr("no key found");
716 while(auth_rpc(rpc, "read", nil, 0) == ARok){
718 if(strtomp(s, &s, 16, m) == nil)
720 if(mpcmp(m, myhost->rsapub->n) != 0)
732 putrsarpc(AuthRpc *rpc)
747 memmove(c->buf, c->rp, c->wp - c->rp);
748 c->wp -= (c->rp - c->buf);
751 if((n = read(c->fd, c->wp, &c->buf[sizeof(c->buf)] - c->wp)) <= 0)
753 if(c->cin->crypt != nil)
754 (*c->cin->crypt)((uchar*)c->wp, n, c->cin->cs);
759 conwrite(Conn *c, char *s, int n)
761 if(c->cout->crypt != nil)
762 (*c->cout->crypt)((uchar*)s, n, c->cout->cs);
763 if(write(c->fd, s, n) != n)
769 conrecv(Conn *c, char **f, int nf)
774 if(c->wp > c->rp && (e = memchr(s = c->rp, '\n', c->wp - c->rp)) != nil){
777 if(debug) fprint(2, "<-%s %s\n", c->host != nil ? c->host->name : "???", s);
778 return tokenize(s, f, nf);
780 } while(conread(c) > 0);
784 consend(Conn *c, char *fmt, ...)
794 n = vsnprint(buf, sizeof(buf)-2, fmt, a);
801 if(debug) fprint(2, "->%s %s", c->host != nil ? c->host->name : "???", buf);
802 n = conwrite(c, buf, n);
811 uchar buf[4+MaxPacket+AESbsize+MAClen], mac[SHA2_256dlen];
816 if((n = read(h->udpfd, buf, sizeof(buf))) <= 0)
819 if(h->cin->crypt == nil || (n -= MAClen) < AESbsize){
823 hmac_sha2_256(buf, n, h->cin->key, sizeof(h->cin->key), mac, nil);
824 if(tsmemcmp(mac, buf+n, MAClen) != 0){
828 memmove(cs, h->cin->cs, sizeof(cs));
829 (*h->cin->crypt)(buf, n, cs);
836 if((o = (int)(seq - h->cin->seq)) > 0){
838 h->ooo = o < 64 ? h->ooo<<o | 1ULL : 0ULL;
841 if(o >= 64 || (h->ooo & 1ULL<<o) != 0){
848 if((n -= buf[n-1]) < 4+EtherHdr)
850 routepkt(h, buf+4, n-4);
854 sendudp(Host *h, uchar *p, int n)
856 uchar buf[4+MaxPacket+AESbsize+SHA2_256dlen];
861 if(h->udpfd < 0 || n > MaxPacket || n > h->pmtu)
864 if(h->cout->crypt == nil){
870 seq = ++h->cout->seq;
876 memmove(buf+4, p, n), n += 4;
877 pad = AESbsize - ((uint)n % AESbsize);
878 memset(buf+n, pad, pad), n += pad;
879 memmove(cs, h->cout->cs, sizeof(cs));
880 (*h->cout->crypt)(buf, n, cs);
881 hmac_sha2_256(buf, n, h->cout->key, sizeof(h->cout->key), buf+n, nil);
884 if(write(h->udpfd, buf, n) != n)
886 if((seq & 0xFFFFF) == 0) needkey(h);
891 sendtcp(Host *h, uchar *p, int n)
897 if((c = h->conn) == nil)
899 m = snprint(buf, sizeof(buf), "17 %d\n", n);
901 if(conwrite(c, buf, m) < 0
902 || conwrite(c, (char*)p, n) < 0)
911 forward(Host *s, Host *d, uchar *p, int n)
915 while(d != s && d != myhost){
916 if(sendudp(d, p, n) == 0)
918 if(sendtcp(d, p, n) == 0)
925 updatebyte(int csum, uchar *b, uchar *p, int v)
932 if(((p - b) & 1) == 0){
938 while(v = csum >> 16)
939 csum = (csum & 0xFFFF) + v;
944 clampmss(Host *d, uchar *p, int n, int o)
946 int oldmss, newmss, csum;
949 if(n <= TcpHdr || (p[13]&2) == 0 || (d->options & OptClampMss) == 0)
951 if((e = p+(p[12]>>4)*4) > p+n)
953 for(h = p+TcpHdr; h < e;){
961 if(h[1] < 2 || h[1] > e - h)
963 if(h[0] == 2 && h[1] == 4)
969 oldmss = h[2]<<8 | h[3];
970 newmss = myhost->pmtu;
973 newmss -= o + TcpHdr;
976 if(debug) fprint(2, "clamping tcp mss %d -> %d for %s\n", oldmss, newmss, d->name);
977 csum = (p[16]<<8 | p[17]) ^ 0xFFFF;
978 csum = updatebyte(csum, p, h+2, newmss>>8);
979 csum = updatebyte(csum, p, h+3, newmss);
986 routepkt(Host *s, uchar *p, int n)
988 uchar src[IPaddrlen], dst[IPaddrlen];
995 switch(p[EtherType+0]<<8 | p[EtherType+1]){
998 case 0x8100: /* VLAN */
999 memmove(p+4, p, 2*Eaddrlen);
1002 case 0x0800: /* IPv4 */
1003 case 0x86DD: /* IPv6 */
1006 switch(p[EtherHdr] & 0xF0){
1010 o = EtherHdr+(p[EtherHdr] & 15)*4;
1011 if(n < EtherHdr+Ip4Hdr || n < o)
1013 type = p[EtherHdr+9];
1014 v4tov6(src, p+EtherHdr+12);
1015 v4tov6(dst, p+EtherHdr+16);
1018 o = EtherHdr+Ip6Hdr;
1021 type = p[EtherHdr+6];
1022 memmove(src, p+EtherHdr+8, 16);
1023 memmove(dst, p+EtherHdr+24, 16);
1027 if((t = lookupnet(dst)) != nil){
1028 if(type == 6) /* TCP */
1029 clampmss(t->owner, p+o, n-o, o);
1030 if(t->owner == myhost)
1031 write(ipdfd, p+EtherHdr, n-EtherHdr);
1033 forward(s, t->owner, p, n);
1039 updateweight(Edge *e, int ms)
1041 e->weight = (e->weight + ms) / 2;
1056 c->pingtime = nsec();
1057 if(consend(c, "%d %s 17", ID, myhost->name) < 0)
1059 n = conrecv(c, f, nelem(f));
1060 if(n != 3 || atoi(f[0]) != ID || atoi(f[2]) != 17)
1062 if((c->host = gethost(f[1], 0)) == nil
1063 || c->host == myhost || c->host->rsapub == nil)
1066 n1 = (mpsignif(c->host->rsapub->n)+7)/8;
1067 if(n1 < AESkeylen+AESbsize || n1 > sizeof(b))
1069 n2 = (mpsignif(myhost->rsapub->n)+7)/8;
1070 if(n2 < AESkeylen+AESbsize || n2 > sizeof(b))
1073 m = mpnrand(c->host->rsapub->n, genrandom, nil);
1075 setupAESstate(c->cout->cs, b+n1-AESkeylen, AESkeylen, b+n1-AESkeylen-AESbsize);
1076 rsaencrypt(c->host->rsapub, m, m);
1079 if(consend(c, "%d %d %d 0 0 %.*H", META_KEY, EVP_AES256CFB, EVP_SHA256, n1, b) < 0)
1081 c->cout->crypt = aesCFBencrypt;
1083 n = conrecv(c, f, nelem(f));
1084 if(n != 6 || atoi(f[0]) != META_KEY || strlen(f[5]) != 2*n2)
1086 if(atoi(f[1]) != EVP_AES256CFB || atoi(f[2]) != EVP_SHA256){
1087 fprint(2, "%s uses unknown cipher/digest agorithms: %s %s\n",
1088 c->host->name, f[1], f[2]);
1092 if((rpc = getrsarpc()) == nil
1093 || auth_rpc(rpc, "write", f[5], strlen(f[5])) != ARok
1094 || auth_rpc(rpc, "read", nil, 0) != ARok){
1099 m = strtomp(rpc->arg, nil, 16, nil);
1103 setupAESstate(c->cin->cs, b+n2-AESkeylen, AESkeylen, b+n2-AESkeylen-AESbsize);
1104 c->cin->crypt = aesCFBdecrypt;
1106 h = mpnrand(c->host->rsapub->n, genrandom, nil);
1108 if(consend(c, "%d %.*H", CHALLENGE, n1, b) < 0){
1112 sha2_256(b, n1, b, nil);
1113 betomp(b, SHA2_256dlen, h);
1115 n = conrecv(c, f, nelem(f));
1116 if(n != 2 || atoi(f[0]) != CHALLENGE){
1120 m = strtomp(f[1], nil, 16, nil);
1123 sha2_256(b, n2, b, nil);
1124 if(consend(c, "%d %.*H", CHAL_REPLY, SHA2_256dlen, b) < 0){
1128 n = conrecv(c, f, nelem(f));
1129 if(n != 2 || atoi(f[0]) != CHAL_REPLY){
1133 m = strtomp(f[1], nil, 16, nil);
1139 ms = (nsec() - c->pingtime)/1000000LL;
1140 if(consend(c, "%d %d %d %x", ACK, myhost->port, ms, myhost->options) < 0)
1142 n = conrecv(c, f, nelem(f));
1143 if(n != 4 || atoi(f[0]) != ACK)
1147 e = getedge(myhost, c->host, 1);
1148 memmove(e->ip, c->ip, IPaddrlen);
1149 e->port = atoi(f[1]);
1150 e->weight = atoi(f[2]);
1151 e->options = strtol(f[3], nil, 16);
1152 updateweight(e, ms);
1164 while(to != nil && to != myhost){
1178 genrandom(to->cin->key, sizeof(to->cin->key));
1179 setupAESstate(to->cin->cs, to->cin->key, AESkeylen, to->cin->key+AESkeylen);
1180 to->cin->crypt = aesCBCdecrypt;
1183 consend(nearcon(to), "%d %s %s %.*H %d %d %d %d", ANS_KEY,
1184 myhost->name, to->name,
1185 sizeof(to->cin->key), to->cin->key,
1186 EVP_AES256CBC, EVP_SHA256, MAClen, 0);
1191 consend(nearcon(from), "%d %s %s", REQ_KEY, myhost->name, from->name);
1194 recvkey(Host *from, char **f)
1196 uchar key[sizeof(from->cout->key)];
1199 if(atoi(f[1]) != EVP_AES256CBC || atoi(f[2]) != EVP_SHA256
1200 || atoi(f[3]) != MAClen || atoi(f[4]) != 0){
1201 fprint(2, "%s key uses unknown parameters: %s %s %s %s\n",
1202 from->name, f[1], f[2], f[3], f[4]);
1205 if(strlen(f[0]) != sizeof(key)*2)
1207 if((m = strtomp(f[0], nil, 16, nil)) == nil)
1209 mptober(m, key, sizeof(key));
1212 if(tsmemcmp(key, from->cout->key, sizeof(key)) == 0)
1214 from->cout->seq = 0;
1215 memmove(from->cout->key, key, sizeof(key));
1216 setupAESstate(from->cout->cs, from->cout->key, AESkeylen, from->cout->key+AESkeylen);
1217 from->cout->crypt = aesCBCencrypt;
1220 memset(key, 0, sizeof(key));
1223 clearkey(Host *from)
1226 from->cout->crypt = nil;
1227 from->cout->seq = 0;
1228 memset(from->cout->cs, 0, sizeof(from->cout->cs));
1229 genrandom(from->cout->key, sizeof(from->cout->key));
1243 for(i=0; i<nsnet; i++)
1244 reportsubnet(c, snet[i]);
1245 for(i=0; i<nedges; i++)
1246 reportedge(c, edges[i]);
1250 while((n = conrecv(c, f, nelem(f))) > 0){
1253 if(consend(c, "%d %x", PONG, rand()) < 0)
1258 if(c->pingtime != 0){
1259 if((e = getedge(myhost, c->host, 0)) != nil)
1260 updateweight(e, (nsec() - c->pingtime) / 1000000LL);
1266 if(n != 4 || (h = gethost(f[2], 1)) == nil || h == myhost)
1269 getsubnet(h, f[3], 1);
1273 if(n != 4 || (h = gethost(f[2], 0)) == nil || h == myhost)
1276 if((t = getsubnet(h, f[3], 0)) != nil)
1281 if(n != 8 || (h = gethost(f[2], 1)) == nil || h == myhost
1282 || (r = gethost(f[3], 1)) == nil)
1285 if((e = getedge(h, r, 1)) != nil){
1286 if(parseip(e->ip, f[4]) == -1)
1287 memmove(e->ip, r->ip, IPaddrlen);
1288 e->port = atoi(f[5]);
1289 e->weight = atoi(f[7]);
1290 e->options = strtol(f[6], nil, 16);
1295 if(n != 4 || (h = gethost(f[2], 0)) == nil || h == myhost
1296 || (r = gethost(f[3], 1)) == nil)
1299 if((e = getedge(h, r, 0)) != nil)
1304 if(n != 3 || (h = gethost(f[2], 0)) == nil || h == myhost)
1308 for(e = myhost->link; e != nil; e = e->next)
1309 if(e->dst->conn != c && e->dst->from == myhost)
1310 consend(e->dst->conn, "%s %s %s", f[0], f[1], f[2]);
1314 if(n != 3 || (h = gethost(f[1], 0)) == nil || h == myhost
1315 || (r = gethost(f[2], 0)) == nil)
1319 consend(nearcon(r), "%s %s %s", f[0], f[1], f[2]);
1325 if(n != 8 || (h = gethost(f[1], 0)) == nil || h == myhost
1326 || (r = gethost(f[2], 0)) == nil)
1330 consend(nearcon(r), "%s %s %s %s %s %s %s %s",
1331 f[0], f[1], f[2], f[3],
1332 f[4], f[5], f[6], f[7]);
1341 if(n < 0 || n > MaxPacket)
1343 while((c->wp - c->rp) < n && conread(c) > 0)
1345 if(c->wp - c->rp < n)
1347 routepkt(c->host, (uchar*)c->rp, n);
1360 c = emalloc(sizeof(Conn));
1363 c->rp = c->wp = c->buf;
1364 c->port = dir2ipport(fd2dir(fd, dir, sizeof(dir)), c->ip);
1365 procsetname("tcpclient %s %s %I!%d", myhost->name,
1366 dir, c->ip, c->port);
1367 if(metaauth(c) == 0){
1368 procsetname("tcpclient %s %s %I!%d %s", myhost->name,
1369 dir, c->ip, c->port, c->host->name);
1373 if(c->host != nil && c->host->conn == c){
1374 c->host->conn = nil;
1375 if(c->edge != nil && c->edge->dst == c->host){
1376 deledge(c->edge->rev);
1379 hangupfd(c->host->udpfd);
1382 memset(c, 0, sizeof(*c));
1390 uchar ip[IPaddrlen];
1394 if((h = findhost(ip, dir2ipport(fd2dir(fd, dir, sizeof(dir)), ip))) != nil
1396 procsetname("udpclient %s %s %I!%d %s", myhost->name,
1397 dir, h->ip, h->port, h->name);
1401 } while(recvudp(h) == 0);
1409 dialer(char *proto, char *host, int rport, int lport)
1411 char addr[40], local[16];
1414 snprint(local, sizeof(local), "%d", lport);
1415 snprint(addr, sizeof(addr), "%s/%s!%s!%d", outside, proto, host, rport);
1416 procsetname("dialer %s %s", myhost->name, addr);
1419 if((dfd = dial(addr, lport ? local : nil, nil, nil)) >= 0){
1420 switch(rfork(RFPROC|RFMEM)){
1435 listener(char *proto, int port, int nprocs)
1437 char addr[40], adir[40], ldir[40];
1438 int acfd, lcfd, dfd;
1440 snprint(addr, sizeof(addr), "%s/%s!*!%d", outside, proto, port);
1441 procsetname("listener %s %s", myhost->name, addr);
1443 if((acfd = announce(addr, adir)) < 0)
1445 while((lcfd = listen(adir, ldir)) >= 0){
1446 if((dfd = accept(lcfd, ldir)) >= 0)
1447 switch(rfork(RFPROC|RFMEM)){
1449 if(nprocs > 1 || waitpid() < 0) nprocs--;
1468 procsetname("pingpong %s", myhost->name);
1470 sleep(15*1000 + (rand() % 3000));
1472 for(e = myhost->link; e != nil; e = e->next){
1473 if((c = e->dst->conn) != nil){
1474 if(c->pingtime != 0){
1478 c->pingtime = nsec();
1479 consend(c, "%d %x", PING, rand());
1492 snprint(buf, sizeof buf, "%s/ipifc/clone", inside);
1493 if((ipcfd = open(buf, ORDWR)) < 0)
1494 sysfatal("can't open ip interface: %r");
1495 if((n = read(ipcfd, buf, sizeof buf - 1)) <= 0)
1496 sysfatal("can't read interface number: %r");
1499 snprint(buf, sizeof buf, "%s/ipifc/%d/data", inside, ipifn);
1500 if((ipdfd = open(buf, ORDWR)) < 0)
1501 sysfatal("can't open ip data: %r");
1502 fprint(ipcfd, "bind pkt");
1503 fprint(ipcfd, "mtu %d", myhost->pmtu-EtherHdr);
1504 fprint(ipcfd, "add %I %M", localip, localmask);
1510 uchar buf[MaxPacket];
1513 procsetname("ip2tunnel %s %s %I %M", myhost->name,
1514 fd2dir(ipdfd, (char*)buf, sizeof(buf)),
1515 localip, localmask);
1516 while((n = read(ipdfd, buf+EtherHdr, sizeof buf-EtherHdr)) > 0){
1517 memset(buf, 0, 2*Eaddrlen);
1518 if((buf[EtherHdr]&0xF0) == 0x60){
1519 buf[EtherType+0] = 0x86;
1520 buf[EtherType+1] = 0xDD;
1522 buf[EtherType+0] = 0x08;
1523 buf[EtherType+1] = 0x00;
1525 routepkt(myhost, buf, n+EtherHdr);
1530 catch(void*, char *msg)
1532 if(strcmp(msg, "alarm") == 0 || strcmp(msg, "interrupt") == 0)
1539 postnote(PNGROUP, getpid(), "shutdown");
1545 fprint(2, "%s [-d] [-p maxprocs] [-x inside] [-o outside] [-c confdir] [-n myname] "
1546 "localip localmask [host...]\n", argv0);
1551 main(int argc, char *argv[])
1559 fmtinstall('I', eipfmt);
1560 fmtinstall('M', eipfmt);
1561 fmtinstall('H', encodefmt);
1568 if((maxprocs = atoi(EARGF(usage()))) < 1)
1569 sysfatal("bad number of procs");
1572 confdir = EARGF(usage());
1575 myname = EARGF(usage());
1578 outside = inside = EARGF(usage());
1581 outside = EARGF(usage());
1589 if(parseip(localip, argv[0]) == -1)
1590 sysfatal("bad local ip: %s", argv[0]);
1591 if(parseipmask(localmask, argv[1]) == -1)
1592 sysfatal("bad local mask: %s", argv[1]);
1593 argv += 2, argc -= 2;
1598 if((myhost = gethost(myname, 0)) == nil)
1599 sysfatal("can't get my host: %r");
1600 if((rpc = getrsarpc()) == nil)
1601 sysfatal("can't find my key in factotum: %r");
1604 for(i = 0; i < argc; i++){
1605 if((h = gethost(argv[i], 0)) == nil)
1606 sysfatal("unknown host: %s", *argv);
1608 sysfatal("will not connect to myself");
1609 if(h->rsapub == nil)
1610 sysfatal("no RSA public key for: %s", h->name);
1613 if((t = lookupnet(localip)) == nil)
1614 sysfatal("no subnet found for local ip %I", localip);
1615 if(t->owner != myhost)
1616 sysfatal("local ip %I belongs to host %s subnet %I/%d",
1617 localip, t->owner->name, t->ip, t->prefixlen);
1621 switch(rfork(RFPROC|RFFDG|RFREND|RFNOTEG)){
1623 sysfatal("can't fork: %r");
1629 fprint(ipcfd, "unbind");
1634 if(rfork(RFPROC|RFMEM) == 0){
1635 tcpclient(listener("tcp", myhost->port, maxprocs));
1638 if(rfork(RFPROC|RFMEM) == 0){
1639 udpclient(listener("udp", myhost->port, maxprocs));
1642 for(i = 0; i < argc; i++){
1643 if((h = gethost(argv[i], 0)) == nil)
1645 if(rfork(RFPROC|RFMEM) == 0){
1646 tcpclient(dialer("tcp", h->addr, h->port, myhost->port));
1649 if(rfork(RFPROC|RFMEM) == 0){
1650 udpclient(dialer("udp", h->addr, h->port, myhost->port));
1654 if(rfork(RFPROC|RFMEM) == 0){