Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
0005  */
0006 #include <linux/errno.h>
0007 #include <linux/types.h>
0008 #include <linux/socket.h>
0009 #include <linux/in.h>
0010 #include <linux/kernel.h>
0011 #include <linux/timer.h>
0012 #include <linux/string.h>
0013 #include <linux/sockios.h>
0014 #include <linux/net.h>
0015 #include <linux/slab.h>
0016 #include <net/ax25.h>
0017 #include <linux/inet.h>
0018 #include <linux/netdevice.h>
0019 #include <linux/skbuff.h>
0020 #include <net/sock.h>
0021 #include <net/tcp_states.h>
0022 #include <linux/fcntl.h>
0023 #include <linux/mm.h>
0024 #include <linux/interrupt.h>
0025 #include <net/rose.h>
0026 
0027 static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose);
0028 
0029 /*
0030  *  This routine purges all of the queues of frames.
0031  */
0032 void rose_clear_queues(struct sock *sk)
0033 {
0034     skb_queue_purge(&sk->sk_write_queue);
0035     skb_queue_purge(&rose_sk(sk)->ack_queue);
0036 }
0037 
0038 /*
0039  * This routine purges the input queue of those frames that have been
0040  * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
0041  * SDL diagram.
0042  */
0043 void rose_frames_acked(struct sock *sk, unsigned short nr)
0044 {
0045     struct sk_buff *skb;
0046     struct rose_sock *rose = rose_sk(sk);
0047 
0048     /*
0049      * Remove all the ack-ed frames from the ack queue.
0050      */
0051     if (rose->va != nr) {
0052         while (skb_peek(&rose->ack_queue) != NULL && rose->va != nr) {
0053             skb = skb_dequeue(&rose->ack_queue);
0054             kfree_skb(skb);
0055             rose->va = (rose->va + 1) % ROSE_MODULUS;
0056         }
0057     }
0058 }
0059 
0060 void rose_requeue_frames(struct sock *sk)
0061 {
0062     struct sk_buff *skb, *skb_prev = NULL;
0063 
0064     /*
0065      * Requeue all the un-ack-ed frames on the output queue to be picked
0066      * up by rose_kick. This arrangement handles the possibility of an
0067      * empty output queue.
0068      */
0069     while ((skb = skb_dequeue(&rose_sk(sk)->ack_queue)) != NULL) {
0070         if (skb_prev == NULL)
0071             skb_queue_head(&sk->sk_write_queue, skb);
0072         else
0073             skb_append(skb_prev, skb, &sk->sk_write_queue);
0074         skb_prev = skb;
0075     }
0076 }
0077 
0078 /*
0079  *  Validate that the value of nr is between va and vs. Return true or
0080  *  false for testing.
0081  */
0082 int rose_validate_nr(struct sock *sk, unsigned short nr)
0083 {
0084     struct rose_sock *rose = rose_sk(sk);
0085     unsigned short vc = rose->va;
0086 
0087     while (vc != rose->vs) {
0088         if (nr == vc) return 1;
0089         vc = (vc + 1) % ROSE_MODULUS;
0090     }
0091 
0092     return nr == rose->vs;
0093 }
0094 
0095 /*
0096  *  This routine is called when the packet layer internally generates a
0097  *  control frame.
0098  */
0099 void rose_write_internal(struct sock *sk, int frametype)
0100 {
0101     struct rose_sock *rose = rose_sk(sk);
0102     struct sk_buff *skb;
0103     unsigned char  *dptr;
0104     unsigned char  lci1, lci2;
0105     int maxfaclen = 0;
0106     int len, faclen;
0107     int reserve;
0108 
0109     reserve = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1;
0110     len = ROSE_MIN_LEN;
0111 
0112     switch (frametype) {
0113     case ROSE_CALL_REQUEST:
0114         len   += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN;
0115         maxfaclen = 256;
0116         break;
0117     case ROSE_CALL_ACCEPTED:
0118     case ROSE_CLEAR_REQUEST:
0119     case ROSE_RESET_REQUEST:
0120         len   += 2;
0121         break;
0122     }
0123 
0124     skb = alloc_skb(reserve + len + maxfaclen, GFP_ATOMIC);
0125     if (!skb)
0126         return;
0127 
0128     /*
0129      *  Space for AX.25 header and PID.
0130      */
0131     skb_reserve(skb, reserve);
0132 
0133     dptr = skb_put(skb, len);
0134 
0135     lci1 = (rose->lci >> 8) & 0x0F;
0136     lci2 = (rose->lci >> 0) & 0xFF;
0137 
0138     switch (frametype) {
0139     case ROSE_CALL_REQUEST:
0140         *dptr++ = ROSE_GFI | lci1;
0141         *dptr++ = lci2;
0142         *dptr++ = frametype;
0143         *dptr++ = ROSE_CALL_REQ_ADDR_LEN_VAL;
0144         memcpy(dptr, &rose->dest_addr,  ROSE_ADDR_LEN);
0145         dptr   += ROSE_ADDR_LEN;
0146         memcpy(dptr, &rose->source_addr, ROSE_ADDR_LEN);
0147         dptr   += ROSE_ADDR_LEN;
0148         faclen = rose_create_facilities(dptr, rose);
0149         skb_put(skb, faclen);
0150         dptr   += faclen;
0151         break;
0152 
0153     case ROSE_CALL_ACCEPTED:
0154         *dptr++ = ROSE_GFI | lci1;
0155         *dptr++ = lci2;
0156         *dptr++ = frametype;
0157         *dptr++ = 0x00;     /* Address length */
0158         *dptr++ = 0;        /* Facilities length */
0159         break;
0160 
0161     case ROSE_CLEAR_REQUEST:
0162         *dptr++ = ROSE_GFI | lci1;
0163         *dptr++ = lci2;
0164         *dptr++ = frametype;
0165         *dptr++ = rose->cause;
0166         *dptr++ = rose->diagnostic;
0167         break;
0168 
0169     case ROSE_RESET_REQUEST:
0170         *dptr++ = ROSE_GFI | lci1;
0171         *dptr++ = lci2;
0172         *dptr++ = frametype;
0173         *dptr++ = ROSE_DTE_ORIGINATED;
0174         *dptr++ = 0;
0175         break;
0176 
0177     case ROSE_RR:
0178     case ROSE_RNR:
0179         *dptr++ = ROSE_GFI | lci1;
0180         *dptr++ = lci2;
0181         *dptr   = frametype;
0182         *dptr++ |= (rose->vr << 5) & 0xE0;
0183         break;
0184 
0185     case ROSE_CLEAR_CONFIRMATION:
0186     case ROSE_RESET_CONFIRMATION:
0187         *dptr++ = ROSE_GFI | lci1;
0188         *dptr++ = lci2;
0189         *dptr++  = frametype;
0190         break;
0191 
0192     default:
0193         printk(KERN_ERR "ROSE: rose_write_internal - invalid frametype %02X\n", frametype);
0194         kfree_skb(skb);
0195         return;
0196     }
0197 
0198     rose_transmit_link(skb, rose->neighbour);
0199 }
0200 
0201 int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m)
0202 {
0203     unsigned char *frame;
0204 
0205     frame = skb->data;
0206 
0207     *ns = *nr = *q = *d = *m = 0;
0208 
0209     switch (frame[2]) {
0210     case ROSE_CALL_REQUEST:
0211     case ROSE_CALL_ACCEPTED:
0212     case ROSE_CLEAR_REQUEST:
0213     case ROSE_CLEAR_CONFIRMATION:
0214     case ROSE_RESET_REQUEST:
0215     case ROSE_RESET_CONFIRMATION:
0216         return frame[2];
0217     default:
0218         break;
0219     }
0220 
0221     if ((frame[2] & 0x1F) == ROSE_RR  ||
0222         (frame[2] & 0x1F) == ROSE_RNR) {
0223         *nr = (frame[2] >> 5) & 0x07;
0224         return frame[2] & 0x1F;
0225     }
0226 
0227     if ((frame[2] & 0x01) == ROSE_DATA) {
0228         *q  = (frame[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
0229         *d  = (frame[0] & ROSE_D_BIT) == ROSE_D_BIT;
0230         *m  = (frame[2] & ROSE_M_BIT) == ROSE_M_BIT;
0231         *nr = (frame[2] >> 5) & 0x07;
0232         *ns = (frame[2] >> 1) & 0x07;
0233         return ROSE_DATA;
0234     }
0235 
0236     return ROSE_ILLEGAL;
0237 }
0238 
0239 static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *facilities, int len)
0240 {
0241     unsigned char *pt;
0242     unsigned char l, lg, n = 0;
0243     int fac_national_digis_received = 0;
0244 
0245     do {
0246         switch (*p & 0xC0) {
0247         case 0x00:
0248             if (len < 2)
0249                 return -1;
0250             p   += 2;
0251             n   += 2;
0252             len -= 2;
0253             break;
0254 
0255         case 0x40:
0256             if (len < 3)
0257                 return -1;
0258             if (*p == FAC_NATIONAL_RAND)
0259                 facilities->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF);
0260             p   += 3;
0261             n   += 3;
0262             len -= 3;
0263             break;
0264 
0265         case 0x80:
0266             if (len < 4)
0267                 return -1;
0268             p   += 4;
0269             n   += 4;
0270             len -= 4;
0271             break;
0272 
0273         case 0xC0:
0274             if (len < 2)
0275                 return -1;
0276             l = p[1];
0277             if (len < 2 + l)
0278                 return -1;
0279             if (*p == FAC_NATIONAL_DEST_DIGI) {
0280                 if (!fac_national_digis_received) {
0281                     if (l < AX25_ADDR_LEN)
0282                         return -1;
0283                     memcpy(&facilities->source_digis[0], p + 2, AX25_ADDR_LEN);
0284                     facilities->source_ndigis = 1;
0285                 }
0286             }
0287             else if (*p == FAC_NATIONAL_SRC_DIGI) {
0288                 if (!fac_national_digis_received) {
0289                     if (l < AX25_ADDR_LEN)
0290                         return -1;
0291                     memcpy(&facilities->dest_digis[0], p + 2, AX25_ADDR_LEN);
0292                     facilities->dest_ndigis = 1;
0293                 }
0294             }
0295             else if (*p == FAC_NATIONAL_FAIL_CALL) {
0296                 if (l < AX25_ADDR_LEN)
0297                     return -1;
0298                 memcpy(&facilities->fail_call, p + 2, AX25_ADDR_LEN);
0299             }
0300             else if (*p == FAC_NATIONAL_FAIL_ADD) {
0301                 if (l < 1 + ROSE_ADDR_LEN)
0302                     return -1;
0303                 memcpy(&facilities->fail_addr, p + 3, ROSE_ADDR_LEN);
0304             }
0305             else if (*p == FAC_NATIONAL_DIGIS) {
0306                 if (l % AX25_ADDR_LEN)
0307                     return -1;
0308                 fac_national_digis_received = 1;
0309                 facilities->source_ndigis = 0;
0310                 facilities->dest_ndigis   = 0;
0311                 for (pt = p + 2, lg = 0 ; lg < l ; pt += AX25_ADDR_LEN, lg += AX25_ADDR_LEN) {
0312                     if (pt[6] & AX25_HBIT) {
0313                         if (facilities->dest_ndigis >= ROSE_MAX_DIGIS)
0314                             return -1;
0315                         memcpy(&facilities->dest_digis[facilities->dest_ndigis++], pt, AX25_ADDR_LEN);
0316                     } else {
0317                         if (facilities->source_ndigis >= ROSE_MAX_DIGIS)
0318                             return -1;
0319                         memcpy(&facilities->source_digis[facilities->source_ndigis++], pt, AX25_ADDR_LEN);
0320                     }
0321                 }
0322             }
0323             p   += l + 2;
0324             n   += l + 2;
0325             len -= l + 2;
0326             break;
0327         }
0328     } while (*p != 0x00 && len > 0);
0329 
0330     return n;
0331 }
0332 
0333 static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *facilities, int len)
0334 {
0335     unsigned char l, n = 0;
0336     char callsign[11];
0337 
0338     do {
0339         switch (*p & 0xC0) {
0340         case 0x00:
0341             if (len < 2)
0342                 return -1;
0343             p   += 2;
0344             n   += 2;
0345             len -= 2;
0346             break;
0347 
0348         case 0x40:
0349             if (len < 3)
0350                 return -1;
0351             p   += 3;
0352             n   += 3;
0353             len -= 3;
0354             break;
0355 
0356         case 0x80:
0357             if (len < 4)
0358                 return -1;
0359             p   += 4;
0360             n   += 4;
0361             len -= 4;
0362             break;
0363 
0364         case 0xC0:
0365             if (len < 2)
0366                 return -1;
0367             l = p[1];
0368 
0369             /* Prevent overflows*/
0370             if (l < 10 || l > 20)
0371                 return -1;
0372 
0373             if (*p == FAC_CCITT_DEST_NSAP) {
0374                 memcpy(&facilities->source_addr, p + 7, ROSE_ADDR_LEN);
0375                 memcpy(callsign, p + 12,   l - 10);
0376                 callsign[l - 10] = '\0';
0377                 asc2ax(&facilities->source_call, callsign);
0378             }
0379             if (*p == FAC_CCITT_SRC_NSAP) {
0380                 memcpy(&facilities->dest_addr, p + 7, ROSE_ADDR_LEN);
0381                 memcpy(callsign, p + 12, l - 10);
0382                 callsign[l - 10] = '\0';
0383                 asc2ax(&facilities->dest_call, callsign);
0384             }
0385             p   += l + 2;
0386             n   += l + 2;
0387             len -= l + 2;
0388             break;
0389         }
0390     } while (*p != 0x00 && len > 0);
0391 
0392     return n;
0393 }
0394 
0395 int rose_parse_facilities(unsigned char *p, unsigned packet_len,
0396     struct rose_facilities_struct *facilities)
0397 {
0398     int facilities_len, len;
0399 
0400     facilities_len = *p++;
0401 
0402     if (facilities_len == 0 || (unsigned int)facilities_len > packet_len)
0403         return 0;
0404 
0405     while (facilities_len >= 3 && *p == 0x00) {
0406         facilities_len--;
0407         p++;
0408 
0409         switch (*p) {
0410         case FAC_NATIONAL:      /* National */
0411             len = rose_parse_national(p + 1, facilities, facilities_len - 1);
0412             break;
0413 
0414         case FAC_CCITT:     /* CCITT */
0415             len = rose_parse_ccitt(p + 1, facilities, facilities_len - 1);
0416             break;
0417 
0418         default:
0419             printk(KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X\n", *p);
0420             len = 1;
0421             break;
0422         }
0423 
0424         if (len < 0)
0425             return 0;
0426         if (WARN_ON(len >= facilities_len))
0427             return 0;
0428         facilities_len -= len + 1;
0429         p += len + 1;
0430     }
0431 
0432     return facilities_len == 0;
0433 }
0434 
0435 static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose)
0436 {
0437     unsigned char *p = buffer + 1;
0438     char *callsign;
0439     char buf[11];
0440     int len, nb;
0441 
0442     /* National Facilities */
0443     if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) {
0444         *p++ = 0x00;
0445         *p++ = FAC_NATIONAL;
0446 
0447         if (rose->rand != 0) {
0448             *p++ = FAC_NATIONAL_RAND;
0449             *p++ = (rose->rand >> 8) & 0xFF;
0450             *p++ = (rose->rand >> 0) & 0xFF;
0451         }
0452 
0453         /* Sent before older facilities */
0454         if ((rose->source_ndigis > 0) || (rose->dest_ndigis > 0)) {
0455             int maxdigi = 0;
0456             *p++ = FAC_NATIONAL_DIGIS;
0457             *p++ = AX25_ADDR_LEN * (rose->source_ndigis + rose->dest_ndigis);
0458             for (nb = 0 ; nb < rose->source_ndigis ; nb++) {
0459                 if (++maxdigi >= ROSE_MAX_DIGIS)
0460                     break;
0461                 memcpy(p, &rose->source_digis[nb], AX25_ADDR_LEN);
0462                 p[6] |= AX25_HBIT;
0463                 p += AX25_ADDR_LEN;
0464             }
0465             for (nb = 0 ; nb < rose->dest_ndigis ; nb++) {
0466                 if (++maxdigi >= ROSE_MAX_DIGIS)
0467                     break;
0468                 memcpy(p, &rose->dest_digis[nb], AX25_ADDR_LEN);
0469                 p[6] &= ~AX25_HBIT;
0470                 p += AX25_ADDR_LEN;
0471             }
0472         }
0473 
0474         /* For compatibility */
0475         if (rose->source_ndigis > 0) {
0476             *p++ = FAC_NATIONAL_SRC_DIGI;
0477             *p++ = AX25_ADDR_LEN;
0478             memcpy(p, &rose->source_digis[0], AX25_ADDR_LEN);
0479             p   += AX25_ADDR_LEN;
0480         }
0481 
0482         /* For compatibility */
0483         if (rose->dest_ndigis > 0) {
0484             *p++ = FAC_NATIONAL_DEST_DIGI;
0485             *p++ = AX25_ADDR_LEN;
0486             memcpy(p, &rose->dest_digis[0], AX25_ADDR_LEN);
0487             p   += AX25_ADDR_LEN;
0488         }
0489     }
0490 
0491     *p++ = 0x00;
0492     *p++ = FAC_CCITT;
0493 
0494     *p++ = FAC_CCITT_DEST_NSAP;
0495 
0496     callsign = ax2asc(buf, &rose->dest_call);
0497 
0498     *p++ = strlen(callsign) + 10;
0499     *p++ = (strlen(callsign) + 9) * 2;      /* ??? */
0500 
0501     *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
0502     *p++ = ROSE_ADDR_LEN * 2;
0503     memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN);
0504     p   += ROSE_ADDR_LEN;
0505 
0506     memcpy(p, callsign, strlen(callsign));
0507     p   += strlen(callsign);
0508 
0509     *p++ = FAC_CCITT_SRC_NSAP;
0510 
0511     callsign = ax2asc(buf, &rose->source_call);
0512 
0513     *p++ = strlen(callsign) + 10;
0514     *p++ = (strlen(callsign) + 9) * 2;      /* ??? */
0515 
0516     *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
0517     *p++ = ROSE_ADDR_LEN * 2;
0518     memcpy(p, &rose->source_addr, ROSE_ADDR_LEN);
0519     p   += ROSE_ADDR_LEN;
0520 
0521     memcpy(p, callsign, strlen(callsign));
0522     p   += strlen(callsign);
0523 
0524     len       = p - buffer;
0525     buffer[0] = len - 1;
0526 
0527     return len;
0528 }
0529 
0530 void rose_disconnect(struct sock *sk, int reason, int cause, int diagnostic)
0531 {
0532     struct rose_sock *rose = rose_sk(sk);
0533 
0534     rose_stop_timer(sk);
0535     rose_stop_idletimer(sk);
0536 
0537     rose_clear_queues(sk);
0538 
0539     rose->lci   = 0;
0540     rose->state = ROSE_STATE_0;
0541 
0542     if (cause != -1)
0543         rose->cause = cause;
0544 
0545     if (diagnostic != -1)
0546         rose->diagnostic = diagnostic;
0547 
0548     sk->sk_state     = TCP_CLOSE;
0549     sk->sk_err       = reason;
0550     sk->sk_shutdown |= SEND_SHUTDOWN;
0551 
0552     if (!sock_flag(sk, SOCK_DEAD)) {
0553         sk->sk_state_change(sk);
0554         sock_set_flag(sk, SOCK_DEAD);
0555     }
0556 }