adds a couple print_vmcb()s for the write CR0 -> shutdown bug
[freebsd-src/fkvm-freebsd.git] / usr.sbin / pppd / cbcp.c
blob81a6f411b242810418eda2e6532c563170fac2be
1 /*
2 * cbcp - Call Back Configuration Protocol.
4 * Copyright (c) 1995 Pedro Roque Marques
5 * All rights reserved.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Pedro Roque Marques. The name of the author may not be used to
13 * endorse or promote products derived from this software without
14 * specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #ifndef lint
22 static char rcsid[] = "$FreeBSD$";
23 #endif
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/time.h>
29 #include <syslog.h>
31 #include "pppd.h"
32 #include "cbcp.h"
33 #include "fsm.h"
34 #include "lcp.h"
35 #include "ipcp.h"
38 * Protocol entry points.
40 static void cbcp_init(int unit);
41 static void cbcp_open(int unit);
42 static void cbcp_lowerup(int unit);
43 static void cbcp_input(int unit, u_char *pkt, int len);
44 static void cbcp_protrej(int unit);
45 static int cbcp_printpkt(u_char *pkt, int len,
46 void (*printer)(void *, char *, ...),
47 void *arg);
49 struct protent cbcp_protent = {
50 PPP_CBCP,
51 cbcp_init,
52 cbcp_input,
53 cbcp_protrej,
54 cbcp_lowerup,
55 NULL,
56 cbcp_open,
57 NULL,
58 cbcp_printpkt,
59 NULL,
61 "CBCP",
62 NULL,
63 NULL,
64 NULL
67 cbcp_state cbcp[NUM_PPP];
69 /* internal prototypes */
71 static void cbcp_recvreq(cbcp_state *us, char *pckt, int len);
72 static void cbcp_resp(cbcp_state *us);
73 static void cbcp_up(cbcp_state *us);
74 static void cbcp_recvack(cbcp_state *us, char *pckt, int len);
75 static void cbcp_send(cbcp_state *us, u_char code, u_char *buf, int len);
77 /* init state */
78 static void
79 cbcp_init(iface)
80 int iface;
82 cbcp_state *us;
84 us = &cbcp[iface];
85 memset(us, 0, sizeof(cbcp_state));
86 us->us_unit = iface;
87 us->us_type |= (1 << CB_CONF_NO);
90 /* lower layer is up */
91 static void
92 cbcp_lowerup(iface)
93 int iface;
95 cbcp_state *us = &cbcp[iface];
97 syslog(LOG_DEBUG, "cbcp_lowerup");
98 syslog(LOG_DEBUG, "want: %d", us->us_type);
100 if (us->us_type == CB_CONF_USER)
101 syslog(LOG_DEBUG, "phone no: %s", us->us_number);
104 static void
105 cbcp_open(unit)
106 int unit;
108 syslog(LOG_DEBUG, "cbcp_open");
111 /* process an incomming packet */
112 static void
113 cbcp_input(unit, inpacket, pktlen)
114 int unit;
115 u_char *inpacket;
116 int pktlen;
118 u_char *inp;
119 u_char code, id;
120 u_short len;
122 cbcp_state *us = &cbcp[unit];
124 inp = inpacket;
126 if (pktlen < CBCP_MINLEN) {
127 syslog(LOG_ERR, "CBCP packet is too small");
128 return;
131 GETCHAR(code, inp);
132 GETCHAR(id, inp);
133 GETSHORT(len, inp);
135 if (len < CBCP_MINLEN || len > pktlen) {
136 syslog(LOG_ERR, "CBCP packet: invalid length");
137 return;
140 len -= CBCP_MINLEN;
142 switch(code) {
143 case CBCP_REQ:
144 us->us_id = id;
145 cbcp_recvreq(us, inp, len);
146 break;
148 case CBCP_RESP:
149 syslog(LOG_DEBUG, "CBCP_RESP received");
150 break;
152 case CBCP_ACK:
153 if (id != us->us_id)
154 syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
155 us->us_id, id);
157 cbcp_recvack(us, inp, len);
158 break;
160 default:
161 break;
165 /* protocol was rejected by foe */
166 void cbcp_protrej(int iface)
170 char *cbcp_codenames[] = {
171 "Request", "Response", "Ack"
174 char *cbcp_optionnames[] = {
175 "NoCallback",
176 "UserDefined",
177 "AdminDefined",
178 "List"
181 /* pretty print a packet */
182 static int
183 cbcp_printpkt(p, plen, printer, arg)
184 u_char *p;
185 int plen;
186 void (*printer)(void *, char *, ...);
187 void *arg;
189 int code, opt, id, len, olen, delay;
190 u_char *pstart;
192 if (plen < HEADERLEN)
193 return 0;
194 pstart = p;
195 GETCHAR(code, p);
196 GETCHAR(id, p);
197 GETSHORT(len, p);
198 if (len < HEADERLEN || len > plen)
199 return 0;
201 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
202 printer(arg, " %s", cbcp_codenames[code-1]);
203 else
204 printer(arg, " code=0x%x", code);
206 printer(arg, " id=0x%x", id);
207 len -= HEADERLEN;
209 switch (code) {
210 case CBCP_REQ:
211 case CBCP_RESP:
212 case CBCP_ACK:
213 while(len >= 2) {
214 GETCHAR(opt, p);
215 GETCHAR(olen, p);
217 if (olen < 2 || olen > len) {
218 break;
221 printer(arg, " <");
222 len -= olen;
224 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
225 printer(arg, " %s", cbcp_optionnames[opt-1]);
226 else
227 printer(arg, " option=0x%x", opt);
229 if (olen > 2) {
230 GETCHAR(delay, p);
231 printer(arg, " delay = %d", delay);
234 if (olen > 3) {
235 int addrt;
236 char str[256];
238 GETCHAR(addrt, p);
239 memcpy(str, p, olen - 4);
240 str[olen - 4] = 0;
241 printer(arg, " number = %s", str);
243 printer(arg, ">");
244 break;
247 default:
248 break;
251 for (; len > 0; --len) {
252 GETCHAR(code, p);
253 printer(arg, " %.2x", code);
256 return p - pstart;
259 /* received CBCP request */
260 static void
261 cbcp_recvreq(us, pckt, pcktlen)
262 cbcp_state *us;
263 char *pckt;
264 int pcktlen;
266 u_char type, opt_len, delay, addr_type;
267 char address[256];
268 int len = pcktlen;
270 address[0] = 0;
272 while (len > 1) {
273 syslog(LOG_DEBUG, "length: %d", len);
275 GETCHAR(type, pckt);
276 GETCHAR(opt_len, pckt);
278 if (len < opt_len)
279 break;
280 len -= opt_len;
282 if (opt_len > 2)
283 GETCHAR(delay, pckt);
285 us->us_allowed |= (1 << type);
287 switch(type) {
288 case CB_CONF_NO:
289 syslog(LOG_DEBUG, "no callback allowed");
290 break;
292 case CB_CONF_USER:
293 syslog(LOG_DEBUG, "user callback allowed");
294 if (opt_len > 4) {
295 GETCHAR(addr_type, pckt);
296 memcpy(address, pckt, opt_len - 4);
297 address[opt_len - 4] = 0;
298 if (address[0])
299 syslog(LOG_DEBUG, "address: %s", address);
301 break;
303 case CB_CONF_ADMIN:
304 syslog(LOG_DEBUG, "user admin defined allowed");
305 break;
307 case CB_CONF_LIST:
308 break;
312 cbcp_resp(us);
315 static void
316 cbcp_resp(us)
317 cbcp_state *us;
319 u_char cb_type;
320 u_char buf[256];
321 u_char *bufp = buf;
322 int len = 0;
324 cb_type = us->us_allowed & us->us_type;
325 syslog(LOG_DEBUG, "cbcp_resp cb_type=%d", cb_type);
327 #if 0
328 if (!cb_type)
329 lcp_down(us->us_unit);
330 #endif
332 if (cb_type & ( 1 << CB_CONF_USER ) ) {
333 syslog(LOG_DEBUG, "cbcp_resp CONF_USER");
334 PUTCHAR(CB_CONF_USER, bufp);
335 len = 3 + 1 + strlen(us->us_number) + 1;
336 PUTCHAR(len , bufp);
337 PUTCHAR(5, bufp); /* delay */
338 PUTCHAR(1, bufp);
339 BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
340 cbcp_send(us, CBCP_RESP, buf, len);
341 return;
344 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
345 syslog(LOG_DEBUG, "cbcp_resp CONF_ADMIN");
346 PUTCHAR(CB_CONF_ADMIN, bufp);
347 len = 3;
348 PUTCHAR(len, bufp);
349 PUTCHAR(5, bufp); /* delay */
350 cbcp_send(us, CBCP_RESP, buf, len);
351 return;
354 if (cb_type & ( 1 << CB_CONF_NO ) ) {
355 syslog(LOG_DEBUG, "cbcp_resp CONF_NO");
356 PUTCHAR(CB_CONF_NO, bufp);
357 len = 2;
358 PUTCHAR(len , bufp);
359 cbcp_send(us, CBCP_RESP, buf, len);
360 (*ipcp_protent.open)(us->us_unit);
361 return;
365 static void
366 cbcp_send(us, code, buf, len)
367 cbcp_state *us;
368 u_char code;
369 u_char *buf;
370 int len;
372 u_char *outp;
373 int outlen;
375 outp = outpacket_buf;
377 outlen = 4 + len;
379 MAKEHEADER(outp, PPP_CBCP);
381 PUTCHAR(code, outp);
382 PUTCHAR(us->us_id, outp);
383 PUTSHORT(outlen, outp);
385 if (len)
386 BCOPY(buf, outp, len);
388 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
391 static void
392 cbcp_recvack(us, pckt, len)
393 cbcp_state *us;
394 char *pckt;
395 int len;
397 u_char type, delay, addr_type;
398 int opt_len;
399 char address[256];
401 if (len > 1) {
402 GETCHAR(type, pckt);
403 GETCHAR(opt_len, pckt);
405 if (opt_len > len)
406 return;
408 if (opt_len > 2)
409 GETCHAR(delay, pckt);
411 if (opt_len > 4) {
412 GETCHAR(addr_type, pckt);
413 memcpy(address, pckt, opt_len - 4);
414 address[opt_len - 4] = 0;
415 if (address[0])
416 syslog(LOG_DEBUG, "peer will call: %s", address);
420 cbcp_up(us);
423 extern int persist;
425 /* ok peer will do callback */
426 static void
427 cbcp_up(us)
428 cbcp_state *us;
430 persist = 0;
431 lcp_close(0, "Call me back, please");