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.6 2005/11/24 23:42:54 swildner 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);
83 memset(us, 0, sizeof(cbcp_state));
85 us->us_type |= (1 << CB_CONF_NO);
88 /* lower layer is up */
90 cbcp_lowerup(int iface)
92 cbcp_state *us = &cbcp[iface];
94 syslog(LOG_DEBUG, "cbcp_lowerup");
95 syslog(LOG_DEBUG, "want: %d", us->us_type);
97 if (us->us_type == CB_CONF_USER)
98 syslog(LOG_DEBUG, "phone no: %s", us->us_number);
104 syslog(LOG_DEBUG, "cbcp_open");
107 /* process an incomming packet */
109 cbcp_input(int unit, u_char *inpacket, int pktlen)
115 cbcp_state *us = &cbcp[unit];
119 if (pktlen < CBCP_MINLEN) {
120 syslog(LOG_ERR, "CBCP packet is too small");
128 if (len < CBCP_MINLEN || len > pktlen) {
129 syslog(LOG_ERR, "CBCP packet: invalid length");
138 cbcp_recvreq(us, inp, len);
142 syslog(LOG_DEBUG, "CBCP_RESP received");
147 syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
150 cbcp_recvack(us, inp, len);
158 /* protocol was rejected by foe */
160 cbcp_protrej(int iface)
164 char *cbcp_codenames[] = {
165 "Request", "Response", "Ack"
168 char *cbcp_optionnames[] = {
175 /* pretty print a packet */
177 cbcp_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...),
180 int code, opt, id, len, olen, delay;
183 if (plen < HEADERLEN)
189 if (len < HEADERLEN || len > plen)
192 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
193 printer(arg, " %s", cbcp_codenames[code-1]);
195 printer(arg, " code=0x%x", code);
197 printer(arg, " id=0x%x", id);
208 if (olen < 2 || olen > len) {
215 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
216 printer(arg, " %s", cbcp_optionnames[opt-1]);
218 printer(arg, " option=0x%x", opt);
222 printer(arg, " delay = %d", delay);
230 memcpy(str, p, olen - 4);
232 printer(arg, " number = %s", str);
242 for (; len > 0; --len) {
244 printer(arg, " %.2x", code);
250 /* received CBCP request */
252 cbcp_recvreq(cbcp_state *us, char *pckt, int pcktlen)
254 u_char type, opt_len, delay, addr_type;
261 syslog(LOG_DEBUG, "length: %d", len);
264 GETCHAR(opt_len, pckt);
271 GETCHAR(delay, pckt);
273 us->us_allowed |= (1 << type);
277 syslog(LOG_DEBUG, "no callback allowed");
281 syslog(LOG_DEBUG, "user callback allowed");
283 GETCHAR(addr_type, pckt);
284 memcpy(address, pckt, opt_len - 4);
285 address[opt_len - 4] = 0;
287 syslog(LOG_DEBUG, "address: %s", address);
292 syslog(LOG_DEBUG, "user admin defined allowed");
304 cbcp_resp(cbcp_state *us)
311 cb_type = us->us_allowed & us->us_type;
312 syslog(LOG_DEBUG, "cbcp_resp cb_type=%d", cb_type);
316 lcp_down(us->us_unit);
319 if (cb_type & ( 1 << CB_CONF_USER ) ) {
320 syslog(LOG_DEBUG, "cbcp_resp CONF_USER");
321 PUTCHAR(CB_CONF_USER, bufp);
322 len = 3 + 1 + strlen(us->us_number) + 1;
324 PUTCHAR(5, bufp); /* delay */
326 BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
327 cbcp_send(us, CBCP_RESP, buf, len);
331 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
332 syslog(LOG_DEBUG, "cbcp_resp CONF_ADMIN");
333 PUTCHAR(CB_CONF_ADMIN, bufp);
336 PUTCHAR(5, bufp); /* delay */
337 cbcp_send(us, CBCP_RESP, buf, len);
341 if (cb_type & ( 1 << CB_CONF_NO ) ) {
342 syslog(LOG_DEBUG, "cbcp_resp CONF_NO");
343 PUTCHAR(CB_CONF_NO, bufp);
346 cbcp_send(us, CBCP_RESP, buf, len);
347 (*ipcp_protent.open)(us->us_unit);
353 cbcp_send(cbcp_state *us, u_char code, u_char *buf, int len)
358 outp = outpacket_buf;
362 MAKEHEADER(outp, PPP_CBCP);
365 PUTCHAR(us->us_id, outp);
366 PUTSHORT(outlen, outp);
369 BCOPY(buf, outp, len);
371 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
375 cbcp_recvack(cbcp_state *us, char *pckt, int len)
377 u_char type, delay, addr_type;
383 GETCHAR(opt_len, pckt);
389 GETCHAR(delay, pckt);
392 GETCHAR(addr_type, pckt);
393 memcpy(address, pckt, opt_len - 4);
394 address[opt_len - 4] = 0;
396 syslog(LOG_DEBUG, "peer will call: %s", address);
405 /* ok peer will do callback */
407 cbcp_up(cbcp_state *us)
410 lcp_close(0, "Call me back, please");