^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * LAPB release 002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This code REQUIRES 2.1.15 or higher/ NET3.038
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * History
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * LAPB 001 Jonathan Naylor Started Coding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * LAPB 002 Jonathan Naylor New timer architecture.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <net/lapb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static void lapb_t1timer_expiry(struct timer_list *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static void lapb_t2timer_expiry(struct timer_list *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) void lapb_start_t1timer(struct lapb_cb *lapb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) del_timer(&lapb->t1timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) lapb->t1timer.function = lapb_t1timer_expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) lapb->t1timer.expires = jiffies + lapb->t1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) add_timer(&lapb->t1timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) void lapb_start_t2timer(struct lapb_cb *lapb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) del_timer(&lapb->t2timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) lapb->t2timer.function = lapb_t2timer_expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) lapb->t2timer.expires = jiffies + lapb->t2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) add_timer(&lapb->t2timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void lapb_stop_t1timer(struct lapb_cb *lapb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) del_timer(&lapb->t1timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) void lapb_stop_t2timer(struct lapb_cb *lapb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) del_timer(&lapb->t2timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int lapb_t1timer_running(struct lapb_cb *lapb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return timer_pending(&lapb->t1timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void lapb_t2timer_expiry(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct lapb_cb *lapb = from_timer(lapb, t, t2timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (lapb->condition & LAPB_ACK_PENDING_CONDITION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) lapb_timeout_response(lapb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static void lapb_t1timer_expiry(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct lapb_cb *lapb = from_timer(lapb, t, t1timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) switch (lapb->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * If we are a DCE, keep going DM .. DM .. DM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) case LAPB_STATE_0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (lapb->mode & LAPB_DCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) lapb_send_control(lapb, LAPB_DM, LAPB_POLLOFF, LAPB_RESPONSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * Awaiting connection state, send SABM(E), up to N2 times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) case LAPB_STATE_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (lapb->n2count == lapb->n2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) lapb_clear_queues(lapb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) lapb->state = LAPB_STATE_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) lapb->n2count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (lapb->mode & LAPB_EXTENDED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) lapb_dbg(1, "(%p) S1 TX SABME(1)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) lapb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) lapb_dbg(1, "(%p) S1 TX SABM(1)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) lapb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * Awaiting disconnection state, send DISC, up to N2 times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) case LAPB_STATE_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (lapb->n2count == lapb->n2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) lapb_clear_queues(lapb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) lapb->state = LAPB_STATE_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) lapb_disconnect_confirmation(lapb, LAPB_TIMEDOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) lapb_dbg(0, "(%p) S2 -> S0\n", lapb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) lapb->n2count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) lapb_dbg(1, "(%p) S2 TX DISC(1)\n", lapb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * Data transfer state, restransmit I frames, up to N2 times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) case LAPB_STATE_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (lapb->n2count == lapb->n2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) lapb_clear_queues(lapb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) lapb->state = LAPB_STATE_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) lapb_stop_t2timer(lapb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) lapb_dbg(0, "(%p) S3 -> S0\n", lapb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) lapb->n2count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) lapb_requeue_frames(lapb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) lapb_kick(lapb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * Frame reject state, restransmit FRMR frames, up to N2 times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) case LAPB_STATE_4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (lapb->n2count == lapb->n2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) lapb_clear_queues(lapb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) lapb->state = LAPB_STATE_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) lapb_dbg(0, "(%p) S4 -> S0\n", lapb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) lapb->n2count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) lapb_transmit_frmr(lapb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) lapb_start_t1timer(lapb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }