pppoectl.c revision 1.3 1 /* $NetBSD: pppoectl.c,v 1.3 2002/01/06 20:23:55 martin Exp $ */
2
3 /*
4 * Copyright (c) 1997 Joerg Wunsch
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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.
27 *
28 * From: spppcontrol.c,v 1.3 1998/01/07 07:55:26 charnier Exp
29 * From: ispppcontrol
30 */
31
32 #include <sys/param.h>
33 #include <sys/callout.h>
34 #include <sys/ioctl.h>
35 #include <sys/mbuf.h>
36 #include <sys/socket.h>
37 #include <sys/time.h>
38 #include <sys/sysctl.h>
39 #include <net/if.h>
40 #include <net/if_sppp.h>
41 #include <net/if_pppoe.h>
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sysexits.h>
47 #include <unistd.h>
48
49 static void usage(void);
50 static void print_error(const char *ifname, int error, const char * str);
51 static void print_vals(const char *ifname, int phase, struct spppauthcfg *sp, int lcp_timeout, time_t idle_timeout);
52 const char *phase_name(int phase);
53 const char *proto_name(int proto);
54 const char *authflags(int flags);
55
56 int hz = 0;
57
58 int
59 main(int argc, char **argv)
60 {
61 int s, c;
62 int errs = 0, verbose = 0, dump = 0;
63 size_t off, len;
64 const char *ifname, *cp;
65 const char *eth_if_name, *access_concentrator, *service;
66 struct spppauthcfg spr;
67 struct sppplcpcfg lcp;
68 struct spppstatus status;
69 struct spppidletimeout timeout;
70 int mib[2];
71 int set_auth = 0, set_lcp = 0, set_idle_to = 0;
72 struct clockinfo clockinfo;
73
74 eth_if_name = NULL;
75 access_concentrator = NULL;
76 service = NULL;
77 while ((c = getopt(argc, argv, "vde:s:a:")) != -1)
78 switch (c) {
79 case 'v':
80 verbose++;
81 break;
82
83 case 'd':
84 dump++;
85 break;
86
87 case 'e':
88 eth_if_name = optarg;
89 break;
90
91 case 's':
92 service = optarg;
93 break;
94
95 case 'a':
96 access_concentrator = optarg;
97 break;
98
99 default:
100 errs++;
101 break;
102 }
103 argv += optind;
104 argc -= optind;
105
106 if (errs || argc < 1)
107 usage();
108
109 ifname = argv[0];
110
111 /* use a random AF to create the socket */
112 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
113 err(EX_UNAVAILABLE, "ifconfig: socket");
114
115 argc--;
116 argv++;
117
118 if (eth_if_name) {
119 struct pppoediscparms parms;
120 int e;
121
122 memset(&parms, 0, sizeof parms);
123 strncpy(parms.ifname, ifname, sizeof(parms.ifname));
124 strncpy(parms.eth_ifname, eth_if_name, sizeof(parms.eth_ifname));
125 if (access_concentrator) {
126 parms.ac_name = (char*)access_concentrator;
127 parms.ac_name_len = strlen(access_concentrator);
128 }
129 if (service) {
130 parms.service_name = (char*)service;
131 parms.service_name_len = strlen(service);
132 }
133
134 e = ioctl(s, PPPOESETPARMS, &parms);
135 if (e)
136 print_error(ifname, e, "PPPOESETPARMS");
137 return 0;
138 }
139
140 if (dump) {
141 /* dump PPPoE session state */
142 struct pppoeconnectionstate state;
143 int e;
144
145 memset(&state, 0, sizeof state);
146 strncpy(state.ifname, ifname, sizeof state.ifname);
147 e = ioctl(s, PPPOEGETSESSION, &state);
148 if (e)
149 print_error(ifname, e, "PPPOEGETSESSION,");
150
151 printf("%s:\tstate = ", ifname);
152 switch(state.state) {
153 case PPPOE_STATE_INITIAL:
154 printf("initial\n"); break;
155 case PPPOE_STATE_PADI_SENT:
156 printf("PADI sent\n"); break;
157 case PPPOE_STATE_PADR_SENT:
158 printf("PADR sent\n"); break;
159 case PPPOE_STATE_SESSION:
160 printf("session\n"); break;
161 case PPPOE_STATE_CLOSING:
162 printf("closing\n"); break;
163 }
164 printf("\tSession ID: 0x%x\n", state.session_id);
165 printf("\tPADI retries: %d\n", state.padi_retry_no);
166 printf("\tPADR retries: %d\n", state.padr_retry_no);
167
168 return 0;
169 }
170
171
172 memset(&spr, 0, sizeof spr);
173 strncpy(spr.ifname, ifname, sizeof spr.ifname);
174 memset(&lcp, 0, sizeof lcp);
175 strncpy(lcp.ifname, ifname, sizeof lcp.ifname);
176 memset(&status, 0, sizeof status);
177 strncpy(status.ifname, ifname, sizeof status.ifname);
178 memset(&timeout, 0, sizeof timeout);
179 strncpy(timeout.ifname, ifname, sizeof timeout.ifname);
180
181 mib[0] = CTL_KERN;
182 mib[1] = KERN_CLOCKRATE;
183 len = sizeof(clockinfo);
184 if(sysctl(mib, 2, &clockinfo, &len, NULL, 0) == -1)
185 {
186 fprintf(stderr, "error, cannot sysctl kern.clockrate!\n");
187 exit(1);
188 }
189
190 hz = clockinfo.hz;
191
192 if (argc == 0) {
193 /* list only mode */
194
195 /* first pass, get name lenghts */
196 if (ioctl(s, SPPPGETAUTHCFG, &spr) == -1)
197 err(EX_OSERR, "SPPPGETAUTHCFG");
198 /* now allocate buffers for strings */
199 if (spr.myname_length)
200 spr.myname = malloc(spr.myname_length);
201 if (spr.hisname_length)
202 spr.hisname = malloc(spr.hisname_length);
203 /* second pass: get names too */
204 if (ioctl(s, SPPPGETAUTHCFG, &spr) == -1)
205 err(EX_OSERR, "SPPPGETAUTHCFG");
206
207 if (ioctl(s, SPPPGETLCPCFG, &lcp) == -1)
208 err(EX_OSERR, "SPPPGETLCPCFG");
209 if (ioctl(s, SPPPGETSTATUS, &status) == -1)
210 err(EX_OSERR, "SPPPGETSTATUS");
211 if (ioctl(s, SPPPGETIDLETO, &timeout) == -1)
212 err(EX_OSERR, "SPPPGETIDLETO");
213
214 print_vals(ifname, status.phase, &spr, lcp.lcp_timeout, timeout.idle_seconds);
215
216 if (spr.hisname) free(spr.hisname);
217 if (spr.myname) free(spr.myname);
218 return 0;
219 }
220
221 #define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0
222
223 while (argc > 0) {
224 if (startswith("authproto=")) {
225 cp = argv[0] + off;
226 if (strcmp(cp, "pap") == 0)
227 spr.myauth =
228 spr.hisauth = SPPP_AUTHPROTO_PAP;
229 else if (strcmp(cp, "chap") == 0)
230 spr.myauth = spr.hisauth = SPPP_AUTHPROTO_CHAP;
231 else if (strcmp(cp, "none") == 0)
232 spr.myauth = spr.hisauth = SPPP_AUTHPROTO_NONE;
233 else
234 errx(EX_DATAERR, "bad auth proto: %s", cp);
235 set_auth = 1;
236 } else if (startswith("myauthproto=")) {
237 cp = argv[0] + off;
238 if (strcmp(cp, "pap") == 0)
239 spr.myauth = SPPP_AUTHPROTO_PAP;
240 else if (strcmp(cp, "chap") == 0)
241 spr.myauth = SPPP_AUTHPROTO_CHAP;
242 else if (strcmp(cp, "none") == 0)
243 spr.myauth = SPPP_AUTHPROTO_NONE;
244 else
245 errx(EX_DATAERR, "bad auth proto: %s", cp);
246 set_auth = 1;
247 } else if (startswith("myauthname=")) {
248 spr.myname = argv[0] + off;
249 spr.myname_length = strlen(spr.myname)+1;
250 set_auth = 1;
251 } else if (startswith("myauthsecret=") || startswith("myauthkey=")) {
252 spr.mysecret = argv[0] + off;
253 spr.mysecret_length = strlen(spr.mysecret)+1;
254 set_auth = 1;
255 } else if (startswith("hisauthproto=")) {
256 cp = argv[0] + off;
257 if (strcmp(cp, "pap") == 0)
258 spr.hisauth = SPPP_AUTHPROTO_PAP;
259 else if (strcmp(cp, "chap") == 0)
260 spr.hisauth = SPPP_AUTHPROTO_CHAP;
261 else if (strcmp(cp, "none") == 0)
262 spr.hisauth = SPPP_AUTHPROTO_NONE;
263 else
264 errx(EX_DATAERR, "bad auth proto: %s", cp);
265 set_auth = 1;
266 } else if (startswith("hisauthname=")) {
267 spr.hisname = argv[0] + off;
268 spr.hisname_length = strlen(spr.hisname)+1;
269 set_auth = 1;
270 } else if (startswith("hisauthsecret=") || startswith("hisauthkey=")) {
271 spr.hissecret = argv[0] + off;
272 spr.hissecret_length = strlen(spr.hissecret)+1;
273 set_auth = 1;
274 } else if (strcmp(argv[0], "callin") == 0)
275 spr.hisauthflags |= SPPP_AUTHFLAG_NOCALLOUT;
276 else if (strcmp(argv[0], "always") == 0)
277 spr.hisauthflags &= ~SPPP_AUTHFLAG_NOCALLOUT;
278 else if (strcmp(argv[0], "norechallenge") == 0)
279 spr.hisauthflags |= SPPP_AUTHFLAG_NORECHALLENGE;
280 else if (strcmp(argv[0], "rechallenge") == 0)
281 spr.hisauthflags &= ~SPPP_AUTHFLAG_NORECHALLENGE;
282 #ifndef __NetBSD__
283 else if (strcmp(argv[0], "enable-vj") == 0)
284 spr.defs.enable_vj = 1;
285 else if (strcmp(argv[0], "disable-vj") == 0)
286 spr.defs.enable_vj = 0;
287 #endif
288 else if (startswith("lcp-timeout=")) {
289 int timeout_arg = atoi(argv[0]+off);
290 if ((timeout_arg > 20000) || (timeout_arg <= 0))
291 errx(EX_DATAERR, "bad lcp timeout value: %s",
292 argv[0]+off);
293 lcp.lcp_timeout = timeout_arg * hz / 1000;
294 set_lcp = 1;
295 } else if (startswith("idle-timeout=")) {
296 timeout.idle_seconds = atol(argv[0]+off);
297 set_idle_to = 1;
298 } else
299 errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
300
301 argv++;
302 argc--;
303 }
304
305 if (set_auth) {
306 if (ioctl(s, SPPPSETAUTHCFG, &spr) == -1)
307 err(EX_OSERR, "SPPPSETAUTHCFG");
308 }
309 if (set_lcp) {
310 if (ioctl(s, SPPPSETLCPCFG, &lcp) == -1)
311 err(EX_OSERR, "SPPPSETLCPCFG");
312 }
313 if (set_idle_to) {
314 if (ioctl(s, SPPPSETIDLETO, &timeout) == -1)
315 err(EX_OSERR, "SPPPSETIDLETO");
316 }
317
318 if (verbose)
319 print_vals(ifname, status.phase, &spr, lcp.lcp_timeout, timeout.idle_seconds);
320
321 return 0;
322 }
323
324 static void
325 usage(void)
326 {
327 fprintf(stderr, "%s\n%s\n",
328 "usage: ispppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
329 " ispppcontrol [-v] ifname callin|always");
330 exit(EX_USAGE);
331 }
332
333 static void
334 print_vals(const char *ifname, int phase, struct spppauthcfg *sp, int lcp_timeout, time_t idle_timeout)
335 {
336 #ifndef __NetBSD__
337 time_t send, recv;
338 #endif
339
340 printf("%s:\tphase=%s\n", ifname, phase_name(phase));
341 if (sp->myauth) {
342 printf("\tmyauthproto=%s myauthname=\"%s\"\n",
343 proto_name(sp->myauth),
344 sp->myname);
345 }
346 if (sp->hisauth) {
347 printf("\thisauthproto=%s hisauthname=\"%s\"%s\n",
348 proto_name(sp->hisauth),
349 sp->hisname,
350 authflags(sp->hisauthflags));
351 }
352 #ifndef __NetBSD__
353 if (sp->defs.pp_phase > PHASE_DEAD) {
354 send = time(NULL) - sp->defs.pp_last_sent;
355 recv = time(NULL) - sp->defs.pp_last_recv;
356 printf("\tidle_time=%ld\n", (send<recv)? send : recv);
357 }
358 #endif
359
360 printf("\tlcp timeout: %.3f s\n",
361 (double)lcp_timeout / hz);
362
363 if (idle_timeout != 0)
364 printf("\tidle timeout = %lu s\n", (unsigned long)idle_timeout);
365 else
366 printf("\tidle timeout = disabled\n");
367
368 #ifndef __NetBSD__
369 printf("\tenable_vj: %s\n",
370 sp->defs.enable_vj ? "on" : "off");
371 #endif
372 }
373
374 const char *
375 phase_name(int phase)
376 {
377 switch (phase) {
378 case SPPP_PHASE_DEAD: return "dead";
379 case SPPP_PHASE_ESTABLISH: return "establish";
380 case SPPP_PHASE_TERMINATE: return "terminate";
381 case SPPP_PHASE_AUTHENTICATE: return "authenticate";
382 case SPPP_PHASE_NETWORK: return "network";
383 }
384 return "illegal";
385 }
386
387 const char *
388 proto_name(int proto)
389 {
390 static char buf[12];
391 switch (proto) {
392 case SPPP_AUTHPROTO_PAP: return "pap";
393 case SPPP_AUTHPROTO_CHAP: return "chap";
394 case SPPP_AUTHPROTO_NONE: return "none";
395 }
396 sprintf(buf, "0x%x", (unsigned)proto);
397 return buf;
398 }
399
400 const char *
401 authflags(int flags)
402 {
403 static char buf[32];
404 buf[0] = '\0';
405 if (flags & SPPP_AUTHFLAG_NOCALLOUT)
406 strcat(buf, " callin");
407 if (flags & SPPP_AUTHFLAG_NORECHALLENGE)
408 strcat(buf, " norechallenge");
409 return buf;
410 }
411
412 static void
413 print_error(const char *ifname, int error, const char * str)
414 {
415 if (error == -1)
416 fprintf(stderr, "%s: interface not found\n", ifname);
417 else
418 fprintf(stderr, "%s: %s: %s\n", ifname, str, strerror(error));
419 exit(EX_DATAERR);
420 }
421
422
423