2 * cbcp - Call Back Configuration Protocol.
4 * Copyright (c) 1995 Pedro Roque Marques
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.
22 static char rcsid[] = "$Id: cbcp.c,v 1.4 1999/03/02 05:34:16 paulus Exp $";
27 #include <sys/types.h>
40 static int setcbcp __P((char **));
42 static option_t cbcp_option_list[] = {
43 { "callback", o_special, setcbcp,
49 * Protocol entry points.
51 static void cbcp_init __P((int unit));
52 static void cbcp_open __P((int unit));
53 static void cbcp_lowerup __P((int unit));
54 static void cbcp_input __P((int unit, u_char *pkt, int len));
55 static void cbcp_protrej __P((int unit));
56 static int cbcp_printpkt __P((u_char *pkt, int len,
57 void (*printer) __P((void *, char *, ...)),
60 struct protent cbcp_protent = {
80 cbcp_state cbcp[NUM_PPP];
82 /* internal prototypes */
84 static void cbcp_recvreq __P((cbcp_state *us, char *pckt, int len));
85 static void cbcp_resp __P((cbcp_state *us));
86 static void cbcp_up __P((cbcp_state *us));
87 static void cbcp_recvack __P((cbcp_state *us, char *pckt, int len));
88 static void cbcp_send __P((cbcp_state *us, u_char code, u_char *buf, int len));
90 /* option processing */
95 lcp_wantoptions[0].neg_cbcp = 1;
96 cbcp_protent.enabled_flag = 1;
97 cbcp[0].us_number = strdup(*argv);
98 if (cbcp[0].us_number == 0)
99 novm("callback number");
100 cbcp[0].us_type |= (1 << CB_CONF_USER);
101 cbcp[0].us_type |= (1 << CB_CONF_ADMIN);
113 memset(us, 0, sizeof(cbcp_state));
115 us->us_type |= (1 << CB_CONF_NO);
118 /* lower layer is up */
123 cbcp_state *us = &cbcp[iface];
125 syslog(LOG_DEBUG, "cbcp_lowerup");
126 syslog(LOG_DEBUG, "want: %d", us->us_type);
128 if (us->us_type == CB_CONF_USER)
129 syslog(LOG_DEBUG, "phone no: %s", us->us_number);
136 syslog(LOG_DEBUG, "cbcp_open");
139 /* process an incomming packet */
141 cbcp_input(unit, inpacket, pktlen)
150 cbcp_state *us = &cbcp[unit];
154 if (pktlen < CBCP_MINLEN) {
155 syslog(LOG_ERR, "CBCP packet is too small");
165 syslog(LOG_ERR, "CBCP packet: invalid length");
175 cbcp_recvreq(us, inp, len);
179 syslog(LOG_DEBUG, "CBCP_RESP received");
184 syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
187 cbcp_recvack(us, inp, len);
195 /* protocol was rejected by foe */
196 void cbcp_protrej(int iface)
200 char *cbcp_codenames[] = {
201 "Request", "Response", "Ack"
204 char *cbcp_optionnames[] = {
211 /* pretty print a packet */
213 cbcp_printpkt(p, plen, printer, arg)
216 void (*printer) __P((void *, char *, ...));
219 int code, opt, id, len, olen, delay;
222 if (plen < HEADERLEN)
228 if (len < HEADERLEN || len > plen)
231 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
232 printer(arg, " %s", cbcp_codenames[code-1]);
234 printer(arg, " code=0x%x", code);
236 printer(arg, " id=0x%x", id);
247 if (olen < 2 || olen > len) {
254 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
255 printer(arg, " %s", cbcp_optionnames[opt-1]);
257 printer(arg, " option=0x%x", opt);
261 printer(arg, " delay = %d", delay);
269 memcpy(str, p, olen - 4);
271 printer(arg, " number = %s", str);
281 for (; len > 0; --len) {
283 printer(arg, " %.2x", code);
289 /* received CBCP request */
291 cbcp_recvreq(us, pckt, pcktlen)
296 u_char type, opt_len, delay, addr_type;
303 syslog(LOG_DEBUG, "length: %d", len);
306 GETCHAR(opt_len, pckt);
309 GETCHAR(delay, pckt);
311 us->us_allowed |= (1 << type);
315 syslog(LOG_DEBUG, "no callback allowed");
319 syslog(LOG_DEBUG, "user callback allowed");
321 GETCHAR(addr_type, pckt);
322 memcpy(address, pckt, opt_len - 4);
323 address[opt_len - 4] = 0;
325 syslog(LOG_DEBUG, "address: %s", address);
330 syslog(LOG_DEBUG, "user admin defined allowed");
351 cb_type = us->us_allowed & us->us_type;
352 syslog(LOG_DEBUG, "cbcp_resp cb_type=%d", cb_type);
356 lcp_down(us->us_unit);
359 if (cb_type & ( 1 << CB_CONF_USER ) ) {
360 syslog(LOG_DEBUG, "cbcp_resp CONF_USER");
361 PUTCHAR(CB_CONF_USER, bufp);
362 len = 3 + 1 + strlen(us->us_number) + 1;
364 PUTCHAR(5, bufp); /* delay */
366 BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
367 cbcp_send(us, CBCP_RESP, buf, len);
371 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
372 syslog(LOG_DEBUG, "cbcp_resp CONF_ADMIN");
373 PUTCHAR(CB_CONF_ADMIN, bufp);
376 PUTCHAR(5, bufp); /* delay */
377 cbcp_send(us, CBCP_RESP, buf, len);
381 if (cb_type & ( 1 << CB_CONF_NO ) ) {
382 syslog(LOG_DEBUG, "cbcp_resp CONF_NO");
383 PUTCHAR(CB_CONF_NO, bufp);
387 cbcp_send(us, CBCP_RESP, buf, len);
388 (*ipcp_protent.open)(us->us_unit);
394 cbcp_send(us, code, buf, len)
403 outp = outpacket_buf;
407 MAKEHEADER(outp, PPP_CBCP);
410 PUTCHAR(us->us_id, outp);
411 PUTSHORT(outlen, outp);
414 BCOPY(buf, outp, len);
416 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
420 cbcp_recvack(us, pckt, len)
425 u_char type, delay, addr_type;
431 GETCHAR(opt_len, pckt);
434 GETCHAR(delay, pckt);
437 GETCHAR(addr_type, pckt);
438 memcpy(address, pckt, opt_len - 4);
439 address[opt_len - 4] = 0;
441 syslog(LOG_DEBUG, "peer will call: %s", address);
450 /* ok peer will do callback */
456 lcp_close(0, "Call me back, please");