Merge branch 'misc'
[dragonfly.git] / usr.sbin / pppd / cbcp.c
blob4ef23b0c390f732aa4e510c6f9da7332d2f4606f
1 /*
2 * cbcp - Call Back Configuration Protocol.
4 * Copyright (c) 1995 Pedro Roque Marques
5 * All rights reserved.
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 $
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <syslog.h>
30 #include "pppd.h"
31 #include "cbcp.h"
32 #include "fsm.h"
33 #include "lcp.h"
34 #include "ipcp.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 *, ...),
46 void *arg);
48 struct protent cbcp_protent = {
49 PPP_CBCP,
50 cbcp_init,
51 cbcp_input,
52 cbcp_protrej,
53 cbcp_lowerup,
54 NULL,
55 cbcp_open,
56 NULL,
57 cbcp_printpkt,
58 NULL,
60 "CBCP",
61 NULL,
62 NULL,
63 NULL
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);
76 /* init state */
77 static void
78 cbcp_init(int iface)
80 cbcp_state *us;
82 us = &cbcp[iface];
83 memset(us, 0, sizeof(cbcp_state));
84 us->us_unit = iface;
85 us->us_type |= (1 << CB_CONF_NO);
88 /* lower layer is up */
89 static void
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);
101 static void
102 cbcp_open(int unit)
104 syslog(LOG_DEBUG, "cbcp_open");
107 /* process an incomming packet */
108 static void
109 cbcp_input(int unit, u_char *inpacket, int pktlen)
111 u_char *inp;
112 u_char code, id;
113 u_short len;
115 cbcp_state *us = &cbcp[unit];
117 inp = inpacket;
119 if (pktlen < CBCP_MINLEN) {
120 syslog(LOG_ERR, "CBCP packet is too small");
121 return;
124 GETCHAR(code, inp);
125 GETCHAR(id, inp);
126 GETSHORT(len, inp);
128 if (len < CBCP_MINLEN || len > pktlen) {
129 syslog(LOG_ERR, "CBCP packet: invalid length");
130 return;
133 len -= CBCP_MINLEN;
135 switch(code) {
136 case CBCP_REQ:
137 us->us_id = id;
138 cbcp_recvreq(us, inp, len);
139 break;
141 case CBCP_RESP:
142 syslog(LOG_DEBUG, "CBCP_RESP received");
143 break;
145 case CBCP_ACK:
146 if (id != us->us_id)
147 syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
148 us->us_id, id);
150 cbcp_recvack(us, inp, len);
151 break;
153 default:
154 break;
158 /* protocol was rejected by foe */
159 void
160 cbcp_protrej(int iface)
164 char *cbcp_codenames[] = {
165 "Request", "Response", "Ack"
168 char *cbcp_optionnames[] = {
169 "NoCallback",
170 "UserDefined",
171 "AdminDefined",
172 "List"
175 /* pretty print a packet */
176 static int
177 cbcp_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...),
178 void *arg)
180 int code, opt, id, len, olen, delay;
181 u_char *pstart;
183 if (plen < HEADERLEN)
184 return 0;
185 pstart = p;
186 GETCHAR(code, p);
187 GETCHAR(id, p);
188 GETSHORT(len, p);
189 if (len < HEADERLEN || len > plen)
190 return 0;
192 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
193 printer(arg, " %s", cbcp_codenames[code-1]);
194 else
195 printer(arg, " code=0x%x", code);
197 printer(arg, " id=0x%x", id);
198 len -= HEADERLEN;
200 switch (code) {
201 case CBCP_REQ:
202 case CBCP_RESP:
203 case CBCP_ACK:
204 while(len >= 2) {
205 GETCHAR(opt, p);
206 GETCHAR(olen, p);
208 if (olen < 2 || olen > len) {
209 break;
212 printer(arg, " <");
213 len -= olen;
215 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
216 printer(arg, " %s", cbcp_optionnames[opt-1]);
217 else
218 printer(arg, " option=0x%x", opt);
220 if (olen > 2) {
221 GETCHAR(delay, p);
222 printer(arg, " delay = %d", delay);
225 if (olen > 3) {
226 int addrt;
227 char str[256];
229 GETCHAR(addrt, p);
230 memcpy(str, p, olen - 4);
231 str[olen - 4] = 0;
232 printer(arg, " number = %s", str);
234 printer(arg, ">");
235 break;
238 default:
239 break;
242 for (; len > 0; --len) {
243 GETCHAR(code, p);
244 printer(arg, " %.2x", code);
247 return p - pstart;
250 /* received CBCP request */
251 static void
252 cbcp_recvreq(cbcp_state *us, char *pckt, int pcktlen)
254 u_char type, opt_len, delay, addr_type;
255 char address[256];
256 int len = pcktlen;
258 address[0] = 0;
260 while (len > 1) {
261 syslog(LOG_DEBUG, "length: %d", len);
263 GETCHAR(type, pckt);
264 GETCHAR(opt_len, pckt);
266 if (len < opt_len)
267 break;
268 len -= opt_len;
270 if (opt_len > 2)
271 GETCHAR(delay, pckt);
273 us->us_allowed |= (1 << type);
275 switch(type) {
276 case CB_CONF_NO:
277 syslog(LOG_DEBUG, "no callback allowed");
278 break;
280 case CB_CONF_USER:
281 syslog(LOG_DEBUG, "user callback allowed");
282 if (opt_len > 4) {
283 GETCHAR(addr_type, pckt);
284 memcpy(address, pckt, opt_len - 4);
285 address[opt_len - 4] = 0;
286 if (address[0])
287 syslog(LOG_DEBUG, "address: %s", address);
289 break;
291 case CB_CONF_ADMIN:
292 syslog(LOG_DEBUG, "user admin defined allowed");
293 break;
295 case CB_CONF_LIST:
296 break;
300 cbcp_resp(us);
303 static void
304 cbcp_resp(cbcp_state *us)
306 u_char cb_type;
307 u_char buf[256];
308 u_char *bufp = buf;
309 int len = 0;
311 cb_type = us->us_allowed & us->us_type;
312 syslog(LOG_DEBUG, "cbcp_resp cb_type=%d", cb_type);
314 #if 0
315 if (!cb_type)
316 lcp_down(us->us_unit);
317 #endif
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;
323 PUTCHAR(len , bufp);
324 PUTCHAR(5, bufp); /* delay */
325 PUTCHAR(1, bufp);
326 BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
327 cbcp_send(us, CBCP_RESP, buf, len);
328 return;
331 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
332 syslog(LOG_DEBUG, "cbcp_resp CONF_ADMIN");
333 PUTCHAR(CB_CONF_ADMIN, bufp);
334 len = 3;
335 PUTCHAR(len, bufp);
336 PUTCHAR(5, bufp); /* delay */
337 cbcp_send(us, CBCP_RESP, buf, len);
338 return;
341 if (cb_type & ( 1 << CB_CONF_NO ) ) {
342 syslog(LOG_DEBUG, "cbcp_resp CONF_NO");
343 PUTCHAR(CB_CONF_NO, bufp);
344 len = 2;
345 PUTCHAR(len , bufp);
346 cbcp_send(us, CBCP_RESP, buf, len);
347 (*ipcp_protent.open)(us->us_unit);
348 return;
352 static void
353 cbcp_send(cbcp_state *us, u_char code, u_char *buf, int len)
355 u_char *outp;
356 int outlen;
358 outp = outpacket_buf;
360 outlen = 4 + len;
362 MAKEHEADER(outp, PPP_CBCP);
364 PUTCHAR(code, outp);
365 PUTCHAR(us->us_id, outp);
366 PUTSHORT(outlen, outp);
368 if (len)
369 BCOPY(buf, outp, len);
371 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
374 static void
375 cbcp_recvack(cbcp_state *us, char *pckt, int len)
377 u_char type, delay, addr_type;
378 int opt_len;
379 char address[256];
381 if (len > 1) {
382 GETCHAR(type, pckt);
383 GETCHAR(opt_len, pckt);
385 if (opt_len > len)
386 return;
388 if (opt_len > 2)
389 GETCHAR(delay, pckt);
391 if (opt_len > 4) {
392 GETCHAR(addr_type, pckt);
393 memcpy(address, pckt, opt_len - 4);
394 address[opt_len - 4] = 0;
395 if (address[0])
396 syslog(LOG_DEBUG, "peer will call: %s", address);
400 cbcp_up(us);
403 extern int persist;
405 /* ok peer will do callback */
406 static void
407 cbcp_up(cbcp_state *us)
409 persist = 0;
410 lcp_close(0, "Call me back, please");