1 /* connection.c: Rx connection routines
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <rxrpc/rxrpc.h>
16 #include <rxrpc/transport.h>
17 #include <rxrpc/peer.h>
18 #include <rxrpc/connection.h>
19 #include <rxrpc/call.h>
20 #include <rxrpc/message.h>
21 #include <linux/udp.h>
24 #include <asm/uaccess.h>
27 __RXACCT_DECL(atomic_t rxrpc_connection_count
);
29 LIST_HEAD(rxrpc_conns
);
30 DECLARE_RWSEM(rxrpc_conns_sem
);
31 unsigned long rxrpc_conn_timeout
= 60 * 60;
33 static void rxrpc_conn_do_timeout(struct rxrpc_connection
*conn
);
35 static void __rxrpc_conn_timeout(rxrpc_timer_t
*timer
)
37 struct rxrpc_connection
*conn
=
38 list_entry(timer
, struct rxrpc_connection
, timeout
);
40 _debug("Rx CONN TIMEOUT [%p{u=%d}]", conn
, atomic_read(&conn
->usage
));
42 rxrpc_conn_do_timeout(conn
);
45 static const struct rxrpc_timer_ops rxrpc_conn_timer_ops
= {
46 .timed_out
= __rxrpc_conn_timeout
,
49 /*****************************************************************************/
51 * create a new connection record
53 static inline int __rxrpc_create_connection(struct rxrpc_peer
*peer
,
54 struct rxrpc_connection
**_conn
)
56 struct rxrpc_connection
*conn
;
60 /* allocate and initialise a connection record */
61 conn
= kmalloc(sizeof(struct rxrpc_connection
), GFP_KERNEL
);
67 memset(conn
, 0, sizeof(struct rxrpc_connection
));
68 atomic_set(&conn
->usage
, 1);
70 INIT_LIST_HEAD(&conn
->link
);
71 INIT_LIST_HEAD(&conn
->id_link
);
72 init_waitqueue_head(&conn
->chanwait
);
73 spin_lock_init(&conn
->lock
);
74 rxrpc_timer_init(&conn
->timeout
, &rxrpc_conn_timer_ops
);
76 do_gettimeofday(&conn
->atime
);
77 conn
->mtu_size
= 1024;
79 conn
->trans
= peer
->trans
;
81 __RXACCT(atomic_inc(&rxrpc_connection_count
));
83 _leave(" = 0 (%p)", conn
);
86 } /* end __rxrpc_create_connection() */
88 /*****************************************************************************/
90 * create a new connection record for outgoing connections
92 int rxrpc_create_connection(struct rxrpc_transport
*trans
,
97 struct rxrpc_connection
**_conn
)
99 struct rxrpc_connection
*candidate
, *conn
;
100 struct rxrpc_peer
*peer
;
101 struct list_head
*_p
;
105 _enter("%p{%hu},%u,%hu", trans
, trans
->port
, ntohs(port
), service_id
);
107 /* get a peer record */
108 ret
= rxrpc_peer_lookup(trans
, addr
, &peer
);
110 _leave(" = %d", ret
);
114 /* allocate and initialise a connection record */
115 ret
= __rxrpc_create_connection(peer
, &candidate
);
117 rxrpc_put_peer(peer
);
118 _leave(" = %d", ret
);
122 /* fill in the specific bits */
123 candidate
->addr
.sin_family
= AF_INET
;
124 candidate
->addr
.sin_port
= port
;
125 candidate
->addr
.sin_addr
.s_addr
= addr
;
127 candidate
->in_epoch
= rxrpc_epoch
;
128 candidate
->out_epoch
= rxrpc_epoch
;
129 candidate
->in_clientflag
= 0;
130 candidate
->out_clientflag
= RXRPC_CLIENT_INITIATED
;
131 candidate
->service_id
= htons(service_id
);
133 /* invent a unique connection ID */
134 write_lock(&peer
->conn_idlock
);
137 connid
= htonl(peer
->conn_idcounter
& RXRPC_CIDMASK
);
138 peer
->conn_idcounter
+= RXRPC_MAXCALLS
;
140 list_for_each(_p
, &peer
->conn_idlist
) {
141 conn
= list_entry(_p
, struct rxrpc_connection
, id_link
);
142 if (connid
== conn
->conn_id
)
144 if (connid
> conn
->conn_id
)
148 _debug("selected candidate conn ID %x.%u",
149 ntohl(peer
->addr
.s_addr
), ntohl(connid
));
151 candidate
->conn_id
= connid
;
152 list_add_tail(&candidate
->id_link
, _p
);
154 write_unlock(&peer
->conn_idlock
);
157 candidate
->peer
= peer
;
159 write_lock(&peer
->conn_lock
);
161 /* search the peer's transport graveyard list */
162 spin_lock(&peer
->conn_gylock
);
163 list_for_each(_p
, &peer
->conn_graveyard
) {
164 conn
= list_entry(_p
, struct rxrpc_connection
, link
);
165 if (conn
->addr
.sin_port
== candidate
->addr
.sin_port
&&
166 conn
->security_ix
== candidate
->security_ix
&&
167 conn
->service_id
== candidate
->service_id
&&
168 conn
->in_clientflag
== 0)
169 goto found_in_graveyard
;
171 spin_unlock(&peer
->conn_gylock
);
173 /* pick the new candidate */
174 _debug("created connection: {%08x} [out]", ntohl(candidate
->conn_id
));
175 atomic_inc(&peer
->conn_count
);
180 list_add_tail(&conn
->link
, &peer
->conn_active
);
181 write_unlock(&peer
->conn_lock
);
184 write_lock(&peer
->conn_idlock
);
185 list_del(&candidate
->id_link
);
186 write_unlock(&peer
->conn_idlock
);
188 __RXACCT(atomic_dec(&rxrpc_connection_count
));
192 down_write(&rxrpc_conns_sem
);
193 list_add_tail(&conn
->proc_link
, &rxrpc_conns
);
194 up_write(&rxrpc_conns_sem
);
198 _leave(" = 0 (%p)", conn
);
202 /* handle resurrecting a connection from the graveyard */
204 _debug("resurrecting connection: {%08x} [out]", ntohl(conn
->conn_id
));
205 rxrpc_get_connection(conn
);
206 rxrpc_krxtimod_del_timer(&conn
->timeout
);
207 list_del_init(&conn
->link
);
208 spin_unlock(&peer
->conn_gylock
);
210 } /* end rxrpc_create_connection() */
212 /*****************************************************************************/
214 * lookup the connection for an incoming packet
215 * - create a new connection record for unrecorded incoming connections
217 int rxrpc_connection_lookup(struct rxrpc_peer
*peer
,
218 struct rxrpc_message
*msg
,
219 struct rxrpc_connection
**_conn
)
221 struct rxrpc_connection
*conn
, *candidate
= NULL
;
222 struct list_head
*_p
;
223 struct sk_buff
*pkt
= msg
->pkt
;
225 __be32 x_epoch
, x_connid
;
226 __be16 x_port
, x_servid
;
230 _enter("%p{{%hu}},%u,%hu",
233 ntohs(pkt
->h
.uh
->source
),
234 ntohs(msg
->hdr
.serviceId
));
236 x_port
= pkt
->h
.uh
->source
;
237 x_epoch
= msg
->hdr
.epoch
;
238 x_clflag
= msg
->hdr
.flags
& RXRPC_CLIENT_INITIATED
;
239 x_connid
= htonl(ntohl(msg
->hdr
.cid
) & RXRPC_CIDMASK
);
240 x_servid
= msg
->hdr
.serviceId
;
241 x_secix
= msg
->hdr
.securityIndex
;
243 /* [common case] search the transport's active list first */
244 read_lock(&peer
->conn_lock
);
245 list_for_each(_p
, &peer
->conn_active
) {
246 conn
= list_entry(_p
, struct rxrpc_connection
, link
);
247 if (conn
->addr
.sin_port
== x_port
&&
248 conn
->in_epoch
== x_epoch
&&
249 conn
->conn_id
== x_connid
&&
250 conn
->security_ix
== x_secix
&&
251 conn
->service_id
== x_servid
&&
252 conn
->in_clientflag
== x_clflag
)
255 read_unlock(&peer
->conn_lock
);
257 /* [uncommon case] not active
258 * - create a candidate for a new record if an inbound connection
259 * - only examine the graveyard for an outbound connection
262 ret
= __rxrpc_create_connection(peer
, &candidate
);
264 _leave(" = %d", ret
);
268 /* fill in the specifics */
269 candidate
->addr
.sin_family
= AF_INET
;
270 candidate
->addr
.sin_port
= x_port
;
271 candidate
->addr
.sin_addr
.s_addr
= pkt
->nh
.iph
->saddr
;
272 candidate
->in_epoch
= x_epoch
;
273 candidate
->out_epoch
= x_epoch
;
274 candidate
->in_clientflag
= RXRPC_CLIENT_INITIATED
;
275 candidate
->out_clientflag
= 0;
276 candidate
->conn_id
= x_connid
;
277 candidate
->service_id
= x_servid
;
278 candidate
->security_ix
= x_secix
;
281 /* search the active list again, just in case it appeared whilst we
283 write_lock(&peer
->conn_lock
);
284 list_for_each(_p
, &peer
->conn_active
) {
285 conn
= list_entry(_p
, struct rxrpc_connection
, link
);
286 if (conn
->addr
.sin_port
== x_port
&&
287 conn
->in_epoch
== x_epoch
&&
288 conn
->conn_id
== x_connid
&&
289 conn
->security_ix
== x_secix
&&
290 conn
->service_id
== x_servid
&&
291 conn
->in_clientflag
== x_clflag
)
292 goto found_active_second_chance
;
295 /* search the transport's graveyard list */
296 spin_lock(&peer
->conn_gylock
);
297 list_for_each(_p
, &peer
->conn_graveyard
) {
298 conn
= list_entry(_p
, struct rxrpc_connection
, link
);
299 if (conn
->addr
.sin_port
== x_port
&&
300 conn
->in_epoch
== x_epoch
&&
301 conn
->conn_id
== x_connid
&&
302 conn
->security_ix
== x_secix
&&
303 conn
->service_id
== x_servid
&&
304 conn
->in_clientflag
== x_clflag
)
305 goto found_in_graveyard
;
307 spin_unlock(&peer
->conn_gylock
);
309 /* outbound connections aren't created here */
311 write_unlock(&peer
->conn_lock
);
312 _leave(" = -ENOENT");
316 /* we can now add the new candidate to the list */
317 _debug("created connection: {%08x} [in]", ntohl(candidate
->conn_id
));
318 rxrpc_get_peer(peer
);
321 atomic_inc(&peer
->conn_count
);
325 list_add_tail(&conn
->link
, &peer
->conn_active
);
328 write_unlock(&peer
->conn_lock
);
331 write_lock(&peer
->conn_idlock
);
332 list_del(&candidate
->id_link
);
333 write_unlock(&peer
->conn_idlock
);
335 __RXACCT(atomic_dec(&rxrpc_connection_count
));
340 down_write(&rxrpc_conns_sem
);
341 list_add_tail(&conn
->proc_link
, &rxrpc_conns
);
342 up_write(&rxrpc_conns_sem
);
347 _leave(" = 0 (%p)", conn
);
350 /* handle the connection being found in the active list straight off */
352 rxrpc_get_connection(conn
);
353 read_unlock(&peer
->conn_lock
);
356 /* handle resurrecting a connection from the graveyard */
358 _debug("resurrecting connection: {%08x} [in]", ntohl(conn
->conn_id
));
359 rxrpc_get_peer(peer
);
360 rxrpc_get_connection(conn
);
361 rxrpc_krxtimod_del_timer(&conn
->timeout
);
362 list_del_init(&conn
->link
);
363 spin_unlock(&peer
->conn_gylock
);
366 /* handle finding the connection on the second time through the active
368 found_active_second_chance
:
369 rxrpc_get_connection(conn
);
372 } /* end rxrpc_connection_lookup() */
374 /*****************************************************************************/
376 * finish using a connection record
377 * - it will be transferred to the peer's connection graveyard when refcount
380 void rxrpc_put_connection(struct rxrpc_connection
*conn
)
382 struct rxrpc_peer
*peer
;
387 _enter("%p{u=%d p=%hu}",
388 conn
, atomic_read(&conn
->usage
), ntohs(conn
->addr
.sin_port
));
391 spin_lock(&peer
->conn_gylock
);
394 if (atomic_read(&conn
->usage
) <= 0)
397 if (likely(!atomic_dec_and_test(&conn
->usage
))) {
398 spin_unlock(&peer
->conn_gylock
);
403 /* move to graveyard queue */
404 _debug("burying connection: {%08x}", ntohl(conn
->conn_id
));
405 list_del(&conn
->link
);
406 list_add_tail(&conn
->link
, &peer
->conn_graveyard
);
408 rxrpc_krxtimod_add_timer(&conn
->timeout
, rxrpc_conn_timeout
* HZ
);
410 spin_unlock(&peer
->conn_gylock
);
412 rxrpc_put_peer(conn
->peer
);
415 } /* end rxrpc_put_connection() */
417 /*****************************************************************************/
419 * free a connection record
421 static void rxrpc_conn_do_timeout(struct rxrpc_connection
*conn
)
423 struct rxrpc_peer
*peer
;
425 _enter("%p{u=%d p=%hu}",
426 conn
, atomic_read(&conn
->usage
), ntohs(conn
->addr
.sin_port
));
430 if (atomic_read(&conn
->usage
) < 0)
433 /* remove from graveyard if still dead */
434 spin_lock(&peer
->conn_gylock
);
435 if (atomic_read(&conn
->usage
) == 0) {
436 list_del_init(&conn
->link
);
441 spin_unlock(&peer
->conn_gylock
);
445 return; /* resurrected */
448 _debug("--- Destroying Connection %p{%08x} ---",
449 conn
, ntohl(conn
->conn_id
));
451 down_write(&rxrpc_conns_sem
);
452 list_del(&conn
->proc_link
);
453 up_write(&rxrpc_conns_sem
);
455 write_lock(&peer
->conn_idlock
);
456 list_del(&conn
->id_link
);
457 write_unlock(&peer
->conn_idlock
);
459 __RXACCT(atomic_dec(&rxrpc_connection_count
));
462 /* if the graveyard is now empty, wake up anyone waiting for that */
463 if (atomic_dec_and_test(&peer
->conn_count
))
464 wake_up(&peer
->conn_gy_waitq
);
466 _leave(" [destroyed]");
467 } /* end rxrpc_conn_do_timeout() */
469 /*****************************************************************************/
471 * clear all connection records from a peer endpoint
473 void rxrpc_conn_clearall(struct rxrpc_peer
*peer
)
475 DECLARE_WAITQUEUE(myself
, current
);
477 struct rxrpc_connection
*conn
;
482 /* there shouldn't be any active conns remaining */
483 if (!list_empty(&peer
->conn_active
))
486 /* manually timeout all conns in the graveyard */
487 spin_lock(&peer
->conn_gylock
);
488 while (!list_empty(&peer
->conn_graveyard
)) {
489 conn
= list_entry(peer
->conn_graveyard
.next
,
490 struct rxrpc_connection
, link
);
491 err
= rxrpc_krxtimod_del_timer(&conn
->timeout
);
492 spin_unlock(&peer
->conn_gylock
);
495 rxrpc_conn_do_timeout(conn
);
497 spin_lock(&peer
->conn_gylock
);
499 spin_unlock(&peer
->conn_gylock
);
501 /* wait for the the conn graveyard to be completely cleared */
502 set_current_state(TASK_UNINTERRUPTIBLE
);
503 add_wait_queue(&peer
->conn_gy_waitq
, &myself
);
505 while (atomic_read(&peer
->conn_count
) != 0) {
507 set_current_state(TASK_UNINTERRUPTIBLE
);
510 remove_wait_queue(&peer
->conn_gy_waitq
, &myself
);
511 set_current_state(TASK_RUNNING
);
514 } /* end rxrpc_conn_clearall() */
516 /*****************************************************************************/
518 * allocate and prepare a message for sending out through the transport
521 int rxrpc_conn_newmsg(struct rxrpc_connection
*conn
,
522 struct rxrpc_call
*call
,
527 struct rxrpc_message
**_msg
)
529 struct rxrpc_message
*msg
;
532 _enter("%p{%d},%p,%u", conn
, ntohs(conn
->addr
.sin_port
), call
, type
);
535 _leave(" = -EINVAL");
539 msg
= kmalloc(sizeof(struct rxrpc_message
), alloc_flags
);
541 _leave(" = -ENOMEM");
545 memset(msg
, 0, sizeof(*msg
));
546 atomic_set(&msg
->usage
, 1);
548 INIT_LIST_HEAD(&msg
->link
);
550 msg
->state
= RXRPC_MSG_PREPARED
;
552 msg
->hdr
.epoch
= conn
->out_epoch
;
553 msg
->hdr
.cid
= conn
->conn_id
| (call
? call
->chan_ix
: 0);
554 msg
->hdr
.callNumber
= call
? call
->call_id
: 0;
555 msg
->hdr
.type
= type
;
556 msg
->hdr
.flags
= conn
->out_clientflag
;
557 msg
->hdr
.securityIndex
= conn
->security_ix
;
558 msg
->hdr
.serviceId
= conn
->service_id
;
560 /* generate sequence numbers for data packets */
563 case RXRPC_PACKET_TYPE_DATA
:
564 msg
->seq
= ++call
->snd_seq_count
;
565 msg
->hdr
.seq
= htonl(msg
->seq
);
567 case RXRPC_PACKET_TYPE_ACK
:
568 /* ACK sequence numbers are complicated. The following
570 * - jumbo packet ACKs should have a seq number
571 * - normal ACKs should not
578 msg
->dcount
= dcount
+ 1;
579 msg
->dsize
= sizeof(msg
->hdr
);
580 msg
->data
[0].iov_len
= sizeof(msg
->hdr
);
581 msg
->data
[0].iov_base
= &msg
->hdr
;
583 for (loop
=0; loop
< dcount
; loop
++) {
584 msg
->dsize
+= diov
[loop
].iov_len
;
585 msg
->data
[loop
+1].iov_len
= diov
[loop
].iov_len
;
586 msg
->data
[loop
+1].iov_base
= diov
[loop
].iov_base
;
589 __RXACCT(atomic_inc(&rxrpc_message_count
));
591 _leave(" = 0 (%p) #%d", msg
, atomic_read(&rxrpc_message_count
));
593 } /* end rxrpc_conn_newmsg() */
595 /*****************************************************************************/
599 void __rxrpc_put_message(struct rxrpc_message
*msg
)
603 _enter("%p #%d", msg
, atomic_read(&rxrpc_message_count
));
607 rxrpc_put_connection(msg
->conn
);
609 for (loop
= 0; loop
< 8; loop
++)
610 if (test_bit(loop
, &msg
->dfree
))
611 kfree(msg
->data
[loop
].iov_base
);
613 __RXACCT(atomic_dec(&rxrpc_message_count
));
617 } /* end __rxrpc_put_message() */
619 /*****************************************************************************/
621 * send a message out through the transport endpoint
623 int rxrpc_conn_sendmsg(struct rxrpc_connection
*conn
,
624 struct rxrpc_message
*msg
)
626 struct msghdr msghdr
;
629 _enter("%p{%d}", conn
, ntohs(conn
->addr
.sin_port
));
631 /* fill in some fields in the header */
632 spin_lock(&conn
->lock
);
633 msg
->hdr
.serial
= htonl(++conn
->serial_counter
);
635 spin_unlock(&conn
->lock
);
637 /* set up the message to be transmitted */
638 msghdr
.msg_name
= &conn
->addr
;
639 msghdr
.msg_namelen
= sizeof(conn
->addr
);
640 msghdr
.msg_control
= NULL
;
641 msghdr
.msg_controllen
= 0;
642 msghdr
.msg_flags
= MSG_CONFIRM
| MSG_DONTWAIT
;
644 _net("Sending message type %d of %Zd bytes to %08x:%d",
647 ntohl(conn
->addr
.sin_addr
.s_addr
),
648 ntohs(conn
->addr
.sin_port
));
650 /* send the message */
651 ret
= kernel_sendmsg(conn
->trans
->socket
, &msghdr
,
652 msg
->data
, msg
->dcount
, msg
->dsize
);
654 msg
->state
= RXRPC_MSG_ERROR
;
656 msg
->state
= RXRPC_MSG_SENT
;
659 spin_lock(&conn
->lock
);
660 do_gettimeofday(&conn
->atime
);
661 msg
->stamp
= conn
->atime
;
662 spin_unlock(&conn
->lock
);
665 _leave(" = %d", ret
);
668 } /* end rxrpc_conn_sendmsg() */
670 /*****************************************************************************/
672 * deal with a subsequent call packet
674 int rxrpc_conn_receive_call_packet(struct rxrpc_connection
*conn
,
675 struct rxrpc_call
*call
,
676 struct rxrpc_message
*msg
)
678 struct rxrpc_message
*pmsg
;
679 struct dst_entry
*dst
;
680 struct list_head
*_p
;
684 _enter("%p,%p,%p", conn
, call
, msg
);
687 cix
= ntohl(msg
->hdr
.cid
) & RXRPC_CHANNELMASK
;
689 spin_lock(&conn
->lock
);
690 call
= conn
->channels
[cix
];
692 if (!call
|| call
->call_id
!= msg
->hdr
.callNumber
) {
693 spin_unlock(&conn
->lock
);
694 rxrpc_trans_immediate_abort(conn
->trans
, msg
, -ENOENT
);
698 rxrpc_get_call(call
);
699 spin_unlock(&conn
->lock
);
703 rxrpc_get_call(call
);
706 _proto("Received packet %%%u [%u] on call %hu:%u:%u",
707 ntohl(msg
->hdr
.serial
),
709 ntohs(msg
->hdr
.serviceId
),
710 ntohl(conn
->conn_id
),
711 ntohl(call
->call_id
));
713 call
->pkt_rcv_count
++;
718 dst
->dev
->mtu
- dst
->dev
->hard_header_len
;
720 /* queue on the call in seq order */
721 rxrpc_get_message(msg
);
724 spin_lock(&call
->lock
);
725 list_for_each(_p
, &call
->rcv_receiveq
) {
726 pmsg
= list_entry(_p
, struct rxrpc_message
, link
);
730 list_add_tail(&msg
->link
, _p
);
732 /* reset the activity timeout */
733 call
->flags
|= RXRPC_CALL_RCV_PKT
;
734 mod_timer(&call
->rcv_timeout
,jiffies
+ rxrpc_call_rcv_timeout
* HZ
);
736 spin_unlock(&call
->lock
);
738 rxrpc_krxiod_queue_call(call
);
740 rxrpc_put_call(call
);
742 _leave(" = %d", ret
);
744 } /* end rxrpc_conn_receive_call_packet() */
746 /*****************************************************************************/
748 * handle an ICMP error being applied to a connection
750 void rxrpc_conn_handle_error(struct rxrpc_connection
*conn
,
751 int local
, int errno
)
753 struct rxrpc_call
*calls
[4];
756 _enter("%p{%d},%d", conn
, ntohs(conn
->addr
.sin_port
), errno
);
758 /* get a ref to all my calls in one go */
759 memset(calls
, 0, sizeof(calls
));
760 spin_lock(&conn
->lock
);
762 for (loop
= 3; loop
>= 0; loop
--) {
763 if (conn
->channels
[loop
]) {
764 calls
[loop
] = conn
->channels
[loop
];
765 rxrpc_get_call(calls
[loop
]);
769 spin_unlock(&conn
->lock
);
771 /* now kick them all */
772 for (loop
= 3; loop
>= 0; loop
--) {
774 rxrpc_call_handle_error(calls
[loop
], local
, errno
);
775 rxrpc_put_call(calls
[loop
]);
780 } /* end rxrpc_conn_handle_error() */