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