Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License, or |
| 5 | * (at your option) any later version. |
| 6 | * |
| 7 | * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) |
| 8 | * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org) |
| 9 | */ |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/socket.h> |
| 13 | #include <linux/in.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/jiffies.h> |
| 16 | #include <linux/timer.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/sockios.h> |
| 19 | #include <linux/net.h> |
| 20 | #include <net/ax25.h> |
| 21 | #include <linux/inet.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/skbuff.h> |
| 24 | #include <net/sock.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 25 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/fcntl.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <net/rose.h> |
| 30 | |
Kees Cook | 99767f2 | 2017-10-16 17:29:36 -0700 | [diff] [blame] | 31 | static void rose_heartbeat_expiry(struct timer_list *t); |
Kees Cook | 4966bab | 2017-10-16 17:28:47 -0700 | [diff] [blame] | 32 | static void rose_timer_expiry(struct timer_list *); |
| 33 | static void rose_idletimer_expiry(struct timer_list *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | void rose_start_heartbeat(struct sock *sk) |
| 36 | { |
| 37 | del_timer(&sk->sk_timer); |
| 38 | |
Kees Cook | 841b86f | 2017-10-23 09:40:42 +0200 | [diff] [blame] | 39 | sk->sk_timer.function = rose_heartbeat_expiry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | sk->sk_timer.expires = jiffies + 5 * HZ; |
| 41 | |
| 42 | add_timer(&sk->sk_timer); |
| 43 | } |
| 44 | |
| 45 | void rose_start_t1timer(struct sock *sk) |
| 46 | { |
| 47 | struct rose_sock *rose = rose_sk(sk); |
| 48 | |
| 49 | del_timer(&rose->timer); |
| 50 | |
Kees Cook | 841b86f | 2017-10-23 09:40:42 +0200 | [diff] [blame] | 51 | rose->timer.function = rose_timer_expiry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | rose->timer.expires = jiffies + rose->t1; |
| 53 | |
| 54 | add_timer(&rose->timer); |
| 55 | } |
| 56 | |
| 57 | void rose_start_t2timer(struct sock *sk) |
| 58 | { |
| 59 | struct rose_sock *rose = rose_sk(sk); |
| 60 | |
| 61 | del_timer(&rose->timer); |
| 62 | |
Kees Cook | 841b86f | 2017-10-23 09:40:42 +0200 | [diff] [blame] | 63 | rose->timer.function = rose_timer_expiry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | rose->timer.expires = jiffies + rose->t2; |
| 65 | |
| 66 | add_timer(&rose->timer); |
| 67 | } |
| 68 | |
| 69 | void rose_start_t3timer(struct sock *sk) |
| 70 | { |
| 71 | struct rose_sock *rose = rose_sk(sk); |
| 72 | |
| 73 | del_timer(&rose->timer); |
| 74 | |
Kees Cook | 841b86f | 2017-10-23 09:40:42 +0200 | [diff] [blame] | 75 | rose->timer.function = rose_timer_expiry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | rose->timer.expires = jiffies + rose->t3; |
| 77 | |
| 78 | add_timer(&rose->timer); |
| 79 | } |
| 80 | |
| 81 | void rose_start_hbtimer(struct sock *sk) |
| 82 | { |
| 83 | struct rose_sock *rose = rose_sk(sk); |
| 84 | |
| 85 | del_timer(&rose->timer); |
| 86 | |
Kees Cook | 841b86f | 2017-10-23 09:40:42 +0200 | [diff] [blame] | 87 | rose->timer.function = rose_timer_expiry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | rose->timer.expires = jiffies + rose->hb; |
| 89 | |
| 90 | add_timer(&rose->timer); |
| 91 | } |
| 92 | |
| 93 | void rose_start_idletimer(struct sock *sk) |
| 94 | { |
| 95 | struct rose_sock *rose = rose_sk(sk); |
| 96 | |
| 97 | del_timer(&rose->idletimer); |
| 98 | |
| 99 | if (rose->idle > 0) { |
Kees Cook | 841b86f | 2017-10-23 09:40:42 +0200 | [diff] [blame] | 100 | rose->idletimer.function = rose_idletimer_expiry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | rose->idletimer.expires = jiffies + rose->idle; |
| 102 | |
| 103 | add_timer(&rose->idletimer); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void rose_stop_heartbeat(struct sock *sk) |
| 108 | { |
| 109 | del_timer(&sk->sk_timer); |
| 110 | } |
| 111 | |
| 112 | void rose_stop_timer(struct sock *sk) |
| 113 | { |
| 114 | del_timer(&rose_sk(sk)->timer); |
| 115 | } |
| 116 | |
| 117 | void rose_stop_idletimer(struct sock *sk) |
| 118 | { |
| 119 | del_timer(&rose_sk(sk)->idletimer); |
| 120 | } |
| 121 | |
Kees Cook | 99767f2 | 2017-10-16 17:29:36 -0700 | [diff] [blame] | 122 | static void rose_heartbeat_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Kees Cook | 99767f2 | 2017-10-16 17:29:36 -0700 | [diff] [blame] | 124 | struct sock *sk = from_timer(sk, t, sk_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | struct rose_sock *rose = rose_sk(sk); |
| 126 | |
| 127 | bh_lock_sock(sk); |
| 128 | switch (rose->state) { |
| 129 | case ROSE_STATE_0: |
| 130 | /* Magic here: If we listen() and a new link dies before it |
| 131 | is accepted() it isn't 'dead' so doesn't get removed. */ |
| 132 | if (sock_flag(sk, SOCK_DESTROY) || |
| 133 | (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) { |
Andrew Morton | a3d7a9d | 2005-10-28 15:12:02 -0700 | [diff] [blame] | 134 | bh_unlock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | rose_destroy_socket(sk); |
| 136 | return; |
| 137 | } |
| 138 | break; |
| 139 | |
| 140 | case ROSE_STATE_3: |
| 141 | /* |
| 142 | * Check for the state of the receive buffer. |
| 143 | */ |
| 144 | if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) && |
| 145 | (rose->condition & ROSE_COND_OWN_RX_BUSY)) { |
| 146 | rose->condition &= ~ROSE_COND_OWN_RX_BUSY; |
| 147 | rose->condition &= ~ROSE_COND_ACK_PENDING; |
| 148 | rose->vl = rose->vr; |
| 149 | rose_write_internal(sk, ROSE_RR); |
| 150 | rose_stop_timer(sk); /* HB */ |
| 151 | break; |
| 152 | } |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | rose_start_heartbeat(sk); |
| 157 | bh_unlock_sock(sk); |
| 158 | } |
| 159 | |
Kees Cook | 4966bab | 2017-10-16 17:28:47 -0700 | [diff] [blame] | 160 | static void rose_timer_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Kees Cook | 4966bab | 2017-10-16 17:28:47 -0700 | [diff] [blame] | 162 | struct rose_sock *rose = from_timer(rose, t, timer); |
| 163 | struct sock *sk = &rose->sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | bh_lock_sock(sk); |
| 166 | switch (rose->state) { |
| 167 | case ROSE_STATE_1: /* T1 */ |
| 168 | case ROSE_STATE_4: /* T2 */ |
| 169 | rose_write_internal(sk, ROSE_CLEAR_REQUEST); |
| 170 | rose->state = ROSE_STATE_2; |
| 171 | rose_start_t3timer(sk); |
| 172 | break; |
| 173 | |
| 174 | case ROSE_STATE_2: /* T3 */ |
| 175 | rose->neighbour->use--; |
| 176 | rose_disconnect(sk, ETIMEDOUT, -1, -1); |
| 177 | break; |
| 178 | |
| 179 | case ROSE_STATE_3: /* HB */ |
| 180 | if (rose->condition & ROSE_COND_ACK_PENDING) { |
| 181 | rose->condition &= ~ROSE_COND_ACK_PENDING; |
| 182 | rose_enquiry_response(sk); |
| 183 | } |
| 184 | break; |
| 185 | } |
| 186 | bh_unlock_sock(sk); |
| 187 | } |
| 188 | |
Kees Cook | 4966bab | 2017-10-16 17:28:47 -0700 | [diff] [blame] | 189 | static void rose_idletimer_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
Kees Cook | 4966bab | 2017-10-16 17:28:47 -0700 | [diff] [blame] | 191 | struct rose_sock *rose = from_timer(rose, t, idletimer); |
| 192 | struct sock *sk = &rose->sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | bh_lock_sock(sk); |
| 195 | rose_clear_queues(sk); |
| 196 | |
| 197 | rose_write_internal(sk, ROSE_CLEAR_REQUEST); |
| 198 | rose_sk(sk)->state = ROSE_STATE_2; |
| 199 | |
| 200 | rose_start_t3timer(sk); |
| 201 | |
| 202 | sk->sk_state = TCP_CLOSE; |
| 203 | sk->sk_err = 0; |
| 204 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 205 | |
| 206 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 207 | sk->sk_state_change(sk); |
| 208 | sock_set_flag(sk, SOCK_DEAD); |
| 209 | } |
| 210 | bh_unlock_sock(sk); |
| 211 | } |