pptpgre.c 15 KB

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