cbcp.c revision 1.1.1.5 1 /*
2 * cbcp - Call Back Configuration Protocol.
3 *
4 * Copyright (c) 1995 Pedro Roque Marques. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * 3. The names of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
21 *
22 * 4. Redistributions of any form whatsoever must retain the following
23 * acknowledgment:
24 * "This product includes software developed by Pedro Roque Marques
25 * <pedro_m (at) yahoo.com>"
26 *
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 */
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/time.h>
43
44 #include "pppd-private.h"
45 #include "cbcp.h"
46 #include "fsm.h"
47 #include "lcp.h"
48 #include "options.h"
49
50
51 /*
52 * Options.
53 */
54 static int setcbcp (char **);
55
56 static struct option cbcp_option_list[] = {
57 { "callback", o_special, (void *)setcbcp,
58 "Ask for callback", OPT_PRIO | OPT_A2STRVAL, &cbcp[0].us_number },
59 { NULL }
60 };
61
62 /*
63 * Protocol entry points.
64 */
65 static void cbcp_init (int unit);
66 static void cbcp_open (int unit);
67 static void cbcp_lowerup (int unit);
68 static void cbcp_input (int unit, u_char *pkt, int len);
69 static void cbcp_protrej (int unit);
70 static int cbcp_printpkt (u_char *pkt, int len,
71 void (*printer)(void *, char *, ...),
72 void *arg);
73
74 struct protent cbcp_protent = {
75 PPP_CBCP,
76 cbcp_init,
77 cbcp_input,
78 cbcp_protrej,
79 cbcp_lowerup,
80 NULL,
81 cbcp_open,
82 NULL,
83 cbcp_printpkt,
84 NULL,
85 0,
86 "CBCP",
87 NULL,
88 cbcp_option_list,
89 NULL,
90 NULL,
91 NULL
92 };
93
94 cbcp_state cbcp[NUM_PPP];
95
96 /* internal prototypes */
97
98 static void cbcp_recvreq (cbcp_state *us, u_char *pckt, int len);
99 static void cbcp_resp (cbcp_state *us);
100 static void cbcp_up (cbcp_state *us);
101 static void cbcp_recvack (cbcp_state *us, u_char *pckt, int len);
102 static void cbcp_send (cbcp_state *us, int code, u_char *buf, int len);
103
104 /* option processing */
105 static int
106 setcbcp(char **argv)
107 {
108 lcp_wantoptions[0].neg_cbcp = 1;
109 cbcp_protent.enabled_flag = 1;
110 cbcp[0].us_number = strdup(*argv);
111 if (cbcp[0].us_number == 0)
112 novm("callback number");
113 cbcp[0].us_type |= (1 << CB_CONF_USER);
114 cbcp[0].us_type |= (1 << CB_CONF_ADMIN);
115 return (1);
116 }
117
118 /* init state */
119 static void
120 cbcp_init(int iface)
121 {
122 cbcp_state *us;
123
124 us = &cbcp[iface];
125 memset(us, 0, sizeof(cbcp_state));
126 us->us_unit = iface;
127 us->us_type |= (1 << CB_CONF_NO);
128 }
129
130 /* lower layer is up */
131 static void
132 cbcp_lowerup(int iface)
133 {
134 cbcp_state *us = &cbcp[iface];
135
136 dbglog("cbcp_lowerup");
137 dbglog("want: %d", us->us_type);
138
139 if (us->us_type == CB_CONF_USER)
140 dbglog("phone no: %s", us->us_number);
141 }
142
143 static void
144 cbcp_open(int unit)
145 {
146 dbglog("cbcp_open");
147 }
148
149 /* process an incomming packet */
150 static void
151 cbcp_input(int unit, u_char *inpacket, int pktlen)
152 {
153 u_char *inp;
154 u_char code, id;
155 u_short len;
156
157 cbcp_state *us = &cbcp[unit];
158
159 inp = inpacket;
160
161 if (pktlen < CBCP_MINLEN) {
162 if (debug)
163 dbglog("CBCP packet is too small");
164 return;
165 }
166
167 GETCHAR(code, inp);
168 GETCHAR(id, inp);
169 GETSHORT(len, inp);
170
171 if (len > pktlen || len < CBCP_MINLEN) {
172 if (debug)
173 dbglog("CBCP packet: invalid length %d", len);
174 return;
175 }
176
177 len -= CBCP_MINLEN;
178
179 switch(code) {
180 case CBCP_REQ:
181 us->us_id = id;
182 cbcp_recvreq(us, inp, len);
183 break;
184
185 case CBCP_RESP:
186 if (debug)
187 dbglog("CBCP_RESP received");
188 break;
189
190 case CBCP_ACK:
191 if (debug && id != us->us_id)
192 dbglog("id doesn't match: expected %d recv %d",
193 us->us_id, id);
194
195 cbcp_recvack(us, inp, len);
196 break;
197
198 default:
199 break;
200 }
201 }
202
203 /* protocol was rejected by foe */
204 void cbcp_protrej(int iface)
205 {
206 }
207
208 char *cbcp_codenames[] = {
209 "Request", "Response", "Ack"
210 };
211
212 char *cbcp_optionnames[] = {
213 "NoCallback",
214 "UserDefined",
215 "AdminDefined",
216 "List"
217 };
218
219 /* pretty print a packet */
220 static int
221 cbcp_printpkt(u_char *p, int plen,
222 void (*printer) (void *, char *, ...), void *arg)
223 {
224 int code, opt, id, len, olen, delay;
225 u_char *pstart;
226
227 if (plen < HEADERLEN)
228 return 0;
229 pstart = p;
230 GETCHAR(code, p);
231 GETCHAR(id, p);
232 GETSHORT(len, p);
233 if (len < HEADERLEN || len > plen)
234 return 0;
235
236 if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
237 printer(arg, " %s", cbcp_codenames[code-1]);
238 else
239 printer(arg, " code=0x%x", code);
240
241 printer(arg, " id=0x%x", id);
242 len -= HEADERLEN;
243
244 switch (code) {
245 case CBCP_REQ:
246 case CBCP_RESP:
247 case CBCP_ACK:
248 while(len >= 2) {
249 GETCHAR(opt, p);
250 GETCHAR(olen, p);
251
252 if (olen < 2 || olen > len) {
253 break;
254 }
255
256 printer(arg, " <");
257 len -= olen;
258
259 if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
260 printer(arg, " %s", cbcp_optionnames[opt-1]);
261 else
262 printer(arg, " option=0x%x", opt);
263
264 if (olen > 2) {
265 GETCHAR(delay, p);
266 printer(arg, " delay = %d", delay);
267 }
268
269 if (olen > 3) {
270 int addrt;
271 char str[256];
272
273 GETCHAR(addrt, p);
274 memcpy(str, p, olen - 4);
275 str[olen - 4] = 0;
276 printer(arg, " number = %s", str);
277 }
278 printer(arg, ">");
279 }
280 break;
281
282 default:
283 break;
284 }
285
286 for (; len > 0; --len) {
287 GETCHAR(code, p);
288 printer(arg, " %.2x", code);
289 }
290
291 return p - pstart;
292 }
293
294 /* received CBCP request */
295 static void
296 cbcp_recvreq(cbcp_state *us, u_char *pckt, int pcktlen)
297 {
298 u_char type, opt_len, delay, addr_type;
299 char address[256];
300 int len = pcktlen;
301
302 address[0] = 0;
303
304 while (len >= 2) {
305 dbglog("length: %d", len);
306
307 GETCHAR(type, pckt);
308 GETCHAR(opt_len, pckt);
309 if (opt_len < 2 || opt_len > len)
310 break;
311
312 if (opt_len > 2)
313 GETCHAR(delay, pckt);
314
315 us->us_allowed |= (1 << type);
316
317 switch(type) {
318 case CB_CONF_NO:
319 dbglog("no callback allowed");
320 break;
321
322 case CB_CONF_USER:
323 dbglog("user callback allowed");
324 if (opt_len > 4) {
325 GETCHAR(addr_type, pckt);
326 memcpy(address, pckt, opt_len - 4);
327 address[opt_len - 4] = 0;
328 if (address[0])
329 dbglog("address: %s", address);
330 }
331 break;
332
333 case CB_CONF_ADMIN:
334 dbglog("user admin defined allowed");
335 break;
336
337 case CB_CONF_LIST:
338 break;
339 }
340 len -= opt_len;
341 }
342 if (len != 0) {
343 if (debug)
344 dbglog("cbcp_recvreq: malformed packet (%d bytes left)", len);
345 return;
346 }
347
348 cbcp_resp(us);
349 }
350
351 static void
352 cbcp_resp(cbcp_state *us)
353 {
354 u_char cb_type;
355 u_char buf[256];
356 u_char *bufp = buf;
357 int len = 0;
358 int slen;
359
360 cb_type = us->us_allowed & us->us_type;
361 dbglog("cbcp_resp cb_type=%d", cb_type);
362
363 #if 0
364 if (!cb_type)
365 lcp_down(us->us_unit);
366 #endif
367
368 if (cb_type & ( 1 << CB_CONF_USER ) ) {
369 dbglog("cbcp_resp CONF_USER");
370 slen = strlen(us->us_number);
371 if (slen > 250) {
372 warn("callback number truncated to 250 characters");
373 slen = 250;
374 }
375 PUTCHAR(CB_CONF_USER, bufp);
376 len = 3 + 1 + slen + 1;
377 PUTCHAR(len , bufp);
378 PUTCHAR(5, bufp); /* delay */
379 PUTCHAR(1, bufp);
380 BCOPY(us->us_number, bufp, slen + 1);
381 cbcp_send(us, CBCP_RESP, buf, len);
382 return;
383 }
384
385 if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
386 dbglog("cbcp_resp CONF_ADMIN");
387 PUTCHAR(CB_CONF_ADMIN, bufp);
388 len = 3;
389 PUTCHAR(len, bufp);
390 PUTCHAR(5, bufp); /* delay */
391 cbcp_send(us, CBCP_RESP, buf, len);
392 return;
393 }
394
395 if (cb_type & ( 1 << CB_CONF_NO ) ) {
396 dbglog("cbcp_resp CONF_NO");
397 PUTCHAR(CB_CONF_NO, bufp);
398 len = 2;
399 PUTCHAR(len , bufp);
400 cbcp_send(us, CBCP_RESP, buf, len);
401 start_networks(us->us_unit);
402 return;
403 }
404 }
405
406 static void
407 cbcp_send(cbcp_state *us, int code, u_char *buf, int len)
408 {
409 u_char *outp;
410 int outlen;
411
412 outp = outpacket_buf;
413
414 outlen = 4 + len;
415
416 MAKEHEADER(outp, PPP_CBCP);
417
418 PUTCHAR(code, outp);
419 PUTCHAR(us->us_id, outp);
420 PUTSHORT(outlen, outp);
421
422 if (len)
423 BCOPY(buf, outp, len);
424
425 output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
426 }
427
428 static void
429 cbcp_recvack(cbcp_state *us, u_char *pckt, int len)
430 {
431 u_char type, delay, addr_type;
432 int opt_len;
433 char address[256];
434
435 if (len >= 2) {
436 GETCHAR(type, pckt);
437 GETCHAR(opt_len, pckt);
438 if (opt_len >= 2 && opt_len <= len) {
439
440 if (opt_len > 2)
441 GETCHAR(delay, pckt);
442
443 if (opt_len > 4) {
444 GETCHAR(addr_type, pckt);
445 memcpy(address, pckt, opt_len - 4);
446 address[opt_len - 4] = 0;
447 if (address[0])
448 dbglog("peer will call: %s", address);
449 }
450 if (type == CB_CONF_NO)
451 return;
452
453 cbcp_up(us);
454
455 } else if (debug)
456 dbglog("cbcp_recvack: malformed packet");
457 }
458 }
459
460 /* ok peer will do callback */
461 static void
462 cbcp_up(cbcp_state *us)
463 {
464 persist = 0;
465 ppp_set_status(EXIT_CALLBACK);
466 lcp_close(0, "Call me back, please");
467 }
468