1 //==========================================================================
5 //==========================================================================
6 //####ECOSGPLCOPYRIGHTBEGIN####
7 // -------------------------------------------
8 // This file is part of eCos, the Embedded Configurable Operating System.
9 // Portions created by Nick Garnett are
10 // Copyright (C) 2003 eCosCentric Ltd.
12 // eCos is free software; you can redistribute it and/or modify it under
13 // the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 2 or (at your option) any later version.
16 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 // You should have received a copy of the GNU General Public License along
22 // with eCos; if not, write to the Free Software Foundation, Inc.,
23 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 // As a special exception, if other files instantiate templates or use macros
26 // or inline functions from this file, or you compile this file and link it
27 // with other works to produce a work based on this file, this file does not
28 // by itself cause the resulting work to be covered by the GNU General Public
29 // License. However the source code for this file must still be made available
30 // in accordance with section (3) of the GNU General Public License.
32 // This exception does not invalidate any other reasons why a work based on
33 // this file might be covered by the GNU General Public License.
35 // -------------------------------------------
36 //####ECOSGPLCOPYRIGHTEND####
37 //####BSDCOPYRIGHTBEGIN####
39 // -------------------------------------------
41 // Portions of this software may have been derived from OpenBSD,
42 // FreeBSD or other sources, and are covered by the appropriate
43 // copyright disclaimers included herein.
45 // -------------------------------------------
47 //####BSDCOPYRIGHTEND####
48 //==========================================================================
51 * cbcp - Call Back Configuration Protocol.
53 * Copyright (c) 1995 Pedro Roque Marques
54 * All rights reserved.
56 * Redistribution and use in source and binary forms are permitted
57 * provided that the above copyright notice and this paragraph are
58 * duplicated in all such forms and that any documentation,
59 * advertising materials, and other materials related to such
60 * distribution and use acknowledge that the software was developed
61 * by Pedro Roque Marques. The name of the author may not be used to
62 * endorse or promote products derived from this software without
63 * specific prior written permission.
65 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
66 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
67 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
71 //static char rcsid[] = "$FreeBSD: src/usr.sbin/pppd/cbcp.c,v 1.4 1999/08/28 01:19:00 peter Exp $";
76 #include <sys/types.h>
78 #include <cyg/ppp/syslog.h>
79 #include "cyg/ppp/pppd.h"
80 #include "cyg/ppp/cbcp.h"
81 #include "cyg/ppp/fsm.h"
82 #include "cyg/ppp/lcp.h"
83 #include "cyg/ppp/ipcp.h"
86 * Protocol entry points.
88 static void cbcp_init __P((int unit));
89 static void cbcp_open __P((int unit));
90 static void cbcp_lowerup __P((int unit));
91 static void cbcp_input __P((int unit, u_char *pkt, int len));
92 static void cbcp_protrej __P((int unit));
93 static int cbcp_printpkt __P((u_char *pkt, int len,
94 void (*printer) __P((void *, char *, ...)),
97 struct protent cbcp_protent = {
115 cbcp_state cbcp[NUM_PPP];
117 /* internal prototypes */
119 static void cbcp_recvreq __P((cbcp_state *us, u_char *pckt, int len));
120 static void cbcp_resp __P((cbcp_state *us));
121 static void cbcp_up __P((cbcp_state *us));
122 static void cbcp_recvack __P((cbcp_state *us, u_char *pckt, int len));
123 static void cbcp_send __P((cbcp_state *us, u_char code, u_char *buf, int len));
133 memset(us, 0, sizeof(cbcp_state));
135 us->us_type |= (1 << CB_CONF_NO);
138 /* lower layer is up */
143 cbcp_state *us = &cbcp[iface];
145 syslog(LOG_DEBUG, "cbcp_lowerup");
146 syslog(LOG_DEBUG, "want: %d", us->us_type);
148 if (us->us_type == CB_CONF_USER)
149 syslog(LOG_DEBUG, "phone no: %s", us->us_number);
156 syslog(LOG_DEBUG, "cbcp_open");
159 /* process an incomming packet */
161 cbcp_input(unit, inpacket, pktlen)
170 cbcp_state *us = &cbcp[unit];
174 if (pktlen < CBCP_MINLEN) {
175 syslog(LOG_ERR, "CBCP packet is too small");
185 syslog(LOG_ERR, "CBCP packet: invalid length");
195 cbcp_recvreq(us, inp, len);
199 syslog(LOG_DEBUG, "CBCP_RESP received");
204 syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
207 cbcp_recvack(us, inp, len);
215 /* protocol was rejected by foe */
216 void cbcp_protrej(int iface)
220 char *cbcp_codenames[] = {
221 "Request", "Response", "Ack"
224 char *cbcp_optionnames[] = {
231 /* pretty print a packet */
233 cbcp_printpkt(p, plen, printer, arg)
236 void (*printer) __P((void *, char *, ...));
239 int code, opt, id, len, olen, delay;
242 if (plen < HEADERLEN)
248 if (len < HEADERLEN || len > plen)
251 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
252 printer(arg, " %s", cbcp_codenames[code-1]);
254 printer(arg, " code=0x%x", code);
256 printer(arg, " id=0x%x", id);
267 if (olen < 2 || olen > len) {
274 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
275 printer(arg, " %s", cbcp_optionnames[opt-1]);
277 printer(arg, " option=0x%x", opt);
281 printer(arg, " delay = %d", delay);
289 memcpy(str, p, olen - 4);
291 printer(arg, " number = %s", str);
301 for (; len > 0; --len) {
303 printer(arg, " %.2x", code);
309 /* received CBCP request */
311 cbcp_recvreq(us, pckt, pcktlen)
316 u_char type, opt_len, delay, addr_type;
323 syslog(LOG_DEBUG, "length: %d", len);
326 GETCHAR(opt_len, pckt);
329 GETCHAR(delay, pckt);
331 us->us_allowed |= (1 << type);
335 syslog(LOG_DEBUG, "no callback allowed");
339 syslog(LOG_DEBUG, "user callback allowed");
341 GETCHAR(addr_type, pckt);
342 memcpy(address, pckt, opt_len - 4);
343 address[opt_len - 4] = 0;
345 syslog(LOG_DEBUG, "address: %s", address);
350 syslog(LOG_DEBUG, "user admin defined allowed");
371 cb_type = us->us_allowed & us->us_type;
372 syslog(LOG_DEBUG, "cbcp_resp cb_type=%d", cb_type);
376 lcp_down(us->us_unit);
379 if (cb_type & ( 1 << CB_CONF_USER ) ) {
380 syslog(LOG_DEBUG, "cbcp_resp CONF_USER");
381 PUTCHAR(CB_CONF_USER, bufp);
382 len = 3 + 1 + strlen(us->us_number) + 1;
384 PUTCHAR(5, bufp); /* delay */
386 BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
387 cbcp_send(us, CBCP_RESP, buf, len);
391 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
392 syslog(LOG_DEBUG, "cbcp_resp CONF_ADMIN");
393 PUTCHAR(CB_CONF_ADMIN, bufp);
396 PUTCHAR(5, bufp); /* delay */
398 cbcp_send(us, CBCP_RESP, buf, len);
402 if (cb_type & ( 1 << CB_CONF_NO ) ) {
403 syslog(LOG_DEBUG, "cbcp_resp CONF_NO");
404 PUTCHAR(CB_CONF_NO, bufp);
408 cbcp_send(us, CBCP_RESP, buf, len);
409 (*ipcp_protent.open)(us->us_unit);
415 cbcp_send(us, code, buf, len)
424 outp = outpacket_buf;
428 MAKEHEADER(outp, PPP_CBCP);
431 PUTCHAR(us->us_id, outp);
432 PUTSHORT(outlen, outp);
435 BCOPY(buf, outp, len);
437 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
441 cbcp_recvack(us, pckt, len)
446 u_char type, delay, addr_type;
452 GETCHAR(opt_len, pckt);
455 GETCHAR(delay, pckt);
458 GETCHAR(addr_type, pckt);
459 memcpy(address, pckt, opt_len - 4);
460 address[opt_len - 4] = 0;
462 syslog(LOG_DEBUG, "peer will call: %s", address);
471 /* ok peer will do callback */
477 lcp_close(0, "Call me back, please");