]> git.ozlabs.org Git - ppp.git/blob - pppd/lcp.c
Initial revision
[ppp.git] / pppd / lcp.c
1 /*
2  * lcp.c - PPP Link Control Protocol.
3  *
4  * Copyright (c) 1989 Carnegie Mellon University.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that the above copyright notice and this paragraph are
9  * duplicated in all such forms and that any documentation,
10  * advertising materials, and other materials related to such
11  * distribution and use acknowledge that the software was developed
12  * by Carnegie Mellon University.  The name of the
13  * University may not be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19
20 #ifndef lint
21 static char rcsid[] = "$Id: lcp.c,v 1.4 1994/05/09 04:32:41 paulus Exp $";
22 #endif
23
24 /*
25  * TODO:
26  * Option tracing.
27  * Test restart.
28  */
29
30 #include <stdio.h>
31 #include <syslog.h>
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/time.h>
36
37 #include <net/if.h>
38 #include <net/if_ppp.h>
39 #include <netinet/in.h>
40
41 #include <string.h>
42
43 #include "pppd.h"
44 #include "ppp.h"
45 #include "fsm.h"
46 #include "lcp.h"
47 #include "magic.h"
48 #include "chap.h"
49 #include "upap.h"
50 #include "ipcp.h"
51
52 /* global vars */
53 fsm lcp_fsm[NPPP];                      /* LCP fsm structure (global)*/
54 lcp_options lcp_wantoptions[NPPP];      /* Options that we want to request */
55 lcp_options lcp_gotoptions[NPPP];       /* Options that peer ack'd */
56 lcp_options lcp_allowoptions[NPPP];     /* Options we allow peer to request */
57 lcp_options lcp_hisoptions[NPPP];       /* Options that we ack'd */
58 u_long xmit_accm[NPPP][8];              /* extended transmit ACCM */
59
60 /*
61  * Callbacks for fsm code.  (CI = Configuration Information)
62  */
63 static void lcp_resetci __ARGS((fsm *));        /* Reset our CI */
64 static int  lcp_cilen __ARGS((fsm *));          /* Return length of our CI */
65 static void lcp_addci __ARGS((fsm *, u_char *, int *)); /* Add our CI to pkt */
66 static int  lcp_ackci __ARGS((fsm *, u_char *, int)); /* Peer ack'd our CI */
67 static int  lcp_nakci __ARGS((fsm *, u_char *, int)); /* Peer nak'd our CI */
68 static int  lcp_rejci __ARGS((fsm *, u_char *, int)); /* Peer rej'd our CI */
69 static int  lcp_reqci __ARGS((fsm *, u_char *, int *, int)); /* Rcv peer CI */
70 static void lcp_up __ARGS((fsm *));             /* We're UP */
71 static void lcp_down __ARGS((fsm *));           /* We're DOWN */
72 static void lcp_starting __ARGS((fsm *));       /* We need lower layer up */
73 static void lcp_finished __ARGS((fsm *));       /* We need lower layer down */
74 static int  lcp_extcode __ARGS((fsm *, int, int, u_char *, int));
75 static void lcp_rprotrej __ARGS((fsm *, u_char *, int));
76
77 static fsm_callbacks lcp_callbacks = {  /* LCP callback routines */
78     lcp_resetci,                /* Reset our Configuration Information */
79     lcp_cilen,                  /* Length of our Configuration Information */
80     lcp_addci,                  /* Add our Configuration Information */
81     lcp_ackci,                  /* ACK our Configuration Information */
82     lcp_nakci,                  /* NAK our Configuration Information */
83     lcp_rejci,                  /* Reject our Configuration Information */
84     lcp_reqci,                  /* Request peer's Configuration Information */
85     lcp_up,                     /* Called when fsm reaches OPENED state */
86     lcp_down,                   /* Called when fsm leaves OPENED state */
87     lcp_starting,               /* Called when we want the lower layer up */
88     lcp_finished,               /* Called when we want the lower layer down */
89     NULL,                       /* Called when Protocol-Reject received */
90     NULL,                       /* Retransmission is necessary */
91     lcp_extcode,                /* Called to handle LCP-specific codes */
92     "LCP"                       /* String name of protocol */
93 };
94
95 int lcp_warnloops = DEFWARNLOOPS; /* Warn about a loopback this often */
96
97 /*
98  * Length of each type of configuration option (in octets)
99  */
100 #define CILEN_VOID      2
101 #define CILEN_SHORT     4       /* CILEN_VOID + sizeof(short) */
102 #define CILEN_CHAP      5       /* CILEN_VOID + sizeof(short) + 1 */
103 #define CILEN_LONG      6       /* CILEN_VOID + sizeof(long) */
104 #define CILEN_LQR       8       /* CILEN_VOID + sizeof(short) + sizeof(long) */
105
106 #define CODENAME(x)     ((x) == CONFACK ? "ACK" : \
107                          (x) == CONFNAK ? "NAK" : "REJ")
108
109
110 /*
111  * lcp_init - Initialize LCP.
112  */
113 void
114 lcp_init(unit)
115     int unit;
116 {
117     fsm *f = &lcp_fsm[unit];
118     lcp_options *wo = &lcp_wantoptions[unit];
119     lcp_options *ao = &lcp_allowoptions[unit];
120
121     f->unit = unit;
122     f->protocol = LCP;
123     f->callbacks = &lcp_callbacks;
124
125     fsm_init(f);
126
127     wo->passive = 0;
128     wo->silent = 0;
129     wo->restart = 0;                    /* Set to 1 in kernels or multi-line
130                                            implementations */
131     wo->neg_mru = 1;
132     wo->mru = DEFMRU;
133     wo->neg_asyncmap = 0;
134     wo->asyncmap = 0;
135     wo->neg_chap = 0;                   /* Set to 1 on server */
136     wo->neg_upap = 0;                   /* Set to 1 on server */
137     wo->chap_mdtype = CHAP_DIGEST_MD5;
138     wo->neg_magicnumber = 1;
139     wo->neg_pcompression = 1;
140     wo->neg_accompression = 1;
141     wo->neg_lqr = 0;                    /* no LQR implementation yet */
142
143     ao->neg_mru = 1;
144     ao->mru = MAXMRU;
145     ao->neg_asyncmap = 1;
146     ao->asyncmap = 0;
147     ao->neg_chap = 1;
148     ao->chap_mdtype = CHAP_DIGEST_MD5;
149     ao->neg_upap = 1;
150     ao->neg_magicnumber = 1;
151     ao->neg_pcompression = 1;
152     ao->neg_accompression = 1;
153     ao->neg_lqr = 0;                    /* no LQR implementation yet */
154
155     memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
156     xmit_accm[unit][3] = 0x60000000;
157 }
158
159
160 /*
161  * lcp_open - LCP is allowed to come up.
162  */
163 void
164 lcp_open(unit)
165     int unit;
166 {
167     fsm *f = &lcp_fsm[unit];
168     lcp_options *wo = &lcp_wantoptions[unit];
169
170     f->flags = 0;
171     if (wo->passive)
172         f->flags |= OPT_PASSIVE;
173     if (wo->silent)
174         f->flags |= OPT_SILENT;
175     fsm_open(f);
176 }
177
178
179 /*
180  * lcp_close - Take LCP down.
181  */
182 void
183 lcp_close(unit)
184     int unit;
185 {
186     fsm *f = &lcp_fsm[unit];
187
188     if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
189         /*
190          * This action is not strictly according to the FSM in RFC1548,
191          * but it does mean that the program terminates if you do a
192          * lcp_close(0) in passive/silent mode when a connection hasn't
193          * been established.
194          */
195         f->state = CLOSED;
196         lcp_finished(f);
197
198     } else
199         fsm_close(&lcp_fsm[unit]);
200 }
201
202
203 /*
204  * lcp_lowerup - The lower layer is up.
205  */
206 void
207 lcp_lowerup(unit)
208     int unit;
209 {
210     sifdown(unit);
211     ppp_set_xaccm(unit, xmit_accm[unit]);
212     ppp_send_config(unit, MTU, 0xffffffff, 0, 0);
213     ppp_recv_config(unit, MTU, 0, 0, 0);
214     peer_mru[unit] = MTU;
215     lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
216
217     fsm_lowerup(&lcp_fsm[unit]);
218 }
219
220
221 /*
222  * lcp_lowerdown - The lower layer is down.
223  */
224 void
225 lcp_lowerdown(unit)
226     int unit;
227 {
228     fsm_lowerdown(&lcp_fsm[unit]);
229 }
230
231
232 /*
233  * lcp_input - Input LCP packet.
234  */
235 void
236 lcp_input(unit, p, len)
237     int unit;
238     u_char *p;
239     int len;
240 {
241     fsm_input(&lcp_fsm[unit], p, len);
242 }
243
244
245 /*
246  * lcp_extcode - Handle a LCP-specific code.
247  */
248 static int
249 lcp_extcode(f, code, id, inp, len)
250     fsm *f;
251     int code, id;
252     u_char *inp;
253     int len;
254 {
255     switch( code ){
256     case PROTREJ:
257         lcp_rprotrej(f, inp, len);
258         break;
259     
260     case ECHOREQ:
261         if( f->state != OPENED )
262             break;
263         LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
264         fsm_sdata(f, ECHOREP, id, inp, len);
265         break;
266     
267     case ECHOREP:
268     case DISCREQ:
269         break;
270
271     default:
272         return 0;
273     }
274     return 1;
275 }
276
277     
278 /*
279  * lcp_rprotrej - Receive an Protocol-Reject.
280  *
281  * Figure out which protocol is rejected and inform it.
282  */
283 static void
284 lcp_rprotrej(f, inp, len)
285     fsm *f;
286     u_char *inp;
287     int len;
288 {
289     u_short prot;
290
291     LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
292
293     if (len < sizeof (u_short)) {
294         LCPDEBUG((LOG_INFO,
295                   "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
296         return;
297     }
298
299     GETSHORT(prot, inp);
300
301     LCPDEBUG((LOG_INFO,
302               "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
303               prot));
304
305     /*
306      * Protocol-Reject packets received in any state other than the LCP
307      * OPENED state SHOULD be silently discarded.
308      */
309     if( f->state != OPENED ){
310         LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
311                   f->state));
312         return;
313     }
314
315     DEMUXPROTREJ(f->unit, prot);        /* Inform protocol */
316 }
317
318
319 /*
320  * lcp_protrej - A Protocol-Reject was received.
321  */
322 /*ARGSUSED*/
323 void
324 lcp_protrej(unit)
325     int unit;
326 {
327     /*
328      * Can't reject LCP!
329      */
330     LCPDEBUG((LOG_WARNING,
331               "lcp_protrej: Received Protocol-Reject for LCP!"));
332     fsm_protreject(&lcp_fsm[unit]);
333 }
334
335
336 /*
337  * lcp_sprotrej - Send a Protocol-Reject for some protocol.
338  */
339 void
340 lcp_sprotrej(unit, p, len)
341     int unit;
342     u_char *p;
343     int len;
344 {
345     /*
346      * Send back the protocol and the information field of the
347      * rejected packet.  We only get here if LCP is in the OPENED state.
348      */
349     p += 2;
350     len -= 2;
351
352     fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
353               p, len);
354 }
355
356
357 /*
358  * lcp_resetci - Reset our CI.
359  */
360 static void
361   lcp_resetci(f)
362 fsm *f;
363 {
364     lcp_wantoptions[f->unit].magicnumber = magic();
365     lcp_wantoptions[f->unit].numloops = 0;
366     lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
367     peer_mru[f->unit] = MTU;
368 }
369
370
371 /*
372  * lcp_cilen - Return length of our CI.
373  */
374 static int
375 lcp_cilen(f)
376     fsm *f;
377 {
378     lcp_options *go = &lcp_gotoptions[f->unit];
379
380 #define LENCIVOID(neg)  (neg ? CILEN_VOID : 0)
381 #define LENCICHAP(neg)  (neg ? CILEN_CHAP : 0)
382 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
383 #define LENCILONG(neg)  (neg ? CILEN_LONG : 0)
384 #define LENCILQR(neg)   (neg ? CILEN_LQR: 0)
385     /*
386      * NB: we only ask for one of CHAP and UPAP, even if we will
387      * accept either.
388      */
389     return (LENCISHORT(go->neg_mru) +
390             LENCILONG(go->neg_asyncmap) +
391             LENCICHAP(go->neg_chap) +
392             LENCISHORT(!go->neg_chap && go->neg_upap) +
393             LENCILQR(go->neg_lqr) +
394             LENCILONG(go->neg_magicnumber) +
395             LENCIVOID(go->neg_pcompression) +
396             LENCIVOID(go->neg_accompression));
397 }
398
399
400 /*
401  * lcp_addci - Add our desired CIs to a packet.
402  */
403 static void
404 lcp_addci(f, ucp, lenp)
405     fsm *f;
406     u_char *ucp;
407     int *lenp;
408 {
409     lcp_options *go = &lcp_gotoptions[f->unit];
410     u_char *start_ucp = ucp;
411
412 #define ADDCIVOID(opt, neg) \
413     if (neg) { \
414         PUTCHAR(opt, ucp); \
415         PUTCHAR(CILEN_VOID, ucp); \
416     }
417 #define ADDCISHORT(opt, neg, val) \
418     if (neg) { \
419         PUTCHAR(opt, ucp); \
420         PUTCHAR(CILEN_SHORT, ucp); \
421         PUTSHORT(val, ucp); \
422     }
423 #define ADDCICHAP(opt, neg, val, digest) \
424     if (neg) { \
425         PUTCHAR(opt, ucp); \
426         PUTCHAR(CILEN_CHAP, ucp); \
427         PUTSHORT(val, ucp); \
428         PUTCHAR(digest, ucp); \
429     }
430 #define ADDCILONG(opt, neg, val) \
431     if (neg) { \
432         PUTCHAR(opt, ucp); \
433         PUTCHAR(CILEN_LONG, ucp); \
434         PUTLONG(val, ucp); \
435     }
436 #define ADDCILQR(opt, neg, val) \
437     if (neg) { \
438         PUTCHAR(opt, ucp); \
439         PUTCHAR(CILEN_LQR, ucp); \
440         PUTSHORT(LQR, ucp); \
441         PUTLONG(val, ucp); \
442     }
443
444     ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
445     ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
446     ADDCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
447     ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
448     ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
449     ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
450     ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
451     ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
452
453     if (ucp - start_ucp != *lenp) {
454         /* this should never happen, because peer_mtu should be 1500 */
455         syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
456     }
457 }
458
459
460 /*
461  * lcp_ackci - Ack our CIs.
462  * This should not modify any state if the Ack is bad.
463  *
464  * Returns:
465  *      0 - Ack was bad.
466  *      1 - Ack was good.
467  */
468 static int
469 lcp_ackci(f, p, len)
470     fsm *f;
471     u_char *p;
472     int len;
473 {
474     lcp_options *go = &lcp_gotoptions[f->unit];
475     u_char cilen, citype, cichar;
476     u_short cishort;
477     u_long cilong;
478
479     /*
480      * CIs must be in exactly the same order that we sent.
481      * Check packet length and CI length at each step.
482      * If we find any deviations, then this packet is bad.
483      */
484 #define ACKCIVOID(opt, neg) \
485     if (neg) { \
486         if ((len -= CILEN_VOID) < 0) \
487             goto bad; \
488         GETCHAR(citype, p); \
489         GETCHAR(cilen, p); \
490         if (cilen != CILEN_VOID || \
491             citype != opt) \
492             goto bad; \
493     }
494 #define ACKCISHORT(opt, neg, val) \
495     if (neg) { \
496         if ((len -= CILEN_SHORT) < 0) \
497             goto bad; \
498         GETCHAR(citype, p); \
499         GETCHAR(cilen, p); \
500         if (cilen != CILEN_SHORT || \
501             citype != opt) \
502             goto bad; \
503         GETSHORT(cishort, p); \
504         if (cishort != val) \
505             goto bad; \
506     }
507 #define ACKCICHAP(opt, neg, val, digest) \
508     if (neg) { \
509         if ((len -= CILEN_CHAP) < 0) \
510             goto bad; \
511         GETCHAR(citype, p); \
512         GETCHAR(cilen, p); \
513         if (cilen != CILEN_CHAP || \
514             citype != opt) \
515             goto bad; \
516         GETSHORT(cishort, p); \
517         if (cishort != val) \
518             goto bad; \
519         GETCHAR(cichar, p); \
520         if (cichar != digest) \
521           goto bad; \
522     }
523 #define ACKCILONG(opt, neg, val) \
524     if (neg) { \
525         if ((len -= CILEN_LONG) < 0) \
526             goto bad; \
527         GETCHAR(citype, p); \
528         GETCHAR(cilen, p); \
529         if (cilen != CILEN_LONG || \
530             citype != opt) \
531             goto bad; \
532         GETLONG(cilong, p); \
533         if (cilong != val) \
534             goto bad; \
535     }
536 #define ACKCILQR(opt, neg, val) \
537     if (neg) { \
538         if ((len -= CILEN_LQR) < 0) \
539             goto bad; \
540         GETCHAR(citype, p); \
541         GETCHAR(cilen, p); \
542         if (cilen != CILEN_LQR || \
543             citype != opt) \
544             goto bad; \
545         GETSHORT(cishort, p); \
546         if (cishort != LQR) \
547             goto bad; \
548         GETLONG(cilong, p); \
549         if (cilong != val) \
550           goto bad; \
551     }
552
553     ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
554     ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
555     ACKCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
556     ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
557     ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
558     ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
559     ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
560     ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
561
562     /*
563      * If there are any remaining CIs, then this packet is bad.
564      */
565     if (len != 0)
566         goto bad;
567     return (1);
568 bad:
569     LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
570     return (0);
571 }
572
573
574 /*
575  * lcp_nakci - Peer has sent a NAK for some of our CIs.
576  * This should not modify any state if the Nak is bad
577  * or if LCP is in the OPENED state.
578  *
579  * Returns:
580  *      0 - Nak was bad.
581  *      1 - Nak was good.
582  */
583 static int
584 lcp_nakci(f, p, len)
585     fsm *f;
586     u_char *p;
587     int len;
588 {
589     lcp_options *go = &lcp_gotoptions[f->unit];
590     lcp_options *wo = &lcp_wantoptions[f->unit];
591     u_char cilen, citype, cichar, *next;
592     u_short cishort;
593     u_long cilong;
594     lcp_options no;             /* options we've seen Naks for */
595     lcp_options try;            /* options to request next time */
596     int looped_back = 0;
597
598     BZERO(&no, sizeof(no));
599     try = *go;
600
601     /*
602      * Any Nak'd CIs must be in exactly the same order that we sent.
603      * Check packet length and CI length at each step.
604      * If we find any deviations, then this packet is bad.
605      */
606 #define NAKCIVOID(opt, neg, code) \
607     if (go->neg && \
608         len >= CILEN_VOID && \
609         p[1] == CILEN_VOID && \
610         p[0] == opt) { \
611         len -= CILEN_VOID; \
612         INCPTR(CILEN_VOID, p); \
613         no.neg = 1; \
614         code \
615     }
616 #define NAKCICHAP(opt, neg, code) \
617     if (go->neg && \
618         len >= CILEN_CHAP && \
619         p[1] == CILEN_CHAP && \
620         p[0] == opt) { \
621         len -= CILEN_CHAP; \
622         INCPTR(2, p); \
623         GETSHORT(cishort, p); \
624         GETCHAR(cichar, p); \
625         no.neg = 1; \
626         code \
627     }
628 #define NAKCISHORT(opt, neg, code) \
629     if (go->neg && \
630         len >= CILEN_SHORT && \
631         p[1] == CILEN_SHORT && \
632         p[0] == opt) { \
633         len -= CILEN_SHORT; \
634         INCPTR(2, p); \
635         GETSHORT(cishort, p); \
636         no.neg = 1; \
637         code \
638     }
639 #define NAKCILONG(opt, neg, code) \
640     if (go->neg && \
641         len >= CILEN_LONG && \
642         p[1] == CILEN_LONG && \
643         p[0] == opt) { \
644         len -= CILEN_LONG; \
645         INCPTR(2, p); \
646         GETLONG(cilong, p); \
647         no.neg = 1; \
648         code \
649     }
650 #define NAKCILQR(opt, neg, code) \
651     if (go->neg && \
652         len >= CILEN_LQR && \
653         p[1] == CILEN_LQR && \
654         p[0] == opt) { \
655         len -= CILEN_LQR; \
656         INCPTR(2, p); \
657         GETSHORT(cishort, p); \
658         GETLONG(cilong, p); \
659         no.neg = 1; \
660         code \
661     }
662
663     /*
664      * We don't care if they want to send us smaller packets than
665      * we want.  Therefore, accept any MRU less than what we asked for,
666      * but then ignore the new value when setting the MRU in the kernel.
667      * If they send us a bigger MRU than what we asked, accept it, up to
668      * the limit of the default MRU we'd get if we didn't negotiate.
669      */
670     NAKCISHORT(CI_MRU, neg_mru,
671                if (cishort <= wo->mru || cishort < DEFMRU)
672                    try.mru = cishort;
673                );
674     /*
675      * Add any characters they want to our (receive-side) asyncmap.
676      */
677     NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
678               try.asyncmap = go->asyncmap | cilong;
679               );
680     /*
681      * If they can't cope with our CHAP hash algorithm, we'll have
682      * to stop asking for CHAP.  We haven't got any other algorithm.
683      */
684     NAKCICHAP(CI_AUTHTYPE, neg_chap,
685               try.neg_chap = 0;
686               );
687     /*
688      * Peer shouldn't send Nak for UPAP, protocol compression or
689      * address/control compression requests; they should send
690      * a Reject instead.  If they send a Nak, treat it as a Reject.
691      */
692     if (!go->neg_chap ){
693         NAKCISHORT(CI_AUTHTYPE, neg_upap,
694                    try.neg_upap = 0;
695                    );
696     }
697     /*
698      * If they can't cope with our link quality protocol, we'll have
699      * to stop asking for LQR.  We haven't got any other protocol.
700      * If they Nak the reporting period, take their value XXX ?
701      */
702     NAKCILONG(CI_QUALITY, neg_lqr,
703               if (cishort != LQR)
704                   try.neg_lqr = 0;
705               else
706                   try.lqr_period = cilong;
707               );
708     /*
709      * Check for a looped-back line.
710      */
711     NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
712               try.magicnumber = magic();
713               ++try.numloops;
714               looped_back = 1;
715               );
716
717     NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
718               try.neg_pcompression = 0;
719               );
720     NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
721               try.neg_accompression = 0;
722               );
723
724     /*
725      * There may be remaining CIs, if the peer is requesting negotiation
726      * on an option that we didn't include in our request packet.
727      * If we see an option that we requested, or one we've already seen
728      * in this packet, then this packet is bad.
729      * If we wanted to respond by starting to negotiate on the requested
730      * option(s), we could, but we don't, because except for the
731      * authentication type and quality protocol, if we are not negotiating
732      * an option, it is because we were told not to.
733      * For the authentication type, the Nak from the peer means
734      * `let me authenticate myself with you' which is a bit pointless.
735      * For the quality protocol, the Nak means `ask me to send you quality
736      * reports', but if we didn't ask for them, we don't want them.
737      */
738     while (len > CILEN_VOID) {
739         GETCHAR(citype, p);
740         GETCHAR(cilen, p);
741         if( (len -= cilen) < 0 )
742             goto bad;
743         next = p + cilen - 2;
744
745         switch (citype) {
746         case CI_MRU:
747             if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
748                 goto bad;
749             break;
750         case CI_ASYNCMAP:
751             if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
752                 goto bad;
753             break;
754         case CI_AUTHTYPE:
755             if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
756                 goto bad;
757             break;
758         case CI_MAGICNUMBER:
759             if (go->neg_magicnumber || no.neg_magicnumber ||
760                 cilen != CILEN_LONG)
761                 goto bad;
762             break;
763         case CI_PCOMPRESSION:
764             if (go->neg_pcompression || no.neg_pcompression
765                 || cilen != CILEN_VOID)
766                 goto bad;
767             break;
768         case CI_ACCOMPRESSION:
769             if (go->neg_accompression || no.neg_accompression
770                 || cilen != CILEN_VOID)
771                 goto bad;
772             break;
773         case CI_QUALITY:
774             if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
775                 goto bad;
776             break;
777         default:
778             goto bad;
779         }
780         p = next;
781     }
782
783     /* If there is still anything left, this packet is bad. */
784     if (len != 0)
785         goto bad;
786
787     /*
788      * OK, the Nak is good.  Now we can update state.
789      */
790     if (f->state != OPENED) {
791         *go = try;
792         if (looped_back && try.numloops % lcp_warnloops == 0)
793             LCPDEBUG((LOG_INFO, "The line appears to be looped back."));
794     }
795
796     return 1;
797
798 bad:
799     LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
800     return 0;
801 }
802
803
804 /*
805  * lcp_rejci - Peer has Rejected some of our CIs.
806  * This should not modify any state if the Reject is bad
807  * or if LCP is in the OPENED state.
808  *
809  * Returns:
810  *      0 - Reject was bad.
811  *      1 - Reject was good.
812  */
813 static int
814 lcp_rejci(f, p, len)
815     fsm *f;
816     u_char *p;
817     int len;
818 {
819     lcp_options *go = &lcp_gotoptions[f->unit];
820     u_char cichar;
821     u_short cishort;
822     u_long cilong;
823     u_char *start = p;
824     int plen = len;
825     lcp_options try;            /* options to request next time */
826
827     try = *go;
828
829     /*
830      * Any Rejected CIs must be in exactly the same order that we sent.
831      * Check packet length and CI length at each step.
832      * If we find any deviations, then this packet is bad.
833      */
834 #define REJCIVOID(opt, neg) \
835     if (go->neg && \
836         len >= CILEN_VOID && \
837         p[1] == CILEN_VOID && \
838         p[0] == opt) { \
839         len -= CILEN_VOID; \
840         INCPTR(CILEN_VOID, p); \
841         try.neg = 0; \
842         LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
843     }
844 #define REJCISHORT(opt, neg, val) \
845     if (go->neg && \
846         len >= CILEN_SHORT && \
847         p[1] == CILEN_SHORT && \
848         p[0] == opt) { \
849         len -= CILEN_SHORT; \
850         INCPTR(2, p); \
851         GETSHORT(cishort, p); \
852         /* Check rejected value. */ \
853         if (cishort != val) \
854             goto bad; \
855         try.neg = 0; \
856         LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
857     }
858 #define REJCICHAP(opt, neg, val, digest) \
859     if (go->neg && \
860         len >= CILEN_CHAP && \
861         p[1] == CILEN_CHAP && \
862         p[0] == opt) { \
863         len -= CILEN_CHAP; \
864         INCPTR(2, p); \
865         GETSHORT(cishort, p); \
866         GETCHAR(cichar, p); \
867         /* Check rejected value. */ \
868         if (cishort != val || cichar != digest) \
869             goto bad; \
870         try.neg = 0; \
871         LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
872     }
873 #define REJCILONG(opt, neg, val) \
874     if (go->neg && \
875         len >= CILEN_LONG && \
876         p[1] == CILEN_LONG && \
877         p[0] == opt) { \
878         len -= CILEN_LONG; \
879         INCPTR(2, p); \
880         GETLONG(cilong, p); \
881         /* Check rejected value. */ \
882         if (cilong != val) \
883             goto bad; \
884         try.neg = 0; \
885         LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
886     }
887 #define REJCILQR(opt, neg, val) \
888     if (go->neg && \
889         len >= CILEN_LQR && \
890         p[1] == CILEN_LQR && \
891         p[0] == opt) { \
892         len -= CILEN_LQR; \
893         INCPTR(2, p); \
894         GETSHORT(cishort, p); \
895         GETLONG(cilong, p); \
896         /* Check rejected value. */ \
897         if (cishort != LQR || cichar != val) \
898             goto bad; \
899         try.neg = 0; \
900         LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
901     }
902
903     REJCISHORT(CI_MRU, neg_mru, go->mru);
904     REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
905     REJCICHAP(CI_AUTHTYPE, neg_chap, CHAP, go->chap_mdtype);
906     if (!go->neg_chap) {
907         REJCISHORT(CI_AUTHTYPE, neg_upap, UPAP);
908     }
909     REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
910     REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
911     REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
912     REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
913
914     /*
915      * If there are any remaining CIs, then this packet is bad.
916      */
917     if (len != 0)
918         goto bad;
919     /*
920      * Now we can update state.
921      */
922     if (f->state != OPENED)
923         *go = try;
924     return 1;
925
926 bad:
927     LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
928     LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
929               plen, len, p - start));
930     return 0;
931 }
932
933
934 /*
935  * lcp_reqci - Check the peer's requested CIs and send appropriate response.
936  *
937  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
938  * appropriately.  If reject_if_disagree is non-zero, doesn't return
939  * CONFNAK; returns CONFREJ if it can't return CONFACK.
940  */
941 static int
942 lcp_reqci(f, inp, lenp, reject_if_disagree)
943     fsm *f;
944     u_char *inp;                /* Requested CIs */
945     int *lenp;                  /* Length of requested CIs */
946     int reject_if_disagree;
947 {
948     lcp_options *go = &lcp_gotoptions[f->unit];
949     lcp_options *ho = &lcp_hisoptions[f->unit];
950     lcp_options *ao = &lcp_allowoptions[f->unit];
951     u_char *cip, *next;         /* Pointer to current and next CIs */
952     u_char cilen, citype, cichar;/* Parsed len, type, char value */
953     u_short cishort;            /* Parsed short value */
954     u_long cilong;              /* Parse long value */
955     int rc = CONFACK;           /* Final packet return code */
956     int orc;                    /* Individual option return code */
957     u_char *p;                  /* Pointer to next char to parse */
958     u_char *ucp = inp;          /* Pointer to current output char */
959     int l = *lenp;              /* Length left */
960
961     /*
962      * Reset all his options.
963      */
964     BZERO(ho, sizeof(*ho));
965
966     /*
967      * Process all his options.
968      */
969     next = inp;
970     while (l) {
971         orc = CONFACK;                  /* Assume success */
972         cip = p = next;                 /* Remember begining of CI */
973         if (l < 2 ||                    /* Not enough data for CI header or */
974             p[1] < 2 ||                 /*  CI length too small or */
975             p[1] > l) {                 /*  CI length too big? */
976             LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
977             orc = CONFREJ;              /* Reject bad CI */
978             cilen = l;                  /* Reject till end of packet */
979             l = 0;                      /* Don't loop again */
980             goto endswitch;
981         }
982         GETCHAR(citype, p);             /* Parse CI type */
983         GETCHAR(cilen, p);              /* Parse CI length */
984         l -= cilen;                     /* Adjust remaining length */
985         next += cilen;                  /* Step to next CI */
986
987         switch (citype) {               /* Check CI type */
988         case CI_MRU:
989             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
990             if (!ao->neg_mru ||         /* Allow option? */
991                 cilen != CILEN_SHORT) { /* Check CI length */
992                 orc = CONFREJ;          /* Reject CI */
993                 break;
994             }
995             GETSHORT(cishort, p);       /* Parse MRU */
996             LCPDEBUG((LOG_INFO, "(%d)", cishort));
997
998             /*
999              * He must be able to receive at least our minimum.
1000              * No need to check a maximum.  If he sends a large number,
1001              * we'll just ignore it.
1002              */
1003             if (cishort < MINMRU) {
1004                 orc = CONFNAK;          /* Nak CI */
1005                 if( !reject_if_disagree ){
1006                     DECPTR(sizeof (short), p);  /* Backup */
1007                     PUTSHORT(MINMRU, p);        /* Give him a hint */
1008                 }
1009                 break;
1010             }
1011             ho->neg_mru = 1;            /* Remember he sent MRU */
1012             ho->mru = cishort;          /* And remember value */
1013             break;
1014
1015         case CI_ASYNCMAP:
1016             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1017             if (!ao->neg_asyncmap ||
1018                 cilen != CILEN_LONG) {
1019                 orc = CONFREJ;
1020                 break;
1021             }
1022             GETLONG(cilong, p);
1023             LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1024
1025             /*
1026              * Asyncmap must have set at least the bits
1027              * which are set in lcp_allowoptions[unit].asyncmap.
1028              */
1029             if ((ao->asyncmap & ~cilong) != 0) {
1030                 orc = CONFNAK;
1031                 if( !reject_if_disagree ){
1032                     DECPTR(sizeof (long), p);
1033                     PUTLONG(ao->asyncmap | cilong, p);
1034                 }
1035                 break;
1036             }
1037             ho->neg_asyncmap = 1;
1038             ho->asyncmap = cilong;
1039             break;
1040
1041         case CI_AUTHTYPE:
1042             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1043             if (cilen < CILEN_SHORT ||
1044                 !(ao->neg_upap || ao->neg_chap)) {
1045                 orc = CONFREJ;
1046                 break;
1047             }
1048             GETSHORT(cishort, p);
1049             LCPDEBUG((LOG_INFO, "(%x)", cishort));
1050
1051             /*
1052              * Authtype must be UPAP or CHAP.
1053              *
1054              * Note: if both ao->neg_upap and ao->neg_chap are set,
1055              * and the peer sends a Configure-Request with two
1056              * authenticate-protocol requests, one for CHAP and one
1057              * for UPAP, then we will reject the second request.
1058              * Whether we end up doing CHAP or UPAP depends then on
1059              * the ordering of the CIs in the peer's Configure-Request.
1060              */
1061
1062             if (cishort == UPAP) {
1063                 if (!ao->neg_upap ||    /* we don't want to do PAP */
1064                     ho->neg_chap ||     /* or we've already accepted CHAP */
1065                     cilen != CILEN_SHORT) {
1066                     LCPDEBUG((LOG_WARNING,
1067                               "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1068                     orc = CONFREJ;
1069                     break;
1070                 }
1071                 ho->neg_upap = 1;
1072                 break;
1073             }
1074             if (cishort == CHAP) {
1075                 if (!ao->neg_chap ||    /* we don't want to do CHAP */
1076                     ho->neg_upap ||     /* or we've already accepted UPAP */
1077                     cilen != CILEN_CHAP) {
1078                     LCPDEBUG((LOG_INFO,
1079                               "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1080                     orc = CONFREJ;
1081                     break;
1082                 }
1083                 GETCHAR(cichar, p);     /* get digest type*/
1084                 if (cichar != ao->chap_mdtype) {
1085                     orc = CONFNAK;
1086                     if( !reject_if_disagree ){
1087                         DECPTR(sizeof (u_char), p);
1088                         PUTCHAR(ao->chap_mdtype, p);
1089                     }
1090                     break;
1091                 }
1092                 ho->chap_mdtype = cichar; /* save md type */
1093                 ho->neg_chap = 1;
1094                 break;
1095             }
1096
1097             /*
1098              * We don't recognize the protocol they're asking for.
1099              * Reject it.
1100              */
1101             orc = CONFREJ;
1102             break;
1103
1104         case CI_QUALITY:
1105             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1106             if (!ao->neg_lqr ||
1107                 cilen != CILEN_LQR) {
1108                 orc = CONFREJ;
1109                 break;
1110             }
1111
1112             GETSHORT(cishort, p);
1113             GETLONG(cilong, p);
1114             LCPDEBUG((LOG_INFO, "(%x %lx)", cishort, cilong));
1115             if (cishort != LQR) {
1116                 orc = CONFREJ;
1117                 break;
1118             }
1119
1120             /*
1121              * Check the reporting period.
1122              * XXX When should we Nak this, and what with?
1123              */
1124             break;
1125
1126         case CI_MAGICNUMBER:
1127             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1128             if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1129                 cilen != CILEN_LONG) {
1130                 orc = CONFREJ;
1131                 break;
1132             }
1133             GETLONG(cilong, p);
1134             LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1135
1136             /*
1137              * He must have a different magic number.
1138              */
1139             if (go->neg_magicnumber &&
1140                 cilong == go->magicnumber) {
1141                 orc = CONFNAK;
1142                 DECPTR(sizeof (long), p);
1143                 cilong = magic();       /* Don't put magic() inside macro! */
1144                 PUTLONG(cilong, p);
1145                 break;
1146             }
1147             ho->neg_magicnumber = 1;
1148             ho->magicnumber = cilong;
1149             break;
1150
1151
1152         case CI_PCOMPRESSION:
1153             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1154             if (!ao->neg_pcompression ||
1155                 cilen != CILEN_VOID) {
1156                 orc = CONFREJ;
1157                 break;
1158             }
1159             ho->neg_pcompression = 1;
1160             break;
1161
1162         case CI_ACCOMPRESSION:
1163             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1164             if (!ao->neg_accompression ||
1165                 cilen != CILEN_VOID) {
1166                 orc = CONFREJ;
1167                 break;
1168             }
1169             ho->neg_accompression = 1;
1170             break;
1171
1172         default:
1173             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1174                       citype));
1175             orc = CONFREJ;
1176             break;
1177         }
1178
1179 endswitch:
1180         LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1181         if (orc == CONFACK &&           /* Good CI */
1182             rc != CONFACK)              /*  but prior CI wasnt? */
1183             continue;                   /* Don't send this one */
1184
1185         if (orc == CONFNAK) {           /* Nak this CI? */
1186             if (reject_if_disagree)     /* Getting fed up with sending NAKs? */
1187                 orc = CONFREJ;          /* Get tough if so */
1188             else {
1189                 if (rc == CONFREJ)      /* Rejecting prior CI? */
1190                     continue;           /* Don't send this one */
1191                 if (rc == CONFACK) {    /* Ack'd all prior CIs? */
1192                     rc = CONFNAK;       /* Not anymore... */
1193                     ucp = inp;          /* Backup */
1194                 }
1195             }
1196         }
1197         if (orc == CONFREJ &&           /* Reject this CI */
1198             rc != CONFREJ) {            /*  but no prior ones? */
1199             rc = CONFREJ;
1200             ucp = inp;                  /* Backup */
1201         }
1202         if (ucp != cip)                 /* Need to move CI? */
1203             BCOPY(cip, ucp, cilen);     /* Move it */
1204         INCPTR(cilen, ucp);             /* Update output pointer */
1205     }
1206
1207     /*
1208      * If we wanted to send additional NAKs (for unsent CIs), the
1209      * code would go here.  This must be done with care since it might
1210      * require a longer packet than we received.  At present there
1211      * are no cases where we want to ask the peer to negotiate an option.
1212      */
1213
1214     *lenp = ucp - inp;                  /* Compute output length */
1215     LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1216     return (rc);                        /* Return final code */
1217 }
1218
1219
1220 /*
1221  * lcp_up - LCP has come UP.
1222  *
1223  * Start UPAP, IPCP, etc.
1224  */
1225 static void
1226 lcp_up(f)
1227     fsm *f;
1228 {
1229     lcp_options *wo = &lcp_wantoptions[f->unit];
1230     lcp_options *ho = &lcp_hisoptions[f->unit];
1231     lcp_options *go = &lcp_gotoptions[f->unit];
1232     lcp_options *ao = &lcp_allowoptions[f->unit];
1233
1234     /*
1235      * Set our MTU to the smaller of the MTU we wanted and
1236      * the MRU our peer wanted.  If we negotiated an MRU,
1237      * set our MRU to the larger of value we wanted and
1238      * the value we got in the negotiation.
1239      */
1240     ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: MTU)),
1241                     (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1242                     ho->neg_pcompression, ho->neg_accompression);
1243     ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): MTU),
1244                     (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1245                     go->neg_pcompression, go->neg_accompression);
1246
1247     if (ho->neg_mru)
1248         peer_mru[f->unit] = ho->mru;
1249
1250     ChapLowerUp(f->unit);       /* Enable CHAP */
1251     upap_lowerup(f->unit);      /* Enable UPAP */
1252     ipcp_lowerup(f->unit);      /* Enable IPCP */
1253
1254     link_established(f->unit);
1255 }
1256
1257
1258 /*
1259  * lcp_down - LCP has gone DOWN.
1260  *
1261  * Alert other protocols.
1262  */
1263 static void
1264 lcp_down(f)
1265     fsm *f;
1266 {
1267     ipcp_lowerdown(f->unit);
1268     ChapLowerDown(f->unit);
1269     upap_lowerdown(f->unit);
1270
1271     sifdown(f->unit);
1272     ppp_send_config(f->unit, MTU, 0xffffffff, 0, 0);
1273     ppp_recv_config(f->unit, MTU, 0, 0, 0);
1274     peer_mru[f->unit] = MTU;
1275
1276     link_down(f->unit);
1277 }
1278
1279
1280 /*
1281  * lcp_starting - LCP needs the lower layer up.
1282  */
1283 static void
1284 lcp_starting(f)
1285     fsm *f;
1286 {
1287     link_required(f->unit);
1288 }
1289
1290
1291 /*
1292  * lcp_finished - LCP has finished with the lower layer.
1293  */
1294 static void
1295 lcp_finished(f)
1296     fsm *f;
1297 {
1298     link_terminated(f->unit);
1299 }
1300
1301
1302 /*
1303  * lcp_printpkt - print the contents of an LCP packet.
1304  */
1305 char *lcp_codenames[] = {
1306     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1307     "TermReq", "TermAck", "CodeRej", "ProtRej",
1308     "EchoReq", "EchoRep", "DiscReq"
1309 };
1310
1311 int
1312 lcp_printpkt(p, plen, printer, arg)
1313     u_char *p;
1314     int plen;
1315     void (*printer) __ARGS((void *, char *, ...));
1316     void *arg;
1317 {
1318     int code, id, len, olen;
1319     u_char *pstart, *optend;
1320     u_short cishort;
1321     u_long cilong;
1322
1323     if (plen < HEADERLEN)
1324         return 0;
1325     pstart = p;
1326     GETCHAR(code, p);
1327     GETCHAR(id, p);
1328     GETSHORT(len, p);
1329     if (len < HEADERLEN || len > plen)
1330         return 0;
1331
1332     if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1333         printer(arg, " %s", lcp_codenames[code-1]);
1334     else
1335         printer(arg, " code=0x%x", code);
1336     printer(arg, " id=0x%x", id);
1337     len -= HEADERLEN;
1338     switch (code) {
1339     case CONFREQ:
1340     case CONFACK:
1341     case CONFNAK:
1342     case CONFREJ:
1343         /* print option list */
1344         while (len >= 2) {
1345             GETCHAR(code, p);
1346             GETCHAR(olen, p);
1347             p -= 2;
1348             if (olen < 2 || olen > len) {
1349                 break;
1350             }
1351             printer(arg, " <");
1352             len -= olen;
1353             optend = p + olen;
1354             switch (code) {
1355             case CI_MRU:
1356                 if (olen == CILEN_SHORT) {
1357                     p += 2;
1358                     GETSHORT(cishort, p);
1359                     printer(arg, "mru %d", cishort);
1360                 }
1361                 break;
1362             case CI_ASYNCMAP:
1363                 if (olen == CILEN_LONG) {
1364                     p += 2;
1365                     GETLONG(cilong, p);
1366                     printer(arg, "asyncmap 0x%x", cilong);
1367                 }
1368                 break;
1369             case CI_AUTHTYPE:
1370                 if (olen >= CILEN_SHORT) {
1371                     p += 2;
1372                     printer(arg, "auth ");
1373                     GETSHORT(cishort, p);
1374                     switch (cishort) {
1375                     case UPAP:
1376                         printer(arg, "upap");
1377                         break;
1378                     case CHAP:
1379                         printer(arg, "chap");
1380                         break;
1381                     default:
1382                         printer(arg, "0x%x", cishort);
1383                     }
1384                 }
1385                 break;
1386             case CI_QUALITY:
1387                 if (olen >= CILEN_SHORT) {
1388                     p += 2;
1389                     printer(arg, "quality ");
1390                     GETSHORT(cishort, p);
1391                     switch (cishort) {
1392                     case LQR:
1393                         printer(arg, "lqr");
1394                         break;
1395                     default:
1396                         printer(arg, "0x%x", cishort);
1397                     }
1398                 }
1399                 break;
1400             case CI_MAGICNUMBER:
1401                 if (olen == CILEN_LONG) {
1402                     p += 2;
1403                     GETLONG(cilong, p);
1404                     printer(arg, "magic 0x%x", cilong);
1405                 }
1406                 break;
1407             case CI_PCOMPRESSION:
1408                 if (olen == CILEN_VOID) {
1409                     p += 2;
1410                     printer(arg, "pcomp");
1411                 }
1412                 break;
1413             case CI_ACCOMPRESSION:
1414                 if (olen == CILEN_VOID) {
1415                     p += 2;
1416                     printer(arg, "accomp");
1417                 }
1418                 break;
1419             }
1420             while (p < optend) {
1421                 GETCHAR(code, p);
1422                 printer(arg, " %.2x", code);
1423             }
1424             printer(arg, ">");
1425         }
1426         break;
1427     }
1428
1429     /* print the rest of the bytes in the packet */
1430     for (; len > 0; --len) {
1431         GETCHAR(code, p);
1432         printer(arg, " %.2x", code);
1433     }
1434
1435     return p - pstart;
1436 }