carminefb: Fix module parameters permissions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / rose / rose_subr.c
blob07bca7d16602d6ee77dd168bf73ed36214723955
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.
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 <net/ax25.h>
19 #include <linux/inet.h>
20 #include <linux/netdevice.h>
21 #include <linux/skbuff.h>
22 #include <net/sock.h>
23 #include <net/tcp_states.h>
24 #include <asm/system.h>
25 #include <linux/fcntl.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <net/rose.h>
30 static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose);
33 * This routine purges all of the queues of frames.
35 void rose_clear_queues(struct sock *sk)
37 skb_queue_purge(&sk->sk_write_queue);
38 skb_queue_purge(&rose_sk(sk)->ack_queue);
42 * This routine purges the input queue of those frames that have been
43 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
44 * SDL diagram.
46 void rose_frames_acked(struct sock *sk, unsigned short nr)
48 struct sk_buff *skb;
49 struct rose_sock *rose = rose_sk(sk);
52 * Remove all the ack-ed frames from the ack queue.
54 if (rose->va != nr) {
55 while (skb_peek(&rose->ack_queue) != NULL && rose->va != nr) {
56 skb = skb_dequeue(&rose->ack_queue);
57 kfree_skb(skb);
58 rose->va = (rose->va + 1) % ROSE_MODULUS;
63 void rose_requeue_frames(struct sock *sk)
65 struct sk_buff *skb, *skb_prev = NULL;
68 * Requeue all the un-ack-ed frames on the output queue to be picked
69 * up by rose_kick. This arrangement handles the possibility of an
70 * empty output queue.
72 while ((skb = skb_dequeue(&rose_sk(sk)->ack_queue)) != NULL) {
73 if (skb_prev == NULL)
74 skb_queue_head(&sk->sk_write_queue, skb);
75 else
76 skb_append(skb_prev, skb, &sk->sk_write_queue);
77 skb_prev = skb;
82 * Validate that the value of nr is between va and vs. Return true or
83 * false for testing.
85 int rose_validate_nr(struct sock *sk, unsigned short nr)
87 struct rose_sock *rose = rose_sk(sk);
88 unsigned short vc = rose->va;
90 while (vc != rose->vs) {
91 if (nr == vc) return 1;
92 vc = (vc + 1) % ROSE_MODULUS;
95 return nr == rose->vs;
99 * This routine is called when the packet layer internally generates a
100 * control frame.
102 void rose_write_internal(struct sock *sk, int frametype)
104 struct rose_sock *rose = rose_sk(sk);
105 struct sk_buff *skb;
106 unsigned char *dptr;
107 unsigned char lci1, lci2;
108 char buffer[100];
109 int len, faclen = 0;
111 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
113 switch (frametype) {
114 case ROSE_CALL_REQUEST:
115 len += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN;
116 faclen = rose_create_facilities(buffer, rose);
117 len += faclen;
118 break;
119 case ROSE_CALL_ACCEPTED:
120 case ROSE_CLEAR_REQUEST:
121 case ROSE_RESET_REQUEST:
122 len += 2;
123 break;
126 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
127 return;
130 * Space for AX.25 header and PID.
132 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1);
134 dptr = skb_put(skb, skb_tailroom(skb));
136 lci1 = (rose->lci >> 8) & 0x0F;
137 lci2 = (rose->lci >> 0) & 0xFF;
139 switch (frametype) {
140 case ROSE_CALL_REQUEST:
141 *dptr++ = ROSE_GFI | lci1;
142 *dptr++ = lci2;
143 *dptr++ = frametype;
144 *dptr++ = 0xAA;
145 memcpy(dptr, &rose->dest_addr, ROSE_ADDR_LEN);
146 dptr += ROSE_ADDR_LEN;
147 memcpy(dptr, &rose->source_addr, ROSE_ADDR_LEN);
148 dptr += ROSE_ADDR_LEN;
149 memcpy(dptr, buffer, faclen);
150 dptr += faclen;
151 break;
153 case ROSE_CALL_ACCEPTED:
154 *dptr++ = ROSE_GFI | lci1;
155 *dptr++ = lci2;
156 *dptr++ = frametype;
157 *dptr++ = 0x00; /* Address length */
158 *dptr++ = 0; /* Facilities length */
159 break;
161 case ROSE_CLEAR_REQUEST:
162 *dptr++ = ROSE_GFI | lci1;
163 *dptr++ = lci2;
164 *dptr++ = frametype;
165 *dptr++ = rose->cause;
166 *dptr++ = rose->diagnostic;
167 break;
169 case ROSE_RESET_REQUEST:
170 *dptr++ = ROSE_GFI | lci1;
171 *dptr++ = lci2;
172 *dptr++ = frametype;
173 *dptr++ = ROSE_DTE_ORIGINATED;
174 *dptr++ = 0;
175 break;
177 case ROSE_RR:
178 case ROSE_RNR:
179 *dptr++ = ROSE_GFI | lci1;
180 *dptr++ = lci2;
181 *dptr = frametype;
182 *dptr++ |= (rose->vr << 5) & 0xE0;
183 break;
185 case ROSE_CLEAR_CONFIRMATION:
186 case ROSE_RESET_CONFIRMATION:
187 *dptr++ = ROSE_GFI | lci1;
188 *dptr++ = lci2;
189 *dptr++ = frametype;
190 break;
192 default:
193 printk(KERN_ERR "ROSE: rose_write_internal - invalid frametype %02X\n", frametype);
194 kfree_skb(skb);
195 return;
198 rose_transmit_link(skb, rose->neighbour);
201 int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m)
203 unsigned char *frame;
205 frame = skb->data;
207 *ns = *nr = *q = *d = *m = 0;
209 switch (frame[2]) {
210 case ROSE_CALL_REQUEST:
211 case ROSE_CALL_ACCEPTED:
212 case ROSE_CLEAR_REQUEST:
213 case ROSE_CLEAR_CONFIRMATION:
214 case ROSE_RESET_REQUEST:
215 case ROSE_RESET_CONFIRMATION:
216 return frame[2];
217 default:
218 break;
221 if ((frame[2] & 0x1F) == ROSE_RR ||
222 (frame[2] & 0x1F) == ROSE_RNR) {
223 *nr = (frame[2] >> 5) & 0x07;
224 return frame[2] & 0x1F;
227 if ((frame[2] & 0x01) == ROSE_DATA) {
228 *q = (frame[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
229 *d = (frame[0] & ROSE_D_BIT) == ROSE_D_BIT;
230 *m = (frame[2] & ROSE_M_BIT) == ROSE_M_BIT;
231 *nr = (frame[2] >> 5) & 0x07;
232 *ns = (frame[2] >> 1) & 0x07;
233 return ROSE_DATA;
236 return ROSE_ILLEGAL;
239 static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *facilities, int len)
241 unsigned char *pt;
242 unsigned char l, lg, n = 0;
243 int fac_national_digis_received = 0;
245 do {
246 switch (*p & 0xC0) {
247 case 0x00:
248 p += 2;
249 n += 2;
250 len -= 2;
251 break;
253 case 0x40:
254 if (*p == FAC_NATIONAL_RAND)
255 facilities->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF);
256 p += 3;
257 n += 3;
258 len -= 3;
259 break;
261 case 0x80:
262 p += 4;
263 n += 4;
264 len -= 4;
265 break;
267 case 0xC0:
268 l = p[1];
269 if (*p == FAC_NATIONAL_DEST_DIGI) {
270 if (!fac_national_digis_received) {
271 memcpy(&facilities->source_digis[0], p + 2, AX25_ADDR_LEN);
272 facilities->source_ndigis = 1;
275 else if (*p == FAC_NATIONAL_SRC_DIGI) {
276 if (!fac_national_digis_received) {
277 memcpy(&facilities->dest_digis[0], p + 2, AX25_ADDR_LEN);
278 facilities->dest_ndigis = 1;
281 else if (*p == FAC_NATIONAL_FAIL_CALL) {
282 memcpy(&facilities->fail_call, p + 2, AX25_ADDR_LEN);
284 else if (*p == FAC_NATIONAL_FAIL_ADD) {
285 memcpy(&facilities->fail_addr, p + 3, ROSE_ADDR_LEN);
287 else if (*p == FAC_NATIONAL_DIGIS) {
288 fac_national_digis_received = 1;
289 facilities->source_ndigis = 0;
290 facilities->dest_ndigis = 0;
291 for (pt = p + 2, lg = 0 ; lg < l ; pt += AX25_ADDR_LEN, lg += AX25_ADDR_LEN) {
292 if (pt[6] & AX25_HBIT) {
293 if (facilities->dest_ndigis >= ROSE_MAX_DIGIS)
294 return -1;
295 memcpy(&facilities->dest_digis[facilities->dest_ndigis++], pt, AX25_ADDR_LEN);
296 } else {
297 if (facilities->source_ndigis >= ROSE_MAX_DIGIS)
298 return -1;
299 memcpy(&facilities->source_digis[facilities->source_ndigis++], pt, AX25_ADDR_LEN);
303 p += l + 2;
304 n += l + 2;
305 len -= l + 2;
306 break;
308 } while (*p != 0x00 && len > 0);
310 return n;
313 static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *facilities, int len)
315 unsigned char l, n = 0;
316 char callsign[11];
318 do {
319 switch (*p & 0xC0) {
320 case 0x00:
321 p += 2;
322 n += 2;
323 len -= 2;
324 break;
326 case 0x40:
327 p += 3;
328 n += 3;
329 len -= 3;
330 break;
332 case 0x80:
333 p += 4;
334 n += 4;
335 len -= 4;
336 break;
338 case 0xC0:
339 l = p[1];
341 /* Prevent overflows*/
342 if (l < 10 || l > 20)
343 return -1;
345 if (*p == FAC_CCITT_DEST_NSAP) {
346 memcpy(&facilities->source_addr, p + 7, ROSE_ADDR_LEN);
347 memcpy(callsign, p + 12, l - 10);
348 callsign[l - 10] = '\0';
349 asc2ax(&facilities->source_call, callsign);
351 if (*p == FAC_CCITT_SRC_NSAP) {
352 memcpy(&facilities->dest_addr, p + 7, ROSE_ADDR_LEN);
353 memcpy(callsign, p + 12, l - 10);
354 callsign[l - 10] = '\0';
355 asc2ax(&facilities->dest_call, callsign);
357 p += l + 2;
358 n += l + 2;
359 len -= l + 2;
360 break;
362 } while (*p != 0x00 && len > 0);
364 return n;
367 int rose_parse_facilities(unsigned char *p,
368 struct rose_facilities_struct *facilities)
370 int facilities_len, len;
372 facilities_len = *p++;
374 if (facilities_len == 0)
375 return 0;
377 while (facilities_len > 0) {
378 if (*p == 0x00) {
379 facilities_len--;
380 p++;
382 switch (*p) {
383 case FAC_NATIONAL: /* National */
384 len = rose_parse_national(p + 1, facilities, facilities_len - 1);
385 if (len < 0)
386 return 0;
387 facilities_len -= len + 1;
388 p += len + 1;
389 break;
391 case FAC_CCITT: /* CCITT */
392 len = rose_parse_ccitt(p + 1, facilities, facilities_len - 1);
393 if (len < 0)
394 return 0;
395 facilities_len -= len + 1;
396 p += len + 1;
397 break;
399 default:
400 printk(KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X\n", *p);
401 facilities_len--;
402 p++;
403 break;
405 } else
406 break; /* Error in facilities format */
409 return 1;
412 static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose)
414 unsigned char *p = buffer + 1;
415 char *callsign;
416 char buf[11];
417 int len, nb;
419 /* National Facilities */
420 if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) {
421 *p++ = 0x00;
422 *p++ = FAC_NATIONAL;
424 if (rose->rand != 0) {
425 *p++ = FAC_NATIONAL_RAND;
426 *p++ = (rose->rand >> 8) & 0xFF;
427 *p++ = (rose->rand >> 0) & 0xFF;
430 /* Sent before older facilities */
431 if ((rose->source_ndigis > 0) || (rose->dest_ndigis > 0)) {
432 int maxdigi = 0;
433 *p++ = FAC_NATIONAL_DIGIS;
434 *p++ = AX25_ADDR_LEN * (rose->source_ndigis + rose->dest_ndigis);
435 for (nb = 0 ; nb < rose->source_ndigis ; nb++) {
436 if (++maxdigi >= ROSE_MAX_DIGIS)
437 break;
438 memcpy(p, &rose->source_digis[nb], AX25_ADDR_LEN);
439 p[6] |= AX25_HBIT;
440 p += AX25_ADDR_LEN;
442 for (nb = 0 ; nb < rose->dest_ndigis ; nb++) {
443 if (++maxdigi >= ROSE_MAX_DIGIS)
444 break;
445 memcpy(p, &rose->dest_digis[nb], AX25_ADDR_LEN);
446 p[6] &= ~AX25_HBIT;
447 p += AX25_ADDR_LEN;
451 /* For compatibility */
452 if (rose->source_ndigis > 0) {
453 *p++ = FAC_NATIONAL_SRC_DIGI;
454 *p++ = AX25_ADDR_LEN;
455 memcpy(p, &rose->source_digis[0], AX25_ADDR_LEN);
456 p += AX25_ADDR_LEN;
459 /* For compatibility */
460 if (rose->dest_ndigis > 0) {
461 *p++ = FAC_NATIONAL_DEST_DIGI;
462 *p++ = AX25_ADDR_LEN;
463 memcpy(p, &rose->dest_digis[0], AX25_ADDR_LEN);
464 p += AX25_ADDR_LEN;
468 *p++ = 0x00;
469 *p++ = FAC_CCITT;
471 *p++ = FAC_CCITT_DEST_NSAP;
473 callsign = ax2asc(buf, &rose->dest_call);
475 *p++ = strlen(callsign) + 10;
476 *p++ = (strlen(callsign) + 9) * 2; /* ??? */
478 *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
479 *p++ = ROSE_ADDR_LEN * 2;
480 memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN);
481 p += ROSE_ADDR_LEN;
483 memcpy(p, callsign, strlen(callsign));
484 p += strlen(callsign);
486 *p++ = FAC_CCITT_SRC_NSAP;
488 callsign = ax2asc(buf, &rose->source_call);
490 *p++ = strlen(callsign) + 10;
491 *p++ = (strlen(callsign) + 9) * 2; /* ??? */
493 *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
494 *p++ = ROSE_ADDR_LEN * 2;
495 memcpy(p, &rose->source_addr, ROSE_ADDR_LEN);
496 p += ROSE_ADDR_LEN;
498 memcpy(p, callsign, strlen(callsign));
499 p += strlen(callsign);
501 len = p - buffer;
502 buffer[0] = len - 1;
504 return len;
507 void rose_disconnect(struct sock *sk, int reason, int cause, int diagnostic)
509 struct rose_sock *rose = rose_sk(sk);
511 rose_stop_timer(sk);
512 rose_stop_idletimer(sk);
514 rose_clear_queues(sk);
516 rose->lci = 0;
517 rose->state = ROSE_STATE_0;
519 if (cause != -1)
520 rose->cause = cause;
522 if (diagnostic != -1)
523 rose->diagnostic = diagnostic;
525 sk->sk_state = TCP_CLOSE;
526 sk->sk_err = reason;
527 sk->sk_shutdown |= SEND_SHUTDOWN;
529 if (!sock_flag(sk, SOCK_DEAD)) {
530 sk->sk_state_change(sk);
531 sock_set_flag(sk, SOCK_DEAD);