1 /* transport.c: Rx Transport 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/transport.h>
16 #include <rxrpc/peer.h>
17 #include <rxrpc/connection.h>
18 #include <rxrpc/call.h>
19 #include <rxrpc/message.h>
20 #include <rxrpc/krxiod.h>
21 #include <rxrpc/krxsecd.h>
22 #include <linux/udp.h>
24 #include <linux/in6.h>
25 #include <linux/icmp.h>
26 #include <linux/skbuff.h>
29 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
30 #include <linux/ipv6.h> /* this should _really_ be in errqueue.h.. */
32 #include <linux/errqueue.h>
33 #include <asm/uaccess.h>
37 struct cmsghdr cmsg; /* control message header */
38 struct sock_extended_err ee; /* extended error information */
39 struct sockaddr_in icmp_src; /* ICMP packet source address */
42 static DEFINE_SPINLOCK(rxrpc_transports_lock);
43 static struct list_head rxrpc_transports = LIST_HEAD_INIT(rxrpc_transports);
45 __RXACCT_DECL(atomic_t rxrpc_transport_count);
46 LIST_HEAD(rxrpc_proc_transports);
47 DECLARE_RWSEM(rxrpc_proc_transports_sem);
49 static void rxrpc_data_ready(struct sock *sk, int count);
50 static void rxrpc_error_report(struct sock *sk);
51 static int rxrpc_trans_receive_new_call(struct rxrpc_transport *trans,
52 struct list_head *msgq);
53 static void rxrpc_trans_receive_error_report(struct rxrpc_transport *trans);
55 /*****************************************************************************/
57 * create a new transport endpoint using the specified UDP port
59 int rxrpc_create_transport(unsigned short port,
60 struct rxrpc_transport **_trans)
62 struct rxrpc_transport *trans;
63 struct sockaddr_in sin;
70 trans = kzalloc(sizeof(struct rxrpc_transport), GFP_KERNEL);
74 atomic_set(&trans->usage, 1);
75 INIT_LIST_HEAD(&trans->services);
76 INIT_LIST_HEAD(&trans->link);
77 INIT_LIST_HEAD(&trans->krxiodq_link);
78 spin_lock_init(&trans->lock);
79 INIT_LIST_HEAD(&trans->peer_active);
80 INIT_LIST_HEAD(&trans->peer_graveyard);
81 spin_lock_init(&trans->peer_gylock);
82 init_waitqueue_head(&trans->peer_gy_waitq);
83 rwlock_init(&trans->peer_lock);
84 atomic_set(&trans->peer_count, 0);
87 /* create a UDP socket to be my actual transport endpoint */
88 ret = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &trans->socket);
92 /* use the specified port */
94 memset(&sin, 0, sizeof(sin));
95 sin.sin_family = AF_INET;
96 sin.sin_port = htons(port);
97 ret = trans->socket->ops->bind(trans->socket,
98 (struct sockaddr *) &sin,
107 ret = trans->socket->ops->setsockopt(trans->socket, SOL_IP, IP_RECVERR,
108 (char *) &opt, sizeof(opt));
111 spin_lock(&rxrpc_transports_lock);
112 list_add(&trans->link, &rxrpc_transports);
113 spin_unlock(&rxrpc_transports_lock);
115 /* set the socket up */
116 sock = trans->socket->sk;
117 sock->sk_user_data = trans;
118 sock->sk_data_ready = rxrpc_data_ready;
119 sock->sk_error_report = rxrpc_error_report;
121 down_write(&rxrpc_proc_transports_sem);
122 list_add_tail(&trans->proc_link, &rxrpc_proc_transports);
123 up_write(&rxrpc_proc_transports_sem);
125 __RXACCT(atomic_inc(&rxrpc_transport_count));
128 _leave(" = 0 (%p)", trans);
132 /* finish cleaning up the transport (not really needed here, but...) */
134 trans->socket->ops->shutdown(trans->socket, 2);
136 /* close the socket */
138 trans->socket->sk->sk_user_data = NULL;
139 sock_release(trans->socket);
140 trans->socket = NULL;
146 _leave(" = %d", ret);
148 } /* end rxrpc_create_transport() */
150 /*****************************************************************************/
152 * destroy a transport endpoint
154 void rxrpc_put_transport(struct rxrpc_transport *trans)
156 _enter("%p{u=%d p=%hu}",
157 trans, atomic_read(&trans->usage), trans->port);
159 BUG_ON(atomic_read(&trans->usage) <= 0);
161 /* to prevent a race, the decrement and the dequeue must be
162 * effectively atomic */
163 spin_lock(&rxrpc_transports_lock);
164 if (likely(!atomic_dec_and_test(&trans->usage))) {
165 spin_unlock(&rxrpc_transports_lock);
170 list_del(&trans->link);
171 spin_unlock(&rxrpc_transports_lock);
173 /* finish cleaning up the transport */
175 trans->socket->ops->shutdown(trans->socket, 2);
177 rxrpc_krxsecd_clear_transport(trans);
178 rxrpc_krxiod_dequeue_transport(trans);
180 /* discard all peer information */
181 rxrpc_peer_clearall(trans);
183 down_write(&rxrpc_proc_transports_sem);
184 list_del(&trans->proc_link);
185 up_write(&rxrpc_proc_transports_sem);
186 __RXACCT(atomic_dec(&rxrpc_transport_count));
188 /* close the socket */
190 trans->socket->sk->sk_user_data = NULL;
191 sock_release(trans->socket);
192 trans->socket = NULL;
198 } /* end rxrpc_put_transport() */
200 /*****************************************************************************/
202 * add a service to a transport to be listened upon
204 int rxrpc_add_service(struct rxrpc_transport *trans,
205 struct rxrpc_service *newsrv)
207 struct rxrpc_service *srv;
208 struct list_head *_p;
211 _enter("%p{%hu},%p{%hu}",
212 trans, trans->port, newsrv, newsrv->service_id);
214 /* verify that the service ID is not already present */
215 spin_lock(&trans->lock);
217 list_for_each(_p, &trans->services) {
218 srv = list_entry(_p, struct rxrpc_service, link);
219 if (srv->service_id == newsrv->service_id)
223 /* okay - add the transport to the list */
224 list_add_tail(&newsrv->link, &trans->services);
225 rxrpc_get_transport(trans);
229 spin_unlock(&trans->lock);
233 } /* end rxrpc_add_service() */
235 /*****************************************************************************/
237 * remove a service from a transport
239 void rxrpc_del_service(struct rxrpc_transport *trans, struct rxrpc_service *srv)
241 _enter("%p{%hu},%p{%hu}", trans, trans->port, srv, srv->service_id);
243 spin_lock(&trans->lock);
244 list_del(&srv->link);
245 spin_unlock(&trans->lock);
247 rxrpc_put_transport(trans);
250 } /* end rxrpc_del_service() */
252 /*****************************************************************************/
254 * INET callback when data has been received on the socket.
256 static void rxrpc_data_ready(struct sock *sk, int count)
258 struct rxrpc_transport *trans;
260 _enter("%p{t=%p},%d", sk, sk->sk_user_data, count);
262 /* queue the transport for attention by krxiod */
263 trans = (struct rxrpc_transport *) sk->sk_user_data;
265 rxrpc_krxiod_queue_transport(trans);
267 /* wake up anyone waiting on the socket */
268 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
269 wake_up_interruptible(sk->sk_sleep);
272 } /* end rxrpc_data_ready() */
274 /*****************************************************************************/
276 * INET callback when an ICMP error packet is received
277 * - sk->err is error (EHOSTUNREACH, EPROTO or EMSGSIZE)
279 static void rxrpc_error_report(struct sock *sk)
281 struct rxrpc_transport *trans;
283 _enter("%p{t=%p}", sk, sk->sk_user_data);
285 /* queue the transport for attention by krxiod */
286 trans = (struct rxrpc_transport *) sk->sk_user_data;
288 trans->error_rcvd = 1;
289 rxrpc_krxiod_queue_transport(trans);
292 /* wake up anyone waiting on the socket */
293 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
294 wake_up_interruptible(sk->sk_sleep);
297 } /* end rxrpc_error_report() */
299 /*****************************************************************************/
301 * split a message up, allocating message records and filling them in
302 * from the contents of a socket buffer
304 static int rxrpc_incoming_msg(struct rxrpc_transport *trans,
306 struct list_head *msgq)
308 struct rxrpc_message *msg;
313 msg = kzalloc(sizeof(struct rxrpc_message), GFP_KERNEL);
315 _leave(" = -ENOMEM");
319 atomic_set(&msg->usage, 1);
320 list_add_tail(&msg->link,msgq);
322 /* dig out the Rx routing parameters */
323 if (skb_copy_bits(pkt, sizeof(struct udphdr),
324 &msg->hdr, sizeof(msg->hdr)) < 0) {
330 msg->state = RXRPC_MSG_RECEIVED;
331 skb_get_timestamp(pkt, &msg->stamp);
332 if (msg->stamp.tv_sec == 0) {
333 do_gettimeofday(&msg->stamp);
335 sock_enable_timestamp(pkt->sk);
337 msg->seq = ntohl(msg->hdr.seq);
339 /* attach the packet */
343 msg->offset = sizeof(struct udphdr) + sizeof(struct rxrpc_header);
344 msg->dsize = msg->pkt->len - msg->offset;
346 _net("Rx Received packet from %s (%08x;%08x,%1x,%d,%s,%02x,%d,%d)",
347 msg->hdr.flags & RXRPC_CLIENT_INITIATED ? "client" : "server",
348 ntohl(msg->hdr.epoch),
349 (ntohl(msg->hdr.cid) & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT,
350 ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK,
351 ntohl(msg->hdr.callNumber),
352 rxrpc_pkts[msg->hdr.type],
354 ntohs(msg->hdr.serviceId),
355 msg->hdr.securityIndex);
357 __RXACCT(atomic_inc(&rxrpc_message_count));
359 /* split off jumbo packets */
360 while (msg->hdr.type == RXRPC_PACKET_TYPE_DATA &&
361 msg->hdr.flags & RXRPC_JUMBO_PACKET
363 struct rxrpc_jumbo_header jumbo;
364 struct rxrpc_message *jumbomsg = msg;
366 _debug("split jumbo packet");
368 /* quick sanity check */
371 RXRPC_JUMBO_DATALEN + sizeof(struct rxrpc_jumbo_header))
373 if (msg->hdr.flags & RXRPC_LAST_PACKET)
376 /* dig out the secondary header */
377 if (skb_copy_bits(pkt, msg->offset + RXRPC_JUMBO_DATALEN,
378 &jumbo, sizeof(jumbo)) < 0)
381 /* allocate a new message record */
383 msg = kmemdup(jumbomsg, sizeof(struct rxrpc_message), GFP_KERNEL);
387 list_add_tail(&msg->link, msgq);
389 /* adjust the jumbo packet */
390 jumbomsg->dsize = RXRPC_JUMBO_DATALEN;
392 /* attach the packet here too */
395 /* adjust the parameters */
397 msg->hdr.seq = htonl(msg->seq);
398 msg->hdr.serial = htonl(ntohl(msg->hdr.serial) + 1);
399 msg->offset += RXRPC_JUMBO_DATALEN +
400 sizeof(struct rxrpc_jumbo_header);
401 msg->dsize -= RXRPC_JUMBO_DATALEN +
402 sizeof(struct rxrpc_jumbo_header);
403 msg->hdr.flags = jumbo.flags;
404 msg->hdr._rsvd = jumbo._rsvd;
406 _net("Rx Split jumbo packet from %s"
407 " (%08x;%08x,%1x,%d,%s,%02x,%d,%d)",
408 msg->hdr.flags & RXRPC_CLIENT_INITIATED ? "client" : "server",
409 ntohl(msg->hdr.epoch),
410 (ntohl(msg->hdr.cid) & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT,
411 ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK,
412 ntohl(msg->hdr.callNumber),
413 rxrpc_pkts[msg->hdr.type],
415 ntohs(msg->hdr.serviceId),
416 msg->hdr.securityIndex);
418 __RXACCT(atomic_inc(&rxrpc_message_count));
421 _leave(" = 0 #%d", atomic_read(&rxrpc_message_count));
425 while (!list_empty(msgq)) {
426 msg = list_entry(msgq->next, struct rxrpc_message, link);
427 list_del_init(&msg->link);
429 rxrpc_put_message(msg);
432 _leave(" = %d", ret);
434 } /* end rxrpc_incoming_msg() */
436 /*****************************************************************************/
439 * - called from krxiod in process context
441 void rxrpc_trans_receive_packet(struct rxrpc_transport *trans)
443 struct rxrpc_message *msg;
444 struct rxrpc_peer *peer;
452 _enter("%p{%d}", trans, trans->port);
455 /* deal with outstanting errors first */
456 if (trans->error_rcvd)
457 rxrpc_trans_receive_error_report(trans);
459 /* attempt to receive a packet */
460 pkt = skb_recv_datagram(trans->socket->sk, 0, 1, &ret);
462 if (ret == -EAGAIN) {
467 /* an icmp error may have occurred */
468 rxrpc_krxiod_queue_transport(trans);
469 _leave(" error %d\n", ret);
473 /* we'll probably need to checksum it (didn't call
475 if (skb_checksum_complete(pkt)) {
477 rxrpc_krxiod_queue_transport(trans);
478 _leave(" CSUM failed");
482 addr = pkt->nh.iph->saddr;
483 port = pkt->h.uh->source;
485 _net("Rx Received UDP packet from %08x:%04hu",
486 ntohl(addr), ntohs(port));
488 /* unmarshall the Rx parameters and split jumbo packets */
489 ret = rxrpc_incoming_msg(trans, pkt, &msgq);
492 rxrpc_krxiod_queue_transport(trans);
493 _leave(" bad packet");
497 BUG_ON(list_empty(&msgq));
499 msg = list_entry(msgq.next, struct rxrpc_message, link);
501 /* locate the record for the peer from which it
503 ret = rxrpc_peer_lookup(trans, addr, &peer);
505 kdebug("Rx No connections from that peer");
506 rxrpc_trans_immediate_abort(trans, msg, -EINVAL);
510 /* try and find a matching connection */
511 ret = rxrpc_connection_lookup(peer, msg, &msg->conn);
513 kdebug("Rx Unknown Connection");
514 rxrpc_trans_immediate_abort(trans, msg, -EINVAL);
515 rxrpc_put_peer(peer);
518 rxrpc_put_peer(peer);
520 /* deal with the first packet of a new call */
521 if (msg->hdr.flags & RXRPC_CLIENT_INITIATED &&
522 msg->hdr.type == RXRPC_PACKET_TYPE_DATA &&
523 ntohl(msg->hdr.seq) == 1
525 _debug("Rx New server call");
526 rxrpc_trans_receive_new_call(trans, &msgq);
530 /* deal with subsequent packet(s) of call */
531 _debug("Rx Call packet");
532 while (!list_empty(&msgq)) {
533 msg = list_entry(msgq.next, struct rxrpc_message, link);
534 list_del_init(&msg->link);
536 ret = rxrpc_conn_receive_call_packet(msg->conn, NULL, msg);
538 rxrpc_trans_immediate_abort(trans, msg, ret);
539 rxrpc_put_message(msg);
543 rxrpc_put_message(msg);
548 /* dispose of the packets */
550 while (!list_empty(&msgq)) {
551 msg = list_entry(msgq.next, struct rxrpc_message, link);
552 list_del_init(&msg->link);
554 rxrpc_put_message(msg);
561 } /* end rxrpc_trans_receive_packet() */
563 /*****************************************************************************/
565 * accept a new call from a client trying to connect to one of my services
566 * - called in process context
568 static int rxrpc_trans_receive_new_call(struct rxrpc_transport *trans,
569 struct list_head *msgq)
571 struct rxrpc_message *msg;
575 /* only bother with the first packet */
576 msg = list_entry(msgq->next, struct rxrpc_message, link);
577 list_del_init(&msg->link);
578 rxrpc_krxsecd_queue_incoming_call(msg);
579 rxrpc_put_message(msg);
584 } /* end rxrpc_trans_receive_new_call() */
586 /*****************************************************************************/
588 * perform an immediate abort without connection or call structures
590 int rxrpc_trans_immediate_abort(struct rxrpc_transport *trans,
591 struct rxrpc_message *msg,
594 struct rxrpc_header ahdr;
595 struct sockaddr_in sin;
596 struct msghdr msghdr;
601 _enter("%p,%p,%d", trans, msg, error);
603 /* don't abort an abort packet */
604 if (msg->hdr.type == RXRPC_PACKET_TYPE_ABORT) {
609 _error = htonl(-error);
611 /* set up the message to be transmitted */
612 memcpy(&ahdr, &msg->hdr, sizeof(ahdr));
613 ahdr.epoch = msg->hdr.epoch;
614 ahdr.serial = htonl(1);
616 ahdr.type = RXRPC_PACKET_TYPE_ABORT;
617 ahdr.flags = RXRPC_LAST_PACKET;
618 ahdr.flags |= ~msg->hdr.flags & RXRPC_CLIENT_INITIATED;
620 iov[0].iov_len = sizeof(ahdr);
621 iov[0].iov_base = &ahdr;
622 iov[1].iov_len = sizeof(_error);
623 iov[1].iov_base = &_error;
625 len = sizeof(ahdr) + sizeof(_error);
627 memset(&sin,0,sizeof(sin));
628 sin.sin_family = AF_INET;
629 sin.sin_port = msg->pkt->h.uh->source;
630 sin.sin_addr.s_addr = msg->pkt->nh.iph->saddr;
632 msghdr.msg_name = &sin;
633 msghdr.msg_namelen = sizeof(sin);
634 msghdr.msg_control = NULL;
635 msghdr.msg_controllen = 0;
636 msghdr.msg_flags = MSG_DONTWAIT;
638 _net("Sending message type %d of %d bytes to %08x:%d",
641 ntohl(sin.sin_addr.s_addr),
642 ntohs(sin.sin_port));
644 /* send the message */
645 ret = kernel_sendmsg(trans->socket, &msghdr, iov, 2, len);
647 _leave(" = %d", ret);
649 } /* end rxrpc_trans_immediate_abort() */
651 /*****************************************************************************/
653 * receive an ICMP error report and percolate it to all connections
654 * heading to the affected host or port
656 static void rxrpc_trans_receive_error_report(struct rxrpc_transport *trans)
658 struct rxrpc_connection *conn;
659 struct sockaddr_in sin;
660 struct rxrpc_peer *peer;
661 struct list_head connq, *_p;
662 struct errormsg emsg;
670 trans->error_rcvd = 0;
672 /* try and receive an error message */
674 msg.msg_namelen = sizeof(sin);
675 msg.msg_control = &emsg;
676 msg.msg_controllen = sizeof(emsg);
679 err = kernel_recvmsg(trans->socket, &msg, NULL, 0, 0,
680 MSG_ERRQUEUE | MSG_DONTWAIT | MSG_TRUNC);
682 if (err == -EAGAIN) {
688 printk("%s: unable to recv an error report: %d\n",
694 msg.msg_controllen = (char *) msg.msg_control - (char *) &emsg;
696 if (msg.msg_controllen < sizeof(emsg.cmsg) ||
697 msg.msg_namelen < sizeof(sin)) {
698 printk("%s: short control message"
699 " (nlen=%u clen=%Zu fl=%x)\n",
707 _net("Rx Received control message"
708 " { len=%Zu level=%u type=%u }",
710 emsg.cmsg.cmsg_level,
711 emsg.cmsg.cmsg_type);
713 if (sin.sin_family != AF_INET) {
714 printk("Rx Ignoring error report with non-INET address"
720 _net("Rx Received message pertaining to host addr=%x port=%hu",
721 ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
723 if (emsg.cmsg.cmsg_level != SOL_IP ||
724 emsg.cmsg.cmsg_type != IP_RECVERR) {
725 printk("Rx Ignoring unknown error report"
726 " { level=%u type=%u }",
727 emsg.cmsg.cmsg_level,
728 emsg.cmsg.cmsg_type);
732 if (msg.msg_controllen < sizeof(emsg.cmsg) + sizeof(emsg.ee)) {
733 printk("%s: short error message (%Zu)\n",
734 __FUNCTION__, msg.msg_controllen);
741 switch (emsg.ee.ee_origin) {
742 case SO_EE_ORIGIN_ICMP:
744 switch (emsg.ee.ee_type) {
745 case ICMP_DEST_UNREACH:
746 switch (emsg.ee.ee_code) {
747 case ICMP_NET_UNREACH:
748 _net("Rx Received ICMP Network Unreachable");
752 case ICMP_HOST_UNREACH:
753 _net("Rx Received ICMP Host Unreachable");
757 case ICMP_PORT_UNREACH:
758 _net("Rx Received ICMP Port Unreachable");
761 case ICMP_NET_UNKNOWN:
762 _net("Rx Received ICMP Unknown Network");
766 case ICMP_HOST_UNKNOWN:
767 _net("Rx Received ICMP Unknown Host");
772 _net("Rx Received ICMP DestUnreach { code=%u }",
774 err = emsg.ee.ee_errno;
779 case ICMP_TIME_EXCEEDED:
780 _net("Rx Received ICMP TTL Exceeded");
781 err = emsg.ee.ee_errno;
785 _proto("Rx Received ICMP error { type=%u code=%u }",
786 emsg.ee.ee_type, emsg.ee.ee_code);
787 err = emsg.ee.ee_errno;
792 case SO_EE_ORIGIN_LOCAL:
793 _proto("Rx Received local error { error=%d }",
796 err = emsg.ee.ee_errno;
799 case SO_EE_ORIGIN_NONE:
800 case SO_EE_ORIGIN_ICMP6:
802 _proto("Rx Received error report { orig=%u }",
805 err = emsg.ee.ee_errno;
809 /* find all the connections between this transport and the
810 * affected destination */
811 INIT_LIST_HEAD(&connq);
813 if (rxrpc_peer_lookup(trans, sin.sin_addr.s_addr,
815 read_lock(&peer->conn_lock);
816 list_for_each(_p, &peer->conn_active) {
817 conn = list_entry(_p, struct rxrpc_connection,
819 if (port && conn->addr.sin_port != port)
821 if (!list_empty(&conn->err_link))
824 rxrpc_get_connection(conn);
825 list_add_tail(&conn->err_link, &connq);
827 read_unlock(&peer->conn_lock);
829 /* service all those connections */
830 while (!list_empty(&connq)) {
831 conn = list_entry(connq.next,
832 struct rxrpc_connection,
834 list_del(&conn->err_link);
836 rxrpc_conn_handle_error(conn, local, err);
838 rxrpc_put_connection(conn);
841 rxrpc_put_peer(peer);
847 } /* end rxrpc_trans_receive_error_report() */