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