ccp.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. /*
  2. * ccp.c - PPP Compression Control Protocol.
  3. *
  4. * Copyright (c) 1994-2002 Paul Mackerras. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. The name(s) of the authors of this software must not be used to
  14. * endorse or promote products derived from this software without
  15. * prior written permission.
  16. *
  17. * 3. Redistributions of any form whatsoever must retain the following
  18. * acknowledgment:
  19. * "This product includes software developed by Paul Mackerras
  20. * <paulus@samba.org>".
  21. *
  22. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  23. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  24. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  25. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  26. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  27. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  28. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  29. */
  30. #define RCSID "$Id: ccp.c,v 1.50 2005/06/26 19:34:41 carlsonj Exp $"
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "pppd.h"
  34. #include "fsm.h"
  35. #include "ccp.h"
  36. #include <net/ppp-comp.h>
  37. #ifdef MPPE
  38. #include "chap_ms.h" /* mppe_xxxx_key, mppe_keys_set */
  39. #include "lcp.h" /* lcp_close(), lcp_fsm */
  40. #endif
  41. static const char rcsid[] = RCSID;
  42. /*
  43. * Unfortunately there is a bug in zlib which means that using a
  44. * size of 8 (window size = 256) for Deflate compression will cause
  45. * buffer overruns and kernel crashes in the deflate module.
  46. * Until this is fixed we only accept sizes in the range 9 .. 15.
  47. * Thanks to James Carlson for pointing this out.
  48. */
  49. #define DEFLATE_MIN_WORKS 9
  50. /*
  51. * Command-line options.
  52. */
  53. static int setbsdcomp __P((char **));
  54. static int setdeflate __P((char **));
  55. static char bsd_value[8];
  56. static char deflate_value[8];
  57. /*
  58. * Option variables.
  59. */
  60. #ifdef MPPE
  61. bool refuse_mppe_stateful = 1; /* Allow stateful mode? */
  62. #endif
  63. static option_t ccp_option_list[] = {
  64. { "noccp", o_bool, &ccp_protent.enabled_flag,
  65. "Disable CCP negotiation" },
  66. { "-ccp", o_bool, &ccp_protent.enabled_flag,
  67. "Disable CCP negotiation", OPT_ALIAS },
  68. { "bsdcomp", o_special, (void *)setbsdcomp,
  69. "Request BSD-Compress packet compression",
  70. OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, bsd_value },
  71. { "nobsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
  72. "don't allow BSD-Compress", OPT_PRIOSUB | OPT_A2CLR,
  73. &ccp_allowoptions[0].bsd_compress },
  74. { "-bsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
  75. "don't allow BSD-Compress", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
  76. &ccp_allowoptions[0].bsd_compress },
  77. { "deflate", o_special, (void *)setdeflate,
  78. "request Deflate compression",
  79. OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, deflate_value },
  80. { "nodeflate", o_bool, &ccp_wantoptions[0].deflate,
  81. "don't allow Deflate compression", OPT_PRIOSUB | OPT_A2CLR,
  82. &ccp_allowoptions[0].deflate },
  83. { "-deflate", o_bool, &ccp_wantoptions[0].deflate,
  84. "don't allow Deflate compression", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
  85. &ccp_allowoptions[0].deflate },
  86. { "nodeflatedraft", o_bool, &ccp_wantoptions[0].deflate_draft,
  87. "don't use draft deflate #", OPT_A2COPY,
  88. &ccp_allowoptions[0].deflate_draft },
  89. { "predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
  90. "request Predictor-1", OPT_PRIO | 1 },
  91. { "nopredictor1", o_bool, &ccp_wantoptions[0].predictor_1,
  92. "don't allow Predictor-1", OPT_PRIOSUB | OPT_A2CLR,
  93. &ccp_allowoptions[0].predictor_1 },
  94. { "-predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
  95. "don't allow Predictor-1", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
  96. &ccp_allowoptions[0].predictor_1 },
  97. #ifdef MPPE
  98. /* MPPE options are symmetrical ... we only set wantoptions here */
  99. { "require-mppe", o_bool, &ccp_wantoptions[0].mppe,
  100. "require MPPE encryption",
  101. OPT_PRIO | MPPE_OPT_40 | MPPE_OPT_128 },
  102. { "+mppe", o_bool, &ccp_wantoptions[0].mppe,
  103. "require MPPE encryption",
  104. OPT_ALIAS | OPT_PRIO | MPPE_OPT_40 | MPPE_OPT_128 },
  105. { "nomppe", o_bool, &ccp_wantoptions[0].mppe,
  106. "don't allow MPPE encryption", OPT_PRIO },
  107. { "-mppe", o_bool, &ccp_wantoptions[0].mppe,
  108. "don't allow MPPE encryption", OPT_ALIAS | OPT_PRIO },
  109. /* We use ccp_allowoptions[0].mppe as a junk var ... it is reset later */
  110. { "require-mppe-40", o_bool, &ccp_allowoptions[0].mppe,
  111. "require MPPE 40-bit encryption", OPT_PRIO | OPT_A2OR | MPPE_OPT_40,
  112. &ccp_wantoptions[0].mppe },
  113. { "+mppe-40", o_bool, &ccp_allowoptions[0].mppe,
  114. "require MPPE 40-bit encryption", OPT_PRIO | OPT_A2OR | MPPE_OPT_40,
  115. &ccp_wantoptions[0].mppe },
  116. { "nomppe-40", o_bool, &ccp_allowoptions[0].mppe,
  117. "don't allow MPPE 40-bit encryption",
  118. OPT_PRIOSUB | OPT_A2CLRB | MPPE_OPT_40, &ccp_wantoptions[0].mppe },
  119. { "-mppe-40", o_bool, &ccp_allowoptions[0].mppe,
  120. "don't allow MPPE 40-bit encryption",
  121. OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLRB | MPPE_OPT_40,
  122. &ccp_wantoptions[0].mppe },
  123. { "require-mppe-128", o_bool, &ccp_allowoptions[0].mppe,
  124. "require MPPE 128-bit encryption", OPT_PRIO | OPT_A2OR | MPPE_OPT_128,
  125. &ccp_wantoptions[0].mppe },
  126. { "+mppe-128", o_bool, &ccp_allowoptions[0].mppe,
  127. "require MPPE 128-bit encryption",
  128. OPT_ALIAS | OPT_PRIO | OPT_A2OR | MPPE_OPT_128,
  129. &ccp_wantoptions[0].mppe },
  130. { "nomppe-128", o_bool, &ccp_allowoptions[0].mppe,
  131. "don't allow MPPE 128-bit encryption",
  132. OPT_PRIOSUB | OPT_A2CLRB | MPPE_OPT_128, &ccp_wantoptions[0].mppe },
  133. { "-mppe-128", o_bool, &ccp_allowoptions[0].mppe,
  134. "don't allow MPPE 128-bit encryption",
  135. OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLRB | MPPE_OPT_128,
  136. &ccp_wantoptions[0].mppe },
  137. /* strange one; we always request stateless, but will we allow stateful? */
  138. { "mppe-stateful", o_bool, &refuse_mppe_stateful,
  139. "allow MPPE stateful mode", OPT_PRIO },
  140. { "nomppe-stateful", o_bool, &refuse_mppe_stateful,
  141. "disallow MPPE stateful mode", OPT_PRIO | 1 },
  142. #endif /* MPPE */
  143. { NULL }
  144. };
  145. /*
  146. * Protocol entry points from main code.
  147. */
  148. static void ccp_init __P((int unit));
  149. static void ccp_open __P((int unit));
  150. static void ccp_close __P((int unit, char *));
  151. static void ccp_lowerup __P((int unit));
  152. static void ccp_lowerdown __P((int));
  153. static void ccp_input __P((int unit, u_char *pkt, int len));
  154. static void ccp_protrej __P((int unit));
  155. static int ccp_printpkt __P((u_char *pkt, int len,
  156. void (*printer) __P((void *, char *, ...)),
  157. void *arg));
  158. static void ccp_datainput __P((int unit, u_char *pkt, int len));
  159. struct protent ccp_protent = {
  160. PPP_CCP,
  161. ccp_init,
  162. ccp_input,
  163. ccp_protrej,
  164. ccp_lowerup,
  165. ccp_lowerdown,
  166. ccp_open,
  167. ccp_close,
  168. ccp_printpkt,
  169. ccp_datainput,
  170. 1,
  171. "CCP",
  172. "Compressed",
  173. ccp_option_list,
  174. NULL,
  175. NULL,
  176. NULL
  177. };
  178. fsm ccp_fsm[NUM_PPP];
  179. ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
  180. ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
  181. ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
  182. ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
  183. /*
  184. * Callbacks for fsm code.
  185. */
  186. static void ccp_resetci __P((fsm *));
  187. static int ccp_cilen __P((fsm *));
  188. static void ccp_addci __P((fsm *, u_char *, int *));
  189. static int ccp_ackci __P((fsm *, u_char *, int));
  190. static int ccp_nakci __P((fsm *, u_char *, int, int));
  191. static int ccp_rejci __P((fsm *, u_char *, int));
  192. static int ccp_reqci __P((fsm *, u_char *, int *, int));
  193. static void ccp_up __P((fsm *));
  194. static void ccp_down __P((fsm *));
  195. static int ccp_extcode __P((fsm *, int, int, u_char *, int));
  196. static void ccp_rack_timeout __P((void *));
  197. static char *method_name __P((ccp_options *, ccp_options *));
  198. static fsm_callbacks ccp_callbacks = {
  199. ccp_resetci,
  200. ccp_cilen,
  201. ccp_addci,
  202. ccp_ackci,
  203. ccp_nakci,
  204. ccp_rejci,
  205. ccp_reqci,
  206. ccp_up,
  207. ccp_down,
  208. NULL,
  209. NULL,
  210. NULL,
  211. NULL,
  212. ccp_extcode,
  213. "CCP"
  214. };
  215. /*
  216. * Do we want / did we get any compression?
  217. */
  218. #define ANY_COMPRESS(opt) ((opt).deflate || (opt).bsd_compress \
  219. || (opt).predictor_1 || (opt).predictor_2 \
  220. || (opt).mppe)
  221. /*
  222. * Local state (mainly for handling reset-reqs and reset-acks).
  223. */
  224. static int ccp_localstate[NUM_PPP];
  225. #define RACK_PENDING 1 /* waiting for reset-ack */
  226. #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
  227. #define RACKTIMEOUT 1 /* second */
  228. static int all_rejected[NUM_PPP]; /* we rejected all peer's options */
  229. /*
  230. * Option parsing.
  231. */
  232. static int
  233. setbsdcomp(argv)
  234. char **argv;
  235. {
  236. int rbits, abits;
  237. char *str, *endp;
  238. str = *argv;
  239. abits = rbits = strtol(str, &endp, 0);
  240. if (endp != str && *endp == ',') {
  241. str = endp + 1;
  242. abits = strtol(str, &endp, 0);
  243. }
  244. if (*endp != 0 || endp == str) {
  245. option_error("invalid parameter '%s' for bsdcomp option", *argv);
  246. return 0;
  247. }
  248. if ((rbits != 0 && (rbits < BSD_MIN_BITS || rbits > BSD_MAX_BITS))
  249. || (abits != 0 && (abits < BSD_MIN_BITS || abits > BSD_MAX_BITS))) {
  250. option_error("bsdcomp option values must be 0 or %d .. %d",
  251. BSD_MIN_BITS, BSD_MAX_BITS);
  252. return 0;
  253. }
  254. if (rbits > 0) {
  255. ccp_wantoptions[0].bsd_compress = 1;
  256. ccp_wantoptions[0].bsd_bits = rbits;
  257. } else
  258. ccp_wantoptions[0].bsd_compress = 0;
  259. if (abits > 0) {
  260. ccp_allowoptions[0].bsd_compress = 1;
  261. ccp_allowoptions[0].bsd_bits = abits;
  262. } else
  263. ccp_allowoptions[0].bsd_compress = 0;
  264. slprintf(bsd_value, sizeof(bsd_value),
  265. rbits == abits? "%d": "%d,%d", rbits, abits);
  266. return 1;
  267. }
  268. static int
  269. setdeflate(argv)
  270. char **argv;
  271. {
  272. int rbits, abits;
  273. char *str, *endp;
  274. str = *argv;
  275. abits = rbits = strtol(str, &endp, 0);
  276. if (endp != str && *endp == ',') {
  277. str = endp + 1;
  278. abits = strtol(str, &endp, 0);
  279. }
  280. if (*endp != 0 || endp == str) {
  281. option_error("invalid parameter '%s' for deflate option", *argv);
  282. return 0;
  283. }
  284. if ((rbits != 0 && (rbits < DEFLATE_MIN_SIZE || rbits > DEFLATE_MAX_SIZE))
  285. || (abits != 0 && (abits < DEFLATE_MIN_SIZE
  286. || abits > DEFLATE_MAX_SIZE))) {
  287. option_error("deflate option values must be 0 or %d .. %d",
  288. DEFLATE_MIN_SIZE, DEFLATE_MAX_SIZE);
  289. return 0;
  290. }
  291. if (rbits == DEFLATE_MIN_SIZE || abits == DEFLATE_MIN_SIZE) {
  292. if (rbits == DEFLATE_MIN_SIZE)
  293. rbits = DEFLATE_MIN_WORKS;
  294. if (abits == DEFLATE_MIN_SIZE)
  295. abits = DEFLATE_MIN_WORKS;
  296. warn("deflate option value of %d changed to %d to avoid zlib bug",
  297. DEFLATE_MIN_SIZE, DEFLATE_MIN_WORKS);
  298. }
  299. if (rbits > 0) {
  300. ccp_wantoptions[0].deflate = 1;
  301. ccp_wantoptions[0].deflate_size = rbits;
  302. } else
  303. ccp_wantoptions[0].deflate = 0;
  304. if (abits > 0) {
  305. ccp_allowoptions[0].deflate = 1;
  306. ccp_allowoptions[0].deflate_size = abits;
  307. } else
  308. ccp_allowoptions[0].deflate = 0;
  309. slprintf(deflate_value, sizeof(deflate_value),
  310. rbits == abits? "%d": "%d,%d", rbits, abits);
  311. return 1;
  312. }
  313. /*
  314. * ccp_init - initialize CCP.
  315. */
  316. static void
  317. ccp_init(unit)
  318. int unit;
  319. {
  320. fsm *f = &ccp_fsm[unit];
  321. f->unit = unit;
  322. f->protocol = PPP_CCP;
  323. f->callbacks = &ccp_callbacks;
  324. fsm_init(f);
  325. memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
  326. memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
  327. memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
  328. memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
  329. ccp_wantoptions[0].deflate = 1;
  330. ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
  331. ccp_wantoptions[0].deflate_correct = 1;
  332. ccp_wantoptions[0].deflate_draft = 1;
  333. ccp_allowoptions[0].deflate = 1;
  334. ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
  335. ccp_allowoptions[0].deflate_correct = 1;
  336. ccp_allowoptions[0].deflate_draft = 1;
  337. ccp_wantoptions[0].bsd_compress = 1;
  338. ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
  339. ccp_allowoptions[0].bsd_compress = 1;
  340. ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
  341. ccp_allowoptions[0].predictor_1 = 1;
  342. }
  343. /*
  344. * ccp_open - CCP is allowed to come up.
  345. */
  346. static void
  347. ccp_open(unit)
  348. int unit;
  349. {
  350. fsm *f = &ccp_fsm[unit];
  351. if (f->state != OPENED)
  352. ccp_flags_set(unit, 1, 0);
  353. /*
  354. * Find out which compressors the kernel supports before
  355. * deciding whether to open in silent mode.
  356. */
  357. ccp_resetci(f);
  358. if (!ANY_COMPRESS(ccp_gotoptions[unit]))
  359. f->flags |= OPT_SILENT;
  360. fsm_open(f);
  361. }
  362. /*
  363. * ccp_close - Terminate CCP.
  364. */
  365. static void
  366. ccp_close(unit, reason)
  367. int unit;
  368. char *reason;
  369. {
  370. ccp_flags_set(unit, 0, 0);
  371. fsm_close(&ccp_fsm[unit], reason);
  372. }
  373. /*
  374. * ccp_lowerup - we may now transmit CCP packets.
  375. */
  376. static void
  377. ccp_lowerup(unit)
  378. int unit;
  379. {
  380. fsm_lowerup(&ccp_fsm[unit]);
  381. }
  382. /*
  383. * ccp_lowerdown - we may not transmit CCP packets.
  384. */
  385. static void
  386. ccp_lowerdown(unit)
  387. int unit;
  388. {
  389. fsm_lowerdown(&ccp_fsm[unit]);
  390. }
  391. /*
  392. * ccp_input - process a received CCP packet.
  393. */
  394. static void
  395. ccp_input(unit, p, len)
  396. int unit;
  397. u_char *p;
  398. int len;
  399. {
  400. fsm *f = &ccp_fsm[unit];
  401. int oldstate;
  402. /*
  403. * Check for a terminate-request so we can print a message.
  404. */
  405. oldstate = f->state;
  406. fsm_input(f, p, len);
  407. if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED) {
  408. notice("Compression disabled by peer.");
  409. #ifdef MPPE
  410. if (ccp_gotoptions[unit].mppe) {
  411. error("MPPE disabled, closing LCP");
  412. lcp_close(unit, "MPPE disabled by peer");
  413. }
  414. #endif
  415. }
  416. /*
  417. * If we get a terminate-ack and we're not asking for compression,
  418. * close CCP.
  419. */
  420. if (oldstate == REQSENT && p[0] == TERMACK
  421. && !ANY_COMPRESS(ccp_gotoptions[unit]))
  422. ccp_close(unit, "No compression negotiated");
  423. }
  424. /*
  425. * Handle a CCP-specific code.
  426. */
  427. static int
  428. ccp_extcode(f, code, id, p, len)
  429. fsm *f;
  430. int code, id;
  431. u_char *p;
  432. int len;
  433. {
  434. switch (code) {
  435. case CCP_RESETREQ:
  436. if (f->state != OPENED)
  437. break;
  438. /* send a reset-ack, which the transmitter will see and
  439. reset its compression state. */
  440. fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
  441. break;
  442. case CCP_RESETACK:
  443. if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
  444. ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
  445. UNTIMEOUT(ccp_rack_timeout, f);
  446. }
  447. break;
  448. default:
  449. return 0;
  450. }
  451. return 1;
  452. }
  453. /*
  454. * ccp_protrej - peer doesn't talk CCP.
  455. */
  456. static void
  457. ccp_protrej(unit)
  458. int unit;
  459. {
  460. ccp_flags_set(unit, 0, 0);
  461. fsm_lowerdown(&ccp_fsm[unit]);
  462. #ifdef MPPE
  463. if (ccp_gotoptions[unit].mppe) {
  464. error("MPPE required but peer negotiation failed");
  465. lcp_close(unit, "MPPE required but peer negotiation failed");
  466. }
  467. #endif
  468. }
  469. /*
  470. * ccp_resetci - initialize at start of negotiation.
  471. */
  472. static void
  473. ccp_resetci(f)
  474. fsm *f;
  475. {
  476. ccp_options *go = &ccp_gotoptions[f->unit];
  477. u_char opt_buf[CCP_MAX_OPTION_LENGTH];
  478. *go = ccp_wantoptions[f->unit];
  479. all_rejected[f->unit] = 0;
  480. #ifdef MPPE
  481. if (go->mppe) {
  482. ccp_options *ao = &ccp_allowoptions[f->unit];
  483. int auth_mschap_bits = auth_done[f->unit];
  484. int numbits;
  485. /*
  486. * Start with a basic sanity check: mschap[v2] auth must be in
  487. * exactly one direction. RFC 3079 says that the keys are
  488. * 'derived from the credentials of the peer that initiated the call',
  489. * however the PPP protocol doesn't have such a concept, and pppd
  490. * cannot get this info externally. Instead we do the best we can.
  491. * NB: If MPPE is required, all other compression opts are invalid.
  492. * So, we return right away if we can't do it.
  493. */
  494. /* Leave only the mschap auth bits set */
  495. auth_mschap_bits &= (CHAP_MS_WITHPEER | CHAP_MS_PEER |
  496. CHAP_MS2_WITHPEER | CHAP_MS2_PEER);
  497. /* Count the mschap auths */
  498. auth_mschap_bits >>= CHAP_MS_SHIFT;
  499. numbits = 0;
  500. do {
  501. numbits += auth_mschap_bits & 1;
  502. auth_mschap_bits >>= 1;
  503. } while (auth_mschap_bits);
  504. if (numbits > 1) {
  505. error("MPPE required, but auth done in both directions.");
  506. lcp_close(f->unit, "MPPE required but not available");
  507. return;
  508. }
  509. if (!numbits) {
  510. error("MPPE required, but MS-CHAP[v2] auth not performed.");
  511. lcp_close(f->unit, "MPPE required but not available");
  512. return;
  513. }
  514. /* A plugin (eg radius) may not have obtained key material. */
  515. if (!mppe_keys_set) {
  516. error("MPPE required, but keys are not available. "
  517. "Possible plugin problem?");
  518. lcp_close(f->unit, "MPPE required but not available");
  519. return;
  520. }
  521. /* LM auth not supported for MPPE */
  522. if (auth_done[f->unit] & (CHAP_MS_WITHPEER | CHAP_MS_PEER)) {
  523. /* This might be noise */
  524. if (go->mppe & MPPE_OPT_40) {
  525. notice("Disabling 40-bit MPPE; MS-CHAP LM not supported");
  526. go->mppe &= ~MPPE_OPT_40;
  527. ccp_wantoptions[f->unit].mppe &= ~MPPE_OPT_40;
  528. }
  529. }
  530. /* Last check: can we actually negotiate something? */
  531. if (!(go->mppe & (MPPE_OPT_40 | MPPE_OPT_128))) {
  532. /* Could be misconfig, could be 40-bit disabled above. */
  533. error("MPPE required, but both 40-bit and 128-bit disabled.");
  534. lcp_close(f->unit, "MPPE required but not available");
  535. return;
  536. }
  537. /* sync options */
  538. ao->mppe = go->mppe;
  539. /* MPPE is not compatible with other compression types */
  540. ao->bsd_compress = go->bsd_compress = 0;
  541. ao->predictor_1 = go->predictor_1 = 0;
  542. ao->predictor_2 = go->predictor_2 = 0;
  543. ao->deflate = go->deflate = 0;
  544. }
  545. #endif /* MPPE */
  546. /*
  547. * Check whether the kernel knows about the various
  548. * compression methods we might request.
  549. */
  550. #ifdef MPPE
  551. if (go->mppe) {
  552. opt_buf[0] = CI_MPPE;
  553. opt_buf[1] = CILEN_MPPE;
  554. MPPE_OPTS_TO_CI(go->mppe, &opt_buf[2]);
  555. /* Key material unimportant here. */
  556. if (ccp_test(f->unit, opt_buf, CILEN_MPPE + MPPE_MAX_KEY_LEN, 0) <= 0) {
  557. error("MPPE required, but kernel has no support.");
  558. lcp_close(f->unit, "MPPE required but not available");
  559. }
  560. }
  561. #endif
  562. if (go->bsd_compress) {
  563. opt_buf[0] = CI_BSD_COMPRESS;
  564. opt_buf[1] = CILEN_BSD_COMPRESS;
  565. opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
  566. if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
  567. go->bsd_compress = 0;
  568. }
  569. if (go->deflate) {
  570. if (go->deflate_correct) {
  571. opt_buf[0] = CI_DEFLATE;
  572. opt_buf[1] = CILEN_DEFLATE;
  573. opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
  574. opt_buf[3] = DEFLATE_CHK_SEQUENCE;
  575. if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
  576. go->deflate_correct = 0;
  577. }
  578. if (go->deflate_draft) {
  579. opt_buf[0] = CI_DEFLATE_DRAFT;
  580. opt_buf[1] = CILEN_DEFLATE;
  581. opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
  582. opt_buf[3] = DEFLATE_CHK_SEQUENCE;
  583. if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
  584. go->deflate_draft = 0;
  585. }
  586. if (!go->deflate_correct && !go->deflate_draft)
  587. go->deflate = 0;
  588. }
  589. if (go->predictor_1) {
  590. opt_buf[0] = CI_PREDICTOR_1;
  591. opt_buf[1] = CILEN_PREDICTOR_1;
  592. if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
  593. go->predictor_1 = 0;
  594. }
  595. if (go->predictor_2) {
  596. opt_buf[0] = CI_PREDICTOR_2;
  597. opt_buf[1] = CILEN_PREDICTOR_2;
  598. if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
  599. go->predictor_2 = 0;
  600. }
  601. }
  602. /*
  603. * ccp_cilen - Return total length of our configuration info.
  604. */
  605. static int
  606. ccp_cilen(f)
  607. fsm *f;
  608. {
  609. ccp_options *go = &ccp_gotoptions[f->unit];
  610. return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
  611. + (go->deflate? CILEN_DEFLATE: 0)
  612. + (go->predictor_1? CILEN_PREDICTOR_1: 0)
  613. + (go->predictor_2? CILEN_PREDICTOR_2: 0)
  614. + (go->mppe? CILEN_MPPE: 0);
  615. }
  616. /*
  617. * ccp_addci - put our requests in a packet.
  618. */
  619. static void
  620. ccp_addci(f, p, lenp)
  621. fsm *f;
  622. u_char *p;
  623. int *lenp;
  624. {
  625. int res;
  626. ccp_options *go = &ccp_gotoptions[f->unit];
  627. u_char *p0 = p;
  628. /*
  629. * Add the compression types that we can receive, in decreasing
  630. * preference order. Get the kernel to allocate the first one
  631. * in case it gets Acked.
  632. */
  633. #ifdef MPPE
  634. if (go->mppe) {
  635. u_char opt_buf[CILEN_MPPE + MPPE_MAX_KEY_LEN];
  636. p[0] = opt_buf[0] = CI_MPPE;
  637. p[1] = opt_buf[1] = CILEN_MPPE;
  638. MPPE_OPTS_TO_CI(go->mppe, &p[2]);
  639. MPPE_OPTS_TO_CI(go->mppe, &opt_buf[2]);
  640. BCOPY(mppe_recv_key, &opt_buf[CILEN_MPPE], MPPE_MAX_KEY_LEN);
  641. res = ccp_test(f->unit, opt_buf, CILEN_MPPE + MPPE_MAX_KEY_LEN, 0);
  642. if (res > 0)
  643. p += CILEN_MPPE;
  644. else
  645. /* This shouldn't happen, we've already tested it! */
  646. lcp_close(f->unit, "MPPE required but not available in kernel");
  647. }
  648. #endif
  649. if (go->deflate) {
  650. p[0] = go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT;
  651. p[1] = CILEN_DEFLATE;
  652. p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
  653. p[3] = DEFLATE_CHK_SEQUENCE;
  654. if (p != p0) {
  655. p += CILEN_DEFLATE;
  656. } else {
  657. for (;;) {
  658. if (go->deflate_size < DEFLATE_MIN_WORKS) {
  659. go->deflate = 0;
  660. break;
  661. }
  662. res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
  663. if (res > 0) {
  664. p += CILEN_DEFLATE;
  665. break;
  666. } else if (res < 0) {
  667. go->deflate = 0;
  668. break;
  669. }
  670. --go->deflate_size;
  671. p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
  672. }
  673. }
  674. if (p != p0 && go->deflate_correct && go->deflate_draft) {
  675. p[0] = CI_DEFLATE_DRAFT;
  676. p[1] = CILEN_DEFLATE;
  677. p[2] = p[2 - CILEN_DEFLATE];
  678. p[3] = DEFLATE_CHK_SEQUENCE;
  679. p += CILEN_DEFLATE;
  680. }
  681. }
  682. if (go->bsd_compress) {
  683. p[0] = CI_BSD_COMPRESS;
  684. p[1] = CILEN_BSD_COMPRESS;
  685. p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
  686. if (p != p0) {
  687. p += CILEN_BSD_COMPRESS; /* not the first option */
  688. } else {
  689. for (;;) {
  690. if (go->bsd_bits < BSD_MIN_BITS) {
  691. go->bsd_compress = 0;
  692. break;
  693. }
  694. res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
  695. if (res > 0) {
  696. p += CILEN_BSD_COMPRESS;
  697. break;
  698. } else if (res < 0) {
  699. go->bsd_compress = 0;
  700. break;
  701. }
  702. --go->bsd_bits;
  703. p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
  704. }
  705. }
  706. }
  707. /* XXX Should Predictor 2 be preferable to Predictor 1? */
  708. if (go->predictor_1) {
  709. p[0] = CI_PREDICTOR_1;
  710. p[1] = CILEN_PREDICTOR_1;
  711. if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
  712. go->predictor_1 = 0;
  713. } else {
  714. p += CILEN_PREDICTOR_1;
  715. }
  716. }
  717. if (go->predictor_2) {
  718. p[0] = CI_PREDICTOR_2;
  719. p[1] = CILEN_PREDICTOR_2;
  720. if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
  721. go->predictor_2 = 0;
  722. } else {
  723. p += CILEN_PREDICTOR_2;
  724. }
  725. }
  726. go->method = (p > p0)? p0[0]: -1;
  727. *lenp = p - p0;
  728. }
  729. /*
  730. * ccp_ackci - process a received configure-ack, and return
  731. * 1 iff the packet was OK.
  732. */
  733. static int
  734. ccp_ackci(f, p, len)
  735. fsm *f;
  736. u_char *p;
  737. int len;
  738. {
  739. ccp_options *go = &ccp_gotoptions[f->unit];
  740. u_char *p0 = p;
  741. #ifdef MPPE
  742. if (go->mppe) {
  743. u_char opt_buf[CILEN_MPPE];
  744. opt_buf[0] = CI_MPPE;
  745. opt_buf[1] = CILEN_MPPE;
  746. MPPE_OPTS_TO_CI(go->mppe, &opt_buf[2]);
  747. if (len < CILEN_MPPE || memcmp(opt_buf, p, CILEN_MPPE))
  748. return 0;
  749. p += CILEN_MPPE;
  750. len -= CILEN_MPPE;
  751. /* XXX Cope with first/fast ack */
  752. if (len == 0)
  753. return 1;
  754. }
  755. #endif
  756. if (go->deflate) {
  757. if (len < CILEN_DEFLATE
  758. || p[0] != (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
  759. || p[1] != CILEN_DEFLATE
  760. || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
  761. || p[3] != DEFLATE_CHK_SEQUENCE)
  762. return 0;
  763. p += CILEN_DEFLATE;
  764. len -= CILEN_DEFLATE;
  765. /* XXX Cope with first/fast ack */
  766. if (len == 0)
  767. return 1;
  768. if (go->deflate_correct && go->deflate_draft) {
  769. if (len < CILEN_DEFLATE
  770. || p[0] != CI_DEFLATE_DRAFT
  771. || p[1] != CILEN_DEFLATE
  772. || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
  773. || p[3] != DEFLATE_CHK_SEQUENCE)
  774. return 0;
  775. p += CILEN_DEFLATE;
  776. len -= CILEN_DEFLATE;
  777. }
  778. }
  779. if (go->bsd_compress) {
  780. if (len < CILEN_BSD_COMPRESS
  781. || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
  782. || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
  783. return 0;
  784. p += CILEN_BSD_COMPRESS;
  785. len -= CILEN_BSD_COMPRESS;
  786. /* XXX Cope with first/fast ack */
  787. if (p == p0 && len == 0)
  788. return 1;
  789. }
  790. if (go->predictor_1) {
  791. if (len < CILEN_PREDICTOR_1
  792. || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
  793. return 0;
  794. p += CILEN_PREDICTOR_1;
  795. len -= CILEN_PREDICTOR_1;
  796. /* XXX Cope with first/fast ack */
  797. if (p == p0 && len == 0)
  798. return 1;
  799. }
  800. if (go->predictor_2) {
  801. if (len < CILEN_PREDICTOR_2
  802. || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
  803. return 0;
  804. p += CILEN_PREDICTOR_2;
  805. len -= CILEN_PREDICTOR_2;
  806. /* XXX Cope with first/fast ack */
  807. if (p == p0 && len == 0)
  808. return 1;
  809. }
  810. if (len != 0)
  811. return 0;
  812. return 1;
  813. }
  814. /*
  815. * ccp_nakci - process received configure-nak.
  816. * Returns 1 iff the nak was OK.
  817. */
  818. static int
  819. ccp_nakci(f, p, len, treat_as_reject)
  820. fsm *f;
  821. u_char *p;
  822. int len;
  823. int treat_as_reject;
  824. {
  825. ccp_options *go = &ccp_gotoptions[f->unit];
  826. ccp_options no; /* options we've seen already */
  827. ccp_options try; /* options to ask for next time */
  828. memset(&no, 0, sizeof(no));
  829. try = *go;
  830. #ifdef MPPE
  831. if (go->mppe && len >= CILEN_MPPE
  832. && p[0] == CI_MPPE && p[1] == CILEN_MPPE) {
  833. no.mppe = 1;
  834. /*
  835. * Peer wants us to use a different strength or other setting.
  836. * Fail if we aren't willing to use his suggestion.
  837. */
  838. MPPE_CI_TO_OPTS(&p[2], try.mppe);
  839. if ((try.mppe & MPPE_OPT_STATEFUL) && refuse_mppe_stateful) {
  840. error("Refusing MPPE stateful mode offered by peer");
  841. try.mppe = 0;
  842. } else if (((go->mppe | MPPE_OPT_STATEFUL) & try.mppe) != try.mppe) {
  843. /* Peer must have set options we didn't request (suggest) */
  844. try.mppe = 0;
  845. }
  846. if (!try.mppe) {
  847. error("MPPE required but peer negotiation failed");
  848. lcp_close(f->unit, "MPPE required but peer negotiation failed");
  849. }
  850. }
  851. #endif /* MPPE */
  852. if (go->deflate && len >= CILEN_DEFLATE
  853. && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
  854. && p[1] == CILEN_DEFLATE) {
  855. no.deflate = 1;
  856. /*
  857. * Peer wants us to use a different code size or something.
  858. * Stop asking for Deflate if we don't understand his suggestion.
  859. */
  860. if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
  861. || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_WORKS
  862. || p[3] != DEFLATE_CHK_SEQUENCE)
  863. try.deflate = 0;
  864. else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
  865. try.deflate_size = DEFLATE_SIZE(p[2]);
  866. p += CILEN_DEFLATE;
  867. len -= CILEN_DEFLATE;
  868. if (go->deflate_correct && go->deflate_draft
  869. && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
  870. && p[1] == CILEN_DEFLATE) {
  871. p += CILEN_DEFLATE;
  872. len -= CILEN_DEFLATE;
  873. }
  874. }
  875. if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
  876. && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
  877. no.bsd_compress = 1;
  878. /*
  879. * Peer wants us to use a different number of bits
  880. * or a different version.
  881. */
  882. if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
  883. try.bsd_compress = 0;
  884. else if (BSD_NBITS(p[2]) < go->bsd_bits)
  885. try.bsd_bits = BSD_NBITS(p[2]);
  886. p += CILEN_BSD_COMPRESS;
  887. len -= CILEN_BSD_COMPRESS;
  888. }
  889. /*
  890. * Predictor-1 and 2 have no options, so they can't be Naked.
  891. *
  892. * There may be remaining options but we ignore them.
  893. */
  894. if (f->state != OPENED)
  895. *go = try;
  896. return 1;
  897. }
  898. /*
  899. * ccp_rejci - reject some of our suggested compression methods.
  900. */
  901. static int
  902. ccp_rejci(f, p, len)
  903. fsm *f;
  904. u_char *p;
  905. int len;
  906. {
  907. ccp_options *go = &ccp_gotoptions[f->unit];
  908. ccp_options try; /* options to request next time */
  909. try = *go;
  910. /*
  911. * Cope with empty configure-rejects by ceasing to send
  912. * configure-requests.
  913. */
  914. if (len == 0 && all_rejected[f->unit])
  915. return -1;
  916. #ifdef MPPE
  917. if (go->mppe && len >= CILEN_MPPE
  918. && p[0] == CI_MPPE && p[1] == CILEN_MPPE) {
  919. error("MPPE required but peer refused");
  920. lcp_close(f->unit, "MPPE required but peer refused");
  921. p += CILEN_MPPE;
  922. len -= CILEN_MPPE;
  923. }
  924. #endif
  925. if (go->deflate_correct && len >= CILEN_DEFLATE
  926. && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
  927. if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
  928. || p[3] != DEFLATE_CHK_SEQUENCE)
  929. return 0; /* Rej is bad */
  930. try.deflate_correct = 0;
  931. p += CILEN_DEFLATE;
  932. len -= CILEN_DEFLATE;
  933. }
  934. if (go->deflate_draft && len >= CILEN_DEFLATE
  935. && p[0] == CI_DEFLATE_DRAFT && p[1] == CILEN_DEFLATE) {
  936. if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
  937. || p[3] != DEFLATE_CHK_SEQUENCE)
  938. return 0; /* Rej is bad */
  939. try.deflate_draft = 0;
  940. p += CILEN_DEFLATE;
  941. len -= CILEN_DEFLATE;
  942. }
  943. if (!try.deflate_correct && !try.deflate_draft)
  944. try.deflate = 0;
  945. if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
  946. && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
  947. if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
  948. return 0;
  949. try.bsd_compress = 0;
  950. p += CILEN_BSD_COMPRESS;
  951. len -= CILEN_BSD_COMPRESS;
  952. }
  953. if (go->predictor_1 && len >= CILEN_PREDICTOR_1
  954. && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
  955. try.predictor_1 = 0;
  956. p += CILEN_PREDICTOR_1;
  957. len -= CILEN_PREDICTOR_1;
  958. }
  959. if (go->predictor_2 && len >= CILEN_PREDICTOR_2
  960. && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
  961. try.predictor_2 = 0;
  962. p += CILEN_PREDICTOR_2;
  963. len -= CILEN_PREDICTOR_2;
  964. }
  965. if (len != 0)
  966. return 0;
  967. if (f->state != OPENED)
  968. *go = try;
  969. return 1;
  970. }
  971. /*
  972. * ccp_reqci - processed a received configure-request.
  973. * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
  974. * appropriately.
  975. */
  976. static int
  977. ccp_reqci(f, p, lenp, dont_nak)
  978. fsm *f;
  979. u_char *p;
  980. int *lenp;
  981. int dont_nak;
  982. {
  983. int ret, newret, res;
  984. u_char *p0, *retp;
  985. int len, clen, type, nb;
  986. ccp_options *ho = &ccp_hisoptions[f->unit];
  987. ccp_options *ao = &ccp_allowoptions[f->unit];
  988. #ifdef MPPE
  989. bool rej_for_ci_mppe = 1; /* Are we rejecting based on a bad/missing */
  990. /* CI_MPPE, or due to other options? */
  991. #endif
  992. ret = CONFACK;
  993. retp = p0 = p;
  994. len = *lenp;
  995. memset(ho, 0, sizeof(ccp_options));
  996. ho->method = (len > 0)? p[0]: -1;
  997. while (len > 0) {
  998. newret = CONFACK;
  999. if (len < 2 || p[1] < 2 || p[1] > len) {
  1000. /* length is bad */
  1001. clen = len;
  1002. newret = CONFREJ;
  1003. } else {
  1004. type = p[0];
  1005. clen = p[1];
  1006. switch (type) {
  1007. #ifdef MPPE
  1008. case CI_MPPE:
  1009. if (!ao->mppe || clen != CILEN_MPPE) {
  1010. newret = CONFREJ;
  1011. break;
  1012. }
  1013. MPPE_CI_TO_OPTS(&p[2], ho->mppe);
  1014. /* Nak if anything unsupported or unknown are set. */
  1015. if (ho->mppe & MPPE_OPT_UNSUPPORTED) {
  1016. newret = CONFNAK;
  1017. ho->mppe &= ~MPPE_OPT_UNSUPPORTED;
  1018. }
  1019. if (ho->mppe & MPPE_OPT_UNKNOWN) {
  1020. newret = CONFNAK;
  1021. ho->mppe &= ~MPPE_OPT_UNKNOWN;
  1022. }
  1023. /* Check state opt */
  1024. if (ho->mppe & MPPE_OPT_STATEFUL) {
  1025. /*
  1026. * We can Nak and request stateless, but it's a
  1027. * lot easier to just assume the peer will request
  1028. * it if he can do it; stateful mode is bad over
  1029. * the Internet -- which is where we expect MPPE.
  1030. */
  1031. if (refuse_mppe_stateful) {
  1032. error("Refusing MPPE stateful mode offered by peer");
  1033. newret = CONFREJ;
  1034. break;
  1035. }
  1036. }
  1037. /* Find out which of {S,L} are set. */
  1038. if ((ho->mppe & MPPE_OPT_128)
  1039. && (ho->mppe & MPPE_OPT_40)) {
  1040. /* Both are set, negotiate the strongest. */
  1041. newret = CONFNAK;
  1042. if (ao->mppe & MPPE_OPT_128)
  1043. ho->mppe &= ~MPPE_OPT_40;
  1044. else if (ao->mppe & MPPE_OPT_40)
  1045. ho->mppe &= ~MPPE_OPT_128;
  1046. else {
  1047. newret = CONFREJ;
  1048. break;
  1049. }
  1050. } else if (ho->mppe & MPPE_OPT_128) {
  1051. if (!(ao->mppe & MPPE_OPT_128)) {
  1052. newret = CONFREJ;
  1053. break;
  1054. }
  1055. } else if (ho->mppe & MPPE_OPT_40) {
  1056. if (!(ao->mppe & MPPE_OPT_40)) {
  1057. newret = CONFREJ;
  1058. break;
  1059. }
  1060. } else {
  1061. /* Neither are set. */
  1062. /* We cannot accept this. */
  1063. newret = CONFNAK;
  1064. /* Give the peer our idea of what can be used,
  1065. so it can choose and confirm */
  1066. ho->mppe = ao->mppe;
  1067. }
  1068. /* rebuild the opts */
  1069. MPPE_OPTS_TO_CI(ho->mppe, &p[2]);
  1070. if (newret == CONFACK) {
  1071. u_char opt_buf[CILEN_MPPE + MPPE_MAX_KEY_LEN];
  1072. int mtu;
  1073. BCOPY(p, opt_buf, CILEN_MPPE);
  1074. BCOPY(mppe_send_key, &opt_buf[CILEN_MPPE],
  1075. MPPE_MAX_KEY_LEN);
  1076. if (ccp_test(f->unit, opt_buf,
  1077. CILEN_MPPE + MPPE_MAX_KEY_LEN, 1) <= 0) {
  1078. /* This shouldn't happen, we've already tested it! */
  1079. error("MPPE required, but kernel has no support.");
  1080. lcp_close(f->unit, "MPPE required but not available");
  1081. newret = CONFREJ;
  1082. break;
  1083. }
  1084. /*
  1085. * We need to decrease the interface MTU by MPPE_PAD
  1086. * because MPPE frames **grow**. The kernel [must]
  1087. * allocate MPPE_PAD extra bytes in xmit buffers.
  1088. */
  1089. mtu = netif_get_mtu(f->unit);
  1090. if (mtu)
  1091. netif_set_mtu(f->unit, mtu - MPPE_PAD);
  1092. else
  1093. newret = CONFREJ;
  1094. }
  1095. /*
  1096. * We have accepted MPPE or are willing to negotiate
  1097. * MPPE parameters. A CONFREJ is due to subsequent
  1098. * (non-MPPE) processing.
  1099. */
  1100. rej_for_ci_mppe = 0;
  1101. break;
  1102. #endif /* MPPE */
  1103. case CI_DEFLATE:
  1104. case CI_DEFLATE_DRAFT:
  1105. if (!ao->deflate || clen != CILEN_DEFLATE
  1106. || (!ao->deflate_correct && type == CI_DEFLATE)
  1107. || (!ao->deflate_draft && type == CI_DEFLATE_DRAFT)) {
  1108. newret = CONFREJ;
  1109. break;
  1110. }
  1111. ho->deflate = 1;
  1112. ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
  1113. if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
  1114. || p[3] != DEFLATE_CHK_SEQUENCE
  1115. || nb > ao->deflate_size || nb < DEFLATE_MIN_WORKS) {
  1116. newret = CONFNAK;
  1117. if (!dont_nak) {
  1118. p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
  1119. p[3] = DEFLATE_CHK_SEQUENCE;
  1120. /* fall through to test this #bits below */
  1121. } else
  1122. break;
  1123. }
  1124. /*
  1125. * Check whether we can do Deflate with the window
  1126. * size they want. If the window is too big, reduce
  1127. * it until the kernel can cope and nak with that.
  1128. * We only check this for the first option.
  1129. */
  1130. if (p == p0) {
  1131. for (;;) {
  1132. res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
  1133. if (res > 0)
  1134. break; /* it's OK now */
  1135. if (res < 0 || nb == DEFLATE_MIN_WORKS || dont_nak) {
  1136. newret = CONFREJ;
  1137. p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
  1138. break;
  1139. }
  1140. newret = CONFNAK;
  1141. --nb;
  1142. p[2] = DEFLATE_MAKE_OPT(nb);
  1143. }
  1144. }
  1145. break;
  1146. case CI_BSD_COMPRESS:
  1147. if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
  1148. newret = CONFREJ;
  1149. break;
  1150. }
  1151. ho->bsd_compress = 1;
  1152. ho->bsd_bits = nb = BSD_NBITS(p[2]);
  1153. if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
  1154. || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
  1155. newret = CONFNAK;
  1156. if (!dont_nak) {
  1157. p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
  1158. /* fall through to test this #bits below */
  1159. } else
  1160. break;
  1161. }
  1162. /*
  1163. * Check whether we can do BSD-Compress with the code
  1164. * size they want. If the code size is too big, reduce
  1165. * it until the kernel can cope and nak with that.
  1166. * We only check this for the first option.
  1167. */
  1168. if (p == p0) {
  1169. for (;;) {
  1170. res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
  1171. if (res > 0)
  1172. break;
  1173. if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
  1174. newret = CONFREJ;
  1175. p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
  1176. ho->bsd_bits);
  1177. break;
  1178. }
  1179. newret = CONFNAK;
  1180. --nb;
  1181. p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
  1182. }
  1183. }
  1184. break;
  1185. case CI_PREDICTOR_1:
  1186. if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
  1187. newret = CONFREJ;
  1188. break;
  1189. }
  1190. ho->predictor_1 = 1;
  1191. if (p == p0
  1192. && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
  1193. newret = CONFREJ;
  1194. }
  1195. break;
  1196. case CI_PREDICTOR_2:
  1197. if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
  1198. newret = CONFREJ;
  1199. break;
  1200. }
  1201. ho->predictor_2 = 1;
  1202. if (p == p0
  1203. && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
  1204. newret = CONFREJ;
  1205. }
  1206. break;
  1207. default:
  1208. newret = CONFREJ;
  1209. }
  1210. }
  1211. if (newret == CONFNAK && dont_nak)
  1212. newret = CONFREJ;
  1213. if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
  1214. /* we're returning this option */
  1215. if (newret == CONFREJ && ret == CONFNAK)
  1216. retp = p0;
  1217. ret = newret;
  1218. if (p != retp)
  1219. BCOPY(p, retp, clen);
  1220. retp += clen;
  1221. }
  1222. p += clen;
  1223. len -= clen;
  1224. }
  1225. if (ret != CONFACK) {
  1226. if (ret == CONFREJ && *lenp == retp - p0)
  1227. all_rejected[f->unit] = 1;
  1228. else
  1229. *lenp = retp - p0;
  1230. }
  1231. #ifdef MPPE
  1232. if (ret == CONFREJ && ao->mppe && rej_for_ci_mppe) {
  1233. error("MPPE required but peer negotiation failed");
  1234. lcp_close(f->unit, "MPPE required but peer negotiation failed");
  1235. }
  1236. #endif
  1237. return ret;
  1238. }
  1239. /*
  1240. * Make a string name for a compression method (or 2).
  1241. */
  1242. static char *
  1243. method_name(opt, opt2)
  1244. ccp_options *opt, *opt2;
  1245. {
  1246. static char result[64];
  1247. if (!ANY_COMPRESS(*opt))
  1248. return "(none)";
  1249. switch (opt->method) {
  1250. #ifdef MPPE
  1251. case CI_MPPE:
  1252. {
  1253. char *p = result;
  1254. char *q = result + sizeof(result); /* 1 past result */
  1255. slprintf(p, q - p, "MPPE ");
  1256. p += 5;
  1257. if (opt->mppe & MPPE_OPT_128) {
  1258. slprintf(p, q - p, "128-bit ");
  1259. p += 8;
  1260. }
  1261. if (opt->mppe & MPPE_OPT_40) {
  1262. slprintf(p, q - p, "40-bit ");
  1263. p += 7;
  1264. }
  1265. if (opt->mppe & MPPE_OPT_STATEFUL)
  1266. slprintf(p, q - p, "stateful");
  1267. else
  1268. slprintf(p, q - p, "stateless");
  1269. break;
  1270. }
  1271. #endif
  1272. case CI_DEFLATE:
  1273. case CI_DEFLATE_DRAFT:
  1274. if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
  1275. slprintf(result, sizeof(result), "Deflate%s (%d/%d)",
  1276. (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
  1277. opt->deflate_size, opt2->deflate_size);
  1278. else
  1279. slprintf(result, sizeof(result), "Deflate%s (%d)",
  1280. (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
  1281. opt->deflate_size);
  1282. break;
  1283. case CI_BSD_COMPRESS:
  1284. if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
  1285. slprintf(result, sizeof(result), "BSD-Compress (%d/%d)",
  1286. opt->bsd_bits, opt2->bsd_bits);
  1287. else
  1288. slprintf(result, sizeof(result), "BSD-Compress (%d)",
  1289. opt->bsd_bits);
  1290. break;
  1291. case CI_PREDICTOR_1:
  1292. return "Predictor 1";
  1293. case CI_PREDICTOR_2:
  1294. return "Predictor 2";
  1295. default:
  1296. slprintf(result, sizeof(result), "Method %d", opt->method);
  1297. }
  1298. return result;
  1299. }
  1300. /*
  1301. * CCP has come up - inform the kernel driver and log a message.
  1302. */
  1303. static void
  1304. ccp_up(f)
  1305. fsm *f;
  1306. {
  1307. ccp_options *go = &ccp_gotoptions[f->unit];
  1308. ccp_options *ho = &ccp_hisoptions[f->unit];
  1309. char method1[64];
  1310. ccp_flags_set(f->unit, 1, 1);
  1311. if (ANY_COMPRESS(*go)) {
  1312. if (ANY_COMPRESS(*ho)) {
  1313. if (go->method == ho->method) {
  1314. notice("%s compression enabled", method_name(go, ho));
  1315. } else {
  1316. strlcpy(method1, method_name(go, NULL), sizeof(method1));
  1317. notice("%s / %s compression enabled",
  1318. method1, method_name(ho, NULL));
  1319. }
  1320. } else
  1321. notice("%s receive compression enabled", method_name(go, NULL));
  1322. } else if (ANY_COMPRESS(*ho))
  1323. notice("%s transmit compression enabled", method_name(ho, NULL));
  1324. #ifdef MPPE
  1325. if (go->mppe) {
  1326. BZERO(mppe_recv_key, MPPE_MAX_KEY_LEN);
  1327. BZERO(mppe_send_key, MPPE_MAX_KEY_LEN);
  1328. continue_networks(f->unit); /* Bring up IP et al */
  1329. }
  1330. #endif
  1331. }
  1332. /*
  1333. * CCP has gone down - inform the kernel driver.
  1334. */
  1335. static void
  1336. ccp_down(f)
  1337. fsm *f;
  1338. {
  1339. if (ccp_localstate[f->unit] & RACK_PENDING)
  1340. UNTIMEOUT(ccp_rack_timeout, f);
  1341. ccp_localstate[f->unit] = 0;
  1342. ccp_flags_set(f->unit, 1, 0);
  1343. #ifdef MPPE
  1344. if (ccp_gotoptions[f->unit].mppe) {
  1345. ccp_gotoptions[f->unit].mppe = 0;
  1346. if (lcp_fsm[f->unit].state == OPENED) {
  1347. /* If LCP is not already going down, make sure it does. */
  1348. error("MPPE disabled");
  1349. lcp_close(f->unit, "MPPE disabled");
  1350. }
  1351. }
  1352. #endif
  1353. }
  1354. /*
  1355. * Print the contents of a CCP packet.
  1356. */
  1357. static char *ccp_codenames[] = {
  1358. "ConfReq", "ConfAck", "ConfNak", "ConfRej",
  1359. "TermReq", "TermAck", "CodeRej",
  1360. NULL, NULL, NULL, NULL, NULL, NULL,
  1361. "ResetReq", "ResetAck",
  1362. };
  1363. static int
  1364. ccp_printpkt(p, plen, printer, arg)
  1365. u_char *p;
  1366. int plen;
  1367. void (*printer) __P((void *, char *, ...));
  1368. void *arg;
  1369. {
  1370. u_char *p0, *optend;
  1371. int code, id, len;
  1372. int optlen;
  1373. p0 = p;
  1374. if (plen < HEADERLEN)
  1375. return 0;
  1376. code = p[0];
  1377. id = p[1];
  1378. len = (p[2] << 8) + p[3];
  1379. if (len < HEADERLEN || len > plen)
  1380. return 0;
  1381. if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
  1382. && ccp_codenames[code-1] != NULL)
  1383. printer(arg, " %s", ccp_codenames[code-1]);
  1384. else
  1385. printer(arg, " code=0x%x", code);
  1386. printer(arg, " id=0x%x", id);
  1387. len -= HEADERLEN;
  1388. p += HEADERLEN;
  1389. switch (code) {
  1390. case CONFREQ:
  1391. case CONFACK:
  1392. case CONFNAK:
  1393. case CONFREJ:
  1394. /* print list of possible compression methods */
  1395. while (len >= 2) {
  1396. code = p[0];
  1397. optlen = p[1];
  1398. if (optlen < 2 || optlen > len)
  1399. break;
  1400. printer(arg, " <");
  1401. len -= optlen;
  1402. optend = p + optlen;
  1403. switch (code) {
  1404. #ifdef MPPE
  1405. case CI_MPPE:
  1406. if (optlen >= CILEN_MPPE) {
  1407. u_char mppe_opts;
  1408. MPPE_CI_TO_OPTS(&p[2], mppe_opts);
  1409. printer(arg, "mppe %s %s %s %s %s %s%s",
  1410. (p[2] & MPPE_H_BIT)? "+H": "-H",
  1411. (p[5] & MPPE_M_BIT)? "+M": "-M",
  1412. (p[5] & MPPE_S_BIT)? "+S": "-S",
  1413. (p[5] & MPPE_L_BIT)? "+L": "-L",
  1414. (p[5] & MPPE_D_BIT)? "+D": "-D",
  1415. (p[5] & MPPE_C_BIT)? "+C": "-C",
  1416. (mppe_opts & MPPE_OPT_UNKNOWN)? " +U": "");
  1417. if (mppe_opts & MPPE_OPT_UNKNOWN)
  1418. printer(arg, " (%.2x %.2x %.2x %.2x)",
  1419. p[2], p[3], p[4], p[5]);
  1420. p += CILEN_MPPE;
  1421. }
  1422. break;
  1423. #endif
  1424. case CI_DEFLATE:
  1425. case CI_DEFLATE_DRAFT:
  1426. if (optlen >= CILEN_DEFLATE) {
  1427. printer(arg, "deflate%s %d",
  1428. (code == CI_DEFLATE_DRAFT? "(old#)": ""),
  1429. DEFLATE_SIZE(p[2]));
  1430. if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
  1431. printer(arg, " method %d", DEFLATE_METHOD(p[2]));
  1432. if (p[3] != DEFLATE_CHK_SEQUENCE)
  1433. printer(arg, " check %d", p[3]);
  1434. p += CILEN_DEFLATE;
  1435. }
  1436. break;
  1437. case CI_BSD_COMPRESS:
  1438. if (optlen >= CILEN_BSD_COMPRESS) {
  1439. printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
  1440. BSD_NBITS(p[2]));
  1441. p += CILEN_BSD_COMPRESS;
  1442. }
  1443. break;
  1444. case CI_PREDICTOR_1:
  1445. if (optlen >= CILEN_PREDICTOR_1) {
  1446. printer(arg, "predictor 1");
  1447. p += CILEN_PREDICTOR_1;
  1448. }
  1449. break;
  1450. case CI_PREDICTOR_2:
  1451. if (optlen >= CILEN_PREDICTOR_2) {
  1452. printer(arg, "predictor 2");
  1453. p += CILEN_PREDICTOR_2;
  1454. }
  1455. break;
  1456. }
  1457. while (p < optend)
  1458. printer(arg, " %.2x", *p++);
  1459. printer(arg, ">");
  1460. }
  1461. break;
  1462. case TERMACK:
  1463. case TERMREQ:
  1464. if (len > 0 && *p >= ' ' && *p < 0x7f) {
  1465. print_string((char *)p, len, printer, arg);
  1466. p += len;
  1467. len = 0;
  1468. }
  1469. break;
  1470. }
  1471. /* dump out the rest of the packet in hex */
  1472. while (--len >= 0)
  1473. printer(arg, " %.2x", *p++);
  1474. return p - p0;
  1475. }
  1476. /*
  1477. * We have received a packet that the decompressor failed to
  1478. * decompress. Here we would expect to issue a reset-request, but
  1479. * Motorola has a patent on resetting the compressor as a result of
  1480. * detecting an error in the decompressed data after decompression.
  1481. * (See US patent 5,130,993; international patent publication number
  1482. * WO 91/10289; Australian patent 73296/91.)
  1483. *
  1484. * So we ask the kernel whether the error was detected after
  1485. * decompression; if it was, we take CCP down, thus disabling
  1486. * compression :-(, otherwise we issue the reset-request.
  1487. */
  1488. static void
  1489. ccp_datainput(unit, pkt, len)
  1490. int unit;
  1491. u_char *pkt;
  1492. int len;
  1493. {
  1494. fsm *f;
  1495. f = &ccp_fsm[unit];
  1496. if (f->state == OPENED) {
  1497. if (ccp_fatal_error(unit)) {
  1498. /*
  1499. * Disable compression by taking CCP down.
  1500. */
  1501. error("Lost compression sync: disabling compression");
  1502. ccp_close(unit, "Lost compression sync");
  1503. #ifdef MPPE
  1504. /*
  1505. * If we were doing MPPE, we must also take the link down.
  1506. */
  1507. if (ccp_gotoptions[unit].mppe) {
  1508. error("Too many MPPE errors, closing LCP");
  1509. lcp_close(unit, "Too many MPPE errors");
  1510. }
  1511. #endif
  1512. } else {
  1513. /*
  1514. * Send a reset-request to reset the peer's compressor.
  1515. * We don't do that if we are still waiting for an
  1516. * acknowledgement to a previous reset-request.
  1517. */
  1518. if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
  1519. fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
  1520. TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
  1521. ccp_localstate[f->unit] |= RACK_PENDING;
  1522. } else
  1523. ccp_localstate[f->unit] |= RREQ_REPEAT;
  1524. }
  1525. }
  1526. }
  1527. /*
  1528. * Timeout waiting for reset-ack.
  1529. */
  1530. static void
  1531. ccp_rack_timeout(arg)
  1532. void *arg;
  1533. {
  1534. fsm *f = arg;
  1535. if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
  1536. fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
  1537. TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
  1538. ccp_localstate[f->unit] &= ~RREQ_REPEAT;
  1539. } else
  1540. ccp_localstate[f->unit] &= ~RACK_PENDING;
  1541. }