Merge branch 'vendor/MDOCML'
[dragonfly.git] / usr.sbin / ppp / ipv6cp.c
1 /*-
2  * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/usr.sbin/ppp/ipv6cp.c,v 1.8.2.4 2003/04/29 16:04:41 ume Exp $
27  */
28
29 #include <sys/param.h>
30 #include <netinet/in_systm.h>
31 #include <netinet/in.h>
32 #include <netinet/ip.h>
33 #include <sys/socket.h>
34 #include <net/route.h>
35 #include <net/if.h>
36 #include <net/if_types.h>
37 #include <net/if_dl.h>
38 #include <sys/un.h>
39
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <termios.h>
45 #include <ifaddrs.h>
46
47 #include "layer.h"
48 #include "defs.h"
49 #include "mbuf.h"
50 #include "timer.h"
51 #include "fsm.h"
52 #include "iplist.h"
53 #include "throughput.h"
54 #include "slcompress.h"
55 #include "lqr.h"
56 #include "hdlc.h"
57 #include "lcp.h"
58 #include "ncpaddr.h"
59 #include "ip.h"
60 #include "ipcp.h"
61 #include "ipv6cp.h"
62 #include "filter.h"
63 #include "descriptor.h"
64 #include "ccp.h"
65 #include "link.h"
66 #include "mp.h"
67 #ifndef NORADIUS
68 #include "radius.h"
69 #endif
70 #include "ncp.h"
71 #include "bundle.h"
72 #include "route.h"
73 #include "iface.h"
74 #include "log.h"
75 #include "proto.h"
76 #include "command.h"
77 #include "prompt.h"
78 #include "async.h"
79 #include "physical.h"
80 #include "probe.h"
81 #include "systems.h"
82
83
84 #ifndef NOINET6
85 #define IN6ADDR_LINKLOCAL_MCAST_INIT \
86         {{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
87             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}}
88 static const struct in6_addr in6addr_linklocal_mcast =
89         IN6ADDR_LINKLOCAL_MCAST_INIT;
90
91 static int ipv6cp_LayerUp(struct fsm *);
92 static void ipv6cp_LayerDown(struct fsm *);
93 static void ipv6cp_LayerStart(struct fsm *);
94 static void ipv6cp_LayerFinish(struct fsm *);
95 static void ipv6cp_InitRestartCounter(struct fsm *, int);
96 static void ipv6cp_SendConfigReq(struct fsm *);
97 static void ipv6cp_SentTerminateReq(struct fsm *);
98 static void ipv6cp_SendTerminateAck(struct fsm *, u_char);
99 static void ipv6cp_DecodeConfig(struct fsm *, u_char *, u_char *, int,
100                                 struct fsm_decode *);
101
102 static struct fsm_callbacks ipv6cp_Callbacks = {
103   ipv6cp_LayerUp,
104   ipv6cp_LayerDown,
105   ipv6cp_LayerStart,
106   ipv6cp_LayerFinish,
107   ipv6cp_InitRestartCounter,
108   ipv6cp_SendConfigReq,
109   ipv6cp_SentTerminateReq,
110   ipv6cp_SendTerminateAck,
111   ipv6cp_DecodeConfig,
112   fsm_NullRecvResetReq,
113   fsm_NullRecvResetAck
114 };
115
116 static void
117 SetInterfaceID(u_char *ifid, int userandom)
118 {
119   struct ifaddrs *ifa, *ifap = NULL;
120   struct sockaddr_dl *sdl;
121   const u_long i32_max = 0xffffffff;
122   u_long r1, r2;
123
124   /* configure an interface ID based on Section 4.1 of RFC 2472 */
125   memset(ifid, 0, IPV6CP_IFIDLEN);
126
127   /*
128    * 1) If an IEEE global identifier (EUI-48 or EUI-64) is
129    * available anywhere on the node, it should be used to construct
130    * the tentative Interface-Identifier due to its uniqueness
131    * properties.
132    */
133   if (userandom)
134     goto randomid;
135   if (getifaddrs(&ifap) < 0)
136     goto randomid;
137         
138   for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
139     char *cp;
140
141     if (ifa->ifa_addr->sa_family != AF_LINK)
142       continue;
143
144     sdl = (struct sockaddr_dl *)ifa->ifa_addr;
145     if (sdl->sdl_alen < 6)
146       continue;
147     /* we're only interested in IEEE hardware addresses */
148     switch(sdl->sdl_type) {
149     case IFT_ETHER:
150     case IFT_FDDI:
151       /* XXX need more cases? */
152       break;
153     default:
154       continue;
155     }
156
157     cp = (char *)(sdl->sdl_data + sdl->sdl_nlen);
158     ifid[0] = cp[0];
159     ifid[0] ^= 0x02; /* reverse the u/l bit*/
160     ifid[1] = cp[1];
161     ifid[2] = cp[2];
162     ifid[3] = 0xff;
163     ifid[4] = 0xfe;
164     ifid[5] = cp[3];
165     ifid[6] = cp[4];
166     ifid[7] = cp[5];
167
168     freeifaddrs(ifap);
169     return;
170   }
171
172   freeifaddrs(ifap);
173
174   /*
175    * 2) If an IEEE global identifier is not available a different source
176    * of uniqueness should be used.
177    * XXX: we skip this case.
178    */
179
180   /*
181    * 3) If a good source of uniqueness cannot be found, it is
182    * recommended that a random number be generated.  In this case the
183    * "u" bit of the interface identifier MUST be set to zero (0).
184    */
185  randomid:
186   randinit();
187   r1 = (((u_long)random()) % i32_max) + 1;
188   r2 = (((u_long)random()) % i32_max) + 1;
189   memcpy(ifid, &r1, sizeof(r1));
190   memcpy(ifid + 4, &r2, sizeof(r2));
191   ifid[0] &= 0xfd;
192   return;
193 }
194
195 static int
196 ipcp_SetIPv6address(struct ipv6cp *ipv6cp, u_char *myifid, u_char *hisifid)
197 {
198   struct bundle *bundle = ipv6cp->fsm.bundle;
199   struct in6_addr myaddr, hisaddr;
200   struct ncprange myrange, range;
201   struct ncpaddr addr;
202   struct sockaddr_storage ssdst, ssgw, ssmask;
203   struct sockaddr *sadst, *sagw, *samask;
204
205   sadst = (struct sockaddr *)&ssdst;
206   sagw = (struct sockaddr *)&ssgw;
207   samask = (struct sockaddr *)&ssmask;
208
209   memset(&myaddr, '\0', sizeof myaddr);
210   memset(&hisaddr, '\0', sizeof hisaddr);
211
212   myaddr.s6_addr[0] = 0xfe;
213   myaddr.s6_addr[1] = 0x80;
214   memcpy(&myaddr.s6_addr[8], myifid, IPV6CP_IFIDLEN);
215 #if 0
216   myaddr.s6_addr[8] |= 0x02;    /* set 'universal' bit */
217 #endif
218
219   hisaddr.s6_addr[0] = 0xfe;
220   hisaddr.s6_addr[1] = 0x80;
221   memcpy(&hisaddr.s6_addr[8], hisifid, IPV6CP_IFIDLEN);
222 #if 0
223   hisaddr.s6_addr[8] |= 0x02;   /* set 'universal' bit */
224 #endif
225
226   ncpaddr_setip6(&ipv6cp->myaddr, &myaddr);
227   ncpaddr_setip6(&ipv6cp->hisaddr, &hisaddr);
228   ncprange_set(&myrange, &ipv6cp->myaddr, 64);
229
230   if (!iface_Add(bundle->iface, &bundle->ncp, &myrange, &ipv6cp->hisaddr,
231                  IFACE_ADD_FIRST|IFACE_FORCE_ADD|IFACE_SYSTEM))
232     return 0;
233
234   if (!Enabled(bundle, OPT_IFACEALIAS))
235     iface_Clear(bundle->iface, &bundle->ncp, AF_INET6,
236                 IFACE_CLEAR_ALIASES|IFACE_SYSTEM);
237
238   ncpaddr_setip6(&addr, &in6addr_linklocal_mcast);
239   ncprange_set(&range, &addr, 32);
240   rt_Set(bundle, RTM_ADD, &range, &ipv6cp->myaddr, 1, 0);
241
242   if (bundle->ncp.cfg.sendpipe > 0 || bundle->ncp.cfg.recvpipe > 0) {
243     ncprange_getsa(&myrange, &ssgw, &ssmask);
244     if (ncpaddr_isset(&ipv6cp->hisaddr))
245       ncpaddr_getsa(&ipv6cp->hisaddr, &ssdst);
246     else
247       sadst = NULL;
248     rt_Update(bundle, sadst, sagw, samask);
249   }
250
251   if (Enabled(bundle, OPT_SROUTES))
252     route_Change(bundle, bundle->ncp.route, &ipv6cp->myaddr, &ipv6cp->hisaddr);
253
254 #ifndef NORADIUS
255   if (bundle->radius.valid)
256     route_Change(bundle, bundle->radius.routes, &ipv6cp->myaddr,
257                  &ipv6cp->hisaddr);
258 #endif
259
260   return 1;     /* Ok */
261 }
262
263 void
264 ipv6cp_Init(struct ipv6cp *ipv6cp, struct bundle *bundle, struct link *l,
265                  const struct fsm_parent *parent)
266 {
267   static const char * const timer_names[] =
268     {"IPV6CP restart", "IPV6CP openmode", "IPV6CP stopped"};
269   int n;
270
271   fsm_Init(&ipv6cp->fsm, "IPV6CP", PROTO_IPV6CP, 1, IPV6CP_MAXCODE, LogIPV6CP,
272            bundle, l, parent, &ipv6cp_Callbacks, timer_names);
273
274   ipv6cp->cfg.fsm.timeout = DEF_FSMRETRY;
275   ipv6cp->cfg.fsm.maxreq = DEF_FSMTRIES;
276   ipv6cp->cfg.fsm.maxtrm = DEF_FSMTRIES;
277
278   SetInterfaceID(ipv6cp->my_ifid, 0);
279   do {
280     SetInterfaceID(ipv6cp->his_ifid, 1);
281   } while (memcmp(ipv6cp->his_ifid, ipv6cp->my_ifid, IPV6CP_IFIDLEN) == 0);
282
283   if (probe.ipv6_available) {
284     n = 100;
285     while (n &&
286            !ipcp_SetIPv6address(ipv6cp, ipv6cp->my_ifid, ipv6cp->his_ifid)) {
287       do {
288         n--;
289         SetInterfaceID(ipv6cp->my_ifid, 1);
290       } while (n
291         && memcmp(ipv6cp->his_ifid, ipv6cp->my_ifid, IPV6CP_IFIDLEN) == 0);
292     }
293   }
294
295   throughput_init(&ipv6cp->throughput, SAMPLE_PERIOD);
296   memset(ipv6cp->Queue, '\0', sizeof ipv6cp->Queue);
297   ipv6cp_Setup(ipv6cp);
298 }
299
300 void
301 ipv6cp_Destroy(struct ipv6cp *ipv6cp)
302 {
303   throughput_destroy(&ipv6cp->throughput);
304 }
305
306 void
307 ipv6cp_Setup(struct ipv6cp *ipv6cp)
308 {
309   ncpaddr_init(&ipv6cp->myaddr);
310   ncpaddr_init(&ipv6cp->hisaddr);
311
312   ipv6cp->his_reject = 0;
313   ipv6cp->my_reject = 0;
314 }
315
316 void
317 ipv6cp_SetLink(struct ipv6cp *ipv6cp, struct link *l)
318 {
319   ipv6cp->fsm.link = l;
320 }
321
322 int
323 ipv6cp_Show(struct cmdargs const *arg)
324 {
325   struct ipv6cp *ipv6cp = &arg->bundle->ncp.ipv6cp;
326
327   prompt_Printf(arg->prompt, "%s [%s]\n", ipv6cp->fsm.name,
328                 State2Nam(ipv6cp->fsm.state));
329   if (ipv6cp->fsm.state == ST_OPENED) {
330     prompt_Printf(arg->prompt, " His side:        %s\n",
331                   ncpaddr_ntoa(&ipv6cp->hisaddr));
332     prompt_Printf(arg->prompt, " My side:         %s\n",
333                   ncpaddr_ntoa(&ipv6cp->myaddr));
334     prompt_Printf(arg->prompt, " Queued packets:  %lu\n",
335                   (unsigned long)ipv6cp_QueueLen(ipv6cp));
336   }
337
338   prompt_Printf(arg->prompt, "\nDefaults:\n");
339   prompt_Printf(arg->prompt, "  FSM retry = %us, max %u Config"
340                 " REQ%s, %u Term REQ%s\n\n", ipv6cp->cfg.fsm.timeout,
341                 ipv6cp->cfg.fsm.maxreq, ipv6cp->cfg.fsm.maxreq == 1 ? "" : "s",
342                 ipv6cp->cfg.fsm.maxtrm, ipv6cp->cfg.fsm.maxtrm == 1 ? "" : "s");
343
344   throughput_disp(&ipv6cp->throughput, arg->prompt);
345
346   return 0;
347 }
348
349 struct mbuf *
350 ipv6cp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
351 {
352   /* Got PROTO_IPV6CP from link */
353   m_settype(bp, MB_IPV6CPIN);
354   if (bundle_Phase(bundle) == PHASE_NETWORK)
355     fsm_Input(&bundle->ncp.ipv6cp.fsm, bp);
356   else {
357     if (bundle_Phase(bundle) < PHASE_NETWORK)
358       log_Printf(LogIPV6CP, "%s: Error: Unexpected IPV6CP in phase %s"
359                  " (ignored)\n", l->name, bundle_PhaseName(bundle));
360     m_freem(bp);
361   }
362   return NULL;
363 }
364
365 void
366 ipv6cp_AddInOctets(struct ipv6cp *ipv6cp, int n)
367 {
368   throughput_addin(&ipv6cp->throughput, n);
369 }
370
371 void
372 ipv6cp_AddOutOctets(struct ipv6cp *ipv6cp, int n)
373 {
374   throughput_addout(&ipv6cp->throughput, n);
375 }
376
377 void
378 ipv6cp_IfaceAddrAdded(struct ipv6cp *ipv6cp __unused,
379                       const struct iface_addr *addr __unused)
380 {
381 }
382
383 void
384 ipv6cp_IfaceAddrDeleted(struct ipv6cp *ipv6cp __unused,
385                         const struct iface_addr *addr __unused)
386 {
387 }
388
389 int
390 ipv6cp_InterfaceUp(struct ipv6cp *ipv6cp)
391 {
392   if (!ipcp_SetIPv6address(ipv6cp, ipv6cp->my_ifid, ipv6cp->his_ifid)) {
393     log_Printf(LogERROR, "ipv6cp_InterfaceUp: unable to set ipv6 address\n");
394     return 0;
395   }
396
397   if (!iface_SetFlags(ipv6cp->fsm.bundle->iface->name, IFF_UP)) {
398     log_Printf(LogERROR, "ipv6cp_InterfaceUp: Can't set the IFF_UP"
399                " flag on %s\n", ipv6cp->fsm.bundle->iface->name);
400     return 0;
401   }
402
403   return 1;
404 }
405
406 size_t
407 ipv6cp_QueueLen(struct ipv6cp *ipv6cp)
408 {
409   struct mqueue *q;
410   size_t result;
411
412   result = 0;
413   for (q = ipv6cp->Queue; q < ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp); q++)
414     result += q->len;
415
416   return result;
417 }
418
419 int
420 ipv6cp_PushPacket(struct ipv6cp *ipv6cp, struct link *l)
421 {
422   struct bundle *bundle = ipv6cp->fsm.bundle;
423   struct mqueue *queue;
424   struct mbuf *bp;
425   int m_len;
426   u_int32_t secs = 0;
427   unsigned alivesecs = 0;
428
429   if (ipv6cp->fsm.state != ST_OPENED)
430     return 0;
431
432   /*
433    * If ccp is not open but is required, do nothing.
434    */
435   if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) {
436     log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name);
437     return 0;
438   }
439
440   queue = ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp) - 1;
441   do {
442     if (queue->top) {
443       bp = m_dequeue(queue);
444       bp = mbuf_Read(bp, &secs, sizeof secs);
445       bp = m_pullup(bp);
446       m_len = m_length(bp);
447       if (!FilterCheck(MBUF_CTOP(bp), AF_INET6, &bundle->filter.alive,
448                        &alivesecs)) {
449         if (secs == 0)
450           secs = alivesecs;
451         bundle_StartIdleTimer(bundle, secs);
452       }
453       link_PushPacket(l, bp, bundle, 0, PROTO_IPV6);
454       ipv6cp_AddOutOctets(ipv6cp, m_len);
455       return 1;
456     }
457   } while (queue-- != ipv6cp->Queue);
458
459   return 0;
460 }
461
462 static int
463 ipv6cp_LayerUp(struct fsm *fp)
464 {
465   /* We're now up */
466   struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
467   char tbuff[40];
468
469   log_Printf(LogIPV6CP, "%s: LayerUp.\n", fp->link->name);
470   if (!ipv6cp_InterfaceUp(ipv6cp))
471     return 0;
472
473   snprintf(tbuff, sizeof tbuff, "%s", ncpaddr_ntoa(&ipv6cp->myaddr));
474   log_Printf(LogIPV6CP, "myaddr %s hisaddr = %s\n",
475              tbuff, ncpaddr_ntoa(&ipv6cp->hisaddr));
476
477   /* XXX: Call radius_Account() */
478
479   if (!Enabled(fp->bundle, OPT_IPCP)) {
480     /*
481      * XXX this stuff should really live in the FSM.  Our config should
482      * associate executable sections in files with events.
483      */
484     if (system_Select(fp->bundle, tbuff, LINKUPFILE, NULL, NULL) < 0) {
485       if (bundle_GetLabel(fp->bundle)) {
486         if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle),
487                           LINKUPFILE, NULL, NULL) < 0)
488           system_Select(fp->bundle, "MYADDR6", LINKUPFILE, NULL, NULL);
489       } else
490         system_Select(fp->bundle, "MYADDR6", LINKUPFILE, NULL, NULL);
491     }
492   }
493
494   fp->more.reqs = fp->more.naks = fp->more.rejs = ipv6cp->cfg.fsm.maxreq * 3;
495   log_DisplayPrompts();
496
497   return 1;
498 }
499
500 static void
501 ipv6cp_LayerDown(struct fsm *fp)
502 {
503   /* About to come down */
504   struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
505   static int recursing;
506   char addr[40];
507
508   if (!recursing++) {
509     snprintf(addr, sizeof addr, "%s", ncpaddr_ntoa(&ipv6cp->myaddr));
510     log_Printf(LogIPV6CP, "%s: LayerDown: %s\n", fp->link->name, addr);
511
512     /* XXX: Call radius_Account() */
513
514     if (!Enabled(fp->bundle, OPT_IPCP)) {
515       /*
516        * XXX this stuff should really live in the FSM.  Our config should
517        * associate executable sections in files with events.
518        */
519       if (system_Select(fp->bundle, addr, LINKDOWNFILE, NULL, NULL) < 0) {
520         if (bundle_GetLabel(fp->bundle)) {
521           if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle),
522                             LINKDOWNFILE, NULL, NULL) < 0)
523             system_Select(fp->bundle, "MYADDR6", LINKDOWNFILE, NULL, NULL);
524         } else
525           system_Select(fp->bundle, "MYADDR6", LINKDOWNFILE, NULL, NULL);
526       }
527     }
528
529     ipv6cp_Setup(ipv6cp);
530   }
531   recursing--;
532 }
533
534 static void
535 ipv6cp_LayerStart(struct fsm *fp)
536 {
537   /* We're about to start up ! */
538   struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
539
540   log_Printf(LogIPV6CP, "%s: LayerStart.\n", fp->link->name);
541   throughput_start(&ipv6cp->throughput, "IPV6CP throughput",
542                    Enabled(fp->bundle, OPT_THROUGHPUT));
543   fp->more.reqs = fp->more.naks = fp->more.rejs = ipv6cp->cfg.fsm.maxreq * 3;
544   ipv6cp->peer_tokenreq = 0;
545 }
546
547 static void
548 ipv6cp_LayerFinish(struct fsm *fp)
549 {
550   /* We're now down */
551   struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
552
553   log_Printf(LogIPV6CP, "%s: LayerFinish.\n", fp->link->name);
554   throughput_stop(&ipv6cp->throughput);
555   throughput_log(&ipv6cp->throughput, LogIPV6CP, NULL);
556 }
557
558 static void
559 ipv6cp_InitRestartCounter(struct fsm *fp, int what)
560 {
561   /* Set fsm timer load */
562   struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
563
564   fp->FsmTimer.load = ipv6cp->cfg.fsm.timeout * SECTICKS;
565   switch (what) {
566     case FSM_REQ_TIMER:
567       fp->restart = ipv6cp->cfg.fsm.maxreq;
568       break;
569     case FSM_TRM_TIMER:
570       fp->restart = ipv6cp->cfg.fsm.maxtrm;
571       break;
572     default:
573       fp->restart = 1;
574       break;
575   }
576 }
577
578 static void
579 ipv6cp_SendConfigReq(struct fsm *fp)
580 {
581   /* Send config REQ please */
582   struct physical *p = link2physical(fp->link);
583   struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
584   u_char buff[IPV6CP_IFIDLEN+2];
585   struct fsm_opt *o;
586
587   o = (struct fsm_opt *)buff;
588
589   if ((p && !physical_IsSync(p)) || !REJECTED(ipv6cp, TY_TOKEN)) {
590     memcpy(o->data, ipv6cp->my_ifid, IPV6CP_IFIDLEN);
591     INC_FSM_OPT(TY_TOKEN, IPV6CP_IFIDLEN + 2, o);
592   }
593
594   fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, (u_char *)o - buff,
595              MB_IPV6CPOUT);
596 }
597
598 static void
599 ipv6cp_SentTerminateReq(struct fsm *fp __unused)
600 {
601   /* Term REQ just sent by FSM */
602 }
603
604 static void
605 ipv6cp_SendTerminateAck(struct fsm *fp, u_char id)
606 {
607   /* Send Term ACK please */
608   fsm_Output(fp, CODE_TERMACK, id, NULL, 0, MB_IPV6CPOUT);
609 }
610
611 static const char *
612 protoname(unsigned proto)
613 {
614   static const char *cftypes[] = { "IFACEID", "COMPPROTO" };
615
616   if (proto > 0 && proto <= sizeof cftypes / sizeof *cftypes)
617     return cftypes[proto - 1];
618
619   return NumStr(proto, NULL, 0);
620 }
621
622 static void
623 ipv6cp_ValidateInterfaceID(struct ipv6cp *ipv6cp, u_char *ifid,
624                            struct fsm_decode *dec)
625 {
626   struct fsm_opt opt;
627   u_char zero[IPV6CP_IFIDLEN];
628
629   memset(zero, 0, IPV6CP_IFIDLEN);
630
631   if (memcmp(ifid, zero, IPV6CP_IFIDLEN) != 0
632       && memcmp(ifid, ipv6cp->my_ifid, IPV6CP_IFIDLEN) != 0)
633     memcpy(ipv6cp->his_ifid, ifid, IPV6CP_IFIDLEN);
634
635   opt.hdr.id = TY_TOKEN;
636   opt.hdr.len = IPV6CP_IFIDLEN + 2;
637   memcpy(opt.data, &ipv6cp->his_ifid, IPV6CP_IFIDLEN);
638   if (memcmp(ifid, ipv6cp->his_ifid, IPV6CP_IFIDLEN) == 0)
639     fsm_ack(dec, &opt);
640   else
641     fsm_nak(dec, &opt);
642 }
643
644 static void
645 ipv6cp_DecodeConfig(struct fsm *fp, u_char *cp, u_char *end, int mode_type,
646                     struct fsm_decode *dec)
647 {
648   /* Deal with incoming PROTO_IPV6CP */
649   struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
650   int n;
651   char tbuff[100];
652   u_char ifid[IPV6CP_IFIDLEN], zero[IPV6CP_IFIDLEN];
653   struct fsm_opt *opt;
654
655   memset(zero, 0, IPV6CP_IFIDLEN);
656
657   while (end - cp >= (int)sizeof(opt->hdr)) {
658     if ((opt = fsm_readopt(&cp)) == NULL)
659       break;
660
661     snprintf(tbuff, sizeof tbuff, " %s[%d]", protoname(opt->hdr.id),
662              opt->hdr.len);
663
664     switch (opt->hdr.id) {
665     case TY_TOKEN:
666       memcpy(ifid, opt->data, IPV6CP_IFIDLEN);
667       log_Printf(LogIPV6CP, "%s 0x%02x%02x%02x%02x%02x%02x%02x%02x\n", tbuff,
668                  ifid[0], ifid[1], ifid[2], ifid[3], ifid[4], ifid[5], ifid[6], ifid[7]);
669
670       switch (mode_type) {
671       case MODE_REQ:
672         ipv6cp->peer_tokenreq = 1;
673         ipv6cp_ValidateInterfaceID(ipv6cp, ifid, dec);
674         break;
675
676       case MODE_NAK:
677         if (memcmp(ifid, zero, IPV6CP_IFIDLEN) == 0) {
678           log_Printf(log_IsKept(LogIPV6CP) ? LogIPV6CP : LogPHASE,
679                      "0x0000000000000000: Unacceptable IntefaceID!\n");
680           fsm_Close(&ipv6cp->fsm);
681         } else if (memcmp(ifid, ipv6cp->his_ifid, IPV6CP_IFIDLEN) == 0) {
682           log_Printf(log_IsKept(LogIPV6CP) ? LogIPV6CP : LogPHASE,
683                      "0x%02x%02x%02x%02x%02x%02x%02x%02x: "
684                      "Unacceptable IntefaceID!\n",
685                      ifid[0], ifid[1], ifid[2], ifid[3],
686                      ifid[4], ifid[5], ifid[6], ifid[7]);
687         } else if (memcmp(ifid, ipv6cp->my_ifid, IPV6CP_IFIDLEN) != 0) {
688           n = 100;
689           while (n && !ipcp_SetIPv6address(ipv6cp, ifid, ipv6cp->his_ifid)) {
690             do {
691               n--;
692               SetInterfaceID(ifid, 1);
693             } while (n && memcmp(ifid, ipv6cp->his_ifid, IPV6CP_IFIDLEN) == 0);
694           }
695
696           if (n == 0) {
697             log_Printf(log_IsKept(LogIPV6CP) ? LogIPV6CP : LogPHASE,
698                        "0x0000000000000000: Unacceptable IntefaceID!\n");
699             fsm_Close(&ipv6cp->fsm);
700           } else {
701             log_Printf(LogIPV6CP, "%s changing IntefaceID: "
702                        "0x%02x%02x%02x%02x%02x%02x%02x%02x "
703                        "--> 0x%02x%02x%02x%02x%02x%02x%02x%02x\n", tbuff,
704                        ipv6cp->my_ifid[0], ipv6cp->my_ifid[1],
705                        ipv6cp->my_ifid[2], ipv6cp->my_ifid[3],
706                        ipv6cp->my_ifid[4], ipv6cp->my_ifid[5],
707                        ipv6cp->my_ifid[6], ipv6cp->my_ifid[7],
708                        ifid[0], ifid[1], ifid[2], ifid[3],
709                        ifid[4], ifid[5], ifid[6], ifid[7]);
710             memcpy(ipv6cp->my_ifid, ifid, IPV6CP_IFIDLEN);
711             bundle_AdjustFilters(fp->bundle, &ipv6cp->myaddr, NULL);
712           }
713         }
714         break;
715
716       case MODE_REJ:
717         ipv6cp->his_reject |= (1 << opt->hdr.id);
718         break;
719       }
720       break;
721
722     default:
723       if (mode_type != MODE_NOP) {
724         ipv6cp->my_reject |= (1 << opt->hdr.id);
725         fsm_rej(dec, opt);
726       }
727       break;
728     }
729   }
730
731   if (mode_type != MODE_NOP) {
732     if (mode_type == MODE_REQ && !ipv6cp->peer_tokenreq) {
733       if (dec->rejend == dec->rej && dec->nakend == dec->nak) {
734         /*
735          * Pretend the peer has requested a TOKEN.
736          * We do this to ensure that we only send one NAK if the only
737          * reason for the NAK is because the peer isn't sending a
738          * TY_TOKEN REQ.  This stops us from repeatedly trying to tell
739          * the peer that we have to have an IP address on their end.
740          */
741         ipv6cp->peer_tokenreq = 1;
742       }
743       memset(ifid, 0, IPV6CP_IFIDLEN);
744       ipv6cp_ValidateInterfaceID(ipv6cp, ifid, dec);
745     }
746     fsm_opt_normalise(dec);
747   }
748 }
749 #endif