http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / userapps / opensource / ppp / pppoe / 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 #define RCSID   "$Id: lcp.c,v 1.5 2006/11/24 03:33:52 andylin Exp $"
21
22 /*
23  * TODO:
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "pppd.h"
31 #include "fsm.h"
32 #include "lcp.h"
33 #include "chap.h"
34 #include "magic.h"
35
36 // brcm
37 #include <syscall.h>
38 #include <syslog.h>
39
40 static const char rcsid[] = RCSID;
41
42 /*wilson add for MS-CHAPv2, 05/26/2005*/
43 extern bool refuse_chap;
44 /*wilson add end*/
45
46 /*
47  * When the link comes up we want to be able to wait for a short while,
48  * or until seeing some input from the peer, before starting to send
49  * configure-requests.  We do this by delaying the fsm_lowerup call.
50  */
51 /* steal a bit in fsm flags word */
52 #define DELAYED_UP      0x100
53
54 static void lcp_delayed_up __P((void *));
55
56 /*
57  * LCP-related command-line options.
58  */
59 //Charles 09/01/2003, original=5, 5, 3
60 static int idle_for_echo=10;
61 //int   lcp_echo_interval = 10;         /* Interval between LCP echo-requests */
62 int     lcp_echo_interval = 20;
63 int     lcp_echo_fails = 6;     /* Tolerance to unanswered echo-requests */
64 //int   lcp_echo_interval = 0;  /* Interval between LCP echo-requests */
65 //int   lcp_echo_fails = 0;     /* Tolerance to unanswered echo-requests */
66 bool    lax_recv = 0;           /* accept control chars in asyncmap */
67 bool    noendpoint = 0;         /* don't send/accept endpoint discriminator */
68 bool    auth_failed = 0;
69
70 #if defined(INCLUDE_MTU_LAN_PPP)
71 extern glb_CONFIG_PPP_MTU;
72 #endif
73
74 extern int ppp_session; //andy add
75
76 static int noopt __P((char **));
77
78 #ifdef HAVE_MULTILINK
79 static int setendpoint __P((char **));
80 static void printendpoint __P((option_t *, void (*)(void *, char *, ...),
81                                void *));
82 #endif /* HAVE_MULTILINK */
83
84 static option_t lcp_option_list[] = {
85     /* LCP options */
86     { "-all", o_special_noarg, (void *)noopt,
87       "Don't request/allow any LCP options" },
88
89     { "noaccomp", o_bool, &lcp_wantoptions[0].neg_accompression,
90       "Disable address/control compression",
91       OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
92     { "-ac", o_bool, &lcp_wantoptions[0].neg_accompression,
93       "Disable address/control compression",
94       OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
95
96     { "asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
97       "Set asyncmap (for received packets)",
98       OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
99     { "-as", o_uint32, &lcp_wantoptions[0].asyncmap,
100       "Set asyncmap (for received packets)",
101       OPT_ALIAS | OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
102     { "default-asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
103       "Disable asyncmap negotiation",
104       OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
105       &lcp_allowoptions[0].neg_asyncmap },
106     { "-am", o_uint32, &lcp_wantoptions[0].asyncmap,
107       "Disable asyncmap negotiation",
108       OPT_ALIAS | OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
109       &lcp_allowoptions[0].neg_asyncmap },
110
111     { "nomagic", o_bool, &lcp_wantoptions[0].neg_magicnumber,
112       "Disable magic number negotiation (looped-back line detection)",
113       OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
114     { "-mn", o_bool, &lcp_wantoptions[0].neg_magicnumber,
115       "Disable magic number negotiation (looped-back line detection)",
116       OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
117
118     { "mru", o_int, &lcp_wantoptions[0].mru,
119       "Set MRU (maximum received packet size) for negotiation",
120       OPT_PRIO, &lcp_wantoptions[0].neg_mru },
121     { "default-mru", o_bool, &lcp_wantoptions[0].neg_mru,
122       "Disable MRU negotiation (use default 1500)",
123       OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
124     { "-mru", o_bool, &lcp_wantoptions[0].neg_mru,
125       "Disable MRU negotiation (use default 1500)",
126       OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
127
128     { "mtu", o_int, &lcp_allowoptions[0].mru,
129       "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
130
131     { "nopcomp", o_bool, &lcp_wantoptions[0].neg_pcompression,
132       "Disable protocol field compression",
133       OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
134     { "-pc", o_bool, &lcp_wantoptions[0].neg_pcompression,
135       "Disable protocol field compression",
136       OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
137
138     { "passive", o_bool, &lcp_wantoptions[0].passive,
139       "Set passive mode", 1 },
140     { "-p", o_bool, &lcp_wantoptions[0].passive,
141       "Set passive mode", OPT_ALIAS | 1 },
142
143     { "silent", o_bool, &lcp_wantoptions[0].silent,
144       "Set silent mode", 1 },
145
146     { "lcp-echo-failure", o_int, &lcp_echo_fails,
147       "Set number of consecutive echo failures to indicate link failure",
148       OPT_PRIO },
149     { "lcp-echo-interval", o_int, &lcp_echo_interval,
150       "Set time in seconds between LCP echo requests", OPT_PRIO },
151     { "lcp-restart", o_int, &lcp_fsm[0].timeouttime,
152       "Set time in seconds between LCP retransmissions", OPT_PRIO },
153     { "lcp-max-terminate", o_int, &lcp_fsm[0].maxtermtransmits,
154       "Set maximum number of LCP terminate-request transmissions", OPT_PRIO },
155     { "lcp-max-configure", o_int, &lcp_fsm[0].maxconfreqtransmits,
156       "Set maximum number of LCP configure-request transmissions", OPT_PRIO },
157     { "lcp-max-failure", o_int, &lcp_fsm[0].maxnakloops,
158       "Set limit on number of LCP configure-naks", OPT_PRIO },
159
160     { "receive-all", o_bool, &lax_recv,
161       "Accept all received control characters", 1 },
162
163 #ifdef HAVE_MULTILINK
164     { "mrru", o_int, &lcp_wantoptions[0].mrru,
165       "Maximum received packet size for multilink bundle",
166       OPT_PRIO, &lcp_wantoptions[0].neg_mrru },
167
168     { "mpshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
169       "Use short sequence numbers in multilink headers",
170       OPT_PRIO | 1, &lcp_allowoptions[0].neg_ssnhf },
171     { "nompshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
172       "Don't use short sequence numbers in multilink headers",
173       OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_ssnhf },
174
175     { "endpoint", o_special, (void *) setendpoint,
176       "Endpoint discriminator for multilink",
177       OPT_PRIO | OPT_A2PRINTER, (void *) printendpoint },
178 #endif /* HAVE_MULTILINK */
179
180     { "noendpoint", o_bool, &noendpoint,
181       "Don't send or accept multilink endpoint discriminator", 1 },
182
183     {NULL}
184 };
185
186 /* global vars */
187 fsm lcp_fsm[NUM_PPP];                   /* LCP fsm structure (global)*/
188 lcp_options lcp_wantoptions[NUM_PPP];   /* Options that we want to request */
189 lcp_options lcp_gotoptions[NUM_PPP];    /* Options that peer ack'd */
190 lcp_options lcp_allowoptions[NUM_PPP];  /* Options we allow peer to request */
191 lcp_options lcp_hisoptions[NUM_PPP];    /* Options that we ack'd */
192
193 static int lcp_echos_pending = 0;       /* Number of outstanding echo msgs */
194 static int lcp_echo_number   = 0;       /* ID number of next echo frame */
195 static int lcp_echo_timer_running = 0;  /* set if a timer is running */
196
197 static u_char nak_buffer[PPP_MRU];      /* where we construct a nak packet */
198
199 /*
200  * Callbacks for fsm code.  (CI = Configuration Information)
201  */
202 static void lcp_resetci __P((fsm *));   /* Reset our CI */
203 static int  lcp_cilen __P((fsm *));             /* Return length of our CI */
204 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
205 static int  lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
206 static int  lcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
207 static int  lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
208 static int  lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
209 static void lcp_up __P((fsm *));                /* We're UP */
210 static void lcp_down __P((fsm *));              /* We're DOWN */
211 static void lcp_starting __P((fsm *));  /* We need lower layer up */
212 static void lcp_finished __P((fsm *));  /* We need lower layer down */
213 static int  lcp_extcode __P((fsm *, int, int, u_char *, int));
214 static void lcp_rprotrej __P((fsm *, u_char *, int));
215
216 /*
217  * routines to send LCP echos to peer
218  */
219
220 static void lcp_echo_lowerup __P((int));
221 static void lcp_echo_lowerdown __P((int));
222 static void LcpEchoTimeout __P((void *));
223 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
224 static void LcpSendEchoRequest __P((fsm *));
225 static void LcpLinkFailure __P((fsm *));
226 static void LcpEchoCheck __P((fsm *));
227
228 static fsm_callbacks lcp_callbacks = {  /* LCP callback routines */
229     lcp_resetci,                /* Reset our Configuration Information */
230     lcp_cilen,                  /* Length of our Configuration Information */
231     lcp_addci,                  /* Add our Configuration Information */
232     lcp_ackci,                  /* ACK our Configuration Information */
233     lcp_nakci,                  /* NAK our Configuration Information */
234     lcp_rejci,                  /* Reject our Configuration Information */
235     lcp_reqci,                  /* Request peer's Configuration Information */
236     lcp_up,                     /* Called when fsm reaches OPENED state */
237     lcp_down,                   /* Called when fsm leaves OPENED state */
238     lcp_starting,               /* Called when we want the lower layer up */
239     lcp_finished,               /* Called when we want the lower layer down */
240     NULL,                       /* Called when Protocol-Reject received */
241     NULL,                       /* Retransmission is necessary */
242     lcp_extcode,                /* Called to handle LCP-specific codes */
243     "LCP"                       /* String name of protocol */
244 };
245
246 /*
247  * Protocol entry points.
248  * Some of these are called directly.
249  */
250
251 static void lcp_init __P((int));
252 static void lcp_input __P((int, u_char *, int));
253 static void lcp_protrej __P((int));
254 static int  lcp_printpkt __P((u_char *, int,
255                               void (*) __P((void *, char *, ...)), void *));
256
257 struct protent lcp_protent = {
258     PPP_LCP,
259     lcp_init,
260     lcp_input,
261     lcp_protrej,
262     lcp_lowerup,
263     lcp_lowerdown,
264     lcp_open,
265     lcp_close,
266     lcp_printpkt,
267     NULL,
268     1,
269     "LCP",
270     NULL,
271     lcp_option_list,
272     NULL,
273     NULL,
274     NULL
275 };
276
277 int lcp_loopbackfail = DEFLOOPBACKFAIL;
278
279 /*
280  * Length of each type of configuration option (in octets)
281  */
282 #define CILEN_VOID      2
283 #define CILEN_CHAR      3
284 #define CILEN_SHORT     4       /* CILEN_VOID + 2 */
285 #define CILEN_CHAP      5       /* CILEN_VOID + 2 + 1 */
286 #define CILEN_LONG      6       /* CILEN_VOID + 4 */
287 #define CILEN_LQR       8       /* CILEN_VOID + 2 + 4 */
288 #define CILEN_CBCP      3
289
290 #define CODENAME(x)     ((x) == CONFACK ? "ACK" : \
291                          (x) == CONFNAK ? "NAK" : "REJ")
292
293 /*
294  * noopt - Disable all options (why?).
295  */
296 static int
297 noopt(argv)
298     char **argv;
299 {
300     BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
301     BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
302
303     return (1);
304 }
305
306 #ifdef HAVE_MULTILINK
307 static int
308 setendpoint(argv)
309     char **argv;
310 {
311     if (str_to_epdisc(&lcp_wantoptions[0].endpoint, *argv)) {
312         lcp_wantoptions[0].neg_endpoint = 1;
313         return 1;
314     }
315     option_error("Can't parse '%s' as an endpoint discriminator", *argv);
316     return 0;
317 }
318
319 static void
320 printendpoint(opt, printer, arg)
321     option_t *opt;
322     void (*printer) __P((void *, char *, ...));
323     void *arg;
324 {
325         printer(arg, "%s", epdisc_to_str(&lcp_wantoptions[0].endpoint));
326 }
327 #endif /* HAVE_MULTILINK */
328
329 /*
330  * lcp_init - Initialize LCP.
331  */
332 static void
333 lcp_init(unit)
334     int unit;
335 {
336     fsm *f = &lcp_fsm[unit];
337     lcp_options *wo = &lcp_wantoptions[unit];
338     lcp_options *ao = &lcp_allowoptions[unit];
339
340     f->unit = unit;
341     f->protocol = PPP_LCP;
342     f->callbacks = &lcp_callbacks;
343
344     fsm_init(f);
345
346     BZERO(wo, sizeof(*wo));
347     wo->neg_mru = 1;
348     wo->mru = DEFMRU;
349     wo->neg_asyncmap = 1;
350     /*wilson modify for MS-CHAPv2, 05/26/2005*/
351     //wo->chap_mdtype = CHAP_DIGEST_MD5;
352     wo->use_digest = 1;
353 #ifdef CHAPMS
354     if(wo->use_chapms_v2)
355         wo->chap_mdtype = CHAP_MICROSOFT_V2;
356     else if(wo->use_chapms)
357         wo->chap_mdtype = CHAP_MICROSOFT;
358     else
359 #endif
360     if(wo->use_digest)
361     wo->chap_mdtype = CHAP_DIGEST_MD5;
362     else
363         refuse_chap = 1;
364     /*wilson modify end*/
365     wo->neg_magicnumber = 1;
366     wo->neg_pcompression = 1;
367     wo->neg_accompression = 1;
368
369     BZERO(ao, sizeof(*ao));
370     ao->neg_mru = 1;
371     ao->mru = MAXMRU;
372     ao->neg_asyncmap = 1;
373     ao->neg_chap = 1;
374     /*wilson modify for MS-CHAPv2, 05/26/2005*/
375     //ao->chap_mdtype = CHAP_DIGEST_MD5;
376     ao->use_digest = 1;
377 #ifdef CHAPMS
378     ao->use_chapms_v2 = ao->use_chapms = 1;
379     if(ao->use_chapms_v2)
380         ao->chap_mdtype = CHAP_MICROSOFT_V2;
381     else if(ao->use_chapms)
382         ao->chap_mdtype = CHAP_MICROSOFT;
383     else
384 #else
385     if(ao->use_digest)
386     ao->chap_mdtype = CHAP_DIGEST_MD5;
387     else
388         refuse_chap = 1;
389 #endif
390     /*wilson modify end*/
391     ao->neg_upap = 1;
392     ao->neg_magicnumber = 1;
393     ao->neg_pcompression = 1;
394     ao->neg_accompression = 1;
395 #ifdef CBCP_SUPPORT
396     ao->neg_cbcp = 1;
397 #endif
398     ao->neg_endpoint = 1;
399
400 }
401
402
403 /*
404  * lcp_open - LCP is allowed to come up.
405  */
406 void
407 lcp_open(unit)
408     int unit;
409 {
410     fsm *f = &lcp_fsm[unit];
411     lcp_options *wo = &lcp_wantoptions[unit];
412     lcp_options *ao = &lcp_allowoptions[unit];
413
414     // brcm
415     wo->neg_mschap = 0;
416     wo->neg_chap = 0;
417     wo->neg_upap = 0;
418     ao->neg_mschap = 0;
419     ao->neg_chap = 0;
420     ao->neg_upap = 0;
421
422     // brcm
423     ao->chap_mdtype = CHAP_DIGEST_MD5;
424     if (opflag==3) {
425         ao->neg_chap = 1;
426         refuse_pap = 1;
427         ao->chap_mdtype = CHAP_MICROSOFT;
428     }
429     else if (opflag==2) {
430         ao->neg_chap = 1;
431         refuse_pap = 1;
432         ao->chap_mdtype = CHAP_DIGEST_MD5;
433     }
434     else if (opflag==1) {
435         ao->neg_upap = 1;
436         refuse_chap = 1;
437     }
438     else if (opflag==0) {
439         ao->neg_chap = 1;
440         ao->neg_upap = 1;
441     }
442     else {
443         ao->neg_chap = 1;
444         ao->neg_upap = 1;
445     }
446
447     f->flags &= ~(OPT_PASSIVE | OPT_SILENT);
448     if (wo->passive)
449         f->flags |= OPT_PASSIVE;
450     if (wo->silent)
451         f->flags |= OPT_SILENT;
452     fsm_open(f);
453 }
454
455
456 /*
457  * lcp_close - Take LCP down.
458  */
459 void
460 lcp_close(unit, reason)
461     int unit;
462     char *reason;
463 {
464     fsm *f = &lcp_fsm[unit];
465
466     if (phase != PHASE_DEAD)
467         new_phase(PHASE_TERMINATE);
468     if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
469         /*
470          * This action is not strictly according to the FSM in RFC1548,
471          * but it does mean that the program terminates if you do a
472          * lcp_close() in passive/silent mode when a connection hasn't
473          * been established.
474          */
475         f->state = CLOSED;
476         lcp_finished(f);
477
478     } else
479         fsm_close(&lcp_fsm[unit], reason);
480     if (autoscan)
481         exit(0);
482 }
483
484
485 /*
486  * lcp_lowerup - The lower layer is up.
487  */
488 void
489 lcp_lowerup(unit)
490     int unit;
491 {
492     lcp_options *wo = &lcp_wantoptions[unit];
493     fsm *f = &lcp_fsm[unit];
494
495     /*
496      * Don't use A/C or protocol compression on transmission,
497      * but accept A/C and protocol compressed packets
498      * if we are going to ask for A/C and protocol compression.
499      */
500     ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
501     ppp_recv_config(unit, PPP_MRU, (lax_recv? 0: 0xffffffff), wo->neg_pcompression, wo->neg_accompression);
502     peer_mru[unit] = PPP_MRU;
503
504     if (listen_time != 0) {
505         f->flags |= DELAYED_UP;
506         timeout(lcp_delayed_up, f, 0, listen_time * 1000);
507     } else
508         fsm_lowerup(f);
509 }
510
511
512 /*
513  * lcp_lowerdown - The lower layer is down.
514  */
515 void
516 lcp_lowerdown(unit)
517     int unit;
518 {
519     fsm *f = &lcp_fsm[unit];
520
521     if (f->flags & DELAYED_UP)
522         f->flags &= ~DELAYED_UP;
523     else
524         fsm_lowerdown(&lcp_fsm[unit]);
525 }
526
527
528 /*
529  * lcp_delayed_up - Bring the lower layer up now.
530  */
531 static void
532 lcp_delayed_up(arg)
533     void *arg;
534 {
535     fsm *f = arg;
536
537     if (f->flags & DELAYED_UP) {
538         f->flags &= ~DELAYED_UP;
539         fsm_lowerup(f);
540     }
541 }
542
543
544 /*
545  * lcp_input - Input LCP packet.
546  */
547 static void
548 lcp_input(unit, p, len)
549     int unit;
550     u_char *p;
551     int len;
552 {
553     fsm *f = &lcp_fsm[unit];
554
555     if (f->flags & DELAYED_UP) {
556         f->flags &= ~DELAYED_UP;
557         fsm_lowerup(f);
558     }
559     fsm_input(f, p, len);
560 }
561
562
563 /*
564  * lcp_extcode - Handle a LCP-specific code.
565  */
566 static int
567 lcp_extcode(f, code, id, inp, len)
568     fsm *f;
569     int code, id;
570     u_char *inp;
571     int len;
572 {
573     u_char *magp;
574
575     switch( code ){
576     case PROTREJ:
577         lcp_rprotrej(f, inp, len);
578         break;
579     
580     case ECHOREQ:
581         if (f->state != OPENED)
582             break;
583         magp = inp;
584         PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
585         fsm_sdata(f, ECHOREP, id, inp, len);
586         break;
587     
588     case ECHOREP:
589         lcp_received_echo_reply(f, id, inp, len);
590         break;
591
592     case DISCREQ:
593         break;
594
595     default:
596         return 0;
597     }
598     return 1;
599 }
600
601     
602 /*
603  * lcp_rprotrej - Receive an Protocol-Reject.
604  *
605  * Figure out which protocol is rejected and inform it.
606  */
607 static void
608 lcp_rprotrej(f, inp, len)
609     fsm *f;
610     u_char *inp;
611     int len;
612 {
613     int i;
614     struct protent *protp;
615     u_short prot;
616
617     if (len < 2) {
618         LCPDEBUG(("lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
619         return;
620     }
621
622     GETSHORT(prot, inp);
623
624     /*
625      * Protocol-Reject packets received in any state other than the LCP
626      * OPENED state SHOULD be silently discarded.
627      */
628     if( f->state != OPENED ){
629         LCPDEBUG(("Protocol-Reject discarded: LCP in state %d", f->state));
630         return;
631     }
632
633     /*
634      * Upcall the proper Protocol-Reject routine.
635      */
636     for (i = 0; (protp = protocols[i]) != NULL; ++i)
637         if (protp->protocol == prot && protp->enabled_flag) {
638             (*protp->protrej)(f->unit);
639             return;
640         }
641
642     warn("Protocol-Reject for unsupported protocol 0x%x", prot);
643 }
644
645
646 /*
647  * lcp_protrej - A Protocol-Reject was received.
648  */
649 /*ARGSUSED*/
650 static void
651 lcp_protrej(unit)
652     int unit;
653 {
654     /*
655      * Can't reject LCP!
656      */
657     error("Received Protocol-Reject for LCP!");
658     fsm_protreject(&lcp_fsm[unit]);
659 }
660
661
662 /*
663  * lcp_sprotrej - Send a Protocol-Reject for some protocol.
664  */
665 void
666 lcp_sprotrej(unit, p, len)
667     int unit;
668     u_char *p;
669     int len;
670 {
671     /*
672      * Send back the protocol and the information field of the
673      * rejected packet.  We only get here if LCP is in the OPENED state.
674      */
675     p += 2;
676     len -= 2;
677
678     fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id, p, len);
679 }
680
681
682 /*
683  * lcp_resetci - Reset our CI.
684  */
685 static void
686 lcp_resetci(f)
687     fsm *f;
688 {
689     lcp_options *wo = &lcp_wantoptions[f->unit];
690     lcp_options *go = &lcp_gotoptions[f->unit];
691     lcp_options *ao = &lcp_allowoptions[f->unit];
692
693     wo->magicnumber = magic();
694     wo->numloops = 0;
695
696     if (ppp_session == PPPOA)//Andy modify
697         wo->mru = 1500;
698     else
699         wo->mru = 1492;
700
701     *go = *wo;
702     if (!multilink) {
703         go->neg_mrru = 0;
704         go->neg_ssnhf = 0;
705         go->neg_endpoint = 0;
706     }
707     if (noendpoint)
708         ao->neg_endpoint = 0;
709     peer_mru[f->unit] = PPP_MRU;
710
711     //auth_reset(f->unit);
712 }
713
714
715 /*
716  * lcp_cilen - Return length of our CI.
717  */
718 static int
719 lcp_cilen(f)
720     fsm *f;
721 {
722     lcp_options *go = &lcp_gotoptions[f->unit];
723
724 #define LENCIVOID(neg)  ((neg) ? CILEN_VOID : 0)
725 #define LENCICHAP(neg)  ((neg) ? CILEN_CHAP : 0)
726 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
727 #define LENCILONG(neg)  ((neg) ? CILEN_LONG : 0)
728 #define LENCILQR(neg)   ((neg) ? CILEN_LQR: 0)
729 #define LENCICBCP(neg)  ((neg) ? CILEN_CBCP: 0)
730     /*
731      * NB: we only ask for one of CHAP and UPAP, even if we will
732      * accept either.
733      */
734     /*wilson add for MS-CHAPv2, 05/26/2005*/
735 #ifdef CHAPMS
736     if(go->use_chapms_v2)
737         go->chap_mdtype = CHAP_MICROSOFT_V2;
738     else if(go->use_chapms)
739         go->chap_mdtype = CHAP_MICROSOFT;
740     else
741 #endif
742     if(go->use_digest)
743         go->chap_mdtype = CHAP_DIGEST_MD5;
744     else
745         go->neg_chap = 0;
746     /*wilson add end*/
747
748     return (LENCISHORT(go->neg_mru /*&& go->mru != DEFMRU //andy modify it*/) +
749             LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
750             LENCICHAP(go->neg_chap) +
751             LENCISHORT(!go->neg_chap && go->neg_upap) +
752             LENCILQR(go->neg_lqr) +
753             LENCICBCP(go->neg_cbcp) +
754             LENCILONG(go->neg_magicnumber) +
755             LENCIVOID(go->neg_pcompression) +
756             LENCIVOID(go->neg_accompression) +
757             LENCISHORT(go->neg_mrru) +
758             LENCIVOID(go->neg_ssnhf) +
759             (go->neg_endpoint? CILEN_CHAR + go->endpoint.length: 0));
760 }
761
762
763 /*
764  * lcp_addci - Add our desired CIs to a packet.
765  */
766 static void
767 lcp_addci(f, ucp, lenp)
768     fsm *f;
769     u_char *ucp;
770     int *lenp;
771 {
772     lcp_options *go = &lcp_gotoptions[f->unit];
773     u_char *start_ucp = ucp;
774 #define ADDCIVOID(opt, neg) \
775     if (neg) { \
776         PUTCHAR(opt, ucp); \
777         PUTCHAR(CILEN_VOID, ucp); \
778     }
779 #define ADDCISHORT(opt, neg, val) \
780     if (neg) { \
781         PUTCHAR(opt, ucp); \
782         PUTCHAR(CILEN_SHORT, ucp); \
783         PUTSHORT(val, ucp); \
784     }
785 #define ADDCICHAP(opt, neg, val, digest) \
786     if (neg) { \
787         PUTCHAR(opt, ucp); \
788         PUTCHAR(CILEN_CHAP, ucp); \
789         PUTSHORT(val, ucp); \
790         PUTCHAR(digest, ucp); \
791     }
792 #define ADDCILONG(opt, neg, val) \
793     if (neg) { \
794         PUTCHAR(opt, ucp); \
795         PUTCHAR(CILEN_LONG, ucp); \
796         PUTLONG(val, ucp); \
797     }
798 #define ADDCILQR(opt, neg, val) \
799     if (neg) { \
800         PUTCHAR(opt, ucp); \
801         PUTCHAR(CILEN_LQR, ucp); \
802         PUTSHORT(PPP_LQR, ucp); \
803         PUTLONG(val, ucp); \
804     }
805 #define ADDCICHAR(opt, neg, val) \
806     if (neg) { \
807         PUTCHAR(opt, ucp); \
808         PUTCHAR(CILEN_CHAR, ucp); \
809         PUTCHAR(val, ucp); \
810     }
811 #define ADDCIENDP(opt, neg, class, val, len) \
812     if (neg) { \
813         int i; \
814         PUTCHAR(opt, ucp); \
815         PUTCHAR(CILEN_CHAR + len, ucp); \
816         PUTCHAR(class, ucp); \
817         for (i = 0; i < len; ++i) \
818             PUTCHAR(val[i], ucp); \
819     }
820     //go->mru = lcp_allowoptions[0].mru;
821
822     //ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
823     ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
824     ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
825               go->asyncmap);
826     ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
827     ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
828     ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
829     ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
830     ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
831     ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
832     ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
833     ADDCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
834     ADDCIVOID(CI_SSNHF, go->neg_ssnhf);
835     ADDCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
836               go->endpoint.value, go->endpoint.length);
837
838     if (ucp - start_ucp != *lenp) {
839         /* this should never happen, because peer_mtu should be 1500 */
840                 printf("Bug in lcp_addci: wrong length\n");
841         error("Bug in lcp_addci: wrong length");
842     }
843 }
844
845
846 /*
847  * lcp_ackci - Ack our CIs.
848  * This should not modify any state if the Ack is bad.
849  *
850  * Returns:
851  *      0 - Ack was bad.
852  *      1 - Ack was good.
853  */
854 static int
855 lcp_ackci(f, p, len)
856     fsm *f;
857     u_char *p;
858     int len;
859 {
860     lcp_options *go = &lcp_gotoptions[f->unit];
861     u_char cilen, citype, cichar;
862     u_short cishort;
863     u_int32_t cilong;
864
865     /*
866      * CIs must be in exactly the same order that we sent.
867      * Check packet length and CI length at each step.
868      * If we find any deviations, then this packet is bad.
869      */
870 #define ACKCIVOID(opt, neg) \
871     if (neg) { \
872         if ((len -= CILEN_VOID) < 0) \
873             goto bad; \
874         GETCHAR(citype, p); \
875         GETCHAR(cilen, p); \
876         if (cilen != CILEN_VOID || \
877             citype != opt) \
878             goto bad; \
879     }
880 #define ACKCISHORT(opt, neg, val) \
881     if (neg) { \
882         if ((len -= CILEN_SHORT) < 0) \
883             goto bad; \
884         GETCHAR(citype, p); \
885         GETCHAR(cilen, p); \
886         if (cilen != CILEN_SHORT || \
887             citype != opt) \
888             goto bad; \
889         GETSHORT(cishort, p); \
890         if (cishort != val) \
891             goto bad; \
892     }
893 #define ACKCICHAR(opt, neg, val) \
894     if (neg) { \
895         if ((len -= CILEN_CHAR) < 0) \
896             goto bad; \
897         GETCHAR(citype, p); \
898         GETCHAR(cilen, p); \
899         if (cilen != CILEN_CHAR || \
900             citype != opt) \
901             goto bad; \
902         GETCHAR(cichar, p); \
903         if (cichar != val) \
904             goto bad; \
905     }
906 #define ACKCICHAP(opt, neg, val, digest) \
907     if (neg) { \
908         if ((len -= CILEN_CHAP) < 0) \
909             goto bad; \
910         GETCHAR(citype, p); \
911         GETCHAR(cilen, p); \
912         if (cilen != CILEN_CHAP || \
913             citype != opt) \
914             goto bad; \
915         GETSHORT(cishort, p); \
916         if (cishort != val) \
917             goto bad; \
918         GETCHAR(cichar, p); \
919         if (cichar != digest) \
920           goto bad; \
921     }
922 #define ACKCILONG(opt, neg, val) \
923     if (neg) { \
924         if ((len -= CILEN_LONG) < 0) \
925             goto bad; \
926         GETCHAR(citype, p); \
927         GETCHAR(cilen, p); \
928         if (cilen != CILEN_LONG || \
929             citype != opt) \
930             goto bad; \
931         GETLONG(cilong, p); \
932         if (cilong != val) \
933             goto bad; \
934     }
935 #define ACKCILQR(opt, neg, val) \
936     if (neg) { \
937         if ((len -= CILEN_LQR) < 0) \
938             goto bad; \
939         GETCHAR(citype, p); \
940         GETCHAR(cilen, p); \
941         if (cilen != CILEN_LQR || \
942             citype != opt) \
943             goto bad; \
944         GETSHORT(cishort, p); \
945         if (cishort != PPP_LQR) \
946             goto bad; \
947         GETLONG(cilong, p); \
948         if (cilong != val) \
949           goto bad; \
950     }
951 #define ACKCIENDP(opt, neg, class, val, vlen) \
952     if (neg) { \
953         int i; \
954         if ((len -= CILEN_CHAR + vlen) < 0) \
955             goto bad; \
956         GETCHAR(citype, p); \
957         GETCHAR(cilen, p); \
958         if (cilen != CILEN_CHAR + vlen || \
959             citype != opt) \
960             goto bad; \
961         GETCHAR(cichar, p); \
962         if (cichar != class) \
963             goto bad; \
964         for (i = 0; i < vlen; ++i) { \
965             GETCHAR(cichar, p); \
966             if (cichar != val[i]) \
967                 goto bad; \
968         } \
969     }
970
971         //ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
972         ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
973     ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
974               go->asyncmap);
975     ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
976     ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
977     ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
978     ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
979     ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
980     ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
981     ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
982     ACKCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
983     ACKCIVOID(CI_SSNHF, go->neg_ssnhf);
984     ACKCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
985               go->endpoint.value, go->endpoint.length);
986
987     /*
988      * If there are any remaining CIs, then this packet is bad.
989      */
990     if (len != 0)
991         goto bad;
992     return (1);
993 bad:
994     LCPDEBUG(("lcp_acki: received bad Ack!"));
995     return (0);
996 }
997
998
999 /*
1000  * lcp_nakci - Peer has sent a NAK for some of our CIs.
1001  * This should not modify any state if the Nak is bad
1002  * or if LCP is in the OPENED state.
1003  *
1004  * Returns:
1005  *      0 - Nak was bad.
1006  *      1 - Nak was good.
1007  */
1008 static int
1009 lcp_nakci(f, p, len)
1010     fsm *f;
1011     u_char *p;
1012     int len;
1013 {
1014     lcp_options *go = &lcp_gotoptions[f->unit];
1015     lcp_options *wo = &lcp_wantoptions[f->unit];
1016     lcp_options *ho = &lcp_hisoptions[f->unit];//andy add
1017     u_char citype, cichar, *next;
1018     u_short cishort;
1019     u_int32_t cilong;
1020     lcp_options no;             /* options we've seen Naks for */
1021     lcp_options try;            /* options to request next time */
1022     int looped_back = 0;
1023     int cilen;
1024     BZERO(&no, sizeof(no));
1025     try = *go;
1026     /*
1027      * Any Nak'd CIs must be in exactly the same order that we sent.
1028      * Check packet length and CI length at each step.
1029      * If we find any deviations, then this packet is bad.
1030      */
1031 #define NAKCIVOID(opt, neg) \
1032     if (go->neg && \
1033         len >= CILEN_VOID && \
1034         p[1] == CILEN_VOID && \
1035         p[0] == opt) { \
1036         len -= CILEN_VOID; \
1037         INCPTR(CILEN_VOID, p); \
1038         no.neg = 1; \
1039         try.neg = 0; \
1040     }
1041 #define NAKCICHAP(opt, neg, code) \
1042     if (go->neg && \
1043         len >= CILEN_CHAP && \
1044         p[1] == CILEN_CHAP && \
1045         p[0] == opt) { \
1046         len -= CILEN_CHAP; \
1047         INCPTR(2, p); \
1048         GETSHORT(cishort, p); \
1049         GETCHAR(cichar, p); \
1050         no.neg = 1; \
1051         code \
1052     }
1053 #define NAKCICHAR(opt, neg, code) \
1054     if (go->neg && \
1055         len >= CILEN_CHAR && \
1056         p[1] == CILEN_CHAR && \
1057         p[0] == opt) { \
1058         len -= CILEN_CHAR; \
1059         INCPTR(2, p); \
1060         GETCHAR(cichar, p); \
1061         no.neg = 1; \
1062         code \
1063     }
1064 #define NAKCISHORT(opt, neg, code) \
1065     if (go->neg && \
1066         len >= CILEN_SHORT && \
1067         p[1] == CILEN_SHORT && \
1068         p[0] == opt) { \
1069         len -= CILEN_SHORT; \
1070         INCPTR(2, p); \
1071         GETSHORT(cishort, p); \
1072         no.neg = 1; \
1073         code \
1074     }
1075 #define NAKCILONG(opt, neg, code) \
1076     if (go->neg && \
1077         len >= CILEN_LONG && \
1078         p[1] == CILEN_LONG && \
1079         p[0] == opt) { \
1080         len -= CILEN_LONG; \
1081         INCPTR(2, p); \
1082         GETLONG(cilong, p); \
1083         no.neg = 1; \
1084         code \
1085     }
1086 #define NAKCILQR(opt, neg, code) \
1087     if (go->neg && \
1088         len >= CILEN_LQR && \
1089         p[1] == CILEN_LQR && \
1090         p[0] == opt) { \
1091         len -= CILEN_LQR; \
1092         INCPTR(2, p); \
1093         GETSHORT(cishort, p); \
1094         GETLONG(cilong, p); \
1095         no.neg = 1; \
1096         code \
1097     }
1098 #define NAKCIENDP(opt, neg) \
1099     if (go->neg && \
1100         len >= CILEN_CHAR && \
1101         p[0] == opt && \
1102         p[1] >= CILEN_CHAR && \
1103         p[1] <= len) { \
1104         len -= p[1]; \
1105         INCPTR(p[1], p); \
1106         no.neg = 1; \
1107         try.neg = 0; \
1108     }
1109
1110     /*
1111      * We don't care if they want to send us smaller packets than
1112      * we want.  Therefore, accept any MRU less than what we asked for,
1113      * but then ignore the new value when setting the MRU in the kernel.
1114      * If they send us a bigger MRU than what we asked, accept it, up to
1115      * the limit of the default MRU we'd get if we didn't negotiate.
1116      */
1117     if (go->neg_mru /*&& go->mru != DEFMRU */) {
1118                 //lcp_allowoptions[0].mru = wo->mru;//andy add
1119                 //go->mru = wo->mru;//andy add
1120 #if 0           
1121                 if (ho->mru != 0){//andy add            
1122                         lcp_allowoptions[0].mru = ho->mru;
1123                 } else {
1124                         //lcp_allowoptions[0].mru = DEFMRU;
1125                         if (wo->neg_mru == 1 && wo->mru != 0)
1126                                 lcp_allowoptions[0].mru = wo->mru;
1127                         else
1128                                 lcp_allowoptions[0].mru = DEFMRU;       
1129                 }
1130 #endif                  
1131         NAKCISHORT(CI_MRU, neg_mru,
1132                    if (cishort <= wo->mru || cishort <= DEFMRU)
1133                        try.mru = cishort;
1134                    );
1135                    
1136                 /* Start andy add */
1137                 if (cishort > 0 && cishort <= DEFMRU){
1138                         lcp_allowoptions[0].mru =  cishort;
1139                 } else {
1140                         if (ho->mru != 0){              
1141                                 lcp_allowoptions[0].mru = ho->mru;
1142                         } else {
1143                                 if (wo->neg_mru == 1 && wo->mru != 0)
1144                                         lcp_allowoptions[0].mru = wo->mru;
1145                                 else
1146                                         lcp_allowoptions[0].mru = DEFMRU;       
1147                         }
1148                 }
1149                 /* End andy add */
1150                    
1151     }
1152
1153     /*
1154      * Add any characters they want to our (receive-side) asyncmap.
1155      */
1156     if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
1157         NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
1158                   try.asyncmap = go->asyncmap | cilong;
1159                   );
1160     }
1161
1162     /*
1163      * If they've nak'd our authentication-protocol, check whether
1164      * they are proposing a different protocol, or a different
1165      * hash algorithm for CHAP.
1166      */
1167     if ((go->neg_chap || go->neg_upap)
1168         && len >= CILEN_SHORT
1169         && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
1170         cilen = p[1];
1171         len -= cilen;
1172         no.neg_chap = go->neg_chap;
1173         no.neg_upap = go->neg_upap;
1174         INCPTR(2, p);
1175         GETSHORT(cishort, p);
1176         if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
1177             /*
1178              * If we were asking for CHAP, they obviously don't want to do it.
1179              * If we weren't asking for CHAP, then we were asking for PAP,
1180              * in which case this Nak is bad.
1181              */
1182             if (!go->neg_chap)
1183                 goto bad;
1184             try.neg_chap = 0;
1185
1186         } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
1187             GETCHAR(cichar, p);
1188             if (go->neg_chap) {
1189                 /*
1190                  * We were asking for CHAP/MD5; they must want a different
1191                  * algorithm.  If they can't do MD5, we can ask for M$-CHAP
1192                  * if we support it, otherwise we'll have to stop
1193                  * asking for CHAP.
1194                  */
1195               /*wilson modify for MS-CHAPv2, 05/26/2005*/
1196                 if (go->chap_mdtype == CHAP_MICROSOFT_V2)
1197                 {
1198                     try.use_chapms_v2 = 0;
1199                     if(try.use_chapms)
1200                         try.chap_mdtype = CHAP_MICROSOFT;
1201                     else if(try.use_digest)
1202                         try.chap_mdtype = CHAP_DIGEST_MD5;
1203                     else
1204                         try.neg_chap = 0;
1205                 }
1206                 else if(go->chap_mdtype == CHAP_MICROSOFT)
1207                 {
1208                     try.use_chapms = 0;
1209                     if(try.use_digest)
1210                         try.chap_mdtype = CHAP_DIGEST_MD5;
1211                     else
1212                         try.neg_chap = 0;
1213                 }
1214                 else if(go->chap_mdtype == CHAP_DIGEST_MD5)
1215                 {
1216                     try.use_digest = 0;
1217                     try.neg_chap = 0;
1218                 }
1219                 else
1220                     try.neg_chap = 0;
1221                 if ((cichar != CHAP_MICROSOFT_V2) &&
1222                     (cichar != CHAP_MICROSOFT) &&
1223                     (cichar != CHAP_DIGEST_MD5))
1224                         try.neg_chap = 0;
1225 //              if (cichar != go->chap_mdtype) {
1226 //#ifdef CHAPMS
1227 //                  if (cichar == CHAP_MICROSOFT)
1228 //                      go->chap_mdtype = CHAP_MICROSOFT;
1229 //                  else
1230 //#endif /* CHAPMS */
1231 //                      try.neg_chap = 0;
1232 //              }
1233               /*wilson modify end*/
1234             } else {
1235                 /*
1236                  * Stop asking for PAP if we were asking for it.
1237                  */
1238                 try.neg_upap = 0;
1239             }
1240
1241         } else {
1242             /*
1243              * We don't recognize what they're suggesting.
1244              * Stop asking for what we were asking for.
1245              */
1246             if (go->neg_chap)
1247                 try.neg_chap = 0;
1248             else
1249                 try.neg_upap = 0;
1250             p += cilen - CILEN_SHORT;
1251         }
1252     }
1253
1254     /*
1255      * If they can't cope with our link quality protocol, we'll have
1256      * to stop asking for LQR.  We haven't got any other protocol.
1257      * If they Nak the reporting period, take their value XXX ?
1258      */
1259     NAKCILQR(CI_QUALITY, neg_lqr,
1260              if (cishort != PPP_LQR)
1261                  try.neg_lqr = 0;
1262              else
1263                  try.lqr_period = cilong;
1264              );
1265
1266     /*
1267      * Only implementing CBCP...not the rest of the callback options
1268      */
1269     NAKCICHAR(CI_CALLBACK, neg_cbcp,
1270               try.neg_cbcp = 0;
1271               );
1272
1273     /*
1274      * Check for a looped-back line.
1275      */
1276     NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
1277               try.magicnumber = magic();
1278               looped_back = 1;
1279               );
1280
1281     /*
1282      * Peer shouldn't send Nak for protocol compression or
1283      * address/control compression requests; they should send
1284      * a Reject instead.  If they send a Nak, treat it as a Reject.
1285      */
1286     NAKCIVOID(CI_PCOMPRESSION, neg_pcompression);
1287     NAKCIVOID(CI_ACCOMPRESSION, neg_accompression);
1288
1289     /*
1290      * Nak for MRRU option - accept their value if it is smaller
1291      * than the one we want.
1292      */
1293     if (go->neg_mrru) {
1294         NAKCISHORT(CI_MRRU, neg_mrru,
1295                    if (cishort <= wo->mrru)
1296                        try.mrru = cishort;
1297                    );
1298     }
1299
1300     /*
1301      * Nak for short sequence numbers shouldn't be sent, treat it
1302      * like a reject.
1303      */
1304     NAKCIVOID(CI_SSNHF, neg_ssnhf);
1305
1306     /*
1307      * Nak of the endpoint discriminator option is not permitted,
1308      * treat it like a reject.
1309      */
1310     NAKCIENDP(CI_EPDISC, neg_endpoint);
1311
1312     /*
1313      * There may be remaining CIs, if the peer is requesting negotiation
1314      * on an option that we didn't include in our request packet.
1315      * If we see an option that we requested, or one we've already seen
1316      * in this packet, then this packet is bad.
1317      * If we wanted to respond by starting to negotiate on the requested
1318      * option(s), we could, but we don't, because except for the
1319      * authentication type and quality protocol, if we are not negotiating
1320      * an option, it is because we were told not to.
1321      * For the authentication type, the Nak from the peer means
1322      * `let me authenticate myself with you' which is a bit pointless.
1323      * For the quality protocol, the Nak means `ask me to send you quality
1324      * reports', but if we didn't ask for them, we don't want them.
1325      * An option we don't recognize represents the peer asking to
1326      * negotiate some option we don't support, so ignore it.
1327      */
1328     while (len > CILEN_VOID) {
1329         GETCHAR(citype, p);
1330         GETCHAR(cilen, p);
1331         if (cilen < CILEN_VOID || (len -= cilen) < 0)
1332             goto bad;
1333         next = p + cilen - 2;
1334
1335         switch (citype) {
1336         case CI_MRU:
1337             if ((go->neg_mru /* && go->mru != DEFMRU */)
1338                 || no.neg_mru || cilen != CILEN_SHORT)
1339                 goto bad;
1340             GETSHORT(cishort, p);
1341             if (cishort < DEFMRU) {
1342                 try.neg_mru = 1;
1343                 try.mru = cishort;
1344             }
1345             break;
1346         case CI_ASYNCMAP:
1347             if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
1348                 || no.neg_asyncmap || cilen != CILEN_LONG)
1349                 goto bad;
1350             break;
1351         case CI_AUTHTYPE:
1352             if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
1353                 goto bad;
1354             break;
1355         case CI_MAGICNUMBER:
1356             if (go->neg_magicnumber || no.neg_magicnumber ||
1357                 cilen != CILEN_LONG)
1358                 goto bad;
1359             break;
1360         case CI_PCOMPRESSION:
1361             if (go->neg_pcompression || no.neg_pcompression
1362                 || cilen != CILEN_VOID)
1363                 goto bad;
1364             break;
1365         case CI_ACCOMPRESSION:
1366             if (go->neg_accompression || no.neg_accompression
1367                 || cilen != CILEN_VOID)
1368                 goto bad;
1369             break;
1370         case CI_QUALITY:
1371             if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
1372                 goto bad;
1373             break;
1374         case CI_MRRU:
1375             if (go->neg_mrru || no.neg_mrru || cilen != CILEN_SHORT)
1376                 goto bad;
1377             break;
1378         case CI_SSNHF:
1379             if (go->neg_ssnhf || no.neg_ssnhf || cilen != CILEN_VOID)
1380                 goto bad;
1381             try.neg_ssnhf = 1;
1382             break;
1383         case CI_EPDISC:
1384             if (go->neg_endpoint || no.neg_endpoint || cilen < CILEN_CHAR)
1385                 goto bad;
1386             break;
1387         }
1388         p = next;
1389     }
1390
1391     /*
1392      * OK, the Nak is good.  Now we can update state.
1393      * If there are any options left we ignore them.
1394      */
1395     if (f->state != OPENED) {
1396         if (looped_back) {
1397             if (++try.numloops >= lcp_loopbackfail) {
1398                 notice("Serial line is looped back.");
1399                 lcp_close(f->unit, "Loopback detected");
1400                 status = EXIT_LOOPBACK;
1401             }
1402         } else
1403             try.numloops = 0;
1404         *go = try;
1405     }
1406     return 1;
1407
1408 bad:
1409     LCPDEBUG(("lcp_nakci: received bad Nak!"));
1410     printf("lcp_nakci: received bad Nak!\n");
1411     return 0;
1412 }
1413
1414
1415 /*
1416  * lcp_rejci - Peer has Rejected some of our CIs.
1417  * This should not modify any state if the Reject is bad
1418  * or if LCP is in the OPENED state.
1419  *
1420  * Returns:
1421  *      0 - Reject was bad.
1422  *      1 - Reject was good.
1423  */
1424 static int
1425 lcp_rejci(f, p, len)
1426     fsm *f;
1427     u_char *p;
1428     int len;
1429 {
1430     lcp_options *go = &lcp_gotoptions[f->unit];
1431     u_char cichar;
1432     u_short cishort;
1433     u_int32_t cilong;
1434     lcp_options try;            /* options to request next time */
1435
1436     try = *go;
1437
1438     /*
1439      * Any Rejected CIs must be in exactly the same order that we sent.
1440      * Check packet length and CI length at each step.
1441      * If we find any deviations, then this packet is bad.
1442      */
1443 #define REJCIVOID(opt, neg) \
1444     if (go->neg && \
1445         len >= CILEN_VOID && \
1446         p[1] == CILEN_VOID && \
1447         p[0] == opt) { \
1448         len -= CILEN_VOID; \
1449         INCPTR(CILEN_VOID, p); \
1450         try.neg = 0; \
1451     }
1452 #define REJCISHORT(opt, neg, val) \
1453     if (go->neg && \
1454         len >= CILEN_SHORT && \
1455         p[1] == CILEN_SHORT && \
1456         p[0] == opt) { \
1457         len -= CILEN_SHORT; \
1458         INCPTR(2, p); \
1459         GETSHORT(cishort, p); \
1460         /* Check rejected value. */ \
1461         if (cishort != val) \
1462             goto bad; \
1463         try.neg = 0; \
1464     }
1465 #define REJCICHAP(opt, neg, val, digest) \
1466     if (go->neg && \
1467         len >= CILEN_CHAP && \
1468         p[1] == CILEN_CHAP && \
1469         p[0] == opt) { \
1470         len -= CILEN_CHAP; \
1471         INCPTR(2, p); \
1472         GETSHORT(cishort, p); \
1473         GETCHAR(cichar, p); \
1474         /* Check rejected value. */ \
1475         if (cishort != val || cichar != digest) \
1476             goto bad; \
1477        /*wilson modify for MS-CHAPv2, 05/26/2005*/ \
1478         switch(digest) \
1479         { \
1480             case CHAP_MICROSOFT_V2: \
1481                 try.use_chapms_v2 = 0; \
1482                 break; \
1483             case CHAP_MICROSOFT: \
1484                 try.use_chapms = 0; \
1485                 break; \
1486             case CHAP_DIGEST_MD5: \
1487                 try.use_digest = 0; \
1488         } \
1489         if(!try.use_chapms_v2 && !try.use_chapms && !try.use_digest) \
1490         { \
1491         try.neg = 0; \
1492         try.neg_upap = 0; \
1493         } \
1494         /*wilson modify end*/ \
1495     }
1496 #define REJCILONG(opt, neg, val) \
1497     if (go->neg && \
1498         len >= CILEN_LONG && \
1499         p[1] == CILEN_LONG && \
1500         p[0] == opt) { \
1501         len -= CILEN_LONG; \
1502         INCPTR(2, p); \
1503         GETLONG(cilong, p); \
1504         /* Check rejected value. */ \
1505         if (cilong != val) \
1506             goto bad; \
1507         try.neg = 0; \
1508     }
1509 #define REJCILQR(opt, neg, val) \
1510     if (go->neg && \
1511         len >= CILEN_LQR && \
1512         p[1] == CILEN_LQR && \
1513         p[0] == opt) { \
1514         len -= CILEN_LQR; \
1515         INCPTR(2, p); \
1516         GETSHORT(cishort, p); \
1517         GETLONG(cilong, p); \
1518         /* Check rejected value. */ \
1519         if (cishort != PPP_LQR || cilong != val) \
1520             goto bad; \
1521         try.neg = 0; \
1522     }
1523 #define REJCICBCP(opt, neg, val) \
1524     if (go->neg && \
1525         len >= CILEN_CBCP && \
1526         p[1] == CILEN_CBCP && \
1527         p[0] == opt) { \
1528         len -= CILEN_CBCP; \
1529         INCPTR(2, p); \
1530         GETCHAR(cichar, p); \
1531         /* Check rejected value. */ \
1532         if (cichar != val) \
1533             goto bad; \
1534         try.neg = 0; \
1535     }
1536 #define REJCIENDP(opt, neg, class, val, vlen) \
1537     if (go->neg && \
1538         len >= CILEN_CHAR + vlen && \
1539         p[0] == opt && \
1540         p[1] == CILEN_CHAR + vlen) { \
1541         int i; \
1542         len -= CILEN_CHAR + vlen; \
1543         INCPTR(2, p); \
1544         GETCHAR(cichar, p); \
1545         if (cichar != class) \
1546             goto bad; \
1547         for (i = 0; i < vlen; ++i) { \
1548             GETCHAR(cichar, p); \
1549             if (cichar != val[i]) \
1550                 goto bad; \
1551         } \
1552         try.neg = 0; \
1553     }
1554
1555     REJCISHORT(CI_MRU, neg_mru, go->mru);
1556     REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1557     REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1558     if (!go->neg_chap) {
1559         REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1560     }
1561     REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1562     REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1563     REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1564     REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1565     REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1566     REJCISHORT(CI_MRRU, neg_mrru, go->mrru);
1567     REJCIVOID(CI_SSNHF, neg_ssnhf);
1568     REJCIENDP(CI_EPDISC, neg_endpoint, go->endpoint.class,
1569               go->endpoint.value, go->endpoint.length);
1570
1571     /*
1572      * If there are any remaining CIs, then this packet is bad.
1573      */
1574     if (len != 0)
1575         goto bad;
1576     /*
1577      * Now we can update state.
1578      */
1579     if (f->state != OPENED)
1580         *go = try;
1581     return 1;
1582
1583 bad:
1584     LCPDEBUG(("lcp_rejci: received bad Reject!"));
1585     return 0;
1586 }
1587
1588
1589 /*
1590  * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1591  *
1592  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1593  * appropriately.  If reject_if_disagree is non-zero, doesn't return
1594  * CONFNAK; returns CONFREJ if it can't return CONFACK.
1595  */
1596 static int
1597 lcp_reqci(f, inp, lenp, reject_if_disagree)
1598     fsm *f;
1599     u_char *inp;                /* Requested CIs */
1600     int *lenp;                  /* Length of requested CIs */
1601     int reject_if_disagree;
1602 {
1603     lcp_options *go = &lcp_gotoptions[f->unit];
1604     lcp_options *ho = &lcp_hisoptions[f->unit];
1605     lcp_options *ao = &lcp_allowoptions[f->unit];
1606     u_char *cip, *next;         /* Pointer to current and next CIs */
1607     int cilen, citype, cichar;  /* Parsed len, type, char value */
1608     u_short cishort;            /* Parsed short value */
1609     u_int32_t cilong;           /* Parse long value */
1610     int rc = CONFACK;           /* Final packet return code */
1611     int orc;                    /* Individual option return code */
1612     u_char *p;                  /* Pointer to next char to parse */
1613     u_char *rejp;               /* Pointer to next char in reject frame */
1614     u_char *nakp;               /* Pointer to next char in Nak frame */
1615     int l = *lenp;              /* Length left */
1616
1617     /*
1618      */
1619     BZERO(ho, sizeof(*ho));
1620
1621     /*
1622      * Process all his options.
1623      */
1624     next = inp;
1625     nakp = nak_buffer;
1626     rejp = inp;
1627     while (l) {
1628         orc = CONFACK;                  /* Assume success */
1629         cip = p = next;                 /* Remember begining of CI */
1630         if (l < 2 ||                    /* Not enough data for CI header or */
1631             p[1] < 2 ||                 /*  CI length too small or */
1632             p[1] > l) {                 /*  CI length too big? */
1633             LCPDEBUG(("lcp_reqci: bad CI length!"));
1634             orc = CONFREJ;              /* Reject bad CI */
1635             cilen = l;                  /* Reject till end of packet */
1636             l = 0;                      /* Don't loop again */
1637             citype = 0;
1638             goto endswitch;
1639         }
1640         GETCHAR(citype, p);             /* Parse CI type */
1641         GETCHAR(cilen, p);              /* Parse CI length */
1642         l -= cilen;                     /* Adjust remaining length */
1643         next += cilen;                  /* Step to next CI */
1644
1645         switch (citype) {               /* Check CI type */
1646         case CI_MRU:
1647             if (!ao->neg_mru ||         /* Allow option? */
1648                 cilen != CILEN_SHORT) { /* Check CI length */
1649                 orc = CONFREJ;          /* Reject CI */
1650                 break;
1651             }
1652             GETSHORT(cishort, p);       /* Parse MRU */
1653
1654             /*
1655              * He must be able to receive at least our minimum.
1656              * No need to check a maximum.  If he sends a large number,
1657              * we'll just ignore it.
1658              */
1659             if (cishort < MINMRU) {
1660                 orc = CONFNAK;          /* Nak CI */
1661                 PUTCHAR(CI_MRU, nakp);
1662                 PUTCHAR(CILEN_SHORT, nakp);
1663                 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1664                 break;
1665             }
1666             ho->neg_mru = 1;            /* Remember he sent MRU */
1667             ho->mru = cishort;          /* And remember value */
1668             break;
1669
1670         case CI_ASYNCMAP:
1671             if (!ao->neg_asyncmap ||
1672                 cilen != CILEN_LONG) {
1673                 orc = CONFREJ;
1674                 break;
1675             }
1676             GETLONG(cilong, p);
1677
1678             /*
1679              * Asyncmap must have set at least the bits
1680              * which are set in lcp_allowoptions[unit].asyncmap.
1681              */
1682             if ((ao->asyncmap & ~cilong) != 0) {
1683                 orc = CONFNAK;
1684                 PUTCHAR(CI_ASYNCMAP, nakp);
1685                 PUTCHAR(CILEN_LONG, nakp);
1686                 PUTLONG(ao->asyncmap | cilong, nakp);
1687                 break;
1688             }
1689             ho->neg_asyncmap = 1;
1690             ho->asyncmap = cilong;
1691             break;
1692
1693         case CI_AUTHTYPE:
1694             if (cilen < CILEN_SHORT || !(ao->neg_upap || ao->neg_chap)) {
1695                 /*
1696                  * Reject the option if we're not willing to authenticate.
1697                  */
1698                 orc = CONFREJ;
1699                 break;
1700             }
1701             GETSHORT(cishort, p);
1702
1703             /*
1704              * Authtype must be PAP or CHAP.
1705              *
1706              * Note: if both ao->neg_upap and ao->neg_chap are set,
1707              * and the peer sends a Configure-Request with two
1708              * authenticate-protocol requests, one for CHAP and one
1709              * for UPAP, then we will reject the second request.
1710              * Whether we end up doing CHAP or UPAP depends then on
1711              * the ordering of the CIs in the peer's Configure-Request.
1712              */
1713                 //Andy add for auto PVC
1714                 if (autoscan == 1 && (cishort == PPP_PAP || cishort == PPP_CHAP)){
1715                         create_msg(BCM_PPPOE_SERVICE_AVAILABLE);                
1716                 }
1717             if (cishort == PPP_PAP) {
1718                 if (ho->neg_chap ||     /* we've already accepted CHAP */
1719                     cilen != CILEN_SHORT) {
1720                     LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1721                     orc = CONFREJ;
1722                     break;
1723                 }
1724                 if (!ao->neg_upap) {    /* we don't want to do PAP */
1725                     orc = CONFNAK;      /* NAK it and suggest PAP */
1726                     PUTCHAR(CI_AUTHTYPE, nakp);
1727                     PUTCHAR(CILEN_CHAP, nakp);
1728                     PUTSHORT(PPP_CHAP, nakp);
1729                     PUTCHAR(ao->chap_mdtype, nakp);
1730                     // brcm
1731                     if (!auth_failed) {
1732                         printf("PPP: Authentication failed.\n");
1733                         create_msg(BCM_PPPOE_AUTH_FAILED); 
1734                             //syslog(LOG_ERR,"Authentication method failed.\n");
1735                         auth_failed = 1;
1736                         lcp_close(0, "Auth failed");
1737                         need_holdoff = 0;
1738                         status = EXIT_PEER_AUTH_FAILED;
1739                         exit(0);
1740                     }
1741                     persist=0;              
1742                     /* XXX if we can do CHAP_MICROSOFT as well, we should
1743                        probably put in another option saying so */
1744                     break;
1745                 }
1746                 ho->neg_upap = 1;
1747                 break;
1748             }
1749             if (cishort == PPP_CHAP) {
1750                 if (ho->neg_upap ||     /* we've already accepted PAP */
1751                     cilen != CILEN_CHAP) {
1752                     LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1753                     orc = CONFREJ;
1754                     break;
1755                 }
1756                 if (!ao->neg_chap) {    /* we don't want to do CHAP */
1757                     orc = CONFNAK;      /* NAK it and suggest PAP */
1758                     PUTCHAR(CI_AUTHTYPE, nakp);
1759                     PUTCHAR(CILEN_SHORT, nakp);
1760                     PUTSHORT(PPP_PAP, nakp);
1761                     // brcm
1762                     if (!auth_failed) {
1763                         printf("PPP: Authentication failed.\n");
1764                         create_msg(BCM_PPPOE_AUTH_FAILED); 
1765                             //syslog(LOG_ERR,"Authentication method failed.\n");
1766                         auth_failed = 1;
1767                         lcp_close(0, "Auth failed");
1768                         need_holdoff = 0;
1769                         exit(0);
1770                     }
1771                     persist=0;              
1772                     break;
1773                 }
1774                 GETCHAR(cichar, p);     /* get digest type*/
1775                 if (cichar != CHAP_DIGEST_MD5
1776 #ifdef CHAPMS
1777                     && cichar != CHAP_MICROSOFT
1778                             && cichar != CHAP_MICROSOFT_V2     /*wilson add for MS-CHAPv2, 05/26/2005*/
1779 #endif
1780                     ) {
1781                          /*wilson modify for MS-CHAPv2, 05/26/2005*/
1782                             //orc = CONFNAK;
1783                             orc = CONFREJ; /* !!! CONFNAK !!! */
1784                          /*wilson modify end*/
1785                     PUTCHAR(CI_AUTHTYPE, nakp);
1786                     PUTCHAR(CILEN_CHAP, nakp);
1787                     PUTSHORT(PPP_CHAP, nakp);
1788                     PUTCHAR(ao->chap_mdtype, nakp);
1789                     break;
1790                 }
1791                 ho->chap_mdtype = cichar; /* save md type */
1792                 ho->neg_chap = 1;
1793                 break;
1794             }
1795
1796             /*
1797              * We don't recognize the protocol they're asking for.
1798              * Nak it with something we're willing to do.
1799              * (At this point we know ao->neg_upap || ao->neg_chap.)
1800              */
1801             orc = CONFNAK;
1802             PUTCHAR(CI_AUTHTYPE, nakp);
1803             if (ao->neg_chap) {
1804                 PUTCHAR(CILEN_CHAP, nakp);
1805                 PUTSHORT(PPP_CHAP, nakp);
1806                 PUTCHAR(ao->chap_mdtype, nakp);
1807             } else {
1808                 PUTCHAR(CILEN_SHORT, nakp);
1809                 PUTSHORT(PPP_PAP, nakp);
1810             }
1811             break;
1812
1813         case CI_QUALITY:
1814             if (!ao->neg_lqr || cilen != CILEN_LQR) {
1815                 orc = CONFREJ;
1816                 break;
1817             }
1818
1819             GETSHORT(cishort, p);
1820             GETLONG(cilong, p);
1821
1822             /*
1823              * Check the protocol and the reporting period.
1824              * XXX When should we Nak this, and what with?
1825              */
1826             if (cishort != PPP_LQR) {
1827                 orc = CONFNAK;
1828                 PUTCHAR(CI_QUALITY, nakp);
1829                 PUTCHAR(CILEN_LQR, nakp);
1830                 PUTSHORT(PPP_LQR, nakp);
1831                 PUTLONG(ao->lqr_period, nakp);
1832                 break;
1833             }
1834             break;
1835
1836         case CI_MAGICNUMBER:
1837             if (!(ao->neg_magicnumber || go->neg_magicnumber) || cilen != CILEN_LONG) {
1838                 orc = CONFREJ;
1839                 break;
1840             }
1841             GETLONG(cilong, p);
1842
1843             /*
1844              * He must have a different magic number.
1845              */
1846             if (go->neg_magicnumber && cilong == go->magicnumber) {
1847                 cilong = magic();       /* Don't put magic() inside macro! */
1848                 orc = CONFNAK;
1849                 PUTCHAR(CI_MAGICNUMBER, nakp);
1850                 PUTCHAR(CILEN_LONG, nakp);
1851                 PUTLONG(cilong, nakp);
1852                 break;
1853             }
1854             ho->neg_magicnumber = 1;
1855             ho->magicnumber = cilong;
1856             break;
1857
1858
1859         case CI_PCOMPRESSION:
1860             if (!ao->neg_pcompression || cilen != CILEN_VOID) {
1861                 orc = CONFREJ;
1862                 break;
1863             }
1864             ho->neg_pcompression = 1;
1865             break;
1866
1867         case CI_ACCOMPRESSION:
1868             if (!ao->neg_accompression || cilen != CILEN_VOID) {
1869                 orc = CONFREJ;
1870                 break;
1871             }
1872             ho->neg_accompression = 1;
1873             break;
1874
1875         case CI_MRRU:
1876             if (!ao->neg_mrru || !multilink || cilen != CILEN_SHORT) {
1877                 orc = CONFREJ;
1878                 break;
1879             }
1880
1881             GETSHORT(cishort, p);
1882             /* possibly should insist on a minimum/maximum MRRU here */
1883             ho->neg_mrru = 1;
1884             ho->mrru = cishort;
1885             break;
1886
1887         case CI_SSNHF:
1888             if (!ao->neg_ssnhf || !multilink || cilen != CILEN_VOID) {
1889                 orc = CONFREJ;
1890                 break;
1891             }
1892             ho->neg_ssnhf = 1;
1893             break;
1894
1895         case CI_EPDISC:
1896             if (!ao->neg_endpoint || cilen < CILEN_CHAR || cilen > CILEN_CHAR + MAX_ENDP_LEN) {
1897                 orc = CONFREJ;
1898                 break;
1899             }
1900             GETCHAR(cichar, p);
1901             cilen -= CILEN_CHAR;
1902             ho->neg_endpoint = 1;
1903             ho->endpoint.class = cichar;
1904             ho->endpoint.length = cilen;
1905             BCOPY(p, ho->endpoint.value, cilen);
1906             INCPTR(cilen, p);
1907             break;
1908
1909         default:
1910             LCPDEBUG(("lcp_reqci: rcvd unknown option %d", citype));
1911             orc = CONFREJ;
1912             break;
1913         }
1914
1915 endswitch:
1916         if (orc == CONFACK &&           /* Good CI */
1917             rc != CONFACK)              /*  but prior CI wasnt? */
1918             continue;                   /* Don't send this one */
1919
1920         if (orc == CONFNAK) {           /* Nak this CI? */
1921             if (reject_if_disagree      /* Getting fed up with sending NAKs? */
1922                 && citype != CI_MAGICNUMBER) {
1923                 orc = CONFREJ;          /* Get tough if so */
1924             } else {
1925                 if (rc == CONFREJ)      /* Rejecting prior CI? */
1926                     continue;           /* Don't send this one */
1927                 rc = CONFNAK;
1928             }
1929         }
1930         if (orc == CONFREJ) {           /* Reject this CI */
1931             rc = CONFREJ;
1932             if (cip != rejp)            /* Need to move rejected CI? */
1933                 BCOPY(cip, rejp, cilen); /* Move it */
1934             INCPTR(cilen, rejp);        /* Update output pointer */
1935         }
1936     }
1937
1938     /*
1939      * If we wanted to send additional NAKs (for unsent CIs), the
1940      * code would go here.  The extra NAKs would go at *nakp.
1941      * At present there are no cases where we want to ask the
1942      * peer to negotiate an option.
1943      */
1944
1945     switch (rc) {
1946     case CONFACK:
1947         *lenp = next - inp;
1948         break;
1949     case CONFNAK:
1950         /*
1951          * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1952          */
1953         *lenp = nakp - nak_buffer;
1954         BCOPY(nak_buffer, inp, *lenp);
1955         break;
1956     case CONFREJ:
1957         *lenp = rejp - inp;
1958         break;
1959     }
1960
1961     LCPDEBUG(("lcp_reqci: returning CONF%s.", CODENAME(rc)));
1962     return (rc);                        /* Return final code */
1963 }
1964
1965
1966 /*
1967  * lcp_up - LCP has come UP.
1968  */
1969 static void
1970 lcp_up(f)
1971     fsm *f;
1972 {
1973     lcp_options *wo = &lcp_wantoptions[f->unit];
1974     lcp_options *ho = &lcp_hisoptions[f->unit];
1975     lcp_options *go = &lcp_gotoptions[f->unit];
1976     lcp_options *ao = &lcp_allowoptions[f->unit];
1977     int mtu;
1978     if (!go->neg_magicnumber)
1979         go->magicnumber = 0;
1980     if (!ho->neg_magicnumber)
1981         ho->magicnumber = 0;
1982
1983     /*
1984      * Set our MTU to the smaller of the MTU we wanted and
1985      * the MRU our peer wanted.  If we negotiated an MRU,
1986      * set our MRU to the larger of value we wanted and
1987      * the value we got in the negotiation.
1988      * Note on the MTU: the link MTU can be the MRU the peer wanted,
1989      * the interface MTU is set to the lower of that and the
1990      * MTU we want to use.
1991      */
1992 #if 0    
1993     mtu = ho->neg_mru? ho->mru: PPP_MRU;
1994 #else
1995         #if defined(CONFIG_LANG_MMM) || defined(ODM_LANG_LLL)//Andy add (2005/06/20)
1996         mtu = glb_CONFIG_PPP_MTU;
1997         #else 
1998         mtu = ho->neg_mru? ho->mru: glb_CONFIG_PPP_MTU;
1999         #endif
2000 #endif    
2001 #ifdef HAVE_MULTILINK
2002     if (!(multilink && go->neg_mrru && ho->neg_mrru))
2003 #endif /* HAVE_MULTILINK */
2004         netif_set_mtu(f->unit, MIN(mtu, ao->mru));
2005     ppp_send_config(f->unit, mtu,
2006                     (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
2007                     ho->neg_pcompression, ho->neg_accompression);
2008     ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
2009                     (lax_recv? 0: go->neg_asyncmap? go->asyncmap: 0xffffffff),
2010                     go->neg_pcompression, go->neg_accompression);
2011 #if 0 //andy commit    
2012     if (ho->neg_mru)
2013         peer_mru[f->unit] = ho->mru;
2014 #else//andy modify
2015         peer_mru[f->unit] = mtu;
2016 #endif
2017     lcp_echo_lowerup(f->unit);  /* Enable echo messages */
2018
2019     link_established(f->unit);
2020
2021     // brcm
2022     if (!auth_failed)
2023         create_msg(BCM_PPPOE_SERVICE_AVAILABLE);
2024     syslog(LOG_NOTICE,"PPP LCP Up\n");
2025 }
2026
2027
2028 /*
2029  * lcp_down - LCP has gone DOWN.
2030  *
2031  * Alert other protocols.
2032  */
2033 static void
2034 lcp_down(f)
2035     fsm *f;
2036 {
2037     lcp_options *go = &lcp_gotoptions[f->unit];
2038
2039     lcp_echo_lowerdown(f->unit);
2040
2041     link_down(f->unit);
2042
2043     ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
2044     ppp_recv_config(f->unit, PPP_MRU,
2045                     (go->neg_asyncmap? go->asyncmap: 0xffffffff),
2046                     go->neg_pcompression, go->neg_accompression);
2047     peer_mru[f->unit] = PPP_MRU;
2048 }
2049
2050
2051 /*
2052  * lcp_starting - LCP needs the lower layer up.
2053  */
2054 static void
2055 lcp_starting(f)
2056     fsm *f;
2057 {
2058     link_required(f->unit);
2059 }
2060
2061
2062 /*
2063  * lcp_finished - LCP has finished with the lower layer.
2064  */
2065 static void
2066 lcp_finished(f)
2067     fsm *f;
2068 {
2069     link_terminated(f->unit);
2070 }
2071
2072
2073 /*
2074  * lcp_printpkt - print the contents of an LCP packet.
2075  */
2076 static char *lcp_codenames[] = {
2077     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
2078     "TermReq", "TermAck", "CodeRej", "ProtRej",
2079     "EchoReq", "EchoRep", "DiscReq"
2080 };
2081
2082 static int
2083 lcp_printpkt(p, plen, printer, arg)
2084     u_char *p;
2085     int plen;
2086     void (*printer) __P((void *, char *, ...));
2087     void *arg;
2088 {
2089     int code, id, len, olen, i;
2090     u_char *pstart, *optend;
2091     u_short cishort;
2092     u_int32_t cilong;
2093
2094     if (plen < HEADERLEN)
2095         return 0;
2096     pstart = p;
2097     GETCHAR(code, p);
2098     GETCHAR(id, p);
2099     GETSHORT(len, p);
2100     if (len < HEADERLEN || len > plen)
2101         return 0;
2102
2103     if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
2104         printer(arg, " %s", lcp_codenames[code-1]);
2105     else
2106         printer(arg, " code=0x%x", code);
2107     printer(arg, " id=0x%x", id);
2108     len -= HEADERLEN;
2109     switch (code) {
2110     case CONFREQ:
2111     case CONFACK:
2112     case CONFNAK:
2113     case CONFREJ:
2114         /* print option list */
2115         while (len >= 2) {
2116             GETCHAR(code, p);
2117             GETCHAR(olen, p);
2118             p -= 2;
2119             if (olen < 2 || olen > len) {
2120                 break;
2121             }
2122             printer(arg, " <");
2123             len -= olen;
2124             optend = p + olen;
2125             switch (code) {
2126             case CI_MRU:
2127                 if (olen == CILEN_SHORT) {
2128                     p += 2;
2129                     GETSHORT(cishort, p);
2130                     printer(arg, "mru %d", cishort);
2131                 }
2132                 break;
2133             case CI_ASYNCMAP:
2134                 if (olen == CILEN_LONG) {
2135                     p += 2;
2136                     GETLONG(cilong, p);
2137                     printer(arg, "asyncmap 0x%x", cilong);
2138                 }
2139                 break;
2140             case CI_AUTHTYPE:
2141                 if (olen >= CILEN_SHORT) {
2142                     p += 2;
2143                     printer(arg, "auth ");
2144                     GETSHORT(cishort, p);
2145                     switch (cishort) {
2146                     case PPP_PAP:
2147                         printer(arg, "pap");
2148                         break;
2149                     case PPP_CHAP:
2150                         printer(arg, "chap");
2151                         if (p < optend) {
2152                             switch (*p) {
2153                             case CHAP_DIGEST_MD5:
2154                                 printer(arg, " MD5");
2155                                 ++p;
2156                                 break;
2157 #ifdef CHAPMS
2158                             case CHAP_MICROSOFT:
2159                                 printer(arg, " m$oft");
2160                                 ++p;
2161                                 break;
2162 #endif
2163                             }
2164                         }
2165                         break;
2166                     default:
2167                         printer(arg, "0x%x", cishort);
2168                     }
2169                 }
2170                 break;
2171             case CI_QUALITY:
2172                 if (olen >= CILEN_SHORT) {
2173                     p += 2;
2174                     printer(arg, "quality ");
2175                     GETSHORT(cishort, p);
2176                     switch (cishort) {
2177                     case PPP_LQR:
2178                         printer(arg, "lqr");
2179                         break;
2180                     default:
2181                         printer(arg, "0x%x", cishort);
2182                     }
2183                 }
2184                 break;
2185             case CI_CALLBACK:
2186                 if (olen >= CILEN_CHAR) {
2187                     p += 2;
2188                     printer(arg, "callback ");
2189                     GETCHAR(cishort, p);
2190                     switch (cishort) {
2191                     case CBCP_OPT:
2192                         printer(arg, "CBCP");
2193                         break;
2194                     default:
2195                         printer(arg, "0x%x", cishort);
2196                     }
2197                 }
2198                 break;
2199             case CI_MAGICNUMBER:
2200                 if (olen == CILEN_LONG) {
2201                     p += 2;
2202                     GETLONG(cilong, p);
2203                     printer(arg, "magic 0x%x", cilong);
2204                 }
2205                 break;
2206             case CI_PCOMPRESSION:
2207                 if (olen == CILEN_VOID) {
2208                     p += 2;
2209                     printer(arg, "pcomp");
2210                 }
2211                 break;
2212             case CI_ACCOMPRESSION:
2213                 if (olen == CILEN_VOID) {
2214                     p += 2;
2215                     printer(arg, "accomp");
2216                 }
2217                 break;
2218             case CI_MRRU:
2219                 if (olen == CILEN_SHORT) {
2220                     p += 2;
2221                     GETSHORT(cishort, p);
2222                     printer(arg, "mrru %d", cishort);
2223                 }
2224                 break;
2225             case CI_SSNHF:
2226                 if (olen == CILEN_VOID) {
2227                     p += 2;
2228                     printer(arg, "ssnhf");
2229                 }
2230                 break;
2231             case CI_EPDISC:
2232 #ifdef HAVE_MULTILINK
2233                 if (olen >= CILEN_CHAR) {
2234                     struct epdisc epd;
2235                     p += 2;
2236                     GETCHAR(epd.class, p);
2237                     epd.length = olen - CILEN_CHAR;
2238                     if (epd.length > MAX_ENDP_LEN)
2239                         epd.length = MAX_ENDP_LEN;
2240                     if (epd.length > 0) {
2241                         BCOPY(p, epd.value, epd.length);
2242                         p += epd.length;
2243                     }
2244                     printer(arg, "endpoint [%s]", epdisc_to_str(&epd));
2245                 }
2246 #else
2247                 printer(arg, "endpoint");
2248 #endif
2249                 break;
2250             }
2251             while (p < optend) {
2252                 GETCHAR(code, p);
2253                 printer(arg, " %.2x", code);
2254             }
2255             printer(arg, ">");
2256         }
2257         break;
2258
2259     case TERMACK:
2260     case TERMREQ:
2261         if (len > 0 && *p >= ' ' && *p < 0x7f) {
2262             printer(arg, " ");
2263             print_string((char *)p, len, printer, arg);
2264             p += len;
2265             len = 0;
2266         }
2267         break;
2268
2269     case ECHOREQ:
2270     case ECHOREP:
2271     case DISCREQ:
2272         if (len >= 4) {
2273             GETLONG(cilong, p);
2274             printer(arg, " magic=0x%x", cilong);
2275             p += 4;
2276             len -= 4;
2277         }
2278         break;
2279     }
2280
2281     /* print the rest of the bytes in the packet */
2282     for (i = 0; i < len && i < 32; ++i) {
2283         GETCHAR(code, p);
2284         printer(arg, " %.2x", code);
2285     }
2286     if (i < len) {
2287         printer(arg, " ...");
2288         p += len - i;
2289     }
2290
2291     return p - pstart;
2292 }
2293
2294 /*
2295  * Time to shut down the link because there is nothing out there.
2296  */
2297
2298 static
2299 void LcpLinkFailure (f)
2300     fsm *f;
2301 {
2302     if (f->state == OPENED) {
2303         info("No response to %d echo-requests", lcp_echos_pending);
2304         notice("Serial link appears to be disconnected.");
2305         lcp_close(f->unit, "Peer not responding");
2306         status = EXIT_PEER_DEAD;
2307     }
2308 }
2309
2310 static int traffic()
2311 {
2312     struct ppp_idle idle;
2313     time_t itime;
2314     int tlim;
2315
2316     get_idle_time(0, &idle);
2317     //tlim = MIN(idle.xmit_idle, idle.recv_idle);
2318     tlim = idle.recv_idle;
2319     if (tlim > idle_for_echo)
2320         return 0;
2321     else
2322         return 1;
2323 }
2324
2325
2326 /*
2327  * Timer expired for the LCP echo requests from this process.
2328  */
2329
2330 static void
2331 LcpEchoCheck (f)
2332     fsm *f;
2333 {
2334     // brcm
2335     if (!traffic())
2336         LcpSendEchoRequest (f);
2337     else
2338         lcp_echos_pending= 0;
2339         
2340     if (f->state != OPENED)
2341         return;
2342
2343     /*
2344      * Start the timer for the next interval.
2345      */
2346     if (lcp_echo_timer_running)
2347         warn("assertion lcp_echo_timer_running==0 failed");
2348     TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
2349     lcp_echo_timer_running = 1;
2350 }
2351
2352 /*
2353  * LcpEchoTimeout - Timer expired on the LCP echo
2354  */
2355
2356 static void
2357 LcpEchoTimeout (arg)
2358     void *arg;
2359 {
2360     if (lcp_echo_timer_running != 0) {
2361         lcp_echo_timer_running = 0;
2362         LcpEchoCheck ((fsm *) arg);
2363     }
2364 }
2365
2366 /*
2367  * LcpEchoReply - LCP has received a reply to the echo
2368  */
2369
2370 static void
2371 lcp_received_echo_reply (f, id, inp, len)
2372     fsm *f;
2373     int id;
2374     u_char *inp;
2375     int len;
2376 {
2377     u_int32_t magic;
2378
2379     /* Check the magic number - don't count replies from ourselves. */
2380     if (len < 4) {
2381         dbglog("lcp: received short Echo-Reply, length %d", len);
2382         return;
2383     }
2384     GETLONG(magic, inp);
2385     if (lcp_gotoptions[f->unit].neg_magicnumber
2386         && magic == lcp_gotoptions[f->unit].magicnumber) {
2387         warn("appear to have received our own echo-reply!");
2388         return;
2389     }
2390
2391     /* Reset the number of outstanding echo frames */
2392     lcp_echos_pending = 0;
2393 }
2394
2395 /*
2396  * LcpSendEchoRequest - Send an echo request frame to the peer
2397  */
2398
2399 static void
2400 LcpSendEchoRequest (f)
2401     fsm *f;
2402 {
2403     u_int32_t lcp_magic;
2404     u_char pkt[4], *pktp;
2405
2406     /*
2407      * Detect the failure of the peer at this point.
2408      */
2409     if (lcp_echo_fails != 0) {
2410         if (lcp_echos_pending >= lcp_echo_fails) {
2411             LcpLinkFailure(f);
2412             lcp_echos_pending = 0;
2413         }
2414     }
2415
2416     /*
2417      * Make and send the echo request frame.
2418      */
2419     if (f->state == OPENED) {
2420         lcp_magic = lcp_gotoptions[f->unit].magicnumber;
2421         pktp = pkt;
2422         PUTLONG(lcp_magic, pktp);
2423         fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
2424         ++lcp_echos_pending;
2425     }
2426 }
2427
2428 /*
2429  * lcp_echo_lowerup - Start the timer for the LCP frame
2430  */
2431
2432 static void
2433 lcp_echo_lowerup (unit)
2434     int unit;
2435 {
2436     fsm *f = &lcp_fsm[unit];
2437
2438     /* Clear the parameters for generating echo frames */
2439     lcp_echos_pending      = 0;
2440     lcp_echo_number        = 0;
2441     lcp_echo_timer_running = 0;
2442   
2443     /* If a timeout interval is specified then start the timer */
2444     if (lcp_echo_interval != 0)
2445         LcpEchoCheck (f);
2446 }
2447
2448 /*
2449  * lcp_echo_lowerdown - Stop the timer for the LCP frame
2450  */
2451
2452 static void
2453 lcp_echo_lowerdown (unit)
2454     int unit;
2455 {
2456     fsm *f = &lcp_fsm[unit];
2457
2458     if (lcp_echo_timer_running != 0) {
2459         UNTIMEOUT (LcpEchoTimeout, f);
2460         lcp_echo_timer_running = 0;
2461     }
2462 }
2463