Index: stable/4/usr.sbin/ppp/bundle.h =================================================================== --- stable/4/usr.sbin/ppp/bundle.h (revision 116983) +++ stable/4/usr.sbin/ppp/bundle.h (revision 116984) @@ -1,207 +1,210 @@ /*- * Copyright (c) 1998 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$ */ #define PHASE_DEAD 0 /* Link is dead */ #define PHASE_ESTABLISH 1 /* Establishing link */ #define PHASE_AUTHENTICATE 2 /* Being authenticated */ #define PHASE_NETWORK 3 /* We're alive ! */ #define PHASE_TERMINATE 4 /* Terminating link */ /* cfg.opt bit settings */ #define OPT_FILTERDECAP 0x0001 #define OPT_IDCHECK 0x0002 #define OPT_IFACEALIAS 0x0004 #ifndef NOINET6 #define OPT_IPCP 0x0008 #define OPT_IPV6CP 0x0010 #endif #define OPT_KEEPSESSION 0x0020 #define OPT_LOOPBACK 0x0040 #define OPT_PASSWDAUTH 0x0080 #define OPT_PROXY 0x0100 #define OPT_PROXYALL 0x0200 #define OPT_SROUTES 0x0400 #define OPT_TCPMSSFIXUP 0x0800 #define OPT_THROUGHPUT 0x1000 #define OPT_UTMP 0x2000 #define MAX_ENDDISC_CLASS 5 #define Enabled(b, o) ((b)->cfg.opt & (o)) /* AutoAdjust() values */ #define AUTO_UP 1 #define AUTO_DOWN 2 struct sockaddr_un; struct datalink; struct physical; struct link; struct server; struct prompt; struct iface; struct bundle { struct fdescriptor desc; /* really all our datalinks */ int unit; /* The device/interface unit number */ struct { char Name[20]; /* The /dev/XXXX name */ int fd; /* The /dev/XXXX descriptor */ unsigned header : 1; /* Family header sent & received ? */ } dev; u_long bandwidth; /* struct tuninfo speed */ struct iface *iface; /* Interface information */ int routing_seq; /* The current routing sequence number */ u_int phase; /* Curent phase */ struct { int all; /* Union of all physical::type's */ int open; /* Union of all open physical::type's */ } phys_type; unsigned CleaningUp : 1; /* Going to exit.... */ unsigned NatEnabled : 1; /* Are we using libalias ? */ struct fsm_parent fsm; /* Our callback functions */ struct datalink *links; /* Our data links */ time_t upat; /* When the link came up */ struct { struct { int timeout; /* NCP Idle timeout value */ int min_timeout; /* Don't idle out before this */ } idle; struct { char name[AUTHLEN]; /* PAP/CHAP system name */ char key[AUTHLEN]; /* PAP/CHAP key */ } auth; unsigned opt; /* Uses OPT_ bits from above */ char label[50]; /* last thing `load'ed */ u_short ifqueue; /* Interface queue size */ struct { int timeout; /* How long to leave the output queue choked */ } choked; } cfg; struct ncp ncp; struct { struct filter in; /* incoming packet filter */ struct filter out; /* outgoing packet filter */ struct filter dial; /* dial-out packet filter */ struct filter alive; /* keep-alive packet filter */ } filter; struct { struct pppTimer timer; /* timeout after cfg.idle_timeout */ time_t done; } idle; #ifndef NORADIUS struct { struct pppTimer timer; time_t done; } session; #endif struct { int fd; /* write status here */ } notify; struct { struct pppTimer timer; /* choked output queue timer */ } choked; #ifndef NORADIUS struct radius radius; /* Info retrieved from radius server */ struct radacct radacct; +#ifndef NOINET6 + struct radacct radacct6; +#endif #endif }; #define descriptor2bundle(d) \ ((d)->type == BUNDLE_DESCRIPTOR ? (struct bundle *)(d) : NULL) extern struct bundle *bundle_Create(const char *, int, int); extern void bundle_Destroy(struct bundle *); extern const char *bundle_PhaseName(struct bundle *); #define bundle_Phase(b) ((b)->phase) extern void bundle_NewPhase(struct bundle *, u_int); extern void bundle_LinksRemoved(struct bundle *); extern void bundle_Close(struct bundle *, const char *, int); extern void bundle_Down(struct bundle *, int); extern void bundle_Open(struct bundle *, const char *, int, int); extern void bundle_LinkClosed(struct bundle *, struct datalink *); extern int bundle_ShowLinks(struct cmdargs const *); extern int bundle_ShowStatus(struct cmdargs const *); extern void bundle_StartIdleTimer(struct bundle *, unsigned secs); extern void bundle_SetIdleTimer(struct bundle *, int, int); extern void bundle_StopIdleTimer(struct bundle *); extern int bundle_IsDead(struct bundle *); extern struct datalink *bundle2datalink(struct bundle *, const char *); #ifndef NORADIUS extern void bundle_StartSessionTimer(struct bundle *, unsigned secs); extern void bundle_StopSessionTimer(struct bundle *); #endif extern void bundle_RegisterDescriptor(struct bundle *, struct fdescriptor *); extern void bundle_UnRegisterDescriptor(struct bundle *, struct fdescriptor *); extern void bundle_SetTtyCommandMode(struct bundle *, struct datalink *); extern int bundle_DatalinkClone(struct bundle *, struct datalink *, const char *); extern void bundle_DatalinkRemove(struct bundle *, struct datalink *); extern void bundle_CleanDatalinks(struct bundle *); extern void bundle_SetLabel(struct bundle *, const char *); extern const char *bundle_GetLabel(struct bundle *); extern void bundle_SendDatalink(struct datalink *, int, struct sockaddr_un *); extern int bundle_LinkSize(void); extern void bundle_ReceiveDatalink(struct bundle *, int); extern int bundle_SetMode(struct bundle *, struct datalink *, int); extern int bundle_RenameDatalink(struct bundle *, struct datalink *, const char *); extern void bundle_setsid(struct bundle *, int); extern void bundle_LockTun(struct bundle *); extern int bundle_HighestState(struct bundle *); extern int bundle_Exception(struct bundle *, int); extern void bundle_AdjustFilters(struct bundle *, struct ncpaddr *, struct ncpaddr *); extern void bundle_AdjustDNS(struct bundle *); extern void bundle_CalculateBandwidth(struct bundle *); extern void bundle_AutoAdjust(struct bundle *, int, int); extern int bundle_WantAutoloadTimer(struct bundle *); extern void bundle_ChangedPID(struct bundle *); extern void bundle_Notify(struct bundle *, char); extern int bundle_Uptime(struct bundle *); Index: stable/4/usr.sbin/ppp/ipcp.c =================================================================== --- stable/4/usr.sbin/ppp/ipcp.c (revision 116983) +++ stable/4/usr.sbin/ppp/ipcp.c (revision 116984) @@ -1,1476 +1,1476 @@ /*- * Copyright (c) 1996 - 2001 Brian Somers * based on work by Toshiharu OHNO * Internet Initiative Japan, Inc (IIJ) * 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 #include #include #ifndef NONAT #ifdef LOCALNAT #include "alias.h" #else #include #endif #endif #include "layer.h" #include "ua.h" #include "defs.h" #include "command.h" #include "mbuf.h" #include "log.h" #include "timer.h" #include "fsm.h" #include "proto.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 "filter.h" #include "descriptor.h" #include "vjcomp.h" #include "async.h" #include "ccp.h" #include "link.h" #include "physical.h" #include "mp.h" #ifndef NORADIUS #include "radius.h" #endif #include "ipv6cp.h" #include "ncp.h" #include "bundle.h" #include "id.h" #include "arp.h" #include "systems.h" #include "prompt.h" #include "route.h" #include "iface.h" #undef REJECTED #define REJECTED(p, x) ((p)->peer_reject & (1<<(x))) #define issep(ch) ((ch) == ' ' || (ch) == '\t') #define isip(ch) (((ch) >= '0' && (ch) <= '9') || (ch) == '.') struct compreq { u_short proto; u_char slots; u_char compcid; }; static int IpcpLayerUp(struct fsm *); static void IpcpLayerDown(struct fsm *); static void IpcpLayerStart(struct fsm *); static void IpcpLayerFinish(struct fsm *); static void IpcpInitRestartCounter(struct fsm *, int); static void IpcpSendConfigReq(struct fsm *); static void IpcpSentTerminateReq(struct fsm *); static void IpcpSendTerminateAck(struct fsm *, u_char); static void IpcpDecodeConfig(struct fsm *, u_char *, u_char *, int, struct fsm_decode *); static struct fsm_callbacks ipcp_Callbacks = { IpcpLayerUp, IpcpLayerDown, IpcpLayerStart, IpcpLayerFinish, IpcpInitRestartCounter, IpcpSendConfigReq, IpcpSentTerminateReq, IpcpSendTerminateAck, IpcpDecodeConfig, fsm_NullRecvResetReq, fsm_NullRecvResetAck }; static const char * protoname(int proto) { static struct { int id; const char *txt; } cftypes[] = { /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */ { 1, "IPADDRS" }, /* IP-Addresses */ /* deprecated */ { 2, "COMPPROTO" }, /* IP-Compression-Protocol */ { 3, "IPADDR" }, /* IP-Address */ { 129, "PRIDNS" }, /* 129: Primary DNS Server Address */ { 130, "PRINBNS" }, /* 130: Primary NBNS Server Address */ { 131, "SECDNS" }, /* 131: Secondary DNS Server Address */ { 132, "SECNBNS" } /* 132: Secondary NBNS Server Address */ }; int f; for (f = 0; f < sizeof cftypes / sizeof *cftypes; f++) if (cftypes[f].id == proto) return cftypes[f].txt; return NumStr(proto, NULL, 0); } void ipcp_AddInOctets(struct ipcp *ipcp, int n) { throughput_addin(&ipcp->throughput, n); } void ipcp_AddOutOctets(struct ipcp *ipcp, int n) { throughput_addout(&ipcp->throughput, n); } void ipcp_LoadDNS(struct ipcp *ipcp) { int fd; ipcp->ns.dns[0].s_addr = ipcp->ns.dns[1].s_addr = INADDR_NONE; if (ipcp->ns.resolv != NULL) { free(ipcp->ns.resolv); ipcp->ns.resolv = NULL; } if (ipcp->ns.resolv_nons != NULL) { free(ipcp->ns.resolv_nons); ipcp->ns.resolv_nons = NULL; } ipcp->ns.resolver = 0; if ((fd = open(_PATH_RESCONF, O_RDONLY)) != -1) { struct stat st; if (fstat(fd, &st) == 0) { ssize_t got; if ((ipcp->ns.resolv_nons = (char *)malloc(st.st_size + 1)) == NULL) log_Printf(LogERROR, "Failed to malloc %lu for %s: %s\n", (unsigned long)st.st_size, _PATH_RESCONF, strerror(errno)); else if ((ipcp->ns.resolv = (char *)malloc(st.st_size + 1)) == NULL) { log_Printf(LogERROR, "Failed(2) to malloc %lu for %s: %s\n", (unsigned long)st.st_size, _PATH_RESCONF, strerror(errno)); free(ipcp->ns.resolv_nons); ipcp->ns.resolv_nons = NULL; } else if ((got = read(fd, ipcp->ns.resolv, st.st_size)) != st.st_size) { if (got == -1) log_Printf(LogERROR, "Failed to read %s: %s\n", _PATH_RESCONF, strerror(errno)); else log_Printf(LogERROR, "Failed to read %s, got %lu not %lu\n", _PATH_RESCONF, (unsigned long)got, (unsigned long)st.st_size); free(ipcp->ns.resolv_nons); ipcp->ns.resolv_nons = NULL; free(ipcp->ns.resolv); ipcp->ns.resolv = NULL; } else { char *cp, *cp_nons, *ncp, ch; int n; ipcp->ns.resolv[st.st_size] = '\0'; ipcp->ns.resolver = 1; cp_nons = ipcp->ns.resolv_nons; cp = ipcp->ns.resolv; n = 0; while ((ncp = strstr(cp, "nameserver")) != NULL) { if (ncp != cp) { memcpy(cp_nons, cp, ncp - cp); cp_nons += ncp - cp; } if ((ncp != cp && ncp[-1] != '\n') || !issep(ncp[10])) { memcpy(cp_nons, ncp, 9); cp_nons += 9; cp = ncp + 9; /* Can't match "nameserver" at cp... */ continue; } for (cp = ncp + 11; issep(*cp); cp++) /* Skip whitespace */ ; for (ncp = cp; isip(*ncp); ncp++) /* Jump over IP */ ; ch = *ncp; *ncp = '\0'; if (n < 2 && inet_aton(cp, ipcp->ns.dns)) n++; *ncp = ch; if ((cp = strchr(ncp, '\n')) == NULL) /* Point at next line */ cp = ncp + strlen(ncp); else cp++; } strcpy(cp_nons, cp); /* Copy the end - including the NUL */ cp_nons += strlen(cp_nons) - 1; while (cp_nons >= ipcp->ns.resolv_nons && *cp_nons == '\n') *cp_nons-- = '\0'; if (n == 2 && ipcp->ns.dns[0].s_addr == INADDR_ANY) { ipcp->ns.dns[0].s_addr = ipcp->ns.dns[1].s_addr; ipcp->ns.dns[1].s_addr = INADDR_ANY; } bundle_AdjustDNS(ipcp->fsm.bundle); } } else log_Printf(LogERROR, "Failed to stat opened %s: %s\n", _PATH_RESCONF, strerror(errno)); close(fd); } } int ipcp_WriteDNS(struct ipcp *ipcp) { const char *paddr; mode_t mask; FILE *fp; if (ipcp->ns.dns[0].s_addr == INADDR_ANY && ipcp->ns.dns[1].s_addr == INADDR_ANY) { log_Printf(LogIPCP, "%s not modified: All nameservers NAKd\n", _PATH_RESCONF); return 0; } if (ipcp->ns.dns[0].s_addr == INADDR_ANY) { ipcp->ns.dns[0].s_addr = ipcp->ns.dns[1].s_addr; ipcp->ns.dns[1].s_addr = INADDR_ANY; } mask = umask(022); if ((fp = ID0fopen(_PATH_RESCONF, "w")) != NULL) { umask(mask); if (ipcp->ns.resolv_nons) fputs(ipcp->ns.resolv_nons, fp); paddr = inet_ntoa(ipcp->ns.dns[0]); log_Printf(LogIPCP, "Primary nameserver set to %s\n", paddr); fprintf(fp, "\nnameserver %s\n", paddr); if (ipcp->ns.dns[1].s_addr != INADDR_ANY && ipcp->ns.dns[1].s_addr != INADDR_NONE && ipcp->ns.dns[1].s_addr != ipcp->ns.dns[0].s_addr) { paddr = inet_ntoa(ipcp->ns.dns[1]); log_Printf(LogIPCP, "Secondary nameserver set to %s\n", paddr); fprintf(fp, "nameserver %s\n", paddr); } if (fclose(fp) == EOF) { log_Printf(LogERROR, "write(): Failed updating %s: %s\n", _PATH_RESCONF, strerror(errno)); return 0; } } else umask(mask); return 1; } void ipcp_RestoreDNS(struct ipcp *ipcp) { if (ipcp->ns.resolver) { ssize_t got; size_t len; int fd; if ((fd = ID0open(_PATH_RESCONF, O_WRONLY|O_TRUNC, 0644)) != -1) { len = strlen(ipcp->ns.resolv); if ((got = write(fd, ipcp->ns.resolv, len)) != len) { if (got == -1) log_Printf(LogERROR, "Failed rewriting %s: write: %s\n", _PATH_RESCONF, strerror(errno)); else log_Printf(LogERROR, "Failed rewriting %s: wrote %lu of %lu\n", _PATH_RESCONF, (unsigned long)got, (unsigned long)len); } close(fd); } else log_Printf(LogERROR, "Failed rewriting %s: open: %s\n", _PATH_RESCONF, strerror(errno)); } else if (remove(_PATH_RESCONF) == -1) log_Printf(LogERROR, "Failed removing %s: %s\n", _PATH_RESCONF, strerror(errno)); } int ipcp_Show(struct cmdargs const *arg) { struct ipcp *ipcp = &arg->bundle->ncp.ipcp; prompt_Printf(arg->prompt, "%s [%s]\n", ipcp->fsm.name, State2Nam(ipcp->fsm.state)); if (ipcp->fsm.state == ST_OPENED) { prompt_Printf(arg->prompt, " His side: %s, %s\n", inet_ntoa(ipcp->peer_ip), vj2asc(ipcp->peer_compproto)); prompt_Printf(arg->prompt, " My side: %s, %s\n", inet_ntoa(ipcp->my_ip), vj2asc(ipcp->my_compproto)); prompt_Printf(arg->prompt, " Queued packets: %lu\n", (unsigned long)ipcp_QueueLen(ipcp)); } prompt_Printf(arg->prompt, "\nDefaults:\n"); prompt_Printf(arg->prompt, " FSM retry = %us, max %u Config" " REQ%s, %u Term REQ%s\n", ipcp->cfg.fsm.timeout, ipcp->cfg.fsm.maxreq, ipcp->cfg.fsm.maxreq == 1 ? "" : "s", ipcp->cfg.fsm.maxtrm, ipcp->cfg.fsm.maxtrm == 1 ? "" : "s"); prompt_Printf(arg->prompt, " My Address: %s\n", ncprange_ntoa(&ipcp->cfg.my_range)); if (ipcp->cfg.HaveTriggerAddress) prompt_Printf(arg->prompt, " Trigger address: %s\n", inet_ntoa(ipcp->cfg.TriggerAddress)); prompt_Printf(arg->prompt, " VJ compression: %s (%d slots %s slot " "compression)\n", command_ShowNegval(ipcp->cfg.vj.neg), ipcp->cfg.vj.slots, ipcp->cfg.vj.slotcomp ? "with" : "without"); if (iplist_isvalid(&ipcp->cfg.peer_list)) prompt_Printf(arg->prompt, " His Address: %s\n", ipcp->cfg.peer_list.src); else prompt_Printf(arg->prompt, " His Address: %s\n", ncprange_ntoa(&ipcp->cfg.peer_range)); prompt_Printf(arg->prompt, " DNS: %s", ipcp->cfg.ns.dns[0].s_addr == INADDR_NONE ? "none" : inet_ntoa(ipcp->cfg.ns.dns[0])); if (ipcp->cfg.ns.dns[1].s_addr != INADDR_NONE) prompt_Printf(arg->prompt, ", %s", inet_ntoa(ipcp->cfg.ns.dns[1])); prompt_Printf(arg->prompt, ", %s\n", command_ShowNegval(ipcp->cfg.ns.dns_neg)); prompt_Printf(arg->prompt, " Resolver DNS: %s", ipcp->ns.dns[0].s_addr == INADDR_NONE ? "none" : inet_ntoa(ipcp->ns.dns[0])); if (ipcp->ns.dns[1].s_addr != INADDR_NONE && ipcp->ns.dns[1].s_addr != ipcp->ns.dns[0].s_addr) prompt_Printf(arg->prompt, ", %s", inet_ntoa(ipcp->ns.dns[1])); prompt_Printf(arg->prompt, "\n NetBIOS NS: %s, ", inet_ntoa(ipcp->cfg.ns.nbns[0])); prompt_Printf(arg->prompt, "%s\n\n", inet_ntoa(ipcp->cfg.ns.nbns[1])); throughput_disp(&ipcp->throughput, arg->prompt); return 0; } int ipcp_vjset(struct cmdargs const *arg) { if (arg->argc != arg->argn+2) return -1; if (!strcasecmp(arg->argv[arg->argn], "slots")) { int slots; slots = atoi(arg->argv[arg->argn+1]); if (slots < 4 || slots > 16) return 1; arg->bundle->ncp.ipcp.cfg.vj.slots = slots; return 0; } else if (!strcasecmp(arg->argv[arg->argn], "slotcomp")) { if (!strcasecmp(arg->argv[arg->argn+1], "on")) arg->bundle->ncp.ipcp.cfg.vj.slotcomp = 1; else if (!strcasecmp(arg->argv[arg->argn+1], "off")) arg->bundle->ncp.ipcp.cfg.vj.slotcomp = 0; else return 2; return 0; } return -1; } void ipcp_Init(struct ipcp *ipcp, struct bundle *bundle, struct link *l, const struct fsm_parent *parent) { struct hostent *hp; struct in_addr host; char name[MAXHOSTNAMELEN]; static const char * const timer_names[] = {"IPCP restart", "IPCP openmode", "IPCP stopped"}; fsm_Init(&ipcp->fsm, "IPCP", PROTO_IPCP, 1, IPCP_MAXCODE, LogIPCP, bundle, l, parent, &ipcp_Callbacks, timer_names); ipcp->cfg.vj.slots = DEF_VJ_STATES; ipcp->cfg.vj.slotcomp = 1; memset(&ipcp->cfg.my_range, '\0', sizeof ipcp->cfg.my_range); host.s_addr = htonl(INADDR_LOOPBACK); ipcp->cfg.netmask.s_addr = INADDR_ANY; if (gethostname(name, sizeof name) == 0) { hp = gethostbyname(name); if (hp && hp->h_addrtype == AF_INET && hp->h_length == sizeof host.s_addr) memcpy(&host.s_addr, hp->h_addr, sizeof host.s_addr); } ncprange_setip4(&ipcp->cfg.my_range, host, ipcp->cfg.netmask); ncprange_setip4(&ipcp->cfg.peer_range, ipcp->cfg.netmask, ipcp->cfg.netmask); iplist_setsrc(&ipcp->cfg.peer_list, ""); ipcp->cfg.HaveTriggerAddress = 0; ipcp->cfg.ns.dns[0].s_addr = INADDR_NONE; ipcp->cfg.ns.dns[1].s_addr = INADDR_NONE; ipcp->cfg.ns.dns_neg = 0; ipcp->cfg.ns.nbns[0].s_addr = INADDR_ANY; ipcp->cfg.ns.nbns[1].s_addr = INADDR_ANY; ipcp->cfg.fsm.timeout = DEF_FSMRETRY; ipcp->cfg.fsm.maxreq = DEF_FSMTRIES; ipcp->cfg.fsm.maxtrm = DEF_FSMTRIES; ipcp->cfg.vj.neg = NEG_ENABLED|NEG_ACCEPTED; memset(&ipcp->vj, '\0', sizeof ipcp->vj); ipcp->ns.resolv = NULL; ipcp->ns.resolv_nons = NULL; ipcp->ns.writable = 1; ipcp_LoadDNS(ipcp); throughput_init(&ipcp->throughput, SAMPLE_PERIOD); memset(ipcp->Queue, '\0', sizeof ipcp->Queue); ipcp_Setup(ipcp, INADDR_NONE); } void ipcp_Destroy(struct ipcp *ipcp) { throughput_destroy(&ipcp->throughput); if (ipcp->ns.resolv != NULL) { free(ipcp->ns.resolv); ipcp->ns.resolv = NULL; } if (ipcp->ns.resolv_nons != NULL) { free(ipcp->ns.resolv_nons); ipcp->ns.resolv_nons = NULL; } } void ipcp_SetLink(struct ipcp *ipcp, struct link *l) { ipcp->fsm.link = l; } void ipcp_Setup(struct ipcp *ipcp, u_int32_t mask) { struct iface *iface = ipcp->fsm.bundle->iface; struct ncpaddr ipaddr; struct in_addr peer; int pos, n; ipcp->fsm.open_mode = 0; ipcp->ifmask.s_addr = mask == INADDR_NONE ? ipcp->cfg.netmask.s_addr : mask; if (iplist_isvalid(&ipcp->cfg.peer_list)) { /* Try to give the peer a previously configured IP address */ for (n = 0; n < iface->addrs; n++) { if (!ncpaddr_getip4(&iface->addr[n].peer, &peer)) continue; if ((pos = iplist_ip2pos(&ipcp->cfg.peer_list, peer)) != -1) { ncpaddr_setip4(&ipaddr, iplist_setcurpos(&ipcp->cfg.peer_list, pos)); break; } } if (n == iface->addrs) /* Ok, so none of 'em fit.... pick a random one */ ncpaddr_setip4(&ipaddr, iplist_setrandpos(&ipcp->cfg.peer_list)); ncprange_sethost(&ipcp->cfg.peer_range, &ipaddr); } ipcp->heis1172 = 0; ipcp->peer_req = 0; ncprange_getip4addr(&ipcp->cfg.peer_range, &ipcp->peer_ip); ipcp->peer_compproto = 0; if (ipcp->cfg.HaveTriggerAddress) { /* * Some implementations of PPP require that we send a * *special* value as our address, even though the rfc specifies * full negotiation (e.g. "0.0.0.0" or Not "0.0.0.0"). */ ipcp->my_ip = ipcp->cfg.TriggerAddress; log_Printf(LogIPCP, "Using trigger address %s\n", inet_ntoa(ipcp->cfg.TriggerAddress)); } else { /* * Otherwise, if we've used an IP number before and it's still within * the network specified on the ``set ifaddr'' line, we really * want to keep that IP number so that we can keep any existing * connections that are bound to that IP. */ for (n = 0; n < iface->addrs; n++) { ncprange_getaddr(&iface->addr[n].ifa, &ipaddr); if (ncprange_contains(&ipcp->cfg.my_range, &ipaddr)) { ncpaddr_getip4(&ipaddr, &ipcp->my_ip); break; } } if (n == iface->addrs) ncprange_getip4addr(&ipcp->cfg.my_range, &ipcp->my_ip); } if (IsEnabled(ipcp->cfg.vj.neg) #ifndef NORADIUS || (ipcp->fsm.bundle->radius.valid && ipcp->fsm.bundle->radius.vj) #endif ) ipcp->my_compproto = (PROTO_VJCOMP << 16) + ((ipcp->cfg.vj.slots - 1) << 8) + ipcp->cfg.vj.slotcomp; else ipcp->my_compproto = 0; sl_compress_init(&ipcp->vj.cslc, ipcp->cfg.vj.slots - 1); ipcp->peer_reject = 0; ipcp->my_reject = 0; /* Copy startup values into ipcp->ns.dns */ if (ipcp->cfg.ns.dns[0].s_addr != INADDR_NONE) memcpy(ipcp->ns.dns, ipcp->cfg.ns.dns, sizeof ipcp->ns.dns); } static int numaddresses(struct in_addr mask) { u_int32_t bit, haddr; int n; haddr = ntohl(mask.s_addr); bit = 1; n = 1; do { if (!(haddr & bit)) n <<= 1; } while (bit <<= 1); return n; } static int ipcp_proxyarp(struct ipcp *ipcp, int (*proxyfun)(struct bundle *, struct in_addr, int), const struct iface_addr *addr) { struct bundle *bundle = ipcp->fsm.bundle; struct in_addr peer, mask, ip; int n, ret, s; if (!ncpaddr_getip4(&addr->peer, &peer)) { log_Printf(LogERROR, "Oops, ipcp_proxyarp() called with unexpected addr\n"); return 0; } if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) { log_Printf(LogERROR, "ipcp_proxyarp: socket: %s\n", strerror(errno)); return 0; } ret = 0; if (Enabled(bundle, OPT_PROXYALL)) { ncprange_getip4mask(&addr->ifa, &mask); if ((n = numaddresses(mask)) > 256) { log_Printf(LogWARN, "%s: Too many addresses for proxyall\n", ncprange_ntoa(&addr->ifa)); return 0; } ip.s_addr = peer.s_addr & mask.s_addr; if (n >= 4) { ip.s_addr = htonl(ntohl(ip.s_addr) + 1); n -= 2; } while (n) { if (!((ip.s_addr ^ peer.s_addr) & mask.s_addr)) { if (!(ret = (*proxyfun)(bundle, ip, s))) break; n--; } ip.s_addr = htonl(ntohl(ip.s_addr) + 1); } ret = !n; } else if (Enabled(bundle, OPT_PROXY)) ret = (*proxyfun)(bundle, peer, s); close(s); return ret; } static int ipcp_SetIPaddress(struct ipcp *ipcp, struct in_addr myaddr, struct in_addr hisaddr) { struct bundle *bundle = ipcp->fsm.bundle; struct ncpaddr myncpaddr, hisncpaddr; struct ncprange myrange; struct in_addr mask; struct sockaddr_storage ssdst, ssgw, ssmask; struct sockaddr *sadst, *sagw, *samask; sadst = (struct sockaddr *)&ssdst; sagw = (struct sockaddr *)&ssgw; samask = (struct sockaddr *)&ssmask; ncpaddr_setip4(&hisncpaddr, hisaddr); ncpaddr_setip4(&myncpaddr, myaddr); ncprange_sethost(&myrange, &myncpaddr); mask = addr2mask(myaddr); if (ipcp->ifmask.s_addr != INADDR_ANY && (ipcp->ifmask.s_addr & mask.s_addr) == mask.s_addr) ncprange_setip4mask(&myrange, ipcp->ifmask); if (!iface_Add(bundle->iface, &bundle->ncp, &myrange, &hisncpaddr, IFACE_ADD_FIRST|IFACE_FORCE_ADD|IFACE_SYSTEM)) return 0; if (!Enabled(bundle, OPT_IFACEALIAS)) iface_Clear(bundle->iface, &bundle->ncp, AF_INET, IFACE_CLEAR_ALIASES|IFACE_SYSTEM); if (bundle->ncp.cfg.sendpipe > 0 || bundle->ncp.cfg.recvpipe > 0) { ncprange_getsa(&myrange, &ssgw, &ssmask); ncpaddr_getsa(&hisncpaddr, &ssdst); rt_Update(bundle, sadst, sagw, samask); } if (Enabled(bundle, OPT_SROUTES)) route_Change(bundle, bundle->ncp.route, &myncpaddr, &hisncpaddr); #ifndef NORADIUS if (bundle->radius.valid) route_Change(bundle, bundle->radius.routes, &myncpaddr, &hisncpaddr); #endif return 1; /* Ok */ } static struct in_addr ChooseHisAddr(struct bundle *bundle, struct in_addr gw) { struct in_addr try; u_long f; for (f = 0; f < bundle->ncp.ipcp.cfg.peer_list.nItems; f++) { try = iplist_next(&bundle->ncp.ipcp.cfg.peer_list); log_Printf(LogDEBUG, "ChooseHisAddr: Check item %ld (%s)\n", f, inet_ntoa(try)); if (ipcp_SetIPaddress(&bundle->ncp.ipcp, gw, try)) { log_Printf(LogIPCP, "Selected IP address %s\n", inet_ntoa(try)); break; } } if (f == bundle->ncp.ipcp.cfg.peer_list.nItems) { log_Printf(LogDEBUG, "ChooseHisAddr: All addresses in use !\n"); try.s_addr = INADDR_ANY; } return try; } static void IpcpInitRestartCounter(struct fsm *fp, int what) { /* Set fsm timer load */ struct ipcp *ipcp = fsm2ipcp(fp); fp->FsmTimer.load = ipcp->cfg.fsm.timeout * SECTICKS; switch (what) { case FSM_REQ_TIMER: fp->restart = ipcp->cfg.fsm.maxreq; break; case FSM_TRM_TIMER: fp->restart = ipcp->cfg.fsm.maxtrm; break; default: fp->restart = 1; break; } } static void IpcpSendConfigReq(struct fsm *fp) { /* Send config REQ please */ struct physical *p = link2physical(fp->link); struct ipcp *ipcp = fsm2ipcp(fp); u_char buff[24]; struct fsm_opt *o; o = (struct fsm_opt *)buff; if ((p && !physical_IsSync(p)) || !REJECTED(ipcp, TY_IPADDR)) { memcpy(o->data, &ipcp->my_ip.s_addr, 4); INC_FSM_OPT(TY_IPADDR, 6, o); } if (ipcp->my_compproto && !REJECTED(ipcp, TY_COMPPROTO)) { if (ipcp->heis1172) { u_int16_t proto = PROTO_VJCOMP; ua_htons(&proto, o->data); INC_FSM_OPT(TY_COMPPROTO, 4, o); } else { struct compreq req; req.proto = htons(ipcp->my_compproto >> 16); req.slots = (ipcp->my_compproto >> 8) & 255; req.compcid = ipcp->my_compproto & 1; memcpy(o->data, &req, 4); INC_FSM_OPT(TY_COMPPROTO, 6, o); } } if (IsEnabled(ipcp->cfg.ns.dns_neg)) { if (!REJECTED(ipcp, TY_PRIMARY_DNS - TY_ADJUST_NS)) { memcpy(o->data, &ipcp->ns.dns[0].s_addr, 4); INC_FSM_OPT(TY_PRIMARY_DNS, 6, o); } if (!REJECTED(ipcp, TY_SECONDARY_DNS - TY_ADJUST_NS)) { memcpy(o->data, &ipcp->ns.dns[1].s_addr, 4); INC_FSM_OPT(TY_SECONDARY_DNS, 6, o); } } fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, (u_char *)o - buff, MB_IPCPOUT); } static void IpcpSentTerminateReq(struct fsm *fp) { /* Term REQ just sent by FSM */ } static void IpcpSendTerminateAck(struct fsm *fp, u_char id) { /* Send Term ACK please */ fsm_Output(fp, CODE_TERMACK, id, NULL, 0, MB_IPCPOUT); } static void IpcpLayerStart(struct fsm *fp) { /* We're about to start up ! */ struct ipcp *ipcp = fsm2ipcp(fp); log_Printf(LogIPCP, "%s: LayerStart.\n", fp->link->name); throughput_start(&ipcp->throughput, "IPCP throughput", Enabled(fp->bundle, OPT_THROUGHPUT)); fp->more.reqs = fp->more.naks = fp->more.rejs = ipcp->cfg.fsm.maxreq * 3; ipcp->peer_req = 0; } static void IpcpLayerFinish(struct fsm *fp) { /* We're now down */ struct ipcp *ipcp = fsm2ipcp(fp); log_Printf(LogIPCP, "%s: LayerFinish.\n", fp->link->name); throughput_stop(&ipcp->throughput); throughput_log(&ipcp->throughput, LogIPCP, NULL); } /* * Called from iface_Add() via ncp_IfaceAddrAdded() */ void ipcp_IfaceAddrAdded(struct ipcp *ipcp, const struct iface_addr *addr) { struct bundle *bundle = ipcp->fsm.bundle; if (Enabled(bundle, OPT_PROXY) || Enabled(bundle, OPT_PROXYALL)) ipcp_proxyarp(ipcp, arp_SetProxy, addr); } /* * Called from iface_Clear() and iface_Delete() via ncp_IfaceAddrDeleted() */ void ipcp_IfaceAddrDeleted(struct ipcp *ipcp, const struct iface_addr *addr) { struct bundle *bundle = ipcp->fsm.bundle; if (Enabled(bundle, OPT_PROXY) || Enabled(bundle, OPT_PROXYALL)) ipcp_proxyarp(ipcp, arp_ClearProxy, addr); } static void IpcpLayerDown(struct fsm *fp) { /* About to come down */ struct ipcp *ipcp = fsm2ipcp(fp); static int recursing; char addr[16]; if (!recursing++) { snprintf(addr, sizeof addr, "%s", inet_ntoa(ipcp->my_ip)); log_Printf(LogIPCP, "%s: LayerDown: %s\n", fp->link->name, addr); #ifndef NORADIUS radius_Account(&fp->bundle->radius, &fp->bundle->radacct, - fp->bundle->links, RAD_STOP, &ipcp->peer_ip, &ipcp->ifmask, - &ipcp->throughput); + fp->bundle->links, RAD_STOP, &ipcp->throughput); if (fp->bundle->radius.cfg.file && fp->bundle->radius.filterid) system_Select(fp->bundle, fp->bundle->radius.filterid, LINKDOWNFILE, NULL, NULL); #endif /* * 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)) { if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle), LINKDOWNFILE, NULL, NULL) < 0) system_Select(fp->bundle, "MYADDR", LINKDOWNFILE, NULL, NULL); } else system_Select(fp->bundle, "MYADDR", LINKDOWNFILE, NULL, NULL); } ipcp_Setup(ipcp, INADDR_NONE); } recursing--; } int ipcp_InterfaceUp(struct ipcp *ipcp) { if (!ipcp_SetIPaddress(ipcp, ipcp->my_ip, ipcp->peer_ip)) { log_Printf(LogERROR, "ipcp_InterfaceUp: unable to set ip address\n"); return 0; } if (!iface_SetFlags(ipcp->fsm.bundle->iface->name, IFF_UP)) { log_Printf(LogERROR, "ipcp_InterfaceUp: Can't set the IFF_UP flag on %s\n", ipcp->fsm.bundle->iface->name); return 0; } #ifndef NONAT if (ipcp->fsm.bundle->NatEnabled) PacketAliasSetAddress(ipcp->my_ip); #endif return 1; } static int IpcpLayerUp(struct fsm *fp) { /* We're now up */ struct ipcp *ipcp = fsm2ipcp(fp); char tbuff[16]; log_Printf(LogIPCP, "%s: LayerUp.\n", fp->link->name); snprintf(tbuff, sizeof tbuff, "%s", inet_ntoa(ipcp->my_ip)); log_Printf(LogIPCP, "myaddr %s hisaddr = %s\n", tbuff, inet_ntoa(ipcp->peer_ip)); if (ipcp->peer_compproto >> 16 == PROTO_VJCOMP) sl_compress_init(&ipcp->vj.cslc, (ipcp->peer_compproto >> 8) & 255); if (!ipcp_InterfaceUp(ipcp)) return 0; #ifndef NORADIUS + radius_Account_Set_Ip(&fp->bundle->radacct, &ipcp->peer_ip, &ipcp->ifmask); radius_Account(&fp->bundle->radius, &fp->bundle->radacct, fp->bundle->links, - RAD_START, &ipcp->peer_ip, &ipcp->ifmask, &ipcp->throughput); + RAD_START, &ipcp->throughput); if (fp->bundle->radius.cfg.file && fp->bundle->radius.filterid) system_Select(fp->bundle, fp->bundle->radius.filterid, LINKUPFILE, NULL, NULL); #endif /* * 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)) { if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle), LINKUPFILE, NULL, NULL) < 0) system_Select(fp->bundle, "MYADDR", LINKUPFILE, NULL, NULL); } else system_Select(fp->bundle, "MYADDR", LINKUPFILE, NULL, NULL); } fp->more.reqs = fp->more.naks = fp->more.rejs = ipcp->cfg.fsm.maxreq * 3; log_DisplayPrompts(); return 1; } static void ipcp_ValidateReq(struct ipcp *ipcp, struct in_addr ip, struct fsm_decode *dec) { struct bundle *bundle = ipcp->fsm.bundle; struct iface *iface = bundle->iface; struct in_addr myaddr, peer; int n; if (iplist_isvalid(&ipcp->cfg.peer_list)) { ncprange_getip4addr(&ipcp->cfg.my_range, &myaddr); if (ip.s_addr == INADDR_ANY || iplist_ip2pos(&ipcp->cfg.peer_list, ip) < 0 || !ipcp_SetIPaddress(ipcp, myaddr, ip)) { log_Printf(LogIPCP, "%s: Address invalid or already in use\n", inet_ntoa(ip)); /* * If we've already had a valid address configured for the peer, * try NAKing with that so that we don't have to upset things * too much. */ for (n = 0; n < iface->addrs; n++) { if (!ncpaddr_getip4(&iface->addr[n].peer, &peer)) continue; if (iplist_ip2pos(&ipcp->cfg.peer_list, peer) >= 0) { ipcp->peer_ip = peer; break; } } if (n == iface->addrs) { /* Just pick an IP number from our list */ ipcp->peer_ip = ChooseHisAddr(bundle, myaddr); } if (ipcp->peer_ip.s_addr == INADDR_ANY) { *dec->rejend++ = TY_IPADDR; *dec->rejend++ = 6; memcpy(dec->rejend, &ip.s_addr, 4); dec->rejend += 4; } else { *dec->nakend++ = TY_IPADDR; *dec->nakend++ = 6; memcpy(dec->nakend, &ipcp->peer_ip.s_addr, 4); dec->nakend += 4; } return; } } else if (ip.s_addr == INADDR_ANY || !ncprange_containsip4(&ipcp->cfg.peer_range, ip)) { /* * If the destination address is not acceptable, NAK with what we * want to use. */ *dec->nakend++ = TY_IPADDR; *dec->nakend++ = 6; for (n = 0; n < iface->addrs; n++) if (ncprange_contains(&ipcp->cfg.peer_range, &iface->addr[n].peer)) { /* We prefer the already-configured address */ ncpaddr_getip4addr(&iface->addr[n].peer, (u_int32_t *)dec->nakend); break; } if (n == iface->addrs) memcpy(dec->nakend, &ipcp->peer_ip.s_addr, 4); dec->nakend += 4; return; } ipcp->peer_ip = ip; *dec->ackend++ = TY_IPADDR; *dec->ackend++ = 6; memcpy(dec->ackend, &ip.s_addr, 4); dec->ackend += 4; } static void IpcpDecodeConfig(struct fsm *fp, u_char *cp, u_char *end, int mode_type, struct fsm_decode *dec) { /* Deal with incoming PROTO_IPCP */ struct ncpaddr ncpaddr; struct ipcp *ipcp = fsm2ipcp(fp); int gotdnsnak; u_int32_t compproto; struct compreq *pcomp; struct in_addr ipaddr, dstipaddr, have_ip; char tbuff[100], tbuff2[100]; struct fsm_opt *opt, nak; gotdnsnak = 0; 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_IPADDR: /* RFC1332 */ memcpy(&ipaddr.s_addr, opt->data, 4); log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr)); switch (mode_type) { case MODE_REQ: ipcp->peer_req = 1; ipcp_ValidateReq(ipcp, ipaddr, dec); break; case MODE_NAK: if (ncprange_containsip4(&ipcp->cfg.my_range, ipaddr)) { /* Use address suggested by peer */ snprintf(tbuff2, sizeof tbuff2, "%s changing address: %s ", tbuff, inet_ntoa(ipcp->my_ip)); log_Printf(LogIPCP, "%s --> %s\n", tbuff2, inet_ntoa(ipaddr)); ipcp->my_ip = ipaddr; ncpaddr_setip4(&ncpaddr, ipcp->my_ip); bundle_AdjustFilters(fp->bundle, &ncpaddr, NULL); } else { log_Printf(log_IsKept(LogIPCP) ? LogIPCP : LogPHASE, "%s: Unacceptable address!\n", inet_ntoa(ipaddr)); fsm_Close(&ipcp->fsm); } break; case MODE_REJ: ipcp->peer_reject |= (1 << opt->hdr.id); break; } break; case TY_COMPPROTO: pcomp = (struct compreq *)opt->data; compproto = (ntohs(pcomp->proto) << 16) + (pcomp->slots << 8) + pcomp->compcid; log_Printf(LogIPCP, "%s %s\n", tbuff, vj2asc(compproto)); switch (mode_type) { case MODE_REQ: if (!IsAccepted(ipcp->cfg.vj.neg)) fsm_rej(dec, opt); else { switch (opt->hdr.len) { case 4: /* RFC1172 */ if (ntohs(pcomp->proto) == PROTO_VJCOMP) { log_Printf(LogWARN, "Peer is speaking RFC1172 compression " "protocol !\n"); ipcp->heis1172 = 1; ipcp->peer_compproto = compproto; fsm_ack(dec, opt); } else { pcomp->proto = htons(PROTO_VJCOMP); nak.hdr.id = TY_COMPPROTO; nak.hdr.len = 4; memcpy(nak.data, &pcomp, 2); fsm_nak(dec, &nak); } break; case 6: /* RFC1332 */ if (ntohs(pcomp->proto) == PROTO_VJCOMP) { if (pcomp->slots <= MAX_VJ_STATES && pcomp->slots >= MIN_VJ_STATES) { /* Ok, we can do that */ ipcp->peer_compproto = compproto; ipcp->heis1172 = 0; fsm_ack(dec, opt); } else { /* Get as close as we can to what he wants */ ipcp->heis1172 = 0; pcomp->slots = pcomp->slots < MIN_VJ_STATES ? MIN_VJ_STATES : MAX_VJ_STATES; nak.hdr.id = TY_COMPPROTO; nak.hdr.len = 4; memcpy(nak.data, &pcomp, 2); fsm_nak(dec, &nak); } } else { /* What we really want */ pcomp->proto = htons(PROTO_VJCOMP); pcomp->slots = DEF_VJ_STATES; pcomp->compcid = 1; nak.hdr.id = TY_COMPPROTO; nak.hdr.len = 6; memcpy(nak.data, &pcomp, sizeof pcomp); fsm_nak(dec, &nak); } break; default: fsm_rej(dec, opt); break; } } break; case MODE_NAK: if (ntohs(pcomp->proto) == PROTO_VJCOMP) { if (pcomp->slots > MAX_VJ_STATES) pcomp->slots = MAX_VJ_STATES; else if (pcomp->slots < MIN_VJ_STATES) pcomp->slots = MIN_VJ_STATES; compproto = (ntohs(pcomp->proto) << 16) + (pcomp->slots << 8) + pcomp->compcid; } else compproto = 0; log_Printf(LogIPCP, "%s changing compproto: %08x --> %08x\n", tbuff, ipcp->my_compproto, compproto); ipcp->my_compproto = compproto; break; case MODE_REJ: ipcp->peer_reject |= (1 << opt->hdr.id); break; } break; case TY_IPADDRS: /* RFC1172 */ memcpy(&ipaddr.s_addr, opt->data, 4); memcpy(&dstipaddr.s_addr, opt->data + 4, 4); snprintf(tbuff2, sizeof tbuff2, "%s %s,", tbuff, inet_ntoa(ipaddr)); log_Printf(LogIPCP, "%s %s\n", tbuff2, inet_ntoa(dstipaddr)); switch (mode_type) { case MODE_REQ: fsm_rej(dec, opt); break; case MODE_NAK: case MODE_REJ: break; } break; case TY_PRIMARY_DNS: /* DNS negotiation (rfc1877) */ case TY_SECONDARY_DNS: memcpy(&ipaddr.s_addr, opt->data, 4); log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr)); switch (mode_type) { case MODE_REQ: if (!IsAccepted(ipcp->cfg.ns.dns_neg)) { ipcp->my_reject |= (1 << (opt->hdr.id - TY_ADJUST_NS)); fsm_rej(dec, opt); break; } have_ip = ipcp->ns.dns[opt->hdr.id == TY_PRIMARY_DNS ? 0 : 1]; if (opt->hdr.id == TY_PRIMARY_DNS && ipaddr.s_addr != have_ip.s_addr && ipaddr.s_addr == ipcp->ns.dns[1].s_addr) { /* Swap 'em 'round */ ipcp->ns.dns[0] = ipcp->ns.dns[1]; ipcp->ns.dns[1] = have_ip; have_ip = ipcp->ns.dns[0]; } if (ipaddr.s_addr != have_ip.s_addr) { /* * The client has got the DNS stuff wrong (first request) so * we'll tell 'em how it is */ nak.hdr.id = opt->hdr.id; nak.hdr.len = 6; memcpy(nak.data, &have_ip.s_addr, 4); fsm_nak(dec, &nak); } else { /* * Otherwise they have it right (this time) so we send a ack packet * back confirming it... end of story */ fsm_ack(dec, opt); } break; case MODE_NAK: if (IsEnabled(ipcp->cfg.ns.dns_neg)) { gotdnsnak = 1; memcpy(&ipcp->ns.dns[opt->hdr.id == TY_PRIMARY_DNS ? 0 : 1].s_addr, opt->data, 4); } break; case MODE_REJ: /* Can't do much, stop asking */ ipcp->peer_reject |= (1 << (opt->hdr.id - TY_ADJUST_NS)); break; } break; case TY_PRIMARY_NBNS: /* M$ NetBIOS nameserver hack (rfc1877) */ case TY_SECONDARY_NBNS: memcpy(&ipaddr.s_addr, opt->data, 4); log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr)); switch (mode_type) { case MODE_REQ: have_ip.s_addr = ipcp->cfg.ns.nbns[opt->hdr.id == TY_PRIMARY_NBNS ? 0 : 1].s_addr; if (have_ip.s_addr == INADDR_ANY) { log_Printf(LogIPCP, "NBNS REQ - rejected - nbns not set\n"); ipcp->my_reject |= (1 << (opt->hdr.id - TY_ADJUST_NS)); fsm_rej(dec, opt); break; } if (ipaddr.s_addr != have_ip.s_addr) { nak.hdr.id = opt->hdr.id; nak.hdr.len = 6; memcpy(nak.data, &have_ip.s_addr, 4); fsm_nak(dec, &nak); } else fsm_ack(dec, opt); break; case MODE_NAK: log_Printf(LogIPCP, "MS NBNS req %d - NAK??\n", opt->hdr.id); break; case MODE_REJ: log_Printf(LogIPCP, "MS NBNS req %d - REJ??\n", opt->hdr.id); break; } break; default: if (mode_type != MODE_NOP) { ipcp->my_reject |= (1 << opt->hdr.id); fsm_rej(dec, opt); } break; } } if (gotdnsnak) { if (ipcp->ns.writable) { log_Printf(LogDEBUG, "Updating resolver\n"); if (!ipcp_WriteDNS(ipcp)) { ipcp->peer_reject |= (1 << (TY_PRIMARY_DNS - TY_ADJUST_NS)); ipcp->peer_reject |= (1 << (TY_SECONDARY_DNS - TY_ADJUST_NS)); } else bundle_AdjustDNS(fp->bundle); } else { log_Printf(LogDEBUG, "Not updating resolver (readonly)\n"); bundle_AdjustDNS(fp->bundle); } } if (mode_type != MODE_NOP) { if (mode_type == MODE_REQ && !ipcp->peer_req) { if (dec->rejend == dec->rej && dec->nakend == dec->nak) { /* * Pretend the peer has requested an IP. * 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_IPADDR REQ. This stops us from repeatedly trying to tell * the peer that we have to have an IP address on their end. */ ipcp->peer_req = 1; } ipaddr.s_addr = INADDR_ANY; ipcp_ValidateReq(ipcp, ipaddr, dec); } fsm_opt_normalise(dec); } } extern struct mbuf * ipcp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) { /* Got PROTO_IPCP from link */ m_settype(bp, MB_IPCPIN); if (bundle_Phase(bundle) == PHASE_NETWORK) fsm_Input(&bundle->ncp.ipcp.fsm, bp); else { if (bundle_Phase(bundle) < PHASE_NETWORK) log_Printf(LogIPCP, "%s: Error: Unexpected IPCP in phase %s (ignored)\n", l->name, bundle_PhaseName(bundle)); m_freem(bp); } return NULL; } int ipcp_UseHisIPaddr(struct bundle *bundle, struct in_addr hisaddr) { struct ipcp *ipcp = &bundle->ncp.ipcp; struct in_addr myaddr; memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range); iplist_reset(&ipcp->cfg.peer_list); ipcp->peer_ip = hisaddr; ncprange_setip4host(&ipcp->cfg.peer_range, hisaddr); ncprange_getip4addr(&ipcp->cfg.my_range, &myaddr); return ipcp_SetIPaddress(ipcp, myaddr, hisaddr); } int ipcp_UseHisaddr(struct bundle *bundle, const char *hisaddr, int setaddr) { struct in_addr myaddr; struct ncp *ncp = &bundle->ncp; struct ipcp *ipcp = &ncp->ipcp; struct ncpaddr ncpaddr; /* Use `hisaddr' for the peers address (set iface if `setaddr') */ memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range); iplist_reset(&ipcp->cfg.peer_list); if (strpbrk(hisaddr, ",-")) { iplist_setsrc(&ipcp->cfg.peer_list, hisaddr); if (iplist_isvalid(&ipcp->cfg.peer_list)) { iplist_setrandpos(&ipcp->cfg.peer_list); ipcp->peer_ip = ChooseHisAddr(bundle, ipcp->my_ip); if (ipcp->peer_ip.s_addr == INADDR_ANY) { log_Printf(LogWARN, "%s: None available !\n", ipcp->cfg.peer_list.src); return 0; } ncprange_setip4host(&ipcp->cfg.peer_range, ipcp->peer_ip); } else { log_Printf(LogWARN, "%s: Invalid range !\n", hisaddr); return 0; } } else if (ncprange_aton(&ipcp->cfg.peer_range, ncp, hisaddr) != 0) { if (ncprange_family(&ipcp->cfg.my_range) != AF_INET) { log_Printf(LogWARN, "%s: Not an AF_INET address !\n", hisaddr); return 0; } ncprange_getip4addr(&ipcp->cfg.my_range, &myaddr); ncprange_getip4addr(&ipcp->cfg.peer_range, &ipcp->peer_ip); if (setaddr && !ipcp_SetIPaddress(ipcp, myaddr, ipcp->peer_ip)) return 0; } else return 0; ncpaddr_setip4(&ncpaddr, ipcp->peer_ip); bundle_AdjustFilters(bundle, NULL, &ncpaddr); return 1; /* Ok */ } struct in_addr addr2mask(struct in_addr addr) { u_int32_t haddr = ntohl(addr.s_addr); haddr = IN_CLASSA(haddr) ? IN_CLASSA_NET : IN_CLASSB(haddr) ? IN_CLASSB_NET : IN_CLASSC_NET; addr.s_addr = htonl(haddr); return addr; } size_t ipcp_QueueLen(struct ipcp *ipcp) { struct mqueue *q; size_t result; result = 0; for (q = ipcp->Queue; q < ipcp->Queue + IPCP_QUEUES(ipcp); q++) result += q->len; return result; } int ipcp_PushPacket(struct ipcp *ipcp, struct link *l) { struct bundle *bundle = ipcp->fsm.bundle; struct mqueue *queue; struct mbuf *bp; int m_len; u_int32_t secs = 0; unsigned alivesecs = 0; if (ipcp->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 = ipcp->Queue + IPCP_QUEUES(ipcp) - 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_INET, &bundle->filter.alive, &alivesecs)) { if (secs == 0) secs = alivesecs; bundle_StartIdleTimer(bundle, secs); } link_PushPacket(l, bp, bundle, 0, PROTO_IP); ipcp_AddOutOctets(ipcp, m_len); return 1; } } while (queue-- != ipcp->Queue); return 0; } Index: stable/4/usr.sbin/ppp/ipv6cp.c =================================================================== --- stable/4/usr.sbin/ppp/ipv6cp.c (revision 116983) +++ stable/4/usr.sbin/ppp/ipv6cp.c (revision 116984) @@ -1,742 +1,781 @@ /*- * 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.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() */ +#ifndef NORADIUS + radius_Account_Set_Ipv6(&fp->bundle->radacct6, ipv6cp->his_ifid); + radius_Account(&fp->bundle->radius, &fp->bundle->radacct6, + fp->bundle->links, RAD_START, &ipv6cp->throughput); /* + * XXX: Avoid duplicate evaluation of filterid between IPCP and + * IPV6CP. When IPCP is enabled and rejected, filterid is not + * evaluated. + */ + if (!Enabled(fp->bundle, OPT_IPCP)) { + if (fp->bundle->radius.cfg.file && fp->bundle->radius.filterid) + system_Select(fp->bundle, fp->bundle->radius.filterid, LINKUPFILE, + NULL, NULL); + } +#endif + + /* * 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) { + /* + * XXX: Avoid duplicate evaluation of label between IPCP and + * IPV6CP. When IPCP is enabled and rejected, label is not + * evaluated. + */ 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() */ +#ifndef NORADIUS + radius_Account(&fp->bundle->radius, &fp->bundle->radacct6, + fp->bundle->links, RAD_STOP, &ipv6cp->throughput); /* + * XXX: Avoid duplicate evaluation of filterid between IPCP and + * IPV6CP. When IPCP is enabled and rejected, filterid is not + * evaluated. + */ + if (!Enabled(fp->bundle, OPT_IPCP)) { + if (fp->bundle->radius.cfg.file && fp->bundle->radius.filterid) + system_Select(fp->bundle, fp->bundle->radius.filterid, LINKDOWNFILE, + NULL, NULL); + } +#endif + + /* * 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) { + /* + * XXX: Avoid duplicate evaluation of label between IPCP and + * IPV6CP. When IPCP is enabled and rejected, label is not + * evaluated. + */ 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 116983) +++ stable/4/usr.sbin/ppp/radius.c (revision 116984) @@ -1,1123 +1,1159 @@ /* * 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; } +/* Fetch IP, netmask from IPCP */ +void +radius_Account_Set_Ip(struct radacct *ac, struct in_addr *peer_ip, + struct in_addr *netmask) +{ + ac->proto = PROTO_IPCP; + memcpy(&ac->ip.addr, peer_ip, sizeof(ac->ip.addr)); + memcpy(&ac->ip.mask, netmask, sizeof(ac->ip.mask)); +} + +#ifndef NOINET6 +/* Fetch interface-id from IPV6CP */ +void +radius_Account_Set_Ipv6(struct radacct *ac, u_char *ifid) +{ + ac->proto = PROTO_IPV6CP; + memcpy(&ac->ipv6.ifid, ifid, sizeof(ac->ipv6.ifid)); +} +#endif + /* * 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) + int acct_type, 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) { + 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; + } + switch (ac->proto) { + case PROTO_IPCP: + if (rad_put_addr(r->cx.rad, RAD_FRAMED_IP_ADDRESS, ac->ip.addr) != 0 || + rad_put_addr(r->cx.rad, RAD_FRAMED_IP_NETMASK, ac->ip.mask) != 0) { + log_Printf(LogERROR, "rad_put: %s\n", rad_strerror(r->cx.rad)); + rad_close(r->cx.rad); + return; + } + break; +#ifndef NOINET6 + case PROTO_IPV6CP: + if (rad_put_attr(r->cx.rad, RAD_FRAMED_INTERFACE_ID, ac->ipv6.ifid, + sizeof(ac->ipv6.ifid)) != 0) { + log_Printf(LogERROR, "rad_put_attr: %s\n", rad_strerror(r->cx.rad)); + rad_close(r->cx.rad); + return; + } + break; +#endif + default: + /* We don't log any protocol specific information */ + break; } 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 116983) +++ stable/4/usr.sbin/ppp/radius.h (revision 116984) @@ -1,103 +1,117 @@ /* * 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; + u_short proto; /* Protocol number */ + union { + struct { + struct in_addr addr; + struct in_addr mask; + } ip; +#ifndef NOINET6 + struct { + u_char ifid[8]; + } ipv6; +#endif + }; }; #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_Set_Ip(struct radacct *, struct in_addr *, + struct in_addr *); +#ifndef NOINET6 +extern void radius_Account_Set_Ipv6(struct radacct *, u_char *); +#endif extern void radius_Account(struct radius *, struct radacct *, - struct datalink *, int, struct in_addr *, - struct in_addr *, struct pppThroughput *); + struct datalink *, int, 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 */