2 * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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>
49 #include "throughput.h"
50 #include "slcompress.h"
59 #include "descriptor.h"
80 static int ipv6cp_LayerUp(struct fsm *);
81 static void ipv6cp_LayerDown(struct fsm *);
82 static void ipv6cp_LayerStart(struct fsm *);
83 static void ipv6cp_LayerFinish(struct fsm *);
84 static void ipv6cp_InitRestartCounter(struct fsm *, int);
85 static void ipv6cp_SendConfigReq(struct fsm *);
86 static void ipv6cp_SentTerminateReq(struct fsm *);
87 static void ipv6cp_SendTerminateAck(struct fsm *, u_char);
88 static void ipv6cp_DecodeConfig(struct fsm *, u_char *, u_char *, int,
91 static struct fsm_callbacks ipv6cp_Callbacks = {
96 ipv6cp_InitRestartCounter,
98 ipv6cp_SentTerminateReq,
99 ipv6cp_SendTerminateAck,
101 fsm_NullRecvResetReq,
108 /* Generate random number which will be used as negotiation token */
115 ipcp_SetIPv6address(struct ipv6cp *ipv6cp, u_int32_t mytok, u_int32_t histok)
117 struct bundle *bundle = ipv6cp->fsm.bundle;
118 struct in6_addr myaddr, hisaddr;
119 struct ncprange myrange;
120 struct sockaddr_storage ssdst, ssgw, ssmask;
121 struct sockaddr *sadst, *sagw, *samask;
123 sadst = (struct sockaddr *)&ssdst;
124 sagw = (struct sockaddr *)&ssgw;
125 samask = (struct sockaddr *)&ssmask;
127 memset(&myaddr, '\0', sizeof myaddr);
128 memset(&hisaddr, '\0', sizeof hisaddr);
130 myaddr.s6_addr[0] = 0xfe;
131 myaddr.s6_addr[1] = 0x80;
132 *(u_int32_t *)(myaddr.s6_addr + 12) = htonl(mytok);
134 hisaddr.s6_addr[0] = 0xfe;
135 hisaddr.s6_addr[1] = 0x80;
136 *(u_int32_t *)(hisaddr.s6_addr + 12) = htonl(histok);
138 ncpaddr_setip6(&ipv6cp->myaddr, &myaddr);
139 ncpaddr_setip6(&ipv6cp->hisaddr, &hisaddr);
140 ncprange_sethost(&myrange, &ipv6cp->myaddr);
142 if (!iface_Add(bundle->iface, &bundle->ncp, &myrange, &ipv6cp->hisaddr,
143 IFACE_ADD_FIRST|IFACE_FORCE_ADD|IFACE_SYSTEM))
146 if (!Enabled(bundle, OPT_IFACEALIAS))
147 iface_Clear(bundle->iface, &bundle->ncp, AF_INET6,
148 IFACE_CLEAR_ALIASES|IFACE_SYSTEM);
150 if (bundle->ncp.cfg.sendpipe > 0 || bundle->ncp.cfg.recvpipe > 0) {
151 ncprange_getsa(&myrange, &ssgw, &ssmask);
152 if (ncpaddr_isset(&ipv6cp->hisaddr))
153 ncpaddr_getsa(&ipv6cp->hisaddr, &ssdst);
156 rt_Update(bundle, sadst, sagw, samask);
159 if (Enabled(bundle, OPT_SROUTES))
160 route_Change(bundle, bundle->ncp.route, &ipv6cp->myaddr, &ipv6cp->hisaddr);
163 if (bundle->radius.valid)
164 route_Change(bundle, bundle->radius.routes, &ipv6cp->myaddr,
172 ipv6cp_Init(struct ipv6cp *ipv6cp, struct bundle *bundle, struct link *l,
173 const struct fsm_parent *parent)
175 static const char * const timer_names[] =
176 {"IPV6CP restart", "IPV6CP openmode", "IPV6CP stopped"};
179 fsm_Init(&ipv6cp->fsm, "IPV6CP", PROTO_IPV6CP, 1, IPV6CP_MAXCODE, LogIPV6CP,
180 bundle, l, parent, &ipv6cp_Callbacks, timer_names);
182 ipv6cp->cfg.fsm.timeout = DEF_FSMRETRY;
183 ipv6cp->cfg.fsm.maxreq = DEF_FSMTRIES;
184 ipv6cp->cfg.fsm.maxtrm = DEF_FSMTRIES;
186 ipv6cp->my_token = GenerateToken();
187 while ((ipv6cp->peer_token = GenerateToken()) == ipv6cp->my_token)
190 if (probe.ipv6_available) {
193 !ipcp_SetIPv6address(ipv6cp, ipv6cp->my_token, ipv6cp->peer_token)) {
195 while (n && (ipv6cp->my_token = GenerateToken()) == ipv6cp->peer_token)
200 throughput_init(&ipv6cp->throughput, SAMPLE_PERIOD);
201 memset(ipv6cp->Queue, '\0', sizeof ipv6cp->Queue);
202 ipv6cp_Setup(ipv6cp);
206 ipv6cp_Destroy(struct ipv6cp *ipv6cp)
208 throughput_destroy(&ipv6cp->throughput);
212 ipv6cp_Setup(struct ipv6cp *ipv6cp)
214 ncpaddr_init(&ipv6cp->myaddr);
215 ncpaddr_init(&ipv6cp->hisaddr);
217 ipv6cp->his_reject = 0;
218 ipv6cp->my_reject = 0;
222 ipv6cp_SetLink(struct ipv6cp *ipv6cp, struct link *l)
224 ipv6cp->fsm.link = l;
228 ipv6cp_Show(struct cmdargs const *arg)
230 struct ipv6cp *ipv6cp = &arg->bundle->ncp.ipv6cp;
232 prompt_Printf(arg->prompt, "%s [%s]\n", ipv6cp->fsm.name,
233 State2Nam(ipv6cp->fsm.state));
234 if (ipv6cp->fsm.state == ST_OPENED) {
235 prompt_Printf(arg->prompt, " His side: %s\n",
236 ncpaddr_ntoa(&ipv6cp->hisaddr));
237 prompt_Printf(arg->prompt, " My side: %s\n",
238 ncpaddr_ntoa(&ipv6cp->myaddr));
239 prompt_Printf(arg->prompt, " Queued packets: %lu\n",
240 (unsigned long)ipv6cp_QueueLen(ipv6cp));
243 prompt_Printf(arg->prompt, "\nDefaults:\n");
244 prompt_Printf(arg->prompt, " FSM retry = %us, max %u Config"
245 " REQ%s, %u Term REQ%s\n\n", ipv6cp->cfg.fsm.timeout,
246 ipv6cp->cfg.fsm.maxreq, ipv6cp->cfg.fsm.maxreq == 1 ? "" : "s",
247 ipv6cp->cfg.fsm.maxtrm, ipv6cp->cfg.fsm.maxtrm == 1 ? "" : "s");
249 throughput_disp(&ipv6cp->throughput, arg->prompt);
255 ipv6cp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
257 /* Got PROTO_IPV6CP from link */
258 m_settype(bp, MB_IPV6CPIN);
259 if (bundle_Phase(bundle) == PHASE_NETWORK)
260 fsm_Input(&bundle->ncp.ipv6cp.fsm, bp);
262 if (bundle_Phase(bundle) < PHASE_NETWORK)
263 log_Printf(LogIPV6CP, "%s: Error: Unexpected IPV6CP in phase %s"
264 " (ignored)\n", l->name, bundle_PhaseName(bundle));
271 ipv6cp_AddInOctets(struct ipv6cp *ipv6cp, int n)
273 throughput_addin(&ipv6cp->throughput, n);
277 ipv6cp_AddOutOctets(struct ipv6cp *ipv6cp, int n)
279 throughput_addout(&ipv6cp->throughput, n);
283 ipv6cp_IfaceAddrAdded(struct ipv6cp *ipv6cp, const struct iface_addr *addr)
288 ipv6cp_IfaceAddrDeleted(struct ipv6cp *ipv6cp, const struct iface_addr *addr)
293 ipv6cp_InterfaceUp(struct ipv6cp *ipv6cp)
295 if (!ipcp_SetIPv6address(ipv6cp, ipv6cp->my_token, ipv6cp->peer_token)) {
296 log_Printf(LogERROR, "ipv6cp_InterfaceUp: unable to set ipv6 address\n");
300 if (!iface_SetFlags(ipv6cp->fsm.bundle->iface->name, IFF_UP)) {
301 log_Printf(LogERROR, "ipv6cp_InterfaceUp: Can't set the IFF_UP"
302 " flag on %s\n", ipv6cp->fsm.bundle->iface->name);
310 ipv6cp_QueueLen(struct ipv6cp *ipv6cp)
316 for (q = ipv6cp->Queue; q < ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp); q++)
323 ipv6cp_PushPacket(struct ipv6cp *ipv6cp, struct link *l)
325 struct bundle *bundle = ipv6cp->fsm.bundle;
326 struct mqueue *queue;
330 unsigned alivesecs = 0;
332 if (ipv6cp->fsm.state != ST_OPENED)
336 * If ccp is not open but is required, do nothing.
338 if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) {
339 log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name);
343 queue = ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp) - 1;
346 bp = m_dequeue(queue);
347 bp = mbuf_Read(bp, &secs, sizeof secs);
349 m_len = m_length(bp);
350 if (!FilterCheck(MBUF_CTOP(bp), AF_INET6, &bundle->filter.alive,
354 bundle_StartIdleTimer(bundle, secs);
356 link_PushPacket(l, bp, bundle, 0, PROTO_IPV6);
357 ipv6cp_AddOutOctets(ipv6cp, m_len);
360 } while (queue-- != ipv6cp->Queue);
366 ipv6cp_LayerUp(struct fsm *fp)
369 struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
372 log_Printf(LogIPV6CP, "%s: LayerUp.\n", fp->link->name);
373 if (!ipv6cp_InterfaceUp(ipv6cp))
376 snprintf(tbuff, sizeof tbuff, "%s", ncpaddr_ntoa(&ipv6cp->myaddr));
377 log_Printf(LogIPV6CP, "myaddr %s hisaddr = %s\n",
378 tbuff, ncpaddr_ntoa(&ipv6cp->hisaddr));
380 /* XXX: Call radius_Account() and system_Select() */
382 fp->more.reqs = fp->more.naks = fp->more.rejs = ipv6cp->cfg.fsm.maxreq * 3;
383 log_DisplayPrompts();
389 ipv6cp_LayerDown(struct fsm *fp)
391 /* About to come down */
392 struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
393 static int recursing;
397 snprintf(addr, sizeof addr, "%s", ncpaddr_ntoa(&ipv6cp->myaddr));
398 log_Printf(LogIPV6CP, "%s: LayerDown: %s\n", fp->link->name, addr);
400 /* XXX: Call radius_Account() and system_Select() */
402 ipv6cp_Setup(ipv6cp);
408 ipv6cp_LayerStart(struct fsm *fp)
410 /* We're about to start up ! */
411 struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
413 log_Printf(LogIPV6CP, "%s: LayerStart.\n", fp->link->name);
414 throughput_start(&ipv6cp->throughput, "IPV6CP throughput",
415 Enabled(fp->bundle, OPT_THROUGHPUT));
416 fp->more.reqs = fp->more.naks = fp->more.rejs = ipv6cp->cfg.fsm.maxreq * 3;
417 ipv6cp->peer_tokenreq = 0;
421 ipv6cp_LayerFinish(struct fsm *fp)
424 struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
426 log_Printf(LogIPV6CP, "%s: LayerFinish.\n", fp->link->name);
427 throughput_stop(&ipv6cp->throughput);
428 throughput_log(&ipv6cp->throughput, LogIPV6CP, NULL);
432 ipv6cp_InitRestartCounter(struct fsm *fp, int what)
434 /* Set fsm timer load */
435 struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
437 fp->FsmTimer.load = ipv6cp->cfg.fsm.timeout * SECTICKS;
440 fp->restart = ipv6cp->cfg.fsm.maxreq;
443 fp->restart = ipv6cp->cfg.fsm.maxtrm;
452 ipv6cp_SendConfigReq(struct fsm *fp)
454 /* Send config REQ please */
455 struct physical *p = link2physical(fp->link);
456 struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
460 o = (struct fsm_opt *)buff;
462 if ((p && !physical_IsSync(p)) || !REJECTED(ipv6cp, TY_TOKEN)) {
463 memcpy(o->data, &ipv6cp->my_token, 4);
464 INC_FSM_OPT(TY_TOKEN, 6, o);
467 fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, (u_char *)o - buff,
472 ipv6cp_SentTerminateReq(struct fsm *fp)
474 /* Term REQ just sent by FSM */
478 ipv6cp_SendTerminateAck(struct fsm *fp, u_char id)
480 /* Send Term ACK please */
481 fsm_Output(fp, CODE_TERMACK, id, NULL, 0, MB_IPV6CPOUT);
487 static const char *cftypes[] = { "TOKEN", "COMPPROTO" };
489 if (proto > 0 && proto <= sizeof cftypes / sizeof *cftypes)
490 return cftypes[proto - 1];
492 return NumStr(proto, NULL, 0);
496 ipv6cp_ValidateToken(struct ipv6cp *ipv6cp, u_int32_t token,
497 struct fsm_decode *dec)
501 if (token != 0 && token != ipv6cp->my_token)
502 ipv6cp->peer_token = token;
504 opt.hdr.id = TY_TOKEN;
506 memcpy(opt.data, &ipv6cp->peer_token, 4);
507 if (token == ipv6cp->peer_token)
514 ipv6cp_DecodeConfig(struct fsm *fp, u_char *cp, u_char *end, int mode_type,
515 struct fsm_decode *dec)
517 /* Deal with incoming PROTO_IPV6CP */
518 struct ipv6cp *ipv6cp = fsm2ipv6cp(fp);
524 while (end - cp >= sizeof(opt->hdr)) {
525 if ((opt = fsm_readopt(&cp)) == NULL)
528 snprintf(tbuff, sizeof tbuff, " %s[%d]", protoname(opt->hdr.id),
531 switch (opt->hdr.id) {
533 memcpy(&token, opt->data, 4);
534 log_Printf(LogIPV6CP, "%s 0x%08lx\n", tbuff, (unsigned long)token);
538 ipv6cp->peer_tokenreq = 1;
539 ipv6cp_ValidateToken(ipv6cp, token, dec);
544 log_Printf(log_IsKept(LogIPV6CP) ? LogIPV6CP : LogPHASE,
545 "0x00000000: Unacceptable token!\n");
546 fsm_Close(&ipv6cp->fsm);
547 } else if (token == ipv6cp->peer_token)
548 log_Printf(log_IsKept(LogIPV6CP) ? LogIPV6CP : LogPHASE,
549 "0x%08lx: Unacceptable token!\n", (unsigned long)token);
550 else if (token != ipv6cp->my_token) {
552 while (n && !ipcp_SetIPv6address(ipv6cp, token, ipv6cp->peer_token)) {
554 while (n && (token = GenerateToken()) == ipv6cp->peer_token)
559 log_Printf(log_IsKept(LogIPV6CP) ? LogIPV6CP : LogPHASE,
560 "0x00000000: Unacceptable token!\n");
561 fsm_Close(&ipv6cp->fsm);
563 log_Printf(LogIPV6CP, "%s changing token: 0x%08lx --> 0x%08lx\n",
564 tbuff, (unsigned long)ipv6cp->my_token,
565 (unsigned long)token);
566 ipv6cp->my_token = token;
567 bundle_AdjustFilters(fp->bundle, &ipv6cp->myaddr, NULL);
573 ipv6cp->his_reject |= (1 << opt->hdr.id);
579 if (mode_type != MODE_NOP) {
580 ipv6cp->my_reject |= (1 << opt->hdr.id);
587 if (mode_type != MODE_NOP) {
588 if (mode_type == MODE_REQ && !ipv6cp->peer_tokenreq) {
589 if (dec->rejend == dec->rej && dec->nakend == dec->nak) {
591 * Pretend the peer has requested a TOKEN.
592 * We do this to ensure that we only send one NAK if the only
593 * reason for the NAK is because the peer isn't sending a
594 * TY_TOKEN REQ. This stops us from repeatedly trying to tell
595 * the peer that we have to have an IP address on their end.
597 ipv6cp->peer_tokenreq = 1;
599 ipv6cp_ValidateToken(ipv6cp, 0, dec);
601 fsm_opt_normalise(dec);