8 typedef struct Call Call;
9 typedef struct Event Event;
11 #define SDB if(debug) fprint(2,
17 Nchan = 10, /* maximum number of channels */
18 Window = 8, /* default window size */
19 Timeout = 60, /* timeout in seconds for control channel */
20 Pktsize = 2000, /* maximum packet size */
21 Tick = 500, /* tick length in milliseconds */
22 Sendtimeout = 4, /* in ticks */
68 int pac; /* server is acting as a PAC */
70 int recvwindow; /* recv windows */
71 int sendwindow; /* send windows */
77 uint seq; /* current seq number - for send */
78 uint ack; /* current acked mesg - for send */
79 uint rseq; /* highest recv seq number for in order packet */
80 uint rack; /* highest ack sent */
82 Event eack; /* recved ack - for send */
85 uchar remoteip[IPaddrlen]; /* remote ip address */
86 int dhcpfd[2]; /* pipe to dhcpclient */
117 uchar local[IPaddrlen];
118 uchar remote[IPaddrlen];
120 uchar ipaddr[IPaddrlen]; /* starting ip addresss to allocate */
127 double rcvtime; /* time at which last request was received */
128 int echoid; /* id of last echo request */
135 GRE_chksum = (1<<15),
136 GRE_routing = (1<<14),
153 void myfatal(char *fmt, ...);
155 #define PSHORT(p, v) ((p)[0]=((v)>>8), (p)[1]=(v))
156 #define PLONG(p, v) (PSHORT(p, (v)>>16), PSHORT(p+2, (v)))
157 #define PSTRING(d,s,n) strncpy((char*)(d), s, n)
158 #define GSHORT(p) (((p)[0]<<8) | ((p)[1]<<0))
159 #define GLONG(p) ((GSHORT((p))<<16) | ((GSHORT((p)+2))<<0))
160 #define GSTRING(d,s,n) strncpy(d, (char*)(s), n), d[(n)-1] = 0
164 int sstart(uchar*, int);
165 int sstop(uchar*, int);
166 int secho(uchar*, int);
167 int scallout(uchar*, int);
168 int scallreq(uchar*, int);
169 int scallcon(uchar*, int);
170 int scallclear(uchar*, int);
171 int scalldis(uchar*, int);
172 int swaninfo(uchar*, int);
173 int slinkinfo(uchar*, int);
175 Call *callalloc(int id);
176 void callclose(Call*);
177 void callfree(Call*);
178 Call *calllookup(int id);
180 void gretimeout(void*);
186 void greack(Call *c);
188 void timeoutthread(void*);
190 int argatoi(char *p);
192 int ipaddralloc(Call *c);
194 void *emallocz(int size);
195 void esignal(Event *e);
196 void ewait(Event *e);
197 int proc(char **argv, int fd0, int fd1, int fd2);
198 double realtime(void);
199 ulong thread(void(*f)(void*), void *a);
202 main(int argc, char *argv[])
205 case 'd': debug++; break;
206 case 'p': srv.pppdir = ARGF(); break;
207 case 'P': srv.pppexec = ARGF(); break;
208 case 'w': srv.recvwindow = argatoi(ARGF()); break;
209 case 'D': drop = atof(ARGF()); break;
214 fmtinstall('I', eipfmt);
215 fmtinstall('E', eipfmt);
216 fmtinstall('V', eipfmt);
217 fmtinstall('M', eipfmt);
219 rfork(RFNOTEG|RFREND);
224 srv.tcpdir = argv[0];
228 syslog(0, LOG, ": src=%I: pptp started: %d", srv.remote, getpid());
230 SDB "\n\n\n%I: pptp started\n", srv.remote EDB
234 thread(timeoutthread, 0);
238 syslog(0, LOG, ": src=%I: server exits", srv.remote);
240 postnote(PNGROUP, getpid(), "die");
247 fprint(2, "usage: pptpd [-dD] [-p ppp-net] [-w window] tcpdir\n");
261 n2 = read(0, buf+n, sizeof(buf)-n);
263 myfatal("bad read on ctl channel: %r");
273 srv.rcvtime = realtime();
281 myfatal("bad magic number: got %x", magic);
283 myfatal("bad message type: %d", type);
286 myfatal("unknown control op: %d", op);
287 case Tstart: /* start-control-connection-request */
308 n2 = scallclear(p, n);
317 n2 = slinkinfo(p, n);
323 myfatal("op=%d: bad length: got %d expected %d", op, len, n2);
329 /* move down partial message */
330 if(p != buf && n != 0)
337 sstart(uchar *p, int n)
339 int ver, frame, bearer, maxchan, firm;
340 char host[64], vendor[64], *sysname;
347 bearer = GLONG(p+20);
348 maxchan = GSHORT(p+24);
350 GSTRING(host, p+28, 64);
351 GSTRING(vendor, p+92, 64);
353 SDB "%I: start ver = %x f = %d b = %d maxchan = %d firm = %d host = %s vendor = %s\n",
354 srv.remote, ver, frame, bearer, maxchan, firm, host, vendor EDB
357 myfatal("bad version: got %x expected %x", ver, Version);
360 myfatal("multiple start messages");
364 sysname = getenv("sysname");
366 strcpy(host, "gnot");
368 strncpy(host, sysname, 64);
371 memset(buf, 0, sizeof(buf));
373 PSHORT(buf+0, sizeof(buf)); /* length */
374 PSHORT(buf+2, 1); /* message type */
375 PLONG(buf+4, Magic); /* magic */
376 PSHORT(buf+8, Rstart); /* op */
377 PSHORT(buf+12, Version); /* version */
378 buf[14] = 1; /* result = ok */
379 PLONG(buf+16, Syncframe|Asyncframe); /* frameing */
380 PLONG(buf+20, Digital|Analog); /* berear capabilities */
381 PSHORT(buf+24, Nchan); /* max channels */
382 PSHORT(buf+26, 1); /* driver version */
383 PSTRING(buf+28, host, 64); /* host name */
384 PSTRING(buf+92, "plan 9", 64); /* vendor */
386 if(write(1, buf, sizeof(buf)) < sizeof(buf))
387 myfatal("write failed: %r");
393 sstop(uchar *p, int n)
402 SDB "%I: stop %d\n", srv.remote, reason EDB
404 memset(buf, 0, sizeof(buf));
405 PSHORT(buf+0, sizeof(buf)); /* length */
406 PSHORT(buf+2, 1); /* message type */
407 PLONG(buf+4, Magic); /* magic */
408 PSHORT(buf+8, Rstop); /* op */
409 buf[12] = 1; /* ok */
411 if(write(1, buf, sizeof(buf)) < sizeof(buf))
412 myfatal("write failed: %r");
418 secho(uchar *p, int n)
427 SDB "%I: echo %d\n", srv.remote, id EDB
429 memset(buf, 0, sizeof(buf));
430 PSHORT(buf+0, sizeof(buf)); /* length */
431 PSHORT(buf+2, 1); /* message type */
432 PLONG(buf+4, Magic); /* magic */
433 PSHORT(buf+8, Recho); /* op */
434 PLONG(buf+12, id); /* id */
437 if(write(1, buf, sizeof(buf)) < sizeof(buf))
438 myfatal("write failed: %r");
444 scallout(uchar *p, int n)
447 int minbps, maxbps, bearer, frame;
450 char phone[64], sub[64], buf[32];
457 myfatal("%I: did not recieve start message", srv.remote);
460 serial = GSHORT(p+14);
461 minbps = GLONG(p+16);
462 maxbps = GLONG(p+20);
463 bearer = GLONG(p+24);
465 window = GSHORT(p+32);
466 delay = GSHORT(p+34);
467 nphone = GSHORT(p+36);
468 GSTRING(phone, p+40, 64);
469 GSTRING(sub, p+104, 64);
471 SDB "%I: callout id = %d serial = %d bps=[%d,%d] b=%x f=%x win = %d delay = %d np=%d phone=%s sub=%s\n",
472 srv.remote, id, serial, minbps, maxbps, bearer, frame, window, delay, nphone, phone, sub EDB
475 c->sendwindow = window;
478 c->recvwindow = srv.recvwindow;
480 memset(buf, 0, sizeof(buf));
481 PSHORT(buf+0, sizeof(buf)); /* length */
482 PSHORT(buf+2, 1); /* message type */
483 PLONG(buf+4, Magic); /* magic */
484 PSHORT(buf+8, Rcallout); /* op */
485 PSHORT(buf+12, id); /* call id */
486 PSHORT(buf+14, id); /* peer id */
487 buf[16] = 1; /* ok */
488 PLONG(buf+20, 10000000); /* speed */
489 PSHORT(buf+24, c->recvwindow); /* window size */
490 PSHORT(buf+26, 0); /* delay */
491 PLONG(buf+28, 0); /* channel id */
493 if(write(1, buf, sizeof(buf)) < sizeof(buf))
494 myfatal("write failed: %r");
500 scallreq(uchar *p, int n)
505 myfatal("callreq: not done yet");
510 scallcon(uchar *p, int n)
515 myfatal("callcon: not done yet");
520 scallclear(uchar *p, int n)
530 SDB "%I: callclear id=%d\n", srv.remote, id EDB
532 if(c = calllookup(id)) {
537 memset(buf, 0, sizeof(buf));
538 PSHORT(buf+0, sizeof(buf)); /* length */
539 PSHORT(buf+2, 1); /* message type */
540 PLONG(buf+4, Magic); /* magic */
541 PSHORT(buf+8, Acalldis); /* op */
542 PSHORT(buf+12, id); /* id */
543 buf[14] = 3; /* reply to callclear */
545 if(write(1, buf, sizeof(buf)) < sizeof(buf))
546 myfatal("write failed: %r");
552 scalldis(uchar *p, int n)
562 SDB "%I: calldis id=%d res=%d\n", srv.remote, id, res EDB
564 if(c = calllookup(id)) {
573 swaninfo(uchar *p, int n)
582 SDB "%I: waninfo id = %d\n", srv.remote, id EDB
586 c->err.crc = GLONG(p+16);
587 c->err.frame = GLONG(p+20);
588 c->err.hardware = GLONG(p+24);
589 c->err.overrun = GLONG(p+28);
590 c->err.timeout = GLONG(p+32);
591 c->err.align = GLONG(p+36);
601 slinkinfo(uchar *p, int n)
605 int sendaccm, recvaccm;
610 sendaccm = GLONG(p+16);
611 recvaccm = GLONG(p+20);
613 SDB "%I: linkinfo id=%d saccm=%ux raccm=%ux\n", srv.remote, id, sendaccm, recvaccm EDB
615 if(c = calllookup(id)) {
616 c->sendaccm = sendaccm;
617 c->recvaccm = recvaccm;
630 char *argv[30], local[20], remote[20], **p;
636 for(c=srv.hash[h]; c; c=c->next)
638 myfatal("callalloc: duplicate id: %d", id);
639 c = emallocz(sizeof(Call));
646 myfatal("callalloc: could not alloc remote ip address");
649 myfatal("callalloc: pipe failed: %r");
658 sprint(local, "%I", srv.ipaddr);
660 sprint(remote, "%I", c->remoteip);
664 proc(argv, pfd[0], pfd[0], 2);
670 c->next = srv.hash[h];
678 thread(gretimeout, c);
680 syslog(0, LOG, ": src=%I: call started: id=%d: remote ip=%I", srv.remote, id, c->remoteip);
692 syslog(0, LOG, ": src=%I: call closed: id=%d: send=%d sendack=%d recv=%d recvack=%d dropped=%d missing=%d sendwait=%d sendtimeout=%d",
693 srv.remote, c->id, c->stat.send, c->stat.sendack, c->stat.recv, c->stat.recvack,
694 c->stat.dropped, c->stat.missing, c->stat.sendwait, c->stat.sendtimeout);
710 for(c=srv.hash[h],oc=0; c; oc=c,c=c->next)
714 srv.hash[h] = c->next;
734 /* already unhooked from hash list - see callclose */
735 assert(c->closed == 1);
737 assert(c->next == 0);
739 SDB "call free\n" EDB
752 for(c=srv.hash[h]; c; c=c->next)
769 sprint(buf, "%s/local", srv.tcpdir);
770 if((fd = open(buf, OREAD)) < 0)
771 myfatal("could not open %s: %r", buf);
772 if((n = read(fd, buf, sizeof(buf))) < 0)
773 myfatal("could not read %s: %r", buf);
775 parseip(srv.local, buf);
778 sprint(buf, "%s/remote", srv.tcpdir);
779 if((fd = open(buf, OREAD)) < 0)
780 myfatal("could not open %s: %r", buf);
781 if((n = read(fd, buf, sizeof(buf))) < 0)
782 myfatal("could not read %s: %r", buf);
784 parseip(srv.remote, buf);
791 srv.pppexec = "/bin/ip/ppp";
793 if(myipaddr(srv.ipaddr, srv.pppdir) < 0)
794 myfatal("could not read local ip addr: %r");
795 if(srv.recvwindow == 0)
796 srv.recvwindow = Window;
805 SDB "srv.tcpdir = %s\n", srv.tcpdir EDB
806 strcpy(addr, srv.tcpdir);
807 p = strrchr(addr, '/');
809 myfatal("bad tcp dir: %s", srv.tcpdir);
811 p = strrchr(addr, '/');
813 myfatal("bad tcp dir: %s", srv.tcpdir);
814 sprint(p, "/gre!%I!34827", srv.remote);
816 SDB "addr = %s\n", addr EDB
818 fd = dial(addr, 0, 0, &cfd);
821 myfatal("%I: dial %s failed: %r", srv.remote, addr);
832 uchar buf[Pktsize], *p;
834 int flag, prot, len, callid;
835 uchar src[IPaddrlen], dst[IPaddrlen];
838 static double t, last;
841 n = read(srv.grefd, buf, sizeof(buf));
843 myfatal("%I: bad read on gre: %r", srv.remote);
845 myfatal("%I: gre read: buf too small", srv.remote);
854 if(ipcmp(src, srv.remote) != 0 || ipcmp(dst, srv.local) != 0)
855 myfatal("%I: gre read bad address src=%I dst=%I", srv.remote, src, dst);
858 myfatal("%I: gre read gave bad protocol", srv.remote);
860 if(flag & (GRE_chksum|GRE_routing)){
865 myfatal("%I: gre packet does not contain a key: f=%ux",
869 callid = GSHORT(p+2);
872 c = calllookup(callid);
874 SDB "%I: unknown callid: %d\n", srv.remote, callid EDB
894 /* skip routing if present */
895 if(flag&GRE_routing) {
896 while((i=p[3]) != 0) {
903 myfatal("%I: bad len in gre packet", srv.remote);
905 if((int)(ack-c->ack) > 0) {
917 SDB "%I: %.3f (%.3f): gre %d: recv ack a=%ux n=%d flag=%ux\n", srv.remote, t, t-last,
918 c->id, ack, n, flag EDB
922 SDB "%I: %.3f (%.3f): gre %d: recv s=%ux a=%ux len=%d\n", srv.remote, t, t-last,
923 c->id, rseq, ack, len EDB
926 * the following handles the case of a single pair of packets
927 * received out of order
930 if(n > 0 && (drop == 0. || frand() > drop)) {
931 c->stat.missing += n-1;
933 write(c->pppfd, p, len);
935 /* out of sequence - drop on the floor */
938 SDB "%I: %.3f: gre %d: recv out of order or dup packet: seq=%ux len=%d\n",
939 srv.remote, realtime(), c->id, rseq, len EDB
944 if((int)(rseq-c->rseq) > 0)
950 /* open up client window */
951 if((int)(c->rseq-c->rack) > (c->recvwindow>>1))
968 SDB "%I: %.3f: gre %d: send ack %ux\n", srv.remote, realtime(), c->id, c->rseq EDB
970 v6tov4(buf+0, srv.local); /* source */
971 v6tov4(buf+4, srv.remote); /* source */
972 PSHORT(buf+8, GRE_key|GRE_ack|1);
973 PSHORT(buf+10, GRE_ppp);
975 PSHORT(buf+14, c->id);
976 PLONG(buf+16, c->rseq);
978 write(srv.grefd, buf, sizeof(buf));
1007 uchar buf[2000], *p;
1014 n = read(c->pppfd, p, sizeof(buf)-24);
1020 /* add gre header */
1024 while(c->seq-c->ack>c->sendwindow && c->tick-tick<Sendtimeout && !c->closed) {
1026 SDB "window full seq = %d ack = %ux window = %ux\n", c->seq, c->ack, c->sendwindow EDB
1032 if(c->tick-tick >= Sendtimeout) {
1033 c->stat.sendtimeout++;
1034 SDB "send timeout = %d ack = %ux window = %ux\n", c->seq, c->ack, c->sendwindow EDB
1037 v6tov4(buf+0, srv.local); /* source */
1038 v6tov4(buf+4, srv.remote); /* source */
1039 PSHORT(buf+8, GRE_key|GRE_seq|GRE_ack|1);
1040 PSHORT(buf+10, GRE_ppp);
1042 PSHORT(buf+14, c->id);
1043 PLONG(buf+16, c->seq);
1044 PLONG(buf+20, c->rseq);
1049 SDB "%I: %.3f: gre %d: send s=%ux a=%ux len=%d\n", srv.remote, realtime(),
1050 c->id, c->seq, c->rseq, n EDB
1052 if(drop == 0. || frand() > drop)
1053 if(write(srv.grefd, buf, n+24)<n+24)
1054 myfatal("pppread: write failed: %r");
1059 SDB "pppread exit: %d\n", c->id);
1066 timeoutthread(void*)
1072 if(realtime() - srv.rcvtime > 5*60)
1073 myfatal("server timedout");
1079 /* use syslog() rather than fprint(2, ...) */
1081 myfatal(char *fmt, ...)
1087 /* NT don't seem to like us just going away */
1088 memset(buf, 0, sizeof(buf));
1089 PSHORT(buf+0, sizeof(buf)); /* length */
1090 PSHORT(buf+2, 1); /* message type */
1091 PLONG(buf+4, Magic); /* magic */
1092 PSHORT(buf+8, Tstop); /* op */
1093 buf[12] = 3; /* local shutdown */
1095 write(1, buf, sizeof(buf));
1098 vseprint(sbuf, sbuf+sizeof(sbuf), fmt, arg);
1101 SDB "%I: fatal: %s\n", srv.remote, sbuf EDB
1102 syslog(0, LOG, ": src=%I: fatal: %s", srv.remote, sbuf);
1109 postnote(PNGROUP, getpid(), "die");
1122 i = strtol(p, &q, 0);
1129 dhcpclientwatch(void *a)
1135 if(read(c->dhcpfd[0], buf, sizeof(buf)) <= 0)
1139 myfatal("dhcpclient terminated");
1145 ipaddralloc(Call *c)
1151 argv[0] = "/bin/ip/dhcpclient";
1153 argv[2] = srv.pppdir;
1157 myfatal("ipaddralloc: pipe failed: %r");
1159 myfatal("ipaddralloc: pipe failed: %r");
1161 if(proc(argv, pfd[0][0], pfd[1][1], 2) < 0)
1162 myfatal("ipaddralloc: proc failed: %r");
1166 c->dhcpfd[0] = pfd[1][0];
1167 c->dhcpfd[1] = pfd[0][1];
1169 Binit(&bio, pfd[1][0], OREAD);
1171 p = Brdline(&bio, '\n');
1174 if(strncmp(p, "ip=", 3) == 0) {
1176 parseip(c->remoteip, p);
1177 } else if(strncmp(p, "end\n", 4) == 0)
1185 thread(dhcpclientwatch, c);
1187 return ipcmp(c->remoteip, IPnoaddr) != 0;
1200 assert(e->ready == 0);
1202 rendezvous(e, (void*)1);
1211 assert(e->wait == 0);
1217 rendezvous(e, (void*)2);
1221 qunlock(&e->waitlk);
1225 thread(void(*f)(void*), void *a)
1228 pid=rfork(RFNOWAIT|RFMEM|RFPROC);
1230 myfatal("rfork failed: %r");
1234 return 0; // never reaches here
1242 return times(0) / 1000.0;
1251 myfatal("malloc failed: %r");
1262 if((fd = open("#d", OREAD)) < 0)
1265 n = dirreadall(fd, &d);
1266 for(p = d; n > 0; n--, p++) {
1275 proc(char **argv, int fd0, int fd1, int fd2)
1278 char *arg0, file[200];
1284 if(access(file, 1) < 0) {
1285 if(strncmp(arg0, "/", 1)==0
1286 || strncmp(arg0, "#", 1)==0
1287 || strncmp(arg0, "./", 2)==0
1288 || strncmp(arg0, "../", 3)==0)
1290 sprint(file, "/bin/%s", arg0);
1291 if(access(file, 1) < 0)
1295 flag = RFPROC|RFFDG|RFENVG|RFNOWAIT;
1296 if((r = rfork(flag)) != 0) {
1329 myfatal("proc: exec failed: %r");