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.5 2005/01/07 20:19:54 dillon 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");
134 if (len < CBCP_MINLEN || len > pktlen) {
135 syslog(LOG_ERR, "CBCP packet: invalid length");
144 cbcp_recvreq(us, inp, len);
148 syslog(LOG_DEBUG, "CBCP_RESP received");
153 syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
156 cbcp_recvack(us, inp, len);
164 /* protocol was rejected by foe */
165 void cbcp_protrej(int iface)
169 char *cbcp_codenames[] = {
170 "Request", "Response", "Ack"
173 char *cbcp_optionnames[] = {
180 /* pretty print a packet */
182 cbcp_printpkt(p, plen, printer, arg)
185 void (*printer)(void *, char *, ...);
188 int code, opt, id, len, olen, delay;
191 if (plen < HEADERLEN)
197 if (len < HEADERLEN || len > plen)
200 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
201 printer(arg, " %s", cbcp_codenames[code-1]);
203 printer(arg, " code=0x%x", code);
205 printer(arg, " id=0x%x", id);
216 if (olen < 2 || olen > len) {
223 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
224 printer(arg, " %s", cbcp_optionnames[opt-1]);
226 printer(arg, " option=0x%x", opt);
230 printer(arg, " delay = %d", delay);
238 memcpy(str, p, olen - 4);
240 printer(arg, " number = %s", str);
250 for (; len > 0; --len) {
252 printer(arg, " %.2x", code);
258 /* received CBCP request */
260 cbcp_recvreq(us, pckt, pcktlen)
265 u_char type, opt_len, delay, addr_type;
272 syslog(LOG_DEBUG, "length: %d", len);
275 GETCHAR(opt_len, pckt);
282 GETCHAR(delay, pckt);
284 us->us_allowed |= (1 << type);
288 syslog(LOG_DEBUG, "no callback allowed");
292 syslog(LOG_DEBUG, "user callback allowed");
294 GETCHAR(addr_type, pckt);
295 memcpy(address, pckt, opt_len - 4);
296 address[opt_len - 4] = 0;
298 syslog(LOG_DEBUG, "address: %s", address);
303 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 */
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);
358 cbcp_send(us, CBCP_RESP, buf, len);
359 (*ipcp_protent.open)(us->us_unit);
365 cbcp_send(us, code, buf, len)
374 outp = outpacket_buf;
378 MAKEHEADER(outp, PPP_CBCP);
381 PUTCHAR(us->us_id, outp);
382 PUTSHORT(outlen, outp);
385 BCOPY(buf, outp, len);
387 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
391 cbcp_recvack(us, pckt, len)
396 u_char type, delay, addr_type;
402 GETCHAR(opt_len, pckt);
408 GETCHAR(delay, pckt);
411 GETCHAR(addr_type, pckt);
412 memcpy(address, pckt, opt_len - 4);
413 address[opt_len - 4] = 0;
415 syslog(LOG_DEBUG, "peer will call: %s", address);
424 /* ok peer will do callback */
430 lcp_close(0, "Call me back, please");