]> git.ozlabs.org Git - ppp.git/blob - pppd/ipv6cp.c
pppd man page: Update header to refer to pppd 2.5.x
[ppp.git] / pppd / ipv6cp.c
1 /*
2  * ipv6cp.c - PPP IPV6 Control Protocol.
3  *
4  * Copyright (c) 1999 Tommi Komulainen.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
17  *
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.
21  *
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>".
26  *
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.
34  *
35  */
36
37 /*  Original version, based on RFC2023 :
38
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.
42
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.
46
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).
49
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.
55
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.
60
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.
66
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).
71
72     This work has been done in the context of GIE DYADE (joint R & D venture
73     between BULL S.A. and INRIA).
74
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.
87
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.
92 */
93
94 /*
95  * Derived from :
96  *
97  *
98  * ipcp.c - PPP IP Control Protocol.
99  *
100  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
101  *
102  * Redistribution and use in source and binary forms, with or without
103  * modification, are permitted provided that the following conditions
104  * are met:
105  *
106  * 1. Redistributions of source code must retain the above copyright
107  *    notice, this list of conditions and the following disclaimer.
108  *
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.
113  *
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
124  *
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/)."
129  *
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.
137  */
138
139 /*
140  * TODO: 
141  *
142  * Proxy Neighbour Discovery.
143  *
144  * Better defines for selecting the ordering of
145  *   interface up / set address. (currently checks for __linux__,
146  *   since SVR4 && (SNI || __USLC__) didn't work properly)
147  */
148
149 #include <stdio.h>
150 #include <stdlib.h>
151 #include <string.h>
152 #include <unistd.h>
153 #include <netdb.h>
154 #include <sys/param.h>
155 #include <sys/types.h>
156 #include <sys/socket.h>
157 #include <netinet/in.h>
158 #include <arpa/inet.h>
159
160 #include "pppd.h"
161 #include "fsm.h"
162 #include "ipcp.h"
163 #include "ipv6cp.h"
164 #include "magic.h"
165 #include "pathnames.h"
166
167
168 /* global vars */
169 ipv6cp_options ipv6cp_wantoptions[NUM_PPP];     /* Options that we want to request */
170 ipv6cp_options ipv6cp_gotoptions[NUM_PPP];      /* Options that peer ack'd */
171 ipv6cp_options ipv6cp_allowoptions[NUM_PPP];    /* Options we allow peer to request */
172 ipv6cp_options ipv6cp_hisoptions[NUM_PPP];      /* Options that we ack'd */
173 int no_ifaceid_neg = 0;
174
175 /* local vars */
176 static int default_route_set[NUM_PPP];          /* Have set up a default route */
177 static int ipv6cp_is_up;
178
179 /* Hook for a plugin to know when IPv6 protocol has come up */
180 void (*ipv6_up_hook)(void) = NULL;
181
182 /* Hook for a plugin to know when IPv6 protocol has come down */
183 void (*ipv6_down_hook)(void) = NULL;
184
185 /* Notifiers for when IPCPv6 goes up and down */
186 struct notifier *ipv6_up_notifier = NULL;
187 struct notifier *ipv6_down_notifier = NULL;
188
189 /*
190  * Callbacks for fsm code.  (CI = Configuration Information)
191  */
192 static void ipv6cp_resetci (fsm *);     /* Reset our CI */
193 static int  ipv6cp_cilen (fsm *);               /* Return length of our CI */
194 static void ipv6cp_addci (fsm *, u_char *, int *); /* Add our CI */
195 static int  ipv6cp_ackci (fsm *, u_char *, int);        /* Peer ack'd our CI */
196 static int  ipv6cp_nakci (fsm *, u_char *, int, int);/* Peer nak'd our CI */
197 static int  ipv6cp_rejci (fsm *, u_char *, int);        /* Peer rej'd our CI */
198 static int  ipv6cp_reqci (fsm *, u_char *, int *, int); /* Rcv CI */
199 static void ipv6cp_up (fsm *);          /* We're UP */
200 static void ipv6cp_down (fsm *);                /* We're DOWN */
201 static void ipv6cp_finished (fsm *);    /* Don't need lower layer */
202
203 fsm ipv6cp_fsm[NUM_PPP];                /* IPV6CP fsm structure */
204
205 static fsm_callbacks ipv6cp_callbacks = { /* IPV6CP callback routines */
206     ipv6cp_resetci,             /* Reset our Configuration Information */
207     ipv6cp_cilen,               /* Length of our Configuration Information */
208     ipv6cp_addci,               /* Add our Configuration Information */
209     ipv6cp_ackci,               /* ACK our Configuration Information */
210     ipv6cp_nakci,               /* NAK our Configuration Information */
211     ipv6cp_rejci,               /* Reject our Configuration Information */
212     ipv6cp_reqci,               /* Request peer's Configuration Information */
213     ipv6cp_up,                  /* Called when fsm reaches OPENED state */
214     ipv6cp_down,                /* Called when fsm leaves OPENED state */
215     NULL,                       /* Called when we want the lower layer up */
216     ipv6cp_finished,            /* Called when we want the lower layer down */
217     NULL,                       /* Called when Protocol-Reject received */
218     NULL,                       /* Retransmission is necessary */
219     NULL,                       /* Called to handle protocol-specific codes */
220     "IPV6CP"                    /* String name of protocol */
221 };
222
223 /*
224  * Command-line options.
225  */
226 static int setifaceid (char **arg);
227 static void printifaceid (option_t *,
228                           void (*)(void *, char *, ...), void *);
229
230 static option_t ipv6cp_option_list[] = {
231     { "ipv6", o_special, (void *)setifaceid,
232       "Set interface identifiers for IPV6",
233       OPT_A2PRINTER, (void *)printifaceid },
234
235     { "+ipv6", o_bool, &ipv6cp_protent.enabled_flag,
236       "Enable IPv6 and IPv6CP", OPT_PRIO | 1 },
237     { "noipv6", o_bool, &ipv6cp_protent.enabled_flag,
238       "Disable IPv6 and IPv6CP", OPT_PRIOSUB },
239     { "-ipv6", o_bool, &ipv6cp_protent.enabled_flag,
240       "Disable IPv6 and IPv6CP", OPT_PRIOSUB | OPT_ALIAS },
241
242     { "ipv6cp-accept-local", o_bool, &ipv6cp_allowoptions[0].accept_local,
243       "Accept peer's interface identifier for us", 1 },
244     { "ipv6cp-accept-remote", o_bool, &ipv6cp_allowoptions[0].accept_remote,
245       "Accept peer's interface identifier for itself", 1 },
246
247     { "defaultroute6", o_bool, &ipv6cp_wantoptions[0].default_route,
248       "Add default IPv6 route", OPT_ENABLE|1, &ipv6cp_allowoptions[0].default_route },
249     { "nodefaultroute6", o_bool, &ipv6cp_allowoptions[0].default_route,
250       "disable defaultroute6 option", OPT_A2CLR,
251       &ipv6cp_wantoptions[0].default_route },
252     { "-defaultroute6", o_bool, &ipv6cp_allowoptions[0].default_route,
253       "disable defaultroute6 option", OPT_ALIAS | OPT_A2CLR,
254       &ipv6cp_wantoptions[0].default_route },
255
256     { "ipv6cp-use-ipaddr", o_bool, &ipv6cp_allowoptions[0].use_ip,
257       "Use (default) IPv4 address as interface identifier", 1 },
258
259     { "ipv6cp-use-persistent", o_bool, &ipv6cp_wantoptions[0].use_persistent,
260       "Use uniquely-available persistent value for link local address", 1 },
261
262     { "ipv6cp-restart", o_int, &ipv6cp_fsm[0].timeouttime,
263       "Set timeout for IPv6CP", OPT_PRIO },
264     { "ipv6cp-max-terminate", o_int, &ipv6cp_fsm[0].maxtermtransmits,
265       "Set max #xmits for term-reqs", OPT_PRIO },
266     { "ipv6cp-max-configure", o_int, &ipv6cp_fsm[0].maxconfreqtransmits,
267       "Set max #xmits for conf-reqs", OPT_PRIO },
268     { "ipv6cp-max-failure", o_int, &ipv6cp_fsm[0].maxnakloops,
269       "Set max #conf-naks for IPv6CP", OPT_PRIO },
270
271    { NULL }
272 };
273
274
275 /*
276  * Protocol entry points from main code.
277  */
278 static void ipv6cp_init (int);
279 static void ipv6cp_open (int);
280 static void ipv6cp_close (int, char *);
281 static void ipv6cp_lowerup (int);
282 static void ipv6cp_lowerdown (int);
283 static void ipv6cp_input (int, u_char *, int);
284 static void ipv6cp_protrej (int);
285 static int  ipv6cp_printpkt (u_char *, int,
286                              void (*) (void *, char *, ...), void *);
287 static void ipv6_check_options (void);
288 static int  ipv6_demand_conf (int);
289 static int  ipv6_active_pkt (u_char *, int);
290
291 struct protent ipv6cp_protent = {
292     PPP_IPV6CP,
293     ipv6cp_init,
294     ipv6cp_input,
295     ipv6cp_protrej,
296     ipv6cp_lowerup,
297     ipv6cp_lowerdown,
298     ipv6cp_open,
299     ipv6cp_close,
300     ipv6cp_printpkt,
301     NULL,
302     1,
303     "IPV6CP",
304     "IPV6",
305     ipv6cp_option_list,
306     ipv6_check_options,
307     ipv6_demand_conf,
308     ipv6_active_pkt
309 };
310
311 static void ipv6cp_clear_addrs (int, eui64_t, eui64_t);
312 static void ipv6cp_script (char *);
313 static void ipv6cp_script_done (void *);
314
315 /*
316  * Lengths of configuration options.
317  */
318 #define CILEN_VOID      2
319 #define CILEN_COMPRESS  4       /* length for RFC2023 compress opt. */
320 #define CILEN_IFACEID   10      /* RFC2472, interface identifier    */
321
322 #define CODENAME(x)     ((x) == CONFACK ? "ACK" : \
323                          (x) == CONFNAK ? "NAK" : "REJ")
324
325 /*
326  * This state variable is used to ensure that we don't
327  * run an ipcp-up/down script while one is already running.
328  */
329 static enum script_state {
330     s_down,
331     s_up,
332 } ipv6cp_script_state;
333 static pid_t ipv6cp_script_pid;
334
335 /*
336  * setifaceid - set the interface identifiers manually
337  */
338 static int
339 setifaceid(char **argv)
340 {
341     char *comma, *arg, c;
342     ipv6cp_options *wo = &ipv6cp_wantoptions[0];
343     struct in6_addr addr;
344     static int prio_local, prio_remote;
345
346 #define VALIDID(a) ( (((a).s6_addr32[0] == 0) && ((a).s6_addr32[1] == 0)) && \
347                         (((a).s6_addr32[2] != 0) || ((a).s6_addr32[3] != 0)) )
348     
349     arg = *argv;
350     if ((comma = strchr(arg, ',')) == NULL)
351         comma = arg + strlen(arg);
352     
353     /* 
354      * If comma first character, then no local identifier
355      */
356     if (comma != arg) {
357         c = *comma;
358         *comma = '\0';
359
360         if (inet_pton(AF_INET6, arg, &addr) == 0 || !VALIDID(addr)) {
361             option_error("Illegal interface identifier (local): %s", arg);
362             return 0;
363         }
364
365         if (option_priority >= prio_local) {
366             eui64_copy(addr.s6_addr32[2], wo->ourid);
367             wo->opt_local = 1;
368             prio_local = option_priority;
369         }
370         *comma = c;
371     }
372     
373     /*
374      * If comma last character, the no remote identifier
375      */
376     if (*comma != 0 && *++comma != '\0') {
377         if (inet_pton(AF_INET6, comma, &addr) == 0 || !VALIDID(addr)) {
378             option_error("Illegal interface identifier (remote): %s", comma);
379             return 0;
380         }
381         if (option_priority >= prio_remote) {
382             eui64_copy(addr.s6_addr32[2], wo->hisid);
383             wo->opt_remote = 1;
384             prio_remote = option_priority;
385         }
386     }
387
388     if (override_value("+ipv6", option_priority, option_source))
389         ipv6cp_protent.enabled_flag = 1;
390     return 1;
391 }
392
393 char *llv6_ntoa(eui64_t ifaceid);
394
395 static void
396 printifaceid(option_t *opt, void (*printer) (void *, char *, ...), void *arg)
397 {
398         ipv6cp_options *wo = &ipv6cp_wantoptions[0];
399
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));
405 }
406
407 /*
408  * Make a string representation of a network address.
409  */
410 char *
411 llv6_ntoa(eui64_t ifaceid)
412 {
413     static char b[64];
414
415     sprintf(b, "fe80::%s", eui64_ntoa(ifaceid));
416     return b;
417 }
418
419
420 /*
421  * ipv6cp_init - Initialize IPV6CP.
422  */
423 static void
424 ipv6cp_init(int unit)
425 {
426     fsm *f = &ipv6cp_fsm[unit];
427     ipv6cp_options *wo = &ipv6cp_wantoptions[unit];
428     ipv6cp_options *ao = &ipv6cp_allowoptions[unit];
429
430     f->unit = unit;
431     f->protocol = PPP_IPV6CP;
432     f->callbacks = &ipv6cp_callbacks;
433     fsm_init(&ipv6cp_fsm[unit]);
434
435     memset(wo, 0, sizeof(*wo));
436     memset(ao, 0, sizeof(*ao));
437
438     wo->accept_local = 0;
439     wo->accept_remote = 0;
440     wo->neg_ifaceid = 1;
441     ao->neg_ifaceid = 1;
442
443 #ifdef IPV6CP_COMP
444     wo->neg_vj = 1;
445     ao->neg_vj = 1;
446     wo->vj_protocol = IPV6CP_COMP;
447 #endif
448
449     /*
450      * XXX This controls whether the user may use the defaultroute option.
451      */
452     ao->default_route = 1;
453 }
454
455
456 /*
457  * ipv6cp_open - IPV6CP is allowed to come up.
458  */
459 static void
460 ipv6cp_open(int unit)
461 {
462     fsm_open(&ipv6cp_fsm[unit]);
463 }
464
465
466 /*
467  * ipv6cp_close - Take IPV6CP down.
468  */
469 static void
470 ipv6cp_close(int unit, char *reason)
471 {
472     fsm_close(&ipv6cp_fsm[unit], reason);
473 }
474
475
476 /*
477  * ipv6cp_lowerup - The lower layer is up.
478  */
479 static void
480 ipv6cp_lowerup(int unit)
481 {
482     fsm_lowerup(&ipv6cp_fsm[unit]);
483 }
484
485
486 /*
487  * ipv6cp_lowerdown - The lower layer is down.
488  */
489 static void
490 ipv6cp_lowerdown(int unit)
491 {
492     fsm_lowerdown(&ipv6cp_fsm[unit]);
493 }
494
495
496 /*
497  * ipv6cp_input - Input IPV6CP packet.
498  */
499 static void
500 ipv6cp_input(int unit, u_char *p, int len)
501 {
502     fsm_input(&ipv6cp_fsm[unit], p, len);
503 }
504
505
506 /*
507  * ipv6cp_protrej - A Protocol-Reject was received for IPV6CP.
508  *
509  * Pretend the lower layer went down, so we shut up.
510  */
511 static void
512 ipv6cp_protrej(int unit)
513 {
514     fsm_lowerdown(&ipv6cp_fsm[unit]);
515 }
516
517
518 /*
519  * ipv6cp_resetci - Reset our CI.
520  */
521 static void
522 ipv6cp_resetci(fsm *f)
523 {
524     ipv6cp_options *wo = &ipv6cp_wantoptions[f->unit];
525     ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
526
527     wo->req_ifaceid = wo->neg_ifaceid && ipv6cp_allowoptions[f->unit].neg_ifaceid;
528     
529     if (!wo->opt_local) {
530         wo->accept_local = 1;
531         if (!demand)
532             eui64_magic_nz(wo->ourid);
533     }
534     if (!wo->opt_remote)
535         wo->accept_remote = 1;
536     
537     *go = *wo;
538     eui64_zero(go->hisid);      /* last proposed interface identifier */
539 }
540
541
542 /*
543  * ipv6cp_cilen - Return length of our CI.
544  */
545 static int
546 ipv6cp_cilen(fsm *f)
547 {
548     ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
549
550 #define LENCIVJ(neg)            (neg ? CILEN_COMPRESS : 0)
551 #define LENCIIFACEID(neg)       (neg ? CILEN_IFACEID : 0)
552
553     return (LENCIIFACEID(go->neg_ifaceid) +
554             LENCIVJ(go->neg_vj));
555 }
556
557
558 /*
559  * ipv6cp_addci - Add our desired CIs to a packet.
560  */
561 static void
562 ipv6cp_addci(fsm *f, u_char *ucp, int *lenp)
563 {
564     ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
565     int len = *lenp;
566
567 #define ADDCIVJ(opt, neg, val) \
568     if (neg) { \
569         int vjlen = CILEN_COMPRESS; \
570         if (len >= vjlen) { \
571             PUTCHAR(opt, ucp); \
572             PUTCHAR(vjlen, ucp); \
573             PUTSHORT(val, ucp); \
574             len -= vjlen; \
575         } else \
576             neg = 0; \
577     }
578
579 #define ADDCIIFACEID(opt, neg, val1) \
580     if (neg) { \
581         int idlen = CILEN_IFACEID; \
582         if (len >= idlen) { \
583             PUTCHAR(opt, ucp); \
584             PUTCHAR(idlen, ucp); \
585             eui64_put(val1, ucp); \
586             len -= idlen; \
587         } else \
588             neg = 0; \
589     }
590
591     ADDCIIFACEID(CI_IFACEID, go->neg_ifaceid, go->ourid);
592
593     ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol);
594
595     *lenp -= len;
596 }
597
598
599 /*
600  * ipv6cp_ackci - Ack our CIs.
601  *
602  * Returns:
603  *      0 - Ack was bad.
604  *      1 - Ack was good.
605  */
606 static int
607 ipv6cp_ackci(fsm *f, u_char *p, int len)
608 {
609     ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
610     u_short cilen, citype, cishort;
611     eui64_t ifaceid;
612
613     /*
614      * CIs must be in exactly the same order that we sent...
615      * Check packet length and CI length at each step.
616      * If we find any deviations, then this packet is bad.
617      */
618
619 #define ACKCIVJ(opt, neg, val) \
620     if (neg) { \
621         int vjlen = CILEN_COMPRESS; \
622         if ((len -= vjlen) < 0) \
623             goto bad; \
624         GETCHAR(citype, p); \
625         GETCHAR(cilen, p); \
626         if (cilen != vjlen || \
627             citype != opt)  \
628             goto bad; \
629         GETSHORT(cishort, p); \
630         if (cishort != val) \
631             goto bad; \
632     }
633
634 #define ACKCIIFACEID(opt, neg, val1) \
635     if (neg) { \
636         int idlen = CILEN_IFACEID; \
637         if ((len -= idlen) < 0) \
638             goto bad; \
639         GETCHAR(citype, p); \
640         GETCHAR(cilen, p); \
641         if (cilen != idlen || \
642             citype != opt) \
643             goto bad; \
644         eui64_get(ifaceid, p); \
645         if (! eui64_equals(val1, ifaceid)) \
646             goto bad; \
647     }
648
649     ACKCIIFACEID(CI_IFACEID, go->neg_ifaceid, go->ourid);
650
651     ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol);
652
653     /*
654      * If there are any remaining CIs, then this packet is bad.
655      */
656     if (len != 0)
657         goto bad;
658     return (1);
659
660 bad:
661     IPV6CPDEBUG(("ipv6cp_ackci: received bad Ack!"));
662     return (0);
663 }
664
665 /*
666  * ipv6cp_nakci - Peer has sent a NAK for some of our CIs.
667  * This should not modify any state if the Nak is bad
668  * or if IPV6CP is in the OPENED state.
669  *
670  * Returns:
671  *      0 - Nak was bad.
672  *      1 - Nak was good.
673  */
674 static int
675 ipv6cp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
676 {
677     ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
678     u_char citype, cilen, *next;
679     u_short cishort;
680     eui64_t ifaceid;
681     ipv6cp_options no;          /* options we've seen Naks for */
682     ipv6cp_options try;         /* options to request next time */
683
684     BZERO(&no, sizeof(no));
685     try = *go;
686
687     /*
688      * Any Nak'd CIs must be in exactly the same order that we sent.
689      * Check packet length and CI length at each step.
690      * If we find any deviations, then this packet is bad.
691      */
692 #define NAKCIIFACEID(opt, neg, code) \
693     if (go->neg && \
694         len >= (cilen = CILEN_IFACEID) && \
695         p[1] == cilen && \
696         p[0] == opt) { \
697         len -= cilen; \
698         INCPTR(2, p); \
699         eui64_get(ifaceid, p); \
700         no.neg = 1; \
701         code \
702     }
703
704 #define NAKCIVJ(opt, neg, code) \
705     if (go->neg && \
706         ((cilen = p[1]) == CILEN_COMPRESS) && \
707         len >= cilen && \
708         p[0] == opt) { \
709         len -= cilen; \
710         INCPTR(2, p); \
711         GETSHORT(cishort, p); \
712         no.neg = 1; \
713         code \
714     }
715
716     /*
717      * Accept the peer's idea of {our,his} interface identifier, if different
718      * from our idea, only if the accept_{local,remote} flag is set.
719      */
720     NAKCIIFACEID(CI_IFACEID, neg_ifaceid,
721                  if (treat_as_reject) {
722                      try.neg_ifaceid = 0;
723                  } else if (go->accept_local) {
724                      while (eui64_iszero(ifaceid) || 
725                             eui64_equals(ifaceid, go->hisid)) /* bad luck */
726                          eui64_magic(ifaceid);
727                      try.ourid = ifaceid;
728                      IPV6CPDEBUG(("local LL address %s", llv6_ntoa(ifaceid)));
729                  }
730                  );
731
732 #ifdef IPV6CP_COMP
733     NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
734             {
735                 if (cishort == IPV6CP_COMP && !treat_as_reject) {
736                     try.vj_protocol = cishort;
737                 } else {
738                     try.neg_vj = 0;
739                 }
740             }
741             );
742 #else
743     NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
744             {
745                 try.neg_vj = 0;
746             }
747             );
748 #endif
749
750     /*
751      * There may be remaining CIs, if the peer is requesting negotiation
752      * on an option that we didn't include in our request packet.
753      * If they want to negotiate about interface identifier, we comply.
754      * If they want us to ask for compression, we refuse.
755      */
756     while (len >= CILEN_VOID) {
757         GETCHAR(citype, p);
758         GETCHAR(cilen, p);
759         if ( cilen < CILEN_VOID || (len -= cilen) < 0 )
760             goto bad;
761         next = p + cilen - 2;
762
763         switch (citype) {
764         case CI_COMPRESSTYPE:
765             if (go->neg_vj || no.neg_vj ||
766                 (cilen != CILEN_COMPRESS))
767                 goto bad;
768             no.neg_vj = 1;
769             break;
770         case CI_IFACEID:
771             if (go->neg_ifaceid || no.neg_ifaceid || cilen != CILEN_IFACEID)
772                 goto bad;
773             try.neg_ifaceid = 1;
774             eui64_get(ifaceid, p);
775             if (go->accept_local) {
776                 while (eui64_iszero(ifaceid) || 
777                        eui64_equals(ifaceid, go->hisid)) /* bad luck */
778                     eui64_magic(ifaceid);
779                 try.ourid = ifaceid;
780             }
781             no.neg_ifaceid = 1;
782             break;
783         }
784         p = next;
785     }
786
787     /* If there is still anything left, this packet is bad. */
788     if (len != 0)
789         goto bad;
790
791     /*
792      * OK, the Nak is good.  Now we can update state.
793      */
794     if (f->state != OPENED)
795         *go = try;
796
797     return 1;
798
799 bad:
800     IPV6CPDEBUG(("ipv6cp_nakci: received bad Nak!"));
801     return 0;
802 }
803
804
805 /*
806  * ipv6cp_rejci - Reject some of our CIs.
807  */
808 static int
809 ipv6cp_rejci(fsm *f, u_char *p, int len)
810 {
811     ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
812     u_char cilen;
813     u_short cishort;
814     eui64_t ifaceid;
815     ipv6cp_options try;         /* options to request next time */
816
817     try = *go;
818     /*
819      * Any Rejected CIs must be in exactly the same order that we sent.
820      * Check packet length and CI length at each step.
821      * If we find any deviations, then this packet is bad.
822      */
823 #define REJCIIFACEID(opt, neg, val1) \
824     if (go->neg && \
825         len >= (cilen = CILEN_IFACEID) && \
826         p[1] == cilen && \
827         p[0] == opt) { \
828         len -= cilen; \
829         INCPTR(2, p); \
830         eui64_get(ifaceid, p); \
831         /* Check rejected value. */ \
832         if (! eui64_equals(ifaceid, val1)) \
833             goto bad; \
834         try.neg = 0; \
835     }
836
837 #define REJCIVJ(opt, neg, val) \
838     if (go->neg && \
839         p[1] == CILEN_COMPRESS && \
840         len >= p[1] && \
841         p[0] == opt) { \
842         len -= p[1]; \
843         INCPTR(2, p); \
844         GETSHORT(cishort, p); \
845         /* Check rejected value. */  \
846         if (cishort != val) \
847             goto bad; \
848         try.neg = 0; \
849      }
850
851     REJCIIFACEID(CI_IFACEID, neg_ifaceid, go->ourid);
852
853     REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol);
854
855     /*
856      * If there are any remaining CIs, then this packet is bad.
857      */
858     if (len != 0)
859         goto bad;
860     /*
861      * Now we can update state.
862      */
863     if (f->state != OPENED)
864         *go = try;
865     return 1;
866
867 bad:
868     IPV6CPDEBUG(("ipv6cp_rejci: received bad Reject!"));
869     return 0;
870 }
871
872
873 /*
874  * ipv6cp_reqci - Check the peer's requested CIs and send appropriate response.
875  *
876  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
877  * appropriately.  If reject_if_disagree is non-zero, doesn't return
878  * CONFNAK; returns CONFREJ if it can't return CONFACK.
879  */
880 static int
881 ipv6cp_reqci(fsm *f, u_char *inp, int *len, int reject_if_disagree)
882 {
883     ipv6cp_options *wo = &ipv6cp_wantoptions[f->unit];
884     ipv6cp_options *ho = &ipv6cp_hisoptions[f->unit];
885     ipv6cp_options *ao = &ipv6cp_allowoptions[f->unit];
886     ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
887     u_char *cip, *next;         /* Pointer to current and next CIs */
888     u_short cilen, citype;      /* Parsed len, type */
889     u_short cishort;            /* Parsed short value */
890     eui64_t ifaceid;            /* Parsed interface identifier */
891     int rc = CONFACK;           /* Final packet return code */
892     int orc;                    /* Individual option return code */
893     u_char *p;                  /* Pointer to next char to parse */
894     u_char *ucp = inp;          /* Pointer to current output char */
895     int l = *len;               /* Length left */
896
897     /*
898      * Reset all his options.
899      */
900     BZERO(ho, sizeof(*ho));
901     
902     /*
903      * Process all his options.
904      */
905     next = inp;
906     while (l) {
907         orc = CONFACK;                  /* Assume success */
908         cip = p = next;                 /* Remember begining of CI */
909         if (l < 2 ||                    /* Not enough data for CI header or */
910             p[1] < 2 ||                 /*  CI length too small or */
911             p[1] > l) {                 /*  CI length too big? */
912             IPV6CPDEBUG(("ipv6cp_reqci: bad CI length!"));
913             orc = CONFREJ;              /* Reject bad CI */
914             cilen = l;                  /* Reject till end of packet */
915             l = 0;                      /* Don't loop again */
916             goto endswitch;
917         }
918         GETCHAR(citype, p);             /* Parse CI type */
919         GETCHAR(cilen, p);              /* Parse CI length */
920         l -= cilen;                     /* Adjust remaining length */
921         next += cilen;                  /* Step to next CI */
922
923         switch (citype) {               /* Check CI type */
924         case CI_IFACEID:
925             IPV6CPDEBUG(("ipv6cp: received interface identifier "));
926
927             if (!ao->neg_ifaceid ||
928                 cilen != CILEN_IFACEID) {       /* Check CI length */
929                 orc = CONFREJ;          /* Reject CI */
930                 break;
931             }
932
933             /*
934              * If he has no interface identifier, or if we both have same 
935              * identifier then NAK it with new idea.
936              * In particular, if we don't know his identifier, but he does,
937              * then accept it.
938              */
939             eui64_get(ifaceid, p);
940             IPV6CPDEBUG(("(%s)", llv6_ntoa(ifaceid)));
941             if (eui64_iszero(ifaceid) && eui64_iszero(go->ourid)) {
942                 orc = CONFREJ;          /* Reject CI */
943                 break;
944             }
945             if (!eui64_iszero(wo->hisid) && !wo->accept_remote &&
946                 !eui64_equals(ifaceid, wo->hisid) && 
947                 eui64_iszero(go->hisid)) {
948                     
949                 orc = CONFNAK;
950                 ifaceid = wo->hisid;
951                 go->hisid = ifaceid;
952                 DECPTR(sizeof(ifaceid), p);
953                 eui64_put(ifaceid, p);
954             } else
955             if (eui64_iszero(ifaceid) || eui64_equals(ifaceid, go->ourid)) {
956                 orc = CONFNAK;
957                 if (eui64_iszero(go->hisid))    /* first time, try option */
958                     ifaceid = wo->hisid;
959                 while (eui64_iszero(ifaceid) || 
960                        eui64_equals(ifaceid, go->ourid)) /* bad luck */
961                     eui64_magic(ifaceid);
962                 go->hisid = ifaceid;
963                 DECPTR(sizeof(ifaceid), p);
964                 eui64_put(ifaceid, p);
965             }
966
967             ho->neg_ifaceid = 1;
968             ho->hisid = ifaceid;
969             break;
970
971         case CI_COMPRESSTYPE:
972             IPV6CPDEBUG(("ipv6cp: received COMPRESSTYPE "));
973             if (!ao->neg_vj ||
974                 (cilen != CILEN_COMPRESS)) {
975                 orc = CONFREJ;
976                 break;
977             }
978             GETSHORT(cishort, p);
979             IPV6CPDEBUG(("(%d)", cishort));
980
981 #ifdef IPV6CP_COMP
982             if (!(cishort == IPV6CP_COMP)) {
983                 orc = CONFREJ;
984                 break;
985             }
986
987             ho->neg_vj = 1;
988             ho->vj_protocol = cishort;
989             break;
990 #else
991             orc = CONFREJ;
992             break;
993 #endif
994
995         default:
996             orc = CONFREJ;
997             break;
998         }
999
1000 endswitch:
1001         IPV6CPDEBUG((" (%s)\n", CODENAME(orc)));
1002
1003         if (orc == CONFACK &&           /* Good CI */
1004             rc != CONFACK)              /*  but prior CI wasnt? */
1005             continue;                   /* Don't send this one */
1006
1007         if (orc == CONFNAK) {           /* Nak this CI? */
1008             if (reject_if_disagree)     /* Getting fed up with sending NAKs? */
1009                 orc = CONFREJ;          /* Get tough if so */
1010             else {
1011                 if (rc == CONFREJ)      /* Rejecting prior CI? */
1012                     continue;           /* Don't send this one */
1013                 if (rc == CONFACK) {    /* Ack'd all prior CIs? */
1014                     rc = CONFNAK;       /* Not anymore... */
1015                     ucp = inp;          /* Backup */
1016                 }
1017             }
1018         }
1019
1020         if (orc == CONFREJ &&           /* Reject this CI */
1021             rc != CONFREJ) {            /*  but no prior ones? */
1022             rc = CONFREJ;
1023             ucp = inp;                  /* Backup */
1024         }
1025
1026         /* Need to move CI? */
1027         if (ucp != cip)
1028             BCOPY(cip, ucp, cilen);     /* Move it */
1029
1030         /* Update output pointer */
1031         INCPTR(cilen, ucp);
1032     }
1033
1034     /*
1035      * If we aren't rejecting this packet, and we want to negotiate
1036      * their identifier and they didn't send their identifier, then we
1037      * send a NAK with a CI_IFACEID option appended.  We assume the
1038      * input buffer is long enough that we can append the extra
1039      * option safely.
1040      */
1041     if (rc != CONFREJ && !ho->neg_ifaceid &&
1042         wo->req_ifaceid && !reject_if_disagree) {
1043         if (rc == CONFACK) {
1044             rc = CONFNAK;
1045             ucp = inp;                          /* reset pointer */
1046             wo->req_ifaceid = 0;                /* don't ask again */
1047         }
1048         PUTCHAR(CI_IFACEID, ucp);
1049         PUTCHAR(CILEN_IFACEID, ucp);
1050         eui64_put(wo->hisid, ucp);
1051     }
1052
1053     *len = ucp - inp;                   /* Compute output length */
1054     IPV6CPDEBUG(("ipv6cp: returning Configure-%s", CODENAME(rc)));
1055     return (rc);                        /* Return final code */
1056 }
1057
1058
1059 /*
1060  * ether_to_eui64 - Convert 48-bit Ethernet address into 64-bit EUI
1061  *
1062  * walks the list of valid ethernet interfaces, starting with devnam
1063  * (for PPPoE it is ethernet interface), and convert the first
1064  * found 48-bit MAC address into EUI 64. caller also assumes that
1065  * the system has a properly configured Ethernet interface for this
1066  * function to return non-zero.
1067  */
1068 static int
1069 ether_to_eui64(eui64_t *p_eui64)
1070 {
1071     u_char addr[6];
1072
1073     if (get_if_hwaddr(addr, devnam) < 0 && get_first_ether_hwaddr(addr) < 0) {
1074         error("ipv6cp: no persistent id can be found");
1075         return 0;
1076     }
1077
1078     /*
1079      * And convert the EUI-48 into EUI-64, per RFC 2472 [sec 4.1]
1080      */
1081     p_eui64->e8[0] = addr[0] | 0x02;
1082     p_eui64->e8[1] = addr[1];
1083     p_eui64->e8[2] = addr[2];
1084     p_eui64->e8[3] = 0xFF;
1085     p_eui64->e8[4] = 0xFE;
1086     p_eui64->e8[5] = addr[3];
1087     p_eui64->e8[6] = addr[4];
1088     p_eui64->e8[7] = addr[5];
1089
1090     return 1;
1091 }
1092
1093
1094 /*
1095  * ipv6_check_options - check that any IP-related options are OK,
1096  * and assign appropriate defaults.
1097  */
1098 static void
1099 ipv6_check_options(void)
1100 {
1101     ipv6cp_options *wo = &ipv6cp_wantoptions[0];
1102
1103     if (!ipv6cp_protent.enabled_flag)
1104         return;
1105
1106     /*
1107      * Persistent link-local id is only used when user has not explicitly
1108      * configure/hard-code the id
1109      */
1110     if ((wo->use_persistent) && (!wo->opt_local)) {
1111
1112         /* 
1113          * On systems where there are no Ethernet interfaces used, there
1114          * may be other ways to obtain a persistent id. Right now, it
1115          * will fall back to using magic [see eui64_magic] below when
1116          * an EUI-48 from MAC address can't be obtained. Other possibilities
1117          * include obtaining EEPROM serial numbers, or some other unique
1118          * yet persistent number. On Sparc platforms, this is possible,
1119          * but too bad there's no standards yet for x86 machines.
1120          */
1121         if (ether_to_eui64(&wo->ourid)) {
1122             wo->opt_local = 1;
1123         }
1124     }
1125
1126     if (!wo->opt_local) {       /* init interface identifier */
1127         if (wo->use_ip && eui64_iszero(wo->ourid)) {
1128             eui64_setlo32(wo->ourid, ntohl(ipcp_wantoptions[0].ouraddr));
1129             if (!eui64_iszero(wo->ourid))
1130                 wo->opt_local = 1;
1131         }
1132         
1133         while (eui64_iszero(wo->ourid))
1134             eui64_magic(wo->ourid);
1135     }
1136
1137     if (!wo->opt_remote) {
1138         if (wo->use_ip && eui64_iszero(wo->hisid)) {
1139             eui64_setlo32(wo->hisid, ntohl(ipcp_wantoptions[0].hisaddr));
1140             if (!eui64_iszero(wo->hisid))
1141                 wo->opt_remote = 1;
1142         }
1143     }
1144 }
1145
1146
1147 /*
1148  * ipv6_demand_conf - configure the interface as though
1149  * IPV6CP were up, for use with dial-on-demand.
1150  */
1151 static int
1152 ipv6_demand_conf(int u)
1153 {
1154     ipv6cp_options *wo = &ipv6cp_wantoptions[u];
1155
1156     if (eui64_iszero(wo->hisid)) {
1157         /* make up an arbitrary identifier for the peer */
1158         eui64_magic_nz(wo->hisid);
1159     }
1160     if (eui64_iszero(wo->ourid)) {
1161         /* make up an arbitrary identifier for us */
1162         eui64_magic_nz(wo->ourid);
1163     }
1164
1165     if (!sif6up(u))
1166         return 0;
1167     if (!sif6addr(u, wo->ourid, wo->hisid))
1168         return 0;
1169 #if !defined(__linux__) && !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1170     if (!sifup(u))
1171         return 0;
1172 #endif
1173     if (!sifnpmode(u, PPP_IPV6, NPMODE_QUEUE))
1174         return 0;
1175     if (wo->default_route)
1176         if (sif6defaultroute(u, wo->ourid, wo->hisid))
1177             default_route_set[u] = 1;
1178
1179     notice("local  LL address %s", llv6_ntoa(wo->ourid));
1180     notice("remote LL address %s", llv6_ntoa(wo->hisid));
1181
1182     return 1;
1183 }
1184
1185
1186 /*
1187  * ipv6cp_up - IPV6CP has come UP.
1188  *
1189  * Configure the IPv6 network interface appropriately and bring it up.
1190  */
1191 static void
1192 ipv6cp_up(fsm *f)
1193 {
1194     ipv6cp_options *ho = &ipv6cp_hisoptions[f->unit];
1195     ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
1196     ipv6cp_options *wo = &ipv6cp_wantoptions[f->unit];
1197
1198     IPV6CPDEBUG(("ipv6cp: up"));
1199
1200     /*
1201      * We must have a non-zero LL address for both ends of the link.
1202      */
1203     if (!ho->neg_ifaceid)
1204         ho->hisid = wo->hisid;
1205
1206     if(!no_ifaceid_neg) {
1207         if (eui64_iszero(ho->hisid)) {
1208             error("Could not determine remote LL address");
1209             ipv6cp_close(f->unit, "Could not determine remote LL address");
1210             return;
1211         }
1212         if (eui64_iszero(go->ourid)) {
1213             error("Could not determine local LL address");
1214             ipv6cp_close(f->unit, "Could not determine local LL address");
1215             return;
1216         }
1217         if (eui64_equals(go->ourid, ho->hisid)) {
1218             error("local and remote LL addresses are equal");
1219             ipv6cp_close(f->unit, "local and remote LL addresses are equal");
1220             return;
1221         }
1222     }
1223     script_setenv("LLLOCAL", llv6_ntoa(go->ourid), 0);
1224     script_setenv("LLREMOTE", llv6_ntoa(ho->hisid), 0);
1225
1226 #ifdef IPV6CP_COMP
1227     /* set tcp compression */
1228     sif6comp(f->unit, ho->neg_vj);
1229 #endif
1230
1231     /*
1232      * If we are doing dial-on-demand, the interface is already
1233      * configured, so we put out any saved-up packets, then set the
1234      * interface to pass IPv6 packets.
1235      */
1236     if (demand) {
1237         if (! eui64_equals(go->ourid, wo->ourid) || 
1238             ! eui64_equals(ho->hisid, wo->hisid)) {
1239             if (! eui64_equals(go->ourid, wo->ourid))
1240                 warn("Local LL address changed to %s", 
1241                      llv6_ntoa(go->ourid));
1242             if (! eui64_equals(ho->hisid, wo->hisid))
1243                 warn("Remote LL address changed to %s", 
1244                      llv6_ntoa(ho->hisid));
1245             ipv6cp_clear_addrs(f->unit, wo->ourid, wo->hisid);
1246
1247             /* Set the interface to the new addresses */
1248             if (!sif6addr(f->unit, go->ourid, ho->hisid)) {
1249                 if (debug)
1250                     warn("sif6addr failed");
1251                 ipv6cp_close(f->unit, "Interface configuration failed");
1252                 return;
1253             }
1254
1255             /* assign a default route through the interface if required */
1256             if (ipv6cp_wantoptions[f->unit].default_route)
1257                 if (sif6defaultroute(f->unit, go->ourid, ho->hisid))
1258                     default_route_set[f->unit] = 1;
1259         }
1260         demand_rexmit(PPP_IPV6);
1261         sifnpmode(f->unit, PPP_IPV6, NPMODE_PASS);
1262
1263     } else {
1264         /* bring the interface up for IPv6 */
1265         if (!sif6up(f->unit)) {
1266             if (debug)
1267                 warn("sif6up failed (IPV6)");
1268             ipv6cp_close(f->unit, "Interface configuration failed");
1269             return;
1270         }
1271
1272         if (!sif6addr(f->unit, go->ourid, ho->hisid)) {
1273             if (debug)
1274                 warn("sif6addr failed");
1275             ipv6cp_close(f->unit, "Interface configuration failed");
1276             return;
1277         }
1278         sifnpmode(f->unit, PPP_IPV6, NPMODE_PASS);
1279
1280         /* assign a default route through the interface if required */
1281         if (ipv6cp_wantoptions[f->unit].default_route)
1282             if (sif6defaultroute(f->unit, go->ourid, ho->hisid))
1283                 default_route_set[f->unit] = 1;
1284
1285         notice("local  LL address %s", llv6_ntoa(go->ourid));
1286         notice("remote LL address %s", llv6_ntoa(ho->hisid));
1287     }
1288
1289     np_up(f->unit, PPP_IPV6);
1290     ipv6cp_is_up = 1;
1291
1292     notify(ipv6_up_notifier, 0);
1293     if (ipv6_up_hook)
1294        ipv6_up_hook();
1295
1296     /*
1297      * Execute the ipv6-up script, like this:
1298      *  /etc/ppp/ipv6-up interface tty speed local-LL remote-LL
1299      */
1300     if (ipv6cp_script_state == s_down && ipv6cp_script_pid == 0) {
1301         ipv6cp_script_state = s_up;
1302         ipv6cp_script(_PATH_IPV6UP);
1303     }
1304 }
1305
1306
1307 /*
1308  * ipv6cp_down - IPV6CP has gone DOWN.
1309  *
1310  * Take the IPv6 network interface down, clear its addresses
1311  * and delete routes through it.
1312  */
1313 static void
1314 ipv6cp_down(fsm *f)
1315 {
1316     IPV6CPDEBUG(("ipv6cp: down"));
1317     update_link_stats(f->unit);
1318     notify(ipv6_down_notifier, 0);
1319     if (ipv6_down_hook)
1320        ipv6_down_hook();
1321     if (ipv6cp_is_up) {
1322         ipv6cp_is_up = 0;
1323         np_down(f->unit, PPP_IPV6);
1324     }
1325 #ifdef IPV6CP_COMP
1326     sif6comp(f->unit, 0);
1327 #endif
1328
1329     /*
1330      * If we are doing dial-on-demand, set the interface
1331      * to queue up outgoing packets (for now).
1332      */
1333     if (demand) {
1334         sifnpmode(f->unit, PPP_IPV6, NPMODE_QUEUE);
1335     } else {
1336         sifnpmode(f->unit, PPP_IPV6, NPMODE_DROP);
1337 #if !defined(__linux__) && !(defined(SVR4) && (defined(SNI) || defined(__USLC)))
1338         sif6down(f->unit);
1339 #endif
1340         ipv6cp_clear_addrs(f->unit, 
1341                            ipv6cp_gotoptions[f->unit].ourid,
1342                            ipv6cp_hisoptions[f->unit].hisid);
1343 #if defined(__linux__)
1344         sif6down(f->unit);
1345 #elif defined(SVR4) && (defined(SNI) || defined(__USLC))
1346         sifdown(f->unit);
1347 #endif
1348     }
1349
1350     /* Execute the ipv6-down script */
1351     if (ipv6cp_script_state == s_up && ipv6cp_script_pid == 0) {
1352         ipv6cp_script_state = s_down;
1353         ipv6cp_script(_PATH_IPV6DOWN);
1354     }
1355 }
1356
1357
1358 /*
1359  * ipv6cp_clear_addrs() - clear the interface addresses, routes,
1360  * proxy neighbour discovery entries, etc.
1361  */
1362 static void
1363 ipv6cp_clear_addrs(int unit, eui64_t ourid, eui64_t hisid)
1364 {
1365     cif6addr(unit, ourid, hisid);
1366 }
1367
1368
1369 /*
1370  * ipv6cp_finished - possibly shut down the lower layers.
1371  */
1372 static void
1373 ipv6cp_finished(fsm *f)
1374 {
1375     np_finished(f->unit, PPP_IPV6);
1376 }
1377
1378
1379 /*
1380  * ipv6cp_script_done - called when the ipv6-up or ipv6-down script
1381  * has finished.
1382  */
1383 static void
1384 ipv6cp_script_done(void *arg)
1385 {
1386     ipv6cp_script_pid = 0;
1387     switch (ipv6cp_script_state) {
1388     case s_up:
1389         if (ipv6cp_fsm[0].state != OPENED) {
1390             ipv6cp_script_state = s_down;
1391             ipv6cp_script(_PATH_IPV6DOWN);
1392         }
1393         break;
1394     case s_down:
1395         if (ipv6cp_fsm[0].state == OPENED) {
1396             ipv6cp_script_state = s_up;
1397             ipv6cp_script(_PATH_IPV6UP);
1398         }
1399         break;
1400     }
1401 }
1402
1403
1404 /*
1405  * ipv6cp_script - Execute a script with arguments
1406  * interface-name tty-name speed local-LL remote-LL.
1407  */
1408 static void
1409 ipv6cp_script(char *script)
1410 {
1411     char strspeed[32], strlocal[32], strremote[32];
1412     char *argv[8];
1413
1414     sprintf(strspeed, "%d", baud_rate);
1415     strcpy(strlocal, llv6_ntoa(ipv6cp_gotoptions[0].ourid));
1416     strcpy(strremote, llv6_ntoa(ipv6cp_hisoptions[0].hisid));
1417
1418     argv[0] = script;
1419     argv[1] = ifname;
1420     argv[2] = devnam;
1421     argv[3] = strspeed;
1422     argv[4] = strlocal;
1423     argv[5] = strremote;
1424     argv[6] = ipparam;
1425     argv[7] = NULL;
1426
1427     ipv6cp_script_pid = run_program(script, argv, 0, ipv6cp_script_done,
1428                                     NULL, 0);
1429 }
1430
1431 /*
1432  * ipv6cp_printpkt - print the contents of an IPV6CP packet.
1433  */
1434 static char *ipv6cp_codenames[] = {
1435     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1436     "TermReq", "TermAck", "CodeRej"
1437 };
1438
1439 static int
1440 ipv6cp_printpkt(u_char *p, int plen,
1441                 void (*printer) (void *, char *, ...), void *arg)
1442 {
1443     int code, id, len, olen;
1444     u_char *pstart, *optend;
1445     u_short cishort;
1446     eui64_t ifaceid;
1447
1448     if (plen < HEADERLEN)
1449         return 0;
1450     pstart = p;
1451     GETCHAR(code, p);
1452     GETCHAR(id, p);
1453     GETSHORT(len, p);
1454     if (len < HEADERLEN || len > plen)
1455         return 0;
1456
1457     if (code >= 1 && code <= sizeof(ipv6cp_codenames) / sizeof(char *))
1458         printer(arg, " %s", ipv6cp_codenames[code-1]);
1459     else
1460         printer(arg, " code=0x%x", code);
1461     printer(arg, " id=0x%x", id);
1462     len -= HEADERLEN;
1463     switch (code) {
1464     case CONFREQ:
1465     case CONFACK:
1466     case CONFNAK:
1467     case CONFREJ:
1468         /* print option list */
1469         while (len >= 2) {
1470             GETCHAR(code, p);
1471             GETCHAR(olen, p);
1472             p -= 2;
1473             if (olen < 2 || olen > len) {
1474                 break;
1475             }
1476             printer(arg, " <");
1477             len -= olen;
1478             optend = p + olen;
1479             switch (code) {
1480             case CI_COMPRESSTYPE:
1481                 if (olen >= CILEN_COMPRESS) {
1482                     p += 2;
1483                     GETSHORT(cishort, p);
1484                     printer(arg, "compress ");
1485                     printer(arg, "0x%x", cishort);
1486                 }
1487                 break;
1488             case CI_IFACEID:
1489                 if (olen == CILEN_IFACEID) {
1490                     p += 2;
1491                     eui64_get(ifaceid, p);
1492                     printer(arg, "addr %s", llv6_ntoa(ifaceid));
1493                 }
1494                 break;
1495             }
1496             while (p < optend) {
1497                 GETCHAR(code, p);
1498                 printer(arg, " %.2x", code);
1499             }
1500             printer(arg, ">");
1501         }
1502         break;
1503
1504     case TERMACK:
1505     case TERMREQ:
1506         if (len > 0 && *p >= ' ' && *p < 0x7f) {
1507             printer(arg, " ");
1508             print_string((char *)p, len, printer, arg);
1509             p += len;
1510             len = 0;
1511         }
1512         break;
1513     }
1514
1515     /* print the rest of the bytes in the packet */
1516     for (; len > 0; --len) {
1517         GETCHAR(code, p);
1518         printer(arg, " %.2x", code);
1519     }
1520
1521     return p - pstart;
1522 }
1523
1524 /*
1525  * ipv6_active_pkt - see if this IP packet is worth bringing the link up for.
1526  * We don't bring the link up for IP fragments or for TCP FIN packets
1527  * with no data.
1528  */
1529 #define IP6_HDRLEN      40      /* bytes */
1530 #define IP6_NHDR_FRAG   44      /* fragment IPv6 header */
1531 #define TCP_HDRLEN      20
1532 #define TH_FIN          0x01
1533
1534 /*
1535  * We use these macros because the IP header may be at an odd address,
1536  * and some compilers might use word loads to get th_off or ip_hl.
1537  */
1538
1539 #define get_ip6nh(x)    (((unsigned char *)(x))[6])
1540 #define get_tcpoff(x)   (((unsigned char *)(x))[12] >> 4)
1541 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1542
1543 static int
1544 ipv6_active_pkt(u_char *pkt, int len)
1545 {
1546     u_char *tcp;
1547
1548     len -= PPP_HDRLEN;
1549     pkt += PPP_HDRLEN;
1550     if (len < IP6_HDRLEN)
1551         return 0;
1552     if (get_ip6nh(pkt) == IP6_NHDR_FRAG)
1553         return 0;
1554     if (get_ip6nh(pkt) != IPPROTO_TCP)
1555         return 1;
1556     if (len < IP6_HDRLEN + TCP_HDRLEN)
1557         return 0;
1558     tcp = pkt + IP6_HDRLEN;
1559     if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == IP6_HDRLEN + get_tcpoff(tcp) * 4)
1560         return 0;
1561     return 1;
1562 }