Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License, or |
| 5 | * (at your option) any later version. |
| 6 | * |
| 7 | * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk) |
| 8 | * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) |
| 9 | * Copyright (C) Darryl Miles G7LED (dlm@g7led.demon.co.uk) |
| 10 | * Copyright (C) Steven Whitehouse GW7RRM (stevew@acm.org) |
| 11 | * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de) |
| 12 | * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de) |
| 13 | * Copyright (C) Hans Alblas PE1AYX (hans@esrac.ele.tue.nl) |
| 14 | * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr) |
| 15 | */ |
| 16 | #include <linux/config.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 17 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/socket.h> |
| 22 | #include <linux/in.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/timer.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/smp_lock.h> |
| 28 | #include <linux/sockios.h> |
| 29 | #include <linux/net.h> |
| 30 | #include <net/ax25.h> |
| 31 | #include <linux/inet.h> |
| 32 | #include <linux/netdevice.h> |
| 33 | #include <linux/if_arp.h> |
| 34 | #include <linux/skbuff.h> |
| 35 | #include <net/sock.h> |
| 36 | #include <asm/uaccess.h> |
| 37 | #include <asm/system.h> |
| 38 | #include <linux/fcntl.h> |
| 39 | #include <linux/termios.h> /* For TIOCINQ/OUTQ */ |
| 40 | #include <linux/mm.h> |
| 41 | #include <linux/interrupt.h> |
| 42 | #include <linux/notifier.h> |
| 43 | #include <linux/proc_fs.h> |
| 44 | #include <linux/stat.h> |
| 45 | #include <linux/netfilter.h> |
| 46 | #include <linux/sysctl.h> |
| 47 | #include <linux/init.h> |
| 48 | #include <linux/spinlock.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 49 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <net/ip.h> |
| 51 | #include <net/arp.h> |
| 52 | |
| 53 | |
| 54 | |
| 55 | HLIST_HEAD(ax25_list); |
| 56 | DEFINE_SPINLOCK(ax25_list_lock); |
| 57 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 58 | static const struct proto_ops ax25_proto_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | static void ax25_free_sock(struct sock *sk) |
| 61 | { |
| 62 | ax25_cb_put(ax25_sk(sk)); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Socket removal during an interrupt is now safe. |
| 67 | */ |
| 68 | static void ax25_cb_del(ax25_cb *ax25) |
| 69 | { |
| 70 | if (!hlist_unhashed(&ax25->ax25_node)) { |
| 71 | spin_lock_bh(&ax25_list_lock); |
| 72 | hlist_del_init(&ax25->ax25_node); |
| 73 | spin_unlock_bh(&ax25_list_lock); |
| 74 | ax25_cb_put(ax25); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Kill all bound sockets on a dropped device. |
| 80 | */ |
| 81 | static void ax25_kill_by_device(struct net_device *dev) |
| 82 | { |
| 83 | ax25_dev *ax25_dev; |
| 84 | ax25_cb *s; |
| 85 | struct hlist_node *node; |
| 86 | |
| 87 | if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) |
| 88 | return; |
| 89 | |
| 90 | spin_lock_bh(&ax25_list_lock); |
| 91 | ax25_for_each(s, node, &ax25_list) { |
| 92 | if (s->ax25_dev == ax25_dev) { |
| 93 | s->ax25_dev = NULL; |
| 94 | ax25_disconnect(s, ENETUNREACH); |
| 95 | } |
| 96 | } |
| 97 | spin_unlock_bh(&ax25_list_lock); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Handle device status changes. |
| 102 | */ |
| 103 | static int ax25_device_event(struct notifier_block *this, unsigned long event, |
| 104 | void *ptr) |
| 105 | { |
| 106 | struct net_device *dev = (struct net_device *)ptr; |
| 107 | |
| 108 | /* Reject non AX.25 devices */ |
| 109 | if (dev->type != ARPHRD_AX25) |
| 110 | return NOTIFY_DONE; |
| 111 | |
| 112 | switch (event) { |
| 113 | case NETDEV_UP: |
| 114 | ax25_dev_device_up(dev); |
| 115 | break; |
| 116 | case NETDEV_DOWN: |
| 117 | ax25_kill_by_device(dev); |
| 118 | ax25_rt_device_down(dev); |
| 119 | ax25_dev_device_down(dev); |
| 120 | break; |
| 121 | default: |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | return NOTIFY_DONE; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Add a socket to the bound sockets list. |
| 130 | */ |
| 131 | void ax25_cb_add(ax25_cb *ax25) |
| 132 | { |
| 133 | spin_lock_bh(&ax25_list_lock); |
| 134 | ax25_cb_hold(ax25); |
| 135 | hlist_add_head(&ax25->ax25_node, &ax25_list); |
| 136 | spin_unlock_bh(&ax25_list_lock); |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Find a socket that wants to accept the SABM we have just |
| 141 | * received. |
| 142 | */ |
| 143 | struct sock *ax25_find_listener(ax25_address *addr, int digi, |
| 144 | struct net_device *dev, int type) |
| 145 | { |
| 146 | ax25_cb *s; |
| 147 | struct hlist_node *node; |
| 148 | |
| 149 | spin_lock_bh(&ax25_list_lock); |
| 150 | ax25_for_each(s, node, &ax25_list) { |
| 151 | if ((s->iamdigi && !digi) || (!s->iamdigi && digi)) |
| 152 | continue; |
| 153 | if (s->sk && !ax25cmp(&s->source_addr, addr) && |
| 154 | s->sk->sk_type == type && s->sk->sk_state == TCP_LISTEN) { |
| 155 | /* If device is null we match any device */ |
| 156 | if (s->ax25_dev == NULL || s->ax25_dev->dev == dev) { |
| 157 | sock_hold(s->sk); |
| 158 | spin_unlock_bh(&ax25_list_lock); |
| 159 | return s->sk; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | spin_unlock_bh(&ax25_list_lock); |
| 164 | |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Find an AX.25 socket given both ends. |
| 170 | */ |
| 171 | struct sock *ax25_get_socket(ax25_address *my_addr, ax25_address *dest_addr, |
| 172 | int type) |
| 173 | { |
| 174 | struct sock *sk = NULL; |
| 175 | ax25_cb *s; |
| 176 | struct hlist_node *node; |
| 177 | |
| 178 | spin_lock_bh(&ax25_list_lock); |
| 179 | ax25_for_each(s, node, &ax25_list) { |
| 180 | if (s->sk && !ax25cmp(&s->source_addr, my_addr) && |
| 181 | !ax25cmp(&s->dest_addr, dest_addr) && |
| 182 | s->sk->sk_type == type) { |
| 183 | sk = s->sk; |
| 184 | sock_hold(sk); |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | spin_unlock_bh(&ax25_list_lock); |
| 190 | |
| 191 | return sk; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Find an AX.25 control block given both ends. It will only pick up |
| 196 | * floating AX.25 control blocks or non Raw socket bound control blocks. |
| 197 | */ |
| 198 | ax25_cb *ax25_find_cb(ax25_address *src_addr, ax25_address *dest_addr, |
| 199 | ax25_digi *digi, struct net_device *dev) |
| 200 | { |
| 201 | ax25_cb *s; |
| 202 | struct hlist_node *node; |
| 203 | |
| 204 | spin_lock_bh(&ax25_list_lock); |
| 205 | ax25_for_each(s, node, &ax25_list) { |
| 206 | if (s->sk && s->sk->sk_type != SOCK_SEQPACKET) |
| 207 | continue; |
| 208 | if (s->ax25_dev == NULL) |
| 209 | continue; |
| 210 | if (ax25cmp(&s->source_addr, src_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->ax25_dev->dev == dev) { |
| 211 | if (digi != NULL && digi->ndigi != 0) { |
| 212 | if (s->digipeat == NULL) |
| 213 | continue; |
| 214 | if (ax25digicmp(s->digipeat, digi) != 0) |
| 215 | continue; |
| 216 | } else { |
| 217 | if (s->digipeat != NULL && s->digipeat->ndigi != 0) |
| 218 | continue; |
| 219 | } |
| 220 | ax25_cb_hold(s); |
| 221 | spin_unlock_bh(&ax25_list_lock); |
| 222 | |
| 223 | return s; |
| 224 | } |
| 225 | } |
| 226 | spin_unlock_bh(&ax25_list_lock); |
| 227 | |
| 228 | return NULL; |
| 229 | } |
| 230 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame^] | 231 | EXPORT_SYMBOL(ax25_find_cb); |
| 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | void ax25_send_to_raw(ax25_address *addr, struct sk_buff *skb, int proto) |
| 234 | { |
| 235 | ax25_cb *s; |
| 236 | struct sk_buff *copy; |
| 237 | struct hlist_node *node; |
| 238 | |
| 239 | spin_lock_bh(&ax25_list_lock); |
| 240 | ax25_for_each(s, node, &ax25_list) { |
| 241 | if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && |
| 242 | s->sk->sk_type == SOCK_RAW && |
| 243 | s->sk->sk_protocol == proto && |
| 244 | s->ax25_dev->dev == skb->dev && |
| 245 | atomic_read(&s->sk->sk_rmem_alloc) <= s->sk->sk_rcvbuf) { |
| 246 | if ((copy = skb_clone(skb, GFP_ATOMIC)) == NULL) |
| 247 | continue; |
| 248 | if (sock_queue_rcv_skb(s->sk, copy) != 0) |
| 249 | kfree_skb(copy); |
| 250 | } |
| 251 | } |
| 252 | spin_unlock_bh(&ax25_list_lock); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Deferred destroy. |
| 257 | */ |
| 258 | void ax25_destroy_socket(ax25_cb *); |
| 259 | |
| 260 | /* |
| 261 | * Handler for deferred kills. |
| 262 | */ |
| 263 | static void ax25_destroy_timer(unsigned long data) |
| 264 | { |
| 265 | ax25_cb *ax25=(ax25_cb *)data; |
| 266 | struct sock *sk; |
| 267 | |
| 268 | sk=ax25->sk; |
| 269 | |
| 270 | bh_lock_sock(sk); |
| 271 | sock_hold(sk); |
| 272 | ax25_destroy_socket(ax25); |
| 273 | bh_unlock_sock(sk); |
| 274 | sock_put(sk); |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * This is called from user mode and the timers. Thus it protects itself |
| 279 | * against interrupt users but doesn't worry about being called during |
| 280 | * work. Once it is removed from the queue no interrupt or bottom half |
| 281 | * will touch it and we are (fairly 8-) ) safe. |
| 282 | */ |
| 283 | void ax25_destroy_socket(ax25_cb *ax25) |
| 284 | { |
| 285 | struct sk_buff *skb; |
| 286 | |
| 287 | ax25_cb_del(ax25); |
| 288 | |
| 289 | ax25_stop_heartbeat(ax25); |
| 290 | ax25_stop_t1timer(ax25); |
| 291 | ax25_stop_t2timer(ax25); |
| 292 | ax25_stop_t3timer(ax25); |
| 293 | ax25_stop_idletimer(ax25); |
| 294 | |
| 295 | ax25_clear_queues(ax25); /* Flush the queues */ |
| 296 | |
| 297 | if (ax25->sk != NULL) { |
| 298 | while ((skb = skb_dequeue(&ax25->sk->sk_receive_queue)) != NULL) { |
| 299 | if (skb->sk != ax25->sk) { |
| 300 | /* A pending connection */ |
| 301 | ax25_cb *sax25 = ax25_sk(skb->sk); |
| 302 | |
| 303 | /* Queue the unaccepted socket for death */ |
| 304 | sock_orphan(skb->sk); |
| 305 | |
| 306 | ax25_start_heartbeat(sax25); |
| 307 | sax25->state = AX25_STATE_0; |
| 308 | } |
| 309 | |
| 310 | kfree_skb(skb); |
| 311 | } |
| 312 | skb_queue_purge(&ax25->sk->sk_write_queue); |
| 313 | } |
| 314 | |
| 315 | if (ax25->sk != NULL) { |
| 316 | if (atomic_read(&ax25->sk->sk_wmem_alloc) || |
| 317 | atomic_read(&ax25->sk->sk_rmem_alloc)) { |
| 318 | /* Defer: outstanding buffers */ |
| 319 | init_timer(&ax25->dtimer); |
| 320 | ax25->dtimer.expires = jiffies + 2 * HZ; |
| 321 | ax25->dtimer.function = ax25_destroy_timer; |
| 322 | ax25->dtimer.data = (unsigned long)ax25; |
| 323 | add_timer(&ax25->dtimer); |
| 324 | } else { |
| 325 | struct sock *sk=ax25->sk; |
| 326 | ax25->sk=NULL; |
| 327 | sock_put(sk); |
| 328 | } |
| 329 | } else { |
| 330 | ax25_cb_put(ax25); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * dl1bke 960311: set parameters for existing AX.25 connections, |
| 336 | * includes a KILL command to abort any connection. |
| 337 | * VERY useful for debugging ;-) |
| 338 | */ |
| 339 | static int ax25_ctl_ioctl(const unsigned int cmd, void __user *arg) |
| 340 | { |
| 341 | struct ax25_ctl_struct ax25_ctl; |
| 342 | ax25_digi digi; |
| 343 | ax25_dev *ax25_dev; |
| 344 | ax25_cb *ax25; |
| 345 | unsigned int k; |
| 346 | |
| 347 | if (copy_from_user(&ax25_ctl, arg, sizeof(ax25_ctl))) |
| 348 | return -EFAULT; |
| 349 | |
| 350 | if ((ax25_dev = ax25_addr_ax25dev(&ax25_ctl.port_addr)) == NULL) |
| 351 | return -ENODEV; |
| 352 | |
| 353 | if (ax25_ctl.digi_count > AX25_MAX_DIGIS) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | digi.ndigi = ax25_ctl.digi_count; |
| 357 | for (k = 0; k < digi.ndigi; k++) |
| 358 | digi.calls[k] = ax25_ctl.digi_addr[k]; |
| 359 | |
| 360 | if ((ax25 = ax25_find_cb(&ax25_ctl.source_addr, &ax25_ctl.dest_addr, &digi, ax25_dev->dev)) == NULL) |
| 361 | return -ENOTCONN; |
| 362 | |
| 363 | switch (ax25_ctl.cmd) { |
| 364 | case AX25_KILL: |
| 365 | ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND); |
| 366 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 367 | if (ax25_dev->dama.slave && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE) |
| 368 | ax25_dama_off(ax25); |
| 369 | #endif |
| 370 | ax25_disconnect(ax25, ENETRESET); |
| 371 | break; |
| 372 | |
| 373 | case AX25_WINDOW: |
| 374 | if (ax25->modulus == AX25_MODULUS) { |
| 375 | if (ax25_ctl.arg < 1 || ax25_ctl.arg > 7) |
| 376 | return -EINVAL; |
| 377 | } else { |
| 378 | if (ax25_ctl.arg < 1 || ax25_ctl.arg > 63) |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | ax25->window = ax25_ctl.arg; |
| 382 | break; |
| 383 | |
| 384 | case AX25_T1: |
| 385 | if (ax25_ctl.arg < 1) |
| 386 | return -EINVAL; |
| 387 | ax25->rtt = (ax25_ctl.arg * HZ) / 2; |
| 388 | ax25->t1 = ax25_ctl.arg * HZ; |
| 389 | break; |
| 390 | |
| 391 | case AX25_T2: |
| 392 | if (ax25_ctl.arg < 1) |
| 393 | return -EINVAL; |
| 394 | ax25->t2 = ax25_ctl.arg * HZ; |
| 395 | break; |
| 396 | |
| 397 | case AX25_N2: |
| 398 | if (ax25_ctl.arg < 1 || ax25_ctl.arg > 31) |
| 399 | return -EINVAL; |
| 400 | ax25->n2count = 0; |
| 401 | ax25->n2 = ax25_ctl.arg; |
| 402 | break; |
| 403 | |
| 404 | case AX25_T3: |
| 405 | if (ax25_ctl.arg < 0) |
| 406 | return -EINVAL; |
| 407 | ax25->t3 = ax25_ctl.arg * HZ; |
| 408 | break; |
| 409 | |
| 410 | case AX25_IDLE: |
| 411 | if (ax25_ctl.arg < 0) |
| 412 | return -EINVAL; |
| 413 | ax25->idle = ax25_ctl.arg * 60 * HZ; |
| 414 | break; |
| 415 | |
| 416 | case AX25_PACLEN: |
| 417 | if (ax25_ctl.arg < 16 || ax25_ctl.arg > 65535) |
| 418 | return -EINVAL; |
| 419 | ax25->paclen = ax25_ctl.arg; |
| 420 | break; |
| 421 | |
| 422 | default: |
| 423 | return -EINVAL; |
| 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Fill in a created AX.25 created control block with the default |
| 431 | * values for a particular device. |
| 432 | */ |
| 433 | void ax25_fillin_cb(ax25_cb *ax25, ax25_dev *ax25_dev) |
| 434 | { |
| 435 | ax25->ax25_dev = ax25_dev; |
| 436 | |
| 437 | if (ax25->ax25_dev != NULL) { |
| 438 | ax25->rtt = ax25_dev->values[AX25_VALUES_T1] / 2; |
| 439 | ax25->t1 = ax25_dev->values[AX25_VALUES_T1]; |
| 440 | ax25->t2 = ax25_dev->values[AX25_VALUES_T2]; |
| 441 | ax25->t3 = ax25_dev->values[AX25_VALUES_T3]; |
| 442 | ax25->n2 = ax25_dev->values[AX25_VALUES_N2]; |
| 443 | ax25->paclen = ax25_dev->values[AX25_VALUES_PACLEN]; |
| 444 | ax25->idle = ax25_dev->values[AX25_VALUES_IDLE]; |
| 445 | ax25->backoff = ax25_dev->values[AX25_VALUES_BACKOFF]; |
| 446 | |
| 447 | if (ax25_dev->values[AX25_VALUES_AXDEFMODE]) { |
| 448 | ax25->modulus = AX25_EMODULUS; |
| 449 | ax25->window = ax25_dev->values[AX25_VALUES_EWINDOW]; |
| 450 | } else { |
| 451 | ax25->modulus = AX25_MODULUS; |
| 452 | ax25->window = ax25_dev->values[AX25_VALUES_WINDOW]; |
| 453 | } |
| 454 | } else { |
| 455 | ax25->rtt = AX25_DEF_T1 / 2; |
| 456 | ax25->t1 = AX25_DEF_T1; |
| 457 | ax25->t2 = AX25_DEF_T2; |
| 458 | ax25->t3 = AX25_DEF_T3; |
| 459 | ax25->n2 = AX25_DEF_N2; |
| 460 | ax25->paclen = AX25_DEF_PACLEN; |
| 461 | ax25->idle = AX25_DEF_IDLE; |
| 462 | ax25->backoff = AX25_DEF_BACKOFF; |
| 463 | |
| 464 | if (AX25_DEF_AXDEFMODE) { |
| 465 | ax25->modulus = AX25_EMODULUS; |
| 466 | ax25->window = AX25_DEF_EWINDOW; |
| 467 | } else { |
| 468 | ax25->modulus = AX25_MODULUS; |
| 469 | ax25->window = AX25_DEF_WINDOW; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Create an empty AX.25 control block. |
| 476 | */ |
| 477 | ax25_cb *ax25_create_cb(void) |
| 478 | { |
| 479 | ax25_cb *ax25; |
| 480 | |
| 481 | if ((ax25 = kmalloc(sizeof(*ax25), GFP_ATOMIC)) == NULL) |
| 482 | return NULL; |
| 483 | |
| 484 | memset(ax25, 0x00, sizeof(*ax25)); |
| 485 | atomic_set(&ax25->refcount, 1); |
| 486 | |
| 487 | skb_queue_head_init(&ax25->write_queue); |
| 488 | skb_queue_head_init(&ax25->frag_queue); |
| 489 | skb_queue_head_init(&ax25->ack_queue); |
| 490 | skb_queue_head_init(&ax25->reseq_queue); |
| 491 | |
| 492 | init_timer(&ax25->timer); |
| 493 | init_timer(&ax25->t1timer); |
| 494 | init_timer(&ax25->t2timer); |
| 495 | init_timer(&ax25->t3timer); |
| 496 | init_timer(&ax25->idletimer); |
| 497 | |
| 498 | ax25_fillin_cb(ax25, NULL); |
| 499 | |
| 500 | ax25->state = AX25_STATE_0; |
| 501 | |
| 502 | return ax25; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Handling for system calls applied via the various interfaces to an |
| 507 | * AX25 socket object |
| 508 | */ |
| 509 | |
| 510 | static int ax25_setsockopt(struct socket *sock, int level, int optname, |
| 511 | char __user *optval, int optlen) |
| 512 | { |
| 513 | struct sock *sk = sock->sk; |
| 514 | ax25_cb *ax25; |
| 515 | struct net_device *dev; |
| 516 | char devname[IFNAMSIZ]; |
| 517 | int opt, res = 0; |
| 518 | |
| 519 | if (level != SOL_AX25) |
| 520 | return -ENOPROTOOPT; |
| 521 | |
| 522 | if (optlen < sizeof(int)) |
| 523 | return -EINVAL; |
| 524 | |
| 525 | if (get_user(opt, (int __user *)optval)) |
| 526 | return -EFAULT; |
| 527 | |
| 528 | lock_sock(sk); |
| 529 | ax25 = ax25_sk(sk); |
| 530 | |
| 531 | switch (optname) { |
| 532 | case AX25_WINDOW: |
| 533 | if (ax25->modulus == AX25_MODULUS) { |
| 534 | if (opt < 1 || opt > 7) { |
| 535 | res = -EINVAL; |
| 536 | break; |
| 537 | } |
| 538 | } else { |
| 539 | if (opt < 1 || opt > 63) { |
| 540 | res = -EINVAL; |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | ax25->window = opt; |
| 545 | break; |
| 546 | |
| 547 | case AX25_T1: |
| 548 | if (opt < 1) { |
| 549 | res = -EINVAL; |
| 550 | break; |
| 551 | } |
| 552 | ax25->rtt = (opt * HZ) / 2; |
| 553 | ax25->t1 = opt * HZ; |
| 554 | break; |
| 555 | |
| 556 | case AX25_T2: |
| 557 | if (opt < 1) { |
| 558 | res = -EINVAL; |
| 559 | break; |
| 560 | } |
| 561 | ax25->t2 = opt * HZ; |
| 562 | break; |
| 563 | |
| 564 | case AX25_N2: |
| 565 | if (opt < 1 || opt > 31) { |
| 566 | res = -EINVAL; |
| 567 | break; |
| 568 | } |
| 569 | ax25->n2 = opt; |
| 570 | break; |
| 571 | |
| 572 | case AX25_T3: |
| 573 | if (opt < 1) { |
| 574 | res = -EINVAL; |
| 575 | break; |
| 576 | } |
| 577 | ax25->t3 = opt * HZ; |
| 578 | break; |
| 579 | |
| 580 | case AX25_IDLE: |
| 581 | if (opt < 0) { |
| 582 | res = -EINVAL; |
| 583 | break; |
| 584 | } |
| 585 | ax25->idle = opt * 60 * HZ; |
| 586 | break; |
| 587 | |
| 588 | case AX25_BACKOFF: |
| 589 | if (opt < 0 || opt > 2) { |
| 590 | res = -EINVAL; |
| 591 | break; |
| 592 | } |
| 593 | ax25->backoff = opt; |
| 594 | break; |
| 595 | |
| 596 | case AX25_EXTSEQ: |
| 597 | ax25->modulus = opt ? AX25_EMODULUS : AX25_MODULUS; |
| 598 | break; |
| 599 | |
| 600 | case AX25_PIDINCL: |
| 601 | ax25->pidincl = opt ? 1 : 0; |
| 602 | break; |
| 603 | |
| 604 | case AX25_IAMDIGI: |
| 605 | ax25->iamdigi = opt ? 1 : 0; |
| 606 | break; |
| 607 | |
| 608 | case AX25_PACLEN: |
| 609 | if (opt < 16 || opt > 65535) { |
| 610 | res = -EINVAL; |
| 611 | break; |
| 612 | } |
| 613 | ax25->paclen = opt; |
| 614 | break; |
| 615 | |
| 616 | case SO_BINDTODEVICE: |
| 617 | if (optlen > IFNAMSIZ) |
| 618 | optlen=IFNAMSIZ; |
| 619 | if (copy_from_user(devname, optval, optlen)) { |
| 620 | res = -EFAULT; |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | dev = dev_get_by_name(devname); |
| 625 | if (dev == NULL) { |
| 626 | res = -ENODEV; |
| 627 | break; |
| 628 | } |
| 629 | |
| 630 | if (sk->sk_type == SOCK_SEQPACKET && |
| 631 | (sock->state != SS_UNCONNECTED || |
| 632 | sk->sk_state == TCP_LISTEN)) { |
| 633 | res = -EADDRNOTAVAIL; |
| 634 | dev_put(dev); |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | ax25->ax25_dev = ax25_dev_ax25dev(dev); |
| 639 | ax25_fillin_cb(ax25, ax25->ax25_dev); |
| 640 | break; |
| 641 | |
| 642 | default: |
| 643 | res = -ENOPROTOOPT; |
| 644 | } |
| 645 | release_sock(sk); |
| 646 | |
| 647 | return res; |
| 648 | } |
| 649 | |
| 650 | static int ax25_getsockopt(struct socket *sock, int level, int optname, |
| 651 | char __user *optval, int __user *optlen) |
| 652 | { |
| 653 | struct sock *sk = sock->sk; |
| 654 | ax25_cb *ax25; |
| 655 | struct ax25_dev *ax25_dev; |
| 656 | char devname[IFNAMSIZ]; |
| 657 | void *valptr; |
| 658 | int val = 0; |
| 659 | int maxlen, length; |
| 660 | |
| 661 | if (level != SOL_AX25) |
| 662 | return -ENOPROTOOPT; |
| 663 | |
| 664 | if (get_user(maxlen, optlen)) |
| 665 | return -EFAULT; |
| 666 | |
| 667 | if (maxlen < 1) |
| 668 | return -EFAULT; |
| 669 | |
| 670 | valptr = (void *) &val; |
| 671 | length = min_t(unsigned int, maxlen, sizeof(int)); |
| 672 | |
| 673 | lock_sock(sk); |
| 674 | ax25 = ax25_sk(sk); |
| 675 | |
| 676 | switch (optname) { |
| 677 | case AX25_WINDOW: |
| 678 | val = ax25->window; |
| 679 | break; |
| 680 | |
| 681 | case AX25_T1: |
| 682 | val = ax25->t1 / HZ; |
| 683 | break; |
| 684 | |
| 685 | case AX25_T2: |
| 686 | val = ax25->t2 / HZ; |
| 687 | break; |
| 688 | |
| 689 | case AX25_N2: |
| 690 | val = ax25->n2; |
| 691 | break; |
| 692 | |
| 693 | case AX25_T3: |
| 694 | val = ax25->t3 / HZ; |
| 695 | break; |
| 696 | |
| 697 | case AX25_IDLE: |
| 698 | val = ax25->idle / (60 * HZ); |
| 699 | break; |
| 700 | |
| 701 | case AX25_BACKOFF: |
| 702 | val = ax25->backoff; |
| 703 | break; |
| 704 | |
| 705 | case AX25_EXTSEQ: |
| 706 | val = (ax25->modulus == AX25_EMODULUS); |
| 707 | break; |
| 708 | |
| 709 | case AX25_PIDINCL: |
| 710 | val = ax25->pidincl; |
| 711 | break; |
| 712 | |
| 713 | case AX25_IAMDIGI: |
| 714 | val = ax25->iamdigi; |
| 715 | break; |
| 716 | |
| 717 | case AX25_PACLEN: |
| 718 | val = ax25->paclen; |
| 719 | break; |
| 720 | |
| 721 | case SO_BINDTODEVICE: |
| 722 | ax25_dev = ax25->ax25_dev; |
| 723 | |
| 724 | if (ax25_dev != NULL && ax25_dev->dev != NULL) { |
| 725 | strlcpy(devname, ax25_dev->dev->name, sizeof(devname)); |
| 726 | length = strlen(devname) + 1; |
| 727 | } else { |
| 728 | *devname = '\0'; |
| 729 | length = 1; |
| 730 | } |
| 731 | |
| 732 | valptr = (void *) devname; |
| 733 | break; |
| 734 | |
| 735 | default: |
| 736 | release_sock(sk); |
| 737 | return -ENOPROTOOPT; |
| 738 | } |
| 739 | release_sock(sk); |
| 740 | |
| 741 | if (put_user(length, optlen)) |
| 742 | return -EFAULT; |
| 743 | |
| 744 | return copy_to_user(optval, valptr, length) ? -EFAULT : 0; |
| 745 | } |
| 746 | |
| 747 | static int ax25_listen(struct socket *sock, int backlog) |
| 748 | { |
| 749 | struct sock *sk = sock->sk; |
| 750 | int res = 0; |
| 751 | |
| 752 | lock_sock(sk); |
| 753 | if (sk->sk_type == SOCK_SEQPACKET && sk->sk_state != TCP_LISTEN) { |
| 754 | sk->sk_max_ack_backlog = backlog; |
| 755 | sk->sk_state = TCP_LISTEN; |
| 756 | goto out; |
| 757 | } |
| 758 | res = -EOPNOTSUPP; |
| 759 | |
| 760 | out: |
| 761 | release_sock(sk); |
| 762 | |
| 763 | return res; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * XXX: when creating ax25_sock we should update the .obj_size setting |
| 768 | * below. |
| 769 | */ |
| 770 | static struct proto ax25_proto = { |
| 771 | .name = "AX25", |
| 772 | .owner = THIS_MODULE, |
| 773 | .obj_size = sizeof(struct sock), |
| 774 | }; |
| 775 | |
| 776 | static int ax25_create(struct socket *sock, int protocol) |
| 777 | { |
| 778 | struct sock *sk; |
| 779 | ax25_cb *ax25; |
| 780 | |
| 781 | switch (sock->type) { |
| 782 | case SOCK_DGRAM: |
| 783 | if (protocol == 0 || protocol == PF_AX25) |
| 784 | protocol = AX25_P_TEXT; |
| 785 | break; |
| 786 | |
| 787 | case SOCK_SEQPACKET: |
| 788 | switch (protocol) { |
| 789 | case 0: |
| 790 | case PF_AX25: /* For CLX */ |
| 791 | protocol = AX25_P_TEXT; |
| 792 | break; |
| 793 | case AX25_P_SEGMENT: |
| 794 | #ifdef CONFIG_INET |
| 795 | case AX25_P_ARP: |
| 796 | case AX25_P_IP: |
| 797 | #endif |
| 798 | #ifdef CONFIG_NETROM |
| 799 | case AX25_P_NETROM: |
| 800 | #endif |
| 801 | #ifdef CONFIG_ROSE |
| 802 | case AX25_P_ROSE: |
| 803 | #endif |
| 804 | return -ESOCKTNOSUPPORT; |
| 805 | #ifdef CONFIG_NETROM_MODULE |
| 806 | case AX25_P_NETROM: |
| 807 | if (ax25_protocol_is_registered(AX25_P_NETROM)) |
| 808 | return -ESOCKTNOSUPPORT; |
| 809 | #endif |
| 810 | #ifdef CONFIG_ROSE_MODULE |
| 811 | case AX25_P_ROSE: |
| 812 | if (ax25_protocol_is_registered(AX25_P_ROSE)) |
| 813 | return -ESOCKTNOSUPPORT; |
| 814 | #endif |
| 815 | default: |
| 816 | break; |
| 817 | } |
| 818 | break; |
| 819 | |
| 820 | case SOCK_RAW: |
| 821 | break; |
| 822 | default: |
| 823 | return -ESOCKTNOSUPPORT; |
| 824 | } |
| 825 | |
| 826 | if ((sk = sk_alloc(PF_AX25, GFP_ATOMIC, &ax25_proto, 1)) == NULL) |
| 827 | return -ENOMEM; |
| 828 | |
| 829 | ax25 = sk->sk_protinfo = ax25_create_cb(); |
| 830 | if (!ax25) { |
| 831 | sk_free(sk); |
| 832 | return -ENOMEM; |
| 833 | } |
| 834 | |
| 835 | sock_init_data(sock, sk); |
| 836 | |
| 837 | sk->sk_destruct = ax25_free_sock; |
| 838 | sock->ops = &ax25_proto_ops; |
| 839 | sk->sk_protocol = protocol; |
| 840 | |
| 841 | ax25->sk = sk; |
| 842 | |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | struct sock *ax25_make_new(struct sock *osk, struct ax25_dev *ax25_dev) |
| 847 | { |
| 848 | struct sock *sk; |
| 849 | ax25_cb *ax25, *oax25; |
| 850 | |
| 851 | if ((sk = sk_alloc(PF_AX25, GFP_ATOMIC, osk->sk_prot, 1)) == NULL) |
| 852 | return NULL; |
| 853 | |
| 854 | if ((ax25 = ax25_create_cb()) == NULL) { |
| 855 | sk_free(sk); |
| 856 | return NULL; |
| 857 | } |
| 858 | |
| 859 | switch (osk->sk_type) { |
| 860 | case SOCK_DGRAM: |
| 861 | break; |
| 862 | case SOCK_SEQPACKET: |
| 863 | break; |
| 864 | default: |
| 865 | sk_free(sk); |
| 866 | ax25_cb_put(ax25); |
| 867 | return NULL; |
| 868 | } |
| 869 | |
| 870 | sock_init_data(NULL, sk); |
| 871 | |
| 872 | sk->sk_destruct = ax25_free_sock; |
| 873 | sk->sk_type = osk->sk_type; |
| 874 | sk->sk_socket = osk->sk_socket; |
| 875 | sk->sk_priority = osk->sk_priority; |
| 876 | sk->sk_protocol = osk->sk_protocol; |
| 877 | sk->sk_rcvbuf = osk->sk_rcvbuf; |
| 878 | sk->sk_sndbuf = osk->sk_sndbuf; |
| 879 | sk->sk_state = TCP_ESTABLISHED; |
| 880 | sk->sk_sleep = osk->sk_sleep; |
Ralf Baechle | 53b924b | 2005-08-23 10:11:30 -0700 | [diff] [blame] | 881 | sock_copy_flags(sk, osk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
| 883 | oax25 = ax25_sk(osk); |
| 884 | |
| 885 | ax25->modulus = oax25->modulus; |
| 886 | ax25->backoff = oax25->backoff; |
| 887 | ax25->pidincl = oax25->pidincl; |
| 888 | ax25->iamdigi = oax25->iamdigi; |
| 889 | ax25->rtt = oax25->rtt; |
| 890 | ax25->t1 = oax25->t1; |
| 891 | ax25->t2 = oax25->t2; |
| 892 | ax25->t3 = oax25->t3; |
| 893 | ax25->n2 = oax25->n2; |
| 894 | ax25->idle = oax25->idle; |
| 895 | ax25->paclen = oax25->paclen; |
| 896 | ax25->window = oax25->window; |
| 897 | |
| 898 | ax25->ax25_dev = ax25_dev; |
| 899 | ax25->source_addr = oax25->source_addr; |
| 900 | |
| 901 | if (oax25->digipeat != NULL) { |
| 902 | if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) { |
| 903 | sk_free(sk); |
| 904 | ax25_cb_put(ax25); |
| 905 | return NULL; |
| 906 | } |
| 907 | |
| 908 | memcpy(ax25->digipeat, oax25->digipeat, sizeof(ax25_digi)); |
| 909 | } |
| 910 | |
| 911 | sk->sk_protinfo = ax25; |
| 912 | ax25->sk = sk; |
| 913 | |
| 914 | return sk; |
| 915 | } |
| 916 | |
| 917 | static int ax25_release(struct socket *sock) |
| 918 | { |
| 919 | struct sock *sk = sock->sk; |
| 920 | ax25_cb *ax25; |
| 921 | |
| 922 | if (sk == NULL) |
| 923 | return 0; |
| 924 | |
| 925 | sock_hold(sk); |
| 926 | sock_orphan(sk); |
| 927 | lock_sock(sk); |
| 928 | ax25 = ax25_sk(sk); |
| 929 | |
| 930 | if (sk->sk_type == SOCK_SEQPACKET) { |
| 931 | switch (ax25->state) { |
| 932 | case AX25_STATE_0: |
| 933 | release_sock(sk); |
| 934 | ax25_disconnect(ax25, 0); |
| 935 | lock_sock(sk); |
| 936 | ax25_destroy_socket(ax25); |
| 937 | break; |
| 938 | |
| 939 | case AX25_STATE_1: |
| 940 | case AX25_STATE_2: |
| 941 | ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND); |
| 942 | release_sock(sk); |
| 943 | ax25_disconnect(ax25, 0); |
| 944 | lock_sock(sk); |
| 945 | ax25_destroy_socket(ax25); |
| 946 | break; |
| 947 | |
| 948 | case AX25_STATE_3: |
| 949 | case AX25_STATE_4: |
| 950 | ax25_clear_queues(ax25); |
| 951 | ax25->n2count = 0; |
| 952 | |
| 953 | switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { |
| 954 | case AX25_PROTO_STD_SIMPLEX: |
| 955 | case AX25_PROTO_STD_DUPLEX: |
| 956 | ax25_send_control(ax25, |
| 957 | AX25_DISC, |
| 958 | AX25_POLLON, |
| 959 | AX25_COMMAND); |
| 960 | ax25_stop_t2timer(ax25); |
| 961 | ax25_stop_t3timer(ax25); |
| 962 | ax25_stop_idletimer(ax25); |
| 963 | break; |
| 964 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 965 | case AX25_PROTO_DAMA_SLAVE: |
| 966 | ax25_stop_t3timer(ax25); |
| 967 | ax25_stop_idletimer(ax25); |
| 968 | break; |
| 969 | #endif |
| 970 | } |
| 971 | ax25_calculate_t1(ax25); |
| 972 | ax25_start_t1timer(ax25); |
| 973 | ax25->state = AX25_STATE_2; |
| 974 | sk->sk_state = TCP_CLOSE; |
| 975 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 976 | sk->sk_state_change(sk); |
| 977 | sock_set_flag(sk, SOCK_DESTROY); |
| 978 | break; |
| 979 | |
| 980 | default: |
| 981 | break; |
| 982 | } |
| 983 | } else { |
| 984 | sk->sk_state = TCP_CLOSE; |
| 985 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 986 | sk->sk_state_change(sk); |
| 987 | ax25_destroy_socket(ax25); |
| 988 | } |
| 989 | |
| 990 | sock->sk = NULL; |
| 991 | release_sock(sk); |
| 992 | sock_put(sk); |
| 993 | |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * We support a funny extension here so you can (as root) give any callsign |
| 999 | * digipeated via a local address as source. This hack is obsolete now |
| 1000 | * that we've implemented support for SO_BINDTODEVICE. It is however small |
| 1001 | * and trivially backward compatible. |
| 1002 | */ |
| 1003 | static int ax25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 1004 | { |
| 1005 | struct sock *sk = sock->sk; |
| 1006 | struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr; |
| 1007 | ax25_dev *ax25_dev = NULL; |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 1008 | ax25_uid_assoc *user; |
| 1009 | ax25_address call; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | ax25_cb *ax25; |
| 1011 | int err = 0; |
| 1012 | |
| 1013 | if (addr_len != sizeof(struct sockaddr_ax25) && |
| 1014 | addr_len != sizeof(struct full_sockaddr_ax25)) { |
| 1015 | /* support for old structure may go away some time */ |
| 1016 | if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) || |
| 1017 | (addr_len > sizeof(struct full_sockaddr_ax25))) { |
| 1018 | return -EINVAL; |
| 1019 | } |
| 1020 | |
| 1021 | printk(KERN_WARNING "ax25_bind(): %s uses old (6 digipeater) socket structure.\n", |
| 1022 | current->comm); |
| 1023 | } |
| 1024 | |
| 1025 | if (addr->fsa_ax25.sax25_family != AF_AX25) |
| 1026 | return -EINVAL; |
| 1027 | |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 1028 | user = ax25_findbyuid(current->euid); |
| 1029 | if (user) { |
| 1030 | call = user->call; |
| 1031 | ax25_uid_put(user); |
| 1032 | } else { |
| 1033 | if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) |
| 1034 | return -EACCES; |
| 1035 | |
| 1036 | call = addr->fsa_ax25.sax25_call; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | lock_sock(sk); |
| 1040 | |
| 1041 | ax25 = ax25_sk(sk); |
| 1042 | if (!sock_flag(sk, SOCK_ZAPPED)) { |
| 1043 | err = -EINVAL; |
| 1044 | goto out; |
| 1045 | } |
| 1046 | |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 1047 | ax25->source_addr = call; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
| 1049 | /* |
| 1050 | * User already set interface with SO_BINDTODEVICE |
| 1051 | */ |
| 1052 | if (ax25->ax25_dev != NULL) |
| 1053 | goto done; |
| 1054 | |
| 1055 | if (addr_len > sizeof(struct sockaddr_ax25) && addr->fsa_ax25.sax25_ndigis == 1) { |
| 1056 | if (ax25cmp(&addr->fsa_digipeater[0], &null_ax25_address) != 0 && |
| 1057 | (ax25_dev = ax25_addr_ax25dev(&addr->fsa_digipeater[0])) == NULL) { |
| 1058 | err = -EADDRNOTAVAIL; |
| 1059 | goto out; |
| 1060 | } |
| 1061 | } else { |
| 1062 | if ((ax25_dev = ax25_addr_ax25dev(&addr->fsa_ax25.sax25_call)) == NULL) { |
| 1063 | err = -EADDRNOTAVAIL; |
| 1064 | goto out; |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | if (ax25_dev != NULL) |
| 1069 | ax25_fillin_cb(ax25, ax25_dev); |
| 1070 | |
| 1071 | done: |
| 1072 | ax25_cb_add(ax25); |
| 1073 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 1074 | |
| 1075 | out: |
| 1076 | release_sock(sk); |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * FIXME: nonblock behaviour looks like it may have a bug. |
| 1083 | */ |
| 1084 | static int ax25_connect(struct socket *sock, struct sockaddr *uaddr, |
| 1085 | int addr_len, int flags) |
| 1086 | { |
| 1087 | struct sock *sk = sock->sk; |
| 1088 | ax25_cb *ax25 = ax25_sk(sk), *ax25t; |
| 1089 | struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)uaddr; |
| 1090 | ax25_digi *digi = NULL; |
| 1091 | int ct = 0, err = 0; |
| 1092 | |
| 1093 | /* |
| 1094 | * some sanity checks. code further down depends on this |
| 1095 | */ |
| 1096 | |
| 1097 | if (addr_len == sizeof(struct sockaddr_ax25)) { |
| 1098 | /* support for this will go away in early 2.5.x */ |
| 1099 | printk(KERN_WARNING "ax25_connect(): %s uses obsolete socket structure\n", |
| 1100 | current->comm); |
| 1101 | } |
| 1102 | else if (addr_len != sizeof(struct full_sockaddr_ax25)) { |
| 1103 | /* support for old structure may go away some time */ |
| 1104 | if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) || |
| 1105 | (addr_len > sizeof(struct full_sockaddr_ax25))) { |
| 1106 | return -EINVAL; |
| 1107 | } |
| 1108 | |
| 1109 | printk(KERN_WARNING "ax25_connect(): %s uses old (6 digipeater) socket structure.\n", |
| 1110 | current->comm); |
| 1111 | } |
| 1112 | |
| 1113 | if (fsa->fsa_ax25.sax25_family != AF_AX25) |
| 1114 | return -EINVAL; |
| 1115 | |
| 1116 | lock_sock(sk); |
| 1117 | |
| 1118 | /* deal with restarts */ |
| 1119 | if (sock->state == SS_CONNECTING) { |
| 1120 | switch (sk->sk_state) { |
| 1121 | case TCP_SYN_SENT: /* still trying */ |
| 1122 | err = -EINPROGRESS; |
| 1123 | goto out; |
| 1124 | |
| 1125 | case TCP_ESTABLISHED: /* connection established */ |
| 1126 | sock->state = SS_CONNECTED; |
| 1127 | goto out; |
| 1128 | |
| 1129 | case TCP_CLOSE: /* connection refused */ |
| 1130 | sock->state = SS_UNCONNECTED; |
| 1131 | err = -ECONNREFUSED; |
| 1132 | goto out; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | if (sk->sk_state == TCP_ESTABLISHED && sk->sk_type == SOCK_SEQPACKET) { |
| 1137 | err = -EISCONN; /* No reconnect on a seqpacket socket */ |
| 1138 | goto out; |
| 1139 | } |
| 1140 | |
| 1141 | sk->sk_state = TCP_CLOSE; |
| 1142 | sock->state = SS_UNCONNECTED; |
| 1143 | |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 1144 | kfree(ax25->digipeat); |
| 1145 | ax25->digipeat = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | |
| 1147 | /* |
| 1148 | * Handle digi-peaters to be used. |
| 1149 | */ |
| 1150 | if (addr_len > sizeof(struct sockaddr_ax25) && |
| 1151 | fsa->fsa_ax25.sax25_ndigis != 0) { |
| 1152 | /* Valid number of digipeaters ? */ |
| 1153 | if (fsa->fsa_ax25.sax25_ndigis < 1 || fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS) { |
| 1154 | err = -EINVAL; |
| 1155 | goto out; |
| 1156 | } |
| 1157 | |
| 1158 | if ((digi = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) { |
| 1159 | err = -ENOBUFS; |
| 1160 | goto out; |
| 1161 | } |
| 1162 | |
| 1163 | digi->ndigi = fsa->fsa_ax25.sax25_ndigis; |
| 1164 | digi->lastrepeat = -1; |
| 1165 | |
| 1166 | while (ct < fsa->fsa_ax25.sax25_ndigis) { |
| 1167 | if ((fsa->fsa_digipeater[ct].ax25_call[6] & |
| 1168 | AX25_HBIT) && ax25->iamdigi) { |
| 1169 | digi->repeated[ct] = 1; |
| 1170 | digi->lastrepeat = ct; |
| 1171 | } else { |
| 1172 | digi->repeated[ct] = 0; |
| 1173 | } |
| 1174 | digi->calls[ct] = fsa->fsa_digipeater[ct]; |
| 1175 | ct++; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * Must bind first - autobinding in this may or may not work. If |
| 1181 | * the socket is already bound, check to see if the device has |
| 1182 | * been filled in, error if it hasn't. |
| 1183 | */ |
| 1184 | if (sock_flag(sk, SOCK_ZAPPED)) { |
| 1185 | /* check if we can remove this feature. It is broken. */ |
| 1186 | printk(KERN_WARNING "ax25_connect(): %s uses autobind, please contact jreuter@yaina.de\n", |
| 1187 | current->comm); |
| 1188 | if ((err = ax25_rt_autobind(ax25, &fsa->fsa_ax25.sax25_call)) < 0) { |
| 1189 | kfree(digi); |
| 1190 | goto out; |
| 1191 | } |
| 1192 | |
| 1193 | ax25_fillin_cb(ax25, ax25->ax25_dev); |
| 1194 | ax25_cb_add(ax25); |
| 1195 | } else { |
| 1196 | if (ax25->ax25_dev == NULL) { |
| 1197 | kfree(digi); |
| 1198 | err = -EHOSTUNREACH; |
| 1199 | goto out; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | if (sk->sk_type == SOCK_SEQPACKET && |
| 1204 | (ax25t=ax25_find_cb(&ax25->source_addr, &fsa->fsa_ax25.sax25_call, digi, |
| 1205 | ax25->ax25_dev->dev))) { |
| 1206 | kfree(digi); |
| 1207 | err = -EADDRINUSE; /* Already such a connection */ |
| 1208 | ax25_cb_put(ax25t); |
| 1209 | goto out; |
| 1210 | } |
| 1211 | |
| 1212 | ax25->dest_addr = fsa->fsa_ax25.sax25_call; |
| 1213 | ax25->digipeat = digi; |
| 1214 | |
| 1215 | /* First the easy one */ |
| 1216 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 1217 | sock->state = SS_CONNECTED; |
| 1218 | sk->sk_state = TCP_ESTABLISHED; |
| 1219 | goto out; |
| 1220 | } |
| 1221 | |
| 1222 | /* Move to connecting socket, ax.25 lapb WAIT_UA.. */ |
| 1223 | sock->state = SS_CONNECTING; |
| 1224 | sk->sk_state = TCP_SYN_SENT; |
| 1225 | |
| 1226 | switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) { |
| 1227 | case AX25_PROTO_STD_SIMPLEX: |
| 1228 | case AX25_PROTO_STD_DUPLEX: |
| 1229 | ax25_std_establish_data_link(ax25); |
| 1230 | break; |
| 1231 | |
| 1232 | #ifdef CONFIG_AX25_DAMA_SLAVE |
| 1233 | case AX25_PROTO_DAMA_SLAVE: |
| 1234 | ax25->modulus = AX25_MODULUS; |
| 1235 | ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW]; |
| 1236 | if (ax25->ax25_dev->dama.slave) |
| 1237 | ax25_ds_establish_data_link(ax25); |
| 1238 | else |
| 1239 | ax25_std_establish_data_link(ax25); |
| 1240 | break; |
| 1241 | #endif |
| 1242 | } |
| 1243 | |
| 1244 | ax25->state = AX25_STATE_1; |
| 1245 | |
| 1246 | ax25_start_heartbeat(ax25); |
| 1247 | |
| 1248 | /* Now the loop */ |
| 1249 | if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) { |
| 1250 | err = -EINPROGRESS; |
| 1251 | goto out; |
| 1252 | } |
| 1253 | |
| 1254 | if (sk->sk_state == TCP_SYN_SENT) { |
| 1255 | struct task_struct *tsk = current; |
| 1256 | DECLARE_WAITQUEUE(wait, tsk); |
| 1257 | |
| 1258 | add_wait_queue(sk->sk_sleep, &wait); |
| 1259 | for (;;) { |
| 1260 | if (sk->sk_state != TCP_SYN_SENT) |
| 1261 | break; |
| 1262 | set_current_state(TASK_INTERRUPTIBLE); |
| 1263 | release_sock(sk); |
| 1264 | if (!signal_pending(tsk)) { |
| 1265 | schedule(); |
| 1266 | lock_sock(sk); |
| 1267 | continue; |
| 1268 | } |
| 1269 | current->state = TASK_RUNNING; |
| 1270 | remove_wait_queue(sk->sk_sleep, &wait); |
| 1271 | return -ERESTARTSYS; |
| 1272 | } |
| 1273 | current->state = TASK_RUNNING; |
| 1274 | remove_wait_queue(sk->sk_sleep, &wait); |
| 1275 | } |
| 1276 | |
| 1277 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1278 | /* Not in ABM, not in WAIT_UA -> failed */ |
| 1279 | sock->state = SS_UNCONNECTED; |
| 1280 | err = sock_error(sk); /* Always set at this point */ |
| 1281 | goto out; |
| 1282 | } |
| 1283 | |
| 1284 | sock->state = SS_CONNECTED; |
| 1285 | |
| 1286 | err=0; |
| 1287 | out: |
| 1288 | release_sock(sk); |
| 1289 | |
| 1290 | return err; |
| 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | static int ax25_accept(struct socket *sock, struct socket *newsock, int flags) |
| 1295 | { |
| 1296 | struct task_struct *tsk = current; |
| 1297 | DECLARE_WAITQUEUE(wait, tsk); |
| 1298 | struct sk_buff *skb; |
| 1299 | struct sock *newsk; |
| 1300 | struct sock *sk; |
| 1301 | int err = 0; |
| 1302 | |
| 1303 | if (sock->state != SS_UNCONNECTED) |
| 1304 | return -EINVAL; |
| 1305 | |
| 1306 | if ((sk = sock->sk) == NULL) |
| 1307 | return -EINVAL; |
| 1308 | |
| 1309 | lock_sock(sk); |
| 1310 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 1311 | err = -EOPNOTSUPP; |
| 1312 | goto out; |
| 1313 | } |
| 1314 | |
| 1315 | if (sk->sk_state != TCP_LISTEN) { |
| 1316 | err = -EINVAL; |
| 1317 | goto out; |
| 1318 | } |
| 1319 | |
| 1320 | /* |
| 1321 | * The read queue this time is holding sockets ready to use |
| 1322 | * hooked into the SABM we saved |
| 1323 | */ |
| 1324 | add_wait_queue(sk->sk_sleep, &wait); |
| 1325 | for (;;) { |
| 1326 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 1327 | if (skb) |
| 1328 | break; |
| 1329 | |
| 1330 | release_sock(sk); |
| 1331 | current->state = TASK_INTERRUPTIBLE; |
| 1332 | if (flags & O_NONBLOCK) { |
| 1333 | current->state = TASK_RUNNING; |
| 1334 | remove_wait_queue(sk->sk_sleep, &wait); |
| 1335 | return -EWOULDBLOCK; |
| 1336 | } |
| 1337 | if (!signal_pending(tsk)) { |
| 1338 | schedule(); |
| 1339 | lock_sock(sk); |
| 1340 | continue; |
| 1341 | } |
| 1342 | current->state = TASK_RUNNING; |
| 1343 | remove_wait_queue(sk->sk_sleep, &wait); |
| 1344 | return -ERESTARTSYS; |
| 1345 | } |
| 1346 | current->state = TASK_RUNNING; |
| 1347 | remove_wait_queue(sk->sk_sleep, &wait); |
| 1348 | |
| 1349 | newsk = skb->sk; |
| 1350 | newsk->sk_socket = newsock; |
| 1351 | newsk->sk_sleep = &newsock->wait; |
| 1352 | |
| 1353 | /* Now attach up the new socket */ |
| 1354 | kfree_skb(skb); |
| 1355 | sk->sk_ack_backlog--; |
| 1356 | newsock->sk = newsk; |
| 1357 | newsock->state = SS_CONNECTED; |
| 1358 | |
| 1359 | out: |
| 1360 | release_sock(sk); |
| 1361 | |
| 1362 | return err; |
| 1363 | } |
| 1364 | |
| 1365 | static int ax25_getname(struct socket *sock, struct sockaddr *uaddr, |
| 1366 | int *uaddr_len, int peer) |
| 1367 | { |
| 1368 | struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)uaddr; |
| 1369 | struct sock *sk = sock->sk; |
| 1370 | unsigned char ndigi, i; |
| 1371 | ax25_cb *ax25; |
| 1372 | int err = 0; |
| 1373 | |
| 1374 | lock_sock(sk); |
| 1375 | ax25 = ax25_sk(sk); |
| 1376 | |
| 1377 | if (peer != 0) { |
| 1378 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1379 | err = -ENOTCONN; |
| 1380 | goto out; |
| 1381 | } |
| 1382 | |
| 1383 | fsa->fsa_ax25.sax25_family = AF_AX25; |
| 1384 | fsa->fsa_ax25.sax25_call = ax25->dest_addr; |
| 1385 | fsa->fsa_ax25.sax25_ndigis = 0; |
| 1386 | |
| 1387 | if (ax25->digipeat != NULL) { |
| 1388 | ndigi = ax25->digipeat->ndigi; |
| 1389 | fsa->fsa_ax25.sax25_ndigis = ndigi; |
| 1390 | for (i = 0; i < ndigi; i++) |
| 1391 | fsa->fsa_digipeater[i] = |
| 1392 | ax25->digipeat->calls[i]; |
| 1393 | } |
| 1394 | } else { |
| 1395 | fsa->fsa_ax25.sax25_family = AF_AX25; |
| 1396 | fsa->fsa_ax25.sax25_call = ax25->source_addr; |
| 1397 | fsa->fsa_ax25.sax25_ndigis = 1; |
| 1398 | if (ax25->ax25_dev != NULL) { |
| 1399 | memcpy(&fsa->fsa_digipeater[0], |
| 1400 | ax25->ax25_dev->dev->dev_addr, AX25_ADDR_LEN); |
| 1401 | } else { |
| 1402 | fsa->fsa_digipeater[0] = null_ax25_address; |
| 1403 | } |
| 1404 | } |
| 1405 | *uaddr_len = sizeof (struct full_sockaddr_ax25); |
| 1406 | |
| 1407 | out: |
| 1408 | release_sock(sk); |
| 1409 | |
| 1410 | return err; |
| 1411 | } |
| 1412 | |
| 1413 | static int ax25_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 1414 | struct msghdr *msg, size_t len) |
| 1415 | { |
| 1416 | struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name; |
| 1417 | struct sock *sk = sock->sk; |
| 1418 | struct sockaddr_ax25 sax; |
| 1419 | struct sk_buff *skb; |
| 1420 | ax25_digi dtmp, *dp; |
| 1421 | unsigned char *asmptr; |
| 1422 | ax25_cb *ax25; |
| 1423 | size_t size; |
| 1424 | int lv, err, addr_len = msg->msg_namelen; |
| 1425 | |
| 1426 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT)) |
| 1427 | return -EINVAL; |
| 1428 | |
| 1429 | lock_sock(sk); |
| 1430 | ax25 = ax25_sk(sk); |
| 1431 | |
| 1432 | if (sock_flag(sk, SOCK_ZAPPED)) { |
| 1433 | err = -EADDRNOTAVAIL; |
| 1434 | goto out; |
| 1435 | } |
| 1436 | |
| 1437 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 1438 | send_sig(SIGPIPE, current, 0); |
| 1439 | err = -EPIPE; |
| 1440 | goto out; |
| 1441 | } |
| 1442 | |
| 1443 | if (ax25->ax25_dev == NULL) { |
| 1444 | err = -ENETUNREACH; |
| 1445 | goto out; |
| 1446 | } |
| 1447 | |
| 1448 | if (len > ax25->ax25_dev->dev->mtu) { |
| 1449 | err = -EMSGSIZE; |
| 1450 | goto out; |
| 1451 | } |
| 1452 | |
| 1453 | if (usax != NULL) { |
| 1454 | if (usax->sax25_family != AF_AX25) { |
| 1455 | err = -EINVAL; |
| 1456 | goto out; |
| 1457 | } |
| 1458 | |
| 1459 | if (addr_len == sizeof(struct sockaddr_ax25)) { |
| 1460 | printk(KERN_WARNING "ax25_sendmsg(): %s uses obsolete socket structure\n", |
| 1461 | current->comm); |
| 1462 | } |
| 1463 | else if (addr_len != sizeof(struct full_sockaddr_ax25)) { |
| 1464 | /* support for old structure may go away some time */ |
| 1465 | if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) || |
| 1466 | (addr_len > sizeof(struct full_sockaddr_ax25))) { |
| 1467 | err = -EINVAL; |
| 1468 | goto out; |
| 1469 | } |
| 1470 | |
| 1471 | printk(KERN_WARNING "ax25_sendmsg(): %s uses old (6 digipeater) socket structure.\n", |
| 1472 | current->comm); |
| 1473 | } |
| 1474 | |
| 1475 | if (addr_len > sizeof(struct sockaddr_ax25) && usax->sax25_ndigis != 0) { |
| 1476 | int ct = 0; |
| 1477 | struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)usax; |
| 1478 | |
| 1479 | /* Valid number of digipeaters ? */ |
| 1480 | if (usax->sax25_ndigis < 1 || usax->sax25_ndigis > AX25_MAX_DIGIS) { |
| 1481 | err = -EINVAL; |
| 1482 | goto out; |
| 1483 | } |
| 1484 | |
| 1485 | dtmp.ndigi = usax->sax25_ndigis; |
| 1486 | |
| 1487 | while (ct < usax->sax25_ndigis) { |
| 1488 | dtmp.repeated[ct] = 0; |
| 1489 | dtmp.calls[ct] = fsa->fsa_digipeater[ct]; |
| 1490 | ct++; |
| 1491 | } |
| 1492 | |
| 1493 | dtmp.lastrepeat = 0; |
| 1494 | } |
| 1495 | |
| 1496 | sax = *usax; |
| 1497 | if (sk->sk_type == SOCK_SEQPACKET && |
| 1498 | ax25cmp(&ax25->dest_addr, &sax.sax25_call)) { |
| 1499 | err = -EISCONN; |
| 1500 | goto out; |
| 1501 | } |
| 1502 | if (usax->sax25_ndigis == 0) |
| 1503 | dp = NULL; |
| 1504 | else |
| 1505 | dp = &dtmp; |
| 1506 | } else { |
| 1507 | /* |
| 1508 | * FIXME: 1003.1g - if the socket is like this because |
| 1509 | * it has become closed (not started closed) and is VC |
| 1510 | * we ought to SIGPIPE, EPIPE |
| 1511 | */ |
| 1512 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1513 | err = -ENOTCONN; |
| 1514 | goto out; |
| 1515 | } |
| 1516 | sax.sax25_family = AF_AX25; |
| 1517 | sax.sax25_call = ax25->dest_addr; |
| 1518 | dp = ax25->digipeat; |
| 1519 | } |
| 1520 | |
| 1521 | SOCK_DEBUG(sk, "AX.25: sendto: Addresses built.\n"); |
| 1522 | |
| 1523 | /* Build a packet */ |
| 1524 | SOCK_DEBUG(sk, "AX.25: sendto: building packet.\n"); |
| 1525 | |
| 1526 | /* Assume the worst case */ |
| 1527 | size = len + ax25->ax25_dev->dev->hard_header_len; |
| 1528 | |
| 1529 | skb = sock_alloc_send_skb(sk, size, msg->msg_flags&MSG_DONTWAIT, &err); |
| 1530 | if (skb == NULL) |
| 1531 | goto out; |
| 1532 | |
| 1533 | skb_reserve(skb, size - len); |
| 1534 | |
| 1535 | SOCK_DEBUG(sk, "AX.25: Appending user data\n"); |
| 1536 | |
| 1537 | /* User data follows immediately after the AX.25 data */ |
| 1538 | if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) { |
| 1539 | err = -EFAULT; |
| 1540 | kfree_skb(skb); |
| 1541 | goto out; |
| 1542 | } |
| 1543 | |
| 1544 | skb->nh.raw = skb->data; |
| 1545 | |
| 1546 | /* Add the PID if one is not supplied by the user in the skb */ |
| 1547 | if (!ax25->pidincl) { |
| 1548 | asmptr = skb_push(skb, 1); |
| 1549 | *asmptr = sk->sk_protocol; |
| 1550 | } |
| 1551 | |
| 1552 | SOCK_DEBUG(sk, "AX.25: Transmitting buffer\n"); |
| 1553 | |
| 1554 | if (sk->sk_type == SOCK_SEQPACKET) { |
| 1555 | /* Connected mode sockets go via the LAPB machine */ |
| 1556 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1557 | kfree_skb(skb); |
| 1558 | err = -ENOTCONN; |
| 1559 | goto out; |
| 1560 | } |
| 1561 | |
| 1562 | /* Shove it onto the queue and kick */ |
| 1563 | ax25_output(ax25, ax25->paclen, skb); |
| 1564 | |
| 1565 | err = len; |
| 1566 | goto out; |
| 1567 | } |
| 1568 | |
| 1569 | asmptr = skb_push(skb, 1 + ax25_addr_size(dp)); |
| 1570 | |
| 1571 | SOCK_DEBUG(sk, "Building AX.25 Header (dp=%p).\n", dp); |
| 1572 | |
| 1573 | if (dp != NULL) |
| 1574 | SOCK_DEBUG(sk, "Num digipeaters=%d\n", dp->ndigi); |
| 1575 | |
| 1576 | /* Build an AX.25 header */ |
| 1577 | asmptr += (lv = ax25_addr_build(asmptr, &ax25->source_addr, |
| 1578 | &sax.sax25_call, dp, |
| 1579 | AX25_COMMAND, AX25_MODULUS)); |
| 1580 | |
| 1581 | SOCK_DEBUG(sk, "Built header (%d bytes)\n",lv); |
| 1582 | |
| 1583 | skb->h.raw = asmptr; |
| 1584 | |
| 1585 | SOCK_DEBUG(sk, "base=%p pos=%p\n", skb->data, asmptr); |
| 1586 | |
| 1587 | *asmptr = AX25_UI; |
| 1588 | |
| 1589 | /* Datagram frames go straight out of the door as UI */ |
Arnaldo Carvalho de Melo | 29c4be5 | 2005-04-21 16:46:56 -0700 | [diff] [blame] | 1590 | ax25_queue_xmit(skb, ax25->ax25_dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | |
| 1592 | err = len; |
| 1593 | |
| 1594 | out: |
| 1595 | release_sock(sk); |
| 1596 | |
| 1597 | return err; |
| 1598 | } |
| 1599 | |
| 1600 | static int ax25_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 1601 | struct msghdr *msg, size_t size, int flags) |
| 1602 | { |
| 1603 | struct sock *sk = sock->sk; |
| 1604 | struct sk_buff *skb; |
| 1605 | int copied; |
| 1606 | int err = 0; |
| 1607 | |
| 1608 | lock_sock(sk); |
| 1609 | /* |
| 1610 | * This works for seqpacket too. The receiver has ordered the |
| 1611 | * queue for us! We do one quick check first though |
| 1612 | */ |
| 1613 | if (sk->sk_type == SOCK_SEQPACKET && sk->sk_state != TCP_ESTABLISHED) { |
| 1614 | err = -ENOTCONN; |
| 1615 | goto out; |
| 1616 | } |
| 1617 | |
| 1618 | /* Now we can treat all alike */ |
| 1619 | skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, |
| 1620 | flags & MSG_DONTWAIT, &err); |
| 1621 | if (skb == NULL) |
| 1622 | goto out; |
| 1623 | |
| 1624 | if (!ax25_sk(sk)->pidincl) |
| 1625 | skb_pull(skb, 1); /* Remove PID */ |
| 1626 | |
| 1627 | skb->h.raw = skb->data; |
| 1628 | copied = skb->len; |
| 1629 | |
| 1630 | if (copied > size) { |
| 1631 | copied = size; |
| 1632 | msg->msg_flags |= MSG_TRUNC; |
| 1633 | } |
| 1634 | |
| 1635 | skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 1636 | |
| 1637 | if (msg->msg_namelen != 0) { |
| 1638 | struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name; |
| 1639 | ax25_digi digi; |
| 1640 | ax25_address src; |
| 1641 | |
| 1642 | ax25_addr_parse(skb->mac.raw+1, skb->data-skb->mac.raw-1, &src, NULL, &digi, NULL, NULL); |
| 1643 | |
| 1644 | sax->sax25_family = AF_AX25; |
| 1645 | /* We set this correctly, even though we may not let the |
| 1646 | application know the digi calls further down (because it |
| 1647 | did NOT ask to know them). This could get political... **/ |
| 1648 | sax->sax25_ndigis = digi.ndigi; |
| 1649 | sax->sax25_call = src; |
| 1650 | |
| 1651 | if (sax->sax25_ndigis != 0) { |
| 1652 | int ct; |
| 1653 | struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)sax; |
| 1654 | |
| 1655 | for (ct = 0; ct < digi.ndigi; ct++) |
| 1656 | fsa->fsa_digipeater[ct] = digi.calls[ct]; |
| 1657 | } |
| 1658 | msg->msg_namelen = sizeof(struct full_sockaddr_ax25); |
| 1659 | } |
| 1660 | |
| 1661 | skb_free_datagram(sk, skb); |
| 1662 | err = copied; |
| 1663 | |
| 1664 | out: |
| 1665 | release_sock(sk); |
| 1666 | |
| 1667 | return err; |
| 1668 | } |
| 1669 | |
| 1670 | static int ax25_shutdown(struct socket *sk, int how) |
| 1671 | { |
| 1672 | /* FIXME - generate DM and RNR states */ |
| 1673 | return -EOPNOTSUPP; |
| 1674 | } |
| 1675 | |
| 1676 | static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 1677 | { |
| 1678 | struct sock *sk = sock->sk; |
| 1679 | void __user *argp = (void __user *)arg; |
| 1680 | int res = 0; |
| 1681 | |
| 1682 | lock_sock(sk); |
| 1683 | switch (cmd) { |
| 1684 | case TIOCOUTQ: { |
| 1685 | long amount; |
| 1686 | amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc); |
| 1687 | if (amount < 0) |
| 1688 | amount = 0; |
| 1689 | res = put_user(amount, (int __user *)argp); |
| 1690 | break; |
| 1691 | } |
| 1692 | |
| 1693 | case TIOCINQ: { |
| 1694 | struct sk_buff *skb; |
| 1695 | long amount = 0L; |
| 1696 | /* These two are safe on a single CPU system as only user tasks fiddle here */ |
| 1697 | if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) |
| 1698 | amount = skb->len; |
Ralf Baechle | 20b7d10 | 2005-09-12 14:24:55 -0700 | [diff] [blame] | 1699 | res = put_user(amount, (int __user *) argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | break; |
| 1701 | } |
| 1702 | |
| 1703 | case SIOCGSTAMP: |
Ralf Baechle | 9b37ee7 | 2005-09-12 14:23:52 -0700 | [diff] [blame] | 1704 | res = sock_get_timestamp(sk, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | break; |
| 1706 | |
| 1707 | case SIOCAX25ADDUID: /* Add a uid to the uid/call map table */ |
| 1708 | case SIOCAX25DELUID: /* Delete a uid from the uid/call map table */ |
| 1709 | case SIOCAX25GETUID: { |
| 1710 | struct sockaddr_ax25 sax25; |
| 1711 | if (copy_from_user(&sax25, argp, sizeof(sax25))) { |
| 1712 | res = -EFAULT; |
| 1713 | break; |
| 1714 | } |
| 1715 | res = ax25_uid_ioctl(cmd, &sax25); |
| 1716 | break; |
| 1717 | } |
| 1718 | |
| 1719 | case SIOCAX25NOUID: { /* Set the default policy (default/bar) */ |
| 1720 | long amount; |
| 1721 | if (!capable(CAP_NET_ADMIN)) { |
| 1722 | res = -EPERM; |
| 1723 | break; |
| 1724 | } |
| 1725 | if (get_user(amount, (long __user *)argp)) { |
| 1726 | res = -EFAULT; |
| 1727 | break; |
| 1728 | } |
| 1729 | if (amount > AX25_NOUID_BLOCK) { |
| 1730 | res = -EINVAL; |
| 1731 | break; |
| 1732 | } |
| 1733 | ax25_uid_policy = amount; |
| 1734 | res = 0; |
| 1735 | break; |
| 1736 | } |
| 1737 | |
| 1738 | case SIOCADDRT: |
| 1739 | case SIOCDELRT: |
| 1740 | case SIOCAX25OPTRT: |
| 1741 | if (!capable(CAP_NET_ADMIN)) { |
| 1742 | res = -EPERM; |
| 1743 | break; |
| 1744 | } |
| 1745 | res = ax25_rt_ioctl(cmd, argp); |
| 1746 | break; |
| 1747 | |
| 1748 | case SIOCAX25CTLCON: |
| 1749 | if (!capable(CAP_NET_ADMIN)) { |
| 1750 | res = -EPERM; |
| 1751 | break; |
| 1752 | } |
| 1753 | res = ax25_ctl_ioctl(cmd, argp); |
| 1754 | break; |
| 1755 | |
| 1756 | case SIOCAX25GETINFO: |
| 1757 | case SIOCAX25GETINFOOLD: { |
| 1758 | ax25_cb *ax25 = ax25_sk(sk); |
| 1759 | struct ax25_info_struct ax25_info; |
| 1760 | |
| 1761 | ax25_info.t1 = ax25->t1 / HZ; |
| 1762 | ax25_info.t2 = ax25->t2 / HZ; |
| 1763 | ax25_info.t3 = ax25->t3 / HZ; |
| 1764 | ax25_info.idle = ax25->idle / (60 * HZ); |
| 1765 | ax25_info.n2 = ax25->n2; |
| 1766 | ax25_info.t1timer = ax25_display_timer(&ax25->t1timer) / HZ; |
| 1767 | ax25_info.t2timer = ax25_display_timer(&ax25->t2timer) / HZ; |
| 1768 | ax25_info.t3timer = ax25_display_timer(&ax25->t3timer) / HZ; |
| 1769 | ax25_info.idletimer = ax25_display_timer(&ax25->idletimer) / (60 * HZ); |
| 1770 | ax25_info.n2count = ax25->n2count; |
| 1771 | ax25_info.state = ax25->state; |
| 1772 | ax25_info.rcv_q = atomic_read(&sk->sk_rmem_alloc); |
| 1773 | ax25_info.snd_q = atomic_read(&sk->sk_wmem_alloc); |
| 1774 | ax25_info.vs = ax25->vs; |
| 1775 | ax25_info.vr = ax25->vr; |
| 1776 | ax25_info.va = ax25->va; |
| 1777 | ax25_info.vs_max = ax25->vs; /* reserved */ |
| 1778 | ax25_info.paclen = ax25->paclen; |
| 1779 | ax25_info.window = ax25->window; |
| 1780 | |
| 1781 | /* old structure? */ |
| 1782 | if (cmd == SIOCAX25GETINFOOLD) { |
| 1783 | static int warned = 0; |
| 1784 | if (!warned) { |
| 1785 | printk(KERN_INFO "%s uses old SIOCAX25GETINFO\n", |
| 1786 | current->comm); |
| 1787 | warned=1; |
| 1788 | } |
| 1789 | |
| 1790 | if (copy_to_user(argp, &ax25_info, sizeof(struct ax25_info_struct_deprecated))) { |
| 1791 | res = -EFAULT; |
| 1792 | break; |
| 1793 | } |
| 1794 | } else { |
| 1795 | if (copy_to_user(argp, &ax25_info, sizeof(struct ax25_info_struct))) { |
| 1796 | res = -EINVAL; |
| 1797 | break; |
| 1798 | } |
| 1799 | } |
| 1800 | res = 0; |
| 1801 | break; |
| 1802 | } |
| 1803 | |
| 1804 | case SIOCAX25ADDFWD: |
| 1805 | case SIOCAX25DELFWD: { |
| 1806 | struct ax25_fwd_struct ax25_fwd; |
| 1807 | if (!capable(CAP_NET_ADMIN)) { |
| 1808 | res = -EPERM; |
| 1809 | break; |
| 1810 | } |
| 1811 | if (copy_from_user(&ax25_fwd, argp, sizeof(ax25_fwd))) { |
| 1812 | res = -EFAULT; |
| 1813 | break; |
| 1814 | } |
| 1815 | res = ax25_fwd_ioctl(cmd, &ax25_fwd); |
| 1816 | break; |
| 1817 | } |
| 1818 | |
| 1819 | case SIOCGIFADDR: |
| 1820 | case SIOCSIFADDR: |
| 1821 | case SIOCGIFDSTADDR: |
| 1822 | case SIOCSIFDSTADDR: |
| 1823 | case SIOCGIFBRDADDR: |
| 1824 | case SIOCSIFBRDADDR: |
| 1825 | case SIOCGIFNETMASK: |
| 1826 | case SIOCSIFNETMASK: |
| 1827 | case SIOCGIFMETRIC: |
| 1828 | case SIOCSIFMETRIC: |
| 1829 | res = -EINVAL; |
| 1830 | break; |
| 1831 | |
| 1832 | default: |
Christoph Hellwig | b5e5fa5 | 2006-01-03 14:18:33 -0800 | [diff] [blame] | 1833 | res = -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | break; |
| 1835 | } |
| 1836 | release_sock(sk); |
| 1837 | |
| 1838 | return res; |
| 1839 | } |
| 1840 | |
| 1841 | #ifdef CONFIG_PROC_FS |
| 1842 | |
| 1843 | static void *ax25_info_start(struct seq_file *seq, loff_t *pos) |
| 1844 | { |
| 1845 | struct ax25_cb *ax25; |
| 1846 | struct hlist_node *node; |
| 1847 | int i = 0; |
| 1848 | |
| 1849 | spin_lock_bh(&ax25_list_lock); |
| 1850 | ax25_for_each(ax25, node, &ax25_list) { |
| 1851 | if (i == *pos) |
| 1852 | return ax25; |
| 1853 | ++i; |
| 1854 | } |
| 1855 | return NULL; |
| 1856 | } |
| 1857 | |
| 1858 | static void *ax25_info_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1859 | { |
| 1860 | ++*pos; |
| 1861 | |
| 1862 | return hlist_entry( ((struct ax25_cb *)v)->ax25_node.next, |
| 1863 | struct ax25_cb, ax25_node); |
| 1864 | } |
| 1865 | |
| 1866 | static void ax25_info_stop(struct seq_file *seq, void *v) |
| 1867 | { |
| 1868 | spin_unlock_bh(&ax25_list_lock); |
| 1869 | } |
| 1870 | |
| 1871 | static int ax25_info_show(struct seq_file *seq, void *v) |
| 1872 | { |
| 1873 | ax25_cb *ax25 = v; |
Ralf Baechle | f75268c | 2005-09-06 15:49:39 -0700 | [diff] [blame] | 1874 | char buf[11]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | int k; |
| 1876 | |
| 1877 | |
| 1878 | /* |
| 1879 | * New format: |
| 1880 | * magic dev src_addr dest_addr,digi1,digi2,.. st vs vr va t1 t1 t2 t2 t3 t3 idle idle n2 n2 rtt window paclen Snd-Q Rcv-Q inode |
| 1881 | */ |
| 1882 | |
| 1883 | seq_printf(seq, "%8.8lx %s %s%s ", |
| 1884 | (long) ax25, |
| 1885 | ax25->ax25_dev == NULL? "???" : ax25->ax25_dev->dev->name, |
Ralf Baechle | f75268c | 2005-09-06 15:49:39 -0700 | [diff] [blame] | 1886 | ax2asc(buf, &ax25->source_addr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | ax25->iamdigi? "*":""); |
Ralf Baechle | f75268c | 2005-09-06 15:49:39 -0700 | [diff] [blame] | 1888 | seq_printf(seq, "%s", ax2asc(buf, &ax25->dest_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | |
| 1890 | for (k=0; (ax25->digipeat != NULL) && (k < ax25->digipeat->ndigi); k++) { |
| 1891 | seq_printf(seq, ",%s%s", |
Ralf Baechle | f75268c | 2005-09-06 15:49:39 -0700 | [diff] [blame] | 1892 | ax2asc(buf, &ax25->digipeat->calls[k]), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | ax25->digipeat->repeated[k]? "*":""); |
| 1894 | } |
| 1895 | |
| 1896 | seq_printf(seq, " %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %d %d", |
| 1897 | ax25->state, |
| 1898 | ax25->vs, ax25->vr, ax25->va, |
| 1899 | ax25_display_timer(&ax25->t1timer) / HZ, ax25->t1 / HZ, |
| 1900 | ax25_display_timer(&ax25->t2timer) / HZ, ax25->t2 / HZ, |
| 1901 | ax25_display_timer(&ax25->t3timer) / HZ, ax25->t3 / HZ, |
| 1902 | ax25_display_timer(&ax25->idletimer) / (60 * HZ), |
| 1903 | ax25->idle / (60 * HZ), |
| 1904 | ax25->n2count, ax25->n2, |
| 1905 | ax25->rtt / HZ, |
| 1906 | ax25->window, |
| 1907 | ax25->paclen); |
| 1908 | |
| 1909 | if (ax25->sk != NULL) { |
| 1910 | bh_lock_sock(ax25->sk); |
| 1911 | seq_printf(seq," %d %d %ld\n", |
| 1912 | atomic_read(&ax25->sk->sk_wmem_alloc), |
| 1913 | atomic_read(&ax25->sk->sk_rmem_alloc), |
| 1914 | ax25->sk->sk_socket != NULL ? SOCK_INODE(ax25->sk->sk_socket)->i_ino : 0L); |
| 1915 | bh_unlock_sock(ax25->sk); |
| 1916 | } else { |
| 1917 | seq_puts(seq, " * * *\n"); |
| 1918 | } |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
| 1922 | static struct seq_operations ax25_info_seqops = { |
| 1923 | .start = ax25_info_start, |
| 1924 | .next = ax25_info_next, |
| 1925 | .stop = ax25_info_stop, |
| 1926 | .show = ax25_info_show, |
| 1927 | }; |
| 1928 | |
| 1929 | static int ax25_info_open(struct inode *inode, struct file *file) |
| 1930 | { |
| 1931 | return seq_open(file, &ax25_info_seqops); |
| 1932 | } |
| 1933 | |
| 1934 | static struct file_operations ax25_info_fops = { |
| 1935 | .owner = THIS_MODULE, |
| 1936 | .open = ax25_info_open, |
| 1937 | .read = seq_read, |
| 1938 | .llseek = seq_lseek, |
| 1939 | .release = seq_release, |
| 1940 | }; |
| 1941 | |
| 1942 | #endif |
| 1943 | |
| 1944 | static struct net_proto_family ax25_family_ops = { |
| 1945 | .family = PF_AX25, |
| 1946 | .create = ax25_create, |
| 1947 | .owner = THIS_MODULE, |
| 1948 | }; |
| 1949 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1950 | static const struct proto_ops ax25_proto_ops = { |
Ralf Baechle | 4676356 | 2005-09-12 14:25:25 -0700 | [diff] [blame] | 1951 | .family = PF_AX25, |
| 1952 | .owner = THIS_MODULE, |
| 1953 | .release = ax25_release, |
| 1954 | .bind = ax25_bind, |
| 1955 | .connect = ax25_connect, |
| 1956 | .socketpair = sock_no_socketpair, |
| 1957 | .accept = ax25_accept, |
| 1958 | .getname = ax25_getname, |
| 1959 | .poll = datagram_poll, |
| 1960 | .ioctl = ax25_ioctl, |
| 1961 | .listen = ax25_listen, |
| 1962 | .shutdown = ax25_shutdown, |
| 1963 | .setsockopt = ax25_setsockopt, |
| 1964 | .getsockopt = ax25_getsockopt, |
| 1965 | .sendmsg = ax25_sendmsg, |
| 1966 | .recvmsg = ax25_recvmsg, |
| 1967 | .mmap = sock_no_mmap, |
| 1968 | .sendpage = sock_no_sendpage, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | }; |
| 1970 | |
| 1971 | /* |
| 1972 | * Called by socket.c on kernel start up |
| 1973 | */ |
| 1974 | static struct packet_type ax25_packet_type = { |
| 1975 | .type = __constant_htons(ETH_P_AX25), |
| 1976 | .dev = NULL, /* All devices */ |
| 1977 | .func = ax25_kiss_rcv, |
| 1978 | }; |
| 1979 | |
| 1980 | static struct notifier_block ax25_dev_notifier = { |
| 1981 | .notifier_call =ax25_device_event, |
| 1982 | }; |
| 1983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | static int __init ax25_init(void) |
| 1985 | { |
| 1986 | int rc = proto_register(&ax25_proto, 0); |
| 1987 | |
| 1988 | if (rc != 0) |
| 1989 | goto out; |
| 1990 | |
| 1991 | sock_register(&ax25_family_ops); |
| 1992 | dev_add_pack(&ax25_packet_type); |
| 1993 | register_netdevice_notifier(&ax25_dev_notifier); |
| 1994 | ax25_register_sysctl(); |
| 1995 | |
| 1996 | proc_net_fops_create("ax25_route", S_IRUGO, &ax25_route_fops); |
| 1997 | proc_net_fops_create("ax25", S_IRUGO, &ax25_info_fops); |
| 1998 | proc_net_fops_create("ax25_calls", S_IRUGO, &ax25_uid_fops); |
| 1999 | out: |
| 2000 | return rc; |
| 2001 | } |
| 2002 | module_init(ax25_init); |
| 2003 | |
| 2004 | |
| 2005 | MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>"); |
| 2006 | MODULE_DESCRIPTION("The amateur radio AX.25 link layer protocol"); |
| 2007 | MODULE_LICENSE("GPL"); |
| 2008 | MODULE_ALIAS_NETPROTO(PF_AX25); |
| 2009 | |
| 2010 | static void __exit ax25_exit(void) |
| 2011 | { |
| 2012 | proc_net_remove("ax25_route"); |
| 2013 | proc_net_remove("ax25"); |
| 2014 | proc_net_remove("ax25_calls"); |
| 2015 | ax25_rt_free(); |
| 2016 | ax25_uid_free(); |
| 2017 | ax25_dev_free(); |
| 2018 | |
| 2019 | ax25_unregister_sysctl(); |
| 2020 | unregister_netdevice_notifier(&ax25_dev_notifier); |
| 2021 | |
| 2022 | dev_remove_pack(&ax25_packet_type); |
| 2023 | |
| 2024 | sock_unregister(PF_AX25); |
| 2025 | proto_unregister(&ax25_proto); |
| 2026 | } |
| 2027 | module_exit(ax25_exit); |