rose_subr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. int maxfaclen = 0;
  99. int len, faclen;
  100. int reserve;
  101. reserve = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1;
  102. len = ROSE_MIN_LEN;
  103. switch (frametype) {
  104. case ROSE_CALL_REQUEST:
  105. len += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN;
  106. maxfaclen = 256;
  107. break;
  108. case ROSE_CALL_ACCEPTED:
  109. case ROSE_CLEAR_REQUEST:
  110. case ROSE_RESET_REQUEST:
  111. len += 2;
  112. break;
  113. }
  114. skb = alloc_skb(reserve + len + maxfaclen, GFP_ATOMIC);
  115. if (!skb)
  116. return;
  117. /*
  118. * Space for AX.25 header and PID.
  119. */
  120. skb_reserve(skb, reserve);
  121. dptr = skb_put(skb, len);
  122. lci1 = (rose->lci >> 8) & 0x0F;
  123. lci2 = (rose->lci >> 0) & 0xFF;
  124. switch (frametype) {
  125. case ROSE_CALL_REQUEST:
  126. *dptr++ = ROSE_GFI | lci1;
  127. *dptr++ = lci2;
  128. *dptr++ = frametype;
  129. *dptr++ = ROSE_CALL_REQ_ADDR_LEN_VAL;
  130. memcpy(dptr, &rose->dest_addr, ROSE_ADDR_LEN);
  131. dptr += ROSE_ADDR_LEN;
  132. memcpy(dptr, &rose->source_addr, ROSE_ADDR_LEN);
  133. dptr += ROSE_ADDR_LEN;
  134. faclen = rose_create_facilities(dptr, rose);
  135. skb_put(skb, faclen);
  136. dptr += faclen;
  137. break;
  138. case ROSE_CALL_ACCEPTED:
  139. *dptr++ = ROSE_GFI | lci1;
  140. *dptr++ = lci2;
  141. *dptr++ = frametype;
  142. *dptr++ = 0x00; /* Address length */
  143. *dptr++ = 0; /* Facilities length */
  144. break;
  145. case ROSE_CLEAR_REQUEST:
  146. *dptr++ = ROSE_GFI | lci1;
  147. *dptr++ = lci2;
  148. *dptr++ = frametype;
  149. *dptr++ = rose->cause;
  150. *dptr++ = rose->diagnostic;
  151. break;
  152. case ROSE_RESET_REQUEST:
  153. *dptr++ = ROSE_GFI | lci1;
  154. *dptr++ = lci2;
  155. *dptr++ = frametype;
  156. *dptr++ = ROSE_DTE_ORIGINATED;
  157. *dptr++ = 0;
  158. break;
  159. case ROSE_RR:
  160. case ROSE_RNR:
  161. *dptr++ = ROSE_GFI | lci1;
  162. *dptr++ = lci2;
  163. *dptr = frametype;
  164. *dptr++ |= (rose->vr << 5) & 0xE0;
  165. break;
  166. case ROSE_CLEAR_CONFIRMATION:
  167. case ROSE_RESET_CONFIRMATION:
  168. *dptr++ = ROSE_GFI | lci1;
  169. *dptr++ = lci2;
  170. *dptr++ = frametype;
  171. break;
  172. default:
  173. printk(KERN_ERR "ROSE: rose_write_internal - invalid frametype %02X\n", frametype);
  174. kfree_skb(skb);
  175. return;
  176. }
  177. rose_transmit_link(skb, rose->neighbour);
  178. }
  179. int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m)
  180. {
  181. unsigned char *frame;
  182. frame = skb->data;
  183. *ns = *nr = *q = *d = *m = 0;
  184. switch (frame[2]) {
  185. case ROSE_CALL_REQUEST:
  186. case ROSE_CALL_ACCEPTED:
  187. case ROSE_CLEAR_REQUEST:
  188. case ROSE_CLEAR_CONFIRMATION:
  189. case ROSE_RESET_REQUEST:
  190. case ROSE_RESET_CONFIRMATION:
  191. return frame[2];
  192. default:
  193. break;
  194. }
  195. if ((frame[2] & 0x1F) == ROSE_RR ||
  196. (frame[2] & 0x1F) == ROSE_RNR) {
  197. *nr = (frame[2] >> 5) & 0x07;
  198. return frame[2] & 0x1F;
  199. }
  200. if ((frame[2] & 0x01) == ROSE_DATA) {
  201. *q = (frame[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
  202. *d = (frame[0] & ROSE_D_BIT) == ROSE_D_BIT;
  203. *m = (frame[2] & ROSE_M_BIT) == ROSE_M_BIT;
  204. *nr = (frame[2] >> 5) & 0x07;
  205. *ns = (frame[2] >> 1) & 0x07;
  206. return ROSE_DATA;
  207. }
  208. return ROSE_ILLEGAL;
  209. }
  210. static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *facilities, int len)
  211. {
  212. unsigned char *pt;
  213. unsigned char l, lg, n = 0;
  214. int fac_national_digis_received = 0;
  215. do {
  216. switch (*p & 0xC0) {
  217. case 0x00:
  218. if (len < 2)
  219. return -1;
  220. p += 2;
  221. n += 2;
  222. len -= 2;
  223. break;
  224. case 0x40:
  225. if (len < 3)
  226. return -1;
  227. if (*p == FAC_NATIONAL_RAND)
  228. facilities->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF);
  229. p += 3;
  230. n += 3;
  231. len -= 3;
  232. break;
  233. case 0x80:
  234. if (len < 4)
  235. return -1;
  236. p += 4;
  237. n += 4;
  238. len -= 4;
  239. break;
  240. case 0xC0:
  241. if (len < 2)
  242. return -1;
  243. l = p[1];
  244. if (len < 2 + l)
  245. return -1;
  246. if (*p == FAC_NATIONAL_DEST_DIGI) {
  247. if (!fac_national_digis_received) {
  248. if (l < AX25_ADDR_LEN)
  249. return -1;
  250. memcpy(&facilities->source_digis[0], p + 2, AX25_ADDR_LEN);
  251. facilities->source_ndigis = 1;
  252. }
  253. }
  254. else if (*p == FAC_NATIONAL_SRC_DIGI) {
  255. if (!fac_national_digis_received) {
  256. if (l < AX25_ADDR_LEN)
  257. return -1;
  258. memcpy(&facilities->dest_digis[0], p + 2, AX25_ADDR_LEN);
  259. facilities->dest_ndigis = 1;
  260. }
  261. }
  262. else if (*p == FAC_NATIONAL_FAIL_CALL) {
  263. if (l < AX25_ADDR_LEN)
  264. return -1;
  265. memcpy(&facilities->fail_call, p + 2, AX25_ADDR_LEN);
  266. }
  267. else if (*p == FAC_NATIONAL_FAIL_ADD) {
  268. if (l < 1 + ROSE_ADDR_LEN)
  269. return -1;
  270. memcpy(&facilities->fail_addr, p + 3, ROSE_ADDR_LEN);
  271. }
  272. else if (*p == FAC_NATIONAL_DIGIS) {
  273. if (l % AX25_ADDR_LEN)
  274. return -1;
  275. fac_national_digis_received = 1;
  276. facilities->source_ndigis = 0;
  277. facilities->dest_ndigis = 0;
  278. for (pt = p + 2, lg = 0 ; lg < l ; pt += AX25_ADDR_LEN, lg += AX25_ADDR_LEN) {
  279. if (pt[6] & AX25_HBIT) {
  280. if (facilities->dest_ndigis >= ROSE_MAX_DIGIS)
  281. return -1;
  282. memcpy(&facilities->dest_digis[facilities->dest_ndigis++], pt, AX25_ADDR_LEN);
  283. } else {
  284. if (facilities->source_ndigis >= ROSE_MAX_DIGIS)
  285. return -1;
  286. memcpy(&facilities->source_digis[facilities->source_ndigis++], pt, AX25_ADDR_LEN);
  287. }
  288. }
  289. }
  290. p += l + 2;
  291. n += l + 2;
  292. len -= l + 2;
  293. break;
  294. }
  295. } while (*p != 0x00 && len > 0);
  296. return n;
  297. }
  298. static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *facilities, int len)
  299. {
  300. unsigned char l, n = 0;
  301. char callsign[11];
  302. do {
  303. switch (*p & 0xC0) {
  304. case 0x00:
  305. if (len < 2)
  306. return -1;
  307. p += 2;
  308. n += 2;
  309. len -= 2;
  310. break;
  311. case 0x40:
  312. if (len < 3)
  313. return -1;
  314. p += 3;
  315. n += 3;
  316. len -= 3;
  317. break;
  318. case 0x80:
  319. if (len < 4)
  320. return -1;
  321. p += 4;
  322. n += 4;
  323. len -= 4;
  324. break;
  325. case 0xC0:
  326. if (len < 2)
  327. return -1;
  328. l = p[1];
  329. /* Prevent overflows*/
  330. if (l < 10 || l > 20)
  331. return -1;
  332. if (*p == FAC_CCITT_DEST_NSAP) {
  333. memcpy(&facilities->source_addr, p + 7, ROSE_ADDR_LEN);
  334. memcpy(callsign, p + 12, l - 10);
  335. callsign[l - 10] = '\0';
  336. asc2ax(&facilities->source_call, callsign);
  337. }
  338. if (*p == FAC_CCITT_SRC_NSAP) {
  339. memcpy(&facilities->dest_addr, p + 7, ROSE_ADDR_LEN);
  340. memcpy(callsign, p + 12, l - 10);
  341. callsign[l - 10] = '\0';
  342. asc2ax(&facilities->dest_call, callsign);
  343. }
  344. p += l + 2;
  345. n += l + 2;
  346. len -= l + 2;
  347. break;
  348. }
  349. } while (*p != 0x00 && len > 0);
  350. return n;
  351. }
  352. int rose_parse_facilities(unsigned char *p, unsigned packet_len,
  353. struct rose_facilities_struct *facilities)
  354. {
  355. int facilities_len, len;
  356. facilities_len = *p++;
  357. if (facilities_len == 0 || (unsigned int)facilities_len > packet_len)
  358. return 0;
  359. while (facilities_len >= 3 && *p == 0x00) {
  360. facilities_len--;
  361. p++;
  362. switch (*p) {
  363. case FAC_NATIONAL: /* National */
  364. len = rose_parse_national(p + 1, facilities, facilities_len - 1);
  365. break;
  366. case FAC_CCITT: /* CCITT */
  367. len = rose_parse_ccitt(p + 1, facilities, facilities_len - 1);
  368. break;
  369. default:
  370. printk(KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X\n", *p);
  371. len = 1;
  372. break;
  373. }
  374. if (len < 0)
  375. return 0;
  376. if (WARN_ON(len >= facilities_len))
  377. return 0;
  378. facilities_len -= len + 1;
  379. p += len + 1;
  380. }
  381. return facilities_len == 0;
  382. }
  383. static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose)
  384. {
  385. unsigned char *p = buffer + 1;
  386. char *callsign;
  387. char buf[11];
  388. int len, nb;
  389. /* National Facilities */
  390. if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) {
  391. *p++ = 0x00;
  392. *p++ = FAC_NATIONAL;
  393. if (rose->rand != 0) {
  394. *p++ = FAC_NATIONAL_RAND;
  395. *p++ = (rose->rand >> 8) & 0xFF;
  396. *p++ = (rose->rand >> 0) & 0xFF;
  397. }
  398. /* Sent before older facilities */
  399. if ((rose->source_ndigis > 0) || (rose->dest_ndigis > 0)) {
  400. int maxdigi = 0;
  401. *p++ = FAC_NATIONAL_DIGIS;
  402. *p++ = AX25_ADDR_LEN * (rose->source_ndigis + rose->dest_ndigis);
  403. for (nb = 0 ; nb < rose->source_ndigis ; nb++) {
  404. if (++maxdigi >= ROSE_MAX_DIGIS)
  405. break;
  406. memcpy(p, &rose->source_digis[nb], AX25_ADDR_LEN);
  407. p[6] |= AX25_HBIT;
  408. p += AX25_ADDR_LEN;
  409. }
  410. for (nb = 0 ; nb < rose->dest_ndigis ; nb++) {
  411. if (++maxdigi >= ROSE_MAX_DIGIS)
  412. break;
  413. memcpy(p, &rose->dest_digis[nb], AX25_ADDR_LEN);
  414. p[6] &= ~AX25_HBIT;
  415. p += AX25_ADDR_LEN;
  416. }
  417. }
  418. /* For compatibility */
  419. if (rose->source_ndigis > 0) {
  420. *p++ = FAC_NATIONAL_SRC_DIGI;
  421. *p++ = AX25_ADDR_LEN;
  422. memcpy(p, &rose->source_digis[0], AX25_ADDR_LEN);
  423. p += AX25_ADDR_LEN;
  424. }
  425. /* For compatibility */
  426. if (rose->dest_ndigis > 0) {
  427. *p++ = FAC_NATIONAL_DEST_DIGI;
  428. *p++ = AX25_ADDR_LEN;
  429. memcpy(p, &rose->dest_digis[0], AX25_ADDR_LEN);
  430. p += AX25_ADDR_LEN;
  431. }
  432. }
  433. *p++ = 0x00;
  434. *p++ = FAC_CCITT;
  435. *p++ = FAC_CCITT_DEST_NSAP;
  436. callsign = ax2asc(buf, &rose->dest_call);
  437. *p++ = strlen(callsign) + 10;
  438. *p++ = (strlen(callsign) + 9) * 2; /* ??? */
  439. *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
  440. *p++ = ROSE_ADDR_LEN * 2;
  441. memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN);
  442. p += ROSE_ADDR_LEN;
  443. memcpy(p, callsign, strlen(callsign));
  444. p += strlen(callsign);
  445. *p++ = FAC_CCITT_SRC_NSAP;
  446. callsign = ax2asc(buf, &rose->source_call);
  447. *p++ = strlen(callsign) + 10;
  448. *p++ = (strlen(callsign) + 9) * 2; /* ??? */
  449. *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
  450. *p++ = ROSE_ADDR_LEN * 2;
  451. memcpy(p, &rose->source_addr, ROSE_ADDR_LEN);
  452. p += ROSE_ADDR_LEN;
  453. memcpy(p, callsign, strlen(callsign));
  454. p += strlen(callsign);
  455. len = p - buffer;
  456. buffer[0] = len - 1;
  457. return len;
  458. }
  459. void rose_disconnect(struct sock *sk, int reason, int cause, int diagnostic)
  460. {
  461. struct rose_sock *rose = rose_sk(sk);
  462. rose_stop_timer(sk);
  463. rose_stop_idletimer(sk);
  464. rose_clear_queues(sk);
  465. rose->lci = 0;
  466. rose->state = ROSE_STATE_0;
  467. if (cause != -1)
  468. rose->cause = cause;
  469. if (diagnostic != -1)
  470. rose->diagnostic = diagnostic;
  471. sk->sk_state = TCP_CLOSE;
  472. sk->sk_err = reason;
  473. sk->sk_shutdown |= SEND_SHUTDOWN;
  474. if (!sock_flag(sk, SOCK_DEAD)) {
  475. sk->sk_state_change(sk);
  476. sock_set_flag(sk, SOCK_DEAD);
  477. }
  478. }