Repo-copy numerous files from sys/emulation/posix4 to sys/sys and sys/kern
[dragonfly/vkernel-mp.git] / sbin / spppcontrol / spppcontrol.c
blob3b2e66d11931e13d306446c25feaaa37e126dbb6
1 /*
2 * Copyright (c) 1997, 2001 Joerg Wunsch
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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 $
27 * $DragonFly: src/sbin/spppcontrol/spppcontrol.c,v 1.3 2003/08/08 06:18:41 dillon Exp $
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
34 #include <net/if.h>
35 #include <net/if_var.h>
36 #include <net/sppp/if_sppp.h>
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sysexits.h>
43 #include <unistd.h>
45 static void usage(void);
46 void print_vals(const char *ifname, struct spppreq *sp);
47 const char *phase_name(enum ppp_phase phase);
48 const char *proto_name(u_short proto);
49 const char *authflags(u_short flags);
51 #define PPP_PAP 0xc023
52 #define PPP_CHAP 0xc223
54 int
55 main(int argc, char **argv)
57 int s, c;
58 int errs = 0, verbose = 0;
59 size_t off;
60 long to;
61 char *endp;
62 const char *ifname, *cp;
63 struct ifreq ifr;
64 struct spppreq spr;
66 while ((c = getopt(argc, argv, "v")) != -1)
67 switch (c) {
68 case 'v':
69 verbose++;
70 break;
72 default:
73 errs++;
74 break;
76 argv += optind;
77 argc -= optind;
79 if (errs || argc < 1)
80 usage();
82 ifname = argv[0];
83 strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
85 /* use a random AF to create the socket */
86 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
87 err(EX_UNAVAILABLE, "ifconfig: socket");
89 argc--;
90 argv++;
92 spr.cmd = (int)SPPPIOGDEFS;
93 ifr.ifr_data = (caddr_t)&spr;
95 if (ioctl(s, SIOCGIFGENERIC, &ifr) == -1)
96 err(EX_OSERR, "SIOCGIFGENERIC(SPPPIOGDEFS)");
98 if (argc == 0) {
99 /* list only mode */
100 print_vals(ifname, &spr);
101 return 0;
104 #define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0
106 while (argc > 0) {
107 if (startswith("authproto=")) {
108 cp = argv[0] + off;
109 if (strcmp(cp, "pap") == 0)
110 spr.defs.myauth.proto =
111 spr.defs.hisauth.proto = PPP_PAP;
112 else if (strcmp(cp, "chap") == 0)
113 spr.defs.myauth.proto =
114 spr.defs.hisauth.proto = PPP_CHAP;
115 else if (strcmp(cp, "none") == 0)
116 spr.defs.myauth.proto =
117 spr.defs.hisauth.proto = 0;
118 else
119 errx(EX_DATAERR, "bad auth proto: %s", cp);
120 } else if (startswith("myauthproto=")) {
121 cp = argv[0] + off;
122 if (strcmp(cp, "pap") == 0)
123 spr.defs.myauth.proto = PPP_PAP;
124 else if (strcmp(cp, "chap") == 0)
125 spr.defs.myauth.proto = PPP_CHAP;
126 else if (strcmp(cp, "none") == 0)
127 spr.defs.myauth.proto = 0;
128 else
129 errx(EX_DATAERR, "bad auth proto: %s", cp);
130 } else if (startswith("myauthname="))
131 strncpy(spr.defs.myauth.name, argv[0] + off,
132 AUTHNAMELEN);
133 else if (startswith("myauthsecret=") ||
134 startswith("myauthkey="))
135 strncpy(spr.defs.myauth.secret, argv[0] + off,
136 AUTHKEYLEN);
137 else if (startswith("hisauthproto=")) {
138 cp = argv[0] + off;
139 if (strcmp(cp, "pap") == 0)
140 spr.defs.hisauth.proto = PPP_PAP;
141 else if (strcmp(cp, "chap") == 0)
142 spr.defs.hisauth.proto = PPP_CHAP;
143 else if (strcmp(cp, "none") == 0)
144 spr.defs.hisauth.proto = 0;
145 else
146 errx(EX_DATAERR, "bad auth proto: %s", cp);
147 } else if (startswith("hisauthname="))
148 strncpy(spr.defs.hisauth.name, argv[0] + off,
149 AUTHNAMELEN);
150 else if (startswith("hisauthsecret=") ||
151 startswith("hisauthkey="))
152 strncpy(spr.defs.hisauth.secret, argv[0] + off,
153 AUTHKEYLEN);
154 else if (strcmp(argv[0], "callin") == 0)
155 spr.defs.hisauth.flags |= AUTHFLAG_NOCALLOUT;
156 else if (strcmp(argv[0], "always") == 0)
157 spr.defs.hisauth.flags &= ~AUTHFLAG_NOCALLOUT;
158 else if (strcmp(argv[0], "norechallenge") == 0)
159 spr.defs.hisauth.flags |= AUTHFLAG_NORECHALLENGE;
160 else if (strcmp(argv[0], "rechallenge") == 0)
161 spr.defs.hisauth.flags &= ~AUTHFLAG_NORECHALLENGE;
162 else if (startswith("lcp-timeout=")) {
163 cp = argv[0] + off;
164 to = strtol(cp, &endp, 10);
165 if (*cp == '\0' || *endp != '\0' ||
167 * NB: 10 ms is the minimal possible value for
168 * hz=100. We assume no kernel has less clock
169 * frequency than that...
171 to < 10 || to > 20000)
172 errx(EX_DATAERR, "bad lcp timeout value: %s",
173 cp);
174 spr.defs.lcp.timeout = to;
175 } else if (strcmp(argv[0], "enable-vj") == 0)
176 spr.defs.enable_vj = 1;
177 else if (strcmp(argv[0], "disable-vj") == 0)
178 spr.defs.enable_vj = 0;
179 else if (strcmp(argv[0], "enable-ipv6") == 0)
180 spr.defs.enable_ipv6 = 1;
181 else if (strcmp(argv[0], "disable-ipv6") == 0)
182 spr.defs.enable_ipv6 = 0;
183 else
184 errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
186 argv++;
187 argc--;
190 spr.cmd = (int)SPPPIOSDEFS;
192 if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1)
193 err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)");
195 if (verbose)
196 print_vals(ifname, &spr);
198 return 0;
201 static void
202 usage(void)
204 fprintf(stderr, "%s\n%s\n",
205 "usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
206 " spppcontrol [-v] ifname callin|always");
207 exit(EX_USAGE);
210 void
211 print_vals(const char *ifname, struct spppreq *sp)
213 printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase));
214 if (sp->defs.myauth.proto) {
215 printf("\tmyauthproto=%s myauthname=\"%.*s\"\n",
216 proto_name(sp->defs.myauth.proto),
217 AUTHNAMELEN, sp->defs.myauth.name);
219 if (sp->defs.hisauth.proto) {
220 printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n",
221 proto_name(sp->defs.hisauth.proto),
222 AUTHNAMELEN, sp->defs.hisauth.name,
223 authflags(sp->defs.hisauth.flags));
225 printf("\tlcp-timeout=%d ms\n", sp->defs.lcp.timeout);
226 printf("\t%sable-vj\n", sp->defs.enable_vj? "en": "dis");
227 printf("\t%sable-ipv6\n", sp->defs.enable_ipv6? "en": "dis");
230 const char *
231 phase_name(enum ppp_phase phase)
233 switch (phase) {
234 case PHASE_DEAD: return "dead";
235 case PHASE_ESTABLISH: return "establish";
236 case PHASE_TERMINATE: return "terminate";
237 case PHASE_AUTHENTICATE: return "authenticate";
238 case PHASE_NETWORK: return "network";
240 return "illegal";
243 const char *
244 proto_name(u_short proto)
246 static char buf[12];
247 switch (proto) {
248 case PPP_PAP: return "pap";
249 case PPP_CHAP: return "chap";
251 sprintf(buf, "0x%x", (unsigned)proto);
252 return buf;
255 const char *
256 authflags(u_short flags)
258 static char buf[30];
259 buf[0] = '\0';
260 if (flags & AUTHFLAG_NOCALLOUT)
261 strcat(buf, " callin");
262 if (flags & AUTHFLAG_NORECHALLENGE)
263 strcat(buf, " norechallenge");
264 return buf;