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