spppcontrol.c revision 88551
1/* 2 * Copyright (c) 1997, 2001 Joerg Wunsch 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#ifndef lint 28static const char rcsid[] = 29 "$FreeBSD: head/sbin/spppcontrol/spppcontrol.c 88551 2001-12-27 21:20:33Z joerg $"; 30#endif /* not lint */ 31 32#include <sys/param.h> 33#include <sys/callout.h> 34#include <sys/ioctl.h> 35#include <sys/mbuf.h> 36#include <sys/socket.h> 37 38#include <net/if.h> 39#include <net/if_var.h> 40#include <netinet/in.h> 41#include <netinet/in_systm.h> 42#include <netinet/ip.h> 43#include <net/slcompress.h> 44#include <net/if_sppp.h> 45 46#include <err.h> 47#include <stdio.h> 48#include <stdlib.h> 49#include <string.h> 50#include <sysexits.h> 51#include <unistd.h> 52 53static void usage(void); 54void print_vals(const char *ifname, struct spppreq *sp); 55const char *phase_name(enum ppp_phase phase); 56const char *proto_name(u_short proto); 57const char *authflags(u_short flags); 58 59#define PPP_PAP 0xc023 60#define PPP_CHAP 0xc223 61 62int 63main(int argc, char **argv) 64{ 65 int s, c; 66 int errs = 0, verbose = 0; 67 size_t off; 68 long to; 69 char *endp; 70 const char *ifname, *cp; 71 struct ifreq ifr; 72 struct spppreq spr; 73 74 while ((c = getopt(argc, argv, "v")) != -1) 75 switch (c) { 76 case 'v': 77 verbose++; 78 break; 79 80 default: 81 errs++; 82 break; 83 } 84 argv += optind; 85 argc -= optind; 86 87 if (errs || argc < 1) 88 usage(); 89 90 ifname = argv[0]; 91 strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name); 92 93 /* use a random AF to create the socket */ 94 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 95 err(EX_UNAVAILABLE, "ifconfig: socket"); 96 97 argc--; 98 argv++; 99 100 spr.cmd = (int)SPPPIOGDEFS; 101 ifr.ifr_data = (caddr_t)&spr; 102 103 if (ioctl(s, SIOCGIFGENERIC, &ifr) == -1) 104 err(EX_OSERR, "SIOCGIFGENERIC(SPPPIOGDEFS)"); 105 106 if (argc == 0) { 107 /* list only mode */ 108 print_vals(ifname, &spr); 109 return 0; 110 } 111 112#define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0 113 114 while (argc > 0) { 115 if (startswith("authproto=")) { 116 cp = argv[0] + off; 117 if (strcmp(cp, "pap") == 0) 118 spr.defs.myauth.proto = 119 spr.defs.hisauth.proto = PPP_PAP; 120 else if (strcmp(cp, "chap") == 0) 121 spr.defs.myauth.proto = 122 spr.defs.hisauth.proto = PPP_CHAP; 123 else if (strcmp(cp, "none") == 0) 124 spr.defs.myauth.proto = 125 spr.defs.hisauth.proto = 0; 126 else 127 errx(EX_DATAERR, "bad auth proto: %s", cp); 128 } else if (startswith("myauthproto=")) { 129 cp = argv[0] + off; 130 if (strcmp(cp, "pap") == 0) 131 spr.defs.myauth.proto = PPP_PAP; 132 else if (strcmp(cp, "chap") == 0) 133 spr.defs.myauth.proto = PPP_CHAP; 134 else if (strcmp(cp, "none") == 0) 135 spr.defs.myauth.proto = 0; 136 else 137 errx(EX_DATAERR, "bad auth proto: %s", cp); 138 } else if (startswith("myauthname=")) 139 strncpy(spr.defs.myauth.name, argv[0] + off, 140 AUTHNAMELEN); 141 else if (startswith("myauthsecret=") || 142 startswith("myauthkey=")) 143 strncpy(spr.defs.myauth.secret, argv[0] + off, 144 AUTHKEYLEN); 145 else if (startswith("hisauthproto=")) { 146 cp = argv[0] + off; 147 if (strcmp(cp, "pap") == 0) 148 spr.defs.hisauth.proto = PPP_PAP; 149 else if (strcmp(cp, "chap") == 0) 150 spr.defs.hisauth.proto = PPP_CHAP; 151 else if (strcmp(cp, "none") == 0) 152 spr.defs.hisauth.proto = 0; 153 else 154 errx(EX_DATAERR, "bad auth proto: %s", cp); 155 } else if (startswith("hisauthname=")) 156 strncpy(spr.defs.hisauth.name, argv[0] + off, 157 AUTHNAMELEN); 158 else if (startswith("hisauthsecret=") || 159 startswith("hisauthkey=")) 160 strncpy(spr.defs.hisauth.secret, argv[0] + off, 161 AUTHKEYLEN); 162 else if (strcmp(argv[0], "callin") == 0) 163 spr.defs.hisauth.flags |= AUTHFLAG_NOCALLOUT; 164 else if (strcmp(argv[0], "always") == 0) 165 spr.defs.hisauth.flags &= ~AUTHFLAG_NOCALLOUT; 166 else if (strcmp(argv[0], "norechallenge") == 0) 167 spr.defs.hisauth.flags |= AUTHFLAG_NORECHALLENGE; 168 else if (strcmp(argv[0], "rechallenge") == 0) 169 spr.defs.hisauth.flags &= ~AUTHFLAG_NORECHALLENGE; 170 else if (startswith("lcp-timeout=")) { 171 cp = argv[0] + off; 172 to = strtol(cp, &endp, 10); 173 if (*cp == '\0' || *endp != '\0' || 174 /* 175 * NB: 10 ms is the minimal possible value for 176 * hz=100. We assume no kernel has less clock 177 * frequency than that... 178 */ 179 to < 10 || to > 20000) 180 errx(EX_DATAERR, "bad lcp timeout value: %s", 181 cp); 182 spr.defs.lcp.timeout = to; 183 } else if (strcmp(argv[0], "enable-vj") == 0) 184 spr.defs.enable_vj = 1; 185 else if (strcmp(argv[0], "disable-vj") == 0) 186 spr.defs.enable_vj = 0; 187 else 188 errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]); 189 190 argv++; 191 argc--; 192 } 193 194 spr.cmd = (int)SPPPIOSDEFS; 195 196 if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1) 197 err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)"); 198 199 if (verbose) 200 print_vals(ifname, &spr); 201 202 return 0; 203} 204 205static void 206usage(void) 207{ 208 fprintf(stderr, "%s\n%s\n", 209 "usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]", 210 " spppcontrol [-v] ifname callin|always"); 211 exit(EX_USAGE); 212} 213 214void 215print_vals(const char *ifname, struct spppreq *sp) 216{ 217 printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase)); 218 if (sp->defs.myauth.proto) { 219 printf("\tmyauthproto=%s myauthname=\"%.*s\"\n", 220 proto_name(sp->defs.myauth.proto), 221 AUTHNAMELEN, sp->defs.myauth.name); 222 } 223 if (sp->defs.hisauth.proto) { 224 printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n", 225 proto_name(sp->defs.hisauth.proto), 226 AUTHNAMELEN, sp->defs.hisauth.name, 227 authflags(sp->defs.hisauth.flags)); 228 } 229 printf("\tlcp-timeout=%d ms\n", sp->defs.lcp.timeout); 230 printf("\t%sable-vj\n", sp->defs.enable_vj? "en": "dis"); 231} 232 233const char * 234phase_name(enum ppp_phase phase) 235{ 236 switch (phase) { 237 case PHASE_DEAD: return "dead"; 238 case PHASE_ESTABLISH: return "establish"; 239 case PHASE_TERMINATE: return "terminate"; 240 case PHASE_AUTHENTICATE: return "authenticate"; 241 case PHASE_NETWORK: return "network"; 242 } 243 return "illegal"; 244} 245 246const char * 247proto_name(u_short proto) 248{ 249 static char buf[12]; 250 switch (proto) { 251 case PPP_PAP: return "pap"; 252 case PPP_CHAP: return "chap"; 253 } 254 sprintf(buf, "0x%x", (unsigned)proto); 255 return buf; 256} 257 258const char * 259authflags(u_short flags) 260{ 261 static char buf[30]; 262 buf[0] = '\0'; 263 if (flags & AUTHFLAG_NOCALLOUT) 264 strcat(buf, " callin"); 265 if (flags & AUTHFLAG_NORECHALLENGE) 266 strcat(buf, " norechallenge"); 267 return buf; 268} 269