2 * cbcp - Call Back Configuration Protocol.
4 * Copyright (c) 1995 Pedro Roque Marques. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * 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
15 * the documentation and/or other materials provided with the
18 * 3. The names of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
22 * 4. Redistributions of any form whatsoever must retain the following
24 * "This product includes software developed by Pedro Roque Marques
25 * <pedro_m@yahoo.com>"
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 #define RCSID "$Id: cbcp.c,v 1.15 2003/01/17 07:23:35 fcusack Exp $"
40 #include <sys/types.h>
48 static const char rcsid[] = RCSID;
53 static int setcbcp __P((char **));
55 static option_t cbcp_option_list[] = {
56 { "callback", o_special, (void *)setcbcp,
57 "Ask for callback", OPT_PRIO | OPT_A2STRVAL, &cbcp[0].us_number },
62 * Protocol entry points.
64 static void cbcp_init __P((int unit));
65 static void cbcp_open __P((int unit));
66 static void cbcp_lowerup __P((int unit));
67 static void cbcp_input __P((int unit, u_char *pkt, int len));
68 static void cbcp_protrej __P((int unit));
69 static int cbcp_printpkt __P((u_char *pkt, int len,
70 void (*printer) __P((void *, char *, ...)),
73 struct protent cbcp_protent = {
93 cbcp_state cbcp[NUM_PPP];
95 /* internal prototypes */
97 static void cbcp_recvreq __P((cbcp_state *us, u_char *pckt, int len));
98 static void cbcp_resp __P((cbcp_state *us));
99 static void cbcp_up __P((cbcp_state *us));
100 static void cbcp_recvack __P((cbcp_state *us, u_char *pckt, int len));
101 static void cbcp_send __P((cbcp_state *us, int code, u_char *buf, int len));
103 /* option processing */
108 lcp_wantoptions[0].neg_cbcp = 1;
109 cbcp_protent.enabled_flag = 1;
110 cbcp[0].us_number = strdup(*argv);
111 if (cbcp[0].us_number == 0)
112 novm("callback number");
113 cbcp[0].us_type |= (1 << CB_CONF_USER);
114 cbcp[0].us_type |= (1 << CB_CONF_ADMIN);
126 memset(us, 0, sizeof(cbcp_state));
128 us->us_type |= (1 << CB_CONF_NO);
131 /* lower layer is up */
136 cbcp_state *us = &cbcp[iface];
138 dbglog("cbcp_lowerup");
139 dbglog("want: %d", us->us_type);
141 if (us->us_type == CB_CONF_USER)
142 dbglog("phone no: %s", us->us_number);
152 /* process an incomming packet */
154 cbcp_input(unit, inpacket, pktlen)
163 cbcp_state *us = &cbcp[unit];
167 if (pktlen < CBCP_MINLEN) {
168 error("CBCP packet is too small");
178 error("CBCP packet: invalid length");
188 cbcp_recvreq(us, inp, len);
192 dbglog("CBCP_RESP received");
197 dbglog("id doesn't match: expected %d recv %d",
200 cbcp_recvack(us, inp, len);
208 /* protocol was rejected by foe */
209 void cbcp_protrej(int iface)
213 char *cbcp_codenames[] = {
214 "Request", "Response", "Ack"
217 char *cbcp_optionnames[] = {
224 /* pretty print a packet */
226 cbcp_printpkt(p, plen, printer, arg)
229 void (*printer) __P((void *, char *, ...));
232 int code, opt, id, len, olen, delay;
235 if (plen < HEADERLEN)
241 if (len < HEADERLEN || len > plen)
244 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
245 printer(arg, " %s", cbcp_codenames[code-1]);
247 printer(arg, " code=0x%x", code);
249 printer(arg, " id=0x%x", id);
260 if (olen < 2 || olen > len) {
267 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
268 printer(arg, " %s", cbcp_optionnames[opt-1]);
270 printer(arg, " option=0x%x", opt);
274 printer(arg, " delay = %d", delay);
282 memcpy(str, p, olen - 4);
284 printer(arg, " number = %s", str);
294 for (; len > 0; --len) {
296 printer(arg, " %.2x", code);
302 /* received CBCP request */
304 cbcp_recvreq(us, pckt, pcktlen)
309 u_char type, opt_len, delay, addr_type;
316 dbglog("length: %d", len);
319 GETCHAR(opt_len, pckt);
322 GETCHAR(delay, pckt);
324 us->us_allowed |= (1 << type);
328 dbglog("no callback allowed");
332 dbglog("user callback allowed");
334 GETCHAR(addr_type, pckt);
335 memcpy(address, pckt, opt_len - 4);
336 address[opt_len - 4] = 0;
338 dbglog("address: %s", address);
343 dbglog("user admin defined allowed");
364 cb_type = us->us_allowed & us->us_type;
365 dbglog("cbcp_resp cb_type=%d", cb_type);
369 lcp_down(us->us_unit);
372 if (cb_type & ( 1 << CB_CONF_USER ) ) {
373 dbglog("cbcp_resp CONF_USER");
374 PUTCHAR(CB_CONF_USER, bufp);
375 len = 3 + 1 + strlen(us->us_number) + 1;
377 PUTCHAR(5, bufp); /* delay */
379 BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
380 cbcp_send(us, CBCP_RESP, buf, len);
384 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
385 dbglog("cbcp_resp CONF_ADMIN");
386 PUTCHAR(CB_CONF_ADMIN, bufp);
389 PUTCHAR(5, bufp); /* delay */
390 cbcp_send(us, CBCP_RESP, buf, len);
394 if (cb_type & ( 1 << CB_CONF_NO ) ) {
395 dbglog("cbcp_resp CONF_NO");
396 PUTCHAR(CB_CONF_NO, bufp);
399 cbcp_send(us, CBCP_RESP, buf, len);
400 start_networks(us->us_unit);
406 cbcp_send(us, code, buf, len)
415 outp = outpacket_buf;
419 MAKEHEADER(outp, PPP_CBCP);
422 PUTCHAR(us->us_id, outp);
423 PUTSHORT(outlen, outp);
426 BCOPY(buf, outp, len);
428 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
432 cbcp_recvack(us, pckt, len)
437 u_char type, delay, addr_type;
443 GETCHAR(opt_len, pckt);
446 GETCHAR(delay, pckt);
449 GETCHAR(addr_type, pckt);
450 memcpy(address, pckt, opt_len - 4);
451 address[opt_len - 4] = 0;
453 dbglog("peer will call: %s", address);
455 if (type == CB_CONF_NO)
462 /* ok peer will do callback */
468 lcp_close(0, "Call me back, please");
469 status = EXIT_CALLBACK;