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