dhcpcd: update README.DRAGONFLY
[dragonfly.git] / sbin / spppcontrol / spppcontrol.c
blobadd6156d1021f4a1c699fefd67baa2fb3b5b05a8
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 $
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
33 #include <net/if.h>
34 #include <net/if_var.h>
35 #include <net/sppp/if_sppp.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sysexits.h>
42 #include <unistd.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
53 int
54 main(int argc, char **argv)
56 int s, c;
57 int errs = 0, verbose = 0;
58 size_t off;
59 long to;
60 char *endp;
61 const char *ifname, *cp;
62 struct ifreq ifr;
63 struct spppreq spr;
65 while ((c = getopt(argc, argv, "v")) != -1)
66 switch (c) {
67 case 'v':
68 verbose++;
69 break;
71 default:
72 errs++;
73 break;
75 argv += optind;
76 argc -= optind;
78 if (errs || argc < 1)
79 usage();
81 ifname = argv[0];
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");
88 argc--;
89 argv++;
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)");
97 if (argc == 0) {
98 /* list only mode */
99 print_vals(ifname, &spr);
100 return 0;
103 #define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0
105 while (argc > 0) {
106 if (startswith("authproto=")) {
107 cp = argv[0] + off;
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;
117 else
118 errx(EX_DATAERR, "bad auth proto: %s", cp);
119 } else if (startswith("myauthproto=")) {
120 cp = argv[0] + off;
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;
127 else
128 errx(EX_DATAERR, "bad auth proto: %s", cp);
129 } else if (startswith("myauthname="))
130 strncpy(spr.defs.myauth.name, argv[0] + off,
131 AUTHNAMELEN);
132 else if (startswith("myauthsecret=") ||
133 startswith("myauthkey="))
134 strncpy(spr.defs.myauth.secret, argv[0] + off,
135 AUTHKEYLEN);
136 else if (startswith("hisauthproto=")) {
137 cp = argv[0] + off;
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;
144 else
145 errx(EX_DATAERR, "bad auth proto: %s", cp);
146 } else if (startswith("hisauthname="))
147 strncpy(spr.defs.hisauth.name, argv[0] + off,
148 AUTHNAMELEN);
149 else if (startswith("hisauthsecret=") ||
150 startswith("hisauthkey="))
151 strncpy(spr.defs.hisauth.secret, argv[0] + off,
152 AUTHKEYLEN);
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=")) {
162 cp = argv[0] + off;
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",
172 cp);
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;
182 else
183 errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
185 argv++;
186 argc--;
189 spr.cmd = (uintptr_t)SPPPIOSDEFS;
191 if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1)
192 err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)");
194 if (verbose)
195 print_vals(ifname, &spr);
197 return 0;
200 static void
201 usage(void)
203 fprintf(stderr, "%s\n%s\n",
204 "usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
205 " spppcontrol [-v] ifname callin|always");
206 exit(EX_USAGE);
209 void
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");
229 const char *
230 phase_name(enum ppp_phase phase)
232 switch (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";
239 return "illegal";
242 const char *
243 proto_name(u_short proto)
245 static char buf[12];
246 switch (proto) {
247 case PPP_PAP: return "pap";
248 case PPP_CHAP: return "chap";
250 sprintf(buf, "0x%x", (unsigned)proto);
251 return buf;
254 const char *
255 authflags(u_short flags)
257 static char buf[30];
258 buf[0] = '\0';
259 if (flags & AUTHFLAG_NOCALLOUT)
260 strcat(buf, " callin");
261 if (flags & AUTHFLAG_NORECHALLENGE)
262 strcat(buf, " norechallenge");
263 return buf;