pptpd.c 23 KB

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