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