Home | History | Annotate | Line # | Download | only in pppoectl
pppoectl.c revision 1.29
      1 /*	$NetBSD: pppoectl.c,v 1.29 2021/05/11 06:58:03 yamaguchi 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 #include <sys/cdefs.h>
     32 
     33 #ifndef lint
     34 __RCSID("$NetBSD: pppoectl.c,v 1.29 2021/05/11 06:58:03 yamaguchi Exp $");
     35 #endif
     36 
     37 
     38 #include <sys/param.h>
     39 #include <sys/callout.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/socket.h>
     42 #include <sys/time.h>
     43 #include <sys/sysctl.h>
     44 #include <net/if.h>
     45 #include <net/if_sppp.h>
     46 #include <net/if_pppoe.h>
     47 #include <arpa/inet.h>
     48 #include <err.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <sysexits.h>
     53 #include <unistd.h>
     54 
     55 __dead static void usage(void);
     56 __dead static void print_error(const char *ifname, int error, const char * str);
     57 static void print_vals(const char *ifname, int phase, struct spppauthcfg *sp,
     58 	int lcp_timeout, time_t idle_timeout, int authfailures,
     59 	int max_auth_failures, u_int maxalive, time_t max_noreceive,
     60 	int ncp_flags);
     61 static void print_dns(const char *ifname, int dns1, int dns2, int s, int tabs);
     62 static void print_stats(const char *ifname, int s, int dump);
     63 static const char *phase_name(int phase);
     64 static const char *proto_name(int proto);
     65 static const char *authflags(int flags);
     66 static const char *pppoe_state_name(int state);
     67 static const char *ppp_state_name(int state);
     68 static void pppoectl_argument(char *arg);
     69 
     70 #define	ISSET(x, a)	((x) & (a))
     71 #define PPPOECTL_IOCTL(_ifname, _s, _cmd, _st)	do {	\
     72 	int __e;					\
     73 	memset((_st), 0, sizeof(*(_st)));		\
     74 	strncpy((_st)->ifname, (_ifname),		\
     75 	    sizeof((_st)->ifname));			\
     76 	__e = ioctl((_s), (_cmd), (_st));		\
     77 	if (__e != 0)					\
     78 		print_error((_ifname), __e, #_cmd);	\
     79 } while (0)
     80 
     81 static int hz = 0;
     82 
     83 static int set_auth, set_lcp, set_idle_to, set_auth_failure, set_dns,
     84     clear_auth_failure_count, set_keepalive;
     85 static u_int set_ncpflags, clr_ncpflags;
     86 static int maxalive = -1;
     87 static int max_noreceive = -1;
     88 static struct spppauthcfg spr;
     89 static struct sppplcpcfg lcp;
     90 static struct spppncpcfg ncp;
     91 static struct spppstatus status;
     92 static struct spppidletimeout timeout;
     93 static struct spppauthfailurestats authfailstats;
     94 static struct spppauthfailuresettings authfailset;
     95 static struct spppdnssettings dnssettings;
     96 static struct spppkeepalivesettings keepalivesettings;
     97 
     98 int
     99 main(int argc, char **argv)
    100 {
    101 	FILE *fp;
    102 	int s, c;
    103 	int errs = 0, verbose = 0, dump = 0, dns1 = 0, dns2 = 0;
    104 	size_t len;
    105 	const char *eth_if_name, *access_concentrator, *service;
    106 	const char *ifname, *configname;
    107 	char *line;
    108 	int mib[2];
    109 	struct clockinfo clockinfo;
    110 	setprogname(argv[0]);
    111 
    112 	eth_if_name = NULL;
    113 	access_concentrator = NULL;
    114 	service = NULL;
    115 	configname = NULL;
    116 	while ((c = getopt(argc, argv, "vde:f:s:a:n:")) != -1)
    117 		switch (c) {
    118 		case 'v':
    119 			verbose++;
    120 			break;
    121 
    122 		case 'd':
    123 			dump++;
    124 			break;
    125 
    126 		case 'e':
    127 			eth_if_name = optarg;
    128 			break;
    129 
    130 		case 'f':
    131 			configname = optarg;
    132 			break;
    133 
    134 		case 's':
    135 			service = optarg;
    136 			break;
    137 
    138 		case 'a':
    139 			access_concentrator = optarg;
    140 			break;
    141 
    142 		case 'n':
    143 			if (strcmp(optarg, "1") == 0)
    144 				dns1 = 1;
    145 			else if (strcmp(optarg, "2") == 0)
    146 				dns2 = 1;
    147 			else {
    148 				fprintf(stderr, "bad argument \"%s\" to -n (only 1 or two allowed)\n",
    149 					optarg);
    150 				errs++;
    151 			}
    152 			break;
    153 
    154 		default:
    155 			errs++;
    156 			break;
    157 		}
    158 	argv += optind;
    159 	argc -= optind;
    160 
    161 	if (errs || argc < 1)
    162 		usage();
    163 
    164 	ifname = argv[0];
    165 
    166 	/* use a random AF to create the socket */
    167 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    168 		err(EX_UNAVAILABLE, "ifconfig: socket");
    169 
    170 	argc--;
    171 	argv++;
    172 
    173 	if (eth_if_name) {
    174 		struct pppoediscparms parms;
    175 		int e;
    176 
    177 		memset(&parms, 0, sizeof parms);
    178 		strncpy(parms.ifname, ifname, sizeof(parms.ifname));
    179 		strncpy(parms.eth_ifname, eth_if_name, sizeof(parms.eth_ifname));
    180 		if (access_concentrator) {
    181 			parms.ac_name = access_concentrator;
    182 			parms.ac_name_len = strlen(access_concentrator);
    183 		}
    184 		if (service) {
    185 			parms.service_name = service;
    186 			parms.service_name_len = strlen(service);
    187 		}
    188 
    189 		e = ioctl(s, PPPOESETPARMS, &parms);
    190 		if (e)
    191 			print_error(ifname, e, "PPPOESETPARMS");
    192 		return 0;
    193 	}
    194 
    195 	if (dns1 || dns2) {
    196 		print_dns(ifname, dns1, dns2, s, 0);
    197 	}
    198 
    199 	if (dump) {
    200 		print_stats(ifname, s, dump);
    201 		return 0;
    202 	}
    203 
    204 	memset(&spr, 0, sizeof spr);
    205 	strncpy(spr.ifname, ifname, sizeof spr.ifname);
    206 	spr.myauth = SPPP_AUTHPROTO_NOCHG;
    207 	spr.hisauth = SPPP_AUTHPROTO_NOCHG;
    208 	memset(&lcp, 0, sizeof lcp);
    209 	strncpy(lcp.ifname, ifname, sizeof lcp.ifname);
    210 	memset(&ncp, 0, sizeof ncp);
    211 	strncpy(ncp.ifname, ifname, sizeof ncp.ifname);
    212 	memset(&status, 0, sizeof status);
    213 	strncpy(status.ifname, ifname, sizeof status.ifname);
    214 	memset(&timeout, 0, sizeof timeout);
    215 	strncpy(timeout.ifname, ifname, sizeof timeout.ifname);
    216 	memset(&authfailstats, 0, sizeof authfailstats);
    217 	strncpy(authfailstats.ifname, ifname, sizeof authfailstats.ifname);
    218 	memset(&authfailset, 0, sizeof authfailset);
    219 	strncpy(authfailset.ifname, ifname, sizeof authfailset.ifname);
    220 	memset(&dnssettings, 0, sizeof dnssettings);
    221 	strncpy(dnssettings.ifname, ifname, sizeof dnssettings.ifname);
    222 	memset(&keepalivesettings, 0, sizeof keepalivesettings);
    223 	strncpy(keepalivesettings.ifname, ifname, sizeof keepalivesettings.ifname);
    224 
    225 	mib[0] = CTL_KERN;
    226 	mib[1] = KERN_CLOCKRATE;
    227 	len = sizeof(clockinfo);
    228 	if(sysctl(mib, 2, &clockinfo, &len, NULL, 0) == -1)
    229 	{
    230 		fprintf(stderr, "error, cannot sysctl kern.clockrate!\n");
    231 		exit(1);
    232 	}
    233 
    234 	hz = clockinfo.hz;
    235 
    236 	if (argc == 0 && !(dns1||dns2) && !configname) {
    237 		/* list only mode */
    238 
    239 		/* first pass, get name lengths */
    240 		if (ioctl(s, SPPPGETAUTHCFG, &spr) == -1)
    241 			err(EX_OSERR, "SPPPGETAUTHCFG");
    242 		/* now allocate buffers for strings */
    243 		if (spr.myname_length)
    244 			if ((spr.myname = malloc(spr.myname_length)) == NULL)
    245 				err(1, NULL);
    246 		if (spr.hisname_length)
    247 			if ((spr.hisname = malloc(spr.hisname_length)) == NULL)
    248 				err(1, NULL);
    249 		/* second pass: get names too */
    250 		if (ioctl(s, SPPPGETAUTHCFG, &spr) == -1)
    251 			err(EX_OSERR, "SPPPGETAUTHCFG");
    252 
    253 		if (ioctl(s, SPPPGETLCPCFG, &lcp) == -1)
    254 			err(EX_OSERR, "SPPPGETLCPCFG");
    255 		if (ioctl(s, SPPPGETNCPCFG, &ncp) == -1)
    256 			err(EX_OSERR, "SPPPGETNCPCFG");
    257 		if (ioctl(s, SPPPGETSTATUS, &status) == -1)
    258 			err(EX_OSERR, "SPPPGETSTATUS");
    259 		if (ioctl(s, SPPPGETIDLETO, &timeout) == -1)
    260 			err(EX_OSERR, "SPPPGETIDLETO");
    261 		if (ioctl(s, SPPPGETAUTHFAILURES, &authfailstats) == -1)
    262 			err(EX_OSERR, "SPPPGETAUTHFAILURES");
    263 		if (ioctl(s, SPPPGETKEEPALIVE, &keepalivesettings) == -1)
    264 			err(EX_OSERR, "SPPPGETKEEPALIVE");
    265 
    266 		print_vals(ifname, status.phase, &spr, lcp.lcp_timeout,
    267 		    timeout.idle_seconds, authfailstats.auth_failures,
    268 		    authfailstats.max_failures,
    269 		    keepalivesettings.maxalive,
    270 		    keepalivesettings.max_noreceive,
    271 		    ncp.ncp_flags);
    272 
    273 		if (spr.hisname) free(spr.hisname);
    274 		if (spr.myname) free(spr.myname);
    275 		return 0;
    276 	}
    277 
    278 	/* first load the config file, then parse command line args */
    279 	if (configname && (fp = fopen(configname, "r")))
    280 		while ((line = fparseln(fp, NULL, NULL, NULL,
    281 		    FPARSELN_UNESCALL)) != NULL) {
    282 			if (line[0] != '\0')
    283 				pppoectl_argument(line);
    284 			/*
    285 			 * We do not free(line) here, because we
    286 			 * still have references to parts of the
    287 			 * string collected in the various ioctl
    288 			 * argument structures (and need those).
    289 			 * Yes, this is a memory leak.
    290 			 * We could copy the partial strings instead,
    291 			 * and free those later - but this is a one-shot
    292 			 * program and memory will be freed at process
    293 			 * exit time anyway.
    294 			 */
    295 		}
    296 
    297 
    298 	while (argc > 0) {
    299 		pppoectl_argument(argv[0]);
    300 
    301 		argv++;
    302 		argc--;
    303 	}
    304 
    305 	if (set_auth) {
    306 		if (ioctl(s, SPPPSETAUTHCFG, &spr) == -1)
    307 			err(EX_OSERR, "SPPPSETAUTHCFG");
    308 	}
    309 	if (set_lcp) {
    310 		if (ioctl(s, SPPPSETLCPCFG, &lcp) == -1)
    311 			err(EX_OSERR, "SPPPSETLCPCFG");
    312 	}
    313 	if (set_ncpflags != 0 || clr_ncpflags != 0) {
    314 		if (ioctl(s, SPPPGETNCPCFG, &ncp) == -1)
    315 			err(EX_OSERR, "SPPPGETNCPCFG");
    316 
    317 		ncp.ncp_flags |= set_ncpflags;
    318 		ncp.ncp_flags &= ~clr_ncpflags;
    319 
    320 		if (ioctl(s, SPPPSETNCPCFG, &ncp) == -1)
    321 			err(EX_OSERR, "SPPPSETNCPCFG");
    322 	}
    323 	if (set_idle_to) {
    324 		if (ioctl(s, SPPPSETIDLETO, &timeout) == -1)
    325 			err(EX_OSERR, "SPPPSETIDLETO");
    326 	}
    327 	if (set_auth_failure) {
    328 		if (ioctl(s, SPPPSETAUTHFAILURE, &authfailset) == -1)
    329 			err(EX_OSERR, "SPPPSETAUTHFAILURE");
    330 	}
    331 	if (clear_auth_failure_count && !(set_auth || set_auth_failure)) {
    332 		/*
    333 		 * We want to clear the auth failure count, but did not
    334 		 * do that implicitly by setting authentication - so
    335 		 * do a zero-effect auth setting change
    336 		 */
    337 		if (ioctl(s, SPPPGETAUTHFAILURES, &authfailstats) == -1)
    338 			err(EX_OSERR, "SPPPGETAUTHFAILURES");
    339 		authfailset.max_failures = authfailstats.max_failures;
    340 		if (ioctl(s, SPPPSETAUTHFAILURE, &authfailset) == -1)
    341 			err(EX_OSERR, "SPPPSETAUTHFAILURE");
    342 	}
    343 	if (set_dns) {
    344 		if (ioctl(s, SPPPSETDNSOPTS, &dnssettings) == -1)
    345 			err(EX_OSERR, "SPPPSETDNSOPTS");
    346 	}
    347 	if (set_keepalive) {
    348 		if (ioctl(s, SPPPGETKEEPALIVE, &keepalivesettings) == -1)
    349 			err(EX_OSERR, "SPPPGETKEEPALIVE");
    350 		if (max_noreceive >= 0)
    351 			keepalivesettings.max_noreceive = max_noreceive;
    352 		if (maxalive >= 0)
    353 			keepalivesettings.maxalive = maxalive;
    354 		if (ioctl(s, SPPPSETKEEPALIVE, &keepalivesettings) == -1)
    355 			err(EX_OSERR, "SPPPSETKEEPALIVE");
    356 	}
    357 
    358 	if (verbose) {
    359 		if (ioctl(s, SPPPGETAUTHFAILURES, &authfailstats) == -1)
    360 			err(EX_OSERR, "SPPPGETAUTHFAILURES");
    361 		if (ioctl(s, SPPPGETKEEPALIVE, &keepalivesettings) == -1)
    362 			err(EX_OSERR, "SPPPGETKEEPALIVE");
    363 		print_vals(ifname, status.phase, &spr, lcp.lcp_timeout,
    364 		    timeout.idle_seconds, authfailstats.auth_failures,
    365 		    authfailstats.max_failures,
    366 		    keepalivesettings.maxalive,
    367 		    keepalivesettings.max_noreceive,
    368 		    ncp.ncp_flags);
    369 	}
    370 
    371 	return 0;
    372 }
    373 
    374 static void
    375 pppoectl_argument(char *arg)
    376 {
    377 	size_t off;
    378 	const char *cp;
    379 
    380 #define startswith(a,s) strncmp(a, s, (off = strlen(s))) == 0
    381 	if (startswith(arg, "authproto=")) {
    382 		cp = arg + off;
    383 		if (strcmp(cp, "pap") == 0)
    384 			spr.myauth =
    385 				spr.hisauth = SPPP_AUTHPROTO_PAP;
    386 		else if (strcmp(cp, "chap") == 0)
    387 			spr.myauth = spr.hisauth = SPPP_AUTHPROTO_CHAP;
    388 		else if (strcmp(cp, "none") == 0)
    389 			spr.myauth = spr.hisauth = SPPP_AUTHPROTO_NONE;
    390 		else
    391 			errx(EX_DATAERR, "bad auth proto: %s", cp);
    392 		set_auth = 1;
    393 	} else if (startswith(arg, "myauthproto=")) {
    394 		cp = arg + off;
    395 		if (strcmp(cp, "pap") == 0)
    396 			spr.myauth = SPPP_AUTHPROTO_PAP;
    397 		else if (strcmp(cp, "chap") == 0)
    398 			spr.myauth = SPPP_AUTHPROTO_CHAP;
    399 		else if (strcmp(cp, "none") == 0)
    400 			spr.myauth = SPPP_AUTHPROTO_NONE;
    401 		else
    402 			errx(EX_DATAERR, "bad auth proto: %s", cp);
    403 		set_auth = 1;
    404 	} else if (startswith(arg, "myauthname=")) {
    405 		spr.myname = arg + off;
    406 		spr.myname_length = strlen(spr.myname)+1;
    407 		set_auth = 1;
    408 	} else if (startswith(arg, "myauthsecret=") || startswith(arg, "myauthkey=")) {
    409 		spr.mysecret = arg + off;
    410 		spr.mysecret_length = strlen(spr.mysecret)+1;
    411 		set_auth = 1;
    412 	} else if (startswith(arg, "hisauthproto=")) {
    413 		cp = arg + off;
    414 		if (strcmp(cp, "pap") == 0)
    415 			spr.hisauth = SPPP_AUTHPROTO_PAP;
    416 		else if (strcmp(cp, "chap") == 0)
    417 			spr.hisauth = SPPP_AUTHPROTO_CHAP;
    418 		else if (strcmp(cp, "none") == 0)
    419 			spr.hisauth = SPPP_AUTHPROTO_NONE;
    420 		else
    421 			errx(EX_DATAERR, "bad auth proto: %s", cp);
    422 		set_auth = 1;
    423 	} else if (startswith(arg, "hisauthname=")) {
    424 		spr.hisname = arg + off;
    425 		spr.hisname_length = strlen(spr.hisname)+1;
    426 		set_auth = 1;
    427 	} else if (startswith(arg, "hisauthsecret=") || startswith(arg, "hisauthkey=")) {
    428 		spr.hissecret = arg + off;
    429 		spr.hissecret_length = strlen(spr.hissecret)+1;
    430 		set_auth = 1;
    431 	} else if (startswith(arg, "max-noreceive=")) {
    432 		max_noreceive = atoi(arg+off);
    433 		if (max_noreceive < 0) {
    434 			fprintf(stderr,
    435 			    "max-noreceive value must be at least 0\n");
    436 			max_noreceive = -1;
    437 		} else {
    438 			set_keepalive = 1;
    439 		}
    440 	} else if (startswith(arg, "max-alive-missed=")) {
    441 		maxalive = atoi(arg+off);
    442 		if (maxalive < 0) {
    443 			fprintf(stderr,
    444 			    "max-alive-missed value must be at least 0\n");
    445 			maxalive = -1;
    446 		} else {
    447 			set_keepalive = 1;
    448 		}
    449 	} else if (strcmp(arg, "callin") == 0)
    450 		spr.hisauthflags |= SPPP_AUTHFLAG_NOCALLOUT;
    451 	else if (strcmp(arg, "always") == 0)
    452 		spr.hisauthflags &= ~SPPP_AUTHFLAG_NOCALLOUT;
    453 	else if (strcmp(arg, "norechallenge") == 0)
    454 		spr.hisauthflags |= SPPP_AUTHFLAG_NORECHALLENGE;
    455 	else if (strcmp(arg, "rechallenge") == 0)
    456 		spr.hisauthflags &= ~SPPP_AUTHFLAG_NORECHALLENGE;
    457 	else if (strcmp(arg, "passiveauthproto") == 0)
    458 		spr.myauthflags |= SPPP_AUTHFLAG_PASSIVEAUTHPROTO;
    459 #ifndef __NetBSD__
    460 	else if (strcmp(arg, "enable-vj") == 0)
    461 		spr.defs.enable_vj = 1;
    462 	else if (strcmp(arg, "disable-vj") == 0)
    463 		spr.defs.enable_vj = 0;
    464 #endif
    465 	else if (startswith(arg, "lcp-timeout=")) {
    466 		int timeout_arg = atoi(arg+off);
    467 		if ((timeout_arg > 20000) || (timeout_arg <= 0))
    468 			errx(EX_DATAERR, "bad lcp timeout value: %s",
    469 			     arg+off);
    470 		lcp.lcp_timeout = timeout_arg * hz / 1000;
    471 		set_lcp = 1;
    472 	} else if (startswith(arg, "idle-timeout=")) {
    473 		timeout.idle_seconds = (time_t)atol(arg+off);
    474 		set_idle_to = 1;
    475 	} else if (startswith(arg, "max-auth-failure=")) {
    476 		authfailset.max_failures = atoi(arg+off);
    477 		set_auth_failure = 1;
    478 	} else if (strcmp(arg, "clear-auth-failure") == 0) {
    479 		clear_auth_failure_count = 1;
    480 	} else if (startswith(arg, "query-dns=")) {
    481 		dnssettings.query_dns = atoi(arg+off);
    482 		set_dns = 1;
    483 	} else if (strcmp(arg, "ipcp") == 0) {
    484 		set_ncpflags |= SPPP_NCP_IPCP;
    485 		clr_ncpflags &= ~SPPP_NCP_IPCP;
    486 	} else if (strcmp(arg, "noipcp") == 0) {
    487 		set_ncpflags &= ~SPPP_NCP_IPCP;
    488 		clr_ncpflags |= SPPP_NCP_IPCP;
    489 	} else if (strcmp(arg, "ipv6cp") == 0) {
    490 		set_ncpflags |= SPPP_NCP_IPV6CP;
    491 		clr_ncpflags &= ~SPPP_NCP_IPV6CP;
    492 	} else if (strcmp(arg, "noipv6cp") == 0) {
    493 		set_ncpflags &= ~SPPP_NCP_IPV6CP;
    494 		clr_ncpflags |= SPPP_NCP_IPV6CP;
    495 	} else
    496 		errx(EX_DATAERR, "bad parameter: \"%s\"", arg);
    497 }
    498 
    499 static void
    500 usage(void)
    501 {
    502 	const char * prog = getprogname();
    503 	fprintf(stderr,
    504 	    "usage:\n"
    505 	    "       %s [-f config] ifname [...]\n"
    506 	    "       %s [-v] ifname [{my|his}auth{proto|name|secret}=...] \\\n"
    507             "                      [callin] [always] [{no}rechallenge]\n"
    508             "                      [query-dns=3] [{no}ipcp] [{no}ipv6cp]\n"
    509 	    "           to set authentication names, passwords\n"
    510 	    "           and (optional) paramaters\n"
    511 	    "       %s [-v] ifname lcp-timeout=ms|idle-timeout=s|\n"
    512 	    "                      max-noreceive=s|max-alive-missed=cnt|\n"
    513 	    "                      max-auth-failure=count|clear-auth-failure\n"
    514 	    "           to set general parameters\n"
    515 	    "   or\n"
    516 	    "       %s -e ethernet-ifname ifname\n"
    517 	    "           to connect an ethernet interface for PPPoE\n"
    518 	    "       %s [-a access-concentrator-name] [-s service-name] ifname\n"
    519 	    "           to specify (optional) data for PPPoE sessions\n"
    520 	    "       %s -d ifname\n"
    521 	    "           to dump the current PPPoE session state\n"
    522 	    "       %s -n (1|2) ifname\n"
    523 	    "           to print DNS addresses retrieved via query-dns\n"
    524 	    , prog, prog, prog, prog, prog, prog, prog);
    525 	exit(EX_USAGE);
    526 }
    527 
    528 static void
    529 print_vals(const char *ifname, int phase, struct spppauthcfg *sp, int lcp_timeout,
    530 	time_t idle_timeout, int authfailures, int max_auth_failures,
    531 	u_int maxalive_cnt, time_t max_noreceive_time, int ncp_flags)
    532 {
    533 #ifndef __NetBSD__
    534 	time_t send, recv;
    535 #endif
    536 
    537 	printf("%s:\tphase=%s\n", ifname, phase_name(phase));
    538 	if (sp->myauth) {
    539 		printf("\tmyauthproto=%s myauthname=\"%s\"\n",
    540 		       proto_name(sp->myauth),
    541 		       sp->myname);
    542 	}
    543 	if (sp->hisauth) {
    544 		printf("\thisauthproto=%s hisauthname=\"%s\"%s\n",
    545 		       proto_name(sp->hisauth),
    546 		       sp->hisname,
    547 		       authflags(sp->hisauthflags));
    548 	}
    549 #ifndef __NetBSD__
    550 	if (sp->defs.pp_phase > PHASE_DEAD) {
    551 		send = time(NULL) - sp->defs.pp_last_sent;
    552 		recv = time(NULL) - sp->defs.pp_last_recv;
    553 		printf("\tidle_time=%ld\n", (send<recv)? send : recv);
    554 	}
    555 #endif
    556 
    557 	printf("\tlcp timeout: %.3f s\n",
    558 	       (double)lcp_timeout / hz);
    559 
    560 	if (idle_timeout != 0)
    561 		printf("\tidle timeout = %lu s\n", (unsigned long)idle_timeout);
    562 	else
    563 		printf("\tidle timeout = disabled\n");
    564 
    565 	if (authfailures != 0)
    566 		printf("\tauthentication failures = %d\n", authfailures);
    567 	printf("\tmax-auth-failure = %d\n", max_auth_failures);
    568 
    569 	printf("\tmax-noreceive = %ld seconds\n", (long)max_noreceive_time);
    570 	printf("\tmax-alive-missed = %u unanswered echo requests\n", maxalive_cnt);
    571 
    572 #ifndef __NetBSD__
    573 	printf("\tenable_vj: %s\n",
    574 	       sp->defs.enable_vj ? "on" : "off");
    575 #endif
    576 
    577 	printf("\tipcp: %s\n",
    578 	    ncp_flags & SPPP_NCP_IPCP ? "enable" : "disable");
    579 	printf("\tipv6cp: %s\n",
    580 	    ncp_flags & SPPP_NCP_IPV6CP ? "enable" : "disable");
    581 }
    582 
    583 static void
    584 print_dns(const char *ifname, int dns1, int dns2, int s, int tabs)
    585 {
    586 	int i;
    587 	struct spppdnsaddrs addrs;
    588 
    589 	if (!dns1 && !dns2)
    590 		return;
    591 
    592 	PPPOECTL_IOCTL(ifname, s, SPPPGETDNSADDRS, &addrs);
    593 	if (dns1) {
    594 		for (i = 0; i < tabs; i++)
    595 			printf("\t");
    596 		if (tabs > 0)
    597 			printf("primary dns address ");
    598 		printf("%d.%d.%d.%d\n",
    599 		       (addrs.dns[0] >> 24) & 0xff,
    600 		       (addrs.dns[0] >> 16) & 0xff,
    601 		       (addrs.dns[0] >> 8) & 0xff,
    602 		       addrs.dns[0] & 0xff);
    603 	}
    604 	if (dns2) {
    605 		for (i = 0; i < tabs; i++)
    606 			printf("\t");
    607 		if (tabs > 0)
    608 			printf("secondary dns address ");
    609 		printf("%d.%d.%d.%d\n",
    610 		       (addrs.dns[1] >> 24) & 0xff,
    611 		       (addrs.dns[1] >> 16) & 0xff,
    612 		       (addrs.dns[1] >> 8) & 0xff,
    613 		       addrs.dns[1] & 0xff);
    614 	}
    615 }
    616 
    617 static void
    618 print_stats(const char *ifname, int s, int dump)
    619 {
    620 	struct pppoeconnectionstate state;
    621 	struct sppplcpstatus lcpst;
    622 	struct spppipcpstatus ipcpst;
    623 	struct spppipv6cpstatus ipv6cpst;
    624 	struct in_addr addr;
    625 
    626 	PPPOECTL_IOCTL(ifname, s, PPPOEGETSESSION, &state);
    627 
    628 	/* dump PPPoE session state */
    629 	printf("%s:\t%s %s\n", ifname,
    630 	    dump > 1 ? "PPPoE state:" : "state =",
    631 	    pppoe_state_name(state.state));
    632 	printf("\tSession ID: 0x%x\n", state.session_id);
    633 	printf("\tPADI retries: %d\n", state.padi_retry_no);
    634 	printf("\tPADR retries: %d\n", state.padr_retry_no);
    635 
    636 	if (dump > 1) {
    637 		PPPOECTL_IOCTL(ifname, s, SPPPGETLCPSTATUS, &lcpst);
    638 		PPPOECTL_IOCTL(ifname, s, SPPPGETIPCPSTATUS, &ipcpst);
    639 		PPPOECTL_IOCTL(ifname, s, SPPPGETIPV6CPSTATUS, &ipv6cpst);
    640 
    641 		printf("\tLCP state: %s\n",
    642 		    ppp_state_name(lcpst.state));
    643 		printf("\tIPCP state: %s\n",
    644 		    ppp_state_name(ipcpst.state));
    645 		printf("\tIPv6CP state: %s\n",
    646 		    ppp_state_name(ipv6cpst.state));
    647 
    648 		if (lcpst.state == SPPP_STATE_OPENED) {
    649 			printf("\tLCP negotiated options:\n");
    650 			printf("\t\tmru %lu\n", lcpst.mru);
    651 			printf("\t\tmagic number 0x%lx\n",
    652 			    lcpst.magic);
    653 		}
    654 
    655 		if (ipcpst.state == SPPP_STATE_OPENED) {
    656 			addr.s_addr = ipcpst.myaddr;
    657 
    658 			printf("\tIPCP negotiated options:\n");
    659 			printf("\t\taddress %s\n", inet_ntoa(addr));
    660 			print_dns(ifname,
    661 			    ISSET(ipcpst.opts, SPPP_IPCP_OPT_PRIMDNS),
    662 			    ISSET(ipcpst.opts, SPPP_IPCP_OPT_SECDNS),
    663 			    s, 2);
    664 		}
    665 
    666 		if (ipv6cpst.state == SPPP_STATE_OPENED) {
    667 			printf("\tIPv6CP negotiated options:\n");
    668 			if (ISSET(ipv6cpst.opts, SPPP_IPV6CP_OPT_COMPRESSION))
    669 				printf("\t\tcompression\n");
    670 			if (ISSET(ipv6cpst.opts, SPPP_IPV6CP_OPT_IFID)) {
    671 				printf("\t\tifid: "
    672 				    "my_ifid=0x%02x%02x%02x%02x%02x%02x%02x%02x, "
    673 				    "his_ifid=0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
    674 				ipv6cpst.my_ifid[0], ipv6cpst.my_ifid[1],
    675 				ipv6cpst.my_ifid[2], ipv6cpst.my_ifid[3],
    676 				ipv6cpst.my_ifid[4], ipv6cpst.my_ifid[5],
    677 				ipv6cpst.my_ifid[6], ipv6cpst.my_ifid[7],
    678 				ipv6cpst.his_ifid[0], ipv6cpst.his_ifid[1],
    679 				ipv6cpst.his_ifid[2], ipv6cpst.his_ifid[3],
    680 				ipv6cpst.his_ifid[4], ipv6cpst.his_ifid[5],
    681 				ipv6cpst.his_ifid[6], ipv6cpst.his_ifid[7]);
    682 			}
    683 		}
    684 	}
    685 }
    686 
    687 static const char *
    688 phase_name(int phase)
    689 {
    690 	switch (phase) {
    691 	case SPPP_PHASE_DEAD:		return "dead";
    692 	case SPPP_PHASE_ESTABLISH:	return "establish";
    693 	case SPPP_PHASE_TERMINATE:	return "terminate";
    694 	case SPPP_PHASE_AUTHENTICATE:	return "authenticate";
    695 	case SPPP_PHASE_NETWORK:	return "network";
    696 	}
    697 	return "illegal";
    698 }
    699 
    700 static const char *
    701 proto_name(int proto)
    702 {
    703 	static char buf[12];
    704 	switch (proto) {
    705 	case SPPP_AUTHPROTO_PAP:	return "pap";
    706 	case SPPP_AUTHPROTO_CHAP:	return "chap";
    707 	case SPPP_AUTHPROTO_NONE:	return "none";
    708 	}
    709 	snprintf(buf, sizeof(buf), "0x%x", (unsigned)proto);
    710 	return buf;
    711 }
    712 
    713 static const char *
    714 authflags(int flags)
    715 {
    716 	static char buf[32];
    717 	buf[0] = '\0';
    718 	if (flags & SPPP_AUTHFLAG_NOCALLOUT)
    719 		strlcat(buf, " callin", sizeof(buf));
    720 	if (flags & SPPP_AUTHFLAG_NORECHALLENGE)
    721 		strlcat(buf, " norechallenge", sizeof(buf));
    722 	return buf;
    723 }
    724 
    725 static const char *
    726 pppoe_state_name(int state)
    727 {
    728 
    729 	switch(state) {
    730 	case PPPOE_STATE_INITIAL:
    731 		return "initial";
    732 	case PPPOE_STATE_PADI_SENT:
    733 		return "PADI sent";
    734 	case PPPOE_STATE_PADR_SENT:
    735 		return "PADR sent";
    736 	case PPPOE_STATE_SESSION:
    737 		return "session";
    738 	case PPPOE_STATE_CLOSING:
    739 		return "closing";
    740 	}
    741 
    742 	return "unknown";
    743 }
    744 static const char *
    745 ppp_state_name(int state)
    746 {
    747 
    748 	switch (state) {
    749 	case SPPP_STATE_INITIAL:	return "initial";
    750 	case SPPP_STATE_STARTING:	return "starting";
    751 	case SPPP_STATE_CLOSED:		return "closed";
    752 	case SPPP_STATE_STOPPED:	return "stopped";
    753 	case SPPP_STATE_CLOSING:	return "closing";
    754 	case SPPP_STATE_STOPPING:	return "stopping";
    755 	case SPPP_STATE_REQ_SENT:	return "req-sent";
    756 	case SPPP_STATE_ACK_RCVD:	return "ack-rcvd";
    757 	case SPPP_STATE_ACK_SENT:	return "ack-sent";
    758 	case SPPP_STATE_OPENED:		return "opened";
    759 	}
    760 
    761 	return "unknown";
    762 }
    763 
    764 static void
    765 print_error(const char *ifname, int error, const char * str)
    766 {
    767 	if (error == -1)
    768 		fprintf(stderr, "%s: interface not found\n", ifname);
    769 	else
    770 		fprintf(stderr, "%s: %s: %s\n", ifname, str, strerror(error));
    771 	exit(EX_DATAERR);
    772 }
    773