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