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; |
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 | |
| 427 | nr->t1 = sysctl_netrom_transport_timeout; |
| 428 | nr->t2 = sysctl_netrom_transport_acknowledge_delay; |
| 429 | nr->n2 = sysctl_netrom_transport_maximum_tries; |
| 430 | nr->t4 = sysctl_netrom_transport_busy_delay; |
| 431 | nr->idle = sysctl_netrom_transport_no_activity_timeout; |
| 432 | nr->window = sysctl_netrom_transport_requested_window_size; |
| 433 | |
| 434 | nr->bpqext = 1; |
| 435 | nr->state = NR_STATE_0; |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static struct sock *nr_make_new(struct sock *osk) |
| 441 | { |
| 442 | struct sock *sk; |
| 443 | struct nr_sock *nr, *onr; |
| 444 | |
| 445 | if (osk->sk_type != SOCK_SEQPACKET) |
| 446 | return NULL; |
| 447 | |
| 448 | if ((sk = sk_alloc(PF_NETROM, GFP_ATOMIC, osk->sk_prot, 1)) == NULL) |
| 449 | return NULL; |
| 450 | |
| 451 | nr = nr_sk(sk); |
| 452 | |
| 453 | sock_init_data(NULL, sk); |
| 454 | |
| 455 | sk->sk_type = osk->sk_type; |
| 456 | sk->sk_socket = osk->sk_socket; |
| 457 | sk->sk_priority = osk->sk_priority; |
| 458 | sk->sk_protocol = osk->sk_protocol; |
| 459 | sk->sk_rcvbuf = osk->sk_rcvbuf; |
| 460 | sk->sk_sndbuf = osk->sk_sndbuf; |
| 461 | sk->sk_state = TCP_ESTABLISHED; |
| 462 | sk->sk_sleep = osk->sk_sleep; |
Ralf Baechle | 53b924b | 2005-08-23 10:11:30 -0700 | [diff] [blame] | 463 | sock_copy_flags(sk, osk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
| 465 | skb_queue_head_init(&nr->ack_queue); |
| 466 | skb_queue_head_init(&nr->reseq_queue); |
| 467 | skb_queue_head_init(&nr->frag_queue); |
| 468 | |
| 469 | nr_init_timers(sk); |
| 470 | |
| 471 | onr = nr_sk(osk); |
| 472 | |
| 473 | nr->t1 = onr->t1; |
| 474 | nr->t2 = onr->t2; |
| 475 | nr->n2 = onr->n2; |
| 476 | nr->t4 = onr->t4; |
| 477 | nr->idle = onr->idle; |
| 478 | nr->window = onr->window; |
| 479 | |
| 480 | nr->device = onr->device; |
| 481 | nr->bpqext = onr->bpqext; |
| 482 | |
| 483 | return sk; |
| 484 | } |
| 485 | |
| 486 | static int nr_release(struct socket *sock) |
| 487 | { |
| 488 | struct sock *sk = sock->sk; |
| 489 | struct nr_sock *nr; |
| 490 | |
| 491 | if (sk == NULL) return 0; |
| 492 | |
| 493 | sock_hold(sk); |
| 494 | lock_sock(sk); |
| 495 | nr = nr_sk(sk); |
| 496 | |
| 497 | switch (nr->state) { |
| 498 | case NR_STATE_0: |
| 499 | case NR_STATE_1: |
| 500 | case NR_STATE_2: |
| 501 | nr_disconnect(sk, 0); |
| 502 | nr_destroy_socket(sk); |
| 503 | break; |
| 504 | |
| 505 | case NR_STATE_3: |
| 506 | nr_clear_queues(sk); |
| 507 | nr->n2count = 0; |
| 508 | nr_write_internal(sk, NR_DISCREQ); |
| 509 | nr_start_t1timer(sk); |
| 510 | nr_stop_t2timer(sk); |
| 511 | nr_stop_t4timer(sk); |
| 512 | nr_stop_idletimer(sk); |
| 513 | nr->state = NR_STATE_2; |
| 514 | sk->sk_state = TCP_CLOSE; |
| 515 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 516 | sk->sk_state_change(sk); |
| 517 | sock_orphan(sk); |
| 518 | sock_set_flag(sk, SOCK_DESTROY); |
| 519 | sk->sk_socket = NULL; |
| 520 | break; |
| 521 | |
| 522 | default: |
| 523 | sk->sk_socket = NULL; |
| 524 | break; |
| 525 | } |
| 526 | |
| 527 | sock->sk = NULL; |
| 528 | release_sock(sk); |
| 529 | sock_put(sk); |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 535 | { |
| 536 | struct sock *sk = sock->sk; |
| 537 | struct nr_sock *nr = nr_sk(sk); |
| 538 | struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr; |
| 539 | struct net_device *dev; |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 540 | ax25_uid_assoc *user; |
| 541 | ax25_address *source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
| 543 | lock_sock(sk); |
| 544 | if (!sock_flag(sk, SOCK_ZAPPED)) { |
| 545 | release_sock(sk); |
| 546 | return -EINVAL; |
| 547 | } |
| 548 | if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) { |
| 549 | release_sock(sk); |
| 550 | return -EINVAL; |
| 551 | } |
| 552 | if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) { |
| 553 | release_sock(sk); |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | if (addr->fsa_ax25.sax25_family != AF_NETROM) { |
| 557 | release_sock(sk); |
| 558 | return -EINVAL; |
| 559 | } |
| 560 | if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) { |
| 561 | SOCK_DEBUG(sk, "NET/ROM: bind failed: invalid node callsign\n"); |
| 562 | release_sock(sk); |
| 563 | return -EADDRNOTAVAIL; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Only the super user can set an arbitrary user callsign. |
| 568 | */ |
| 569 | if (addr->fsa_ax25.sax25_ndigis == 1) { |
| 570 | if (!capable(CAP_NET_BIND_SERVICE)) { |
| 571 | dev_put(dev); |
| 572 | release_sock(sk); |
| 573 | return -EACCES; |
| 574 | } |
| 575 | nr->user_addr = addr->fsa_digipeater[0]; |
| 576 | nr->source_addr = addr->fsa_ax25.sax25_call; |
| 577 | } else { |
| 578 | source = &addr->fsa_ax25.sax25_call; |
| 579 | |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 580 | user = ax25_findbyuid(current->euid); |
| 581 | if (user) { |
| 582 | nr->user_addr = user->call; |
| 583 | ax25_uid_put(user); |
| 584 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) { |
| 586 | release_sock(sk); |
| 587 | dev_put(dev); |
| 588 | return -EPERM; |
| 589 | } |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 590 | nr->user_addr = *source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | nr->source_addr = *source; |
| 594 | } |
| 595 | |
| 596 | nr->device = dev; |
| 597 | nr_insert_socket(sk); |
| 598 | |
| 599 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 600 | dev_put(dev); |
| 601 | release_sock(sk); |
| 602 | SOCK_DEBUG(sk, "NET/ROM: socket is bound\n"); |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static int nr_connect(struct socket *sock, struct sockaddr *uaddr, |
| 607 | int addr_len, int flags) |
| 608 | { |
| 609 | struct sock *sk = sock->sk; |
| 610 | struct nr_sock *nr = nr_sk(sk); |
| 611 | struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr; |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 612 | ax25_address *source = NULL; |
| 613 | ax25_uid_assoc *user; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | struct net_device *dev; |
| 615 | |
| 616 | lock_sock(sk); |
| 617 | if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) { |
| 618 | sock->state = SS_CONNECTED; |
| 619 | release_sock(sk); |
| 620 | return 0; /* Connect completed during a ERESTARTSYS event */ |
| 621 | } |
| 622 | |
| 623 | if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) { |
| 624 | sock->state = SS_UNCONNECTED; |
| 625 | release_sock(sk); |
| 626 | return -ECONNREFUSED; |
| 627 | } |
| 628 | |
| 629 | if (sk->sk_state == TCP_ESTABLISHED) { |
| 630 | release_sock(sk); |
| 631 | return -EISCONN; /* No reconnect on a seqpacket socket */ |
| 632 | } |
| 633 | |
| 634 | sk->sk_state = TCP_CLOSE; |
| 635 | sock->state = SS_UNCONNECTED; |
| 636 | |
| 637 | if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) { |
| 638 | release_sock(sk); |
| 639 | return -EINVAL; |
| 640 | } |
| 641 | if (addr->sax25_family != AF_NETROM) { |
| 642 | release_sock(sk); |
| 643 | return -EINVAL; |
| 644 | } |
| 645 | if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */ |
| 646 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 647 | |
| 648 | if ((dev = nr_dev_first()) == NULL) { |
| 649 | release_sock(sk); |
| 650 | return -ENETUNREACH; |
| 651 | } |
| 652 | source = (ax25_address *)dev->dev_addr; |
| 653 | |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 654 | user = ax25_findbyuid(current->euid); |
| 655 | if (user) { |
| 656 | nr->user_addr = user->call; |
| 657 | ax25_uid_put(user); |
| 658 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) { |
| 660 | dev_put(dev); |
| 661 | release_sock(sk); |
| 662 | return -EPERM; |
| 663 | } |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 664 | nr->user_addr = *source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | nr->source_addr = *source; |
| 668 | nr->device = dev; |
| 669 | |
| 670 | dev_put(dev); |
| 671 | nr_insert_socket(sk); /* Finish the bind */ |
| 672 | } |
| 673 | |
| 674 | nr->dest_addr = addr->sax25_call; |
| 675 | |
| 676 | release_sock(sk); |
| 677 | circuit = nr_find_next_circuit(); |
| 678 | lock_sock(sk); |
| 679 | |
| 680 | nr->my_index = circuit / 256; |
| 681 | nr->my_id = circuit % 256; |
| 682 | |
| 683 | circuit++; |
| 684 | |
| 685 | /* Move to connecting socket, start sending Connect Requests */ |
| 686 | sock->state = SS_CONNECTING; |
| 687 | sk->sk_state = TCP_SYN_SENT; |
| 688 | |
| 689 | nr_establish_data_link(sk); |
| 690 | |
| 691 | nr->state = NR_STATE_1; |
| 692 | |
| 693 | nr_start_heartbeat(sk); |
| 694 | |
| 695 | /* Now the loop */ |
| 696 | if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) { |
| 697 | release_sock(sk); |
| 698 | return -EINPROGRESS; |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * A Connect Ack with Choke or timeout or failed routing will go to |
| 703 | * closed. |
| 704 | */ |
| 705 | if (sk->sk_state == TCP_SYN_SENT) { |
| 706 | struct task_struct *tsk = current; |
| 707 | DECLARE_WAITQUEUE(wait, tsk); |
| 708 | |
| 709 | add_wait_queue(sk->sk_sleep, &wait); |
| 710 | for (;;) { |
| 711 | set_current_state(TASK_INTERRUPTIBLE); |
| 712 | if (sk->sk_state != TCP_SYN_SENT) |
| 713 | break; |
| 714 | release_sock(sk); |
| 715 | if (!signal_pending(tsk)) { |
| 716 | schedule(); |
| 717 | lock_sock(sk); |
| 718 | continue; |
| 719 | } |
| 720 | current->state = TASK_RUNNING; |
| 721 | remove_wait_queue(sk->sk_sleep, &wait); |
| 722 | return -ERESTARTSYS; |
| 723 | } |
| 724 | current->state = TASK_RUNNING; |
| 725 | remove_wait_queue(sk->sk_sleep, &wait); |
| 726 | } |
| 727 | |
| 728 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 729 | sock->state = SS_UNCONNECTED; |
| 730 | release_sock(sk); |
| 731 | return sock_error(sk); /* Always set at this point */ |
| 732 | } |
| 733 | |
| 734 | sock->state = SS_CONNECTED; |
| 735 | release_sock(sk); |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | static int nr_accept(struct socket *sock, struct socket *newsock, int flags) |
| 741 | { |
| 742 | struct task_struct *tsk = current; |
| 743 | DECLARE_WAITQUEUE(wait, tsk); |
| 744 | struct sk_buff *skb; |
| 745 | struct sock *newsk; |
| 746 | struct sock *sk; |
| 747 | int err = 0; |
| 748 | |
| 749 | if ((sk = sock->sk) == NULL) |
| 750 | return -EINVAL; |
| 751 | |
| 752 | lock_sock(sk); |
| 753 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 754 | err = -EOPNOTSUPP; |
| 755 | goto out; |
| 756 | } |
| 757 | |
| 758 | if (sk->sk_state != TCP_LISTEN) { |
| 759 | err = -EINVAL; |
| 760 | goto out; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * The write queue this time is holding sockets ready to use |
| 765 | * hooked into the SABM we saved |
| 766 | */ |
| 767 | add_wait_queue(sk->sk_sleep, &wait); |
| 768 | for (;;) { |
| 769 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 770 | if (skb) |
| 771 | break; |
| 772 | |
| 773 | current->state = TASK_INTERRUPTIBLE; |
| 774 | release_sock(sk); |
| 775 | if (flags & O_NONBLOCK) { |
| 776 | current->state = TASK_RUNNING; |
| 777 | remove_wait_queue(sk->sk_sleep, &wait); |
| 778 | return -EWOULDBLOCK; |
| 779 | } |
| 780 | if (!signal_pending(tsk)) { |
| 781 | schedule(); |
| 782 | lock_sock(sk); |
| 783 | continue; |
| 784 | } |
| 785 | current->state = TASK_RUNNING; |
| 786 | remove_wait_queue(sk->sk_sleep, &wait); |
| 787 | return -ERESTARTSYS; |
| 788 | } |
| 789 | current->state = TASK_RUNNING; |
| 790 | remove_wait_queue(sk->sk_sleep, &wait); |
| 791 | |
| 792 | newsk = skb->sk; |
| 793 | newsk->sk_socket = newsock; |
| 794 | newsk->sk_sleep = &newsock->wait; |
| 795 | |
| 796 | /* Now attach up the new socket */ |
| 797 | kfree_skb(skb); |
| 798 | sk->sk_ack_backlog--; |
| 799 | newsock->sk = newsk; |
| 800 | |
| 801 | out: |
| 802 | release_sock(sk); |
| 803 | return err; |
| 804 | } |
| 805 | |
| 806 | static int nr_getname(struct socket *sock, struct sockaddr *uaddr, |
| 807 | int *uaddr_len, int peer) |
| 808 | { |
| 809 | struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr; |
| 810 | struct sock *sk = sock->sk; |
| 811 | struct nr_sock *nr = nr_sk(sk); |
| 812 | |
| 813 | lock_sock(sk); |
| 814 | if (peer != 0) { |
| 815 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 816 | release_sock(sk); |
| 817 | return -ENOTCONN; |
| 818 | } |
| 819 | sax->fsa_ax25.sax25_family = AF_NETROM; |
| 820 | sax->fsa_ax25.sax25_ndigis = 1; |
| 821 | sax->fsa_ax25.sax25_call = nr->user_addr; |
| 822 | sax->fsa_digipeater[0] = nr->dest_addr; |
| 823 | *uaddr_len = sizeof(struct full_sockaddr_ax25); |
| 824 | } else { |
| 825 | sax->fsa_ax25.sax25_family = AF_NETROM; |
| 826 | sax->fsa_ax25.sax25_ndigis = 0; |
| 827 | sax->fsa_ax25.sax25_call = nr->source_addr; |
| 828 | *uaddr_len = sizeof(struct sockaddr_ax25); |
| 829 | } |
| 830 | release_sock(sk); |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | int nr_rx_frame(struct sk_buff *skb, struct net_device *dev) |
| 836 | { |
| 837 | struct sock *sk; |
| 838 | struct sock *make; |
| 839 | struct nr_sock *nr_make; |
| 840 | ax25_address *src, *dest, *user; |
| 841 | unsigned short circuit_index, circuit_id; |
| 842 | unsigned short peer_circuit_index, peer_circuit_id; |
| 843 | unsigned short frametype, flags, window, timeout; |
| 844 | int ret; |
| 845 | |
| 846 | skb->sk = NULL; /* Initially we don't know who it's for */ |
| 847 | |
| 848 | /* |
| 849 | * skb->data points to the netrom frame start |
| 850 | */ |
| 851 | |
| 852 | src = (ax25_address *)(skb->data + 0); |
| 853 | dest = (ax25_address *)(skb->data + 7); |
| 854 | |
| 855 | circuit_index = skb->data[15]; |
| 856 | circuit_id = skb->data[16]; |
| 857 | peer_circuit_index = skb->data[17]; |
| 858 | peer_circuit_id = skb->data[18]; |
| 859 | frametype = skb->data[19] & 0x0F; |
| 860 | flags = skb->data[19] & 0xF0; |
| 861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | /* |
| 863 | * Check for an incoming IP over NET/ROM frame. |
| 864 | */ |
Ralf Baechle | 98a82fe | 2005-08-24 11:35:51 -0700 | [diff] [blame] | 865 | if (frametype == NR_PROTOEXT && |
| 866 | circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN); |
| 868 | skb->h.raw = skb->data; |
| 869 | |
| 870 | return nr_rx_ip(skb, dev); |
| 871 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
| 873 | /* |
| 874 | * Find an existing socket connection, based on circuit ID, if it's |
| 875 | * a Connect Request base it on their circuit ID. |
| 876 | * |
| 877 | * Circuit ID 0/0 is not valid but it could still be a "reset" for a |
| 878 | * circuit that no longer exists at the other end ... |
| 879 | */ |
| 880 | |
| 881 | sk = NULL; |
| 882 | |
| 883 | if (circuit_index == 0 && circuit_id == 0) { |
| 884 | if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG) |
| 885 | sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src); |
| 886 | } else { |
| 887 | if (frametype == NR_CONNREQ) |
| 888 | sk = nr_find_peer(circuit_index, circuit_id, src); |
| 889 | else |
| 890 | sk = nr_find_socket(circuit_index, circuit_id); |
| 891 | } |
| 892 | |
| 893 | if (sk != NULL) { |
| 894 | skb->h.raw = skb->data; |
| 895 | |
| 896 | if (frametype == NR_CONNACK && skb->len == 22) |
| 897 | nr_sk(sk)->bpqext = 1; |
| 898 | else |
| 899 | nr_sk(sk)->bpqext = 0; |
| 900 | |
| 901 | ret = nr_process_rx_frame(sk, skb); |
| 902 | bh_unlock_sock(sk); |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Now it should be a CONNREQ. |
| 908 | */ |
| 909 | if (frametype != NR_CONNREQ) { |
| 910 | /* |
| 911 | * 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] | 912 | * NET/ROM doesn't have one. We've tried to extend the protocol |
| 913 | * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that |
| 914 | * apparently kills BPQ boxes... :-( |
| 915 | * So now we try to follow the established behaviour of |
| 916 | * G8PZT's Xrouter which is sending packets with command type 7 |
| 917 | * as an extension of the protocol. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | */ |
Ralf Baechle | e21ce8c | 2005-09-12 14:27:37 -0700 | [diff] [blame] | 919 | if (sysctl_netrom_reset_circuit && |
| 920 | (frametype != NR_RESET || flags != 0)) |
| 921 | nr_transmit_reset(skb, 1); |
| 922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | sk = nr_find_listener(dest); |
| 927 | |
| 928 | user = (ax25_address *)(skb->data + 21); |
| 929 | |
| 930 | if (sk == NULL || sk_acceptq_is_full(sk) || |
| 931 | (make = nr_make_new(sk)) == NULL) { |
| 932 | nr_transmit_refusal(skb, 0); |
| 933 | if (sk) |
| 934 | bh_unlock_sock(sk); |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | window = skb->data[20]; |
| 939 | |
| 940 | skb->sk = make; |
| 941 | make->sk_state = TCP_ESTABLISHED; |
| 942 | |
| 943 | /* Fill in his circuit details */ |
| 944 | nr_make = nr_sk(make); |
| 945 | nr_make->source_addr = *dest; |
| 946 | nr_make->dest_addr = *src; |
| 947 | nr_make->user_addr = *user; |
| 948 | |
| 949 | nr_make->your_index = circuit_index; |
| 950 | nr_make->your_id = circuit_id; |
| 951 | |
| 952 | bh_unlock_sock(sk); |
| 953 | circuit = nr_find_next_circuit(); |
| 954 | bh_lock_sock(sk); |
| 955 | |
| 956 | nr_make->my_index = circuit / 256; |
| 957 | nr_make->my_id = circuit % 256; |
| 958 | |
| 959 | circuit++; |
| 960 | |
| 961 | /* Window negotiation */ |
| 962 | if (window < nr_make->window) |
| 963 | nr_make->window = window; |
| 964 | |
| 965 | /* L4 timeout negotiation */ |
| 966 | if (skb->len == 37) { |
| 967 | timeout = skb->data[36] * 256 + skb->data[35]; |
| 968 | if (timeout * HZ < nr_make->t1) |
| 969 | nr_make->t1 = timeout * HZ; |
| 970 | nr_make->bpqext = 1; |
| 971 | } else { |
| 972 | nr_make->bpqext = 0; |
| 973 | } |
| 974 | |
| 975 | nr_write_internal(make, NR_CONNACK); |
| 976 | |
| 977 | nr_make->condition = 0x00; |
| 978 | nr_make->vs = 0; |
| 979 | nr_make->va = 0; |
| 980 | nr_make->vr = 0; |
| 981 | nr_make->vl = 0; |
| 982 | nr_make->state = NR_STATE_3; |
| 983 | sk->sk_ack_backlog++; |
| 984 | |
| 985 | nr_insert_socket(make); |
| 986 | |
| 987 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 988 | |
| 989 | nr_start_heartbeat(make); |
| 990 | nr_start_idletimer(make); |
| 991 | |
| 992 | if (!sock_flag(sk, SOCK_DEAD)) |
| 993 | sk->sk_data_ready(sk, skb->len); |
| 994 | |
| 995 | bh_unlock_sock(sk); |
| 996 | return 1; |
| 997 | } |
| 998 | |
| 999 | static int nr_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 1000 | struct msghdr *msg, size_t len) |
| 1001 | { |
| 1002 | struct sock *sk = sock->sk; |
| 1003 | struct nr_sock *nr = nr_sk(sk); |
| 1004 | struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name; |
| 1005 | int err; |
| 1006 | struct sockaddr_ax25 sax; |
| 1007 | struct sk_buff *skb; |
| 1008 | unsigned char *asmptr; |
| 1009 | int size; |
| 1010 | |
| 1011 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT)) |
| 1012 | return -EINVAL; |
| 1013 | |
| 1014 | lock_sock(sk); |
| 1015 | if (sock_flag(sk, SOCK_ZAPPED)) { |
| 1016 | err = -EADDRNOTAVAIL; |
| 1017 | goto out; |
| 1018 | } |
| 1019 | |
| 1020 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 1021 | send_sig(SIGPIPE, current, 0); |
| 1022 | err = -EPIPE; |
| 1023 | goto out; |
| 1024 | } |
| 1025 | |
| 1026 | if (nr->device == NULL) { |
| 1027 | err = -ENETUNREACH; |
| 1028 | goto out; |
| 1029 | } |
| 1030 | |
| 1031 | if (usax) { |
| 1032 | if (msg->msg_namelen < sizeof(sax)) { |
| 1033 | err = -EINVAL; |
| 1034 | goto out; |
| 1035 | } |
| 1036 | sax = *usax; |
| 1037 | if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) { |
| 1038 | err = -EISCONN; |
| 1039 | goto out; |
| 1040 | } |
| 1041 | if (sax.sax25_family != AF_NETROM) { |
| 1042 | err = -EINVAL; |
| 1043 | goto out; |
| 1044 | } |
| 1045 | } else { |
| 1046 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1047 | err = -ENOTCONN; |
| 1048 | goto out; |
| 1049 | } |
| 1050 | sax.sax25_family = AF_NETROM; |
| 1051 | sax.sax25_call = nr->dest_addr; |
| 1052 | } |
| 1053 | |
| 1054 | SOCK_DEBUG(sk, "NET/ROM: sendto: Addresses built.\n"); |
| 1055 | |
| 1056 | /* Build a packet */ |
| 1057 | SOCK_DEBUG(sk, "NET/ROM: sendto: building packet.\n"); |
| 1058 | size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN; |
| 1059 | |
| 1060 | if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL) |
| 1061 | goto out; |
| 1062 | |
| 1063 | skb_reserve(skb, size - len); |
| 1064 | |
| 1065 | /* |
| 1066 | * Push down the NET/ROM header |
| 1067 | */ |
| 1068 | |
| 1069 | asmptr = skb_push(skb, NR_TRANSPORT_LEN); |
| 1070 | SOCK_DEBUG(sk, "Building NET/ROM Header.\n"); |
| 1071 | |
| 1072 | /* Build a NET/ROM Transport header */ |
| 1073 | |
| 1074 | *asmptr++ = nr->your_index; |
| 1075 | *asmptr++ = nr->your_id; |
| 1076 | *asmptr++ = 0; /* To be filled in later */ |
| 1077 | *asmptr++ = 0; /* Ditto */ |
| 1078 | *asmptr++ = NR_INFO; |
| 1079 | SOCK_DEBUG(sk, "Built header.\n"); |
| 1080 | |
| 1081 | /* |
| 1082 | * Put the data on the end |
| 1083 | */ |
| 1084 | |
| 1085 | skb->h.raw = skb_put(skb, len); |
| 1086 | |
| 1087 | asmptr = skb->h.raw; |
| 1088 | SOCK_DEBUG(sk, "NET/ROM: Appending user data\n"); |
| 1089 | |
| 1090 | /* User data follows immediately after the NET/ROM transport header */ |
| 1091 | if (memcpy_fromiovec(asmptr, msg->msg_iov, len)) { |
| 1092 | kfree_skb(skb); |
| 1093 | err = -EFAULT; |
| 1094 | goto out; |
| 1095 | } |
| 1096 | |
| 1097 | SOCK_DEBUG(sk, "NET/ROM: Transmitting buffer\n"); |
| 1098 | |
| 1099 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1100 | kfree_skb(skb); |
| 1101 | err = -ENOTCONN; |
| 1102 | goto out; |
| 1103 | } |
| 1104 | |
| 1105 | nr_output(sk, skb); /* Shove it onto the queue */ |
| 1106 | |
| 1107 | err = len; |
| 1108 | out: |
| 1109 | release_sock(sk); |
| 1110 | return err; |
| 1111 | } |
| 1112 | |
| 1113 | static int nr_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 1114 | struct msghdr *msg, size_t size, int flags) |
| 1115 | { |
| 1116 | struct sock *sk = sock->sk; |
| 1117 | struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name; |
| 1118 | size_t copied; |
| 1119 | struct sk_buff *skb; |
| 1120 | int er; |
| 1121 | |
| 1122 | /* |
| 1123 | * This works for seqpacket too. The receiver has ordered the queue for |
| 1124 | * us! We do one quick check first though |
| 1125 | */ |
| 1126 | |
| 1127 | lock_sock(sk); |
| 1128 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1129 | release_sock(sk); |
| 1130 | return -ENOTCONN; |
| 1131 | } |
| 1132 | |
| 1133 | /* Now we can treat all alike */ |
| 1134 | if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) { |
| 1135 | release_sock(sk); |
| 1136 | return er; |
| 1137 | } |
| 1138 | |
| 1139 | skb->h.raw = skb->data; |
| 1140 | copied = skb->len; |
| 1141 | |
| 1142 | if (copied > size) { |
| 1143 | copied = size; |
| 1144 | msg->msg_flags |= MSG_TRUNC; |
| 1145 | } |
| 1146 | |
| 1147 | skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 1148 | |
| 1149 | if (sax != NULL) { |
| 1150 | sax->sax25_family = AF_NETROM; |
| 1151 | memcpy(sax->sax25_call.ax25_call, skb->data + 7, AX25_ADDR_LEN); |
| 1152 | } |
| 1153 | |
| 1154 | msg->msg_namelen = sizeof(*sax); |
| 1155 | |
| 1156 | skb_free_datagram(sk, skb); |
| 1157 | |
| 1158 | release_sock(sk); |
| 1159 | return copied; |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 1164 | { |
| 1165 | struct sock *sk = sock->sk; |
| 1166 | void __user *argp = (void __user *)arg; |
| 1167 | int ret; |
| 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | switch (cmd) { |
| 1170 | case TIOCOUTQ: { |
| 1171 | long amount; |
Christoph Hellwig | 5ff7630 | 2006-01-03 14:14:46 -0800 | [diff] [blame^] | 1172 | |
| 1173 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc); |
| 1175 | if (amount < 0) |
| 1176 | amount = 0; |
| 1177 | release_sock(sk); |
| 1178 | return put_user(amount, (int __user *)argp); |
| 1179 | } |
| 1180 | |
| 1181 | case TIOCINQ: { |
| 1182 | struct sk_buff *skb; |
| 1183 | long amount = 0L; |
Christoph Hellwig | 5ff7630 | 2006-01-03 14:14:46 -0800 | [diff] [blame^] | 1184 | |
| 1185 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | /* These two are safe on a single CPU system as only user tasks fiddle here */ |
| 1187 | if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) |
| 1188 | amount = skb->len; |
| 1189 | release_sock(sk); |
| 1190 | return put_user(amount, (int __user *)argp); |
| 1191 | } |
| 1192 | |
| 1193 | case SIOCGSTAMP: |
Christoph Hellwig | 5ff7630 | 2006-01-03 14:14:46 -0800 | [diff] [blame^] | 1194 | lock_sock(sk); |
Ralf Baechle | 9b37ee7 | 2005-09-12 14:23:52 -0700 | [diff] [blame] | 1195 | ret = sock_get_timestamp(sk, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | release_sock(sk); |
| 1197 | return ret; |
| 1198 | |
| 1199 | case SIOCGIFADDR: |
| 1200 | case SIOCSIFADDR: |
| 1201 | case SIOCGIFDSTADDR: |
| 1202 | case SIOCSIFDSTADDR: |
| 1203 | case SIOCGIFBRDADDR: |
| 1204 | case SIOCSIFBRDADDR: |
| 1205 | case SIOCGIFNETMASK: |
| 1206 | case SIOCSIFNETMASK: |
| 1207 | case SIOCGIFMETRIC: |
| 1208 | case SIOCSIFMETRIC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | return -EINVAL; |
| 1210 | |
| 1211 | case SIOCADDRT: |
| 1212 | case SIOCDELRT: |
| 1213 | case SIOCNRDECOBS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | if (!capable(CAP_NET_ADMIN)) return -EPERM; |
| 1215 | return nr_rt_ioctl(cmd, argp); |
| 1216 | |
| 1217 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | return dev_ioctl(cmd, argp); |
| 1219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | #ifdef CONFIG_PROC_FS |
| 1225 | |
| 1226 | static void *nr_info_start(struct seq_file *seq, loff_t *pos) |
| 1227 | { |
| 1228 | struct sock *s; |
| 1229 | struct hlist_node *node; |
| 1230 | int i = 1; |
| 1231 | |
| 1232 | spin_lock_bh(&nr_list_lock); |
| 1233 | if (*pos == 0) |
| 1234 | return SEQ_START_TOKEN; |
| 1235 | |
| 1236 | sk_for_each(s, node, &nr_list) { |
| 1237 | if (i == *pos) |
| 1238 | return s; |
| 1239 | ++i; |
| 1240 | } |
| 1241 | return NULL; |
| 1242 | } |
| 1243 | |
| 1244 | static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1245 | { |
| 1246 | ++*pos; |
| 1247 | |
| 1248 | return (v == SEQ_START_TOKEN) ? sk_head(&nr_list) |
| 1249 | : sk_next((struct sock *)v); |
| 1250 | } |
| 1251 | |
| 1252 | static void nr_info_stop(struct seq_file *seq, void *v) |
| 1253 | { |
| 1254 | spin_unlock_bh(&nr_list_lock); |
| 1255 | } |
| 1256 | |
| 1257 | static int nr_info_show(struct seq_file *seq, void *v) |
| 1258 | { |
| 1259 | struct sock *s = v; |
| 1260 | struct net_device *dev; |
| 1261 | struct nr_sock *nr; |
| 1262 | const char *devname; |
Ralf Baechle | f75268c | 2005-09-06 15:49:39 -0700 | [diff] [blame] | 1263 | char buf[11]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | |
| 1265 | if (v == SEQ_START_TOKEN) |
| 1266 | seq_puts(seq, |
| 1267 | "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"); |
| 1268 | |
| 1269 | else { |
| 1270 | |
| 1271 | bh_lock_sock(s); |
| 1272 | nr = nr_sk(s); |
| 1273 | |
| 1274 | if ((dev = nr->device) == NULL) |
| 1275 | devname = "???"; |
| 1276 | else |
| 1277 | devname = dev->name; |
| 1278 | |
Ralf Baechle | f75268c | 2005-09-06 15:49:39 -0700 | [diff] [blame] | 1279 | seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr)); |
| 1280 | seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | seq_printf(seq, |
| 1282 | "%-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] | 1283 | ax2asc(buf, &nr->source_addr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | devname, |
| 1285 | nr->my_index, |
| 1286 | nr->my_id, |
| 1287 | nr->your_index, |
| 1288 | nr->your_id, |
| 1289 | nr->state, |
| 1290 | nr->vs, |
| 1291 | nr->vr, |
| 1292 | nr->va, |
| 1293 | ax25_display_timer(&nr->t1timer) / HZ, |
| 1294 | nr->t1 / HZ, |
| 1295 | ax25_display_timer(&nr->t2timer) / HZ, |
| 1296 | nr->t2 / HZ, |
| 1297 | ax25_display_timer(&nr->t4timer) / HZ, |
| 1298 | nr->t4 / HZ, |
| 1299 | ax25_display_timer(&nr->idletimer) / (60 * HZ), |
| 1300 | nr->idle / (60 * HZ), |
| 1301 | nr->n2count, |
| 1302 | nr->n2, |
| 1303 | nr->window, |
| 1304 | atomic_read(&s->sk_wmem_alloc), |
| 1305 | atomic_read(&s->sk_rmem_alloc), |
| 1306 | s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L); |
| 1307 | |
| 1308 | bh_unlock_sock(s); |
| 1309 | } |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | static struct seq_operations nr_info_seqops = { |
| 1314 | .start = nr_info_start, |
| 1315 | .next = nr_info_next, |
| 1316 | .stop = nr_info_stop, |
| 1317 | .show = nr_info_show, |
| 1318 | }; |
| 1319 | |
| 1320 | static int nr_info_open(struct inode *inode, struct file *file) |
| 1321 | { |
| 1322 | return seq_open(file, &nr_info_seqops); |
| 1323 | } |
| 1324 | |
| 1325 | static struct file_operations nr_info_fops = { |
| 1326 | .owner = THIS_MODULE, |
| 1327 | .open = nr_info_open, |
| 1328 | .read = seq_read, |
| 1329 | .llseek = seq_lseek, |
| 1330 | .release = seq_release, |
| 1331 | }; |
| 1332 | #endif /* CONFIG_PROC_FS */ |
| 1333 | |
| 1334 | static struct net_proto_family nr_family_ops = { |
| 1335 | .family = PF_NETROM, |
| 1336 | .create = nr_create, |
| 1337 | .owner = THIS_MODULE, |
| 1338 | }; |
| 1339 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1340 | static const struct proto_ops nr_proto_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | .family = PF_NETROM, |
| 1342 | .owner = THIS_MODULE, |
| 1343 | .release = nr_release, |
| 1344 | .bind = nr_bind, |
| 1345 | .connect = nr_connect, |
| 1346 | .socketpair = sock_no_socketpair, |
| 1347 | .accept = nr_accept, |
| 1348 | .getname = nr_getname, |
| 1349 | .poll = datagram_poll, |
| 1350 | .ioctl = nr_ioctl, |
| 1351 | .listen = nr_listen, |
| 1352 | .shutdown = sock_no_shutdown, |
| 1353 | .setsockopt = nr_setsockopt, |
| 1354 | .getsockopt = nr_getsockopt, |
| 1355 | .sendmsg = nr_sendmsg, |
| 1356 | .recvmsg = nr_recvmsg, |
| 1357 | .mmap = sock_no_mmap, |
| 1358 | .sendpage = sock_no_sendpage, |
| 1359 | }; |
| 1360 | |
| 1361 | static struct notifier_block nr_dev_notifier = { |
| 1362 | .notifier_call = nr_device_event, |
| 1363 | }; |
| 1364 | |
| 1365 | static struct net_device **dev_nr; |
| 1366 | |
| 1367 | static char banner[] __initdata = KERN_INFO "G4KLX NET/ROM for Linux. Version 0.7 for AX25.037 Linux 2.4\n"; |
| 1368 | |
| 1369 | static int __init nr_proto_init(void) |
| 1370 | { |
| 1371 | int i; |
| 1372 | int rc = proto_register(&nr_proto, 0); |
| 1373 | |
| 1374 | if (rc != 0) |
| 1375 | goto out; |
| 1376 | |
| 1377 | if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) { |
| 1378 | printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n"); |
| 1379 | return -1; |
| 1380 | } |
| 1381 | |
| 1382 | dev_nr = kmalloc(nr_ndevs * sizeof(struct net_device *), GFP_KERNEL); |
| 1383 | if (dev_nr == NULL) { |
| 1384 | printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device array\n"); |
| 1385 | return -1; |
| 1386 | } |
| 1387 | |
| 1388 | memset(dev_nr, 0x00, nr_ndevs * sizeof(struct net_device *)); |
| 1389 | |
| 1390 | for (i = 0; i < nr_ndevs; i++) { |
| 1391 | char name[IFNAMSIZ]; |
| 1392 | struct net_device *dev; |
| 1393 | |
| 1394 | sprintf(name, "nr%d", i); |
Ralf Baechle | b88a762 | 2005-09-12 14:28:03 -0700 | [diff] [blame] | 1395 | dev = alloc_netdev(sizeof(struct nr_private), name, nr_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | if (!dev) { |
| 1397 | printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n"); |
| 1398 | goto fail; |
| 1399 | } |
| 1400 | |
| 1401 | dev->base_addr = i; |
| 1402 | if (register_netdev(dev)) { |
| 1403 | printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n"); |
| 1404 | free_netdev(dev); |
| 1405 | goto fail; |
| 1406 | } |
| 1407 | dev_nr[i] = dev; |
| 1408 | } |
| 1409 | |
| 1410 | if (sock_register(&nr_family_ops)) { |
| 1411 | printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n"); |
| 1412 | goto fail; |
| 1413 | } |
| 1414 | |
| 1415 | register_netdevice_notifier(&nr_dev_notifier); |
| 1416 | printk(banner); |
| 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); |