pptpd.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ip.h>
  5. #define LOG "pptpd"
  6. typedef struct Call Call;
  7. typedef struct Event Event;
  8. #define SDB if(debug) fprint(2,
  9. #define EDB );
  10. enum {
  11. Magic = 0x1a2b3c4d,
  12. Nhash = 17,
  13. Nchan = 10, /* maximum number of channels */
  14. Window = 8, /* default window size */
  15. Timeout = 60, /* timeout in seconds for control channel */
  16. Pktsize = 2000, /* maximum packet size */
  17. Tick = 500, /* tick length in milliseconds */
  18. Sendtimeout = 4, /* in ticks */
  19. };
  20. enum {
  21. Syncframe = 0x1,
  22. Asyncframe = 0x2,
  23. Analog = 0x1,
  24. Digital = 0x2,
  25. Version = 0x100,
  26. };
  27. enum {
  28. Tstart = 1,
  29. Rstart = 2,
  30. Tstop = 3,
  31. Rstop = 4,
  32. Techo = 5,
  33. Recho = 6,
  34. Tcallout = 7,
  35. Rcallout = 8,
  36. Tcallreq = 9,
  37. Rcallreq = 10,
  38. Acallcon = 11,
  39. Tcallclear = 12,
  40. Acalldis = 13,
  41. Awaninfo = 14,
  42. Alinkinfo = 15,
  43. };
  44. struct Event {
  45. QLock;
  46. QLock waitlk;
  47. int wait;
  48. int ready;
  49. };
  50. struct Call {
  51. int ref;
  52. QLock lk;
  53. int id;
  54. int serial;
  55. int pppfd;
  56. int closed;
  57. int pac; /* server is acting as a PAC */
  58. int recvwindow; /* recv windows */
  59. int sendwindow; /* send windows */
  60. int delay;
  61. int sendaccm;
  62. int recvaccm;
  63. uint seq; /* current seq number - for send */
  64. uint ack; /* current acked mesg - for send */
  65. uint rseq; /* highest recv seq number for in order packet */
  66. uint rack; /* highest ack sent */
  67. Event eack; /* recved ack - for send */
  68. ulong tick;
  69. uchar remoteip[IPaddrlen]; /* remote ip address */
  70. int dhcpfd[2]; /* pipe to dhcpclient */
  71. /* error stats */
  72. struct {
  73. int crc;
  74. int frame;
  75. int hardware;
  76. int overrun;
  77. int timeout;
  78. int align;
  79. } err;
  80. struct {
  81. int send;
  82. int sendack;
  83. int recv;
  84. int recvack;
  85. int dropped;
  86. int missing;
  87. int sendwait;
  88. int sendtimeout;
  89. } stat;
  90. Call *next;
  91. };
  92. struct {
  93. QLock lk;
  94. int start;
  95. int grefd;
  96. int grecfd;
  97. uchar local[IPaddrlen];
  98. uchar remote[IPaddrlen];
  99. char *tcpdir;
  100. uchar ipaddr[IPaddrlen]; /* starting ip addresss to allocate */
  101. int recvwindow;
  102. char *pppdir;
  103. char *pppexec;
  104. double rcvtime; /* time at which last request was received */
  105. int echoid; /* id of last echo request */
  106. Call *hash[Nhash];
  107. } srv;
  108. /* GRE flag bits */
  109. enum {
  110. GRE_chksum = (1<<15),
  111. GRE_routing = (1<<14),
  112. GRE_key = (1<<13),
  113. GRE_seq = (1<<12),
  114. GRE_srcrt = (1<<11),
  115. GRE_recur = (7<<8),
  116. GRE_ack = (1<<7),
  117. GRE_ver = 0x7,
  118. };
  119. /* GRE protocols */
  120. enum {
  121. GRE_ppp = 0x880b,
  122. };
  123. int debug;
  124. double drop;
  125. void myfatal(char *fmt, ...);
  126. #define PSHORT(p, v) ((p)[0]=((v)>>8), (p)[1]=(v))
  127. #define PLONG(p, v) (PSHORT(p, (v)>>16), PSHORT(p+2, (v)))
  128. #define PSTRING(d,s,n) strncpy((char*)(d), s, n)
  129. #define GSHORT(p) (((p)[0]<<8) | ((p)[1]<<0))
  130. #define GLONG(p) ((GSHORT((p))<<16) | ((GSHORT((p)+2))<<0))
  131. #define GSTRING(d,s,n) strncpy(d, (char*)(s), n), d[(n)-1] = 0
  132. void serve(void);
  133. int sstart(uchar*, int);
  134. int sstop(uchar*, int);
  135. int secho(uchar*, int);
  136. int scallout(uchar*, int);
  137. int scallreq(uchar*, int);
  138. int scallcon(uchar*, int);
  139. int scallclear(uchar*, int);
  140. int scalldis(uchar*, int);
  141. int swaninfo(uchar*, int);
  142. int slinkinfo(uchar*, int);
  143. Call *callalloc(int id);
  144. void callclose(Call*);
  145. void callfree(Call*);
  146. Call *calllookup(int id);
  147. void gretimeout(void*);
  148. void pppread(void*);
  149. void srvinit(void);
  150. void greinit(void);
  151. void greread(void*);
  152. void greack(Call *c);
  153. void timeoutthread(void*);
  154. ulong parseip(uchar *ato, char *from);
  155. int argatoi(char *p);
  156. void usage(void);
  157. int ipaddralloc(Call *c);
  158. void esignal(Event *e);
  159. void ewait(Event *e);
  160. ulong thread(void(*f)(void*), void *a);
  161. double realtime(void);
  162. void *emallocz(int size);
  163. int proc(char **argv, int fd0, int fd1, int fd2);
  164. void
  165. main(int argc, char *argv[])
  166. {
  167. ARGBEGIN{
  168. case 'd': debug++; break;
  169. case 'p': srv.pppdir = ARGF(); break;
  170. case 'P': srv.pppexec = ARGF(); break;
  171. case 'w': srv.recvwindow = argatoi(ARGF()); break;
  172. case 'D': drop = atof(ARGF()); break;
  173. default:
  174. usage();
  175. }ARGEND
  176. fmtinstall('I', eipfmt);
  177. fmtinstall('E', eipfmt);
  178. fmtinstall('V', eipfmt);
  179. fmtinstall('M', eipfmt);
  180. rfork(RFNOTEG|RFREND);
  181. if(argc != 1)
  182. usage();
  183. srv.tcpdir = argv[0];
  184. srvinit();
  185. syslog(0, LOG, ": src=%I: pptp started: %d", srv.remote, getpid());
  186. SDB "\n\n\n%I: pptp started\n", srv.remote EDB
  187. greinit();
  188. thread(timeoutthread, 0);
  189. serve();
  190. syslog(0, LOG, ": src=%I: server exits", srv.remote);
  191. exits(0);
  192. }
  193. void
  194. usage(void)
  195. {
  196. fprint(2, "usage: pptpd [-dD] [-p ppp-net] [-w window] tcpdir\n");
  197. exits("usage");
  198. }
  199. void
  200. serve(void)
  201. {
  202. uchar buf[2000], *p;
  203. int n, n2, len;
  204. int magic;
  205. int op, type;
  206. n = 0;
  207. for(;;) {
  208. n2 = read(0, buf+n, sizeof(buf)-n);
  209. if(n2 < 0)
  210. myfatal("bad read on ctl channel: %r");
  211. if(n2 == 0)
  212. break;
  213. n += n2;
  214. p = buf;
  215. for(;;) {
  216. if(n < 12)
  217. break;
  218. qlock(&srv.lk);
  219. srv.rcvtime = realtime();
  220. qunlock(&srv.lk);
  221. len = GSHORT(p);
  222. type = GSHORT(p+2);
  223. magic = GLONG(p+4);
  224. op = GSHORT(p+8);
  225. if(magic != Magic)
  226. myfatal("bad magic number: got %x", magic);
  227. if(type != 1)
  228. myfatal("bad message type: %d", type);
  229. switch(op) {
  230. default:
  231. myfatal("unknown control op: %d", op);
  232. case Tstart: /* start-control-connection-request */
  233. n2 = sstart(p, n);
  234. break;
  235. case Tstop:
  236. n2 = sstop(p, n);
  237. if(n2 > 0)
  238. return;
  239. break;
  240. case Techo:
  241. n2 = secho(p, n);
  242. break;
  243. case Tcallout:
  244. n2 = scallout(p, n);
  245. break;
  246. case Tcallreq:
  247. n2 = scallreq(p, n);
  248. break;
  249. case Acallcon:
  250. n2 = scallcon(p, n);
  251. break;
  252. case Tcallclear:
  253. n2 = scallclear(p, n);
  254. break;
  255. case Acalldis:
  256. n2 = scalldis(p, n);
  257. break;
  258. case Awaninfo:
  259. n2 = swaninfo(p, n);
  260. break;
  261. case Alinkinfo:
  262. n2 = slinkinfo(p, n);
  263. break;
  264. }
  265. if(n2 == 0)
  266. break;
  267. if(n2 != len)
  268. myfatal("op=%d: bad length: got %d expected %d", op, len, n2);
  269. n -= n2;
  270. p += n2;
  271. }
  272. /* move down partial message */
  273. if(p != buf && n != 0)
  274. memmove(buf, p, n);
  275. }
  276. }
  277. int
  278. sstart(uchar *p, int n)
  279. {
  280. int ver, frame, bearer, maxchan, firm;
  281. char host[64], vendor[64], *sysname;
  282. uchar buf[156];
  283. if(n < 156)
  284. return 0;
  285. ver = GSHORT(p+12);
  286. frame = GLONG(p+16);
  287. bearer = GLONG(p+20);
  288. maxchan = GSHORT(p+24);
  289. firm = GSHORT(p+26);
  290. GSTRING(host, p+28, 64);
  291. GSTRING(vendor, p+92, 64);
  292. SDB "%I: start ver = %x f = %d b = %d maxchan = %d firm = %d host = %s vendor = %s\n",
  293. srv.remote, ver, frame, bearer, maxchan, firm, host, vendor EDB
  294. if(ver != Version)
  295. myfatal("bad version: got %x expected %x", ver, Version);
  296. if(srv.start)
  297. myfatal("multiple start messages");
  298. srv.start = 1;
  299. sysname = getenv("sysname");
  300. if(sysname == 0)
  301. strcpy(host, "gnot");
  302. else
  303. strncpy(host, sysname, 64);
  304. free(sysname);
  305. memset(buf, 0, sizeof(buf));
  306. PSHORT(buf+0, sizeof(buf)); /* length */
  307. PSHORT(buf+2, 1); /* message type */
  308. PLONG(buf+4, Magic); /* magic */
  309. PSHORT(buf+8, Rstart); /* op */
  310. PSHORT(buf+12, Version); /* version */
  311. buf[14] = 1; /* result = ok */
  312. PLONG(buf+16, Syncframe|Asyncframe); /* frameing */
  313. PLONG(buf+20, Digital|Analog); /* berear capabilities */
  314. PSHORT(buf+24, Nchan); /* max channels */
  315. PSHORT(buf+26, 1); /* driver version */
  316. PSTRING(buf+28, host, 64); /* host name */
  317. PSTRING(buf+92, "plan 9", 64); /* vendor */
  318. if(write(1, buf, sizeof(buf)) < sizeof(buf))
  319. myfatal("write failed: %r");
  320. return 156;
  321. }
  322. int
  323. sstop(uchar *p, int n)
  324. {
  325. int reason;
  326. uchar buf[16];
  327. if(n < 16)
  328. return 0;
  329. reason = p[12];
  330. SDB "%I: stop %d\n", srv.remote, reason EDB
  331. memset(buf, 0, sizeof(buf));
  332. PSHORT(buf+0, sizeof(buf)); /* length */
  333. PSHORT(buf+2, 1); /* message type */
  334. PLONG(buf+4, Magic); /* magic */
  335. PSHORT(buf+8, Rstop); /* op */
  336. buf[12] = 1; /* ok */
  337. if(write(1, buf, sizeof(buf)) < sizeof(buf))
  338. myfatal("write failed: %r");
  339. return 16;
  340. }
  341. int
  342. secho(uchar *p, int n)
  343. {
  344. int id;
  345. uchar buf[20];
  346. if(n < 16)
  347. return 0;
  348. id = GLONG(p+12);
  349. SDB "%I: echo %d\n", srv.remote, id EDB
  350. memset(buf, 0, sizeof(buf));
  351. PSHORT(buf+0, sizeof(buf)); /* length */
  352. PSHORT(buf+2, 1); /* message type */
  353. PLONG(buf+4, Magic); /* magic */
  354. PSHORT(buf+8, Recho); /* op */
  355. PLONG(buf+12, id); /* id */
  356. p[16] = 1; /* ok */
  357. if(write(1, buf, sizeof(buf)) < sizeof(buf))
  358. myfatal("write failed: %r");
  359. return 16;
  360. }
  361. int
  362. scallout(uchar *p, int n)
  363. {
  364. int id, serial;
  365. int minbps, maxbps, bearer, frame;
  366. int window, delay;
  367. int nphone;
  368. char phone[64], sub[64], buf[32];
  369. Call *c;
  370. if(n < 168)
  371. return 0;
  372. if(!srv.start)
  373. myfatal("%I: did not recieve start message", srv.remote);
  374. id = GSHORT(p+12);
  375. serial = GSHORT(p+14);
  376. minbps = GLONG(p+16);
  377. maxbps = GLONG(p+20);
  378. bearer = GLONG(p+24);
  379. frame = GLONG(p+28);
  380. window = GSHORT(p+32);
  381. delay = GSHORT(p+34);
  382. nphone = GSHORT(p+36);
  383. GSTRING(phone, p+40, 64);
  384. GSTRING(sub, p+104, 64);
  385. 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",
  386. srv.remote, id, serial, minbps, maxbps, bearer, frame, window, delay, nphone, phone, sub EDB
  387. c = callalloc(id);
  388. c->sendwindow = window;
  389. c->delay = delay;
  390. c->pac = 1;
  391. c->recvwindow = srv.recvwindow;
  392. memset(buf, 0, sizeof(buf));
  393. PSHORT(buf+0, sizeof(buf)); /* length */
  394. PSHORT(buf+2, 1); /* message type */
  395. PLONG(buf+4, Magic); /* magic */
  396. PSHORT(buf+8, Rcallout); /* op */
  397. PSHORT(buf+12, id); /* call id */
  398. PSHORT(buf+14, id); /* peer id */
  399. buf[16] = 1; /* ok */
  400. PLONG(buf+20, 10000000); /* speed */
  401. PSHORT(buf+24, c->recvwindow); /* window size */
  402. PSHORT(buf+26, 0); /* delay */
  403. PLONG(buf+28, 0); /* channel id */
  404. if(write(1, buf, sizeof(buf)) < sizeof(buf))
  405. myfatal("write failed: %r");
  406. return 168;
  407. }
  408. int
  409. scallreq(uchar *p, int n)
  410. {
  411. USED(p);
  412. USED(n);
  413. myfatal("callreq: not done yet");
  414. return 0;
  415. }
  416. int
  417. scallcon(uchar *p, int n)
  418. {
  419. USED(p);
  420. USED(n);
  421. myfatal("callcon: not done yet");
  422. return 0;
  423. }
  424. int
  425. scallclear(uchar *p, int n)
  426. {
  427. Call *c;
  428. int id;
  429. uchar buf[148];
  430. if(n < 16)
  431. return 0;
  432. id = GSHORT(p+12);
  433. SDB "%I: callclear id=%d\n", srv.remote, id EDB
  434. if(c = calllookup(id)) {
  435. callclose(c);
  436. callfree(c);
  437. }
  438. memset(buf, 0, sizeof(buf));
  439. PSHORT(buf+0, sizeof(buf)); /* length */
  440. PSHORT(buf+2, 1); /* message type */
  441. PLONG(buf+4, Magic); /* magic */
  442. PSHORT(buf+8, Acalldis); /* op */
  443. PSHORT(buf+12, id); /* id */
  444. buf[14] = 3; /* reply to callclear */
  445. if(write(1, buf, sizeof(buf)) < sizeof(buf))
  446. myfatal("write failed: %r");
  447. return 16;
  448. }
  449. int
  450. scalldis(uchar *p, int n)
  451. {
  452. Call *c;
  453. int id, res;
  454. if(n < 148)
  455. return 0;
  456. id = GSHORT(p+12);
  457. res = p[14];
  458. SDB "%I: calldis id=%d res=%d\n", srv.remote, id, res EDB
  459. if(c = calllookup(id)) {
  460. callclose(c);
  461. callfree(c);
  462. }
  463. return 148;
  464. }
  465. int
  466. swaninfo(uchar *p, int n)
  467. {
  468. Call *c;
  469. int id;
  470. if(n < 40)
  471. return 0;
  472. id = GSHORT(p+12);
  473. SDB "%I: waninfo id = %d\n", srv.remote, id EDB
  474. c = calllookup(id);
  475. if(c != 0) {
  476. c->err.crc = GLONG(p+16);
  477. c->err.frame = GLONG(p+20);
  478. c->err.hardware = GLONG(p+24);
  479. c->err.overrun = GLONG(p+28);
  480. c->err.timeout = GLONG(p+32);
  481. c->err.align = GLONG(p+36);
  482. callfree(c);
  483. }
  484. return 40;
  485. }
  486. int
  487. slinkinfo(uchar *p, int n)
  488. {
  489. Call *c;
  490. int id;
  491. int sendaccm, recvaccm;
  492. if(n < 24)
  493. return 0;
  494. id = GSHORT(p+12);
  495. sendaccm = GLONG(p+16);
  496. recvaccm = GLONG(p+20);
  497. SDB "%I: linkinfo id=%d saccm=%ux raccm=%ux\n", srv.remote, id, sendaccm, recvaccm EDB
  498. if(c = calllookup(id)) {
  499. c->sendaccm = sendaccm;
  500. c->recvaccm = recvaccm;
  501. callfree(c);
  502. }
  503. return 24;
  504. }
  505. Call*
  506. callalloc(int id)
  507. {
  508. uint h;
  509. Call *c;
  510. char buf[300], *argv[30], local[20], remote[20], **p;
  511. int fd, pfd[2], n;
  512. h = id%Nhash;
  513. qlock(&srv.lk);
  514. for(c=srv.hash[h]; c; c=c->next)
  515. if(c->id == id)
  516. myfatal("callalloc: duplicate id: %d", id);
  517. c = emallocz(sizeof(Call));
  518. c->ref = 1;
  519. c->id = id;
  520. c->sendaccm = ~0;
  521. c->recvaccm = ~0;
  522. if(!ipaddralloc(c))
  523. myfatal("callalloc: could not alloc remote ip address");
  524. if(pipe(pfd) < 0)
  525. myfatal("callalloc: pipe failed: %r");
  526. sprint(buf, "%s/ipifc/clone", srv.pppdir);
  527. fd = open(buf, OWRITE);
  528. if(fd < 0)
  529. myfatal("callalloc: could not open %s: %r", buf);
  530. n = sprint(buf, "iprouting");
  531. if(write(fd, buf, n) < n)
  532. myfatal("callalloc: write to ifc failed: %r");
  533. close(fd);
  534. p = argv;
  535. *p++ = srv.pppexec;
  536. *p++ = "-SC";
  537. *p++ = "-x";
  538. *p++ = srv.pppdir;
  539. if(debug)
  540. *p++ = "-d";
  541. sprint(local, "%I", srv.ipaddr);
  542. *p++ = local;
  543. sprint(remote, "%I", c->remoteip);
  544. *p++ = remote;
  545. *p = 0;
  546. proc(argv, pfd[0], pfd[0], 2);
  547. close(pfd[0]);
  548. c->pppfd = pfd[1];
  549. c->next = srv.hash[h];
  550. srv.hash[h] = c;
  551. qunlock(&srv.lk);
  552. c->ref++;
  553. thread(pppread, c);
  554. c->ref++;
  555. thread(gretimeout, c);
  556. syslog(0, LOG, ": src=%I: call started: id=%d: remote ip=%I", srv.remote, id, c->remoteip);
  557. return c;
  558. }
  559. void
  560. callclose(Call *c)
  561. {
  562. Call *oc;
  563. int id;
  564. uint h;
  565. 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",
  566. srv.remote, c->id, c->stat.send, c->stat.sendack, c->stat.recv, c->stat.recvack,
  567. c->stat.dropped, c->stat.missing, c->stat.sendwait, c->stat.sendtimeout);
  568. qlock(&srv.lk);
  569. if(c->closed) {
  570. qunlock(&srv.lk);
  571. return;
  572. }
  573. c->closed = 1;
  574. close(c->dhcpfd[0]);
  575. close(c->dhcpfd[1]);
  576. close(c->pppfd);
  577. c->pppfd = -1;
  578. h = c->id%Nhash;
  579. id = c->id;
  580. for(c=srv.hash[h],oc=0; c; oc=c,c=c->next)
  581. if(c->id == id)
  582. break;
  583. if(oc == 0)
  584. srv.hash[h] = c->next;
  585. else
  586. oc->next = c->next;
  587. c->next = 0;
  588. qunlock(&srv.lk);
  589. callfree(c);
  590. }
  591. void
  592. callfree(Call *c)
  593. {
  594. int ref;
  595. qlock(&srv.lk);
  596. ref = --c->ref;
  597. qunlock(&srv.lk);
  598. if(ref > 0)
  599. return;
  600. /* already unhooked from hash list - see callclose */
  601. assert(c->closed == 1);
  602. assert(ref == 0);
  603. assert(c->next == 0);
  604. SDB "call free\n" EDB
  605. free(c);
  606. }
  607. Call*
  608. calllookup(int id)
  609. {
  610. uint h;
  611. Call *c;
  612. h = id%Nhash;
  613. qlock(&srv.lk);
  614. for(c=srv.hash[h]; c; c=c->next)
  615. if(c->id == id)
  616. break;
  617. if(c != 0)
  618. c->ref++;
  619. qunlock(&srv.lk);
  620. return c;
  621. }
  622. void
  623. srvinit(void)
  624. {
  625. char buf[100];
  626. int fd, n;
  627. sprint(buf, "%s/local", srv.tcpdir);
  628. if((fd = open(buf, OREAD)) < 0)
  629. myfatal("could not open %s: %r", buf);
  630. if((n = read(fd, buf, sizeof(buf))) < 0)
  631. myfatal("could not read %s: %r", buf);
  632. buf[n] = 0;
  633. parseip(srv.local, buf);
  634. close(fd);
  635. sprint(buf, "%s/remote", srv.tcpdir);
  636. if((fd = open(buf, OREAD)) < 0)
  637. myfatal("could not open %s: %r", buf);
  638. if((n = read(fd, buf, sizeof(buf))) < 0)
  639. myfatal("could not read %s: %r", buf);
  640. buf[n] = 0;
  641. parseip(srv.remote, buf);
  642. close(fd);
  643. if(srv.pppdir == 0)
  644. srv.pppdir = "/net";
  645. if(srv.pppexec == 0)
  646. srv.pppexec = "/bin/ip/ppp";
  647. if(myipaddr(srv.ipaddr, srv.pppdir) < 0)
  648. myfatal("could not read local ip addr: %r");
  649. if(srv.recvwindow == 0)
  650. srv.recvwindow = Window;
  651. }
  652. void
  653. greinit(void)
  654. {
  655. char addr[100], *p;
  656. int fd, cfd;
  657. SDB "srv.tcpdir = %s\n", srv.tcpdir EDB
  658. strcpy(addr, srv.tcpdir);
  659. p = strrchr(addr, '/');
  660. if(p == 0)
  661. myfatal("bad tcp dir: %s", srv.tcpdir);
  662. *p = 0;
  663. p = strrchr(addr, '/');
  664. if(p == 0)
  665. myfatal("bad tcp dir: %s", srv.tcpdir);
  666. sprint(p, "/gre!%I!34827", srv.remote);
  667. SDB "addr = %s\n", addr EDB
  668. fd = dial(addr, 0, 0, &cfd);
  669. if(fd < 0)
  670. myfatal("%I: dial %s failed: %r", srv.remote, addr);
  671. srv.grefd = fd;
  672. srv.grecfd = cfd;
  673. thread(greread, 0);
  674. }
  675. void
  676. greread(void *)
  677. {
  678. uchar buf[Pktsize], *p;
  679. int n, i;
  680. int flag, prot, len, callid;
  681. uchar src[IPaddrlen], dst[IPaddrlen];
  682. uint rseq, ack;
  683. Call *c;
  684. static double t, last;
  685. for(;;) {
  686. n = read(srv.grefd, buf, sizeof(buf));
  687. if(n < 0)
  688. myfatal("%I: bad read on gre: %r", srv.remote);
  689. if(n == sizeof(buf))
  690. myfatal("%I: gre read: buf too small", srv.remote);
  691. p = buf;
  692. v4tov6(src, p);
  693. v4tov6(dst, p+4);
  694. flag = GSHORT(p+8);
  695. prot = GSHORT(p+10);
  696. p += 12; n -= 12;
  697. if(ipcmp(src, srv.remote) != 0 || ipcmp(dst, srv.local) != 0)
  698. myfatal("%I: gre read bad address src=%I dst=%I", srv.remote, src, dst);
  699. if(prot != GRE_ppp)
  700. myfatal("%I: gre read gave bad protocol", srv.remote);
  701. if(flag & (GRE_chksum|GRE_routing)){
  702. p += 4; n -= 4;
  703. }
  704. if(!(flag&GRE_key))
  705. myfatal("%I: gre packet does not contain a key: f=%ux",
  706. srv.remote, flag);
  707. len = GSHORT(p);
  708. callid = GSHORT(p+2);
  709. p += 4; n -= 4;
  710. c = calllookup(callid);
  711. if(c == 0) {
  712. SDB "%I: unknown callid: %d\n", srv.remote, callid EDB
  713. continue;
  714. }
  715. qlock(&c->lk);
  716. c->stat.recv++;
  717. if(flag&GRE_seq) {
  718. rseq = GLONG(p);
  719. p += 4; n -= 4;
  720. } else
  721. rseq = c->rseq;
  722. if(flag&GRE_ack){
  723. ack = GLONG(p);
  724. p += 4; n -= 4;
  725. } else
  726. ack = c->ack;
  727. /* skip routing if present */
  728. if(flag&GRE_routing) {
  729. while((i=p[3]) != 0) {
  730. n -= i;
  731. p += i;
  732. }
  733. }
  734. if(len > n)
  735. myfatal("%I: bad len in gre packet", srv.remote);
  736. if((int)(ack-c->ack) > 0) {
  737. c->ack = ack;
  738. esignal(&c->eack);
  739. }
  740. if(debug)
  741. t = realtime();
  742. if(len == 0) {
  743. /* ack packet */
  744. c->stat.recvack++;
  745. SDB "%I: %.3f (%.3f): gre %d: recv ack a=%ux n=%d flag=%ux\n", srv.remote, t, t-last,
  746. c->id, ack, n, flag EDB
  747. } else {
  748. SDB "%I: %.3f (%.3f): gre %d: recv s=%ux a=%ux len=%d\n", srv.remote, t, t-last,
  749. c->id, rseq, ack, len EDB
  750. /*
  751. * the following handles the case of a single pair of packets
  752. * received out of order
  753. */
  754. n = rseq-c->rseq;
  755. if(n > 0 && (drop == 0. || frand() > drop)) {
  756. c->stat.missing += n-1;
  757. /* current packet */
  758. write(c->pppfd, p, len);
  759. } else {
  760. /* out of sequence - drop on the floor */
  761. c->stat.dropped++;
  762. SDB "%I: %.3f: gre %d: recv out of order or dup packet: seq=%ux len=%d\n",
  763. srv.remote, realtime(), c->id, rseq, len EDB
  764. }
  765. }
  766. if((int)(rseq-c->rseq) > 0)
  767. c->rseq = rseq;
  768. if(debug)
  769. last=t;
  770. /* open up client window */
  771. if((int)(c->rseq-c->rack) > (c->recvwindow>>1))
  772. greack(c);
  773. qunlock(&c->lk);
  774. callfree(c);
  775. }
  776. }
  777. void
  778. greack(Call *c)
  779. {
  780. uchar buf[20];
  781. c->stat.sendack++;
  782. SDB "%I: %.3f: gre %d: send ack %ux\n", srv.remote, realtime(), c->id, c->rseq EDB
  783. v6tov4(buf+0, srv.local); /* source */
  784. v6tov4(buf+4, srv.remote); /* source */
  785. PSHORT(buf+8, GRE_key|GRE_ack|1);
  786. PSHORT(buf+10, GRE_ppp);
  787. PSHORT(buf+12, 0);
  788. PSHORT(buf+14, c->id);
  789. PLONG(buf+16, c->rseq);
  790. write(srv.grefd, buf, sizeof(buf));
  791. c->rack = c->rseq;
  792. }
  793. void
  794. gretimeout(void *a)
  795. {
  796. Call *c;
  797. c = a;
  798. while(!c->closed) {
  799. sleep(Tick);
  800. qlock(&c->lk);
  801. c->tick++;
  802. qunlock(&c->lk);
  803. esignal(&c->eack);
  804. }
  805. callfree(c);
  806. exits(0);
  807. }
  808. void
  809. pppread(void *a)
  810. {
  811. Call *c;
  812. uchar buf[2000], *p;
  813. int n;
  814. ulong tick;
  815. c = a;
  816. for(;;) {
  817. p = buf+24;
  818. n = read(c->pppfd, p, sizeof(buf)-24);
  819. if(n <= 0)
  820. break;
  821. qlock(&c->lk);
  822. /* add gre header */
  823. c->seq++;
  824. tick = c->tick;
  825. while(c->seq-c->ack>c->sendwindow && c->tick-tick<Sendtimeout && !c->closed) {
  826. c->stat.sendwait++;
  827. SDB "window full seq = %d ack = %ux window = %ux\n", c->seq, c->ack, c->sendwindow EDB
  828. qunlock(&c->lk);
  829. ewait(&c->eack);
  830. qlock(&c->lk);
  831. }
  832. if(c->tick-tick >= Sendtimeout) {
  833. c->stat.sendtimeout++;
  834. SDB "send timeout = %d ack = %ux window = %ux\n", c->seq, c->ack, c->sendwindow EDB
  835. }
  836. v6tov4(buf+0, srv.local); /* source */
  837. v6tov4(buf+4, srv.remote); /* source */
  838. PSHORT(buf+8, GRE_key|GRE_seq|GRE_ack|1);
  839. PSHORT(buf+10, GRE_ppp);
  840. PSHORT(buf+12, n);
  841. PSHORT(buf+14, c->id);
  842. PLONG(buf+16, c->seq);
  843. PLONG(buf+20, c->rseq);
  844. c->stat.send++;
  845. c->rack = c->rseq;
  846. SDB "%I: %.3f: gre %d: send s=%ux a=%ux len=%d\n", srv.remote, realtime(),
  847. c->id, c->seq, c->rseq, n EDB
  848. if(drop == 0. || frand() > drop)
  849. if(write(srv.grefd, buf, n+24)<n+24)
  850. myfatal("pppread: write failed: %r");
  851. qunlock(&c->lk);
  852. }
  853. SDB "pppread exit: %d\n", c->id);
  854. callfree(c);
  855. exits(0);
  856. }
  857. void
  858. timeoutthread(void*)
  859. {
  860. for(;;) {
  861. sleep(30*1000);
  862. qlock(&srv.lk);
  863. if(realtime() - srv.rcvtime > 5*60)
  864. myfatal("server timedout");
  865. qunlock(&srv.lk);
  866. }
  867. }
  868. /* use syslog() rather than fprint(2, ...) */
  869. void
  870. myfatal(char *fmt, ...)
  871. {
  872. char sbuf[512];
  873. va_list arg;
  874. uchar buf[16];
  875. /* NT don't seem to like us just going away */
  876. memset(buf, 0, sizeof(buf));
  877. PSHORT(buf+0, sizeof(buf)); /* length */
  878. PSHORT(buf+2, 1); /* message type */
  879. PLONG(buf+4, Magic); /* magic */
  880. PSHORT(buf+8, Tstop); /* op */
  881. buf[12] = 3; /* local shutdown */
  882. write(1, buf, sizeof(buf));
  883. va_start(arg, fmt);
  884. vseprint(sbuf, sbuf+sizeof(sbuf), fmt, arg);
  885. va_end(arg);
  886. SDB "%I: fatal: %s\n", srv.remote, sbuf EDB
  887. syslog(0, LOG, ": src=%I: fatal: %s", srv.remote, sbuf);
  888. close(0);
  889. close(1);
  890. close(srv.grefd);
  891. close(srv.grecfd);
  892. postnote(PNGROUP, getpid(), "die");
  893. exits(sbuf);
  894. }
  895. int
  896. argatoi(char *p)
  897. {
  898. char *q;
  899. int i;
  900. if(p == 0)
  901. usage();
  902. i = strtol(p, &q, 0);
  903. if(q == p)
  904. usage();
  905. return i;
  906. }
  907. void
  908. dhcpclientwatch(void *a)
  909. {
  910. Call *c = a;
  911. uchar buf[1];
  912. for(;;) {
  913. if(read(c->dhcpfd[0], buf, sizeof(buf)) <= 0)
  914. break;
  915. }
  916. if(!c->closed)
  917. myfatal("dhcpclient terminated");
  918. callfree(c);
  919. exits(0);
  920. }
  921. int
  922. ipaddralloc(Call *c)
  923. {
  924. int pfd[2][2];
  925. char *argv[4], *p;
  926. Biobuf bio;
  927. argv[0] = "/bin/ip/dhcpclient";
  928. argv[1] = "-x";
  929. argv[2] = srv.pppdir;
  930. argv[3] = 0;
  931. if(pipe(pfd[0])<0)
  932. myfatal("ipaddralloc: pipe failed: %r");
  933. if(pipe(pfd[1])<0)
  934. myfatal("ipaddralloc: pipe failed: %r");
  935. if(proc(argv, pfd[0][0], pfd[1][1], 2) < 0)
  936. myfatal("ipaddralloc: proc failed: %r");
  937. close(pfd[0][0]);
  938. close(pfd[1][1]);
  939. c->dhcpfd[0] = pfd[1][0];
  940. c->dhcpfd[1] = pfd[0][1];
  941. Binit(&bio, pfd[1][0], OREAD);
  942. for(;;) {
  943. p = Brdline(&bio, '\n');
  944. if(p == 0)
  945. break;
  946. if(strncmp(p, "ip=", 3) == 0) {
  947. p += 3;
  948. parseip(c->remoteip, p);
  949. } else if(strncmp(p, "end\n", 4) == 0)
  950. break;
  951. }
  952. Bterm(&bio);
  953. c->ref++;
  954. thread(dhcpclientwatch, c);
  955. return ipcmp(c->remoteip, IPnoaddr) != 0;
  956. }
  957. void
  958. esignal(Event *e)
  959. {
  960. qlock(e);
  961. if(e->wait == 0) {
  962. e->ready = 1;
  963. qunlock(e);
  964. return;
  965. }
  966. assert(e->ready == 0);
  967. e->wait = 0;
  968. rendezvous(e, (void*)1);
  969. qunlock(e);
  970. }
  971. void
  972. ewait(Event *e)
  973. {
  974. qlock(&e->waitlk);
  975. qlock(e);
  976. assert(e->wait == 0);
  977. if(e->ready) {
  978. e->ready = 0;
  979. } else {
  980. e->wait = 1;
  981. qunlock(e);
  982. rendezvous(e, (void*)2);
  983. qlock(e);
  984. }
  985. qunlock(e);
  986. qunlock(&e->waitlk);
  987. }
  988. ulong
  989. thread(void(*f)(void*), void *a)
  990. {
  991. int pid;
  992. pid=rfork(RFNOWAIT|RFMEM|RFPROC);
  993. if(pid < 0)
  994. myfatal("rfork failed: %r");
  995. if(pid != 0)
  996. return pid;
  997. (*f)(a);
  998. return 0; // never reaches here
  999. }
  1000. double
  1001. realtime(void)
  1002. {
  1003. long times(long*);
  1004. return times(0) / 1000.0;
  1005. }
  1006. void *
  1007. emallocz(int size)
  1008. {
  1009. void *p;
  1010. p = malloc(size);
  1011. if(p == 0)
  1012. myfatal("malloc failed: %r");
  1013. memset(p, 0, size);
  1014. return p;
  1015. }
  1016. static void
  1017. fdclose(void)
  1018. {
  1019. int fd, n, i;
  1020. Dir *d, *p;
  1021. if((fd = open("#d", OREAD)) < 0)
  1022. return;
  1023. n = dirreadall(fd, &d);
  1024. for(p = d; n > 0; n--, p++) {
  1025. i = atoi(p->name);
  1026. if(i > 2)
  1027. close(i);
  1028. }
  1029. free(d);
  1030. }
  1031. int
  1032. proc(char **argv, int fd0, int fd1, int fd2)
  1033. {
  1034. int r, flag;
  1035. char *arg0, file[200];
  1036. arg0 = argv[0];
  1037. strcpy(file, arg0);
  1038. if(access(file, 1) < 0) {
  1039. if(strncmp(arg0, "/", 1)==0
  1040. || strncmp(arg0, "#", 1)==0
  1041. || strncmp(arg0, "./", 2)==0
  1042. || strncmp(arg0, "../", 3)==0)
  1043. return 0;
  1044. sprint(file, "/bin/%s", arg0);
  1045. if(access(file, 1) < 0)
  1046. return 0;
  1047. }
  1048. flag = RFPROC|RFFDG|RFENVG|RFNOWAIT;
  1049. if((r = rfork(flag)) != 0) {
  1050. if(r < 0)
  1051. return 0;
  1052. return r;
  1053. }
  1054. if(fd0 != 0) {
  1055. if(fd1 == 0)
  1056. fd1 = dup(0, -1);
  1057. if(fd2 == 0)
  1058. fd2 = dup(0, -1);
  1059. close(0);
  1060. if(fd0 >= 0)
  1061. dup(fd0, 0);
  1062. }
  1063. if(fd1 != 1) {
  1064. if(fd2 == 1)
  1065. fd2 = dup(1, -1);
  1066. close(1);
  1067. if(fd1 >= 0)
  1068. dup(fd1, 1);
  1069. }
  1070. if(fd2 != 2) {
  1071. close(2);
  1072. if(fd2 >= 0)
  1073. dup(fd2, 2);
  1074. }
  1075. fdclose();
  1076. exec(file, argv);
  1077. myfatal("proc: exec failed: %r");
  1078. return 0;
  1079. }