ppp_ahdlc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*
  2. * ppp_ahdlc.c - STREAMS module for doing PPP asynchronous HDLC.
  3. *
  4. * Re-written by Adi Masputra <adi.masputra@sun.com>, based on
  5. * the original ppp_ahdlc.c
  6. *
  7. * Copyright (c) 2000 by Sun Microsystems, Inc.
  8. * All rights reserved.
  9. *
  10. * Permission to use, copy, modify, and distribute this software and its
  11. * documentation is hereby granted, provided that the above copyright
  12. * notice appears in all copies.
  13. *
  14. * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
  15. * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18. * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
  20. *
  21. * Copyright (c) 1994 Paul Mackerras. All rights reserved.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. *
  27. * 1. Redistributions of source code must retain the above copyright
  28. * notice, this list of conditions and the following disclaimer.
  29. *
  30. * 2. Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in
  32. * the documentation and/or other materials provided with the
  33. * distribution.
  34. *
  35. * 3. The name(s) of the authors of this software must not be used to
  36. * endorse or promote products derived from this software without
  37. * prior written permission.
  38. *
  39. * 4. Redistributions of any form whatsoever must retain the following
  40. * acknowledgment:
  41. * "This product includes software developed by Paul Mackerras
  42. * <paulus@samba.org>".
  43. *
  44. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  45. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  46. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  47. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  48. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  49. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  50. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  51. *
  52. * $Id: ppp_ahdlc.c,v 1.5 2005/06/27 00:59:57 carlsonj Exp $
  53. */
  54. /*
  55. * This file is used under Solaris 2, SVR4, SunOS 4, and Digital UNIX.
  56. */
  57. #include <sys/types.h>
  58. #include <sys/param.h>
  59. #include <sys/stream.h>
  60. #include <sys/stropts.h>
  61. #include <sys/errno.h>
  62. #ifdef SVR4
  63. #include <sys/conf.h>
  64. #include <sys/kmem.h>
  65. #include <sys/cmn_err.h>
  66. #include <sys/ddi.h>
  67. #else
  68. #include <sys/user.h>
  69. #ifdef __osf__
  70. #include <sys/cmn_err.h>
  71. #endif
  72. #endif /* SVR4 */
  73. #include <net/ppp_defs.h>
  74. #include <net/pppio.h>
  75. #include "ppp_mod.h"
  76. /*
  77. * Right now, mutex is only enabled for Solaris 2.x
  78. */
  79. #if defined(SOL2)
  80. #define USE_MUTEX
  81. #endif /* SOL2 */
  82. #ifdef USE_MUTEX
  83. #define MUTEX_ENTER(x) mutex_enter(x)
  84. #define MUTEX_EXIT(x) mutex_exit(x)
  85. #else
  86. #define MUTEX_ENTER(x)
  87. #define MUTEX_EXIT(x)
  88. #endif
  89. /*
  90. * intpointer_t and uintpointer_t are signed and unsigned integer types
  91. * large enough to hold any data pointer; that is, data pointers can be
  92. * assigned into or from these integer types without losing precision.
  93. * On recent Solaris releases, these types are defined in sys/int_types.h,
  94. * but not on SunOS 4.x or the earlier Solaris versions.
  95. */
  96. #if defined(_LP64) || defined(_I32LPx)
  97. typedef long intpointer_t;
  98. typedef unsigned long uintpointer_t;
  99. #else
  100. typedef int intpointer_t;
  101. typedef unsigned int uintpointer_t;
  102. #endif
  103. MOD_OPEN_DECL(ahdlc_open);
  104. MOD_CLOSE_DECL(ahdlc_close);
  105. static int ahdlc_wput __P((queue_t *, mblk_t *));
  106. static int ahdlc_rput __P((queue_t *, mblk_t *));
  107. static void ahdlc_encode __P((queue_t *, mblk_t *));
  108. static void ahdlc_decode __P((queue_t *, mblk_t *));
  109. static int msg_byte __P((mblk_t *, unsigned int));
  110. #if defined(SOL2)
  111. /*
  112. * Don't send HDLC start flag is last transmit is within 1.5 seconds -
  113. * FLAG_TIME is defined is microseconds
  114. */
  115. #define FLAG_TIME 1500
  116. #define ABS(x) (x >= 0 ? x : (-x))
  117. #endif /* SOL2 */
  118. /*
  119. * Extract byte i of message mp
  120. */
  121. #define MSG_BYTE(mp, i) ((i) < (mp)->b_wptr - (mp)->b_rptr? (mp)->b_rptr[i]: \
  122. msg_byte((mp), (i)))
  123. /*
  124. * Is this LCP packet one we have to transmit using LCP defaults?
  125. */
  126. #define LCP_USE_DFLT(mp) (1 <= (code = MSG_BYTE((mp), 4)) && code <= 7)
  127. /*
  128. * Standard STREAMS declarations
  129. */
  130. static struct module_info minfo = {
  131. 0x7d23, "ppp_ahdl", 0, INFPSZ, 32768, 512
  132. };
  133. static struct qinit rinit = {
  134. ahdlc_rput, NULL, ahdlc_open, ahdlc_close, NULL, &minfo, NULL
  135. };
  136. static struct qinit winit = {
  137. ahdlc_wput, NULL, NULL, NULL, NULL, &minfo, NULL
  138. };
  139. #if defined(SVR4) && !defined(SOL2)
  140. int phdldevflag = 0;
  141. #define ppp_ahdlcinfo phdlinfo
  142. #endif /* defined(SVR4) && !defined(SOL2) */
  143. struct streamtab ppp_ahdlcinfo = {
  144. &rinit, /* ptr to st_rdinit */
  145. &winit, /* ptr to st_wrinit */
  146. NULL, /* ptr to st_muxrinit */
  147. NULL, /* ptr to st_muxwinit */
  148. #if defined(SUNOS4)
  149. NULL /* ptr to ptr to st_modlist */
  150. #endif /* SUNOS4 */
  151. };
  152. #if defined(SUNOS4)
  153. int ppp_ahdlc_count = 0; /* open counter */
  154. #endif /* SUNOS4 */
  155. /*
  156. * Per-stream state structure
  157. */
  158. typedef struct ahdlc_state {
  159. #if defined(USE_MUTEX)
  160. kmutex_t lock; /* lock for this structure */
  161. #endif /* USE_MUTEX */
  162. int flags; /* link flags */
  163. mblk_t *rx_buf; /* ptr to receive buffer */
  164. int rx_buf_size; /* receive buffer size */
  165. ushort_t infcs; /* calculated rx HDLC FCS */
  166. u_int32_t xaccm[8]; /* 256-bit xmit ACCM */
  167. u_int32_t raccm; /* 32-bit rcv ACCM */
  168. int mtu; /* interface MTU */
  169. int mru; /* link MRU */
  170. int unit; /* current PPP unit number */
  171. struct pppstat stats; /* statistic structure */
  172. #if defined(SOL2)
  173. clock_t flag_time; /* time in usec between flags */
  174. clock_t lbolt; /* last updated lbolt */
  175. #endif /* SOL2 */
  176. } ahdlc_state_t;
  177. /*
  178. * Values for flags
  179. */
  180. #define ESCAPED 0x100 /* last saw escape char on input */
  181. #define IFLUSH 0x200 /* flushing input due to error */
  182. /*
  183. * RCV_B7_1, etc., defined in net/pppio.h, are stored in flags also.
  184. */
  185. #define RCV_FLAGS (RCV_B7_1|RCV_B7_0|RCV_ODDP|RCV_EVNP)
  186. /*
  187. * FCS lookup table as calculated by genfcstab.
  188. */
  189. static u_short fcstab[256] = {
  190. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  191. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  192. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  193. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  194. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  195. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  196. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  197. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  198. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  199. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  200. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  201. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  202. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  203. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  204. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  205. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  206. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  207. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  208. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  209. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  210. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  211. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  212. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  213. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  214. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  215. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  216. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  217. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  218. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  219. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  220. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  221. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  222. };
  223. static u_int32_t paritytab[8] =
  224. {
  225. 0x96696996, 0x69969669, 0x69969669, 0x96696996,
  226. 0x69969669, 0x96696996, 0x96696996, 0x69969669
  227. };
  228. /*
  229. * STREAMS module open (entry) point
  230. */
  231. MOD_OPEN(ahdlc_open)
  232. {
  233. ahdlc_state_t *state;
  234. mblk_t *mp;
  235. /*
  236. * Return if it's already opened
  237. */
  238. if (q->q_ptr) {
  239. return 0;
  240. }
  241. /*
  242. * This can only be opened as a module
  243. */
  244. if (sflag != MODOPEN) {
  245. OPEN_ERROR(EINVAL);
  246. }
  247. state = (ahdlc_state_t *) ALLOC_NOSLEEP(sizeof(ahdlc_state_t));
  248. if (state == 0)
  249. OPEN_ERROR(ENOSR);
  250. bzero((caddr_t) state, sizeof(ahdlc_state_t));
  251. q->q_ptr = (caddr_t) state;
  252. WR(q)->q_ptr = (caddr_t) state;
  253. #if defined(USE_MUTEX)
  254. mutex_init(&state->lock, NULL, MUTEX_DEFAULT, NULL);
  255. #endif /* USE_MUTEX */
  256. state->xaccm[0] = ~0; /* escape 0x00 through 0x1f */
  257. state->xaccm[3] = 0x60000000; /* escape 0x7d and 0x7e */
  258. state->mru = PPP_MRU; /* default of 1500 bytes */
  259. #if defined(SOL2)
  260. state->flag_time = drv_usectohz(FLAG_TIME);
  261. #endif /* SOL2 */
  262. #if defined(SUNOS4)
  263. ppp_ahdlc_count++;
  264. #endif /* SUNOS4 */
  265. qprocson(q);
  266. if ((mp = allocb(1, BPRI_HI)) != NULL) {
  267. mp->b_datap->db_type = M_FLUSH;
  268. *mp->b_wptr++ = FLUSHR;
  269. putnext(q, mp);
  270. }
  271. return 0;
  272. }
  273. /*
  274. * STREAMS module close (exit) point
  275. */
  276. MOD_CLOSE(ahdlc_close)
  277. {
  278. ahdlc_state_t *state;
  279. qprocsoff(q);
  280. state = (ahdlc_state_t *) q->q_ptr;
  281. if (state == 0) {
  282. DPRINT("state == 0 in ahdlc_close\n");
  283. return 0;
  284. }
  285. if (state->rx_buf != 0) {
  286. freemsg(state->rx_buf);
  287. state->rx_buf = 0;
  288. }
  289. #if defined(USE_MUTEX)
  290. mutex_destroy(&state->lock);
  291. #endif /* USE_MUTEX */
  292. FREE(q->q_ptr, sizeof(ahdlc_state_t));
  293. q->q_ptr = NULL;
  294. OTHERQ(q)->q_ptr = NULL;
  295. #if defined(SUNOS4)
  296. if (ppp_ahdlc_count)
  297. ppp_ahdlc_count--;
  298. #endif /* SUNOS4 */
  299. return 0;
  300. }
  301. /*
  302. * Write side put routine
  303. */
  304. static int
  305. ahdlc_wput(q, mp)
  306. queue_t *q;
  307. mblk_t *mp;
  308. {
  309. ahdlc_state_t *state;
  310. struct iocblk *iop;
  311. int error;
  312. mblk_t *np;
  313. struct ppp_stats *psp;
  314. state = (ahdlc_state_t *) q->q_ptr;
  315. if (state == 0) {
  316. DPRINT("state == 0 in ahdlc_wput\n");
  317. freemsg(mp);
  318. return 0;
  319. }
  320. switch (mp->b_datap->db_type) {
  321. case M_DATA:
  322. /*
  323. * A data packet - do character-stuffing and FCS, and
  324. * send it onwards.
  325. */
  326. ahdlc_encode(q, mp);
  327. freemsg(mp);
  328. break;
  329. case M_IOCTL:
  330. iop = (struct iocblk *) mp->b_rptr;
  331. error = EINVAL;
  332. switch (iop->ioc_cmd) {
  333. case PPPIO_XACCM:
  334. if ((iop->ioc_count < sizeof(u_int32_t)) ||
  335. (iop->ioc_count > sizeof(ext_accm))) {
  336. break;
  337. }
  338. if (mp->b_cont == 0) {
  339. DPRINT1("ahdlc_wput/%d: PPPIO_XACCM b_cont = 0!\n", state->unit);
  340. break;
  341. }
  342. MUTEX_ENTER(&state->lock);
  343. bcopy((caddr_t)mp->b_cont->b_rptr, (caddr_t)state->xaccm,
  344. iop->ioc_count);
  345. state->xaccm[2] &= ~0x40000000; /* don't escape 0x5e */
  346. state->xaccm[3] |= 0x60000000; /* do escape 0x7d, 0x7e */
  347. MUTEX_EXIT(&state->lock);
  348. iop->ioc_count = 0;
  349. error = 0;
  350. break;
  351. case PPPIO_RACCM:
  352. if (iop->ioc_count != sizeof(u_int32_t))
  353. break;
  354. if (mp->b_cont == 0) {
  355. DPRINT1("ahdlc_wput/%d: PPPIO_RACCM b_cont = 0!\n", state->unit);
  356. break;
  357. }
  358. MUTEX_ENTER(&state->lock);
  359. bcopy((caddr_t)mp->b_cont->b_rptr, (caddr_t)&state->raccm,
  360. sizeof(u_int32_t));
  361. MUTEX_EXIT(&state->lock);
  362. iop->ioc_count = 0;
  363. error = 0;
  364. break;
  365. case PPPIO_GCLEAN:
  366. np = allocb(sizeof(int), BPRI_HI);
  367. if (np == 0) {
  368. error = ENOSR;
  369. break;
  370. }
  371. if (mp->b_cont != 0)
  372. freemsg(mp->b_cont);
  373. mp->b_cont = np;
  374. MUTEX_ENTER(&state->lock);
  375. *(int *)np->b_wptr = state->flags & RCV_FLAGS;
  376. MUTEX_EXIT(&state->lock);
  377. np->b_wptr += sizeof(int);
  378. iop->ioc_count = sizeof(int);
  379. error = 0;
  380. break;
  381. case PPPIO_GETSTAT:
  382. np = allocb(sizeof(struct ppp_stats), BPRI_HI);
  383. if (np == 0) {
  384. error = ENOSR;
  385. break;
  386. }
  387. if (mp->b_cont != 0)
  388. freemsg(mp->b_cont);
  389. mp->b_cont = np;
  390. psp = (struct ppp_stats *) np->b_wptr;
  391. np->b_wptr += sizeof(struct ppp_stats);
  392. bzero((caddr_t)psp, sizeof(struct ppp_stats));
  393. psp->p = state->stats;
  394. iop->ioc_count = sizeof(struct ppp_stats);
  395. error = 0;
  396. break;
  397. case PPPIO_LASTMOD:
  398. /* we knew this anyway */
  399. error = 0;
  400. break;
  401. default:
  402. error = -1;
  403. break;
  404. }
  405. if (error < 0)
  406. putnext(q, mp);
  407. else if (error == 0) {
  408. mp->b_datap->db_type = M_IOCACK;
  409. qreply(q, mp);
  410. } else {
  411. mp->b_datap->db_type = M_IOCNAK;
  412. iop->ioc_count = 0;
  413. iop->ioc_error = error;
  414. qreply(q, mp);
  415. }
  416. break;
  417. case M_CTL:
  418. switch (*mp->b_rptr) {
  419. case PPPCTL_MTU:
  420. MUTEX_ENTER(&state->lock);
  421. state->mtu = ((unsigned short *)mp->b_rptr)[1];
  422. MUTEX_EXIT(&state->lock);
  423. break;
  424. case PPPCTL_MRU:
  425. MUTEX_ENTER(&state->lock);
  426. state->mru = ((unsigned short *)mp->b_rptr)[1];
  427. MUTEX_EXIT(&state->lock);
  428. break;
  429. case PPPCTL_UNIT:
  430. MUTEX_ENTER(&state->lock);
  431. state->unit = mp->b_rptr[1];
  432. MUTEX_EXIT(&state->lock);
  433. break;
  434. default:
  435. putnext(q, mp);
  436. return 0;
  437. }
  438. freemsg(mp);
  439. break;
  440. default:
  441. putnext(q, mp);
  442. }
  443. return 0;
  444. }
  445. /*
  446. * Read side put routine
  447. */
  448. static int
  449. ahdlc_rput(q, mp)
  450. queue_t *q;
  451. mblk_t *mp;
  452. {
  453. ahdlc_state_t *state;
  454. state = (ahdlc_state_t *) q->q_ptr;
  455. if (state == 0) {
  456. DPRINT("state == 0 in ahdlc_rput\n");
  457. freemsg(mp);
  458. return 0;
  459. }
  460. switch (mp->b_datap->db_type) {
  461. case M_DATA:
  462. ahdlc_decode(q, mp);
  463. break;
  464. case M_HANGUP:
  465. MUTEX_ENTER(&state->lock);
  466. if (state->rx_buf != 0) {
  467. /* XXX would like to send this up for debugging */
  468. freemsg(state->rx_buf);
  469. state->rx_buf = 0;
  470. }
  471. state->flags = IFLUSH;
  472. MUTEX_EXIT(&state->lock);
  473. putnext(q, mp);
  474. break;
  475. default:
  476. putnext(q, mp);
  477. }
  478. return 0;
  479. }
  480. /*
  481. * Extract bit c from map m, to determine if c needs to be escaped
  482. */
  483. #define IN_TX_MAP(c, m) ((m)[(c) >> 5] & (1 << ((c) & 0x1f)))
  484. static void
  485. ahdlc_encode(q, mp)
  486. queue_t *q;
  487. mblk_t *mp;
  488. {
  489. ahdlc_state_t *state;
  490. u_int32_t *xaccm, loc_xaccm[8];
  491. ushort_t fcs;
  492. size_t outmp_len;
  493. mblk_t *outmp, *tmp;
  494. uchar_t *dp, fcs_val;
  495. int is_lcp, code;
  496. #if defined(SOL2)
  497. clock_t lbolt;
  498. #endif /* SOL2 */
  499. if (msgdsize(mp) < 4) {
  500. return;
  501. }
  502. state = (ahdlc_state_t *)q->q_ptr;
  503. MUTEX_ENTER(&state->lock);
  504. /*
  505. * Allocate an output buffer large enough to handle a case where all
  506. * characters need to be escaped
  507. */
  508. outmp_len = (msgdsize(mp) << 1) + /* input block x 2 */
  509. (sizeof(fcs) << 2) + /* HDLC FCS x 4 */
  510. (sizeof(uchar_t) << 1); /* HDLC flags x 2 */
  511. outmp = allocb(outmp_len, BPRI_MED);
  512. if (outmp == NULL) {
  513. state->stats.ppp_oerrors++;
  514. MUTEX_EXIT(&state->lock);
  515. putctl1(RD(q)->q_next, M_CTL, PPPCTL_OERROR);
  516. return;
  517. }
  518. #if defined(SOL2)
  519. /*
  520. * Check if our last transmit happenned within flag_time, using
  521. * the system's LBOLT value in clock ticks
  522. */
  523. if (drv_getparm(LBOLT, &lbolt) != -1) {
  524. if (ABS((clock_t)lbolt - state->lbolt) > state->flag_time) {
  525. *outmp->b_wptr++ = PPP_FLAG;
  526. }
  527. state->lbolt = lbolt;
  528. } else {
  529. *outmp->b_wptr++ = PPP_FLAG;
  530. }
  531. #else
  532. /*
  533. * If the driver below still has a message to process, skip the
  534. * HDLC flag, otherwise, put one in the beginning
  535. */
  536. if (qsize(q->q_next) == 0) {
  537. *outmp->b_wptr++ = PPP_FLAG;
  538. }
  539. #endif
  540. /*
  541. * All control characters must be escaped for LCP packets with code
  542. * values between 1 (Conf-Req) and 7 (Code-Rej).
  543. */
  544. is_lcp = ((MSG_BYTE(mp, 0) == PPP_ALLSTATIONS) &&
  545. (MSG_BYTE(mp, 1) == PPP_UI) &&
  546. (MSG_BYTE(mp, 2) == (PPP_LCP >> 8)) &&
  547. (MSG_BYTE(mp, 3) == (PPP_LCP & 0xff)) &&
  548. LCP_USE_DFLT(mp));
  549. xaccm = state->xaccm;
  550. if (is_lcp) {
  551. bcopy((caddr_t)state->xaccm, (caddr_t)loc_xaccm, sizeof(loc_xaccm));
  552. loc_xaccm[0] = ~0; /* force escape on 0x00 through 0x1f */
  553. xaccm = loc_xaccm;
  554. }
  555. fcs = PPP_INITFCS; /* Initial FCS is 0xffff */
  556. /*
  557. * Process this block and the rest (if any) attached to the this one
  558. */
  559. for (tmp = mp; tmp; tmp = tmp->b_cont) {
  560. if (tmp->b_datap->db_type == M_DATA) {
  561. for (dp = tmp->b_rptr; dp < tmp->b_wptr; dp++) {
  562. fcs = PPP_FCS(fcs, *dp);
  563. if (IN_TX_MAP(*dp, xaccm)) {
  564. *outmp->b_wptr++ = PPP_ESCAPE;
  565. *outmp->b_wptr++ = *dp ^ PPP_TRANS;
  566. } else {
  567. *outmp->b_wptr++ = *dp;
  568. }
  569. }
  570. } else {
  571. continue; /* skip if db_type is something other than M_DATA */
  572. }
  573. }
  574. /*
  575. * Append the HDLC FCS, making sure that escaping is done on any
  576. * necessary bytes
  577. */
  578. fcs_val = (fcs ^ 0xffff) & 0xff;
  579. if (IN_TX_MAP(fcs_val, xaccm)) {
  580. *outmp->b_wptr++ = PPP_ESCAPE;
  581. *outmp->b_wptr++ = fcs_val ^ PPP_TRANS;
  582. } else {
  583. *outmp->b_wptr++ = fcs_val;
  584. }
  585. fcs_val = ((fcs ^ 0xffff) >> 8) & 0xff;
  586. if (IN_TX_MAP(fcs_val, xaccm)) {
  587. *outmp->b_wptr++ = PPP_ESCAPE;
  588. *outmp->b_wptr++ = fcs_val ^ PPP_TRANS;
  589. } else {
  590. *outmp->b_wptr++ = fcs_val;
  591. }
  592. /*
  593. * And finally, append the HDLC flag, and send it away
  594. */
  595. *outmp->b_wptr++ = PPP_FLAG;
  596. state->stats.ppp_obytes += msgdsize(outmp);
  597. state->stats.ppp_opackets++;
  598. MUTEX_EXIT(&state->lock);
  599. putnext(q, outmp);
  600. }
  601. /*
  602. * Checks the 32-bit receive ACCM to see if the byte needs un-escaping
  603. */
  604. #define IN_RX_MAP(c, m) ((((unsigned int) (uchar_t) (c)) < 0x20) && \
  605. (m) & (1 << (c)))
  606. /*
  607. * Process received characters.
  608. */
  609. static void
  610. ahdlc_decode(q, mp)
  611. queue_t *q;
  612. mblk_t *mp;
  613. {
  614. ahdlc_state_t *state;
  615. mblk_t *om;
  616. uchar_t *dp;
  617. state = (ahdlc_state_t *) q->q_ptr;
  618. MUTEX_ENTER(&state->lock);
  619. state->stats.ppp_ibytes += msgdsize(mp);
  620. for (; mp != 0; om = mp->b_cont, freeb(mp), mp = om)
  621. for (dp = mp->b_rptr; dp < mp->b_wptr; dp++) {
  622. /*
  623. * This should detect the lack of 8-bit communication channel
  624. * which is necessary for PPP to work. In addition, it also
  625. * checks on the parity.
  626. */
  627. if (*dp & 0x80)
  628. state->flags |= RCV_B7_1;
  629. else
  630. state->flags |= RCV_B7_0;
  631. if (paritytab[*dp >> 5] & (1 << (*dp & 0x1f)))
  632. state->flags |= RCV_ODDP;
  633. else
  634. state->flags |= RCV_EVNP;
  635. /*
  636. * So we have a HDLC flag ...
  637. */
  638. if (*dp == PPP_FLAG) {
  639. /*
  640. * If we think that it marks the beginning of the frame,
  641. * then continue to process the next octects
  642. */
  643. if ((state->flags & IFLUSH) ||
  644. (state->rx_buf == 0) ||
  645. (msgdsize(state->rx_buf) == 0)) {
  646. state->flags &= ~IFLUSH;
  647. continue;
  648. }
  649. /*
  650. * We get here because the above condition isn't true,
  651. * in which case the HDLC flag was there to mark the end
  652. * of the frame (or so we think)
  653. */
  654. om = state->rx_buf;
  655. if (state->infcs == PPP_GOODFCS) {
  656. state->stats.ppp_ipackets++;
  657. adjmsg(om, -PPP_FCSLEN);
  658. putnext(q, om);
  659. } else {
  660. DPRINT2("ppp%d: bad fcs (len=%d)\n",
  661. state->unit, msgdsize(state->rx_buf));
  662. freemsg(state->rx_buf);
  663. state->flags &= ~(IFLUSH | ESCAPED);
  664. state->stats.ppp_ierrors++;
  665. putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
  666. }
  667. state->rx_buf = 0;
  668. continue;
  669. }
  670. if (state->flags & IFLUSH) {
  671. continue;
  672. }
  673. /*
  674. * Allocate a receive buffer, large enough to store a frame (after
  675. * un-escaping) of at least 1500 octets. If MRU is negotiated to
  676. * be more than the default, then allocate that much. In addition,
  677. * we add an extra 32-bytes for a fudge factor
  678. */
  679. if (state->rx_buf == 0) {
  680. state->rx_buf_size = (state->mru < PPP_MRU ? PPP_MRU : state->mru);
  681. state->rx_buf_size += (sizeof(u_int32_t) << 3);
  682. state->rx_buf = allocb(state->rx_buf_size, BPRI_MED);
  683. /*
  684. * If allocation fails, try again on the next frame
  685. */
  686. if (state->rx_buf == 0) {
  687. state->flags |= IFLUSH;
  688. continue;
  689. }
  690. state->flags &= ~(IFLUSH | ESCAPED);
  691. state->infcs = PPP_INITFCS;
  692. }
  693. if (*dp == PPP_ESCAPE) {
  694. state->flags |= ESCAPED;
  695. continue;
  696. }
  697. /*
  698. * Make sure we un-escape the necessary characters, as well as the
  699. * ones in our receive async control character map
  700. */
  701. if (state->flags & ESCAPED) {
  702. *dp ^= PPP_TRANS;
  703. state->flags &= ~ESCAPED;
  704. } else if (IN_RX_MAP(*dp, state->raccm))
  705. continue;
  706. /*
  707. * Unless the peer lied to us about the negotiated MRU, we should
  708. * never get a frame which is too long. If it happens, toss it away
  709. * and grab the next incoming one
  710. */
  711. if (msgdsize(state->rx_buf) < state->rx_buf_size) {
  712. state->infcs = PPP_FCS(state->infcs, *dp);
  713. *state->rx_buf->b_wptr++ = *dp;
  714. } else {
  715. DPRINT2("ppp%d: frame too long (%d)\n",
  716. state->unit, msgdsize(state->rx_buf));
  717. freemsg(state->rx_buf);
  718. state->rx_buf = 0;
  719. state->flags |= IFLUSH;
  720. }
  721. }
  722. MUTEX_EXIT(&state->lock);
  723. }
  724. static int
  725. msg_byte(mp, i)
  726. mblk_t *mp;
  727. unsigned int i;
  728. {
  729. while (mp != 0 && i >= mp->b_wptr - mp->b_rptr)
  730. mp = mp->b_cont;
  731. if (mp == 0)
  732. return -1;
  733. return mp->b_rptr[i];
  734. }