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