pptpd.c 23 KB

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