* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / net / x25 / af_x25.c
blobae5d6c2d45d4c4c374ad61b19602502cdc5b4a8f
1 /*
2 * X.25 Packet Layer release 002
4 * This is ALPHA test software. This code may break your machine, randomly fail to work with new
5 * releases, misbehave and/or generally screw up. It might even work.
7 * This code REQUIRES 2.1.15 or higher
9 * This module:
10 * This module is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * History
16 * X.25 001 Jonathan Naylor Started coding.
17 * X.25 002 Jonathan Naylor Centralised disconnect handling.
18 * New timer architecture.
21 #include <linux/config.h>
22 #if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/in.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/string.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
34 #include <linux/stat.h>
35 #include <linux/inet.h>
36 #include <linux/netdevice.h>
37 #include <linux/if_arp.h>
38 #include <linux/skbuff.h>
39 #include <net/sock.h>
40 #include <asm/segment.h>
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
43 #include <linux/fcntl.h>
44 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
45 #include <linux/mm.h>
46 #include <linux/interrupt.h>
47 #include <linux/notifier.h>
48 #include <linux/proc_fs.h>
49 #include <linux/init.h>
50 #include <net/x25.h>
52 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
53 int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
54 int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
55 int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
56 int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
58 static struct sock *volatile x25_list = NULL;
60 static struct proto_ops x25_proto_ops;
62 static x25_address null_x25_address = {" "};
64 int x25_addr_ntoa(unsigned char *p, x25_address *called_addr, x25_address *calling_addr)
66 int called_len, calling_len;
67 char *called, *calling;
68 int i;
70 called_len = (*p >> 0) & 0x0F;
71 calling_len = (*p >> 4) & 0x0F;
73 called = called_addr->x25_addr;
74 calling = calling_addr->x25_addr;
75 p++;
77 for (i = 0; i < (called_len + calling_len); i++) {
78 if (i < called_len) {
79 if (i % 2 != 0) {
80 *called++ = ((*p >> 0) & 0x0F) + '0';
81 p++;
82 } else {
83 *called++ = ((*p >> 4) & 0x0F) + '0';
85 } else {
86 if (i % 2 != 0) {
87 *calling++ = ((*p >> 0) & 0x0F) + '0';
88 p++;
89 } else {
90 *calling++ = ((*p >> 4) & 0x0F) + '0';
95 *called = '\0';
96 *calling = '\0';
98 return 1 + (called_len + calling_len + 1) / 2;
101 int x25_addr_aton(unsigned char *p, x25_address *called_addr, x25_address *calling_addr)
103 unsigned int called_len, calling_len;
104 char *called, *calling;
105 int i;
107 called = called_addr->x25_addr;
108 calling = calling_addr->x25_addr;
110 called_len = strlen(called);
111 calling_len = strlen(calling);
113 *p++ = (calling_len << 4) | (called_len << 0);
115 for (i = 0; i < (called_len + calling_len); i++) {
116 if (i < called_len) {
117 if (i % 2 != 0) {
118 *p |= (*called++ - '0') << 0;
119 p++;
120 } else {
121 *p = 0x00;
122 *p |= (*called++ - '0') << 4;
124 } else {
125 if (i % 2 != 0) {
126 *p |= (*calling++ - '0') << 0;
127 p++;
128 } else {
129 *p = 0x00;
130 *p |= (*calling++ - '0') << 4;
135 return 1 + (called_len + calling_len + 1) / 2;
139 * Socket removal during an interrupt is now safe.
141 static void x25_remove_socket(struct sock *sk)
143 struct sock *s;
144 unsigned long flags;
146 save_flags(flags);
147 cli();
149 if ((s = x25_list) == sk) {
150 x25_list = s->next;
151 restore_flags(flags);
152 return;
155 while (s != NULL && s->next != NULL) {
156 if (s->next == sk) {
157 s->next = sk->next;
158 restore_flags(flags);
159 return;
162 s = s->next;
165 restore_flags(flags);
169 * Kill all bound sockets on a dropped device.
171 static void x25_kill_by_device(struct net_device *dev)
173 struct sock *s;
175 for (s = x25_list; s != NULL; s = s->next)
176 if (s->protinfo.x25->neighbour &&
177 s->protinfo.x25->neighbour->dev == dev)
178 x25_disconnect(s, ENETUNREACH, 0, 0);
182 * Handle device status changes.
184 static int x25_device_event(struct notifier_block *this, unsigned long event, void *ptr)
186 struct net_device *dev = (struct net_device *)ptr;
188 if (dev->type == ARPHRD_X25
189 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
190 || dev->type == ARPHRD_ETHER
191 #endif
193 switch (event) {
194 case NETDEV_UP:
195 x25_link_device_up(dev);
196 break;
197 case NETDEV_DOWN:
198 x25_kill_by_device(dev);
199 x25_route_device_down(dev);
200 x25_link_device_down(dev);
201 break;
205 return NOTIFY_DONE;
209 * Add a socket to the bound sockets list.
211 static void x25_insert_socket(struct sock *sk)
213 unsigned long flags;
215 save_flags(flags);
216 cli();
218 sk->next = x25_list;
219 x25_list = sk;
221 restore_flags(flags);
225 * Find a socket that wants to accept the Call Request we just
226 * received.
228 static struct sock *x25_find_listener(x25_address *addr)
230 unsigned long flags;
231 struct sock *s;
233 save_flags(flags);
234 cli();
236 for (s = x25_list; s != NULL; s = s->next) {
237 if ((strcmp(addr->x25_addr, s->protinfo.x25->source_addr.x25_addr) == 0 ||
238 strcmp(addr->x25_addr, null_x25_address.x25_addr) == 0) &&
239 s->state == TCP_LISTEN) {
240 restore_flags(flags);
241 return s;
245 restore_flags(flags);
246 return NULL;
250 * Find a connected X.25 socket given my LCI and neighbour.
252 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *neigh)
254 struct sock *s;
255 unsigned long flags;
257 save_flags(flags);
258 cli();
260 for (s = x25_list; s != NULL; s = s->next) {
261 if (s->protinfo.x25->lci == lci && s->protinfo.x25->neighbour == neigh) {
262 restore_flags(flags);
263 return s;
267 restore_flags(flags);
268 return NULL;
272 * Find a unique LCI for a given device.
274 unsigned int x25_new_lci(struct x25_neigh *neigh)
276 unsigned int lci = 1;
278 while (x25_find_socket(lci, neigh) != NULL) {
279 lci++;
280 if (lci == 4096) return 0;
283 return lci;
287 * Deferred destroy.
289 void x25_destroy_socket(struct sock *);
292 * handler for deferred kills.
294 static void x25_destroy_timer(unsigned long data)
296 x25_destroy_socket((struct sock *)data);
300 * This is called from user mode and the timers. Thus it protects itself against
301 * interrupt users but doesn't worry about being called during work.
302 * Once it is removed from the queue no interrupt or bottom half will
303 * touch it and we are (fairly 8-) ) safe.
305 void x25_destroy_socket(struct sock *sk) /* Not static as it's used by the timer */
307 struct sk_buff *skb;
308 unsigned long flags;
310 save_flags(flags);
311 cli();
313 x25_stop_heartbeat(sk);
314 x25_stop_timer(sk);
316 x25_remove_socket(sk);
317 x25_clear_queues(sk); /* Flush the queues */
319 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
320 if (skb->sk != sk) { /* A pending connection */
321 skb->sk->dead = 1; /* Queue the unaccepted socket for death */
322 x25_start_heartbeat(skb->sk);
323 skb->sk->protinfo.x25->state = X25_STATE_0;
326 kfree_skb(skb);
329 if (atomic_read(&sk->wmem_alloc) != 0 || atomic_read(&sk->rmem_alloc) != 0) {
330 /* Defer: outstanding buffers */
331 init_timer(&sk->timer);
332 sk->timer.expires = jiffies + 10 * HZ;
333 sk->timer.function = x25_destroy_timer;
334 sk->timer.data = (unsigned long)sk;
335 add_timer(&sk->timer);
336 } else {
337 sk_free(sk);
338 MOD_DEC_USE_COUNT;
341 restore_flags(flags);
345 * Handling for system calls applied via the various interfaces to a
346 * X.25 socket object.
349 static int x25_setsockopt(struct socket *sock, int level, int optname,
350 char *optval, int optlen)
352 struct sock *sk = sock->sk;
353 int opt;
355 if (level != SOL_X25)
356 return -ENOPROTOOPT;
358 if (optlen < sizeof(int))
359 return-EINVAL;
361 if (get_user(opt, (int *)optval))
362 return -EFAULT;
364 switch (optname) {
365 case X25_QBITINCL:
366 sk->protinfo.x25->qbitincl = opt ? 1 : 0;
367 return 0;
369 default:
370 return -ENOPROTOOPT;
374 static int x25_getsockopt(struct socket *sock, int level, int optname,
375 char *optval, int *optlen)
377 struct sock *sk = sock->sk;
378 int val = 0;
379 int len;
381 if (level != SOL_X25)
382 return -ENOPROTOOPT;
384 if (get_user(len, optlen))
385 return -EFAULT;
387 switch (optname) {
388 case X25_QBITINCL:
389 val = sk->protinfo.x25->qbitincl;
390 break;
392 default:
393 return -ENOPROTOOPT;
396 len = min(len, sizeof(int));
398 if (put_user(len, optlen))
399 return -EFAULT;
401 if (copy_to_user(optval, &val, len))
402 return -EFAULT;
404 return 0;
407 static int x25_listen(struct socket *sock, int backlog)
409 struct sock *sk = sock->sk;
411 if (sk->state != TCP_LISTEN) {
412 memset(&sk->protinfo.x25->dest_addr, '\0', X25_ADDR_LEN);
413 sk->max_ack_backlog = backlog;
414 sk->state = TCP_LISTEN;
415 return 0;
418 return -EOPNOTSUPP;
421 static struct sock *x25_alloc_socket(void)
423 struct sock *sk;
424 x25_cb *x25;
426 if ((sk = sk_alloc(AF_X25, GFP_ATOMIC, 1)) == NULL)
427 return NULL;
429 if ((x25 = kmalloc(sizeof(*x25), GFP_ATOMIC)) == NULL) {
430 sk_free(sk);
431 return NULL;
434 memset(x25, 0x00, sizeof(*x25));
436 x25->sk = sk;
437 sk->protinfo.x25 = x25;
439 MOD_INC_USE_COUNT;
441 sock_init_data(NULL, sk);
443 skb_queue_head_init(&x25->ack_queue);
444 skb_queue_head_init(&x25->fragment_queue);
445 skb_queue_head_init(&x25->interrupt_in_queue);
446 skb_queue_head_init(&x25->interrupt_out_queue);
448 return sk;
451 static int x25_create(struct socket *sock, int protocol)
453 struct sock *sk;
454 x25_cb *x25;
456 if (sock->type != SOCK_SEQPACKET || protocol != 0)
457 return -ESOCKTNOSUPPORT;
459 if ((sk = x25_alloc_socket()) == NULL)
460 return -ENOMEM;
462 x25 = sk->protinfo.x25;
464 sock_init_data(sock, sk);
466 init_timer(&x25->timer);
468 sock->ops = &x25_proto_ops;
469 sk->protocol = protocol;
471 x25->t21 = sysctl_x25_call_request_timeout;
472 x25->t22 = sysctl_x25_reset_request_timeout;
473 x25->t23 = sysctl_x25_clear_request_timeout;
474 x25->t2 = sysctl_x25_ack_holdback_timeout;
475 x25->state = X25_STATE_0;
477 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
478 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
479 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
480 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
481 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
482 x25->facilities.reverse = X25_DEFAULT_REVERSE;
484 return 0;
487 static struct sock *x25_make_new(struct sock *osk)
489 struct sock *sk;
490 x25_cb *x25;
492 if (osk->type != SOCK_SEQPACKET)
493 return NULL;
495 if ((sk = x25_alloc_socket()) == NULL)
496 return NULL;
498 x25 = sk->protinfo.x25;
500 sk->type = osk->type;
501 sk->socket = osk->socket;
502 sk->priority = osk->priority;
503 sk->protocol = osk->protocol;
504 sk->rcvbuf = osk->rcvbuf;
505 sk->sndbuf = osk->sndbuf;
506 sk->debug = osk->debug;
507 sk->state = TCP_ESTABLISHED;
508 sk->sleep = osk->sleep;
509 sk->zapped = osk->zapped;
511 x25->t21 = osk->protinfo.x25->t21;
512 x25->t22 = osk->protinfo.x25->t22;
513 x25->t23 = osk->protinfo.x25->t23;
514 x25->t2 = osk->protinfo.x25->t2;
516 x25->facilities = osk->protinfo.x25->facilities;
518 x25->qbitincl = osk->protinfo.x25->qbitincl;
520 init_timer(&x25->timer);
522 return sk;
525 static int x25_release(struct socket *sock)
527 struct sock *sk = sock->sk;
529 if (sk == NULL) return 0;
531 switch (sk->protinfo.x25->state) {
533 case X25_STATE_0:
534 case X25_STATE_2:
535 x25_disconnect(sk, 0, 0, 0);
536 x25_destroy_socket(sk);
537 break;
539 case X25_STATE_1:
540 case X25_STATE_3:
541 case X25_STATE_4:
542 x25_clear_queues(sk);
543 x25_write_internal(sk, X25_CLEAR_REQUEST);
544 x25_start_t23timer(sk);
545 sk->protinfo.x25->state = X25_STATE_2;
546 sk->state = TCP_CLOSE;
547 sk->shutdown |= SEND_SHUTDOWN;
548 sk->state_change(sk);
549 sk->dead = 1;
550 sk->destroy = 1;
551 break;
553 default:
554 break;
557 sock->sk = NULL;
558 sk->socket = NULL; /* Not used, but we should do this */
560 return 0;
563 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
565 struct sock *sk = sock->sk;
566 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
568 if (sk->zapped == 0)
569 return -EINVAL;
571 if (addr_len != sizeof(struct sockaddr_x25))
572 return -EINVAL;
574 if (addr->sx25_family != AF_X25)
575 return -EINVAL;
577 sk->protinfo.x25->source_addr = addr->sx25_addr;
579 x25_insert_socket(sk);
581 sk->zapped = 0;
583 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
585 return 0;
588 static int x25_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
590 struct sock *sk = sock->sk;
591 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
592 struct net_device *dev;
594 if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
595 sock->state = SS_CONNECTED;
596 return 0; /* Connect completed during a ERESTARTSYS event */
599 if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
600 sock->state = SS_UNCONNECTED;
601 return -ECONNREFUSED;
604 if (sk->state == TCP_ESTABLISHED)
605 return -EISCONN; /* No reconnect on a seqpacket socket */
607 sk->state = TCP_CLOSE;
608 sock->state = SS_UNCONNECTED;
610 if (addr_len != sizeof(struct sockaddr_x25))
611 return -EINVAL;
613 if (addr->sx25_family != AF_X25)
614 return -EINVAL;
616 if ((dev = x25_get_route(&addr->sx25_addr)) == NULL)
617 return -ENETUNREACH;
619 if ((sk->protinfo.x25->neighbour = x25_get_neigh(dev)) == NULL)
620 return -ENETUNREACH;
622 x25_limit_facilities(&sk->protinfo.x25->facilities,
623 sk->protinfo.x25->neighbour);
625 if ((sk->protinfo.x25->lci = x25_new_lci(sk->protinfo.x25->neighbour)) == 0)
626 return -ENETUNREACH;
628 if (sk->zapped) /* Must bind first - autobinding does not work */
629 return -EINVAL;
631 if (strcmp(sk->protinfo.x25->source_addr.x25_addr, null_x25_address.x25_addr) == 0)
632 memset(&sk->protinfo.x25->source_addr, '\0', X25_ADDR_LEN);
634 sk->protinfo.x25->dest_addr = addr->sx25_addr;
636 /* Move to connecting socket, start sending Connect Requests */
637 sock->state = SS_CONNECTING;
638 sk->state = TCP_SYN_SENT;
640 sk->protinfo.x25->state = X25_STATE_1;
642 x25_write_internal(sk, X25_CALL_REQUEST);
644 x25_start_heartbeat(sk);
645 x25_start_t21timer(sk);
647 /* Now the loop */
648 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
649 return -EINPROGRESS;
651 cli(); /* To avoid races on the sleep */
654 * A Clear Request or timeout or failed routing will go to closed.
656 while (sk->state == TCP_SYN_SENT) {
657 interruptible_sleep_on(sk->sleep);
658 if (signal_pending(current)) {
659 sti();
660 return -ERESTARTSYS;
664 if (sk->state != TCP_ESTABLISHED) {
665 sti();
666 sock->state = SS_UNCONNECTED;
667 return sock_error(sk); /* Always set at this point */
670 sock->state = SS_CONNECTED;
672 sti();
674 return 0;
677 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
679 struct sock *sk;
680 struct sock *newsk;
681 struct sk_buff *skb;
683 if ((sk = sock->sk) == NULL)
684 return -EINVAL;
686 if (sk->type != SOCK_SEQPACKET)
687 return -EOPNOTSUPP;
689 if (sk->state != TCP_LISTEN)
690 return -EINVAL;
693 * The write queue this time is holding sockets ready to use
694 * hooked into the CALL INDICATION we saved
696 do {
697 cli();
698 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
699 if (flags & O_NONBLOCK) {
700 sti();
701 return -EWOULDBLOCK;
703 interruptible_sleep_on(sk->sleep);
704 if (signal_pending(current)) {
705 sti();
706 return -ERESTARTSYS;
709 } while (skb == NULL);
711 newsk = skb->sk;
712 newsk->pair = NULL;
713 newsk->socket = newsock;
714 newsk->sleep = &newsock->wait;
715 sti();
717 /* Now attach up the new socket */
718 skb->sk = NULL;
719 kfree_skb(skb);
720 sk->ack_backlog--;
721 newsock->sk = newsk;
723 return 0;
726 static int x25_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
728 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
729 struct sock *sk = sock->sk;
731 if (peer != 0) {
732 if (sk->state != TCP_ESTABLISHED)
733 return -ENOTCONN;
734 sx25->sx25_addr = sk->protinfo.x25->dest_addr;
735 } else {
736 sx25->sx25_addr = sk->protinfo.x25->source_addr;
739 sx25->sx25_family = AF_X25;
740 *uaddr_len = sizeof(struct sockaddr_x25);
742 return 0;
745 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *neigh, unsigned int lci)
747 struct sock *sk;
748 struct sock *make;
749 x25_address source_addr, dest_addr;
750 struct x25_facilities facilities;
751 int len;
754 * Remove the LCI and frame type.
756 skb_pull(skb, X25_STD_MIN_LEN);
759 * Extract the X.25 addresses and convert them to ASCII strings,
760 * and remove them.
762 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
765 * Find a listener for the particular address.
767 sk = x25_find_listener(&source_addr);
770 * We can't accept the Call Request.
772 if (sk == NULL || sk->ack_backlog == sk->max_ack_backlog) {
773 x25_transmit_clear_request(neigh, lci, 0x01);
774 return 0;
778 * Try to reach a compromise on the requested facilities.
780 if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1) {
781 x25_transmit_clear_request(neigh, lci, 0x01);
782 return 0;
786 * current neighbour/link might impose additional limits
787 * on certain facilties
790 x25_limit_facilities(&facilities,neigh);
793 * Try to create a new socket.
795 if ((make = x25_make_new(sk)) == NULL) {
796 x25_transmit_clear_request(neigh, lci, 0x01);
797 return 0;
801 * Remove the facilities, leaving any Call User Data.
803 skb_pull(skb, len);
805 skb->sk = make;
806 make->state = TCP_ESTABLISHED;
808 make->protinfo.x25->lci = lci;
809 make->protinfo.x25->dest_addr = dest_addr;
810 make->protinfo.x25->source_addr = source_addr;
811 make->protinfo.x25->neighbour = neigh;
812 make->protinfo.x25->facilities = facilities;
814 x25_write_internal(make, X25_CALL_ACCEPTED);
817 * Incoming Call User Data.
819 if (skb->len >= 0) {
820 memcpy(make->protinfo.x25->calluserdata.cuddata, skb->data, skb->len);
821 make->protinfo.x25->calluserdata.cudlength = skb->len;
824 make->protinfo.x25->state = X25_STATE_3;
826 sk->ack_backlog++;
827 make->pair = sk;
829 x25_insert_socket(make);
831 skb_queue_head(&sk->receive_queue, skb);
833 x25_start_heartbeat(make);
835 if (!sk->dead)
836 sk->data_ready(sk, skb->len);
838 return 1;
841 static int x25_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
843 struct sock *sk = sock->sk;
844 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
845 int err;
846 struct sockaddr_x25 sx25;
847 struct sk_buff *skb;
848 unsigned char *asmptr;
849 int size, qbit = 0;
851 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_OOB))
852 return -EINVAL;
854 if (sk->zapped)
855 return -EADDRNOTAVAIL;
857 if (sk->shutdown & SEND_SHUTDOWN) {
858 send_sig(SIGPIPE, current, 0);
859 return -EPIPE;
862 if (sk->protinfo.x25->neighbour == NULL)
863 return -ENETUNREACH;
865 if (usx25 != NULL) {
866 if (msg->msg_namelen < sizeof(sx25))
867 return -EINVAL;
868 sx25 = *usx25;
869 if (strcmp(sk->protinfo.x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr) != 0)
870 return -EISCONN;
871 if (sx25.sx25_family != AF_X25)
872 return -EINVAL;
873 } else {
875 * FIXME 1003.1g - if the socket is like this because
876 * it has become closed (not started closed) we ought
877 * to SIGPIPE, EPIPE;
879 if (sk->state != TCP_ESTABLISHED)
880 return -ENOTCONN;
882 sx25.sx25_family = AF_X25;
883 sx25.sx25_addr = sk->protinfo.x25->dest_addr;
886 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
888 /* Build a packet */
889 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
891 if ((msg->msg_flags & MSG_OOB) && len > 32)
892 len = 32;
894 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
896 if ((skb = sock_alloc_send_skb(sk, size, 0, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
897 return err;
899 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
902 * Put the data on the end
904 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
906 asmptr = skb->h.raw = skb_put(skb, len);
908 memcpy_fromiovec(asmptr, msg->msg_iov, len);
911 * If the Q BIT Include socket option is in force, the first
912 * byte of the user data is the logical value of the Q Bit.
914 if (sk->protinfo.x25->qbitincl) {
915 qbit = skb->data[0];
916 skb_pull(skb, 1);
920 * Push down the X.25 header
922 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
924 if (msg->msg_flags & MSG_OOB) {
925 if (sk->protinfo.x25->neighbour->extended) {
926 asmptr = skb_push(skb, X25_STD_MIN_LEN);
927 *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
928 *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
929 *asmptr++ = X25_INTERRUPT;
930 } else {
931 asmptr = skb_push(skb, X25_STD_MIN_LEN);
932 *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
933 *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
934 *asmptr++ = X25_INTERRUPT;
936 } else {
937 if (sk->protinfo.x25->neighbour->extended) {
938 /* Build an Extended X.25 header */
939 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
940 *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
941 *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
942 *asmptr++ = X25_DATA;
943 *asmptr++ = X25_DATA;
944 } else {
945 /* Build an Standard X.25 header */
946 asmptr = skb_push(skb, X25_STD_MIN_LEN);
947 *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
948 *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
949 *asmptr++ = X25_DATA;
952 if (qbit)
953 skb->data[0] |= X25_Q_BIT;
956 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
957 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
959 if (sk->state != TCP_ESTABLISHED) {
960 kfree_skb(skb);
961 return -ENOTCONN;
964 if (msg->msg_flags & MSG_OOB) {
965 skb_queue_tail(&sk->protinfo.x25->interrupt_out_queue, skb);
966 } else {
967 x25_output(sk, skb);
970 x25_kick(sk);
972 return len;
976 static int x25_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm)
978 struct sock *sk = sock->sk;
979 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
980 int copied, qbit;
981 struct sk_buff *skb;
982 unsigned char *asmptr;
983 int er;
986 * This works for seqpacket too. The receiver has ordered the queue for
987 * us! We do one quick check first though
989 if (sk->state != TCP_ESTABLISHED)
990 return -ENOTCONN;
992 if (flags & MSG_OOB) {
993 if (sk->urginline || skb_peek(&sk->protinfo.x25->interrupt_in_queue) == NULL)
994 return -EINVAL;
996 skb = skb_dequeue(&sk->protinfo.x25->interrupt_in_queue);
998 skb_pull(skb, X25_STD_MIN_LEN);
1001 * No Q bit information on Interrupt data.
1003 if (sk->protinfo.x25->qbitincl) {
1004 asmptr = skb_push(skb, 1);
1005 *asmptr = 0x00;
1008 msg->msg_flags |= MSG_OOB;
1009 } else {
1010 /* Now we can treat all alike */
1011 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1012 return er;
1014 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1016 skb_pull(skb, (sk->protinfo.x25->neighbour->extended) ? X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1018 if (sk->protinfo.x25->qbitincl) {
1019 asmptr = skb_push(skb, 1);
1020 *asmptr = qbit;
1024 skb->h.raw = skb->data;
1026 copied = skb->len;
1028 if (copied > size) {
1029 copied = size;
1030 msg->msg_flags |= MSG_TRUNC;
1033 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1035 if (sx25 != NULL) {
1036 sx25->sx25_family = AF_X25;
1037 sx25->sx25_addr = sk->protinfo.x25->dest_addr;
1040 msg->msg_namelen = sizeof(struct sockaddr_x25);
1042 skb_free_datagram(sk, skb);
1044 return copied;
1048 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1050 struct sock *sk = sock->sk;
1052 switch (cmd) {
1053 case TIOCOUTQ: {
1054 int amount;
1055 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1056 if (amount < 0)
1057 amount = 0;
1058 if (put_user(amount, (unsigned int *)arg))
1059 return -EFAULT;
1060 return 0;
1063 case TIOCINQ: {
1064 struct sk_buff *skb;
1065 int amount = 0;
1066 /* These two are safe on a single CPU system as only user tasks fiddle here */
1067 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1068 amount = skb->len;
1069 if (put_user(amount, (unsigned int *)arg))
1070 return -EFAULT;
1071 return 0;
1074 case SIOCGSTAMP:
1075 if (sk != NULL) {
1076 if (sk->stamp.tv_sec == 0)
1077 return -ENOENT;
1078 if (copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)))
1079 return -EFAULT;
1080 return 0;
1082 return -EINVAL;
1084 case SIOCGIFADDR:
1085 case SIOCSIFADDR:
1086 case SIOCGIFDSTADDR:
1087 case SIOCSIFDSTADDR:
1088 case SIOCGIFBRDADDR:
1089 case SIOCSIFBRDADDR:
1090 case SIOCGIFNETMASK:
1091 case SIOCSIFNETMASK:
1092 case SIOCGIFMETRIC:
1093 case SIOCSIFMETRIC:
1094 return -EINVAL;
1096 case SIOCADDRT:
1097 case SIOCDELRT:
1098 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1099 return x25_route_ioctl(cmd, (void *)arg);
1101 case SIOCX25GSUBSCRIP:
1102 return x25_subscr_ioctl(cmd, (void *)arg);
1104 case SIOCX25SSUBSCRIP:
1105 if (!suser()) return -EPERM;
1106 return x25_subscr_ioctl(cmd, (void *)arg);
1108 case SIOCX25GFACILITIES: {
1109 struct x25_facilities facilities;
1110 facilities = sk->protinfo.x25->facilities;
1111 if (copy_to_user((void *)arg, &facilities, sizeof(facilities)))
1112 return -EFAULT;
1113 return 0;
1116 case SIOCX25SFACILITIES: {
1117 struct x25_facilities facilities;
1118 if (copy_from_user(&facilities, (void *)arg, sizeof(facilities)))
1119 return -EFAULT;
1120 if (sk->state != TCP_LISTEN && sk->state != TCP_CLOSE)
1121 return -EINVAL;
1122 if (facilities.pacsize_in < X25_PS16 || facilities.pacsize_in > X25_PS4096)
1123 return -EINVAL;
1124 if (facilities.pacsize_out < X25_PS16 || facilities.pacsize_out > X25_PS4096)
1125 return -EINVAL;
1126 if (facilities.winsize_in < 1 || facilities.winsize_in > 127)
1127 return -EINVAL;
1128 if (facilities.throughput < 0x03 || facilities.throughput > 0x2C)
1129 return -EINVAL;
1130 if (facilities.reverse != 0 && facilities.reverse != 1)
1131 return -EINVAL;
1132 sk->protinfo.x25->facilities = facilities;
1133 return 0;
1136 case SIOCX25GCALLUSERDATA: {
1137 struct x25_calluserdata calluserdata;
1138 calluserdata = sk->protinfo.x25->calluserdata;
1139 if (copy_to_user((void *)arg, &calluserdata, sizeof(calluserdata)))
1140 return -EFAULT;
1141 return 0;
1144 case SIOCX25SCALLUSERDATA: {
1145 struct x25_calluserdata calluserdata;
1146 if (copy_from_user(&calluserdata, (void *)arg, sizeof(calluserdata)))
1147 return -EFAULT;
1148 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1149 return -EINVAL;
1150 sk->protinfo.x25->calluserdata = calluserdata;
1151 return 0;
1154 case SIOCX25GCAUSEDIAG: {
1155 struct x25_causediag causediag;
1156 causediag = sk->protinfo.x25->causediag;
1157 if (copy_to_user((void *)arg, &causediag, sizeof(causediag)))
1158 return -EFAULT;
1159 return 0;
1162 default:
1163 return dev_ioctl(cmd, (void *)arg);
1166 /*NOTREACHED*/
1167 return 0;
1170 static int x25_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1172 struct sock *s;
1173 struct net_device *dev;
1174 const char *devname;
1175 int len = 0;
1176 off_t pos = 0;
1177 off_t begin = 0;
1179 cli();
1181 len += sprintf(buffer, "dest_addr src_addr dev lci st vs vr va t t2 t21 t22 t23 Snd-Q Rcv-Q inode\n");
1183 for (s = x25_list; s != NULL; s = s->next) {
1184 if (s->protinfo.x25->neighbour == NULL || (dev = s->protinfo.x25->neighbour->dev) == NULL)
1185 devname = "???";
1186 else
1187 devname = s->protinfo.x25->neighbour->dev->name;
1189 len += sprintf(buffer + len, "%-10s %-10s %-5s %3.3X %d %d %d %d %3lu %3lu %3lu %3lu %3lu %5d %5d %ld\n",
1190 (s->protinfo.x25->dest_addr.x25_addr[0] == '\0') ? "*" : s->protinfo.x25->dest_addr.x25_addr,
1191 (s->protinfo.x25->source_addr.x25_addr[0] == '\0') ? "*" : s->protinfo.x25->source_addr.x25_addr,
1192 devname,
1193 s->protinfo.x25->lci & 0x0FFF,
1194 s->protinfo.x25->state,
1195 s->protinfo.x25->vs,
1196 s->protinfo.x25->vr,
1197 s->protinfo.x25->va,
1198 x25_display_timer(s) / HZ,
1199 s->protinfo.x25->t2 / HZ,
1200 s->protinfo.x25->t21 / HZ,
1201 s->protinfo.x25->t22 / HZ,
1202 s->protinfo.x25->t23 / HZ,
1203 atomic_read(&s->wmem_alloc),
1204 atomic_read(&s->rmem_alloc),
1205 s->socket != NULL ? s->socket->inode->i_ino : 0L);
1207 pos = begin + len;
1209 if (pos < offset) {
1210 len = 0;
1211 begin = pos;
1214 if (pos > offset + length)
1215 break;
1218 sti();
1220 *start = buffer + (offset - begin);
1221 len -= (offset - begin);
1223 if (len > length) len = length;
1225 return(len);
1228 struct net_proto_family x25_family_ops = {
1229 AF_X25,
1230 x25_create
1233 static struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1234 AF_X25,
1236 x25_release,
1237 x25_bind,
1238 x25_connect,
1239 sock_no_socketpair,
1240 x25_accept,
1241 x25_getname,
1242 datagram_poll,
1243 x25_ioctl,
1244 x25_listen,
1245 sock_no_shutdown,
1246 x25_setsockopt,
1247 x25_getsockopt,
1248 sock_no_fcntl,
1249 x25_sendmsg,
1250 x25_recvmsg,
1251 sock_no_mmap
1254 #include <linux/smp_lock.h>
1255 SOCKOPS_WRAP(x25_proto, AF_X25);
1258 static struct packet_type x25_packet_type =
1260 0, /* MUTTER ntohs(ETH_P_X25),*/
1261 0, /* copy */
1262 x25_lapb_receive_frame,
1263 NULL,
1264 NULL,
1267 struct notifier_block x25_dev_notifier = {
1268 x25_device_event,
1272 void x25_kill_by_neigh(struct x25_neigh *neigh)
1274 struct sock *s;
1276 for( s=x25_list; s != NULL; s=s->next){
1277 if( s->protinfo.x25->neighbour == neigh )
1278 x25_disconnect(s, ENETUNREACH, 0, 0);
1282 #ifdef CONFIG_PROC_FS
1283 static struct proc_dir_entry proc_net_x25 = {
1284 PROC_NET_X25, 3, "x25",
1285 S_IFREG | S_IRUGO, 1, 0, 0,
1286 0, &proc_net_inode_operations,
1287 x25_get_info
1289 static struct proc_dir_entry proc_net_x25_routes = {
1290 PROC_NET_X25_ROUTES, 10, "x25_routes",
1291 S_IFREG | S_IRUGO, 1, 0, 0,
1292 0, &proc_net_inode_operations,
1293 x25_routes_get_info
1295 #endif
1297 void __init x25_proto_init(struct net_proto *pro)
1299 sock_register(&x25_family_ops);
1301 x25_packet_type.type = htons(ETH_P_X25);
1302 dev_add_pack(&x25_packet_type);
1304 register_netdevice_notifier(&x25_dev_notifier);
1306 printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1308 #ifdef CONFIG_SYSCTL
1309 x25_register_sysctl();
1310 #endif
1312 #ifdef CONFIG_PROC_FS
1313 proc_net_register(&proc_net_x25);
1314 proc_net_register(&proc_net_x25_routes);
1315 #endif
1318 #ifdef MODULE
1319 EXPORT_NO_SYMBOLS;
1321 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1322 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1324 int init_module(void)
1326 struct net_device *dev;
1328 x25_proto_init(NULL);
1331 * Register any pre existing devices.
1333 read_lock(&dev_base_lock);
1334 for (dev = dev_base; dev != NULL; dev = dev->next) {
1335 if ((dev->flags & IFF_UP) && (dev->type == ARPHRD_X25
1336 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
1337 || dev->type == ARPHRD_ETHER
1338 #endif
1340 x25_link_device_up(dev);
1342 read_unlock(&dev_base_lock);
1344 return 0;
1347 void cleanup_module(void)
1350 #ifdef CONFIG_PROC_FS
1351 proc_net_unregister(PROC_NET_X25);
1352 proc_net_unregister(PROC_NET_X25_ROUTES);
1353 #endif
1355 x25_link_free();
1356 x25_route_free();
1358 #ifdef CONFIG_SYSCTL
1359 x25_unregister_sysctl();
1360 #endif
1362 unregister_netdevice_notifier(&x25_dev_notifier);
1364 dev_remove_pack(&x25_packet_type);
1366 sock_unregister(AF_X25);
1369 #endif
1371 #endif