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.
41 #include <sys/types.h>
44 #include "pppd-private.h"
54 static int setcbcp (char **);
56 static struct option cbcp_option_list[] = {
57 { "callback", o_special, (void *)setcbcp,
58 "Ask for callback", OPT_PRIO | OPT_A2STRVAL, &cbcp[0].us_number },
63 * Protocol entry points.
65 static void cbcp_init (int unit);
66 static void cbcp_open (int unit);
67 static void cbcp_lowerup (int unit);
68 static void cbcp_input (int unit, u_char *pkt, int len);
69 static void cbcp_protrej (int unit);
70 static int cbcp_printpkt (u_char *pkt, int len,
71 void (*printer)(void *, char *, ...),
74 struct protent cbcp_protent = {
94 cbcp_state cbcp[NUM_PPP];
96 /* internal prototypes */
98 static void cbcp_recvreq (cbcp_state *us, u_char *pckt, int len);
99 static void cbcp_resp (cbcp_state *us);
100 static void cbcp_up (cbcp_state *us);
101 static void cbcp_recvack (cbcp_state *us, u_char *pckt, int len);
102 static void cbcp_send (cbcp_state *us, int code, u_char *buf, int len);
104 /* 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);
125 memset(us, 0, sizeof(cbcp_state));
127 us->us_type |= (1 << CB_CONF_NO);
130 /* lower layer is up */
132 cbcp_lowerup(int iface)
134 cbcp_state *us = &cbcp[iface];
136 dbglog("cbcp_lowerup");
137 dbglog("want: %d", us->us_type);
139 if (us->us_type == CB_CONF_USER)
140 dbglog("phone no: %s", us->us_number);
149 /* process an incomming packet */
151 cbcp_input(int unit, u_char *inpacket, int pktlen)
157 cbcp_state *us = &cbcp[unit];
161 if (pktlen < CBCP_MINLEN) {
163 dbglog("CBCP packet is too small");
171 if (len > pktlen || len < CBCP_MINLEN) {
173 dbglog("CBCP packet: invalid length %d", len);
182 cbcp_recvreq(us, inp, len);
187 dbglog("CBCP_RESP received");
191 if (debug && id != us->us_id)
192 dbglog("id doesn't match: expected %d recv %d",
195 cbcp_recvack(us, inp, len);
203 /* protocol was rejected by foe */
204 void cbcp_protrej(int iface)
208 char *cbcp_codenames[] = {
209 "Request", "Response", "Ack"
212 char *cbcp_optionnames[] = {
219 /* pretty print a packet */
221 cbcp_printpkt(u_char *p, int plen,
222 void (*printer) (void *, char *, ...), void *arg)
224 int code, opt, id, len, olen, delay;
227 if (plen < HEADERLEN)
233 if (len < HEADERLEN || len > plen)
236 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
237 printer(arg, " %s", cbcp_codenames[code-1]);
239 printer(arg, " code=0x%x", code);
241 printer(arg, " id=0x%x", id);
252 if (olen < 2 || olen > len) {
259 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
260 printer(arg, " %s", cbcp_optionnames[opt-1]);
262 printer(arg, " option=0x%x", opt);
266 printer(arg, " delay = %d", delay);
274 memcpy(str, p, olen - 4);
276 printer(arg, " number = %s", str);
286 for (; len > 0; --len) {
288 printer(arg, " %.2x", code);
294 /* received CBCP request */
296 cbcp_recvreq(cbcp_state *us, u_char *pckt, int pcktlen)
298 u_char type, opt_len, delay, addr_type;
305 dbglog("length: %d", len);
308 GETCHAR(opt_len, pckt);
309 if (opt_len < 2 || opt_len > len)
313 GETCHAR(delay, pckt);
315 us->us_allowed |= (1 << type);
319 dbglog("no callback allowed");
323 dbglog("user callback allowed");
325 GETCHAR(addr_type, pckt);
326 memcpy(address, pckt, opt_len - 4);
327 address[opt_len - 4] = 0;
329 dbglog("address: %s", address);
334 dbglog("user admin defined allowed");
344 dbglog("cbcp_recvreq: malformed packet (%d bytes left)", len);
352 cbcp_resp(cbcp_state *us)
360 cb_type = us->us_allowed & us->us_type;
361 dbglog("cbcp_resp cb_type=%d", cb_type);
365 lcp_down(us->us_unit);
368 if (cb_type & ( 1 << CB_CONF_USER ) ) {
369 dbglog("cbcp_resp CONF_USER");
370 slen = strlen(us->us_number);
372 warn("callback number truncated to 250 characters");
375 PUTCHAR(CB_CONF_USER, bufp);
376 len = 3 + 1 + slen + 1;
378 PUTCHAR(5, bufp); /* delay */
380 BCOPY(us->us_number, bufp, slen + 1);
381 cbcp_send(us, CBCP_RESP, buf, len);
385 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
386 dbglog("cbcp_resp CONF_ADMIN");
387 PUTCHAR(CB_CONF_ADMIN, bufp);
390 PUTCHAR(5, bufp); /* delay */
391 cbcp_send(us, CBCP_RESP, buf, len);
395 if (cb_type & ( 1 << CB_CONF_NO ) ) {
396 dbglog("cbcp_resp CONF_NO");
397 PUTCHAR(CB_CONF_NO, bufp);
400 cbcp_send(us, CBCP_RESP, buf, len);
401 start_networks(us->us_unit);
407 cbcp_send(cbcp_state *us, int code, u_char *buf, int len)
412 outp = outpacket_buf;
416 MAKEHEADER(outp, PPP_CBCP);
419 PUTCHAR(us->us_id, outp);
420 PUTSHORT(outlen, outp);
423 BCOPY(buf, outp, len);
425 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
429 cbcp_recvack(cbcp_state *us, u_char *pckt, int len)
431 u_char type, delay, addr_type;
437 GETCHAR(opt_len, pckt);
438 if (opt_len >= 2 && opt_len <= len) {
441 GETCHAR(delay, pckt);
444 GETCHAR(addr_type, pckt);
445 memcpy(address, pckt, opt_len - 4);
446 address[opt_len - 4] = 0;
448 dbglog("peer will call: %s", address);
450 if (type == CB_CONF_NO)
456 dbglog("cbcp_recvack: malformed packet");
460 /* ok peer will do callback */
462 cbcp_up(cbcp_state *us)
465 ppp_set_status(EXIT_CALLBACK);
466 lcp_close(0, "Call me back, please");