Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  LAPB release 002
0004  *
0005  *  This code REQUIRES 2.1.15 or higher/ NET3.038
0006  *
0007  *  History
0008  *  LAPB 001    Jonathan Naylor Started Coding
0009  *  LAPB 002    Jonathan Naylor New timer architecture.
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <linux/errno.h>
0015 #include <linux/types.h>
0016 #include <linux/socket.h>
0017 #include <linux/in.h>
0018 #include <linux/kernel.h>
0019 #include <linux/jiffies.h>
0020 #include <linux/timer.h>
0021 #include <linux/string.h>
0022 #include <linux/sockios.h>
0023 #include <linux/net.h>
0024 #include <linux/inet.h>
0025 #include <linux/skbuff.h>
0026 #include <net/sock.h>
0027 #include <linux/uaccess.h>
0028 #include <linux/fcntl.h>
0029 #include <linux/mm.h>
0030 #include <linux/interrupt.h>
0031 #include <net/lapb.h>
0032 
0033 static void lapb_t1timer_expiry(struct timer_list *);
0034 static void lapb_t2timer_expiry(struct timer_list *);
0035 
0036 void lapb_start_t1timer(struct lapb_cb *lapb)
0037 {
0038     del_timer(&lapb->t1timer);
0039 
0040     lapb->t1timer.function = lapb_t1timer_expiry;
0041     lapb->t1timer.expires  = jiffies + lapb->t1;
0042 
0043     lapb->t1timer_running = true;
0044     add_timer(&lapb->t1timer);
0045 }
0046 
0047 void lapb_start_t2timer(struct lapb_cb *lapb)
0048 {
0049     del_timer(&lapb->t2timer);
0050 
0051     lapb->t2timer.function = lapb_t2timer_expiry;
0052     lapb->t2timer.expires  = jiffies + lapb->t2;
0053 
0054     lapb->t2timer_running = true;
0055     add_timer(&lapb->t2timer);
0056 }
0057 
0058 void lapb_stop_t1timer(struct lapb_cb *lapb)
0059 {
0060     lapb->t1timer_running = false;
0061     del_timer(&lapb->t1timer);
0062 }
0063 
0064 void lapb_stop_t2timer(struct lapb_cb *lapb)
0065 {
0066     lapb->t2timer_running = false;
0067     del_timer(&lapb->t2timer);
0068 }
0069 
0070 int lapb_t1timer_running(struct lapb_cb *lapb)
0071 {
0072     return lapb->t1timer_running;
0073 }
0074 
0075 static void lapb_t2timer_expiry(struct timer_list *t)
0076 {
0077     struct lapb_cb *lapb = from_timer(lapb, t, t2timer);
0078 
0079     spin_lock_bh(&lapb->lock);
0080     if (timer_pending(&lapb->t2timer)) /* A new timer has been set up */
0081         goto out;
0082     if (!lapb->t2timer_running) /* The timer has been stopped */
0083         goto out;
0084 
0085     if (lapb->condition & LAPB_ACK_PENDING_CONDITION) {
0086         lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
0087         lapb_timeout_response(lapb);
0088     }
0089     lapb->t2timer_running = false;
0090 
0091 out:
0092     spin_unlock_bh(&lapb->lock);
0093 }
0094 
0095 static void lapb_t1timer_expiry(struct timer_list *t)
0096 {
0097     struct lapb_cb *lapb = from_timer(lapb, t, t1timer);
0098 
0099     spin_lock_bh(&lapb->lock);
0100     if (timer_pending(&lapb->t1timer)) /* A new timer has been set up */
0101         goto out;
0102     if (!lapb->t1timer_running) /* The timer has been stopped */
0103         goto out;
0104 
0105     switch (lapb->state) {
0106 
0107         /*
0108          *  If we are a DCE, send DM up to N2 times, then switch to
0109          *  STATE_1 and send SABM(E).
0110          */
0111         case LAPB_STATE_0:
0112             if (lapb->mode & LAPB_DCE &&
0113                 lapb->n2count != lapb->n2) {
0114                 lapb->n2count++;
0115                 lapb_send_control(lapb, LAPB_DM, LAPB_POLLOFF, LAPB_RESPONSE);
0116             } else {
0117                 lapb->state = LAPB_STATE_1;
0118                 lapb_establish_data_link(lapb);
0119             }
0120             break;
0121 
0122         /*
0123          *  Awaiting connection state, send SABM(E), up to N2 times.
0124          */
0125         case LAPB_STATE_1:
0126             if (lapb->n2count == lapb->n2) {
0127                 lapb_clear_queues(lapb);
0128                 lapb->state = LAPB_STATE_0;
0129                 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
0130                 lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
0131                 lapb->t1timer_running = false;
0132                 goto out;
0133             } else {
0134                 lapb->n2count++;
0135                 if (lapb->mode & LAPB_EXTENDED) {
0136                     lapb_dbg(1, "(%p) S1 TX SABME(1)\n",
0137                          lapb->dev);
0138                     lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
0139                 } else {
0140                     lapb_dbg(1, "(%p) S1 TX SABM(1)\n",
0141                          lapb->dev);
0142                     lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
0143                 }
0144             }
0145             break;
0146 
0147         /*
0148          *  Awaiting disconnection state, send DISC, up to N2 times.
0149          */
0150         case LAPB_STATE_2:
0151             if (lapb->n2count == lapb->n2) {
0152                 lapb_clear_queues(lapb);
0153                 lapb->state = LAPB_STATE_0;
0154                 lapb_disconnect_confirmation(lapb, LAPB_TIMEDOUT);
0155                 lapb_dbg(0, "(%p) S2 -> S0\n", lapb->dev);
0156                 lapb->t1timer_running = false;
0157                 goto out;
0158             } else {
0159                 lapb->n2count++;
0160                 lapb_dbg(1, "(%p) S2 TX DISC(1)\n", lapb->dev);
0161                 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
0162             }
0163             break;
0164 
0165         /*
0166          *  Data transfer state, restransmit I frames, up to N2 times.
0167          */
0168         case LAPB_STATE_3:
0169             if (lapb->n2count == lapb->n2) {
0170                 lapb_clear_queues(lapb);
0171                 lapb->state = LAPB_STATE_0;
0172                 lapb_stop_t2timer(lapb);
0173                 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
0174                 lapb_dbg(0, "(%p) S3 -> S0\n", lapb->dev);
0175                 lapb->t1timer_running = false;
0176                 goto out;
0177             } else {
0178                 lapb->n2count++;
0179                 lapb_requeue_frames(lapb);
0180                 lapb_kick(lapb);
0181             }
0182             break;
0183 
0184         /*
0185          *  Frame reject state, restransmit FRMR frames, up to N2 times.
0186          */
0187         case LAPB_STATE_4:
0188             if (lapb->n2count == lapb->n2) {
0189                 lapb_clear_queues(lapb);
0190                 lapb->state = LAPB_STATE_0;
0191                 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
0192                 lapb_dbg(0, "(%p) S4 -> S0\n", lapb->dev);
0193                 lapb->t1timer_running = false;
0194                 goto out;
0195             } else {
0196                 lapb->n2count++;
0197                 lapb_transmit_frmr(lapb);
0198             }
0199             break;
0200     }
0201 
0202     lapb_start_t1timer(lapb);
0203 
0204 out:
0205     spin_unlock_bh(&lapb->lock);
0206 }