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