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