Home | History | Annotate | Line # | Download | only in pppoectl
pppoectl.c revision 1.4
      1 /* $NetBSD: pppoectl.c,v 1.4 2002/01/07 11:10:26 martin Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1997 Joerg Wunsch
      5  *
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  * From: spppcontrol.c,v 1.3 1998/01/07 07:55:26 charnier Exp
     29  * From: ispppcontrol
     30  */
     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 #include <sys/time.h>
     38 #include <sys/sysctl.h>
     39 #include <net/if.h>
     40 #include <net/if_sppp.h>
     41 #include <net/if_pppoe.h>
     42 #include <err.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <sysexits.h>
     47 #include <unistd.h>
     48 
     49 static void usage(void);
     50 static void print_error(const char *ifname, int error, const char * str);
     51 static void print_vals(const char *ifname, int phase, struct spppauthcfg *sp,
     52 	int lcp_timeout, time_t idle_timeout, int authfailures,
     53 	int max_auth_failures);
     54 const char *phase_name(int phase);
     55 const char *proto_name(int proto);
     56 const char *authflags(int flags);
     57 
     58 int hz = 0;
     59 
     60 int
     61 main(int argc, char **argv)
     62 {
     63 	int s, c;
     64 	int errs = 0, verbose = 0, dump = 0;
     65 	size_t off, len;
     66 	const char *ifname, *cp;
     67 	const char *eth_if_name, *access_concentrator, *service;
     68 	struct spppauthcfg spr;
     69 	struct sppplcpcfg lcp;
     70 	struct spppstatus status;
     71 	struct spppidletimeout timeout;
     72 	struct spppauthfailurestats authfailstats;
     73 	struct spppauthfailuresettings authfailset;
     74 	int mib[2];
     75 	int set_auth = 0, set_lcp = 0, set_idle_to = 0, set_auth_failure = 0;
     76 	struct clockinfo clockinfo;
     77 
     78 	eth_if_name = NULL;
     79 	access_concentrator = NULL;
     80 	service = NULL;
     81 	while ((c = getopt(argc, argv, "vde:s:a:")) != -1)
     82 		switch (c) {
     83 		case 'v':
     84 			verbose++;
     85 			break;
     86 
     87 		case 'd':
     88 			dump++;
     89 			break;
     90 
     91 		case 'e':
     92 			eth_if_name = optarg;
     93 			break;
     94 
     95 		case 's':
     96 			service = optarg;
     97 			break;
     98 
     99 		case 'a':
    100 			access_concentrator = optarg;
    101 			break;
    102 
    103 		default:
    104 			errs++;
    105 			break;
    106 		}
    107 	argv += optind;
    108 	argc -= optind;
    109 
    110 	if (errs || argc < 1)
    111 		usage();
    112 
    113 	ifname = argv[0];
    114 
    115 	/* use a random AF to create the socket */
    116 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    117 		err(EX_UNAVAILABLE, "ifconfig: socket");
    118 
    119 	argc--;
    120 	argv++;
    121 
    122 	if (eth_if_name) {
    123 		struct pppoediscparms parms;
    124 		int e;
    125 
    126 		memset(&parms, 0, sizeof parms);
    127 		strncpy(parms.ifname, ifname, sizeof(parms.ifname));
    128 		strncpy(parms.eth_ifname, eth_if_name, sizeof(parms.eth_ifname));
    129 		if (access_concentrator) {
    130 			parms.ac_name = (char*)access_concentrator;
    131 			parms.ac_name_len = strlen(access_concentrator);
    132 		}
    133 		if (service) {
    134 			parms.service_name = (char*)service;
    135 			parms.service_name_len = strlen(service);
    136 		}
    137 
    138 		e = ioctl(s, PPPOESETPARMS, &parms);
    139 		if (e)
    140 			print_error(ifname, e, "PPPOESETPARMS");
    141 		return 0;
    142 	}
    143 
    144 	if (dump) {
    145 		/* dump PPPoE session state */
    146 		struct pppoeconnectionstate state;
    147 		int e;
    148 
    149 		memset(&state, 0, sizeof state);
    150 		strncpy(state.ifname, ifname, sizeof state.ifname);
    151 		e = ioctl(s, PPPOEGETSESSION, &state);
    152 		if (e)
    153 			print_error(ifname, e, "PPPOEGETSESSION,");
    154 
    155 		printf("%s:\tstate = ", ifname);
    156 		switch(state.state) {
    157 		case PPPOE_STATE_INITIAL:
    158 			printf("initial\n"); break;
    159 		case PPPOE_STATE_PADI_SENT:
    160 			printf("PADI sent\n"); break;
    161 		case PPPOE_STATE_PADR_SENT:
    162 			printf("PADR sent\n"); break;
    163 		case PPPOE_STATE_SESSION:
    164 			printf("session\n"); break;
    165 		case PPPOE_STATE_CLOSING:
    166 			printf("closing\n"); break;
    167 		}
    168 		printf("\tSession ID: 0x%x\n", state.session_id);
    169 		printf("\tPADI retries: %d\n", state.padi_retry_no);
    170 		printf("\tPADR retries: %d\n", state.padr_retry_no);
    171 
    172 		return 0;
    173 	}
    174 
    175 
    176 	memset(&spr, 0, sizeof spr);
    177 	strncpy(spr.ifname, ifname, sizeof spr.ifname);
    178 	memset(&lcp, 0, sizeof lcp);
    179 	strncpy(lcp.ifname, ifname, sizeof lcp.ifname);
    180 	memset(&status, 0, sizeof status);
    181 	strncpy(status.ifname, ifname, sizeof status.ifname);
    182 	memset(&timeout, 0, sizeof timeout);
    183 	strncpy(timeout.ifname, ifname, sizeof timeout.ifname);
    184 	memset(&authfailstats, 0, sizeof &authfailstats);
    185 	strncpy(authfailstats.ifname, ifname, sizeof authfailstats.ifname);
    186 	memset(&authfailset, 0, sizeof authfailset);
    187 	strncpy(authfailset.ifname, ifname, sizeof authfailset.ifname);
    188 
    189 	mib[0] = CTL_KERN;
    190 	mib[1] = KERN_CLOCKRATE;
    191 	len = sizeof(clockinfo);
    192 	if(sysctl(mib, 2, &clockinfo, &len, NULL, 0) == -1)
    193 	{
    194 		fprintf(stderr, "error, cannot sysctl kern.clockrate!\n");
    195 		exit(1);
    196 	}
    197 
    198 	hz = clockinfo.hz;
    199 
    200 	if (argc == 0) {
    201 		/* list only mode */
    202 
    203 		/* first pass, get name lenghts */
    204 		if (ioctl(s, SPPPGETAUTHCFG, &spr) == -1)
    205 			err(EX_OSERR, "SPPPGETAUTHCFG");
    206 		/* now allocate buffers for strings */
    207 		if (spr.myname_length)
    208 			spr.myname = malloc(spr.myname_length);
    209 		if (spr.hisname_length)
    210 			spr.hisname = malloc(spr.hisname_length);
    211 		/* second pass: get names too */
    212 		if (ioctl(s, SPPPGETAUTHCFG, &spr) == -1)
    213 			err(EX_OSERR, "SPPPGETAUTHCFG");
    214 
    215 		if (ioctl(s, SPPPGETLCPCFG, &lcp) == -1)
    216 			err(EX_OSERR, "SPPPGETLCPCFG");
    217 		if (ioctl(s, SPPPGETSTATUS, &status) == -1)
    218 			err(EX_OSERR, "SPPPGETSTATUS");
    219 		if (ioctl(s, SPPPGETIDLETO, &timeout) == -1)
    220 			err(EX_OSERR, "SPPPGETIDLETO");
    221 		if (ioctl(s, SPPPGETAUTHFAILURES, &authfailstats) == -1)
    222 			err(EX_OSERR, "SPPPGETAUTHFAILURES");
    223 
    224 		print_vals(ifname, status.phase, &spr, lcp.lcp_timeout, timeout.idle_seconds, authfailstats.auth_failures, authfailstats.max_failures);
    225 
    226 		if (spr.hisname) free(spr.hisname);
    227 		if (spr.myname) free(spr.myname);
    228 		return 0;
    229 	}
    230 
    231 #define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0
    232 
    233 	while (argc > 0) {
    234 		if (startswith("authproto=")) {
    235 			cp = argv[0] + off;
    236 			if (strcmp(cp, "pap") == 0)
    237 				spr.myauth =
    238 					spr.hisauth = SPPP_AUTHPROTO_PAP;
    239 			else if (strcmp(cp, "chap") == 0)
    240 				spr.myauth = spr.hisauth = SPPP_AUTHPROTO_CHAP;
    241 			else if (strcmp(cp, "none") == 0)
    242 				spr.myauth = spr.hisauth = SPPP_AUTHPROTO_NONE;
    243 			else
    244 				errx(EX_DATAERR, "bad auth proto: %s", cp);
    245 			set_auth = 1;
    246 		} else if (startswith("myauthproto=")) {
    247 			cp = argv[0] + off;
    248 			if (strcmp(cp, "pap") == 0)
    249 				spr.myauth = SPPP_AUTHPROTO_PAP;
    250 			else if (strcmp(cp, "chap") == 0)
    251 				spr.myauth = SPPP_AUTHPROTO_CHAP;
    252 			else if (strcmp(cp, "none") == 0)
    253 				spr.myauth = SPPP_AUTHPROTO_NONE;
    254 			else
    255 				errx(EX_DATAERR, "bad auth proto: %s", cp);
    256 			set_auth = 1;
    257 		} else if (startswith("myauthname=")) {
    258 			spr.myname = argv[0] + off;
    259 			spr.myname_length = strlen(spr.myname)+1;
    260 			set_auth = 1;
    261 		} else if (startswith("myauthsecret=") || startswith("myauthkey=")) {
    262 			spr.mysecret = argv[0] + off;
    263 			spr.mysecret_length = strlen(spr.mysecret)+1;
    264 			set_auth = 1;
    265 		} else if (startswith("hisauthproto=")) {
    266 			cp = argv[0] + off;
    267 			if (strcmp(cp, "pap") == 0)
    268 				spr.hisauth = SPPP_AUTHPROTO_PAP;
    269 			else if (strcmp(cp, "chap") == 0)
    270 				spr.hisauth = SPPP_AUTHPROTO_CHAP;
    271 			else if (strcmp(cp, "none") == 0)
    272 				spr.hisauth = SPPP_AUTHPROTO_NONE;
    273 			else
    274 				errx(EX_DATAERR, "bad auth proto: %s", cp);
    275 			set_auth = 1;
    276 		} else if (startswith("hisauthname=")) {
    277 			spr.hisname = argv[0] + off;
    278 			spr.hisname_length = strlen(spr.hisname)+1;
    279 			set_auth = 1;
    280 		} else if (startswith("hisauthsecret=") || startswith("hisauthkey=")) {
    281 			spr.hissecret = argv[0] + off;
    282 			spr.hissecret_length = strlen(spr.hissecret)+1;
    283 			set_auth = 1;
    284 		} else if (strcmp(argv[0], "callin") == 0)
    285 			spr.hisauthflags |= SPPP_AUTHFLAG_NOCALLOUT;
    286 		else if (strcmp(argv[0], "always") == 0)
    287 			spr.hisauthflags &= ~SPPP_AUTHFLAG_NOCALLOUT;
    288 		else if (strcmp(argv[0], "norechallenge") == 0)
    289 			spr.hisauthflags |= SPPP_AUTHFLAG_NORECHALLENGE;
    290 		else if (strcmp(argv[0], "rechallenge") == 0)
    291 			spr.hisauthflags &= ~SPPP_AUTHFLAG_NORECHALLENGE;
    292 #ifndef __NetBSD__
    293 		else if (strcmp(argv[0], "enable-vj") == 0)
    294 			spr.defs.enable_vj = 1;
    295 		else if (strcmp(argv[0], "disable-vj") == 0)
    296 			spr.defs.enable_vj = 0;
    297 #endif
    298 		else if (startswith("lcp-timeout=")) {
    299 			int timeout_arg = atoi(argv[0]+off);
    300 			if ((timeout_arg > 20000) || (timeout_arg <= 0))
    301 				errx(EX_DATAERR, "bad lcp timeout value: %s",
    302 				     argv[0]+off);
    303 			lcp.lcp_timeout = timeout_arg * hz / 1000;
    304 			set_lcp = 1;
    305 		} else if (startswith("idle-timeout=")) {
    306 			timeout.idle_seconds = (time_t)atol(argv[0]+off);
    307 			set_idle_to = 1;
    308 		} else if (startswith("max-auth-failure=")) {
    309 			authfailset.max_failures = atoi(argv[0]+off);
    310 			set_auth_failure = 1;
    311 		} else
    312 			errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
    313 
    314 		argv++;
    315 		argc--;
    316 	}
    317 
    318 	if (set_auth) {
    319 		if (ioctl(s, SPPPSETAUTHCFG, &spr) == -1)
    320 			err(EX_OSERR, "SPPPSETAUTHCFG");
    321 	}
    322 	if (set_lcp) {
    323 		if (ioctl(s, SPPPSETLCPCFG, &lcp) == -1)
    324 			err(EX_OSERR, "SPPPSETLCPCFG");
    325 	}
    326 	if (set_idle_to) {
    327 		if (ioctl(s, SPPPSETIDLETO, &timeout) == -1)
    328 			err(EX_OSERR, "SPPPSETIDLETO");
    329 	}
    330 	if (set_auth_failure) {
    331 		if (ioctl(s, SPPPSETAUTHFAILURE, &authfailset) == -1)
    332 			err(EX_OSERR, "SPPPSETAUTHFAILURE");
    333 	}
    334 
    335 	if (verbose) {
    336 		if (ioctl(s, SPPPGETAUTHFAILURES, &authfailstats) == -1)
    337 			err(EX_OSERR, "SPPPGETAUTHFAILURES");
    338 		print_vals(ifname, status.phase, &spr, lcp.lcp_timeout, timeout.idle_seconds, authfailstats.auth_failures, authfailstats.max_failures);
    339 	}
    340 
    341 	return 0;
    342 }
    343 
    344 static void
    345 usage(void)
    346 {
    347 	fprintf(stderr, "%s\n%s\n",
    348 	"usage: ispppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
    349 	"       ispppcontrol [-v] ifname callin|always");
    350 	exit(EX_USAGE);
    351 }
    352 
    353 static void
    354 print_vals(const char *ifname, int phase, struct spppauthcfg *sp, int lcp_timeout,
    355 	time_t idle_timeout, int authfailures, int max_auth_failures)
    356 {
    357 #ifndef __NetBSD__
    358 	time_t send, recv;
    359 #endif
    360 
    361 	printf("%s:\tphase=%s\n", ifname, phase_name(phase));
    362 	if (sp->myauth) {
    363 		printf("\tmyauthproto=%s myauthname=\"%s\"\n",
    364 		       proto_name(sp->myauth),
    365 		       sp->myname);
    366 	}
    367 	if (sp->hisauth) {
    368 		printf("\thisauthproto=%s hisauthname=\"%s\"%s\n",
    369 		       proto_name(sp->hisauth),
    370 		       sp->hisname,
    371 		       authflags(sp->hisauthflags));
    372 	}
    373 #ifndef __NetBSD__
    374 	if (sp->defs.pp_phase > PHASE_DEAD) {
    375 		send = time(NULL) - sp->defs.pp_last_sent;
    376 		recv = time(NULL) - sp->defs.pp_last_recv;
    377 		printf("\tidle_time=%ld\n", (send<recv)? send : recv);
    378 	}
    379 #endif
    380 
    381 	printf("\tlcp timeout: %.3f s\n",
    382 	       (double)lcp_timeout / hz);
    383 
    384 	if (idle_timeout != 0)
    385 		printf("\tidle timeout = %lu s\n", (unsigned long)idle_timeout);
    386 	else
    387 		printf("\tidle timeout = disabled\n");
    388 
    389 	if (authfailures != 0)
    390 		printf("\tauthentication failures = %d\n", authfailures);
    391 	printf("\tmax-auth-failure = %d\n", max_auth_failures);
    392 
    393 #ifndef __NetBSD__
    394 	printf("\tenable_vj: %s\n",
    395 	       sp->defs.enable_vj ? "on" : "off");
    396 #endif
    397 }
    398 
    399 const char *
    400 phase_name(int phase)
    401 {
    402 	switch (phase) {
    403 	case SPPP_PHASE_DEAD:		return "dead";
    404 	case SPPP_PHASE_ESTABLISH:	return "establish";
    405 	case SPPP_PHASE_TERMINATE:	return "terminate";
    406 	case SPPP_PHASE_AUTHENTICATE:	return "authenticate";
    407 	case SPPP_PHASE_NETWORK:	return "network";
    408 	}
    409 	return "illegal";
    410 }
    411 
    412 const char *
    413 proto_name(int proto)
    414 {
    415 	static char buf[12];
    416 	switch (proto) {
    417 	case SPPP_AUTHPROTO_PAP:	return "pap";
    418 	case SPPP_AUTHPROTO_CHAP:	return "chap";
    419 	case SPPP_AUTHPROTO_NONE:	return "none";
    420 	}
    421 	sprintf(buf, "0x%x", (unsigned)proto);
    422 	return buf;
    423 }
    424 
    425 const char *
    426 authflags(int flags)
    427 {
    428 	static char buf[32];
    429 	buf[0] = '\0';
    430 	if (flags & SPPP_AUTHFLAG_NOCALLOUT)
    431 		strcat(buf, " callin");
    432 	if (flags & SPPP_AUTHFLAG_NORECHALLENGE)
    433 		strcat(buf, " norechallenge");
    434 	return buf;
    435 }
    436 
    437 static void
    438 print_error(const char *ifname, int error, const char * str)
    439 {
    440 	if (error == -1)
    441 		fprintf(stderr, "%s: interface not found\n", ifname);
    442 	else
    443 		fprintf(stderr, "%s: %s: %s\n", ifname, str, strerror(error));
    444 	exit(EX_DATAERR);
    445 }
    446 
    447 
    448