ipv6cp.c revision 330449
181634Sbrian/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
481634Sbrian * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
581634Sbrian * All rights reserved.
681634Sbrian *
781634Sbrian * Redistribution and use in source and binary forms, with or without
881634Sbrian * modification, are permitted provided that the following conditions
981634Sbrian * are met:
1081634Sbrian * 1. Redistributions of source code must retain the above copyright
1181634Sbrian *    notice, this list of conditions and the following disclaimer.
1281634Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1381634Sbrian *    notice, this list of conditions and the following disclaimer in the
1481634Sbrian *    documentation and/or other materials provided with the distribution.
1581634Sbrian *
1681634Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1781634Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1881634Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1981634Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2081634Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2181634Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2281634Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2381634Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2481634Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2581634Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2681634Sbrian * SUCH DAMAGE.
2781634Sbrian *
2881634Sbrian * $FreeBSD: stable/11/usr.sbin/ppp/ipv6cp.c 330449 2018-03-05 07:26:05Z eadler $
2981634Sbrian */
3081634Sbrian
3181634Sbrian#include <sys/param.h>
3281634Sbrian#include <netinet/in_systm.h>
3381634Sbrian#include <netinet/in.h>
3481634Sbrian#include <netinet/ip.h>
3581634Sbrian#include <sys/socket.h>
3681634Sbrian#include <net/route.h>
3781634Sbrian#include <net/if.h>
38102558Sbrian#include <net/if_types.h>
39102558Sbrian#include <net/if_dl.h>
4081634Sbrian#include <sys/un.h>
4181634Sbrian
42102500Sbrian#include <stdarg.h>
4381634Sbrian#include <stdio.h>
4481634Sbrian#include <stdlib.h>
4581634Sbrian#include <string.h>
4681634Sbrian#include <termios.h>
47102558Sbrian#include <ifaddrs.h>
4881634Sbrian
4981634Sbrian#include "layer.h"
5081634Sbrian#include "defs.h"
5181634Sbrian#include "mbuf.h"
5281634Sbrian#include "timer.h"
5381634Sbrian#include "fsm.h"
5481634Sbrian#include "iplist.h"
5581634Sbrian#include "throughput.h"
5681634Sbrian#include "slcompress.h"
5781634Sbrian#include "lqr.h"
5881634Sbrian#include "hdlc.h"
5981634Sbrian#include "lcp.h"
6081634Sbrian#include "ncpaddr.h"
6181634Sbrian#include "ip.h"
6281634Sbrian#include "ipcp.h"
6381634Sbrian#include "ipv6cp.h"
6481634Sbrian#include "filter.h"
6581634Sbrian#include "descriptor.h"
6681634Sbrian#include "ccp.h"
6781634Sbrian#include "link.h"
6881634Sbrian#include "mp.h"
6981634Sbrian#ifndef NORADIUS
7081634Sbrian#include "radius.h"
7181634Sbrian#endif
7281634Sbrian#include "ncp.h"
7381634Sbrian#include "bundle.h"
7481634Sbrian#include "route.h"
7581634Sbrian#include "iface.h"
7681634Sbrian#include "log.h"
7781634Sbrian#include "proto.h"
7881634Sbrian#include "command.h"
7981634Sbrian#include "prompt.h"
8081634Sbrian#include "async.h"
8181634Sbrian#include "physical.h"
8281888Sbrian#include "probe.h"
83116251Skris#include "systems.h"
8481634Sbrian
8581634Sbrian
8681634Sbrian#ifndef NOINET6
87112618Sume#define IN6ADDR_LINKLOCAL_MCAST_INIT \
88112618Sume	{{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
89112618Sume	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}}
90112618Sumestatic const struct in6_addr in6addr_linklocal_mcast =
91112618Sume	IN6ADDR_LINKLOCAL_MCAST_INIT;
92112618Sume
9381634Sbrianstatic int ipv6cp_LayerUp(struct fsm *);
9481634Sbrianstatic void ipv6cp_LayerDown(struct fsm *);
9581634Sbrianstatic void ipv6cp_LayerStart(struct fsm *);
9681634Sbrianstatic void ipv6cp_LayerFinish(struct fsm *);
9781634Sbrianstatic void ipv6cp_InitRestartCounter(struct fsm *, int);
9881634Sbrianstatic void ipv6cp_SendConfigReq(struct fsm *);
9981634Sbrianstatic void ipv6cp_SentTerminateReq(struct fsm *);
10081634Sbrianstatic void ipv6cp_SendTerminateAck(struct fsm *, u_char);
10194894Sbrianstatic void ipv6cp_DecodeConfig(struct fsm *, u_char *, u_char *, int,
10281634Sbrian                                struct fsm_decode *);
10381634Sbrian
10481634Sbrianstatic struct fsm_callbacks ipv6cp_Callbacks = {
10581634Sbrian  ipv6cp_LayerUp,
10681634Sbrian  ipv6cp_LayerDown,
10781634Sbrian  ipv6cp_LayerStart,
10881634Sbrian  ipv6cp_LayerFinish,
10981634Sbrian  ipv6cp_InitRestartCounter,
11081634Sbrian  ipv6cp_SendConfigReq,
11181634Sbrian  ipv6cp_SentTerminateReq,
11281634Sbrian  ipv6cp_SendTerminateAck,
11381634Sbrian  ipv6cp_DecodeConfig,
11481634Sbrian  fsm_NullRecvResetReq,
11581634Sbrian  fsm_NullRecvResetAck
11681634Sbrian};
11781634Sbrian
118102558Sbrianstatic void
119102558SbrianSetInterfaceID(u_char *ifid, int userandom)
12081634Sbrian{
121102558Sbrian  struct ifaddrs *ifa, *ifap = NULL;
122102558Sbrian  struct sockaddr_dl *sdl;
123102558Sbrian  const u_long i32_max = 0xffffffff;
124102558Sbrian  u_long r1, r2;
125102558Sbrian
126102558Sbrian  /* configure an interface ID based on Section 4.1 of RFC 2472 */
127102558Sbrian  memset(ifid, 0, IPV6CP_IFIDLEN);
128102558Sbrian
129102558Sbrian  /*
130102558Sbrian   * 1) If an IEEE global identifier (EUI-48 or EUI-64) is
131102558Sbrian   * available anywhere on the node, it should be used to construct
132102558Sbrian   * the tentative Interface-Identifier due to its uniqueness
133102558Sbrian   * properties.
134102558Sbrian   */
135102558Sbrian  if (userandom)
136102558Sbrian    goto randomid;
137102558Sbrian  if (getifaddrs(&ifap) < 0)
138102558Sbrian    goto randomid;
139102558Sbrian
140102558Sbrian  for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
141102558Sbrian    char *cp;
142102558Sbrian
143102558Sbrian    if (ifa->ifa_addr->sa_family != AF_LINK)
144102558Sbrian      continue;
145102558Sbrian
146102558Sbrian    sdl = (struct sockaddr_dl *)ifa->ifa_addr;
147102558Sbrian    if (sdl->sdl_alen < 6)
148102558Sbrian      continue;
149102558Sbrian    /* we're only interested in IEEE hardware addresses */
150102558Sbrian    switch(sdl->sdl_type) {
151102558Sbrian    case IFT_ETHER:
152102558Sbrian    case IFT_FDDI:
153210936Sjhb    case IFT_L2VLAN:
154102558Sbrian      /* XXX need more cases? */
155102558Sbrian      break;
156102558Sbrian    default:
157102558Sbrian      continue;
158102558Sbrian    }
159102558Sbrian
160102558Sbrian    cp = (char *)(sdl->sdl_data + sdl->sdl_nlen);
161102558Sbrian    ifid[0] = cp[0];
162102558Sbrian    ifid[0] ^= 0x02; /* reverse the u/l bit*/
163102558Sbrian    ifid[1] = cp[1];
164102558Sbrian    ifid[2] = cp[2];
165102558Sbrian    ifid[3] = 0xff;
166102558Sbrian    ifid[4] = 0xfe;
167102558Sbrian    ifid[5] = cp[3];
168102558Sbrian    ifid[6] = cp[4];
169102558Sbrian    ifid[7] = cp[5];
170102558Sbrian
171102558Sbrian    freeifaddrs(ifap);
172102558Sbrian    return;
173102558Sbrian  }
174102558Sbrian
175102558Sbrian  freeifaddrs(ifap);
176102558Sbrian
177102558Sbrian  /*
178102558Sbrian   * 2) If an IEEE global identifier is not available a different source
179102558Sbrian   * of uniqueness should be used.
180102558Sbrian   * XXX: we skip this case.
181102558Sbrian   */
182102558Sbrian
183102558Sbrian  /*
184102558Sbrian   * 3) If a good source of uniqueness cannot be found, it is
185102558Sbrian   * recommended that a random number be generated.  In this case the
186102558Sbrian   * "u" bit of the interface identifier MUST be set to zero (0).
187102558Sbrian   */
188102558Sbrian randomid:
18981634Sbrian  randinit();
190102558Sbrian  r1 = (((u_long)random()) % i32_max) + 1;
191102558Sbrian  r2 = (((u_long)random()) % i32_max) + 1;
192102558Sbrian  memcpy(ifid, &r1, sizeof(r1));
193102558Sbrian  memcpy(ifid + 4, &r2, sizeof(r2));
194102558Sbrian  ifid[0] &= 0xfd;
195102558Sbrian  return;
19681634Sbrian}
19781634Sbrian
19881634Sbrianstatic int
199102558Sbrianipcp_SetIPv6address(struct ipv6cp *ipv6cp, u_char *myifid, u_char *hisifid)
20081634Sbrian{
20181634Sbrian  struct bundle *bundle = ipv6cp->fsm.bundle;
20281634Sbrian  struct in6_addr myaddr, hisaddr;
203112618Sume  struct ncprange myrange, range;
204112618Sume  struct ncpaddr addr;
20581739Sbrian  struct sockaddr_storage ssdst, ssgw, ssmask;
20681739Sbrian  struct sockaddr *sadst, *sagw, *samask;
20781634Sbrian
20881739Sbrian  sadst = (struct sockaddr *)&ssdst;
20981739Sbrian  sagw = (struct sockaddr *)&ssgw;
21081739Sbrian  samask = (struct sockaddr *)&ssmask;
21181739Sbrian
21281634Sbrian  memset(&myaddr, '\0', sizeof myaddr);
21381634Sbrian  memset(&hisaddr, '\0', sizeof hisaddr);
21481634Sbrian
21581634Sbrian  myaddr.s6_addr[0] = 0xfe;
21681634Sbrian  myaddr.s6_addr[1] = 0x80;
217102558Sbrian  memcpy(&myaddr.s6_addr[8], myifid, IPV6CP_IFIDLEN);
218102558Sbrian#if 0
219102558Sbrian  myaddr.s6_addr[8] |= 0x02;	/* set 'universal' bit */
220102558Sbrian#endif
22181634Sbrian
22281634Sbrian  hisaddr.s6_addr[0] = 0xfe;
22381634Sbrian  hisaddr.s6_addr[1] = 0x80;
224102558Sbrian  memcpy(&hisaddr.s6_addr[8], hisifid, IPV6CP_IFIDLEN);
225102558Sbrian#if 0
226102558Sbrian  hisaddr.s6_addr[8] |= 0x02;	/* set 'universal' bit */
227102558Sbrian#endif
22881634Sbrian
22981634Sbrian  ncpaddr_setip6(&ipv6cp->myaddr, &myaddr);
23081634Sbrian  ncpaddr_setip6(&ipv6cp->hisaddr, &hisaddr);
231113067Sume  ncprange_set(&myrange, &ipv6cp->myaddr, 64);
23281634Sbrian
23381634Sbrian  if (!iface_Add(bundle->iface, &bundle->ncp, &myrange, &ipv6cp->hisaddr,
23481634Sbrian                 IFACE_ADD_FIRST|IFACE_FORCE_ADD|IFACE_SYSTEM))
23581634Sbrian    return 0;
23681634Sbrian
23781634Sbrian  if (!Enabled(bundle, OPT_IFACEALIAS))
23881634Sbrian    iface_Clear(bundle->iface, &bundle->ncp, AF_INET6,
23981634Sbrian                IFACE_CLEAR_ALIASES|IFACE_SYSTEM);
24081634Sbrian
241112618Sume  ncpaddr_setip6(&addr, &in6addr_linklocal_mcast);
242112618Sume  ncprange_set(&range, &addr, 32);
243112618Sume  rt_Set(bundle, RTM_ADD, &range, &ipv6cp->myaddr, 1, 0);
244112618Sume
24581634Sbrian  if (bundle->ncp.cfg.sendpipe > 0 || bundle->ncp.cfg.recvpipe > 0) {
24681739Sbrian    ncprange_getsa(&myrange, &ssgw, &ssmask);
24781739Sbrian    if (ncpaddr_isset(&ipv6cp->hisaddr))
24881739Sbrian      ncpaddr_getsa(&ipv6cp->hisaddr, &ssdst);
24981739Sbrian    else
25081739Sbrian      sadst = NULL;
251191316Sbz    rt_Update(bundle, sadst, sagw, samask, NULL, NULL);
25281634Sbrian  }
25381634Sbrian
25481634Sbrian  if (Enabled(bundle, OPT_SROUTES))
25581634Sbrian    route_Change(bundle, bundle->ncp.route, &ipv6cp->myaddr, &ipv6cp->hisaddr);
25681634Sbrian
25781634Sbrian#ifndef NORADIUS
25881634Sbrian  if (bundle->radius.valid)
259116586Sume    route_Change(bundle, bundle->radius.ipv6routes, &ipv6cp->myaddr,
26081634Sbrian                 &ipv6cp->hisaddr);
26181634Sbrian#endif
26281634Sbrian
26381634Sbrian  return 1;	/* Ok */
26481634Sbrian}
26581634Sbrian
26681634Sbrianvoid
26781634Sbrianipv6cp_Init(struct ipv6cp *ipv6cp, struct bundle *bundle, struct link *l,
26881634Sbrian                 const struct fsm_parent *parent)
26981634Sbrian{
27081634Sbrian  static const char * const timer_names[] =
27181634Sbrian    {"IPV6CP restart", "IPV6CP openmode", "IPV6CP stopped"};
27281634Sbrian  int n;
27381634Sbrian
27481634Sbrian  fsm_Init(&ipv6cp->fsm, "IPV6CP", PROTO_IPV6CP, 1, IPV6CP_MAXCODE, LogIPV6CP,
27581634Sbrian           bundle, l, parent, &ipv6cp_Callbacks, timer_names);
27681634Sbrian
27781634Sbrian  ipv6cp->cfg.fsm.timeout = DEF_FSMRETRY;
27881634Sbrian  ipv6cp->cfg.fsm.maxreq = DEF_FSMTRIES;
27981634Sbrian  ipv6cp->cfg.fsm.maxtrm = DEF_FSMTRIES;
28081634Sbrian
281102558Sbrian  SetInterfaceID(ipv6cp->my_ifid, 0);
282102558Sbrian  do {
283102558Sbrian    SetInterfaceID(ipv6cp->his_ifid, 1);
284102558Sbrian  } while (memcmp(ipv6cp->his_ifid, ipv6cp->my_ifid, IPV6CP_IFIDLEN) == 0);
28581634Sbrian
28681897Sbrian  if (probe.ipv6_available) {
28781897Sbrian    n = 100;
28881897Sbrian    while (n &&
289102558Sbrian           !ipcp_SetIPv6address(ipv6cp, ipv6cp->my_ifid, ipv6cp->his_ifid)) {
290102558Sbrian      do {
291102558Sbrian	n--;
292102558Sbrian    	SetInterfaceID(ipv6cp->my_ifid, 1);
293102558Sbrian      } while (n
294102558Sbrian	&& memcmp(ipv6cp->his_ifid, ipv6cp->my_ifid, IPV6CP_IFIDLEN) == 0);
29581897Sbrian    }
29681897Sbrian  }
29781634Sbrian
29881634Sbrian  throughput_init(&ipv6cp->throughput, SAMPLE_PERIOD);
29981634Sbrian  memset(ipv6cp->Queue, '\0', sizeof ipv6cp->Queue);
30081634Sbrian  ipv6cp_Setup(ipv6cp);
30181634Sbrian}
30281634Sbrian
30381634Sbrianvoid
30481634Sbrianipv6cp_Destroy(struct ipv6cp *ipv6cp)
30581634Sbrian{
30681634Sbrian  throughput_destroy(&ipv6cp->throughput);
30781634Sbrian}
30881634Sbrian
30981634Sbrianvoid
31081634Sbrianipv6cp_Setup(struct ipv6cp *ipv6cp)
31181634Sbrian{
31281634Sbrian  ncpaddr_init(&ipv6cp->myaddr);
31381634Sbrian  ncpaddr_init(&ipv6cp->hisaddr);
31481634Sbrian
31581634Sbrian  ipv6cp->his_reject = 0;
31681634Sbrian  ipv6cp->my_reject = 0;
31781634Sbrian}
31881634Sbrian
31981634Sbrianvoid
32081634Sbrianipv6cp_SetLink(struct ipv6cp *ipv6cp, struct link *l)
32181634Sbrian{
32281634Sbrian  ipv6cp->fsm.link = l;
32381634Sbrian}
32481634Sbrian
32581634Sbrianint
32681634Sbrianipv6cp_Show(struct cmdargs const *arg)
32781634Sbrian{
32881634Sbrian  struct ipv6cp *ipv6cp = &arg->bundle->ncp.ipv6cp;
32981634Sbrian
33081634Sbrian  prompt_Printf(arg->prompt, "%s [%s]\n", ipv6cp->fsm.name,
33181634Sbrian                State2Nam(ipv6cp->fsm.state));
33281634Sbrian  if (ipv6cp->fsm.state == ST_OPENED) {
33381634Sbrian    prompt_Printf(arg->prompt, " His side:        %s\n",
33494894Sbrian                  ncpaddr_ntoa(&ipv6cp->hisaddr));
33581634Sbrian    prompt_Printf(arg->prompt, " My side:         %s\n",
33694894Sbrian                  ncpaddr_ntoa(&ipv6cp->myaddr));
33781634Sbrian    prompt_Printf(arg->prompt, " Queued packets:  %lu\n",
33881634Sbrian                  (unsigned long)ipv6cp_QueueLen(ipv6cp));
33981634Sbrian  }
34081634Sbrian
34181634Sbrian  prompt_Printf(arg->prompt, "\nDefaults:\n");
34281634Sbrian  prompt_Printf(arg->prompt, "  FSM retry = %us, max %u Config"
34381634Sbrian                " REQ%s, %u Term REQ%s\n\n", ipv6cp->cfg.fsm.timeout,
34481634Sbrian                ipv6cp->cfg.fsm.maxreq, ipv6cp->cfg.fsm.maxreq == 1 ? "" : "s",
34581634Sbrian                ipv6cp->cfg.fsm.maxtrm, ipv6cp->cfg.fsm.maxtrm == 1 ? "" : "s");
34681634Sbrian
34781634Sbrian  throughput_disp(&ipv6cp->throughput, arg->prompt);
34881634Sbrian
34981634Sbrian  return 0;
35081634Sbrian}
35181634Sbrian
35281634Sbrianstruct mbuf *
35381634Sbrianipv6cp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
35481634Sbrian{
35581634Sbrian  /* Got PROTO_IPV6CP from link */
35681634Sbrian  m_settype(bp, MB_IPV6CPIN);
35781897Sbrian  if (bundle_Phase(bundle) == PHASE_NETWORK)
35881634Sbrian    fsm_Input(&bundle->ncp.ipv6cp.fsm, bp);
35981634Sbrian  else {
36081634Sbrian    if (bundle_Phase(bundle) < PHASE_NETWORK)
36181634Sbrian      log_Printf(LogIPV6CP, "%s: Error: Unexpected IPV6CP in phase %s"
36281634Sbrian                 " (ignored)\n", l->name, bundle_PhaseName(bundle));
36381634Sbrian    m_freem(bp);
36481634Sbrian  }
36581634Sbrian  return NULL;
36681634Sbrian}
36781634Sbrian
36881634Sbrianvoid
36981634Sbrianipv6cp_AddInOctets(struct ipv6cp *ipv6cp, int n)
37081634Sbrian{
37181634Sbrian  throughput_addin(&ipv6cp->throughput, n);
37281634Sbrian}
37381634Sbrian
37481634Sbrianvoid
37581634Sbrianipv6cp_AddOutOctets(struct ipv6cp *ipv6cp, int n)
37681634Sbrian{
37781634Sbrian  throughput_addout(&ipv6cp->throughput, n);
37881634Sbrian}
37981634Sbrian
38081634Sbrianvoid
381134789Sbrianipv6cp_IfaceAddrAdded(struct ipv6cp *ipv6cp __unused,
382134789Sbrian		      const struct iface_addr *addr __unused)
38381634Sbrian{
38481634Sbrian}
38581634Sbrian
38681634Sbrianvoid
387134789Sbrianipv6cp_IfaceAddrDeleted(struct ipv6cp *ipv6cp __unused,
388134789Sbrian			const struct iface_addr *addr __unused)
38981634Sbrian{
39081634Sbrian}
39181634Sbrian
39281634Sbrianint
39381634Sbrianipv6cp_InterfaceUp(struct ipv6cp *ipv6cp)
39481634Sbrian{
395102558Sbrian  if (!ipcp_SetIPv6address(ipv6cp, ipv6cp->my_ifid, ipv6cp->his_ifid)) {
39681634Sbrian    log_Printf(LogERROR, "ipv6cp_InterfaceUp: unable to set ipv6 address\n");
39781634Sbrian    return 0;
39881634Sbrian  }
39981634Sbrian
40081634Sbrian  if (!iface_SetFlags(ipv6cp->fsm.bundle->iface->name, IFF_UP)) {
40181634Sbrian    log_Printf(LogERROR, "ipv6cp_InterfaceUp: Can't set the IFF_UP"
402