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  * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
0006  */
0007 #include <linux/errno.h>
0008 #include <linux/types.h>
0009 #include <linux/socket.h>
0010 #include <linux/in.h>
0011 #include <linux/kernel.h>
0012 #include <linux/timer.h>
0013 #include <linux/string.h>
0014 #include <linux/sockios.h>
0015 #include <linux/net.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/uaccess.h>
0023 #include <linux/fcntl.h>
0024 #include <linux/mm.h>
0025 #include <linux/interrupt.h>
0026 
0027 /*
0028  *  State machine for state 1, Awaiting Connection State.
0029  *  The handling of the timer(s) is in file ax25_ds_timer.c.
0030  *  Handling of state 0 and connection release is in ax25.c.
0031  */
0032 static int ax25_ds_state1_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
0033 {
0034     switch (frametype) {
0035     case AX25_SABM:
0036         ax25->modulus = AX25_MODULUS;
0037         ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
0038         ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
0039         break;
0040 
0041     case AX25_SABME:
0042         ax25->modulus = AX25_EMODULUS;
0043         ax25->window  =  ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
0044         ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
0045         break;
0046 
0047     case AX25_DISC:
0048         ax25_send_control(ax25, AX25_DM, pf, AX25_RESPONSE);
0049         break;
0050 
0051     case AX25_UA:
0052         ax25_calculate_rtt(ax25);
0053         ax25_stop_t1timer(ax25);
0054         ax25_start_t3timer(ax25);
0055         ax25_start_idletimer(ax25);
0056         ax25->vs      = 0;
0057         ax25->va      = 0;
0058         ax25->vr      = 0;
0059         ax25->state   = AX25_STATE_3;
0060         ax25->n2count = 0;
0061         if (ax25->sk != NULL) {
0062             bh_lock_sock(ax25->sk);
0063             ax25->sk->sk_state = TCP_ESTABLISHED;
0064             /*
0065              * For WAIT_SABM connections we will produce an accept
0066              * ready socket here
0067              */
0068             if (!sock_flag(ax25->sk, SOCK_DEAD))
0069                 ax25->sk->sk_state_change(ax25->sk);
0070             bh_unlock_sock(ax25->sk);
0071         }
0072         ax25_dama_on(ax25);
0073 
0074         /* according to DK4EG's spec we are required to
0075          * send a RR RESPONSE FINAL NR=0.
0076          */
0077 
0078         ax25_std_enquiry_response(ax25);
0079         break;
0080 
0081     case AX25_DM:
0082         if (pf)
0083             ax25_disconnect(ax25, ECONNREFUSED);
0084         break;
0085 
0086     default:
0087         if (pf)
0088             ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
0089         break;
0090     }
0091 
0092     return 0;
0093 }
0094 
0095 /*
0096  *  State machine for state 2, Awaiting Release State.
0097  *  The handling of the timer(s) is in file ax25_ds_timer.c
0098  *  Handling of state 0 and connection release is in ax25.c.
0099  */
0100 static int ax25_ds_state2_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
0101 {
0102     switch (frametype) {
0103     case AX25_SABM:
0104     case AX25_SABME:
0105         ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
0106         ax25_dama_off(ax25);
0107         break;
0108 
0109     case AX25_DISC:
0110         ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
0111         ax25_dama_off(ax25);
0112         ax25_disconnect(ax25, 0);
0113         break;
0114 
0115     case AX25_DM:
0116     case AX25_UA:
0117         if (pf) {
0118             ax25_dama_off(ax25);
0119             ax25_disconnect(ax25, 0);
0120         }
0121         break;
0122 
0123     case AX25_I:
0124     case AX25_REJ:
0125     case AX25_RNR:
0126     case AX25_RR:
0127         if (pf) {
0128             ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
0129             ax25_dama_off(ax25);
0130         }
0131         break;
0132 
0133     default:
0134         break;
0135     }
0136 
0137     return 0;
0138 }
0139 
0140 /*
0141  *  State machine for state 3, Connected State.
0142  *  The handling of the timer(s) is in file ax25_timer.c
0143  *  Handling of state 0 and connection release is in ax25.c.
0144  */
0145 static int ax25_ds_state3_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int ns, int nr, int pf, int type)
0146 {
0147     int queued = 0;
0148 
0149     switch (frametype) {
0150     case AX25_SABM:
0151     case AX25_SABME:
0152         if (frametype == AX25_SABM) {
0153             ax25->modulus   = AX25_MODULUS;
0154             ax25->window    = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
0155         } else {
0156             ax25->modulus   = AX25_EMODULUS;
0157             ax25->window    = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
0158         }
0159         ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
0160         ax25_stop_t1timer(ax25);
0161         ax25_start_t3timer(ax25);
0162         ax25_start_idletimer(ax25);
0163         ax25->condition = 0x00;
0164         ax25->vs        = 0;
0165         ax25->va        = 0;
0166         ax25->vr        = 0;
0167         ax25_requeue_frames(ax25);
0168         ax25_dama_on(ax25);
0169         break;
0170 
0171     case AX25_DISC:
0172         ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
0173         ax25_dama_off(ax25);
0174         ax25_disconnect(ax25, 0);
0175         break;
0176 
0177     case AX25_DM:
0178         ax25_dama_off(ax25);
0179         ax25_disconnect(ax25, ECONNRESET);
0180         break;
0181 
0182     case AX25_RR:
0183     case AX25_RNR:
0184         if (frametype == AX25_RR)
0185             ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
0186         else
0187             ax25->condition |= AX25_COND_PEER_RX_BUSY;
0188 
0189         if (ax25_validate_nr(ax25, nr)) {
0190             if (ax25_check_iframes_acked(ax25, nr))
0191                 ax25->n2count=0;
0192             if (type == AX25_COMMAND && pf)
0193                 ax25_ds_enquiry_response(ax25);
0194         } else {
0195             ax25_ds_nr_error_recovery(ax25);
0196             ax25->state = AX25_STATE_1;
0197         }
0198         break;
0199 
0200     case AX25_REJ:
0201         ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
0202 
0203         if (ax25_validate_nr(ax25, nr)) {
0204             if (ax25->va != nr)
0205                 ax25->n2count=0;
0206 
0207             ax25_frames_acked(ax25, nr);
0208             ax25_calculate_rtt(ax25);
0209             ax25_stop_t1timer(ax25);
0210             ax25_start_t3timer(ax25);
0211             ax25_requeue_frames(ax25);
0212 
0213             if (type == AX25_COMMAND && pf)
0214                 ax25_ds_enquiry_response(ax25);
0215         } else {
0216             ax25_ds_nr_error_recovery(ax25);
0217             ax25->state = AX25_STATE_1;
0218         }
0219         break;
0220 
0221     case AX25_I:
0222         if (!ax25_validate_nr(ax25, nr)) {
0223             ax25_ds_nr_error_recovery(ax25);
0224             ax25->state = AX25_STATE_1;
0225             break;
0226         }
0227         if (ax25->condition & AX25_COND_PEER_RX_BUSY) {
0228             ax25_frames_acked(ax25, nr);
0229             ax25->n2count = 0;
0230         } else {
0231             if (ax25_check_iframes_acked(ax25, nr))
0232                 ax25->n2count = 0;
0233         }
0234         if (ax25->condition & AX25_COND_OWN_RX_BUSY) {
0235             if (pf) ax25_ds_enquiry_response(ax25);
0236             break;
0237         }
0238         if (ns == ax25->vr) {
0239             ax25->vr = (ax25->vr + 1) % ax25->modulus;
0240             queued = ax25_rx_iframe(ax25, skb);
0241             if (ax25->condition & AX25_COND_OWN_RX_BUSY)
0242                 ax25->vr = ns;  /* ax25->vr - 1 */
0243             ax25->condition &= ~AX25_COND_REJECT;
0244             if (pf) {
0245                 ax25_ds_enquiry_response(ax25);
0246             } else {
0247                 if (!(ax25->condition & AX25_COND_ACK_PENDING)) {
0248                     ax25->condition |= AX25_COND_ACK_PENDING;
0249                     ax25_start_t2timer(ax25);
0250                 }
0251             }
0252         } else {
0253             if (ax25->condition & AX25_COND_REJECT) {
0254                 if (pf) ax25_ds_enquiry_response(ax25);
0255             } else {
0256                 ax25->condition |= AX25_COND_REJECT;
0257                 ax25_ds_enquiry_response(ax25);
0258                 ax25->condition &= ~AX25_COND_ACK_PENDING;
0259             }
0260         }
0261         break;
0262 
0263     case AX25_FRMR:
0264     case AX25_ILLEGAL:
0265         ax25_ds_establish_data_link(ax25);
0266         ax25->state = AX25_STATE_1;
0267         break;
0268 
0269     default:
0270         break;
0271     }
0272 
0273     return queued;
0274 }
0275 
0276 /*
0277  *  Higher level upcall for a LAPB frame
0278  */
0279 int ax25_ds_frame_in(ax25_cb *ax25, struct sk_buff *skb, int type)
0280 {
0281     int queued = 0, frametype, ns, nr, pf;
0282 
0283     frametype = ax25_decode(ax25, skb, &ns, &nr, &pf);
0284 
0285     switch (ax25->state) {
0286     case AX25_STATE_1:
0287         queued = ax25_ds_state1_machine(ax25, skb, frametype, pf, type);
0288         break;
0289     case AX25_STATE_2:
0290         queued = ax25_ds_state2_machine(ax25, skb, frametype, pf, type);
0291         break;
0292     case AX25_STATE_3:
0293         queued = ax25_ds_state3_machine(ax25, skb, frametype, ns, nr, pf, type);
0294         break;
0295     }
0296 
0297     return queued;
0298 }