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