Home | History | Annotate | Line # | Download | only in pppoectl
pppoectl.c revision 1.1
      1  1.1  martin /* $NetBSD: pppoectl.c,v 1.1 2001/12/10 17:22:09 martin Exp $ */
      2  1.1  martin 
      3  1.1  martin /*
      4  1.1  martin  * Copyright (c) 1997 Joerg Wunsch
      5  1.1  martin  *
      6  1.1  martin  * All rights reserved.
      7  1.1  martin  *
      8  1.1  martin  * Redistribution and use in source and binary forms, with or without
      9  1.1  martin  * modification, are permitted provided that the following conditions
     10  1.1  martin  * are met:
     11  1.1  martin  * 1. Redistributions of source code must retain the above copyright
     12  1.1  martin  *    notice, this list of conditions and the following disclaimer.
     13  1.1  martin  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  martin  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  martin  *    documentation and/or other materials provided with the distribution.
     16  1.1  martin  *
     17  1.1  martin  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
     18  1.1  martin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1  martin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  martin  * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1  martin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1  martin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1  martin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1  martin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1  martin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1  martin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  martin  *
     28  1.1  martin  * From: spppcontrol.c,v 1.3 1998/01/07 07:55:26 charnier Exp
     29  1.1  martin  * From: ispppcontrol
     30  1.1  martin  */
     31  1.1  martin 
     32  1.1  martin #include <sys/param.h>
     33  1.1  martin #include <sys/callout.h>
     34  1.1  martin #include <sys/ioctl.h>
     35  1.1  martin #include <sys/mbuf.h>
     36  1.1  martin #include <sys/socket.h>
     37  1.1  martin #include <sys/time.h>
     38  1.1  martin #include <sys/sysctl.h>
     39  1.1  martin #include <net/if.h>
     40  1.1  martin #include <net/if_sppp.h>
     41  1.1  martin #include <net/if_pppoe.h>
     42  1.1  martin #include <err.h>
     43  1.1  martin #include <stdio.h>
     44  1.1  martin #include <stdlib.h>
     45  1.1  martin #include <string.h>
     46  1.1  martin #include <sysexits.h>
     47  1.1  martin #include <unistd.h>
     48  1.1  martin 
     49  1.1  martin static void usage(void);
     50  1.1  martin void	print_vals(const char *ifname, struct spppreq *sp);
     51  1.1  martin const char *phase_name(enum ppp_phase phase);
     52  1.1  martin const char *proto_name(u_short proto);
     53  1.1  martin const char *authflags(u_short flags);
     54  1.1  martin 
     55  1.1  martin #define PPP_PAP		0xc023
     56  1.1  martin #define PPP_CHAP	0xc223
     57  1.1  martin 
     58  1.1  martin int hz = 0;
     59  1.1  martin 
     60  1.1  martin int
     61  1.1  martin main(int argc, char **argv)
     62  1.1  martin {
     63  1.1  martin 	int s, c;
     64  1.1  martin 	int errs = 0, verbose = 0;
     65  1.1  martin 	size_t off, len;
     66  1.1  martin 	const char *ifname, *cp;
     67  1.1  martin 	const char *eth_if_name, *access_concentrator, *service;
     68  1.1  martin 	struct ifreq ifr;
     69  1.1  martin 	struct spppreq spr;
     70  1.1  martin 	int mib[2];
     71  1.1  martin 	struct clockinfo clockinfo;
     72  1.1  martin 
     73  1.1  martin 	eth_if_name = NULL;
     74  1.1  martin 	access_concentrator = NULL;
     75  1.1  martin 	service = NULL;
     76  1.1  martin 	while ((c = getopt(argc, argv, "vde:s:a:")) != -1)
     77  1.1  martin 		switch (c) {
     78  1.1  martin 		case 'v':
     79  1.1  martin 			verbose++;
     80  1.1  martin 			break;
     81  1.1  martin 
     82  1.1  martin 		case 'e':
     83  1.1  martin 			eth_if_name = optarg;
     84  1.1  martin 			break;
     85  1.1  martin 
     86  1.1  martin 		case 's':
     87  1.1  martin 			service = optarg;
     88  1.1  martin 			break;
     89  1.1  martin 
     90  1.1  martin 		case 'a':
     91  1.1  martin 			access_concentrator = optarg;
     92  1.1  martin 			break;
     93  1.1  martin 
     94  1.1  martin 		default:
     95  1.1  martin 			errs++;
     96  1.1  martin 			break;
     97  1.1  martin 		}
     98  1.1  martin 	argv += optind;
     99  1.1  martin 	argc -= optind;
    100  1.1  martin 
    101  1.1  martin 	if (errs || argc < 1)
    102  1.1  martin 		usage();
    103  1.1  martin 
    104  1.1  martin 	ifname = argv[0];
    105  1.1  martin 
    106  1.1  martin 	strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
    107  1.1  martin 
    108  1.1  martin 	/* use a random AF to create the socket */
    109  1.1  martin 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    110  1.1  martin 		err(EX_UNAVAILABLE, "ifconfig: socket");
    111  1.1  martin 
    112  1.1  martin 	argc--;
    113  1.1  martin 	argv++;
    114  1.1  martin 
    115  1.1  martin 	if (eth_if_name) {
    116  1.1  martin 		struct pppoediscparms parms;
    117  1.1  martin 		int e;
    118  1.1  martin 
    119  1.1  martin 		memset(&parms, 0, sizeof parms);
    120  1.1  martin 		strncpy(parms.ifname, ifname, sizeof(parms.ifname));
    121  1.1  martin 		strncpy(parms.eth_ifname, eth_if_name, sizeof(parms.eth_ifname));
    122  1.1  martin 		if (access_concentrator) {
    123  1.1  martin 			parms.ac_name = (char*)access_concentrator;
    124  1.1  martin 			parms.ac_name_len = strlen(access_concentrator);
    125  1.1  martin 		}
    126  1.1  martin 		if (service) {
    127  1.1  martin 			parms.service_name = (char*)service;
    128  1.1  martin 			parms.service_name_len = strlen(service);
    129  1.1  martin 		}
    130  1.1  martin 
    131  1.1  martin 		e = ioctl(s, PPPOESETPARMS, &parms);
    132  1.1  martin 		if (e) {
    133  1.1  martin 			fprintf(stderr, "%s: ioctl(PPPOESETPARMS): %s\n",
    134  1.1  martin 			ifname, strerror(e));
    135  1.1  martin 		}
    136  1.1  martin 		return 0;
    137  1.1  martin 	}
    138  1.1  martin 
    139  1.1  martin 	spr.cmd = (int)SPPPIOGDEFS;
    140  1.1  martin 	ifr.ifr_data = (caddr_t)&spr;
    141  1.1  martin 
    142  1.1  martin 	if (ioctl(s, SIOCGIFGENERIC, &ifr) == -1)
    143  1.1  martin 		err(EX_OSERR, "SIOCGIFGENERIC(SPPPIOGDEFS)");
    144  1.1  martin 
    145  1.1  martin 	mib[0] = CTL_KERN;
    146  1.1  martin 	mib[1] = KERN_CLOCKRATE;
    147  1.1  martin 	len = sizeof(clockinfo);
    148  1.1  martin 	if(sysctl(mib, 2, &clockinfo, &len, NULL, 0) == -1)
    149  1.1  martin 	{
    150  1.1  martin 		fprintf(stderr, "error, cannot sysctl kern.clockrate!\n");
    151  1.1  martin 		exit(1);
    152  1.1  martin 	}
    153  1.1  martin 
    154  1.1  martin 	hz = clockinfo.hz;
    155  1.1  martin 
    156  1.1  martin 	if (argc == 0) {
    157  1.1  martin 		/* list only mode */
    158  1.1  martin 		print_vals(ifname, &spr);
    159  1.1  martin 		return 0;
    160  1.1  martin 	}
    161  1.1  martin 
    162  1.1  martin #define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0
    163  1.1  martin 
    164  1.1  martin 	while (argc > 0) {
    165  1.1  martin 		if (startswith("authproto=")) {
    166  1.1  martin 			cp = argv[0] + off;
    167  1.1  martin 			if (strcmp(cp, "pap") == 0)
    168  1.1  martin 				spr.defs.myauth.proto =
    169  1.1  martin 					spr.defs.hisauth.proto = PPP_PAP;
    170  1.1  martin 			else if (strcmp(cp, "chap") == 0)
    171  1.1  martin 				spr.defs.myauth.proto =
    172  1.1  martin 					spr.defs.hisauth.proto = PPP_CHAP;
    173  1.1  martin 			else if (strcmp(cp, "none") == 0)
    174  1.1  martin 				spr.defs.myauth.proto =
    175  1.1  martin 					spr.defs.hisauth.proto = 0;
    176  1.1  martin 			else
    177  1.1  martin 				errx(EX_DATAERR, "bad auth proto: %s", cp);
    178  1.1  martin 		} else if (startswith("myauthproto=")) {
    179  1.1  martin 			cp = argv[0] + off;
    180  1.1  martin 			if (strcmp(cp, "pap") == 0)
    181  1.1  martin 				spr.defs.myauth.proto = PPP_PAP;
    182  1.1  martin 			else if (strcmp(cp, "chap") == 0)
    183  1.1  martin 				spr.defs.myauth.proto = PPP_CHAP;
    184  1.1  martin 			else if (strcmp(cp, "none") == 0)
    185  1.1  martin 				spr.defs.myauth.proto = 0;
    186  1.1  martin 			else
    187  1.1  martin 				errx(EX_DATAERR, "bad auth proto: %s", cp);
    188  1.1  martin 		} else if (startswith("myauthname="))
    189  1.1  martin 			strncpy(spr.defs.myauth.name, argv[0] + off,
    190  1.1  martin 				AUTHNAMELEN);
    191  1.1  martin 		else if (startswith("myauthsecret=") ||
    192  1.1  martin 			 startswith("myauthkey="))
    193  1.1  martin 			strncpy(spr.defs.myauth.secret, argv[0] + off,
    194  1.1  martin 				AUTHKEYLEN);
    195  1.1  martin 		else if (startswith("hisauthproto=")) {
    196  1.1  martin 			cp = argv[0] + off;
    197  1.1  martin 			if (strcmp(cp, "pap") == 0)
    198  1.1  martin 				spr.defs.hisauth.proto = PPP_PAP;
    199  1.1  martin 			else if (strcmp(cp, "chap") == 0)
    200  1.1  martin 				spr.defs.hisauth.proto = PPP_CHAP;
    201  1.1  martin 			else if (strcmp(cp, "none") == 0)
    202  1.1  martin 				spr.defs.hisauth.proto = 0;
    203  1.1  martin 			else
    204  1.1  martin 				errx(EX_DATAERR, "bad auth proto: %s", cp);
    205  1.1  martin 		} else if (startswith("hisauthname="))
    206  1.1  martin 			strncpy(spr.defs.hisauth.name, argv[0] + off,
    207  1.1  martin 				AUTHNAMELEN);
    208  1.1  martin 		else if (startswith("hisauthsecret=") ||
    209  1.1  martin 			 startswith("hisauthkey="))
    210  1.1  martin 			strncpy(spr.defs.hisauth.secret, argv[0] + off,
    211  1.1  martin 				AUTHKEYLEN);
    212  1.1  martin 		else if (strcmp(argv[0], "callin") == 0)
    213  1.1  martin 			spr.defs.hisauth.flags |= AUTHFLAG_NOCALLOUT;
    214  1.1  martin 		else if (strcmp(argv[0], "always") == 0)
    215  1.1  martin 			spr.defs.hisauth.flags &= ~AUTHFLAG_NOCALLOUT;
    216  1.1  martin 		else if (strcmp(argv[0], "norechallenge") == 0)
    217  1.1  martin 			spr.defs.hisauth.flags |= AUTHFLAG_NORECHALLENGE;
    218  1.1  martin 		else if (strcmp(argv[0], "rechallenge") == 0)
    219  1.1  martin 			spr.defs.hisauth.flags &= ~AUTHFLAG_NORECHALLENGE;
    220  1.1  martin #ifndef __NetBSD__
    221  1.1  martin 		else if (strcmp(argv[0], "enable-vj") == 0)
    222  1.1  martin 			spr.defs.enable_vj = 1;
    223  1.1  martin 		else if (strcmp(argv[0], "disable-vj") == 0)
    224  1.1  martin 			spr.defs.enable_vj = 0;
    225  1.1  martin #endif
    226  1.1  martin 		else if (startswith("lcp-timeout=")) {
    227  1.1  martin 			int timeout_arg = atoi(argv[0]+off);
    228  1.1  martin 			if ((timeout_arg > 20000) || (timeout_arg <= 0))
    229  1.1  martin 				errx(EX_DATAERR, "bad lcp timeout value: %s",
    230  1.1  martin 				     argv[0]+off);
    231  1.1  martin 			spr.defs.lcp.timeout = timeout_arg * hz / 1000;
    232  1.1  martin 		} else
    233  1.1  martin 			errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
    234  1.1  martin 
    235  1.1  martin 		argv++;
    236  1.1  martin 		argc--;
    237  1.1  martin 	}
    238  1.1  martin 
    239  1.1  martin 	spr.cmd = (int)SPPPIOSDEFS;
    240  1.1  martin 
    241  1.1  martin 	if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1)
    242  1.1  martin 		err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)");
    243  1.1  martin 
    244  1.1  martin 	if (verbose)
    245  1.1  martin 		print_vals(ifname, &spr);
    246  1.1  martin 
    247  1.1  martin 	return 0;
    248  1.1  martin }
    249  1.1  martin 
    250  1.1  martin static void
    251  1.1  martin usage(void)
    252  1.1  martin {
    253  1.1  martin 	fprintf(stderr, "%s\n%s\n",
    254  1.1  martin 	"usage: ispppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
    255  1.1  martin 	"       ispppcontrol [-v] ifname callin|always");
    256  1.1  martin 	exit(EX_USAGE);
    257  1.1  martin }
    258  1.1  martin 
    259  1.1  martin void
    260  1.1  martin print_vals(const char *ifname, struct spppreq *sp)
    261  1.1  martin {
    262  1.1  martin #ifndef __NetBSD__
    263  1.1  martin 	time_t send, recv;
    264  1.1  martin #endif
    265  1.1  martin 
    266  1.1  martin 	printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase));
    267  1.1  martin 	if (sp->defs.myauth.proto) {
    268  1.1  martin 		printf("\tmyauthproto=%s myauthname=\"%.*s\"\n",
    269  1.1  martin 		       proto_name(sp->defs.myauth.proto),
    270  1.1  martin 		       AUTHNAMELEN, sp->defs.myauth.name);
    271  1.1  martin 	}
    272  1.1  martin 	if (sp->defs.hisauth.proto) {
    273  1.1  martin 		printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n",
    274  1.1  martin 		       proto_name(sp->defs.hisauth.proto),
    275  1.1  martin 		       AUTHNAMELEN, sp->defs.hisauth.name,
    276  1.1  martin 		       authflags(sp->defs.hisauth.flags));
    277  1.1  martin 	}
    278  1.1  martin #ifndef __NetBSD__
    279  1.1  martin 	if (sp->defs.pp_phase > PHASE_DEAD) {
    280  1.1  martin 		send = time(NULL) - sp->defs.pp_last_sent;
    281  1.1  martin 		recv = time(NULL) - sp->defs.pp_last_recv;
    282  1.1  martin 		printf("\tidle_time=%ld\n", (send<recv)? send : recv);
    283  1.1  martin 	}
    284  1.1  martin #endif
    285  1.1  martin 	printf("\tlcp timeout: %.3f s\n",
    286  1.1  martin 	       (double)sp->defs.lcp.timeout / hz);
    287  1.1  martin #ifndef __NetBSD__
    288  1.1  martin 	printf("\tenable_vj: %s\n",
    289  1.1  martin 	       sp->defs.enable_vj ? "on" : "off");
    290  1.1  martin #endif
    291  1.1  martin }
    292  1.1  martin 
    293  1.1  martin const char *
    294  1.1  martin phase_name(enum ppp_phase phase)
    295  1.1  martin {
    296  1.1  martin 	switch (phase) {
    297  1.1  martin 	case PHASE_DEAD:	return "dead";
    298  1.1  martin 	case PHASE_ESTABLISH:	return "establish";
    299  1.1  martin 	case PHASE_TERMINATE:	return "terminate";
    300  1.1  martin 	case PHASE_AUTHENTICATE: return "authenticate";
    301  1.1  martin 	case PHASE_NETWORK:	return "network";
    302  1.1  martin 	}
    303  1.1  martin 	return "illegal";
    304  1.1  martin }
    305  1.1  martin 
    306  1.1  martin const char *
    307  1.1  martin proto_name(u_short proto)
    308  1.1  martin {
    309  1.1  martin 	static char buf[12];
    310  1.1  martin 	switch (proto) {
    311  1.1  martin 	case PPP_PAP:	return "pap";
    312  1.1  martin 	case PPP_CHAP:	return "chap";
    313  1.1  martin 	}
    314  1.1  martin 	sprintf(buf, "0x%x", (unsigned)proto);
    315  1.1  martin 	return buf;
    316  1.1  martin }
    317  1.1  martin 
    318  1.1  martin const char *
    319  1.1  martin authflags(u_short flags)
    320  1.1  martin {
    321  1.1  martin 	static char buf[32];
    322  1.1  martin 	buf[0] = '\0';
    323  1.1  martin 	if (flags & AUTHFLAG_NOCALLOUT)
    324  1.1  martin 		strcat(buf, " callin");
    325  1.1  martin 	if (flags & AUTHFLAG_NORECHALLENGE)
    326  1.1  martin 		strcat(buf, " norechallenge");
    327  1.1  martin 	return buf;
    328  1.1  martin }
    329