Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +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 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) |
| 5 | * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk) |
| 6 | * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk) |
| 7 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/moduleparam.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/errno.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/socket.h> |
| 14 | #include <linux/in.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 17 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/timer.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/sockios.h> |
| 21 | #include <linux/net.h> |
| 22 | #include <linux/stat.h> |
| 23 | #include <net/ax25.h> |
| 24 | #include <linux/inet.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/if_arp.h> |
| 27 | #include <linux/skbuff.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 28 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <net/sock.h> |
Fabian Frederick | dc8e541 | 2014-10-17 22:00:22 +0200 | [diff] [blame] | 30 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/fcntl.h> |
| 32 | #include <linux/termios.h> /* For TIOCINQ/OUTQ */ |
| 33 | #include <linux/mm.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | #include <linux/notifier.h> |
| 36 | #include <net/netrom.h> |
| 37 | #include <linux/proc_fs.h> |
| 38 | #include <linux/seq_file.h> |
| 39 | #include <net/ip.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 40 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <net/arp.h> |
| 42 | #include <linux/init.h> |
| 43 | |
| 44 | static int nr_ndevs = 4; |
| 45 | |
| 46 | int sysctl_netrom_default_path_quality = NR_DEFAULT_QUAL; |
| 47 | int sysctl_netrom_obsolescence_count_initialiser = NR_DEFAULT_OBS; |
| 48 | int sysctl_netrom_network_ttl_initialiser = NR_DEFAULT_TTL; |
| 49 | int sysctl_netrom_transport_timeout = NR_DEFAULT_T1; |
| 50 | int sysctl_netrom_transport_maximum_tries = NR_DEFAULT_N2; |
| 51 | int sysctl_netrom_transport_acknowledge_delay = NR_DEFAULT_T2; |
| 52 | int sysctl_netrom_transport_busy_delay = NR_DEFAULT_T4; |
| 53 | int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW; |
| 54 | int sysctl_netrom_transport_no_activity_timeout = NR_DEFAULT_IDLE; |
| 55 | int sysctl_netrom_routing_control = NR_DEFAULT_ROUTING; |
| 56 | int sysctl_netrom_link_fails_count = NR_DEFAULT_FAILS; |
Ralf Baechle | e21ce8c | 2005-09-12 14:27:37 -0700 | [diff] [blame] | 57 | int sysctl_netrom_reset_circuit = NR_DEFAULT_RESET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | static unsigned short circuit = 0x101; |
| 60 | |
| 61 | static HLIST_HEAD(nr_list); |
| 62 | static DEFINE_SPINLOCK(nr_list_lock); |
| 63 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 64 | static const struct proto_ops nr_proto_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
Cong Wang | 1a33e10 | 2020-05-02 22:22:19 -0700 | [diff] [blame] | 67 | * NETROM network devices are virtual network devices encapsulating NETROM |
| 68 | * frames into AX.25 which will be sent through an AX.25 device, so form a |
| 69 | * special "super class" of normal net devices; split their locks off into a |
| 70 | * separate class since they always nest. |
| 71 | */ |
| 72 | static struct lock_class_key nr_netdev_xmit_lock_key; |
Cong Wang | 845e0eb | 2020-06-08 14:53:01 -0700 | [diff] [blame] | 73 | static struct lock_class_key nr_netdev_addr_lock_key; |
Cong Wang | 1a33e10 | 2020-05-02 22:22:19 -0700 | [diff] [blame] | 74 | |
| 75 | static void nr_set_lockdep_one(struct net_device *dev, |
| 76 | struct netdev_queue *txq, |
| 77 | void *_unused) |
| 78 | { |
| 79 | lockdep_set_class(&txq->_xmit_lock, &nr_netdev_xmit_lock_key); |
| 80 | } |
| 81 | |
| 82 | static void nr_set_lockdep_key(struct net_device *dev) |
| 83 | { |
Cong Wang | 845e0eb | 2020-06-08 14:53:01 -0700 | [diff] [blame] | 84 | lockdep_set_class(&dev->addr_list_lock, &nr_netdev_addr_lock_key); |
Cong Wang | 1a33e10 | 2020-05-02 22:22:19 -0700 | [diff] [blame] | 85 | netdev_for_each_tx_queue(dev, nr_set_lockdep_one, NULL); |
| 86 | } |
| 87 | |
| 88 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * Socket removal during an interrupt is now safe. |
| 90 | */ |
| 91 | static void nr_remove_socket(struct sock *sk) |
| 92 | { |
| 93 | spin_lock_bh(&nr_list_lock); |
| 94 | sk_del_node_init(sk); |
| 95 | spin_unlock_bh(&nr_list_lock); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Kill all bound sockets on a dropped device. |
| 100 | */ |
| 101 | static void nr_kill_by_device(struct net_device *dev) |
| 102 | { |
| 103 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | spin_lock_bh(&nr_list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 106 | sk_for_each(s, &nr_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | if (nr_sk(s)->device == dev) |
| 108 | nr_disconnect(s, ENETUNREACH); |
| 109 | spin_unlock_bh(&nr_list_lock); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Handle device status changes. |
| 114 | */ |
| 115 | static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 116 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 117 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 119 | if (!net_eq(dev_net(dev), &init_net)) |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 120 | return NOTIFY_DONE; |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (event != NETDEV_DOWN) |
| 123 | return NOTIFY_DONE; |
| 124 | |
| 125 | nr_kill_by_device(dev); |
| 126 | nr_rt_device_down(dev); |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | return NOTIFY_DONE; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Add a socket to the bound sockets list. |
| 133 | */ |
| 134 | static void nr_insert_socket(struct sock *sk) |
| 135 | { |
| 136 | spin_lock_bh(&nr_list_lock); |
| 137 | sk_add_node(sk, &nr_list); |
| 138 | spin_unlock_bh(&nr_list_lock); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Find a socket that wants to accept the Connect Request we just |
| 143 | * received. |
| 144 | */ |
| 145 | static struct sock *nr_find_listener(ax25_address *addr) |
| 146 | { |
| 147 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | spin_lock_bh(&nr_list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 150 | sk_for_each(s, &nr_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | if (!ax25cmp(&nr_sk(s)->source_addr, addr) && |
| 152 | s->sk_state == TCP_LISTEN) { |
Cong Wang | 7314f54 | 2018-12-29 13:56:38 -0800 | [diff] [blame] | 153 | sock_hold(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | goto found; |
| 155 | } |
| 156 | s = NULL; |
| 157 | found: |
| 158 | spin_unlock_bh(&nr_list_lock); |
| 159 | return s; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Find a connected NET/ROM socket given my circuit IDs. |
| 164 | */ |
| 165 | static struct sock *nr_find_socket(unsigned char index, unsigned char id) |
| 166 | { |
| 167 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | spin_lock_bh(&nr_list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 170 | sk_for_each(s, &nr_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | struct nr_sock *nr = nr_sk(s); |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (nr->my_index == index && nr->my_id == id) { |
Cong Wang | 7314f54 | 2018-12-29 13:56:38 -0800 | [diff] [blame] | 174 | sock_hold(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | goto found; |
| 176 | } |
| 177 | } |
| 178 | s = NULL; |
| 179 | found: |
| 180 | spin_unlock_bh(&nr_list_lock); |
| 181 | return s; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Find a connected NET/ROM socket given their circuit IDs. |
| 186 | */ |
| 187 | static struct sock *nr_find_peer(unsigned char index, unsigned char id, |
| 188 | ax25_address *dest) |
| 189 | { |
| 190 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | spin_lock_bh(&nr_list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 193 | sk_for_each(s, &nr_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | struct nr_sock *nr = nr_sk(s); |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (nr->your_index == index && nr->your_id == id && |
| 197 | !ax25cmp(&nr->dest_addr, dest)) { |
Cong Wang | 7314f54 | 2018-12-29 13:56:38 -0800 | [diff] [blame] | 198 | sock_hold(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | goto found; |
| 200 | } |
| 201 | } |
| 202 | s = NULL; |
| 203 | found: |
| 204 | spin_unlock_bh(&nr_list_lock); |
| 205 | return s; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Find next free circuit ID. |
| 210 | */ |
| 211 | static unsigned short nr_find_next_circuit(void) |
| 212 | { |
| 213 | unsigned short id = circuit; |
| 214 | unsigned char i, j; |
| 215 | struct sock *sk; |
| 216 | |
| 217 | for (;;) { |
| 218 | i = id / 256; |
| 219 | j = id % 256; |
| 220 | |
| 221 | if (i != 0 && j != 0) { |
| 222 | if ((sk=nr_find_socket(i, j)) == NULL) |
| 223 | break; |
Cong Wang | 7314f54 | 2018-12-29 13:56:38 -0800 | [diff] [blame] | 224 | sock_put(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | id++; |
| 228 | } |
| 229 | |
| 230 | return id; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Deferred destroy. |
| 235 | */ |
| 236 | void nr_destroy_socket(struct sock *); |
| 237 | |
| 238 | /* |
| 239 | * Handler for deferred kills. |
| 240 | */ |
Kees Cook | 99767f2 | 2017-10-16 17:29:36 -0700 | [diff] [blame] | 241 | static void nr_destroy_timer(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
Kees Cook | 99767f2 | 2017-10-16 17:29:36 -0700 | [diff] [blame] | 243 | struct sock *sk = from_timer(sk, t, sk_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | bh_lock_sock(sk); |
| 245 | sock_hold(sk); |
| 246 | nr_destroy_socket(sk); |
| 247 | bh_unlock_sock(sk); |
| 248 | sock_put(sk); |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * This is called from user mode and the timers. Thus it protects itself |
| 253 | * against interrupt users but doesn't worry about being called during |
| 254 | * work. Once it is removed from the queue no interrupt or bottom half |
| 255 | * will touch it and we are (fairly 8-) ) safe. |
| 256 | */ |
| 257 | void nr_destroy_socket(struct sock *sk) |
| 258 | { |
| 259 | struct sk_buff *skb; |
| 260 | |
| 261 | nr_remove_socket(sk); |
| 262 | |
| 263 | nr_stop_heartbeat(sk); |
| 264 | nr_stop_t1timer(sk); |
| 265 | nr_stop_t2timer(sk); |
| 266 | nr_stop_t4timer(sk); |
| 267 | nr_stop_idletimer(sk); |
| 268 | |
| 269 | nr_clear_queues(sk); /* Flush the queues */ |
| 270 | |
| 271 | while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { |
| 272 | if (skb->sk != sk) { /* A pending connection */ |
| 273 | /* Queue the unaccepted socket for death */ |
| 274 | sock_set_flag(skb->sk, SOCK_DEAD); |
| 275 | nr_start_heartbeat(skb->sk); |
| 276 | nr_sk(skb->sk)->state = NR_STATE_0; |
| 277 | } |
| 278 | |
| 279 | kfree_skb(skb); |
| 280 | } |
| 281 | |
Eric Dumazet | c564039 | 2009-06-16 10:12:03 +0000 | [diff] [blame] | 282 | if (sk_has_allocations(sk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | /* Defer: outstanding buffers */ |
Kees Cook | 841b86f | 2017-10-23 09:40:42 +0200 | [diff] [blame] | 284 | sk->sk_timer.function = nr_destroy_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | sk->sk_timer.expires = jiffies + 2 * HZ; |
| 286 | add_timer(&sk->sk_timer); |
| 287 | } else |
| 288 | sock_put(sk); |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Handling for system calls applied via the various interfaces to a |
| 293 | * NET/ROM socket object. |
| 294 | */ |
| 295 | |
| 296 | static int nr_setsockopt(struct socket *sock, int level, int optname, |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 297 | sockptr_t optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
| 299 | struct sock *sk = sock->sk; |
| 300 | struct nr_sock *nr = nr_sk(sk); |
Dan Carpenter | dc35616e | 2022-01-07 10:12:10 +0300 | [diff] [blame] | 301 | unsigned int opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | if (level != SOL_NETROM) |
| 304 | return -ENOPROTOOPT; |
| 305 | |
Xi Wang | 32288eb | 2011-12-27 09:44:53 +0000 | [diff] [blame] | 306 | if (optlen < sizeof(unsigned int)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | return -EINVAL; |
| 308 | |
Dan Carpenter | dc35616e | 2022-01-07 10:12:10 +0300 | [diff] [blame] | 309 | if (copy_from_sockptr(&opt, optval, sizeof(opt))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | return -EFAULT; |
| 311 | |
| 312 | switch (optname) { |
| 313 | case NETROM_T1: |
Dan Carpenter | dc35616e | 2022-01-07 10:12:10 +0300 | [diff] [blame] | 314 | if (opt < 1 || opt > UINT_MAX / HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return -EINVAL; |
| 316 | nr->t1 = opt * HZ; |
| 317 | return 0; |
| 318 | |
| 319 | case NETROM_T2: |
Dan Carpenter | dc35616e | 2022-01-07 10:12:10 +0300 | [diff] [blame] | 320 | if (opt < 1 || opt > UINT_MAX / HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | return -EINVAL; |
| 322 | nr->t2 = opt * HZ; |
| 323 | return 0; |
| 324 | |
| 325 | case NETROM_N2: |
| 326 | if (opt < 1 || opt > 31) |
| 327 | return -EINVAL; |
| 328 | nr->n2 = opt; |
| 329 | return 0; |
| 330 | |
| 331 | case NETROM_T4: |
Dan Carpenter | dc35616e | 2022-01-07 10:12:10 +0300 | [diff] [blame] | 332 | if (opt < 1 || opt > UINT_MAX / HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | return -EINVAL; |
| 334 | nr->t4 = opt * HZ; |
| 335 | return 0; |
| 336 | |
| 337 | case NETROM_IDLE: |
Dan Carpenter | dc35616e | 2022-01-07 10:12:10 +0300 | [diff] [blame] | 338 | if (opt > UINT_MAX / (60 * HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return -EINVAL; |
| 340 | nr->idle = opt * 60 * HZ; |
| 341 | return 0; |
| 342 | |
| 343 | default: |
| 344 | return -ENOPROTOOPT; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static int nr_getsockopt(struct socket *sock, int level, int optname, |
| 349 | char __user *optval, int __user *optlen) |
| 350 | { |
| 351 | struct sock *sk = sock->sk; |
| 352 | struct nr_sock *nr = nr_sk(sk); |
| 353 | int val = 0; |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 354 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
| 356 | if (level != SOL_NETROM) |
| 357 | return -ENOPROTOOPT; |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | if (get_user(len, optlen)) |
| 360 | return -EFAULT; |
| 361 | |
| 362 | if (len < 0) |
| 363 | return -EINVAL; |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | switch (optname) { |
| 366 | case NETROM_T1: |
| 367 | val = nr->t1 / HZ; |
| 368 | break; |
| 369 | |
| 370 | case NETROM_T2: |
| 371 | val = nr->t2 / HZ; |
| 372 | break; |
| 373 | |
| 374 | case NETROM_N2: |
| 375 | val = nr->n2; |
| 376 | break; |
| 377 | |
| 378 | case NETROM_T4: |
| 379 | val = nr->t4 / HZ; |
| 380 | break; |
| 381 | |
| 382 | case NETROM_IDLE: |
| 383 | val = nr->idle / (60 * HZ); |
| 384 | break; |
| 385 | |
| 386 | default: |
| 387 | return -ENOPROTOOPT; |
| 388 | } |
| 389 | |
| 390 | len = min_t(unsigned int, len, sizeof(int)); |
| 391 | |
| 392 | if (put_user(len, optlen)) |
| 393 | return -EFAULT; |
| 394 | |
| 395 | return copy_to_user(optval, &val, len) ? -EFAULT : 0; |
| 396 | } |
| 397 | |
| 398 | static int nr_listen(struct socket *sock, int backlog) |
| 399 | { |
| 400 | struct sock *sk = sock->sk; |
| 401 | |
| 402 | lock_sock(sk); |
| 403 | if (sk->sk_state != TCP_LISTEN) { |
| 404 | memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN); |
| 405 | sk->sk_max_ack_backlog = backlog; |
| 406 | sk->sk_state = TCP_LISTEN; |
| 407 | release_sock(sk); |
| 408 | return 0; |
| 409 | } |
| 410 | release_sock(sk); |
| 411 | |
| 412 | return -EOPNOTSUPP; |
| 413 | } |
| 414 | |
| 415 | static struct proto nr_proto = { |
| 416 | .name = "NETROM", |
| 417 | .owner = THIS_MODULE, |
| 418 | .obj_size = sizeof(struct nr_sock), |
| 419 | }; |
| 420 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 421 | static int nr_create(struct net *net, struct socket *sock, int protocol, |
| 422 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
| 424 | struct sock *sk; |
| 425 | struct nr_sock *nr; |
| 426 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 427 | if (!net_eq(net, &init_net)) |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 428 | return -EAFNOSUPPORT; |
| 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | if (sock->type != SOCK_SEQPACKET || protocol != 0) |
| 431 | return -ESOCKTNOSUPPORT; |
| 432 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 433 | sk = sk_alloc(net, PF_NETROM, GFP_ATOMIC, &nr_proto, kern); |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 434 | if (sk == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | return -ENOMEM; |
| 436 | |
| 437 | nr = nr_sk(sk); |
| 438 | |
| 439 | sock_init_data(sock, sk); |
| 440 | |
| 441 | sock->ops = &nr_proto_ops; |
| 442 | sk->sk_protocol = protocol; |
| 443 | |
| 444 | skb_queue_head_init(&nr->ack_queue); |
| 445 | skb_queue_head_init(&nr->reseq_queue); |
| 446 | skb_queue_head_init(&nr->frag_queue); |
| 447 | |
| 448 | nr_init_timers(sk); |
| 449 | |
Ralf Baechle | 4d8937d | 2006-05-03 23:27:47 -0700 | [diff] [blame] | 450 | nr->t1 = |
| 451 | msecs_to_jiffies(sysctl_netrom_transport_timeout); |
| 452 | nr->t2 = |
| 453 | msecs_to_jiffies(sysctl_netrom_transport_acknowledge_delay); |
| 454 | nr->n2 = |
| 455 | msecs_to_jiffies(sysctl_netrom_transport_maximum_tries); |
| 456 | nr->t4 = |
| 457 | msecs_to_jiffies(sysctl_netrom_transport_busy_delay); |
| 458 | nr->idle = |
| 459 | msecs_to_jiffies(sysctl_netrom_transport_no_activity_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | nr->window = sysctl_netrom_transport_requested_window_size; |
| 461 | |
| 462 | nr->bpqext = 1; |
| 463 | nr->state = NR_STATE_0; |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | static struct sock *nr_make_new(struct sock *osk) |
| 469 | { |
| 470 | struct sock *sk; |
| 471 | struct nr_sock *nr, *onr; |
| 472 | |
| 473 | if (osk->sk_type != SOCK_SEQPACKET) |
| 474 | return NULL; |
| 475 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 476 | sk = sk_alloc(sock_net(osk), PF_NETROM, GFP_ATOMIC, osk->sk_prot, 0); |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 477 | if (sk == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | return NULL; |
| 479 | |
| 480 | nr = nr_sk(sk); |
| 481 | |
| 482 | sock_init_data(NULL, sk); |
| 483 | |
| 484 | sk->sk_type = osk->sk_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | sk->sk_priority = osk->sk_priority; |
| 486 | sk->sk_protocol = osk->sk_protocol; |
| 487 | sk->sk_rcvbuf = osk->sk_rcvbuf; |
| 488 | sk->sk_sndbuf = osk->sk_sndbuf; |
| 489 | sk->sk_state = TCP_ESTABLISHED; |
Ralf Baechle | 53b924b | 2005-08-23 10:11:30 -0700 | [diff] [blame] | 490 | sock_copy_flags(sk, osk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | skb_queue_head_init(&nr->ack_queue); |
| 493 | skb_queue_head_init(&nr->reseq_queue); |
| 494 | skb_queue_head_init(&nr->frag_queue); |
| 495 | |
| 496 | nr_init_timers(sk); |
| 497 | |
| 498 | onr = nr_sk(osk); |
| 499 | |
| 500 | nr->t1 = onr->t1; |
| 501 | nr->t2 = onr->t2; |
| 502 | nr->n2 = onr->n2; |
| 503 | nr->t4 = onr->t4; |
| 504 | nr->idle = onr->idle; |
| 505 | nr->window = onr->window; |
| 506 | |
| 507 | nr->device = onr->device; |
| 508 | nr->bpqext = onr->bpqext; |
| 509 | |
| 510 | return sk; |
| 511 | } |
| 512 | |
| 513 | static int nr_release(struct socket *sock) |
| 514 | { |
| 515 | struct sock *sk = sock->sk; |
| 516 | struct nr_sock *nr; |
| 517 | |
| 518 | if (sk == NULL) return 0; |
| 519 | |
| 520 | sock_hold(sk); |
Jarek Poplawski | 859f4c7 | 2008-10-06 12:54:57 -0700 | [diff] [blame] | 521 | sock_orphan(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | lock_sock(sk); |
| 523 | nr = nr_sk(sk); |
| 524 | |
| 525 | switch (nr->state) { |
| 526 | case NR_STATE_0: |
| 527 | case NR_STATE_1: |
| 528 | case NR_STATE_2: |
| 529 | nr_disconnect(sk, 0); |
| 530 | nr_destroy_socket(sk); |
| 531 | break; |
| 532 | |
| 533 | case NR_STATE_3: |
| 534 | nr_clear_queues(sk); |
| 535 | nr->n2count = 0; |
| 536 | nr_write_internal(sk, NR_DISCREQ); |
| 537 | nr_start_t1timer(sk); |
| 538 | nr_stop_t2timer(sk); |
| 539 | nr_stop_t4timer(sk); |
| 540 | nr_stop_idletimer(sk); |
| 541 | nr->state = NR_STATE_2; |
| 542 | sk->sk_state = TCP_CLOSE; |
| 543 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 544 | sk->sk_state_change(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | sock_set_flag(sk, SOCK_DESTROY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | break; |
| 547 | |
| 548 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | break; |
| 550 | } |
| 551 | |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 552 | sock->sk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | release_sock(sk); |
| 554 | sock_put(sk); |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 560 | { |
| 561 | struct sock *sk = sock->sk; |
| 562 | struct nr_sock *nr = nr_sk(sk); |
| 563 | struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr; |
| 564 | struct net_device *dev; |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 565 | ax25_uid_assoc *user; |
| 566 | ax25_address *source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
| 568 | lock_sock(sk); |
| 569 | if (!sock_flag(sk, SOCK_ZAPPED)) { |
| 570 | release_sock(sk); |
| 571 | return -EINVAL; |
| 572 | } |
| 573 | if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) { |
| 574 | release_sock(sk); |
| 575 | return -EINVAL; |
| 576 | } |
| 577 | if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) { |
| 578 | release_sock(sk); |
| 579 | return -EINVAL; |
| 580 | } |
| 581 | if (addr->fsa_ax25.sax25_family != AF_NETROM) { |
| 582 | release_sock(sk); |
| 583 | return -EINVAL; |
| 584 | } |
| 585 | if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | release_sock(sk); |
| 587 | return -EADDRNOTAVAIL; |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Only the super user can set an arbitrary user callsign. |
| 592 | */ |
| 593 | if (addr->fsa_ax25.sax25_ndigis == 1) { |
| 594 | if (!capable(CAP_NET_BIND_SERVICE)) { |
| 595 | dev_put(dev); |
| 596 | release_sock(sk); |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 597 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | nr->user_addr = addr->fsa_digipeater[0]; |
| 600 | nr->source_addr = addr->fsa_ax25.sax25_call; |
| 601 | } else { |
| 602 | source = &addr->fsa_ax25.sax25_call; |
| 603 | |
David Howells | ba95b23 | 2008-11-14 10:39:08 +1100 | [diff] [blame] | 604 | user = ax25_findbyuid(current_euid()); |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 605 | if (user) { |
| 606 | nr->user_addr = user->call; |
| 607 | ax25_uid_put(user); |
| 608 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) { |
| 610 | release_sock(sk); |
| 611 | dev_put(dev); |
| 612 | return -EPERM; |
| 613 | } |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 614 | nr->user_addr = *source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | nr->source_addr = *source; |
| 618 | } |
| 619 | |
| 620 | nr->device = dev; |
| 621 | nr_insert_socket(sk); |
| 622 | |
| 623 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 624 | dev_put(dev); |
| 625 | release_sock(sk); |
Ralf Baechle | 8849b72 | 2011-04-14 00:20:07 -0700 | [diff] [blame] | 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static int nr_connect(struct socket *sock, struct sockaddr *uaddr, |
| 631 | int addr_len, int flags) |
| 632 | { |
| 633 | struct sock *sk = sock->sk; |
| 634 | struct nr_sock *nr = nr_sk(sk); |
| 635 | struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr; |
Jakub Kicinski | c045ad2 | 2021-10-12 08:58:35 -0700 | [diff] [blame] | 636 | const ax25_address *source = NULL; |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 637 | ax25_uid_assoc *user; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | struct net_device *dev; |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 639 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | lock_sock(sk); |
| 642 | if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) { |
| 643 | sock->state = SS_CONNECTED; |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 644 | goto out_release; /* Connect completed during a ERESTARTSYS event */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) { |
| 648 | sock->state = SS_UNCONNECTED; |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 649 | err = -ECONNREFUSED; |
| 650 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | if (sk->sk_state == TCP_ESTABLISHED) { |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 654 | err = -EISCONN; /* No reconnect on a seqpacket socket */ |
| 655 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | } |
| 657 | |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 658 | sk->sk_state = TCP_CLOSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | sock->state = SS_UNCONNECTED; |
| 660 | |
| 661 | if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) { |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 662 | err = -EINVAL; |
| 663 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | if (addr->sax25_family != AF_NETROM) { |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 666 | err = -EINVAL; |
| 667 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */ |
| 670 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 671 | |
| 672 | if ((dev = nr_dev_first()) == NULL) { |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 673 | err = -ENETUNREACH; |
| 674 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
Jakub Kicinski | c045ad2 | 2021-10-12 08:58:35 -0700 | [diff] [blame] | 676 | source = (const ax25_address *)dev->dev_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
David Howells | ba95b23 | 2008-11-14 10:39:08 +1100 | [diff] [blame] | 678 | user = ax25_findbyuid(current_euid()); |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 679 | if (user) { |
| 680 | nr->user_addr = user->call; |
| 681 | ax25_uid_put(user); |
| 682 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) { |
| 684 | dev_put(dev); |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 685 | err = -EPERM; |
| 686 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 688 | nr->user_addr = *source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | nr->source_addr = *source; |
| 692 | nr->device = dev; |
| 693 | |
| 694 | dev_put(dev); |
| 695 | nr_insert_socket(sk); /* Finish the bind */ |
| 696 | } |
| 697 | |
| 698 | nr->dest_addr = addr->sax25_call; |
| 699 | |
| 700 | release_sock(sk); |
| 701 | circuit = nr_find_next_circuit(); |
| 702 | lock_sock(sk); |
| 703 | |
| 704 | nr->my_index = circuit / 256; |
| 705 | nr->my_id = circuit % 256; |
| 706 | |
| 707 | circuit++; |
| 708 | |
| 709 | /* Move to connecting socket, start sending Connect Requests */ |
| 710 | sock->state = SS_CONNECTING; |
| 711 | sk->sk_state = TCP_SYN_SENT; |
| 712 | |
| 713 | nr_establish_data_link(sk); |
| 714 | |
| 715 | nr->state = NR_STATE_1; |
| 716 | |
| 717 | nr_start_heartbeat(sk); |
| 718 | |
| 719 | /* Now the loop */ |
| 720 | if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) { |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 721 | err = -EINPROGRESS; |
| 722 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | /* |
| 726 | * A Connect Ack with Choke or timeout or failed routing will go to |
| 727 | * closed. |
| 728 | */ |
| 729 | if (sk->sk_state == TCP_SYN_SENT) { |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 730 | DEFINE_WAIT(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | for (;;) { |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 733 | prepare_to_wait(sk_sleep(sk), &wait, |
YOSHIFUJI Hideaki | 639fc4c | 2007-07-19 10:44:32 +0900 | [diff] [blame] | 734 | TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | if (sk->sk_state != TCP_SYN_SENT) |
| 736 | break; |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 737 | if (!signal_pending(current)) { |
| 738 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | schedule(); |
| 740 | lock_sock(sk); |
| 741 | continue; |
| 742 | } |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 743 | err = -ERESTARTSYS; |
| 744 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 746 | finish_wait(sk_sleep(sk), &wait); |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 747 | if (err) |
| 748 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 752 | sock->state = SS_UNCONNECTED; |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 753 | err = sock_error(sk); /* Always set at this point */ |
| 754 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | sock->state = SS_CONNECTED; |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 758 | |
| 759 | out_release: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | release_sock(sk); |
| 761 | |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 762 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | } |
| 764 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 765 | static int nr_accept(struct socket *sock, struct socket *newsock, int flags, |
| 766 | bool kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | struct sk_buff *skb; |
| 769 | struct sock *newsk; |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 770 | DEFINE_WAIT(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | struct sock *sk; |
| 772 | int err = 0; |
| 773 | |
| 774 | if ((sk = sock->sk) == NULL) |
| 775 | return -EINVAL; |
| 776 | |
| 777 | lock_sock(sk); |
| 778 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 779 | err = -EOPNOTSUPP; |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 780 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | if (sk->sk_state != TCP_LISTEN) { |
| 784 | err = -EINVAL; |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 785 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* |
| 789 | * The write queue this time is holding sockets ready to use |
| 790 | * hooked into the SABM we saved |
| 791 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | for (;;) { |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 793 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 795 | if (skb) |
| 796 | break; |
| 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | if (flags & O_NONBLOCK) { |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 799 | err = -EWOULDBLOCK; |
| 800 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 802 | if (!signal_pending(current)) { |
| 803 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | schedule(); |
| 805 | lock_sock(sk); |
| 806 | continue; |
| 807 | } |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 808 | err = -ERESTARTSYS; |
| 809 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 811 | finish_wait(sk_sleep(sk), &wait); |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 812 | if (err) |
| 813 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
| 815 | newsk = skb->sk; |
David S. Miller | 7b66767 | 2008-06-17 02:36:44 -0700 | [diff] [blame] | 816 | sock_graft(newsk, newsock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | /* Now attach up the new socket */ |
| 819 | kfree_skb(skb); |
Ralf Baechle DL5RB | 18601a7 | 2006-07-03 19:31:14 -0700 | [diff] [blame] | 820 | sk_acceptq_removed(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 822 | out_release: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | release_sock(sk); |
Ralf Baechle | 75606dc | 2007-04-20 16:06:45 -0700 | [diff] [blame] | 824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | return err; |
| 826 | } |
| 827 | |
| 828 | static int nr_getname(struct socket *sock, struct sockaddr *uaddr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 829 | int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
| 831 | struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr; |
| 832 | struct sock *sk = sock->sk; |
| 833 | struct nr_sock *nr = nr_sk(sk); |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 834 | int uaddr_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
Dan Carpenter | 7a3b684 | 2013-04-22 20:22:51 +0000 | [diff] [blame] | 836 | memset(&sax->fsa_ax25, 0, sizeof(struct sockaddr_ax25)); |
| 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | lock_sock(sk); |
| 839 | if (peer != 0) { |
| 840 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 841 | release_sock(sk); |
| 842 | return -ENOTCONN; |
| 843 | } |
| 844 | sax->fsa_ax25.sax25_family = AF_NETROM; |
| 845 | sax->fsa_ax25.sax25_ndigis = 1; |
| 846 | sax->fsa_ax25.sax25_call = nr->user_addr; |
Eric Dumazet | f6b97b2 | 2009-08-06 03:31:07 +0000 | [diff] [blame] | 847 | memset(sax->fsa_digipeater, 0, sizeof(sax->fsa_digipeater)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | sax->fsa_digipeater[0] = nr->dest_addr; |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 849 | uaddr_len = sizeof(struct full_sockaddr_ax25); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } else { |
| 851 | sax->fsa_ax25.sax25_family = AF_NETROM; |
| 852 | sax->fsa_ax25.sax25_ndigis = 0; |
| 853 | sax->fsa_ax25.sax25_call = nr->source_addr; |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 854 | uaddr_len = sizeof(struct sockaddr_ax25); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
| 856 | release_sock(sk); |
| 857 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 858 | return uaddr_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | int nr_rx_frame(struct sk_buff *skb, struct net_device *dev) |
| 862 | { |
| 863 | struct sock *sk; |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 864 | struct sock *make; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | struct nr_sock *nr_make; |
| 866 | ax25_address *src, *dest, *user; |
| 867 | unsigned short circuit_index, circuit_id; |
| 868 | unsigned short peer_circuit_index, peer_circuit_id; |
| 869 | unsigned short frametype, flags, window, timeout; |
| 870 | int ret; |
| 871 | |
Cong Wang | c8c8218 | 2019-06-27 14:30:58 -0700 | [diff] [blame] | 872 | skb_orphan(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
| 874 | /* |
| 875 | * skb->data points to the netrom frame start |
| 876 | */ |
| 877 | |
| 878 | src = (ax25_address *)(skb->data + 0); |
| 879 | dest = (ax25_address *)(skb->data + 7); |
| 880 | |
| 881 | circuit_index = skb->data[15]; |
| 882 | circuit_id = skb->data[16]; |
| 883 | peer_circuit_index = skb->data[17]; |
| 884 | peer_circuit_id = skb->data[18]; |
| 885 | frametype = skb->data[19] & 0x0F; |
| 886 | flags = skb->data[19] & 0xF0; |
| 887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | /* |
| 889 | * Check for an incoming IP over NET/ROM frame. |
| 890 | */ |
Ralf Baechle | 98a82fe | 2005-08-24 11:35:51 -0700 | [diff] [blame] | 891 | if (frametype == NR_PROTOEXT && |
| 892 | circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN); |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 894 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
| 896 | return nr_rx_ip(skb, dev); |
| 897 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | /* |
| 900 | * Find an existing socket connection, based on circuit ID, if it's |
| 901 | * a Connect Request base it on their circuit ID. |
| 902 | * |
| 903 | * Circuit ID 0/0 is not valid but it could still be a "reset" for a |
| 904 | * circuit that no longer exists at the other end ... |
| 905 | */ |
| 906 | |
| 907 | sk = NULL; |
| 908 | |
| 909 | if (circuit_index == 0 && circuit_id == 0) { |
| 910 | if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG) |
| 911 | sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src); |
| 912 | } else { |
| 913 | if (frametype == NR_CONNREQ) |
| 914 | sk = nr_find_peer(circuit_index, circuit_id, src); |
| 915 | else |
| 916 | sk = nr_find_socket(circuit_index, circuit_id); |
| 917 | } |
| 918 | |
| 919 | if (sk != NULL) { |
Cong Wang | 7314f54 | 2018-12-29 13:56:38 -0800 | [diff] [blame] | 920 | bh_lock_sock(sk); |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 921 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
| 923 | if (frametype == NR_CONNACK && skb->len == 22) |
| 924 | nr_sk(sk)->bpqext = 1; |
| 925 | else |
| 926 | nr_sk(sk)->bpqext = 0; |
| 927 | |
| 928 | ret = nr_process_rx_frame(sk, skb); |
| 929 | bh_unlock_sock(sk); |
Cong Wang | 7314f54 | 2018-12-29 13:56:38 -0800 | [diff] [blame] | 930 | sock_put(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | return ret; |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * Now it should be a CONNREQ. |
| 936 | */ |
| 937 | if (frametype != NR_CONNREQ) { |
| 938 | /* |
| 939 | * Here it would be nice to be able to send a reset but |
Ralf Baechle | e21ce8c | 2005-09-12 14:27:37 -0700 | [diff] [blame] | 940 | * NET/ROM doesn't have one. We've tried to extend the protocol |
| 941 | * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that |
| 942 | * apparently kills BPQ boxes... :-( |
| 943 | * So now we try to follow the established behaviour of |
| 944 | * G8PZT's Xrouter which is sending packets with command type 7 |
| 945 | * as an extension of the protocol. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | */ |
Ralf Baechle | e21ce8c | 2005-09-12 14:27:37 -0700 | [diff] [blame] | 947 | if (sysctl_netrom_reset_circuit && |
| 948 | (frametype != NR_RESET || flags != 0)) |
| 949 | nr_transmit_reset(skb, 1); |
| 950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | sk = nr_find_listener(dest); |
| 955 | |
| 956 | user = (ax25_address *)(skb->data + 21); |
| 957 | |
| 958 | if (sk == NULL || sk_acceptq_is_full(sk) || |
| 959 | (make = nr_make_new(sk)) == NULL) { |
| 960 | nr_transmit_refusal(skb, 0); |
| 961 | if (sk) |
Cong Wang | 7314f54 | 2018-12-29 13:56:38 -0800 | [diff] [blame] | 962 | sock_put(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | return 0; |
| 964 | } |
| 965 | |
Cong Wang | 7314f54 | 2018-12-29 13:56:38 -0800 | [diff] [blame] | 966 | bh_lock_sock(sk); |
| 967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | window = skb->data[20]; |
| 969 | |
Cong Wang | 4638faa | 2019-07-22 20:41:22 -0700 | [diff] [blame] | 970 | sock_hold(make); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | skb->sk = make; |
Cong Wang | c8c8218 | 2019-06-27 14:30:58 -0700 | [diff] [blame] | 972 | skb->destructor = sock_efree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | make->sk_state = TCP_ESTABLISHED; |
| 974 | |
| 975 | /* Fill in his circuit details */ |
| 976 | nr_make = nr_sk(make); |
| 977 | nr_make->source_addr = *dest; |
| 978 | nr_make->dest_addr = *src; |
| 979 | nr_make->user_addr = *user; |
| 980 | |
| 981 | nr_make->your_index = circuit_index; |
| 982 | nr_make->your_id = circuit_id; |
| 983 | |
| 984 | bh_unlock_sock(sk); |
| 985 | circuit = nr_find_next_circuit(); |
| 986 | bh_lock_sock(sk); |
| 987 | |
| 988 | nr_make->my_index = circuit / 256; |
| 989 | nr_make->my_id = circuit % 256; |
| 990 | |
| 991 | circuit++; |
| 992 | |
| 993 | /* Window negotiation */ |
| 994 | if (window < nr_make->window) |
| 995 | nr_make->window = window; |
| 996 | |
| 997 | /* L4 timeout negotiation */ |
| 998 | if (skb->len == 37) { |
| 999 | timeout = skb->data[36] * 256 + skb->data[35]; |
| 1000 | if (timeout * HZ < nr_make->t1) |
| 1001 | nr_make->t1 = timeout * HZ; |
| 1002 | nr_make->bpqext = 1; |
| 1003 | } else { |
| 1004 | nr_make->bpqext = 0; |
| 1005 | } |
| 1006 | |
| 1007 | nr_write_internal(make, NR_CONNACK); |
| 1008 | |
| 1009 | nr_make->condition = 0x00; |
| 1010 | nr_make->vs = 0; |
| 1011 | nr_make->va = 0; |
| 1012 | nr_make->vr = 0; |
| 1013 | nr_make->vl = 0; |
| 1014 | nr_make->state = NR_STATE_3; |
Ralf Baechle DL5RB | 18601a7 | 2006-07-03 19:31:14 -0700 | [diff] [blame] | 1015 | sk_acceptq_added(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | if (!sock_flag(sk, SOCK_DEAD)) |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1019 | sk->sk_data_ready(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | bh_unlock_sock(sk); |
Cong Wang | 7314f54 | 2018-12-29 13:56:38 -0800 | [diff] [blame] | 1022 | sock_put(sk); |
Ralf Baechle | 5cc29e3 | 2006-07-10 16:23:21 -0700 | [diff] [blame] | 1023 | |
| 1024 | nr_insert_socket(make); |
| 1025 | |
| 1026 | nr_start_heartbeat(make); |
| 1027 | nr_start_idletimer(make); |
| 1028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | return 1; |
| 1030 | } |
| 1031 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1032 | static int nr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { |
| 1034 | struct sock *sk = sock->sk; |
| 1035 | struct nr_sock *nr = nr_sk(sk); |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1036 | DECLARE_SOCKADDR(struct sockaddr_ax25 *, usax, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | int err; |
| 1038 | struct sockaddr_ax25 sax; |
| 1039 | struct sk_buff *skb; |
| 1040 | unsigned char *asmptr; |
| 1041 | int size; |
| 1042 | |
| 1043 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT)) |
| 1044 | return -EINVAL; |
| 1045 | |
| 1046 | lock_sock(sk); |
| 1047 | if (sock_flag(sk, SOCK_ZAPPED)) { |
| 1048 | err = -EADDRNOTAVAIL; |
| 1049 | goto out; |
| 1050 | } |
| 1051 | |
| 1052 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 1053 | send_sig(SIGPIPE, current, 0); |
| 1054 | err = -EPIPE; |
| 1055 | goto out; |
| 1056 | } |
| 1057 | |
| 1058 | if (nr->device == NULL) { |
| 1059 | err = -ENETUNREACH; |
| 1060 | goto out; |
| 1061 | } |
| 1062 | |
| 1063 | if (usax) { |
| 1064 | if (msg->msg_namelen < sizeof(sax)) { |
| 1065 | err = -EINVAL; |
| 1066 | goto out; |
| 1067 | } |
| 1068 | sax = *usax; |
| 1069 | if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) { |
| 1070 | err = -EISCONN; |
| 1071 | goto out; |
| 1072 | } |
| 1073 | if (sax.sax25_family != AF_NETROM) { |
| 1074 | err = -EINVAL; |
| 1075 | goto out; |
| 1076 | } |
| 1077 | } else { |
| 1078 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1079 | err = -ENOTCONN; |
| 1080 | goto out; |
| 1081 | } |
| 1082 | sax.sax25_family = AF_NETROM; |
| 1083 | sax.sax25_call = nr->dest_addr; |
| 1084 | } |
| 1085 | |
Alan Cox | 83e0bbc | 2009-03-27 00:28:21 -0700 | [diff] [blame] | 1086 | /* Build a packet - the conventional user limit is 236 bytes. We can |
| 1087 | do ludicrously large NetROM frames but must not overflow */ |
Jean Delvare | cc29c70 | 2009-04-22 00:49:51 -0700 | [diff] [blame] | 1088 | if (len > 65536) { |
| 1089 | err = -EMSGSIZE; |
| 1090 | goto out; |
| 1091 | } |
Alan Cox | 83e0bbc | 2009-03-27 00:28:21 -0700 | [diff] [blame] | 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN; |
| 1094 | |
| 1095 | if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL) |
| 1096 | goto out; |
| 1097 | |
| 1098 | skb_reserve(skb, size - len); |
Arnaldo Carvalho de Melo | eeeb037 | 2007-03-14 21:04:34 -0300 | [diff] [blame] | 1099 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
| 1101 | /* |
| 1102 | * Push down the NET/ROM header |
| 1103 | */ |
| 1104 | |
| 1105 | asmptr = skb_push(skb, NR_TRANSPORT_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
| 1107 | /* Build a NET/ROM Transport header */ |
| 1108 | |
| 1109 | *asmptr++ = nr->your_index; |
| 1110 | *asmptr++ = nr->your_id; |
| 1111 | *asmptr++ = 0; /* To be filled in later */ |
| 1112 | *asmptr++ = 0; /* Ditto */ |
| 1113 | *asmptr++ = NR_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
| 1115 | /* |
| 1116 | * Put the data on the end |
| 1117 | */ |
Arnaldo Carvalho de Melo | eeeb037 | 2007-03-14 21:04:34 -0300 | [diff] [blame] | 1118 | skb_put(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | /* User data follows immediately after the NET/ROM transport header */ |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 1121 | if (memcpy_from_msg(skb_transport_header(skb), msg, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | kfree_skb(skb); |
| 1123 | err = -EFAULT; |
| 1124 | goto out; |
| 1125 | } |
| 1126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1128 | kfree_skb(skb); |
| 1129 | err = -ENOTCONN; |
| 1130 | goto out; |
| 1131 | } |
| 1132 | |
| 1133 | nr_output(sk, skb); /* Shove it onto the queue */ |
| 1134 | |
| 1135 | err = len; |
| 1136 | out: |
| 1137 | release_sock(sk); |
| 1138 | return err; |
| 1139 | } |
| 1140 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1141 | static int nr_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 1142 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | { |
| 1144 | struct sock *sk = sock->sk; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1145 | DECLARE_SOCKADDR(struct sockaddr_ax25 *, sax, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | size_t copied; |
| 1147 | struct sk_buff *skb; |
| 1148 | int er; |
| 1149 | |
| 1150 | /* |
| 1151 | * This works for seqpacket too. The receiver has ordered the queue for |
| 1152 | * us! We do one quick check first though |
| 1153 | */ |
| 1154 | |
| 1155 | lock_sock(sk); |
| 1156 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1157 | release_sock(sk); |
| 1158 | return -ENOTCONN; |
| 1159 | } |
| 1160 | |
| 1161 | /* Now we can treat all alike */ |
| 1162 | if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) { |
| 1163 | release_sock(sk); |
| 1164 | return er; |
| 1165 | } |
| 1166 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 1167 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | copied = skb->len; |
| 1169 | |
| 1170 | if (copied > size) { |
| 1171 | copied = size; |
| 1172 | msg->msg_flags |= MSG_TRUNC; |
| 1173 | } |
| 1174 | |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 1175 | er = skb_copy_datagram_msg(skb, 0, msg, copied); |
Alan Cox | 6cf5c95 | 2012-09-04 04:13:18 +0000 | [diff] [blame] | 1176 | if (er < 0) { |
| 1177 | skb_free_datagram(sk, skb); |
| 1178 | release_sock(sk); |
| 1179 | return er; |
| 1180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | |
| 1182 | if (sax != NULL) { |
Wei Yongjun | c802d75 | 2013-04-09 10:07:19 +0800 | [diff] [blame] | 1183 | memset(sax, 0, sizeof(*sax)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | sax->sax25_family = AF_NETROM; |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1185 | skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call, |
| 1186 | AX25_ADDR_LEN); |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 1187 | msg->msg_namelen = sizeof(*sax); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
David S. Miller | 80e20f6 | 2009-03-27 17:22:55 -0700 | [diff] [blame] | 1190 | skb_free_datagram(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | |
| 1192 | release_sock(sk); |
| 1193 | return copied; |
| 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 1198 | { |
| 1199 | struct sock *sk = sock->sk; |
| 1200 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | switch (cmd) { |
| 1203 | case TIOCOUTQ: { |
| 1204 | long amount; |
Christoph Hellwig | 5ff7630 | 2006-01-03 14:14:46 -0800 | [diff] [blame] | 1205 | |
| 1206 | lock_sock(sk); |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 1207 | amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | if (amount < 0) |
| 1209 | amount = 0; |
| 1210 | release_sock(sk); |
| 1211 | return put_user(amount, (int __user *)argp); |
| 1212 | } |
| 1213 | |
| 1214 | case TIOCINQ: { |
| 1215 | struct sk_buff *skb; |
| 1216 | long amount = 0L; |
Christoph Hellwig | 5ff7630 | 2006-01-03 14:14:46 -0800 | [diff] [blame] | 1217 | |
| 1218 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | /* These two are safe on a single CPU system as only user tasks fiddle here */ |
| 1220 | if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) |
| 1221 | amount = skb->len; |
| 1222 | release_sock(sk); |
| 1223 | return put_user(amount, (int __user *)argp); |
| 1224 | } |
| 1225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | case SIOCGIFADDR: |
| 1227 | case SIOCSIFADDR: |
| 1228 | case SIOCGIFDSTADDR: |
| 1229 | case SIOCSIFDSTADDR: |
| 1230 | case SIOCGIFBRDADDR: |
| 1231 | case SIOCSIFBRDADDR: |
| 1232 | case SIOCGIFNETMASK: |
| 1233 | case SIOCSIFNETMASK: |
| 1234 | case SIOCGIFMETRIC: |
| 1235 | case SIOCSIFMETRIC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | return -EINVAL; |
| 1237 | |
| 1238 | case SIOCADDRT: |
| 1239 | case SIOCDELRT: |
| 1240 | case SIOCNRDECOBS: |
Ralf Baechle | 0f20f5a | 2011-11-24 23:54:10 +0000 | [diff] [blame] | 1241 | if (!capable(CAP_NET_ADMIN)) |
| 1242 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | return nr_rt_ioctl(cmd, argp); |
| 1244 | |
| 1245 | default: |
Christoph Hellwig | b5e5fa5 | 2006-01-03 14:18:33 -0800 | [diff] [blame] | 1246 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | #ifdef CONFIG_PROC_FS |
| 1253 | |
| 1254 | static void *nr_info_start(struct seq_file *seq, loff_t *pos) |
Jules Irenge | d087f18 | 2020-02-23 23:16:49 +0000 | [diff] [blame] | 1255 | __acquires(&nr_list_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | spin_lock_bh(&nr_list_lock); |
Li Zefan | 90dd7f5 | 2010-02-08 23:19:42 +0000 | [diff] [blame] | 1258 | return seq_hlist_start_head(&nr_list, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1262 | { |
Li Zefan | 90dd7f5 | 2010-02-08 23:19:42 +0000 | [diff] [blame] | 1263 | return seq_hlist_next(v, &nr_list, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 1265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | static void nr_info_stop(struct seq_file *seq, void *v) |
Jules Irenge | 8b003f0 | 2020-02-23 23:16:50 +0000 | [diff] [blame] | 1267 | __releases(&nr_list_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | { |
| 1269 | spin_unlock_bh(&nr_list_lock); |
| 1270 | } |
| 1271 | |
| 1272 | static int nr_info_show(struct seq_file *seq, void *v) |
| 1273 | { |
Li Zefan | 90dd7f5 | 2010-02-08 23:19:42 +0000 | [diff] [blame] | 1274 | struct sock *s = sk_entry(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | struct net_device *dev; |
| 1276 | struct nr_sock *nr; |
| 1277 | const char *devname; |
Ralf Baechle | f75268c | 2005-09-06 15:49:39 -0700 | [diff] [blame] | 1278 | char buf[11]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | |
| 1280 | if (v == SEQ_START_TOKEN) |
| 1281 | seq_puts(seq, |
| 1282 | "user_addr dest_node src_node dev my your st vs vr va t1 t2 t4 idle n2 wnd Snd-Q Rcv-Q inode\n"); |
| 1283 | |
| 1284 | else { |
| 1285 | |
| 1286 | bh_lock_sock(s); |
| 1287 | nr = nr_sk(s); |
| 1288 | |
| 1289 | if ((dev = nr->device) == NULL) |
| 1290 | devname = "???"; |
| 1291 | else |
| 1292 | devname = dev->name; |
| 1293 | |
Ralf Baechle | f75268c | 2005-09-06 15:49:39 -0700 | [diff] [blame] | 1294 | seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr)); |
| 1295 | seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr)); |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 1296 | seq_printf(seq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | "%-9s %-3s %02X/%02X %02X/%02X %2d %3d %3d %3d %3lu/%03lu %2lu/%02lu %3lu/%03lu %3lu/%03lu %2d/%02d %3d %5d %5d %ld\n", |
Ralf Baechle | f75268c | 2005-09-06 15:49:39 -0700 | [diff] [blame] | 1298 | ax2asc(buf, &nr->source_addr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | devname, |
| 1300 | nr->my_index, |
| 1301 | nr->my_id, |
| 1302 | nr->your_index, |
| 1303 | nr->your_id, |
| 1304 | nr->state, |
| 1305 | nr->vs, |
| 1306 | nr->vr, |
| 1307 | nr->va, |
| 1308 | ax25_display_timer(&nr->t1timer) / HZ, |
| 1309 | nr->t1 / HZ, |
| 1310 | ax25_display_timer(&nr->t2timer) / HZ, |
| 1311 | nr->t2 / HZ, |
| 1312 | ax25_display_timer(&nr->t4timer) / HZ, |
| 1313 | nr->t4 / HZ, |
| 1314 | ax25_display_timer(&nr->idletimer) / (60 * HZ), |
| 1315 | nr->idle / (60 * HZ), |
| 1316 | nr->n2count, |
| 1317 | nr->n2, |
| 1318 | nr->window, |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 1319 | sk_wmem_alloc_get(s), |
| 1320 | sk_rmem_alloc_get(s), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L); |
| 1322 | |
| 1323 | bh_unlock_sock(s); |
| 1324 | } |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 1328 | static const struct seq_operations nr_info_seqops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | .start = nr_info_start, |
| 1330 | .next = nr_info_next, |
| 1331 | .stop = nr_info_stop, |
| 1332 | .show = nr_info_show, |
| 1333 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | #endif /* CONFIG_PROC_FS */ |
| 1335 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1336 | static const struct net_proto_family nr_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | .family = PF_NETROM, |
| 1338 | .create = nr_create, |
| 1339 | .owner = THIS_MODULE, |
| 1340 | }; |
| 1341 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1342 | static const struct proto_ops nr_proto_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | .family = PF_NETROM, |
| 1344 | .owner = THIS_MODULE, |
| 1345 | .release = nr_release, |
| 1346 | .bind = nr_bind, |
| 1347 | .connect = nr_connect, |
| 1348 | .socketpair = sock_no_socketpair, |
| 1349 | .accept = nr_accept, |
| 1350 | .getname = nr_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1351 | .poll = datagram_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | .ioctl = nr_ioctl, |
Arnd Bergmann | c7cbdbf | 2019-04-17 22:51:48 +0200 | [diff] [blame] | 1353 | .gettstamp = sock_gettstamp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | .listen = nr_listen, |
| 1355 | .shutdown = sock_no_shutdown, |
| 1356 | .setsockopt = nr_setsockopt, |
| 1357 | .getsockopt = nr_getsockopt, |
| 1358 | .sendmsg = nr_sendmsg, |
| 1359 | .recvmsg = nr_recvmsg, |
| 1360 | .mmap = sock_no_mmap, |
| 1361 | .sendpage = sock_no_sendpage, |
| 1362 | }; |
| 1363 | |
| 1364 | static struct notifier_block nr_dev_notifier = { |
| 1365 | .notifier_call = nr_device_event, |
| 1366 | }; |
| 1367 | |
| 1368 | static struct net_device **dev_nr; |
| 1369 | |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 1370 | static struct ax25_protocol nr_pid = { |
| 1371 | .pid = AX25_P_NETROM, |
| 1372 | .func = nr_route_frame |
| 1373 | }; |
| 1374 | |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame] | 1375 | static struct ax25_linkfail nr_linkfail_notifier = { |
| 1376 | .func = nr_link_failed, |
| 1377 | }; |
| 1378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | static int __init nr_proto_init(void) |
| 1380 | { |
| 1381 | int i; |
| 1382 | int rc = proto_register(&nr_proto, 0); |
| 1383 | |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1384 | if (rc) |
| 1385 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
| 1387 | if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) { |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1388 | pr_err("NET/ROM: %s - nr_ndevs parameter too large\n", |
| 1389 | __func__); |
| 1390 | rc = -EINVAL; |
| 1391 | goto unregister_proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1394 | dev_nr = kcalloc(nr_ndevs, sizeof(struct net_device *), GFP_KERNEL); |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1395 | if (!dev_nr) { |
| 1396 | pr_err("NET/ROM: %s - unable to allocate device array\n", |
| 1397 | __func__); |
| 1398 | rc = -ENOMEM; |
| 1399 | goto unregister_proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | for (i = 0; i < nr_ndevs; i++) { |
| 1403 | char name[IFNAMSIZ]; |
| 1404 | struct net_device *dev; |
| 1405 | |
| 1406 | sprintf(name, "nr%d", i); |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 1407 | dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, nr_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | if (!dev) { |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1409 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | goto fail; |
| 1411 | } |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 1412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | dev->base_addr = i; |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1414 | rc = register_netdev(dev); |
| 1415 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | free_netdev(dev); |
| 1417 | goto fail; |
| 1418 | } |
Cong Wang | 1a33e10 | 2020-05-02 22:22:19 -0700 | [diff] [blame] | 1419 | nr_set_lockdep_key(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | dev_nr[i] = dev; |
| 1421 | } |
| 1422 | |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1423 | rc = sock_register(&nr_family_ops); |
| 1424 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | goto fail; |
YOSHIFUJI Hideaki | 5f8f59d | 2007-02-09 23:25:09 +0900 | [diff] [blame] | 1426 | |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1427 | rc = register_netdevice_notifier(&nr_dev_notifier); |
| 1428 | if (rc) |
| 1429 | goto out_sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | |
Ralf Baechle | 8d5cf59 | 2006-12-14 15:50:01 -0800 | [diff] [blame] | 1431 | ax25_register_pid(&nr_pid); |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame] | 1432 | ax25_linkfail_register(&nr_linkfail_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
| 1434 | #ifdef CONFIG_SYSCTL |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1435 | rc = nr_register_sysctl(); |
| 1436 | if (rc) |
| 1437 | goto out_sysctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | #endif |
| 1439 | |
| 1440 | nr_loopback_init(); |
| 1441 | |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1442 | rc = -ENOMEM; |
| 1443 | if (!proc_create_seq("nr", 0444, init_net.proc_net, &nr_info_seqops)) |
| 1444 | goto proc_remove1; |
| 1445 | if (!proc_create_seq("nr_neigh", 0444, init_net.proc_net, |
| 1446 | &nr_neigh_seqops)) |
| 1447 | goto proc_remove2; |
| 1448 | if (!proc_create_seq("nr_nodes", 0444, init_net.proc_net, |
| 1449 | &nr_node_seqops)) |
| 1450 | goto proc_remove3; |
| 1451 | |
| 1452 | return 0; |
| 1453 | |
| 1454 | proc_remove3: |
| 1455 | remove_proc_entry("nr_neigh", init_net.proc_net); |
| 1456 | proc_remove2: |
| 1457 | remove_proc_entry("nr", init_net.proc_net); |
| 1458 | proc_remove1: |
| 1459 | |
| 1460 | nr_loopback_clear(); |
| 1461 | nr_rt_free(); |
| 1462 | |
| 1463 | #ifdef CONFIG_SYSCTL |
| 1464 | nr_unregister_sysctl(); |
| 1465 | out_sysctl: |
| 1466 | #endif |
| 1467 | ax25_linkfail_release(&nr_linkfail_notifier); |
| 1468 | ax25_protocol_release(AX25_P_NETROM); |
| 1469 | unregister_netdevice_notifier(&nr_dev_notifier); |
| 1470 | out_sock: |
| 1471 | sock_unregister(PF_NETROM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | fail: |
| 1473 | while (--i >= 0) { |
| 1474 | unregister_netdev(dev_nr[i]); |
| 1475 | free_netdev(dev_nr[i]); |
| 1476 | } |
| 1477 | kfree(dev_nr); |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1478 | unregister_proto: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | proto_unregister(&nr_proto); |
YueHaibing | d370656 | 2019-04-09 19:53:55 +0800 | [diff] [blame] | 1480 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | module_init(nr_proto_init); |
| 1484 | |
| 1485 | module_param(nr_ndevs, int, 0); |
| 1486 | MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices"); |
| 1487 | |
| 1488 | MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>"); |
| 1489 | MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol"); |
| 1490 | MODULE_LICENSE("GPL"); |
| 1491 | MODULE_ALIAS_NETPROTO(PF_NETROM); |
| 1492 | |
| 1493 | static void __exit nr_exit(void) |
| 1494 | { |
| 1495 | int i; |
| 1496 | |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1497 | remove_proc_entry("nr", init_net.proc_net); |
| 1498 | remove_proc_entry("nr_neigh", init_net.proc_net); |
| 1499 | remove_proc_entry("nr_nodes", init_net.proc_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | nr_loopback_clear(); |
| 1501 | |
| 1502 | nr_rt_free(); |
| 1503 | |
| 1504 | #ifdef CONFIG_SYSCTL |
| 1505 | nr_unregister_sysctl(); |
| 1506 | #endif |
| 1507 | |
Ralf Baechle | a428271 | 2006-12-14 15:51:23 -0800 | [diff] [blame] | 1508 | ax25_linkfail_release(&nr_linkfail_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | ax25_protocol_release(AX25_P_NETROM); |
| 1510 | |
| 1511 | unregister_netdevice_notifier(&nr_dev_notifier); |
| 1512 | |
| 1513 | sock_unregister(PF_NETROM); |
| 1514 | |
| 1515 | for (i = 0; i < nr_ndevs; i++) { |
| 1516 | struct net_device *dev = dev_nr[i]; |
| 1517 | if (dev) { |
| 1518 | unregister_netdev(dev); |
| 1519 | free_netdev(dev); |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | kfree(dev_nr); |
| 1524 | proto_unregister(&nr_proto); |
| 1525 | } |
| 1526 | module_exit(nr_exit); |