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