rose_subr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/types.h>
  11. #include <linux/socket.h>
  12. #include <linux/in.h>
  13. #include <linux/kernel.h>
  14. #include <linux/timer.h>
  15. #include <linux/string.h>
  16. #include <linux/sockios.h>
  17. #include <linux/net.h>
  18. #include <linux/slab.h>
  19. #include <net/ax25.h>
  20. #include <linux/inet.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/skbuff.h>
  23. #include <net/sock.h>
  24. #include <net/tcp_states.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/mm.h>
  27. #include <linux/interrupt.h>
  28. #include <net/rose.h>
  29. static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose);
  30. /*
  31. * This routine purges all of the queues of frames.
  32. */
  33. void rose_clear_queues(struct sock *sk)
  34. {
  35. skb_queue_purge(&sk->sk_write_queue);
  36. skb_queue_purge(&rose_sk(sk)->ack_queue);
  37. }
  38. /*
  39. * This routine purges the input queue of those frames that have been
  40. * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
  41. * SDL diagram.
  42. */
  43. void rose_frames_acked(struct sock *sk, unsigned short nr)
  44. {
  45. struct sk_buff *skb;
  46. struct rose_sock *rose = rose_sk(sk);
  47. /*
  48. * Remove all the ack-ed frames from the ack queue.
  49. */
  50. if (rose->va != nr) {
  51. while (skb_peek(&rose->ack_queue) != NULL && rose->va != nr) {
  52. skb = skb_dequeue(&rose->ack_queue);
  53. kfree_skb(skb);
  54. rose->va = (rose->va + 1) % ROSE_MODULUS;
  55. }
  56. }
  57. }
  58. void rose_requeue_frames(struct sock *sk)
  59. {
  60. struct sk_buff *skb, *skb_prev = NULL;
  61. /*
  62. * Requeue all the un-ack-ed frames on the output queue to be picked
  63. * up by rose_kick. This arrangement handles the possibility of an
  64. * empty output queue.
  65. */
  66. while ((skb = skb_dequeue(&rose_sk(sk)->ack_queue)) != NULL) {
  67. if (skb_prev == NULL)
  68. skb_queue_head(&sk->sk_write_queue, skb);
  69. else
  70. skb_append(skb_prev, skb, &sk->sk_write_queue);
  71. skb_prev = skb;
  72. }
  73. }
  74. /*
  75. * Validate that the value of nr is between va and vs. Return true or
  76. * false for testing.
  77. */
  78. int rose_validate_nr(struct sock *sk, unsigned short nr)
  79. {
  80. struct rose_sock *rose = rose_sk(sk);
  81. unsigned short vc = rose->va;
  82. while (vc != rose->vs) {
  83. if (nr == vc) return 1;
  84. vc = (vc + 1) % ROSE_MODULUS;
  85. }
  86. return nr == rose->vs;
  87. }
  88. /*
  89. * This routine is called when the packet layer internally generates a
  90. * control frame.
  91. */
  92. void rose_write_internal(struct sock *sk, int frametype)
  93. {
  94. struct rose_sock *rose = rose_sk(sk);
  95. struct sk_buff *skb;
  96. unsigned char *dptr;
  97. unsigned char lci1, lci2;
  98. char buffer[100];
  99. int len, faclen = 0;
  100. len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
  101. switch (frametype) {
  102. case ROSE_CALL_REQUEST:
  103. len += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN;
  104. faclen = rose_create_facilities(buffer, rose);
  105. len += faclen;
  106. break;
  107. case ROSE_CALL_ACCEPTED:
  108. case ROSE_CLEAR_REQUEST:
  109. case ROSE_RESET_REQUEST:
  110. len += 2;
  111. break;
  112. }
  113. if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
  114. return;
  115. /*
  116. * Space for AX.25 header and PID.
  117. */
  118. skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1);
  119. dptr = skb_put(skb, skb_tailroom(skb));
  120. lci1 = (rose->lci >> 8) & 0x0F;
  121. lci2 = (rose->lci >> 0) & 0xFF;
  122. switch (frametype) {
  123. case ROSE_CALL_REQUEST:
  124. *dptr++ = ROSE_GFI | lci1;
  125. *dptr++ = lci2;
  126. *dptr++ = frametype;
  127. *dptr++ = ROSE_CALL_REQ_ADDR_LEN_VAL;
  128. memcpy(dptr, &rose->dest_addr, ROSE_ADDR_LEN);
  129. dptr += ROSE_ADDR_LEN;
  130. memcpy(dptr, &rose->source_addr, ROSE_ADDR_LEN);
  131. dptr += ROSE_ADDR_LEN;
  132. memcpy(dptr, buffer, faclen);
  133. dptr += faclen;
  134. break;
  135. case ROSE_CALL_ACCEPTED:
  136. *dptr++ = ROSE_GFI | lci1;
  137. *dptr++ = lci2;
  138. *dptr++ = frametype;
  139. *dptr++ = 0x00; /* Address length */
  140. *dptr++ = 0; /* Facilities length */
  141. break;
  142. case ROSE_CLEAR_REQUEST:
  143. *dptr++ = ROSE_GFI | lci1;
  144. *dptr++ = lci2;
  145. *dptr++ = frametype;
  146. *dptr++ = rose->cause;
  147. *dptr++ = rose->diagnostic;
  148. break;
  149. case ROSE_RESET_REQUEST:
  150. *dptr++ = ROSE_GFI | lci1;
  151. *dptr++ = lci2;
  152. *dptr++ = frametype;
  153. *dptr++ = ROSE_DTE_ORIGINATED;
  154. *dptr++ = 0;
  155. break;
  156. case ROSE_RR:
  157. case ROSE_RNR:
  158. *dptr++ = ROSE_GFI | lci1;
  159. *dptr++ = lci2;
  160. *dptr = frametype;
  161. *dptr++ |= (rose->vr << 5) & 0xE0;
  162. break;
  163. case ROSE_CLEAR_CONFIRMATION:
  164. case ROSE_RESET_CONFIRMATION:
  165. *dptr++ = ROSE_GFI | lci1;
  166. *dptr++ = lci2;
  167. *dptr++ = frametype;
  168. break;
  169. default:
  170. printk(KERN_ERR "ROSE: rose_write_internal - invalid frametype %02X\n", frametype);
  171. kfree_skb(skb);
  172. return;
  173. }
  174. rose_transmit_link(skb, rose->neighbour);
  175. }
  176. int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m)
  177. {
  178. unsigned char *frame;
  179. frame = skb->data;
  180. *ns = *nr = *q = *d = *m = 0;
  181. switch (frame[2]) {
  182. case ROSE_CALL_REQUEST:
  183. case ROSE_CALL_ACCEPTED:
  184. case ROSE_CLEAR_REQUEST:
  185. case ROSE_CLEAR_CONFIRMATION:
  186. case ROSE_RESET_REQUEST:
  187. case ROSE_RESET_CONFIRMATION:
  188. return frame[2];
  189. default:
  190. break;
  191. }
  192. if ((frame[2] & 0x1F) == ROSE_RR ||
  193. (frame[2] & 0x1F) == ROSE_RNR) {
  194. *nr = (frame[2] >> 5) & 0x07;
  195. return frame[2] & 0x1F;
  196. }
  197. if ((frame[2] & 0x01) == ROSE_DATA) {
  198. *q = (frame[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
  199. *d = (frame[0] & ROSE_D_BIT) == ROSE_D_BIT;
  200. *m = (frame[2] & ROSE_M_BIT) == ROSE_M_BIT;
  201. *nr = (frame[2] >> 5) & 0x07;
  202. *ns = (frame[2] >> 1) & 0x07;
  203. return ROSE_DATA;
  204. }
  205. return ROSE_ILLEGAL;
  206. }
  207. static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *facilities, int len)
  208. {
  209. unsigned char *pt;
  210. unsigned char l, lg, n = 0;
  211. int fac_national_digis_received = 0;
  212. do {
  213. switch (*p & 0xC0) {
  214. case 0x00:
  215. if (len < 2)
  216. return -1;
  217. p += 2;
  218. n += 2;
  219. len -= 2;
  220. break;
  221. case 0x40:
  222. if (len < 3)
  223. return -1;
  224. if (*p == FAC_NATIONAL_RAND)
  225. facilities->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF);
  226. p += 3;
  227. n += 3;
  228. len -= 3;
  229. break;
  230. case 0x80:
  231. if (len < 4)
  232. return -1;
  233. p += 4;
  234. n += 4;
  235. len -= 4;
  236. break;
  237. case 0xC0:
  238. if (len < 2)
  239. return -1;
  240. l = p[1];
  241. if (len < 2 + l)
  242. return -1;
  243. if (*p == FAC_NATIONAL_DEST_DIGI) {
  244. if (!fac_national_digis_received) {
  245. if (l < AX25_ADDR_LEN)
  246. return -1;
  247. memcpy(&facilities->source_digis[0], p + 2, AX25_ADDR_LEN);
  248. facilities->source_ndigis = 1;
  249. }
  250. }
  251. else if (*p == FAC_NATIONAL_SRC_DIGI) {
  252. if (!fac_national_digis_received) {
  253. if (l < AX25_ADDR_LEN)
  254. return -1;
  255. memcpy(&facilities->dest_digis[0], p + 2, AX25_ADDR_LEN);
  256. facilities->dest_ndigis = 1;
  257. }
  258. }
  259. else if (*p == FAC_NATIONAL_FAIL_CALL) {
  260. if (l < AX25_ADDR_LEN)
  261. return -1;
  262. memcpy(&facilities->fail_call, p + 2, AX25_ADDR_LEN);
  263. }
  264. else if (*p == FAC_NATIONAL_FAIL_ADD) {
  265. if (l < 1 + ROSE_ADDR_LEN)
  266. return -1;
  267. memcpy(&facilities->fail_addr, p + 3, ROSE_ADDR_LEN);
  268. }
  269. else if (*p == FAC_NATIONAL_DIGIS) {
  270. if (l % AX25_ADDR_LEN)
  271. return -1;
  272. fac_national_digis_received = 1;
  273. facilities->source_ndigis = 0;
  274. facilities->dest_ndigis = 0;
  275. for (pt = p + 2, lg = 0 ; lg < l ; pt += AX25_ADDR_LEN, lg += AX25_ADDR_LEN) {
  276. if (pt[6] & AX25_HBIT) {
  277. if (facilities->dest_ndigis >= ROSE_MAX_DIGIS)
  278. return -1;
  279. memcpy(&facilities->dest_digis[facilities->dest_ndigis++], pt, AX25_ADDR_LEN);
  280. } else {
  281. if (facilities->source_ndigis >= ROSE_MAX_DIGIS)
  282. return -1;
  283. memcpy(&facilities->source_digis[facilities->source_ndigis++], pt, AX25_ADDR_LEN);
  284. }
  285. }
  286. }
  287. p += l + 2;
  288. n += l + 2;
  289. len -= l + 2;
  290. break;
  291. }
  292. } while (*p != 0x00 && len > 0);
  293. return n;
  294. }
  295. static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *facilities, int len)
  296. {
  297. unsigned char l, n = 0;
  298. char callsign[11];
  299. do {
  300. switch (*p & 0xC0) {
  301. case 0x00:
  302. if (len < 2)
  303. return -1;
  304. p += 2;
  305. n += 2;
  306. len -= 2;
  307. break;
  308. case 0x40:
  309. if (len < 3)
  310. return -1;
  311. p += 3;
  312. n += 3;
  313. len -= 3;
  314. break;
  315. case 0x80:
  316. if (len < 4)
  317. return -1;
  318. p += 4;
  319. n += 4;
  320. len -= 4;
  321. break;
  322. case 0xC0:
  323. if (len < 2)
  324. return -1;
  325. l = p[1];
  326. /* Prevent overflows*/
  327. if (l < 10 || l > 20)
  328. return -1;
  329. if (*p == FAC_CCITT_DEST_NSAP) {
  330. memcpy(&facilities->source_addr, p + 7, ROSE_ADDR_LEN);
  331. memcpy(callsign, p + 12, l - 10);
  332. callsign[l - 10] = '\0';
  333. asc2ax(&facilities->source_call, callsign);
  334. }
  335. if (*p == FAC_CCITT_SRC_NSAP) {
  336. memcpy(&facilities->dest_addr, p + 7, ROSE_ADDR_LEN);
  337. memcpy(callsign, p + 12, l - 10);
  338. callsign[l - 10] = '\0';
  339. asc2ax(&facilities->dest_call, callsign);
  340. }
  341. p += l + 2;
  342. n += l + 2;
  343. len -= l + 2;
  344. break;
  345. }
  346. } while (*p != 0x00 && len > 0);
  347. return n;
  348. }
  349. int rose_parse_facilities(unsigned char *p, unsigned packet_len,
  350. struct rose_facilities_struct *facilities)
  351. {
  352. int facilities_len, len;
  353. facilities_len = *p++;
  354. if (facilities_len == 0 || (unsigned int)facilities_len > packet_len)
  355. return 0;
  356. while (facilities_len >= 3 && *p == 0x00) {
  357. facilities_len--;
  358. p++;
  359. switch (*p) {
  360. case FAC_NATIONAL: /* National */
  361. len = rose_parse_national(p + 1, facilities, facilities_len - 1);
  362. break;
  363. case FAC_CCITT: /* CCITT */
  364. len = rose_parse_ccitt(p + 1, facilities, facilities_len - 1);
  365. break;
  366. default:
  367. printk(KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X\n", *p);
  368. len = 1;
  369. break;
  370. }
  371. if (len < 0)
  372. return 0;
  373. if (WARN_ON(len >= facilities_len))
  374. return 0;
  375. facilities_len -= len + 1;
  376. p += len + 1;
  377. }
  378. return facilities_len == 0;
  379. }
  380. static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose)
  381. {
  382. unsigned char *p = buffer + 1;
  383. char *callsign;
  384. char buf[11];
  385. int len, nb;
  386. /* National Facilities */
  387. if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) {
  388. *p++ = 0x00;
  389. *p++ = FAC_NATIONAL;
  390. if (rose->rand != 0) {
  391. *p++ = FAC_NATIONAL_RAND;
  392. *p++ = (rose->rand >> 8) & 0xFF;
  393. *p++ = (rose->rand >> 0) & 0xFF;
  394. }
  395. /* Sent before older facilities */
  396. if ((rose->source_ndigis > 0) || (rose->dest_ndigis > 0)) {
  397. int maxdigi = 0;
  398. *p++ = FAC_NATIONAL_DIGIS;
  399. *p++ = AX25_ADDR_LEN * (rose->source_ndigis + rose->dest_ndigis);
  400. for (nb = 0 ; nb < rose->source_ndigis ; nb++) {
  401. if (++maxdigi >= ROSE_MAX_DIGIS)
  402. break;
  403. memcpy(p, &rose->source_digis[nb], AX25_ADDR_LEN);
  404. p[6] |= AX25_HBIT;
  405. p += AX25_ADDR_LEN;
  406. }
  407. for (nb = 0 ; nb < rose->dest_ndigis ; nb++) {
  408. if (++maxdigi >= ROSE_MAX_DIGIS)
  409. break;
  410. memcpy(p, &rose->dest_digis[nb], AX25_ADDR_LEN);
  411. p[6] &= ~AX25_HBIT;
  412. p += AX25_ADDR_LEN;
  413. }
  414. }
  415. /* For compatibility */
  416. if (rose->source_ndigis > 0) {
  417. *p++ = FAC_NATIONAL_SRC_DIGI;
  418. *p++ = AX25_ADDR_LEN;
  419. memcpy(p, &rose->source_digis[0], AX25_ADDR_LEN);
  420. p += AX25_ADDR_LEN;
  421. }
  422. /* For compatibility */
  423. if (rose->dest_ndigis > 0) {
  424. *p++ = FAC_NATIONAL_DEST_DIGI;
  425. *p++ = AX25_ADDR_LEN;
  426. memcpy(p, &rose->dest_digis[0], AX25_ADDR_LEN);
  427. p += AX25_ADDR_LEN;
  428. }
  429. }
  430. *p++ = 0x00;
  431. *p++ = FAC_CCITT;
  432. *p++ = FAC_CCITT_DEST_NSAP;
  433. callsign = ax2asc(buf, &rose->dest_call);
  434. *p++ = strlen(callsign) + 10;
  435. *p++ = (strlen(callsign) + 9) * 2; /* ??? */
  436. *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
  437. *p++ = ROSE_ADDR_LEN * 2;
  438. memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN);
  439. p += ROSE_ADDR_LEN;
  440. memcpy(p, callsign, strlen(callsign));
  441. p += strlen(callsign);
  442. *p++ = FAC_CCITT_SRC_NSAP;
  443. callsign = ax2asc(buf, &rose->source_call);
  444. *p++ = strlen(callsign) + 10;
  445. *p++ = (strlen(callsign) + 9) * 2; /* ??? */
  446. *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
  447. *p++ = ROSE_ADDR_LEN * 2;
  448. memcpy(p, &rose->source_addr, ROSE_ADDR_LEN);
  449. p += ROSE_ADDR_LEN;
  450. memcpy(p, callsign, strlen(callsign));
  451. p += strlen(callsign);
  452. len = p - buffer;
  453. buffer[0] = len - 1;
  454. return len;
  455. }
  456. void rose_disconnect(struct sock *sk, int reason, int cause, int diagnostic)
  457. {
  458. struct rose_sock *rose = rose_sk(sk);
  459. rose_stop_timer(sk);
  460. rose_stop_idletimer(sk);
  461. rose_clear_queues(sk);
  462. rose->lci = 0;
  463. rose->state = ROSE_STATE_0;
  464. if (cause != -1)
  465. rose->cause = cause;
  466. if (diagnostic != -1)
  467. rose->diagnostic = diagnostic;
  468. sk->sk_state = TCP_CLOSE;
  469. sk->sk_err = reason;
  470. sk->sk_shutdown |= SEND_SHUTDOWN;
  471. if (!sock_flag(sk, SOCK_DEAD)) {
  472. sk->sk_state_change(sk);
  473. sock_set_flag(sk, SOCK_DEAD);
  474. }
  475. }