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.
38 #include <sys/types.h>
50 static int setcbcp (char **);
52 static option_t cbcp_option_list[] = {
53 { "callback", o_special, (void *)setcbcp,
54 "Ask for callback", OPT_PRIO | OPT_A2STRVAL, &cbcp[0].us_number },
59 * Protocol entry points.
61 static void cbcp_init (int unit);
62 static void cbcp_open (int unit);
63 static void cbcp_lowerup (int unit);
64 static void cbcp_input (int unit, u_char *pkt, int len);
65 static void cbcp_protrej (int unit);
66 static int cbcp_printpkt (u_char *pkt, int len,
67 void (*printer)(void *, char *, ...),
70 struct protent cbcp_protent = {
90 cbcp_state cbcp[NUM_PPP];
92 /* internal prototypes */
94 static void cbcp_recvreq (cbcp_state *us, u_char *pckt, int len);
95 static void cbcp_resp (cbcp_state *us);
96 static void cbcp_up (cbcp_state *us);
97 static void cbcp_recvack (cbcp_state *us, u_char *pckt, int len);
98 static void cbcp_send (cbcp_state *us, int code, u_char *buf, int len);
100 /* option processing */
104 lcp_wantoptions[0].neg_cbcp = 1;
105 cbcp_protent.enabled_flag = 1;
106 cbcp[0].us_number = strdup(*argv);
107 if (cbcp[0].us_number == 0)
108 novm("callback number");
109 cbcp[0].us_type |= (1 << CB_CONF_USER);
110 cbcp[0].us_type |= (1 << CB_CONF_ADMIN);
121 memset(us, 0, sizeof(cbcp_state));
123 us->us_type |= (1 << CB_CONF_NO);
126 /* lower layer is up */
128 cbcp_lowerup(int iface)
130 cbcp_state *us = &cbcp[iface];
132 dbglog("cbcp_lowerup");
133 dbglog("want: %d", us->us_type);
135 if (us->us_type == CB_CONF_USER)
136 dbglog("phone no: %s", us->us_number);
145 /* process an incomming packet */
147 cbcp_input(int unit, u_char *inpacket, int pktlen)
153 cbcp_state *us = &cbcp[unit];
157 if (pktlen < CBCP_MINLEN) {
159 dbglog("CBCP packet is too small");
167 if (len > pktlen || len < CBCP_MINLEN) {
169 dbglog("CBCP packet: invalid length %d", len);
178 cbcp_recvreq(us, inp, len);
183 dbglog("CBCP_RESP received");
187 if (debug && id != us->us_id)
188 dbglog("id doesn't match: expected %d recv %d",
191 cbcp_recvack(us, inp, len);
199 /* protocol was rejected by foe */
200 void cbcp_protrej(int iface)
204 char *cbcp_codenames[] = {
205 "Request", "Response", "Ack"
208 char *cbcp_optionnames[] = {
215 /* pretty print a packet */
217 cbcp_printpkt(u_char *p, int plen,
218 void (*printer) (void *, char *, ...), void *arg)
220 int code, opt, id, len, olen, delay;
223 if (plen < HEADERLEN)
229 if (len < HEADERLEN || len > plen)
232 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
233 printer(arg, " %s", cbcp_codenames[code-1]);
235 printer(arg, " code=0x%x", code);
237 printer(arg, " id=0x%x", id);
248 if (olen < 2 || olen > len) {
255 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
256 printer(arg, " %s", cbcp_optionnames[opt-1]);
258 printer(arg, " option=0x%x", opt);
262 printer(arg, " delay = %d", delay);
270 memcpy(str, p, olen - 4);
272 printer(arg, " number = %s", str);
282 for (; len > 0; --len) {
284 printer(arg, " %.2x", code);
290 /* received CBCP request */
292 cbcp_recvreq(cbcp_state *us, u_char *pckt, int pcktlen)
294 u_char type, opt_len, delay, addr_type;
301 dbglog("length: %d", len);
304 GETCHAR(opt_len, pckt);
305 if (opt_len < 2 || opt_len > len)
309 GETCHAR(delay, pckt);
311 us->us_allowed |= (1 << type);
315 dbglog("no callback allowed");
319 dbglog("user callback allowed");
321 GETCHAR(addr_type, pckt);
322 memcpy(address, pckt, opt_len - 4);
323 address[opt_len - 4] = 0;
325 dbglog("address: %s", address);
330 dbglog("user admin defined allowed");
340 dbglog("cbcp_recvreq: malformed packet (%d bytes left)", len);
348 cbcp_resp(cbcp_state *us)
356 cb_type = us->us_allowed & us->us_type;
357 dbglog("cbcp_resp cb_type=%d", cb_type);
361 lcp_down(us->us_unit);
364 if (cb_type & ( 1 << CB_CONF_USER ) ) {
365 dbglog("cbcp_resp CONF_USER");
366 slen = strlen(us->us_number);
368 warn("callback number truncated to 250 characters");
371 PUTCHAR(CB_CONF_USER, bufp);
372 len = 3 + 1 + slen + 1;
374 PUTCHAR(5, bufp); /* delay */
376 BCOPY(us->us_number, bufp, slen + 1);
377 cbcp_send(us, CBCP_RESP, buf, len);
381 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
382 dbglog("cbcp_resp CONF_ADMIN");
383 PUTCHAR(CB_CONF_ADMIN, bufp);
386 PUTCHAR(5, bufp); /* delay */
387 cbcp_send(us, CBCP_RESP, buf, len);
391 if (cb_type & ( 1 << CB_CONF_NO ) ) {
392 dbglog("cbcp_resp CONF_NO");
393 PUTCHAR(CB_CONF_NO, bufp);
396 cbcp_send(us, CBCP_RESP, buf, len);
397 start_networks(us->us_unit);
403 cbcp_send(cbcp_state *us, int code, u_char *buf, int len)
408 outp = outpacket_buf;
412 MAKEHEADER(outp, PPP_CBCP);
415 PUTCHAR(us->us_id, outp);
416 PUTSHORT(outlen, outp);
419 BCOPY(buf, outp, len);
421 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
425 cbcp_recvack(cbcp_state *us, u_char *pckt, int len)
427 u_char type, delay, addr_type;
433 GETCHAR(opt_len, pckt);
434 if (opt_len >= 2 && opt_len <= len) {
437 GETCHAR(delay, pckt);
440 GETCHAR(addr_type, pckt);
441 memcpy(address, pckt, opt_len - 4);
442 address[opt_len - 4] = 0;
444 dbglog("peer will call: %s", address);
446 if (type == CB_CONF_NO)
452 dbglog("cbcp_recvack: malformed packet");
456 /* ok peer will do callback */
458 cbcp_up(cbcp_state *us)
461 status = EXIT_CALLBACK;
462 lcp_close(0, "Call me back, please");