102 #include <linux/module.h>
103 #include <linux/errno.h>
104 #include <linux/types.h>
105 #include <linux/slab.h>
106 #include <linux/socket.h>
107 #include <linux/in.h>
108 #include <linux/kernel.h>
109 #include <linux/sched.h>
111 #include <linux/string.h>
113 #include <linux/net.h>
114 #include <linux/netdevice.h>
117 #include <linux/netfilter.h>
122 #include <asm/ioctls.h>
123 #include <linux/capability.h>
127 #include <linux/stat.h>
129 #include <linux/poll.h>
146 static void dn_keepalive(
struct sock *
sk);
148 #define DN_SK_HASH_SHIFT 8
149 #define DN_SK_HASH_SIZE (1 << DN_SK_HASH_SHIFT)
150 #define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1)
153 static const struct proto_ops dn_proto_ops;
159 static int __dn_setsockopt(
struct socket *
sock,
int level,
int optname,
char __user *optval,
unsigned int optlen,
int flags);
160 static int __dn_getsockopt(
struct socket *
sock,
int level,
int optname,
char __user *optval,
int __user *optlen,
int flags);
167 return hlist_empty(&dn_wild_sk) ? &dn_wild_sk :
NULL;
184 struct dn_scp *scp = DN_SK(sk);
191 static unsigned short port_alloc(
struct sock *sk)
193 struct dn_scp *scp = DN_SK(sk);
194 static unsigned short port = 0x2000;
195 unsigned short i_port =
port;
212 static int dn_hash_sock(
struct sock *sk)
214 struct dn_scp *scp = DN_SK(sk);
222 if (!scp->
addrloc && !port_alloc(sk))
226 if ((list = dn_find_list(sk)) ==
NULL)
229 sk_add_node(sk, list);
236 static void dn_unhash_sock(
struct sock *sk)
239 sk_del_node_init(sk);
243 static void dn_unhash_sock_bh(
struct sock *sk)
246 sk_del_node_init(sk);
271 static void dn_rehash_sock(
struct sock *sk)
274 struct dn_scp *scp = DN_SK(sk);
280 sk_del_node_init(sk);
281 DN_SK(sk)->addrloc = 0;
282 list = listen_hash(&DN_SK(sk)->addr);
283 sk_add_node(sk, list);
382 struct dn_scp *scp = DN_SK(sk);
385 if (scp->
addr.sdn_objnum) {
401 sk = sk_head(&dn_wild_sk);
423 if (cb->
src != dn_saddr2dn(&scp->
peer))
440 static void dn_destruct(
struct sock *sk)
442 struct dn_scp *scp = DN_SK(sk);
451 static int dn_memory_pressure;
453 static void dn_enter_memory_pressure(
struct sock *sk)
455 if (!dn_memory_pressure) {
456 dn_memory_pressure = 1;
460 static struct proto dn_proto = {
463 .enter_memory_pressure = dn_enter_memory_pressure,
464 .memory_pressure = &dn_memory_pressure,
465 .memory_allocated = &decnet_memory_allocated,
470 .obj_size =
sizeof(
struct dn_sock),
482 sock->
ops = &dn_proto_ops;
550 static void dn_keepalive(
struct sock *sk)
552 struct dn_scp *scp = DN_SK(sk);
573 struct dn_scp *scp = DN_SK(sk);
577 switch (scp->
state) {
613 static void dn_destroy_sock(
struct sock *sk)
615 struct dn_scp *scp = DN_SK(sk);
626 switch (scp->
state) {
654 printk(
KERN_DEBUG "DECnet: dn_destroy_sock passed socket in invalid state\n");
658 dn_unhash_sock_bh(sk);
667 unsigned short node, area;
669 node = addr & 0x03ff;
671 sprintf(buf,
"%hd.%hd", area, node);
678 static int dn_create(
struct net *net,
struct socket *sock,
int protocol,
686 switch (sock->
type) {
708 dn_release(
struct socket *sock)
710 struct sock *sk = sock->
sk;
726 struct sock *sk = sock->
sk;
727 struct dn_scp *scp = DN_SK(sk);
758 if (dn_dev_islocal(dev, dn_saddr2dn(saddr))) {
775 rv = dn_hash_sock(sk);
785 static int dn_auto_bind(
struct socket *sock)
787 struct sock *sk = sock->
sk;
788 struct dn_scp *scp = DN_SK(sk);
793 scp->
addr.sdn_flags = 0;
794 scp->
addr.sdn_objnum = 0;
814 rv = dn_hash_sock(sk);
822 static int dn_confirm_accept(
struct sock *sk,
long *timeo,
gfp_t allocation)
824 struct dn_scp *scp = DN_SK(sk);
832 scp->
segsize_loc = dst_metric_advmss(__sk_dst_get(sk));
844 err = sock_error(sk);
847 err = sock_intr_errno(*timeo);
864 static int dn_wait_run(
struct sock *sk,
long *timeo)
866 struct dn_scp *scp = DN_SK(sk);
885 err = sock_error(sk);
888 err = sock_intr_errno(*timeo);
906 static int __dn_connect(
struct sock *sk,
struct sockaddr_dn *addr,
int addrlen,
long *timeo,
int flags)
909 struct dn_scp *scp = DN_SK(sk);
927 return dn_wait_run(sk, timeo);
950 memset(&fld, 0,
sizeof(fld));
951 fld.flowidn_oif = sk->sk_bound_dev_if;
952 fld.daddr = dn_saddr2dn(&scp->
peer);
953 fld.saddr = dn_saddr2dn(&scp->
addr);
954 dn_sk_ports_copy(&fld, scp);
966 err = dn_wait_run(sk, timeo);
972 static int dn_connect(
struct socket *sock,
struct sockaddr *uaddr,
int addrlen,
int flags)
975 struct sock *sk = sock->
sk;
977 long timeo = sock_sndtimeo(sk, flags &
O_NONBLOCK);
980 err = __dn_connect(sk, addr, addrlen, &timeo, 0);
986 static inline int dn_check_state(
struct sock *sk,
struct sockaddr_dn *addr,
int addrlen,
long *timeo,
int flags)
988 struct dn_scp *scp = DN_SK(sk);
990 switch (scp->
state) {
997 return dn_wait_run(sk, timeo);
999 return __dn_connect(sk, addr, addrlen, timeo, flags);
1008 unsigned char *
ptr = skb->
data;
1027 unsigned char *
ptr = skb->
data;
1037 static struct sk_buff *dn_wait_for_connect(
struct sock *sk,
long *timeo)
1057 err = sock_intr_errno(*timeo);
1067 return skb ==
NULL ? ERR_PTR(err) : skb;
1070 static int dn_accept(
struct socket *sock,
struct socket *newsock,
int flags)
1072 struct sock *sk = sock->
sk, *newsk;
1075 unsigned char menuver;
1078 long timeo = sock_rcvtimeo(sk, flags &
O_NONBLOCK);
1090 skb = dn_wait_for_connect(sk, &timeo);
1093 return PTR_ERR(skb);
1099 newsk = dn_alloc_sock(sock_net(sk), newsock, sk->
sk_allocation);
1100 if (newsk ==
NULL) {
1108 sk_dst_set(newsk, dst);
1109 skb_dst_set(skb,
NULL);
1111 DN_SK(newsk)->state =
DN_CR;
1112 DN_SK(newsk)->addrrem = cb->
src_port;
1113 DN_SK(newsk)->services_rem = cb->
services;
1114 DN_SK(newsk)->info_rem = cb->
info;
1115 DN_SK(newsk)->segsize_rem = cb->
segsize;
1116 DN_SK(newsk)->accept_mode = DN_SK(sk)->accept_mode;
1118 if (DN_SK(newsk)->segsize_rem < 230)
1119 DN_SK(newsk)->segsize_rem = 230;
1131 DN_SK(newsk)->addr.sdn_flags &= ~
SDF_WILD;
1135 *(
__le16 *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->
src;
1136 *(
__le16 *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->
dst;
1138 menuver = *skb->
data;
1142 dn_access_copy(skb, &(DN_SK(newsk)->accessdata));
1145 dn_user_copy(skb, &(DN_SK(newsk)->conndata_in));
1148 DN_SK(newsk)->peer.sdn_flags |=
SDF_PROXY;
1155 memcpy(&(DN_SK(newsk)->conndata_out), &(DN_SK(sk)->conndata_out),
1157 memcpy(&(DN_SK(newsk)->discdata_out), &(DN_SK(sk)->discdata_out),
1161 err = dn_hash_sock(newsk);
1170 if (DN_SK(newsk)->accept_mode ==
ACC_IMMED)
1171 err = dn_confirm_accept(newsk, &timeo,
1179 static int dn_getname(
struct socket *sock,
struct sockaddr *uaddr,
int *uaddr_len,
int peer)
1182 struct sock *sk = sock->
sk;
1183 struct dn_scp *scp = DN_SK(sk);
1210 struct sock *sk = sock->
sk;
1211 struct dn_scp *scp = DN_SK(sk);
1220 static int dn_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
1222 struct sock *sk = sock->
sk;
1223 struct dn_scp *scp = DN_SK(sk);
1244 amount = sk->
sk_sndbuf - sk_wmem_alloc_get(sk);
1247 err =
put_user(amount, (
int __user *)arg);
1260 err =
put_user(amount, (
int __user *)arg);
1273 struct sock *sk = sock->sk;
1297 static int dn_shutdown(
struct socket *sock,
int how)
1299 struct sock *sk = sock->
sk;
1300 struct dn_scp *scp = DN_SK(sk);
1320 dn_destroy_sock(sk);
1329 static int dn_setsockopt(
struct socket *sock,
int level,
int optname,
char __user *optval,
unsigned int optlen)
1331 struct sock *sk = sock->
sk;
1335 err = __dn_setsockopt(sock, level, optname, optval, optlen, 0);
1341 static int __dn_setsockopt(
struct socket *sock,
int level,
int optname,
char __user *optval,
unsigned int optlen,
int flags)
1343 struct sock *sk = sock->
sk;
1344 struct dn_scp *scp = DN_SK(sk);
1352 unsigned char services;
1357 if (optlen && !optval)
1360 if (optlen >
sizeof(
u))
1419 if (optlen !=
sizeof(
int))
1431 timeo = sock_rcvtimeo(sk, 0);
1445 #ifdef CONFIG_NETFILTER
1454 if (optlen !=
sizeof(
unsigned long))
1466 if (optlen !=
sizeof(
int))
1470 scp->
nonagle = (
u.val == 0) ? 0 : 1;
1475 if (optlen !=
sizeof(
int))
1479 scp->
nonagle = (
u.val == 0) ? 0 : 2;
1484 if (optlen !=
sizeof(
unsigned char))
1486 if ((
u.services & ~NSP_FC_MASK) != 0x01)
1494 if (optlen !=
sizeof(
unsigned char))
1505 static int dn_getsockopt(
struct socket *sock,
int level,
int optname,
char __user *optval,
int __user *optlen)
1507 struct sock *sk = sock->
sk;
1511 err = __dn_getsockopt(sock, level, optname, optval, optlen, 0);
1517 static int __dn_getsockopt(
struct socket *sock,
int level,
int optname,
char __user *optval,
int __user *optlen,
int flags)
1519 struct sock *sk = sock->
sk;
1520 struct dn_scp *scp = DN_SK(sk);
1523 void *r_data =
NULL;
1549 if (r_len >
sizeof(
unsigned char))
1550 r_len =
sizeof(
unsigned char);
1560 switch (sock->
state) {
1579 #ifdef CONFIG_NETFILTER
1599 if (r_len >
sizeof(
unsigned long))
1600 r_len =
sizeof(
unsigned long);
1605 if (r_len >
sizeof(
int))
1606 r_len =
sizeof(
int);
1612 if (r_len >
sizeof(
int))
1613 r_len =
sizeof(
int);
1619 if (r_len >
sizeof(
unsigned char))
1620 r_len =
sizeof(
unsigned char);
1625 if (r_len >
sizeof(
unsigned char))
1626 r_len =
sizeof(
unsigned char);
1648 return !skb_queue_empty(q) ? 1 : 0;
1650 skb_queue_walk(q, skb) {
1675 struct sock *sk = sock->
sk;
1676 struct dn_scp *scp = DN_SK(sk);
1678 size_t target = size > 1 ? 1 : 0;
1683 unsigned char eor = 0;
1698 rv = dn_check_state(sk,
NULL, 0, &timeo, flags);
1707 if (flags & MSG_OOB)
1724 if (!(flags & MSG_OOB)) {
1736 if (signal_pending(
current)) {
1737 rv = sock_intr_errno(timeo);
1741 if (dn_data_ready(sk, queue, flags, target))
1751 sk_wait_event(sk, &timeo, dn_data_ready(sk, queue, flags, target));
1756 skb_queue_walk_safe(queue, skb, n) {
1760 if ((chunk + copied) > size)
1761 chunk = size - copied;
1774 if (skb->
len == 0) {
1790 if (!(flags & MSG_WAITALL))
1794 if (flags & MSG_OOB)
1797 if (copied >= target)
1822 static inline int dn_queue_too_long(
struct dn_scp *scp,
struct sk_buff_head *queue,
int flags)
1828 if (flags & MSG_OOB) {
1869 static inline unsigned int dn_current_mss(
struct sock *sk,
int flags)
1871 struct dst_entry *dst = __sk_dst_get(sk);
1872 struct dn_scp *scp = DN_SK(sk);
1876 if (flags & MSG_OOB)
1881 u32 mtu = dst_mtu(dst);
1894 static inline struct sk_buff *dn_alloc_send_pskb(
struct sock *sk,
1895 unsigned long datalen,
int noblock,
1907 static int dn_sendmsg(
struct kiocb *iocb,
struct socket *sock,
1908 struct msghdr *msg,
size_t size)
1910 struct sock *sk = sock->
sk;
1911 struct dn_scp *scp = DN_SK(sk);
1922 unsigned char fctype;
1928 if (addr_len && (addr_len !=
sizeof(
struct sockaddr_dn)))
1932 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1947 err = dn_check_state(sk, addr, addr_len, &timeo, flags);
1959 dst_negative_advice(sk);
1964 mss = dn_current_mss(sk, flags);
1966 if (flags & MSG_OOB) {
1976 while(sent < size) {
1977 err = sock_error(sk);
1981 if (signal_pending(
current)) {
1982 err = sock_intr_errno(timeo);
1998 if (dn_queue_too_long(scp, queue, flags)) {
2001 if (flags & MSG_DONTWAIT) {
2009 !dn_queue_too_long(scp, queue, flags));
2022 flags & MSG_DONTWAIT, &err);
2039 if (flags & MSG_OOB) {
2050 if (((sent + len) == size) && (flags &
MSG_EOR)) {
2073 return sent ? sent :
err;
2086 if (!net_eq(dev_net(dev), &
init_net))
2104 .notifier_call = dn_device_event,
2114 #ifdef CONFIG_PROC_FS
2115 struct dn_iter_state {
2119 static struct sock *dn_socket_get_first(
struct seq_file *seq)
2122 struct sock *n =
NULL;
2124 for(state->bucket = 0;
2127 n = sk_head(&dn_sk_hash[state->bucket]);
2135 static struct sock *dn_socket_get_next(
struct seq_file *seq,
2138 struct dn_iter_state *state = seq->
private;
2146 n = sk_head(&dn_sk_hash[state->bucket]);
2152 static struct sock *socket_get_idx(
struct seq_file *seq, loff_t *
pos)
2154 struct sock *sk = dn_socket_get_first(seq);
2157 while(*pos && (sk = dn_socket_get_next(seq, sk)))
2163 static void *dn_socket_get_idx(
struct seq_file *seq, loff_t pos)
2167 rc = socket_get_idx(seq, &pos);
2174 static void *dn_socket_seq_start(
struct seq_file *seq, loff_t *pos)
2179 static void *dn_socket_seq_next(
struct seq_file *seq,
void *
v, loff_t *pos)
2184 rc = dn_socket_get_idx(seq, 0);
2188 rc = dn_socket_get_next(seq, v);
2197 static void dn_socket_seq_stop(
struct seq_file *seq,
void *v)
2203 #define IS_NOT_PRINTABLE(x) ((x) < 32 || (x) > 126)
2205 static void dn_printable_object(
struct sockaddr_dn *
dn,
unsigned char *
buf)
2216 if (IS_NOT_PRINTABLE(buf[i]))
2223 static char *dn_state2asc(
unsigned char state)
2263 static inline void dn_socket_format_entry(
struct seq_file *seq,
struct sock *sk)
2265 struct dn_scp *scp = DN_SK(sk);
2271 dn_printable_object(&scp->
addr, local_object);
2272 dn_printable_object(&scp->
peer, remote_object);
2275 "%6s/%04X %04d:%04d %04d:%04d %01d %-16s "
2276 "%6s/%04X %04d:%04d %04d:%04d %01d %-16s %4s %s\n",
2293 dn_state2asc(scp->
state),
2297 static int dn_socket_seq_show(
struct seq_file *seq,
void *v)
2302 dn_socket_format_entry(seq, v);
2308 .
start = dn_socket_seq_start,
2309 .next = dn_socket_seq_next,
2310 .stop = dn_socket_seq_stop,
2311 .show = dn_socket_seq_show,
2314 static int dn_socket_seq_open(
struct inode *
inode,
struct file *file)
2317 sizeof(
struct dn_iter_state));
2322 .open = dn_socket_seq_open,
2331 .create = dn_create,
2335 static const struct proto_ops dn_proto_ops = {
2338 .release = dn_release,
2340 .connect = dn_connect,
2342 .accept = dn_accept,
2343 .getname = dn_getname,
2346 .listen = dn_listen,
2347 .shutdown = dn_shutdown,
2348 .setsockopt = dn_setsockopt,
2349 .getsockopt = dn_getsockopt,
2350 .sendmsg = dn_sendmsg,
2351 .recvmsg = dn_recvmsg,
2364 static char banner[]
__initdata =
KERN_INFO "NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n";
2366 static int __init decnet_init(
void)
2399 static void __exit decnet_exit(
void)