]> git.sur5r.net Git - freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/lwip-1.1.0/src/netif/ppp/lcp.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / lwIP_Demo_Rowley_ARM7 / lwip-1.1.0 / src / netif / ppp / lcp.c
1 /*****************************************************************************\r
2 * lcp.c - Network Link Control Protocol program file.\r
3 *\r
4 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.\r
5 * portions Copyright (c) 1997 by Global Election Systems Inc.\r
6 *\r
7 * The authors hereby grant permission to use, copy, modify, distribute,\r
8 * and license this software and its documentation for any purpose, provided\r
9 * that existing copyright notices are retained in all copies and that this\r
10 * notice and the following disclaimer are included verbatim in any \r
11 * distributions. No written agreement, license, or royalty fee is required\r
12 * for any of the authorized uses.\r
13 *\r
14 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR\r
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
17 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
24 *\r
25 ******************************************************************************\r
26 * REVISION HISTORY\r
27 *\r
28 * 03-01-01 Marc Boucher <marc@mbsi.ca>\r
29 *   Ported to lwIP.\r
30 * 97-12-01 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.\r
31 *       Original.\r
32 *****************************************************************************/\r
33 \r
34 /*\r
35  * lcp.c - PPP Link Control Protocol.\r
36  *\r
37  * Copyright (c) 1989 Carnegie Mellon University.\r
38  * All rights reserved.\r
39  *\r
40  * Redistribution and use in source and binary forms are permitted\r
41  * provided that the above copyright notice and this paragraph are\r
42  * duplicated in all such forms and that any documentation,\r
43  * advertising materials, and other materials related to such\r
44  * distribution and use acknowledge that the software was developed\r
45  * by Carnegie Mellon University.  The name of the\r
46  * University may not be used to endorse or promote products derived\r
47  * from this software without specific prior written permission.\r
48  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR\r
49  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED\r
50  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.\r
51  */\r
52  \r
53 #include <string.h>\r
54  \r
55 #include "ppp.h"\r
56 #if PPP_SUPPORT > 0\r
57 #include "fsm.h"\r
58 #include "chap.h"\r
59 #include "magic.h"\r
60 #include "auth.h"\r
61 #include "lcp.h"\r
62 #include "pppdebug.h"\r
63 \r
64 \r
65 /*************************/\r
66 /*** LOCAL DEFINITIONS ***/\r
67 /*************************/\r
68 /*\r
69  * Length of each type of configuration option (in octets)\r
70  */\r
71 #define CILEN_VOID      2\r
72 #define CILEN_CHAR      3\r
73 #define CILEN_SHORT     4       /* CILEN_VOID + sizeof(short) */\r
74 #define CILEN_CHAP      5       /* CILEN_VOID + sizeof(short) + 1 */\r
75 #define CILEN_LONG      6       /* CILEN_VOID + sizeof(long) */\r
76 #define CILEN_LQR       8       /* CILEN_VOID + sizeof(short) + sizeof(long) */\r
77 #define CILEN_CBCP      3\r
78 \r
79 \r
80 /***********************************/\r
81 /*** LOCAL FUNCTION DECLARATIONS ***/\r
82 /***********************************/\r
83 /*\r
84  * Callbacks for fsm code.  (CI = Configuration Information)\r
85  */\r
86 static void lcp_resetci (fsm*);         /* Reset our CI */\r
87 static int  lcp_cilen (fsm*);                   /* Return length of our CI */\r
88 static void lcp_addci (fsm*, u_char*, int*);       /* Add our CI to pkt */\r
89 static int  lcp_ackci (fsm*, u_char*, int);/* Peer ack'd our CI */\r
90 static int  lcp_nakci (fsm*, u_char*, int);/* Peer nak'd our CI */\r
91 static int  lcp_rejci (fsm*, u_char*, int);/* Peer rej'd our CI */\r
92 static int  lcp_reqci (fsm*, u_char*, int*, int);  /* Rcv peer CI */\r
93 static void lcp_up (fsm*);                          /* We're UP */\r
94 static void lcp_down (fsm*);                /* We're DOWN */\r
95 static void lcp_starting (fsm*);            /* We need lower layer up */\r
96 static void lcp_finished (fsm*);                /* We need lower layer down */\r
97 static int  lcp_extcode (fsm*, int, u_char, u_char*, int);\r
98 \r
99 static void lcp_rprotrej (fsm*, u_char*, int);\r
100 \r
101 /*\r
102  * routines to send LCP echos to peer\r
103  */\r
104 static void lcp_echo_lowerup (int);\r
105 static void lcp_echo_lowerdown (int);\r
106 static void LcpEchoTimeout (void*);\r
107 static void lcp_received_echo_reply (fsm*, int, u_char*, int);\r
108 static void LcpSendEchoRequest (fsm*);\r
109 static void LcpLinkFailure (fsm*);\r
110 static void LcpEchoCheck (fsm*);\r
111 \r
112 /*\r
113  * Protocol entry points.\r
114  * Some of these are called directly.\r
115  */\r
116 static void lcp_input (int, u_char *, int);\r
117 static void lcp_protrej (int);\r
118 \r
119 #define CODENAME(x)     ((x) == CONFACK ? "ACK" : \\r
120                          (x) == CONFNAK ? "NAK" : "REJ")\r
121 \r
122 \r
123 /******************************/\r
124 /*** PUBLIC DATA STRUCTURES ***/\r
125 /******************************/\r
126 /* global vars */\r
127 LinkPhase lcp_phase[NUM_PPP];                   /* Phase of link session (RFC 1661) */\r
128 lcp_options lcp_wantoptions[NUM_PPP];   /* Options that we want to request */\r
129 lcp_options lcp_gotoptions[NUM_PPP];    /* Options that peer ack'd */\r
130 lcp_options lcp_allowoptions[NUM_PPP];  /* Options we allow peer to request */\r
131 lcp_options lcp_hisoptions[NUM_PPP];    /* Options that we ack'd */\r
132 ext_accm xmit_accm[NUM_PPP];                    /* extended transmit ACCM */\r
133 \r
134 \r
135 \r
136 /*****************************/\r
137 /*** LOCAL DATA STRUCTURES ***/\r
138 /*****************************/\r
139 static fsm lcp_fsm[NUM_PPP];                    /* LCP fsm structure (global)*/\r
140 static u_int     lcp_echo_interval = LCP_ECHOINTERVAL; /* Interval between LCP echo-requests */\r
141 static u_int     lcp_echo_fails = LCP_MAXECHOFAILS; /* Tolerance to unanswered echo-requests */\r
142 static u32_t lcp_echos_pending = 0;     /* Number of outstanding echo msgs */\r
143 static u32_t lcp_echo_number   = 0;     /* ID number of next echo frame */\r
144 static u32_t lcp_echo_timer_running = 0;  /* TRUE if a timer is running */\r
145 \r
146 static u_char nak_buffer[PPP_MRU];      /* where we construct a nak packet */\r
147 \r
148 static fsm_callbacks lcp_callbacks = {  /* LCP callback routines */\r
149     lcp_resetci,                /* Reset our Configuration Information */\r
150     lcp_cilen,                  /* Length of our Configuration Information */\r
151     lcp_addci,                  /* Add our Configuration Information */\r
152     lcp_ackci,                  /* ACK our Configuration Information */\r
153     lcp_nakci,                  /* NAK our Configuration Information */\r
154     lcp_rejci,                  /* Reject our Configuration Information */\r
155     lcp_reqci,                  /* Request peer's Configuration Information */\r
156     lcp_up,                             /* Called when fsm reaches OPENED state */\r
157     lcp_down,                   /* Called when fsm leaves OPENED state */\r
158     lcp_starting,               /* Called when we want the lower layer up */\r
159     lcp_finished,               /* Called when we want the lower layer down */\r
160     NULL,                               /* Called when Protocol-Reject received */\r
161     NULL,                               /* Retransmission is necessary */\r
162     lcp_extcode,                /* Called to handle LCP-specific codes */\r
163     "LCP"                               /* String name of protocol */\r
164 };\r
165 \r
166 struct protent lcp_protent = {\r
167     PPP_LCP,\r
168     lcp_init,\r
169     lcp_input,\r
170     lcp_protrej,\r
171     lcp_lowerup,\r
172     lcp_lowerdown,\r
173     lcp_open,\r
174     lcp_close,\r
175 #if 0\r
176     lcp_printpkt,\r
177     NULL,\r
178 #endif\r
179     1,\r
180     "LCP",\r
181 #if 0\r
182     NULL,\r
183     NULL,\r
184     NULL\r
185 #endif\r
186 };\r
187 \r
188 int lcp_loopbackfail = DEFLOOPBACKFAIL;\r
189 \r
190 \r
191 \r
192 /***********************************/\r
193 /*** PUBLIC FUNCTION DEFINITIONS ***/\r
194 /***********************************/\r
195 /*\r
196  * lcp_init - Initialize LCP.\r
197  */\r
198 void lcp_init(int unit)\r
199 {\r
200         fsm *f = &lcp_fsm[unit];\r
201         lcp_options *wo = &lcp_wantoptions[unit];\r
202         lcp_options *ao = &lcp_allowoptions[unit];\r
203         \r
204         f->unit = unit;\r
205         f->protocol = PPP_LCP;\r
206         f->callbacks = &lcp_callbacks;\r
207         \r
208         fsm_init(f);\r
209         \r
210         wo->passive = 0;\r
211         wo->silent = 0;\r
212         wo->restart = 0;                        /* Set to 1 in kernels or multi-line\r
213                                                                  * implementations */\r
214         wo->neg_mru = 1;\r
215         wo->mru = PPP_DEFMRU;\r
216         wo->neg_asyncmap = 1;\r
217         wo->asyncmap = 0x00000000l;     /* Assume don't need to escape any ctl chars. */\r
218         wo->neg_chap = 0;                       /* Set to 1 on server */\r
219         wo->neg_upap = 0;                       /* Set to 1 on server */\r
220         wo->chap_mdtype = CHAP_DIGEST_MD5;\r
221         wo->neg_magicnumber = 1;\r
222         wo->neg_pcompression = 1;\r
223         wo->neg_accompression = 1;\r
224         wo->neg_lqr = 0;                        /* no LQR implementation yet */\r
225         wo->neg_cbcp = 0;\r
226         \r
227         ao->neg_mru = 1;\r
228         ao->mru = PPP_MAXMRU;\r
229         ao->neg_asyncmap = 1;\r
230         ao->asyncmap = 0x00000000l;     /* Assume don't need to escape any ctl chars. */\r
231         ao->neg_chap = (CHAP_SUPPORT != 0);\r
232         ao->chap_mdtype = CHAP_DIGEST_MD5;\r
233         ao->neg_upap = (PAP_SUPPORT != 0);\r
234         ao->neg_magicnumber = 1;\r
235         ao->neg_pcompression = 1;\r
236         ao->neg_accompression = 1;\r
237         ao->neg_lqr = 0;                        /* no LQR implementation yet */\r
238         ao->neg_cbcp = (CBCP_SUPPORT != 0);\r
239 \r
240         /* \r
241          * Set transmit escape for the flag and escape characters plus anything\r
242          * set for the allowable options.\r
243          */\r
244         memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));\r
245         xmit_accm[unit][15] = 0x60;\r
246         xmit_accm[unit][0] = (u_char)(ao->asyncmap & 0xFF);\r
247         xmit_accm[unit][1] = (u_char)((ao->asyncmap >> 8) & 0xFF);\r
248         xmit_accm[unit][2] = (u_char)((ao->asyncmap >> 16) & 0xFF);\r
249         xmit_accm[unit][3] = (u_char)((ao->asyncmap >> 24) & 0xFF);\r
250         LCPDEBUG((LOG_INFO, "lcp_init: xmit_accm=%X %X %X %X\n",\r
251                                 xmit_accm[unit][0],\r
252                                 xmit_accm[unit][1],\r
253                                 xmit_accm[unit][2],\r
254                                 xmit_accm[unit][3]));\r
255         \r
256         lcp_phase[unit] = PHASE_INITIALIZE;\r
257 }\r
258 \r
259 \r
260 /*\r
261  * lcp_open - LCP is allowed to come up.\r
262  */\r
263 void lcp_open(int unit)\r
264 {\r
265         fsm *f = &lcp_fsm[unit];\r
266         lcp_options *wo = &lcp_wantoptions[unit];\r
267         \r
268         f->flags = 0;\r
269         if (wo->passive)\r
270                 f->flags |= OPT_PASSIVE;\r
271         if (wo->silent)\r
272                 f->flags |= OPT_SILENT;\r
273         fsm_open(f);\r
274         \r
275         lcp_phase[unit] = PHASE_ESTABLISH; \r
276 }\r
277 \r
278 \r
279 /*\r
280  * lcp_close - Take LCP down.\r
281  */\r
282 void lcp_close(int unit, char *reason)\r
283 {\r
284         fsm *f = &lcp_fsm[unit];\r
285         \r
286         if (lcp_phase[unit] != PHASE_DEAD)\r
287                 lcp_phase[unit] = PHASE_TERMINATE;\r
288         if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {\r
289                 /*\r
290                  * This action is not strictly according to the FSM in RFC1548,\r
291                  * but it does mean that the program terminates if you do an\r
292                  * lcp_close() in passive/silent mode when a connection hasn't\r
293                  * been established.\r
294                  */\r
295                 f->state = CLOSED;\r
296                 lcp_finished(f);\r
297         }\r
298         else\r
299                 fsm_close(&lcp_fsm[unit], reason);\r
300 }\r
301 \r
302 \r
303 /*\r
304  * lcp_lowerup - The lower layer is up.\r
305  */\r
306 void lcp_lowerup(int unit)\r
307 {\r
308         lcp_options *wo = &lcp_wantoptions[unit];\r
309         \r
310         /*\r
311         * Don't use A/C or protocol compression on transmission,\r
312         * but accept A/C and protocol compressed packets\r
313         * if we are going to ask for A/C and protocol compression.\r
314         */\r
315         ppp_set_xaccm(unit, &xmit_accm[unit]);\r
316         ppp_send_config(unit, PPP_MRU, 0xffffffffl, 0, 0);\r
317         ppp_recv_config(unit, PPP_MRU, 0x00000000l,\r
318                                         wo->neg_pcompression, wo->neg_accompression);\r
319         peer_mru[unit] = PPP_MRU;\r
320         lcp_allowoptions[unit].asyncmap \r
321                 = (u_long)xmit_accm[unit][0]\r
322                         | ((u_long)xmit_accm[unit][1] << 8)\r
323                         | ((u_long)xmit_accm[unit][2] << 16)\r
324                         | ((u_long)xmit_accm[unit][3] << 24);\r
325         LCPDEBUG((LOG_INFO, "lcp_lowerup: asyncmap=%X %X %X %X\n",\r
326                                 xmit_accm[unit][3],\r
327                                 xmit_accm[unit][2],\r
328                                 xmit_accm[unit][1],\r
329                                 xmit_accm[unit][0]));\r
330         \r
331         fsm_lowerup(&lcp_fsm[unit]);\r
332 }\r
333 \r
334 \r
335 /*\r
336  * lcp_lowerdown - The lower layer is down.\r
337  */\r
338 void lcp_lowerdown(int unit)\r
339 {\r
340         fsm_lowerdown(&lcp_fsm[unit]);\r
341 }\r
342 \r
343 /*\r
344  * lcp_sprotrej - Send a Protocol-Reject for some protocol.\r
345  */\r
346 void lcp_sprotrej(int unit, u_char *p, int len)\r
347 {\r
348         /*\r
349         * Send back the protocol and the information field of the\r
350         * rejected packet.  We only get here if LCP is in the OPENED state.\r
351         */\r
352 \r
353         fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,\r
354                                 p, len);\r
355 }\r
356 \r
357 \r
358 \r
359 /**********************************/\r
360 /*** LOCAL FUNCTION DEFINITIONS ***/\r
361 /**********************************/\r
362 /*\r
363  * lcp_input - Input LCP packet.\r
364  */\r
365 static void lcp_input(int unit, u_char *p, int len)\r
366 {\r
367         fsm *f = &lcp_fsm[unit];\r
368         \r
369         fsm_input(f, p, len);\r
370 }\r
371 \r
372 \r
373 /*\r
374  * lcp_extcode - Handle a LCP-specific code.\r
375  */\r
376 static int lcp_extcode(fsm *f, int code, u_char id, u_char *inp, int len)\r
377 {\r
378         u_char *magp;\r
379         \r
380         switch( code ){\r
381         case PROTREJ:\r
382                 lcp_rprotrej(f, inp, len);\r
383                 break;\r
384         \r
385         case ECHOREQ:\r
386                 if (f->state != OPENED)\r
387                         break;\r
388                 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d\n", id));\r
389                 magp = inp;\r
390                 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);\r
391                 fsm_sdata(f, ECHOREP, id, inp, len);\r
392                 break;\r
393         \r
394         case ECHOREP:\r
395                 lcp_received_echo_reply(f, id, inp, len);\r
396                 break;\r
397         \r
398         case DISCREQ:\r
399                 break;\r
400         \r
401         default:\r
402                 return 0;\r
403         }\r
404         return 1;\r
405 }\r
406 \r
407     \r
408 /*\r
409  * lcp_rprotrej - Receive an Protocol-Reject.\r
410  *\r
411  * Figure out which protocol is rejected and inform it.\r
412  */\r
413 static void lcp_rprotrej(fsm *f, u_char *inp, int len)\r
414 {\r
415         int i;\r
416         struct protent *protp;\r
417         u_short prot;\r
418         \r
419         if (len < sizeof (u_short)) {\r
420                 LCPDEBUG((LOG_INFO,\r
421                                 "lcp_rprotrej: Rcvd short Protocol-Reject packet!\n"));\r
422                 return;\r
423         }\r
424         \r
425         GETSHORT(prot, inp);\r
426         \r
427         LCPDEBUG((LOG_INFO,\r
428                         "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!\n",\r
429                         prot));\r
430         \r
431         /*\r
432         * Protocol-Reject packets received in any state other than the LCP\r
433         * OPENED state SHOULD be silently discarded.\r
434         */\r
435         if( f->state != OPENED ){\r
436                 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d\n",\r
437                                 f->state));\r
438                 return;\r
439         }\r
440         \r
441         /*\r
442         * Upcall the proper Protocol-Reject routine.\r
443         */\r
444         for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i)\r
445                 if (protp->protocol == prot && protp->enabled_flag) {\r
446                         (*protp->protrej)(f->unit);\r
447                         return;\r
448                 }\r
449         \r
450         LCPDEBUG((LOG_WARNING, "Protocol-Reject for unsupported protocol 0x%x\n",\r
451                         prot));\r
452 }\r
453 \r
454 \r
455 /*\r
456  * lcp_protrej - A Protocol-Reject was received.\r
457  */\r
458 static void lcp_protrej(int unit)\r
459 {\r
460         (void)unit;\r
461         /*\r
462         * Can't reject LCP!\r
463         */\r
464         LCPDEBUG((LOG_WARNING,\r
465                         "lcp_protrej: Received Protocol-Reject for LCP!\n"));\r
466         fsm_protreject(&lcp_fsm[unit]);\r
467 }\r
468 \r
469 \r
470 /*\r
471  * lcp_resetci - Reset our CI.\r
472  */\r
473 static void lcp_resetci(fsm *f)\r
474 {\r
475         lcp_wantoptions[f->unit].magicnumber = magic();\r
476         lcp_wantoptions[f->unit].numloops = 0;\r
477         lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];\r
478         peer_mru[f->unit] = PPP_MRU;\r
479         auth_reset(f->unit);\r
480 }\r
481 \r
482 \r
483 /*\r
484  * lcp_cilen - Return length of our CI.\r
485  */\r
486 static int lcp_cilen(fsm *f)\r
487 {\r
488         lcp_options *go = &lcp_gotoptions[f->unit];\r
489 \r
490 #define LENCIVOID(neg)  ((neg) ? CILEN_VOID : 0)\r
491 #define LENCICHAP(neg)  ((neg) ? CILEN_CHAP : 0)\r
492 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)\r
493 #define LENCILONG(neg)  ((neg) ? CILEN_LONG : 0)\r
494 #define LENCILQR(neg)   ((neg) ? CILEN_LQR: 0)\r
495 #define LENCICBCP(neg)  ((neg) ? CILEN_CBCP: 0)\r
496         /*\r
497         * NB: we only ask for one of CHAP and UPAP, even if we will\r
498         * accept either.\r
499         */\r
500         return (LENCISHORT(go->neg_mru && go->mru != PPP_DEFMRU) +\r
501                 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl) +\r
502                 LENCICHAP(go->neg_chap) +\r
503                 LENCISHORT(!go->neg_chap && go->neg_upap) +\r
504                 LENCILQR(go->neg_lqr) +\r
505                 LENCICBCP(go->neg_cbcp) +\r
506                 LENCILONG(go->neg_magicnumber) +\r
507                 LENCIVOID(go->neg_pcompression) +\r
508                 LENCIVOID(go->neg_accompression));\r
509 }\r
510 \r
511 \r
512 /*\r
513  * lcp_addci - Add our desired CIs to a packet.\r
514  */\r
515 static void lcp_addci(fsm *f, u_char *ucp, int *lenp)\r
516 {\r
517         lcp_options *go = &lcp_gotoptions[f->unit];\r
518         u_char *start_ucp = ucp;\r
519         \r
520 #define ADDCIVOID(opt, neg) \\r
521         if (neg) { \\r
522             LCPDEBUG((LOG_INFO, "lcp_addci: opt=%d\n", opt)); \\r
523                 PUTCHAR(opt, ucp); \\r
524                 PUTCHAR(CILEN_VOID, ucp); \\r
525         }\r
526 #define ADDCISHORT(opt, neg, val) \\r
527         if (neg) { \\r
528             LCPDEBUG((LOG_INFO, "lcp_addci: INT opt=%d %X\n", opt, val)); \\r
529                 PUTCHAR(opt, ucp); \\r
530                 PUTCHAR(CILEN_SHORT, ucp); \\r
531                 PUTSHORT(val, ucp); \\r
532         }\r
533 #define ADDCICHAP(opt, neg, val, digest) \\r
534         if (neg) { \\r
535             LCPDEBUG((LOG_INFO, "lcp_addci: CHAP opt=%d %X\n", opt, val)); \\r
536                 PUTCHAR(opt, ucp); \\r
537                 PUTCHAR(CILEN_CHAP, ucp); \\r
538                 PUTSHORT(val, ucp); \\r
539                 PUTCHAR(digest, ucp); \\r
540         }\r
541 #define ADDCILONG(opt, neg, val) \\r
542         if (neg) { \\r
543             LCPDEBUG((LOG_INFO, "lcp_addci: L opt=%d %lX\n", opt, val)); \\r
544                 PUTCHAR(opt, ucp); \\r
545                 PUTCHAR(CILEN_LONG, ucp); \\r
546                 PUTLONG(val, ucp); \\r
547         }\r
548 #define ADDCILQR(opt, neg, val) \\r
549         if (neg) { \\r
550             LCPDEBUG((LOG_INFO, "lcp_addci: LQR opt=%d %lX\n", opt, val)); \\r
551                 PUTCHAR(opt, ucp); \\r
552                 PUTCHAR(CILEN_LQR, ucp); \\r
553                 PUTSHORT(PPP_LQR, ucp); \\r
554                 PUTLONG(val, ucp); \\r
555         }\r
556 #define ADDCICHAR(opt, neg, val) \\r
557         if (neg) { \\r
558             LCPDEBUG((LOG_INFO, "lcp_addci: CHAR opt=%d %X '%z'\n", opt, val, val)); \\r
559                 PUTCHAR(opt, ucp); \\r
560                 PUTCHAR(CILEN_CHAR, ucp); \\r
561                 PUTCHAR(val, ucp); \\r
562         }\r
563         \r
564         ADDCISHORT(CI_MRU, go->neg_mru && go->mru != PPP_DEFMRU, go->mru);\r
565         ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl,\r
566                         go->asyncmap);\r
567         ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);\r
568         ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);\r
569         ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);\r
570         ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);\r
571         ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);\r
572         ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);\r
573         ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);\r
574         \r
575         if (ucp - start_ucp != *lenp) {\r
576                 /* this should never happen, because peer_mtu should be 1500 */\r
577                 LCPDEBUG((LOG_ERR, "Bug in lcp_addci: wrong length\n"));\r
578         }\r
579 }\r
580 \r
581 \r
582 /*\r
583  * lcp_ackci - Ack our CIs.\r
584  * This should not modify any state if the Ack is bad.\r
585  *\r
586  * Returns:\r
587  *      0 - Ack was bad.\r
588  *      1 - Ack was good.\r
589  */\r
590 static int lcp_ackci(fsm *f, u_char *p, int len)\r
591 {\r
592         lcp_options *go = &lcp_gotoptions[f->unit];\r
593         u_char cilen, citype, cichar;\r
594         u_short cishort;\r
595         u32_t cilong;\r
596         \r
597         /*\r
598         * CIs must be in exactly the same order that we sent.\r
599         * Check packet length and CI length at each step.\r
600         * If we find any deviations, then this packet is bad.\r
601         */\r
602 #define ACKCIVOID(opt, neg) \\r
603         if (neg) { \\r
604                 if ((len -= CILEN_VOID) < 0) \\r
605                         goto bad; \\r
606                 GETCHAR(citype, p); \\r
607                 GETCHAR(cilen, p); \\r
608                 if (cilen != CILEN_VOID || \\r
609                                 citype != opt) \\r
610                         goto bad; \\r
611         }\r
612 #define ACKCISHORT(opt, neg, val) \\r
613         if (neg) { \\r
614                 if ((len -= CILEN_SHORT) < 0) \\r
615                         goto bad; \\r
616                 GETCHAR(citype, p); \\r
617                 GETCHAR(cilen, p); \\r
618                 if (cilen != CILEN_SHORT || \\r
619                                 citype != opt) \\r
620                         goto bad; \\r
621                 GETSHORT(cishort, p); \\r
622                 if (cishort != val) \\r
623                         goto bad; \\r
624         }\r
625 #define ACKCICHAR(opt, neg, val) \\r
626         if (neg) { \\r
627                 if ((len -= CILEN_CHAR) < 0) \\r
628                         goto bad; \\r
629                 GETCHAR(citype, p); \\r
630                 GETCHAR(cilen, p); \\r
631                 if (cilen != CILEN_CHAR || \\r
632                                 citype != opt) \\r
633                         goto bad; \\r
634                 GETCHAR(cichar, p); \\r
635                 if (cichar != val) \\r
636                         goto bad; \\r
637         }\r
638 #define ACKCICHAP(opt, neg, val, digest) \\r
639         if (neg) { \\r
640                 if ((len -= CILEN_CHAP) < 0) \\r
641                         goto bad; \\r
642                 GETCHAR(citype, p); \\r
643                 GETCHAR(cilen, p); \\r
644                 if (cilen != CILEN_CHAP || \\r
645                                 citype != opt) \\r
646                         goto bad; \\r
647                 GETSHORT(cishort, p); \\r
648                 if (cishort != val) \\r
649                         goto bad; \\r
650                 GETCHAR(cichar, p); \\r
651                 if (cichar != digest) \\r
652                         goto bad; \\r
653         }\r
654 #define ACKCILONG(opt, neg, val) \\r
655         if (neg) { \\r
656                 if ((len -= CILEN_LONG) < 0) \\r
657                         goto bad; \\r
658                 GETCHAR(citype, p); \\r
659                 GETCHAR(cilen, p); \\r
660                 if (cilen != CILEN_LONG || \\r
661                                 citype != opt) \\r
662                         goto bad; \\r
663                 GETLONG(cilong, p); \\r
664                 if (cilong != val) \\r
665                         goto bad; \\r
666         }\r
667 #define ACKCILQR(opt, neg, val) \\r
668         if (neg) { \\r
669                 if ((len -= CILEN_LQR) < 0) \\r
670                         goto bad; \\r
671                 GETCHAR(citype, p); \\r
672                 GETCHAR(cilen, p); \\r
673                 if (cilen != CILEN_LQR || \\r
674                                 citype != opt) \\r
675                         goto bad; \\r
676                 GETSHORT(cishort, p); \\r
677                 if (cishort != PPP_LQR) \\r
678                         goto bad; \\r
679                 GETLONG(cilong, p); \\r
680                 if (cilong != val) \\r
681                         goto bad; \\r
682         }\r
683         \r
684         ACKCISHORT(CI_MRU, go->neg_mru && go->mru != PPP_DEFMRU, go->mru);\r
685         ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl,\r
686                         go->asyncmap);\r
687         ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);\r
688         ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);\r
689         ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);\r
690         ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);\r
691         ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);\r
692         ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);\r
693         ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);\r
694         \r
695         /*\r
696          * If there are any remaining CIs, then this packet is bad.\r
697          */\r
698         if (len != 0)\r
699                 goto bad;\r
700         LCPDEBUG((LOG_INFO, "lcp_acki: Ack\n"));\r
701         return (1);\r
702 bad:\r
703         LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!\n"));\r
704         return (0);\r
705 }\r
706 \r
707 \r
708 /*\r
709  * lcp_nakci - Peer has sent a NAK for some of our CIs.\r
710  * This should not modify any state if the Nak is bad\r
711  * or if LCP is in the OPENED state.\r
712  *\r
713  * Returns:\r
714  *      0 - Nak was bad.\r
715  *      1 - Nak was good.\r
716  */\r
717 static int lcp_nakci(fsm *f, u_char *p, int len)\r
718 {\r
719         lcp_options *go = &lcp_gotoptions[f->unit];\r
720         lcp_options *wo = &lcp_wantoptions[f->unit];\r
721         u_char citype, cichar, *next;\r
722         u_short cishort;\r
723         u32_t cilong;\r
724         lcp_options no;         /* options we've seen Naks for */\r
725         lcp_options try;                /* options to request next time */\r
726         int looped_back = 0;\r
727         int cilen;\r
728         \r
729         BZERO(&no, sizeof(no));\r
730         try = *go;\r
731         \r
732         /*\r
733         * Any Nak'd CIs must be in exactly the same order that we sent.\r
734         * Check packet length and CI length at each step.\r
735         * If we find any deviations, then this packet is bad.\r
736         */\r
737 #define NAKCIVOID(opt, neg, code) \\r
738         if (go->neg && \\r
739                         len >= CILEN_VOID && \\r
740                         p[1] == CILEN_VOID && \\r
741                         p[0] == opt) { \\r
742                 len -= CILEN_VOID; \\r
743                 INCPTR(CILEN_VOID, p); \\r
744                 no.neg = 1; \\r
745                 code \\r
746         }\r
747 #define NAKCICHAP(opt, neg, code) \\r
748         if (go->neg && \\r
749                         len >= CILEN_CHAP && \\r
750                         p[1] == CILEN_CHAP && \\r
751                         p[0] == opt) { \\r
752                 len -= CILEN_CHAP; \\r
753                 INCPTR(2, p); \\r
754                 GETSHORT(cishort, p); \\r
755                 GETCHAR(cichar, p); \\r
756                 no.neg = 1; \\r
757                 code \\r
758         }\r
759 #define NAKCICHAR(opt, neg, code) \\r
760         if (go->neg && \\r
761                         len >= CILEN_CHAR && \\r
762                         p[1] == CILEN_CHAR && \\r
763                         p[0] == opt) { \\r
764                 len -= CILEN_CHAR; \\r
765                 INCPTR(2, p); \\r
766                 GETCHAR(cichar, p); \\r
767                 no.neg = 1; \\r
768                 code \\r
769         }\r
770 #define NAKCISHORT(opt, neg, code) \\r
771         if (go->neg && \\r
772                         len >= CILEN_SHORT && \\r
773                         p[1] == CILEN_SHORT && \\r
774                         p[0] == opt) { \\r
775                 len -= CILEN_SHORT; \\r
776                 INCPTR(2, p); \\r
777                 GETSHORT(cishort, p); \\r
778                 no.neg = 1; \\r
779                 code \\r
780         }\r
781 #define NAKCILONG(opt, neg, code) \\r
782         if (go->neg && \\r
783                         len >= CILEN_LONG && \\r
784                         p[1] == CILEN_LONG && \\r
785                         p[0] == opt) { \\r
786                 len -= CILEN_LONG; \\r
787                 INCPTR(2, p); \\r
788                 GETLONG(cilong, p); \\r
789                 no.neg = 1; \\r
790                 code \\r
791         }\r
792 #define NAKCILQR(opt, neg, code) \\r
793         if (go->neg && \\r
794                         len >= CILEN_LQR && \\r
795                         p[1] == CILEN_LQR && \\r
796                         p[0] == opt) { \\r
797                 len -= CILEN_LQR; \\r
798                 INCPTR(2, p); \\r
799                 GETSHORT(cishort, p); \\r
800                 GETLONG(cilong, p); \\r
801                 no.neg = 1; \\r
802                 code \\r
803         }\r
804         \r
805         /*\r
806         * We don't care if they want to send us smaller packets than\r
807         * we want.  Therefore, accept any MRU less than what we asked for,\r
808         * but then ignore the new value when setting the MRU in the kernel.\r
809         * If they send us a bigger MRU than what we asked, accept it, up to\r
810         * the limit of the default MRU we'd get if we didn't negotiate.\r
811         */\r
812         if (go->neg_mru && go->mru != PPP_DEFMRU) {\r
813                 NAKCISHORT(CI_MRU, neg_mru,\r
814                         if (cishort <= wo->mru || cishort < PPP_DEFMRU)\r
815                                 try.mru = cishort;\r
816                 );\r
817         }\r
818         \r
819         /*\r
820         * Add any characters they want to our (receive-side) asyncmap.\r
821         */\r
822         if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl) {\r
823                 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,\r
824                         try.asyncmap = go->asyncmap | cilong;\r
825                 );\r
826         }\r
827         \r
828         /*\r
829         * If they've nak'd our authentication-protocol, check whether\r
830         * they are proposing a different protocol, or a different\r
831         * hash algorithm for CHAP.\r
832         */\r
833         if ((go->neg_chap || go->neg_upap)\r
834                         && len >= CILEN_SHORT\r
835                         && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {\r
836                 cilen = p[1];\r
837         len -= cilen;\r
838         no.neg_chap = go->neg_chap;\r
839         no.neg_upap = go->neg_upap;\r
840         INCPTR(2, p);\r
841         GETSHORT(cishort, p);\r
842         if (cishort == PPP_PAP && cilen == CILEN_SHORT) {\r
843                 /*\r
844                  * If we were asking for CHAP, they obviously don't want to do it.\r
845                  * If we weren't asking for CHAP, then we were asking for PAP,\r
846                  * in which case this Nak is bad.\r
847                  */\r
848                 if (!go->neg_chap)\r
849                         goto bad;\r
850                 try.neg_chap = 0;\r
851         \r
852         } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {\r
853                 GETCHAR(cichar, p);\r
854                 if (go->neg_chap) {\r
855                         /*\r
856                          * We were asking for CHAP/MD5; they must want a different\r
857                          * algorithm.  If they can't do MD5, we'll have to stop\r
858                          * asking for CHAP.\r
859                          */\r
860                         if (cichar != go->chap_mdtype)\r
861                                 try.neg_chap = 0;\r
862                 } else {\r
863                         /*\r
864                          * Stop asking for PAP if we were asking for it.\r
865                          */\r
866                         try.neg_upap = 0;\r
867                 }\r
868         \r
869         } else {\r
870                 /*\r
871                  * We don't recognize what they're suggesting.\r
872                  * Stop asking for what we were asking for.\r
873                  */\r
874                 if (go->neg_chap)\r
875                         try.neg_chap = 0;\r
876                 else\r
877                         try.neg_upap = 0;\r
878                 p += cilen - CILEN_SHORT;\r
879         }\r
880         }\r
881         \r
882         /*\r
883         * If they can't cope with our link quality protocol, we'll have\r
884         * to stop asking for LQR.  We haven't got any other protocol.\r
885         * If they Nak the reporting period, take their value XXX ?\r
886         */\r
887         NAKCILQR(CI_QUALITY, neg_lqr,\r
888                 if (cishort != PPP_LQR)\r
889                         try.neg_lqr = 0;\r
890                 else\r
891                         try.lqr_period = cilong;\r
892         );\r
893         \r
894         /*\r
895         * Only implementing CBCP...not the rest of the callback options\r
896         */\r
897         NAKCICHAR(CI_CALLBACK, neg_cbcp,\r
898                 try.neg_cbcp = 0;\r
899         );\r
900         \r
901         /*\r
902         * Check for a looped-back line.\r
903         */\r
904         NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,\r
905                 try.magicnumber = magic();\r
906                 looped_back = 1;\r
907         );\r
908         \r
909         /*\r
910         * Peer shouldn't send Nak for protocol compression or\r
911         * address/control compression requests; they should send\r
912         * a Reject instead.  If they send a Nak, treat it as a Reject.\r
913         */\r
914         NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,\r
915                 try.neg_pcompression = 0;\r
916         );\r
917         NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,\r
918                 try.neg_accompression = 0;\r
919         );\r
920         \r
921         /*\r
922         * There may be remaining CIs, if the peer is requesting negotiation\r
923         * on an option that we didn't include in our request packet.\r
924         * If we see an option that we requested, or one we've already seen\r
925         * in this packet, then this packet is bad.\r
926         * If we wanted to respond by starting to negotiate on the requested\r
927         * option(s), we could, but we don't, because except for the\r
928         * authentication type and quality protocol, if we are not negotiating\r
929         * an option, it is because we were told not to.\r
930         * For the authentication type, the Nak from the peer means\r
931         * `let me authenticate myself with you' which is a bit pointless.\r
932         * For the quality protocol, the Nak means `ask me to send you quality\r
933         * reports', but if we didn't ask for them, we don't want them.\r
934         * An option we don't recognize represents the peer asking to\r
935         * negotiate some option we don't support, so ignore it.\r
936         */\r
937         while (len > CILEN_VOID) {\r
938                 GETCHAR(citype, p);\r
939                 GETCHAR(cilen, p);\r
940                 if (cilen < CILEN_VOID || (len -= cilen) < 0)\r
941                         goto bad;\r
942                 next = p + cilen - 2;\r
943                 \r
944                 switch (citype) {\r
945                 case CI_MRU:\r
946                         if ((go->neg_mru && go->mru != PPP_DEFMRU)\r
947                                         || no.neg_mru || cilen != CILEN_SHORT)\r
948                                 goto bad;\r
949                         GETSHORT(cishort, p);\r
950                         if (cishort < PPP_DEFMRU)\r
951                                 try.mru = cishort;\r
952                         break;\r
953                 case CI_ASYNCMAP:\r
954                         if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl)\r
955                                         || no.neg_asyncmap || cilen != CILEN_LONG)\r
956                                 goto bad;\r
957                         break;\r
958                 case CI_AUTHTYPE:\r
959                         if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)\r
960                                 goto bad;\r
961                         break;\r
962                 case CI_MAGICNUMBER:\r
963                         if (go->neg_magicnumber || no.neg_magicnumber ||\r
964                                         cilen != CILEN_LONG)\r
965                                 goto bad;\r
966                         break;\r
967                 case CI_PCOMPRESSION:\r
968                         if (go->neg_pcompression || no.neg_pcompression\r
969                                         || cilen != CILEN_VOID)\r
970                                 goto bad;\r
971                         break;\r
972                 case CI_ACCOMPRESSION:\r
973                         if (go->neg_accompression || no.neg_accompression\r
974                                         || cilen != CILEN_VOID)\r
975                                 goto bad;\r
976                         break;\r
977                 case CI_QUALITY:\r
978                         if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)\r
979                                 goto bad;\r
980                         break;\r
981                 }\r
982                 p = next;\r
983         }\r
984         \r
985         /* If there is still anything left, this packet is bad. */\r
986         if (len != 0)\r
987                 goto bad;\r
988         \r
989         /*\r
990         * OK, the Nak is good.  Now we can update state.\r
991         */\r
992         if (f->state != OPENED) {\r
993                 if (looped_back) {\r
994                         if (++try.numloops >= lcp_loopbackfail) {\r
995                                 LCPDEBUG((LOG_NOTICE, "Serial line is looped back.\n"));\r
996                                 lcp_close(f->unit, "Loopback detected");\r
997                         }\r
998                 } \r
999                 else\r
1000                         try.numloops = 0;\r
1001                 *go = try;\r
1002         }\r
1003         \r
1004         return 1;\r
1005         \r
1006 bad:\r
1007         LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!\n"));\r
1008         return 0;\r
1009 }\r
1010 \r
1011 \r
1012 /*\r
1013  * lcp_rejci - Peer has Rejected some of our CIs.\r
1014  * This should not modify any state if the Reject is bad\r
1015  * or if LCP is in the OPENED state.\r
1016  *\r
1017  * Returns:\r
1018  *      0 - Reject was bad.\r
1019  *      1 - Reject was good.\r
1020  */\r
1021 static int lcp_rejci(fsm *f, u_char *p, int len)\r
1022 {\r
1023         lcp_options *go = &lcp_gotoptions[f->unit];\r
1024         u_char cichar;\r
1025         u_short cishort;\r
1026         u32_t cilong;\r
1027         lcp_options try;                /* options to request next time */\r
1028         \r
1029         try = *go;\r
1030         \r
1031         /*\r
1032         * Any Rejected CIs must be in exactly the same order that we sent.\r
1033         * Check packet length and CI length at each step.\r
1034         * If we find any deviations, then this packet is bad.\r
1035         */\r
1036 #define REJCIVOID(opt, neg) \\r
1037         if (go->neg && \\r
1038                         len >= CILEN_VOID && \\r
1039                         p[1] == CILEN_VOID && \\r
1040                         p[0] == opt) { \\r
1041                 len -= CILEN_VOID; \\r
1042                 INCPTR(CILEN_VOID, p); \\r
1043                 try.neg = 0; \\r
1044                 LCPDEBUG((LOG_INFO, "lcp_rejci: void opt %d rejected\n", opt)); \\r
1045         }\r
1046 #define REJCISHORT(opt, neg, val) \\r
1047         if (go->neg && \\r
1048                         len >= CILEN_SHORT && \\r
1049                         p[1] == CILEN_SHORT && \\r
1050                         p[0] == opt) { \\r
1051                 len -= CILEN_SHORT; \\r
1052                 INCPTR(2, p); \\r
1053                 GETSHORT(cishort, p); \\r
1054                 /* Check rejected value. */ \\r
1055                 if (cishort != val) \\r
1056                         goto bad; \\r
1057                 try.neg = 0; \\r
1058                 LCPDEBUG((LOG_INFO,"lcp_rejci: short opt %d rejected\n", opt)); \\r
1059         }\r
1060 #define REJCICHAP(opt, neg, val, digest) \\r
1061         if (go->neg && \\r
1062                         len >= CILEN_CHAP && \\r
1063                         p[1] == CILEN_CHAP && \\r
1064                         p[0] == opt) { \\r
1065                 len -= CILEN_CHAP; \\r
1066                 INCPTR(2, p); \\r
1067                 GETSHORT(cishort, p); \\r
1068                 GETCHAR(cichar, p); \\r
1069                 /* Check rejected value. */ \\r
1070                 if (cishort != val || cichar != digest) \\r
1071                         goto bad; \\r
1072                 try.neg = 0; \\r
1073                 try.neg_upap = 0; \\r
1074                 LCPDEBUG((LOG_INFO,"lcp_rejci: chap opt %d rejected\n", opt)); \\r
1075         }\r
1076 #define REJCILONG(opt, neg, val) \\r
1077         if (go->neg && \\r
1078                         len >= CILEN_LONG && \\r
1079                         p[1] == CILEN_LONG && \\r
1080                         p[0] == opt) { \\r
1081                 len -= CILEN_LONG; \\r
1082                 INCPTR(2, p); \\r
1083                 GETLONG(cilong, p); \\r
1084                 /* Check rejected value. */ \\r
1085                 if (cilong != val) \\r
1086                         goto bad; \\r
1087                 try.neg = 0; \\r
1088                 LCPDEBUG((LOG_INFO,"lcp_rejci: long opt %d rejected\n", opt)); \\r
1089         }\r
1090 #define REJCILQR(opt, neg, val) \\r
1091         if (go->neg && \\r
1092                         len >= CILEN_LQR && \\r
1093                         p[1] == CILEN_LQR && \\r
1094                         p[0] == opt) { \\r
1095                 len -= CILEN_LQR; \\r
1096                 INCPTR(2, p); \\r
1097                 GETSHORT(cishort, p); \\r
1098                 GETLONG(cilong, p); \\r
1099                 /* Check rejected value. */ \\r
1100                 if (cishort != PPP_LQR || cilong != val) \\r
1101                         goto bad; \\r
1102                 try.neg = 0; \\r
1103                 LCPDEBUG((LOG_INFO,"lcp_rejci: LQR opt %d rejected\n", opt)); \\r
1104         }\r
1105 #define REJCICBCP(opt, neg, val) \\r
1106         if (go->neg && \\r
1107                         len >= CILEN_CBCP && \\r
1108                         p[1] == CILEN_CBCP && \\r
1109                         p[0] == opt) { \\r
1110                 len -= CILEN_CBCP; \\r
1111                 INCPTR(2, p); \\r
1112                 GETCHAR(cichar, p); \\r
1113                 /* Check rejected value. */ \\r
1114                 if (cichar != val) \\r
1115                         goto bad; \\r
1116                 try.neg = 0; \\r
1117                 LCPDEBUG((LOG_INFO,"lcp_rejci: Callback opt %d rejected\n", opt)); \\r
1118         }\r
1119         \r
1120         REJCISHORT(CI_MRU, neg_mru, go->mru);\r
1121         REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);\r
1122         REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);\r
1123         if (!go->neg_chap) {\r
1124                 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);\r
1125         }\r
1126         REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);\r
1127         REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);\r
1128         REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);\r
1129         REJCIVOID(CI_PCOMPRESSION, neg_pcompression);\r
1130         REJCIVOID(CI_ACCOMPRESSION, neg_accompression);\r
1131         \r
1132         /*\r
1133         * If there are any remaining CIs, then this packet is bad.\r
1134         */\r
1135         if (len != 0)\r
1136                 goto bad;\r
1137         /*\r
1138         * Now we can update state.\r
1139         */\r
1140         if (f->state != OPENED)\r
1141                 *go = try;\r
1142         return 1;\r
1143         \r
1144 bad:\r
1145         LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!\n"));\r
1146         return 0;\r
1147 }\r
1148 \r
1149 \r
1150 /*\r
1151  * lcp_reqci - Check the peer's requested CIs and send appropriate response.\r
1152  *\r
1153  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified\r
1154  * appropriately.  If reject_if_disagree is non-zero, doesn't return\r
1155  * CONFNAK; returns CONFREJ if it can't return CONFACK.\r
1156  */\r
1157 static int lcp_reqci(fsm *f, \r
1158                                                 u_char *inp,            /* Requested CIs */\r
1159                                                 int *lenp,                      /* Length of requested CIs */\r
1160                                                 int reject_if_disagree)\r
1161 {\r
1162         lcp_options *go = &lcp_gotoptions[f->unit];\r
1163         lcp_options *ho = &lcp_hisoptions[f->unit];\r
1164         lcp_options *ao = &lcp_allowoptions[f->unit];\r
1165         u_char *cip, *next;                     /* Pointer to current and next CIs */\r
1166         int cilen, citype, cichar;      /* Parsed len, type, char value */\r
1167         u_short cishort;                        /* Parsed short value */\r
1168         u32_t cilong;                   /* Parse long value */\r
1169         int rc = CONFACK;                       /* Final packet return code */\r
1170         int orc;                                        /* Individual option return code */\r
1171         u_char *p;                                      /* Pointer to next char to parse */\r
1172         u_char *rejp;                           /* Pointer to next char in reject frame */\r
1173         u_char *nakp;                           /* Pointer to next char in Nak frame */\r
1174         int l = *lenp;                          /* Length left */\r
1175 #if TRACELCP > 0\r
1176         char traceBuf[80];\r
1177         int traceNdx = 0;\r
1178 #endif\r
1179         \r
1180         /*\r
1181          * Reset all his options.\r
1182          */\r
1183         BZERO(ho, sizeof(*ho));\r
1184         \r
1185         /*\r
1186          * Process all his options.\r
1187          */\r
1188         next = inp;\r
1189         nakp = nak_buffer;\r
1190         rejp = inp;\r
1191         while (l) {\r
1192                 orc = CONFACK;                  /* Assume success */\r
1193                 cip = p = next;                 /* Remember begining of CI */\r
1194                 if (l < 2 ||                    /* Not enough data for CI header or */\r
1195                                 p[1] < 2 ||                     /*  CI length too small or */\r
1196                                 p[1] > l) {                     /*  CI length too big? */\r
1197                         LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!\n"));\r
1198                         orc = CONFREJ;          /* Reject bad CI */\r
1199                         cilen = l;                      /* Reject till end of packet */\r
1200                         l = 0;                  /* Don't loop again */\r
1201                         citype = 0;\r
1202                         goto endswitch;\r
1203                 }\r
1204                 GETCHAR(citype, p);             /* Parse CI type */\r
1205                 GETCHAR(cilen, p);              /* Parse CI length */\r
1206                 l -= cilen;                     /* Adjust remaining length */\r
1207                 next += cilen;                  /* Step to next CI */\r
1208                 \r
1209                 switch (citype) {               /* Check CI type */\r
1210                 case CI_MRU:\r
1211                         if (!ao->neg_mru) {             /* Allow option? */\r
1212                                 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject MRU - not allowed\n"));\r
1213                                 orc = CONFREJ;          /* Reject CI */\r
1214                                 break;\r
1215                         } else if (cilen != CILEN_SHORT) {      /* Check CI length */\r
1216                                 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject MRU - bad length\n"));\r
1217                                 orc = CONFREJ;          /* Reject CI */\r
1218                                 break;\r
1219                         }\r
1220                         GETSHORT(cishort, p);   /* Parse MRU */\r
1221                         \r
1222                         /*\r
1223                          * He must be able to receive at least our minimum.\r
1224                          * No need to check a maximum.  If he sends a large number,\r
1225                          * we'll just ignore it.\r
1226                          */\r
1227                         if (cishort < PPP_MINMRU) {\r
1228                                 LCPDEBUG((LOG_INFO, "lcp_reqci: Nak - MRU too small\n"));\r
1229                                 orc = CONFNAK;          /* Nak CI */\r
1230                                 PUTCHAR(CI_MRU, nakp);\r
1231                                 PUTCHAR(CILEN_SHORT, nakp);\r
1232                                 PUTSHORT(PPP_MINMRU, nakp);     /* Give him a hint */\r
1233                                 break;\r
1234                         }\r
1235                         ho->neg_mru = 1;                /* Remember he sent MRU */\r
1236                         ho->mru = cishort;              /* And remember value */\r
1237 #if TRACELCP > 0\r
1238                         sprintf(&traceBuf[traceNdx], " MRU %d", cishort);\r
1239                         traceNdx = strlen(traceBuf);\r
1240 #endif\r
1241                         break;\r
1242                 \r
1243                 case CI_ASYNCMAP:\r
1244                         if (!ao->neg_asyncmap) {\r
1245                                 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject ASYNCMAP not allowed\n"));\r
1246                                 orc = CONFREJ;\r
1247                                 break;\r
1248                         } else if (cilen != CILEN_LONG) {\r
1249                                 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject ASYNCMAP bad length\n"));\r
1250                                 orc = CONFREJ;\r
1251                                 break;\r
1252                         }\r
1253                         GETLONG(cilong, p);\r
1254                         \r
1255                         /*\r
1256                          * Asyncmap must have set at least the bits\r
1257                          * which are set in lcp_allowoptions[unit].asyncmap.\r
1258                          */\r
1259                         if ((ao->asyncmap & ~cilong) != 0) {\r
1260                                 LCPDEBUG((LOG_INFO, "lcp_reqci: Nak ASYNCMAP %lX missing %lX\n", \r
1261                                                         cilong, ao->asyncmap));\r
1262                                 orc = CONFNAK;\r
1263                                 PUTCHAR(CI_ASYNCMAP, nakp);\r
1264                                 PUTCHAR(CILEN_LONG, nakp);\r
1265                                 PUTLONG(ao->asyncmap | cilong, nakp);\r
1266                                 break;\r
1267                         }\r
1268                         ho->neg_asyncmap = 1;\r
1269                         ho->asyncmap = cilong;\r
1270 #if TRACELCP > 0\r
1271                         sprintf(&traceBuf[traceNdx], " ASYNCMAP=%lX", cilong);\r
1272                         traceNdx = strlen(traceBuf);\r
1273 #endif\r
1274                         break;\r
1275                 \r
1276                 case CI_AUTHTYPE:\r
1277                         if (cilen < CILEN_SHORT) {\r
1278                                 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject AUTHTYPE missing arg\n"));\r
1279                                 orc = CONFREJ;\r
1280                                 break;\r
1281                         } else if (!(ao->neg_upap || ao->neg_chap)) {\r
1282                                 /*\r
1283                                  * Reject the option if we're not willing to authenticate.\r
1284                                  */\r
1285                                 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject AUTHTYPE not allowed\n"));\r
1286                                 orc = CONFREJ;\r
1287                                 break;\r
1288                         }\r
1289                         GETSHORT(cishort, p);\r
1290                         \r
1291                         /*\r
1292                          * Authtype must be UPAP or CHAP.\r
1293                          *\r
1294                          * Note: if both ao->neg_upap and ao->neg_chap are set,\r
1295                          * and the peer sends a Configure-Request with two\r
1296                          * authenticate-protocol requests, one for CHAP and one\r
1297                          * for UPAP, then we will reject the second request.\r
1298                          * Whether we end up doing CHAP or UPAP depends then on\r
1299                          * the ordering of the CIs in the peer's Configure-Request.\r
1300                          */\r
1301                         \r
1302                         if (cishort == PPP_PAP) {\r
1303                                 if (ho->neg_chap) {     /* we've already accepted CHAP */\r
1304                                         LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE PAP already accepted\n"));\r
1305                                         orc = CONFREJ;\r
1306                                         break;\r
1307                                 } else if (cilen != CILEN_SHORT) {\r
1308                                         LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE PAP bad len\n"));\r
1309                                         orc = CONFREJ;\r
1310                                         break;\r
1311                                 }\r
1312                                 if (!ao->neg_upap) {    /* we don't want to do PAP */\r
1313                                         LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE PAP not allowed\n"));\r
1314                                         orc = CONFNAK;  /* NAK it and suggest CHAP */\r
1315                                         PUTCHAR(CI_AUTHTYPE, nakp);\r
1316                                         PUTCHAR(CILEN_CHAP, nakp);\r
1317                                         PUTSHORT(PPP_CHAP, nakp);\r
1318                                         PUTCHAR(ao->chap_mdtype, nakp);\r
1319                                         break;\r
1320                                 }\r
1321                                 ho->neg_upap = 1;\r
1322 #if TRACELCP > 0\r
1323                                 sprintf(&traceBuf[traceNdx], " PAP (%X)", cishort);\r
1324                                 traceNdx = strlen(traceBuf);\r
1325 #endif\r
1326                                 break;\r
1327                         }\r
1328                         if (cishort == PPP_CHAP) {\r
1329                                 if (ho->neg_upap) {     /* we've already accepted PAP */\r
1330                                         LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE CHAP accepted PAP\n"));\r
1331                                         orc = CONFREJ;\r
1332                                         break;\r
1333                                 } else if (cilen != CILEN_CHAP) {\r
1334                                         LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE CHAP bad len\n"));\r
1335                                         orc = CONFREJ;\r
1336                                         break;\r
1337                                 }\r
1338                                 if (!ao->neg_chap) {    /* we don't want to do CHAP */\r
1339                                         LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE CHAP not allowed\n"));\r
1340                                         orc = CONFNAK;  /* NAK it and suggest PAP */\r
1341                                         PUTCHAR(CI_AUTHTYPE, nakp);\r
1342                                         PUTCHAR(CILEN_SHORT, nakp);\r
1343                                         PUTSHORT(PPP_PAP, nakp);\r
1344                                         break;\r
1345                                 }\r
1346                                 GETCHAR(cichar, p);     /* get digest type*/\r
1347                                 if (cichar != CHAP_DIGEST_MD5\r
1348 #ifdef CHAPMS\r
1349                                                 && cichar != CHAP_MICROSOFT\r
1350 #endif\r
1351                                 ) {\r
1352                                         LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE CHAP digest=%d\n", cichar));\r
1353                                         orc = CONFNAK;\r
1354                                         PUTCHAR(CI_AUTHTYPE, nakp);\r
1355                                         PUTCHAR(CILEN_CHAP, nakp);\r
1356                                         PUTSHORT(PPP_CHAP, nakp);\r
1357                                         PUTCHAR(ao->chap_mdtype, nakp);\r
1358                                         break;\r
1359                                 }\r
1360 #if TRACELCP > 0\r
1361                                 sprintf(&traceBuf[traceNdx], " CHAP %X,%d", cishort, cichar);\r
1362                                 traceNdx = strlen(traceBuf);\r
1363 #endif\r
1364                                 ho->chap_mdtype = cichar; /* save md type */\r
1365                                 ho->neg_chap = 1;\r
1366                                 break;\r
1367                         }\r
1368                         \r
1369                         /*\r
1370                          * We don't recognize the protocol they're asking for.\r
1371                          * Nak it with something we're willing to do.\r
1372                          * (At this point we know ao->neg_upap || ao->neg_chap.)\r
1373                          */\r
1374                         orc = CONFNAK;\r
1375                         PUTCHAR(CI_AUTHTYPE, nakp);\r
1376                         if (ao->neg_chap) {\r
1377                                 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE %d req CHAP\n", cishort));\r
1378                                 PUTCHAR(CILEN_CHAP, nakp);\r
1379                                 PUTSHORT(PPP_CHAP, nakp);\r
1380                                 PUTCHAR(ao->chap_mdtype, nakp);\r
1381                         } \r
1382                         else {\r
1383                                 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE %d req PAP\n", cishort));\r
1384                                 PUTCHAR(CILEN_SHORT, nakp);\r
1385                                 PUTSHORT(PPP_PAP, nakp);\r
1386                         }\r
1387                         break;\r
1388                 \r
1389                 case CI_QUALITY:\r
1390                         GETSHORT(cishort, p);\r
1391                         GETLONG(cilong, p);\r
1392 #if TRACELCP > 0\r
1393                         sprintf(&traceBuf[traceNdx], " QUALITY (%x %x)", cishort, (unsigned int) cilong);\r
1394                         traceNdx = strlen(traceBuf);\r
1395 #endif\r
1396 \r
1397                         if (!ao->neg_lqr ||\r
1398                                         cilen != CILEN_LQR) {\r
1399                                 orc = CONFREJ;\r
1400                                 break;\r
1401                         }\r
1402                         \r
1403                         /*\r
1404                          * Check the protocol and the reporting period.\r
1405                          * XXX When should we Nak this, and what with?\r
1406                          */\r
1407                         if (cishort != PPP_LQR) {\r
1408                                 orc = CONFNAK;\r
1409                                 PUTCHAR(CI_QUALITY, nakp);\r
1410                                 PUTCHAR(CILEN_LQR, nakp);\r
1411                                 PUTSHORT(PPP_LQR, nakp);\r
1412                                 PUTLONG(ao->lqr_period, nakp);\r
1413                                 break;\r
1414                         }\r
1415                         break;\r
1416                 \r
1417                 case CI_MAGICNUMBER:\r
1418                         if (!(ao->neg_magicnumber || go->neg_magicnumber) ||\r
1419                                         cilen != CILEN_LONG) {\r
1420                                 orc = CONFREJ;\r
1421                                 break;\r
1422                         }\r
1423                         GETLONG(cilong, p);\r
1424 #if TRACELCP > 0\r
1425                         sprintf(&traceBuf[traceNdx], " MAGICNUMBER (%lX)", cilong);\r
1426                         traceNdx = strlen(traceBuf);\r
1427 #endif\r
1428 \r
1429                         /*\r
1430                          * He must have a different magic number.\r
1431                          */\r
1432                         if (go->neg_magicnumber &&\r
1433                                         cilong == go->magicnumber) {\r
1434                                 cilong = magic();       /* Don't put magic() inside macro! */\r
1435                                 orc = CONFNAK;\r
1436                                 PUTCHAR(CI_MAGICNUMBER, nakp);\r
1437                                 PUTCHAR(CILEN_LONG, nakp);\r
1438                                 PUTLONG(cilong, nakp);\r
1439                                 break;\r
1440                         }\r
1441                         ho->neg_magicnumber = 1;\r
1442                         ho->magicnumber = cilong;\r
1443                         break;\r
1444                 \r
1445                 \r
1446                 case CI_PCOMPRESSION:\r
1447 #if TRACELCP > 0\r
1448                         sprintf(&traceBuf[traceNdx], " PCOMPRESSION");\r
1449                         traceNdx = strlen(traceBuf);\r
1450 #endif\r
1451                         if (!ao->neg_pcompression ||\r
1452                                         cilen != CILEN_VOID) {\r
1453                                 orc = CONFREJ;\r
1454                                 break;\r
1455                         }\r
1456                         ho->neg_pcompression = 1;\r
1457                         break;\r
1458                 \r
1459                 case CI_ACCOMPRESSION:\r
1460 #if TRACELCP > 0\r
1461                         sprintf(&traceBuf[traceNdx], " ACCOMPRESSION");\r
1462                         traceNdx = strlen(traceBuf);\r
1463 #endif\r
1464                         if (!ao->neg_accompression ||\r
1465                                         cilen != CILEN_VOID) {\r
1466                                 orc = CONFREJ;\r
1467                                 break;\r
1468                         }\r
1469                         ho->neg_accompression = 1;\r
1470                         break;\r
1471                 \r
1472                 case CI_MRRU:\r
1473 #if TRACELCP > 0\r
1474                         sprintf(&traceBuf[traceNdx], " CI_MRRU");\r
1475                         traceNdx = strlen(traceBuf);\r
1476 #endif\r
1477                         orc = CONFREJ;\r
1478                         break;\r
1479                 \r
1480                 case CI_SSNHF:\r
1481 #if TRACELCP > 0\r
1482                         sprintf(&traceBuf[traceNdx], " CI_SSNHF");\r
1483                         traceNdx = strlen(traceBuf);\r
1484 #endif\r
1485                         orc = CONFREJ;\r
1486                         break;\r
1487                 \r
1488                 case CI_EPDISC:\r
1489 #if TRACELCP > 0\r
1490                         sprintf(&traceBuf[traceNdx], " CI_EPDISC");\r
1491                         traceNdx = strlen(traceBuf);\r
1492 #endif\r
1493                         orc = CONFREJ;\r
1494                         break;\r
1495                 \r
1496                 default:\r
1497 #if TRACELCP\r
1498                         sprintf(&traceBuf[traceNdx], " unknown %d", citype);\r
1499                         traceNdx = strlen(traceBuf);\r
1500 #endif\r
1501                         orc = CONFREJ;\r
1502                         break;\r
1503                 }\r
1504                 \r
1505         endswitch:\r
1506 #if TRACELCP\r
1507                 if (traceNdx >= 80 - 32) {\r
1508                         LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd%s\n", traceBuf));\r
1509                         traceNdx = 0;\r
1510                 }\r
1511 #endif\r
1512                 if (orc == CONFACK &&           /* Good CI */\r
1513                                 rc != CONFACK)          /*  but prior CI wasnt? */\r
1514                         continue;                       /* Don't send this one */\r
1515                 \r
1516                 if (orc == CONFNAK) {           /* Nak this CI? */\r
1517                         if (reject_if_disagree  /* Getting fed up with sending NAKs? */\r
1518                                         && citype != CI_MAGICNUMBER) {\r
1519                                 orc = CONFREJ;          /* Get tough if so */\r
1520                         } \r
1521                         else {\r
1522                                 if (rc == CONFREJ)      /* Rejecting prior CI? */\r
1523                                         continue;               /* Don't send this one */\r
1524                                 rc = CONFNAK;\r
1525                         }\r
1526                 }\r
1527                 if (orc == CONFREJ) {           /* Reject this CI */\r
1528                         rc = CONFREJ;\r
1529                         if (cip != rejp)                /* Need to move rejected CI? */\r
1530                                 BCOPY(cip, rejp, cilen); /* Move it */\r
1531                         INCPTR(cilen, rejp);    /* Update output pointer */\r
1532                 }\r
1533         }\r
1534         \r
1535         /*\r
1536          * If we wanted to send additional NAKs (for unsent CIs), the\r
1537          * code would go here.  The extra NAKs would go at *nakp.\r
1538          * At present there are no cases where we want to ask the\r
1539          * peer to negotiate an option.\r
1540          */\r
1541         \r
1542         switch (rc) {\r
1543         case CONFACK:\r
1544                 *lenp = (int)(next - inp);\r
1545                 break;\r
1546         case CONFNAK:\r
1547                 /*\r
1548                  * Copy the Nak'd options from the nak_buffer to the caller's buffer.\r
1549                  */\r
1550                 *lenp = (int)(nakp - nak_buffer);\r
1551                 BCOPY(nak_buffer, inp, *lenp);\r
1552                 break;\r
1553         case CONFREJ:\r
1554                 *lenp = (int)(rejp - inp);\r
1555                 break;\r
1556         }\r
1557         \r
1558 #if TRACELCP > 0\r
1559         if (traceNdx > 0) {\r
1560                 LCPDEBUG((LOG_INFO, "lcp_reqci: %s\n", traceBuf));\r
1561         }\r
1562 #endif\r
1563         LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.\n", CODENAME(rc)));\r
1564         return (rc);                    /* Return final code */\r
1565 }\r
1566 \r
1567 \r
1568 /*\r
1569  * lcp_up - LCP has come UP.\r
1570  */\r
1571 static void lcp_up(fsm *f)\r
1572 {\r
1573         lcp_options *wo = &lcp_wantoptions[f->unit];\r
1574         lcp_options *ho = &lcp_hisoptions[f->unit];\r
1575         lcp_options *go = &lcp_gotoptions[f->unit];\r
1576         lcp_options *ao = &lcp_allowoptions[f->unit];\r
1577         \r
1578         if (!go->neg_magicnumber)\r
1579                 go->magicnumber = 0;\r
1580         if (!ho->neg_magicnumber)\r
1581                 ho->magicnumber = 0;\r
1582         \r
1583         /*\r
1584         * Set our MTU to the smaller of the MTU we wanted and\r
1585         * the MRU our peer wanted.  If we negotiated an MRU,\r
1586         * set our MRU to the larger of value we wanted and\r
1587         * the value we got in the negotiation.\r
1588         */\r
1589         ppp_send_config(f->unit, LWIP_MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),\r
1590                                 (ho->neg_asyncmap? ho->asyncmap: 0xffffffffl),\r
1591                                 ho->neg_pcompression, ho->neg_accompression);\r
1592         /*\r
1593         * If the asyncmap hasn't been negotiated, we really should\r
1594         * set the receive asyncmap to ffffffff, but we set it to 0\r
1595         * for backwards contemptibility.\r
1596         */\r
1597         ppp_recv_config(f->unit, (go->neg_mru? LWIP_MAX(wo->mru, go->mru): PPP_MRU),\r
1598                                 (go->neg_asyncmap? go->asyncmap: 0x00000000),\r
1599                                 go->neg_pcompression, go->neg_accompression);\r
1600         \r
1601         if (ho->neg_mru)\r
1602                 peer_mru[f->unit] = ho->mru;\r
1603         \r
1604         lcp_echo_lowerup(f->unit);  /* Enable echo messages */\r
1605         \r
1606         link_established(f->unit);\r
1607 }\r
1608 \r
1609 \r
1610 /*\r
1611  * lcp_down - LCP has gone DOWN.\r
1612  *\r
1613  * Alert other protocols.\r
1614  */\r
1615 static void lcp_down(fsm *f)\r
1616 {\r
1617         lcp_options *go = &lcp_gotoptions[f->unit];\r
1618         \r
1619         lcp_echo_lowerdown(f->unit);\r
1620         \r
1621         link_down(f->unit);\r
1622         \r
1623         ppp_send_config(f->unit, PPP_MRU, 0xffffffffl, 0, 0);\r
1624         ppp_recv_config(f->unit, PPP_MRU,\r
1625                                 (go->neg_asyncmap? go->asyncmap: 0x00000000),\r
1626                                 go->neg_pcompression, go->neg_accompression);\r
1627         peer_mru[f->unit] = PPP_MRU;\r
1628 }\r
1629 \r
1630 \r
1631 /*\r
1632  * lcp_starting - LCP needs the lower layer up.\r
1633  */\r
1634 static void lcp_starting(fsm *f)\r
1635 {\r
1636         link_required(f->unit);\r
1637 }\r
1638 \r
1639 \r
1640 /*\r
1641  * lcp_finished - LCP has finished with the lower layer.\r
1642  */\r
1643 static void lcp_finished(fsm *f)\r
1644 {\r
1645         link_terminated(f->unit);\r
1646 }\r
1647 \r
1648 \r
1649 #if 0\r
1650 /*\r
1651  * print_string - print a readable representation of a string using\r
1652  * printer.\r
1653  */\r
1654 static void print_string(\r
1655     char *p,\r
1656     int len,\r
1657     void (*printer) (void *, char *, ...),\r
1658     void *arg\r
1659 )\r
1660 {\r
1661     int c;\r
1662     \r
1663     printer(arg, "\"");\r
1664     for (; len > 0; --len) {\r
1665         c = *p++;\r
1666         if (' ' <= c && c <= '~') {\r
1667             if (c == '\\' || c == '"')\r
1668                 printer(arg, "\\");\r
1669             printer(arg, "%c", c);\r
1670         } else {\r
1671             switch (c) {\r
1672             case '\n':\r
1673                 printer(arg, "\\n");\r
1674                 break;\r
1675             case '\r':\r
1676                 printer(arg, "\\r");\r
1677                 break;\r
1678             case '\t':\r
1679                 printer(arg, "\\t");\r
1680                 break;\r
1681             default:\r
1682                 printer(arg, "\\%.3o", c);\r
1683             }\r
1684         }\r
1685     }\r
1686     printer(arg, "\"");\r
1687 }\r
1688 \r
1689 \r
1690 /*\r
1691  * lcp_printpkt - print the contents of an LCP packet.\r
1692  */\r
1693 static char *lcp_codenames[] = {\r
1694         "ConfReq", "ConfAck", "ConfNak", "ConfRej",\r
1695         "TermReq", "TermAck", "CodeRej", "ProtRej",\r
1696         "EchoReq", "EchoRep", "DiscReq"\r
1697 };\r
1698 \r
1699 static int lcp_printpkt(\r
1700         u_char *p,\r
1701         int plen,\r
1702         void (*printer) (void *, char *, ...),\r
1703         void *arg\r
1704 )\r
1705 {\r
1706         int code, id, len, olen;\r
1707         u_char *pstart, *optend;\r
1708         u_short cishort;\r
1709         u32_t cilong;\r
1710         \r
1711         if (plen < HEADERLEN)\r
1712                 return 0;\r
1713         pstart = p;\r
1714         GETCHAR(code, p);\r
1715         GETCHAR(id, p);\r
1716         GETSHORT(len, p);\r
1717         if (len < HEADERLEN || len > plen)\r
1718                 return 0;\r
1719         \r
1720         if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))\r
1721                 printer(arg, " %s", lcp_codenames[code-1]);\r
1722         else\r
1723                 printer(arg, " code=0x%x", code);\r
1724         printer(arg, " id=0x%x", id);\r
1725         len -= HEADERLEN;\r
1726         switch (code) {\r
1727         case CONFREQ:\r
1728         case CONFACK:\r
1729         case CONFNAK:\r
1730         case CONFREJ:\r
1731                 /* print option list */\r
1732                 while (len >= 2) {\r
1733                         GETCHAR(code, p);\r
1734                         GETCHAR(olen, p);\r
1735                         p -= 2;\r
1736                         if (olen < 2 || olen > len) {\r
1737                                 break;\r
1738                         }\r
1739                         printer(arg, " <");\r
1740                         len -= olen;\r
1741                         optend = p + olen;\r
1742                         switch (code) {\r
1743                         case CI_MRU:\r
1744                                 if (olen == CILEN_SHORT) {\r
1745                                         p += 2;\r
1746                                         GETSHORT(cishort, p);\r
1747                                         printer(arg, "mru %d", cishort);\r
1748                                 }\r
1749                                 break;\r
1750                         case CI_ASYNCMAP:\r
1751                                 if (olen == CILEN_LONG) {\r
1752                                         p += 2;\r
1753                                         GETLONG(cilong, p);\r
1754                                         printer(arg, "asyncmap 0x%lx", cilong);\r
1755                                 }\r
1756                                 break;\r
1757                         case CI_AUTHTYPE:\r
1758                                 if (olen >= CILEN_SHORT) {\r
1759                                         p += 2;\r
1760                                         printer(arg, "auth ");\r
1761                                         GETSHORT(cishort, p);\r
1762                                         switch (cishort) {\r
1763                                         case PPP_PAP:\r
1764                                                 printer(arg, "pap");\r
1765                                                 break;\r
1766                                         case PPP_CHAP:\r
1767                                                 printer(arg, "chap");\r
1768                                                 break;\r
1769                                         default:\r
1770                                                 printer(arg, "0x%x", cishort);\r
1771                                         }\r
1772                                 }\r
1773                                 break;\r
1774                         case CI_QUALITY:\r
1775                                 if (olen >= CILEN_SHORT) {\r
1776                                         p += 2;\r
1777                                         printer(arg, "quality ");\r
1778                                         GETSHORT(cishort, p);\r
1779                                         switch (cishort) {\r
1780                                         case PPP_LQR:\r
1781                                                 printer(arg, "lqr");\r
1782                                                 break;\r
1783                                         default:\r
1784                                                 printer(arg, "0x%x", cishort);\r
1785                                         }\r
1786                                 }\r
1787                                 break;\r
1788                         case CI_CALLBACK:\r
1789                                 if (olen >= CILEN_CHAR) {\r
1790                                         p += 2;\r
1791                                         printer(arg, "callback ");\r
1792                                         GETSHORT(cishort, p);\r
1793                                         switch (cishort) {\r
1794                                         case CBCP_OPT:\r
1795                                                 printer(arg, "CBCP");\r
1796                                                 break;\r
1797                                         default:\r
1798                                                 printer(arg, "0x%x", cishort);\r
1799                                         }\r
1800                                 }\r
1801                                 break;\r
1802                         case CI_MAGICNUMBER:\r
1803                                 if (olen == CILEN_LONG) {\r
1804                                         p += 2;\r
1805                                         GETLONG(cilong, p);\r
1806                                         printer(arg, "magic 0x%x", cilong);\r
1807                                 }\r
1808                                 break;\r
1809                         case CI_PCOMPRESSION:\r
1810                                 if (olen == CILEN_VOID) {\r
1811                                         p += 2;\r
1812                                         printer(arg, "pcomp");\r
1813                                 }\r
1814                                 break;\r
1815                         case CI_ACCOMPRESSION:\r
1816                                 if (olen == CILEN_VOID) {\r
1817                                         p += 2;\r
1818                                         printer(arg, "accomp");\r
1819                                 }\r
1820                                 break;\r
1821                         }\r
1822                         while (p < optend) {\r
1823                                 GETCHAR(code, p);\r
1824                                 printer(arg, " %.2x", code);\r
1825                         }\r
1826                         printer(arg, ">");\r
1827                 }\r
1828                 break;\r
1829         \r
1830         case TERMACK:\r
1831         case TERMREQ:\r
1832                 if (len > 0 && *p >= ' ' && *p < 0x7f) {\r
1833                         printer(arg, " ");\r
1834                         print_string((char*)p, len, printer, arg);\r
1835                         p += len;\r
1836                         len = 0;\r
1837                 }\r
1838                 break;\r
1839         \r
1840         case ECHOREQ:\r
1841         case ECHOREP:\r
1842         case DISCREQ:\r
1843                 if (len >= 4) {\r
1844                         GETLONG(cilong, p);\r
1845                         printer(arg, " magic=0x%x", cilong);\r
1846                         p += 4;\r
1847                         len -= 4;\r
1848                 }\r
1849                 break;\r
1850         }\r
1851         \r
1852         /* print the rest of the bytes in the packet */\r
1853         for (; len > 0; --len) {\r
1854                 GETCHAR(code, p);\r
1855                 printer(arg, " %.2x", code);\r
1856         }\r
1857         \r
1858         return (int)(p - pstart);\r
1859 }\r
1860 #endif\r
1861 \r
1862 /*\r
1863  * Time to shut down the link because there is nothing out there.\r
1864  */\r
1865 \r
1866 static void LcpLinkFailure (fsm *f)\r
1867 {\r
1868         if (f->state == OPENED) {\r
1869                 LCPDEBUG((LOG_INFO, "No response to %d echo-requests\n", lcp_echos_pending));\r
1870                 LCPDEBUG((LOG_NOTICE, "Serial link appears to be disconnected.\n"));\r
1871                 lcp_close(f->unit, "Peer not responding");\r
1872         }\r
1873 }\r
1874 \r
1875 /*\r
1876  * Timer expired for the LCP echo requests from this process.\r
1877  */\r
1878 \r
1879 static void LcpEchoCheck (fsm *f)\r
1880 {\r
1881         LcpSendEchoRequest (f);\r
1882         \r
1883         /*\r
1884          * Start the timer for the next interval.\r
1885          */\r
1886         LWIP_ASSERT("lcp_echo_timer_running == 0", lcp_echo_timer_running == 0);\r
1887 \r
1888         TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);\r
1889         lcp_echo_timer_running = 1;\r
1890 }\r
1891 \r
1892 /*\r
1893  * LcpEchoTimeout - Timer expired on the LCP echo\r
1894  */\r
1895 \r
1896 static void LcpEchoTimeout (void *arg)\r
1897 {\r
1898         if (lcp_echo_timer_running != 0) {\r
1899                 lcp_echo_timer_running = 0;\r
1900                 LcpEchoCheck ((fsm *) arg);\r
1901         }\r
1902 }\r
1903 \r
1904 /*\r
1905  * LcpEchoReply - LCP has received a reply to the echo\r
1906  */\r
1907 static void lcp_received_echo_reply (fsm *f, int id, u_char *inp, int len)\r
1908 {\r
1909         u32_t magic;\r
1910         \r
1911         (void)id;\r
1912 \r
1913         /* Check the magic number - don't count replies from ourselves. */\r
1914         if (len < 4) {\r
1915                 LCPDEBUG((LOG_WARNING, "lcp: received short Echo-Reply, length %d\n", len));\r
1916                 return;\r
1917         }\r
1918         GETLONG(magic, inp);\r
1919         if (lcp_gotoptions[f->unit].neg_magicnumber\r
1920                         && magic == lcp_gotoptions[f->unit].magicnumber) {\r
1921                 LCPDEBUG((LOG_WARNING, "appear to have received our own echo-reply!\n"));\r
1922                 return;\r
1923         }\r
1924         \r
1925         /* Reset the number of outstanding echo frames */\r
1926         lcp_echos_pending = 0;\r
1927 }\r
1928 \r
1929 /*\r
1930  * LcpSendEchoRequest - Send an echo request frame to the peer\r
1931  */\r
1932 \r
1933 static void LcpSendEchoRequest (fsm *f)\r
1934 {\r
1935         u32_t lcp_magic;\r
1936         u_char pkt[4], *pktp;\r
1937         \r
1938         /*\r
1939         * Detect the failure of the peer at this point.\r
1940         */\r
1941         if (lcp_echo_fails != 0) {\r
1942                 if (lcp_echos_pending++ >= lcp_echo_fails) {\r
1943                         LcpLinkFailure(f);\r
1944                         lcp_echos_pending = 0;\r
1945                 }\r
1946         }\r
1947         \r
1948         /*\r
1949         * Make and send the echo request frame.\r
1950         */\r
1951         if (f->state == OPENED) {\r
1952                 lcp_magic = lcp_gotoptions[f->unit].magicnumber;\r
1953                 pktp = pkt;\r
1954                 PUTLONG(lcp_magic, pktp);\r
1955                 fsm_sdata(f, ECHOREQ, (u_char)(lcp_echo_number++ & 0xFF), pkt, (int)(pktp - pkt));\r
1956         }\r
1957 }\r
1958 \r
1959 /*\r
1960  * lcp_echo_lowerup - Start the timer for the LCP frame\r
1961  */\r
1962 \r
1963 static void lcp_echo_lowerup (int unit)\r
1964 {\r
1965         fsm *f = &lcp_fsm[unit];\r
1966         \r
1967         /* Clear the parameters for generating echo frames */\r
1968         lcp_echos_pending      = 0;\r
1969         lcp_echo_number        = 0;\r
1970         lcp_echo_timer_running = 0;\r
1971         \r
1972         /* If a timeout interval is specified then start the timer */\r
1973         if (lcp_echo_interval != 0)\r
1974                 LcpEchoCheck (f);\r
1975 }\r
1976 \r
1977 /*\r
1978  * lcp_echo_lowerdown - Stop the timer for the LCP frame\r
1979  */\r
1980 \r
1981 static void lcp_echo_lowerdown (int unit)\r
1982 {\r
1983         fsm *f = &lcp_fsm[unit];\r
1984         \r
1985         if (lcp_echo_timer_running != 0) {\r
1986                 UNTIMEOUT (LcpEchoTimeout, f);\r
1987                 lcp_echo_timer_running = 0;\r
1988         }\r
1989 }\r
1990 \r
1991 #endif /* PPP_SUPPORT */\r