38 #include <linux/module.h>
39 #include <linux/capability.h>
40 #include <linux/errno.h>
41 #include <linux/kernel.h>
42 #include <linux/sched.h>
44 #include <linux/string.h>
45 #include <linux/net.h>
46 #include <linux/netdevice.h>
47 #include <linux/if_arp.h>
49 #include <linux/slab.h>
52 #include <asm/uaccess.h>
53 #include <linux/fcntl.h>
58 #include <linux/ctype.h>
73 static const struct proto_ops x25_proto_ops;
78 struct compat_x25_subscrip_struct {
94 if (!pskb_may_pull(skb, 1)) {
101 needed = 1 + (len >> 4) + (len & 0x0f);
103 if (!pskb_may_pull(skb, needed)) {
124 char *called, *calling;
127 called_len = (*p >> 0) & 0x0F;
128 calling_len = (*p >> 4) & 0x0F;
135 if (i < called_len) {
137 *called++ = ((*p >> 0) & 0x0F) +
'0';
140 *called++ = ((*p >> 4) & 0x0F) +
'0';
144 *calling++ = ((*p >> 0) & 0x0F) +
'0';
147 *calling++ = ((*p >> 4) & 0x0F) +
'0';
152 *called = *calling =
'\0';
154 return 1 + (called_len + calling_len + 1) / 2;
161 char *called, *calling;
167 called_len =
strlen(called);
168 calling_len =
strlen(calling);
170 *p++ = (calling_len << 4) | (called_len << 0);
173 if (i < called_len) {
175 *p |= (*called++ -
'0') << 0;
179 *p |= (*called++ -
'0') << 4;
183 *p |= (*calling++ -
'0') << 0;
187 *p |= (*calling++ -
'0') << 4;
192 return 1 + (called_len + calling_len + 1) / 2;
198 static void x25_remove_socket(
struct sock *
sk)
201 sk_del_node_init(sk);
231 if (!net_eq(dev_net(dev), &
init_net))
251 x25_kill_by_device(dev);
264 static void x25_insert_socket(
struct sock *
sk)
267 sk_add_node(sk, &x25_list);
282 struct sock *next_best;
292 null_x25_address.x25_addr)) &&
298 if (x25_sk(s)->cudmatchlength > 0 &&
299 skb->
len >= x25_sk(s)->cudmatchlength) {
300 if((
memcmp(x25_sk(s)->calluserdata.cuddata,
302 x25_sk(s)->cudmatchlength)) == 0) {
323 static struct sock *__x25_find_socket(
unsigned int lci,
struct x25_neigh *nb)
329 if (x25_sk(s)->lci == lci && x25_sk(s)->
neighbour == nb) {
343 s = __x25_find_socket(lci, nb);
351 static unsigned int x25_new_lci(
struct x25_neigh *nb)
353 unsigned int lci = 1;
358 while ((sk = __x25_find_socket(lci, nb)) !=
NULL) {
373 static void __x25_destroy_socket(
struct sock *);
378 static void x25_destroy_timer(
unsigned long data)
390 static void __x25_destroy_socket(
struct sock *sk)
397 x25_remove_socket(sk);
414 if (sk_has_allocations(sk)) {
417 sk->
sk_timer.function = x25_destroy_timer;
430 __x25_destroy_socket(sk);
441 char __user *optval,
unsigned int optlen)
444 struct sock *sk = sock->
sk;
451 if (optlen <
sizeof(
int))
455 if (
get_user(opt, (
int __user *)optval))
467 static int x25_getsockopt(
struct socket *sock,
int level,
int optname,
468 char __user *optval,
int __user *optlen)
470 struct sock *sk = sock->
sk;
480 len =
min_t(
unsigned int, len,
sizeof(
int));
498 struct sock *sk = sock->
sk;
502 if (sk->sk_state != TCP_LISTEN) {
513 static struct proto x25_proto = {
516 .obj_size =
sizeof(
struct x25_sock),
519 static struct sock *x25_alloc_socket(
struct net *
net)
538 static int x25_create(
struct net *net,
struct socket *sock,
int protocol,
557 if ((sk = x25_alloc_socket(net)) ==
NULL)
566 sock->
ops = &x25_proto_ops;
598 static struct sock *x25_make_new(
struct sock *osk)
600 struct sock *sk =
NULL;
606 if ((sk = x25_alloc_socket(sock_net(osk))) ==
NULL)
618 sock_copy_flags(sk, osk);
636 static int x25_release(
struct socket *sock)
638 struct sock *sk = sock->
sk;
648 switch (x25->
state) {
653 __x25_destroy_socket(sk);
680 struct sock *sk = sock->
sk;
692 for (i = 0; i < len; i++) {
700 x25_sk(sk)->source_addr = addr->
sx25_addr;
701 x25_insert_socket(sk);
704 SOCK_DEBUG(sk,
"x25_bind: socket is bound\n");
709 static int x25_wait_for_connection_establishment(
struct sock *sk)
738 static int x25_connect(
struct socket *sock,
struct sockaddr *uaddr,
739 int addr_len,
int flags)
741 struct sock *sk = sock->
sk;
790 if (!
strcmp(x25->source_addr.x25_addr, null_x25_address.
x25_addr))
811 rc = x25_wait_for_connection_establishment(sk);
827 static int x25_wait_for_data(
struct sock *sk,
long timeout)
856 static int x25_accept(
struct socket *sock,
struct socket *newsock,
int flags)
858 struct sock *sk = sock->
sk;
872 if (sk->sk_state != TCP_LISTEN)
883 sock_graft(newsk, newsock);
897 static int x25_getname(
struct socket *sock,
struct sockaddr *uaddr,
898 int *uaddr_len,
int peer)
901 struct sock *sk = sock->
sk;
915 *uaddr_len =
sizeof(*sx25);
930 int len, addr_len,
rc;
945 goto out_clear_request;
955 if (!pskb_may_pull(skb, 1))
956 goto out_clear_request;
957 len = skb->
data[0] + 1;
958 if (!pskb_may_pull(skb, len))
959 goto out_clear_request;
966 goto out_clear_request;
972 if (!pskb_may_pull(skb, skb->
len))
973 goto out_clear_request;
978 sk = x25_find_listener(&source_addr,skb);
981 if (sk !=
NULL && sk_acceptq_is_full(sk)) {
1000 goto out_clear_request;
1021 make = x25_make_new(sk);
1033 makex25 = x25_sk(make);
1056 skb_copy_from_linear_data(skb, makex25->
calluserdata.cuddata, skb->
len);
1061 x25_insert_socket(make);
1084 struct sock *sk = sock->
sk;
1089 unsigned char *asmptr;
1092 int qbit = 0, rc = -
EINVAL;
1120 memcpy(&sx25, usx25,
sizeof(sx25));
1147 SOCK_DEBUG(sk,
"x25_sendmsg: sendto: Addresses built.\n");
1150 SOCK_DEBUG(sk,
"x25_sendmsg: sendto: building packet.\n");
1169 SOCK_DEBUG(sk,
"x25_sendmsg: Copying user data\n");
1171 skb_reset_transport_header(skb);
1183 if (!pskb_may_pull(skb, 1))
1186 qbit = skb->
data[0];
1193 SOCK_DEBUG(sk,
"x25_sendmsg: Building X.25 Header.\n");
1199 *asmptr++ = (x25->
lci >> 0) & 0xFF;
1204 *asmptr++ = (x25->
lci >> 0) & 0xFF;
1210 asmptr =
skb_push(skb, X25_EXT_MIN_LEN);
1212 *asmptr++ = (x25->
lci >> 0) & 0xFF;
1219 *asmptr++ = (x25->
lci >> 0) & 0xFF;
1227 SOCK_DEBUG(sk,
"x25_sendmsg: Built header.\n");
1228 SOCK_DEBUG(sk,
"x25_sendmsg: Transmitting buffer\n");
1256 static int x25_recvmsg(
struct kiocb *iocb,
struct socket *sock,
1260 struct sock *sk = sock->
sk;
1266 unsigned char *asmptr;
1293 goto out_free_dgram;
1316 goto out_free_dgram;
1328 skb_reset_transport_header(skb);
1331 if (copied > size) {
1341 goto out_free_dgram;
1360 static int x25_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
1362 struct sock *sk = sock->
sk;
1371 amount = sk->
sk_sndbuf - sk_wmem_alloc_get(sk);
1374 rc =
put_user(amount, (
unsigned int __user *)argp);
1389 rc =
put_user(amount, (
unsigned int __user *)argp);
1397 (
struct timeval __user *)argp);
1449 if (sk->sk_state != TCP_LISTEN &&
1451 goto out_fac_release;
1454 goto out_fac_release;
1457 goto out_fac_release;
1460 goto out_fac_release;
1467 else if (out < 0x30 || out > 0xD0)
1468 goto out_fac_release;
1472 else if (in < 0x03 || in > 0x0D)
1473 goto out_fac_release;
1477 goto out_fac_release;
1502 if (sk->sk_state != TCP_LISTEN &&
1504 goto out_dtefac_release;
1506 goto out_dtefac_release;
1507 if (dtefacs.calling_ae ==
NULL)
1508 goto out_dtefac_release;
1510 goto out_dtefac_release;
1511 if (dtefacs.called_ae ==
NULL)
1512 goto out_dtefac_release;
1571 goto out_cud_release;
1575 goto out_cud_release;
1578 goto out_cud_release;
1622 .create = x25_create,
1626 #ifdef CONFIG_COMPAT
1627 static int compat_x25_subscr_ioctl(
unsigned int cmd,
1628 struct compat_x25_subscrip_struct __user *x25_subscr32)
1630 struct compat_x25_subscrip_struct x25_subscr;
1636 if (
copy_from_user(&x25_subscr, x25_subscr32,
sizeof(*x25_subscr32)))
1652 x25_subscr.extended = nb->
extended;
1656 sizeof(*x25_subscr32)) ? -
EFAULT : 0;
1659 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1662 nb->
extended = x25_subscr.extended;
1675 static int compat_x25_ioctl(
struct socket *sock,
unsigned int cmd,
1678 void __user *argp = compat_ptr(arg);
1679 struct sock *sk = sock->
sk;
1686 rc = x25_ioctl(sock, cmd, (
unsigned long)argp);
1692 (
struct timeval __user*)argp);
1720 rc = compat_x25_subscr_ioctl(cmd, argp);
1726 rc = compat_x25_subscr_ioctl(cmd, argp);
1739 rc = x25_ioctl(sock, cmd, (
unsigned long)argp);
1749 static const struct proto_ops x25_proto_ops = {
1752 .release = x25_release,
1754 .connect = x25_connect,
1756 .accept = x25_accept,
1757 .getname = x25_getname,
1760 #ifdef CONFIG_COMPAT
1761 .compat_ioctl = compat_x25_ioctl,
1763 .listen = x25_listen,
1765 .setsockopt = x25_setsockopt,
1766 .getsockopt = x25_getsockopt,
1767 .sendmsg = x25_sendmsg,
1768 .recvmsg = x25_recvmsg,
1779 .notifier_call = x25_device_event,
1799 static int __init x25_init(
void)
1834 static void __exit x25_exit(
void)