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.
20 * $FreeBSD: src/usr.sbin/pppd/cbcp.c,v 1.4 1999/08/28 01:19:00 peter Exp $
21 * $DragonFly: src/usr.sbin/pppd/cbcp.c,v 1.3 2003/11/03 19:31:40 eirikn Exp $
26 #include <sys/types.h>
37 * Protocol entry points.
39 static void cbcp_init (int unit);
40 static void cbcp_open (int unit);
41 static void cbcp_lowerup (int unit);
42 static void cbcp_input (int unit, u_char *pkt, int len);
43 static void cbcp_protrej (int unit);
44 static int cbcp_printpkt (u_char *pkt, int len,
45 void (*printer)(void *, char *, ...),
48 struct protent cbcp_protent = {
66 cbcp_state cbcp[NUM_PPP];
68 /* internal prototypes */
70 static void cbcp_recvreq(cbcp_state *us, char *pckt, int len);
71 static void cbcp_resp(cbcp_state *us);
72 static void cbcp_up(cbcp_state *us);
73 static void cbcp_recvack(cbcp_state *us, char *pckt, int len);
74 static void cbcp_send(cbcp_state *us, u_char code, u_char *buf, int len);
84 memset(us, 0, sizeof(cbcp_state));
86 us->us_type |= (1 << CB_CONF_NO);
89 /* lower layer is up */
94 cbcp_state *us = &cbcp[iface];
96 syslog(LOG_DEBUG, "cbcp_lowerup");
97 syslog(LOG_DEBUG, "want: %d", us->us_type);
99 if (us->us_type == CB_CONF_USER)
100 syslog(LOG_DEBUG, "phone no: %s", us->us_number);
107 syslog(LOG_DEBUG, "cbcp_open");
110 /* process an incomming packet */
112 cbcp_input(unit, inpacket, pktlen)
121 cbcp_state *us = &cbcp[unit];
125 if (pktlen < CBCP_MINLEN) {
126 syslog(LOG_ERR, "CBCP packet is too small");
136 syslog(LOG_ERR, "CBCP packet: invalid length");
146 cbcp_recvreq(us, inp, len);
150 syslog(LOG_DEBUG, "CBCP_RESP received");
155 syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
158 cbcp_recvack(us, inp, len);
166 /* protocol was rejected by foe */
167 void cbcp_protrej(int iface)
171 char *cbcp_codenames[] = {
172 "Request", "Response", "Ack"
175 char *cbcp_optionnames[] = {
182 /* pretty print a packet */
184 cbcp_printpkt(p, plen, printer, arg)
187 void (*printer)(void *, char *, ...);
190 int code, opt, id, len, olen, delay;
193 if (plen < HEADERLEN)
199 if (len < HEADERLEN || len > plen)
202 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
203 printer(arg, " %s", cbcp_codenames[code-1]);
205 printer(arg, " code=0x%x", code);
207 printer(arg, " id=0x%x", id);
218 if (olen < 2 || olen > len) {
225 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
226 printer(arg, " %s", cbcp_optionnames[opt-1]);
228 printer(arg, " option=0x%x", opt);
232 printer(arg, " delay = %d", delay);
240 memcpy(str, p, olen - 4);
242 printer(arg, " number = %s", str);
252 for (; len > 0; --len) {
254 printer(arg, " %.2x", code);
260 /* received CBCP request */
262 cbcp_recvreq(us, pckt, pcktlen)
267 u_char type, opt_len, delay, addr_type;
274 syslog(LOG_DEBUG, "length: %d", len);
277 GETCHAR(opt_len, pckt);
280 GETCHAR(delay, pckt);
282 us->us_allowed |= (1 << type);
286 syslog(LOG_DEBUG, "no callback allowed");
290 syslog(LOG_DEBUG, "user callback allowed");
292 GETCHAR(addr_type, pckt);
293 memcpy(address, pckt, opt_len - 4);
294 address[opt_len - 4] = 0;
296 syslog(LOG_DEBUG, "address: %s", address);
301 syslog(LOG_DEBUG, "user admin defined allowed");
322 cb_type = us->us_allowed & us->us_type;
323 syslog(LOG_DEBUG, "cbcp_resp cb_type=%d", cb_type);
327 lcp_down(us->us_unit);
330 if (cb_type & ( 1 << CB_CONF_USER ) ) {
331 syslog(LOG_DEBUG, "cbcp_resp CONF_USER");
332 PUTCHAR(CB_CONF_USER, bufp);
333 len = 3 + 1 + strlen(us->us_number) + 1;
335 PUTCHAR(5, bufp); /* delay */
337 BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
338 cbcp_send(us, CBCP_RESP, buf, len);
342 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
343 syslog(LOG_DEBUG, "cbcp_resp CONF_ADMIN");
344 PUTCHAR(CB_CONF_ADMIN, bufp);
347 PUTCHAR(5, bufp); /* delay */
349 cbcp_send(us, CBCP_RESP, buf, len);
353 if (cb_type & ( 1 << CB_CONF_NO ) ) {
354 syslog(LOG_DEBUG, "cbcp_resp CONF_NO");
355 PUTCHAR(CB_CONF_NO, bufp);
359 cbcp_send(us, CBCP_RESP, buf, len);
360 (*ipcp_protent.open)(us->us_unit);
366 cbcp_send(us, code, buf, len)
375 outp = outpacket_buf;
379 MAKEHEADER(outp, PPP_CBCP);
382 PUTCHAR(us->us_id, outp);
383 PUTSHORT(outlen, outp);
386 BCOPY(buf, outp, len);
388 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
392 cbcp_recvack(us, pckt, len)
397 u_char type, delay, addr_type;
403 GETCHAR(opt_len, pckt);
406 GETCHAR(delay, pckt);
409 GETCHAR(addr_type, pckt);
410 memcpy(address, pckt, opt_len - 4);
411 address[opt_len - 4] = 0;
413 syslog(LOG_DEBUG, "peer will call: %s", address);
422 /* ok peer will do callback */
428 lcp_close(0, "Call me back, please");