Changes to update Tomato RAF.
[tomato.git] / release / src / router / pppd / pppd / ipv6cp.c
blob4d0a07927f31860d2695dd144697acc9139b2f2b
1 /*
2 * ipv6cp.c - PPP IPV6 Control Protocol.
4 * Copyright (c) 1999 Tommi Komulainen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
18 * 3. The name(s) of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
22 * 4. Redistributions of any form whatsoever must retain the following
23 * acknowledgment:
24 * "This product includes software developed by Tommi Komulainen
25 * <Tommi.Komulainen@iki.fi>".
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 /* Original version, based on RFC2023 :
39 Copyright (c) 1995, 1996, 1997 Francis.Dupont@inria.fr, INRIA Rocquencourt,
40 Alain.Durand@imag.fr, IMAG,
41 Jean-Luc.Richier@imag.fr, IMAG-LSR.
43 Copyright (c) 1998, 1999 Francis.Dupont@inria.fr, GIE DYADE,
44 Alain.Durand@imag.fr, IMAG,
45 Jean-Luc.Richier@imag.fr, IMAG-LSR.
47 Ce travail a été fait au sein du GIE DYADE (Groupement d'Intérêt
48 Économique ayant pour membres BULL S.A. et l'INRIA).
50 Ce logiciel informatique est disponible aux conditions
51 usuelles dans la recherche, c'est-à-dire qu'il peut
52 être utilisé, copié, modifié, distribué à l'unique
53 condition que ce texte soit conservé afin que
54 l'origine de ce logiciel soit reconnue.
56 Le nom de l'Institut National de Recherche en Informatique
57 et en Automatique (INRIA), de l'IMAG, ou d'une personne morale
58 ou physique ayant participé à l'élaboration de ce logiciel ne peut
59 être utilisé sans son accord préalable explicite.
61 Ce logiciel est fourni tel quel sans aucune garantie,
62 support ou responsabilité d'aucune sorte.
63 Ce logiciel est dérivé de sources d'origine
64 "University of California at Berkeley" et
65 "Digital Equipment Corporation" couvertes par des copyrights.
67 L'Institut d'Informatique et de Mathématiques Appliquées de Grenoble (IMAG)
68 est une fédération d'unités mixtes de recherche du CNRS, de l'Institut National
69 Polytechnique de Grenoble et de l'Université Joseph Fourier regroupant
70 sept laboratoires dont le laboratoire Logiciels, Systèmes, Réseaux (LSR).
72 This work has been done in the context of GIE DYADE (joint R & D venture
73 between BULL S.A. and INRIA).
75 This software is available with usual "research" terms
76 with the aim of retain credits of the software.
77 Permission to use, copy, modify and distribute this software for any
78 purpose and without fee is hereby granted, provided that the above
79 copyright notice and this permission notice appear in all copies,
80 and the name of INRIA, IMAG, or any contributor not be used in advertising
81 or publicity pertaining to this material without the prior explicit
82 permission. The software is provided "as is" without any
83 warranties, support or liabilities of any kind.
84 This software is derived from source code from
85 "University of California at Berkeley" and
86 "Digital Equipment Corporation" protected by copyrights.
88 Grenoble's Institute of Computer Science and Applied Mathematics (IMAG)
89 is a federation of seven research units funded by the CNRS, National
90 Polytechnic Institute of Grenoble and University Joseph Fourier.
91 The research unit in Software, Systems, Networks (LSR) is member of IMAG.
95 * Derived from :
98 * ipcp.c - PPP IP Control Protocol.
100 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
102 * Redistribution and use in source and binary forms, with or without
103 * modification, are permitted provided that the following conditions
104 * are met:
106 * 1. Redistributions of source code must retain the above copyright
107 * notice, this list of conditions and the following disclaimer.
109 * 2. Redistributions in binary form must reproduce the above copyright
110 * notice, this list of conditions and the following disclaimer in
111 * the documentation and/or other materials provided with the
112 * distribution.
114 * 3. The name "Carnegie Mellon University" must not be used to
115 * endorse or promote products derived from this software without
116 * prior written permission. For permission or any legal
117 * details, please contact
118 * Office of Technology Transfer
119 * Carnegie Mellon University
120 * 5000 Forbes Avenue
121 * Pittsburgh, PA 15213-3890
122 * (412) 268-4387, fax: (412) 268-7395
123 * tech-transfer@andrew.cmu.edu
125 * 4. Redistributions of any form whatsoever must retain the following
126 * acknowledgment:
127 * "This product includes software developed by Computing Services
128 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
130 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
131 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
132 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
133 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
134 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
135 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
136 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
138 * $Id: ipv6cp.c,v 1.21 2005/08/25 23:59:34 paulus Exp $
141 #define RCSID "$Id: ipv6cp.c,v 1.21 2005/08/25 23:59:34 paulus Exp $"
144 * TODO:
146 * Proxy Neighbour Discovery.
148 * Better defines for selecting the ordering of
149 * interface up / set address. (currently checks for __linux__,
150 * since SVR4 && (SNI || __USLC__) didn't work properly)
153 #include <stdio.h>
154 #include <stdlib.h>
155 #include <string.h>
156 #include <unistd.h>
157 #include <netdb.h>
158 #include <sys/param.h>
159 #include <sys/types.h>
160 #include <sys/socket.h>
161 #include <netinet/in.h>
162 #include <arpa/inet.h>
164 #include "pppd.h"
165 #include "fsm.h"
166 #include "ipcp.h"
167 #include "ipv6cp.h"
168 #include "magic.h"
169 #include "pathnames.h"
171 static const char rcsid[] = RCSID;
173 /* global vars */
174 ipv6cp_options ipv6cp_wantoptions[NUM_PPP]; /* Options that we want to request */
175 ipv6cp_options ipv6cp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
176 ipv6cp_options ipv6cp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
177 ipv6cp_options ipv6cp_hisoptions[NUM_PPP]; /* Options that we ack'd */
178 int no_ifaceid_neg = 0;
179 char path_ipv6up[MAXPATHLEN]; /* pathname of ipv6-up script */
180 char path_ipv6down[MAXPATHLEN]; /* pathname of ipv6-down script */
182 /* local vars */
183 static int ipv6cp_is_up;
186 * Callbacks for fsm code. (CI = Configuration Information)
188 static void ipv6cp_resetci __P((fsm *)); /* Reset our CI */
189 static int ipv6cp_cilen __P((fsm *)); /* Return length of our CI */
190 static void ipv6cp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
191 static int ipv6cp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
192 static int ipv6cp_nakci __P((fsm *, u_char *, int, int));/* Peer nak'd our CI */
193 static int ipv6cp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
194 static int ipv6cp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
195 static void ipv6cp_up __P((fsm *)); /* We're UP */
196 static void ipv6cp_down __P((fsm *)); /* We're DOWN */
197 static void ipv6cp_finished __P((fsm *)); /* Don't need lower layer */
199 fsm ipv6cp_fsm[NUM_PPP]; /* IPV6CP fsm structure */
201 static fsm_callbacks ipv6cp_callbacks = { /* IPV6CP callback routines */
202 ipv6cp_resetci, /* Reset our Configuration Information */
203 ipv6cp_cilen, /* Length of our Configuration Information */
204 ipv6cp_addci, /* Add our Configuration Information */
205 ipv6cp_ackci, /* ACK our Configuration Information */
206 ipv6cp_nakci, /* NAK our Configuration Information */
207 ipv6cp_rejci, /* Reject our Configuration Information */
208 ipv6cp_reqci, /* Request peer's Configuration Information */
209 ipv6cp_up, /* Called when fsm reaches OPENED state */
210 ipv6cp_down, /* Called when fsm leaves OPENED state */
211 NULL, /* Called when we want the lower layer up */
212 ipv6cp_finished, /* Called when we want the lower layer down */
213 NULL, /* Called when Protocol-Reject received */
214 NULL, /* Retransmission is necessary */
215 NULL, /* Called to handle protocol-specific codes */
216 "IPV6CP" /* String name of protocol */
220 * Command-line options.
222 static int setifaceid __P((char **arg));
223 static void printifaceid __P((option_t *,
224 void (*)(void *, char *, ...), void *));
226 static option_t ipv6cp_option_list[] = {
227 { "ipv6", o_special, (void *)setifaceid,
228 "Set interface identifiers for IPV6",
229 OPT_A2PRINTER, (void *)printifaceid },
231 { "+ipv6", o_bool, &ipv6cp_protent.enabled_flag,
232 "Enable IPv6 and IPv6CP", OPT_PRIO | 1 },
233 { "noipv6", o_bool, &ipv6cp_protent.enabled_flag,
234 "Disable IPv6 and IPv6CP", OPT_PRIOSUB },
235 { "-ipv6", o_bool, &ipv6cp_protent.enabled_flag,
236 "Disable IPv6 and IPv6CP", OPT_PRIOSUB | OPT_ALIAS },
238 { "ipv6cp-accept-local", o_bool, &ipv6cp_allowoptions[0].accept_local,
239 "Accept peer's interface identifier for us", 1 },
240 { "ipv6cp-accept-remote", o_bool, &ipv6cp_allowoptions[0].accept_remote,
241 "Accept peer's interface identifier for itself", 1 },
243 { "ipv6cp-use-ipaddr", o_bool, &ipv6cp_allowoptions[0].use_ip,
244 "Use (default) IPv4 address as interface identifier", 1 },
246 #if defined(SOL2) || defined(__linux__)
247 { "ipv6cp-use-persistent", o_bool, &ipv6cp_wantoptions[0].use_persistent,
248 "Use uniquely-available persistent value for link local address", 1 },
249 #endif /* defined(SOL2) */
251 { "ipv6cp-restart", o_int, &ipv6cp_fsm[0].timeouttime,
252 "Set timeout for IPv6CP", OPT_PRIO },
253 { "ipv6cp-max-terminate", o_int, &ipv6cp_fsm[0].maxtermtransmits,
254 "Set max #xmits for term-reqs", OPT_PRIO },
255 { "ipv6cp-max-configure", o_int, &ipv6cp_fsm[0].maxconfreqtransmits,
256 "Set max #xmits for conf-reqs", OPT_PRIO },
257 { "ipv6cp-max-failure", o_int, &ipv6cp_fsm[0].maxnakloops,
258 "Set max #conf-naks for IPv6CP", OPT_PRIO },
260 { "ipv6-up-script", o_string, path_ipv6up,
261 "Set pathname of ipv6-up script",
262 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
263 { "ipv6-down-script", o_string, path_ipv6down,
264 "Set pathname of ipv6-down script",
265 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
267 { NULL }
272 * Protocol entry points from main code.
274 static void ipv6cp_init __P((int));
275 static void ipv6cp_open __P((int));
276 static void ipv6cp_close __P((int, char *));
277 static void ipv6cp_lowerup __P((int));
278 static void ipv6cp_lowerdown __P((int));
279 static void ipv6cp_input __P((int, u_char *, int));
280 static void ipv6cp_protrej __P((int));
281 static int ipv6cp_printpkt __P((u_char *, int,
282 void (*) __P((void *, char *, ...)), void *));
283 static void ipv6_check_options __P((void));
284 static int ipv6_demand_conf __P((int));
285 static int ipv6_active_pkt __P((u_char *, int));
287 struct protent ipv6cp_protent = {
288 PPP_IPV6CP,
289 ipv6cp_init,
290 ipv6cp_input,
291 ipv6cp_protrej,
292 ipv6cp_lowerup,
293 ipv6cp_lowerdown,
294 ipv6cp_open,
295 ipv6cp_close,
296 ipv6cp_printpkt,
297 NULL,
299 "IPV6CP",
300 "IPV6",
301 ipv6cp_option_list,
302 ipv6_check_options,
303 ipv6_demand_conf,
304 ipv6_active_pkt
307 static void ipv6cp_clear_addrs __P((int, eui64_t, eui64_t));
308 static void ipv6cp_script __P((char *));
309 static void ipv6cp_script_done __P((void *));
312 * Lengths of configuration options.
314 #define CILEN_VOID 2
315 #define CILEN_COMPRESS 4 /* length for RFC2023 compress opt. */
316 #define CILEN_IFACEID 10 /* RFC2472, interface identifier */
318 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
319 (x) == CONFNAK ? "NAK" : "REJ")
322 * This state variable is used to ensure that we don't
323 * run an ipcp-up/down script while one is already running.
325 static enum script_state {
326 s_down,
327 s_up,
328 } ipv6cp_script_state;
329 static pid_t ipv6cp_script_pid;
332 * setifaceid - set the interface identifiers manually
334 static int
335 setifaceid(argv)
336 char **argv;
338 char *comma, *arg, c;
339 ipv6cp_options *wo = &ipv6cp_wantoptions[0];
340 struct in6_addr addr;
341 static int prio_local, prio_remote;
343 #define VALIDID(a) ( (((a).s6_addr32[0] == 0) && ((a).s6_addr32[1] == 0)) && \
344 (((a).s6_addr32[2] != 0) || ((a).s6_addr32[3] != 0)) )
346 arg = *argv;
347 if ((comma = strchr(arg, ',')) == NULL)
348 comma = arg + strlen(arg);
351 * If comma first character, then no local identifier
353 if (comma != arg) {
354 c = *comma;
355 *comma = '\0';
357 if (inet_pton(AF_INET6, arg, &addr) == 0 || !VALIDID(addr)) {
358 option_error("Illegal interface identifier (local): %s", arg);
359 return 0;
362 if (option_priority >= prio_local) {
363 eui64_copy(addr.s6_addr32[2], wo->ourid);
364 wo->opt_local = 1;
365 prio_local = option_priority;
367 *comma = c;
371 * If comma last character, the no remote identifier
373 if (*comma != 0 && *++comma != '\0') {
374 if (inet_pton(AF_INET6, comma, &addr) == 0 || !VALIDID(addr)) {
375 option_error("Illegal interface identifier (remote): %s", comma);
376 return 0;
378 if (option_priority >= prio_remote) {
379 eui64_copy(addr.s6_addr32[2], wo->hisid);
380 wo->opt_remote = 1;
381 prio_remote = option_priority;
385 if (override_value("+ipv6", option_priority, option_source))
386 ipv6cp_protent.enabled_flag = 1;
387 return 1;
390 char *llv6_ntoa(eui64_t ifaceid);
392 static void
393 printifaceid(opt, printer, arg)
394 option_t *opt;
395 void (*printer) __P((void *, char *, ...));
396 void *arg;
398 ipv6cp_options *wo = &ipv6cp_wantoptions[0];
400 if (wo->opt_local)
401 printer(arg, "%s", llv6_ntoa(wo->ourid));
402 printer(arg, ",");
403 if (wo->opt_remote)
404 printer(arg, "%s", llv6_ntoa(wo->hisid));
408 * Make a string representation of a network address.
410 char *
411 llv6_ntoa(ifaceid)
412 eui64_t ifaceid;
414 static char b[64];
416 sprintf(b, "fe80::%s", eui64_ntoa(ifaceid));
417 return b;
422 * ipv6cp_init - Initialize IPV6CP.
424 static void
425 ipv6cp_init(unit)
426 int unit;
428 fsm *f = &ipv6cp_fsm[unit];
429 ipv6cp_options *wo = &ipv6cp_wantoptions[unit];
430 ipv6cp_options *ao = &ipv6cp_allowoptions[unit];
432 f->unit = unit;
433 f->protocol = PPP_IPV6CP;
434 f->callbacks = &ipv6cp_callbacks;
435 fsm_init(&ipv6cp_fsm[unit]);
437 memset(wo, 0, sizeof(*wo));
438 memset(ao, 0, sizeof(*ao));
440 wo->accept_local = 1;
441 wo->accept_remote = 1;
442 wo->neg_ifaceid = 1;
443 ao->neg_ifaceid = 1;
445 #ifdef IPV6CP_COMP
446 wo->neg_vj = 1;
447 ao->neg_vj = 1;
448 wo->vj_protocol = IPV6CP_COMP;
449 #endif
455 * ipv6cp_open - IPV6CP is allowed to come up.
457 static void
458 ipv6cp_open(unit)
459 int unit;
461 fsm_open(&ipv6cp_fsm[unit]);
466 * ipv6cp_close - Take IPV6CP down.
468 static void
469 ipv6cp_close(unit, reason)
470 int unit;
471 char *reason;
473 fsm_close(&ipv6cp_fsm[unit], reason);
478 * ipv6cp_lowerup - The lower layer is up.
480 static void
481 ipv6cp_lowerup(unit)
482 int unit;
484 fsm_lowerup(&ipv6cp_fsm[unit]);
489 * ipv6cp_lowerdown - The lower layer is down.
491 static void
492 ipv6cp_lowerdown(unit)
493 int unit;
495 fsm_lowerdown(&ipv6cp_fsm[unit]);
500 * ipv6cp_input - Input IPV6CP packet.
502 static void
503 ipv6cp_input(unit, p, len)
504 int unit;
505 u_char *p;
506 int len;
508 fsm_input(&ipv6cp_fsm[unit], p, len);
513 * ipv6cp_protrej - A Protocol-Reject was received for IPV6CP.
515 * Pretend the lower layer went down, so we shut up.
517 static void
518 ipv6cp_protrej(unit)
519 int unit;
521 fsm_lowerdown(&ipv6cp_fsm[unit]);
526 * ipv6cp_resetci - Reset our CI.
528 static void
529 ipv6cp_resetci(f)
530 fsm *f;
532 ipv6cp_options *wo = &ipv6cp_wantoptions[f->unit];
533 ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
535 wo->req_ifaceid = wo->neg_ifaceid && ipv6cp_allowoptions[f->unit].neg_ifaceid;
537 if (!wo->opt_local) {
538 eui64_magic_nz(wo->ourid);
541 *go = *wo;
542 eui64_zero(go->hisid); /* last proposed interface identifier */
547 * ipv6cp_cilen - Return length of our CI.
549 static int
550 ipv6cp_cilen(f)
551 fsm *f;
553 ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
555 #define LENCIVJ(neg) (neg ? CILEN_COMPRESS : 0)
556 #define LENCIIFACEID(neg) (neg ? CILEN_IFACEID : 0)
558 return (LENCIIFACEID(go->neg_ifaceid) +
559 LENCIVJ(go->neg_vj));
564 * ipv6cp_addci - Add our desired CIs to a packet.
566 static void
567 ipv6cp_addci(f, ucp, lenp)
568 fsm *f;
569 u_char *ucp;
570 int *lenp;
572 ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
573 int len = *lenp;
575 #define ADDCIVJ(opt, neg, val) \
576 if (neg) { \
577 int vjlen = CILEN_COMPRESS; \
578 if (len >= vjlen) { \
579 PUTCHAR(opt, ucp); \
580 PUTCHAR(vjlen, ucp); \
581 PUTSHORT(val, ucp); \
582 len -= vjlen; \
583 } else \
584 neg = 0; \
587 #define ADDCIIFACEID(opt, neg, val1) \
588 if (neg) { \
589 int idlen = CILEN_IFACEID; \
590 if (len >= idlen) { \
591 PUTCHAR(opt, ucp); \
592 PUTCHAR(idlen, ucp); \
593 eui64_put(val1, ucp); \
594 len -= idlen; \
595 } else \
596 neg = 0; \
599 ADDCIIFACEID(CI_IFACEID, go->neg_ifaceid, go->ourid);
601 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol);
603 *lenp -= len;
608 * ipv6cp_ackci - Ack our CIs.
610 * Returns:
611 * 0 - Ack was bad.
612 * 1 - Ack was good.
614 static int
615 ipv6cp_ackci(f, p, len)
616 fsm *f;
617 u_char *p;
618 int len;
620 ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
621 u_short cilen, citype, cishort;
622 eui64_t ifaceid;
625 * CIs must be in exactly the same order that we sent...
626 * Check packet length and CI length at each step.
627 * If we find any deviations, then this packet is bad.
630 #define ACKCIVJ(opt, neg, val) \
631 if (neg) { \
632 int vjlen = CILEN_COMPRESS; \
633 if ((len -= vjlen) < 0) \
634 goto bad; \
635 GETCHAR(citype, p); \
636 GETCHAR(cilen, p); \
637 if (cilen != vjlen || \
638 citype != opt) \
639 goto bad; \
640 GETSHORT(cishort, p); \
641 if (cishort != val) \
642 goto bad; \
645 #define ACKCIIFACEID(opt, neg, val1) \
646 if (neg) { \
647 int idlen = CILEN_IFACEID; \
648 if ((len -= idlen) < 0) \
649 goto bad; \
650 GETCHAR(citype, p); \
651 GETCHAR(cilen, p); \
652 if (cilen != idlen || \
653 citype != opt) \
654 goto bad; \
655 eui64_get(ifaceid, p); \
656 if (! eui64_equals(val1, ifaceid)) \
657 goto bad; \
660 ACKCIIFACEID(CI_IFACEID, go->neg_ifaceid, go->ourid);
662 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol);
665 * If there are any remaining CIs, then this packet is bad.
667 if (len != 0)
668 goto bad;
669 return (1);
671 bad:
672 IPV6CPDEBUG(("ipv6cp_ackci: received bad Ack!"));
673 return (0);
677 * ipv6cp_nakci - Peer has sent a NAK for some of our CIs.
678 * This should not modify any state if the Nak is bad
679 * or if IPV6CP is in the OPENED state.
681 * Returns:
682 * 0 - Nak was bad.
683 * 1 - Nak was good.
685 static int
686 ipv6cp_nakci(f, p, len, treat_as_reject)
687 fsm *f;
688 u_char *p;
689 int len;
690 int treat_as_reject;
692 ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
693 u_char citype, cilen, *next;
694 u_short cishort;
695 eui64_t ifaceid;
696 ipv6cp_options no; /* options we've seen Naks for */
697 ipv6cp_options try; /* options to request next time */
699 BZERO(&no, sizeof(no));
700 try = *go;
703 * Any Nak'd CIs must be in exactly the same order that we sent.
704 * Check packet length and CI length at each step.
705 * If we find any deviations, then this packet is bad.
707 #define NAKCIIFACEID(opt, neg, code) \
708 if (go->neg && \
709 len >= (cilen = CILEN_IFACEID) && \
710 p[1] == cilen && \
711 p[0] == opt) { \
712 len -= cilen; \
713 INCPTR(2, p); \
714 eui64_get(ifaceid, p); \
715 no.neg = 1; \
716 code \
719 #define NAKCIVJ(opt, neg, code) \
720 if (go->neg && \
721 ((cilen = p[1]) == CILEN_COMPRESS) && \
722 len >= cilen && \
723 p[0] == opt) { \
724 len -= cilen; \
725 INCPTR(2, p); \
726 GETSHORT(cishort, p); \
727 no.neg = 1; \
728 code \
732 * Accept the peer's idea of {our,his} interface identifier, if different
733 * from our idea, only if the accept_{local,remote} flag is set.
735 NAKCIIFACEID(CI_IFACEID, neg_ifaceid,
736 if (treat_as_reject) {
737 try.neg_ifaceid = 0;
738 } else if (go->accept_local) {
739 while (eui64_iszero(ifaceid) ||
740 eui64_equals(ifaceid, go->hisid)) /* bad luck */
741 eui64_magic(ifaceid);
742 try.ourid = ifaceid;
743 IPV6CPDEBUG(("local LL address %s", llv6_ntoa(ifaceid)));
747 #ifdef IPV6CP_COMP
748 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
750 if (cishort == IPV6CP_COMP && !treat_as_reject) {
751 try.vj_protocol = cishort;
752 } else {
753 try.neg_vj = 0;
757 #else
758 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
760 try.neg_vj = 0;
763 #endif
766 * There may be remaining CIs, if the peer is requesting negotiation
767 * on an option that we didn't include in our request packet.
768 * If they want to negotiate about interface identifier, we comply.
769 * If they want us to ask for compression, we refuse.
771 while (len >= CILEN_VOID) {
772 GETCHAR(citype, p);
773 GETCHAR(cilen, p);
774 if ( cilen < CILEN_VOID || (len -= cilen) < 0 )
775 goto bad;
776 next = p + cilen - 2;
778 switch (citype) {
779 case CI_COMPRESSTYPE:
780 if (go->neg_vj || no.neg_vj ||
781 (cilen != CILEN_COMPRESS))
782 goto bad;
783 no.neg_vj = 1;
784 break;
785 case CI_IFACEID:
786 if (go->neg_ifaceid || no.neg_ifaceid || cilen != CILEN_IFACEID)
787 goto bad;
788 try.neg_ifaceid = 1;
789 eui64_get(ifaceid, p);
790 if (go->accept_local) {
791 while (eui64_iszero(ifaceid) ||
792 eui64_equals(ifaceid, go->hisid)) /* bad luck */
793 eui64_magic(ifaceid);
794 try.ourid = ifaceid;
796 no.neg_ifaceid = 1;
797 break;
799 p = next;
802 /* If there is still anything left, this packet is bad. */
803 if (len != 0)
804 goto bad;
807 * OK, the Nak is good. Now we can update state.
809 if (f->state != OPENED)
810 *go = try;
812 return 1;
814 bad:
815 IPV6CPDEBUG(("ipv6cp_nakci: received bad Nak!"));
816 return 0;
821 * ipv6cp_rejci - Reject some of our CIs.
823 static int
824 ipv6cp_rejci(f, p, len)
825 fsm *f;
826 u_char *p;
827 int len;
829 ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
830 u_char cilen;
831 u_short cishort;
832 eui64_t ifaceid;
833 ipv6cp_options try; /* options to request next time */
835 try = *go;
837 * Any Rejected CIs must be in exactly the same order that we sent.
838 * Check packet length and CI length at each step.
839 * If we find any deviations, then this packet is bad.
841 #define REJCIIFACEID(opt, neg, val1) \
842 if (go->neg && \
843 len >= (cilen = CILEN_IFACEID) && \
844 p[1] == cilen && \
845 p[0] == opt) { \
846 len -= cilen; \
847 INCPTR(2, p); \
848 eui64_get(ifaceid, p); \
849 /* Check rejected value. */ \
850 if (! eui64_equals(ifaceid, val1)) \
851 goto bad; \
852 try.neg = 0; \
855 #define REJCIVJ(opt, neg, val) \
856 if (go->neg && \
857 p[1] == CILEN_COMPRESS && \
858 len >= p[1] && \
859 p[0] == opt) { \
860 len -= p[1]; \
861 INCPTR(2, p); \
862 GETSHORT(cishort, p); \
863 /* Check rejected value. */ \
864 if (cishort != val) \
865 goto bad; \
866 try.neg = 0; \
869 REJCIIFACEID(CI_IFACEID, neg_ifaceid, go->ourid);
871 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol);
874 * If there are any remaining CIs, then this packet is bad.
876 if (len != 0)
877 goto bad;
879 * Now we can update state.
881 if (f->state != OPENED)
882 *go = try;
883 return 1;
885 bad:
886 IPV6CPDEBUG(("ipv6cp_rejci: received bad Reject!"));
887 return 0;
892 * ipv6cp_reqci - Check the peer's requested CIs and send appropriate response.
894 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
895 * appropriately. If reject_if_disagree is non-zero, doesn't return
896 * CONFNAK; returns CONFREJ if it can't return CONFACK.
898 static int
899 ipv6cp_reqci(f, inp, len, reject_if_disagree)
900 fsm *f;
901 u_char *inp; /* Requested CIs */
902 int *len; /* Length of requested CIs */
903 int reject_if_disagree;
905 ipv6cp_options *wo = &ipv6cp_wantoptions[f->unit];
906 ipv6cp_options *ho = &ipv6cp_hisoptions[f->unit];
907 ipv6cp_options *ao = &ipv6cp_allowoptions[f->unit];
908 ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
909 u_char *cip, *next; /* Pointer to current and next CIs */
910 u_short cilen, citype; /* Parsed len, type */
911 u_short cishort; /* Parsed short value */
912 eui64_t ifaceid; /* Parsed interface identifier */
913 int rc = CONFACK; /* Final packet return code */
914 int orc; /* Individual option return code */
915 u_char *p; /* Pointer to next char to parse */
916 u_char *ucp = inp; /* Pointer to current output char */
917 int l = *len; /* Length left */
920 * Reset all his options.
922 BZERO(ho, sizeof(*ho));
925 * Process all his options.
927 next = inp;
928 while (l) {
929 orc = CONFACK; /* Assume success */
930 cip = p = next; /* Remember begining of CI */
931 if (l < 2 || /* Not enough data for CI header or */
932 p[1] < 2 || /* CI length too small or */
933 p[1] > l) { /* CI length too big? */
934 IPV6CPDEBUG(("ipv6cp_reqci: bad CI length!"));
935 orc = CONFREJ; /* Reject bad CI */
936 cilen = l; /* Reject till end of packet */
937 l = 0; /* Don't loop again */
938 goto endswitch;
940 GETCHAR(citype, p); /* Parse CI type */
941 GETCHAR(cilen, p); /* Parse CI length */
942 l -= cilen; /* Adjust remaining length */
943 next += cilen; /* Step to next CI */
945 switch (citype) { /* Check CI type */
946 case CI_IFACEID:
947 IPV6CPDEBUG(("ipv6cp: received interface identifier "));
949 if (!ao->neg_ifaceid ||
950 cilen != CILEN_IFACEID) { /* Check CI length */
951 orc = CONFREJ; /* Reject CI */
952 break;
956 * If he has no interface identifier, or if we both have same
957 * identifier then NAK it with new idea.
958 * In particular, if we don't know his identifier, but he does,
959 * then accept it.
961 eui64_get(ifaceid, p);
962 IPV6CPDEBUG(("(%s)", llv6_ntoa(ifaceid)));
963 if (eui64_iszero(ifaceid) && eui64_iszero(go->ourid)) {
964 orc = CONFREJ; /* Reject CI */
965 break;
967 if (!eui64_iszero(wo->hisid) && !wo->accept_remote &&
968 !eui64_equals(ifaceid, wo->hisid) &&
969 eui64_iszero(go->hisid)) {
971 orc = CONFNAK;
972 ifaceid = wo->hisid;
973 go->hisid = ifaceid;
974 DECPTR(sizeof(ifaceid), p);
975 eui64_put(ifaceid, p);
976 } else
977 if (eui64_iszero(ifaceid) || eui64_equals(ifaceid, go->ourid)) {
978 orc = CONFNAK;
979 if (eui64_iszero(go->hisid)) /* first time, try option */
980 ifaceid = wo->hisid;
981 while (eui64_iszero(ifaceid) ||
982 eui64_equals(ifaceid, go->ourid)) /* bad luck */
983 eui64_magic(ifaceid);
984 go->hisid = ifaceid;
985 DECPTR(sizeof(ifaceid), p);
986 eui64_put(ifaceid, p);
989 ho->neg_ifaceid = 1;
990 ho->hisid = ifaceid;
991 break;
993 case CI_COMPRESSTYPE:
994 IPV6CPDEBUG(("ipv6cp: received COMPRESSTYPE "));
995 if (!ao->neg_vj ||
996 (cilen != CILEN_COMPRESS)) {
997 orc = CONFREJ;
998 break;
1000 GETSHORT(cishort, p);
1001 IPV6CPDEBUG(("(%d)", cishort));
1003 #ifdef IPV6CP_COMP
1004 if (!(cishort == IPV6CP_COMP)) {
1005 orc = CONFREJ;
1006 break;
1009 ho->neg_vj = 1;
1010 ho->vj_protocol = cishort;
1011 break;
1012 #else
1013 orc = CONFREJ;
1014 break;
1015 #endif
1017 default:
1018 orc = CONFREJ;
1019 break;
1022 endswitch:
1023 IPV6CPDEBUG((" (%s)\n", CODENAME(orc)));
1025 if (orc == CONFACK && /* Good CI */
1026 rc != CONFACK) /* but prior CI wasnt? */
1027 continue; /* Don't send this one */
1029 if (orc == CONFNAK) { /* Nak this CI? */
1030 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1031 orc = CONFREJ; /* Get tough if so */
1032 else {
1033 if (rc == CONFREJ) /* Rejecting prior CI? */
1034 continue; /* Don't send this one */
1035 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1036 rc = CONFNAK; /* Not anymore... */
1037 ucp = inp; /* Backup */
1042 if (orc == CONFREJ && /* Reject this CI */
1043 rc != CONFREJ) { /* but no prior ones? */
1044 rc = CONFREJ;
1045 ucp = inp; /* Backup */
1048 /* Need to move CI? */
1049 if (ucp != cip)
1050 BCOPY(cip, ucp, cilen); /* Move it */
1052 /* Update output pointer */
1053 INCPTR(cilen, ucp);
1057 * If we aren't rejecting this packet, and we want to negotiate
1058 * their identifier and they didn't send their identifier, then we
1059 * send a NAK with a CI_IFACEID option appended. We assume the
1060 * input buffer is long enough that we can append the extra
1061 * option safely.
1063 if (rc != CONFREJ && !ho->neg_ifaceid &&
1064 wo->req_ifaceid && !reject_if_disagree) {
1065 if (rc == CONFACK) {
1066 rc = CONFNAK;
1067 ucp = inp; /* reset pointer */
1068 wo->req_ifaceid = 0; /* don't ask again */
1070 PUTCHAR(CI_IFACEID, ucp);
1071 PUTCHAR(CILEN_IFACEID, ucp);
1072 eui64_put(wo->hisid, ucp);
1075 *len = ucp - inp; /* Compute output length */
1076 IPV6CPDEBUG(("ipv6cp: returning Configure-%s", CODENAME(rc)));
1077 return (rc); /* Return final code */
1080 #if defined(SOL2) || defined(__linux__)
1081 int ether_to_eui64(eui64_t *p_eui64);
1082 #endif
1084 * ipv6_check_options - check that any IP-related options are OK,
1085 * and assign appropriate defaults.
1087 static void
1088 ipv6_check_options()
1090 ipv6cp_options *wo = &ipv6cp_wantoptions[0];
1092 if (!ipv6cp_protent.enabled_flag)
1093 return;
1095 #if defined(SOL2) || defined(__linux__)
1097 * Persistent link-local id is only used when user has not explicitly
1098 * configure/hard-code the id
1100 if ((wo->use_persistent) && (!wo->opt_local) && (!wo->opt_remote)) {
1103 * On systems where there are no Ethernet interfaces used, there
1104 * may be other ways to obtain a persistent id. Right now, it
1105 * will fall back to using magic [see eui64_magic] below when
1106 * an EUI-48 from MAC address can't be obtained. Other possibilities
1107 * include obtaining EEPROM serial numbers, or some other unique
1108 * yet persistent number. On Sparc platforms, this is possible,
1109 * but too bad there's no standards yet for x86 machines.
1111 if (ether_to_eui64(&wo->ourid)) {
1112 wo->opt_local = 1;
1115 #endif
1117 if (!wo->opt_local) { /* init interface identifier */
1118 if (wo->use_ip && eui64_iszero(wo->ourid)) {
1119 eui64_setlo32(wo->ourid, ntohl(ipcp_wantoptions[0].ouraddr));
1120 if (!eui64_iszero(wo->ourid))
1121 wo->opt_local = 1;
1124 while (eui64_iszero(wo->ourid))
1125 eui64_magic(wo->ourid);
1128 if (!wo->opt_remote) {
1129 if (wo->use_ip && eui64_iszero(wo->hisid)) {
1130 eui64_setlo32(wo->hisid, ntohl(ipcp_wantoptions[0].hisaddr));
1131 if (!eui64_iszero(wo->hisid))
1132 wo->opt_remote = 1;
1136 if (demand && (eui64_iszero(wo->ourid) || eui64_iszero(wo->hisid))) {
1137 option_error("local/remote LL address required for demand-dialling\n");
1138 exit(1);
1144 * ipv6_demand_conf - configure the interface as though
1145 * IPV6CP were up, for use with dial-on-demand.
1147 static int
1148 ipv6_demand_conf(u)
1149 int u;
1151 ipv6cp_options *wo = &ipv6cp_wantoptions[u];
1153 #if defined(__linux__) || defined(SOL2) || (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1154 #if defined(SOL2)
1155 if (!sif6up(u))
1156 return 0;
1157 #else
1158 if (!sifup(u))
1159 return 0;
1160 #endif /* defined(SOL2) */
1161 #endif
1162 if (!sif6addr(u, wo->ourid, wo->hisid))
1163 return 0;
1164 #if !defined(__linux__) && !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1165 if (!sifup(u))
1166 return 0;
1167 #endif
1168 if (!sifnpmode(u, PPP_IPV6, NPMODE_QUEUE))
1169 return 0;
1171 notice("ipv6_demand_conf");
1172 notice("local LL address %s", llv6_ntoa(wo->ourid));
1173 notice("remote LL address %s", llv6_ntoa(wo->hisid));
1175 return 1;
1180 * ipv6cp_up - IPV6CP has come UP.
1182 * Configure the IPv6 network interface appropriately and bring it up.
1184 static void
1185 ipv6cp_up(f)
1186 fsm *f;
1188 ipv6cp_options *ho = &ipv6cp_hisoptions[f->unit];
1189 ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
1190 ipv6cp_options *wo = &ipv6cp_wantoptions[f->unit];
1192 IPV6CPDEBUG(("ipv6cp: up"));
1195 * We must have a non-zero LL address for both ends of the link.
1197 if (!ho->neg_ifaceid)
1198 ho->hisid = wo->hisid;
1200 if(!no_ifaceid_neg) {
1201 if (eui64_iszero(ho->hisid)) {
1202 error("Could not determine remote LL address");
1203 ipv6cp_close(f->unit, "Could not determine remote LL address");
1204 return;
1206 if (eui64_iszero(go->ourid)) {
1207 error("Could not determine local LL address");
1208 ipv6cp_close(f->unit, "Could not determine local LL address");
1209 return;
1211 if (eui64_equals(go->ourid, ho->hisid)) {
1212 error("local and remote LL addresses are equal");
1213 ipv6cp_close(f->unit, "local and remote LL addresses are equal");
1214 return;
1217 script_setenv("LLLOCAL", llv6_ntoa(go->ourid), 0);
1218 script_setenv("LLREMOTE", llv6_ntoa(ho->hisid), 0);
1220 #ifdef IPV6CP_COMP
1221 /* set tcp compression */
1222 sif6comp(f->unit, ho->neg_vj);
1223 #endif
1226 * If we are doing dial-on-demand, the interface is already
1227 * configured, so we put out any saved-up packets, then set the
1228 * interface to pass IPv6 packets.
1230 if (demand) {
1231 if (! eui64_equals(go->ourid, wo->ourid) ||
1232 ! eui64_equals(ho->hisid, wo->hisid)) {
1233 if (! eui64_equals(go->ourid, wo->ourid))
1234 warn("Local LL address changed to %s",
1235 llv6_ntoa(go->ourid));
1236 if (! eui64_equals(ho->hisid, wo->hisid))
1237 warn("Remote LL address changed to %s",
1238 llv6_ntoa(ho->hisid));
1239 ipv6cp_clear_addrs(f->unit, go->ourid, ho->hisid);
1241 /* Set the interface to the new addresses */
1242 if (!sif6addr(f->unit, go->ourid, ho->hisid)) {
1243 if (debug)
1244 warn("sif6addr failed");
1245 ipv6cp_close(f->unit, "Interface configuration failed");
1246 return;
1250 demand_rexmit(PPP_IPV6,0);
1251 sifnpmode(f->unit, PPP_IPV6, NPMODE_PASS);
1253 } else {
1255 * Set LL addresses
1257 #if !defined(__linux__) && !defined(SOL2) && !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1258 if (!sif6addr(f->unit, go->ourid, ho->hisid)) {
1259 if (debug)
1260 warn("sif6addr failed");
1261 ipv6cp_close(f->unit, "Interface configuration failed");
1262 return;
1264 #endif
1266 /* bring the interface up for IPv6 */
1267 #if defined(SOL2)
1268 if (!sif6up(f->unit)) {
1269 if (debug)
1270 warn("sifup failed (IPV6)");
1271 ipv6cp_close(f->unit, "Interface configuration failed");
1272 return;
1274 #else
1275 if (!sifup(f->unit)) {
1276 if (debug)
1277 warn("sifup failed (IPV6)");
1278 ipv6cp_close(f->unit, "Interface configuration failed");
1279 return;
1281 #endif /* defined(SOL2) */
1283 #if defined(__linux__) || defined(SOL2) || (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1284 if (!sif6addr(f->unit, go->ourid, ho->hisid)) {
1285 if (debug)
1286 warn("sif6addr failed");
1287 ipv6cp_close(f->unit, "Interface configuration failed");
1288 return;
1290 #endif
1291 sifnpmode(f->unit, PPP_IPV6, NPMODE_PASS);
1293 notice("local LL address %s", llv6_ntoa(go->ourid));
1294 notice("remote LL address %s", llv6_ntoa(ho->hisid));
1297 np_up(f->unit, PPP_IPV6);
1298 ipv6cp_is_up = 1;
1301 * Execute the ipv6-up script, like this:
1302 * /etc/ppp/ipv6-up interface tty speed local-LL remote-LL
1304 if (ipv6cp_script_state == s_down && ipv6cp_script_pid == 0) {
1305 ipv6cp_script_state = s_up;
1306 ipv6cp_script(path_ipv6up);
1312 * ipv6cp_down - IPV6CP has gone DOWN.
1314 * Take the IPv6 network interface down, clear its addresses
1315 * and delete routes through it.
1317 static void
1318 ipv6cp_down(f)
1319 fsm *f;
1321 IPV6CPDEBUG(("ipv6cp: down"));
1322 update_link_stats(f->unit);
1323 if (ipv6cp_is_up) {
1324 ipv6cp_is_up = 0;
1325 np_down(f->unit, PPP_IPV6);
1327 #ifdef IPV6CP_COMP
1328 sif6comp(f->unit, 0);
1329 #endif
1332 * If we are doing dial-on-demand, set the interface
1333 * to queue up outgoing packets (for now).
1335 if (demand) {
1336 sifnpmode(f->unit, PPP_IPV6, NPMODE_QUEUE);
1337 } else {
1338 sifnpmode(f->unit, PPP_IPV6, NPMODE_DROP);
1339 #if !defined(__linux__) && !(defined(SVR4) && (defined(SNI) || defined(__USLC)))
1340 #if defined(SOL2)
1341 sif6down(f->unit);
1342 #else
1343 sifdown(f->unit);
1344 #endif /* defined(SOL2) */
1345 #endif
1346 ipv6cp_clear_addrs(f->unit,
1347 ipv6cp_gotoptions[f->unit].ourid,
1348 ipv6cp_hisoptions[f->unit].hisid);
1349 #if defined(__linux__) || (defined(SVR4) && (defined(SNI) || defined(__USLC)))
1350 sifdown(f->unit);
1351 #endif
1354 /* Execute the ipv6-down script */
1355 if (ipv6cp_script_state == s_up && ipv6cp_script_pid == 0) {
1356 ipv6cp_script_state = s_down;
1357 ipv6cp_script(path_ipv6down);
1363 * ipv6cp_clear_addrs() - clear the interface addresses, routes,
1364 * proxy neighbour discovery entries, etc.
1366 static void
1367 ipv6cp_clear_addrs(unit, ourid, hisid)
1368 int unit;
1369 eui64_t ourid;
1370 eui64_t hisid;
1372 cif6addr(unit, ourid, hisid);
1377 * ipv6cp_finished - possibly shut down the lower layers.
1379 static void
1380 ipv6cp_finished(f)
1381 fsm *f;
1383 np_finished(f->unit, PPP_IPV6);
1388 * ipv6cp_script_done - called when the ipv6-up or ipv6-down script
1389 * has finished.
1391 static void
1392 ipv6cp_script_done(arg)
1393 void *arg;
1395 ipv6cp_script_pid = 0;
1396 switch (ipv6cp_script_state) {
1397 case s_up:
1398 if (ipv6cp_fsm[0].state != OPENED) {
1399 ipv6cp_script_state = s_down;
1400 ipv6cp_script(path_ipv6down);
1402 break;
1403 case s_down:
1404 if (ipv6cp_fsm[0].state == OPENED) {
1405 ipv6cp_script_state = s_up;
1406 ipv6cp_script(path_ipv6up);
1408 break;
1414 * ipv6cp_script - Execute a script with arguments
1415 * interface-name tty-name speed local-LL remote-LL.
1417 static void
1418 ipv6cp_script(script)
1419 char *script;
1421 char strspeed[32], strlocal[32], strremote[32];
1422 char *argv[8];
1424 sprintf(strspeed, "%d", baud_rate);
1425 strcpy(strlocal, llv6_ntoa(ipv6cp_gotoptions[0].ourid));
1426 strcpy(strremote, llv6_ntoa(ipv6cp_hisoptions[0].hisid));
1428 argv[0] = script;
1429 argv[1] = ifname;
1430 argv[2] = devnam;
1431 argv[3] = strspeed;
1432 argv[4] = strlocal;
1433 argv[5] = strremote;
1434 argv[6] = ipparam;
1435 argv[7] = NULL;
1437 ipv6cp_script_pid = run_program(script, argv, 0, ipv6cp_script_done,
1438 NULL, 0);
1442 * ipv6cp_printpkt - print the contents of an IPV6CP packet.
1444 static char *ipv6cp_codenames[] = {
1445 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1446 "TermReq", "TermAck", "CodeRej"
1449 static int
1450 ipv6cp_printpkt(p, plen, printer, arg)
1451 u_char *p;
1452 int plen;
1453 void (*printer) __P((void *, char *, ...));
1454 void *arg;
1456 int code, id, len, olen;
1457 u_char *pstart, *optend;
1458 u_short cishort;
1459 eui64_t ifaceid;
1461 if (plen < HEADERLEN)
1462 return 0;
1463 pstart = p;
1464 GETCHAR(code, p);
1465 GETCHAR(id, p);
1466 GETSHORT(len, p);
1467 if (len < HEADERLEN || len > plen)
1468 return 0;
1470 if (code >= 1 && code <= sizeof(ipv6cp_codenames) / sizeof(char *))
1471 printer(arg, " %s", ipv6cp_codenames[code-1]);
1472 else
1473 printer(arg, " code=0x%x", code);
1474 printer(arg, " id=0x%x", id);
1475 len -= HEADERLEN;
1476 switch (code) {
1477 case CONFREQ:
1478 case CONFACK:
1479 case CONFNAK:
1480 case CONFREJ:
1481 /* print option list */
1482 while (len >= 2) {
1483 GETCHAR(code, p);
1484 GETCHAR(olen, p);
1485 p -= 2;
1486 if (olen < 2 || olen > len) {
1487 break;
1489 printer(arg, " <");
1490 len -= olen;
1491 optend = p + olen;
1492 switch (code) {
1493 case CI_COMPRESSTYPE:
1494 if (olen >= CILEN_COMPRESS) {
1495 p += 2;
1496 GETSHORT(cishort, p);
1497 printer(arg, "compress ");
1498 printer(arg, "0x%x", cishort);
1500 break;
1501 case CI_IFACEID:
1502 if (olen == CILEN_IFACEID) {
1503 p += 2;
1504 eui64_get(ifaceid, p);
1505 printer(arg, "addr %s", llv6_ntoa(ifaceid));
1507 break;
1509 while (p < optend) {
1510 GETCHAR(code, p);
1511 printer(arg, " %.2x", code);
1513 printer(arg, ">");
1515 break;
1517 case TERMACK:
1518 case TERMREQ:
1519 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1520 printer(arg, " ");
1521 print_string((char *)p, len, printer, arg);
1522 p += len;
1523 len = 0;
1525 break;
1528 /* print the rest of the bytes in the packet */
1529 for (; len > 0; --len) {
1530 GETCHAR(code, p);
1531 printer(arg, " %.2x", code);
1534 return p - pstart;
1538 * ipv6_active_pkt - see if this IP packet is worth bringing the link up for.
1539 * We don't bring the link up for IP fragments or for TCP FIN packets
1540 * with no data.
1542 #define IP6_HDRLEN 40 /* bytes */
1543 #define IP6_NHDR_FRAG 44 /* fragment IPv6 header */
1544 #define TCP_HDRLEN 20
1545 #define TH_FIN 0x01
1548 * We use these macros because the IP header may be at an odd address,
1549 * and some compilers might use word loads to get th_off or ip_hl.
1552 #define get_ip6nh(x) (((unsigned char *)(x))[6])
1553 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
1554 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1556 static int
1557 ipv6_active_pkt(pkt, len)
1558 u_char *pkt;
1559 int len;
1561 u_char *tcp;
1563 len -= PPP_HDRLEN;
1564 pkt += PPP_HDRLEN;
1565 if (len < IP6_HDRLEN)
1566 return 0;
1567 if (get_ip6nh(pkt) == IP6_NHDR_FRAG)
1568 return 0;
1569 if (get_ip6nh(pkt) != IPPROTO_TCP)
1570 return 1;
1571 if (len < IP6_HDRLEN + TCP_HDRLEN)
1572 return 0;
1573 tcp = pkt + IP6_HDRLEN;
1574 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == IP6_HDRLEN + get_tcpoff(tcp) * 4)
1575 return 0;
1576 return 1;