cbcp.c 9.8 KB

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