| 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) Joerg Reuter DL1BKE (jreuter@yaina.de) | 
|  | 9 | */ | 
|  | 10 | #include <linux/errno.h> | 
|  | 11 | #include <linux/types.h> | 
|  | 12 | #include <linux/socket.h> | 
|  | 13 | #include <linux/spinlock.h> | 
|  | 14 | #include <linux/in.h> | 
|  | 15 | #include <linux/kernel.h> | 
|  | 16 | #include <linux/jiffies.h> | 
|  | 17 | #include <linux/timer.h> | 
|  | 18 | #include <linux/string.h> | 
|  | 19 | #include <linux/sockios.h> | 
|  | 20 | #include <linux/net.h> | 
| Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 21 | #include <net/tcp_states.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <net/ax25.h> | 
|  | 23 | #include <linux/inet.h> | 
|  | 24 | #include <linux/netdevice.h> | 
|  | 25 | #include <linux/skbuff.h> | 
|  | 26 | #include <net/sock.h> | 
|  | 27 | #include <asm/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 |  | 
|  | 32 | static void ax25_ds_timeout(unsigned long); | 
|  | 33 |  | 
|  | 34 | /* | 
|  | 35 | *	Add DAMA slave timeout timer to timer list. | 
|  | 36 | *	Unlike the connection based timers the timeout function gets | 
|  | 37 | *	triggered every second. Please note that NET_AX25_DAMA_SLAVE_TIMEOUT | 
|  | 38 | *	(aka /proc/sys/net/ax25/{dev}/dama_slave_timeout) is still in | 
|  | 39 | *	1/10th of a second. | 
|  | 40 | */ | 
|  | 41 |  | 
| Jarek Poplawski | e848b58 | 2008-02-11 21:38:32 -0800 | [diff] [blame] | 42 | void ax25_ds_setup_timer(ax25_dev *ax25_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { | 
| Jarek Poplawski | e848b58 | 2008-02-11 21:38:32 -0800 | [diff] [blame] | 44 | setup_timer(&ax25_dev->dama.slave_timer, ax25_ds_timeout, | 
|  | 45 | (unsigned long)ax25_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
|  | 48 | void ax25_ds_del_timer(ax25_dev *ax25_dev) | 
|  | 49 | { | 
|  | 50 | if (ax25_dev) | 
|  | 51 | del_timer(&ax25_dev->dama.slave_timer); | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | void ax25_ds_set_timer(ax25_dev *ax25_dev) | 
|  | 55 | { | 
|  | 56 | if (ax25_dev == NULL)		/* paranoia */ | 
|  | 57 | return; | 
|  | 58 |  | 
| Ralf Baechle | e1fdb5b | 2006-05-03 23:27:16 -0700 | [diff] [blame] | 59 | ax25_dev->dama.slave_timeout = | 
|  | 60 | msecs_to_jiffies(ax25_dev->values[AX25_VALUES_DS_TIMEOUT]) / 10; | 
| Jarek Poplawski | e848b58 | 2008-02-11 21:38:32 -0800 | [diff] [blame] | 61 | mod_timer(&ax25_dev->dama.slave_timer, jiffies + HZ); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
|  | 64 | /* | 
|  | 65 | *	DAMA Slave Timeout | 
|  | 66 | *	Silently discard all (slave) connections in case our master forgot us... | 
|  | 67 | */ | 
|  | 68 |  | 
|  | 69 | static void ax25_ds_timeout(unsigned long arg) | 
|  | 70 | { | 
|  | 71 | ax25_dev *ax25_dev = (struct ax25_dev *) arg; | 
|  | 72 | ax25_cb *ax25; | 
|  | 73 | struct hlist_node *node; | 
|  | 74 |  | 
|  | 75 | if (ax25_dev == NULL || !ax25_dev->dama.slave) | 
|  | 76 | return;			/* Yikes! */ | 
|  | 77 |  | 
|  | 78 | if (!ax25_dev->dama.slave_timeout || --ax25_dev->dama.slave_timeout) { | 
|  | 79 | ax25_ds_set_timer(ax25_dev); | 
|  | 80 | return; | 
|  | 81 | } | 
|  | 82 |  | 
| Ralf Baechle | c19c4b9 | 2006-07-12 13:25:23 -0700 | [diff] [blame] | 83 | spin_lock(&ax25_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | ax25_for_each(ax25, node, &ax25_list) { | 
|  | 85 | if (ax25->ax25_dev != ax25_dev || !(ax25->condition & AX25_COND_DAMA_MODE)) | 
|  | 86 | continue; | 
|  | 87 |  | 
|  | 88 | ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND); | 
|  | 89 | ax25_disconnect(ax25, ETIMEDOUT); | 
|  | 90 | } | 
| Ralf Baechle | c19c4b9 | 2006-07-12 13:25:23 -0700 | [diff] [blame] | 91 | spin_unlock(&ax25_list_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 |  | 
|  | 93 | ax25_dev_dama_off(ax25_dev); | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | void ax25_ds_heartbeat_expiry(ax25_cb *ax25) | 
|  | 97 | { | 
|  | 98 | struct sock *sk=ax25->sk; | 
|  | 99 |  | 
|  | 100 | if (sk) | 
|  | 101 | bh_lock_sock(sk); | 
|  | 102 |  | 
|  | 103 | switch (ax25->state) { | 
|  | 104 |  | 
|  | 105 | case AX25_STATE_0: | 
|  | 106 | /* Magic here: If we listen() and a new link dies before it | 
|  | 107 | is accepted() it isn't 'dead' so doesn't get removed. */ | 
|  | 108 | if (!sk || sock_flag(sk, SOCK_DESTROY) || | 
|  | 109 | (sk->sk_state == TCP_LISTEN && | 
|  | 110 | sock_flag(sk, SOCK_DEAD))) { | 
|  | 111 | if (sk) { | 
|  | 112 | sock_hold(sk); | 
|  | 113 | ax25_destroy_socket(ax25); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | bh_unlock_sock(sk); | 
| Bernard Pidoux F6BVP | d71b0e9 | 2010-08-26 11:40:00 +0000 | [diff] [blame] | 115 | sock_put(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } else | 
|  | 117 | ax25_destroy_socket(ax25); | 
|  | 118 | return; | 
|  | 119 | } | 
|  | 120 | break; | 
|  | 121 |  | 
|  | 122 | case AX25_STATE_3: | 
|  | 123 | /* | 
|  | 124 | * Check the state of the receive buffer. | 
|  | 125 | */ | 
|  | 126 | if (sk != NULL) { | 
|  | 127 | if (atomic_read(&sk->sk_rmem_alloc) < | 
| Eric Dumazet | f16f302 | 2008-01-13 22:29:41 -0800 | [diff] [blame] | 128 | (sk->sk_rcvbuf >> 1) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | (ax25->condition & AX25_COND_OWN_RX_BUSY)) { | 
|  | 130 | ax25->condition &= ~AX25_COND_OWN_RX_BUSY; | 
|  | 131 | ax25->condition &= ~AX25_COND_ACK_PENDING; | 
|  | 132 | break; | 
|  | 133 | } | 
|  | 134 | } | 
|  | 135 | break; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | if (sk) | 
|  | 139 | bh_unlock_sock(sk); | 
|  | 140 |  | 
|  | 141 | ax25_start_heartbeat(ax25); | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | /* dl1bke 960114: T3 works much like the IDLE timeout, but | 
|  | 145 | *                gets reloaded with every frame for this | 
|  | 146 | *		  connection. | 
|  | 147 | */ | 
|  | 148 | void ax25_ds_t3timer_expiry(ax25_cb *ax25) | 
|  | 149 | { | 
|  | 150 | ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND); | 
|  | 151 | ax25_dama_off(ax25); | 
|  | 152 | ax25_disconnect(ax25, ETIMEDOUT); | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | /* dl1bke 960228: close the connection when IDLE expires. | 
|  | 156 | *		  unlike T3 this timer gets reloaded only on | 
|  | 157 | *		  I frames. | 
|  | 158 | */ | 
|  | 159 | void ax25_ds_idletimer_expiry(ax25_cb *ax25) | 
|  | 160 | { | 
|  | 161 | ax25_clear_queues(ax25); | 
|  | 162 |  | 
|  | 163 | ax25->n2count = 0; | 
|  | 164 | ax25->state = AX25_STATE_2; | 
|  | 165 |  | 
|  | 166 | ax25_calculate_t1(ax25); | 
|  | 167 | ax25_start_t1timer(ax25); | 
|  | 168 | ax25_stop_t3timer(ax25); | 
|  | 169 |  | 
|  | 170 | if (ax25->sk != NULL) { | 
|  | 171 | bh_lock_sock(ax25->sk); | 
|  | 172 | ax25->sk->sk_state     = TCP_CLOSE; | 
|  | 173 | ax25->sk->sk_err       = 0; | 
|  | 174 | ax25->sk->sk_shutdown |= SEND_SHUTDOWN; | 
|  | 175 | if (!sock_flag(ax25->sk, SOCK_DEAD)) { | 
|  | 176 | ax25->sk->sk_state_change(ax25->sk); | 
|  | 177 | sock_set_flag(ax25->sk, SOCK_DEAD); | 
|  | 178 | } | 
|  | 179 | bh_unlock_sock(ax25->sk); | 
|  | 180 | } | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | /* dl1bke 960114: The DAMA protocol requires to send data and SABM/DISC | 
|  | 184 | *                within the poll of any connected channel. Remember | 
|  | 185 | *                that we are not allowed to send anything unless we | 
|  | 186 | *                get polled by the Master. | 
|  | 187 | * | 
|  | 188 | *                Thus we'll have to do parts of our T1 handling in | 
|  | 189 | *                ax25_enquiry_response(). | 
|  | 190 | */ | 
|  | 191 | void ax25_ds_t1_timeout(ax25_cb *ax25) | 
|  | 192 | { | 
|  | 193 | switch (ax25->state) { | 
|  | 194 | case AX25_STATE_1: | 
|  | 195 | if (ax25->n2count == ax25->n2) { | 
|  | 196 | if (ax25->modulus == AX25_MODULUS) { | 
|  | 197 | ax25_disconnect(ax25, ETIMEDOUT); | 
|  | 198 | return; | 
|  | 199 | } else { | 
|  | 200 | ax25->modulus = AX25_MODULUS; | 
|  | 201 | ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW]; | 
|  | 202 | ax25->n2count = 0; | 
|  | 203 | ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND); | 
|  | 204 | } | 
|  | 205 | } else { | 
|  | 206 | ax25->n2count++; | 
|  | 207 | if (ax25->modulus == AX25_MODULUS) | 
|  | 208 | ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND); | 
|  | 209 | else | 
|  | 210 | ax25_send_control(ax25, AX25_SABME, AX25_POLLOFF, AX25_COMMAND); | 
|  | 211 | } | 
|  | 212 | break; | 
|  | 213 |  | 
|  | 214 | case AX25_STATE_2: | 
|  | 215 | if (ax25->n2count == ax25->n2) { | 
|  | 216 | ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND); | 
|  | 217 | ax25_disconnect(ax25, ETIMEDOUT); | 
|  | 218 | return; | 
|  | 219 | } else { | 
|  | 220 | ax25->n2count++; | 
|  | 221 | } | 
|  | 222 | break; | 
|  | 223 |  | 
|  | 224 | case AX25_STATE_3: | 
|  | 225 | if (ax25->n2count == ax25->n2) { | 
|  | 226 | ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE); | 
|  | 227 | ax25_disconnect(ax25, ETIMEDOUT); | 
|  | 228 | return; | 
|  | 229 | } else { | 
|  | 230 | ax25->n2count++; | 
|  | 231 | } | 
|  | 232 | break; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | ax25_calculate_t1(ax25); | 
|  | 236 | ax25_start_t1timer(ax25); | 
|  | 237 | } |