1
0

pptpgre.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * pptpgre.c
  3. *
  4. * originally by C. S. Ananian
  5. * Modified for PoPToP
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif
  10. #ifdef __linux__
  11. #define _GNU_SOURCE 1 /* broken arpa/inet.h */
  12. #endif
  13. #include "our_syslog.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20. #include <sys/stat.h>
  21. #include <time.h>
  22. #include <sys/time.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #ifdef HAVE_SYS_UIO_H
  28. #include <sys/uio.h>
  29. #endif
  30. #ifdef VRF
  31. #include <vrf.h>
  32. #endif
  33. #include "ppphdlc.h"
  34. #include "pptpgre.h"
  35. #include "pptpdefs.h"
  36. #include "pptpctrl.h"
  37. #include "defaults.h"
  38. #include "pqueue.h"
  39. #ifndef HAVE_STRERROR
  40. #include "compat.h"
  41. #endif
  42. #define PACKET_MAX 8196
  43. typedef int (*callback_t)(int cl, void *pack, unsigned int len);
  44. /* test for a 32 bit counter overflow */
  45. #define WRAPPED( curseq, lastseq) \
  46. ((((curseq) & 0xffffff00) == 0) && \
  47. (((lastseq) & 0xffffff00 ) == 0xffffff00))
  48. static struct gre_state gre;
  49. gre_stats_t stats;
  50. static uint64_t time_now_usecs()
  51. {
  52. struct timeval tv;
  53. gettimeofday(&tv, NULL);
  54. return (tv.tv_sec * 1000000) + tv.tv_usec;
  55. }
  56. int pptp_gre_init(u_int32_t call_id_pair, int pty_fd, struct in_addr *inetaddrs)
  57. {
  58. struct sockaddr_in addr;
  59. int gre_fd;
  60. /* Open IP protocol socket */
  61. gre_fd = vrf_socket(vrf, AF_INET, SOCK_RAW, PPTP_PROTO);
  62. if (gre_fd < 0) {
  63. syslog(LOG_ERR, "GRE: socket() failed");
  64. return -1;
  65. }
  66. memset(&addr, 0, sizeof(addr));
  67. addr.sin_family = AF_INET;
  68. addr.sin_addr = inetaddrs[0];
  69. addr.sin_port = 0;
  70. if (bind(gre_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  71. syslog(LOG_ERR, "GRE: bind() failed: %s", strerror(errno));
  72. syslog(LOG_ERR, "GRE: continuing, but may not work if multi-homed");
  73. }
  74. addr.sin_family = AF_INET;
  75. addr.sin_addr = inetaddrs[1];
  76. addr.sin_port = 0;
  77. if (connect(gre_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  78. syslog(LOG_ERR, "GRE: connect() failed: %s", strerror(errno));
  79. close(gre_fd);
  80. return -1;
  81. }
  82. gre.seq_sent = 0;
  83. gre.ack_sent = gre.ack_recv = gre.seq_recv = 0xFFFFFFFF;
  84. /* seq_recv is -1, therefore next packet expected is seq 0,
  85. to comply with RFC 2637: 'The sequence number for each
  86. user session is set to zero at session startup.' */
  87. gre.call_id_pair = call_id_pair; /* network byte order */
  88. return gre_fd;
  89. }
  90. /* ONE blocking read per call; dispatches all packets possible */
  91. /* returns 0 on success, or <0 on read failure */
  92. int decaps_hdlc(int fd, int (*cb) (int cl, void *pack, unsigned len), int cl)
  93. {
  94. static unsigned char buffer[PACKET_MAX], copy[PACKET_MAX];
  95. static unsigned start = 0, end = 0;
  96. static unsigned len = 0, escape = 0;
  97. static u_int16_t fcs = PPPINITFCS16;
  98. static unsigned char err = 0;
  99. unsigned char c;
  100. int status;
  101. /* we do one read only, since it may block. and only if the
  102. * buffer is empty (start == end)
  103. */
  104. if (fd == -1) {
  105. if(cb == NULL) {
  106. /* peek mode */
  107. return err ? -1 : 0;
  108. } else if (!err) {
  109. /* re-xmit and nothing queued */
  110. syslog(LOG_ERR, "GRE: Re-xmit called with nothing queued");
  111. return -1;
  112. }
  113. }
  114. if (!err) {
  115. /* All known data is processed. This true unless the last
  116. * network write failed.
  117. */
  118. if ((status = read(fd, buffer, sizeof(buffer))) <= 0) {
  119. syslog(LOG_ERR, "GRE: read(fd=%d,buffer=%lx,len=%d) from PTY failed: status = %d error = %s%s",
  120. fd, (unsigned long) buffer,
  121. (int) sizeof(buffer),
  122. status, status ? strerror(errno) : "No error",
  123. errno != EIO ? "" : ", usually caused by unexpected termination of pppd, check option syntax and pppd logs");
  124. /* FAQ: mistakes in pppd option spelling in
  125. * /etc/ppp/options.pptpd often cause EIO,
  126. * with pppd not reporting the problem to any
  127. * logs. Termination of pppd by signal can
  128. * *also* cause this situation. -- James Cameron
  129. */
  130. return -1;
  131. }
  132. end = status;
  133. start = 0;
  134. } else {
  135. /* We're here because of a network write failure. Try again.
  136. * Then do what we would do normally and enter the loop as if
  137. * just continuing the while(1). Not sure that this ever
  138. * really happens, but since we error-check status then we
  139. * should have the code to handle an error :-)
  140. */
  141. err = 0;
  142. if ((status = cb(cl, copy, len)) < 0) {
  143. syslog(LOG_ERR, "GRE: re-xmit failed from decaps_hdlc: %s", strerror(errno));
  144. err = 1;
  145. return status; /* return error */
  146. }
  147. /* Great! Let's do more! */
  148. fcs = PPPINITFCS16;
  149. len = 0;
  150. escape = 0;
  151. }
  152. while (1) {
  153. /* Infinite loop, we return when we're out of data */
  154. /* Check if out of data */
  155. if (start == end)
  156. return 0;
  157. /* Add to the packet up till the next HDLC_FLAG (start/end of
  158. * packet marker). Copy to 'copy', un-escape and checksum as we go.
  159. */
  160. while (buffer[start] != HDLC_FLAG) {
  161. /* Dispose of 'too long' packets */
  162. if (len >= PACKET_MAX) {
  163. syslog(LOG_ERR, "GRE: Received too long packet from pppd.");
  164. while (buffer[start] != HDLC_FLAG && start < end)
  165. start++;
  166. if (start < end) {
  167. goto newpacket;
  168. } else
  169. return 0;
  170. }
  171. /* Read a character, un-escaping if needed */
  172. if (buffer[start] == HDLC_ESCAPE && !escape)
  173. escape = 1;
  174. else {
  175. if (escape) {
  176. copy[len] = c = buffer[start] ^ 0x20;
  177. escape = 0;
  178. } else
  179. copy[len] = c = buffer[start];
  180. fcs = (fcs >> 8) ^ fcstab[(fcs ^ c) & 0xff];
  181. len++;
  182. }
  183. start++;
  184. /* Check if out of data */
  185. if (start == end)
  186. return 0;
  187. }
  188. /* Found flag. Skip past it */
  189. start++;
  190. /* Check for over-short packets and silently discard, as per RFC1662 */
  191. if ((len < 4) || (escape == 1)) {
  192. /* len == 0 is possible, we generate it :-) [using HDLC_ESCAPE at
  193. * start and end of packet]. Others are worth recording.
  194. */
  195. if (len && len < 4)
  196. syslog(LOG_ERR, "GRE: Received too short packet from pppd.");
  197. if (escape)
  198. syslog(LOG_ERR, "GRE: Received bad packet from pppd.");
  199. goto newpacket;
  200. }
  201. /* Check, then remove the 16-bit FCS checksum field */
  202. if (fcs != PPPGOODFCS16) {
  203. syslog(LOG_ERR, "GRE: Bad checksum from pppd.");
  204. goto newpacket;
  205. }
  206. len -= sizeof(u_int16_t);
  207. /* So now we have a packet of length 'len' in 'copy' */
  208. if ((status = cb(cl, copy, len)) < 0) {
  209. syslog(LOG_ERR, "GRE: xmit failed from decaps_hdlc: %s", strerror(errno));
  210. err = 1;
  211. return status; /* return error */
  212. }
  213. newpacket:
  214. /* Great! Let's do more! */
  215. fcs = PPPINITFCS16;
  216. len = 0;
  217. escape = 0;
  218. }
  219. }
  220. #define seq_greater(A,B) ((A)>(B) || \
  221. (((u_int32_t)(A)<0xff) && ((~((u_int32_t)(B)))<0xff)))
  222. /* Macro used in encaps_hdlc(). add "val" to "dest" at position "pos",
  223. * incrementing "pos" to point after the added value. set "tmp" to "val"
  224. * as a side-effect.
  225. */
  226. #define ADD_CHAR(dest, pos, val, tmp) \
  227. tmp = (val); \
  228. if ((tmp<0x20) || (tmp==HDLC_FLAG) || (tmp==HDLC_ESCAPE)) { \
  229. dest[pos++]=HDLC_ESCAPE; \
  230. dest[pos++]=tmp^0x20; \
  231. } else \
  232. dest[pos++]=tmp
  233. /* Make stripped packet into HDLC packet */
  234. int encaps_hdlc(int fd, void *pack, unsigned len)
  235. {
  236. unsigned char *source = (unsigned char *) pack;
  237. /* largest expansion possible - double all + double fcs + 2 flags */
  238. static unsigned char dest[2 * PACKET_MAX + 6];
  239. unsigned pos = 1, i;
  240. u_int16_t fcs;
  241. unsigned char c;
  242. fcs = PPPINITFCS16;
  243. /* make sure overflow is impossible so we don't have to bounds check
  244. * in loop. drop large packets.
  245. */
  246. if (len > PACKET_MAX) {
  247. syslog(LOG_ERR, "GRE: Asked to encapsulate too large packet (len = %d)", len);
  248. return -1;
  249. }
  250. /* start character */
  251. dest[0] = HDLC_FLAG;
  252. /* escape the payload */
  253. for (i = 0; i < len; i++) {
  254. ADD_CHAR(dest, pos, source[i], c);
  255. fcs = (fcs >> 8) ^ fcstab[(fcs ^ c) & 0xff];
  256. }
  257. fcs ^= 0xFFFF;
  258. ADD_CHAR(dest, pos, fcs & 0xFF, c);
  259. ADD_CHAR(dest, pos, fcs >> 8, c);
  260. /* tack on the end-flag */
  261. dest[pos++] = HDLC_FLAG;
  262. /* now write this packet */
  263. return write(fd, dest, pos);
  264. }
  265. #undef ADD_CHAR
  266. static int dequeue_gre (callback_t callback, int cl)
  267. {
  268. pqueue_t *head;
  269. int status;
  270. /* process packets in the queue that either are expected or
  271. have timed out. */
  272. head = pqueue_head();
  273. while ( head != NULL &&
  274. ( (head->seq == gre.seq_recv + 1) || /* wrap-around safe */
  275. (pqueue_expiry_time(head) <= 0)
  276. )
  277. ) {
  278. /* if it is timed out... */
  279. if (head->seq != gre.seq_recv + 1 ) { /* wrap-around safe */
  280. stats.rx_lost += head->seq - gre.seq_recv - 1;
  281. if (pptpctrl_debug)
  282. syslog(LOG_DEBUG,
  283. "GRE: timeout waiting for %d packets",
  284. head->seq - gre.seq_recv - 1);
  285. }
  286. if (pptpctrl_debug)
  287. syslog(LOG_DEBUG, "GRE: accepting #%d from queue",
  288. head->seq);
  289. gre.seq_recv = head->seq;
  290. status = callback(cl, head->packet, head->packlen);
  291. pqueue_del(head);
  292. if (status < 0) return status;
  293. head = pqueue_head();
  294. }
  295. return 0;
  296. }
  297. int decaps_gre(int fd, int (*cb) (int cl, void *pack, unsigned len), int cl)
  298. {
  299. static unsigned char buffer[PACKET_MAX + 64 /*ip header */ ];
  300. struct pptp_gre_header *header;
  301. int status, ip_len = 0;
  302. dequeue_gre(cb, cl);
  303. if ((status = read(fd, buffer, sizeof(buffer))) <= 0) {
  304. syslog(LOG_ERR, "GRE: read(fd=%d,buffer=%lx,len=%d) from network failed: status = %d error = %s",
  305. fd, (unsigned long) buffer, (int) sizeof(buffer),
  306. status, status ? strerror(errno) : "No error");
  307. stats.rx_errors++;
  308. return -1;
  309. }
  310. /* strip off IP header, if present */
  311. if ((buffer[0] & 0xF0) == 0x40)
  312. ip_len = (buffer[0] & 0xF) * 4;
  313. header = (struct pptp_gre_header *) (buffer + ip_len);
  314. /* verify packet (else discard) */
  315. if (((ntoh8(header->ver) & 0x7F) != PPTP_GRE_VER) || /* version should be 1 */
  316. (ntoh16(header->protocol) != PPTP_GRE_PROTO) || /* GRE protocol for PPTP */
  317. PPTP_GRE_IS_C(ntoh8(header->flags)) || /* flag C should be clear */
  318. PPTP_GRE_IS_R(ntoh8(header->flags)) || /* flag R should be clear */
  319. (!PPTP_GRE_IS_K(ntoh8(header->flags))) || /* flag K should be set */
  320. ((ntoh8(header->flags) & 0xF) != 0)) { /* routing and recursion ctrl = 0 */
  321. /* if invalid, discard this packet */
  322. syslog(LOG_ERR, "GRE: Discarding packet by header check");
  323. stats.rx_invalid++;
  324. return 0;
  325. }
  326. if (header->call_id != GET_VALUE(PAC, gre.call_id_pair)) {
  327. /*
  328. * Discard silently to allow more than one GRE tunnel from
  329. * the same IP address in case clients are behind the
  330. * firewall.
  331. *
  332. * syslog(LOG_ERR, "GRE: Discarding for incorrect call");
  333. */
  334. return 0;
  335. }
  336. if (PPTP_GRE_IS_A(ntoh8(header->ver))) { /* acknowledgement present */
  337. u_int32_t ack = (PPTP_GRE_IS_S(ntoh8(header->flags))) ?
  338. ntoh32(header->ack) : ntoh32(header->seq);
  339. /* ack in different place if S=0 */
  340. if (seq_greater(ack, gre.ack_recv))
  341. gre.ack_recv = ack;
  342. /* also handle sequence number wrap-around */
  343. if (WRAPPED(ack,gre.ack_recv)) gre.ack_recv = ack;
  344. if (gre.ack_recv == stats.pt.seq) {
  345. int rtt = time_now_usecs() - stats.pt.time;
  346. stats.rtt = (stats.rtt + rtt) / 2;
  347. }
  348. }
  349. if (PPTP_GRE_IS_S(ntoh8(header->flags))) { /* payload present */
  350. unsigned headersize = sizeof(*header);
  351. unsigned payload_len = ntoh16(header->payload_len);
  352. u_int32_t seq = ntoh32(header->seq);
  353. if (!PPTP_GRE_IS_A(ntoh8(header->ver)))
  354. headersize -= sizeof(header->ack);
  355. /* check for incomplete packet (length smaller than expected) */
  356. if (status - headersize < payload_len) {
  357. stats.rx_truncated++;
  358. return 0;
  359. }
  360. /* check for out-of-order sequence number
  361. * N.B.: some client implementations violate RFC 2637
  362. * and start their sequence numbers at 1 instead of 0,
  363. * so we have to introduce a kludge to deal with it.
  364. * on wrap we may allow an out of order packet to pass
  365. */
  366. if (seq == gre.seq_recv + 1 || seq == 1) {
  367. if (pptpctrl_debug)
  368. syslog(LOG_DEBUG, "GRE: accepting packet #%d",
  369. seq);
  370. stats.rx_accepted++;
  371. gre.seq_recv = seq;
  372. return cb(cl, buffer + ip_len + headersize, payload_len);
  373. } else if (!seq_greater(seq, gre.seq_recv)) {
  374. if (pptpctrl_debug)
  375. syslog(LOG_DEBUG,
  376. "GRE: discarding duplicate or old packet #%d (expecting #%d)",
  377. seq, gre.seq_recv + 1);
  378. return 0; /* discard duplicate packets */
  379. } else {
  380. stats.rx_buffered++;
  381. if (pptpctrl_debug)
  382. syslog(LOG_DEBUG,
  383. "GRE: buffering packet #%d (expecting #%d, lost or reordered)",
  384. seq, gre.seq_recv + 1);
  385. pqueue_add(seq, buffer + ip_len + headersize, payload_len);
  386. return 0; /* discard out-of-order packets */
  387. }
  388. }
  389. return 0; /* ack, but no payload */
  390. }
  391. int encaps_gre(int fd, void *pack, unsigned len)
  392. {
  393. static union {
  394. struct pptp_gre_header header;
  395. unsigned char buffer[PACKET_MAX + sizeof(struct pptp_gre_header)];
  396. } u;
  397. unsigned header_len;
  398. ssize_t status;
  399. #ifdef HAVE_WRITEV
  400. struct iovec iovec[2];
  401. #endif
  402. if(fd == -1)
  403. /* peek mode */
  404. return (gre.ack_sent == gre.seq_recv) ? 0 : -1;
  405. /* package this up in a GRE shell. */
  406. u.header.flags = hton8(PPTP_GRE_FLAG_K);
  407. u.header.ver = hton8(PPTP_GRE_VER);
  408. u.header.protocol = hton16(PPTP_GRE_PROTO);
  409. u.header.payload_len = hton16(len);
  410. u.header.call_id = GET_VALUE(PNS, gre.call_id_pair);
  411. /* special case ACK with no payload */
  412. if (pack == NULL) {
  413. if (gre.ack_sent != gre.seq_recv) {
  414. u.header.ver |= hton8(PPTP_GRE_FLAG_A);
  415. u.header.payload_len = hton16(0);
  416. u.header.seq = hton32(gre.seq_recv); /* ack is in odd place because S=0 */
  417. gre.ack_sent = gre.seq_recv;
  418. /* don't sent ACK field, ACK is in SYN field */
  419. return write(fd, u.buffer, sizeof(u.header) - sizeof(u.header.ack));
  420. } else
  421. return 0; /* we don't need to send ACK */
  422. }
  423. /* send packet with payload */
  424. u.header.flags |= hton8(PPTP_GRE_FLAG_S);
  425. u.header.seq = hton32(gre.seq_sent);
  426. gre.seq_sent++;
  427. if (gre.ack_sent != gre.seq_recv) { /* send ack with this message */
  428. u.header.ver |= hton8(PPTP_GRE_FLAG_A);
  429. u.header.ack = hton32(gre.seq_recv);
  430. gre.ack_sent = gre.seq_recv;
  431. header_len = sizeof(u.header);
  432. } else { /* don't send ack */
  433. header_len = sizeof(u.header) - sizeof(u.header.ack);
  434. }
  435. if (len > PACKET_MAX) {
  436. syslog(LOG_ERR, "GRE: packet is too large %d", len);
  437. stats.tx_oversize++;
  438. return 0; /* drop this, it's too big */
  439. }
  440. #ifdef HAVE_WRITEV
  441. /* write header and buffer without copying. */
  442. iovec[0].iov_base = u.buffer;
  443. iovec[0].iov_len = header_len;
  444. iovec[1].iov_base = pack;
  445. iovec[1].iov_len = len;
  446. status = writev(fd, iovec, 2);
  447. #else
  448. /* copy payload into buffer */
  449. memcpy(u.buffer + header_len, pack, len);
  450. /* record and increment sequence numbers */
  451. /* write this baby out to the net */
  452. status = write(fd, u.buffer, header_len + len);
  453. #endif
  454. /* if ENOBUFS, do not close the connection */
  455. if ((status < 0) && (errno == ENOBUFS)) {
  456. gre.seq_sent--;
  457. status = 0;
  458. }
  459. return status;
  460. }