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