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