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