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