2 * Copyright (c) 1997, 2001 Joerg Wunsch
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sbin/spppcontrol/spppcontrol.c,v 1.7.2.2 2002/04/24 18:47:22 joerg Exp $
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
34 #include <net/if_var.h>
35 #include <net/sppp/if_sppp.h>
44 static void usage(void);
45 void print_vals(const char *ifname
, struct spppreq
*sp
);
46 const char *phase_name(enum ppp_phase phase
);
47 const char *proto_name(u_short proto
);
48 const char *authflags(u_short flags
);
50 #define PPP_PAP 0xc023
51 #define PPP_CHAP 0xc223
54 main(int argc
, char **argv
)
57 int errs
= 0, verbose
= 0;
61 const char *ifname
, *cp
;
65 while ((c
= getopt(argc
, argv
, "v")) != -1)
82 strncpy(ifr
.ifr_name
, ifname
, sizeof ifr
.ifr_name
);
84 /* use a random AF to create the socket */
85 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
86 err(EX_UNAVAILABLE
, "ifconfig: socket");
91 spr
.cmd
= (uintptr_t)SPPPIOGDEFS
;
92 ifr
.ifr_data
= (caddr_t
)&spr
;
94 if (ioctl(s
, SIOCGIFGENERIC
, &ifr
) == -1)
95 err(EX_OSERR
, "SIOCGIFGENERIC(SPPPIOGDEFS)");
99 print_vals(ifname
, &spr
);
103 #define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0
106 if (startswith("authproto=")) {
108 if (strcmp(cp
, "pap") == 0)
109 spr
.defs
.myauth
.proto
=
110 spr
.defs
.hisauth
.proto
= PPP_PAP
;
111 else if (strcmp(cp
, "chap") == 0)
112 spr
.defs
.myauth
.proto
=
113 spr
.defs
.hisauth
.proto
= PPP_CHAP
;
114 else if (strcmp(cp
, "none") == 0)
115 spr
.defs
.myauth
.proto
=
116 spr
.defs
.hisauth
.proto
= 0;
118 errx(EX_DATAERR
, "bad auth proto: %s", cp
);
119 } else if (startswith("myauthproto=")) {
121 if (strcmp(cp
, "pap") == 0)
122 spr
.defs
.myauth
.proto
= PPP_PAP
;
123 else if (strcmp(cp
, "chap") == 0)
124 spr
.defs
.myauth
.proto
= PPP_CHAP
;
125 else if (strcmp(cp
, "none") == 0)
126 spr
.defs
.myauth
.proto
= 0;
128 errx(EX_DATAERR
, "bad auth proto: %s", cp
);
129 } else if (startswith("myauthname="))
130 strncpy(spr
.defs
.myauth
.name
, argv
[0] + off
,
132 else if (startswith("myauthsecret=") ||
133 startswith("myauthkey="))
134 strncpy(spr
.defs
.myauth
.secret
, argv
[0] + off
,
136 else if (startswith("hisauthproto=")) {
138 if (strcmp(cp
, "pap") == 0)
139 spr
.defs
.hisauth
.proto
= PPP_PAP
;
140 else if (strcmp(cp
, "chap") == 0)
141 spr
.defs
.hisauth
.proto
= PPP_CHAP
;
142 else if (strcmp(cp
, "none") == 0)
143 spr
.defs
.hisauth
.proto
= 0;
145 errx(EX_DATAERR
, "bad auth proto: %s", cp
);
146 } else if (startswith("hisauthname="))
147 strncpy(spr
.defs
.hisauth
.name
, argv
[0] + off
,
149 else if (startswith("hisauthsecret=") ||
150 startswith("hisauthkey="))
151 strncpy(spr
.defs
.hisauth
.secret
, argv
[0] + off
,
153 else if (strcmp(argv
[0], "callin") == 0)
154 spr
.defs
.hisauth
.flags
|= AUTHFLAG_NOCALLOUT
;
155 else if (strcmp(argv
[0], "always") == 0)
156 spr
.defs
.hisauth
.flags
&= ~AUTHFLAG_NOCALLOUT
;
157 else if (strcmp(argv
[0], "norechallenge") == 0)
158 spr
.defs
.hisauth
.flags
|= AUTHFLAG_NORECHALLENGE
;
159 else if (strcmp(argv
[0], "rechallenge") == 0)
160 spr
.defs
.hisauth
.flags
&= ~AUTHFLAG_NORECHALLENGE
;
161 else if (startswith("lcp-timeout=")) {
163 to
= strtol(cp
, &endp
, 10);
164 if (*cp
== '\0' || *endp
!= '\0' ||
166 * NB: 10 ms is the minimal possible value for
167 * hz=100. We assume no kernel has less clock
168 * frequency than that...
170 to
< 10 || to
> 20000)
171 errx(EX_DATAERR
, "bad lcp timeout value: %s",
173 spr
.defs
.lcp
.timeout
= to
;
174 } else if (strcmp(argv
[0], "enable-vj") == 0)
175 spr
.defs
.enable_vj
= 1;
176 else if (strcmp(argv
[0], "disable-vj") == 0)
177 spr
.defs
.enable_vj
= 0;
178 else if (strcmp(argv
[0], "enable-ipv6") == 0)
179 spr
.defs
.enable_ipv6
= 1;
180 else if (strcmp(argv
[0], "disable-ipv6") == 0)
181 spr
.defs
.enable_ipv6
= 0;
183 errx(EX_DATAERR
, "bad parameter: \"%s\"", argv
[0]);
189 spr
.cmd
= (uintptr_t)SPPPIOSDEFS
;
191 if (ioctl(s
, SIOCSIFGENERIC
, &ifr
) == -1)
192 err(EX_OSERR
, "SIOCSIFGENERIC(SPPPIOSDEFS)");
195 print_vals(ifname
, &spr
);
203 fprintf(stderr
, "%s\n%s\n",
204 "usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
205 " spppcontrol [-v] ifname callin|always");
210 print_vals(const char *ifname
, struct spppreq
*sp
)
212 printf("%s:\tphase=%s\n", ifname
, phase_name(sp
->defs
.pp_phase
));
213 if (sp
->defs
.myauth
.proto
) {
214 printf("\tmyauthproto=%s myauthname=\"%.*s\"\n",
215 proto_name(sp
->defs
.myauth
.proto
),
216 AUTHNAMELEN
, sp
->defs
.myauth
.name
);
218 if (sp
->defs
.hisauth
.proto
) {
219 printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n",
220 proto_name(sp
->defs
.hisauth
.proto
),
221 AUTHNAMELEN
, sp
->defs
.hisauth
.name
,
222 authflags(sp
->defs
.hisauth
.flags
));
224 printf("\tlcp-timeout=%d ms\n", sp
->defs
.lcp
.timeout
);
225 printf("\t%sable-vj\n", sp
->defs
.enable_vj
? "en": "dis");
226 printf("\t%sable-ipv6\n", sp
->defs
.enable_ipv6
? "en": "dis");
230 phase_name(enum ppp_phase phase
)
233 case PHASE_DEAD
: return "dead";
234 case PHASE_ESTABLISH
: return "establish";
235 case PHASE_TERMINATE
: return "terminate";
236 case PHASE_AUTHENTICATE
: return "authenticate";
237 case PHASE_NETWORK
: return "network";
243 proto_name(u_short proto
)
247 case PPP_PAP
: return "pap";
248 case PPP_CHAP
: return "chap";
250 sprintf(buf
, "0x%x", (unsigned)proto
);
255 authflags(u_short flags
)
259 if (flags
& AUTHFLAG_NOCALLOUT
)
260 strcat(buf
, " callin");
261 if (flags
& AUTHFLAG_NORECHALLENGE
)
262 strcat(buf
, " norechallenge");