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  * Most of this code is based on the SDL diagrams published in the 7th ARRL
0007  * Computer Networking Conference papers. The diagrams have mistakes in them,
0008  * but are mostly correct. Before you modify the code could you read the SDL
0009  * diagrams as the code is not obvious and probably very easy to break.
0010  */
0011 #include <linux/errno.h>
0012 #include <linux/filter.h>
0013 #include <linux/types.h>
0014 #include <linux/socket.h>
0015 #include <linux/in.h>
0016 #include <linux/kernel.h>
0017 #include <linux/timer.h>
0018 #include <linux/string.h>
0019 #include <linux/sockios.h>
0020 #include <linux/net.h>
0021 #include <net/ax25.h>
0022 #include <linux/inet.h>
0023 #include <linux/netdevice.h>
0024 #include <linux/skbuff.h>
0025 #include <net/sock.h>
0026 #include <net/tcp_states.h>
0027 #include <linux/fcntl.h>
0028 #include <linux/mm.h>
0029 #include <linux/interrupt.h>
0030 #include <net/rose.h>
0031 
0032 /*
0033  * State machine for state 1, Awaiting Call Accepted State.
0034  * The handling of the timer(s) is in file rose_timer.c.
0035  * Handling of state 0 and connection release is in af_rose.c.
0036  */
0037 static int rose_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
0038 {
0039     struct rose_sock *rose = rose_sk(sk);
0040 
0041     switch (frametype) {
0042     case ROSE_CALL_ACCEPTED:
0043         rose_stop_timer(sk);
0044         rose_start_idletimer(sk);
0045         rose->condition = 0x00;
0046         rose->vs        = 0;
0047         rose->va        = 0;
0048         rose->vr        = 0;
0049         rose->vl        = 0;
0050         rose->state     = ROSE_STATE_3;
0051         sk->sk_state    = TCP_ESTABLISHED;
0052         if (!sock_flag(sk, SOCK_DEAD))
0053             sk->sk_state_change(sk);
0054         break;
0055 
0056     case ROSE_CLEAR_REQUEST:
0057         rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
0058         rose_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
0059         rose->neighbour->use--;
0060         break;
0061 
0062     default:
0063         break;
0064     }
0065 
0066     return 0;
0067 }
0068 
0069 /*
0070  * State machine for state 2, Awaiting Clear Confirmation State.
0071  * The handling of the timer(s) is in file rose_timer.c
0072  * Handling of state 0 and connection release is in af_rose.c.
0073  */
0074 static int rose_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
0075 {
0076     struct rose_sock *rose = rose_sk(sk);
0077 
0078     switch (frametype) {
0079     case ROSE_CLEAR_REQUEST:
0080         rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
0081         rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
0082         rose->neighbour->use--;
0083         break;
0084 
0085     case ROSE_CLEAR_CONFIRMATION:
0086         rose_disconnect(sk, 0, -1, -1);
0087         rose->neighbour->use--;
0088         break;
0089 
0090     default:
0091         break;
0092     }
0093 
0094     return 0;
0095 }
0096 
0097 /*
0098  * State machine for state 3, Connected State.
0099  * The handling of the timer(s) is in file rose_timer.c
0100  * Handling of state 0 and connection release is in af_rose.c.
0101  */
0102 static int rose_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
0103 {
0104     struct rose_sock *rose = rose_sk(sk);
0105     int queued = 0;
0106 
0107     switch (frametype) {
0108     case ROSE_RESET_REQUEST:
0109         rose_stop_timer(sk);
0110         rose_start_idletimer(sk);
0111         rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
0112         rose->condition = 0x00;
0113         rose->vs        = 0;
0114         rose->vr        = 0;
0115         rose->va        = 0;
0116         rose->vl        = 0;
0117         rose_requeue_frames(sk);
0118         break;
0119 
0120     case ROSE_CLEAR_REQUEST:
0121         rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
0122         rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
0123         rose->neighbour->use--;
0124         break;
0125 
0126     case ROSE_RR:
0127     case ROSE_RNR:
0128         if (!rose_validate_nr(sk, nr)) {
0129             rose_write_internal(sk, ROSE_RESET_REQUEST);
0130             rose->condition = 0x00;
0131             rose->vs        = 0;
0132             rose->vr        = 0;
0133             rose->va        = 0;
0134             rose->vl        = 0;
0135             rose->state     = ROSE_STATE_4;
0136             rose_start_t2timer(sk);
0137             rose_stop_idletimer(sk);
0138         } else {
0139             rose_frames_acked(sk, nr);
0140             if (frametype == ROSE_RNR) {
0141                 rose->condition |= ROSE_COND_PEER_RX_BUSY;
0142             } else {
0143                 rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
0144             }
0145         }
0146         break;
0147 
0148     case ROSE_DATA: /* XXX */
0149         rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
0150         if (!rose_validate_nr(sk, nr)) {
0151             rose_write_internal(sk, ROSE_RESET_REQUEST);
0152             rose->condition = 0x00;
0153             rose->vs        = 0;
0154             rose->vr        = 0;
0155             rose->va        = 0;
0156             rose->vl        = 0;
0157             rose->state     = ROSE_STATE_4;
0158             rose_start_t2timer(sk);
0159             rose_stop_idletimer(sk);
0160             break;
0161         }
0162         rose_frames_acked(sk, nr);
0163         if (ns == rose->vr) {
0164             rose_start_idletimer(sk);
0165             if (sk_filter_trim_cap(sk, skb, ROSE_MIN_LEN) == 0 &&
0166                 __sock_queue_rcv_skb(sk, skb) == 0) {
0167                 rose->vr = (rose->vr + 1) % ROSE_MODULUS;
0168                 queued = 1;
0169             } else {
0170                 /* Should never happen ! */
0171                 rose_write_internal(sk, ROSE_RESET_REQUEST);
0172                 rose->condition = 0x00;
0173                 rose->vs        = 0;
0174                 rose->vr        = 0;
0175                 rose->va        = 0;
0176                 rose->vl        = 0;
0177                 rose->state     = ROSE_STATE_4;
0178                 rose_start_t2timer(sk);
0179                 rose_stop_idletimer(sk);
0180                 break;
0181             }
0182             if (atomic_read(&sk->sk_rmem_alloc) >
0183                 (sk->sk_rcvbuf >> 1))
0184                 rose->condition |= ROSE_COND_OWN_RX_BUSY;
0185         }
0186         /*
0187          * If the window is full, ack the frame, else start the
0188          * acknowledge hold back timer.
0189          */
0190         if (((rose->vl + sysctl_rose_window_size) % ROSE_MODULUS) == rose->vr) {
0191             rose->condition &= ~ROSE_COND_ACK_PENDING;
0192             rose_stop_timer(sk);
0193             rose_enquiry_response(sk);
0194         } else {
0195             rose->condition |= ROSE_COND_ACK_PENDING;
0196             rose_start_hbtimer(sk);
0197         }
0198         break;
0199 
0200     default:
0201         printk(KERN_WARNING "ROSE: unknown %02X in state 3\n", frametype);
0202         break;
0203     }
0204 
0205     return queued;
0206 }
0207 
0208 /*
0209  * State machine for state 4, Awaiting Reset Confirmation State.
0210  * The handling of the timer(s) is in file rose_timer.c
0211  * Handling of state 0 and connection release is in af_rose.c.
0212  */
0213 static int rose_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
0214 {
0215     struct rose_sock *rose = rose_sk(sk);
0216 
0217     switch (frametype) {
0218     case ROSE_RESET_REQUEST:
0219         rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
0220         fallthrough;
0221     case ROSE_RESET_CONFIRMATION:
0222         rose_stop_timer(sk);
0223         rose_start_idletimer(sk);
0224         rose->condition = 0x00;
0225         rose->va        = 0;
0226         rose->vr        = 0;
0227         rose->vs        = 0;
0228         rose->vl        = 0;
0229         rose->state     = ROSE_STATE_3;
0230         rose_requeue_frames(sk);
0231         break;
0232 
0233     case ROSE_CLEAR_REQUEST:
0234         rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
0235         rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
0236         rose->neighbour->use--;
0237         break;
0238 
0239     default:
0240         break;
0241     }
0242 
0243     return 0;
0244 }
0245 
0246 /*
0247  * State machine for state 5, Awaiting Call Acceptance State.
0248  * The handling of the timer(s) is in file rose_timer.c
0249  * Handling of state 0 and connection release is in af_rose.c.
0250  */
0251 static int rose_state5_machine(struct sock *sk, struct sk_buff *skb, int frametype)
0252 {
0253     if (frametype == ROSE_CLEAR_REQUEST) {
0254         rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
0255         rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
0256         rose_sk(sk)->neighbour->use--;
0257     }
0258 
0259     return 0;
0260 }
0261 
0262 /* Higher level upcall for a LAPB frame */
0263 int rose_process_rx_frame(struct sock *sk, struct sk_buff *skb)
0264 {
0265     struct rose_sock *rose = rose_sk(sk);
0266     int queued = 0, frametype, ns, nr, q, d, m;
0267 
0268     if (rose->state == ROSE_STATE_0)
0269         return 0;
0270 
0271     frametype = rose_decode(skb, &ns, &nr, &q, &d, &m);
0272 
0273     switch (rose->state) {
0274     case ROSE_STATE_1:
0275         queued = rose_state1_machine(sk, skb, frametype);
0276         break;
0277     case ROSE_STATE_2:
0278         queued = rose_state2_machine(sk, skb, frametype);
0279         break;
0280     case ROSE_STATE_3:
0281         queued = rose_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
0282         break;
0283     case ROSE_STATE_4:
0284         queued = rose_state4_machine(sk, skb, frametype);
0285         break;
0286     case ROSE_STATE_5:
0287         queued = rose_state5_machine(sk, skb, frametype);
0288         break;
0289     }
0290 
0291     rose_kick(sk);
0292 
0293     return queued;
0294 }