Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * X.25 Packet Layer release 002 |
| 3 | * |
| 4 | * This is ALPHA test software. This code may break your machine, |
| 5 | * randomly fail to work with new releases, misbehave and/or generally |
| 6 | * screw up. It might even work. |
| 7 | * |
| 8 | * This code REQUIRES 2.1.15 or higher |
| 9 | * |
| 10 | * This module: |
| 11 | * This module is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * as published by the Free Software Foundation; either version |
| 14 | * 2 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * History |
| 17 | * X.25 001 Jonathan Naylor Started coding. |
| 18 | * X.25 002 Jonathan Naylor Centralised disconnect handling. |
| 19 | * New timer architecture. |
| 20 | * 2000-03-11 Henner Eisen MSG_EOR handling more POSIX compliant. |
| 21 | * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of |
| 22 | * facilities negotiation and increased |
| 23 | * the throughput upper limit. |
| 24 | * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups |
| 25 | * 2000-09-04 Henner Eisen Set sock->state in x25_accept(). |
| 26 | * Fixed x25_output() related skb leakage. |
| 27 | * 2000-10-02 Henner Eisen Made x25_kick() single threaded per socket. |
| 28 | * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation. |
| 29 | * 2000-11-14 Henner Eisen Closing datalink from NETDEV_GOING_DOWN |
| 30 | * 2002-10-06 Arnaldo C. Melo Get rid of cli/sti, move proc stuff to |
| 31 | * x25_proc.c, using seq_file |
| 32 | */ |
| 33 | |
| 34 | #include <linux/config.h> |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/errno.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/sched.h> |
| 39 | #include <linux/timer.h> |
| 40 | #include <linux/string.h> |
| 41 | #include <linux/net.h> |
| 42 | #include <linux/netdevice.h> |
| 43 | #include <linux/if_arp.h> |
| 44 | #include <linux/skbuff.h> |
| 45 | #include <net/sock.h> |
| 46 | #include <net/tcp.h> |
| 47 | #include <asm/uaccess.h> |
| 48 | #include <linux/fcntl.h> |
| 49 | #include <linux/termios.h> /* For TIOCINQ/OUTQ */ |
| 50 | #include <linux/notifier.h> |
| 51 | #include <linux/init.h> |
| 52 | #include <net/x25.h> |
| 53 | |
| 54 | int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20; |
| 55 | int sysctl_x25_call_request_timeout = X25_DEFAULT_T21; |
| 56 | int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22; |
| 57 | int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23; |
| 58 | int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2; |
| 59 | |
| 60 | HLIST_HEAD(x25_list); |
| 61 | DEFINE_RWLOCK(x25_list_lock); |
| 62 | |
| 63 | static struct proto_ops x25_proto_ops; |
| 64 | |
| 65 | static struct x25_address null_x25_address = {" "}; |
| 66 | |
| 67 | int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr, |
| 68 | struct x25_address *calling_addr) |
| 69 | { |
| 70 | int called_len, calling_len; |
| 71 | char *called, *calling; |
| 72 | int i; |
| 73 | |
| 74 | called_len = (*p >> 0) & 0x0F; |
| 75 | calling_len = (*p >> 4) & 0x0F; |
| 76 | |
| 77 | called = called_addr->x25_addr; |
| 78 | calling = calling_addr->x25_addr; |
| 79 | p++; |
| 80 | |
| 81 | for (i = 0; i < (called_len + calling_len); i++) { |
| 82 | if (i < called_len) { |
| 83 | if (i % 2 != 0) { |
| 84 | *called++ = ((*p >> 0) & 0x0F) + '0'; |
| 85 | p++; |
| 86 | } else { |
| 87 | *called++ = ((*p >> 4) & 0x0F) + '0'; |
| 88 | } |
| 89 | } else { |
| 90 | if (i % 2 != 0) { |
| 91 | *calling++ = ((*p >> 0) & 0x0F) + '0'; |
| 92 | p++; |
| 93 | } else { |
| 94 | *calling++ = ((*p >> 4) & 0x0F) + '0'; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | *called = *calling = '\0'; |
| 100 | |
| 101 | return 1 + (called_len + calling_len + 1) / 2; |
| 102 | } |
| 103 | |
| 104 | int x25_addr_aton(unsigned char *p, struct x25_address *called_addr, |
| 105 | struct x25_address *calling_addr) |
| 106 | { |
| 107 | unsigned int called_len, calling_len; |
| 108 | char *called, *calling; |
| 109 | int i; |
| 110 | |
| 111 | called = called_addr->x25_addr; |
| 112 | calling = calling_addr->x25_addr; |
| 113 | |
| 114 | called_len = strlen(called); |
| 115 | calling_len = strlen(calling); |
| 116 | |
| 117 | *p++ = (calling_len << 4) | (called_len << 0); |
| 118 | |
| 119 | for (i = 0; i < (called_len + calling_len); i++) { |
| 120 | if (i < called_len) { |
| 121 | if (i % 2 != 0) { |
| 122 | *p |= (*called++ - '0') << 0; |
| 123 | p++; |
| 124 | } else { |
| 125 | *p = 0x00; |
| 126 | *p |= (*called++ - '0') << 4; |
| 127 | } |
| 128 | } else { |
| 129 | if (i % 2 != 0) { |
| 130 | *p |= (*calling++ - '0') << 0; |
| 131 | p++; |
| 132 | } else { |
| 133 | *p = 0x00; |
| 134 | *p |= (*calling++ - '0') << 4; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return 1 + (called_len + calling_len + 1) / 2; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Socket removal during an interrupt is now safe. |
| 144 | */ |
| 145 | static void x25_remove_socket(struct sock *sk) |
| 146 | { |
| 147 | write_lock_bh(&x25_list_lock); |
| 148 | sk_del_node_init(sk); |
| 149 | write_unlock_bh(&x25_list_lock); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Kill all bound sockets on a dropped device. |
| 154 | */ |
| 155 | static void x25_kill_by_device(struct net_device *dev) |
| 156 | { |
| 157 | struct sock *s; |
| 158 | struct hlist_node *node; |
| 159 | |
| 160 | write_lock_bh(&x25_list_lock); |
| 161 | |
| 162 | sk_for_each(s, node, &x25_list) |
| 163 | if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev) |
| 164 | x25_disconnect(s, ENETUNREACH, 0, 0); |
| 165 | |
| 166 | write_unlock_bh(&x25_list_lock); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Handle device status changes. |
| 171 | */ |
| 172 | static int x25_device_event(struct notifier_block *this, unsigned long event, |
| 173 | void *ptr) |
| 174 | { |
| 175 | struct net_device *dev = ptr; |
| 176 | struct x25_neigh *nb; |
| 177 | |
| 178 | if (dev->type == ARPHRD_X25 |
| 179 | #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE) |
| 180 | || dev->type == ARPHRD_ETHER |
| 181 | #endif |
| 182 | ) { |
| 183 | switch (event) { |
| 184 | case NETDEV_UP: |
| 185 | x25_link_device_up(dev); |
| 186 | break; |
| 187 | case NETDEV_GOING_DOWN: |
| 188 | nb = x25_get_neigh(dev); |
| 189 | if (nb) { |
| 190 | x25_terminate_link(nb); |
| 191 | x25_neigh_put(nb); |
| 192 | } |
| 193 | break; |
| 194 | case NETDEV_DOWN: |
| 195 | x25_kill_by_device(dev); |
| 196 | x25_route_device_down(dev); |
| 197 | x25_link_device_down(dev); |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return NOTIFY_DONE; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Add a socket to the bound sockets list. |
| 207 | */ |
| 208 | static void x25_insert_socket(struct sock *sk) |
| 209 | { |
| 210 | write_lock_bh(&x25_list_lock); |
| 211 | sk_add_node(sk, &x25_list); |
| 212 | write_unlock_bh(&x25_list_lock); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Find a socket that wants to accept the Call Request we just |
| 217 | * received. Check the full list for an address/cud match. |
| 218 | * If no cuds match return the next_best thing, an address match. |
| 219 | * Note: if a listening socket has cud set it must only get calls |
| 220 | * with matching cud. |
| 221 | */ |
| 222 | static struct sock *x25_find_listener(struct x25_address *addr, struct x25_calluserdata *calluserdata) |
| 223 | { |
| 224 | struct sock *s; |
| 225 | struct sock *next_best; |
| 226 | struct hlist_node *node; |
| 227 | |
| 228 | read_lock_bh(&x25_list_lock); |
| 229 | next_best = NULL; |
| 230 | |
| 231 | sk_for_each(s, node, &x25_list) |
| 232 | if ((!strcmp(addr->x25_addr, |
| 233 | x25_sk(s)->source_addr.x25_addr) || |
| 234 | !strcmp(addr->x25_addr, |
| 235 | null_x25_address.x25_addr)) && |
| 236 | s->sk_state == TCP_LISTEN) { |
| 237 | |
| 238 | /* |
| 239 | * Found a listening socket, now check the incoming |
| 240 | * call user data vs this sockets call user data |
| 241 | */ |
| 242 | if (x25_check_calluserdata(&x25_sk(s)->calluserdata, calluserdata)) { |
| 243 | sock_hold(s); |
| 244 | goto found; |
| 245 | } |
| 246 | if (x25_sk(s)->calluserdata.cudlength == 0) { |
| 247 | next_best = s; |
| 248 | } |
| 249 | } |
| 250 | if (next_best) { |
| 251 | s = next_best; |
| 252 | sock_hold(s); |
| 253 | goto found; |
| 254 | } |
| 255 | s = NULL; |
| 256 | found: |
| 257 | read_unlock_bh(&x25_list_lock); |
| 258 | return s; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Find a connected X.25 socket given my LCI and neighbour. |
| 263 | */ |
| 264 | static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb) |
| 265 | { |
| 266 | struct sock *s; |
| 267 | struct hlist_node *node; |
| 268 | |
| 269 | sk_for_each(s, node, &x25_list) |
| 270 | if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) { |
| 271 | sock_hold(s); |
| 272 | goto found; |
| 273 | } |
| 274 | s = NULL; |
| 275 | found: |
| 276 | return s; |
| 277 | } |
| 278 | |
| 279 | struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb) |
| 280 | { |
| 281 | struct sock *s; |
| 282 | |
| 283 | read_lock_bh(&x25_list_lock); |
| 284 | s = __x25_find_socket(lci, nb); |
| 285 | read_unlock_bh(&x25_list_lock); |
| 286 | return s; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Find a unique LCI for a given device. |
| 291 | */ |
| 292 | static unsigned int x25_new_lci(struct x25_neigh *nb) |
| 293 | { |
| 294 | unsigned int lci = 1; |
| 295 | struct sock *sk; |
| 296 | |
| 297 | read_lock_bh(&x25_list_lock); |
| 298 | |
| 299 | while ((sk = __x25_find_socket(lci, nb)) != NULL) { |
| 300 | sock_put(sk); |
| 301 | if (++lci == 4096) { |
| 302 | lci = 0; |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | read_unlock_bh(&x25_list_lock); |
| 308 | return lci; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Deferred destroy. |
| 313 | */ |
| 314 | void x25_destroy_socket(struct sock *); |
| 315 | |
| 316 | /* |
| 317 | * handler for deferred kills. |
| 318 | */ |
| 319 | static void x25_destroy_timer(unsigned long data) |
| 320 | { |
| 321 | x25_destroy_socket((struct sock *)data); |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * This is called from user mode and the timers. Thus it protects itself |
| 326 | * against interrupt users but doesn't worry about being called during |
| 327 | * work. Once it is removed from the queue no interrupt or bottom half |
| 328 | * will touch it and we are (fairly 8-) ) safe. |
| 329 | * Not static as it's used by the timer |
| 330 | */ |
| 331 | void x25_destroy_socket(struct sock *sk) |
| 332 | { |
| 333 | struct sk_buff *skb; |
| 334 | |
| 335 | sock_hold(sk); |
| 336 | lock_sock(sk); |
| 337 | x25_stop_heartbeat(sk); |
| 338 | x25_stop_timer(sk); |
| 339 | |
| 340 | x25_remove_socket(sk); |
| 341 | x25_clear_queues(sk); /* Flush the queues */ |
| 342 | |
| 343 | while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { |
| 344 | if (skb->sk != sk) { /* A pending connection */ |
| 345 | /* |
| 346 | * Queue the unaccepted socket for death |
| 347 | */ |
| 348 | sock_set_flag(skb->sk, SOCK_DEAD); |
| 349 | x25_start_heartbeat(skb->sk); |
| 350 | x25_sk(skb->sk)->state = X25_STATE_0; |
| 351 | } |
| 352 | |
| 353 | kfree_skb(skb); |
| 354 | } |
| 355 | |
| 356 | if (atomic_read(&sk->sk_wmem_alloc) || |
| 357 | atomic_read(&sk->sk_rmem_alloc)) { |
| 358 | /* Defer: outstanding buffers */ |
| 359 | sk->sk_timer.expires = jiffies + 10 * HZ; |
| 360 | sk->sk_timer.function = x25_destroy_timer; |
| 361 | sk->sk_timer.data = (unsigned long)sk; |
| 362 | add_timer(&sk->sk_timer); |
| 363 | } else { |
| 364 | /* drop last reference so sock_put will free */ |
| 365 | __sock_put(sk); |
| 366 | } |
| 367 | |
| 368 | release_sock(sk); |
| 369 | sock_put(sk); |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Handling for system calls applied via the various interfaces to a |
| 374 | * X.25 socket object. |
| 375 | */ |
| 376 | |
| 377 | static int x25_setsockopt(struct socket *sock, int level, int optname, |
| 378 | char __user *optval, int optlen) |
| 379 | { |
| 380 | int opt; |
| 381 | struct sock *sk = sock->sk; |
| 382 | int rc = -ENOPROTOOPT; |
| 383 | |
| 384 | if (level != SOL_X25 || optname != X25_QBITINCL) |
| 385 | goto out; |
| 386 | |
| 387 | rc = -EINVAL; |
| 388 | if (optlen < sizeof(int)) |
| 389 | goto out; |
| 390 | |
| 391 | rc = -EFAULT; |
| 392 | if (get_user(opt, (int __user *)optval)) |
| 393 | goto out; |
| 394 | |
| 395 | x25_sk(sk)->qbitincl = !!opt; |
| 396 | rc = 0; |
| 397 | out: |
| 398 | return rc; |
| 399 | } |
| 400 | |
| 401 | static int x25_getsockopt(struct socket *sock, int level, int optname, |
| 402 | char __user *optval, int __user *optlen) |
| 403 | { |
| 404 | struct sock *sk = sock->sk; |
| 405 | int val, len, rc = -ENOPROTOOPT; |
| 406 | |
| 407 | if (level != SOL_X25 || optname != X25_QBITINCL) |
| 408 | goto out; |
| 409 | |
| 410 | rc = -EFAULT; |
| 411 | if (get_user(len, optlen)) |
| 412 | goto out; |
| 413 | |
| 414 | len = min_t(unsigned int, len, sizeof(int)); |
| 415 | |
| 416 | rc = -EINVAL; |
| 417 | if (len < 0) |
| 418 | goto out; |
| 419 | |
| 420 | rc = -EFAULT; |
| 421 | if (put_user(len, optlen)) |
| 422 | goto out; |
| 423 | |
| 424 | val = x25_sk(sk)->qbitincl; |
| 425 | rc = copy_to_user(optval, &val, len) ? -EFAULT : 0; |
| 426 | out: |
| 427 | return rc; |
| 428 | } |
| 429 | |
| 430 | static int x25_listen(struct socket *sock, int backlog) |
| 431 | { |
| 432 | struct sock *sk = sock->sk; |
| 433 | int rc = -EOPNOTSUPP; |
| 434 | |
| 435 | if (sk->sk_state != TCP_LISTEN) { |
| 436 | memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN); |
| 437 | sk->sk_max_ack_backlog = backlog; |
| 438 | sk->sk_state = TCP_LISTEN; |
| 439 | rc = 0; |
| 440 | } |
| 441 | |
| 442 | return rc; |
| 443 | } |
| 444 | |
| 445 | static struct proto x25_proto = { |
| 446 | .name = "X25", |
| 447 | .owner = THIS_MODULE, |
| 448 | .obj_size = sizeof(struct x25_sock), |
| 449 | }; |
| 450 | |
| 451 | static struct sock *x25_alloc_socket(void) |
| 452 | { |
| 453 | struct x25_sock *x25; |
| 454 | struct sock *sk = sk_alloc(AF_X25, GFP_ATOMIC, &x25_proto, 1); |
| 455 | |
| 456 | if (!sk) |
| 457 | goto out; |
| 458 | |
| 459 | sock_init_data(NULL, sk); |
| 460 | |
| 461 | x25 = x25_sk(sk); |
| 462 | skb_queue_head_init(&x25->ack_queue); |
| 463 | skb_queue_head_init(&x25->fragment_queue); |
| 464 | skb_queue_head_init(&x25->interrupt_in_queue); |
| 465 | skb_queue_head_init(&x25->interrupt_out_queue); |
| 466 | out: |
| 467 | return sk; |
| 468 | } |
| 469 | |
| 470 | void x25_init_timers(struct sock *sk); |
| 471 | |
| 472 | static int x25_create(struct socket *sock, int protocol) |
| 473 | { |
| 474 | struct sock *sk; |
| 475 | struct x25_sock *x25; |
| 476 | int rc = -ESOCKTNOSUPPORT; |
| 477 | |
| 478 | if (sock->type != SOCK_SEQPACKET || protocol) |
| 479 | goto out; |
| 480 | |
| 481 | rc = -ENOMEM; |
| 482 | if ((sk = x25_alloc_socket()) == NULL) |
| 483 | goto out; |
| 484 | |
| 485 | x25 = x25_sk(sk); |
| 486 | |
| 487 | sock_init_data(sock, sk); |
| 488 | |
| 489 | x25_init_timers(sk); |
| 490 | |
| 491 | sock->ops = &x25_proto_ops; |
| 492 | sk->sk_protocol = protocol; |
| 493 | sk->sk_backlog_rcv = x25_backlog_rcv; |
| 494 | |
| 495 | x25->t21 = sysctl_x25_call_request_timeout; |
| 496 | x25->t22 = sysctl_x25_reset_request_timeout; |
| 497 | x25->t23 = sysctl_x25_clear_request_timeout; |
| 498 | x25->t2 = sysctl_x25_ack_holdback_timeout; |
| 499 | x25->state = X25_STATE_0; |
| 500 | |
| 501 | x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE; |
| 502 | x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE; |
| 503 | x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE; |
| 504 | x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE; |
| 505 | x25->facilities.throughput = X25_DEFAULT_THROUGHPUT; |
| 506 | x25->facilities.reverse = X25_DEFAULT_REVERSE; |
| 507 | rc = 0; |
| 508 | out: |
| 509 | return rc; |
| 510 | } |
| 511 | |
| 512 | static struct sock *x25_make_new(struct sock *osk) |
| 513 | { |
| 514 | struct sock *sk = NULL; |
| 515 | struct x25_sock *x25, *ox25; |
| 516 | |
| 517 | if (osk->sk_type != SOCK_SEQPACKET) |
| 518 | goto out; |
| 519 | |
| 520 | if ((sk = x25_alloc_socket()) == NULL) |
| 521 | goto out; |
| 522 | |
| 523 | x25 = x25_sk(sk); |
| 524 | |
| 525 | sk->sk_type = osk->sk_type; |
| 526 | sk->sk_socket = osk->sk_socket; |
| 527 | sk->sk_priority = osk->sk_priority; |
| 528 | sk->sk_protocol = osk->sk_protocol; |
| 529 | sk->sk_rcvbuf = osk->sk_rcvbuf; |
| 530 | sk->sk_sndbuf = osk->sk_sndbuf; |
| 531 | sk->sk_state = TCP_ESTABLISHED; |
| 532 | sk->sk_sleep = osk->sk_sleep; |
| 533 | sk->sk_backlog_rcv = osk->sk_backlog_rcv; |
| 534 | |
| 535 | if (sock_flag(osk, SOCK_ZAPPED)) |
| 536 | sock_set_flag(sk, SOCK_ZAPPED); |
| 537 | |
| 538 | if (sock_flag(osk, SOCK_DBG)) |
| 539 | sock_set_flag(sk, SOCK_DBG); |
| 540 | |
| 541 | ox25 = x25_sk(osk); |
| 542 | x25->t21 = ox25->t21; |
| 543 | x25->t22 = ox25->t22; |
| 544 | x25->t23 = ox25->t23; |
| 545 | x25->t2 = ox25->t2; |
| 546 | x25->facilities = ox25->facilities; |
| 547 | x25->qbitincl = ox25->qbitincl; |
| 548 | |
| 549 | x25_init_timers(sk); |
| 550 | out: |
| 551 | return sk; |
| 552 | } |
| 553 | |
| 554 | static int x25_release(struct socket *sock) |
| 555 | { |
| 556 | struct sock *sk = sock->sk; |
| 557 | struct x25_sock *x25; |
| 558 | |
| 559 | if (!sk) |
| 560 | goto out; |
| 561 | |
| 562 | x25 = x25_sk(sk); |
| 563 | |
| 564 | switch (x25->state) { |
| 565 | |
| 566 | case X25_STATE_0: |
| 567 | case X25_STATE_2: |
| 568 | x25_disconnect(sk, 0, 0, 0); |
| 569 | x25_destroy_socket(sk); |
| 570 | goto out; |
| 571 | |
| 572 | case X25_STATE_1: |
| 573 | case X25_STATE_3: |
| 574 | case X25_STATE_4: |
| 575 | x25_clear_queues(sk); |
| 576 | x25_write_internal(sk, X25_CLEAR_REQUEST); |
| 577 | x25_start_t23timer(sk); |
| 578 | x25->state = X25_STATE_2; |
| 579 | sk->sk_state = TCP_CLOSE; |
| 580 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 581 | sk->sk_state_change(sk); |
| 582 | sock_set_flag(sk, SOCK_DEAD); |
| 583 | sock_set_flag(sk, SOCK_DESTROY); |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | sock->sk = NULL; |
| 588 | sk->sk_socket = NULL; /* Not used, but we should do this */ |
| 589 | out: |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 594 | { |
| 595 | struct sock *sk = sock->sk; |
| 596 | struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr; |
| 597 | |
| 598 | if (!sock_flag(sk, SOCK_ZAPPED) || |
| 599 | addr_len != sizeof(struct sockaddr_x25) || |
| 600 | addr->sx25_family != AF_X25) |
| 601 | return -EINVAL; |
| 602 | |
| 603 | x25_sk(sk)->source_addr = addr->sx25_addr; |
| 604 | x25_insert_socket(sk); |
| 605 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 606 | SOCK_DEBUG(sk, "x25_bind: socket is bound\n"); |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static int x25_wait_for_connection_establishment(struct sock *sk) |
| 612 | { |
| 613 | DECLARE_WAITQUEUE(wait, current); |
| 614 | int rc; |
| 615 | |
| 616 | add_wait_queue_exclusive(sk->sk_sleep, &wait); |
| 617 | for (;;) { |
| 618 | __set_current_state(TASK_INTERRUPTIBLE); |
| 619 | rc = -ERESTARTSYS; |
| 620 | if (signal_pending(current)) |
| 621 | break; |
| 622 | rc = sock_error(sk); |
| 623 | if (rc) { |
| 624 | sk->sk_socket->state = SS_UNCONNECTED; |
| 625 | break; |
| 626 | } |
| 627 | rc = 0; |
| 628 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 629 | release_sock(sk); |
| 630 | schedule(); |
| 631 | lock_sock(sk); |
| 632 | } else |
| 633 | break; |
| 634 | } |
| 635 | __set_current_state(TASK_RUNNING); |
| 636 | remove_wait_queue(sk->sk_sleep, &wait); |
| 637 | return rc; |
| 638 | } |
| 639 | |
| 640 | static int x25_connect(struct socket *sock, struct sockaddr *uaddr, |
| 641 | int addr_len, int flags) |
| 642 | { |
| 643 | struct sock *sk = sock->sk; |
| 644 | struct x25_sock *x25 = x25_sk(sk); |
| 645 | struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr; |
| 646 | struct x25_route *rt; |
| 647 | int rc = 0; |
| 648 | |
| 649 | lock_sock(sk); |
| 650 | if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) { |
| 651 | sock->state = SS_CONNECTED; |
| 652 | goto out; /* Connect completed during a ERESTARTSYS event */ |
| 653 | } |
| 654 | |
| 655 | rc = -ECONNREFUSED; |
| 656 | if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) { |
| 657 | sock->state = SS_UNCONNECTED; |
| 658 | goto out; |
| 659 | } |
| 660 | |
| 661 | rc = -EISCONN; /* No reconnect on a seqpacket socket */ |
| 662 | if (sk->sk_state == TCP_ESTABLISHED) |
| 663 | goto out; |
| 664 | |
| 665 | sk->sk_state = TCP_CLOSE; |
| 666 | sock->state = SS_UNCONNECTED; |
| 667 | |
| 668 | rc = -EINVAL; |
| 669 | if (addr_len != sizeof(struct sockaddr_x25) || |
| 670 | addr->sx25_family != AF_X25) |
| 671 | goto out; |
| 672 | |
| 673 | rc = -ENETUNREACH; |
| 674 | rt = x25_get_route(&addr->sx25_addr); |
| 675 | if (!rt) |
| 676 | goto out; |
| 677 | |
| 678 | x25->neighbour = x25_get_neigh(rt->dev); |
| 679 | if (!x25->neighbour) |
| 680 | goto out_put_route; |
| 681 | |
| 682 | x25_limit_facilities(&x25->facilities, x25->neighbour); |
| 683 | |
| 684 | x25->lci = x25_new_lci(x25->neighbour); |
| 685 | if (!x25->lci) |
| 686 | goto out_put_neigh; |
| 687 | |
| 688 | rc = -EINVAL; |
| 689 | if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */ |
| 690 | goto out_put_neigh; |
| 691 | |
| 692 | if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr)) |
| 693 | memset(&x25->source_addr, '\0', X25_ADDR_LEN); |
| 694 | |
| 695 | x25->dest_addr = addr->sx25_addr; |
| 696 | |
| 697 | /* Move to connecting socket, start sending Connect Requests */ |
| 698 | sock->state = SS_CONNECTING; |
| 699 | sk->sk_state = TCP_SYN_SENT; |
| 700 | |
| 701 | x25->state = X25_STATE_1; |
| 702 | |
| 703 | x25_write_internal(sk, X25_CALL_REQUEST); |
| 704 | |
| 705 | x25_start_heartbeat(sk); |
| 706 | x25_start_t21timer(sk); |
| 707 | |
| 708 | /* Now the loop */ |
| 709 | rc = -EINPROGRESS; |
| 710 | if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) |
| 711 | goto out_put_neigh; |
| 712 | |
| 713 | rc = x25_wait_for_connection_establishment(sk); |
| 714 | if (rc) |
| 715 | goto out_put_neigh; |
| 716 | |
| 717 | sock->state = SS_CONNECTED; |
| 718 | rc = 0; |
| 719 | out_put_neigh: |
| 720 | if (rc) |
| 721 | x25_neigh_put(x25->neighbour); |
| 722 | out_put_route: |
| 723 | x25_route_put(rt); |
| 724 | out: |
| 725 | release_sock(sk); |
| 726 | return rc; |
| 727 | } |
| 728 | |
| 729 | static int x25_wait_for_data(struct sock *sk, int timeout) |
| 730 | { |
| 731 | DECLARE_WAITQUEUE(wait, current); |
| 732 | int rc = 0; |
| 733 | |
| 734 | add_wait_queue_exclusive(sk->sk_sleep, &wait); |
| 735 | for (;;) { |
| 736 | __set_current_state(TASK_INTERRUPTIBLE); |
| 737 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 738 | break; |
| 739 | rc = -ERESTARTSYS; |
| 740 | if (signal_pending(current)) |
| 741 | break; |
| 742 | rc = -EAGAIN; |
| 743 | if (!timeout) |
| 744 | break; |
| 745 | rc = 0; |
| 746 | if (skb_queue_empty(&sk->sk_receive_queue)) { |
| 747 | release_sock(sk); |
| 748 | timeout = schedule_timeout(timeout); |
| 749 | lock_sock(sk); |
| 750 | } else |
| 751 | break; |
| 752 | } |
| 753 | __set_current_state(TASK_RUNNING); |
| 754 | remove_wait_queue(sk->sk_sleep, &wait); |
| 755 | return rc; |
| 756 | } |
| 757 | |
| 758 | static int x25_accept(struct socket *sock, struct socket *newsock, int flags) |
| 759 | { |
| 760 | struct sock *sk = sock->sk; |
| 761 | struct sock *newsk; |
| 762 | struct sk_buff *skb; |
| 763 | int rc = -EINVAL; |
| 764 | |
| 765 | if (!sk || sk->sk_state != TCP_LISTEN) |
| 766 | goto out; |
| 767 | |
| 768 | rc = -EOPNOTSUPP; |
| 769 | if (sk->sk_type != SOCK_SEQPACKET) |
| 770 | goto out; |
| 771 | |
| 772 | lock_sock(sk); |
| 773 | rc = x25_wait_for_data(sk, sk->sk_rcvtimeo); |
| 774 | if (rc) |
| 775 | goto out2; |
| 776 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 777 | rc = -EINVAL; |
| 778 | if (!skb->sk) |
| 779 | goto out2; |
| 780 | newsk = skb->sk; |
| 781 | newsk->sk_socket = newsock; |
| 782 | newsk->sk_sleep = &newsock->wait; |
| 783 | |
| 784 | /* Now attach up the new socket */ |
| 785 | skb->sk = NULL; |
| 786 | kfree_skb(skb); |
| 787 | sk->sk_ack_backlog--; |
| 788 | newsock->sk = newsk; |
| 789 | newsock->state = SS_CONNECTED; |
| 790 | rc = 0; |
| 791 | out2: |
| 792 | release_sock(sk); |
| 793 | out: |
| 794 | return rc; |
| 795 | } |
| 796 | |
| 797 | static int x25_getname(struct socket *sock, struct sockaddr *uaddr, |
| 798 | int *uaddr_len, int peer) |
| 799 | { |
| 800 | struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr; |
| 801 | struct sock *sk = sock->sk; |
| 802 | struct x25_sock *x25 = x25_sk(sk); |
| 803 | |
| 804 | if (peer) { |
| 805 | if (sk->sk_state != TCP_ESTABLISHED) |
| 806 | return -ENOTCONN; |
| 807 | sx25->sx25_addr = x25->dest_addr; |
| 808 | } else |
| 809 | sx25->sx25_addr = x25->source_addr; |
| 810 | |
| 811 | sx25->sx25_family = AF_X25; |
| 812 | *uaddr_len = sizeof(*sx25); |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb, |
| 818 | unsigned int lci) |
| 819 | { |
| 820 | struct sock *sk; |
| 821 | struct sock *make; |
| 822 | struct x25_sock *makex25; |
| 823 | struct x25_address source_addr, dest_addr; |
| 824 | struct x25_facilities facilities; |
| 825 | struct x25_calluserdata calluserdata; |
| 826 | int len, rc; |
| 827 | |
| 828 | /* |
| 829 | * Remove the LCI and frame type. |
| 830 | */ |
| 831 | skb_pull(skb, X25_STD_MIN_LEN); |
| 832 | |
| 833 | /* |
| 834 | * Extract the X.25 addresses and convert them to ASCII strings, |
| 835 | * and remove them. |
| 836 | */ |
| 837 | skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr)); |
| 838 | |
| 839 | /* |
| 840 | * Get the length of the facilities, skip past them for the moment |
| 841 | * get the call user data because this is needed to determine |
| 842 | * the correct listener |
| 843 | */ |
| 844 | len = skb->data[0] + 1; |
| 845 | skb_pull(skb,len); |
| 846 | |
| 847 | /* |
| 848 | * Incoming Call User Data. |
| 849 | */ |
| 850 | if (skb->len >= 0) { |
| 851 | memcpy(calluserdata.cuddata, skb->data, skb->len); |
| 852 | calluserdata.cudlength = skb->len; |
| 853 | } |
| 854 | |
| 855 | skb_push(skb,len); |
| 856 | |
| 857 | /* |
| 858 | * Find a listener for the particular address/cud pair. |
| 859 | */ |
| 860 | sk = x25_find_listener(&source_addr,&calluserdata); |
| 861 | |
| 862 | /* |
| 863 | * We can't accept the Call Request. |
| 864 | */ |
| 865 | if (sk == NULL || sk_acceptq_is_full(sk)) |
| 866 | goto out_clear_request; |
| 867 | |
| 868 | /* |
| 869 | * Try to reach a compromise on the requested facilities. |
| 870 | */ |
| 871 | if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1) |
| 872 | goto out_sock_put; |
| 873 | |
| 874 | /* |
| 875 | * current neighbour/link might impose additional limits |
| 876 | * on certain facilties |
| 877 | */ |
| 878 | |
| 879 | x25_limit_facilities(&facilities, nb); |
| 880 | |
| 881 | /* |
| 882 | * Try to create a new socket. |
| 883 | */ |
| 884 | make = x25_make_new(sk); |
| 885 | if (!make) |
| 886 | goto out_sock_put; |
| 887 | |
| 888 | /* |
| 889 | * Remove the facilities |
| 890 | */ |
| 891 | skb_pull(skb, len); |
| 892 | |
| 893 | skb->sk = make; |
| 894 | make->sk_state = TCP_ESTABLISHED; |
| 895 | |
| 896 | makex25 = x25_sk(make); |
| 897 | makex25->lci = lci; |
| 898 | makex25->dest_addr = dest_addr; |
| 899 | makex25->source_addr = source_addr; |
| 900 | makex25->neighbour = nb; |
| 901 | makex25->facilities = facilities; |
| 902 | makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask; |
| 903 | makex25->calluserdata = calluserdata; |
| 904 | |
| 905 | x25_write_internal(make, X25_CALL_ACCEPTED); |
| 906 | |
| 907 | makex25->state = X25_STATE_3; |
| 908 | |
| 909 | sk->sk_ack_backlog++; |
| 910 | |
| 911 | x25_insert_socket(make); |
| 912 | |
| 913 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 914 | |
| 915 | x25_start_heartbeat(make); |
| 916 | |
| 917 | if (!sock_flag(sk, SOCK_DEAD)) |
| 918 | sk->sk_data_ready(sk, skb->len); |
| 919 | rc = 1; |
| 920 | sock_put(sk); |
| 921 | out: |
| 922 | return rc; |
| 923 | out_sock_put: |
| 924 | sock_put(sk); |
| 925 | out_clear_request: |
| 926 | rc = 0; |
| 927 | x25_transmit_clear_request(nb, lci, 0x01); |
| 928 | goto out; |
| 929 | } |
| 930 | |
| 931 | static int x25_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 932 | struct msghdr *msg, size_t len) |
| 933 | { |
| 934 | struct sock *sk = sock->sk; |
| 935 | struct x25_sock *x25 = x25_sk(sk); |
| 936 | struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name; |
| 937 | struct sockaddr_x25 sx25; |
| 938 | struct sk_buff *skb; |
| 939 | unsigned char *asmptr; |
| 940 | int noblock = msg->msg_flags & MSG_DONTWAIT; |
| 941 | size_t size; |
| 942 | int qbit = 0, rc = -EINVAL; |
| 943 | |
| 944 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT)) |
| 945 | goto out; |
| 946 | |
| 947 | /* we currently don't support segmented records at the user interface */ |
| 948 | if (!(msg->msg_flags & (MSG_EOR|MSG_OOB))) |
| 949 | goto out; |
| 950 | |
| 951 | rc = -EADDRNOTAVAIL; |
| 952 | if (sock_flag(sk, SOCK_ZAPPED)) |
| 953 | goto out; |
| 954 | |
| 955 | rc = -EPIPE; |
| 956 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 957 | send_sig(SIGPIPE, current, 0); |
| 958 | goto out; |
| 959 | } |
| 960 | |
| 961 | rc = -ENETUNREACH; |
| 962 | if (!x25->neighbour) |
| 963 | goto out; |
| 964 | |
| 965 | if (usx25) { |
| 966 | rc = -EINVAL; |
| 967 | if (msg->msg_namelen < sizeof(sx25)) |
| 968 | goto out; |
| 969 | memcpy(&sx25, usx25, sizeof(sx25)); |
| 970 | rc = -EISCONN; |
| 971 | if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr)) |
| 972 | goto out; |
| 973 | rc = -EINVAL; |
| 974 | if (sx25.sx25_family != AF_X25) |
| 975 | goto out; |
| 976 | } else { |
| 977 | /* |
| 978 | * FIXME 1003.1g - if the socket is like this because |
| 979 | * it has become closed (not started closed) we ought |
| 980 | * to SIGPIPE, EPIPE; |
| 981 | */ |
| 982 | rc = -ENOTCONN; |
| 983 | if (sk->sk_state != TCP_ESTABLISHED) |
| 984 | goto out; |
| 985 | |
| 986 | sx25.sx25_family = AF_X25; |
| 987 | sx25.sx25_addr = x25->dest_addr; |
| 988 | } |
| 989 | |
| 990 | SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n"); |
| 991 | |
| 992 | /* Build a packet */ |
| 993 | SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n"); |
| 994 | |
| 995 | if ((msg->msg_flags & MSG_OOB) && len > 32) |
| 996 | len = 32; |
| 997 | |
| 998 | size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN; |
| 999 | |
| 1000 | skb = sock_alloc_send_skb(sk, size, noblock, &rc); |
| 1001 | if (!skb) |
| 1002 | goto out; |
| 1003 | X25_SKB_CB(skb)->flags = msg->msg_flags; |
| 1004 | |
| 1005 | skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN); |
| 1006 | |
| 1007 | /* |
| 1008 | * Put the data on the end |
| 1009 | */ |
| 1010 | SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n"); |
| 1011 | |
| 1012 | asmptr = skb->h.raw = skb_put(skb, len); |
| 1013 | |
| 1014 | rc = memcpy_fromiovec(asmptr, msg->msg_iov, len); |
| 1015 | if (rc) |
| 1016 | goto out_kfree_skb; |
| 1017 | |
| 1018 | /* |
| 1019 | * If the Q BIT Include socket option is in force, the first |
| 1020 | * byte of the user data is the logical value of the Q Bit. |
| 1021 | */ |
| 1022 | if (x25->qbitincl) { |
| 1023 | qbit = skb->data[0]; |
| 1024 | skb_pull(skb, 1); |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * Push down the X.25 header |
| 1029 | */ |
| 1030 | SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n"); |
| 1031 | |
| 1032 | if (msg->msg_flags & MSG_OOB) { |
| 1033 | if (x25->neighbour->extended) { |
| 1034 | asmptr = skb_push(skb, X25_STD_MIN_LEN); |
| 1035 | *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ; |
| 1036 | *asmptr++ = (x25->lci >> 0) & 0xFF; |
| 1037 | *asmptr++ = X25_INTERRUPT; |
| 1038 | } else { |
| 1039 | asmptr = skb_push(skb, X25_STD_MIN_LEN); |
| 1040 | *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ; |
| 1041 | *asmptr++ = (x25->lci >> 0) & 0xFF; |
| 1042 | *asmptr++ = X25_INTERRUPT; |
| 1043 | } |
| 1044 | } else { |
| 1045 | if (x25->neighbour->extended) { |
| 1046 | /* Build an Extended X.25 header */ |
| 1047 | asmptr = skb_push(skb, X25_EXT_MIN_LEN); |
| 1048 | *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ; |
| 1049 | *asmptr++ = (x25->lci >> 0) & 0xFF; |
| 1050 | *asmptr++ = X25_DATA; |
| 1051 | *asmptr++ = X25_DATA; |
| 1052 | } else { |
| 1053 | /* Build an Standard X.25 header */ |
| 1054 | asmptr = skb_push(skb, X25_STD_MIN_LEN); |
| 1055 | *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ; |
| 1056 | *asmptr++ = (x25->lci >> 0) & 0xFF; |
| 1057 | *asmptr++ = X25_DATA; |
| 1058 | } |
| 1059 | |
| 1060 | if (qbit) |
| 1061 | skb->data[0] |= X25_Q_BIT; |
| 1062 | } |
| 1063 | |
| 1064 | SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n"); |
| 1065 | SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n"); |
| 1066 | |
| 1067 | rc = -ENOTCONN; |
| 1068 | if (sk->sk_state != TCP_ESTABLISHED) |
| 1069 | goto out_kfree_skb; |
| 1070 | |
| 1071 | if (msg->msg_flags & MSG_OOB) |
| 1072 | skb_queue_tail(&x25->interrupt_out_queue, skb); |
| 1073 | else { |
| 1074 | len = x25_output(sk, skb); |
| 1075 | if (len < 0) |
| 1076 | kfree_skb(skb); |
| 1077 | else if (x25->qbitincl) |
| 1078 | len++; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * lock_sock() is currently only used to serialize this x25_kick() |
| 1083 | * against input-driven x25_kick() calls. It currently only blocks |
| 1084 | * incoming packets for this socket and does not protect against |
| 1085 | * any other socket state changes and is not called from anywhere |
| 1086 | * else. As x25_kick() cannot block and as long as all socket |
| 1087 | * operations are BKL-wrapped, we don't need take to care about |
| 1088 | * purging the backlog queue in x25_release(). |
| 1089 | * |
| 1090 | * Using lock_sock() to protect all socket operations entirely |
| 1091 | * (and making the whole x25 stack SMP aware) unfortunately would |
| 1092 | * require major changes to {send,recv}msg and skb allocation methods. |
| 1093 | * -> 2.5 ;) |
| 1094 | */ |
| 1095 | lock_sock(sk); |
| 1096 | x25_kick(sk); |
| 1097 | release_sock(sk); |
| 1098 | rc = len; |
| 1099 | out: |
| 1100 | return rc; |
| 1101 | out_kfree_skb: |
| 1102 | kfree_skb(skb); |
| 1103 | goto out; |
| 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | static int x25_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 1108 | struct msghdr *msg, size_t size, |
| 1109 | int flags) |
| 1110 | { |
| 1111 | struct sock *sk = sock->sk; |
| 1112 | struct x25_sock *x25 = x25_sk(sk); |
| 1113 | struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name; |
| 1114 | size_t copied; |
| 1115 | int qbit; |
| 1116 | struct sk_buff *skb; |
| 1117 | unsigned char *asmptr; |
| 1118 | int rc = -ENOTCONN; |
| 1119 | |
| 1120 | /* |
| 1121 | * This works for seqpacket too. The receiver has ordered the queue for |
| 1122 | * us! We do one quick check first though |
| 1123 | */ |
| 1124 | if (sk->sk_state != TCP_ESTABLISHED) |
| 1125 | goto out; |
| 1126 | |
| 1127 | if (flags & MSG_OOB) { |
| 1128 | rc = -EINVAL; |
| 1129 | if (sock_flag(sk, SOCK_URGINLINE) || |
| 1130 | !skb_peek(&x25->interrupt_in_queue)) |
| 1131 | goto out; |
| 1132 | |
| 1133 | skb = skb_dequeue(&x25->interrupt_in_queue); |
| 1134 | |
| 1135 | skb_pull(skb, X25_STD_MIN_LEN); |
| 1136 | |
| 1137 | /* |
| 1138 | * No Q bit information on Interrupt data. |
| 1139 | */ |
| 1140 | if (x25->qbitincl) { |
| 1141 | asmptr = skb_push(skb, 1); |
| 1142 | *asmptr = 0x00; |
| 1143 | } |
| 1144 | |
| 1145 | msg->msg_flags |= MSG_OOB; |
| 1146 | } else { |
| 1147 | /* Now we can treat all alike */ |
| 1148 | skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, |
| 1149 | flags & MSG_DONTWAIT, &rc); |
| 1150 | if (!skb) |
| 1151 | goto out; |
| 1152 | |
| 1153 | qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT; |
| 1154 | |
| 1155 | skb_pull(skb, x25->neighbour->extended ? |
| 1156 | X25_EXT_MIN_LEN : X25_STD_MIN_LEN); |
| 1157 | |
| 1158 | if (x25->qbitincl) { |
| 1159 | asmptr = skb_push(skb, 1); |
| 1160 | *asmptr = qbit; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | skb->h.raw = skb->data; |
| 1165 | |
| 1166 | copied = skb->len; |
| 1167 | |
| 1168 | if (copied > size) { |
| 1169 | copied = size; |
| 1170 | msg->msg_flags |= MSG_TRUNC; |
| 1171 | } |
| 1172 | |
| 1173 | /* Currently, each datagram always contains a complete record */ |
| 1174 | msg->msg_flags |= MSG_EOR; |
| 1175 | |
| 1176 | rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 1177 | if (rc) |
| 1178 | goto out_free_dgram; |
| 1179 | |
| 1180 | if (sx25) { |
| 1181 | sx25->sx25_family = AF_X25; |
| 1182 | sx25->sx25_addr = x25->dest_addr; |
| 1183 | } |
| 1184 | |
| 1185 | msg->msg_namelen = sizeof(struct sockaddr_x25); |
| 1186 | |
| 1187 | lock_sock(sk); |
| 1188 | x25_check_rbuf(sk); |
| 1189 | release_sock(sk); |
| 1190 | rc = copied; |
| 1191 | out_free_dgram: |
| 1192 | skb_free_datagram(sk, skb); |
| 1193 | out: |
| 1194 | return rc; |
| 1195 | } |
| 1196 | |
| 1197 | |
| 1198 | static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 1199 | { |
| 1200 | struct sock *sk = sock->sk; |
| 1201 | struct x25_sock *x25 = x25_sk(sk); |
| 1202 | void __user *argp = (void __user *)arg; |
| 1203 | int rc; |
| 1204 | |
| 1205 | switch (cmd) { |
| 1206 | case TIOCOUTQ: { |
| 1207 | int amount = sk->sk_sndbuf - |
| 1208 | atomic_read(&sk->sk_wmem_alloc); |
| 1209 | if (amount < 0) |
| 1210 | amount = 0; |
| 1211 | rc = put_user(amount, (unsigned int __user *)argp); |
| 1212 | break; |
| 1213 | } |
| 1214 | |
| 1215 | case TIOCINQ: { |
| 1216 | struct sk_buff *skb; |
| 1217 | int amount = 0; |
| 1218 | /* |
| 1219 | * These two are safe on a single CPU system as |
| 1220 | * only user tasks fiddle here |
| 1221 | */ |
| 1222 | if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) |
| 1223 | amount = skb->len; |
| 1224 | rc = put_user(amount, (unsigned int __user *)argp); |
| 1225 | break; |
| 1226 | } |
| 1227 | |
| 1228 | case SIOCGSTAMP: |
| 1229 | rc = -EINVAL; |
| 1230 | if (sk) |
| 1231 | rc = sock_get_timestamp(sk, |
| 1232 | (struct timeval __user *)argp); |
| 1233 | break; |
| 1234 | case SIOCGIFADDR: |
| 1235 | case SIOCSIFADDR: |
| 1236 | case SIOCGIFDSTADDR: |
| 1237 | case SIOCSIFDSTADDR: |
| 1238 | case SIOCGIFBRDADDR: |
| 1239 | case SIOCSIFBRDADDR: |
| 1240 | case SIOCGIFNETMASK: |
| 1241 | case SIOCSIFNETMASK: |
| 1242 | case SIOCGIFMETRIC: |
| 1243 | case SIOCSIFMETRIC: |
| 1244 | rc = -EINVAL; |
| 1245 | break; |
| 1246 | case SIOCADDRT: |
| 1247 | case SIOCDELRT: |
| 1248 | rc = -EPERM; |
| 1249 | if (!capable(CAP_NET_ADMIN)) |
| 1250 | break; |
| 1251 | rc = x25_route_ioctl(cmd, argp); |
| 1252 | break; |
| 1253 | case SIOCX25GSUBSCRIP: |
| 1254 | rc = x25_subscr_ioctl(cmd, argp); |
| 1255 | break; |
| 1256 | case SIOCX25SSUBSCRIP: |
| 1257 | rc = -EPERM; |
| 1258 | if (!capable(CAP_NET_ADMIN)) |
| 1259 | break; |
| 1260 | rc = x25_subscr_ioctl(cmd, argp); |
| 1261 | break; |
| 1262 | case SIOCX25GFACILITIES: { |
| 1263 | struct x25_facilities fac = x25->facilities; |
| 1264 | rc = copy_to_user(argp, &fac, |
| 1265 | sizeof(fac)) ? -EFAULT : 0; |
| 1266 | break; |
| 1267 | } |
| 1268 | |
| 1269 | case SIOCX25SFACILITIES: { |
| 1270 | struct x25_facilities facilities; |
| 1271 | rc = -EFAULT; |
| 1272 | if (copy_from_user(&facilities, argp, |
| 1273 | sizeof(facilities))) |
| 1274 | break; |
| 1275 | rc = -EINVAL; |
| 1276 | if (sk->sk_state != TCP_LISTEN && |
| 1277 | sk->sk_state != TCP_CLOSE) |
| 1278 | break; |
| 1279 | if (facilities.pacsize_in < X25_PS16 || |
| 1280 | facilities.pacsize_in > X25_PS4096) |
| 1281 | break; |
| 1282 | if (facilities.pacsize_out < X25_PS16 || |
| 1283 | facilities.pacsize_out > X25_PS4096) |
| 1284 | break; |
| 1285 | if (facilities.winsize_in < 1 || |
| 1286 | facilities.winsize_in > 127) |
| 1287 | break; |
| 1288 | if (facilities.throughput < 0x03 || |
| 1289 | facilities.throughput > 0xDD) |
| 1290 | break; |
| 1291 | if (facilities.reverse && facilities.reverse != 1) |
| 1292 | break; |
| 1293 | x25->facilities = facilities; |
| 1294 | rc = 0; |
| 1295 | break; |
| 1296 | } |
| 1297 | |
| 1298 | case SIOCX25GCALLUSERDATA: { |
| 1299 | struct x25_calluserdata cud = x25->calluserdata; |
| 1300 | rc = copy_to_user(argp, &cud, |
| 1301 | sizeof(cud)) ? -EFAULT : 0; |
| 1302 | break; |
| 1303 | } |
| 1304 | |
| 1305 | case SIOCX25SCALLUSERDATA: { |
| 1306 | struct x25_calluserdata calluserdata; |
| 1307 | |
| 1308 | rc = -EFAULT; |
| 1309 | if (copy_from_user(&calluserdata, argp, |
| 1310 | sizeof(calluserdata))) |
| 1311 | break; |
| 1312 | rc = -EINVAL; |
| 1313 | if (calluserdata.cudlength > X25_MAX_CUD_LEN) |
| 1314 | break; |
| 1315 | x25->calluserdata = calluserdata; |
| 1316 | rc = 0; |
| 1317 | break; |
| 1318 | } |
| 1319 | |
| 1320 | case SIOCX25GCAUSEDIAG: { |
| 1321 | struct x25_causediag causediag; |
| 1322 | causediag = x25->causediag; |
| 1323 | rc = copy_to_user(argp, &causediag, |
| 1324 | sizeof(causediag)) ? -EFAULT : 0; |
| 1325 | break; |
| 1326 | } |
| 1327 | |
| 1328 | default: |
| 1329 | rc = dev_ioctl(cmd, argp); |
| 1330 | break; |
| 1331 | } |
| 1332 | |
| 1333 | return rc; |
| 1334 | } |
| 1335 | |
| 1336 | static struct net_proto_family x25_family_ops = { |
| 1337 | .family = AF_X25, |
| 1338 | .create = x25_create, |
| 1339 | .owner = THIS_MODULE, |
| 1340 | }; |
| 1341 | |
| 1342 | static struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = { |
| 1343 | .family = AF_X25, |
| 1344 | .owner = THIS_MODULE, |
| 1345 | .release = x25_release, |
| 1346 | .bind = x25_bind, |
| 1347 | .connect = x25_connect, |
| 1348 | .socketpair = sock_no_socketpair, |
| 1349 | .accept = x25_accept, |
| 1350 | .getname = x25_getname, |
| 1351 | .poll = datagram_poll, |
| 1352 | .ioctl = x25_ioctl, |
| 1353 | .listen = x25_listen, |
| 1354 | .shutdown = sock_no_shutdown, |
| 1355 | .setsockopt = x25_setsockopt, |
| 1356 | .getsockopt = x25_getsockopt, |
| 1357 | .sendmsg = x25_sendmsg, |
| 1358 | .recvmsg = x25_recvmsg, |
| 1359 | .mmap = sock_no_mmap, |
| 1360 | .sendpage = sock_no_sendpage, |
| 1361 | }; |
| 1362 | |
| 1363 | #include <linux/smp_lock.h> |
| 1364 | SOCKOPS_WRAP(x25_proto, AF_X25); |
| 1365 | |
| 1366 | static struct packet_type x25_packet_type = { |
| 1367 | .type = __constant_htons(ETH_P_X25), |
| 1368 | .func = x25_lapb_receive_frame, |
| 1369 | }; |
| 1370 | |
| 1371 | static struct notifier_block x25_dev_notifier = { |
| 1372 | .notifier_call = x25_device_event, |
| 1373 | }; |
| 1374 | |
| 1375 | void x25_kill_by_neigh(struct x25_neigh *nb) |
| 1376 | { |
| 1377 | struct sock *s; |
| 1378 | struct hlist_node *node; |
| 1379 | |
| 1380 | write_lock_bh(&x25_list_lock); |
| 1381 | |
| 1382 | sk_for_each(s, node, &x25_list) |
| 1383 | if (x25_sk(s)->neighbour == nb) |
| 1384 | x25_disconnect(s, ENETUNREACH, 0, 0); |
| 1385 | |
| 1386 | write_unlock_bh(&x25_list_lock); |
| 1387 | } |
| 1388 | |
| 1389 | static int __init x25_init(void) |
| 1390 | { |
| 1391 | int rc = proto_register(&x25_proto, 0); |
| 1392 | |
| 1393 | if (rc != 0) |
| 1394 | goto out; |
| 1395 | |
| 1396 | sock_register(&x25_family_ops); |
| 1397 | |
| 1398 | dev_add_pack(&x25_packet_type); |
| 1399 | |
| 1400 | register_netdevice_notifier(&x25_dev_notifier); |
| 1401 | |
| 1402 | printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n"); |
| 1403 | |
| 1404 | #ifdef CONFIG_SYSCTL |
| 1405 | x25_register_sysctl(); |
| 1406 | #endif |
| 1407 | x25_proc_init(); |
| 1408 | out: |
| 1409 | return rc; |
| 1410 | } |
| 1411 | module_init(x25_init); |
| 1412 | |
| 1413 | static void __exit x25_exit(void) |
| 1414 | { |
| 1415 | x25_proc_exit(); |
| 1416 | x25_link_free(); |
| 1417 | x25_route_free(); |
| 1418 | |
| 1419 | #ifdef CONFIG_SYSCTL |
| 1420 | x25_unregister_sysctl(); |
| 1421 | #endif |
| 1422 | |
| 1423 | unregister_netdevice_notifier(&x25_dev_notifier); |
| 1424 | |
| 1425 | dev_remove_pack(&x25_packet_type); |
| 1426 | |
| 1427 | sock_unregister(AF_X25); |
| 1428 | proto_unregister(&x25_proto); |
| 1429 | } |
| 1430 | module_exit(x25_exit); |
| 1431 | |
| 1432 | MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>"); |
| 1433 | MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol"); |
| 1434 | MODULE_LICENSE("GPL"); |
| 1435 | MODULE_ALIAS_NETPROTO(PF_X25); |