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