rose_subr.c 12 KB

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