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