Thomas Gleixner | fd9871f | 2019-05-19 15:51:54 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * DECnet An implementation of the DECnet protocol suite for the LINUX |
| 5 | * operating system. DECnet is implemented using the BSD Socket |
| 6 | * interface as the means of communication with the user level. |
| 7 | * |
| 8 | * DECnet Socket Layer Interface |
| 9 | * |
| 10 | * Authors: Eduardo Marcelo Serrat <emserrat@geocities.com> |
| 11 | * Patrick Caulfield <patrick@pandh.demon.co.uk> |
| 12 | * |
| 13 | * Changes: |
| 14 | * Steve Whitehouse: Copied from Eduardo Serrat and Patrick Caulfield's |
| 15 | * version of the code. Original copyright preserved |
| 16 | * below. |
| 17 | * Steve Whitehouse: Some bug fixes, cleaning up some code to make it |
| 18 | * compatible with my routing layer. |
| 19 | * Steve Whitehouse: Merging changes from Eduardo Serrat and Patrick |
| 20 | * Caulfield. |
| 21 | * Steve Whitehouse: Further bug fixes, checking module code still works |
| 22 | * with new routing layer. |
| 23 | * Steve Whitehouse: Additional set/get_sockopt() calls. |
| 24 | * Steve Whitehouse: Fixed TIOCINQ ioctl to be same as Eduardo's new |
| 25 | * code. |
| 26 | * Steve Whitehouse: recvmsg() changed to try and behave in a POSIX like |
| 27 | * way. Didn't manage it entirely, but its better. |
| 28 | * Steve Whitehouse: ditto for sendmsg(). |
| 29 | * Steve Whitehouse: A selection of bug fixes to various things. |
| 30 | * Steve Whitehouse: Added TIOCOUTQ ioctl. |
| 31 | * Steve Whitehouse: Fixes to username2sockaddr & sockaddr2username. |
| 32 | * Steve Whitehouse: Fixes to connect() error returns. |
| 33 | * Patrick Caulfield: Fixes to delayed acceptance logic. |
| 34 | * David S. Miller: New socket locking |
| 35 | * Steve Whitehouse: Socket list hashing/locking |
| 36 | * Arnaldo C. Melo: use capable, not suser |
| 37 | * Steve Whitehouse: Removed unused code. Fix to use sk->allocation |
| 38 | * when required. |
| 39 | * Patrick Caulfield: /proc/net/decnet now has object name/number |
| 40 | * Steve Whitehouse: Fixed local port allocation, hashed sk list |
| 41 | * Matthew Wilcox: Fixes for dn_ioctl() |
| 42 | * Steve Whitehouse: New connect/accept logic to allow timeouts and |
| 43 | * prepare for sendpage etc. |
| 44 | */ |
| 45 | |
| 46 | |
| 47 | /****************************************************************************** |
| 48 | (c) 1995-1998 E.M. Serrat emserrat@geocities.com |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | HISTORY: |
| 52 | |
| 53 | Version Kernel Date Author/Comments |
| 54 | ------- ------ ---- --------------- |
| 55 | Version 0.0.1 2.0.30 01-dic-97 Eduardo Marcelo Serrat |
| 56 | (emserrat@geocities.com) |
| 57 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 58 | First Development of DECnet Socket La- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | yer for Linux. Only supports outgoing |
| 60 | connections. |
| 61 | |
| 62 | Version 0.0.2 2.1.105 20-jun-98 Patrick J. Caulfield |
| 63 | (patrick@pandh.demon.co.uk) |
| 64 | |
| 65 | Port to new kernel development version. |
| 66 | |
| 67 | Version 0.0.3 2.1.106 25-jun-98 Eduardo Marcelo Serrat |
| 68 | (emserrat@geocities.com) |
| 69 | _ |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 70 | Added support for incoming connections |
| 71 | so we can start developing server apps |
| 72 | on Linux. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | - |
| 74 | Module Support |
| 75 | Version 0.0.4 2.1.109 21-jul-98 Eduardo Marcelo Serrat |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 76 | (emserrat@geocities.com) |
| 77 | _ |
| 78 | Added support for X11R6.4. Now we can |
| 79 | use DECnet transport for X on Linux!!! |
| 80 | - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | Version 0.0.5 2.1.110 01-aug-98 Eduardo Marcelo Serrat |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 82 | (emserrat@geocities.com) |
| 83 | Removed bugs on flow control |
| 84 | Removed bugs on incoming accessdata |
| 85 | order |
| 86 | - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | Version 0.0.6 2.1.110 07-aug-98 Eduardo Marcelo Serrat |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 88 | dn_recvmsg fixes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 90 | Patrick J. Caulfield |
| 91 | dn_bind fixes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | *******************************************************************************/ |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #include <linux/module.h> |
| 95 | #include <linux/errno.h> |
| 96 | #include <linux/types.h> |
| 97 | #include <linux/slab.h> |
| 98 | #include <linux/socket.h> |
| 99 | #include <linux/in.h> |
| 100 | #include <linux/kernel.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 101 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | #include <linux/timer.h> |
| 103 | #include <linux/string.h> |
| 104 | #include <linux/sockios.h> |
| 105 | #include <linux/net.h> |
| 106 | #include <linux/netdevice.h> |
| 107 | #include <linux/inet.h> |
| 108 | #include <linux/route.h> |
| 109 | #include <linux/netfilter.h> |
| 110 | #include <linux/seq_file.h> |
| 111 | #include <net/sock.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 112 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | #include <net/flow.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | #include <asm/ioctls.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 115 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | #include <linux/mm.h> |
| 117 | #include <linux/interrupt.h> |
| 118 | #include <linux/proc_fs.h> |
| 119 | #include <linux/stat.h> |
| 120 | #include <linux/init.h> |
| 121 | #include <linux/poll.h> |
Himangi Saraogi | c0b8023 | 2014-08-20 23:24:40 +0530 | [diff] [blame] | 122 | #include <linux/jiffies.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 123 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | #include <net/neighbour.h> |
| 125 | #include <net/dst.h> |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 126 | #include <net/fib_rules.h> |
Gao Feng | 9c28286 | 2017-03-04 22:10:28 +0800 | [diff] [blame] | 127 | #include <net/tcp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | #include <net/dn.h> |
| 129 | #include <net/dn_nsp.h> |
| 130 | #include <net/dn_dev.h> |
| 131 | #include <net/dn_route.h> |
| 132 | #include <net/dn_fib.h> |
| 133 | #include <net/dn_neigh.h> |
| 134 | |
| 135 | struct dn_sock { |
| 136 | struct sock sk; |
| 137 | struct dn_scp scp; |
| 138 | }; |
| 139 | |
| 140 | static void dn_keepalive(struct sock *sk); |
| 141 | |
| 142 | #define DN_SK_HASH_SHIFT 8 |
| 143 | #define DN_SK_HASH_SIZE (1 << DN_SK_HASH_SHIFT) |
| 144 | #define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1) |
| 145 | |
| 146 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 147 | static const struct proto_ops dn_proto_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | static DEFINE_RWLOCK(dn_hash_lock); |
| 149 | static struct hlist_head dn_sk_hash[DN_SK_HASH_SIZE]; |
| 150 | static struct hlist_head dn_wild_sk; |
Eric Dumazet | 8d987e5 | 2010-11-09 23:24:26 +0000 | [diff] [blame] | 151 | static atomic_long_t decnet_memory_allocated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 153 | static int __dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen, int flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | static int __dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen, int flags); |
| 155 | |
| 156 | static struct hlist_head *dn_find_list(struct sock *sk) |
| 157 | { |
| 158 | struct dn_scp *scp = DN_SK(sk); |
| 159 | |
| 160 | if (scp->addr.sdn_flags & SDF_WILD) |
| 161 | return hlist_empty(&dn_wild_sk) ? &dn_wild_sk : NULL; |
| 162 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 163 | return &dn_sk_hash[le16_to_cpu(scp->addrloc) & DN_SK_HASH_MASK]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 166 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | * Valid ports are those greater than zero and not already in use. |
| 168 | */ |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 169 | static int check_port(__le16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
| 171 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | if (port == 0) |
| 174 | return -1; |
| 175 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 176 | sk_for_each(sk, &dn_sk_hash[le16_to_cpu(port) & DN_SK_HASH_MASK]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | struct dn_scp *scp = DN_SK(sk); |
| 178 | if (scp->addrloc == port) |
| 179 | return -1; |
| 180 | } |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static unsigned short port_alloc(struct sock *sk) |
| 185 | { |
| 186 | struct dn_scp *scp = DN_SK(sk); |
Colin Ian King | 99310e7 | 2018-11-13 14:18:17 +0000 | [diff] [blame] | 187 | static unsigned short port = 0x2000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | unsigned short i_port = port; |
| 189 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 190 | while(check_port(cpu_to_le16(++port)) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (port == i_port) |
| 192 | return 0; |
| 193 | } |
| 194 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 195 | scp->addrloc = cpu_to_le16(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Since this is only ever called from user |
| 202 | * level, we don't need a write_lock() version |
| 203 | * of this. |
| 204 | */ |
| 205 | static int dn_hash_sock(struct sock *sk) |
| 206 | { |
| 207 | struct dn_scp *scp = DN_SK(sk); |
| 208 | struct hlist_head *list; |
| 209 | int rv = -EUSERS; |
| 210 | |
| 211 | BUG_ON(sk_hashed(sk)); |
| 212 | |
| 213 | write_lock_bh(&dn_hash_lock); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | if (!scp->addrloc && !port_alloc(sk)) |
| 216 | goto out; |
| 217 | |
| 218 | rv = -EADDRINUSE; |
| 219 | if ((list = dn_find_list(sk)) == NULL) |
| 220 | goto out; |
| 221 | |
| 222 | sk_add_node(sk, list); |
| 223 | rv = 0; |
| 224 | out: |
| 225 | write_unlock_bh(&dn_hash_lock); |
| 226 | return rv; |
| 227 | } |
| 228 | |
| 229 | static void dn_unhash_sock(struct sock *sk) |
| 230 | { |
| 231 | write_lock(&dn_hash_lock); |
| 232 | sk_del_node_init(sk); |
| 233 | write_unlock(&dn_hash_lock); |
| 234 | } |
| 235 | |
| 236 | static void dn_unhash_sock_bh(struct sock *sk) |
| 237 | { |
| 238 | write_lock_bh(&dn_hash_lock); |
| 239 | sk_del_node_init(sk); |
| 240 | write_unlock_bh(&dn_hash_lock); |
| 241 | } |
| 242 | |
| 243 | static struct hlist_head *listen_hash(struct sockaddr_dn *addr) |
| 244 | { |
| 245 | int i; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 246 | unsigned int hash = addr->sdn_objnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | if (hash == 0) { |
| 249 | hash = addr->sdn_objnamel; |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 250 | for(i = 0; i < le16_to_cpu(addr->sdn_objnamel); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | hash ^= addr->sdn_objname[i]; |
| 252 | hash ^= (hash << 3); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return &dn_sk_hash[hash & DN_SK_HASH_MASK]; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Called to transform a socket from bound (i.e. with a local address) |
| 261 | * into a listening socket (doesn't need a local port number) and rehashes |
| 262 | * based upon the object name/number. |
| 263 | */ |
| 264 | static void dn_rehash_sock(struct sock *sk) |
| 265 | { |
| 266 | struct hlist_head *list; |
| 267 | struct dn_scp *scp = DN_SK(sk); |
| 268 | |
| 269 | if (scp->addr.sdn_flags & SDF_WILD) |
| 270 | return; |
| 271 | |
| 272 | write_lock_bh(&dn_hash_lock); |
| 273 | sk_del_node_init(sk); |
| 274 | DN_SK(sk)->addrloc = 0; |
| 275 | list = listen_hash(&DN_SK(sk)->addr); |
| 276 | sk_add_node(sk, list); |
| 277 | write_unlock_bh(&dn_hash_lock); |
| 278 | } |
| 279 | |
| 280 | int dn_sockaddr2username(struct sockaddr_dn *sdn, unsigned char *buf, unsigned char type) |
| 281 | { |
| 282 | int len = 2; |
| 283 | |
| 284 | *buf++ = type; |
| 285 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 286 | switch (type) { |
| 287 | case 0: |
| 288 | *buf++ = sdn->sdn_objnum; |
| 289 | break; |
| 290 | case 1: |
| 291 | *buf++ = 0; |
| 292 | *buf++ = le16_to_cpu(sdn->sdn_objnamel); |
| 293 | memcpy(buf, sdn->sdn_objname, le16_to_cpu(sdn->sdn_objnamel)); |
| 294 | len = 3 + le16_to_cpu(sdn->sdn_objnamel); |
| 295 | break; |
| 296 | case 2: |
| 297 | memset(buf, 0, 5); |
| 298 | buf += 5; |
| 299 | *buf++ = le16_to_cpu(sdn->sdn_objnamel); |
| 300 | memcpy(buf, sdn->sdn_objname, le16_to_cpu(sdn->sdn_objnamel)); |
| 301 | len = 7 + le16_to_cpu(sdn->sdn_objnamel); |
| 302 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | return len; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * On reception of usernames, we handle types 1 and 0 for destination |
| 310 | * addresses only. Types 2 and 4 are used for source addresses, but the |
| 311 | * UIC, GIC are ignored and they are both treated the same way. Type 3 |
| 312 | * is never used as I've no idea what its purpose might be or what its |
| 313 | * format is. |
| 314 | */ |
| 315 | int dn_username2sockaddr(unsigned char *data, int len, struct sockaddr_dn *sdn, unsigned char *fmt) |
| 316 | { |
| 317 | unsigned char type; |
| 318 | int size = len; |
| 319 | int namel = 12; |
| 320 | |
| 321 | sdn->sdn_objnum = 0; |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 322 | sdn->sdn_objnamel = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | memset(sdn->sdn_objname, 0, DN_MAXOBJL); |
| 324 | |
| 325 | if (len < 2) |
| 326 | return -1; |
| 327 | |
| 328 | len -= 2; |
| 329 | *fmt = *data++; |
| 330 | type = *data++; |
| 331 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 332 | switch (*fmt) { |
| 333 | case 0: |
| 334 | sdn->sdn_objnum = type; |
| 335 | return 2; |
| 336 | case 1: |
| 337 | namel = 16; |
| 338 | break; |
| 339 | case 2: |
| 340 | len -= 4; |
| 341 | data += 4; |
| 342 | break; |
| 343 | case 4: |
| 344 | len -= 8; |
| 345 | data += 8; |
| 346 | break; |
| 347 | default: |
| 348 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | len -= 1; |
| 352 | |
| 353 | if (len < 0) |
| 354 | return -1; |
| 355 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 356 | sdn->sdn_objnamel = cpu_to_le16(*data++); |
| 357 | len -= le16_to_cpu(sdn->sdn_objnamel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 359 | if ((len < 0) || (le16_to_cpu(sdn->sdn_objnamel) > namel)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return -1; |
| 361 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 362 | memcpy(sdn->sdn_objname, data, le16_to_cpu(sdn->sdn_objnamel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | return size - len; |
| 365 | } |
| 366 | |
| 367 | struct sock *dn_sklist_find_listener(struct sockaddr_dn *addr) |
| 368 | { |
| 369 | struct hlist_head *list = listen_hash(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | struct sock *sk; |
| 371 | |
| 372 | read_lock(&dn_hash_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 373 | sk_for_each(sk, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | struct dn_scp *scp = DN_SK(sk); |
| 375 | if (sk->sk_state != TCP_LISTEN) |
| 376 | continue; |
| 377 | if (scp->addr.sdn_objnum) { |
| 378 | if (scp->addr.sdn_objnum != addr->sdn_objnum) |
| 379 | continue; |
| 380 | } else { |
| 381 | if (addr->sdn_objnum) |
| 382 | continue; |
| 383 | if (scp->addr.sdn_objnamel != addr->sdn_objnamel) |
| 384 | continue; |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 385 | if (memcmp(scp->addr.sdn_objname, addr->sdn_objname, le16_to_cpu(addr->sdn_objnamel)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | continue; |
| 387 | } |
| 388 | sock_hold(sk); |
| 389 | read_unlock(&dn_hash_lock); |
| 390 | return sk; |
| 391 | } |
| 392 | |
| 393 | sk = sk_head(&dn_wild_sk); |
| 394 | if (sk) { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 395 | if (sk->sk_state == TCP_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | sock_hold(sk); |
| 397 | else |
| 398 | sk = NULL; |
| 399 | } |
| 400 | |
| 401 | read_unlock(&dn_hash_lock); |
| 402 | return sk; |
| 403 | } |
| 404 | |
| 405 | struct sock *dn_find_by_skb(struct sk_buff *skb) |
| 406 | { |
| 407 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 408 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | struct dn_scp *scp; |
| 410 | |
| 411 | read_lock(&dn_hash_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 412 | sk_for_each(sk, &dn_sk_hash[le16_to_cpu(cb->dst_port) & DN_SK_HASH_MASK]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | scp = DN_SK(sk); |
| 414 | if (cb->src != dn_saddr2dn(&scp->peer)) |
| 415 | continue; |
| 416 | if (cb->dst_port != scp->addrloc) |
| 417 | continue; |
| 418 | if (scp->addrrem && (cb->src_port != scp->addrrem)) |
| 419 | continue; |
| 420 | sock_hold(sk); |
| 421 | goto found; |
| 422 | } |
| 423 | sk = NULL; |
| 424 | found: |
| 425 | read_unlock(&dn_hash_lock); |
| 426 | return sk; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | |
| 431 | static void dn_destruct(struct sock *sk) |
| 432 | { |
| 433 | struct dn_scp *scp = DN_SK(sk); |
| 434 | |
| 435 | skb_queue_purge(&scp->data_xmit_queue); |
| 436 | skb_queue_purge(&scp->other_xmit_queue); |
| 437 | skb_queue_purge(&scp->other_receive_queue); |
| 438 | |
Xin Long | 5869b8f | 2019-03-31 17:03:02 +0800 | [diff] [blame] | 439 | dst_release(rcu_dereference_protected(sk->sk_dst_cache, 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Eric Dumazet | 0604475 | 2017-06-07 13:29:12 -0700 | [diff] [blame] | 442 | static unsigned long dn_memory_pressure; |
Steven Whitehouse | 1f12bcc9 | 2005-12-05 13:42:06 -0800 | [diff] [blame] | 443 | |
Pavel Emelyanov | 5c52ba1 | 2008-07-16 20:28:10 -0700 | [diff] [blame] | 444 | static void dn_enter_memory_pressure(struct sock *sk) |
Steven Whitehouse | 1f12bcc9 | 2005-12-05 13:42:06 -0800 | [diff] [blame] | 445 | { |
| 446 | if (!dn_memory_pressure) { |
| 447 | dn_memory_pressure = 1; |
| 448 | } |
| 449 | } |
| 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | static struct proto dn_proto = { |
Steven Whitehouse | 1f12bcc9 | 2005-12-05 13:42:06 -0800 | [diff] [blame] | 452 | .name = "NSP", |
| 453 | .owner = THIS_MODULE, |
| 454 | .enter_memory_pressure = dn_enter_memory_pressure, |
| 455 | .memory_pressure = &dn_memory_pressure, |
| 456 | .memory_allocated = &decnet_memory_allocated, |
| 457 | .sysctl_mem = sysctl_decnet_mem, |
| 458 | .sysctl_wmem = sysctl_decnet_wmem, |
| 459 | .sysctl_rmem = sysctl_decnet_rmem, |
| 460 | .max_header = DN_MAX_NSP_DATA_HEADER + 64, |
| 461 | .obj_size = sizeof(struct dn_sock), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 464 | static struct sock *dn_alloc_sock(struct net *net, struct socket *sock, gfp_t gfp, int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | { |
| 466 | struct dn_scp *scp; |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 467 | struct sock *sk = sk_alloc(net, PF_DECnet, gfp, &dn_proto, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
| 469 | if (!sk) |
| 470 | goto out; |
| 471 | |
| 472 | if (sock) |
| 473 | sock->ops = &dn_proto_ops; |
| 474 | sock_init_data(sock, sk); |
| 475 | |
| 476 | sk->sk_backlog_rcv = dn_nsp_backlog_rcv; |
| 477 | sk->sk_destruct = dn_destruct; |
Tom Herbert | 28448b8 | 2014-05-23 08:47:19 -0700 | [diff] [blame] | 478 | sk->sk_no_check_tx = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | sk->sk_family = PF_DECnet; |
| 480 | sk->sk_protocol = 0; |
| 481 | sk->sk_allocation = gfp; |
Steven Whitehouse | 1f12bcc9 | 2005-12-05 13:42:06 -0800 | [diff] [blame] | 482 | sk->sk_sndbuf = sysctl_decnet_wmem[1]; |
| 483 | sk->sk_rcvbuf = sysctl_decnet_rmem[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
| 485 | /* Initialization of DECnet Session Control Port */ |
| 486 | scp = DN_SK(sk); |
| 487 | scp->state = DN_O; /* Open */ |
| 488 | scp->numdat = 1; /* Next data seg to tx */ |
| 489 | scp->numoth = 1; /* Next oth data to tx */ |
| 490 | scp->ackxmt_dat = 0; /* Last data seg ack'ed */ |
| 491 | scp->ackxmt_oth = 0; /* Last oth data ack'ed */ |
| 492 | scp->ackrcv_dat = 0; /* Highest data ack recv*/ |
| 493 | scp->ackrcv_oth = 0; /* Last oth data ack rec*/ |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 494 | scp->flowrem_sw = DN_SEND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | scp->flowloc_sw = DN_SEND; |
| 496 | scp->flowrem_dat = 0; |
| 497 | scp->flowrem_oth = 1; |
| 498 | scp->flowloc_dat = 0; |
| 499 | scp->flowloc_oth = 1; |
| 500 | scp->services_rem = 0; |
| 501 | scp->services_loc = 1 | NSP_FC_NONE; |
| 502 | scp->info_rem = 0; |
| 503 | scp->info_loc = 0x03; /* NSP version 4.1 */ |
| 504 | scp->segsize_rem = 230 - DN_MAX_NSP_DATA_HEADER; /* Default: Updated by remote segsize */ |
| 505 | scp->nonagle = 0; |
| 506 | scp->multi_ireq = 1; |
| 507 | scp->accept_mode = ACC_IMMED; |
| 508 | scp->addr.sdn_family = AF_DECnet; |
| 509 | scp->peer.sdn_family = AF_DECnet; |
| 510 | scp->accessdata.acc_accl = 5; |
| 511 | memcpy(scp->accessdata.acc_acc, "LINUX", 5); |
| 512 | |
| 513 | scp->max_window = NSP_MAX_WINDOW; |
| 514 | scp->snd_window = NSP_MIN_WINDOW; |
| 515 | scp->nsp_srtt = NSP_INITIAL_SRTT; |
| 516 | scp->nsp_rttvar = NSP_INITIAL_RTTVAR; |
| 517 | scp->nsp_rxtshift = 0; |
| 518 | |
| 519 | skb_queue_head_init(&scp->data_xmit_queue); |
| 520 | skb_queue_head_init(&scp->other_xmit_queue); |
| 521 | skb_queue_head_init(&scp->other_receive_queue); |
| 522 | |
| 523 | scp->persist = 0; |
| 524 | scp->persist_fxn = NULL; |
| 525 | scp->keepalive = 10 * HZ; |
| 526 | scp->keepalive_fxn = dn_keepalive; |
| 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | dn_start_slow_timer(sk); |
| 529 | out: |
| 530 | return sk; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Keepalive timer. |
| 535 | * FIXME: Should respond to SO_KEEPALIVE etc. |
| 536 | */ |
| 537 | static void dn_keepalive(struct sock *sk) |
| 538 | { |
| 539 | struct dn_scp *scp = DN_SK(sk); |
| 540 | |
| 541 | /* |
| 542 | * By checking the other_data transmit queue is empty |
| 543 | * we are double checking that we are not sending too |
| 544 | * many of these keepalive frames. |
| 545 | */ |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 546 | if (skb_queue_empty(&scp->other_xmit_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | dn_nsp_send_link(sk, DN_NOCHANGE, 0); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | /* |
| 552 | * Timer for shutdown/destroyed sockets. |
| 553 | * When socket is dead & no packets have been sent for a |
| 554 | * certain amount of time, they are removed by this |
| 555 | * routine. Also takes care of sending out DI & DC |
| 556 | * frames at correct times. |
| 557 | */ |
| 558 | int dn_destroy_timer(struct sock *sk) |
| 559 | { |
| 560 | struct dn_scp *scp = DN_SK(sk); |
| 561 | |
| 562 | scp->persist = dn_nsp_persist(sk); |
| 563 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 564 | switch (scp->state) { |
| 565 | case DN_DI: |
| 566 | dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC); |
| 567 | if (scp->nsp_rxtshift >= decnet_di_count) |
| 568 | scp->state = DN_CN; |
| 569 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 571 | case DN_DR: |
| 572 | dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC); |
| 573 | if (scp->nsp_rxtshift >= decnet_dr_count) |
| 574 | scp->state = DN_DRC; |
| 575 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 577 | case DN_DN: |
| 578 | if (scp->nsp_rxtshift < decnet_dn_count) { |
| 579 | /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */ |
| 580 | dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, |
| 581 | GFP_ATOMIC); |
| 582 | return 0; |
| 583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | scp->persist = (HZ * decnet_time_wait); |
| 587 | |
| 588 | if (sk->sk_socket) |
| 589 | return 0; |
| 590 | |
Himangi Saraogi | c0b8023 | 2014-08-20 23:24:40 +0530 | [diff] [blame] | 591 | if (time_after_eq(jiffies, scp->stamp + HZ * decnet_time_wait)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | dn_unhash_sock(sk); |
| 593 | sock_put(sk); |
| 594 | return 1; |
| 595 | } |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static void dn_destroy_sock(struct sock *sk) |
| 601 | { |
| 602 | struct dn_scp *scp = DN_SK(sk); |
| 603 | |
| 604 | scp->nsp_rxtshift = 0; /* reset back off */ |
| 605 | |
| 606 | if (sk->sk_socket) { |
| 607 | if (sk->sk_socket->state != SS_UNCONNECTED) |
| 608 | sk->sk_socket->state = SS_DISCONNECTING; |
| 609 | } |
| 610 | |
| 611 | sk->sk_state = TCP_CLOSE; |
| 612 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 613 | switch (scp->state) { |
| 614 | case DN_DN: |
| 615 | dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, |
| 616 | sk->sk_allocation); |
| 617 | scp->persist_fxn = dn_destroy_timer; |
| 618 | scp->persist = dn_nsp_persist(sk); |
| 619 | break; |
| 620 | case DN_CR: |
| 621 | scp->state = DN_DR; |
| 622 | goto disc_reject; |
| 623 | case DN_RUN: |
| 624 | scp->state = DN_DI; |
Gustavo A. R. Silva | 86e58cc | 2017-10-16 15:11:22 -0500 | [diff] [blame] | 625 | /* fall through */ |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 626 | case DN_DI: |
| 627 | case DN_DR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | disc_reject: |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 629 | dn_nsp_send_disc(sk, NSP_DISCINIT, 0, sk->sk_allocation); |
Gustavo A. R. Silva | 86e58cc | 2017-10-16 15:11:22 -0500 | [diff] [blame] | 630 | /* fall through */ |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 631 | case DN_NC: |
| 632 | case DN_NR: |
| 633 | case DN_RJ: |
| 634 | case DN_DIC: |
| 635 | case DN_CN: |
| 636 | case DN_DRC: |
| 637 | case DN_CI: |
| 638 | case DN_CD: |
| 639 | scp->persist_fxn = dn_destroy_timer; |
| 640 | scp->persist = dn_nsp_persist(sk); |
| 641 | break; |
| 642 | default: |
| 643 | printk(KERN_DEBUG "DECnet: dn_destroy_sock passed socket in invalid state\n"); |
Gustavo A. R. Silva | 86e58cc | 2017-10-16 15:11:22 -0500 | [diff] [blame] | 644 | /* fall through */ |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 645 | case DN_O: |
| 646 | dn_stop_slow_timer(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 648 | dn_unhash_sock_bh(sk); |
| 649 | sock_put(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 651 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 655 | char *dn_addr2asc(__u16 addr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
| 657 | unsigned short node, area; |
| 658 | |
| 659 | node = addr & 0x03ff; |
| 660 | area = addr >> 10; |
| 661 | sprintf(buf, "%hd.%hd", area, node); |
| 662 | |
| 663 | return buf; |
| 664 | } |
| 665 | |
| 666 | |
| 667 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 668 | static int dn_create(struct net *net, struct socket *sock, int protocol, |
| 669 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
| 671 | struct sock *sk; |
| 672 | |
Mat Martineau | e9cdced | 2020-01-09 07:59:14 -0800 | [diff] [blame] | 673 | if (protocol < 0 || protocol > U8_MAX) |
Hannes Frederic Sowa | 79462ad0 | 2015-12-14 22:03:39 +0100 | [diff] [blame] | 674 | return -EINVAL; |
| 675 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 676 | if (!net_eq(net, &init_net)) |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 677 | return -EAFNOSUPPORT; |
| 678 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 679 | switch (sock->type) { |
| 680 | case SOCK_SEQPACKET: |
| 681 | if (protocol != DNPROTO_NSP) |
| 682 | return -EPROTONOSUPPORT; |
| 683 | break; |
| 684 | case SOCK_STREAM: |
| 685 | break; |
| 686 | default: |
| 687 | return -ESOCKTNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 691 | if ((sk = dn_alloc_sock(net, sock, GFP_KERNEL, kern)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | return -ENOBUFS; |
| 693 | |
| 694 | sk->sk_protocol = protocol; |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | static int |
| 701 | dn_release(struct socket *sock) |
| 702 | { |
| 703 | struct sock *sk = sock->sk; |
| 704 | |
| 705 | if (sk) { |
| 706 | sock_orphan(sk); |
| 707 | sock_hold(sk); |
| 708 | lock_sock(sk); |
| 709 | dn_destroy_sock(sk); |
| 710 | release_sock(sk); |
| 711 | sock_put(sk); |
| 712 | } |
| 713 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 714 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static int dn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 718 | { |
| 719 | struct sock *sk = sock->sk; |
| 720 | struct dn_scp *scp = DN_SK(sk); |
| 721 | struct sockaddr_dn *saddr = (struct sockaddr_dn *)uaddr; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 722 | struct net_device *dev, *ldev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | int rv; |
| 724 | |
| 725 | if (addr_len != sizeof(struct sockaddr_dn)) |
| 726 | return -EINVAL; |
| 727 | |
| 728 | if (saddr->sdn_family != AF_DECnet) |
| 729 | return -EINVAL; |
| 730 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 731 | if (le16_to_cpu(saddr->sdn_nodeaddrl) && (le16_to_cpu(saddr->sdn_nodeaddrl) != 2)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | return -EINVAL; |
| 733 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 734 | if (le16_to_cpu(saddr->sdn_objnamel) > DN_MAXOBJL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return -EINVAL; |
| 736 | |
| 737 | if (saddr->sdn_flags & ~SDF_WILD) |
| 738 | return -EINVAL; |
| 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (!capable(CAP_NET_BIND_SERVICE) && (saddr->sdn_objnum || |
| 741 | (saddr->sdn_flags & SDF_WILD))) |
| 742 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
| 744 | if (!(saddr->sdn_flags & SDF_WILD)) { |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 745 | if (le16_to_cpu(saddr->sdn_nodeaddrl)) { |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 746 | rcu_read_lock(); |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 747 | ldev = NULL; |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 748 | for_each_netdev_rcu(&init_net, dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | if (!dev->dn_ptr) |
| 750 | continue; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 751 | if (dn_dev_islocal(dev, dn_saddr2dn(saddr))) { |
| 752 | ldev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | break; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 754 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 756 | rcu_read_unlock(); |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 757 | if (ldev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | return -EADDRNOTAVAIL; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | rv = -EINVAL; |
| 763 | lock_sock(sk); |
| 764 | if (sock_flag(sk, SOCK_ZAPPED)) { |
| 765 | memcpy(&scp->addr, saddr, addr_len); |
| 766 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 767 | |
| 768 | rv = dn_hash_sock(sk); |
| 769 | if (rv) |
| 770 | sock_set_flag(sk, SOCK_ZAPPED); |
| 771 | } |
| 772 | release_sock(sk); |
| 773 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 774 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | |
| 778 | static int dn_auto_bind(struct socket *sock) |
| 779 | { |
| 780 | struct sock *sk = sock->sk; |
| 781 | struct dn_scp *scp = DN_SK(sk); |
| 782 | int rv; |
| 783 | |
| 784 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 785 | |
| 786 | scp->addr.sdn_flags = 0; |
| 787 | scp->addr.sdn_objnum = 0; |
| 788 | |
| 789 | /* |
| 790 | * This stuff is to keep compatibility with Eduardo's |
| 791 | * patch. I hope I can dispense with it shortly... |
| 792 | */ |
| 793 | if ((scp->accessdata.acc_accl != 0) && |
| 794 | (scp->accessdata.acc_accl <= 12)) { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 795 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 796 | scp->addr.sdn_objnamel = cpu_to_le16(scp->accessdata.acc_accl); |
| 797 | memcpy(scp->addr.sdn_objname, scp->accessdata.acc_acc, le16_to_cpu(scp->addr.sdn_objnamel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
| 799 | scp->accessdata.acc_accl = 0; |
| 800 | memset(scp->accessdata.acc_acc, 0, 40); |
| 801 | } |
| 802 | /* End of compatibility stuff */ |
| 803 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 804 | scp->addr.sdn_add.a_len = cpu_to_le16(2); |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 805 | rv = dn_dev_bind_default((__le16 *)scp->addr.sdn_add.a_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | if (rv == 0) { |
| 807 | rv = dn_hash_sock(sk); |
| 808 | if (rv) |
| 809 | sock_set_flag(sk, SOCK_ZAPPED); |
| 810 | } |
| 811 | |
| 812 | return rv; |
| 813 | } |
| 814 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 815 | static int dn_confirm_accept(struct sock *sk, long *timeo, gfp_t allocation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | { |
| 817 | struct dn_scp *scp = DN_SK(sk); |
| 818 | DEFINE_WAIT(wait); |
| 819 | int err; |
| 820 | |
| 821 | if (scp->state != DN_CR) |
| 822 | return -EINVAL; |
| 823 | |
| 824 | scp->state = DN_CC; |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 825 | scp->segsize_loc = dst_metric_advmss(__sk_dst_get(sk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | dn_send_conn_conf(sk, allocation); |
| 827 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 828 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | for(;;) { |
| 830 | release_sock(sk); |
| 831 | if (scp->state == DN_CC) |
| 832 | *timeo = schedule_timeout(*timeo); |
| 833 | lock_sock(sk); |
| 834 | err = 0; |
| 835 | if (scp->state == DN_RUN) |
| 836 | break; |
| 837 | err = sock_error(sk); |
| 838 | if (err) |
| 839 | break; |
| 840 | err = sock_intr_errno(*timeo); |
| 841 | if (signal_pending(current)) |
| 842 | break; |
| 843 | err = -EAGAIN; |
| 844 | if (!*timeo) |
| 845 | break; |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 846 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 848 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | if (err == 0) { |
| 850 | sk->sk_socket->state = SS_CONNECTED; |
| 851 | } else if (scp->state != DN_CC) { |
| 852 | sk->sk_socket->state = SS_UNCONNECTED; |
| 853 | } |
| 854 | return err; |
| 855 | } |
| 856 | |
| 857 | static int dn_wait_run(struct sock *sk, long *timeo) |
| 858 | { |
| 859 | struct dn_scp *scp = DN_SK(sk); |
| 860 | DEFINE_WAIT(wait); |
| 861 | int err = 0; |
| 862 | |
| 863 | if (scp->state == DN_RUN) |
| 864 | goto out; |
| 865 | |
| 866 | if (!*timeo) |
| 867 | return -EALREADY; |
| 868 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 869 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | for(;;) { |
| 871 | release_sock(sk); |
| 872 | if (scp->state == DN_CI || scp->state == DN_CC) |
| 873 | *timeo = schedule_timeout(*timeo); |
| 874 | lock_sock(sk); |
| 875 | err = 0; |
| 876 | if (scp->state == DN_RUN) |
| 877 | break; |
| 878 | err = sock_error(sk); |
| 879 | if (err) |
| 880 | break; |
| 881 | err = sock_intr_errno(*timeo); |
| 882 | if (signal_pending(current)) |
| 883 | break; |
| 884 | err = -ETIMEDOUT; |
| 885 | if (!*timeo) |
| 886 | break; |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 887 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | } |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 889 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | out: |
| 891 | if (err == 0) { |
| 892 | sk->sk_socket->state = SS_CONNECTED; |
| 893 | } else if (scp->state != DN_CI && scp->state != DN_CC) { |
| 894 | sk->sk_socket->state = SS_UNCONNECTED; |
| 895 | } |
| 896 | return err; |
| 897 | } |
| 898 | |
| 899 | static int __dn_connect(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags) |
| 900 | { |
| 901 | struct socket *sock = sk->sk_socket; |
| 902 | struct dn_scp *scp = DN_SK(sk); |
| 903 | int err = -EISCONN; |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 904 | struct flowidn fld; |
Cong Wang | cec771d | 2013-01-22 21:09:50 +0000 | [diff] [blame] | 905 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
| 907 | if (sock->state == SS_CONNECTED) |
| 908 | goto out; |
| 909 | |
| 910 | if (sock->state == SS_CONNECTING) { |
| 911 | err = 0; |
| 912 | if (scp->state == DN_RUN) { |
| 913 | sock->state = SS_CONNECTED; |
| 914 | goto out; |
| 915 | } |
| 916 | err = -ECONNREFUSED; |
| 917 | if (scp->state != DN_CI && scp->state != DN_CC) { |
| 918 | sock->state = SS_UNCONNECTED; |
| 919 | goto out; |
| 920 | } |
| 921 | return dn_wait_run(sk, timeo); |
| 922 | } |
| 923 | |
| 924 | err = -EINVAL; |
| 925 | if (scp->state != DN_O) |
| 926 | goto out; |
| 927 | |
| 928 | if (addr == NULL || addrlen != sizeof(struct sockaddr_dn)) |
| 929 | goto out; |
| 930 | if (addr->sdn_family != AF_DECnet) |
| 931 | goto out; |
| 932 | if (addr->sdn_flags & SDF_WILD) |
| 933 | goto out; |
| 934 | |
| 935 | if (sock_flag(sk, SOCK_ZAPPED)) { |
| 936 | err = dn_auto_bind(sk->sk_socket); |
| 937 | if (err) |
| 938 | goto out; |
| 939 | } |
| 940 | |
| 941 | memcpy(&scp->peer, addr, sizeof(struct sockaddr_dn)); |
| 942 | |
| 943 | err = -EHOSTUNREACH; |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 944 | memset(&fld, 0, sizeof(fld)); |
| 945 | fld.flowidn_oif = sk->sk_bound_dev_if; |
| 946 | fld.daddr = dn_saddr2dn(&scp->peer); |
| 947 | fld.saddr = dn_saddr2dn(&scp->addr); |
| 948 | dn_sk_ports_copy(&fld, scp); |
| 949 | fld.flowidn_proto = DNPROTO_NSP; |
| 950 | if (dn_route_output_sock(&sk->sk_dst_cache, &fld, sk, flags) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | goto out; |
Cong Wang | cec771d | 2013-01-22 21:09:50 +0000 | [diff] [blame] | 952 | dst = __sk_dst_get(sk); |
| 953 | sk->sk_route_caps = dst->dev->features; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | sock->state = SS_CONNECTING; |
| 955 | scp->state = DN_CI; |
Cong Wang | cec771d | 2013-01-22 21:09:50 +0000 | [diff] [blame] | 956 | scp->segsize_loc = dst_metric_advmss(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
| 958 | dn_nsp_send_conninit(sk, NSP_CI); |
| 959 | err = -EINPROGRESS; |
| 960 | if (*timeo) { |
| 961 | err = dn_wait_run(sk, timeo); |
| 962 | } |
| 963 | out: |
| 964 | return err; |
| 965 | } |
| 966 | |
| 967 | static int dn_connect(struct socket *sock, struct sockaddr *uaddr, int addrlen, int flags) |
| 968 | { |
| 969 | struct sockaddr_dn *addr = (struct sockaddr_dn *)uaddr; |
| 970 | struct sock *sk = sock->sk; |
| 971 | int err; |
| 972 | long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); |
| 973 | |
| 974 | lock_sock(sk); |
| 975 | err = __dn_connect(sk, addr, addrlen, &timeo, 0); |
| 976 | release_sock(sk); |
| 977 | |
| 978 | return err; |
| 979 | } |
| 980 | |
| 981 | static inline int dn_check_state(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags) |
| 982 | { |
| 983 | struct dn_scp *scp = DN_SK(sk); |
| 984 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 985 | switch (scp->state) { |
| 986 | case DN_RUN: |
| 987 | return 0; |
| 988 | case DN_CR: |
| 989 | return dn_confirm_accept(sk, timeo, sk->sk_allocation); |
| 990 | case DN_CI: |
| 991 | case DN_CC: |
| 992 | return dn_wait_run(sk, timeo); |
| 993 | case DN_O: |
| 994 | return __dn_connect(sk, addr, addrlen, timeo, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | return -EINVAL; |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | static void dn_access_copy(struct sk_buff *skb, struct accessdata_dn *acc) |
| 1002 | { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1003 | unsigned char *ptr = skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1005 | acc->acc_userl = *ptr++; |
| 1006 | memcpy(&acc->acc_user, ptr, acc->acc_userl); |
| 1007 | ptr += acc->acc_userl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1009 | acc->acc_passl = *ptr++; |
| 1010 | memcpy(&acc->acc_pass, ptr, acc->acc_passl); |
| 1011 | ptr += acc->acc_passl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1013 | acc->acc_accl = *ptr++; |
| 1014 | memcpy(&acc->acc_acc, ptr, acc->acc_accl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1016 | skb_pull(skb, acc->acc_accl + acc->acc_passl + acc->acc_userl + 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
| 1018 | } |
| 1019 | |
| 1020 | static void dn_user_copy(struct sk_buff *skb, struct optdata_dn *opt) |
| 1021 | { |
Steven Whitehouse | 375d9d7 | 2006-11-07 15:09:17 -0800 | [diff] [blame] | 1022 | unsigned char *ptr = skb->data; |
| 1023 | u16 len = *ptr++; /* yes, it's 8bit on the wire */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | |
Steven Whitehouse | 375d9d7 | 2006-11-07 15:09:17 -0800 | [diff] [blame] | 1025 | BUG_ON(len > 16); /* we've checked the contents earlier */ |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 1026 | opt->opt_optl = cpu_to_le16(len); |
Steven Whitehouse | 375d9d7 | 2006-11-07 15:09:17 -0800 | [diff] [blame] | 1027 | opt->opt_status = 0; |
| 1028 | memcpy(opt->opt_data, ptr, len); |
| 1029 | skb_pull(skb, len + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static struct sk_buff *dn_wait_for_connect(struct sock *sk, long *timeo) |
| 1033 | { |
| 1034 | DEFINE_WAIT(wait); |
| 1035 | struct sk_buff *skb = NULL; |
| 1036 | int err = 0; |
| 1037 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 1038 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | for(;;) { |
| 1040 | release_sock(sk); |
| 1041 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 1042 | if (skb == NULL) { |
| 1043 | *timeo = schedule_timeout(*timeo); |
| 1044 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 1045 | } |
| 1046 | lock_sock(sk); |
| 1047 | if (skb != NULL) |
| 1048 | break; |
| 1049 | err = -EINVAL; |
| 1050 | if (sk->sk_state != TCP_LISTEN) |
| 1051 | break; |
| 1052 | err = sock_intr_errno(*timeo); |
| 1053 | if (signal_pending(current)) |
| 1054 | break; |
| 1055 | err = -EAGAIN; |
| 1056 | if (!*timeo) |
| 1057 | break; |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 1058 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 1060 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
| 1062 | return skb == NULL ? ERR_PTR(err) : skb; |
| 1063 | } |
| 1064 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 1065 | static int dn_accept(struct socket *sock, struct socket *newsock, int flags, |
| 1066 | bool kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | { |
| 1068 | struct sock *sk = sock->sk, *newsk; |
| 1069 | struct sk_buff *skb = NULL; |
| 1070 | struct dn_skb_cb *cb; |
| 1071 | unsigned char menuver; |
| 1072 | int err = 0; |
| 1073 | unsigned char type; |
| 1074 | long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1075 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
| 1077 | lock_sock(sk); |
| 1078 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1079 | if (sk->sk_state != TCP_LISTEN || DN_SK(sk)->state != DN_O) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | release_sock(sk); |
| 1081 | return -EINVAL; |
| 1082 | } |
| 1083 | |
| 1084 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 1085 | if (skb == NULL) { |
| 1086 | skb = dn_wait_for_connect(sk, &timeo); |
| 1087 | if (IS_ERR(skb)) { |
| 1088 | release_sock(sk); |
| 1089 | return PTR_ERR(skb); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | cb = DN_SKB_CB(skb); |
Eric Dumazet | 7976a11 | 2019-11-05 14:11:52 -0800 | [diff] [blame] | 1094 | sk_acceptq_removed(sk); |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 1095 | newsk = dn_alloc_sock(sock_net(sk), newsock, sk->sk_allocation, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | if (newsk == NULL) { |
| 1097 | release_sock(sk); |
| 1098 | kfree_skb(skb); |
| 1099 | return -ENOBUFS; |
| 1100 | } |
| 1101 | release_sock(sk); |
| 1102 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1103 | dst = skb_dst(skb); |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1104 | sk_dst_set(newsk, dst); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1105 | skb_dst_set(skb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1107 | DN_SK(newsk)->state = DN_CR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | DN_SK(newsk)->addrrem = cb->src_port; |
| 1109 | DN_SK(newsk)->services_rem = cb->services; |
| 1110 | DN_SK(newsk)->info_rem = cb->info; |
| 1111 | DN_SK(newsk)->segsize_rem = cb->segsize; |
| 1112 | DN_SK(newsk)->accept_mode = DN_SK(sk)->accept_mode; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | if (DN_SK(newsk)->segsize_rem < 230) |
| 1115 | DN_SK(newsk)->segsize_rem = 230; |
| 1116 | |
| 1117 | if ((DN_SK(newsk)->services_rem & NSP_FC_MASK) == NSP_FC_NONE) |
| 1118 | DN_SK(newsk)->max_window = decnet_no_fc_max_cwnd; |
| 1119 | |
| 1120 | newsk->sk_state = TCP_LISTEN; |
| 1121 | memcpy(&(DN_SK(newsk)->addr), &(DN_SK(sk)->addr), sizeof(struct sockaddr_dn)); |
| 1122 | |
| 1123 | /* |
| 1124 | * If we are listening on a wild socket, we don't want |
| 1125 | * the newly created socket on the wrong hash queue. |
| 1126 | */ |
| 1127 | DN_SK(newsk)->addr.sdn_flags &= ~SDF_WILD; |
| 1128 | |
| 1129 | skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->addr), &type)); |
| 1130 | skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->peer), &type)); |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 1131 | *(__le16 *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->src; |
| 1132 | *(__le16 *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
| 1134 | menuver = *skb->data; |
| 1135 | skb_pull(skb, 1); |
| 1136 | |
| 1137 | if (menuver & DN_MENUVER_ACC) |
| 1138 | dn_access_copy(skb, &(DN_SK(newsk)->accessdata)); |
| 1139 | |
| 1140 | if (menuver & DN_MENUVER_USR) |
| 1141 | dn_user_copy(skb, &(DN_SK(newsk)->conndata_in)); |
| 1142 | |
| 1143 | if (menuver & DN_MENUVER_PRX) |
| 1144 | DN_SK(newsk)->peer.sdn_flags |= SDF_PROXY; |
| 1145 | |
| 1146 | if (menuver & DN_MENUVER_UIC) |
| 1147 | DN_SK(newsk)->peer.sdn_flags |= SDF_UICPROXY; |
| 1148 | |
| 1149 | kfree_skb(skb); |
| 1150 | |
| 1151 | memcpy(&(DN_SK(newsk)->conndata_out), &(DN_SK(sk)->conndata_out), |
| 1152 | sizeof(struct optdata_dn)); |
| 1153 | memcpy(&(DN_SK(newsk)->discdata_out), &(DN_SK(sk)->discdata_out), |
| 1154 | sizeof(struct optdata_dn)); |
| 1155 | |
| 1156 | lock_sock(newsk); |
| 1157 | err = dn_hash_sock(newsk); |
| 1158 | if (err == 0) { |
| 1159 | sock_reset_flag(newsk, SOCK_ZAPPED); |
| 1160 | dn_send_conn_ack(newsk); |
| 1161 | |
| 1162 | /* |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1163 | * Here we use sk->sk_allocation since although the conn conf is |
| 1164 | * for the newsk, the context is the old socket. |
| 1165 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | if (DN_SK(newsk)->accept_mode == ACC_IMMED) |
| 1167 | err = dn_confirm_accept(newsk, &timeo, |
| 1168 | sk->sk_allocation); |
| 1169 | } |
| 1170 | release_sock(newsk); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1171 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1175 | static int dn_getname(struct socket *sock, struct sockaddr *uaddr,int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | { |
| 1177 | struct sockaddr_dn *sa = (struct sockaddr_dn *)uaddr; |
| 1178 | struct sock *sk = sock->sk; |
| 1179 | struct dn_scp *scp = DN_SK(sk); |
| 1180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | lock_sock(sk); |
| 1182 | |
| 1183 | if (peer) { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1184 | if ((sock->state != SS_CONNECTED && |
| 1185 | sock->state != SS_CONNECTING) && |
Patrick McHardy | 52c41a3 | 2006-10-12 01:48:20 -0700 | [diff] [blame] | 1186 | scp->accept_mode == ACC_IMMED) { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1187 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | return -ENOTCONN; |
Patrick McHardy | 52c41a3 | 2006-10-12 01:48:20 -0700 | [diff] [blame] | 1189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | memcpy(sa, &scp->peer, sizeof(struct sockaddr_dn)); |
| 1192 | } else { |
| 1193 | memcpy(sa, &scp->addr, sizeof(struct sockaddr_dn)); |
| 1194 | } |
| 1195 | |
| 1196 | release_sock(sk); |
| 1197 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1198 | return sizeof(struct sockaddr_dn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1202 | static __poll_t dn_poll(struct file *file, struct socket *sock, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | { |
| 1204 | struct sock *sk = sock->sk; |
| 1205 | struct dn_scp *scp = DN_SK(sk); |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1206 | __poll_t mask = datagram_poll(file, sock, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 1208 | if (!skb_queue_empty_lockless(&scp->other_receive_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1209 | mask |= EPOLLRDBAND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | |
| 1211 | return mask; |
| 1212 | } |
| 1213 | |
| 1214 | static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 1215 | { |
| 1216 | struct sock *sk = sock->sk; |
| 1217 | struct dn_scp *scp = DN_SK(sk); |
| 1218 | int err = -EOPNOTSUPP; |
| 1219 | long amount = 0; |
| 1220 | struct sk_buff *skb; |
| 1221 | int val; |
| 1222 | |
| 1223 | switch(cmd) |
| 1224 | { |
| 1225 | case SIOCGIFADDR: |
| 1226 | case SIOCSIFADDR: |
| 1227 | return dn_dev_ioctl(cmd, (void __user *)arg); |
| 1228 | |
| 1229 | case SIOCATMARK: |
| 1230 | lock_sock(sk); |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 1231 | val = !skb_queue_empty(&scp->other_receive_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | if (scp->state != DN_RUN) |
| 1233 | val = -ENOTCONN; |
| 1234 | release_sock(sk); |
| 1235 | return val; |
| 1236 | |
| 1237 | case TIOCOUTQ: |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 1238 | amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | if (amount < 0) |
| 1240 | amount = 0; |
| 1241 | err = put_user(amount, (int __user *)arg); |
| 1242 | break; |
| 1243 | |
| 1244 | case TIOCINQ: |
| 1245 | lock_sock(sk); |
Hannes Eder | e57c624 | 2009-02-25 10:32:39 +0000 | [diff] [blame] | 1246 | skb = skb_peek(&scp->other_receive_queue); |
| 1247 | if (skb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | amount = skb->len; |
| 1249 | } else { |
David S. Miller | bec571e | 2009-05-28 16:43:52 -0700 | [diff] [blame] | 1250 | skb_queue_walk(&sk->sk_receive_queue, skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | amount += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | } |
| 1253 | release_sock(sk); |
| 1254 | err = put_user(amount, (int __user *)arg); |
| 1255 | break; |
| 1256 | |
| 1257 | default: |
Christoph Hellwig | b5e5fa5 | 2006-01-03 14:18:33 -0800 | [diff] [blame] | 1258 | err = -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | break; |
| 1260 | } |
| 1261 | |
| 1262 | return err; |
| 1263 | } |
| 1264 | |
| 1265 | static int dn_listen(struct socket *sock, int backlog) |
| 1266 | { |
| 1267 | struct sock *sk = sock->sk; |
| 1268 | int err = -EINVAL; |
| 1269 | |
| 1270 | lock_sock(sk); |
| 1271 | |
| 1272 | if (sock_flag(sk, SOCK_ZAPPED)) |
| 1273 | goto out; |
| 1274 | |
| 1275 | if ((DN_SK(sk)->state != DN_O) || (sk->sk_state == TCP_LISTEN)) |
| 1276 | goto out; |
| 1277 | |
| 1278 | sk->sk_max_ack_backlog = backlog; |
| 1279 | sk->sk_ack_backlog = 0; |
| 1280 | sk->sk_state = TCP_LISTEN; |
| 1281 | err = 0; |
| 1282 | dn_rehash_sock(sk); |
| 1283 | |
| 1284 | out: |
| 1285 | release_sock(sk); |
| 1286 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1287 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | |
| 1291 | static int dn_shutdown(struct socket *sock, int how) |
| 1292 | { |
| 1293 | struct sock *sk = sock->sk; |
| 1294 | struct dn_scp *scp = DN_SK(sk); |
| 1295 | int err = -ENOTCONN; |
| 1296 | |
| 1297 | lock_sock(sk); |
| 1298 | |
| 1299 | if (sock->state == SS_UNCONNECTED) |
| 1300 | goto out; |
| 1301 | |
| 1302 | err = 0; |
| 1303 | if (sock->state == SS_DISCONNECTING) |
| 1304 | goto out; |
| 1305 | |
| 1306 | err = -EINVAL; |
| 1307 | if (scp->state == DN_O) |
| 1308 | goto out; |
| 1309 | |
Xi Wang | 46b66d7 | 2012-08-26 16:37:07 +0000 | [diff] [blame] | 1310 | if (how != SHUT_RDWR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | goto out; |
| 1312 | |
Xi Wang | 46b66d7 | 2012-08-26 16:37:07 +0000 | [diff] [blame] | 1313 | sk->sk_shutdown = SHUTDOWN_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | dn_destroy_sock(sk); |
| 1315 | err = 0; |
| 1316 | |
| 1317 | out: |
| 1318 | release_sock(sk); |
| 1319 | |
| 1320 | return err; |
| 1321 | } |
| 1322 | |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1323 | static int dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | { |
| 1325 | struct sock *sk = sock->sk; |
| 1326 | int err; |
| 1327 | |
| 1328 | lock_sock(sk); |
| 1329 | err = __dn_setsockopt(sock, level, optname, optval, optlen, 0); |
| 1330 | release_sock(sk); |
Paolo Abeni | dfec091 | 2018-02-15 16:59:49 +0100 | [diff] [blame] | 1331 | #ifdef CONFIG_NETFILTER |
| 1332 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1333 | if (err == -ENOPROTOOPT && optname != DSO_LINKINFO && |
| 1334 | optname != DSO_STREAM && optname != DSO_SEQPACKET) |
| 1335 | err = nf_setsockopt(sk, PF_DECnet, optname, optval, optlen); |
| 1336 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
| 1338 | return err; |
| 1339 | } |
| 1340 | |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1341 | static int __dn_setsockopt(struct socket *sock, int level,int optname, char __user *optval, unsigned int optlen, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | { |
| 1343 | struct sock *sk = sock->sk; |
| 1344 | struct dn_scp *scp = DN_SK(sk); |
| 1345 | long timeo; |
| 1346 | union { |
| 1347 | struct optdata_dn opt; |
| 1348 | struct accessdata_dn acc; |
| 1349 | int mode; |
| 1350 | unsigned long win; |
| 1351 | int val; |
| 1352 | unsigned char services; |
| 1353 | unsigned char info; |
| 1354 | } u; |
| 1355 | int err; |
| 1356 | |
| 1357 | if (optlen && !optval) |
| 1358 | return -EINVAL; |
| 1359 | |
| 1360 | if (optlen > sizeof(u)) |
| 1361 | return -EINVAL; |
| 1362 | |
| 1363 | if (copy_from_user(&u, optval, optlen)) |
| 1364 | return -EFAULT; |
| 1365 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1366 | switch (optname) { |
| 1367 | case DSO_CONDATA: |
| 1368 | if (sock->state == SS_CONNECTED) |
| 1369 | return -EISCONN; |
| 1370 | if ((scp->state != DN_O) && (scp->state != DN_CR)) |
| 1371 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1373 | if (optlen != sizeof(struct optdata_dn)) |
| 1374 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1376 | if (le16_to_cpu(u.opt.opt_optl) > 16) |
| 1377 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1379 | memcpy(&scp->conndata_out, &u.opt, optlen); |
| 1380 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1382 | case DSO_DISDATA: |
| 1383 | if (sock->state != SS_CONNECTED && |
| 1384 | scp->accept_mode == ACC_IMMED) |
| 1385 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1387 | if (optlen != sizeof(struct optdata_dn)) |
| 1388 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1390 | if (le16_to_cpu(u.opt.opt_optl) > 16) |
| 1391 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1393 | memcpy(&scp->discdata_out, &u.opt, optlen); |
| 1394 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1396 | case DSO_CONACCESS: |
| 1397 | if (sock->state == SS_CONNECTED) |
| 1398 | return -EISCONN; |
| 1399 | if (scp->state != DN_O) |
| 1400 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1402 | if (optlen != sizeof(struct accessdata_dn)) |
| 1403 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1405 | if ((u.acc.acc_accl > DN_MAXACCL) || |
| 1406 | (u.acc.acc_passl > DN_MAXACCL) || |
| 1407 | (u.acc.acc_userl > DN_MAXACCL)) |
| 1408 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1410 | memcpy(&scp->accessdata, &u.acc, optlen); |
| 1411 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1413 | case DSO_ACCEPTMODE: |
| 1414 | if (sock->state == SS_CONNECTED) |
| 1415 | return -EISCONN; |
| 1416 | if (scp->state != DN_O) |
| 1417 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1419 | if (optlen != sizeof(int)) |
| 1420 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1422 | if ((u.mode != ACC_IMMED) && (u.mode != ACC_DEFER)) |
| 1423 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1425 | scp->accept_mode = (unsigned char)u.mode; |
| 1426 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1428 | case DSO_CONACCEPT: |
| 1429 | if (scp->state != DN_CR) |
| 1430 | return -EINVAL; |
| 1431 | timeo = sock_rcvtimeo(sk, 0); |
| 1432 | err = dn_confirm_accept(sk, &timeo, sk->sk_allocation); |
| 1433 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1435 | case DSO_CONREJECT: |
| 1436 | if (scp->state != DN_CR) |
| 1437 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1439 | scp->state = DN_DR; |
| 1440 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 1441 | dn_nsp_send_disc(sk, 0x38, 0, sk->sk_allocation); |
| 1442 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1444 | case DSO_MAXWINDOW: |
| 1445 | if (optlen != sizeof(unsigned long)) |
| 1446 | return -EINVAL; |
| 1447 | if (u.win > NSP_MAX_WINDOW) |
| 1448 | u.win = NSP_MAX_WINDOW; |
| 1449 | if (u.win == 0) |
| 1450 | return -EINVAL; |
| 1451 | scp->max_window = u.win; |
| 1452 | if (scp->snd_window > u.win) |
| 1453 | scp->snd_window = u.win; |
| 1454 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1456 | case DSO_NODELAY: |
| 1457 | if (optlen != sizeof(int)) |
| 1458 | return -EINVAL; |
Gao Feng | 9c28286 | 2017-03-04 22:10:28 +0800 | [diff] [blame] | 1459 | if (scp->nonagle == TCP_NAGLE_CORK) |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1460 | return -EINVAL; |
Gao Feng | 9c28286 | 2017-03-04 22:10:28 +0800 | [diff] [blame] | 1461 | scp->nonagle = (u.val == 0) ? 0 : TCP_NAGLE_OFF; |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1462 | /* if (scp->nonagle == 1) { Push pending frames } */ |
| 1463 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1465 | case DSO_CORK: |
| 1466 | if (optlen != sizeof(int)) |
| 1467 | return -EINVAL; |
Gao Feng | 9c28286 | 2017-03-04 22:10:28 +0800 | [diff] [blame] | 1468 | if (scp->nonagle == TCP_NAGLE_OFF) |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1469 | return -EINVAL; |
Gao Feng | 9c28286 | 2017-03-04 22:10:28 +0800 | [diff] [blame] | 1470 | scp->nonagle = (u.val == 0) ? 0 : TCP_NAGLE_CORK; |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1471 | /* if (scp->nonagle == 0) { Push pending frames } */ |
| 1472 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1474 | case DSO_SERVICES: |
| 1475 | if (optlen != sizeof(unsigned char)) |
| 1476 | return -EINVAL; |
| 1477 | if ((u.services & ~NSP_FC_MASK) != 0x01) |
| 1478 | return -EINVAL; |
| 1479 | if ((u.services & NSP_FC_MASK) == NSP_FC_MASK) |
| 1480 | return -EINVAL; |
| 1481 | scp->services_loc = u.services; |
| 1482 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1484 | case DSO_INFO: |
| 1485 | if (optlen != sizeof(unsigned char)) |
| 1486 | return -EINVAL; |
| 1487 | if (u.info & 0xfc) |
| 1488 | return -EINVAL; |
| 1489 | scp->info_loc = u.info; |
| 1490 | break; |
Paolo Abeni | dfec091 | 2018-02-15 16:59:49 +0100 | [diff] [blame] | 1491 | |
| 1492 | case DSO_LINKINFO: |
| 1493 | case DSO_STREAM: |
| 1494 | case DSO_SEQPACKET: |
| 1495 | default: |
| 1496 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | static int dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) |
| 1503 | { |
| 1504 | struct sock *sk = sock->sk; |
| 1505 | int err; |
| 1506 | |
| 1507 | lock_sock(sk); |
| 1508 | err = __dn_getsockopt(sock, level, optname, optval, optlen, 0); |
| 1509 | release_sock(sk); |
Paolo Abeni | dfec091 | 2018-02-15 16:59:49 +0100 | [diff] [blame] | 1510 | #ifdef CONFIG_NETFILTER |
| 1511 | if (err == -ENOPROTOOPT && optname != DSO_STREAM && |
| 1512 | optname != DSO_SEQPACKET && optname != DSO_CONACCEPT && |
| 1513 | optname != DSO_CONREJECT) { |
| 1514 | int len; |
| 1515 | |
| 1516 | if (get_user(len, optlen)) |
| 1517 | return -EFAULT; |
| 1518 | |
| 1519 | err = nf_getsockopt(sk, PF_DECnet, optname, optval, &len); |
| 1520 | if (err >= 0) |
| 1521 | err = put_user(len, optlen); |
| 1522 | } |
| 1523 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | |
| 1525 | return err; |
| 1526 | } |
| 1527 | |
| 1528 | static int __dn_getsockopt(struct socket *sock, int level,int optname, char __user *optval,int __user *optlen, int flags) |
| 1529 | { |
| 1530 | struct sock *sk = sock->sk; |
| 1531 | struct dn_scp *scp = DN_SK(sk); |
| 1532 | struct linkinfo_dn link; |
| 1533 | unsigned int r_len; |
| 1534 | void *r_data = NULL; |
| 1535 | unsigned int val; |
| 1536 | |
| 1537 | if(get_user(r_len , optlen)) |
| 1538 | return -EFAULT; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1539 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1540 | switch (optname) { |
| 1541 | case DSO_CONDATA: |
| 1542 | if (r_len > sizeof(struct optdata_dn)) |
| 1543 | r_len = sizeof(struct optdata_dn); |
| 1544 | r_data = &scp->conndata_in; |
| 1545 | break; |
| 1546 | |
| 1547 | case DSO_DISDATA: |
| 1548 | if (r_len > sizeof(struct optdata_dn)) |
| 1549 | r_len = sizeof(struct optdata_dn); |
| 1550 | r_data = &scp->discdata_in; |
| 1551 | break; |
| 1552 | |
| 1553 | case DSO_CONACCESS: |
| 1554 | if (r_len > sizeof(struct accessdata_dn)) |
| 1555 | r_len = sizeof(struct accessdata_dn); |
| 1556 | r_data = &scp->accessdata; |
| 1557 | break; |
| 1558 | |
| 1559 | case DSO_ACCEPTMODE: |
| 1560 | if (r_len > sizeof(unsigned char)) |
| 1561 | r_len = sizeof(unsigned char); |
| 1562 | r_data = &scp->accept_mode; |
| 1563 | break; |
| 1564 | |
| 1565 | case DSO_LINKINFO: |
| 1566 | if (r_len > sizeof(struct linkinfo_dn)) |
| 1567 | r_len = sizeof(struct linkinfo_dn); |
| 1568 | |
| 1569 | memset(&link, 0, sizeof(link)); |
| 1570 | |
| 1571 | switch (sock->state) { |
| 1572 | case SS_CONNECTING: |
| 1573 | link.idn_linkstate = LL_CONNECTING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | break; |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1575 | case SS_DISCONNECTING: |
| 1576 | link.idn_linkstate = LL_DISCONNECTING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | break; |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1578 | case SS_CONNECTED: |
| 1579 | link.idn_linkstate = LL_RUNNING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | default: |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1582 | link.idn_linkstate = LL_INACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | } |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1584 | |
| 1585 | link.idn_segsize = scp->segsize_rem; |
| 1586 | r_data = &link; |
| 1587 | break; |
| 1588 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1589 | case DSO_MAXWINDOW: |
| 1590 | if (r_len > sizeof(unsigned long)) |
| 1591 | r_len = sizeof(unsigned long); |
| 1592 | r_data = &scp->max_window; |
| 1593 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1595 | case DSO_NODELAY: |
| 1596 | if (r_len > sizeof(int)) |
| 1597 | r_len = sizeof(int); |
Gao Feng | 9c28286 | 2017-03-04 22:10:28 +0800 | [diff] [blame] | 1598 | val = (scp->nonagle == TCP_NAGLE_OFF); |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1599 | r_data = &val; |
| 1600 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1602 | case DSO_CORK: |
| 1603 | if (r_len > sizeof(int)) |
| 1604 | r_len = sizeof(int); |
Gao Feng | 9c28286 | 2017-03-04 22:10:28 +0800 | [diff] [blame] | 1605 | val = (scp->nonagle == TCP_NAGLE_CORK); |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1606 | r_data = &val; |
| 1607 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1609 | case DSO_SERVICES: |
| 1610 | if (r_len > sizeof(unsigned char)) |
| 1611 | r_len = sizeof(unsigned char); |
| 1612 | r_data = &scp->services_rem; |
| 1613 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 1615 | case DSO_INFO: |
| 1616 | if (r_len > sizeof(unsigned char)) |
| 1617 | r_len = sizeof(unsigned char); |
| 1618 | r_data = &scp->info_rem; |
| 1619 | break; |
Paolo Abeni | dfec091 | 2018-02-15 16:59:49 +0100 | [diff] [blame] | 1620 | |
| 1621 | case DSO_STREAM: |
| 1622 | case DSO_SEQPACKET: |
| 1623 | case DSO_CONACCEPT: |
| 1624 | case DSO_CONREJECT: |
| 1625 | default: |
| 1626 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | if (r_data) { |
| 1630 | if (copy_to_user(optval, r_data, r_len)) |
| 1631 | return -EFAULT; |
| 1632 | if (put_user(r_len, optlen)) |
| 1633 | return -EFAULT; |
| 1634 | } |
| 1635 | |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | |
| 1640 | static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int target) |
| 1641 | { |
David S. Miller | bec571e | 2009-05-28 16:43:52 -0700 | [diff] [blame] | 1642 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | int len = 0; |
| 1644 | |
| 1645 | if (flags & MSG_OOB) |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 1646 | return !skb_queue_empty(q) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
David S. Miller | bec571e | 2009-05-28 16:43:52 -0700 | [diff] [blame] | 1648 | skb_queue_walk(q, skb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 1650 | len += skb->len; |
| 1651 | |
| 1652 | if (cb->nsp_flags & 0x40) { |
| 1653 | /* SOCK_SEQPACKET reads to EOM */ |
| 1654 | if (sk->sk_type == SOCK_SEQPACKET) |
| 1655 | return 1; |
| 1656 | /* so does SOCK_STREAM unless WAITALL is specified */ |
| 1657 | if (!(flags & MSG_WAITALL)) |
| 1658 | return 1; |
| 1659 | } |
| 1660 | |
| 1661 | /* minimum data length for read exceeded */ |
| 1662 | if (len >= target) |
| 1663 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1670 | static int dn_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 1671 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | { |
| 1673 | struct sock *sk = sock->sk; |
| 1674 | struct dn_scp *scp = DN_SK(sk); |
| 1675 | struct sk_buff_head *queue = &sk->sk_receive_queue; |
| 1676 | size_t target = size > 1 ? 1 : 0; |
| 1677 | size_t copied = 0; |
| 1678 | int rv = 0; |
David S. Miller | bec571e | 2009-05-28 16:43:52 -0700 | [diff] [blame] | 1679 | struct sk_buff *skb, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | struct dn_skb_cb *cb = NULL; |
| 1681 | unsigned char eor = 0; |
| 1682 | long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
| 1683 | |
| 1684 | lock_sock(sk); |
| 1685 | |
| 1686 | if (sock_flag(sk, SOCK_ZAPPED)) { |
| 1687 | rv = -EADDRNOTAVAIL; |
| 1688 | goto out; |
| 1689 | } |
| 1690 | |
Patrick Caulfield | 9eb5c94 | 2005-11-11 12:04:28 -0800 | [diff] [blame] | 1691 | if (sk->sk_shutdown & RCV_SHUTDOWN) { |
| 1692 | rv = 0; |
| 1693 | goto out; |
| 1694 | } |
| 1695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | rv = dn_check_state(sk, NULL, 0, &timeo, flags); |
| 1697 | if (rv) |
| 1698 | goto out; |
| 1699 | |
Patrick Caulfield | c60992d | 2006-03-20 22:43:05 -0800 | [diff] [blame] | 1700 | if (flags & ~(MSG_CMSG_COMPAT|MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | rv = -EOPNOTSUPP; |
| 1702 | goto out; |
| 1703 | } |
| 1704 | |
| 1705 | if (flags & MSG_OOB) |
| 1706 | queue = &scp->other_receive_queue; |
| 1707 | |
| 1708 | if (flags & MSG_WAITALL) |
| 1709 | target = size; |
| 1710 | |
| 1711 | |
| 1712 | /* |
| 1713 | * See if there is data ready to read, sleep if there isn't |
| 1714 | */ |
| 1715 | for(;;) { |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 1716 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
David S. Miller | 22196d3 | 2008-06-17 01:06:01 -0700 | [diff] [blame] | 1717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | if (sk->sk_err) |
| 1719 | goto out; |
| 1720 | |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 1721 | if (!skb_queue_empty(&scp->other_receive_queue)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | if (!(flags & MSG_OOB)) { |
| 1723 | msg->msg_flags |= MSG_OOB; |
| 1724 | if (!scp->other_report) { |
| 1725 | scp->other_report = 1; |
| 1726 | goto out; |
| 1727 | } |
| 1728 | } |
| 1729 | } |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | if (scp->state != DN_RUN) |
| 1732 | goto out; |
| 1733 | |
| 1734 | if (signal_pending(current)) { |
| 1735 | rv = sock_intr_errno(timeo); |
| 1736 | goto out; |
| 1737 | } |
| 1738 | |
| 1739 | if (dn_data_ready(sk, queue, flags, target)) |
| 1740 | break; |
| 1741 | |
| 1742 | if (flags & MSG_DONTWAIT) { |
| 1743 | rv = -EWOULDBLOCK; |
| 1744 | goto out; |
| 1745 | } |
| 1746 | |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 1747 | add_wait_queue(sk_sleep(sk), &wait); |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 1748 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 1749 | sk_wait_event(sk, &timeo, dn_data_ready(sk, queue, flags, target), &wait); |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 1750 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 1751 | remove_wait_queue(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
David S. Miller | bec571e | 2009-05-28 16:43:52 -0700 | [diff] [blame] | 1754 | skb_queue_walk_safe(queue, skb, n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | unsigned int chunk = skb->len; |
| 1756 | cb = DN_SKB_CB(skb); |
| 1757 | |
| 1758 | if ((chunk + copied) > size) |
| 1759 | chunk = size - copied; |
| 1760 | |
Al Viro | 7eab8d9 | 2014-04-06 21:51:23 -0400 | [diff] [blame] | 1761 | if (memcpy_to_msg(msg, skb->data, chunk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | rv = -EFAULT; |
| 1763 | break; |
| 1764 | } |
| 1765 | copied += chunk; |
| 1766 | |
| 1767 | if (!(flags & MSG_PEEK)) |
| 1768 | skb_pull(skb, chunk); |
| 1769 | |
| 1770 | eor = cb->nsp_flags & 0x40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
| 1772 | if (skb->len == 0) { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1773 | skb_unlink(skb, queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | kfree_skb(skb); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1775 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | * N.B. Don't refer to skb or cb after this point |
| 1777 | * in loop. |
| 1778 | */ |
| 1779 | if ((scp->flowloc_sw == DN_DONTSEND) && !dn_congested(sk)) { |
| 1780 | scp->flowloc_sw = DN_SEND; |
| 1781 | dn_nsp_send_link(sk, DN_SEND, 0); |
| 1782 | } |
| 1783 | } |
| 1784 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1785 | if (eor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | if (sk->sk_type == SOCK_SEQPACKET) |
| 1787 | break; |
| 1788 | if (!(flags & MSG_WAITALL)) |
| 1789 | break; |
| 1790 | } |
| 1791 | |
| 1792 | if (flags & MSG_OOB) |
| 1793 | break; |
| 1794 | |
| 1795 | if (copied >= target) |
| 1796 | break; |
| 1797 | } |
| 1798 | |
| 1799 | rv = copied; |
| 1800 | |
| 1801 | |
| 1802 | if (eor && (sk->sk_type == SOCK_SEQPACKET)) |
| 1803 | msg->msg_flags |= MSG_EOR; |
| 1804 | |
| 1805 | out: |
| 1806 | if (rv == 0) |
| 1807 | rv = (flags & MSG_PEEK) ? -sk->sk_err : sock_error(sk); |
| 1808 | |
| 1809 | if ((rv >= 0) && msg->msg_name) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1810 | __sockaddr_check_size(sizeof(struct sockaddr_dn)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | memcpy(msg->msg_name, &scp->peer, sizeof(struct sockaddr_dn)); |
| 1812 | msg->msg_namelen = sizeof(struct sockaddr_dn); |
| 1813 | } |
| 1814 | |
| 1815 | release_sock(sk); |
| 1816 | |
| 1817 | return rv; |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | static inline int dn_queue_too_long(struct dn_scp *scp, struct sk_buff_head *queue, int flags) |
| 1822 | { |
| 1823 | unsigned char fctype = scp->services_rem & NSP_FC_MASK; |
| 1824 | if (skb_queue_len(queue) >= scp->snd_window) |
| 1825 | return 1; |
| 1826 | if (fctype != NSP_FC_NONE) { |
| 1827 | if (flags & MSG_OOB) { |
| 1828 | if (scp->flowrem_oth == 0) |
| 1829 | return 1; |
| 1830 | } else { |
| 1831 | if (scp->flowrem_dat == 0) |
| 1832 | return 1; |
| 1833 | } |
| 1834 | } |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
| 1838 | /* |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 1839 | * The DECnet spec requires that the "routing layer" accepts packets which |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | * are at least 230 bytes in size. This excludes any headers which the NSP |
| 1841 | * layer might add, so we always assume that we'll be using the maximal |
| 1842 | * length header on data packets. The variation in length is due to the |
| 1843 | * inclusion (or not) of the two 16 bit acknowledgement fields so it doesn't |
| 1844 | * make much practical difference. |
| 1845 | */ |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1846 | unsigned int dn_mss_from_pmtu(struct net_device *dev, int mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1848 | unsigned int mss = 230 - DN_MAX_NSP_DATA_HEADER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | if (dev) { |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 1850 | struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | mtu -= LL_RESERVED_SPACE(dev); |
| 1852 | if (dn_db->use_long) |
| 1853 | mtu -= 21; |
| 1854 | else |
| 1855 | mtu -= 6; |
| 1856 | mtu -= DN_MAX_NSP_DATA_HEADER; |
| 1857 | } else { |
| 1858 | /* |
| 1859 | * 21 = long header, 16 = guess at MAC header length |
| 1860 | */ |
| 1861 | mtu -= (21 + DN_MAX_NSP_DATA_HEADER + 16); |
| 1862 | } |
| 1863 | if (mtu > mss) |
| 1864 | mss = mtu; |
| 1865 | return mss; |
| 1866 | } |
| 1867 | |
| 1868 | static inline unsigned int dn_current_mss(struct sock *sk, int flags) |
| 1869 | { |
| 1870 | struct dst_entry *dst = __sk_dst_get(sk); |
| 1871 | struct dn_scp *scp = DN_SK(sk); |
| 1872 | int mss_now = min_t(int, scp->segsize_loc, scp->segsize_rem); |
| 1873 | |
| 1874 | /* Other data messages are limited to 16 bytes per packet */ |
| 1875 | if (flags & MSG_OOB) |
| 1876 | return 16; |
| 1877 | |
| 1878 | /* This works out the maximum size of segment we can send out */ |
| 1879 | if (dst) { |
| 1880 | u32 mtu = dst_mtu(dst); |
| 1881 | mss_now = min_t(int, dn_mss_from_pmtu(dst->dev, mtu), mss_now); |
| 1882 | } |
| 1883 | |
| 1884 | return mss_now; |
| 1885 | } |
| 1886 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1887 | /* |
Patrick Caulfield | 12a49ff | 2005-09-01 17:43:45 -0700 | [diff] [blame] | 1888 | * N.B. We get the timeout wrong here, but then we always did get it |
| 1889 | * wrong before and this is another step along the road to correcting |
| 1890 | * it. It ought to get updated each time we pass through the routine, |
| 1891 | * but in practise it probably doesn't matter too much for now. |
| 1892 | */ |
| 1893 | static inline struct sk_buff *dn_alloc_send_pskb(struct sock *sk, |
| 1894 | unsigned long datalen, int noblock, |
| 1895 | int *errcode) |
| 1896 | { |
| 1897 | struct sk_buff *skb = sock_alloc_send_skb(sk, datalen, |
| 1898 | noblock, errcode); |
| 1899 | if (skb) { |
YOSHIFUJI Hideaki | b98999d | 2007-12-12 03:51:49 +0900 | [diff] [blame] | 1900 | skb->protocol = htons(ETH_P_DNA_RT); |
Patrick Caulfield | 12a49ff | 2005-09-01 17:43:45 -0700 | [diff] [blame] | 1901 | skb->pkt_type = PACKET_OUTGOING; |
| 1902 | } |
| 1903 | return skb; |
| 1904 | } |
| 1905 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1906 | static int dn_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | { |
| 1908 | struct sock *sk = sock->sk; |
| 1909 | struct dn_scp *scp = DN_SK(sk); |
| 1910 | size_t mss; |
| 1911 | struct sk_buff_head *queue = &scp->data_xmit_queue; |
| 1912 | int flags = msg->msg_flags; |
| 1913 | int err = 0; |
| 1914 | size_t sent = 0; |
| 1915 | int addr_len = msg->msg_namelen; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1916 | DECLARE_SOCKADDR(struct sockaddr_dn *, addr, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | struct sk_buff *skb = NULL; |
| 1918 | struct dn_skb_cb *cb; |
| 1919 | size_t len; |
| 1920 | unsigned char fctype; |
Patrick Caulfield | 12a49ff | 2005-09-01 17:43:45 -0700 | [diff] [blame] | 1921 | long timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | |
| 1923 | if (flags & ~(MSG_TRYHARD|MSG_OOB|MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|MSG_MORE|MSG_CMSG_COMPAT)) |
| 1924 | return -EOPNOTSUPP; |
| 1925 | |
| 1926 | if (addr_len && (addr_len != sizeof(struct sockaddr_dn))) |
| 1927 | return -EINVAL; |
| 1928 | |
Patrick Caulfield | 12a49ff | 2005-09-01 17:43:45 -0700 | [diff] [blame] | 1929 | lock_sock(sk); |
| 1930 | timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | /* |
| 1932 | * The only difference between stream sockets and sequenced packet |
| 1933 | * sockets is that the stream sockets always behave as if MSG_EOR |
| 1934 | * has been set. |
| 1935 | */ |
| 1936 | if (sock->type == SOCK_STREAM) { |
Patrick Caulfield | 12a49ff | 2005-09-01 17:43:45 -0700 | [diff] [blame] | 1937 | if (flags & MSG_EOR) { |
| 1938 | err = -EINVAL; |
| 1939 | goto out; |
| 1940 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | flags |= MSG_EOR; |
| 1942 | } |
| 1943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | |
| 1945 | err = dn_check_state(sk, addr, addr_len, &timeo, flags); |
| 1946 | if (err) |
| 1947 | goto out_err; |
| 1948 | |
| 1949 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 1950 | err = -EPIPE; |
Patrick Caulfield | 9eb5c94 | 2005-11-11 12:04:28 -0800 | [diff] [blame] | 1951 | if (!(flags & MSG_NOSIGNAL)) |
| 1952 | send_sig(SIGPIPE, current, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | goto out_err; |
| 1954 | } |
| 1955 | |
| 1956 | if ((flags & MSG_TRYHARD) && sk->sk_dst_cache) |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1957 | dst_negative_advice(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | |
| 1959 | mss = scp->segsize_rem; |
| 1960 | fctype = scp->services_rem & NSP_FC_MASK; |
| 1961 | |
| 1962 | mss = dn_current_mss(sk, flags); |
| 1963 | |
| 1964 | if (flags & MSG_OOB) { |
| 1965 | queue = &scp->other_xmit_queue; |
| 1966 | if (size > mss) { |
| 1967 | err = -EMSGSIZE; |
| 1968 | goto out; |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | scp->persist_fxn = dn_nsp_xmit_timeout; |
| 1973 | |
| 1974 | while(sent < size) { |
| 1975 | err = sock_error(sk); |
| 1976 | if (err) |
| 1977 | goto out; |
| 1978 | |
| 1979 | if (signal_pending(current)) { |
| 1980 | err = sock_intr_errno(timeo); |
| 1981 | goto out; |
| 1982 | } |
| 1983 | |
| 1984 | /* |
| 1985 | * Calculate size that we wish to send. |
| 1986 | */ |
| 1987 | len = size - sent; |
| 1988 | |
| 1989 | if (len > mss) |
| 1990 | len = mss; |
| 1991 | |
| 1992 | /* |
| 1993 | * Wait for queue size to go down below the window |
| 1994 | * size. |
| 1995 | */ |
| 1996 | if (dn_queue_too_long(scp, queue, flags)) { |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 1997 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
David S. Miller | 22196d3 | 2008-06-17 01:06:01 -0700 | [diff] [blame] | 1998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | if (flags & MSG_DONTWAIT) { |
| 2000 | err = -EWOULDBLOCK; |
| 2001 | goto out; |
| 2002 | } |
| 2003 | |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 2004 | add_wait_queue(sk_sleep(sk), &wait); |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 2005 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
David S. Miller | 22196d3 | 2008-06-17 01:06:01 -0700 | [diff] [blame] | 2006 | sk_wait_event(sk, &timeo, |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 2007 | !dn_queue_too_long(scp, queue, flags), &wait); |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 2008 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 2009 | remove_wait_queue(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | continue; |
| 2011 | } |
| 2012 | |
| 2013 | /* |
| 2014 | * Get a suitably sized skb. |
Patrick Caulfield | 12a49ff | 2005-09-01 17:43:45 -0700 | [diff] [blame] | 2015 | * 64 is a bit of a hack really, but its larger than any |
| 2016 | * link-layer headers and has served us well as a good |
| 2017 | * guess as to their real length. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | */ |
Patrick Caulfield | 12a49ff | 2005-09-01 17:43:45 -0700 | [diff] [blame] | 2019 | skb = dn_alloc_send_pskb(sk, len + 64 + DN_MAX_NSP_DATA_HEADER, |
| 2020 | flags & MSG_DONTWAIT, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | |
| 2022 | if (err) |
| 2023 | break; |
| 2024 | |
| 2025 | if (!skb) |
| 2026 | continue; |
| 2027 | |
| 2028 | cb = DN_SKB_CB(skb); |
| 2029 | |
Patrick Caulfield | 12a49ff | 2005-09-01 17:43:45 -0700 | [diff] [blame] | 2030 | skb_reserve(skb, 64 + DN_MAX_NSP_DATA_HEADER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 2032 | if (memcpy_from_msg(skb_put(skb, len), msg, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | err = -EFAULT; |
| 2034 | goto out; |
| 2035 | } |
| 2036 | |
| 2037 | if (flags & MSG_OOB) { |
| 2038 | cb->nsp_flags = 0x30; |
| 2039 | if (fctype != NSP_FC_NONE) |
| 2040 | scp->flowrem_oth--; |
| 2041 | } else { |
| 2042 | cb->nsp_flags = 0x00; |
| 2043 | if (scp->seg_total == 0) |
| 2044 | cb->nsp_flags |= 0x20; |
| 2045 | |
| 2046 | scp->seg_total += len; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 2047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | if (((sent + len) == size) && (flags & MSG_EOR)) { |
| 2049 | cb->nsp_flags |= 0x40; |
| 2050 | scp->seg_total = 0; |
| 2051 | if (fctype == NSP_FC_SCMC) |
| 2052 | scp->flowrem_dat--; |
| 2053 | } |
| 2054 | if (fctype == NSP_FC_SRC) |
| 2055 | scp->flowrem_dat--; |
| 2056 | } |
| 2057 | |
| 2058 | sent += len; |
| 2059 | dn_nsp_queue_xmit(sk, skb, sk->sk_allocation, flags & MSG_OOB); |
| 2060 | skb = NULL; |
| 2061 | |
| 2062 | scp->persist = dn_nsp_persist(sk); |
| 2063 | |
| 2064 | } |
| 2065 | out: |
| 2066 | |
Wei Yongjun | db849df | 2009-02-25 00:39:01 +0000 | [diff] [blame] | 2067 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | |
| 2069 | release_sock(sk); |
| 2070 | |
| 2071 | return sent ? sent : err; |
| 2072 | |
| 2073 | out_err: |
Steven Whitehouse | 001ab02 | 2005-08-10 11:32:57 -0700 | [diff] [blame] | 2074 | err = sk_stream_error(sk, flags, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | release_sock(sk); |
| 2076 | return err; |
| 2077 | } |
| 2078 | |
| 2079 | static int dn_device_event(struct notifier_block *this, unsigned long event, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 2080 | void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 2082 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 2084 | if (!net_eq(dev_net(dev), &init_net)) |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 2085 | return NOTIFY_DONE; |
| 2086 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 2087 | switch (event) { |
| 2088 | case NETDEV_UP: |
| 2089 | dn_dev_up(dev); |
| 2090 | break; |
| 2091 | case NETDEV_DOWN: |
| 2092 | dn_dev_down(dev); |
| 2093 | break; |
| 2094 | default: |
| 2095 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | return NOTIFY_DONE; |
| 2099 | } |
| 2100 | |
| 2101 | static struct notifier_block dn_dev_notifier = { |
| 2102 | .notifier_call = dn_device_event, |
| 2103 | }; |
| 2104 | |
Stephen Hemminger | 7546dd9 | 2009-03-09 08:18:29 +0000 | [diff] [blame] | 2105 | static struct packet_type dn_dix_packet_type __read_mostly = { |
Harvey Harrison | 09640e63 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 2106 | .type = cpu_to_be16(ETH_P_DNA_RT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | .func = dn_route_rcv, |
| 2108 | }; |
| 2109 | |
| 2110 | #ifdef CONFIG_PROC_FS |
| 2111 | struct dn_iter_state { |
| 2112 | int bucket; |
| 2113 | }; |
| 2114 | |
| 2115 | static struct sock *dn_socket_get_first(struct seq_file *seq) |
| 2116 | { |
| 2117 | struct dn_iter_state *state = seq->private; |
| 2118 | struct sock *n = NULL; |
| 2119 | |
| 2120 | for(state->bucket = 0; |
| 2121 | state->bucket < DN_SK_HASH_SIZE; |
| 2122 | ++state->bucket) { |
| 2123 | n = sk_head(&dn_sk_hash[state->bucket]); |
| 2124 | if (n) |
| 2125 | break; |
| 2126 | } |
| 2127 | |
| 2128 | return n; |
| 2129 | } |
| 2130 | |
| 2131 | static struct sock *dn_socket_get_next(struct seq_file *seq, |
| 2132 | struct sock *n) |
| 2133 | { |
| 2134 | struct dn_iter_state *state = seq->private; |
| 2135 | |
| 2136 | n = sk_next(n); |
Suraj Upadhyay | e0c3f4c | 2020-07-17 00:46:45 +0530 | [diff] [blame] | 2137 | while (!n) { |
| 2138 | if (++state->bucket >= DN_SK_HASH_SIZE) |
| 2139 | break; |
| 2140 | n = sk_head(&dn_sk_hash[state->bucket]); |
| 2141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | return n; |
| 2143 | } |
| 2144 | |
| 2145 | static struct sock *socket_get_idx(struct seq_file *seq, loff_t *pos) |
| 2146 | { |
| 2147 | struct sock *sk = dn_socket_get_first(seq); |
| 2148 | |
| 2149 | if (sk) { |
| 2150 | while(*pos && (sk = dn_socket_get_next(seq, sk))) |
| 2151 | --*pos; |
| 2152 | } |
| 2153 | return *pos ? NULL : sk; |
| 2154 | } |
| 2155 | |
| 2156 | static void *dn_socket_get_idx(struct seq_file *seq, loff_t pos) |
| 2157 | { |
| 2158 | void *rc; |
| 2159 | read_lock_bh(&dn_hash_lock); |
| 2160 | rc = socket_get_idx(seq, &pos); |
| 2161 | if (!rc) { |
| 2162 | read_unlock_bh(&dn_hash_lock); |
| 2163 | } |
| 2164 | return rc; |
| 2165 | } |
| 2166 | |
| 2167 | static void *dn_socket_seq_start(struct seq_file *seq, loff_t *pos) |
| 2168 | { |
| 2169 | return *pos ? dn_socket_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2170 | } |
| 2171 | |
| 2172 | static void *dn_socket_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2173 | { |
| 2174 | void *rc; |
| 2175 | |
| 2176 | if (v == SEQ_START_TOKEN) { |
| 2177 | rc = dn_socket_get_idx(seq, 0); |
| 2178 | goto out; |
| 2179 | } |
| 2180 | |
| 2181 | rc = dn_socket_get_next(seq, v); |
| 2182 | if (rc) |
| 2183 | goto out; |
| 2184 | read_unlock_bh(&dn_hash_lock); |
| 2185 | out: |
| 2186 | ++*pos; |
| 2187 | return rc; |
| 2188 | } |
| 2189 | |
| 2190 | static void dn_socket_seq_stop(struct seq_file *seq, void *v) |
| 2191 | { |
| 2192 | if (v && v != SEQ_START_TOKEN) |
| 2193 | read_unlock_bh(&dn_hash_lock); |
| 2194 | } |
| 2195 | |
| 2196 | #define IS_NOT_PRINTABLE(x) ((x) < 32 || (x) > 126) |
| 2197 | |
| 2198 | static void dn_printable_object(struct sockaddr_dn *dn, unsigned char *buf) |
| 2199 | { |
| 2200 | int i; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 2201 | |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 2202 | switch (le16_to_cpu(dn->sdn_objnamel)) { |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 2203 | case 0: |
| 2204 | sprintf(buf, "%d", dn->sdn_objnum); |
| 2205 | break; |
| 2206 | default: |
| 2207 | for (i = 0; i < le16_to_cpu(dn->sdn_objnamel); i++) { |
| 2208 | buf[i] = dn->sdn_objname[i]; |
| 2209 | if (IS_NOT_PRINTABLE(buf[i])) |
| 2210 | buf[i] = '.'; |
| 2211 | } |
| 2212 | buf[i] = 0; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 2213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | static char *dn_state2asc(unsigned char state) |
| 2217 | { |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 2218 | switch (state) { |
| 2219 | case DN_O: |
| 2220 | return "OPEN"; |
| 2221 | case DN_CR: |
| 2222 | return " CR"; |
| 2223 | case DN_DR: |
| 2224 | return " DR"; |
| 2225 | case DN_DRC: |
| 2226 | return " DRC"; |
| 2227 | case DN_CC: |
| 2228 | return " CC"; |
| 2229 | case DN_CI: |
| 2230 | return " CI"; |
| 2231 | case DN_NR: |
| 2232 | return " NR"; |
| 2233 | case DN_NC: |
| 2234 | return " NC"; |
| 2235 | case DN_CD: |
| 2236 | return " CD"; |
| 2237 | case DN_RJ: |
| 2238 | return " RJ"; |
| 2239 | case DN_RUN: |
| 2240 | return " RUN"; |
| 2241 | case DN_DI: |
| 2242 | return " DI"; |
| 2243 | case DN_DIC: |
| 2244 | return " DIC"; |
| 2245 | case DN_DN: |
| 2246 | return " DN"; |
| 2247 | case DN_CL: |
| 2248 | return " CL"; |
| 2249 | case DN_CN: |
| 2250 | return " CN"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | return "????"; |
| 2254 | } |
| 2255 | |
| 2256 | static inline void dn_socket_format_entry(struct seq_file *seq, struct sock *sk) |
| 2257 | { |
| 2258 | struct dn_scp *scp = DN_SK(sk); |
| 2259 | char buf1[DN_ASCBUF_LEN]; |
| 2260 | char buf2[DN_ASCBUF_LEN]; |
| 2261 | char local_object[DN_MAXOBJL+3]; |
| 2262 | char remote_object[DN_MAXOBJL+3]; |
| 2263 | |
| 2264 | dn_printable_object(&scp->addr, local_object); |
| 2265 | dn_printable_object(&scp->peer, remote_object); |
| 2266 | |
| 2267 | seq_printf(seq, |
| 2268 | "%6s/%04X %04d:%04d %04d:%04d %01d %-16s " |
| 2269 | "%6s/%04X %04d:%04d %04d:%04d %01d %-16s %4s %s\n", |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 2270 | dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp->addr)), buf1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | scp->addrloc, |
| 2272 | scp->numdat, |
| 2273 | scp->numoth, |
| 2274 | scp->ackxmt_dat, |
| 2275 | scp->ackxmt_oth, |
| 2276 | scp->flowloc_sw, |
| 2277 | local_object, |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 2278 | dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp->peer)), buf2), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | scp->addrrem, |
| 2280 | scp->numdat_rcv, |
| 2281 | scp->numoth_rcv, |
| 2282 | scp->ackrcv_dat, |
| 2283 | scp->ackrcv_oth, |
| 2284 | scp->flowrem_sw, |
| 2285 | remote_object, |
| 2286 | dn_state2asc(scp->state), |
| 2287 | ((scp->accept_mode == ACC_IMMED) ? "IMMED" : "DEFER")); |
| 2288 | } |
| 2289 | |
| 2290 | static int dn_socket_seq_show(struct seq_file *seq, void *v) |
| 2291 | { |
| 2292 | if (v == SEQ_START_TOKEN) { |
| 2293 | seq_puts(seq, "Local Remote\n"); |
| 2294 | } else { |
| 2295 | dn_socket_format_entry(seq, v); |
| 2296 | } |
| 2297 | return 0; |
| 2298 | } |
| 2299 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 2300 | static const struct seq_operations dn_socket_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | .start = dn_socket_seq_start, |
| 2302 | .next = dn_socket_seq_next, |
| 2303 | .stop = dn_socket_seq_stop, |
| 2304 | .show = dn_socket_seq_show, |
| 2305 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | #endif |
| 2307 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 2308 | static const struct net_proto_family dn_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | .family = AF_DECnet, |
| 2310 | .create = dn_create, |
| 2311 | .owner = THIS_MODULE, |
| 2312 | }; |
| 2313 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 2314 | static const struct proto_ops dn_proto_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | .family = AF_DECnet, |
| 2316 | .owner = THIS_MODULE, |
| 2317 | .release = dn_release, |
| 2318 | .bind = dn_bind, |
| 2319 | .connect = dn_connect, |
| 2320 | .socketpair = sock_no_socketpair, |
| 2321 | .accept = dn_accept, |
| 2322 | .getname = dn_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 2323 | .poll = dn_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | .ioctl = dn_ioctl, |
| 2325 | .listen = dn_listen, |
| 2326 | .shutdown = dn_shutdown, |
| 2327 | .setsockopt = dn_setsockopt, |
| 2328 | .getsockopt = dn_getsockopt, |
| 2329 | .sendmsg = dn_sendmsg, |
| 2330 | .recvmsg = dn_recvmsg, |
| 2331 | .mmap = sock_no_mmap, |
| 2332 | .sendpage = sock_no_sendpage, |
| 2333 | }; |
| 2334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | MODULE_DESCRIPTION("The Linux DECnet Network Protocol"); |
| 2336 | MODULE_AUTHOR("Linux DECnet Project Team"); |
| 2337 | MODULE_LICENSE("GPL"); |
| 2338 | MODULE_ALIAS_NETPROTO(PF_DECnet); |
| 2339 | |
Kees Cook | 0632466 | 2017-05-08 15:59:05 -0700 | [diff] [blame] | 2340 | static const char banner[] __initconst = KERN_INFO |
| 2341 | "NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | |
| 2343 | static int __init decnet_init(void) |
| 2344 | { |
| 2345 | int rc; |
| 2346 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 2347 | printk(banner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | |
| 2349 | rc = proto_register(&dn_proto, 1); |
| 2350 | if (rc != 0) |
| 2351 | goto out; |
| 2352 | |
| 2353 | dn_neigh_init(); |
| 2354 | dn_dev_init(); |
| 2355 | dn_route_init(); |
| 2356 | dn_fib_init(); |
| 2357 | |
| 2358 | sock_register(&dn_family_ops); |
| 2359 | dev_add_pack(&dn_dix_packet_type); |
| 2360 | register_netdevice_notifier(&dn_dev_notifier); |
| 2361 | |
Christoph Hellwig | 44414d8 | 2018-04-24 17:05:17 +0200 | [diff] [blame] | 2362 | proc_create_seq_private("decnet", 0444, init_net.proc_net, |
| 2363 | &dn_socket_seq_ops, sizeof(struct dn_iter_state), |
| 2364 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | dn_register_sysctl(); |
| 2366 | out: |
| 2367 | return rc; |
| 2368 | |
| 2369 | } |
| 2370 | module_init(decnet_init); |
| 2371 | |
| 2372 | /* |
| 2373 | * Prevent DECnet module unloading until its fixed properly. |
| 2374 | * Requires an audit of the code to check for memory leaks and |
| 2375 | * initialisation problems etc. |
| 2376 | */ |
| 2377 | #if 0 |
| 2378 | static void __exit decnet_exit(void) |
| 2379 | { |
| 2380 | sock_unregister(AF_DECnet); |
Thomas Graf | fa34ddd | 2007-03-22 11:57:46 -0700 | [diff] [blame] | 2381 | rtnl_unregister_all(PF_DECnet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | dev_remove_pack(&dn_dix_packet_type); |
| 2383 | |
| 2384 | dn_unregister_sysctl(); |
| 2385 | |
| 2386 | unregister_netdevice_notifier(&dn_dev_notifier); |
| 2387 | |
| 2388 | dn_route_cleanup(); |
| 2389 | dn_dev_cleanup(); |
| 2390 | dn_neigh_cleanup(); |
| 2391 | dn_fib_cleanup(); |
| 2392 | |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 2393 | remove_proc_entry("decnet", init_net.proc_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | |
| 2395 | proto_unregister(&dn_proto); |
Jesper Dangaard Brouer | 10e8544 | 2009-06-26 10:46:08 +0000 | [diff] [blame] | 2396 | |
Paul E. McKenney | dd06d25 | 2018-11-07 15:12:34 -0800 | [diff] [blame] | 2397 | rcu_barrier(); /* Wait for completion of call_rcu()'s */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | } |
| 2399 | module_exit(decnet_exit); |
| 2400 | #endif |