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