Index: stable/4/usr.sbin/ppp/ipv6cp.c =================================================================== --- stable/4/usr.sbin/ppp/ipv6cp.c (revision 116982) +++ stable/4/usr.sbin/ppp/ipv6cp.c (revision 116983) @@ -1,742 +1,742 @@ /*- * Copyright (c) 2001 Brian Somers * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "layer.h" #include "defs.h" #include "mbuf.h" #include "timer.h" #include "fsm.h" #include "iplist.h" #include "throughput.h" #include "slcompress.h" #include "lqr.h" #include "hdlc.h" #include "lcp.h" #include "ncpaddr.h" #include "ip.h" #include "ipcp.h" #include "ipv6cp.h" #include "filter.h" #include "descriptor.h" #include "ccp.h" #include "link.h" #include "mp.h" #ifndef NORADIUS #include "radius.h" #endif #include "ncp.h" #include "bundle.h" #include "route.h" #include "iface.h" #include "log.h" #include "proto.h" #include "command.h" #include "prompt.h" #include "async.h" #include "physical.h" #include "probe.h" #ifndef NOINET6 #define IN6ADDR_LINKLOCAL_MCAST_INIT \ {{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}} static const struct in6_addr in6addr_linklocal_mcast = IN6ADDR_LINKLOCAL_MCAST_INIT; static int ipv6cp_LayerUp(struct fsm *); static void ipv6cp_LayerDown(struct fsm *); static void ipv6cp_LayerStart(struct fsm *); static void ipv6cp_LayerFinish(struct fsm *); static void ipv6cp_InitRestartCounter(struct fsm *, int); static void ipv6cp_SendConfigReq(struct fsm *); static void ipv6cp_SentTerminateReq(struct fsm *); static void ipv6cp_SendTerminateAck(struct fsm *, u_char); static void ipv6cp_DecodeConfig(struct fsm *, u_char *, u_char *, int, struct fsm_decode *); static struct fsm_callbacks ipv6cp_Callbacks = { ipv6cp_LayerUp, ipv6cp_LayerDown, ipv6cp_LayerStart, ipv6cp_LayerFinish, ipv6cp_InitRestartCounter, ipv6cp_SendConfigReq, ipv6cp_SentTerminateReq, ipv6cp_SendTerminateAck, ipv6cp_DecodeConfig, fsm_NullRecvResetReq, fsm_NullRecvResetAck }; static void SetInterfaceID(u_char *ifid, int userandom) { struct ifaddrs *ifa, *ifap = NULL; struct sockaddr_dl *sdl; const u_long i32_max = 0xffffffff; u_long r1, r2; /* configure an interface ID based on Section 4.1 of RFC 2472 */ memset(ifid, 0, IPV6CP_IFIDLEN); /* * 1) If an IEEE global identifier (EUI-48 or EUI-64) is * available anywhere on the node, it should be used to construct * the tentative Interface-Identifier due to its uniqueness * properties. */ if (userandom) goto randomid; if (getifaddrs(&ifap) < 0) goto randomid; for (ifa = ifap; ifa; ifa = ifa->ifa_next) { char *cp; if (ifa->ifa_addr->sa_family != AF_LINK) continue; sdl = (struct sockaddr_dl *)ifa->ifa_addr; if (sdl->sdl_alen < 6) continue; /* we're only interested in IEEE hardware addresses */ switch(sdl->sdl_type) { case IFT_ETHER: case IFT_FDDI: /* XXX need more cases? */ break; default: continue; } cp = (char *)(sdl->sdl_data + sdl->sdl_nlen); ifid[0] = cp[0]; ifid[0] ^= 0x02; /* reverse the u/l bit*/ ifid[1] = cp[1]; ifid[2] = cp[2]; ifid[3] = 0xff; ifid[4] = 0xfe; ifid[5] = cp[3]; ifid[6] = cp[4]; ifid[7] = cp[5]; freeifaddrs(ifap); return; } freeifaddrs(ifap); /* * 2) If an IEEE global identifier is not available a different source * of uniqueness should be used. * XXX: we skip this case. */ /* * 3) If a good source of uniqueness cannot be found, it is * recommended that a random number be generated. In this case the * "u" bit of the interface identifier MUST be set to zero (0). */ randomid: randinit(); r1 = (((u_long)random()) % i32_max) + 1; r2 = (((u_long)random()) % i32_max) + 1; memcpy(ifid, &r1, sizeof(r1)); memcpy(ifid + 4, &r2, sizeof(r2)); ifid[0] &= 0xfd; return; } static int ipcp_SetIPv6address(struct ipv6cp *ipv6cp, u_char *myifid, u_char *hisifid) { struct bundle *bundle = ipv6cp->fsm.bundle; struct in6_addr myaddr, hisaddr; struct ncprange myrange, range; struct ncpaddr addr; struct sockaddr_storage ssdst, ssgw, ssmask; struct sockaddr *sadst, *sagw, *samask; sadst = (struct sockaddr *)&ssdst; sagw = (struct sockaddr *)&ssgw; samask = (struct sockaddr *)&ssmask; memset(&myaddr, '\0', sizeof myaddr); memset(&hisaddr, '\0', sizeof hisaddr); myaddr.s6_addr[0] = 0xfe; myaddr.s6_addr[1] = 0x80; memcpy(&myaddr.s6_addr[8], myifid, IPV6CP_IFIDLEN); #if 0 myaddr.s6_addr[8] |= 0x02; /* set 'universal' bit */ #endif hisaddr.s6_addr[0] = 0xfe; hisaddr.s6_addr[1] = 0x80; memcpy(&hisaddr.s6_addr[8], hisifid, IPV6CP_IFIDLEN); #if 0 hisaddr.s6_addr[8] |= 0x02; /* set 'universal' bit */ #endif ncpaddr_setip6(&ipv6cp->myaddr, &myaddr); ncpaddr_setip6(&ipv6cp->hisaddr, &hisaddr); ncprange_set(&myrange, &ipv6cp->myaddr, 64); if (!iface_Add(bundle->iface, &bundle->ncp, &myrange, &ipv6cp->hisaddr, IFACE_ADD_FIRST|IFACE_FORCE_ADD|IFACE_SYSTEM)) return 0; if (!Enabled(bundle, OPT_IFACEALIAS)) iface_Clear(bundle->iface, &bundle->ncp, AF_INET6, IFACE_CLEAR_ALIASES|IFACE_SYSTEM); ncpaddr_setip6(&addr, &in6addr_linklocal_mcast); ncprange_set(&range, &addr, 32); rt_Set(bundle, RTM_ADD, &range, &ipv6cp->myaddr, 1, 0); if (bundle->ncp.cfg.sendpipe > 0 || bundle->ncp.cfg.recvpipe > 0) { ncprange_getsa(&myrange, &ssgw, &ssmask); if (ncpaddr_isset(&ipv6cp->hisaddr)) ncpaddr_getsa(&ipv6cp->hisaddr, &ssdst); else sadst = NULL; rt_Update(bundle, sadst, sagw, samask); } if (Enabled(bundle, OPT_SROUTES)) route_Change(bundle, bundle->ncp.route, &ipv6cp->myaddr, &ipv6cp->hisaddr); #ifndef NORADIUS if (bundle->radius.valid) - route_Change(bundle, bundle->radius.routes, &ipv6cp->myaddr, + route_Change(bundle, bundle->radius.ipv6routes, &ipv6cp->myaddr, &ipv6cp->hisaddr); #endif return 1; /* Ok */ } void ipv6cp_Init(struct ipv6cp *ipv6cp, struct bundle *bundle, struct link *l, const struct fsm_parent *parent) { static const char * const timer_names[] = {"IPV6CP restart", "IPV6CP openmode", "IPV6CP stopped"}; int n; fsm_Init(&ipv6cp->fsm, "IPV6CP", PROTO_IPV6CP, 1, IPV6CP_MAXCODE, LogIPV6CP, bundle, l, parent, &ipv6cp_Callbacks, timer_names); ipv6cp->cfg.fsm.timeout = DEF_FSMRETRY; ipv6cp->cfg.fsm.maxreq = DEF_FSMTRIES; ipv6cp->cfg.fsm.maxtrm = DEF_FSMTRIES; SetInterfaceID(ipv6cp->my_ifid, 0); do { SetInterfaceID(ipv6cp->his_ifid, 1); } while (memcmp(ipv6cp->his_ifid, ipv6cp->my_ifid, IPV6CP_IFIDLEN) == 0); if (probe.ipv6_available) { n = 100; while (n && !ipcp_SetIPv6address(ipv6cp, ipv6cp->my_ifid, ipv6cp->his_ifid)) { do { n--; SetInterfaceID(ipv6cp->my_ifid, 1); } while (n && memcmp(ipv6cp->his_ifid, ipv6cp->my_ifid, IPV6CP_IFIDLEN) == 0); } } throughput_init(&ipv6cp->throughput, SAMPLE_PERIOD); memset(ipv6cp->Queue, '\0', sizeof ipv6cp->Queue); ipv6cp_Setup(ipv6cp); } void ipv6cp_Destroy(struct ipv6cp *ipv6cp) { throughput_destroy(&ipv6cp->throughput); } void ipv6cp_Setup(struct ipv6cp *ipv6cp) { ncpaddr_init(&ipv6cp->myaddr); ncpaddr_init(&ipv6cp->hisaddr); ipv6cp->his_reject = 0; ipv6cp->my_reject = 0; } void ipv6cp_SetLink(struct ipv6cp *ipv6cp, struct link *l) { ipv6cp->fsm.link = l; } int ipv6cp_Show(struct cmdargs const *arg) { struct ipv6cp *ipv6cp = &arg->bundle->ncp.ipv6cp; prompt_Printf(arg->prompt, "%s [%s]\n", ipv6cp->fsm.name, State2Nam(ipv6cp->fsm.state)); if (ipv6cp->fsm.state == ST_OPENED) { prompt_Printf(arg->prompt, " His side: %s\n", ncpaddr_ntoa(&ipv6cp->hisaddr)); prompt_Printf(arg->prompt, " My side: %s\n", ncpaddr_ntoa(&ipv6cp->myaddr)); prompt_Printf(arg->prompt, " Queued packets: %lu\n", (unsigned long)ipv6cp_QueueLen(ipv6cp)); } prompt_Printf(arg->prompt, "\nDefaults:\n"); prompt_Printf(arg->prompt, " FSM retry = %us, max %u Config" " REQ%s, %u Term REQ%s\n\n", ipv6cp->cfg.fsm.timeout, ipv6cp->cfg.fsm.maxreq, ipv6cp->cfg.fsm.maxreq == 1 ? "" : "s", ipv6cp->cfg.fsm.maxtrm, ipv6cp->cfg.fsm.maxtrm == 1 ? "" : "s"); throughput_disp(&ipv6cp->throughput, arg->prompt); return 0; } struct mbuf * ipv6cp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) { /* Got PROTO_IPV6CP from link */ m_settype(bp, MB_IPV6CPIN); if (bundle_Phase(bundle) == PHASE_NETWORK) fsm_Input(&bundle->ncp.ipv6cp.fsm, bp); else { if (bundle_Phase(bundle) < PHASE_NETWORK) log_Printf(LogIPV6CP, "%s: Error: Unexpected IPV6CP in phase %s" " (ignored)\n", l->name, bundle_PhaseName(bundle)); m_freem(bp); } return NULL; } void ipv6cp_AddInOctets(struct ipv6cp *ipv6cp, int n) { throughput_addin(&ipv6cp->throughput, n); } void ipv6cp_AddOutOctets(struct ipv6cp *ipv6cp, int n) { throughput_addout(&ipv6cp->throughput, n); } void ipv6cp_IfaceAddrAdded(struct ipv6cp *ipv6cp, const struct iface_addr *addr) { } void ipv6cp_IfaceAddrDeleted(struct ipv6cp *ipv6cp, const struct iface_addr *addr) { } int ipv6cp_InterfaceUp(struct ipv6cp *ipv6cp) { if (!ipcp_SetIPv6address(ipv6cp, ipv6cp->my_ifid, ipv6cp->his_ifid)) { log_Printf(LogERROR, "ipv6cp_InterfaceUp: unable to set ipv6 address\n"); return 0; } if (!iface_SetFlags(ipv6cp->fsm.bundle->iface->name, IFF_UP)) { log_Printf(LogERROR, "ipv6cp_InterfaceUp: Can't set the IFF_UP" " flag on %s\n", ipv6cp->fsm.bundle->iface->name); return 0; } return 1; } size_t ipv6cp_QueueLen(struct ipv6cp *ipv6cp) { struct mqueue *q; size_t result; result = 0; for (q = ipv6cp->Queue; q < ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp); q++) result += q->len; return result; } int ipv6cp_PushPacket(struct ipv6cp *ipv6cp, struct link *l) { struct bundle *bundle = ipv6cp->fsm.bundle; struct mqueue *queue; struct mbuf *bp; int m_len; u_int32_t secs = 0; unsigned alivesecs = 0; if (ipv6cp->fsm.state != ST_OPENED) return 0; /* * If ccp is not open but is required, do nothing. */ if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) { log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name); return 0; } queue = ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp) - 1; do { if (queue->top) { bp = m_dequeue(queue); bp = mbuf_Read(bp, &secs, sizeof secs); bp = m_pullup(bp); m_len = m_length(bp); if (!FilterCheck(MBUF_CTOP(bp), AF_INET6, &bundle->filter.alive, &alivesecs)) { if (secs == 0) secs = alivesecs; bundle_StartIdleTimer(bundle, secs); } link_PushPacket(l, bp, bundle, 0, PROTO_IPV6); ipv6cp_AddOutOctets(ipv6cp, m_len); return 1; } } while (queue-- != ipv6cp->Queue); return 0; } static int ipv6cp_LayerUp(struct fsm *fp) { /* We're now up */ struct ipv6cp *ipv6cp = fsm2ipv6cp(fp); char tbuff[40]; log_Printf(LogIPV6CP, "%s: LayerUp.\n", fp->link->name); if (!ipv6cp_InterfaceUp(ipv6cp)) return 0; snprintf(tbuff, sizeof tbuff, "%s", ncpaddr_ntoa(&ipv6cp->myaddr)); log_Printf(LogIPV6CP, "myaddr %s hisaddr = %s\n", tbuff, ncpaddr_ntoa(&ipv6cp->hisaddr)); /* XXX: Call radius_Account() */ /* * XXX this stuff should really live in the FSM. Our config should * associate executable sections in files with events. */ if (system_Select(fp->bundle, tbuff, LINKUPFILE, NULL, NULL) < 0) { if (bundle_GetLabel(fp->bundle) && !Enabled(fp->bundle, OPT_IPCP)) { if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle), LINKUPFILE, NULL, NULL) < 0) system_Select(fp->bundle, "MYADDR6", LINKUPFILE, NULL, NULL); } else system_Select(fp->bundle, "MYADDR6", LINKUPFILE, NULL, NULL); } fp->more.reqs = fp->more.naks = fp->more.rejs = ipv6cp->cfg.fsm.maxreq * 3; log_DisplayPrompts(); return 1; } static void ipv6cp_LayerDown(struct fsm *fp) { /* About to come down */ struct ipv6cp *ipv6cp = fsm2ipv6cp(fp); static int recursing; char addr[40]; if (!recursing++) { snprintf(addr, sizeof addr, "%s", ncpaddr_ntoa(&ipv6cp->myaddr)); log_Printf(LogIPV6CP, "%s: LayerDown: %s\n", fp->link->name, addr); /* XXX: Call radius_Account() */ /* * XXX this stuff should really live in the FSM. Our config should * associate executable sections in files with events. */ if (system_Select(fp->bundle, addr, LINKDOWNFILE, NULL, NULL) < 0) { if (bundle_GetLabel(fp->bundle) && !Enabled(fp->bundle, OPT_IPCP)) { if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle), LINKDOWNFILE, NULL, NULL) < 0) system_Select(fp->bundle, "MYADDR6", LINKDOWNFILE, NULL, NULL); } else system_Select(fp->bundle, "MYADDR6", LINKDOWNFILE, NULL, NULL); } ipv6cp_Setup(ipv6cp); } recursing--; } static void ipv6cp_LayerStart(struct fsm *fp) { /* We're about to start up ! */ struct ipv6cp *ipv6cp = fsm2ipv6cp(fp); log_Printf(LogIPV6CP, "%s: LayerStart.\n", fp->link->name); throughput_start(&ipv6cp->throughput, "IPV6CP throughput", Enabled(fp->bundle, OPT_THROUGHPUT)); fp->more.reqs = fp->more.naks = fp->more.rejs = ipv6cp->cfg.fsm.maxreq * 3; ipv6cp->peer_tokenreq = 0; } static void ipv6cp_LayerFinish(struct fsm *fp) { /* We're now down */ struct ipv6cp *ipv6cp = fsm2ipv6cp(fp); log_Printf(LogIPV6CP, "%s: LayerFinish.\n", fp->link->name); throughput_stop(&ipv6cp->throughput); throughput_log(&ipv6cp->throughput, LogIPV6CP, NULL); } static void ipv6cp_InitRestartCounter(struct fsm *fp, int what) { /* Set fsm timer load */ struct ipv6cp *ipv6cp = fsm2ipv6cp(fp); fp->FsmTimer.load = ipv6cp->cfg.fsm.timeout * SECTICKS; switch (what) { case FSM_REQ_TIMER: fp->restart = ipv6cp->cfg.fsm.maxreq; break; case FSM_TRM_TIMER: fp->restart = ipv6cp->cfg.fsm.maxtrm; break; default: fp->restart = 1; break; } } static void ipv6cp_SendConfigReq(struct fsm *fp) { /* Send config REQ please */ struct physical *p = link2physical(fp->link); struct ipv6cp *ipv6cp = fsm2ipv6cp(fp); u_char buff[IPV6CP_IFIDLEN+2]; struct fsm_opt *o; o = (struct fsm_opt *)buff; if ((p && !physical_IsSync(p)) || !REJECTED(ipv6cp, TY_TOKEN)) { memcpy(o->data, ipv6cp->my_ifid, IPV6CP_IFIDLEN); INC_FSM_OPT(TY_TOKEN, IPV6CP_IFIDLEN + 2, o); } fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, (u_char *)o - buff, MB_IPV6CPOUT); } static void ipv6cp_SentTerminateReq(struct fsm *fp) { /* Term REQ just sent by FSM */ } static void ipv6cp_SendTerminateAck(struct fsm *fp, u_char id) { /* Send Term ACK please */ fsm_Output(fp, CODE_TERMACK, id, NULL, 0, MB_IPV6CPOUT); } static const char * protoname(int proto) { static const char *cftypes[] = { "IFACEID", "COMPPROTO" }; if (proto > 0 && proto <= sizeof cftypes / sizeof *cftypes) return cftypes[proto - 1]; return NumStr(proto, NULL, 0); } static void ipv6cp_ValidateInterfaceID(struct ipv6cp *ipv6cp, u_char *ifid, struct fsm_decode *dec) { struct fsm_opt opt; u_char zero[IPV6CP_IFIDLEN]; memset(zero, 0, IPV6CP_IFIDLEN); if (memcmp(ifid, zero, IPV6CP_IFIDLEN) != 0 && memcmp(ifid, ipv6cp->my_ifid, IPV6CP_IFIDLEN) != 0) memcpy(ipv6cp->his_ifid, ifid, IPV6CP_IFIDLEN); opt.hdr.id = TY_TOKEN; opt.hdr.len = IPV6CP_IFIDLEN + 2; memcpy(opt.data, &ipv6cp->his_ifid, IPV6CP_IFIDLEN); if (memcmp(ifid, ipv6cp->his_ifid, IPV6CP_IFIDLEN) == 0) fsm_ack(dec, &opt); else fsm_nak(dec, &opt); } static void ipv6cp_DecodeConfig(struct fsm *fp, u_char *cp, u_char *end, int mode_type, struct fsm_decode *dec) { /* Deal with incoming PROTO_IPV6CP */ struct ipv6cp *ipv6cp = fsm2ipv6cp(fp); int n; char tbuff[100]; u_char ifid[IPV6CP_IFIDLEN], zero[IPV6CP_IFIDLEN]; struct fsm_opt *opt; memset(zero, 0, IPV6CP_IFIDLEN); while (end - cp >= sizeof(opt->hdr)) { if ((opt = fsm_readopt(&cp)) == NULL) break; snprintf(tbuff, sizeof tbuff, " %s[%d]", protoname(opt->hdr.id), opt->hdr.len); switch (opt->hdr.id) { case TY_TOKEN: memcpy(ifid, opt->data, IPV6CP_IFIDLEN); log_Printf(LogIPV6CP, "%s 0x%02x%02x%02x%02x%02x%02x%02x%02x\n", tbuff, ifid[0], ifid[1], ifid[2], ifid[3], ifid[4], ifid[5], ifid[6], ifid[7]); switch (mode_type) { case MODE_REQ: ipv6cp->peer_tokenreq = 1; ipv6cp_ValidateInterfaceID(ipv6cp, ifid, dec); break; case MODE_NAK: if (memcmp(ifid, zero, IPV6CP_IFIDLEN) == 0) { log_Printf(log_IsKept(LogIPV6CP) ? LogIPV6CP : LogPHASE, "0x0000000000000000: Unacceptable IntefaceID!\n"); fsm_Close(&ipv6cp->fsm); } else if (memcmp(ifid, ipv6cp->his_ifid, IPV6CP_IFIDLEN) == 0) { log_Printf(log_IsKept(LogIPV6CP) ? LogIPV6CP : LogPHASE, "0x%02x%02x%02x%02x%02x%02x%02x%02x: " "Unacceptable IntefaceID!\n", ifid[0], ifid[1], ifid[2], ifid[3], ifid[4], ifid[5], ifid[6], ifid[7]); } else if (memcmp(ifid, ipv6cp->my_ifid, IPV6CP_IFIDLEN) != 0) { n = 100; while (n && !ipcp_SetIPv6address(ipv6cp, ifid, ipv6cp->his_ifid)) { do { n--; SetInterfaceID(ifid, 1); } while (n && memcmp(ifid, ipv6cp->his_ifid, IPV6CP_IFIDLEN) == 0); } if (n == 0) { log_Printf(log_IsKept(LogIPV6CP) ? LogIPV6CP : LogPHASE, "0x0000000000000000: Unacceptable IntefaceID!\n"); fsm_Close(&ipv6cp->fsm); } else { log_Printf(LogIPV6CP, "%s changing IntefaceID: " "0x%02x%02x%02x%02x%02x%02x%02x%02x " "--> 0x%02x%02x%02x%02x%02x%02x%02x%02x\n", tbuff, ipv6cp->my_ifid[0], ipv6cp->my_ifid[1], ipv6cp->my_ifid[2], ipv6cp->my_ifid[3], ipv6cp->my_ifid[4], ipv6cp->my_ifid[5], ipv6cp->my_ifid[6], ipv6cp->my_ifid[7], ifid[0], ifid[1], ifid[2], ifid[3], ifid[4], ifid[5], ifid[6], ifid[7]); memcpy(ipv6cp->my_ifid, ifid, IPV6CP_IFIDLEN); bundle_AdjustFilters(fp->bundle, &ipv6cp->myaddr, NULL); } } break; case MODE_REJ: ipv6cp->his_reject |= (1 << opt->hdr.id); break; } break; default: if (mode_type != MODE_NOP) { ipv6cp->my_reject |= (1 << opt->hdr.id); fsm_rej(dec, opt); } break; } } if (mode_type != MODE_NOP) { if (mode_type == MODE_REQ && !ipv6cp->peer_tokenreq) { if (dec->rejend == dec->rej && dec->nakend == dec->nak) { /* * Pretend the peer has requested a TOKEN. * We do this to ensure that we only send one NAK if the only * reason for the NAK is because the peer isn't sending a * TY_TOKEN REQ. This stops us from repeatedly trying to tell * the peer that we have to have an IP address on their end. */ ipv6cp->peer_tokenreq = 1; } memset(ifid, 0, IPV6CP_IFIDLEN); ipv6cp_ValidateInterfaceID(ipv6cp, ifid, dec); } fsm_opt_normalise(dec); } } #endif Index: stable/4/usr.sbin/ppp/radius.c =================================================================== --- stable/4/usr.sbin/ppp/radius.c (revision 116982) +++ stable/4/usr.sbin/ppp/radius.c (revision 116983) @@ -1,1057 +1,1123 @@ /* * Copyright 1999 Internet Business Solutions Ltd., Switzerland * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ * */ #include #include #include #include #include #include #include #include #ifdef LOCALRAD #include "radlib.h" #include "radlib_vs.h" #else #include #include #endif #include #ifndef NODES #include #endif #include #include #include #include #include #include #include #include #include "layer.h" #include "defs.h" #include "log.h" #include "descriptor.h" #include "prompt.h" #include "timer.h" #include "fsm.h" #include "iplist.h" #include "slcompress.h" #include "throughput.h" #include "lqr.h" #include "hdlc.h" #include "mbuf.h" #include "ncpaddr.h" #include "ip.h" #include "ipcp.h" #include "ipv6cp.h" #include "route.h" #include "command.h" #include "filter.h" #include "lcp.h" #include "ccp.h" #include "link.h" #include "mp.h" #include "radius.h" #include "auth.h" #include "async.h" #include "physical.h" #include "chat.h" #include "cbcp.h" #include "chap.h" #include "datalink.h" #include "ncp.h" #include "bundle.h" #include "proto.h" #ifndef NODES struct mschap_response { u_char ident; u_char flags; u_char lm_response[24]; u_char nt_response[24]; }; struct mschap2_response { u_char ident; u_char flags; u_char pchallenge[16]; u_char reserved[8]; u_char response[24]; }; #define AUTH_LEN 16 #define SALT_LEN 2 #endif static const char * radius_policyname(int policy) { switch(policy) { case MPPE_POLICY_ALLOWED: return "Allowed"; case MPPE_POLICY_REQUIRED: return "Required"; } return NumStr(policy, NULL, 0); } static const char * radius_typesname(int types) { switch(types) { case MPPE_TYPE_40BIT: return "40 bit"; case MPPE_TYPE_128BIT: return "128 bit"; case MPPE_TYPE_40BIT|MPPE_TYPE_128BIT: return "40 or 128 bit"; } return NumStr(types, NULL, 0); } #ifndef NODES static void demangle(struct radius *r, const void *mangled, size_t mlen, char **buf, size_t *len) { char R[AUTH_LEN]; /* variable names as per rfc2548 */ const char *S; u_char b[16]; const u_char *A, *C; MD5_CTX Context; int Slen, i, Clen, Ppos; u_char *P; if (mlen % 16 != SALT_LEN) { log_Printf(LogWARN, "Cannot interpret mangled data of length %ld\n", (u_long)mlen); *buf = NULL; *len = 0; return; } /* We need the RADIUS Request-Authenticator */ if (rad_request_authenticator(r->cx.rad, R, sizeof R) != AUTH_LEN) { log_Printf(LogWARN, "Cannot obtain the RADIUS request authenticator\n"); *buf = NULL; *len = 0; return; } A = (const u_char *)mangled; /* Salt comes first */ C = (const u_char *)mangled + SALT_LEN; /* Then the ciphertext */ Clen = mlen - SALT_LEN; S = rad_server_secret(r->cx.rad); /* We need the RADIUS secret */ Slen = strlen(S); P = alloca(Clen); /* We derive our plaintext */ MD5Init(&Context); MD5Update(&Context, S, Slen); MD5Update(&Context, R, AUTH_LEN); MD5Update(&Context, A, SALT_LEN); MD5Final(b, &Context); Ppos = 0; while (Clen) { Clen -= 16; for (i = 0; i < 16; i++) P[Ppos++] = C[i] ^ b[i]; if (Clen) { MD5Init(&Context); MD5Update(&Context, S, Slen); MD5Update(&Context, C, 16); MD5Final(b, &Context); } C += 16; } /* * The resulting plain text consists of a one-byte length, the text and * maybe some padding. */ *len = *P; if (*len > mlen - 1) { log_Printf(LogWARN, "Mangled data seems to be garbage\n"); *buf = NULL; *len = 0; return; } *buf = malloc(*len); memcpy(*buf, P + 1, *len); } #endif /* * rad_continue_send_request() has given us `got' (non-zero). Deal with it. */ static void radius_Process(struct radius *r, int got) { char *argv[MAXARGS], *nuke; struct bundle *bundle; int argc, addrs, res, width; size_t len; struct ncprange dest; struct ncpaddr gw; const void *data; const char *stype; u_int32_t ipaddr, vendor; struct in_addr ip; +#ifndef NOINET6 + struct in6_addr ip6; +#endif r->cx.fd = -1; /* Stop select()ing */ stype = r->cx.auth ? "auth" : "acct"; switch (got) { case RAD_ACCESS_ACCEPT: log_Printf(LogPHASE, "Radius(%s): ACCEPT received\n", stype); if (!r->cx.auth) { rad_close(r->cx.rad); return; } break; case RAD_ACCESS_REJECT: log_Printf(LogPHASE, "Radius(%s): REJECT received\n", stype); if (!r->cx.auth) { rad_close(r->cx.rad); return; } break; case RAD_ACCESS_CHALLENGE: /* we can't deal with this (for now) ! */ log_Printf(LogPHASE, "Radius: CHALLENGE received (can't handle yet)\n"); if (r->cx.auth) auth_Failure(r->cx.auth); rad_close(r->cx.rad); return; case RAD_ACCOUNTING_RESPONSE: log_Printf(LogPHASE, "Radius(%s): Accounting response received\n", stype); if (r->cx.auth) auth_Failure(r->cx.auth); /* unexpected !!! */ /* No further processing for accounting requests, please */ rad_close(r->cx.rad); return; case -1: log_Printf(LogPHASE, "radius(%s): %s\n", stype, rad_strerror(r->cx.rad)); if (r->cx.auth) auth_Failure(r->cx.auth); rad_close(r->cx.rad); return; default: log_Printf(LogERROR, "rad_send_request(%s): Failed %d: %s\n", stype, got, rad_strerror(r->cx.rad)); if (r->cx.auth) auth_Failure(r->cx.auth); rad_close(r->cx.rad); return; } /* Let's see what we've got in our reply */ r->ip.s_addr = r->mask.s_addr = INADDR_NONE; r->mtu = 0; r->vj = 0; while ((res = rad_get_attr(r->cx.rad, &data, &len)) > 0) { switch (res) { case RAD_FRAMED_IP_ADDRESS: r->ip = rad_cvt_addr(data); log_Printf(LogPHASE, " IP %s\n", inet_ntoa(r->ip)); break; case RAD_FILTER_ID: free(r->filterid); if ((r->filterid = rad_cvt_string(data, len)) == NULL) { log_Printf(LogERROR, "rad_cvt_string: %s\n", rad_strerror(r->cx.rad)); auth_Failure(r->cx.auth); rad_close(r->cx.rad); return; } log_Printf(LogPHASE, " Filter \"%s\"\n", r->filterid); break; case RAD_SESSION_TIMEOUT: r->sessiontime = rad_cvt_int(data); log_Printf(LogPHASE, " Session-Timeout %lu\n", r->sessiontime); break; case RAD_FRAMED_IP_NETMASK: r->mask = rad_cvt_addr(data); log_Printf(LogPHASE, " Netmask %s\n", inet_ntoa(r->mask)); break; case RAD_FRAMED_MTU: r->mtu = rad_cvt_int(data); log_Printf(LogPHASE, " MTU %lu\n", r->mtu); break; case RAD_FRAMED_ROUTING: /* Disabled for now - should we automatically set up some filters ? */ /* rad_cvt_int(data); */ /* bit 1 = Send routing packets */ /* bit 2 = Receive routing packets */ break; case RAD_FRAMED_COMPRESSION: r->vj = rad_cvt_int(data) == 1 ? 1 : 0; log_Printf(LogPHASE, " VJ %sabled\n", r->vj ? "en" : "dis"); break; case RAD_FRAMED_ROUTE: /* * We expect a string of the format ``dest[/bits] gw [metrics]'' * Any specified metrics are ignored. MYADDR and HISADDR are * understood for ``dest'' and ``gw'' and ``0.0.0.0'' is the same * as ``HISADDR''. */ if ((nuke = rad_cvt_string(data, len)) == NULL) { log_Printf(LogERROR, "rad_cvt_string: %s\n", rad_strerror(r->cx.rad)); auth_Failure(r->cx.auth); rad_close(r->cx.rad); return; } log_Printf(LogPHASE, " Route: %s\n", nuke); bundle = r->cx.auth->physical->dl->bundle; ip.s_addr = INADDR_ANY; + ncpaddr_setip4(&gw, ip); ncprange_setip4host(&dest, ip); argc = command_Interpret(nuke, strlen(nuke), argv); if (argc < 0) log_Printf(LogWARN, "radius: %s: Syntax error\n", argc == 1 ? argv[0] : "\"\""); else if (argc < 2) log_Printf(LogWARN, "radius: %s: Invalid route\n", argc == 1 ? argv[0] : "\"\""); else if ((strcasecmp(argv[0], "default") != 0 && !ncprange_aton(&dest, &bundle->ncp, argv[0])) || !ncpaddr_aton(&gw, &bundle->ncp, argv[1])) log_Printf(LogWARN, "radius: %s %s: Invalid route\n", argv[0], argv[1]); else { ncprange_getwidth(&dest, &width); if (width == 32 && strchr(argv[0], '/') == NULL) { /* No mask specified - use the natural mask */ ncprange_getip4addr(&dest, &ip); ncprange_setip4mask(&dest, addr2mask(ip)); } addrs = 0; if (!strncasecmp(argv[0], "HISADDR", 7)) addrs = ROUTE_DSTHISADDR; else if (!strncasecmp(argv[0], "MYADDR", 6)) addrs = ROUTE_DSTMYADDR; if (ncpaddr_getip4addr(&gw, &ipaddr) && ipaddr == INADDR_ANY) { addrs |= ROUTE_GWHISADDR; ncpaddr_setip4(&gw, bundle->ncp.ipcp.peer_ip); } else if (strcasecmp(argv[1], "HISADDR") == 0) addrs |= ROUTE_GWHISADDR; route_Add(&r->routes, addrs, &dest, &gw); } free(nuke); break; case RAD_REPLY_MESSAGE: free(r->repstr); if ((r->repstr = rad_cvt_string(data, len)) == NULL) { log_Printf(LogERROR, "rad_cvt_string: %s\n", rad_strerror(r->cx.rad)); auth_Failure(r->cx.auth); rad_close(r->cx.rad); return; } log_Printf(LogPHASE, " Reply-Message \"%s\"\n", r->repstr); break; +#ifndef NOINET6 + case RAD_FRAMED_IPV6_ROUTE: + /* + * We expect a string of the format ``dest[/bits] gw [metrics]'' + * Any specified metrics are ignored. MYADDR6 and HISADDR6 are + * understood for ``dest'' and ``gw'' and ``::'' is the same + * as ``HISADDR6''. + */ + + if ((nuke = rad_cvt_string(data, len)) == NULL) { + log_Printf(LogERROR, "rad_cvt_string: %s\n", rad_strerror(r->cx.rad)); + auth_Failure(r->cx.auth); + rad_close(r->cx.rad); + return; + } + + log_Printf(LogPHASE, " IPv6 Route: %s\n", nuke); + bundle = r->cx.auth->physical->dl->bundle; + ncpaddr_setip6(&gw, &in6addr_any); + ncprange_set(&dest, &gw, 0); + argc = command_Interpret(nuke, strlen(nuke), argv); + if (argc < 0) + log_Printf(LogWARN, "radius: %s: Syntax error\n", + argc == 1 ? argv[0] : "\"\""); + else if (argc < 2) + log_Printf(LogWARN, "radius: %s: Invalid route\n", + argc == 1 ? argv[0] : "\"\""); + else if ((strcasecmp(argv[0], "default") != 0 && + !ncprange_aton(&dest, &bundle->ncp, argv[0])) || + !ncpaddr_aton(&gw, &bundle->ncp, argv[1])) + log_Printf(LogWARN, "radius: %s %s: Invalid route\n", + argv[0], argv[1]); + else { + addrs = 0; + + if (!strncasecmp(argv[0], "HISADDR6", 8)) + addrs = ROUTE_DSTHISADDR6; + else if (!strncasecmp(argv[0], "MYADDR6", 7)) + addrs = ROUTE_DSTMYADDR6; + + if (ncpaddr_getip6(&gw, &ip6) && IN6_IS_ADDR_UNSPECIFIED(&ip6)) { + addrs |= ROUTE_GWHISADDR6; + ncpaddr_copy(&gw, &bundle->ncp.ipv6cp.hisaddr); + } else if (strcasecmp(argv[1], "HISADDR6") == 0) + addrs |= ROUTE_GWHISADDR6; + + route_Add(&r->ipv6routes, addrs, &dest, &gw); + } + free(nuke); + break; +#endif + case RAD_VENDOR_SPECIFIC: if ((res = rad_get_vendor_attr(&vendor, &data, &len)) <= 0) { log_Printf(LogERROR, "rad_get_vendor_attr: %s (failing!)\n", rad_strerror(r->cx.rad)); auth_Failure(r->cx.auth); rad_close(r->cx.rad); return; } switch (vendor) { case RAD_VENDOR_MICROSOFT: switch (res) { #ifndef NODES case RAD_MICROSOFT_MS_CHAP_ERROR: free(r->errstr); if (len == 0) r->errstr = NULL; else { if (len < 3 || ((const char *)data)[1] != '=') { /* * Only point at the String field if we don't think the * peer has misformatted the response. */ ((const char *)data)++; len--; } else log_Printf(LogWARN, "Warning: The MS-CHAP-Error " "attribute is mis-formatted. Compensating\n"); if ((r->errstr = rad_cvt_string((const char *)data, len)) == NULL) { log_Printf(LogERROR, "rad_cvt_string: %s\n", rad_strerror(r->cx.rad)); auth_Failure(r->cx.auth); rad_close(r->cx.rad); return; } log_Printf(LogPHASE, " MS-CHAP-Error \"%s\"\n", r->errstr); } break; case RAD_MICROSOFT_MS_CHAP2_SUCCESS: free(r->msrepstr); if (len == 0) r->msrepstr = NULL; else { if (len < 3 || ((const char *)data)[1] != '=') { /* * Only point at the String field if we don't think the * peer has misformatted the response. */ ((const char *)data)++; len--; } else log_Printf(LogWARN, "Warning: The MS-CHAP2-Success " "attribute is mis-formatted. Compensating\n"); if ((r->msrepstr = rad_cvt_string((const char *)data, len)) == NULL) { log_Printf(LogERROR, "rad_cvt_string: %s\n", rad_strerror(r->cx.rad)); auth_Failure(r->cx.auth); rad_close(r->cx.rad); return; } log_Printf(LogPHASE, " MS-CHAP2-Success \"%s\"\n", r->msrepstr); } break; case RAD_MICROSOFT_MS_MPPE_ENCRYPTION_POLICY: r->mppe.policy = rad_cvt_int(data); log_Printf(LogPHASE, " MS-MPPE-Encryption-Policy %s\n", radius_policyname(r->mppe.policy)); break; case RAD_MICROSOFT_MS_MPPE_ENCRYPTION_TYPES: r->mppe.types = rad_cvt_int(data); log_Printf(LogPHASE, " MS-MPPE-Encryption-Types %s\n", radius_typesname(r->mppe.types)); break; case RAD_MICROSOFT_MS_MPPE_RECV_KEY: free(r->mppe.recvkey); demangle(r, data, len, &r->mppe.recvkey, &r->mppe.recvkeylen); log_Printf(LogPHASE, " MS-MPPE-Recv-Key ********\n"); break; case RAD_MICROSOFT_MS_MPPE_SEND_KEY: demangle(r, data, len, &r->mppe.sendkey, &r->mppe.sendkeylen); log_Printf(LogPHASE, " MS-MPPE-Send-Key ********\n"); break; #endif default: log_Printf(LogDEBUG, "Dropping MICROSOFT vendor specific " "RADIUS attribute %d\n", res); break; } break; default: log_Printf(LogDEBUG, "Dropping vendor %lu RADIUS attribute %d\n", (unsigned long)vendor, res); break; } break; default: log_Printf(LogDEBUG, "Dropping RADIUS attribute %d\n", res); break; } } if (res == -1) { log_Printf(LogERROR, "rad_get_attr: %s (failing!)\n", rad_strerror(r->cx.rad)); auth_Failure(r->cx.auth); } else if (got == RAD_ACCESS_REJECT) auth_Failure(r->cx.auth); else { r->valid = 1; auth_Success(r->cx.auth); } rad_close(r->cx.rad); } /* * We've either timed out or select()ed on the read descriptor */ static void radius_Continue(struct radius *r, int sel) { struct timeval tv; int got; timer_Stop(&r->cx.timer); if ((got = rad_continue_send_request(r->cx.rad, sel, &r->cx.fd, &tv)) == 0) { log_Printf(LogPHASE, "Radius: Request re-sent\n"); r->cx.timer.load = tv.tv_usec / TICKUNIT + tv.tv_sec * SECTICKS; timer_Start(&r->cx.timer); return; } radius_Process(r, got); } /* * Time to call rad_continue_send_request() - timed out. */ static void radius_Timeout(void *v) { radius_Continue((struct radius *)v, 0); } /* * Time to call rad_continue_send_request() - something to read. */ static void radius_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset) { radius_Continue(descriptor2radius(d), 1); } /* * Behave as a struct fdescriptor (descriptor.h) */ static int radius_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) { struct radius *rad = descriptor2radius(d); if (r && rad->cx.fd != -1) { FD_SET(rad->cx.fd, r); if (*n < rad->cx.fd + 1) *n = rad->cx.fd + 1; log_Printf(LogTIMER, "Radius: fdset(r) %d\n", rad->cx.fd); return 1; } return 0; } /* * Behave as a struct fdescriptor (descriptor.h) */ static int radius_IsSet(struct fdescriptor *d, const fd_set *fdset) { struct radius *r = descriptor2radius(d); return r && r->cx.fd != -1 && FD_ISSET(r->cx.fd, fdset); } /* * Behave as a struct fdescriptor (descriptor.h) */ static int radius_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset) { /* We never want to write here ! */ log_Printf(LogALERT, "radius_Write: Internal error: Bad call !\n"); return 0; } /* * Initialise ourselves */ void radius_Init(struct radius *r) { r->desc.type = RADIUS_DESCRIPTOR; r->desc.UpdateSet = radius_UpdateSet; r->desc.IsSet = radius_IsSet; r->desc.Read = radius_Read; r->desc.Write = radius_Write; r->cx.fd = -1; r->cx.rad = NULL; memset(&r->cx.timer, '\0', sizeof r->cx.timer); r->cx.auth = NULL; r->valid = 0; r->vj = 0; r->ip.s_addr = INADDR_ANY; r->mask.s_addr = INADDR_NONE; r->routes = NULL; r->mtu = DEF_MTU; r->msrepstr = NULL; r->repstr = NULL; +#ifndef NOINET6 + r->ipv6routes = NULL; +#endif r->errstr = NULL; r->mppe.policy = 0; r->mppe.types = 0; r->mppe.recvkey = NULL; r->mppe.recvkeylen = 0; r->mppe.sendkey = NULL; r->mppe.sendkeylen = 0; *r->cfg.file = '\0';; log_Printf(LogDEBUG, "Radius: radius_Init\n"); } /* * Forget everything and go back to initialised state. */ void radius_Destroy(struct radius *r) { r->valid = 0; log_Printf(LogDEBUG, "Radius: radius_Destroy\n"); timer_Stop(&r->cx.timer); route_DeleteAll(&r->routes); +#ifndef NOINET6 + route_DeleteAll(&r->ipv6routes); +#endif free(r->filterid); r->filterid = NULL; free(r->msrepstr); r->msrepstr = NULL; free(r->repstr); r->repstr = NULL; free(r->errstr); r->errstr = NULL; free(r->mppe.recvkey); r->mppe.recvkey = NULL; r->mppe.recvkeylen = 0; free(r->mppe.sendkey); r->mppe.sendkey = NULL; r->mppe.sendkeylen = 0; if (r->cx.fd != -1) { r->cx.fd = -1; rad_close(r->cx.rad); } } static int radius_put_physical_details(struct rad_handle *rad, struct physical *p) { int slot, type; type = RAD_VIRTUAL; if (p->handler) switch (p->handler->type) { case I4B_DEVICE: type = RAD_ISDN_SYNC; break; case TTY_DEVICE: type = RAD_ASYNC; break; case ETHER_DEVICE: type = RAD_ETHERNET; break; case TCP_DEVICE: case UDP_DEVICE: case EXEC_DEVICE: case ATM_DEVICE: case NG_DEVICE: type = RAD_VIRTUAL; break; } if (rad_put_int(rad, RAD_NAS_PORT_TYPE, type) != 0) { log_Printf(LogERROR, "rad_put: rad_put_int: %s\n", rad_strerror(rad)); rad_close(rad); return 0; } if ((slot = physical_Slot(p)) >= 0) if (rad_put_int(rad, RAD_NAS_PORT, slot) != 0) { log_Printf(LogERROR, "rad_put: rad_put_int: %s\n", rad_strerror(rad)); rad_close(rad); return 0; } return 1; } /* * Start an authentication request to the RADIUS server. */ int radius_Authenticate(struct radius *r, struct authinfo *authp, const char *name, const char *key, int klen, const char *nchallenge, int nclen) { struct timeval tv; int got; char hostname[MAXHOSTNAMELEN]; #if 0 struct hostent *hp; struct in_addr hostaddr; #endif #ifndef NODES struct mschap_response msresp; struct mschap2_response msresp2; const struct MSCHAPv2_resp *keyv2; #endif if (!*r->cfg.file) return 0; if (r->cx.fd != -1) /* * We assume that our name/key/challenge is the same as last time, * and just continue to wait for the RADIUS server(s). */ return 1; radius_Destroy(r); if ((r->cx.rad = rad_auth_open()) == NULL) { log_Printf(LogERROR, "rad_auth_open: %s\n", strerror(errno)); return 0; } if (rad_config(r->cx.rad, r->cfg.file) != 0) { log_Printf(LogERROR, "rad_config: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return 0; } if (rad_create_request(r->cx.rad, RAD_ACCESS_REQUEST) != 0) { log_Printf(LogERROR, "rad_create_request: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return 0; } if (rad_put_string(r->cx.rad, RAD_USER_NAME, name) != 0 || rad_put_int(r->cx.rad, RAD_SERVICE_TYPE, RAD_FRAMED) != 0 || rad_put_int(r->cx.rad, RAD_FRAMED_PROTOCOL, RAD_PPP) != 0) { log_Printf(LogERROR, "rad_put: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return 0; } switch (authp->physical->link.lcp.want_auth) { case PROTO_PAP: /* We're talking PAP */ if (rad_put_attr(r->cx.rad, RAD_USER_PASSWORD, key, klen) != 0) { log_Printf(LogERROR, "PAP: rad_put_string: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return 0; } break; case PROTO_CHAP: switch (authp->physical->link.lcp.want_authtype) { case 0x5: if (rad_put_attr(r->cx.rad, RAD_CHAP_PASSWORD, key, klen) != 0 || rad_put_attr(r->cx.rad, RAD_CHAP_CHALLENGE, nchallenge, nclen) != 0) { log_Printf(LogERROR, "CHAP: rad_put_string: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return 0; } break; #ifndef NODES case 0x80: if (klen != 50) { log_Printf(LogERROR, "CHAP80: Unrecognised key length %d\n", klen); rad_close(r->cx.rad); return 0; } rad_put_vendor_attr(r->cx.rad, RAD_VENDOR_MICROSOFT, RAD_MICROSOFT_MS_CHAP_CHALLENGE, nchallenge, nclen); msresp.ident = *key; msresp.flags = 0x01; memcpy(msresp.lm_response, key + 1, 24); memcpy(msresp.nt_response, key + 25, 24); rad_put_vendor_attr(r->cx.rad, RAD_VENDOR_MICROSOFT, RAD_MICROSOFT_MS_CHAP_RESPONSE, &msresp, sizeof msresp); break; case 0x81: if (klen != sizeof(*keyv2) + 1) { log_Printf(LogERROR, "CHAP81: Unrecognised key length %d\n", klen); rad_close(r->cx.rad); return 0; } keyv2 = (const struct MSCHAPv2_resp *)(key + 1); rad_put_vendor_attr(r->cx.rad, RAD_VENDOR_MICROSOFT, RAD_MICROSOFT_MS_CHAP_CHALLENGE, nchallenge, nclen); msresp2.ident = *key; msresp2.flags = keyv2->Flags; memcpy(msresp2.response, keyv2->NTResponse, sizeof msresp2.response); memset(msresp2.reserved, '\0', sizeof msresp2.reserved); memcpy(msresp2.pchallenge, keyv2->PeerChallenge, sizeof msresp2.pchallenge); rad_put_vendor_attr(r->cx.rad, RAD_VENDOR_MICROSOFT, RAD_MICROSOFT_MS_CHAP2_RESPONSE, &msresp2, sizeof msresp2); break; #endif default: log_Printf(LogERROR, "CHAP: Unrecognised type 0x%02x\n", authp->physical->link.lcp.want_authtype); rad_close(r->cx.rad); return 0; } } if (gethostname(hostname, sizeof hostname) != 0) log_Printf(LogERROR, "rad_put: gethostname(): %s\n", strerror(errno)); else { #if 0 if ((hp = gethostbyname(hostname)) != NULL) { hostaddr.s_addr = *(u_long *)hp->h_addr; if (rad_put_addr(r->cx.rad, RAD_NAS_IP_ADDRESS, hostaddr) != 0) { log_Printf(LogERROR, "rad_put: rad_put_string: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return 0; } } #endif if (rad_put_string(r->cx.rad, RAD_NAS_IDENTIFIER, hostname) != 0) { log_Printf(LogERROR, "rad_put: rad_put_string: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return 0; } } radius_put_physical_details(r->cx.rad, authp->physical); r->cx.auth = authp; if ((got = rad_init_send_request(r->cx.rad, &r->cx.fd, &tv))) radius_Process(r, got); else { log_Printf(LogPHASE, "Radius: Request sent\n"); log_Printf(LogDEBUG, "Using radius_Timeout [%p]\n", radius_Timeout); r->cx.timer.load = tv.tv_usec / TICKUNIT + tv.tv_sec * SECTICKS; r->cx.timer.func = radius_Timeout; r->cx.timer.name = "radius auth"; r->cx.timer.arg = r; timer_Start(&r->cx.timer); } return 1; } /* * Send an accounting request to the RADIUS server */ void radius_Account(struct radius *r, struct radacct *ac, struct datalink *dl, int acct_type, struct in_addr *peer_ip, struct in_addr *netmask, struct pppThroughput *stats) { struct timeval tv; int got; char hostname[MAXHOSTNAMELEN]; #if 0 struct hostent *hp; struct in_addr hostaddr; #endif if (!*r->cfg.file) return; if (r->cx.fd != -1) /* * We assume that our name/key/challenge is the same as last time, * and just continue to wait for the RADIUS server(s). */ return; timer_Stop(&r->cx.timer); if ((r->cx.rad = rad_acct_open()) == NULL) { log_Printf(LogERROR, "rad_auth_open: %s\n", strerror(errno)); return; } if (rad_config(r->cx.rad, r->cfg.file) != 0) { log_Printf(LogERROR, "rad_config: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return; } if (rad_create_request(r->cx.rad, RAD_ACCOUNTING_REQUEST) != 0) { log_Printf(LogERROR, "rad_create_request: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return; } /* Grab some accounting data and initialize structure */ if (acct_type == RAD_START) { ac->rad_parent = r; /* Fetch username from datalink */ strncpy(ac->user_name, dl->peer.authname, sizeof ac->user_name); ac->user_name[AUTHLEN-1] = '\0'; ac->authentic = 2; /* Assume RADIUS verified auth data */ /* Generate a session ID */ snprintf(ac->session_id, sizeof ac->session_id, "%s%ld-%s%lu", dl->bundle->cfg.auth.name, (long)getpid(), dl->peer.authname, (unsigned long)stats->uptime); /* And grab our MP socket name */ snprintf(ac->multi_session_id, sizeof ac->multi_session_id, "%s", dl->bundle->ncp.mp.active ? dl->bundle->ncp.mp.server.socket.sun_path : ""); /* Fetch IP, netmask from IPCP */ memcpy(&ac->ip, peer_ip, sizeof(ac->ip)); memcpy(&ac->mask, netmask, sizeof(ac->mask)); }; if (rad_put_string(r->cx.rad, RAD_USER_NAME, ac->user_name) != 0 || rad_put_int(r->cx.rad, RAD_SERVICE_TYPE, RAD_FRAMED) != 0 || rad_put_int(r->cx.rad, RAD_FRAMED_PROTOCOL, RAD_PPP) != 0 || rad_put_addr(r->cx.rad, RAD_FRAMED_IP_ADDRESS, ac->ip) != 0 || rad_put_addr(r->cx.rad, RAD_FRAMED_IP_NETMASK, ac->mask) != 0) { log_Printf(LogERROR, "rad_put: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return; } if (gethostname(hostname, sizeof hostname) != 0) log_Printf(LogERROR, "rad_put: gethostname(): %s\n", strerror(errno)); else { #if 0 if ((hp = gethostbyname(hostname)) != NULL) { hostaddr.s_addr = *(u_long *)hp->h_addr; if (rad_put_addr(r->cx.rad, RAD_NAS_IP_ADDRESS, hostaddr) != 0) { log_Printf(LogERROR, "rad_put: rad_put_string: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return; } } #endif if (rad_put_string(r->cx.rad, RAD_NAS_IDENTIFIER, hostname) != 0) { log_Printf(LogERROR, "rad_put: rad_put_string: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return; } } radius_put_physical_details(r->cx.rad, dl->physical); if (rad_put_int(r->cx.rad, RAD_ACCT_STATUS_TYPE, acct_type) != 0 || rad_put_string(r->cx.rad, RAD_ACCT_SESSION_ID, ac->session_id) != 0 || rad_put_string(r->cx.rad, RAD_ACCT_MULTI_SESSION_ID, ac->multi_session_id) != 0 || rad_put_int(r->cx.rad, RAD_ACCT_DELAY_TIME, 0) != 0) { /* XXX ACCT_DELAY_TIME should be increased each time a packet is waiting */ log_Printf(LogERROR, "rad_put: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return; } if (acct_type == RAD_STOP) /* Show some statistics */ if (rad_put_int(r->cx.rad, RAD_ACCT_INPUT_OCTETS, stats->OctetsIn) != 0 || rad_put_int(r->cx.rad, RAD_ACCT_INPUT_PACKETS, stats->PacketsIn) != 0 || rad_put_int(r->cx.rad, RAD_ACCT_OUTPUT_OCTETS, stats->OctetsOut) != 0 || rad_put_int(r->cx.rad, RAD_ACCT_OUTPUT_PACKETS, stats->PacketsOut) != 0 || rad_put_int(r->cx.rad, RAD_ACCT_SESSION_TIME, throughput_uptime(stats)) != 0) { log_Printf(LogERROR, "rad_put: %s\n", rad_strerror(r->cx.rad)); rad_close(r->cx.rad); return; } r->cx.auth = NULL; /* Not valid for accounting requests */ if ((got = rad_init_send_request(r->cx.rad, &r->cx.fd, &tv))) radius_Process(r, got); else { log_Printf(LogDEBUG, "Using radius_Timeout [%p]\n", radius_Timeout); r->cx.timer.load = tv.tv_usec / TICKUNIT + tv.tv_sec * SECTICKS; r->cx.timer.func = radius_Timeout; r->cx.timer.name = "radius acct"; r->cx.timer.arg = r; timer_Start(&r->cx.timer); } } /* * How do things look at the moment ? */ void radius_Show(struct radius *r, struct prompt *p) { prompt_Printf(p, " Radius config: %s", *r->cfg.file ? r->cfg.file : "none"); if (r->valid) { prompt_Printf(p, "\n IP: %s\n", inet_ntoa(r->ip)); prompt_Printf(p, " Netmask: %s\n", inet_ntoa(r->mask)); prompt_Printf(p, " MTU: %lu\n", r->mtu); prompt_Printf(p, " VJ: %sabled\n", r->vj ? "en" : "dis"); prompt_Printf(p, " Message: %s\n", r->repstr ? r->repstr : ""); prompt_Printf(p, " MPPE Enc Policy: %s\n", radius_policyname(r->mppe.policy)); prompt_Printf(p, " MPPE Enc Types: %s\n", radius_typesname(r->mppe.types)); prompt_Printf(p, " MPPE Recv Key: %seceived\n", r->mppe.recvkey ? "R" : "Not r"); prompt_Printf(p, " MPPE Send Key: %seceived\n", r->mppe.sendkey ? "R" : "Not r"); prompt_Printf(p, " MS-CHAP2-Response: %s\n", r->msrepstr ? r->msrepstr : ""); prompt_Printf(p, " Error Message: %s\n", r->errstr ? r->errstr : ""); if (r->routes) route_ShowSticky(p, r->routes, " Routes", 16); +#ifndef NOINET6 + if (r->ipv6routes) + route_ShowSticky(p, r->ipv6routes, " IPv6 Routes", 16); +#endif } else prompt_Printf(p, " (not authenticated)\n"); } Index: stable/4/usr.sbin/ppp/radius.h =================================================================== --- stable/4/usr.sbin/ppp/radius.h (revision 116982) +++ stable/4/usr.sbin/ppp/radius.h (revision 116983) @@ -1,100 +1,103 @@ /* * Copyright 1999 Internet Business Solutions Ltd., Switzerland * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #define MPPE_POLICY_ALLOWED 1 #define MPPE_POLICY_REQUIRED 2 #define MPPE_TYPE_40BIT 2 #define MPPE_TYPE_128BIT 4 struct radius { struct fdescriptor desc; /* We're a sort of (selectable) fdescriptor */ struct { int fd; /* We're selecting on this */ struct rad_handle *rad; /* Using this to talk to our lib */ struct pppTimer timer; /* for this long */ struct authinfo *auth; /* Tell this about success/failure */ } cx; unsigned valid : 1; /* Is this structure valid ? */ unsigned vj : 1; /* FRAMED Compression */ struct in_addr ip; /* FRAMED IP */ struct in_addr mask; /* FRAMED Netmask */ unsigned long mtu; /* FRAMED MTU */ unsigned long sessiontime; /* Session-Timeout */ char *filterid; /* FRAMED Filter Id */ struct sticky_route *routes; /* FRAMED Routes */ char *msrepstr; /* MS-CHAP2-Response */ char *repstr; /* Reply-Message */ char *errstr; /* Error-Message */ +#ifndef NOINET6 + struct sticky_route *ipv6routes; /* FRAMED IPv6 Routes */ +#endif struct { int policy; /* MPPE_POLICY_* */ int types; /* MPPE_TYPE_*BIT bitmask */ char *recvkey; size_t recvkeylen; char *sendkey; size_t sendkeylen; } mppe; struct { char file[PATH_MAX]; /* Radius config file */ } cfg; }; struct radacct { struct radius *rad_parent; /* "Parent" struct radius stored in bundle */ char user_name[AUTHLEN]; /* Session User-Name */ char session_id[256]; /* Unique session ID */ char multi_session_id[51]; /* Unique MP session ID */ int authentic; /* How the session has been authenticated */ struct in_addr ip; struct in_addr mask; }; #define descriptor2radius(d) \ ((d)->type == RADIUS_DESCRIPTOR ? (struct radius *)(d) : NULL) struct bundle; extern void radius_Init(struct radius *); extern void radius_Destroy(struct radius *); extern void radius_Show(struct radius *, struct prompt *); extern int radius_Authenticate(struct radius *, struct authinfo *, const char *, const char *, int, const char *, int); extern void radius_Account(struct radius *, struct radacct *, struct datalink *, int, struct in_addr *, struct in_addr *, struct pppThroughput *); /* An (int) parameter to radius_Account, from radlib.h */ #if !defined(RAD_START) #define RAD_START 1 #define RAD_STOP 2 #endif /* Get address from NAS pool */ #define RADIUS_INADDR_POOL htonl(0xfffffffe) /* 255.255.255.254 */