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