xref: /openbmc/linux/net/x25/af_x25.c (revision c7fd0d48bde943e228e9c28ce971a22d6a1744c4)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	X.25 Packet Layer release 002
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	This is ALPHA test software. This code may break your machine,
51da177e4SLinus Torvalds  *	randomly fail to work with new releases, misbehave and/or generally
61da177e4SLinus Torvalds  *	screw up. It might even work.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This code REQUIRES 2.1.15 or higher
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	This module:
111da177e4SLinus Torvalds  *		This module is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	History
171da177e4SLinus Torvalds  *	X.25 001	Jonathan Naylor	Started coding.
181da177e4SLinus Torvalds  *	X.25 002	Jonathan Naylor	Centralised disconnect handling.
191da177e4SLinus Torvalds  *					New timer architecture.
201da177e4SLinus Torvalds  *	2000-03-11	Henner Eisen	MSG_EOR handling more POSIX compliant.
211da177e4SLinus Torvalds  *	2000-03-22	Daniela Squassoni Allowed disabling/enabling of
221da177e4SLinus Torvalds  *					  facilities negotiation and increased
231da177e4SLinus Torvalds  *					  the throughput upper limit.
241da177e4SLinus Torvalds  *	2000-08-27	Arnaldo C. Melo s/suser/capable/ + micro cleanups
251da177e4SLinus Torvalds  *	2000-09-04	Henner Eisen	Set sock->state in x25_accept().
261da177e4SLinus Torvalds  *					Fixed x25_output() related skb leakage.
271da177e4SLinus Torvalds  *	2000-10-02	Henner Eisen	Made x25_kick() single threaded per socket.
281da177e4SLinus Torvalds  *	2000-10-27	Henner Eisen    MSG_DONTWAIT for fragment allocation.
291da177e4SLinus Torvalds  *	2000-11-14	Henner Eisen    Closing datalink from NETDEV_GOING_DOWN
301da177e4SLinus Torvalds  *	2002-10-06	Arnaldo C. Melo Get rid of cli/sti, move proc stuff to
311da177e4SLinus Torvalds  *					x25_proc.c, using seq_file
32cb65d506SShaun Pereira  *	2005-04-02	Shaun Pereira	Selective sub address matching
33cb65d506SShaun Pereira  *					with call user data
34ebc3f64bSShaun Pereira  *	2005-04-15	Shaun Pereira	Fast select with no restriction on
35ebc3f64bSShaun Pereira  *					response
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include <linux/module.h>
394fc268d2SRandy Dunlap #include <linux/capability.h>
401da177e4SLinus Torvalds #include <linux/errno.h>
411da177e4SLinus Torvalds #include <linux/kernel.h>
421da177e4SLinus Torvalds #include <linux/sched.h>
431da177e4SLinus Torvalds #include <linux/timer.h>
441da177e4SLinus Torvalds #include <linux/string.h>
451da177e4SLinus Torvalds #include <linux/net.h>
461da177e4SLinus Torvalds #include <linux/netdevice.h>
471da177e4SLinus Torvalds #include <linux/if_arp.h>
481da177e4SLinus Torvalds #include <linux/skbuff.h>
495a0e3ad6STejun Heo #include <linux/slab.h>
501da177e4SLinus Torvalds #include <net/sock.h>
51c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
521da177e4SLinus Torvalds #include <asm/uaccess.h>
531da177e4SLinus Torvalds #include <linux/fcntl.h>
541da177e4SLinus Torvalds #include <linux/termios.h>	/* For TIOCINQ/OUTQ */
551da177e4SLinus Torvalds #include <linux/notifier.h>
561da177e4SLinus Torvalds #include <linux/init.h>
571b06e6baSShaun Pereira #include <linux/compat.h>
58a9288525Sandrew hendry #include <linux/ctype.h>
591b06e6baSShaun Pereira 
601da177e4SLinus Torvalds #include <net/x25.h>
611b06e6baSShaun Pereira #include <net/compat.h>
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
641da177e4SLinus Torvalds int sysctl_x25_call_request_timeout    = X25_DEFAULT_T21;
651da177e4SLinus Torvalds int sysctl_x25_reset_request_timeout   = X25_DEFAULT_T22;
661da177e4SLinus Torvalds int sysctl_x25_clear_request_timeout   = X25_DEFAULT_T23;
671da177e4SLinus Torvalds int sysctl_x25_ack_holdback_timeout    = X25_DEFAULT_T2;
6839e21c0dSAndrew Hendry int sysctl_x25_forward                 = 0;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds HLIST_HEAD(x25_list);
711da177e4SLinus Torvalds DEFINE_RWLOCK(x25_list_lock);
721da177e4SLinus Torvalds 
7390ddc4f0SEric Dumazet static const struct proto_ops x25_proto_ops;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static struct x25_address null_x25_address = {"               "};
761da177e4SLinus Torvalds 
771b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
781b06e6baSShaun Pereira struct compat_x25_subscrip_struct {
791b06e6baSShaun Pereira 	char device[200-sizeof(compat_ulong_t)];
801b06e6baSShaun Pereira 	compat_ulong_t global_facil_mask;
811b06e6baSShaun Pereira 	compat_uint_t extended;
821b06e6baSShaun Pereira };
831b06e6baSShaun Pereira #endif
841b06e6baSShaun Pereira 
85f5eb917bSJohn Hughes 
86f5eb917bSJohn Hughes int x25_parse_address_block(struct sk_buff *skb,
87f5eb917bSJohn Hughes 		struct x25_address *called_addr,
88f5eb917bSJohn Hughes 		struct x25_address *calling_addr)
89f5eb917bSJohn Hughes {
90f5eb917bSJohn Hughes 	unsigned char len;
91f5eb917bSJohn Hughes 	int needed;
92f5eb917bSJohn Hughes 	int rc;
93f5eb917bSJohn Hughes 
94f5eb917bSJohn Hughes 	if (skb->len < 1) {
95f5eb917bSJohn Hughes 		/* packet has no address block */
96f5eb917bSJohn Hughes 		rc = 0;
97f5eb917bSJohn Hughes 		goto empty;
98f5eb917bSJohn Hughes 	}
99f5eb917bSJohn Hughes 
100f5eb917bSJohn Hughes 	len = *skb->data;
101f5eb917bSJohn Hughes 	needed = 1 + (len >> 4) + (len & 0x0f);
102f5eb917bSJohn Hughes 
103f5eb917bSJohn Hughes 	if (skb->len < needed) {
104f5eb917bSJohn Hughes 		/* packet is too short to hold the addresses it claims
105f5eb917bSJohn Hughes 		   to hold */
106f5eb917bSJohn Hughes 		rc = -1;
107f5eb917bSJohn Hughes 		goto empty;
108f5eb917bSJohn Hughes 	}
109f5eb917bSJohn Hughes 
110f5eb917bSJohn Hughes 	return x25_addr_ntoa(skb->data, called_addr, calling_addr);
111f5eb917bSJohn Hughes 
112f5eb917bSJohn Hughes empty:
113f5eb917bSJohn Hughes 	*called_addr->x25_addr = 0;
114f5eb917bSJohn Hughes 	*calling_addr->x25_addr = 0;
115f5eb917bSJohn Hughes 
116f5eb917bSJohn Hughes 	return rc;
117f5eb917bSJohn Hughes }
118f5eb917bSJohn Hughes 
119f5eb917bSJohn Hughes 
1201da177e4SLinus Torvalds int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
1211da177e4SLinus Torvalds 		  struct x25_address *calling_addr)
1221da177e4SLinus Torvalds {
1236bf1574eSEric Dumazet 	unsigned int called_len, calling_len;
1241da177e4SLinus Torvalds 	char *called, *calling;
1256bf1574eSEric Dumazet 	unsigned int i;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	called_len  = (*p >> 0) & 0x0F;
1281da177e4SLinus Torvalds 	calling_len = (*p >> 4) & 0x0F;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	called  = called_addr->x25_addr;
1311da177e4SLinus Torvalds 	calling = calling_addr->x25_addr;
1321da177e4SLinus Torvalds 	p++;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	for (i = 0; i < (called_len + calling_len); i++) {
1351da177e4SLinus Torvalds 		if (i < called_len) {
1361da177e4SLinus Torvalds 			if (i % 2 != 0) {
1371da177e4SLinus Torvalds 				*called++ = ((*p >> 0) & 0x0F) + '0';
1381da177e4SLinus Torvalds 				p++;
1391da177e4SLinus Torvalds 			} else {
1401da177e4SLinus Torvalds 				*called++ = ((*p >> 4) & 0x0F) + '0';
1411da177e4SLinus Torvalds 			}
1421da177e4SLinus Torvalds 		} else {
1431da177e4SLinus Torvalds 			if (i % 2 != 0) {
1441da177e4SLinus Torvalds 				*calling++ = ((*p >> 0) & 0x0F) + '0';
1451da177e4SLinus Torvalds 				p++;
1461da177e4SLinus Torvalds 			} else {
1471da177e4SLinus Torvalds 				*calling++ = ((*p >> 4) & 0x0F) + '0';
1481da177e4SLinus Torvalds 			}
1491da177e4SLinus Torvalds 		}
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	*called = *calling = '\0';
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	return 1 + (called_len + calling_len + 1) / 2;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
1581da177e4SLinus Torvalds 		  struct x25_address *calling_addr)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	unsigned int called_len, calling_len;
1611da177e4SLinus Torvalds 	char *called, *calling;
1621da177e4SLinus Torvalds 	int i;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	called  = called_addr->x25_addr;
1651da177e4SLinus Torvalds 	calling = calling_addr->x25_addr;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	called_len  = strlen(called);
1681da177e4SLinus Torvalds 	calling_len = strlen(calling);
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	*p++ = (calling_len << 4) | (called_len << 0);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	for (i = 0; i < (called_len + calling_len); i++) {
1731da177e4SLinus Torvalds 		if (i < called_len) {
1741da177e4SLinus Torvalds 			if (i % 2 != 0) {
1751da177e4SLinus Torvalds 				*p |= (*called++ - '0') << 0;
1761da177e4SLinus Torvalds 				p++;
1771da177e4SLinus Torvalds 			} else {
1781da177e4SLinus Torvalds 				*p = 0x00;
1791da177e4SLinus Torvalds 				*p |= (*called++ - '0') << 4;
1801da177e4SLinus Torvalds 			}
1811da177e4SLinus Torvalds 		} else {
1821da177e4SLinus Torvalds 			if (i % 2 != 0) {
1831da177e4SLinus Torvalds 				*p |= (*calling++ - '0') << 0;
1841da177e4SLinus Torvalds 				p++;
1851da177e4SLinus Torvalds 			} else {
1861da177e4SLinus Torvalds 				*p = 0x00;
1871da177e4SLinus Torvalds 				*p |= (*calling++ - '0') << 4;
1881da177e4SLinus Torvalds 			}
1891da177e4SLinus Torvalds 		}
1901da177e4SLinus Torvalds 	}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	return 1 + (called_len + calling_len + 1) / 2;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /*
1961da177e4SLinus Torvalds  *	Socket removal during an interrupt is now safe.
1971da177e4SLinus Torvalds  */
1981da177e4SLinus Torvalds static void x25_remove_socket(struct sock *sk)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
2011da177e4SLinus Torvalds 	sk_del_node_init(sk);
2021da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /*
2061da177e4SLinus Torvalds  *	Kill all bound sockets on a dropped device.
2071da177e4SLinus Torvalds  */
2081da177e4SLinus Torvalds static void x25_kill_by_device(struct net_device *dev)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	struct sock *s;
2111da177e4SLinus Torvalds 	struct hlist_node *node;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	sk_for_each(s, node, &x25_list)
2161da177e4SLinus Torvalds 		if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
2171da177e4SLinus Torvalds 			x25_disconnect(s, ENETUNREACH, 0, 0);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /*
2231da177e4SLinus Torvalds  *	Handle device status changes.
2241da177e4SLinus Torvalds  */
2251da177e4SLinus Torvalds static int x25_device_event(struct notifier_block *this, unsigned long event,
2261da177e4SLinus Torvalds 			    void *ptr)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	struct net_device *dev = ptr;
2291da177e4SLinus Torvalds 	struct x25_neigh *nb;
2301da177e4SLinus Torvalds 
231721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
232e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
233e9dc8653SEric W. Biederman 
2341da177e4SLinus Torvalds 	if (dev->type == ARPHRD_X25
2351da177e4SLinus Torvalds #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
2361da177e4SLinus Torvalds 	 || dev->type == ARPHRD_ETHER
2371da177e4SLinus Torvalds #endif
2381da177e4SLinus Torvalds 	 ) {
2391da177e4SLinus Torvalds 		switch (event) {
2401da177e4SLinus Torvalds 		case NETDEV_UP:
2411da177e4SLinus Torvalds 			x25_link_device_up(dev);
2421da177e4SLinus Torvalds 			break;
2431da177e4SLinus Torvalds 		case NETDEV_GOING_DOWN:
2441da177e4SLinus Torvalds 			nb = x25_get_neigh(dev);
2451da177e4SLinus Torvalds 			if (nb) {
2461da177e4SLinus Torvalds 				x25_terminate_link(nb);
2471da177e4SLinus Torvalds 				x25_neigh_put(nb);
2481da177e4SLinus Torvalds 			}
2491da177e4SLinus Torvalds 			break;
2501da177e4SLinus Torvalds 		case NETDEV_DOWN:
2511da177e4SLinus Torvalds 			x25_kill_by_device(dev);
2521da177e4SLinus Torvalds 			x25_route_device_down(dev);
2531da177e4SLinus Torvalds 			x25_link_device_down(dev);
2541da177e4SLinus Torvalds 			break;
2551da177e4SLinus Torvalds 		}
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	return NOTIFY_DONE;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds /*
2621da177e4SLinus Torvalds  *	Add a socket to the bound sockets list.
2631da177e4SLinus Torvalds  */
2641da177e4SLinus Torvalds static void x25_insert_socket(struct sock *sk)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
2671da177e4SLinus Torvalds 	sk_add_node(sk, &x25_list);
2681da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /*
2721da177e4SLinus Torvalds  *	Find a socket that wants to accept the Call Request we just
2731da177e4SLinus Torvalds  *	received. Check the full list for an address/cud match.
2741da177e4SLinus Torvalds  *	If no cuds match return the next_best thing, an address match.
2751da177e4SLinus Torvalds  *	Note: if a listening socket has cud set it must only get calls
2761da177e4SLinus Torvalds  *	with matching cud.
2771da177e4SLinus Torvalds  */
278cb65d506SShaun Pereira static struct sock *x25_find_listener(struct x25_address *addr,
279cb65d506SShaun Pereira 					struct sk_buff *skb)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	struct sock *s;
2821da177e4SLinus Torvalds 	struct sock *next_best;
2831da177e4SLinus Torvalds 	struct hlist_node *node;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
2861da177e4SLinus Torvalds 	next_best = NULL;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	sk_for_each(s, node, &x25_list)
2891da177e4SLinus Torvalds 		if ((!strcmp(addr->x25_addr,
2901da177e4SLinus Torvalds 			x25_sk(s)->source_addr.x25_addr) ||
2911da177e4SLinus Torvalds 				!strcmp(addr->x25_addr,
2921da177e4SLinus Torvalds 					null_x25_address.x25_addr)) &&
2931da177e4SLinus Torvalds 					s->sk_state == TCP_LISTEN) {
2941da177e4SLinus Torvalds 			/*
2951da177e4SLinus Torvalds 			 * Found a listening socket, now check the incoming
2961da177e4SLinus Torvalds 			 * call user data vs this sockets call user data
2971da177e4SLinus Torvalds 			 */
298cb65d506SShaun Pereira 			if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
299cb65d506SShaun Pereira 				if((memcmp(x25_sk(s)->calluserdata.cuddata,
300cb65d506SShaun Pereira 					skb->data,
301cb65d506SShaun Pereira 					x25_sk(s)->cudmatchlength)) == 0) {
3021da177e4SLinus Torvalds 					sock_hold(s);
3031da177e4SLinus Torvalds 					goto found;
3041da177e4SLinus Torvalds 				 }
305cb65d506SShaun Pereira 			} else
3061da177e4SLinus Torvalds 				next_best = s;
3071da177e4SLinus Torvalds 		}
3081da177e4SLinus Torvalds 	if (next_best) {
3091da177e4SLinus Torvalds 		s = next_best;
3101da177e4SLinus Torvalds 		sock_hold(s);
3111da177e4SLinus Torvalds 		goto found;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	s = NULL;
3141da177e4SLinus Torvalds found:
3151da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
3161da177e4SLinus Torvalds 	return s;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds /*
3201da177e4SLinus Torvalds  *	Find a connected X.25 socket given my LCI and neighbour.
3211da177e4SLinus Torvalds  */
3221da177e4SLinus Torvalds static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	struct sock *s;
3251da177e4SLinus Torvalds 	struct hlist_node *node;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	sk_for_each(s, node, &x25_list)
3281da177e4SLinus Torvalds 		if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
3291da177e4SLinus Torvalds 			sock_hold(s);
3301da177e4SLinus Torvalds 			goto found;
3311da177e4SLinus Torvalds 		}
3321da177e4SLinus Torvalds 	s = NULL;
3331da177e4SLinus Torvalds found:
3341da177e4SLinus Torvalds 	return s;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	struct sock *s;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
3421da177e4SLinus Torvalds 	s = __x25_find_socket(lci, nb);
3431da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
3441da177e4SLinus Torvalds 	return s;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /*
3481da177e4SLinus Torvalds  *	Find a unique LCI for a given device.
3491da177e4SLinus Torvalds  */
3501da177e4SLinus Torvalds static unsigned int x25_new_lci(struct x25_neigh *nb)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	unsigned int lci = 1;
3531da177e4SLinus Torvalds 	struct sock *sk;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	while ((sk = __x25_find_socket(lci, nb)) != NULL) {
3581da177e4SLinus Torvalds 		sock_put(sk);
3591da177e4SLinus Torvalds 		if (++lci == 4096) {
3601da177e4SLinus Torvalds 			lci = 0;
3611da177e4SLinus Torvalds 			break;
3621da177e4SLinus Torvalds 		}
3631da177e4SLinus Torvalds 	}
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
3661da177e4SLinus Torvalds 	return lci;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds /*
3701da177e4SLinus Torvalds  *	Deferred destroy.
3711da177e4SLinus Torvalds  */
37214ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /*
3751da177e4SLinus Torvalds  *	handler for deferred kills.
3761da177e4SLinus Torvalds  */
3771da177e4SLinus Torvalds static void x25_destroy_timer(unsigned long data)
3781da177e4SLinus Torvalds {
37914ebaf81SDavid S. Miller 	x25_destroy_socket_from_timer((struct sock *)data);
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds /*
3831da177e4SLinus Torvalds  *	This is called from user mode and the timers. Thus it protects itself
3841da177e4SLinus Torvalds  *	against interrupt users but doesn't worry about being called during
3851da177e4SLinus Torvalds  *	work. Once it is removed from the queue no interrupt or bottom half
3861da177e4SLinus Torvalds  *	will touch it and we are (fairly 8-) ) safe.
3871da177e4SLinus Torvalds  *	Not static as it's used by the timer
3881da177e4SLinus Torvalds  */
38914ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *sk)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	struct sk_buff *skb;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	x25_stop_heartbeat(sk);
3941da177e4SLinus Torvalds 	x25_stop_timer(sk);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	x25_remove_socket(sk);
3971da177e4SLinus Torvalds 	x25_clear_queues(sk);		/* Flush the queues */
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
4001da177e4SLinus Torvalds 		if (skb->sk != sk) {		/* A pending connection */
4011da177e4SLinus Torvalds 			/*
4021da177e4SLinus Torvalds 			 * Queue the unaccepted socket for death
4031da177e4SLinus Torvalds 			 */
4042cec6b01Sandrew hendry 			skb->sk->sk_state = TCP_LISTEN;
4051da177e4SLinus Torvalds 			sock_set_flag(skb->sk, SOCK_DEAD);
4061da177e4SLinus Torvalds 			x25_start_heartbeat(skb->sk);
4071da177e4SLinus Torvalds 			x25_sk(skb->sk)->state = X25_STATE_0;
4081da177e4SLinus Torvalds 		}
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 		kfree_skb(skb);
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
413c564039fSEric Dumazet 	if (sk_has_allocations(sk)) {
4141da177e4SLinus Torvalds 		/* Defer: outstanding buffers */
4151da177e4SLinus Torvalds 		sk->sk_timer.expires  = jiffies + 10 * HZ;
4161da177e4SLinus Torvalds 		sk->sk_timer.function = x25_destroy_timer;
4171da177e4SLinus Torvalds 		sk->sk_timer.data = (unsigned long)sk;
4181da177e4SLinus Torvalds 		add_timer(&sk->sk_timer);
4191da177e4SLinus Torvalds 	} else {
4201da177e4SLinus Torvalds 		/* drop last reference so sock_put will free */
4211da177e4SLinus Torvalds 		__sock_put(sk);
4221da177e4SLinus Torvalds 	}
42314ebaf81SDavid S. Miller }
4241da177e4SLinus Torvalds 
42514ebaf81SDavid S. Miller void x25_destroy_socket_from_timer(struct sock *sk)
42614ebaf81SDavid S. Miller {
42714ebaf81SDavid S. Miller 	sock_hold(sk);
42814ebaf81SDavid S. Miller 	bh_lock_sock(sk);
42914ebaf81SDavid S. Miller 	__x25_destroy_socket(sk);
43014ebaf81SDavid S. Miller 	bh_unlock_sock(sk);
43114ebaf81SDavid S. Miller 	sock_put(sk);
43214ebaf81SDavid S. Miller }
43314ebaf81SDavid S. Miller 
4341da177e4SLinus Torvalds /*
4351da177e4SLinus Torvalds  *	Handling for system calls applied via the various interfaces to a
4361da177e4SLinus Torvalds  *	X.25 socket object.
4371da177e4SLinus Torvalds  */
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds static int x25_setsockopt(struct socket *sock, int level, int optname,
440b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	int opt;
4431da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4441da177e4SLinus Torvalds 	int rc = -ENOPROTOOPT;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	if (level != SOL_X25 || optname != X25_QBITINCL)
4471da177e4SLinus Torvalds 		goto out;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	rc = -EINVAL;
4501da177e4SLinus Torvalds 	if (optlen < sizeof(int))
4511da177e4SLinus Torvalds 		goto out;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	rc = -EFAULT;
4541da177e4SLinus Torvalds 	if (get_user(opt, (int __user *)optval))
4551da177e4SLinus Torvalds 		goto out;
4561da177e4SLinus Torvalds 
457cb863ffdSandrew hendry 	if (opt)
458cb863ffdSandrew hendry 		set_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
459cb863ffdSandrew hendry 	else
460cb863ffdSandrew hendry 		clear_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
4611da177e4SLinus Torvalds 	rc = 0;
4621da177e4SLinus Torvalds out:
4631da177e4SLinus Torvalds 	return rc;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds static int x25_getsockopt(struct socket *sock, int level, int optname,
4671da177e4SLinus Torvalds 			  char __user *optval, int __user *optlen)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4701da177e4SLinus Torvalds 	int val, len, rc = -ENOPROTOOPT;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	if (level != SOL_X25 || optname != X25_QBITINCL)
4731da177e4SLinus Torvalds 		goto out;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	rc = -EFAULT;
4761da177e4SLinus Torvalds 	if (get_user(len, optlen))
4771da177e4SLinus Torvalds 		goto out;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	rc = -EINVAL;
4821da177e4SLinus Torvalds 	if (len < 0)
4831da177e4SLinus Torvalds 		goto out;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	rc = -EFAULT;
4861da177e4SLinus Torvalds 	if (put_user(len, optlen))
4871da177e4SLinus Torvalds 		goto out;
4881da177e4SLinus Torvalds 
489cb863ffdSandrew hendry 	val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
4901da177e4SLinus Torvalds 	rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
4911da177e4SLinus Torvalds out:
4921da177e4SLinus Torvalds 	return rc;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds static int x25_listen(struct socket *sock, int backlog)
4961da177e4SLinus Torvalds {
4971da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4981da177e4SLinus Torvalds 	int rc = -EOPNOTSUPP;
4991da177e4SLinus Torvalds 
50025aa4efeSandrew hendry 	lock_sock(sk);
5011da177e4SLinus Torvalds 	if (sk->sk_state != TCP_LISTEN) {
5021da177e4SLinus Torvalds 		memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
5031da177e4SLinus Torvalds 		sk->sk_max_ack_backlog = backlog;
5041da177e4SLinus Torvalds 		sk->sk_state           = TCP_LISTEN;
5051da177e4SLinus Torvalds 		rc = 0;
5061da177e4SLinus Torvalds 	}
50725aa4efeSandrew hendry 	release_sock(sk);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	return rc;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds static struct proto x25_proto = {
5131da177e4SLinus Torvalds 	.name	  = "X25",
5141da177e4SLinus Torvalds 	.owner	  = THIS_MODULE,
5151da177e4SLinus Torvalds 	.obj_size = sizeof(struct x25_sock),
5161da177e4SLinus Torvalds };
5171da177e4SLinus Torvalds 
5181b8d7ae4SEric W. Biederman static struct sock *x25_alloc_socket(struct net *net)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	struct x25_sock *x25;
5216257ff21SPavel Emelyanov 	struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	if (!sk)
5241da177e4SLinus Torvalds 		goto out;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	sock_init_data(NULL, sk);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	x25 = x25_sk(sk);
5291da177e4SLinus Torvalds 	skb_queue_head_init(&x25->ack_queue);
5301da177e4SLinus Torvalds 	skb_queue_head_init(&x25->fragment_queue);
5311da177e4SLinus Torvalds 	skb_queue_head_init(&x25->interrupt_in_queue);
5321da177e4SLinus Torvalds 	skb_queue_head_init(&x25->interrupt_out_queue);
5331da177e4SLinus Torvalds out:
5341da177e4SLinus Torvalds 	return sk;
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds 
5373f378b68SEric Paris static int x25_create(struct net *net, struct socket *sock, int protocol,
5383f378b68SEric Paris 		      int kern)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	struct sock *sk;
5411da177e4SLinus Torvalds 	struct x25_sock *x25;
542b18e7a06Sandrew hendry 	int rc = -EAFNOSUPPORT;
5431da177e4SLinus Torvalds 
54409ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
5451da177e4SLinus Torvalds 		goto out;
5461da177e4SLinus Torvalds 
547b18e7a06Sandrew hendry 	rc = -ESOCKTNOSUPPORT;
548b18e7a06Sandrew hendry 	if (sock->type != SOCK_SEQPACKET)
549b18e7a06Sandrew hendry 		goto out;
550b18e7a06Sandrew hendry 
551b18e7a06Sandrew hendry 	rc = -EINVAL;
552b18e7a06Sandrew hendry 	if (protocol)
553b18e7a06Sandrew hendry 		goto out;
554b18e7a06Sandrew hendry 
555b18e7a06Sandrew hendry 	rc = -ENOBUFS;
5561b8d7ae4SEric W. Biederman 	if ((sk = x25_alloc_socket(net)) == NULL)
5571da177e4SLinus Torvalds 		goto out;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	x25 = x25_sk(sk);
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	sock_init_data(sock, sk);
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	x25_init_timers(sk);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	sock->ops    = &x25_proto_ops;
5661da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
5671da177e4SLinus Torvalds 	sk->sk_backlog_rcv = x25_backlog_rcv;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	x25->t21   = sysctl_x25_call_request_timeout;
5701da177e4SLinus Torvalds 	x25->t22   = sysctl_x25_reset_request_timeout;
5711da177e4SLinus Torvalds 	x25->t23   = sysctl_x25_clear_request_timeout;
5721da177e4SLinus Torvalds 	x25->t2    = sysctl_x25_ack_holdback_timeout;
5731da177e4SLinus Torvalds 	x25->state = X25_STATE_0;
574cb65d506SShaun Pereira 	x25->cudmatchlength = 0;
57537cda787Sandrew hendry 	set_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);	/* normally no cud  */
576ebc3f64bSShaun Pereira 							/* on call accept   */
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	x25->facilities.winsize_in  = X25_DEFAULT_WINDOW_SIZE;
5791da177e4SLinus Torvalds 	x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
5801da177e4SLinus Torvalds 	x25->facilities.pacsize_in  = X25_DEFAULT_PACKET_SIZE;
5811da177e4SLinus Torvalds 	x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
582ddd0451fSJohn Hughes 	x25->facilities.throughput  = 0;	/* by default don't negotiate
583ddd0451fSJohn Hughes 						   throughput */
5841da177e4SLinus Torvalds 	x25->facilities.reverse     = X25_DEFAULT_REVERSE;
585a64b7b93SShaun Pereira 	x25->dte_facilities.calling_len = 0;
586a64b7b93SShaun Pereira 	x25->dte_facilities.called_len = 0;
587a64b7b93SShaun Pereira 	memset(x25->dte_facilities.called_ae, '\0',
588a64b7b93SShaun Pereira 			sizeof(x25->dte_facilities.called_ae));
589a64b7b93SShaun Pereira 	memset(x25->dte_facilities.calling_ae, '\0',
590a64b7b93SShaun Pereira 			sizeof(x25->dte_facilities.calling_ae));
591a64b7b93SShaun Pereira 
5921da177e4SLinus Torvalds 	rc = 0;
5931da177e4SLinus Torvalds out:
5941da177e4SLinus Torvalds 	return rc;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds static struct sock *x25_make_new(struct sock *osk)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	struct sock *sk = NULL;
6001da177e4SLinus Torvalds 	struct x25_sock *x25, *ox25;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	if (osk->sk_type != SOCK_SEQPACKET)
6031da177e4SLinus Torvalds 		goto out;
6041da177e4SLinus Torvalds 
6053b1e0a65SYOSHIFUJI Hideaki 	if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
6061da177e4SLinus Torvalds 		goto out;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	x25 = x25_sk(sk);
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	sk->sk_type        = osk->sk_type;
6111da177e4SLinus Torvalds 	sk->sk_priority    = osk->sk_priority;
6121da177e4SLinus Torvalds 	sk->sk_protocol    = osk->sk_protocol;
6131da177e4SLinus Torvalds 	sk->sk_rcvbuf      = osk->sk_rcvbuf;
6141da177e4SLinus Torvalds 	sk->sk_sndbuf      = osk->sk_sndbuf;
6151da177e4SLinus Torvalds 	sk->sk_state       = TCP_ESTABLISHED;
6161da177e4SLinus Torvalds 	sk->sk_backlog_rcv = osk->sk_backlog_rcv;
617a20a8554SShaun Pereira 	sock_copy_flags(sk, osk);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	ox25 = x25_sk(osk);
6201da177e4SLinus Torvalds 	x25->t21        = ox25->t21;
6211da177e4SLinus Torvalds 	x25->t22        = ox25->t22;
6221da177e4SLinus Torvalds 	x25->t23        = ox25->t23;
6231da177e4SLinus Torvalds 	x25->t2         = ox25->t2;
624cb863ffdSandrew hendry 	x25->flags	= ox25->flags;
6251da177e4SLinus Torvalds 	x25->facilities = ox25->facilities;
626a64b7b93SShaun Pereira 	x25->dte_facilities = ox25->dte_facilities;
627cb65d506SShaun Pereira 	x25->cudmatchlength = ox25->cudmatchlength;
6281da177e4SLinus Torvalds 
629b7792e34Sandrew hendry 	clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
6301da177e4SLinus Torvalds 	x25_init_timers(sk);
6311da177e4SLinus Torvalds out:
6321da177e4SLinus Torvalds 	return sk;
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds static int x25_release(struct socket *sock)
6361da177e4SLinus Torvalds {
6371da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6381da177e4SLinus Torvalds 	struct x25_sock *x25;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	if (!sk)
64177b22836SArnd Bergmann 		return 0;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	x25 = x25_sk(sk);
6441da177e4SLinus Torvalds 
64577b22836SArnd Bergmann 	sock_hold(sk);
64677b22836SArnd Bergmann 	lock_sock(sk);
6471da177e4SLinus Torvalds 	switch (x25->state) {
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 		case X25_STATE_0:
6501da177e4SLinus Torvalds 		case X25_STATE_2:
6511da177e4SLinus Torvalds 			x25_disconnect(sk, 0, 0, 0);
65277b22836SArnd Bergmann 			__x25_destroy_socket(sk);
6531da177e4SLinus Torvalds 			goto out;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 		case X25_STATE_1:
6561da177e4SLinus Torvalds 		case X25_STATE_3:
6571da177e4SLinus Torvalds 		case X25_STATE_4:
6581da177e4SLinus Torvalds 			x25_clear_queues(sk);
6591da177e4SLinus Torvalds 			x25_write_internal(sk, X25_CLEAR_REQUEST);
6601da177e4SLinus Torvalds 			x25_start_t23timer(sk);
6611da177e4SLinus Torvalds 			x25->state = X25_STATE_2;
6621da177e4SLinus Torvalds 			sk->sk_state	= TCP_CLOSE;
6631da177e4SLinus Torvalds 			sk->sk_shutdown	|= SEND_SHUTDOWN;
6641da177e4SLinus Torvalds 			sk->sk_state_change(sk);
6651da177e4SLinus Torvalds 			sock_set_flag(sk, SOCK_DEAD);
6661da177e4SLinus Torvalds 			sock_set_flag(sk, SOCK_DESTROY);
6671da177e4SLinus Torvalds 			break;
6681da177e4SLinus Torvalds 	}
6691da177e4SLinus Torvalds 
670c751e4f8SDavid S. Miller 	sock_orphan(sk);
6711da177e4SLinus Torvalds out:
67277b22836SArnd Bergmann 	release_sock(sk);
67377b22836SArnd Bergmann 	sock_put(sk);
6741da177e4SLinus Torvalds 	return 0;
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6801da177e4SLinus Torvalds 	struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
681a9288525Sandrew hendry 	int len, i, rc = 0;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_ZAPPED) ||
6841da177e4SLinus Torvalds 	    addr_len != sizeof(struct sockaddr_x25) ||
68591774904SArnd Bergmann 	    addr->sx25_family != AF_X25) {
68691774904SArnd Bergmann 		rc = -EINVAL;
68791774904SArnd Bergmann 		goto out;
68891774904SArnd Bergmann 	}
6891da177e4SLinus Torvalds 
690a9288525Sandrew hendry 	len = strlen(addr->sx25_addr.x25_addr);
691a9288525Sandrew hendry 	for (i = 0; i < len; i++) {
692a9288525Sandrew hendry 		if (!isdigit(addr->sx25_addr.x25_addr[i])) {
693a9288525Sandrew hendry 			rc = -EINVAL;
694a9288525Sandrew hendry 			goto out;
695a9288525Sandrew hendry 		}
696a9288525Sandrew hendry 	}
697a9288525Sandrew hendry 
69890c27297Sandrew hendry 	lock_sock(sk);
6991da177e4SLinus Torvalds 	x25_sk(sk)->source_addr = addr->sx25_addr;
7001da177e4SLinus Torvalds 	x25_insert_socket(sk);
7011da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
70290c27297Sandrew hendry 	release_sock(sk);
7031da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
70491774904SArnd Bergmann out:
70591774904SArnd Bergmann 	return rc;
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds static int x25_wait_for_connection_establishment(struct sock *sk)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
7111da177e4SLinus Torvalds 	int rc;
7121da177e4SLinus Torvalds 
713aa395145SEric Dumazet 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
7141da177e4SLinus Torvalds 	for (;;) {
7151da177e4SLinus Torvalds 		__set_current_state(TASK_INTERRUPTIBLE);
7161da177e4SLinus Torvalds 		rc = -ERESTARTSYS;
7171da177e4SLinus Torvalds 		if (signal_pending(current))
7181da177e4SLinus Torvalds 			break;
7191da177e4SLinus Torvalds 		rc = sock_error(sk);
7201da177e4SLinus Torvalds 		if (rc) {
7211da177e4SLinus Torvalds 			sk->sk_socket->state = SS_UNCONNECTED;
7221da177e4SLinus Torvalds 			break;
7231da177e4SLinus Torvalds 		}
7241da177e4SLinus Torvalds 		rc = 0;
7251da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED) {
7261da177e4SLinus Torvalds 			release_sock(sk);
7271da177e4SLinus Torvalds 			schedule();
7281da177e4SLinus Torvalds 			lock_sock(sk);
7291da177e4SLinus Torvalds 		} else
7301da177e4SLinus Torvalds 			break;
7311da177e4SLinus Torvalds 	}
7321da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
733aa395145SEric Dumazet 	remove_wait_queue(sk_sleep(sk), &wait);
7341da177e4SLinus Torvalds 	return rc;
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
7381da177e4SLinus Torvalds 		       int addr_len, int flags)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
7411da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
7421da177e4SLinus Torvalds 	struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
7431da177e4SLinus Torvalds 	struct x25_route *rt;
7441da177e4SLinus Torvalds 	int rc = 0;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	lock_sock(sk);
7471da177e4SLinus Torvalds 	if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
7481da177e4SLinus Torvalds 		sock->state = SS_CONNECTED;
7491da177e4SLinus Torvalds 		goto out; /* Connect completed during a ERESTARTSYS event */
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	rc = -ECONNREFUSED;
7531da177e4SLinus Torvalds 	if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
7541da177e4SLinus Torvalds 		sock->state = SS_UNCONNECTED;
7551da177e4SLinus Torvalds 		goto out;
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	rc = -EISCONN;	/* No reconnect on a seqpacket socket */
7591da177e4SLinus Torvalds 	if (sk->sk_state == TCP_ESTABLISHED)
7601da177e4SLinus Torvalds 		goto out;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	sk->sk_state   = TCP_CLOSE;
7631da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	rc = -EINVAL;
7661da177e4SLinus Torvalds 	if (addr_len != sizeof(struct sockaddr_x25) ||
7671da177e4SLinus Torvalds 	    addr->sx25_family != AF_X25)
7681da177e4SLinus Torvalds 		goto out;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	rc = -ENETUNREACH;
7711da177e4SLinus Torvalds 	rt = x25_get_route(&addr->sx25_addr);
7721da177e4SLinus Torvalds 	if (!rt)
7731da177e4SLinus Torvalds 		goto out;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	x25->neighbour = x25_get_neigh(rt->dev);
7761da177e4SLinus Torvalds 	if (!x25->neighbour)
7771da177e4SLinus Torvalds 		goto out_put_route;
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	x25_limit_facilities(&x25->facilities, x25->neighbour);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	x25->lci = x25_new_lci(x25->neighbour);
7821da177e4SLinus Torvalds 	if (!x25->lci)
7831da177e4SLinus Torvalds 		goto out_put_neigh;
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	rc = -EINVAL;
7861da177e4SLinus Torvalds 	if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
7871da177e4SLinus Torvalds 		goto out_put_neigh;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
7901da177e4SLinus Torvalds 		memset(&x25->source_addr, '\0', X25_ADDR_LEN);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	x25->dest_addr = addr->sx25_addr;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	/* Move to connecting socket, start sending Connect Requests */
7951da177e4SLinus Torvalds 	sock->state   = SS_CONNECTING;
7961da177e4SLinus Torvalds 	sk->sk_state  = TCP_SYN_SENT;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	x25->state = X25_STATE_1;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	x25_write_internal(sk, X25_CALL_REQUEST);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	x25_start_heartbeat(sk);
8031da177e4SLinus Torvalds 	x25_start_t21timer(sk);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	/* Now the loop */
8061da177e4SLinus Torvalds 	rc = -EINPROGRESS;
8071da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
8081da177e4SLinus Torvalds 		goto out_put_neigh;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	rc = x25_wait_for_connection_establishment(sk);
8111da177e4SLinus Torvalds 	if (rc)
8121da177e4SLinus Torvalds 		goto out_put_neigh;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	sock->state = SS_CONNECTED;
8151da177e4SLinus Torvalds 	rc = 0;
8161da177e4SLinus Torvalds out_put_neigh:
8171da177e4SLinus Torvalds 	if (rc)
8181da177e4SLinus Torvalds 		x25_neigh_put(x25->neighbour);
8191da177e4SLinus Torvalds out_put_route:
8201da177e4SLinus Torvalds 	x25_route_put(rt);
8211da177e4SLinus Torvalds out:
8221da177e4SLinus Torvalds 	release_sock(sk);
8231da177e4SLinus Torvalds 	return rc;
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
826bac37ec8SShaun Pereira static int x25_wait_for_data(struct sock *sk, long timeout)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
8291da177e4SLinus Torvalds 	int rc = 0;
8301da177e4SLinus Torvalds 
831aa395145SEric Dumazet 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
8321da177e4SLinus Torvalds 	for (;;) {
8331da177e4SLinus Torvalds 		__set_current_state(TASK_INTERRUPTIBLE);
8341da177e4SLinus Torvalds 		if (sk->sk_shutdown & RCV_SHUTDOWN)
8351da177e4SLinus Torvalds 			break;
8361da177e4SLinus Torvalds 		rc = -ERESTARTSYS;
8371da177e4SLinus Torvalds 		if (signal_pending(current))
8381da177e4SLinus Torvalds 			break;
8391da177e4SLinus Torvalds 		rc = -EAGAIN;
8401da177e4SLinus Torvalds 		if (!timeout)
8411da177e4SLinus Torvalds 			break;
8421da177e4SLinus Torvalds 		rc = 0;
8431da177e4SLinus Torvalds 		if (skb_queue_empty(&sk->sk_receive_queue)) {
8441da177e4SLinus Torvalds 			release_sock(sk);
8451da177e4SLinus Torvalds 			timeout = schedule_timeout(timeout);
8461da177e4SLinus Torvalds 			lock_sock(sk);
8471da177e4SLinus Torvalds 		} else
8481da177e4SLinus Torvalds 			break;
8491da177e4SLinus Torvalds 	}
8501da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
851aa395145SEric Dumazet 	remove_wait_queue(sk_sleep(sk), &wait);
8521da177e4SLinus Torvalds 	return rc;
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8581da177e4SLinus Torvalds 	struct sock *newsk;
8591da177e4SLinus Torvalds 	struct sk_buff *skb;
8601da177e4SLinus Torvalds 	int rc = -EINVAL;
8611da177e4SLinus Torvalds 
862141646ceSAndrew Hendry 	if (!sk)
8631da177e4SLinus Torvalds 		goto out;
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	rc = -EOPNOTSUPP;
8661da177e4SLinus Torvalds 	if (sk->sk_type != SOCK_SEQPACKET)
8671da177e4SLinus Torvalds 		goto out;
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	lock_sock(sk);
870141646ceSAndrew Hendry 	rc = -EINVAL;
871141646ceSAndrew Hendry 	if (sk->sk_state != TCP_LISTEN)
872141646ceSAndrew Hendry 		goto out2;
873141646ceSAndrew Hendry 
8741da177e4SLinus Torvalds 	rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
8751da177e4SLinus Torvalds 	if (rc)
8761da177e4SLinus Torvalds 		goto out2;
8771da177e4SLinus Torvalds 	skb = skb_dequeue(&sk->sk_receive_queue);
8781da177e4SLinus Torvalds 	rc = -EINVAL;
8791da177e4SLinus Torvalds 	if (!skb->sk)
8801da177e4SLinus Torvalds 		goto out2;
8811da177e4SLinus Torvalds 	newsk		 = skb->sk;
882b61d38e0SDavid S. Miller 	sock_graft(newsk, newsock);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	/* Now attach up the new socket */
8851da177e4SLinus Torvalds 	skb->sk = NULL;
8861da177e4SLinus Torvalds 	kfree_skb(skb);
8871da177e4SLinus Torvalds 	sk->sk_ack_backlog--;
8881da177e4SLinus Torvalds 	newsock->state = SS_CONNECTED;
8891da177e4SLinus Torvalds 	rc = 0;
8901da177e4SLinus Torvalds out2:
8911da177e4SLinus Torvalds 	release_sock(sk);
8921da177e4SLinus Torvalds out:
8931da177e4SLinus Torvalds 	return rc;
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
8971da177e4SLinus Torvalds 		       int *uaddr_len, int peer)
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
9001da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
9011da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
90291774904SArnd Bergmann 	int rc = 0;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	if (peer) {
90591774904SArnd Bergmann 		if (sk->sk_state != TCP_ESTABLISHED) {
90691774904SArnd Bergmann 			rc = -ENOTCONN;
90791774904SArnd Bergmann 			goto out;
90891774904SArnd Bergmann 		}
9091da177e4SLinus Torvalds 		sx25->sx25_addr = x25->dest_addr;
9101da177e4SLinus Torvalds 	} else
9111da177e4SLinus Torvalds 		sx25->sx25_addr = x25->source_addr;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	sx25->sx25_family = AF_X25;
9141da177e4SLinus Torvalds 	*uaddr_len = sizeof(*sx25);
9151da177e4SLinus Torvalds 
91691774904SArnd Bergmann out:
91791774904SArnd Bergmann 	return rc;
91891774904SArnd Bergmann }
91991774904SArnd Bergmann 
9201da177e4SLinus Torvalds int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
9211da177e4SLinus Torvalds 			unsigned int lci)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds 	struct sock *sk;
9241da177e4SLinus Torvalds 	struct sock *make;
9251da177e4SLinus Torvalds 	struct x25_sock *makex25;
9261da177e4SLinus Torvalds 	struct x25_address source_addr, dest_addr;
9271da177e4SLinus Torvalds 	struct x25_facilities facilities;
928a64b7b93SShaun Pereira 	struct x25_dte_facilities dte_facilities;
92995a9dc43SAndrew Hendry 	int len, addr_len, rc;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	/*
9321da177e4SLinus Torvalds 	 *	Remove the LCI and frame type.
9331da177e4SLinus Torvalds 	 */
9341da177e4SLinus Torvalds 	skb_pull(skb, X25_STD_MIN_LEN);
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	/*
9371da177e4SLinus Torvalds 	 *	Extract the X.25 addresses and convert them to ASCII strings,
9381da177e4SLinus Torvalds 	 *	and remove them.
939f5eb917bSJohn Hughes 	 *
940f5eb917bSJohn Hughes 	 *	Address block is mandatory in call request packets
9411da177e4SLinus Torvalds 	 */
942f5eb917bSJohn Hughes 	addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
943f5eb917bSJohn Hughes 	if (addr_len <= 0)
944f5eb917bSJohn Hughes 		goto out_clear_request;
94595a9dc43SAndrew Hendry 	skb_pull(skb, addr_len);
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	/*
9481da177e4SLinus Torvalds 	 *	Get the length of the facilities, skip past them for the moment
9491da177e4SLinus Torvalds 	 *	get the call user data because this is needed to determine
9501da177e4SLinus Torvalds 	 *	the correct listener
951f5eb917bSJohn Hughes 	 *
952f5eb917bSJohn Hughes 	 *	Facilities length is mandatory in call request packets
9531da177e4SLinus Torvalds 	 */
954f5eb917bSJohn Hughes 	if (skb->len < 1)
955f5eb917bSJohn Hughes 		goto out_clear_request;
9561da177e4SLinus Torvalds 	len = skb->data[0] + 1;
957f5eb917bSJohn Hughes 	if (skb->len < len)
958f5eb917bSJohn Hughes 		goto out_clear_request;
9591da177e4SLinus Torvalds 	skb_pull(skb,len);
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	/*
962*c7fd0d48SMatthew Daley 	 *	Ensure that the amount of call user data is valid.
963*c7fd0d48SMatthew Daley 	 */
964*c7fd0d48SMatthew Daley 	if (skb->len > X25_MAX_CUD_LEN)
965*c7fd0d48SMatthew Daley 		goto out_clear_request;
966*c7fd0d48SMatthew Daley 
967*c7fd0d48SMatthew Daley 	/*
9681da177e4SLinus Torvalds 	 *	Find a listener for the particular address/cud pair.
9691da177e4SLinus Torvalds 	 */
970cb65d506SShaun Pereira 	sk = x25_find_listener(&source_addr,skb);
971cb65d506SShaun Pereira 	skb_push(skb,len);
9721da177e4SLinus Torvalds 
97395a9dc43SAndrew Hendry 	if (sk != NULL && sk_acceptq_is_full(sk)) {
97495a9dc43SAndrew Hendry 		goto out_sock_put;
97595a9dc43SAndrew Hendry 	}
97695a9dc43SAndrew Hendry 
9771da177e4SLinus Torvalds 	/*
97895a9dc43SAndrew Hendry 	 *	We dont have any listeners for this incoming call.
97995a9dc43SAndrew Hendry 	 *	Try forwarding it.
9801da177e4SLinus Torvalds 	 */
98195a9dc43SAndrew Hendry 	if (sk == NULL) {
98295a9dc43SAndrew Hendry 		skb_push(skb, addr_len + X25_STD_MIN_LEN);
98339e21c0dSAndrew Hendry 		if (sysctl_x25_forward &&
98439e21c0dSAndrew Hendry 				x25_forward_call(&dest_addr, nb, skb, lci) > 0)
98595a9dc43SAndrew Hendry 		{
98695a9dc43SAndrew Hendry 			/* Call was forwarded, dont process it any more */
98795a9dc43SAndrew Hendry 			kfree_skb(skb);
98895a9dc43SAndrew Hendry 			rc = 1;
98995a9dc43SAndrew Hendry 			goto out;
99095a9dc43SAndrew Hendry 		} else {
99195a9dc43SAndrew Hendry 			/* No listeners, can't forward, clear the call */
9921da177e4SLinus Torvalds 			goto out_clear_request;
99395a9dc43SAndrew Hendry 		}
99495a9dc43SAndrew Hendry 	}
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	/*
9971da177e4SLinus Torvalds 	 *	Try to reach a compromise on the requested facilities.
9981da177e4SLinus Torvalds 	 */
999a64b7b93SShaun Pereira 	len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
1000a64b7b93SShaun Pereira 	if (len == -1)
10011da177e4SLinus Torvalds 		goto out_sock_put;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	/*
10041da177e4SLinus Torvalds 	 * current neighbour/link might impose additional limits
10051da177e4SLinus Torvalds 	 * on certain facilties
10061da177e4SLinus Torvalds 	 */
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	x25_limit_facilities(&facilities, nb);
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	/*
10111da177e4SLinus Torvalds 	 *	Try to create a new socket.
10121da177e4SLinus Torvalds 	 */
10131da177e4SLinus Torvalds 	make = x25_make_new(sk);
10141da177e4SLinus Torvalds 	if (!make)
10151da177e4SLinus Torvalds 		goto out_sock_put;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	/*
10181da177e4SLinus Torvalds 	 *	Remove the facilities
10191da177e4SLinus Torvalds 	 */
10201da177e4SLinus Torvalds 	skb_pull(skb, len);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	skb->sk     = make;
10231da177e4SLinus Torvalds 	make->sk_state = TCP_ESTABLISHED;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	makex25 = x25_sk(make);
10261da177e4SLinus Torvalds 	makex25->lci           = lci;
10271da177e4SLinus Torvalds 	makex25->dest_addr     = dest_addr;
10281da177e4SLinus Torvalds 	makex25->source_addr   = source_addr;
10291da177e4SLinus Torvalds 	makex25->neighbour     = nb;
10301da177e4SLinus Torvalds 	makex25->facilities    = facilities;
1031a64b7b93SShaun Pereira 	makex25->dte_facilities= dte_facilities;
10321da177e4SLinus Torvalds 	makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
1033cb65d506SShaun Pereira 	/* ensure no reverse facil on accept */
1034cb65d506SShaun Pereira 	makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
1035a64b7b93SShaun Pereira 	/* ensure no calling address extension on accept */
1036a64b7b93SShaun Pereira 	makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
1037cb65d506SShaun Pereira 	makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
10381da177e4SLinus Torvalds 
103937cda787Sandrew hendry 	/* Normally all calls are accepted immediately */
104037cda787Sandrew hendry 	if (test_bit(X25_ACCPT_APPRV_FLAG, &makex25->flags)) {
10411da177e4SLinus Torvalds 		x25_write_internal(make, X25_CALL_ACCEPTED);
10421da177e4SLinus Torvalds 		makex25->state = X25_STATE_3;
1043ebc3f64bSShaun Pereira 	}
10441da177e4SLinus Torvalds 
1045cb65d506SShaun Pereira 	/*
1046cb65d506SShaun Pereira 	 *	Incoming Call User Data.
1047cb65d506SShaun Pereira 	 */
1048d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1049cb65d506SShaun Pereira 	makex25->calluserdata.cudlength = skb->len;
1050cb65d506SShaun Pereira 
10511da177e4SLinus Torvalds 	sk->sk_ack_backlog++;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	x25_insert_socket(make);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	skb_queue_head(&sk->sk_receive_queue, skb);
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	x25_start_heartbeat(make);
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD))
10601da177e4SLinus Torvalds 		sk->sk_data_ready(sk, skb->len);
10611da177e4SLinus Torvalds 	rc = 1;
10621da177e4SLinus Torvalds 	sock_put(sk);
10631da177e4SLinus Torvalds out:
10641da177e4SLinus Torvalds 	return rc;
10651da177e4SLinus Torvalds out_sock_put:
10661da177e4SLinus Torvalds 	sock_put(sk);
10671da177e4SLinus Torvalds out_clear_request:
10681da177e4SLinus Torvalds 	rc = 0;
10691da177e4SLinus Torvalds 	x25_transmit_clear_request(nb, lci, 0x01);
10701da177e4SLinus Torvalds 	goto out;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
10741da177e4SLinus Torvalds 		       struct msghdr *msg, size_t len)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
10771da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
10781da177e4SLinus Torvalds 	struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
10791da177e4SLinus Torvalds 	struct sockaddr_x25 sx25;
10801da177e4SLinus Torvalds 	struct sk_buff *skb;
10811da177e4SLinus Torvalds 	unsigned char *asmptr;
10821da177e4SLinus Torvalds 	int noblock = msg->msg_flags & MSG_DONTWAIT;
10831da177e4SLinus Torvalds 	size_t size;
10841da177e4SLinus Torvalds 	int qbit = 0, rc = -EINVAL;
10851da177e4SLinus Torvalds 
108677b22836SArnd Bergmann 	lock_sock(sk);
10871da177e4SLinus Torvalds 	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
10881da177e4SLinus Torvalds 		goto out;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	/* we currently don't support segmented records at the user interface */
10911da177e4SLinus Torvalds 	if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
10921da177e4SLinus Torvalds 		goto out;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	rc = -EADDRNOTAVAIL;
10951da177e4SLinus Torvalds 	if (sock_flag(sk, SOCK_ZAPPED))
10961da177e4SLinus Torvalds 		goto out;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	rc = -EPIPE;
10991da177e4SLinus Torvalds 	if (sk->sk_shutdown & SEND_SHUTDOWN) {
11001da177e4SLinus Torvalds 		send_sig(SIGPIPE, current, 0);
11011da177e4SLinus Torvalds 		goto out;
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	rc = -ENETUNREACH;
11051da177e4SLinus Torvalds 	if (!x25->neighbour)
11061da177e4SLinus Torvalds 		goto out;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	if (usx25) {
11091da177e4SLinus Torvalds 		rc = -EINVAL;
11101da177e4SLinus Torvalds 		if (msg->msg_namelen < sizeof(sx25))
11111da177e4SLinus Torvalds 			goto out;
11121da177e4SLinus Torvalds 		memcpy(&sx25, usx25, sizeof(sx25));
11131da177e4SLinus Torvalds 		rc = -EISCONN;
11141da177e4SLinus Torvalds 		if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
11151da177e4SLinus Torvalds 			goto out;
11161da177e4SLinus Torvalds 		rc = -EINVAL;
11171da177e4SLinus Torvalds 		if (sx25.sx25_family != AF_X25)
11181da177e4SLinus Torvalds 			goto out;
11191da177e4SLinus Torvalds 	} else {
11201da177e4SLinus Torvalds 		/*
11211da177e4SLinus Torvalds 		 *	FIXME 1003.1g - if the socket is like this because
11221da177e4SLinus Torvalds 		 *	it has become closed (not started closed) we ought
11231da177e4SLinus Torvalds 		 *	to SIGPIPE, EPIPE;
11241da177e4SLinus Torvalds 		 */
11251da177e4SLinus Torvalds 		rc = -ENOTCONN;
11261da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
11271da177e4SLinus Torvalds 			goto out;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 		sx25.sx25_family = AF_X25;
11301da177e4SLinus Torvalds 		sx25.sx25_addr   = x25->dest_addr;
11311da177e4SLinus Torvalds 	}
11321da177e4SLinus Torvalds 
113383e0bbcbSAlan Cox 	/* Sanity check the packet size */
113483e0bbcbSAlan Cox 	if (len > 65535) {
113583e0bbcbSAlan Cox 		rc = -EMSGSIZE;
113683e0bbcbSAlan Cox 		goto out;
113783e0bbcbSAlan Cox 	}
113883e0bbcbSAlan Cox 
11391da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	/* Build a packet */
11421da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	if ((msg->msg_flags & MSG_OOB) && len > 32)
11451da177e4SLinus Torvalds 		len = 32;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
11481da177e4SLinus Torvalds 
114977b22836SArnd Bergmann 	release_sock(sk);
11501da177e4SLinus Torvalds 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
115177b22836SArnd Bergmann 	lock_sock(sk);
11521da177e4SLinus Torvalds 	if (!skb)
11531da177e4SLinus Torvalds 		goto out;
11541da177e4SLinus Torvalds 	X25_SKB_CB(skb)->flags = msg->msg_flags;
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	/*
11591da177e4SLinus Torvalds 	 *	Put the data on the end
11601da177e4SLinus Torvalds 	 */
11611da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
11621da177e4SLinus Torvalds 
1163eeeb0374SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
1164eeeb0374SArnaldo Carvalho de Melo 	skb_put(skb, len);
11651da177e4SLinus Torvalds 
1166eeeb0374SArnaldo Carvalho de Melo 	rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
11671da177e4SLinus Torvalds 	if (rc)
11681da177e4SLinus Torvalds 		goto out_kfree_skb;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	/*
11711da177e4SLinus Torvalds 	 *	If the Q BIT Include socket option is in force, the first
11721da177e4SLinus Torvalds 	 *	byte of the user data is the logical value of the Q Bit.
11731da177e4SLinus Torvalds 	 */
1174cb863ffdSandrew hendry 	if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
11751da177e4SLinus Torvalds 		qbit = skb->data[0];
11761da177e4SLinus Torvalds 		skb_pull(skb, 1);
11771da177e4SLinus Torvalds 	}
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	/*
11801da177e4SLinus Torvalds 	 *	Push down the X.25 header
11811da177e4SLinus Torvalds 	 */
11821da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB) {
11851da177e4SLinus Torvalds 		if (x25->neighbour->extended) {
11861da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
11871da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
11881da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
11891da177e4SLinus Torvalds 			*asmptr++ = X25_INTERRUPT;
11901da177e4SLinus Torvalds 		} else {
11911da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
11921da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
11931da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
11941da177e4SLinus Torvalds 			*asmptr++ = X25_INTERRUPT;
11951da177e4SLinus Torvalds 		}
11961da177e4SLinus Torvalds 	} else {
11971da177e4SLinus Torvalds 		if (x25->neighbour->extended) {
11981da177e4SLinus Torvalds 			/* Build an Extended X.25 header */
11991da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_EXT_MIN_LEN);
12001da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
12011da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
12021da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
12031da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
12041da177e4SLinus Torvalds 		} else {
12051da177e4SLinus Torvalds 			/* Build an Standard X.25 header */
12061da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
12071da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
12081da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
12091da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
12101da177e4SLinus Torvalds 		}
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 		if (qbit)
12131da177e4SLinus Torvalds 			skb->data[0] |= X25_Q_BIT;
12141da177e4SLinus Torvalds 	}
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
12171da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	rc = -ENOTCONN;
12201da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED)
12211da177e4SLinus Torvalds 		goto out_kfree_skb;
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
12241da177e4SLinus Torvalds 		skb_queue_tail(&x25->interrupt_out_queue, skb);
12251da177e4SLinus Torvalds 	else {
12268db09f26SRoel Kluin 		rc = x25_output(sk, skb);
12278db09f26SRoel Kluin 		len = rc;
12288db09f26SRoel Kluin 		if (rc < 0)
12291da177e4SLinus Torvalds 			kfree_skb(skb);
1230cb863ffdSandrew hendry 		else if (test_bit(X25_Q_BIT_FLAG, &x25->flags))
12311da177e4SLinus Torvalds 			len++;
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 	x25_kick(sk);
12351da177e4SLinus Torvalds 	rc = len;
12361da177e4SLinus Torvalds out:
123777b22836SArnd Bergmann 	release_sock(sk);
12381da177e4SLinus Torvalds 	return rc;
12391da177e4SLinus Torvalds out_kfree_skb:
12401da177e4SLinus Torvalds 	kfree_skb(skb);
12411da177e4SLinus Torvalds 	goto out;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
12461da177e4SLinus Torvalds 		       struct msghdr *msg, size_t size,
12471da177e4SLinus Torvalds 		       int flags)
12481da177e4SLinus Torvalds {
12491da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
12501da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
12511da177e4SLinus Torvalds 	struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
12521da177e4SLinus Torvalds 	size_t copied;
12531da177e4SLinus Torvalds 	int qbit;
12541da177e4SLinus Torvalds 	struct sk_buff *skb;
12551da177e4SLinus Torvalds 	unsigned char *asmptr;
12561da177e4SLinus Torvalds 	int rc = -ENOTCONN;
12571da177e4SLinus Torvalds 
125877b22836SArnd Bergmann 	lock_sock(sk);
12591da177e4SLinus Torvalds 	/*
12601da177e4SLinus Torvalds 	 * This works for seqpacket too. The receiver has ordered the queue for
12611da177e4SLinus Torvalds 	 * us! We do one quick check first though
12621da177e4SLinus Torvalds 	 */
12631da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED)
12641da177e4SLinus Torvalds 		goto out;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	if (flags & MSG_OOB) {
12671da177e4SLinus Torvalds 		rc = -EINVAL;
12681da177e4SLinus Torvalds 		if (sock_flag(sk, SOCK_URGINLINE) ||
12691da177e4SLinus Torvalds 		    !skb_peek(&x25->interrupt_in_queue))
12701da177e4SLinus Torvalds 			goto out;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 		skb = skb_dequeue(&x25->interrupt_in_queue);
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 		skb_pull(skb, X25_STD_MIN_LEN);
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 		/*
12771da177e4SLinus Torvalds 		 *	No Q bit information on Interrupt data.
12781da177e4SLinus Torvalds 		 */
1279cb863ffdSandrew hendry 		if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
12801da177e4SLinus Torvalds 			asmptr  = skb_push(skb, 1);
12811da177e4SLinus Torvalds 			*asmptr = 0x00;
12821da177e4SLinus Torvalds 		}
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 		msg->msg_flags |= MSG_OOB;
12851da177e4SLinus Torvalds 	} else {
12861da177e4SLinus Torvalds 		/* Now we can treat all alike */
128777b22836SArnd Bergmann 		release_sock(sk);
12881da177e4SLinus Torvalds 		skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
12891da177e4SLinus Torvalds 					flags & MSG_DONTWAIT, &rc);
129077b22836SArnd Bergmann 		lock_sock(sk);
12911da177e4SLinus Torvalds 		if (!skb)
12921da177e4SLinus Torvalds 			goto out;
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 		qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 		skb_pull(skb, x25->neighbour->extended ?
12971da177e4SLinus Torvalds 				X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
12981da177e4SLinus Torvalds 
1299cb863ffdSandrew hendry 		if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
13001da177e4SLinus Torvalds 			asmptr  = skb_push(skb, 1);
13011da177e4SLinus Torvalds 			*asmptr = qbit;
13021da177e4SLinus Torvalds 		}
13031da177e4SLinus Torvalds 	}
13041da177e4SLinus Torvalds 
1305badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
13061da177e4SLinus Torvalds 	copied = skb->len;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	if (copied > size) {
13091da177e4SLinus Torvalds 		copied = size;
13101da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
13111da177e4SLinus Torvalds 	}
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	/* Currently, each datagram always contains a complete record */
13141da177e4SLinus Torvalds 	msg->msg_flags |= MSG_EOR;
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
13171da177e4SLinus Torvalds 	if (rc)
13181da177e4SLinus Torvalds 		goto out_free_dgram;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	if (sx25) {
13211da177e4SLinus Torvalds 		sx25->sx25_family = AF_X25;
13221da177e4SLinus Torvalds 		sx25->sx25_addr   = x25->dest_addr;
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	msg->msg_namelen = sizeof(struct sockaddr_x25);
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	x25_check_rbuf(sk);
13281da177e4SLinus Torvalds 	rc = copied;
13291da177e4SLinus Torvalds out_free_dgram:
13301da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
13311da177e4SLinus Torvalds out:
133277b22836SArnd Bergmann 	release_sock(sk);
13331da177e4SLinus Torvalds 	return rc;
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
13381da177e4SLinus Torvalds {
13391da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
13401da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
13411da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
13421da177e4SLinus Torvalds 	int rc;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	switch (cmd) {
13451da177e4SLinus Torvalds 	case TIOCOUTQ: {
134670be998cSandrew hendry 		int amount;
134731e6d363SEric Dumazet 
134870be998cSandrew hendry 		amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
13491da177e4SLinus Torvalds 		if (amount < 0)
13501da177e4SLinus Torvalds 			amount = 0;
13511da177e4SLinus Torvalds 		rc = put_user(amount, (unsigned int __user *)argp);
13521da177e4SLinus Torvalds 		break;
13531da177e4SLinus Torvalds 	}
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	case TIOCINQ: {
13561da177e4SLinus Torvalds 		struct sk_buff *skb;
13571da177e4SLinus Torvalds 		int amount = 0;
13581da177e4SLinus Torvalds 		/*
13591da177e4SLinus Torvalds 		 * These two are safe on a single CPU system as
13601da177e4SLinus Torvalds 		 * only user tasks fiddle here
13611da177e4SLinus Torvalds 		 */
136254aafbd4Sandrew hendry 		lock_sock(sk);
13631da177e4SLinus Torvalds 		if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
13641da177e4SLinus Torvalds 			amount = skb->len;
136554aafbd4Sandrew hendry 		release_sock(sk);
13661da177e4SLinus Torvalds 		rc = put_user(amount, (unsigned int __user *)argp);
13671da177e4SLinus Torvalds 		break;
13681da177e4SLinus Torvalds 	}
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	case SIOCGSTAMP:
13711da177e4SLinus Torvalds 		rc = -EINVAL;
13721da177e4SLinus Torvalds 		if (sk)
13731da177e4SLinus Torvalds 			rc = sock_get_timestamp(sk,
13741da177e4SLinus Torvalds 						(struct timeval __user *)argp);
13751da177e4SLinus Torvalds 		break;
1376ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
1377ae40eb1eSEric Dumazet 		rc = -EINVAL;
1378ae40eb1eSEric Dumazet 		if (sk)
1379ae40eb1eSEric Dumazet 			rc = sock_get_timestampns(sk,
1380ae40eb1eSEric Dumazet 					(struct timespec __user *)argp);
1381ae40eb1eSEric Dumazet 		break;
13821da177e4SLinus Torvalds 	case SIOCGIFADDR:
13831da177e4SLinus Torvalds 	case SIOCSIFADDR:
13841da177e4SLinus Torvalds 	case SIOCGIFDSTADDR:
13851da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
13861da177e4SLinus Torvalds 	case SIOCGIFBRDADDR:
13871da177e4SLinus Torvalds 	case SIOCSIFBRDADDR:
13881da177e4SLinus Torvalds 	case SIOCGIFNETMASK:
13891da177e4SLinus Torvalds 	case SIOCSIFNETMASK:
13901da177e4SLinus Torvalds 	case SIOCGIFMETRIC:
13911da177e4SLinus Torvalds 	case SIOCSIFMETRIC:
13921da177e4SLinus Torvalds 		rc = -EINVAL;
13931da177e4SLinus Torvalds 		break;
13941da177e4SLinus Torvalds 	case SIOCADDRT:
13951da177e4SLinus Torvalds 	case SIOCDELRT:
13961da177e4SLinus Torvalds 		rc = -EPERM;
13971da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
13981da177e4SLinus Torvalds 			break;
13991da177e4SLinus Torvalds 		rc = x25_route_ioctl(cmd, argp);
14001da177e4SLinus Torvalds 		break;
14011da177e4SLinus Torvalds 	case SIOCX25GSUBSCRIP:
14021da177e4SLinus Torvalds 		rc = x25_subscr_ioctl(cmd, argp);
14031da177e4SLinus Torvalds 		break;
14041da177e4SLinus Torvalds 	case SIOCX25SSUBSCRIP:
14051da177e4SLinus Torvalds 		rc = -EPERM;
14061da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
14071da177e4SLinus Torvalds 			break;
14081da177e4SLinus Torvalds 		rc = x25_subscr_ioctl(cmd, argp);
14091da177e4SLinus Torvalds 		break;
14101da177e4SLinus Torvalds 	case SIOCX25GFACILITIES: {
1411f90de660Sandrew hendry 		lock_sock(sk);
1412f90de660Sandrew hendry 		rc = copy_to_user(argp, &x25->facilities,
1413f90de660Sandrew hendry 				  sizeof(x25->facilities))
1414f90de660Sandrew hendry 			? -EFAULT : 0;
1415f90de660Sandrew hendry 		release_sock(sk);
14161da177e4SLinus Torvalds 		break;
14171da177e4SLinus Torvalds 	}
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	case SIOCX25SFACILITIES: {
14201da177e4SLinus Torvalds 		struct x25_facilities facilities;
14211da177e4SLinus Torvalds 		rc = -EFAULT;
1422fddc5f3eSJoe Perches 		if (copy_from_user(&facilities, argp, sizeof(facilities)))
14231da177e4SLinus Torvalds 			break;
14241da177e4SLinus Torvalds 		rc = -EINVAL;
1425f90de660Sandrew hendry 		lock_sock(sk);
14261da177e4SLinus Torvalds 		if (sk->sk_state != TCP_LISTEN &&
14271da177e4SLinus Torvalds 		    sk->sk_state != TCP_CLOSE)
1428f90de660Sandrew hendry 			goto out_fac_release;
14291da177e4SLinus Torvalds 		if (facilities.pacsize_in < X25_PS16 ||
14301da177e4SLinus Torvalds 		    facilities.pacsize_in > X25_PS4096)
1431f90de660Sandrew hendry 			goto out_fac_release;
14321da177e4SLinus Torvalds 		if (facilities.pacsize_out < X25_PS16 ||
14331da177e4SLinus Torvalds 		    facilities.pacsize_out > X25_PS4096)
1434f90de660Sandrew hendry 			goto out_fac_release;
14351da177e4SLinus Torvalds 		if (facilities.winsize_in < 1 ||
14361da177e4SLinus Torvalds 		    facilities.winsize_in > 127)
1437f90de660Sandrew hendry 			goto out_fac_release;
1438ddd0451fSJohn Hughes 		if (facilities.throughput) {
1439ddd0451fSJohn Hughes 			int out = facilities.throughput & 0xf0;
1440ddd0451fSJohn Hughes 			int in  = facilities.throughput & 0x0f;
1441ddd0451fSJohn Hughes 			if (!out)
1442ddd0451fSJohn Hughes 				facilities.throughput |=
1443ddd0451fSJohn Hughes 					X25_DEFAULT_THROUGHPUT << 4;
1444ddd0451fSJohn Hughes 			else if (out < 0x30 || out > 0xD0)
1445f90de660Sandrew hendry 				goto out_fac_release;
1446ddd0451fSJohn Hughes 			if (!in)
1447ddd0451fSJohn Hughes 				facilities.throughput |=
1448ddd0451fSJohn Hughes 					X25_DEFAULT_THROUGHPUT;
1449ddd0451fSJohn Hughes 			else if (in < 0x03 || in > 0x0D)
1450f90de660Sandrew hendry 				goto out_fac_release;
1451ddd0451fSJohn Hughes 		}
1452ebc3f64bSShaun Pereira 		if (facilities.reverse &&
145306a96b33Sroel kluin 		    (facilities.reverse & 0x81) != 0x81)
1454f90de660Sandrew hendry 			goto out_fac_release;
14551da177e4SLinus Torvalds 		x25->facilities = facilities;
14561da177e4SLinus Torvalds 		rc = 0;
1457f90de660Sandrew hendry out_fac_release:
1458f90de660Sandrew hendry 		release_sock(sk);
14591da177e4SLinus Torvalds 		break;
14601da177e4SLinus Torvalds 	}
14611da177e4SLinus Torvalds 
1462a64b7b93SShaun Pereira 	case SIOCX25GDTEFACILITIES: {
1463f90de660Sandrew hendry 		lock_sock(sk);
1464a64b7b93SShaun Pereira 		rc = copy_to_user(argp, &x25->dte_facilities,
1465a64b7b93SShaun Pereira 				  sizeof(x25->dte_facilities));
1466f90de660Sandrew hendry 		release_sock(sk);
1467a64b7b93SShaun Pereira 		if (rc)
1468a64b7b93SShaun Pereira 			rc = -EFAULT;
1469a64b7b93SShaun Pereira 		break;
1470a64b7b93SShaun Pereira 	}
1471a64b7b93SShaun Pereira 
1472a64b7b93SShaun Pereira 	case SIOCX25SDTEFACILITIES: {
1473a64b7b93SShaun Pereira 		struct x25_dte_facilities dtefacs;
1474a64b7b93SShaun Pereira 		rc = -EFAULT;
1475a64b7b93SShaun Pereira 		if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1476a64b7b93SShaun Pereira 			break;
1477a64b7b93SShaun Pereira 		rc = -EINVAL;
1478f90de660Sandrew hendry 		lock_sock(sk);
1479a64b7b93SShaun Pereira 		if (sk->sk_state != TCP_LISTEN &&
1480a64b7b93SShaun Pereira 		    sk->sk_state != TCP_CLOSE)
1481f90de660Sandrew hendry 			goto out_dtefac_release;
1482a64b7b93SShaun Pereira 		if (dtefacs.calling_len > X25_MAX_AE_LEN)
1483f90de660Sandrew hendry 			goto out_dtefac_release;
1484a64b7b93SShaun Pereira 		if (dtefacs.calling_ae == NULL)
1485f90de660Sandrew hendry 			goto out_dtefac_release;
1486a64b7b93SShaun Pereira 		if (dtefacs.called_len > X25_MAX_AE_LEN)
1487f90de660Sandrew hendry 			goto out_dtefac_release;
1488a64b7b93SShaun Pereira 		if (dtefacs.called_ae == NULL)
1489f90de660Sandrew hendry 			goto out_dtefac_release;
1490a64b7b93SShaun Pereira 		x25->dte_facilities = dtefacs;
1491a64b7b93SShaun Pereira 		rc = 0;
1492f90de660Sandrew hendry out_dtefac_release:
1493f90de660Sandrew hendry 		release_sock(sk);
1494a64b7b93SShaun Pereira 		break;
1495a64b7b93SShaun Pereira 	}
1496a64b7b93SShaun Pereira 
14971da177e4SLinus Torvalds 	case SIOCX25GCALLUSERDATA: {
14985b7958dfSandrew hendry 		lock_sock(sk);
14995b7958dfSandrew hendry 		rc = copy_to_user(argp, &x25->calluserdata,
15005b7958dfSandrew hendry 				  sizeof(x25->calluserdata))
15015b7958dfSandrew hendry 			? -EFAULT : 0;
15025b7958dfSandrew hendry 		release_sock(sk);
15031da177e4SLinus Torvalds 		break;
15041da177e4SLinus Torvalds 	}
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 	case SIOCX25SCALLUSERDATA: {
15071da177e4SLinus Torvalds 		struct x25_calluserdata calluserdata;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 		rc = -EFAULT;
1510fddc5f3eSJoe Perches 		if (copy_from_user(&calluserdata, argp, sizeof(calluserdata)))
15111da177e4SLinus Torvalds 			break;
15121da177e4SLinus Torvalds 		rc = -EINVAL;
15131da177e4SLinus Torvalds 		if (calluserdata.cudlength > X25_MAX_CUD_LEN)
15141da177e4SLinus Torvalds 			break;
15155b7958dfSandrew hendry 		lock_sock(sk);
15161da177e4SLinus Torvalds 		x25->calluserdata = calluserdata;
15175b7958dfSandrew hendry 		release_sock(sk);
15181da177e4SLinus Torvalds 		rc = 0;
15191da177e4SLinus Torvalds 		break;
15201da177e4SLinus Torvalds 	}
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	case SIOCX25GCAUSEDIAG: {
152374a7e440Sandrew hendry 		lock_sock(sk);
1524fddc5f3eSJoe Perches 		rc = copy_to_user(argp, &x25->causediag, sizeof(x25->causediag))
152574a7e440Sandrew hendry 			? -EFAULT : 0;
152674a7e440Sandrew hendry 		release_sock(sk);
15271da177e4SLinus Torvalds 		break;
15281da177e4SLinus Torvalds 	}
15291da177e4SLinus Torvalds 
1530386e50ccSAndrew Hendry 	case SIOCX25SCAUSEDIAG: {
1531386e50ccSAndrew Hendry 		struct x25_causediag causediag;
1532386e50ccSAndrew Hendry 		rc = -EFAULT;
1533386e50ccSAndrew Hendry 		if (copy_from_user(&causediag, argp, sizeof(causediag)))
1534386e50ccSAndrew Hendry 			break;
153574a7e440Sandrew hendry 		lock_sock(sk);
1536386e50ccSAndrew Hendry 		x25->causediag = causediag;
153774a7e440Sandrew hendry 		release_sock(sk);
1538386e50ccSAndrew Hendry 		rc = 0;
1539386e50ccSAndrew Hendry 		break;
1540386e50ccSAndrew Hendry 
1541386e50ccSAndrew Hendry 	}
1542386e50ccSAndrew Hendry 
1543cb65d506SShaun Pereira 	case SIOCX25SCUDMATCHLEN: {
1544cb65d506SShaun Pereira 		struct x25_subaddr sub_addr;
1545cb65d506SShaun Pereira 		rc = -EINVAL;
15463f0a069aSandrew hendry 		lock_sock(sk);
1547cb65d506SShaun Pereira 		if(sk->sk_state != TCP_CLOSE)
15483f0a069aSandrew hendry 			goto out_cud_release;
1549cb65d506SShaun Pereira 		rc = -EFAULT;
1550cb65d506SShaun Pereira 		if (copy_from_user(&sub_addr, argp,
1551cb65d506SShaun Pereira 				   sizeof(sub_addr)))
15523f0a069aSandrew hendry 			goto out_cud_release;
1553cb65d506SShaun Pereira 		rc = -EINVAL;
1554cb65d506SShaun Pereira 		if (sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
15553f0a069aSandrew hendry 			goto out_cud_release;
1556cb65d506SShaun Pereira 		x25->cudmatchlength = sub_addr.cudmatchlength;
1557cb65d506SShaun Pereira 		rc = 0;
15583f0a069aSandrew hendry out_cud_release:
15593f0a069aSandrew hendry 		release_sock(sk);
1560cb65d506SShaun Pereira 		break;
1561cb65d506SShaun Pereira 	}
1562cb65d506SShaun Pereira 
1563ebc3f64bSShaun Pereira 	case SIOCX25CALLACCPTAPPRV: {
1564ebc3f64bSShaun Pereira 		rc = -EINVAL;
156577b22836SArnd Bergmann 		lock_sock(sk);
1566ebc3f64bSShaun Pereira 		if (sk->sk_state != TCP_CLOSE)
1567ebc3f64bSShaun Pereira 			break;
156837cda787Sandrew hendry 		clear_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);
156977b22836SArnd Bergmann 		release_sock(sk);
1570ebc3f64bSShaun Pereira 		rc = 0;
1571ebc3f64bSShaun Pereira 		break;
1572ebc3f64bSShaun Pereira 	}
1573ebc3f64bSShaun Pereira 
1574ebc3f64bSShaun Pereira 	case SIOCX25SENDCALLACCPT:  {
1575ebc3f64bSShaun Pereira 		rc = -EINVAL;
157677b22836SArnd Bergmann 		lock_sock(sk);
1577ebc3f64bSShaun Pereira 		if (sk->sk_state != TCP_ESTABLISHED)
1578ebc3f64bSShaun Pereira 			break;
157937cda787Sandrew hendry 		/* must call accptapprv above */
158037cda787Sandrew hendry 		if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags))
1581ebc3f64bSShaun Pereira 			break;
1582ebc3f64bSShaun Pereira 		x25_write_internal(sk, X25_CALL_ACCEPTED);
1583ebc3f64bSShaun Pereira 		x25->state = X25_STATE_3;
158477b22836SArnd Bergmann 		release_sock(sk);
1585ebc3f64bSShaun Pereira 		rc = 0;
1586ebc3f64bSShaun Pereira 		break;
1587ebc3f64bSShaun Pereira 	}
1588ebc3f64bSShaun Pereira 
15891da177e4SLinus Torvalds 	default:
1590b5e5fa5eSChristoph Hellwig 		rc = -ENOIOCTLCMD;
15911da177e4SLinus Torvalds 		break;
15921da177e4SLinus Torvalds 	}
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	return rc;
15951da177e4SLinus Torvalds }
15961da177e4SLinus Torvalds 
1597ec1b4cf7SStephen Hemminger static const struct net_proto_family x25_family_ops = {
15981da177e4SLinus Torvalds 	.family =	AF_X25,
15991da177e4SLinus Torvalds 	.create =	x25_create,
16001da177e4SLinus Torvalds 	.owner	=	THIS_MODULE,
16011da177e4SLinus Torvalds };
16021da177e4SLinus Torvalds 
16031b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
16041b06e6baSShaun Pereira static int compat_x25_subscr_ioctl(unsigned int cmd,
16051b06e6baSShaun Pereira 		struct compat_x25_subscrip_struct __user *x25_subscr32)
16061b06e6baSShaun Pereira {
16071b06e6baSShaun Pereira 	struct compat_x25_subscrip_struct x25_subscr;
16081b06e6baSShaun Pereira 	struct x25_neigh *nb;
16091b06e6baSShaun Pereira 	struct net_device *dev;
16101b06e6baSShaun Pereira 	int rc = -EINVAL;
16111b06e6baSShaun Pereira 
16121b06e6baSShaun Pereira 	rc = -EFAULT;
16131b06e6baSShaun Pereira 	if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
16141b06e6baSShaun Pereira 		goto out;
16151b06e6baSShaun Pereira 
16161b06e6baSShaun Pereira 	rc = -EINVAL;
16171b06e6baSShaun Pereira 	dev = x25_dev_get(x25_subscr.device);
16181b06e6baSShaun Pereira 	if (dev == NULL)
16191b06e6baSShaun Pereira 		goto out;
16201b06e6baSShaun Pereira 
16211b06e6baSShaun Pereira 	nb = x25_get_neigh(dev);
16221b06e6baSShaun Pereira 	if (nb == NULL)
16231b06e6baSShaun Pereira 		goto out_dev_put;
16241b06e6baSShaun Pereira 
16251b06e6baSShaun Pereira 	dev_put(dev);
16261b06e6baSShaun Pereira 
16271b06e6baSShaun Pereira 	if (cmd == SIOCX25GSUBSCRIP) {
16285595a1a5Sandrew hendry 		read_lock_bh(&x25_neigh_list_lock);
16291b06e6baSShaun Pereira 		x25_subscr.extended = nb->extended;
16301b06e6baSShaun Pereira 		x25_subscr.global_facil_mask = nb->global_facil_mask;
16315595a1a5Sandrew hendry 		read_unlock_bh(&x25_neigh_list_lock);
16321b06e6baSShaun Pereira 		rc = copy_to_user(x25_subscr32, &x25_subscr,
16331b06e6baSShaun Pereira 				sizeof(*x25_subscr32)) ? -EFAULT : 0;
16341b06e6baSShaun Pereira 	} else {
16351b06e6baSShaun Pereira 		rc = -EINVAL;
16361b06e6baSShaun Pereira 		if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
16371b06e6baSShaun Pereira 			rc = 0;
16385595a1a5Sandrew hendry 			write_lock_bh(&x25_neigh_list_lock);
16391b06e6baSShaun Pereira 			nb->extended = x25_subscr.extended;
16401b06e6baSShaun Pereira 			nb->global_facil_mask = x25_subscr.global_facil_mask;
16415595a1a5Sandrew hendry 			write_unlock_bh(&x25_neigh_list_lock);
16421b06e6baSShaun Pereira 		}
16431b06e6baSShaun Pereira 	}
16441b06e6baSShaun Pereira 	x25_neigh_put(nb);
16451b06e6baSShaun Pereira out:
16461b06e6baSShaun Pereira 	return rc;
16471b06e6baSShaun Pereira out_dev_put:
16481b06e6baSShaun Pereira 	dev_put(dev);
16491b06e6baSShaun Pereira 	goto out;
16501b06e6baSShaun Pereira }
16511b06e6baSShaun Pereira 
16521b06e6baSShaun Pereira static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
16531b06e6baSShaun Pereira 				unsigned long arg)
16541b06e6baSShaun Pereira {
16551b06e6baSShaun Pereira 	void __user *argp = compat_ptr(arg);
16561b06e6baSShaun Pereira 	struct sock *sk = sock->sk;
16571b06e6baSShaun Pereira 
16581b06e6baSShaun Pereira 	int rc = -ENOIOCTLCMD;
16591b06e6baSShaun Pereira 
16601b06e6baSShaun Pereira 	switch(cmd) {
16611b06e6baSShaun Pereira 	case TIOCOUTQ:
16621b06e6baSShaun Pereira 	case TIOCINQ:
16631b06e6baSShaun Pereira 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
16641b06e6baSShaun Pereira 		break;
16651b06e6baSShaun Pereira 	case SIOCGSTAMP:
16661b06e6baSShaun Pereira 		rc = -EINVAL;
16671b06e6baSShaun Pereira 		if (sk)
16681b06e6baSShaun Pereira 			rc = compat_sock_get_timestamp(sk,
16691b06e6baSShaun Pereira 					(struct timeval __user*)argp);
16701b06e6baSShaun Pereira 		break;
1671ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
1672ae40eb1eSEric Dumazet 		rc = -EINVAL;
1673ae40eb1eSEric Dumazet 		if (sk)
1674ae40eb1eSEric Dumazet 			rc = compat_sock_get_timestampns(sk,
1675ae40eb1eSEric Dumazet 					(struct timespec __user*)argp);
1676ae40eb1eSEric Dumazet 		break;
16771b06e6baSShaun Pereira 	case SIOCGIFADDR:
16781b06e6baSShaun Pereira 	case SIOCSIFADDR:
16791b06e6baSShaun Pereira 	case SIOCGIFDSTADDR:
16801b06e6baSShaun Pereira 	case SIOCSIFDSTADDR:
16811b06e6baSShaun Pereira 	case SIOCGIFBRDADDR:
16821b06e6baSShaun Pereira 	case SIOCSIFBRDADDR:
16831b06e6baSShaun Pereira 	case SIOCGIFNETMASK:
16841b06e6baSShaun Pereira 	case SIOCSIFNETMASK:
16851b06e6baSShaun Pereira 	case SIOCGIFMETRIC:
16861b06e6baSShaun Pereira 	case SIOCSIFMETRIC:
16871b06e6baSShaun Pereira 		rc = -EINVAL;
16881b06e6baSShaun Pereira 		break;
16891b06e6baSShaun Pereira 	case SIOCADDRT:
16901b06e6baSShaun Pereira 	case SIOCDELRT:
16911b06e6baSShaun Pereira 		rc = -EPERM;
16921b06e6baSShaun Pereira 		if (!capable(CAP_NET_ADMIN))
16931b06e6baSShaun Pereira 			break;
16941b06e6baSShaun Pereira 		rc = x25_route_ioctl(cmd, argp);
16951b06e6baSShaun Pereira 		break;
16961b06e6baSShaun Pereira 	case SIOCX25GSUBSCRIP:
16971b06e6baSShaun Pereira 		rc = compat_x25_subscr_ioctl(cmd, argp);
16981b06e6baSShaun Pereira 		break;
16991b06e6baSShaun Pereira 	case SIOCX25SSUBSCRIP:
17001b06e6baSShaun Pereira 		rc = -EPERM;
17011b06e6baSShaun Pereira 		if (!capable(CAP_NET_ADMIN))
17021b06e6baSShaun Pereira 			break;
17031b06e6baSShaun Pereira 		rc = compat_x25_subscr_ioctl(cmd, argp);
17041b06e6baSShaun Pereira 		break;
17051b06e6baSShaun Pereira 	case SIOCX25GFACILITIES:
17061b06e6baSShaun Pereira 	case SIOCX25SFACILITIES:
17079a6b9f2eSShaun Pereira 	case SIOCX25GDTEFACILITIES:
17089a6b9f2eSShaun Pereira 	case SIOCX25SDTEFACILITIES:
17091b06e6baSShaun Pereira 	case SIOCX25GCALLUSERDATA:
17101b06e6baSShaun Pereira 	case SIOCX25SCALLUSERDATA:
17111b06e6baSShaun Pereira 	case SIOCX25GCAUSEDIAG:
1712386e50ccSAndrew Hendry 	case SIOCX25SCAUSEDIAG:
17131b06e6baSShaun Pereira 	case SIOCX25SCUDMATCHLEN:
17141b06e6baSShaun Pereira 	case SIOCX25CALLACCPTAPPRV:
17151b06e6baSShaun Pereira 	case SIOCX25SENDCALLACCPT:
17161b06e6baSShaun Pereira 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
17171b06e6baSShaun Pereira 		break;
17181b06e6baSShaun Pereira 	default:
17191b06e6baSShaun Pereira 		rc = -ENOIOCTLCMD;
17201b06e6baSShaun Pereira 		break;
17211b06e6baSShaun Pereira 	}
17221b06e6baSShaun Pereira 	return rc;
17231b06e6baSShaun Pereira }
17241b06e6baSShaun Pereira #endif
17251b06e6baSShaun Pereira 
172691774904SArnd Bergmann static const struct proto_ops x25_proto_ops = {
17271da177e4SLinus Torvalds 	.family =	AF_X25,
17281da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
17291da177e4SLinus Torvalds 	.release =	x25_release,
17301da177e4SLinus Torvalds 	.bind =		x25_bind,
17311da177e4SLinus Torvalds 	.connect =	x25_connect,
17321da177e4SLinus Torvalds 	.socketpair =	sock_no_socketpair,
17331da177e4SLinus Torvalds 	.accept =	x25_accept,
17341da177e4SLinus Torvalds 	.getname =	x25_getname,
1735768190fdSandrew hendry 	.poll =		datagram_poll,
17361da177e4SLinus Torvalds 	.ioctl =	x25_ioctl,
17371b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
17381b06e6baSShaun Pereira 	.compat_ioctl = compat_x25_ioctl,
17391b06e6baSShaun Pereira #endif
17401da177e4SLinus Torvalds 	.listen =	x25_listen,
17411da177e4SLinus Torvalds 	.shutdown =	sock_no_shutdown,
17421da177e4SLinus Torvalds 	.setsockopt =	x25_setsockopt,
17431da177e4SLinus Torvalds 	.getsockopt =	x25_getsockopt,
17441da177e4SLinus Torvalds 	.sendmsg =	x25_sendmsg,
17451da177e4SLinus Torvalds 	.recvmsg =	x25_recvmsg,
17461da177e4SLinus Torvalds 	.mmap =		sock_no_mmap,
17471da177e4SLinus Torvalds 	.sendpage =	sock_no_sendpage,
17481da177e4SLinus Torvalds };
17491da177e4SLinus Torvalds 
17507546dd97SStephen Hemminger static struct packet_type x25_packet_type __read_mostly = {
175109640e63SHarvey Harrison 	.type =	cpu_to_be16(ETH_P_X25),
17521da177e4SLinus Torvalds 	.func =	x25_lapb_receive_frame,
17531da177e4SLinus Torvalds };
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds static struct notifier_block x25_dev_notifier = {
17561da177e4SLinus Torvalds 	.notifier_call = x25_device_event,
17571da177e4SLinus Torvalds };
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds void x25_kill_by_neigh(struct x25_neigh *nb)
17601da177e4SLinus Torvalds {
17611da177e4SLinus Torvalds 	struct sock *s;
17621da177e4SLinus Torvalds 	struct hlist_node *node;
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	sk_for_each(s, node, &x25_list)
17671da177e4SLinus Torvalds 		if (x25_sk(s)->neighbour == nb)
17681da177e4SLinus Torvalds 			x25_disconnect(s, ENETUNREACH, 0, 0);
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
177195a9dc43SAndrew Hendry 
177295a9dc43SAndrew Hendry 	/* Remove any related forwards */
177395a9dc43SAndrew Hendry 	x25_clear_forward_by_dev(nb->dev);
17741da177e4SLinus Torvalds }
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds static int __init x25_init(void)
17771da177e4SLinus Torvalds {
17781da177e4SLinus Torvalds 	int rc = proto_register(&x25_proto, 0);
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	if (rc != 0)
17811da177e4SLinus Torvalds 		goto out;
17821da177e4SLinus Torvalds 
17831fd975a0Sandrew hendry 	rc = sock_register(&x25_family_ops);
17841fd975a0Sandrew hendry 	if (rc != 0)
17851fd975a0Sandrew hendry 		goto out_proto;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	dev_add_pack(&x25_packet_type);
17881da177e4SLinus Torvalds 
17891fd975a0Sandrew hendry 	rc = register_netdevice_notifier(&x25_dev_notifier);
17901fd975a0Sandrew hendry 	if (rc != 0)
17911fd975a0Sandrew hendry 		goto out_sock;
17921da177e4SLinus Torvalds 
1793a44562e4Smaximilian attems 	printk(KERN_INFO "X.25 for Linux Version 0.2\n");
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	x25_register_sysctl();
17961fd975a0Sandrew hendry 	rc = x25_proc_init();
17971fd975a0Sandrew hendry 	if (rc != 0)
17981fd975a0Sandrew hendry 		goto out_dev;
17991da177e4SLinus Torvalds out:
18001da177e4SLinus Torvalds 	return rc;
18011fd975a0Sandrew hendry out_dev:
18021fd975a0Sandrew hendry 	unregister_netdevice_notifier(&x25_dev_notifier);
18031fd975a0Sandrew hendry out_sock:
18041fd975a0Sandrew hendry 	sock_unregister(AF_X25);
18051fd975a0Sandrew hendry out_proto:
18061fd975a0Sandrew hendry 	proto_unregister(&x25_proto);
18071fd975a0Sandrew hendry 	goto out;
18081da177e4SLinus Torvalds }
18091da177e4SLinus Torvalds module_init(x25_init);
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds static void __exit x25_exit(void)
18121da177e4SLinus Torvalds {
18131da177e4SLinus Torvalds 	x25_proc_exit();
18141da177e4SLinus Torvalds 	x25_link_free();
18151da177e4SLinus Torvalds 	x25_route_free();
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	x25_unregister_sysctl();
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	unregister_netdevice_notifier(&x25_dev_notifier);
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	dev_remove_pack(&x25_packet_type);
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 	sock_unregister(AF_X25);
18241da177e4SLinus Torvalds 	proto_unregister(&x25_proto);
18251da177e4SLinus Torvalds }
18261da177e4SLinus Torvalds module_exit(x25_exit);
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
18291da177e4SLinus Torvalds MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
18301da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18311da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_X25);
1832