• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	X.25 Packet Layer release 002
4  *
5  *	This is ALPHA test software. This code may break your machine,
6  *	randomly fail to work with new releases, misbehave and/or generally
7  *	screw up. It might even work.
8  *
9  *	This code REQUIRES 2.1.15 or higher
10  *
11  *	History
12  *	X.25 001	Jonathan Naylor	Started coding.
13  *	X.25 002	Jonathan Naylor	Centralised disconnect handling.
14  *					New timer architecture.
15  *	2000-03-11	Henner Eisen	MSG_EOR handling more POSIX compliant.
16  *	2000-03-22	Daniela Squassoni Allowed disabling/enabling of
17  *					  facilities negotiation and increased
18  *					  the throughput upper limit.
19  *	2000-08-27	Arnaldo C. Melo s/suser/capable/ + micro cleanups
20  *	2000-09-04	Henner Eisen	Set sock->state in x25_accept().
21  *					Fixed x25_output() related skb leakage.
22  *	2000-10-02	Henner Eisen	Made x25_kick() single threaded per socket.
23  *	2000-10-27	Henner Eisen    MSG_DONTWAIT for fragment allocation.
24  *	2000-11-14	Henner Eisen    Closing datalink from NETDEV_GOING_DOWN
25  *	2002-10-06	Arnaldo C. Melo Get rid of cli/sti, move proc stuff to
26  *					x25_proc.c, using seq_file
27  *	2005-04-02	Shaun Pereira	Selective sub address matching
28  *					with call user data
29  *	2005-04-15	Shaun Pereira	Fast select with no restriction on
30  *					response
31  */
32 
33 #define pr_fmt(fmt) "X25: " fmt
34 
35 #include <linux/module.h>
36 #include <linux/capability.h>
37 #include <linux/errno.h>
38 #include <linux/kernel.h>
39 #include <linux/sched/signal.h>
40 #include <linux/timer.h>
41 #include <linux/string.h>
42 #include <linux/net.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/skbuff.h>
46 #include <linux/slab.h>
47 #include <net/sock.h>
48 #include <net/tcp_states.h>
49 #include <linux/uaccess.h>
50 #include <linux/fcntl.h>
51 #include <linux/termios.h>	/* For TIOCINQ/OUTQ */
52 #include <linux/notifier.h>
53 #include <linux/init.h>
54 #include <linux/compat.h>
55 #include <linux/ctype.h>
56 
57 #include <net/x25.h>
58 #include <net/compat.h>
59 
60 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
61 int sysctl_x25_call_request_timeout    = X25_DEFAULT_T21;
62 int sysctl_x25_reset_request_timeout   = X25_DEFAULT_T22;
63 int sysctl_x25_clear_request_timeout   = X25_DEFAULT_T23;
64 int sysctl_x25_ack_holdback_timeout    = X25_DEFAULT_T2;
65 int sysctl_x25_forward                 = 0;
66 
67 HLIST_HEAD(x25_list);
68 DEFINE_RWLOCK(x25_list_lock);
69 
70 static const struct proto_ops x25_proto_ops;
71 
72 static const struct x25_address null_x25_address = {"               "};
73 
74 #ifdef CONFIG_COMPAT
75 struct compat_x25_subscrip_struct {
76 	char device[200-sizeof(compat_ulong_t)];
77 	compat_ulong_t global_facil_mask;
78 	compat_uint_t extended;
79 };
80 #endif
81 
82 
x25_parse_address_block(struct sk_buff * skb,struct x25_address * called_addr,struct x25_address * calling_addr)83 int x25_parse_address_block(struct sk_buff *skb,
84 		struct x25_address *called_addr,
85 		struct x25_address *calling_addr)
86 {
87 	unsigned char len;
88 	int needed;
89 	int rc;
90 
91 	if (!pskb_may_pull(skb, 1)) {
92 		/* packet has no address block */
93 		rc = 0;
94 		goto empty;
95 	}
96 
97 	len = *skb->data;
98 	needed = 1 + ((len >> 4) + (len & 0x0f) + 1) / 2;
99 
100 	if (!pskb_may_pull(skb, needed)) {
101 		/* packet is too short to hold the addresses it claims
102 		   to hold */
103 		rc = -1;
104 		goto empty;
105 	}
106 
107 	return x25_addr_ntoa(skb->data, called_addr, calling_addr);
108 
109 empty:
110 	*called_addr->x25_addr = 0;
111 	*calling_addr->x25_addr = 0;
112 
113 	return rc;
114 }
115 
116 
x25_addr_ntoa(unsigned char * p,struct x25_address * called_addr,struct x25_address * calling_addr)117 int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
118 		  struct x25_address *calling_addr)
119 {
120 	unsigned int called_len, calling_len;
121 	char *called, *calling;
122 	unsigned int i;
123 
124 	called_len  = (*p >> 0) & 0x0F;
125 	calling_len = (*p >> 4) & 0x0F;
126 
127 	called  = called_addr->x25_addr;
128 	calling = calling_addr->x25_addr;
129 	p++;
130 
131 	for (i = 0; i < (called_len + calling_len); i++) {
132 		if (i < called_len) {
133 			if (i % 2 != 0) {
134 				*called++ = ((*p >> 0) & 0x0F) + '0';
135 				p++;
136 			} else {
137 				*called++ = ((*p >> 4) & 0x0F) + '0';
138 			}
139 		} else {
140 			if (i % 2 != 0) {
141 				*calling++ = ((*p >> 0) & 0x0F) + '0';
142 				p++;
143 			} else {
144 				*calling++ = ((*p >> 4) & 0x0F) + '0';
145 			}
146 		}
147 	}
148 
149 	*called = *calling = '\0';
150 
151 	return 1 + (called_len + calling_len + 1) / 2;
152 }
153 
x25_addr_aton(unsigned char * p,struct x25_address * called_addr,struct x25_address * calling_addr)154 int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
155 		  struct x25_address *calling_addr)
156 {
157 	unsigned int called_len, calling_len;
158 	char *called, *calling;
159 	int i;
160 
161 	called  = called_addr->x25_addr;
162 	calling = calling_addr->x25_addr;
163 
164 	called_len  = strlen(called);
165 	calling_len = strlen(calling);
166 
167 	*p++ = (calling_len << 4) | (called_len << 0);
168 
169 	for (i = 0; i < (called_len + calling_len); i++) {
170 		if (i < called_len) {
171 			if (i % 2 != 0) {
172 				*p |= (*called++ - '0') << 0;
173 				p++;
174 			} else {
175 				*p = 0x00;
176 				*p |= (*called++ - '0') << 4;
177 			}
178 		} else {
179 			if (i % 2 != 0) {
180 				*p |= (*calling++ - '0') << 0;
181 				p++;
182 			} else {
183 				*p = 0x00;
184 				*p |= (*calling++ - '0') << 4;
185 			}
186 		}
187 	}
188 
189 	return 1 + (called_len + calling_len + 1) / 2;
190 }
191 
192 /*
193  *	Socket removal during an interrupt is now safe.
194  */
x25_remove_socket(struct sock * sk)195 static void x25_remove_socket(struct sock *sk)
196 {
197 	write_lock_bh(&x25_list_lock);
198 	sk_del_node_init(sk);
199 	write_unlock_bh(&x25_list_lock);
200 }
201 
202 /*
203  *	Kill all bound sockets on a dropped device.
204  */
x25_kill_by_device(struct net_device * dev)205 static void x25_kill_by_device(struct net_device *dev)
206 {
207 	struct sock *s;
208 
209 	write_lock_bh(&x25_list_lock);
210 
211 	sk_for_each(s, &x25_list)
212 		if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
213 			x25_disconnect(s, ENETUNREACH, 0, 0);
214 
215 	write_unlock_bh(&x25_list_lock);
216 }
217 
218 /*
219  *	Handle device status changes.
220  */
x25_device_event(struct notifier_block * this,unsigned long event,void * ptr)221 static int x25_device_event(struct notifier_block *this, unsigned long event,
222 			    void *ptr)
223 {
224 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
225 	struct x25_neigh *nb;
226 
227 	if (!net_eq(dev_net(dev), &init_net))
228 		return NOTIFY_DONE;
229 
230 	if (dev->type == ARPHRD_X25
231 #if IS_ENABLED(CONFIG_LLC)
232 	 || dev->type == ARPHRD_ETHER
233 #endif
234 	 ) {
235 		switch (event) {
236 		case NETDEV_UP:
237 			x25_link_device_up(dev);
238 			break;
239 		case NETDEV_GOING_DOWN:
240 			nb = x25_get_neigh(dev);
241 			if (nb) {
242 				x25_terminate_link(nb);
243 				x25_neigh_put(nb);
244 			}
245 			break;
246 		case NETDEV_DOWN:
247 			x25_kill_by_device(dev);
248 			x25_route_device_down(dev);
249 			x25_link_device_down(dev);
250 			break;
251 		}
252 	}
253 
254 	return NOTIFY_DONE;
255 }
256 
257 /*
258  *	Add a socket to the bound sockets list.
259  */
x25_insert_socket(struct sock * sk)260 static void x25_insert_socket(struct sock *sk)
261 {
262 	write_lock_bh(&x25_list_lock);
263 	sk_add_node(sk, &x25_list);
264 	write_unlock_bh(&x25_list_lock);
265 }
266 
267 /*
268  *	Find a socket that wants to accept the Call Request we just
269  *	received. Check the full list for an address/cud match.
270  *	If no cuds match return the next_best thing, an address match.
271  *	Note: if a listening socket has cud set it must only get calls
272  *	with matching cud.
273  */
x25_find_listener(struct x25_address * addr,struct sk_buff * skb)274 static struct sock *x25_find_listener(struct x25_address *addr,
275 					struct sk_buff *skb)
276 {
277 	struct sock *s;
278 	struct sock *next_best;
279 
280 	read_lock_bh(&x25_list_lock);
281 	next_best = NULL;
282 
283 	sk_for_each(s, &x25_list)
284 		if ((!strcmp(addr->x25_addr,
285 			x25_sk(s)->source_addr.x25_addr) ||
286 				!strcmp(x25_sk(s)->source_addr.x25_addr,
287 					null_x25_address.x25_addr)) &&
288 					s->sk_state == TCP_LISTEN) {
289 			/*
290 			 * Found a listening socket, now check the incoming
291 			 * call user data vs this sockets call user data
292 			 */
293 			if (x25_sk(s)->cudmatchlength > 0 &&
294 				skb->len >= x25_sk(s)->cudmatchlength) {
295 				if((memcmp(x25_sk(s)->calluserdata.cuddata,
296 					skb->data,
297 					x25_sk(s)->cudmatchlength)) == 0) {
298 					sock_hold(s);
299 					goto found;
300 				 }
301 			} else
302 				next_best = s;
303 		}
304 	if (next_best) {
305 		s = next_best;
306 		sock_hold(s);
307 		goto found;
308 	}
309 	s = NULL;
310 found:
311 	read_unlock_bh(&x25_list_lock);
312 	return s;
313 }
314 
315 /*
316  *	Find a connected X.25 socket given my LCI and neighbour.
317  */
__x25_find_socket(unsigned int lci,struct x25_neigh * nb)318 static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
319 {
320 	struct sock *s;
321 
322 	sk_for_each(s, &x25_list)
323 		if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
324 			sock_hold(s);
325 			goto found;
326 		}
327 	s = NULL;
328 found:
329 	return s;
330 }
331 
x25_find_socket(unsigned int lci,struct x25_neigh * nb)332 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
333 {
334 	struct sock *s;
335 
336 	read_lock_bh(&x25_list_lock);
337 	s = __x25_find_socket(lci, nb);
338 	read_unlock_bh(&x25_list_lock);
339 	return s;
340 }
341 
342 /*
343  *	Find a unique LCI for a given device.
344  */
x25_new_lci(struct x25_neigh * nb)345 static unsigned int x25_new_lci(struct x25_neigh *nb)
346 {
347 	unsigned int lci = 1;
348 	struct sock *sk;
349 
350 	while ((sk = x25_find_socket(lci, nb)) != NULL) {
351 		sock_put(sk);
352 		if (++lci == 4096) {
353 			lci = 0;
354 			break;
355 		}
356 		cond_resched();
357 	}
358 
359 	return lci;
360 }
361 
362 /*
363  *	Deferred destroy.
364  */
365 static void __x25_destroy_socket(struct sock *);
366 
367 /*
368  *	handler for deferred kills.
369  */
x25_destroy_timer(struct timer_list * t)370 static void x25_destroy_timer(struct timer_list *t)
371 {
372 	struct sock *sk = from_timer(sk, t, sk_timer);
373 
374 	x25_destroy_socket_from_timer(sk);
375 }
376 
377 /*
378  *	This is called from user mode and the timers. Thus it protects itself
379  *	against interrupt users but doesn't worry about being called during
380  *	work. Once it is removed from the queue no interrupt or bottom half
381  *	will touch it and we are (fairly 8-) ) safe.
382  *	Not static as it's used by the timer
383  */
__x25_destroy_socket(struct sock * sk)384 static void __x25_destroy_socket(struct sock *sk)
385 {
386 	struct sk_buff *skb;
387 
388 	x25_stop_heartbeat(sk);
389 	x25_stop_timer(sk);
390 
391 	x25_remove_socket(sk);
392 	x25_clear_queues(sk);		/* Flush the queues */
393 
394 	while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
395 		if (skb->sk != sk) {		/* A pending connection */
396 			/*
397 			 * Queue the unaccepted socket for death
398 			 */
399 			skb->sk->sk_state = TCP_LISTEN;
400 			sock_set_flag(skb->sk, SOCK_DEAD);
401 			x25_start_heartbeat(skb->sk);
402 			x25_sk(skb->sk)->state = X25_STATE_0;
403 		}
404 
405 		kfree_skb(skb);
406 	}
407 
408 	if (sk_has_allocations(sk)) {
409 		/* Defer: outstanding buffers */
410 		sk->sk_timer.expires  = jiffies + 10 * HZ;
411 		sk->sk_timer.function = x25_destroy_timer;
412 		add_timer(&sk->sk_timer);
413 	} else {
414 		/* drop last reference so sock_put will free */
415 		__sock_put(sk);
416 	}
417 }
418 
x25_destroy_socket_from_timer(struct sock * sk)419 void x25_destroy_socket_from_timer(struct sock *sk)
420 {
421 	sock_hold(sk);
422 	bh_lock_sock(sk);
423 	__x25_destroy_socket(sk);
424 	bh_unlock_sock(sk);
425 	sock_put(sk);
426 }
427 
428 /*
429  *	Handling for system calls applied via the various interfaces to a
430  *	X.25 socket object.
431  */
432 
x25_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)433 static int x25_setsockopt(struct socket *sock, int level, int optname,
434 			  sockptr_t optval, unsigned int optlen)
435 {
436 	int opt;
437 	struct sock *sk = sock->sk;
438 	int rc = -ENOPROTOOPT;
439 
440 	if (level != SOL_X25 || optname != X25_QBITINCL)
441 		goto out;
442 
443 	rc = -EINVAL;
444 	if (optlen < sizeof(int))
445 		goto out;
446 
447 	rc = -EFAULT;
448 	if (copy_from_sockptr(&opt, optval, sizeof(int)))
449 		goto out;
450 
451 	if (opt)
452 		set_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
453 	else
454 		clear_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
455 	rc = 0;
456 out:
457 	return rc;
458 }
459 
x25_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)460 static int x25_getsockopt(struct socket *sock, int level, int optname,
461 			  char __user *optval, int __user *optlen)
462 {
463 	struct sock *sk = sock->sk;
464 	int val, len, rc = -ENOPROTOOPT;
465 
466 	if (level != SOL_X25 || optname != X25_QBITINCL)
467 		goto out;
468 
469 	rc = -EFAULT;
470 	if (get_user(len, optlen))
471 		goto out;
472 
473 	len = min_t(unsigned int, len, sizeof(int));
474 
475 	rc = -EINVAL;
476 	if (len < 0)
477 		goto out;
478 
479 	rc = -EFAULT;
480 	if (put_user(len, optlen))
481 		goto out;
482 
483 	val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
484 	rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
485 out:
486 	return rc;
487 }
488 
x25_listen(struct socket * sock,int backlog)489 static int x25_listen(struct socket *sock, int backlog)
490 {
491 	struct sock *sk = sock->sk;
492 	int rc = -EOPNOTSUPP;
493 
494 	lock_sock(sk);
495 	if (sock->state != SS_UNCONNECTED) {
496 		rc = -EINVAL;
497 		release_sock(sk);
498 		return rc;
499 	}
500 
501 	if (sk->sk_state != TCP_LISTEN) {
502 		memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
503 		sk->sk_max_ack_backlog = backlog;
504 		sk->sk_state           = TCP_LISTEN;
505 		rc = 0;
506 	}
507 	release_sock(sk);
508 
509 	return rc;
510 }
511 
512 static struct proto x25_proto = {
513 	.name	  = "X25",
514 	.owner	  = THIS_MODULE,
515 	.obj_size = sizeof(struct x25_sock),
516 };
517 
x25_alloc_socket(struct net * net,int kern)518 static struct sock *x25_alloc_socket(struct net *net, int kern)
519 {
520 	struct x25_sock *x25;
521 	struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto, kern);
522 
523 	if (!sk)
524 		goto out;
525 
526 	sock_init_data(NULL, sk);
527 
528 	x25 = x25_sk(sk);
529 	skb_queue_head_init(&x25->ack_queue);
530 	skb_queue_head_init(&x25->fragment_queue);
531 	skb_queue_head_init(&x25->interrupt_in_queue);
532 	skb_queue_head_init(&x25->interrupt_out_queue);
533 out:
534 	return sk;
535 }
536 
x25_create(struct net * net,struct socket * sock,int protocol,int kern)537 static int x25_create(struct net *net, struct socket *sock, int protocol,
538 		      int kern)
539 {
540 	struct sock *sk;
541 	struct x25_sock *x25;
542 	int rc = -EAFNOSUPPORT;
543 
544 	if (!net_eq(net, &init_net))
545 		goto out;
546 
547 	rc = -ESOCKTNOSUPPORT;
548 	if (sock->type != SOCK_SEQPACKET)
549 		goto out;
550 
551 	rc = -EINVAL;
552 	if (protocol)
553 		goto out;
554 
555 	rc = -ENOMEM;
556 	if ((sk = x25_alloc_socket(net, kern)) == NULL)
557 		goto out;
558 
559 	x25 = x25_sk(sk);
560 
561 	sock_init_data(sock, sk);
562 
563 	x25_init_timers(sk);
564 
565 	sock->ops    = &x25_proto_ops;
566 	sk->sk_protocol = protocol;
567 	sk->sk_backlog_rcv = x25_backlog_rcv;
568 
569 	x25->t21   = sysctl_x25_call_request_timeout;
570 	x25->t22   = sysctl_x25_reset_request_timeout;
571 	x25->t23   = sysctl_x25_clear_request_timeout;
572 	x25->t2    = sysctl_x25_ack_holdback_timeout;
573 	x25->state = X25_STATE_0;
574 	x25->cudmatchlength = 0;
575 	set_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);	/* normally no cud  */
576 							/* on call accept   */
577 
578 	x25->facilities.winsize_in  = X25_DEFAULT_WINDOW_SIZE;
579 	x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
580 	x25->facilities.pacsize_in  = X25_DEFAULT_PACKET_SIZE;
581 	x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
582 	x25->facilities.throughput  = 0;	/* by default don't negotiate
583 						   throughput */
584 	x25->facilities.reverse     = X25_DEFAULT_REVERSE;
585 	x25->dte_facilities.calling_len = 0;
586 	x25->dte_facilities.called_len = 0;
587 	memset(x25->dte_facilities.called_ae, '\0',
588 			sizeof(x25->dte_facilities.called_ae));
589 	memset(x25->dte_facilities.calling_ae, '\0',
590 			sizeof(x25->dte_facilities.calling_ae));
591 
592 	rc = 0;
593 out:
594 	return rc;
595 }
596 
x25_make_new(struct sock * osk)597 static struct sock *x25_make_new(struct sock *osk)
598 {
599 	struct sock *sk = NULL;
600 	struct x25_sock *x25, *ox25;
601 
602 	if (osk->sk_type != SOCK_SEQPACKET)
603 		goto out;
604 
605 	if ((sk = x25_alloc_socket(sock_net(osk), 0)) == NULL)
606 		goto out;
607 
608 	x25 = x25_sk(sk);
609 
610 	sk->sk_type        = osk->sk_type;
611 	sk->sk_priority    = osk->sk_priority;
612 	sk->sk_protocol    = osk->sk_protocol;
613 	sk->sk_rcvbuf      = osk->sk_rcvbuf;
614 	sk->sk_sndbuf      = osk->sk_sndbuf;
615 	sk->sk_state       = TCP_ESTABLISHED;
616 	sk->sk_backlog_rcv = osk->sk_backlog_rcv;
617 	sock_copy_flags(sk, osk);
618 
619 	ox25 = x25_sk(osk);
620 	x25->t21        = ox25->t21;
621 	x25->t22        = ox25->t22;
622 	x25->t23        = ox25->t23;
623 	x25->t2         = ox25->t2;
624 	x25->flags	= ox25->flags;
625 	x25->facilities = ox25->facilities;
626 	x25->dte_facilities = ox25->dte_facilities;
627 	x25->cudmatchlength = ox25->cudmatchlength;
628 
629 	clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
630 	x25_init_timers(sk);
631 out:
632 	return sk;
633 }
634 
x25_release(struct socket * sock)635 static int x25_release(struct socket *sock)
636 {
637 	struct sock *sk = sock->sk;
638 	struct x25_sock *x25;
639 
640 	if (!sk)
641 		return 0;
642 
643 	x25 = x25_sk(sk);
644 
645 	sock_hold(sk);
646 	lock_sock(sk);
647 	switch (x25->state) {
648 
649 		case X25_STATE_0:
650 		case X25_STATE_2:
651 			x25_disconnect(sk, 0, 0, 0);
652 			__x25_destroy_socket(sk);
653 			goto out;
654 
655 		case X25_STATE_1:
656 		case X25_STATE_3:
657 		case X25_STATE_4:
658 			x25_clear_queues(sk);
659 			x25_write_internal(sk, X25_CLEAR_REQUEST);
660 			x25_start_t23timer(sk);
661 			x25->state = X25_STATE_2;
662 			sk->sk_state	= TCP_CLOSE;
663 			sk->sk_shutdown	|= SEND_SHUTDOWN;
664 			sk->sk_state_change(sk);
665 			sock_set_flag(sk, SOCK_DEAD);
666 			sock_set_flag(sk, SOCK_DESTROY);
667 			break;
668 
669 		case X25_STATE_5:
670 			x25_write_internal(sk, X25_CLEAR_REQUEST);
671 			x25_disconnect(sk, 0, 0, 0);
672 			__x25_destroy_socket(sk);
673 			goto out;
674 	}
675 
676 	sock_orphan(sk);
677 out:
678 	release_sock(sk);
679 	sock_put(sk);
680 	return 0;
681 }
682 
x25_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)683 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
684 {
685 	struct sock *sk = sock->sk;
686 	struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
687 	int len, i, rc = 0;
688 
689 	if (addr_len != sizeof(struct sockaddr_x25) ||
690 	    addr->sx25_family != AF_X25 ||
691 	    strnlen(addr->sx25_addr.x25_addr, X25_ADDR_LEN) == X25_ADDR_LEN) {
692 		rc = -EINVAL;
693 		goto out;
694 	}
695 
696 	/* check for the null_x25_address */
697 	if (strcmp(addr->sx25_addr.x25_addr, null_x25_address.x25_addr)) {
698 
699 		len = strlen(addr->sx25_addr.x25_addr);
700 		for (i = 0; i < len; i++) {
701 			if (!isdigit(addr->sx25_addr.x25_addr[i])) {
702 				rc = -EINVAL;
703 				goto out;
704 			}
705 		}
706 	}
707 
708 	lock_sock(sk);
709 	if (sock_flag(sk, SOCK_ZAPPED)) {
710 		x25_sk(sk)->source_addr = addr->sx25_addr;
711 		x25_insert_socket(sk);
712 		sock_reset_flag(sk, SOCK_ZAPPED);
713 	} else {
714 		rc = -EINVAL;
715 	}
716 	release_sock(sk);
717 	SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
718 out:
719 	return rc;
720 }
721 
x25_wait_for_connection_establishment(struct sock * sk)722 static int x25_wait_for_connection_establishment(struct sock *sk)
723 {
724 	DECLARE_WAITQUEUE(wait, current);
725 	int rc;
726 
727 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
728 	for (;;) {
729 		__set_current_state(TASK_INTERRUPTIBLE);
730 		rc = -ERESTARTSYS;
731 		if (signal_pending(current))
732 			break;
733 		rc = sock_error(sk);
734 		if (rc) {
735 			sk->sk_socket->state = SS_UNCONNECTED;
736 			break;
737 		}
738 		rc = 0;
739 		if (sk->sk_state != TCP_ESTABLISHED) {
740 			release_sock(sk);
741 			schedule();
742 			lock_sock(sk);
743 		} else
744 			break;
745 	}
746 	__set_current_state(TASK_RUNNING);
747 	remove_wait_queue(sk_sleep(sk), &wait);
748 	return rc;
749 }
750 
x25_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)751 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
752 		       int addr_len, int flags)
753 {
754 	struct sock *sk = sock->sk;
755 	struct x25_sock *x25 = x25_sk(sk);
756 	struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
757 	struct x25_route *rt;
758 	int rc = 0;
759 
760 	lock_sock(sk);
761 	if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
762 		sock->state = SS_CONNECTED;
763 		goto out; /* Connect completed during a ERESTARTSYS event */
764 	}
765 
766 	rc = -ECONNREFUSED;
767 	if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
768 		sock->state = SS_UNCONNECTED;
769 		goto out;
770 	}
771 
772 	rc = -EISCONN;	/* No reconnect on a seqpacket socket */
773 	if (sk->sk_state == TCP_ESTABLISHED)
774 		goto out;
775 
776 	rc = -EALREADY;	/* Do nothing if call is already in progress */
777 	if (sk->sk_state == TCP_SYN_SENT)
778 		goto out;
779 
780 	sk->sk_state   = TCP_CLOSE;
781 	sock->state = SS_UNCONNECTED;
782 
783 	rc = -EINVAL;
784 	if (addr_len != sizeof(struct sockaddr_x25) ||
785 	    addr->sx25_family != AF_X25 ||
786 	    strnlen(addr->sx25_addr.x25_addr, X25_ADDR_LEN) == X25_ADDR_LEN)
787 		goto out;
788 
789 	rc = -ENETUNREACH;
790 	rt = x25_get_route(&addr->sx25_addr);
791 	if (!rt)
792 		goto out;
793 
794 	x25->neighbour = x25_get_neigh(rt->dev);
795 	if (!x25->neighbour)
796 		goto out_put_route;
797 
798 	x25_limit_facilities(&x25->facilities, x25->neighbour);
799 
800 	x25->lci = x25_new_lci(x25->neighbour);
801 	if (!x25->lci)
802 		goto out_put_neigh;
803 
804 	rc = -EINVAL;
805 	if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
806 		goto out_put_neigh;
807 
808 	if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
809 		memset(&x25->source_addr, '\0', X25_ADDR_LEN);
810 
811 	x25->dest_addr = addr->sx25_addr;
812 
813 	/* Move to connecting socket, start sending Connect Requests */
814 	sock->state   = SS_CONNECTING;
815 	sk->sk_state  = TCP_SYN_SENT;
816 
817 	x25->state = X25_STATE_1;
818 
819 	x25_write_internal(sk, X25_CALL_REQUEST);
820 
821 	x25_start_heartbeat(sk);
822 	x25_start_t21timer(sk);
823 
824 	/* Now the loop */
825 	rc = -EINPROGRESS;
826 	if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
827 		goto out;
828 
829 	rc = x25_wait_for_connection_establishment(sk);
830 	if (rc)
831 		goto out_put_neigh;
832 
833 	sock->state = SS_CONNECTED;
834 	rc = 0;
835 out_put_neigh:
836 	if (rc && x25->neighbour) {
837 		read_lock_bh(&x25_list_lock);
838 		x25_neigh_put(x25->neighbour);
839 		x25->neighbour = NULL;
840 		read_unlock_bh(&x25_list_lock);
841 		x25->state = X25_STATE_0;
842 	}
843 out_put_route:
844 	x25_route_put(rt);
845 out:
846 	release_sock(sk);
847 	return rc;
848 }
849 
x25_wait_for_data(struct sock * sk,long timeout)850 static int x25_wait_for_data(struct sock *sk, long timeout)
851 {
852 	DECLARE_WAITQUEUE(wait, current);
853 	int rc = 0;
854 
855 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
856 	for (;;) {
857 		__set_current_state(TASK_INTERRUPTIBLE);
858 		if (sk->sk_shutdown & RCV_SHUTDOWN)
859 			break;
860 		rc = -ERESTARTSYS;
861 		if (signal_pending(current))
862 			break;
863 		rc = -EAGAIN;
864 		if (!timeout)
865 			break;
866 		rc = 0;
867 		if (skb_queue_empty(&sk->sk_receive_queue)) {
868 			release_sock(sk);
869 			timeout = schedule_timeout(timeout);
870 			lock_sock(sk);
871 		} else
872 			break;
873 	}
874 	__set_current_state(TASK_RUNNING);
875 	remove_wait_queue(sk_sleep(sk), &wait);
876 	return rc;
877 }
878 
x25_accept(struct socket * sock,struct socket * newsock,int flags,bool kern)879 static int x25_accept(struct socket *sock, struct socket *newsock, int flags,
880 		      bool kern)
881 {
882 	struct sock *sk = sock->sk;
883 	struct sock *newsk;
884 	struct sk_buff *skb;
885 	int rc = -EINVAL;
886 
887 	if (!sk)
888 		goto out;
889 
890 	rc = -EOPNOTSUPP;
891 	if (sk->sk_type != SOCK_SEQPACKET)
892 		goto out;
893 
894 	lock_sock(sk);
895 	rc = -EINVAL;
896 	if (sk->sk_state != TCP_LISTEN)
897 		goto out2;
898 
899 	rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
900 	if (rc)
901 		goto out2;
902 	skb = skb_dequeue(&sk->sk_receive_queue);
903 	rc = -EINVAL;
904 	if (!skb->sk)
905 		goto out2;
906 	newsk		 = skb->sk;
907 	sock_graft(newsk, newsock);
908 
909 	/* Now attach up the new socket */
910 	skb->sk = NULL;
911 	kfree_skb(skb);
912 	sk_acceptq_removed(sk);
913 	newsock->state = SS_CONNECTED;
914 	rc = 0;
915 out2:
916 	release_sock(sk);
917 out:
918 	return rc;
919 }
920 
x25_getname(struct socket * sock,struct sockaddr * uaddr,int peer)921 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
922 		       int peer)
923 {
924 	struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
925 	struct sock *sk = sock->sk;
926 	struct x25_sock *x25 = x25_sk(sk);
927 	int rc = 0;
928 
929 	if (peer) {
930 		if (sk->sk_state != TCP_ESTABLISHED) {
931 			rc = -ENOTCONN;
932 			goto out;
933 		}
934 		sx25->sx25_addr = x25->dest_addr;
935 	} else
936 		sx25->sx25_addr = x25->source_addr;
937 
938 	sx25->sx25_family = AF_X25;
939 	rc = sizeof(*sx25);
940 
941 out:
942 	return rc;
943 }
944 
x25_rx_call_request(struct sk_buff * skb,struct x25_neigh * nb,unsigned int lci)945 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
946 			unsigned int lci)
947 {
948 	struct sock *sk;
949 	struct sock *make;
950 	struct x25_sock *makex25;
951 	struct x25_address source_addr, dest_addr;
952 	struct x25_facilities facilities;
953 	struct x25_dte_facilities dte_facilities;
954 	int len, addr_len, rc;
955 
956 	/*
957 	 *	Remove the LCI and frame type.
958 	 */
959 	skb_pull(skb, X25_STD_MIN_LEN);
960 
961 	/*
962 	 *	Extract the X.25 addresses and convert them to ASCII strings,
963 	 *	and remove them.
964 	 *
965 	 *	Address block is mandatory in call request packets
966 	 */
967 	addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
968 	if (addr_len <= 0)
969 		goto out_clear_request;
970 	skb_pull(skb, addr_len);
971 
972 	/*
973 	 *	Get the length of the facilities, skip past them for the moment
974 	 *	get the call user data because this is needed to determine
975 	 *	the correct listener
976 	 *
977 	 *	Facilities length is mandatory in call request packets
978 	 */
979 	if (!pskb_may_pull(skb, 1))
980 		goto out_clear_request;
981 	len = skb->data[0] + 1;
982 	if (!pskb_may_pull(skb, len))
983 		goto out_clear_request;
984 	skb_pull(skb,len);
985 
986 	/*
987 	 *	Ensure that the amount of call user data is valid.
988 	 */
989 	if (skb->len > X25_MAX_CUD_LEN)
990 		goto out_clear_request;
991 
992 	/*
993 	 *	Get all the call user data so it can be used in
994 	 *	x25_find_listener and skb_copy_from_linear_data up ahead.
995 	 */
996 	if (!pskb_may_pull(skb, skb->len))
997 		goto out_clear_request;
998 
999 	/*
1000 	 *	Find a listener for the particular address/cud pair.
1001 	 */
1002 	sk = x25_find_listener(&source_addr,skb);
1003 	skb_push(skb,len);
1004 
1005 	if (sk != NULL && sk_acceptq_is_full(sk)) {
1006 		goto out_sock_put;
1007 	}
1008 
1009 	/*
1010 	 *	We dont have any listeners for this incoming call.
1011 	 *	Try forwarding it.
1012 	 */
1013 	if (sk == NULL) {
1014 		skb_push(skb, addr_len + X25_STD_MIN_LEN);
1015 		if (sysctl_x25_forward &&
1016 				x25_forward_call(&dest_addr, nb, skb, lci) > 0)
1017 		{
1018 			/* Call was forwarded, dont process it any more */
1019 			kfree_skb(skb);
1020 			rc = 1;
1021 			goto out;
1022 		} else {
1023 			/* No listeners, can't forward, clear the call */
1024 			goto out_clear_request;
1025 		}
1026 	}
1027 
1028 	/*
1029 	 *	Try to reach a compromise on the requested facilities.
1030 	 */
1031 	len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
1032 	if (len == -1)
1033 		goto out_sock_put;
1034 
1035 	/*
1036 	 * current neighbour/link might impose additional limits
1037 	 * on certain facilties
1038 	 */
1039 
1040 	x25_limit_facilities(&facilities, nb);
1041 
1042 	/*
1043 	 *	Try to create a new socket.
1044 	 */
1045 	make = x25_make_new(sk);
1046 	if (!make)
1047 		goto out_sock_put;
1048 
1049 	/*
1050 	 *	Remove the facilities
1051 	 */
1052 	skb_pull(skb, len);
1053 
1054 	skb->sk     = make;
1055 	make->sk_state = TCP_ESTABLISHED;
1056 
1057 	makex25 = x25_sk(make);
1058 	makex25->lci           = lci;
1059 	makex25->dest_addr     = dest_addr;
1060 	makex25->source_addr   = source_addr;
1061 	x25_neigh_hold(nb);
1062 	makex25->neighbour     = nb;
1063 	makex25->facilities    = facilities;
1064 	makex25->dte_facilities= dte_facilities;
1065 	makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
1066 	/* ensure no reverse facil on accept */
1067 	makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
1068 	/* ensure no calling address extension on accept */
1069 	makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
1070 	makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
1071 
1072 	/* Normally all calls are accepted immediately */
1073 	if (test_bit(X25_ACCPT_APPRV_FLAG, &makex25->flags)) {
1074 		x25_write_internal(make, X25_CALL_ACCEPTED);
1075 		makex25->state = X25_STATE_3;
1076 	} else {
1077 		makex25->state = X25_STATE_5;
1078 	}
1079 
1080 	/*
1081 	 *	Incoming Call User Data.
1082 	 */
1083 	skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1084 	makex25->calluserdata.cudlength = skb->len;
1085 
1086 	sk_acceptq_added(sk);
1087 
1088 	x25_insert_socket(make);
1089 
1090 	skb_queue_head(&sk->sk_receive_queue, skb);
1091 
1092 	x25_start_heartbeat(make);
1093 
1094 	if (!sock_flag(sk, SOCK_DEAD))
1095 		sk->sk_data_ready(sk);
1096 	rc = 1;
1097 	sock_put(sk);
1098 out:
1099 	return rc;
1100 out_sock_put:
1101 	sock_put(sk);
1102 out_clear_request:
1103 	rc = 0;
1104 	x25_transmit_clear_request(nb, lci, 0x01);
1105 	goto out;
1106 }
1107 
x25_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)1108 static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1109 {
1110 	struct sock *sk = sock->sk;
1111 	struct x25_sock *x25 = x25_sk(sk);
1112 	DECLARE_SOCKADDR(struct sockaddr_x25 *, usx25, msg->msg_name);
1113 	struct sockaddr_x25 sx25;
1114 	struct sk_buff *skb;
1115 	unsigned char *asmptr;
1116 	int noblock = msg->msg_flags & MSG_DONTWAIT;
1117 	size_t size;
1118 	int qbit = 0, rc = -EINVAL;
1119 
1120 	lock_sock(sk);
1121 	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
1122 		goto out;
1123 
1124 	/* we currently don't support segmented records at the user interface */
1125 	if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1126 		goto out;
1127 
1128 	rc = -EADDRNOTAVAIL;
1129 	if (sock_flag(sk, SOCK_ZAPPED))
1130 		goto out;
1131 
1132 	rc = -EPIPE;
1133 	if (sk->sk_shutdown & SEND_SHUTDOWN) {
1134 		send_sig(SIGPIPE, current, 0);
1135 		goto out;
1136 	}
1137 
1138 	rc = -ENETUNREACH;
1139 	if (!x25->neighbour)
1140 		goto out;
1141 
1142 	if (usx25) {
1143 		rc = -EINVAL;
1144 		if (msg->msg_namelen < sizeof(sx25))
1145 			goto out;
1146 		memcpy(&sx25, usx25, sizeof(sx25));
1147 		rc = -EISCONN;
1148 		if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1149 			goto out;
1150 		rc = -EINVAL;
1151 		if (sx25.sx25_family != AF_X25)
1152 			goto out;
1153 	} else {
1154 		/*
1155 		 *	FIXME 1003.1g - if the socket is like this because
1156 		 *	it has become closed (not started closed) we ought
1157 		 *	to SIGPIPE, EPIPE;
1158 		 */
1159 		rc = -ENOTCONN;
1160 		if (sk->sk_state != TCP_ESTABLISHED)
1161 			goto out;
1162 
1163 		sx25.sx25_family = AF_X25;
1164 		sx25.sx25_addr   = x25->dest_addr;
1165 	}
1166 
1167 	/* Sanity check the packet size */
1168 	if (len > 65535) {
1169 		rc = -EMSGSIZE;
1170 		goto out;
1171 	}
1172 
1173 	SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1174 
1175 	/* Build a packet */
1176 	SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1177 
1178 	if ((msg->msg_flags & MSG_OOB) && len > 32)
1179 		len = 32;
1180 
1181 	size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1182 
1183 	release_sock(sk);
1184 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1185 	lock_sock(sk);
1186 	if (!skb)
1187 		goto out;
1188 	X25_SKB_CB(skb)->flags = msg->msg_flags;
1189 
1190 	skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1191 
1192 	/*
1193 	 *	Put the data on the end
1194 	 */
1195 	SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1196 
1197 	skb_reset_transport_header(skb);
1198 	skb_put(skb, len);
1199 
1200 	rc = memcpy_from_msg(skb_transport_header(skb), msg, len);
1201 	if (rc)
1202 		goto out_kfree_skb;
1203 
1204 	/*
1205 	 *	If the Q BIT Include socket option is in force, the first
1206 	 *	byte of the user data is the logical value of the Q Bit.
1207 	 */
1208 	if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
1209 		if (!pskb_may_pull(skb, 1))
1210 			goto out_kfree_skb;
1211 
1212 		qbit = skb->data[0];
1213 		skb_pull(skb, 1);
1214 	}
1215 
1216 	/*
1217 	 *	Push down the X.25 header
1218 	 */
1219 	SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1220 
1221 	if (msg->msg_flags & MSG_OOB) {
1222 		if (x25->neighbour->extended) {
1223 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
1224 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1225 			*asmptr++ = (x25->lci >> 0) & 0xFF;
1226 			*asmptr++ = X25_INTERRUPT;
1227 		} else {
1228 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
1229 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1230 			*asmptr++ = (x25->lci >> 0) & 0xFF;
1231 			*asmptr++ = X25_INTERRUPT;
1232 		}
1233 	} else {
1234 		if (x25->neighbour->extended) {
1235 			/* Build an Extended X.25 header */
1236 			asmptr    = skb_push(skb, X25_EXT_MIN_LEN);
1237 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1238 			*asmptr++ = (x25->lci >> 0) & 0xFF;
1239 			*asmptr++ = X25_DATA;
1240 			*asmptr++ = X25_DATA;
1241 		} else {
1242 			/* Build an Standard X.25 header */
1243 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
1244 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1245 			*asmptr++ = (x25->lci >> 0) & 0xFF;
1246 			*asmptr++ = X25_DATA;
1247 		}
1248 
1249 		if (qbit)
1250 			skb->data[0] |= X25_Q_BIT;
1251 	}
1252 
1253 	SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1254 	SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1255 
1256 	rc = -ENOTCONN;
1257 	if (sk->sk_state != TCP_ESTABLISHED)
1258 		goto out_kfree_skb;
1259 
1260 	if (msg->msg_flags & MSG_OOB)
1261 		skb_queue_tail(&x25->interrupt_out_queue, skb);
1262 	else {
1263 		rc = x25_output(sk, skb);
1264 		len = rc;
1265 		if (rc < 0)
1266 			kfree_skb(skb);
1267 		else if (test_bit(X25_Q_BIT_FLAG, &x25->flags))
1268 			len++;
1269 	}
1270 
1271 	x25_kick(sk);
1272 	rc = len;
1273 out:
1274 	release_sock(sk);
1275 	return rc;
1276 out_kfree_skb:
1277 	kfree_skb(skb);
1278 	goto out;
1279 }
1280 
1281 
x25_recvmsg(struct socket * sock,struct msghdr * msg,size_t size,int flags)1282 static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1283 		       int flags)
1284 {
1285 	struct sock *sk = sock->sk;
1286 	struct x25_sock *x25 = x25_sk(sk);
1287 	DECLARE_SOCKADDR(struct sockaddr_x25 *, sx25, msg->msg_name);
1288 	size_t copied;
1289 	int qbit, header_len;
1290 	struct sk_buff *skb;
1291 	unsigned char *asmptr;
1292 	int rc = -ENOTCONN;
1293 
1294 	lock_sock(sk);
1295 
1296 	if (x25->neighbour == NULL)
1297 		goto out;
1298 
1299 	header_len = x25->neighbour->extended ?
1300 		X25_EXT_MIN_LEN : X25_STD_MIN_LEN;
1301 
1302 	/*
1303 	 * This works for seqpacket too. The receiver has ordered the queue for
1304 	 * us! We do one quick check first though
1305 	 */
1306 	if (sk->sk_state != TCP_ESTABLISHED)
1307 		goto out;
1308 
1309 	if (flags & MSG_OOB) {
1310 		rc = -EINVAL;
1311 		if (sock_flag(sk, SOCK_URGINLINE) ||
1312 		    !skb_peek(&x25->interrupt_in_queue))
1313 			goto out;
1314 
1315 		skb = skb_dequeue(&x25->interrupt_in_queue);
1316 
1317 		if (!pskb_may_pull(skb, X25_STD_MIN_LEN))
1318 			goto out_free_dgram;
1319 
1320 		skb_pull(skb, X25_STD_MIN_LEN);
1321 
1322 		/*
1323 		 *	No Q bit information on Interrupt data.
1324 		 */
1325 		if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
1326 			asmptr  = skb_push(skb, 1);
1327 			*asmptr = 0x00;
1328 		}
1329 
1330 		msg->msg_flags |= MSG_OOB;
1331 	} else {
1332 		/* Now we can treat all alike */
1333 		release_sock(sk);
1334 		skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1335 					flags & MSG_DONTWAIT, &rc);
1336 		lock_sock(sk);
1337 		if (!skb)
1338 			goto out;
1339 
1340 		if (!pskb_may_pull(skb, header_len))
1341 			goto out_free_dgram;
1342 
1343 		qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1344 
1345 		skb_pull(skb, header_len);
1346 
1347 		if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
1348 			asmptr  = skb_push(skb, 1);
1349 			*asmptr = qbit;
1350 		}
1351 	}
1352 
1353 	skb_reset_transport_header(skb);
1354 	copied = skb->len;
1355 
1356 	if (copied > size) {
1357 		copied = size;
1358 		msg->msg_flags |= MSG_TRUNC;
1359 	}
1360 
1361 	/* Currently, each datagram always contains a complete record */
1362 	msg->msg_flags |= MSG_EOR;
1363 
1364 	rc = skb_copy_datagram_msg(skb, 0, msg, copied);
1365 	if (rc)
1366 		goto out_free_dgram;
1367 
1368 	if (sx25) {
1369 		sx25->sx25_family = AF_X25;
1370 		sx25->sx25_addr   = x25->dest_addr;
1371 		msg->msg_namelen = sizeof(*sx25);
1372 	}
1373 
1374 	x25_check_rbuf(sk);
1375 	rc = copied;
1376 out_free_dgram:
1377 	skb_free_datagram(sk, skb);
1378 out:
1379 	release_sock(sk);
1380 	return rc;
1381 }
1382 
1383 
x25_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)1384 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1385 {
1386 	struct sock *sk = sock->sk;
1387 	struct x25_sock *x25 = x25_sk(sk);
1388 	void __user *argp = (void __user *)arg;
1389 	int rc;
1390 
1391 	switch (cmd) {
1392 	case TIOCOUTQ: {
1393 		int amount;
1394 
1395 		amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1396 		if (amount < 0)
1397 			amount = 0;
1398 		rc = put_user(amount, (unsigned int __user *)argp);
1399 		break;
1400 	}
1401 
1402 	case TIOCINQ: {
1403 		struct sk_buff *skb;
1404 		int amount = 0;
1405 		/*
1406 		 * These two are safe on a single CPU system as
1407 		 * only user tasks fiddle here
1408 		 */
1409 		lock_sock(sk);
1410 		if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1411 			amount = skb->len;
1412 		release_sock(sk);
1413 		rc = put_user(amount, (unsigned int __user *)argp);
1414 		break;
1415 	}
1416 
1417 	case SIOCGIFADDR:
1418 	case SIOCSIFADDR:
1419 	case SIOCGIFDSTADDR:
1420 	case SIOCSIFDSTADDR:
1421 	case SIOCGIFBRDADDR:
1422 	case SIOCSIFBRDADDR:
1423 	case SIOCGIFNETMASK:
1424 	case SIOCSIFNETMASK:
1425 	case SIOCGIFMETRIC:
1426 	case SIOCSIFMETRIC:
1427 		rc = -EINVAL;
1428 		break;
1429 	case SIOCADDRT:
1430 	case SIOCDELRT:
1431 		rc = -EPERM;
1432 		if (!capable(CAP_NET_ADMIN))
1433 			break;
1434 		rc = x25_route_ioctl(cmd, argp);
1435 		break;
1436 	case SIOCX25GSUBSCRIP:
1437 		rc = x25_subscr_ioctl(cmd, argp);
1438 		break;
1439 	case SIOCX25SSUBSCRIP:
1440 		rc = -EPERM;
1441 		if (!capable(CAP_NET_ADMIN))
1442 			break;
1443 		rc = x25_subscr_ioctl(cmd, argp);
1444 		break;
1445 	case SIOCX25GFACILITIES: {
1446 		lock_sock(sk);
1447 		rc = copy_to_user(argp, &x25->facilities,
1448 				  sizeof(x25->facilities))
1449 			? -EFAULT : 0;
1450 		release_sock(sk);
1451 		break;
1452 	}
1453 
1454 	case SIOCX25SFACILITIES: {
1455 		struct x25_facilities facilities;
1456 		rc = -EFAULT;
1457 		if (copy_from_user(&facilities, argp, sizeof(facilities)))
1458 			break;
1459 		rc = -EINVAL;
1460 		lock_sock(sk);
1461 		if (sk->sk_state != TCP_LISTEN &&
1462 		    sk->sk_state != TCP_CLOSE)
1463 			goto out_fac_release;
1464 		if (facilities.pacsize_in < X25_PS16 ||
1465 		    facilities.pacsize_in > X25_PS4096)
1466 			goto out_fac_release;
1467 		if (facilities.pacsize_out < X25_PS16 ||
1468 		    facilities.pacsize_out > X25_PS4096)
1469 			goto out_fac_release;
1470 		if (facilities.winsize_in < 1 ||
1471 		    facilities.winsize_in > 127)
1472 			goto out_fac_release;
1473 		if (facilities.throughput) {
1474 			int out = facilities.throughput & 0xf0;
1475 			int in  = facilities.throughput & 0x0f;
1476 			if (!out)
1477 				facilities.throughput |=
1478 					X25_DEFAULT_THROUGHPUT << 4;
1479 			else if (out < 0x30 || out > 0xD0)
1480 				goto out_fac_release;
1481 			if (!in)
1482 				facilities.throughput |=
1483 					X25_DEFAULT_THROUGHPUT;
1484 			else if (in < 0x03 || in > 0x0D)
1485 				goto out_fac_release;
1486 		}
1487 		if (facilities.reverse &&
1488 		    (facilities.reverse & 0x81) != 0x81)
1489 			goto out_fac_release;
1490 		x25->facilities = facilities;
1491 		rc = 0;
1492 out_fac_release:
1493 		release_sock(sk);
1494 		break;
1495 	}
1496 
1497 	case SIOCX25GDTEFACILITIES: {
1498 		lock_sock(sk);
1499 		rc = copy_to_user(argp, &x25->dte_facilities,
1500 				  sizeof(x25->dte_facilities));
1501 		release_sock(sk);
1502 		if (rc)
1503 			rc = -EFAULT;
1504 		break;
1505 	}
1506 
1507 	case SIOCX25SDTEFACILITIES: {
1508 		struct x25_dte_facilities dtefacs;
1509 		rc = -EFAULT;
1510 		if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1511 			break;
1512 		rc = -EINVAL;
1513 		lock_sock(sk);
1514 		if (sk->sk_state != TCP_LISTEN &&
1515 		    sk->sk_state != TCP_CLOSE)
1516 			goto out_dtefac_release;
1517 		if (dtefacs.calling_len > X25_MAX_AE_LEN)
1518 			goto out_dtefac_release;
1519 		if (dtefacs.called_len > X25_MAX_AE_LEN)
1520 			goto out_dtefac_release;
1521 		x25->dte_facilities = dtefacs;
1522 		rc = 0;
1523 out_dtefac_release:
1524 		release_sock(sk);
1525 		break;
1526 	}
1527 
1528 	case SIOCX25GCALLUSERDATA: {
1529 		lock_sock(sk);
1530 		rc = copy_to_user(argp, &x25->calluserdata,
1531 				  sizeof(x25->calluserdata))
1532 			? -EFAULT : 0;
1533 		release_sock(sk);
1534 		break;
1535 	}
1536 
1537 	case SIOCX25SCALLUSERDATA: {
1538 		struct x25_calluserdata calluserdata;
1539 
1540 		rc = -EFAULT;
1541 		if (copy_from_user(&calluserdata, argp, sizeof(calluserdata)))
1542 			break;
1543 		rc = -EINVAL;
1544 		if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1545 			break;
1546 		lock_sock(sk);
1547 		x25->calluserdata = calluserdata;
1548 		release_sock(sk);
1549 		rc = 0;
1550 		break;
1551 	}
1552 
1553 	case SIOCX25GCAUSEDIAG: {
1554 		lock_sock(sk);
1555 		rc = copy_to_user(argp, &x25->causediag, sizeof(x25->causediag))
1556 			? -EFAULT : 0;
1557 		release_sock(sk);
1558 		break;
1559 	}
1560 
1561 	case SIOCX25SCAUSEDIAG: {
1562 		struct x25_causediag causediag;
1563 		rc = -EFAULT;
1564 		if (copy_from_user(&causediag, argp, sizeof(causediag)))
1565 			break;
1566 		lock_sock(sk);
1567 		x25->causediag = causediag;
1568 		release_sock(sk);
1569 		rc = 0;
1570 		break;
1571 
1572 	}
1573 
1574 	case SIOCX25SCUDMATCHLEN: {
1575 		struct x25_subaddr sub_addr;
1576 		rc = -EINVAL;
1577 		lock_sock(sk);
1578 		if(sk->sk_state != TCP_CLOSE)
1579 			goto out_cud_release;
1580 		rc = -EFAULT;
1581 		if (copy_from_user(&sub_addr, argp,
1582 				   sizeof(sub_addr)))
1583 			goto out_cud_release;
1584 		rc = -EINVAL;
1585 		if (sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1586 			goto out_cud_release;
1587 		x25->cudmatchlength = sub_addr.cudmatchlength;
1588 		rc = 0;
1589 out_cud_release:
1590 		release_sock(sk);
1591 		break;
1592 	}
1593 
1594 	case SIOCX25CALLACCPTAPPRV: {
1595 		rc = -EINVAL;
1596 		lock_sock(sk);
1597 		if (sk->sk_state == TCP_CLOSE) {
1598 			clear_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);
1599 			rc = 0;
1600 		}
1601 		release_sock(sk);
1602 		break;
1603 	}
1604 
1605 	case SIOCX25SENDCALLACCPT:  {
1606 		rc = -EINVAL;
1607 		lock_sock(sk);
1608 		if (sk->sk_state != TCP_ESTABLISHED)
1609 			goto out_sendcallaccpt_release;
1610 		/* must call accptapprv above */
1611 		if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags))
1612 			goto out_sendcallaccpt_release;
1613 		x25_write_internal(sk, X25_CALL_ACCEPTED);
1614 		x25->state = X25_STATE_3;
1615 		rc = 0;
1616 out_sendcallaccpt_release:
1617 		release_sock(sk);
1618 		break;
1619 	}
1620 
1621 	default:
1622 		rc = -ENOIOCTLCMD;
1623 		break;
1624 	}
1625 
1626 	return rc;
1627 }
1628 
1629 static const struct net_proto_family x25_family_ops = {
1630 	.family =	AF_X25,
1631 	.create =	x25_create,
1632 	.owner	=	THIS_MODULE,
1633 };
1634 
1635 #ifdef CONFIG_COMPAT
compat_x25_subscr_ioctl(unsigned int cmd,struct compat_x25_subscrip_struct __user * x25_subscr32)1636 static int compat_x25_subscr_ioctl(unsigned int cmd,
1637 		struct compat_x25_subscrip_struct __user *x25_subscr32)
1638 {
1639 	struct compat_x25_subscrip_struct x25_subscr;
1640 	struct x25_neigh *nb;
1641 	struct net_device *dev;
1642 	int rc = -EINVAL;
1643 
1644 	rc = -EFAULT;
1645 	if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1646 		goto out;
1647 
1648 	rc = -EINVAL;
1649 	dev = x25_dev_get(x25_subscr.device);
1650 	if (dev == NULL)
1651 		goto out;
1652 
1653 	nb = x25_get_neigh(dev);
1654 	if (nb == NULL)
1655 		goto out_dev_put;
1656 
1657 	dev_put(dev);
1658 
1659 	if (cmd == SIOCX25GSUBSCRIP) {
1660 		read_lock_bh(&x25_neigh_list_lock);
1661 		x25_subscr.extended = nb->extended;
1662 		x25_subscr.global_facil_mask = nb->global_facil_mask;
1663 		read_unlock_bh(&x25_neigh_list_lock);
1664 		rc = copy_to_user(x25_subscr32, &x25_subscr,
1665 				sizeof(*x25_subscr32)) ? -EFAULT : 0;
1666 	} else {
1667 		rc = -EINVAL;
1668 		if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1669 			rc = 0;
1670 			write_lock_bh(&x25_neigh_list_lock);
1671 			nb->extended = x25_subscr.extended;
1672 			nb->global_facil_mask = x25_subscr.global_facil_mask;
1673 			write_unlock_bh(&x25_neigh_list_lock);
1674 		}
1675 	}
1676 	x25_neigh_put(nb);
1677 out:
1678 	return rc;
1679 out_dev_put:
1680 	dev_put(dev);
1681 	goto out;
1682 }
1683 
compat_x25_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)1684 static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1685 				unsigned long arg)
1686 {
1687 	void __user *argp = compat_ptr(arg);
1688 	int rc = -ENOIOCTLCMD;
1689 
1690 	switch(cmd) {
1691 	case TIOCOUTQ:
1692 	case TIOCINQ:
1693 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1694 		break;
1695 	case SIOCGIFADDR:
1696 	case SIOCSIFADDR:
1697 	case SIOCGIFDSTADDR:
1698 	case SIOCSIFDSTADDR:
1699 	case SIOCGIFBRDADDR:
1700 	case SIOCSIFBRDADDR:
1701 	case SIOCGIFNETMASK:
1702 	case SIOCSIFNETMASK:
1703 	case SIOCGIFMETRIC:
1704 	case SIOCSIFMETRIC:
1705 		rc = -EINVAL;
1706 		break;
1707 	case SIOCADDRT:
1708 	case SIOCDELRT:
1709 		rc = -EPERM;
1710 		if (!capable(CAP_NET_ADMIN))
1711 			break;
1712 		rc = x25_route_ioctl(cmd, argp);
1713 		break;
1714 	case SIOCX25GSUBSCRIP:
1715 		rc = compat_x25_subscr_ioctl(cmd, argp);
1716 		break;
1717 	case SIOCX25SSUBSCRIP:
1718 		rc = -EPERM;
1719 		if (!capable(CAP_NET_ADMIN))
1720 			break;
1721 		rc = compat_x25_subscr_ioctl(cmd, argp);
1722 		break;
1723 	case SIOCX25GFACILITIES:
1724 	case SIOCX25SFACILITIES:
1725 	case SIOCX25GDTEFACILITIES:
1726 	case SIOCX25SDTEFACILITIES:
1727 	case SIOCX25GCALLUSERDATA:
1728 	case SIOCX25SCALLUSERDATA:
1729 	case SIOCX25GCAUSEDIAG:
1730 	case SIOCX25SCAUSEDIAG:
1731 	case SIOCX25SCUDMATCHLEN:
1732 	case SIOCX25CALLACCPTAPPRV:
1733 	case SIOCX25SENDCALLACCPT:
1734 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1735 		break;
1736 	default:
1737 		rc = -ENOIOCTLCMD;
1738 		break;
1739 	}
1740 	return rc;
1741 }
1742 #endif
1743 
1744 static const struct proto_ops x25_proto_ops = {
1745 	.family =	AF_X25,
1746 	.owner =	THIS_MODULE,
1747 	.release =	x25_release,
1748 	.bind =		x25_bind,
1749 	.connect =	x25_connect,
1750 	.socketpair =	sock_no_socketpair,
1751 	.accept =	x25_accept,
1752 	.getname =	x25_getname,
1753 	.poll =		datagram_poll,
1754 	.ioctl =	x25_ioctl,
1755 #ifdef CONFIG_COMPAT
1756 	.compat_ioctl = compat_x25_ioctl,
1757 #endif
1758 	.gettstamp =	sock_gettstamp,
1759 	.listen =	x25_listen,
1760 	.shutdown =	sock_no_shutdown,
1761 	.setsockopt =	x25_setsockopt,
1762 	.getsockopt =	x25_getsockopt,
1763 	.sendmsg =	x25_sendmsg,
1764 	.recvmsg =	x25_recvmsg,
1765 	.mmap =		sock_no_mmap,
1766 	.sendpage =	sock_no_sendpage,
1767 };
1768 
1769 static struct packet_type x25_packet_type __read_mostly = {
1770 	.type =	cpu_to_be16(ETH_P_X25),
1771 	.func =	x25_lapb_receive_frame,
1772 };
1773 
1774 static struct notifier_block x25_dev_notifier = {
1775 	.notifier_call = x25_device_event,
1776 };
1777 
x25_kill_by_neigh(struct x25_neigh * nb)1778 void x25_kill_by_neigh(struct x25_neigh *nb)
1779 {
1780 	struct sock *s;
1781 
1782 	write_lock_bh(&x25_list_lock);
1783 
1784 	sk_for_each(s, &x25_list) {
1785 		if (x25_sk(s)->neighbour == nb) {
1786 			write_unlock_bh(&x25_list_lock);
1787 			lock_sock(s);
1788 			x25_disconnect(s, ENETUNREACH, 0, 0);
1789 			release_sock(s);
1790 			write_lock_bh(&x25_list_lock);
1791 		}
1792 	}
1793 	write_unlock_bh(&x25_list_lock);
1794 
1795 	/* Remove any related forwards */
1796 	x25_clear_forward_by_dev(nb->dev);
1797 }
1798 
x25_init(void)1799 static int __init x25_init(void)
1800 {
1801 	int rc;
1802 
1803 	rc = proto_register(&x25_proto, 0);
1804 	if (rc)
1805 		goto out;
1806 
1807 	rc = sock_register(&x25_family_ops);
1808 	if (rc)
1809 		goto out_proto;
1810 
1811 	dev_add_pack(&x25_packet_type);
1812 
1813 	rc = register_netdevice_notifier(&x25_dev_notifier);
1814 	if (rc)
1815 		goto out_sock;
1816 
1817 	rc = x25_register_sysctl();
1818 	if (rc)
1819 		goto out_dev;
1820 
1821 	rc = x25_proc_init();
1822 	if (rc)
1823 		goto out_sysctl;
1824 
1825 	pr_info("Linux Version 0.2\n");
1826 
1827 out:
1828 	return rc;
1829 out_sysctl:
1830 	x25_unregister_sysctl();
1831 out_dev:
1832 	unregister_netdevice_notifier(&x25_dev_notifier);
1833 out_sock:
1834 	dev_remove_pack(&x25_packet_type);
1835 	sock_unregister(AF_X25);
1836 out_proto:
1837 	proto_unregister(&x25_proto);
1838 	goto out;
1839 }
1840 module_init(x25_init);
1841 
x25_exit(void)1842 static void __exit x25_exit(void)
1843 {
1844 	x25_proc_exit();
1845 	x25_link_free();
1846 	x25_route_free();
1847 
1848 	x25_unregister_sysctl();
1849 
1850 	unregister_netdevice_notifier(&x25_dev_notifier);
1851 
1852 	dev_remove_pack(&x25_packet_type);
1853 
1854 	sock_unregister(AF_X25);
1855 	proto_unregister(&x25_proto);
1856 }
1857 module_exit(x25_exit);
1858 
1859 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1860 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1861 MODULE_LICENSE("GPL");
1862 MODULE_ALIAS_NETPROTO(PF_X25);
1863