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.2 1997/04/30 05:50:26 paulus Exp $";
27 #include <sys/types.h>
38 * Protocol entry points.
40 static void cbcp_init __P((int unit));
41 static void cbcp_open __P((int unit));
42 static void cbcp_lowerup __P((int unit));
43 static void cbcp_input __P((int unit, u_char *pkt, int len));
44 static void cbcp_protrej __P((int unit));
45 static int cbcp_printpkt __P((u_char *pkt, int len,
46 void (*printer) __P((void *, char *, ...)),
49 struct protent cbcp_protent = {
67 cbcp_state cbcp[NUM_PPP];
69 /* internal prototypes */
71 static void cbcp_recvreq __P((cbcp_state *us, char *pckt, int len));
72 static void cbcp_resp __P((cbcp_state *us));
73 static void cbcp_up __P((cbcp_state *us));
74 static void cbcp_recvack __P((cbcp_state *us, char *pckt, int len));
75 static void cbcp_send __P((cbcp_state *us, u_char code, u_char *buf, int len));
85 memset(us, 0, sizeof(cbcp_state));
87 us->us_type |= (1 << CB_CONF_NO);
90 /* lower layer is up */
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);
108 syslog(LOG_DEBUG, "cbcp_open");
111 /* process an incomming packet */
113 cbcp_input(unit, inpacket, pktlen)
122 cbcp_state *us = &cbcp[unit];
126 if (pktlen < CBCP_MINLEN) {
127 syslog(LOG_ERR, "CBCP packet is too small");
137 syslog(LOG_ERR, "CBCP packet: invalid length");
147 cbcp_recvreq(us, inp, len);
151 syslog(LOG_DEBUG, "CBCP_RESP received");
156 syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
159 cbcp_recvack(us, inp, len);
167 /* protocol was rejected by foe */
168 void cbcp_protrej(int iface)
172 char *cbcp_codenames[] = {
173 "Request", "Response", "Ack"
176 char *cbcp_optionnames[] = {
183 /* pretty print a packet */
185 cbcp_printpkt(p, plen, printer, arg)
188 void (*printer) __P((void *, char *, ...));
191 int code, opt, id, len, olen, delay;
194 if (plen < HEADERLEN)
200 if (len < HEADERLEN || len > plen)
203 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
204 printer(arg, " %s", cbcp_codenames[code-1]);
206 printer(arg, " code=0x%x", code);
208 printer(arg, " id=0x%x", id);
219 if (olen < 2 || olen > len) {
226 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
227 printer(arg, " %s", cbcp_optionnames[opt-1]);
229 printer(arg, " option=0x%x", opt);
233 printer(arg, " delay = %d", delay);
241 memcpy(str, p, olen - 4);
243 printer(arg, " number = %s", str);
253 for (; len > 0; --len) {
255 printer(arg, " %.2x", code);
261 /* received CBCP request */
263 cbcp_recvreq(us, pckt, pcktlen)
268 u_char type, opt_len, delay, addr_type;
275 syslog(LOG_DEBUG, "length: %d", len);
278 GETCHAR(opt_len, pckt);
281 GETCHAR(delay, pckt);
283 us->us_allowed |= (1 << type);
287 syslog(LOG_DEBUG, "no callback allowed");
291 syslog(LOG_DEBUG, "user callback allowed");
293 GETCHAR(addr_type, pckt);
294 memcpy(address, pckt, opt_len - 4);
295 address[opt_len - 4] = 0;
297 syslog(LOG_DEBUG, "address: %s", address);
302 syslog(LOG_DEBUG, "user admin defined allowed");
323 cb_type = us->us_allowed & us->us_type;
324 syslog(LOG_DEBUG, "cbcp_resp cb_type=%d", cb_type);
328 lcp_down(us->us_unit);
331 if (cb_type & ( 1 << CB_CONF_USER ) ) {
332 syslog(LOG_DEBUG, "cbcp_resp CONF_USER");
333 PUTCHAR(CB_CONF_USER, bufp);
334 len = 3 + 1 + strlen(us->us_number) + 1;
336 PUTCHAR(5, bufp); /* delay */
338 BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
339 cbcp_send(us, CBCP_RESP, buf, len);
343 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
344 syslog(LOG_DEBUG, "cbcp_resp CONF_ADMIN");
345 PUTCHAR(CB_CONF_ADMIN, bufp);
348 PUTCHAR(5, bufp); /* delay */
350 cbcp_send(us, CBCP_RESP, buf, len);
354 if (cb_type & ( 1 << CB_CONF_NO ) ) {
355 syslog(LOG_DEBUG, "cbcp_resp CONF_NO");
356 PUTCHAR(CB_CONF_NO, bufp);
360 cbcp_send(us, CBCP_RESP, buf, len);
361 (*ipcp_protent.open)(us->us_unit);
367 cbcp_send(us, code, buf, len)
376 outp = outpacket_buf;
380 MAKEHEADER(outp, PPP_CBCP);
383 PUTCHAR(us->us_id, outp);
384 PUTSHORT(outlen, outp);
387 BCOPY(buf, outp, len);
389 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
393 cbcp_recvack(us, pckt, len)
398 u_char type, delay, addr_type;
404 GETCHAR(opt_len, pckt);
407 GETCHAR(delay, pckt);
410 GETCHAR(addr_type, pckt);
411 memcpy(address, pckt, opt_len - 4);
412 address[opt_len - 4] = 0;
414 syslog(LOG_DEBUG, "peer will call: %s", address);
423 /* ok peer will do callback */
429 lcp_close(0, "Call me back, please");