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) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk) |
| 8 | * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) |
| 9 | * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi) |
| 10 | * Copyright (C) Darryl Miles G7LED (dlm@g7led.demon.co.uk) |
| 11 | * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de) |
| 12 | * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr) |
| 13 | * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org) |
| 14 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/errno.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/socket.h> |
| 18 | #include <linux/in.h> |
| 19 | #include <linux/kernel.h> |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/jiffies.h> |
| 22 | #include <linux/timer.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/sockios.h> |
| 25 | #include <linux/net.h> |
| 26 | #include <net/ax25.h> |
| 27 | #include <linux/inet.h> |
| 28 | #include <linux/netdevice.h> |
| 29 | #include <linux/skbuff.h> |
| 30 | #include <net/sock.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 31 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/fcntl.h> |
| 33 | #include <linux/mm.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 36 | static void ax25_heartbeat_expiry(struct timer_list *); |
| 37 | static void ax25_t1timer_expiry(struct timer_list *); |
| 38 | static void ax25_t2timer_expiry(struct timer_list *); |
| 39 | static void ax25_t3timer_expiry(struct timer_list *); |
| 40 | static void ax25_idletimer_expiry(struct timer_list *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 42 | void ax25_setup_timers(ax25_cb *ax25) |
| 43 | { |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 44 | timer_setup(&ax25->timer, ax25_heartbeat_expiry, 0); |
| 45 | timer_setup(&ax25->t1timer, ax25_t1timer_expiry, 0); |
| 46 | timer_setup(&ax25->t2timer, ax25_t2timer_expiry, 0); |
| 47 | timer_setup(&ax25->t3timer, ax25_t3timer_expiry, 0); |
| 48 | timer_setup(&ax25->idletimer, ax25_idletimer_expiry, 0); |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | void ax25_start_heartbeat(ax25_cb *ax25) |
| 52 | { |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 53 | mod_timer(&ax25->timer, jiffies + 5 * HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void ax25_start_t1timer(ax25_cb *ax25) |
| 57 | { |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 58 | mod_timer(&ax25->t1timer, jiffies + ax25->t1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void ax25_start_t2timer(ax25_cb *ax25) |
| 62 | { |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 63 | mod_timer(&ax25->t2timer, jiffies + ax25->t2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void ax25_start_t3timer(ax25_cb *ax25) |
| 67 | { |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 68 | if (ax25->t3 > 0) |
| 69 | mod_timer(&ax25->t3timer, jiffies + ax25->t3); |
| 70 | else |
| 71 | del_timer(&ax25->t3timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void ax25_start_idletimer(ax25_cb *ax25) |
| 75 | { |
Jarek Poplawski | 21fab4a | 2008-02-11 21:36:39 -0800 | [diff] [blame] | 76 | if (ax25->idle > 0) |
| 77 | mod_timer(&ax25->idletimer, jiffies + ax25->idle); |
| 78 | else |
| 79 | del_timer(&ax25->idletimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void ax25_stop_heartbeat(ax25_cb *ax25) |
| 83 | { |
| 84 | del_timer(&ax25->timer); |
| 85 | } |
| 86 | |
| 87 | void ax25_stop_t1timer(ax25_cb *ax25) |
| 88 | { |
| 89 | del_timer(&ax25->t1timer); |
| 90 | } |
| 91 | |
| 92 | void ax25_stop_t2timer(ax25_cb *ax25) |
| 93 | { |
| 94 | del_timer(&ax25->t2timer); |
| 95 | } |
| 96 | |
| 97 | void ax25_stop_t3timer(ax25_cb *ax25) |
| 98 | { |
| 99 | del_timer(&ax25->t3timer); |
| 100 | } |
| 101 | |
| 102 | void ax25_stop_idletimer(ax25_cb *ax25) |
| 103 | { |
| 104 | del_timer(&ax25->idletimer); |
| 105 | } |
| 106 | |
| 107 | int ax25_t1timer_running(ax25_cb *ax25) |
| 108 | { |
| 109 | return timer_pending(&ax25->t1timer); |
| 110 | } |
| 111 | |
| 112 | unsigned long ax25_display_timer(struct timer_list *timer) |
| 113 | { |
| 114 | if (!timer_pending(timer)) |
| 115 | return 0; |
| 116 | |
| 117 | return timer->expires - jiffies; |
| 118 | } |
| 119 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 120 | EXPORT_SYMBOL(ax25_display_timer); |
| 121 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 122 | static void ax25_heartbeat_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | int proto = AX25_PROTO_STD_SIMPLEX; |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 125 | ax25_cb *ax25 = from_timer(ax25, t, timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | if (ax25->ax25_dev) |
| 128 | proto = ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]; |
| 129 | |
| 130 | switch (proto) { |
| 131 | case AX25_PROTO_STD_SIMPLEX: |
| 132 | case AX25_PROTO_STD_DUPLEX: |
| 133 | ax25_std_heartbeat_expiry(ax25); |
| 134 | break; |
| 135 | |
| 136 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 137 | case AX25_PROTO_DAMA_SLAVE: |
| 138 | if (ax25->ax25_dev->dama.slave) |
| 139 | ax25_ds_heartbeat_expiry(ax25); |
| 140 | else |
| 141 | ax25_std_heartbeat_expiry(ax25); |
| 142 | break; |
| 143 | #endif |
| 144 | } |
| 145 | } |
| 146 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 147 | static void ax25_t1timer_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 149 | ax25_cb *ax25 = from_timer(ax25, t, t1timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { |
| 152 | case AX25_PROTO_STD_SIMPLEX: |
| 153 | case AX25_PROTO_STD_DUPLEX: |
| 154 | ax25_std_t1timer_expiry(ax25); |
| 155 | break; |
| 156 | |
| 157 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 158 | case AX25_PROTO_DAMA_SLAVE: |
| 159 | if (!ax25->ax25_dev->dama.slave) |
| 160 | ax25_std_t1timer_expiry(ax25); |
| 161 | break; |
| 162 | #endif |
| 163 | } |
| 164 | } |
| 165 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 166 | static void ax25_t2timer_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 168 | ax25_cb *ax25 = from_timer(ax25, t, t2timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { |
| 171 | case AX25_PROTO_STD_SIMPLEX: |
| 172 | case AX25_PROTO_STD_DUPLEX: |
| 173 | ax25_std_t2timer_expiry(ax25); |
| 174 | break; |
| 175 | |
| 176 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 177 | case AX25_PROTO_DAMA_SLAVE: |
| 178 | if (!ax25->ax25_dev->dama.slave) |
| 179 | ax25_std_t2timer_expiry(ax25); |
| 180 | break; |
| 181 | #endif |
| 182 | } |
| 183 | } |
| 184 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 185 | static void ax25_t3timer_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 187 | ax25_cb *ax25 = from_timer(ax25, t, t3timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { |
| 190 | case AX25_PROTO_STD_SIMPLEX: |
| 191 | case AX25_PROTO_STD_DUPLEX: |
| 192 | ax25_std_t3timer_expiry(ax25); |
| 193 | break; |
| 194 | |
| 195 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 196 | case AX25_PROTO_DAMA_SLAVE: |
| 197 | if (ax25->ax25_dev->dama.slave) |
| 198 | ax25_ds_t3timer_expiry(ax25); |
| 199 | else |
| 200 | ax25_std_t3timer_expiry(ax25); |
| 201 | break; |
| 202 | #endif |
| 203 | } |
| 204 | } |
| 205 | |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 206 | static void ax25_idletimer_expiry(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Kees Cook | 8dbd05f | 2017-10-24 01:45:39 -0700 | [diff] [blame] | 208 | ax25_cb *ax25 = from_timer(ax25, t, idletimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { |
| 211 | case AX25_PROTO_STD_SIMPLEX: |
| 212 | case AX25_PROTO_STD_DUPLEX: |
| 213 | ax25_std_idletimer_expiry(ax25); |
| 214 | break; |
| 215 | |
| 216 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 217 | case AX25_PROTO_DAMA_SLAVE: |
| 218 | if (ax25->ax25_dev->dama.slave) |
| 219 | ax25_ds_idletimer_expiry(ax25); |
| 220 | else |
| 221 | ax25_std_idletimer_expiry(ax25); |
| 222 | break; |
| 223 | #endif |
| 224 | } |
| 225 | } |