Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /********************************************************************* |
| 2 | * |
| 3 | * Filename: af_irda.c |
| 4 | * Version: 0.9 |
| 5 | * Description: IrDA sockets implementation |
| 6 | * Status: Stable |
| 7 | * Author: Dag Brattli <dagb@cs.uit.no> |
| 8 | * Created at: Sun May 31 10:12:43 1998 |
| 9 | * Modified at: Sat Dec 25 21:10:23 1999 |
| 10 | * Modified by: Dag Brattli <dag@brattli.net> |
| 11 | * Sources: af_netroom.c, af_ax25.c, af_rose.c, af_x25.c etc. |
| 12 | * |
| 13 | * Copyright (c) 1999 Dag Brattli <dagb@cs.uit.no> |
| 14 | * Copyright (c) 1999-2003 Jean Tourrilhes <jt@hpl.hp.com> |
| 15 | * All Rights Reserved. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License as |
| 19 | * published by the Free Software Foundation; either version 2 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 30 | * MA 02111-1307 USA |
| 31 | * |
| 32 | * Linux-IrDA now supports four different types of IrDA sockets: |
| 33 | * |
| 34 | * o SOCK_STREAM: TinyTP connections with SAR disabled. The |
| 35 | * max SDU size is 0 for conn. of this type |
| 36 | * o SOCK_SEQPACKET: TinyTP connections with SAR enabled. TTP may |
| 37 | * fragment the messages, but will preserve |
| 38 | * the message boundaries |
| 39 | * o SOCK_DGRAM: IRDAPROTO_UNITDATA: TinyTP connections with Unitdata |
| 40 | * (unreliable) transfers |
| 41 | * IRDAPROTO_ULTRA: Connectionless and unreliable data |
| 42 | * |
| 43 | ********************************************************************/ |
| 44 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 45 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/module.h> |
| 47 | #include <linux/types.h> |
| 48 | #include <linux/socket.h> |
| 49 | #include <linux/sockios.h> |
| 50 | #include <linux/init.h> |
| 51 | #include <linux/net.h> |
| 52 | #include <linux/irda.h> |
| 53 | #include <linux/poll.h> |
| 54 | |
| 55 | #include <asm/ioctls.h> /* TIOCOUTQ, TIOCINQ */ |
| 56 | #include <asm/uaccess.h> |
| 57 | |
| 58 | #include <net/sock.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 59 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | #include <net/irda/af_irda.h> |
| 62 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 63 | static int irda_create(struct net *net, struct socket *sock, int protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 65 | static const struct proto_ops irda_stream_ops; |
| 66 | static const struct proto_ops irda_seqpacket_ops; |
| 67 | static const struct proto_ops irda_dgram_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | #ifdef CONFIG_IRDA_ULTRA |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 70 | static const struct proto_ops irda_ultra_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #define ULTRA_MAX_DATA 382 |
| 72 | #endif /* CONFIG_IRDA_ULTRA */ |
| 73 | |
| 74 | #define IRDA_MAX_HEADER (TTP_MAX_HEADER) |
| 75 | |
| 76 | /* |
| 77 | * Function irda_data_indication (instance, sap, skb) |
| 78 | * |
| 79 | * Received some data from TinyTP. Just queue it on the receive queue |
| 80 | * |
| 81 | */ |
| 82 | static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb) |
| 83 | { |
| 84 | struct irda_sock *self; |
| 85 | struct sock *sk; |
| 86 | int err; |
| 87 | |
| 88 | IRDA_DEBUG(3, "%s()\n", __FUNCTION__); |
| 89 | |
| 90 | self = instance; |
| 91 | sk = instance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | err = sock_queue_rcv_skb(sk, skb); |
| 94 | if (err) { |
| 95 | IRDA_DEBUG(1, "%s(), error: no more mem!\n", __FUNCTION__); |
| 96 | self->rx_flow = FLOW_STOP; |
| 97 | |
| 98 | /* When we return error, TTP will need to requeue the skb */ |
| 99 | return err; |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Function irda_disconnect_indication (instance, sap, reason, skb) |
| 107 | * |
| 108 | * Connection has been closed. Check reason to find out why |
| 109 | * |
| 110 | */ |
| 111 | static void irda_disconnect_indication(void *instance, void *sap, |
| 112 | LM_REASON reason, struct sk_buff *skb) |
| 113 | { |
| 114 | struct irda_sock *self; |
| 115 | struct sock *sk; |
| 116 | |
| 117 | self = instance; |
| 118 | |
| 119 | IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self); |
| 120 | |
| 121 | /* Don't care about it, but let's not leak it */ |
| 122 | if(skb) |
| 123 | dev_kfree_skb(skb); |
| 124 | |
| 125 | sk = instance; |
| 126 | if (sk == NULL) { |
| 127 | IRDA_DEBUG(0, "%s(%p) : BUG : sk is NULL\n", |
| 128 | __FUNCTION__, self); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | /* Prevent race conditions with irda_release() and irda_shutdown() */ |
Olaf Kirch | 6e66aa1 | 2007-04-20 22:08:15 -0700 | [diff] [blame] | 133 | bh_lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | if (!sock_flag(sk, SOCK_DEAD) && sk->sk_state != TCP_CLOSE) { |
| 135 | sk->sk_state = TCP_CLOSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 137 | |
| 138 | sk->sk_state_change(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | /* Close our TSAP. |
| 141 | * If we leave it open, IrLMP put it back into the list of |
| 142 | * unconnected LSAPs. The problem is that any incoming request |
| 143 | * can then be matched to this socket (and it will be, because |
| 144 | * it is at the head of the list). This would prevent any |
| 145 | * listening socket waiting on the same TSAP to get those |
| 146 | * requests. Some apps forget to close sockets, or hang to it |
| 147 | * a bit too long, so we may stay in this dead state long |
| 148 | * enough to be noticed... |
| 149 | * Note : all socket function do check sk->sk_state, so we are |
| 150 | * safe... |
| 151 | * Jean II |
| 152 | */ |
| 153 | if (self->tsap) { |
| 154 | irttp_close_tsap(self->tsap); |
| 155 | self->tsap = NULL; |
| 156 | } |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 157 | } |
Olaf Kirch | 6e66aa1 | 2007-04-20 22:08:15 -0700 | [diff] [blame] | 158 | bh_unlock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | /* Note : once we are there, there is not much you want to do |
| 161 | * with the socket anymore, apart from closing it. |
| 162 | * For example, bind() and connect() won't reset sk->sk_err, |
| 163 | * sk->sk_shutdown and sk->sk_flags to valid values... |
| 164 | * Jean II |
| 165 | */ |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb) |
| 170 | * |
| 171 | * Connections has been confirmed by the remote device |
| 172 | * |
| 173 | */ |
| 174 | static void irda_connect_confirm(void *instance, void *sap, |
| 175 | struct qos_info *qos, |
| 176 | __u32 max_sdu_size, __u8 max_header_size, |
| 177 | struct sk_buff *skb) |
| 178 | { |
| 179 | struct irda_sock *self; |
| 180 | struct sock *sk; |
| 181 | |
| 182 | self = instance; |
| 183 | |
| 184 | IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self); |
| 185 | |
| 186 | sk = instance; |
| 187 | if (sk == NULL) { |
| 188 | dev_kfree_skb(skb); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | dev_kfree_skb(skb); |
| 193 | // Should be ??? skb_queue_tail(&sk->sk_receive_queue, skb); |
| 194 | |
| 195 | /* How much header space do we need to reserve */ |
| 196 | self->max_header_size = max_header_size; |
| 197 | |
| 198 | /* IrTTP max SDU size in transmit direction */ |
| 199 | self->max_sdu_size_tx = max_sdu_size; |
| 200 | |
| 201 | /* Find out what the largest chunk of data that we can transmit is */ |
| 202 | switch (sk->sk_type) { |
| 203 | case SOCK_STREAM: |
| 204 | if (max_sdu_size != 0) { |
| 205 | IRDA_ERROR("%s: max_sdu_size must be 0\n", |
| 206 | __FUNCTION__); |
| 207 | return; |
| 208 | } |
| 209 | self->max_data_size = irttp_get_max_seg_size(self->tsap); |
| 210 | break; |
| 211 | case SOCK_SEQPACKET: |
| 212 | if (max_sdu_size == 0) { |
| 213 | IRDA_ERROR("%s: max_sdu_size cannot be 0\n", |
| 214 | __FUNCTION__); |
| 215 | return; |
| 216 | } |
| 217 | self->max_data_size = max_sdu_size; |
| 218 | break; |
| 219 | default: |
| 220 | self->max_data_size = irttp_get_max_seg_size(self->tsap); |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __FUNCTION__, |
| 224 | self->max_data_size); |
| 225 | |
| 226 | memcpy(&self->qos_tx, qos, sizeof(struct qos_info)); |
| 227 | |
| 228 | /* We are now connected! */ |
| 229 | sk->sk_state = TCP_ESTABLISHED; |
| 230 | sk->sk_state_change(sk); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata) |
| 235 | * |
| 236 | * Incoming connection |
| 237 | * |
| 238 | */ |
| 239 | static void irda_connect_indication(void *instance, void *sap, |
| 240 | struct qos_info *qos, __u32 max_sdu_size, |
| 241 | __u8 max_header_size, struct sk_buff *skb) |
| 242 | { |
| 243 | struct irda_sock *self; |
| 244 | struct sock *sk; |
| 245 | |
| 246 | self = instance; |
| 247 | |
| 248 | IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self); |
| 249 | |
| 250 | sk = instance; |
| 251 | if (sk == NULL) { |
| 252 | dev_kfree_skb(skb); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | /* How much header space do we need to reserve */ |
| 257 | self->max_header_size = max_header_size; |
| 258 | |
| 259 | /* IrTTP max SDU size in transmit direction */ |
| 260 | self->max_sdu_size_tx = max_sdu_size; |
| 261 | |
| 262 | /* Find out what the largest chunk of data that we can transmit is */ |
| 263 | switch (sk->sk_type) { |
| 264 | case SOCK_STREAM: |
| 265 | if (max_sdu_size != 0) { |
| 266 | IRDA_ERROR("%s: max_sdu_size must be 0\n", |
| 267 | __FUNCTION__); |
| 268 | kfree_skb(skb); |
| 269 | return; |
| 270 | } |
| 271 | self->max_data_size = irttp_get_max_seg_size(self->tsap); |
| 272 | break; |
| 273 | case SOCK_SEQPACKET: |
| 274 | if (max_sdu_size == 0) { |
| 275 | IRDA_ERROR("%s: max_sdu_size cannot be 0\n", |
| 276 | __FUNCTION__); |
| 277 | kfree_skb(skb); |
| 278 | return; |
| 279 | } |
| 280 | self->max_data_size = max_sdu_size; |
| 281 | break; |
| 282 | default: |
| 283 | self->max_data_size = irttp_get_max_seg_size(self->tsap); |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __FUNCTION__, |
| 287 | self->max_data_size); |
| 288 | |
| 289 | memcpy(&self->qos_tx, qos, sizeof(struct qos_info)); |
| 290 | |
| 291 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 292 | sk->sk_state_change(sk); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Function irda_connect_response (handle) |
| 297 | * |
| 298 | * Accept incoming connection |
| 299 | * |
| 300 | */ |
| 301 | static void irda_connect_response(struct irda_sock *self) |
| 302 | { |
| 303 | struct sk_buff *skb; |
| 304 | |
| 305 | IRDA_DEBUG(2, "%s()\n", __FUNCTION__); |
| 306 | |
Samuel Ortiz | 1b0fee7 | 2006-09-27 20:06:44 -0700 | [diff] [blame] | 307 | skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER, |
| 308 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | if (skb == NULL) { |
| 310 | IRDA_DEBUG(0, "%s() Unable to allocate sk_buff!\n", |
| 311 | __FUNCTION__); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | /* Reserve space for MUX_CONTROL and LAP header */ |
| 316 | skb_reserve(skb, IRDA_MAX_HEADER); |
| 317 | |
| 318 | irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb); |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Function irda_flow_indication (instance, sap, flow) |
| 323 | * |
| 324 | * Used by TinyTP to tell us if it can accept more data or not |
| 325 | * |
| 326 | */ |
| 327 | static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow) |
| 328 | { |
| 329 | struct irda_sock *self; |
| 330 | struct sock *sk; |
| 331 | |
| 332 | IRDA_DEBUG(2, "%s()\n", __FUNCTION__); |
| 333 | |
| 334 | self = instance; |
| 335 | sk = instance; |
Samuel Ortiz | c3ea9fa | 2007-04-20 22:10:13 -0700 | [diff] [blame] | 336 | BUG_ON(sk == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
| 338 | switch (flow) { |
| 339 | case FLOW_STOP: |
| 340 | IRDA_DEBUG(1, "%s(), IrTTP wants us to slow down\n", |
| 341 | __FUNCTION__); |
| 342 | self->tx_flow = flow; |
| 343 | break; |
| 344 | case FLOW_START: |
| 345 | self->tx_flow = flow; |
| 346 | IRDA_DEBUG(1, "%s(), IrTTP wants us to start again\n", |
| 347 | __FUNCTION__); |
| 348 | wake_up_interruptible(sk->sk_sleep); |
| 349 | break; |
| 350 | default: |
| 351 | IRDA_DEBUG(0, "%s(), Unknown flow command!\n", __FUNCTION__); |
| 352 | /* Unknown flow command, better stop */ |
| 353 | self->tx_flow = flow; |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Function irda_getvalue_confirm (obj_id, value, priv) |
| 360 | * |
| 361 | * Got answer from remote LM-IAS, just pass object to requester... |
| 362 | * |
| 363 | * Note : duplicate from above, but we need our own version that |
| 364 | * doesn't touch the dtsap_sel and save the full value structure... |
| 365 | */ |
| 366 | static void irda_getvalue_confirm(int result, __u16 obj_id, |
| 367 | struct ias_value *value, void *priv) |
| 368 | { |
| 369 | struct irda_sock *self; |
| 370 | |
| 371 | self = (struct irda_sock *) priv; |
| 372 | if (!self) { |
| 373 | IRDA_WARNING("%s: lost myself!\n", __FUNCTION__); |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self); |
| 378 | |
| 379 | /* We probably don't need to make any more queries */ |
| 380 | iriap_close(self->iriap); |
| 381 | self->iriap = NULL; |
| 382 | |
| 383 | /* Check if request succeeded */ |
| 384 | if (result != IAS_SUCCESS) { |
| 385 | IRDA_DEBUG(1, "%s(), IAS query failed! (%d)\n", __FUNCTION__, |
| 386 | result); |
| 387 | |
| 388 | self->errno = result; /* We really need it later */ |
| 389 | |
| 390 | /* Wake up any processes waiting for result */ |
| 391 | wake_up_interruptible(&self->query_wait); |
| 392 | |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | /* Pass the object to the caller (so the caller must delete it) */ |
| 397 | self->ias_result = value; |
| 398 | self->errno = 0; |
| 399 | |
| 400 | /* Wake up any processes waiting for result */ |
| 401 | wake_up_interruptible(&self->query_wait); |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Function irda_selective_discovery_indication (discovery) |
| 406 | * |
| 407 | * Got a selective discovery indication from IrLMP. |
| 408 | * |
| 409 | * IrLMP is telling us that this node is new and matching our hint bit |
| 410 | * filter. Wake up any process waiting for answer... |
| 411 | */ |
| 412 | static void irda_selective_discovery_indication(discinfo_t *discovery, |
| 413 | DISCOVERY_MODE mode, |
| 414 | void *priv) |
| 415 | { |
| 416 | struct irda_sock *self; |
| 417 | |
| 418 | IRDA_DEBUG(2, "%s()\n", __FUNCTION__); |
| 419 | |
| 420 | self = (struct irda_sock *) priv; |
| 421 | if (!self) { |
| 422 | IRDA_WARNING("%s: lost myself!\n", __FUNCTION__); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | /* Pass parameter to the caller */ |
| 427 | self->cachedaddr = discovery->daddr; |
| 428 | |
| 429 | /* Wake up process if its waiting for device to be discovered */ |
| 430 | wake_up_interruptible(&self->query_wait); |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * Function irda_discovery_timeout (priv) |
| 435 | * |
| 436 | * Timeout in the selective discovery process |
| 437 | * |
| 438 | * We were waiting for a node to be discovered, but nothing has come up |
| 439 | * so far. Wake up the user and tell him that we failed... |
| 440 | */ |
| 441 | static void irda_discovery_timeout(u_long priv) |
| 442 | { |
| 443 | struct irda_sock *self; |
| 444 | |
| 445 | IRDA_DEBUG(2, "%s()\n", __FUNCTION__); |
| 446 | |
| 447 | self = (struct irda_sock *) priv; |
Samuel Ortiz | c3ea9fa | 2007-04-20 22:10:13 -0700 | [diff] [blame] | 448 | BUG_ON(self == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | /* Nothing for the caller */ |
| 451 | self->cachelog = NULL; |
| 452 | self->cachedaddr = 0; |
| 453 | self->errno = -ETIME; |
| 454 | |
| 455 | /* Wake up process if its still waiting... */ |
| 456 | wake_up_interruptible(&self->query_wait); |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Function irda_open_tsap (self) |
| 461 | * |
| 462 | * Open local Transport Service Access Point (TSAP) |
| 463 | * |
| 464 | */ |
| 465 | static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name) |
| 466 | { |
| 467 | notify_t notify; |
| 468 | |
| 469 | if (self->tsap) { |
| 470 | IRDA_WARNING("%s: busy!\n", __FUNCTION__); |
| 471 | return -EBUSY; |
| 472 | } |
| 473 | |
| 474 | /* Initialize callbacks to be used by the IrDA stack */ |
| 475 | irda_notify_init(¬ify); |
| 476 | notify.connect_confirm = irda_connect_confirm; |
| 477 | notify.connect_indication = irda_connect_indication; |
| 478 | notify.disconnect_indication = irda_disconnect_indication; |
| 479 | notify.data_indication = irda_data_indication; |
| 480 | notify.udata_indication = irda_data_indication; |
| 481 | notify.flow_indication = irda_flow_indication; |
| 482 | notify.instance = self; |
| 483 | strncpy(notify.name, name, NOTIFY_MAX_NAME); |
| 484 | |
| 485 | self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT, |
| 486 | ¬ify); |
| 487 | if (self->tsap == NULL) { |
| 488 | IRDA_DEBUG(0, "%s(), Unable to allocate TSAP!\n", |
| 489 | __FUNCTION__); |
| 490 | return -ENOMEM; |
| 491 | } |
| 492 | /* Remember which TSAP selector we actually got */ |
| 493 | self->stsap_sel = self->tsap->stsap_sel; |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Function irda_open_lsap (self) |
| 500 | * |
| 501 | * Open local Link Service Access Point (LSAP). Used for opening Ultra |
| 502 | * sockets |
| 503 | */ |
| 504 | #ifdef CONFIG_IRDA_ULTRA |
| 505 | static int irda_open_lsap(struct irda_sock *self, int pid) |
| 506 | { |
| 507 | notify_t notify; |
| 508 | |
| 509 | if (self->lsap) { |
| 510 | IRDA_WARNING("%s(), busy!\n", __FUNCTION__); |
| 511 | return -EBUSY; |
| 512 | } |
| 513 | |
| 514 | /* Initialize callbacks to be used by the IrDA stack */ |
| 515 | irda_notify_init(¬ify); |
| 516 | notify.udata_indication = irda_data_indication; |
| 517 | notify.instance = self; |
| 518 | strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME); |
| 519 | |
| 520 | self->lsap = irlmp_open_lsap(LSAP_CONNLESS, ¬ify, pid); |
| 521 | if (self->lsap == NULL) { |
| 522 | IRDA_DEBUG( 0, "%s(), Unable to allocate LSAP!\n", __FUNCTION__); |
| 523 | return -ENOMEM; |
| 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | #endif /* CONFIG_IRDA_ULTRA */ |
| 529 | |
| 530 | /* |
| 531 | * Function irda_find_lsap_sel (self, name) |
| 532 | * |
| 533 | * Try to lookup LSAP selector in remote LM-IAS |
| 534 | * |
| 535 | * Basically, we start a IAP query, and then go to sleep. When the query |
| 536 | * return, irda_getvalue_confirm will wake us up, and we can examine the |
| 537 | * result of the query... |
| 538 | * Note that in some case, the query fail even before we go to sleep, |
| 539 | * creating some races... |
| 540 | */ |
| 541 | static int irda_find_lsap_sel(struct irda_sock *self, char *name) |
| 542 | { |
| 543 | IRDA_DEBUG(2, "%s(%p, %s)\n", __FUNCTION__, self, name); |
| 544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | if (self->iriap) { |
| 546 | IRDA_WARNING("%s(): busy with a previous query\n", |
| 547 | __FUNCTION__); |
| 548 | return -EBUSY; |
| 549 | } |
| 550 | |
| 551 | self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self, |
| 552 | irda_getvalue_confirm); |
| 553 | if(self->iriap == NULL) |
| 554 | return -ENOMEM; |
| 555 | |
| 556 | /* Treat unexpected wakeup as disconnect */ |
| 557 | self->errno = -EHOSTUNREACH; |
| 558 | |
| 559 | /* Query remote LM-IAS */ |
| 560 | iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr, |
| 561 | name, "IrDA:TinyTP:LsapSel"); |
| 562 | |
| 563 | /* Wait for answer, if not yet finished (or failed) */ |
| 564 | if (wait_event_interruptible(self->query_wait, (self->iriap==NULL))) |
| 565 | /* Treat signals as disconnect */ |
| 566 | return -EHOSTUNREACH; |
| 567 | |
| 568 | /* Check what happened */ |
| 569 | if (self->errno) |
| 570 | { |
| 571 | /* Requested object/attribute doesn't exist */ |
| 572 | if((self->errno == IAS_CLASS_UNKNOWN) || |
| 573 | (self->errno == IAS_ATTRIB_UNKNOWN)) |
| 574 | return (-EADDRNOTAVAIL); |
| 575 | else |
| 576 | return (-EHOSTUNREACH); |
| 577 | } |
| 578 | |
| 579 | /* Get the remote TSAP selector */ |
| 580 | switch (self->ias_result->type) { |
| 581 | case IAS_INTEGER: |
| 582 | IRDA_DEBUG(4, "%s() int=%d\n", |
| 583 | __FUNCTION__, self->ias_result->t.integer); |
| 584 | |
| 585 | if (self->ias_result->t.integer != -1) |
| 586 | self->dtsap_sel = self->ias_result->t.integer; |
| 587 | else |
| 588 | self->dtsap_sel = 0; |
| 589 | break; |
| 590 | default: |
| 591 | self->dtsap_sel = 0; |
| 592 | IRDA_DEBUG(0, "%s(), bad type!\n", __FUNCTION__); |
| 593 | break; |
| 594 | } |
| 595 | if (self->ias_result) |
| 596 | irias_delete_value(self->ias_result); |
| 597 | |
| 598 | if (self->dtsap_sel) |
| 599 | return 0; |
| 600 | |
| 601 | return -EADDRNOTAVAIL; |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Function irda_discover_daddr_and_lsap_sel (self, name) |
| 606 | * |
| 607 | * This try to find a device with the requested service. |
| 608 | * |
| 609 | * It basically look into the discovery log. For each address in the list, |
| 610 | * it queries the LM-IAS of the device to find if this device offer |
| 611 | * the requested service. |
| 612 | * If there is more than one node supporting the service, we complain |
| 613 | * to the user (it should move devices around). |
| 614 | * The, we set both the destination address and the lsap selector to point |
| 615 | * on the service on the unique device we have found. |
| 616 | * |
| 617 | * Note : this function fails if there is more than one device in range, |
| 618 | * because IrLMP doesn't disconnect the LAP when the last LSAP is closed. |
| 619 | * Moreover, we would need to wait the LAP disconnection... |
| 620 | */ |
| 621 | static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name) |
| 622 | { |
| 623 | discinfo_t *discoveries; /* Copy of the discovery log */ |
| 624 | int number; /* Number of nodes in the log */ |
| 625 | int i; |
| 626 | int err = -ENETUNREACH; |
| 627 | __u32 daddr = DEV_ADDR_ANY; /* Address we found the service on */ |
| 628 | __u8 dtsap_sel = 0x0; /* TSAP associated with it */ |
| 629 | |
| 630 | IRDA_DEBUG(2, "%s(), name=%s\n", __FUNCTION__, name); |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | /* Ask lmp for the current discovery log |
| 633 | * Note : we have to use irlmp_get_discoveries(), as opposed |
| 634 | * to play with the cachelog directly, because while we are |
| 635 | * making our ias query, le log might change... */ |
| 636 | discoveries = irlmp_get_discoveries(&number, self->mask.word, |
| 637 | self->nslots); |
| 638 | /* Check if the we got some results */ |
| 639 | if (discoveries == NULL) |
| 640 | return -ENETUNREACH; /* No nodes discovered */ |
| 641 | |
| 642 | /* |
| 643 | * Now, check all discovered devices (if any), and connect |
| 644 | * client only about the services that the client is |
| 645 | * interested in... |
| 646 | */ |
| 647 | for(i = 0; i < number; i++) { |
| 648 | /* Try the address in the log */ |
| 649 | self->daddr = discoveries[i].daddr; |
| 650 | self->saddr = 0x0; |
| 651 | IRDA_DEBUG(1, "%s(), trying daddr = %08x\n", |
| 652 | __FUNCTION__, self->daddr); |
| 653 | |
| 654 | /* Query remote LM-IAS for this service */ |
| 655 | err = irda_find_lsap_sel(self, name); |
| 656 | switch (err) { |
| 657 | case 0: |
| 658 | /* We found the requested service */ |
| 659 | if(daddr != DEV_ADDR_ANY) { |
| 660 | IRDA_DEBUG(1, "%s(), discovered service ''%s'' in two different devices !!!\n", |
| 661 | __FUNCTION__, name); |
| 662 | self->daddr = DEV_ADDR_ANY; |
| 663 | kfree(discoveries); |
| 664 | return(-ENOTUNIQ); |
| 665 | } |
| 666 | /* First time we found that one, save it ! */ |
| 667 | daddr = self->daddr; |
| 668 | dtsap_sel = self->dtsap_sel; |
| 669 | break; |
| 670 | case -EADDRNOTAVAIL: |
| 671 | /* Requested service simply doesn't exist on this node */ |
| 672 | break; |
| 673 | default: |
| 674 | /* Something bad did happen :-( */ |
| 675 | IRDA_DEBUG(0, "%s(), unexpected IAS query failure\n", __FUNCTION__); |
| 676 | self->daddr = DEV_ADDR_ANY; |
| 677 | kfree(discoveries); |
| 678 | return(-EHOSTUNREACH); |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | /* Cleanup our copy of the discovery log */ |
| 683 | kfree(discoveries); |
| 684 | |
| 685 | /* Check out what we found */ |
| 686 | if(daddr == DEV_ADDR_ANY) { |
| 687 | IRDA_DEBUG(1, "%s(), cannot discover service ''%s'' in any device !!!\n", |
| 688 | __FUNCTION__, name); |
| 689 | self->daddr = DEV_ADDR_ANY; |
| 690 | return(-EADDRNOTAVAIL); |
| 691 | } |
| 692 | |
| 693 | /* Revert back to discovered device & service */ |
| 694 | self->daddr = daddr; |
| 695 | self->saddr = 0x0; |
| 696 | self->dtsap_sel = dtsap_sel; |
| 697 | |
| 698 | IRDA_DEBUG(1, "%s(), discovered requested service ''%s'' at address %08x\n", |
| 699 | __FUNCTION__, name, self->daddr); |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Function irda_getname (sock, uaddr, uaddr_len, peer) |
| 706 | * |
| 707 | * Return the our own, or peers socket address (sockaddr_irda) |
| 708 | * |
| 709 | */ |
| 710 | static int irda_getname(struct socket *sock, struct sockaddr *uaddr, |
| 711 | int *uaddr_len, int peer) |
| 712 | { |
| 713 | struct sockaddr_irda saddr; |
| 714 | struct sock *sk = sock->sk; |
| 715 | struct irda_sock *self = irda_sk(sk); |
| 716 | |
| 717 | if (peer) { |
| 718 | if (sk->sk_state != TCP_ESTABLISHED) |
| 719 | return -ENOTCONN; |
| 720 | |
| 721 | saddr.sir_family = AF_IRDA; |
| 722 | saddr.sir_lsap_sel = self->dtsap_sel; |
| 723 | saddr.sir_addr = self->daddr; |
| 724 | } else { |
| 725 | saddr.sir_family = AF_IRDA; |
| 726 | saddr.sir_lsap_sel = self->stsap_sel; |
| 727 | saddr.sir_addr = self->saddr; |
| 728 | } |
| 729 | |
| 730 | IRDA_DEBUG(1, "%s(), tsap_sel = %#x\n", __FUNCTION__, saddr.sir_lsap_sel); |
| 731 | IRDA_DEBUG(1, "%s(), addr = %08x\n", __FUNCTION__, saddr.sir_addr); |
| 732 | |
| 733 | /* uaddr_len come to us uninitialised */ |
| 734 | *uaddr_len = sizeof (struct sockaddr_irda); |
| 735 | memcpy(uaddr, &saddr, *uaddr_len); |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Function irda_listen (sock, backlog) |
| 742 | * |
| 743 | * Just move to the listen state |
| 744 | * |
| 745 | */ |
| 746 | static int irda_listen(struct socket *sock, int backlog) |
| 747 | { |
| 748 | struct sock *sk = sock->sk; |
| 749 | |
| 750 | IRDA_DEBUG(2, "%s()\n", __FUNCTION__); |
| 751 | |
| 752 | if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) && |
| 753 | (sk->sk_type != SOCK_DGRAM)) |
| 754 | return -EOPNOTSUPP; |
| 755 | |
| 756 | if (sk->sk_state != TCP_LISTEN) { |
| 757 | sk->sk_max_ack_backlog = backlog; |
| 758 | sk->sk_state = TCP_LISTEN; |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | return -EOPNOTSUPP; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Function irda_bind (sock, uaddr, addr_len) |
| 768 | * |
| 769 | * Used by servers to register their well known TSAP |
| 770 | * |
| 771 | */ |
| 772 | static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 773 | { |
| 774 | struct sock *sk = sock->sk; |
| 775 | struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr; |
| 776 | struct irda_sock *self = irda_sk(sk); |
| 777 | int err; |
| 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self); |
| 780 | |
| 781 | if (addr_len != sizeof(struct sockaddr_irda)) |
| 782 | return -EINVAL; |
| 783 | |
| 784 | #ifdef CONFIG_IRDA_ULTRA |
| 785 | /* Special care for Ultra sockets */ |
| 786 | if ((sk->sk_type == SOCK_DGRAM) && |
| 787 | (sk->sk_protocol == IRDAPROTO_ULTRA)) { |
| 788 | self->pid = addr->sir_lsap_sel; |
| 789 | if (self->pid & 0x80) { |
| 790 | IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __FUNCTION__); |
| 791 | return -EOPNOTSUPP; |
| 792 | } |
| 793 | err = irda_open_lsap(self, self->pid); |
| 794 | if (err < 0) |
| 795 | return err; |
| 796 | |
| 797 | /* Pretend we are connected */ |
| 798 | sock->state = SS_CONNECTED; |
| 799 | sk->sk_state = TCP_ESTABLISHED; |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | #endif /* CONFIG_IRDA_ULTRA */ |
| 804 | |
| 805 | err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name); |
| 806 | if (err < 0) |
| 807 | return err; |
| 808 | |
| 809 | /* Register with LM-IAS */ |
| 810 | self->ias_obj = irias_new_object(addr->sir_name, jiffies); |
| 811 | irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel", |
| 812 | self->stsap_sel, IAS_KERNEL_ATTR); |
| 813 | irias_insert_object(self->ias_obj); |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * Function irda_accept (sock, newsock, flags) |
| 820 | * |
| 821 | * Wait for incoming connection |
| 822 | * |
| 823 | */ |
| 824 | static int irda_accept(struct socket *sock, struct socket *newsock, int flags) |
| 825 | { |
| 826 | struct sock *sk = sock->sk; |
| 827 | struct irda_sock *new, *self = irda_sk(sk); |
| 828 | struct sock *newsk; |
| 829 | struct sk_buff *skb; |
| 830 | int err; |
| 831 | |
| 832 | IRDA_DEBUG(2, "%s()\n", __FUNCTION__); |
| 833 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 834 | err = irda_create(sk->sk_net, newsock, sk->sk_protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | if (err) |
| 836 | return err; |
| 837 | |
| 838 | if (sock->state != SS_UNCONNECTED) |
| 839 | return -EINVAL; |
| 840 | |
| 841 | if ((sk = sock->sk) == NULL) |
| 842 | return -EINVAL; |
| 843 | |
| 844 | if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) && |
| 845 | (sk->sk_type != SOCK_DGRAM)) |
| 846 | return -EOPNOTSUPP; |
| 847 | |
| 848 | if (sk->sk_state != TCP_LISTEN) |
| 849 | return -EINVAL; |
| 850 | |
| 851 | /* |
| 852 | * The read queue this time is holding sockets ready to use |
| 853 | * hooked into the SABM we saved |
| 854 | */ |
| 855 | |
| 856 | /* |
| 857 | * We can perform the accept only if there is incoming data |
| 858 | * on the listening socket. |
| 859 | * So, we will block the caller until we receive any data. |
| 860 | * If the caller was waiting on select() or poll() before |
| 861 | * calling us, the data is waiting for us ;-) |
| 862 | * Jean II |
| 863 | */ |
Samuel Ortiz | d7f48d1 | 2007-04-20 22:09:33 -0700 | [diff] [blame] | 864 | while (1) { |
| 865 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 866 | if (skb) |
| 867 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
| 869 | /* Non blocking operation */ |
| 870 | if (flags & O_NONBLOCK) |
| 871 | return -EWOULDBLOCK; |
| 872 | |
Samuel Ortiz | d7f48d1 | 2007-04-20 22:09:33 -0700 | [diff] [blame] | 873 | err = wait_event_interruptible(*(sk->sk_sleep), |
| 874 | skb_peek(&sk->sk_receive_queue)); |
| 875 | if (err) |
| 876 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | newsk = newsock->sk; |
Samuel Ortiz | c3ea9fa | 2007-04-20 22:10:13 -0700 | [diff] [blame] | 880 | if (newsk == NULL) |
| 881 | return -EIO; |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | newsk->sk_state = TCP_ESTABLISHED; |
| 884 | |
| 885 | new = irda_sk(newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
| 887 | /* Now attach up the new socket */ |
| 888 | new->tsap = irttp_dup(self->tsap, new); |
| 889 | if (!new->tsap) { |
| 890 | IRDA_DEBUG(0, "%s(), dup failed!\n", __FUNCTION__); |
| 891 | kfree_skb(skb); |
| 892 | return -1; |
| 893 | } |
| 894 | |
| 895 | new->stsap_sel = new->tsap->stsap_sel; |
| 896 | new->dtsap_sel = new->tsap->dtsap_sel; |
| 897 | new->saddr = irttp_get_saddr(new->tsap); |
| 898 | new->daddr = irttp_get_daddr(new->tsap); |
| 899 | |
| 900 | new->max_sdu_size_tx = self->max_sdu_size_tx; |
| 901 | new->max_sdu_size_rx = self->max_sdu_size_rx; |
| 902 | new->max_data_size = self->max_data_size; |
| 903 | new->max_header_size = self->max_header_size; |
| 904 | |
| 905 | memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info)); |
| 906 | |
| 907 | /* Clean up the original one to keep it in listen state */ |
| 908 | irttp_listen(self->tsap); |
| 909 | |
| 910 | /* Wow ! What is that ? Jean II */ |
| 911 | skb->sk = NULL; |
| 912 | skb->destructor = NULL; |
| 913 | kfree_skb(skb); |
| 914 | sk->sk_ack_backlog--; |
| 915 | |
| 916 | newsock->state = SS_CONNECTED; |
| 917 | |
| 918 | irda_connect_response(new); |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Function irda_connect (sock, uaddr, addr_len, flags) |
| 925 | * |
| 926 | * Connect to a IrDA device |
| 927 | * |
| 928 | * The main difference with a "standard" connect is that with IrDA we need |
| 929 | * to resolve the service name into a TSAP selector (in TCP, port number |
| 930 | * doesn't have to be resolved). |
| 931 | * Because of this service name resoltion, we can offer "auto-connect", |
| 932 | * where we connect to a service without specifying a destination address. |
| 933 | * |
| 934 | * Note : by consulting "errno", the user space caller may learn the cause |
| 935 | * of the failure. Most of them are visible in the function, others may come |
| 936 | * from subroutines called and are listed here : |
| 937 | * o EBUSY : already processing a connect |
| 938 | * o EHOSTUNREACH : bad addr->sir_addr argument |
| 939 | * o EADDRNOTAVAIL : bad addr->sir_name argument |
| 940 | * o ENOTUNIQ : more than one node has addr->sir_name (auto-connect) |
| 941 | * o ENETUNREACH : no node found on the network (auto-connect) |
| 942 | */ |
| 943 | static int irda_connect(struct socket *sock, struct sockaddr *uaddr, |
| 944 | int addr_len, int flags) |
| 945 | { |
| 946 | struct sock *sk = sock->sk; |
| 947 | struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr; |
| 948 | struct irda_sock *self = irda_sk(sk); |
| 949 | int err; |
| 950 | |
| 951 | IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self); |
| 952 | |
| 953 | /* Don't allow connect for Ultra sockets */ |
| 954 | if ((sk->sk_type == SOCK_DGRAM) && (sk->sk_protocol == IRDAPROTO_ULTRA)) |
| 955 | return -ESOCKTNOSUPPORT; |
| 956 | |
| 957 | if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) { |
| 958 | sock->state = SS_CONNECTED; |
| 959 | return 0; /* Connect completed during a ERESTARTSYS event */ |
| 960 | } |
| 961 | |
| 962 | if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) { |
| 963 | sock->state = SS_UNCONNECTED; |
| 964 | return -ECONNREFUSED; |
| 965 | } |
| 966 | |
| 967 | if (sk->sk_state == TCP_ESTABLISHED) |
| 968 | return -EISCONN; /* No reconnect on a seqpacket socket */ |
| 969 | |
| 970 | sk->sk_state = TCP_CLOSE; |
| 971 | sock->state = SS_UNCONNECTED; |
| 972 | |
| 973 | if (addr_len != sizeof(struct sockaddr_irda)) |
| 974 | return -EINVAL; |
| 975 | |
| 976 | /* Check if user supplied any destination device address */ |
| 977 | if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) { |
| 978 | /* Try to find one suitable */ |
| 979 | err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name); |
| 980 | if (err) { |
| 981 | IRDA_DEBUG(0, "%s(), auto-connect failed!\n", __FUNCTION__); |
| 982 | return err; |
| 983 | } |
| 984 | } else { |
| 985 | /* Use the one provided by the user */ |
| 986 | self->daddr = addr->sir_addr; |
| 987 | IRDA_DEBUG(1, "%s(), daddr = %08x\n", __FUNCTION__, self->daddr); |
| 988 | |
| 989 | /* If we don't have a valid service name, we assume the |
| 990 | * user want to connect on a specific LSAP. Prevent |
| 991 | * the use of invalid LSAPs (IrLMP 1.1 p10). Jean II */ |
| 992 | if((addr->sir_name[0] != '\0') || |
| 993 | (addr->sir_lsap_sel >= 0x70)) { |
| 994 | /* Query remote LM-IAS using service name */ |
| 995 | err = irda_find_lsap_sel(self, addr->sir_name); |
| 996 | if (err) { |
| 997 | IRDA_DEBUG(0, "%s(), connect failed!\n", __FUNCTION__); |
| 998 | return err; |
| 999 | } |
| 1000 | } else { |
| 1001 | /* Directly connect to the remote LSAP |
| 1002 | * specified by the sir_lsap field. |
| 1003 | * Please use with caution, in IrDA LSAPs are |
| 1004 | * dynamic and there is no "well-known" LSAP. */ |
| 1005 | self->dtsap_sel = addr->sir_lsap_sel; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | /* Check if we have opened a local TSAP */ |
| 1010 | if (!self->tsap) |
| 1011 | irda_open_tsap(self, LSAP_ANY, addr->sir_name); |
| 1012 | |
| 1013 | /* Move to connecting socket, start sending Connect Requests */ |
| 1014 | sock->state = SS_CONNECTING; |
| 1015 | sk->sk_state = TCP_SYN_SENT; |
| 1016 | |
| 1017 | /* Connect to remote device */ |
| 1018 | err = irttp_connect_request(self->tsap, self->dtsap_sel, |
| 1019 | self->saddr, self->daddr, NULL, |
| 1020 | self->max_sdu_size_rx, NULL); |
| 1021 | if (err) { |
| 1022 | IRDA_DEBUG(0, "%s(), connect failed!\n", __FUNCTION__); |
| 1023 | return err; |
| 1024 | } |
| 1025 | |
| 1026 | /* Now the loop */ |
| 1027 | if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) |
| 1028 | return -EINPROGRESS; |
| 1029 | |
| 1030 | if (wait_event_interruptible(*(sk->sk_sleep), |
| 1031 | (sk->sk_state != TCP_SYN_SENT))) |
| 1032 | return -ERESTARTSYS; |
| 1033 | |
| 1034 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1035 | sock->state = SS_UNCONNECTED; |
Olaf Kirch | 6e66aa1 | 2007-04-20 22:08:15 -0700 | [diff] [blame] | 1036 | err = sock_error(sk); |
| 1037 | return err? err : -ECONNRESET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | sock->state = SS_CONNECTED; |
| 1041 | |
| 1042 | /* At this point, IrLMP has assigned our source address */ |
| 1043 | self->saddr = irttp_get_saddr(self->tsap); |
| 1044 | |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | static struct proto irda_proto = { |
| 1049 | .name = "IRDA", |
| 1050 | .owner = THIS_MODULE, |
| 1051 | .obj_size = sizeof(struct irda_sock), |
| 1052 | }; |
| 1053 | |
| 1054 | /* |
| 1055 | * Function irda_create (sock, protocol) |
| 1056 | * |
| 1057 | * Create IrDA socket |
| 1058 | * |
| 1059 | */ |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 1060 | static int irda_create(struct net *net, struct socket *sock, int protocol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | { |
| 1062 | struct sock *sk; |
| 1063 | struct irda_sock *self; |
| 1064 | |
| 1065 | IRDA_DEBUG(2, "%s()\n", __FUNCTION__); |
| 1066 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 1067 | if (net != &init_net) |
| 1068 | return -EAFNOSUPPORT; |
| 1069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | /* Check for valid socket type */ |
| 1071 | switch (sock->type) { |
| 1072 | case SOCK_STREAM: /* For TTP connections with SAR disabled */ |
| 1073 | case SOCK_SEQPACKET: /* For TTP connections with SAR enabled */ |
| 1074 | case SOCK_DGRAM: /* For TTP Unitdata or LMP Ultra transfers */ |
| 1075 | break; |
| 1076 | default: |
| 1077 | return -ESOCKTNOSUPPORT; |
| 1078 | } |
| 1079 | |
| 1080 | /* Allocate networking socket */ |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 1081 | sk = sk_alloc(net, PF_IRDA, GFP_ATOMIC, &irda_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | if (sk == NULL) |
| 1083 | return -ENOMEM; |
| 1084 | |
| 1085 | self = irda_sk(sk); |
| 1086 | IRDA_DEBUG(2, "%s() : self is %p\n", __FUNCTION__, self); |
| 1087 | |
| 1088 | init_waitqueue_head(&self->query_wait); |
| 1089 | |
| 1090 | /* Initialise networking socket struct */ |
| 1091 | sock_init_data(sock, sk); /* Note : set sk->sk_refcnt to 1 */ |
| 1092 | sk->sk_family = PF_IRDA; |
| 1093 | sk->sk_protocol = protocol; |
| 1094 | |
| 1095 | switch (sock->type) { |
| 1096 | case SOCK_STREAM: |
| 1097 | sock->ops = &irda_stream_ops; |
| 1098 | self->max_sdu_size_rx = TTP_SAR_DISABLE; |
| 1099 | break; |
| 1100 | case SOCK_SEQPACKET: |
| 1101 | sock->ops = &irda_seqpacket_ops; |
| 1102 | self->max_sdu_size_rx = TTP_SAR_UNBOUND; |
| 1103 | break; |
| 1104 | case SOCK_DGRAM: |
| 1105 | switch (protocol) { |
| 1106 | #ifdef CONFIG_IRDA_ULTRA |
| 1107 | case IRDAPROTO_ULTRA: |
| 1108 | sock->ops = &irda_ultra_ops; |
| 1109 | /* Initialise now, because we may send on unbound |
| 1110 | * sockets. Jean II */ |
| 1111 | self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER; |
| 1112 | self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER; |
| 1113 | break; |
| 1114 | #endif /* CONFIG_IRDA_ULTRA */ |
| 1115 | case IRDAPROTO_UNITDATA: |
| 1116 | sock->ops = &irda_dgram_ops; |
| 1117 | /* We let Unitdata conn. be like seqpack conn. */ |
| 1118 | self->max_sdu_size_rx = TTP_SAR_UNBOUND; |
| 1119 | break; |
| 1120 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | return -ESOCKTNOSUPPORT; |
| 1122 | } |
| 1123 | break; |
| 1124 | default: |
| 1125 | return -ESOCKTNOSUPPORT; |
| 1126 | } |
| 1127 | |
| 1128 | /* Register as a client with IrLMP */ |
| 1129 | self->ckey = irlmp_register_client(0, NULL, NULL, NULL); |
| 1130 | self->mask.word = 0xffff; |
| 1131 | self->rx_flow = self->tx_flow = FLOW_START; |
| 1132 | self->nslots = DISCOVERY_DEFAULT_SLOTS; |
| 1133 | self->daddr = DEV_ADDR_ANY; /* Until we get connected */ |
| 1134 | self->saddr = 0x0; /* so IrLMP assign us any link */ |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * Function irda_destroy_socket (self) |
| 1140 | * |
| 1141 | * Destroy socket |
| 1142 | * |
| 1143 | */ |
| 1144 | static void irda_destroy_socket(struct irda_sock *self) |
| 1145 | { |
| 1146 | IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self); |
| 1147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | /* Unregister with IrLMP */ |
| 1149 | irlmp_unregister_client(self->ckey); |
| 1150 | irlmp_unregister_service(self->skey); |
| 1151 | |
| 1152 | /* Unregister with LM-IAS */ |
| 1153 | if (self->ias_obj) { |
| 1154 | irias_delete_object(self->ias_obj); |
| 1155 | self->ias_obj = NULL; |
| 1156 | } |
| 1157 | |
| 1158 | if (self->iriap) { |
| 1159 | iriap_close(self->iriap); |
| 1160 | self->iriap = NULL; |
| 1161 | } |
| 1162 | |
| 1163 | if (self->tsap) { |
| 1164 | irttp_disconnect_request(self->tsap, NULL, P_NORMAL); |
| 1165 | irttp_close_tsap(self->tsap); |
| 1166 | self->tsap = NULL; |
| 1167 | } |
| 1168 | #ifdef CONFIG_IRDA_ULTRA |
| 1169 | if (self->lsap) { |
| 1170 | irlmp_close_lsap(self->lsap); |
| 1171 | self->lsap = NULL; |
| 1172 | } |
| 1173 | #endif /* CONFIG_IRDA_ULTRA */ |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | * Function irda_release (sock) |
| 1178 | */ |
| 1179 | static int irda_release(struct socket *sock) |
| 1180 | { |
| 1181 | struct sock *sk = sock->sk; |
| 1182 | |
| 1183 | IRDA_DEBUG(2, "%s()\n", __FUNCTION__); |
| 1184 | |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 1185 | if (sk == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | return 0; |
| 1187 | |
Samuel Ortiz | da349f1 | 2006-09-27 20:05:38 -0700 | [diff] [blame] | 1188 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | sk->sk_state = TCP_CLOSE; |
| 1190 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 1191 | sk->sk_state_change(sk); |
| 1192 | |
| 1193 | /* Destroy IrDA socket */ |
| 1194 | irda_destroy_socket(irda_sk(sk)); |
| 1195 | |
| 1196 | sock_orphan(sk); |
| 1197 | sock->sk = NULL; |
Samuel Ortiz | da349f1 | 2006-09-27 20:05:38 -0700 | [diff] [blame] | 1198 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
| 1200 | /* Purge queues (see sock_init_data()) */ |
| 1201 | skb_queue_purge(&sk->sk_receive_queue); |
| 1202 | |
| 1203 | /* Destroy networking socket if we are the last reference on it, |
| 1204 | * i.e. if(sk->sk_refcnt == 0) -> sk_free(sk) */ |
| 1205 | sock_put(sk); |
| 1206 | |
| 1207 | /* Notes on socket locking and deallocation... - Jean II |
| 1208 | * In theory we should put pairs of sock_hold() / sock_put() to |
| 1209 | * prevent the socket to be destroyed whenever there is an |
| 1210 | * outstanding request or outstanding incoming packet or event. |
| 1211 | * |
| 1212 | * 1) This may include IAS request, both in connect and getsockopt. |
| 1213 | * Unfortunately, the situation is a bit more messy than it looks, |
| 1214 | * because we close iriap and kfree(self) above. |
| 1215 | * |
| 1216 | * 2) This may include selective discovery in getsockopt. |
| 1217 | * Same stuff as above, irlmp registration and self are gone. |
| 1218 | * |
| 1219 | * Probably 1 and 2 may not matter, because it's all triggered |
| 1220 | * by a process and the socket layer already prevent the |
| 1221 | * socket to go away while a process is holding it, through |
| 1222 | * sockfd_put() and fput()... |
| 1223 | * |
| 1224 | * 3) This may include deferred TSAP closure. In particular, |
| 1225 | * we may receive a late irda_disconnect_indication() |
| 1226 | * Fortunately, (tsap_cb *)->close_pend should protect us |
| 1227 | * from that. |
| 1228 | * |
| 1229 | * I did some testing on SMP, and it looks solid. And the socket |
| 1230 | * memory leak is now gone... - Jean II |
| 1231 | */ |
| 1232 | |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 1233 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* |
| 1237 | * Function irda_sendmsg (iocb, sock, msg, len) |
| 1238 | * |
| 1239 | * Send message down to TinyTP. This function is used for both STREAM and |
| 1240 | * SEQPACK services. This is possible since it forces the client to |
| 1241 | * fragment the message if necessary |
| 1242 | */ |
| 1243 | static int irda_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 1244 | struct msghdr *msg, size_t len) |
| 1245 | { |
| 1246 | struct sock *sk = sock->sk; |
| 1247 | struct irda_sock *self; |
| 1248 | struct sk_buff *skb; |
Samuel Ortiz | bcb5e0e | 2007-08-28 15:57:12 -0700 | [diff] [blame] | 1249 | int err = -EPIPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | |
| 1251 | IRDA_DEBUG(4, "%s(), len=%zd\n", __FUNCTION__, len); |
| 1252 | |
| 1253 | /* Note : socket.c set MSG_EOR on SEQPACKET sockets */ |
Samuel Ortiz | bcb5e0e | 2007-08-28 15:57:12 -0700 | [diff] [blame] | 1254 | if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR | MSG_CMSG_COMPAT | |
| 1255 | MSG_NOSIGNAL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | return -EINVAL; |
| 1257 | |
Samuel Ortiz | bcb5e0e | 2007-08-28 15:57:12 -0700 | [diff] [blame] | 1258 | if (sk->sk_shutdown & SEND_SHUTDOWN) |
| 1259 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | |
| 1261 | if (sk->sk_state != TCP_ESTABLISHED) |
| 1262 | return -ENOTCONN; |
| 1263 | |
| 1264 | self = irda_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
| 1266 | /* Check if IrTTP is wants us to slow down */ |
| 1267 | |
| 1268 | if (wait_event_interruptible(*(sk->sk_sleep), |
| 1269 | (self->tx_flow != FLOW_STOP || sk->sk_state != TCP_ESTABLISHED))) |
| 1270 | return -ERESTARTSYS; |
| 1271 | |
| 1272 | /* Check if we are still connected */ |
| 1273 | if (sk->sk_state != TCP_ESTABLISHED) |
| 1274 | return -ENOTCONN; |
| 1275 | |
Alexey Dobriyan | 7f927fc | 2006-03-28 01:56:53 -0800 | [diff] [blame] | 1276 | /* Check that we don't send out too big frames */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | if (len > self->max_data_size) { |
| 1278 | IRDA_DEBUG(2, "%s(), Chopping frame from %zd to %d bytes!\n", |
| 1279 | __FUNCTION__, len, self->max_data_size); |
| 1280 | len = self->max_data_size; |
| 1281 | } |
| 1282 | |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 1283 | skb = sock_alloc_send_skb(sk, len + self->max_header_size + 16, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1285 | if (!skb) |
Samuel Ortiz | bcb5e0e | 2007-08-28 15:57:12 -0700 | [diff] [blame] | 1286 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
| 1288 | skb_reserve(skb, self->max_header_size + 16); |
Arnaldo Carvalho de Melo | eeeb037 | 2007-03-14 21:04:34 -0300 | [diff] [blame] | 1289 | skb_reset_transport_header(skb); |
| 1290 | skb_put(skb, len); |
| 1291 | err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | if (err) { |
| 1293 | kfree_skb(skb); |
Samuel Ortiz | bcb5e0e | 2007-08-28 15:57:12 -0700 | [diff] [blame] | 1294 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * Just send the message to TinyTP, and let it deal with possible |
| 1299 | * errors. No need to duplicate all that here |
| 1300 | */ |
| 1301 | err = irttp_data_request(self->tsap, skb); |
| 1302 | if (err) { |
| 1303 | IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err); |
Samuel Ortiz | bcb5e0e | 2007-08-28 15:57:12 -0700 | [diff] [blame] | 1304 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | } |
| 1306 | /* Tell client how much data we actually sent */ |
| 1307 | return len; |
Samuel Ortiz | bcb5e0e | 2007-08-28 15:57:12 -0700 | [diff] [blame] | 1308 | |
| 1309 | out_err: |
| 1310 | return sk_stream_error(sk, msg->msg_flags, err); |
| 1311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /* |
| 1315 | * Function irda_recvmsg_dgram (iocb, sock, msg, size, flags) |
| 1316 | * |
| 1317 | * Try to receive message and copy it to user. The frame is discarded |
| 1318 | * after being read, regardless of how much the user actually read |
| 1319 | */ |
| 1320 | static int irda_recvmsg_dgram(struct kiocb *iocb, struct socket *sock, |
| 1321 | struct msghdr *msg, size_t size, int flags) |
| 1322 | { |
| 1323 | struct sock *sk = sock->sk; |
| 1324 | struct irda_sock *self = irda_sk(sk); |
| 1325 | struct sk_buff *skb; |
| 1326 | size_t copied; |
| 1327 | int err; |
| 1328 | |
| 1329 | IRDA_DEBUG(4, "%s()\n", __FUNCTION__); |
| 1330 | |
Olaf Kirch | 6e66aa1 | 2007-04-20 22:08:15 -0700 | [diff] [blame] | 1331 | if ((err = sock_error(sk)) < 0) |
| 1332 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
| 1334 | skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, |
| 1335 | flags & MSG_DONTWAIT, &err); |
| 1336 | if (!skb) |
| 1337 | return err; |
| 1338 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 1339 | skb_reset_transport_header(skb); |
| 1340 | copied = skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
| 1342 | if (copied > size) { |
| 1343 | IRDA_DEBUG(2, "%s(), Received truncated frame (%zd < %zd)!\n", |
| 1344 | __FUNCTION__, copied, size); |
| 1345 | copied = size; |
| 1346 | msg->msg_flags |= MSG_TRUNC; |
| 1347 | } |
| 1348 | skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 1349 | |
| 1350 | skb_free_datagram(sk, skb); |
| 1351 | |
| 1352 | /* |
| 1353 | * Check if we have previously stopped IrTTP and we know |
| 1354 | * have more free space in our rx_queue. If so tell IrTTP |
| 1355 | * to start delivering frames again before our rx_queue gets |
| 1356 | * empty |
| 1357 | */ |
| 1358 | if (self->rx_flow == FLOW_STOP) { |
| 1359 | if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) { |
| 1360 | IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __FUNCTION__); |
| 1361 | self->rx_flow = FLOW_START; |
| 1362 | irttp_flow_request(self->tsap, FLOW_START); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | return copied; |
| 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | * Function irda_recvmsg_stream (iocb, sock, msg, size, flags) |
| 1371 | */ |
| 1372 | static int irda_recvmsg_stream(struct kiocb *iocb, struct socket *sock, |
| 1373 | struct msghdr *msg, size_t size, int flags) |
| 1374 | { |
| 1375 | struct sock *sk = sock->sk; |
| 1376 | struct irda_sock *self = irda_sk(sk); |
| 1377 | int noblock = flags & MSG_DONTWAIT; |
| 1378 | size_t copied = 0; |
Olaf Kirch | 6e66aa1 | 2007-04-20 22:08:15 -0700 | [diff] [blame] | 1379 | int target, err; |
Olaf Kirch | 305f2aa | 2007-04-20 22:05:27 -0700 | [diff] [blame] | 1380 | long timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
| 1382 | IRDA_DEBUG(3, "%s()\n", __FUNCTION__); |
| 1383 | |
Olaf Kirch | 6e66aa1 | 2007-04-20 22:08:15 -0700 | [diff] [blame] | 1384 | if ((err = sock_error(sk)) < 0) |
| 1385 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
| 1387 | if (sock->flags & __SO_ACCEPTCON) |
| 1388 | return(-EINVAL); |
| 1389 | |
| 1390 | if (flags & MSG_OOB) |
| 1391 | return -EOPNOTSUPP; |
| 1392 | |
Olaf Kirch | 305f2aa | 2007-04-20 22:05:27 -0700 | [diff] [blame] | 1393 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, size); |
| 1394 | timeo = sock_rcvtimeo(sk, noblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
| 1396 | msg->msg_namelen = 0; |
| 1397 | |
| 1398 | do { |
| 1399 | int chunk; |
| 1400 | struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue); |
| 1401 | |
Olaf Kirch | 305f2aa | 2007-04-20 22:05:27 -0700 | [diff] [blame] | 1402 | if (skb == NULL) { |
| 1403 | DEFINE_WAIT(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | int ret = 0; |
| 1405 | |
| 1406 | if (copied >= target) |
| 1407 | break; |
| 1408 | |
Olaf Kirch | 305f2aa | 2007-04-20 22:05:27 -0700 | [diff] [blame] | 1409 | prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | |
| 1411 | /* |
| 1412 | * POSIX 1003.1g mandates this order. |
| 1413 | */ |
Benjamin LaHaise | c1cbe4b | 2005-12-13 23:22:19 -0800 | [diff] [blame] | 1414 | ret = sock_error(sk); |
| 1415 | if (ret) |
Olaf Kirch | bfb6709 | 2007-04-18 15:07:22 -0700 | [diff] [blame] | 1416 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | else if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1418 | ; |
| 1419 | else if (noblock) |
| 1420 | ret = -EAGAIN; |
| 1421 | else if (signal_pending(current)) |
Olaf Kirch | 305f2aa | 2007-04-20 22:05:27 -0700 | [diff] [blame] | 1422 | ret = sock_intr_errno(timeo); |
| 1423 | else if (sk->sk_state != TCP_ESTABLISHED) |
| 1424 | ret = -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | else if (skb_peek(&sk->sk_receive_queue) == NULL) |
| 1426 | /* Wait process until data arrives */ |
| 1427 | schedule(); |
| 1428 | |
Olaf Kirch | 305f2aa | 2007-04-20 22:05:27 -0700 | [diff] [blame] | 1429 | finish_wait(sk->sk_sleep, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | |
Olaf Kirch | 305f2aa | 2007-04-20 22:05:27 -0700 | [diff] [blame] | 1431 | if (ret) |
| 1432 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1434 | break; |
| 1435 | |
| 1436 | continue; |
| 1437 | } |
| 1438 | |
| 1439 | chunk = min_t(unsigned int, skb->len, size); |
| 1440 | if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) { |
| 1441 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1442 | if (copied == 0) |
| 1443 | copied = -EFAULT; |
| 1444 | break; |
| 1445 | } |
| 1446 | copied += chunk; |
| 1447 | size -= chunk; |
| 1448 | |
| 1449 | /* Mark read part of skb as used */ |
| 1450 | if (!(flags & MSG_PEEK)) { |
| 1451 | skb_pull(skb, chunk); |
| 1452 | |
| 1453 | /* put the skb back if we didn't use it up.. */ |
| 1454 | if (skb->len) { |
| 1455 | IRDA_DEBUG(1, "%s(), back on q!\n", |
| 1456 | __FUNCTION__); |
| 1457 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1458 | break; |
| 1459 | } |
| 1460 | |
| 1461 | kfree_skb(skb); |
| 1462 | } else { |
| 1463 | IRDA_DEBUG(0, "%s() questionable!?\n", __FUNCTION__); |
| 1464 | |
| 1465 | /* put message back and return */ |
| 1466 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1467 | break; |
| 1468 | } |
| 1469 | } while (size); |
| 1470 | |
| 1471 | /* |
| 1472 | * Check if we have previously stopped IrTTP and we know |
| 1473 | * have more free space in our rx_queue. If so tell IrTTP |
| 1474 | * to start delivering frames again before our rx_queue gets |
| 1475 | * empty |
| 1476 | */ |
| 1477 | if (self->rx_flow == FLOW_STOP) { |
| 1478 | if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) { |
| 1479 | IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __FUNCTION__); |
| 1480 | self->rx_flow = FLOW_START; |
| 1481 | irttp_flow_request(self->tsap, FLOW_START); |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | return copied; |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | * Function irda_sendmsg_dgram (iocb, sock, msg, len) |
| 1490 | * |
| 1491 | * Send message down to TinyTP for the unreliable sequenced |
| 1492 | * packet service... |
| 1493 | * |
| 1494 | */ |
| 1495 | static int irda_sendmsg_dgram(struct kiocb *iocb, struct socket *sock, |
| 1496 | struct msghdr *msg, size_t len) |
| 1497 | { |
| 1498 | struct sock *sk = sock->sk; |
| 1499 | struct irda_sock *self; |
| 1500 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | int err; |
| 1502 | |
| 1503 | IRDA_DEBUG(4, "%s(), len=%zd\n", __FUNCTION__, len); |
| 1504 | |
| 1505 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT)) |
| 1506 | return -EINVAL; |
| 1507 | |
| 1508 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 1509 | send_sig(SIGPIPE, current, 0); |
| 1510 | return -EPIPE; |
| 1511 | } |
| 1512 | |
| 1513 | if (sk->sk_state != TCP_ESTABLISHED) |
| 1514 | return -ENOTCONN; |
| 1515 | |
| 1516 | self = irda_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
| 1518 | /* |
Alexey Dobriyan | 7f927fc | 2006-03-28 01:56:53 -0800 | [diff] [blame] | 1519 | * Check that we don't send out too big frames. This is an unreliable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | * service, so we have no fragmentation and no coalescence |
| 1521 | */ |
| 1522 | if (len > self->max_data_size) { |
| 1523 | IRDA_DEBUG(0, "%s(), Warning to much data! " |
| 1524 | "Chopping frame from %zd to %d bytes!\n", |
| 1525 | __FUNCTION__, len, self->max_data_size); |
| 1526 | len = self->max_data_size; |
| 1527 | } |
| 1528 | |
| 1529 | skb = sock_alloc_send_skb(sk, len + self->max_header_size, |
| 1530 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1531 | if (!skb) |
| 1532 | return -ENOBUFS; |
| 1533 | |
| 1534 | skb_reserve(skb, self->max_header_size); |
Arnaldo Carvalho de Melo | eeeb037 | 2007-03-14 21:04:34 -0300 | [diff] [blame] | 1535 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | |
| 1537 | IRDA_DEBUG(4, "%s(), appending user data\n", __FUNCTION__); |
Arnaldo Carvalho de Melo | eeeb037 | 2007-03-14 21:04:34 -0300 | [diff] [blame] | 1538 | skb_put(skb, len); |
| 1539 | err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | if (err) { |
| 1541 | kfree_skb(skb); |
| 1542 | return err; |
| 1543 | } |
| 1544 | |
| 1545 | /* |
| 1546 | * Just send the message to TinyTP, and let it deal with possible |
| 1547 | * errors. No need to duplicate all that here |
| 1548 | */ |
| 1549 | err = irttp_udata_request(self->tsap, skb); |
| 1550 | if (err) { |
| 1551 | IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err); |
| 1552 | return err; |
| 1553 | } |
| 1554 | return len; |
| 1555 | } |
| 1556 | |
| 1557 | /* |
| 1558 | * Function irda_sendmsg_ultra (iocb, sock, msg, len) |
| 1559 | * |
| 1560 | * Send message down to IrLMP for the unreliable Ultra |
| 1561 | * packet service... |
| 1562 | */ |
| 1563 | #ifdef CONFIG_IRDA_ULTRA |
| 1564 | static int irda_sendmsg_ultra(struct kiocb *iocb, struct socket *sock, |
| 1565 | struct msghdr *msg, size_t len) |
| 1566 | { |
| 1567 | struct sock *sk = sock->sk; |
| 1568 | struct irda_sock *self; |
| 1569 | __u8 pid = 0; |
| 1570 | int bound = 0; |
| 1571 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | int err; |
| 1573 | |
| 1574 | IRDA_DEBUG(4, "%s(), len=%zd\n", __FUNCTION__, len); |
| 1575 | |
| 1576 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT)) |
| 1577 | return -EINVAL; |
| 1578 | |
| 1579 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 1580 | send_sig(SIGPIPE, current, 0); |
| 1581 | return -EPIPE; |
| 1582 | } |
| 1583 | |
| 1584 | self = irda_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | |
| 1586 | /* Check if an address was specified with sendto. Jean II */ |
| 1587 | if (msg->msg_name) { |
| 1588 | struct sockaddr_irda *addr = (struct sockaddr_irda *) msg->msg_name; |
| 1589 | /* Check address, extract pid. Jean II */ |
| 1590 | if (msg->msg_namelen < sizeof(*addr)) |
| 1591 | return -EINVAL; |
| 1592 | if (addr->sir_family != AF_IRDA) |
| 1593 | return -EINVAL; |
| 1594 | |
| 1595 | pid = addr->sir_lsap_sel; |
| 1596 | if (pid & 0x80) { |
| 1597 | IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __FUNCTION__); |
| 1598 | return -EOPNOTSUPP; |
| 1599 | } |
| 1600 | } else { |
| 1601 | /* Check that the socket is properly bound to an Ultra |
| 1602 | * port. Jean II */ |
| 1603 | if ((self->lsap == NULL) || |
| 1604 | (sk->sk_state != TCP_ESTABLISHED)) { |
| 1605 | IRDA_DEBUG(0, "%s(), socket not bound to Ultra PID.\n", |
| 1606 | __FUNCTION__); |
| 1607 | return -ENOTCONN; |
| 1608 | } |
| 1609 | /* Use PID from socket */ |
| 1610 | bound = 1; |
| 1611 | } |
| 1612 | |
| 1613 | /* |
Alexey Dobriyan | 7f927fc | 2006-03-28 01:56:53 -0800 | [diff] [blame] | 1614 | * Check that we don't send out too big frames. This is an unreliable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | * service, so we have no fragmentation and no coalescence |
| 1616 | */ |
| 1617 | if (len > self->max_data_size) { |
| 1618 | IRDA_DEBUG(0, "%s(), Warning to much data! " |
| 1619 | "Chopping frame from %zd to %d bytes!\n", |
| 1620 | __FUNCTION__, len, self->max_data_size); |
| 1621 | len = self->max_data_size; |
| 1622 | } |
| 1623 | |
| 1624 | skb = sock_alloc_send_skb(sk, len + self->max_header_size, |
| 1625 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1626 | if (!skb) |
| 1627 | return -ENOBUFS; |
| 1628 | |
| 1629 | skb_reserve(skb, self->max_header_size); |
Arnaldo Carvalho de Melo | eeeb037 | 2007-03-14 21:04:34 -0300 | [diff] [blame] | 1630 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
| 1632 | IRDA_DEBUG(4, "%s(), appending user data\n", __FUNCTION__); |
Arnaldo Carvalho de Melo | eeeb037 | 2007-03-14 21:04:34 -0300 | [diff] [blame] | 1633 | skb_put(skb, len); |
| 1634 | err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | if (err) { |
| 1636 | kfree_skb(skb); |
| 1637 | return err; |
| 1638 | } |
| 1639 | |
| 1640 | err = irlmp_connless_data_request((bound ? self->lsap : NULL), |
| 1641 | skb, pid); |
| 1642 | if (err) { |
| 1643 | IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err); |
| 1644 | return err; |
| 1645 | } |
| 1646 | return len; |
| 1647 | } |
| 1648 | #endif /* CONFIG_IRDA_ULTRA */ |
| 1649 | |
| 1650 | /* |
| 1651 | * Function irda_shutdown (sk, how) |
| 1652 | */ |
| 1653 | static int irda_shutdown(struct socket *sock, int how) |
| 1654 | { |
| 1655 | struct sock *sk = sock->sk; |
| 1656 | struct irda_sock *self = irda_sk(sk); |
| 1657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | IRDA_DEBUG(1, "%s(%p)\n", __FUNCTION__, self); |
| 1659 | |
| 1660 | sk->sk_state = TCP_CLOSE; |
| 1661 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 1662 | sk->sk_state_change(sk); |
| 1663 | |
| 1664 | if (self->iriap) { |
| 1665 | iriap_close(self->iriap); |
| 1666 | self->iriap = NULL; |
| 1667 | } |
| 1668 | |
| 1669 | if (self->tsap) { |
| 1670 | irttp_disconnect_request(self->tsap, NULL, P_NORMAL); |
| 1671 | irttp_close_tsap(self->tsap); |
| 1672 | self->tsap = NULL; |
| 1673 | } |
| 1674 | |
| 1675 | /* A few cleanup so the socket look as good as new... */ |
| 1676 | self->rx_flow = self->tx_flow = FLOW_START; /* needed ??? */ |
| 1677 | self->daddr = DEV_ADDR_ANY; /* Until we get re-connected */ |
| 1678 | self->saddr = 0x0; /* so IrLMP assign us any link */ |
| 1679 | |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 1680 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /* |
| 1684 | * Function irda_poll (file, sock, wait) |
| 1685 | */ |
| 1686 | static unsigned int irda_poll(struct file * file, struct socket *sock, |
| 1687 | poll_table *wait) |
| 1688 | { |
| 1689 | struct sock *sk = sock->sk; |
| 1690 | struct irda_sock *self = irda_sk(sk); |
| 1691 | unsigned int mask; |
| 1692 | |
| 1693 | IRDA_DEBUG(4, "%s()\n", __FUNCTION__); |
| 1694 | |
| 1695 | poll_wait(file, sk->sk_sleep, wait); |
| 1696 | mask = 0; |
| 1697 | |
| 1698 | /* Exceptional events? */ |
| 1699 | if (sk->sk_err) |
| 1700 | mask |= POLLERR; |
| 1701 | if (sk->sk_shutdown & RCV_SHUTDOWN) { |
| 1702 | IRDA_DEBUG(0, "%s(), POLLHUP\n", __FUNCTION__); |
| 1703 | mask |= POLLHUP; |
| 1704 | } |
| 1705 | |
| 1706 | /* Readable? */ |
| 1707 | if (!skb_queue_empty(&sk->sk_receive_queue)) { |
| 1708 | IRDA_DEBUG(4, "Socket is readable\n"); |
| 1709 | mask |= POLLIN | POLLRDNORM; |
| 1710 | } |
| 1711 | |
| 1712 | /* Connection-based need to check for termination and startup */ |
| 1713 | switch (sk->sk_type) { |
| 1714 | case SOCK_STREAM: |
| 1715 | if (sk->sk_state == TCP_CLOSE) { |
| 1716 | IRDA_DEBUG(0, "%s(), POLLHUP\n", __FUNCTION__); |
| 1717 | mask |= POLLHUP; |
| 1718 | } |
| 1719 | |
| 1720 | if (sk->sk_state == TCP_ESTABLISHED) { |
| 1721 | if ((self->tx_flow == FLOW_START) && |
| 1722 | sock_writeable(sk)) |
| 1723 | { |
| 1724 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 1725 | } |
| 1726 | } |
| 1727 | break; |
| 1728 | case SOCK_SEQPACKET: |
| 1729 | if ((self->tx_flow == FLOW_START) && |
| 1730 | sock_writeable(sk)) |
| 1731 | { |
| 1732 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 1733 | } |
| 1734 | break; |
| 1735 | case SOCK_DGRAM: |
| 1736 | if (sock_writeable(sk)) |
| 1737 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 1738 | break; |
| 1739 | default: |
| 1740 | break; |
| 1741 | } |
| 1742 | return mask; |
| 1743 | } |
| 1744 | |
| 1745 | /* |
| 1746 | * Function irda_ioctl (sock, cmd, arg) |
| 1747 | */ |
| 1748 | static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 1749 | { |
| 1750 | struct sock *sk = sock->sk; |
| 1751 | |
| 1752 | IRDA_DEBUG(4, "%s(), cmd=%#x\n", __FUNCTION__, cmd); |
| 1753 | |
| 1754 | switch (cmd) { |
| 1755 | case TIOCOUTQ: { |
| 1756 | long amount; |
| 1757 | amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc); |
| 1758 | if (amount < 0) |
| 1759 | amount = 0; |
| 1760 | if (put_user(amount, (unsigned int __user *)arg)) |
| 1761 | return -EFAULT; |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
| 1765 | case TIOCINQ: { |
| 1766 | struct sk_buff *skb; |
| 1767 | long amount = 0L; |
| 1768 | /* These two are safe on a single CPU system as only user tasks fiddle here */ |
| 1769 | if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) |
| 1770 | amount = skb->len; |
| 1771 | if (put_user(amount, (unsigned int __user *)arg)) |
| 1772 | return -EFAULT; |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | case SIOCGSTAMP: |
| 1777 | if (sk != NULL) |
| 1778 | return sock_get_timestamp(sk, (struct timeval __user *)arg); |
| 1779 | return -EINVAL; |
| 1780 | |
| 1781 | case SIOCGIFADDR: |
| 1782 | case SIOCSIFADDR: |
| 1783 | case SIOCGIFDSTADDR: |
| 1784 | case SIOCSIFDSTADDR: |
| 1785 | case SIOCGIFBRDADDR: |
| 1786 | case SIOCSIFBRDADDR: |
| 1787 | case SIOCGIFNETMASK: |
| 1788 | case SIOCSIFNETMASK: |
| 1789 | case SIOCGIFMETRIC: |
| 1790 | case SIOCSIFMETRIC: |
| 1791 | return -EINVAL; |
| 1792 | default: |
| 1793 | IRDA_DEBUG(1, "%s(), doing device ioctl!\n", __FUNCTION__); |
Christoph Hellwig | b5e5fa5 | 2006-01-03 14:18:33 -0800 | [diff] [blame] | 1794 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | /*NOTREACHED*/ |
| 1798 | return 0; |
| 1799 | } |
| 1800 | |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 1801 | #ifdef CONFIG_COMPAT |
| 1802 | /* |
| 1803 | * Function irda_ioctl (sock, cmd, arg) |
| 1804 | */ |
| 1805 | static int irda_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 1806 | { |
| 1807 | /* |
| 1808 | * All IRDA's ioctl are standard ones. |
| 1809 | */ |
| 1810 | return -ENOIOCTLCMD; |
| 1811 | } |
| 1812 | #endif |
| 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | /* |
| 1815 | * Function irda_setsockopt (sock, level, optname, optval, optlen) |
| 1816 | * |
| 1817 | * Set some options for the socket |
| 1818 | * |
| 1819 | */ |
| 1820 | static int irda_setsockopt(struct socket *sock, int level, int optname, |
| 1821 | char __user *optval, int optlen) |
| 1822 | { |
| 1823 | struct sock *sk = sock->sk; |
| 1824 | struct irda_sock *self = irda_sk(sk); |
| 1825 | struct irda_ias_set *ias_opt; |
| 1826 | struct ias_object *ias_obj; |
| 1827 | struct ias_attrib * ias_attr; /* Attribute in IAS object */ |
| 1828 | int opt; |
| 1829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self); |
| 1831 | |
| 1832 | if (level != SOL_IRLMP) |
| 1833 | return -ENOPROTOOPT; |
| 1834 | |
| 1835 | switch (optname) { |
| 1836 | case IRLMP_IAS_SET: |
| 1837 | /* The user want to add an attribute to an existing IAS object |
| 1838 | * (in the IAS database) or to create a new object with this |
| 1839 | * attribute. |
| 1840 | * We first query IAS to know if the object exist, and then |
| 1841 | * create the right attribute... |
| 1842 | */ |
| 1843 | |
| 1844 | if (optlen != sizeof(struct irda_ias_set)) |
| 1845 | return -EINVAL; |
| 1846 | |
| 1847 | ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC); |
| 1848 | if (ias_opt == NULL) |
| 1849 | return -ENOMEM; |
| 1850 | |
| 1851 | /* Copy query to the driver. */ |
| 1852 | if (copy_from_user(ias_opt, optval, optlen)) { |
| 1853 | kfree(ias_opt); |
| 1854 | return -EFAULT; |
| 1855 | } |
| 1856 | |
| 1857 | /* Find the object we target. |
| 1858 | * If the user gives us an empty string, we use the object |
| 1859 | * associated with this socket. This will workaround |
| 1860 | * duplicated class name - Jean II */ |
| 1861 | if(ias_opt->irda_class_name[0] == '\0') { |
| 1862 | if(self->ias_obj == NULL) { |
| 1863 | kfree(ias_opt); |
| 1864 | return -EINVAL; |
| 1865 | } |
| 1866 | ias_obj = self->ias_obj; |
| 1867 | } else |
| 1868 | ias_obj = irias_find_object(ias_opt->irda_class_name); |
| 1869 | |
| 1870 | /* Only ROOT can mess with the global IAS database. |
| 1871 | * Users can only add attributes to the object associated |
| 1872 | * with the socket they own - Jean II */ |
| 1873 | if((!capable(CAP_NET_ADMIN)) && |
| 1874 | ((ias_obj == NULL) || (ias_obj != self->ias_obj))) { |
| 1875 | kfree(ias_opt); |
| 1876 | return -EPERM; |
| 1877 | } |
| 1878 | |
| 1879 | /* If the object doesn't exist, create it */ |
| 1880 | if(ias_obj == (struct ias_object *) NULL) { |
| 1881 | /* Create a new object */ |
| 1882 | ias_obj = irias_new_object(ias_opt->irda_class_name, |
| 1883 | jiffies); |
| 1884 | } |
| 1885 | |
| 1886 | /* Do we have the attribute already ? */ |
| 1887 | if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) { |
| 1888 | kfree(ias_opt); |
| 1889 | return -EINVAL; |
| 1890 | } |
| 1891 | |
| 1892 | /* Look at the type */ |
| 1893 | switch(ias_opt->irda_attrib_type) { |
| 1894 | case IAS_INTEGER: |
| 1895 | /* Add an integer attribute */ |
| 1896 | irias_add_integer_attrib( |
| 1897 | ias_obj, |
| 1898 | ias_opt->irda_attrib_name, |
| 1899 | ias_opt->attribute.irda_attrib_int, |
| 1900 | IAS_USER_ATTR); |
| 1901 | break; |
| 1902 | case IAS_OCT_SEQ: |
| 1903 | /* Check length */ |
| 1904 | if(ias_opt->attribute.irda_attrib_octet_seq.len > |
| 1905 | IAS_MAX_OCTET_STRING) { |
| 1906 | kfree(ias_opt); |
| 1907 | return -EINVAL; |
| 1908 | } |
| 1909 | /* Add an octet sequence attribute */ |
| 1910 | irias_add_octseq_attrib( |
| 1911 | ias_obj, |
| 1912 | ias_opt->irda_attrib_name, |
| 1913 | ias_opt->attribute.irda_attrib_octet_seq.octet_seq, |
| 1914 | ias_opt->attribute.irda_attrib_octet_seq.len, |
| 1915 | IAS_USER_ATTR); |
| 1916 | break; |
| 1917 | case IAS_STRING: |
| 1918 | /* Should check charset & co */ |
| 1919 | /* Check length */ |
| 1920 | /* The length is encoded in a __u8, and |
| 1921 | * IAS_MAX_STRING == 256, so there is no way |
| 1922 | * userspace can pass us a string too large. |
| 1923 | * Jean II */ |
| 1924 | /* NULL terminate the string (avoid troubles) */ |
| 1925 | ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0'; |
| 1926 | /* Add a string attribute */ |
| 1927 | irias_add_string_attrib( |
| 1928 | ias_obj, |
| 1929 | ias_opt->irda_attrib_name, |
| 1930 | ias_opt->attribute.irda_attrib_string.string, |
| 1931 | IAS_USER_ATTR); |
| 1932 | break; |
| 1933 | default : |
| 1934 | kfree(ias_opt); |
| 1935 | return -EINVAL; |
| 1936 | } |
| 1937 | irias_insert_object(ias_obj); |
| 1938 | kfree(ias_opt); |
| 1939 | break; |
| 1940 | case IRLMP_IAS_DEL: |
| 1941 | /* The user want to delete an object from our local IAS |
| 1942 | * database. We just need to query the IAS, check is the |
| 1943 | * object is not owned by the kernel and delete it. |
| 1944 | */ |
| 1945 | |
| 1946 | if (optlen != sizeof(struct irda_ias_set)) |
| 1947 | return -EINVAL; |
| 1948 | |
| 1949 | ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC); |
| 1950 | if (ias_opt == NULL) |
| 1951 | return -ENOMEM; |
| 1952 | |
| 1953 | /* Copy query to the driver. */ |
| 1954 | if (copy_from_user(ias_opt, optval, optlen)) { |
| 1955 | kfree(ias_opt); |
| 1956 | return -EFAULT; |
| 1957 | } |
| 1958 | |
| 1959 | /* Find the object we target. |
| 1960 | * If the user gives us an empty string, we use the object |
| 1961 | * associated with this socket. This will workaround |
| 1962 | * duplicated class name - Jean II */ |
| 1963 | if(ias_opt->irda_class_name[0] == '\0') |
| 1964 | ias_obj = self->ias_obj; |
| 1965 | else |
| 1966 | ias_obj = irias_find_object(ias_opt->irda_class_name); |
| 1967 | if(ias_obj == (struct ias_object *) NULL) { |
| 1968 | kfree(ias_opt); |
| 1969 | return -EINVAL; |
| 1970 | } |
| 1971 | |
| 1972 | /* Only ROOT can mess with the global IAS database. |
| 1973 | * Users can only del attributes from the object associated |
| 1974 | * with the socket they own - Jean II */ |
| 1975 | if((!capable(CAP_NET_ADMIN)) && |
| 1976 | ((ias_obj == NULL) || (ias_obj != self->ias_obj))) { |
| 1977 | kfree(ias_opt); |
| 1978 | return -EPERM; |
| 1979 | } |
| 1980 | |
| 1981 | /* Find the attribute (in the object) we target */ |
| 1982 | ias_attr = irias_find_attrib(ias_obj, |
| 1983 | ias_opt->irda_attrib_name); |
| 1984 | if(ias_attr == (struct ias_attrib *) NULL) { |
| 1985 | kfree(ias_opt); |
| 1986 | return -EINVAL; |
| 1987 | } |
| 1988 | |
| 1989 | /* Check is the user space own the object */ |
| 1990 | if(ias_attr->value->owner != IAS_USER_ATTR) { |
| 1991 | IRDA_DEBUG(1, "%s(), attempting to delete a kernel attribute\n", __FUNCTION__); |
| 1992 | kfree(ias_opt); |
| 1993 | return -EPERM; |
| 1994 | } |
| 1995 | |
| 1996 | /* Remove the attribute (and maybe the object) */ |
| 1997 | irias_delete_attrib(ias_obj, ias_attr, 1); |
| 1998 | kfree(ias_opt); |
| 1999 | break; |
| 2000 | case IRLMP_MAX_SDU_SIZE: |
| 2001 | if (optlen < sizeof(int)) |
| 2002 | return -EINVAL; |
| 2003 | |
| 2004 | if (get_user(opt, (int __user *)optval)) |
| 2005 | return -EFAULT; |
| 2006 | |
| 2007 | /* Only possible for a seqpacket service (TTP with SAR) */ |
| 2008 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 2009 | IRDA_DEBUG(2, "%s(), setting max_sdu_size = %d\n", |
| 2010 | __FUNCTION__, opt); |
| 2011 | self->max_sdu_size_rx = opt; |
| 2012 | } else { |
| 2013 | IRDA_WARNING("%s: not allowed to set MAXSDUSIZE for this socket type!\n", |
| 2014 | __FUNCTION__); |
| 2015 | return -ENOPROTOOPT; |
| 2016 | } |
| 2017 | break; |
| 2018 | case IRLMP_HINTS_SET: |
| 2019 | if (optlen < sizeof(int)) |
| 2020 | return -EINVAL; |
| 2021 | |
| 2022 | /* The input is really a (__u8 hints[2]), easier as an int */ |
| 2023 | if (get_user(opt, (int __user *)optval)) |
| 2024 | return -EFAULT; |
| 2025 | |
| 2026 | /* Unregister any old registration */ |
| 2027 | if (self->skey) |
| 2028 | irlmp_unregister_service(self->skey); |
| 2029 | |
| 2030 | self->skey = irlmp_register_service((__u16) opt); |
| 2031 | break; |
| 2032 | case IRLMP_HINT_MASK_SET: |
| 2033 | /* As opposed to the previous case which set the hint bits |
| 2034 | * that we advertise, this one set the filter we use when |
| 2035 | * making a discovery (nodes which don't match any hint |
| 2036 | * bit in the mask are not reported). |
| 2037 | */ |
| 2038 | if (optlen < sizeof(int)) |
| 2039 | return -EINVAL; |
| 2040 | |
| 2041 | /* The input is really a (__u8 hints[2]), easier as an int */ |
| 2042 | if (get_user(opt, (int __user *)optval)) |
| 2043 | return -EFAULT; |
| 2044 | |
| 2045 | /* Set the new hint mask */ |
| 2046 | self->mask.word = (__u16) opt; |
| 2047 | /* Mask out extension bits */ |
| 2048 | self->mask.word &= 0x7f7f; |
| 2049 | /* Check if no bits */ |
| 2050 | if(!self->mask.word) |
| 2051 | self->mask.word = 0xFFFF; |
| 2052 | |
| 2053 | break; |
| 2054 | default: |
| 2055 | return -ENOPROTOOPT; |
| 2056 | } |
| 2057 | return 0; |
| 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * Function irda_extract_ias_value(ias_opt, ias_value) |
| 2062 | * |
| 2063 | * Translate internal IAS value structure to the user space representation |
| 2064 | * |
| 2065 | * The external representation of IAS values, as we exchange them with |
| 2066 | * user space program is quite different from the internal representation, |
| 2067 | * as stored in the IAS database (because we need a flat structure for |
| 2068 | * crossing kernel boundary). |
| 2069 | * This function transform the former in the latter. We also check |
| 2070 | * that the value type is valid. |
| 2071 | */ |
| 2072 | static int irda_extract_ias_value(struct irda_ias_set *ias_opt, |
| 2073 | struct ias_value *ias_value) |
| 2074 | { |
| 2075 | /* Look at the type */ |
| 2076 | switch (ias_value->type) { |
| 2077 | case IAS_INTEGER: |
| 2078 | /* Copy the integer */ |
| 2079 | ias_opt->attribute.irda_attrib_int = ias_value->t.integer; |
| 2080 | break; |
| 2081 | case IAS_OCT_SEQ: |
| 2082 | /* Set length */ |
| 2083 | ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len; |
| 2084 | /* Copy over */ |
| 2085 | memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq, |
| 2086 | ias_value->t.oct_seq, ias_value->len); |
| 2087 | break; |
| 2088 | case IAS_STRING: |
| 2089 | /* Set length */ |
| 2090 | ias_opt->attribute.irda_attrib_string.len = ias_value->len; |
| 2091 | ias_opt->attribute.irda_attrib_string.charset = ias_value->charset; |
| 2092 | /* Copy over */ |
| 2093 | memcpy(ias_opt->attribute.irda_attrib_string.string, |
| 2094 | ias_value->t.string, ias_value->len); |
| 2095 | /* NULL terminate the string (avoid troubles) */ |
| 2096 | ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0'; |
| 2097 | break; |
| 2098 | case IAS_MISSING: |
| 2099 | default : |
| 2100 | return -EINVAL; |
| 2101 | } |
| 2102 | |
| 2103 | /* Copy type over */ |
| 2104 | ias_opt->irda_attrib_type = ias_value->type; |
| 2105 | |
| 2106 | return 0; |
| 2107 | } |
| 2108 | |
| 2109 | /* |
| 2110 | * Function irda_getsockopt (sock, level, optname, optval, optlen) |
| 2111 | */ |
| 2112 | static int irda_getsockopt(struct socket *sock, int level, int optname, |
| 2113 | char __user *optval, int __user *optlen) |
| 2114 | { |
| 2115 | struct sock *sk = sock->sk; |
| 2116 | struct irda_sock *self = irda_sk(sk); |
| 2117 | struct irda_device_list list; |
| 2118 | struct irda_device_info *discoveries; |
| 2119 | struct irda_ias_set * ias_opt; /* IAS get/query params */ |
| 2120 | struct ias_object * ias_obj; /* Object in IAS */ |
| 2121 | struct ias_attrib * ias_attr; /* Attribute in IAS object */ |
| 2122 | int daddr = DEV_ADDR_ANY; /* Dest address for IAS queries */ |
| 2123 | int val = 0; |
| 2124 | int len = 0; |
| 2125 | int err; |
| 2126 | int offset, total; |
| 2127 | |
| 2128 | IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self); |
| 2129 | |
| 2130 | if (level != SOL_IRLMP) |
| 2131 | return -ENOPROTOOPT; |
| 2132 | |
| 2133 | if (get_user(len, optlen)) |
| 2134 | return -EFAULT; |
| 2135 | |
| 2136 | if(len < 0) |
| 2137 | return -EINVAL; |
| 2138 | |
| 2139 | switch (optname) { |
| 2140 | case IRLMP_ENUMDEVICES: |
| 2141 | /* Ask lmp for the current discovery log */ |
| 2142 | discoveries = irlmp_get_discoveries(&list.len, self->mask.word, |
| 2143 | self->nslots); |
| 2144 | /* Check if the we got some results */ |
| 2145 | if (discoveries == NULL) |
| 2146 | return -EAGAIN; /* Didn't find any devices */ |
| 2147 | err = 0; |
| 2148 | |
| 2149 | /* Write total list length back to client */ |
| 2150 | if (copy_to_user(optval, &list, |
| 2151 | sizeof(struct irda_device_list) - |
| 2152 | sizeof(struct irda_device_info))) |
| 2153 | err = -EFAULT; |
| 2154 | |
| 2155 | /* Offset to first device entry */ |
| 2156 | offset = sizeof(struct irda_device_list) - |
| 2157 | sizeof(struct irda_device_info); |
| 2158 | |
| 2159 | /* Copy the list itself - watch for overflow */ |
| 2160 | if(list.len > 2048) |
| 2161 | { |
| 2162 | err = -EINVAL; |
| 2163 | goto bed; |
| 2164 | } |
| 2165 | total = offset + (list.len * sizeof(struct irda_device_info)); |
| 2166 | if (total > len) |
| 2167 | total = len; |
| 2168 | if (copy_to_user(optval+offset, discoveries, total - offset)) |
| 2169 | err = -EFAULT; |
| 2170 | |
| 2171 | /* Write total number of bytes used back to client */ |
| 2172 | if (put_user(total, optlen)) |
| 2173 | err = -EFAULT; |
| 2174 | bed: |
| 2175 | /* Free up our buffer */ |
| 2176 | kfree(discoveries); |
| 2177 | if (err) |
| 2178 | return err; |
| 2179 | break; |
| 2180 | case IRLMP_MAX_SDU_SIZE: |
| 2181 | val = self->max_data_size; |
| 2182 | len = sizeof(int); |
| 2183 | if (put_user(len, optlen)) |
| 2184 | return -EFAULT; |
| 2185 | |
| 2186 | if (copy_to_user(optval, &val, len)) |
| 2187 | return -EFAULT; |
| 2188 | break; |
| 2189 | case IRLMP_IAS_GET: |
| 2190 | /* The user want an object from our local IAS database. |
| 2191 | * We just need to query the IAS and return the value |
| 2192 | * that we found */ |
| 2193 | |
| 2194 | /* Check that the user has allocated the right space for us */ |
| 2195 | if (len != sizeof(struct irda_ias_set)) |
| 2196 | return -EINVAL; |
| 2197 | |
| 2198 | ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC); |
| 2199 | if (ias_opt == NULL) |
| 2200 | return -ENOMEM; |
| 2201 | |
| 2202 | /* Copy query to the driver. */ |
| 2203 | if (copy_from_user(ias_opt, optval, len)) { |
| 2204 | kfree(ias_opt); |
| 2205 | return -EFAULT; |
| 2206 | } |
| 2207 | |
| 2208 | /* Find the object we target. |
| 2209 | * If the user gives us an empty string, we use the object |
| 2210 | * associated with this socket. This will workaround |
| 2211 | * duplicated class name - Jean II */ |
| 2212 | if(ias_opt->irda_class_name[0] == '\0') |
| 2213 | ias_obj = self->ias_obj; |
| 2214 | else |
| 2215 | ias_obj = irias_find_object(ias_opt->irda_class_name); |
| 2216 | if(ias_obj == (struct ias_object *) NULL) { |
| 2217 | kfree(ias_opt); |
| 2218 | return -EINVAL; |
| 2219 | } |
| 2220 | |
| 2221 | /* Find the attribute (in the object) we target */ |
| 2222 | ias_attr = irias_find_attrib(ias_obj, |
| 2223 | ias_opt->irda_attrib_name); |
| 2224 | if(ias_attr == (struct ias_attrib *) NULL) { |
| 2225 | kfree(ias_opt); |
| 2226 | return -EINVAL; |
| 2227 | } |
| 2228 | |
| 2229 | /* Translate from internal to user structure */ |
| 2230 | err = irda_extract_ias_value(ias_opt, ias_attr->value); |
| 2231 | if(err) { |
| 2232 | kfree(ias_opt); |
| 2233 | return err; |
| 2234 | } |
| 2235 | |
| 2236 | /* Copy reply to the user */ |
| 2237 | if (copy_to_user(optval, ias_opt, |
| 2238 | sizeof(struct irda_ias_set))) { |
| 2239 | kfree(ias_opt); |
| 2240 | return -EFAULT; |
| 2241 | } |
| 2242 | /* Note : don't need to put optlen, we checked it */ |
| 2243 | kfree(ias_opt); |
| 2244 | break; |
| 2245 | case IRLMP_IAS_QUERY: |
| 2246 | /* The user want an object from a remote IAS database. |
| 2247 | * We need to use IAP to query the remote database and |
| 2248 | * then wait for the answer to come back. */ |
| 2249 | |
| 2250 | /* Check that the user has allocated the right space for us */ |
| 2251 | if (len != sizeof(struct irda_ias_set)) |
| 2252 | return -EINVAL; |
| 2253 | |
| 2254 | ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC); |
| 2255 | if (ias_opt == NULL) |
| 2256 | return -ENOMEM; |
| 2257 | |
| 2258 | /* Copy query to the driver. */ |
| 2259 | if (copy_from_user(ias_opt, optval, len)) { |
| 2260 | kfree(ias_opt); |
| 2261 | return -EFAULT; |
| 2262 | } |
| 2263 | |
| 2264 | /* At this point, there are two cases... |
| 2265 | * 1) the socket is connected - that's the easy case, we |
| 2266 | * just query the device we are connected to... |
| 2267 | * 2) the socket is not connected - the user doesn't want |
| 2268 | * to connect and/or may not have a valid service name |
| 2269 | * (so can't create a fake connection). In this case, |
| 2270 | * we assume that the user pass us a valid destination |
| 2271 | * address in the requesting structure... |
| 2272 | */ |
| 2273 | if(self->daddr != DEV_ADDR_ANY) { |
| 2274 | /* We are connected - reuse known daddr */ |
| 2275 | daddr = self->daddr; |
| 2276 | } else { |
| 2277 | /* We are not connected, we must specify a valid |
| 2278 | * destination address */ |
| 2279 | daddr = ias_opt->daddr; |
| 2280 | if((!daddr) || (daddr == DEV_ADDR_ANY)) { |
| 2281 | kfree(ias_opt); |
| 2282 | return -EINVAL; |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | /* Check that we can proceed with IAP */ |
| 2287 | if (self->iriap) { |
| 2288 | IRDA_WARNING("%s: busy with a previous query\n", |
| 2289 | __FUNCTION__); |
| 2290 | kfree(ias_opt); |
| 2291 | return -EBUSY; |
| 2292 | } |
| 2293 | |
| 2294 | self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self, |
| 2295 | irda_getvalue_confirm); |
| 2296 | |
| 2297 | if (self->iriap == NULL) { |
| 2298 | kfree(ias_opt); |
| 2299 | return -ENOMEM; |
| 2300 | } |
| 2301 | |
| 2302 | /* Treat unexpected wakeup as disconnect */ |
| 2303 | self->errno = -EHOSTUNREACH; |
| 2304 | |
| 2305 | /* Query remote LM-IAS */ |
| 2306 | iriap_getvaluebyclass_request(self->iriap, |
| 2307 | self->saddr, daddr, |
| 2308 | ias_opt->irda_class_name, |
| 2309 | ias_opt->irda_attrib_name); |
| 2310 | |
| 2311 | /* Wait for answer, if not yet finished (or failed) */ |
| 2312 | if (wait_event_interruptible(self->query_wait, |
| 2313 | (self->iriap == NULL))) { |
| 2314 | /* pending request uses copy of ias_opt-content |
| 2315 | * we can free it regardless! */ |
| 2316 | kfree(ias_opt); |
| 2317 | /* Treat signals as disconnect */ |
| 2318 | return -EHOSTUNREACH; |
| 2319 | } |
| 2320 | |
| 2321 | /* Check what happened */ |
| 2322 | if (self->errno) |
| 2323 | { |
| 2324 | kfree(ias_opt); |
| 2325 | /* Requested object/attribute doesn't exist */ |
| 2326 | if((self->errno == IAS_CLASS_UNKNOWN) || |
| 2327 | (self->errno == IAS_ATTRIB_UNKNOWN)) |
| 2328 | return (-EADDRNOTAVAIL); |
| 2329 | else |
| 2330 | return (-EHOSTUNREACH); |
| 2331 | } |
| 2332 | |
| 2333 | /* Translate from internal to user structure */ |
| 2334 | err = irda_extract_ias_value(ias_opt, self->ias_result); |
| 2335 | if (self->ias_result) |
| 2336 | irias_delete_value(self->ias_result); |
| 2337 | if (err) { |
| 2338 | kfree(ias_opt); |
| 2339 | return err; |
| 2340 | } |
| 2341 | |
| 2342 | /* Copy reply to the user */ |
| 2343 | if (copy_to_user(optval, ias_opt, |
| 2344 | sizeof(struct irda_ias_set))) { |
| 2345 | kfree(ias_opt); |
| 2346 | return -EFAULT; |
| 2347 | } |
| 2348 | /* Note : don't need to put optlen, we checked it */ |
| 2349 | kfree(ias_opt); |
| 2350 | break; |
| 2351 | case IRLMP_WAITDEVICE: |
| 2352 | /* This function is just another way of seeing life ;-) |
| 2353 | * IRLMP_ENUMDEVICES assumes that you have a static network, |
| 2354 | * and that you just want to pick one of the devices present. |
| 2355 | * On the other hand, in here we assume that no device is |
| 2356 | * present and that at some point in the future a device will |
| 2357 | * come into range. When this device arrive, we just wake |
| 2358 | * up the caller, so that he has time to connect to it before |
| 2359 | * the device goes away... |
| 2360 | * Note : once the node has been discovered for more than a |
| 2361 | * few second, it won't trigger this function, unless it |
| 2362 | * goes away and come back changes its hint bits (so we |
| 2363 | * might call it IRLMP_WAITNEWDEVICE). |
| 2364 | */ |
| 2365 | |
| 2366 | /* Check that the user is passing us an int */ |
| 2367 | if (len != sizeof(int)) |
| 2368 | return -EINVAL; |
| 2369 | /* Get timeout in ms (max time we block the caller) */ |
| 2370 | if (get_user(val, (int __user *)optval)) |
| 2371 | return -EFAULT; |
| 2372 | |
| 2373 | /* Tell IrLMP we want to be notified */ |
| 2374 | irlmp_update_client(self->ckey, self->mask.word, |
| 2375 | irda_selective_discovery_indication, |
| 2376 | NULL, (void *) self); |
| 2377 | |
| 2378 | /* Do some discovery (and also return cached results) */ |
| 2379 | irlmp_discovery_request(self->nslots); |
| 2380 | |
| 2381 | /* Wait until a node is discovered */ |
| 2382 | if (!self->cachedaddr) { |
| 2383 | int ret = 0; |
| 2384 | |
| 2385 | IRDA_DEBUG(1, "%s(), nothing discovered yet, going to sleep...\n", __FUNCTION__); |
| 2386 | |
| 2387 | /* Set watchdog timer to expire in <val> ms. */ |
| 2388 | self->errno = 0; |
| 2389 | init_timer(&self->watchdog); |
| 2390 | self->watchdog.function = irda_discovery_timeout; |
| 2391 | self->watchdog.data = (unsigned long) self; |
| 2392 | self->watchdog.expires = jiffies + (val * HZ/1000); |
| 2393 | add_timer(&(self->watchdog)); |
| 2394 | |
| 2395 | /* Wait for IR-LMP to call us back */ |
| 2396 | __wait_event_interruptible(self->query_wait, |
| 2397 | (self->cachedaddr != 0 || self->errno == -ETIME), |
| 2398 | ret); |
| 2399 | |
| 2400 | /* If watchdog is still activated, kill it! */ |
| 2401 | if(timer_pending(&(self->watchdog))) |
| 2402 | del_timer(&(self->watchdog)); |
| 2403 | |
| 2404 | IRDA_DEBUG(1, "%s(), ...waking up !\n", __FUNCTION__); |
| 2405 | |
| 2406 | if (ret != 0) |
| 2407 | return ret; |
| 2408 | } |
| 2409 | else |
| 2410 | IRDA_DEBUG(1, "%s(), found immediately !\n", |
| 2411 | __FUNCTION__); |
| 2412 | |
| 2413 | /* Tell IrLMP that we have been notified */ |
| 2414 | irlmp_update_client(self->ckey, self->mask.word, |
| 2415 | NULL, NULL, NULL); |
| 2416 | |
| 2417 | /* Check if the we got some results */ |
| 2418 | if (!self->cachedaddr) |
| 2419 | return -EAGAIN; /* Didn't find any devices */ |
| 2420 | daddr = self->cachedaddr; |
| 2421 | /* Cleanup */ |
| 2422 | self->cachedaddr = 0; |
| 2423 | |
| 2424 | /* We return the daddr of the device that trigger the |
| 2425 | * wakeup. As irlmp pass us only the new devices, we |
| 2426 | * are sure that it's not an old device. |
| 2427 | * If the user want more details, he should query |
| 2428 | * the whole discovery log and pick one device... |
| 2429 | */ |
| 2430 | if (put_user(daddr, (int __user *)optval)) |
| 2431 | return -EFAULT; |
| 2432 | |
| 2433 | break; |
| 2434 | default: |
| 2435 | return -ENOPROTOOPT; |
| 2436 | } |
| 2437 | |
| 2438 | return 0; |
| 2439 | } |
| 2440 | |
| 2441 | static struct net_proto_family irda_family_ops = { |
| 2442 | .family = PF_IRDA, |
| 2443 | .create = irda_create, |
| 2444 | .owner = THIS_MODULE, |
| 2445 | }; |
| 2446 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 2447 | static const struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | .family = PF_IRDA, |
| 2449 | .owner = THIS_MODULE, |
| 2450 | .release = irda_release, |
| 2451 | .bind = irda_bind, |
| 2452 | .connect = irda_connect, |
| 2453 | .socketpair = sock_no_socketpair, |
| 2454 | .accept = irda_accept, |
| 2455 | .getname = irda_getname, |
| 2456 | .poll = irda_poll, |
| 2457 | .ioctl = irda_ioctl, |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 2458 | #ifdef CONFIG_COMPAT |
| 2459 | .compat_ioctl = irda_compat_ioctl, |
| 2460 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | .listen = irda_listen, |
| 2462 | .shutdown = irda_shutdown, |
| 2463 | .setsockopt = irda_setsockopt, |
| 2464 | .getsockopt = irda_getsockopt, |
| 2465 | .sendmsg = irda_sendmsg, |
| 2466 | .recvmsg = irda_recvmsg_stream, |
| 2467 | .mmap = sock_no_mmap, |
| 2468 | .sendpage = sock_no_sendpage, |
| 2469 | }; |
| 2470 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 2471 | static const struct proto_ops SOCKOPS_WRAPPED(irda_seqpacket_ops) = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | .family = PF_IRDA, |
| 2473 | .owner = THIS_MODULE, |
| 2474 | .release = irda_release, |
| 2475 | .bind = irda_bind, |
| 2476 | .connect = irda_connect, |
| 2477 | .socketpair = sock_no_socketpair, |
| 2478 | .accept = irda_accept, |
| 2479 | .getname = irda_getname, |
| 2480 | .poll = datagram_poll, |
| 2481 | .ioctl = irda_ioctl, |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 2482 | #ifdef CONFIG_COMPAT |
| 2483 | .compat_ioctl = irda_compat_ioctl, |
| 2484 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | .listen = irda_listen, |
| 2486 | .shutdown = irda_shutdown, |
| 2487 | .setsockopt = irda_setsockopt, |
| 2488 | .getsockopt = irda_getsockopt, |
| 2489 | .sendmsg = irda_sendmsg, |
| 2490 | .recvmsg = irda_recvmsg_dgram, |
| 2491 | .mmap = sock_no_mmap, |
| 2492 | .sendpage = sock_no_sendpage, |
| 2493 | }; |
| 2494 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 2495 | static const struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | .family = PF_IRDA, |
| 2497 | .owner = THIS_MODULE, |
| 2498 | .release = irda_release, |
| 2499 | .bind = irda_bind, |
| 2500 | .connect = irda_connect, |
| 2501 | .socketpair = sock_no_socketpair, |
| 2502 | .accept = irda_accept, |
| 2503 | .getname = irda_getname, |
| 2504 | .poll = datagram_poll, |
| 2505 | .ioctl = irda_ioctl, |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 2506 | #ifdef CONFIG_COMPAT |
| 2507 | .compat_ioctl = irda_compat_ioctl, |
| 2508 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | .listen = irda_listen, |
| 2510 | .shutdown = irda_shutdown, |
| 2511 | .setsockopt = irda_setsockopt, |
| 2512 | .getsockopt = irda_getsockopt, |
| 2513 | .sendmsg = irda_sendmsg_dgram, |
| 2514 | .recvmsg = irda_recvmsg_dgram, |
| 2515 | .mmap = sock_no_mmap, |
| 2516 | .sendpage = sock_no_sendpage, |
| 2517 | }; |
| 2518 | |
| 2519 | #ifdef CONFIG_IRDA_ULTRA |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 2520 | static const struct proto_ops SOCKOPS_WRAPPED(irda_ultra_ops) = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | .family = PF_IRDA, |
| 2522 | .owner = THIS_MODULE, |
| 2523 | .release = irda_release, |
| 2524 | .bind = irda_bind, |
| 2525 | .connect = sock_no_connect, |
| 2526 | .socketpair = sock_no_socketpair, |
| 2527 | .accept = sock_no_accept, |
| 2528 | .getname = irda_getname, |
| 2529 | .poll = datagram_poll, |
| 2530 | .ioctl = irda_ioctl, |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 2531 | #ifdef CONFIG_COMPAT |
| 2532 | .compat_ioctl = irda_compat_ioctl, |
| 2533 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | .listen = sock_no_listen, |
| 2535 | .shutdown = irda_shutdown, |
| 2536 | .setsockopt = irda_setsockopt, |
| 2537 | .getsockopt = irda_getsockopt, |
| 2538 | .sendmsg = irda_sendmsg_ultra, |
| 2539 | .recvmsg = irda_recvmsg_dgram, |
| 2540 | .mmap = sock_no_mmap, |
| 2541 | .sendpage = sock_no_sendpage, |
| 2542 | }; |
| 2543 | #endif /* CONFIG_IRDA_ULTRA */ |
| 2544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | SOCKOPS_WRAP(irda_stream, PF_IRDA); |
| 2546 | SOCKOPS_WRAP(irda_seqpacket, PF_IRDA); |
| 2547 | SOCKOPS_WRAP(irda_dgram, PF_IRDA); |
| 2548 | #ifdef CONFIG_IRDA_ULTRA |
| 2549 | SOCKOPS_WRAP(irda_ultra, PF_IRDA); |
| 2550 | #endif /* CONFIG_IRDA_ULTRA */ |
| 2551 | |
| 2552 | /* |
| 2553 | * Function irsock_init (pro) |
| 2554 | * |
| 2555 | * Initialize IrDA protocol |
| 2556 | * |
| 2557 | */ |
| 2558 | int __init irsock_init(void) |
| 2559 | { |
| 2560 | int rc = proto_register(&irda_proto, 0); |
| 2561 | |
| 2562 | if (rc == 0) |
| 2563 | rc = sock_register(&irda_family_ops); |
| 2564 | |
| 2565 | return rc; |
| 2566 | } |
| 2567 | |
| 2568 | /* |
| 2569 | * Function irsock_cleanup (void) |
| 2570 | * |
| 2571 | * Remove IrDA protocol |
| 2572 | * |
| 2573 | */ |
Samuel Ortiz | 75a69ac | 2007-07-18 02:16:30 -0700 | [diff] [blame] | 2574 | void irsock_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | { |
| 2576 | sock_unregister(PF_IRDA); |
| 2577 | proto_unregister(&irda_proto); |
| 2578 | } |