2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, 2001 Joerg Wunsch
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
37 #include <net/if_sppp.h>
46 static void usage(void);
47 void print_vals(const char *ifname, struct spppreq *sp);
48 const char *phase_name(enum ppp_phase phase);
49 const char *proto_name(u_short proto);
50 const char *authflags(u_short flags);
52 #define PPP_PAP 0xc023
53 #define PPP_CHAP 0xc223
56 main(int argc, char **argv)
59 int errs = 0, verbose = 0;
63 const char *ifname, *cp;
67 while ((c = getopt(argc, argv, "v")) != -1)
84 strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
86 /* use a random AF to create the socket */
87 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
88 err(EX_UNAVAILABLE, "ifconfig: socket");
93 spr.cmd = (uintptr_t) SPPPIOGDEFS;
94 ifr.ifr_data = (caddr_t)&spr;
96 if (ioctl(s, SIOCGIFGENERIC, &ifr) == -1)
97 err(EX_OSERR, "SIOCGIFGENERIC(SPPPIOGDEFS)");
101 print_vals(ifname, &spr);
105 #define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0
108 if (startswith("authproto=")) {
110 if (strcmp(cp, "pap") == 0)
111 spr.defs.myauth.proto =
112 spr.defs.hisauth.proto = PPP_PAP;
113 else if (strcmp(cp, "chap") == 0)
114 spr.defs.myauth.proto =
115 spr.defs.hisauth.proto = PPP_CHAP;
116 else if (strcmp(cp, "none") == 0)
117 spr.defs.myauth.proto =
118 spr.defs.hisauth.proto = 0;
120 errx(EX_DATAERR, "bad auth proto: %s", cp);
121 } else if (startswith("myauthproto=")) {
123 if (strcmp(cp, "pap") == 0)
124 spr.defs.myauth.proto = PPP_PAP;
125 else if (strcmp(cp, "chap") == 0)
126 spr.defs.myauth.proto = PPP_CHAP;
127 else if (strcmp(cp, "none") == 0)
128 spr.defs.myauth.proto = 0;
130 errx(EX_DATAERR, "bad auth proto: %s", cp);
131 } else if (startswith("myauthname="))
132 strncpy(spr.defs.myauth.name, argv[0] + off,
134 else if (startswith("myauthsecret=") ||
135 startswith("myauthkey="))
136 strncpy(spr.defs.myauth.secret, argv[0] + off,
138 else if (startswith("hisauthproto=")) {
140 if (strcmp(cp, "pap") == 0)
141 spr.defs.hisauth.proto = PPP_PAP;
142 else if (strcmp(cp, "chap") == 0)
143 spr.defs.hisauth.proto = PPP_CHAP;
144 else if (strcmp(cp, "none") == 0)
145 spr.defs.hisauth.proto = 0;
147 errx(EX_DATAERR, "bad auth proto: %s", cp);
148 } else if (startswith("hisauthname="))
149 strncpy(spr.defs.hisauth.name, argv[0] + off,
151 else if (startswith("hisauthsecret=") ||
152 startswith("hisauthkey="))
153 strncpy(spr.defs.hisauth.secret, argv[0] + off,
155 else if (strcmp(argv[0], "callin") == 0)
156 spr.defs.hisauth.flags |= AUTHFLAG_NOCALLOUT;
157 else if (strcmp(argv[0], "always") == 0)
158 spr.defs.hisauth.flags &= ~AUTHFLAG_NOCALLOUT;
159 else if (strcmp(argv[0], "norechallenge") == 0)
160 spr.defs.hisauth.flags |= AUTHFLAG_NORECHALLENGE;
161 else if (strcmp(argv[0], "rechallenge") == 0)
162 spr.defs.hisauth.flags &= ~AUTHFLAG_NORECHALLENGE;
163 else if (startswith("lcp-timeout=")) {
165 to = strtol(cp, &endp, 10);
166 if (*cp == '\0' || *endp != '\0' ||
168 * NB: 10 ms is the minimal possible value for
169 * hz=100. We assume no kernel has less clock
170 * frequency than that...
172 to < 10 || to > 20000)
173 errx(EX_DATAERR, "bad lcp timeout value: %s",
175 spr.defs.lcp.timeout = to;
176 } else if (strcmp(argv[0], "enable-vj") == 0)
177 spr.defs.enable_vj = 1;
178 else if (strcmp(argv[0], "disable-vj") == 0)
179 spr.defs.enable_vj = 0;
180 else if (strcmp(argv[0], "enable-ipv6") == 0)
181 spr.defs.enable_ipv6 = 1;
182 else if (strcmp(argv[0], "disable-ipv6") == 0)
183 spr.defs.enable_ipv6 = 0;
185 errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
191 spr.cmd = (uintptr_t)SPPPIOSDEFS;
193 if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1)
194 err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)");
197 print_vals(ifname, &spr);
205 fprintf(stderr, "%s\n%s\n",
206 "usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
207 " spppcontrol [-v] ifname callin|always");
212 print_vals(const char *ifname, struct spppreq *sp)
214 printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase));
215 if (sp->defs.myauth.proto) {
216 printf("\tmyauthproto=%s myauthname=\"%.*s\"\n",
217 proto_name(sp->defs.myauth.proto),
218 AUTHNAMELEN, sp->defs.myauth.name);
220 if (sp->defs.hisauth.proto) {
221 printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n",
222 proto_name(sp->defs.hisauth.proto),
223 AUTHNAMELEN, sp->defs.hisauth.name,
224 authflags(sp->defs.hisauth.flags));
226 printf("\tlcp-timeout=%d ms\n", sp->defs.lcp.timeout);
227 printf("\t%sable-vj\n", sp->defs.enable_vj? "en": "dis");
228 printf("\t%sable-ipv6\n", sp->defs.enable_ipv6? "en": "dis");
232 phase_name(enum ppp_phase phase)
235 case PHASE_DEAD: return "dead";
236 case PHASE_ESTABLISH: return "establish";
237 case PHASE_TERMINATE: return "terminate";
238 case PHASE_AUTHENTICATE: return "authenticate";
239 case PHASE_NETWORK: return "network";
245 proto_name(u_short proto)
249 case PPP_PAP: return "pap";
250 case PPP_CHAP: return "chap";
252 sprintf(buf, "0x%x", (unsigned)proto);
257 authflags(u_short flags)
261 if (flags & AUTHFLAG_NOCALLOUT)
262 strcat(buf, " callin");
263 if (flags & AUTHFLAG_NORECHALLENGE)
264 strcat(buf, " norechallenge");