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