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