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