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/slab.h>
13 #include <linux/module.h>
14 #include <rxrpc/transport.h>
15 #include <rxrpc/peer.h>
16 #include <rxrpc/connection.h>
17 #include <rxrpc/call.h>
18 #include <rxrpc/message.h>
19 #include <rxrpc/krxiod.h>
20 #include <rxrpc/krxsecd.h>
21 #include <linux/udp.h>
23 #include <linux/in6.h>
24 #include <linux/icmp.h>
25 #include <linux/skbuff.h>
28 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
29 #include <linux/ipv6.h> /* this should _really_ be in errqueue.h.. */
31 #include <linux/errqueue.h>
32 #include <asm/uaccess.h>
36 struct cmsghdr cmsg; /* control message header */
37 struct sock_extended_err ee; /* extended error information */
38 struct sockaddr_in icmp_src; /* ICMP packet source address */
41 static DEFINE_SPINLOCK(rxrpc_transports_lock);
42 static struct list_head rxrpc_transports = LIST_HEAD_INIT(rxrpc_transports);
44 __RXACCT_DECL(atomic_t rxrpc_transport_count);
45 LIST_HEAD(rxrpc_proc_transports);
46 DECLARE_RWSEM(rxrpc_proc_transports_sem);
48 static void rxrpc_data_ready(struct sock *sk, int count);
49 static void rxrpc_error_report(struct sock *sk);
50 static int rxrpc_trans_receive_new_call(struct rxrpc_transport *trans,
51 struct list_head *msgq);
52 static void rxrpc_trans_receive_error_report(struct rxrpc_transport *trans);
54 /*****************************************************************************/
56 * create a new transport endpoint using the specified UDP port
58 int rxrpc_create_transport(unsigned short port,
59 struct rxrpc_transport **_trans)
61 struct rxrpc_transport *trans;
62 struct sockaddr_in sin;
69 trans = kzalloc(sizeof(struct rxrpc_transport), GFP_KERNEL);
73 atomic_set(&trans->usage, 1);
74 INIT_LIST_HEAD(&trans->services);
75 INIT_LIST_HEAD(&trans->link);
76 INIT_LIST_HEAD(&trans->krxiodq_link);
77 spin_lock_init(&trans->lock);
78 INIT_LIST_HEAD(&trans->peer_active);
79 INIT_LIST_HEAD(&trans->peer_graveyard);
80 spin_lock_init(&trans->peer_gylock);
81 init_waitqueue_head(&trans->peer_gy_waitq);
82 rwlock_init(&trans->peer_lock);
83 atomic_set(&trans->peer_count, 0);
86 /* create a UDP socket to be my actual transport endpoint */
87 ret = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &trans->socket);
91 /* use the specified port */
93 memset(&sin, 0, sizeof(sin));
94 sin.sin_family = AF_INET;
95 sin.sin_port = htons(port);
96 ret = trans->socket->ops->bind(trans->socket,
97 (struct sockaddr *) &sin,
106 ret = trans->socket->ops->setsockopt(trans->socket, SOL_IP, IP_RECVERR,
107 (char *) &opt, sizeof(opt));
110 spin_lock(&rxrpc_transports_lock);
111 list_add(&trans->link, &rxrpc_transports);
112 spin_unlock(&rxrpc_transports_lock);
114 /* set the socket up */
115 sock = trans->socket->sk;
116 sock->sk_user_data = trans;
117 sock->sk_data_ready = rxrpc_data_ready;
118 sock->sk_error_report = rxrpc_error_report;
120 down_write(&rxrpc_proc_transports_sem);
121 list_add_tail(&trans->proc_link, &rxrpc_proc_transports);
122 up_write(&rxrpc_proc_transports_sem);
124 __RXACCT(atomic_inc(&rxrpc_transport_count));
127 _leave(" = 0 (%p)", trans);
131 /* finish cleaning up the transport (not really needed here, but...) */
133 trans->socket->ops->shutdown(trans->socket, 2);
135 /* close the socket */
137 trans->socket->sk->sk_user_data = NULL;
138 sock_release(trans->socket);
139 trans->socket = NULL;
145 _leave(" = %d", ret);
147 } /* end rxrpc_create_transport() */
149 /*****************************************************************************/
151 * destroy a transport endpoint
153 void rxrpc_put_transport(struct rxrpc_transport *trans)
155 _enter("%p{u=%d p=%hu}",
156 trans, atomic_read(&trans->usage), trans->port);
158 BUG_ON(atomic_read(&trans->usage) <= 0);
160 /* to prevent a race, the decrement and the dequeue must be
161 * effectively atomic */
162 spin_lock(&rxrpc_transports_lock);
163 if (likely(!atomic_dec_and_test(&trans->usage))) {
164 spin_unlock(&rxrpc_transports_lock);
169 list_del(&trans->link);
170 spin_unlock(&rxrpc_transports_lock);
172 /* finish cleaning up the transport */
174 trans->socket->ops->shutdown(trans->socket, 2);
176 rxrpc_krxsecd_clear_transport(trans);
177 rxrpc_krxiod_dequeue_transport(trans);
179 /* discard all peer information */
180 rxrpc_peer_clearall(trans);
182 down_write(&rxrpc_proc_transports_sem);
183 list_del(&trans->proc_link);
184 up_write(&rxrpc_proc_transports_sem);
185 __RXACCT(atomic_dec(&rxrpc_transport_count));
187 /* close the socket */
189 trans->socket->sk->sk_user_data = NULL;
190 sock_release(trans->socket);
191 trans->socket = NULL;
197 } /* end rxrpc_put_transport() */
199 /*****************************************************************************/
201 * add a service to a transport to be listened upon
203 int rxrpc_add_service(struct rxrpc_transport *trans,
204 struct rxrpc_service *newsrv)
206 struct rxrpc_service *srv;
207 struct list_head *_p;
210 _enter("%p{%hu},%p{%hu}",
211 trans, trans->port, newsrv, newsrv->service_id);
213 /* verify that the service ID is not already present */
214 spin_lock(&trans->lock);
216 list_for_each(_p, &trans->services) {
217 srv = list_entry(_p, struct rxrpc_service, link);
218 if (srv->service_id == newsrv->service_id)
222 /* okay - add the transport to the list */
223 list_add_tail(&newsrv->link, &trans->services);
224 rxrpc_get_transport(trans);
228 spin_unlock(&trans->lock);
232 } /* end rxrpc_add_service() */
234 /*****************************************************************************/
236 * remove a service from a transport
238 void rxrpc_del_service(struct rxrpc_transport *trans, struct rxrpc_service *srv)
240 _enter("%p{%hu},%p{%hu}", trans, trans->port, srv, srv->service_id);
242 spin_lock(&trans->lock);
243 list_del(&srv->link);
244 spin_unlock(&trans->lock);
246 rxrpc_put_transport(trans);
249 } /* end rxrpc_del_service() */
251 /*****************************************************************************/
253 * INET callback when data has been received on the socket.
255 static void rxrpc_data_ready(struct sock *sk, int count)
257 struct rxrpc_transport *trans;
259 _enter("%p{t=%p},%d", sk, sk->sk_user_data, count);
261 /* queue the transport for attention by krxiod */
262 trans = (struct rxrpc_transport *) sk->sk_user_data;
264 rxrpc_krxiod_queue_transport(trans);
266 /* wake up anyone waiting on the socket */
267 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
268 wake_up_interruptible(sk->sk_sleep);
271 } /* end rxrpc_data_ready() */
273 /*****************************************************************************/
275 * INET callback when an ICMP error packet is received
276 * - sk->err is error (EHOSTUNREACH, EPROTO or EMSGSIZE)
278 static void rxrpc_error_report(struct sock *sk)
280 struct rxrpc_transport *trans;
282 _enter("%p{t=%p}", sk, sk->sk_user_data);
284 /* queue the transport for attention by krxiod */
285 trans = (struct rxrpc_transport *) sk->sk_user_data;
287 trans->error_rcvd = 1;
288 rxrpc_krxiod_queue_transport(trans);
291 /* wake up anyone waiting on the socket */
292 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
293 wake_up_interruptible(sk->sk_sleep);
296 } /* end rxrpc_error_report() */
298 /*****************************************************************************/
300 * split a message up, allocating message records and filling them in
301 * from the contents of a socket buffer
303 static int rxrpc_incoming_msg(struct rxrpc_transport *trans,
305 struct list_head *msgq)
307 struct rxrpc_message *msg;
312 msg = kzalloc(sizeof(struct rxrpc_message), GFP_KERNEL);
314 _leave(" = -ENOMEM");
318 atomic_set(&msg->usage, 1);
319 list_add_tail(&msg->link,msgq);
321 /* dig out the Rx routing parameters */
322 if (skb_copy_bits(pkt, sizeof(struct udphdr),
323 &msg->hdr, sizeof(msg->hdr)) < 0) {
329 msg->state = RXRPC_MSG_RECEIVED;
330 skb_get_timestamp(pkt, &msg->stamp);
331 if (msg->stamp.tv_sec == 0) {
332 do_gettimeofday(&msg->stamp);
334 sock_enable_timestamp(pkt->sk);
336 msg->seq = ntohl(msg->hdr.seq);
338 /* attach the packet */
342 msg->offset = sizeof(struct udphdr) + sizeof(struct rxrpc_header);
343 msg->dsize = msg->pkt->len - msg->offset;
345 _net("Rx Received packet from %s (%08x;%08x,%1x,%d,%s,%02x,%d,%d)",
346 msg->hdr.flags & RXRPC_CLIENT_INITIATED ? "client" : "server",
347 ntohl(msg->hdr.epoch),
348 (ntohl(msg->hdr.cid) & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT,
349 ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK,
350 ntohl(msg->hdr.callNumber),
351 rxrpc_pkts[msg->hdr.type],
353 ntohs(msg->hdr.serviceId),
354 msg->hdr.securityIndex);
356 __RXACCT(atomic_inc(&rxrpc_message_count));
358 /* split off jumbo packets */
359 while (msg->hdr.type == RXRPC_PACKET_TYPE_DATA &&
360 msg->hdr.flags & RXRPC_JUMBO_PACKET
362 struct rxrpc_jumbo_header jumbo;
363 struct rxrpc_message *jumbomsg = msg;
365 _debug("split jumbo packet");
367 /* quick sanity check */
370 RXRPC_JUMBO_DATALEN + sizeof(struct rxrpc_jumbo_header))
372 if (msg->hdr.flags & RXRPC_LAST_PACKET)
375 /* dig out the secondary header */
376 if (skb_copy_bits(pkt, msg->offset + RXRPC_JUMBO_DATALEN,
377 &jumbo, sizeof(jumbo)) < 0)
380 /* allocate a new message record */
382 msg = kmemdup(jumbomsg, sizeof(struct rxrpc_message), GFP_KERNEL);
386 list_add_tail(&msg->link, msgq);
388 /* adjust the jumbo packet */
389 jumbomsg->dsize = RXRPC_JUMBO_DATALEN;
391 /* attach the packet here too */
394 /* adjust the parameters */
396 msg->hdr.seq = htonl(msg->seq);
397 msg->hdr.serial = htonl(ntohl(msg->hdr.serial) + 1);
398 msg->offset += RXRPC_JUMBO_DATALEN +
399 sizeof(struct rxrpc_jumbo_header);
400 msg->dsize -= RXRPC_JUMBO_DATALEN +
401 sizeof(struct rxrpc_jumbo_header);
402 msg->hdr.flags = jumbo.flags;
403 msg->hdr._rsvd = jumbo._rsvd;
405 _net("Rx Split jumbo packet from %s"
406 " (%08x;%08x,%1x,%d,%s,%02x,%d,%d)",
407 msg->hdr.flags & RXRPC_CLIENT_INITIATED ? "client" : "server",
408 ntohl(msg->hdr.epoch),
409 (ntohl(msg->hdr.cid) & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT,
410 ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK,
411 ntohl(msg->hdr.callNumber),
412 rxrpc_pkts[msg->hdr.type],
414 ntohs(msg->hdr.serviceId),
415 msg->hdr.securityIndex);
417 __RXACCT(atomic_inc(&rxrpc_message_count));
420 _leave(" = 0 #%d", atomic_read(&rxrpc_message_count));
424 while (!list_empty(msgq)) {
425 msg = list_entry(msgq->next, struct rxrpc_message, link);
426 list_del_init(&msg->link);
428 rxrpc_put_message(msg);
431 _leave(" = %d", ret);
433 } /* end rxrpc_incoming_msg() */
435 /*****************************************************************************/
438 * - called from krxiod in process context
440 void rxrpc_trans_receive_packet(struct rxrpc_transport *trans)
442 struct rxrpc_message *msg;
443 struct rxrpc_peer *peer;
451 _enter("%p{%d}", trans, trans->port);
454 /* deal with outstanting errors first */
455 if (trans->error_rcvd)
456 rxrpc_trans_receive_error_report(trans);
458 /* attempt to receive a packet */
459 pkt = skb_recv_datagram(trans->socket->sk, 0, 1, &ret);
461 if (ret == -EAGAIN) {
466 /* an icmp error may have occurred */
467 rxrpc_krxiod_queue_transport(trans);
468 _leave(" error %d\n", ret);
472 /* we'll probably need to checksum it (didn't call
474 if (skb_checksum_complete(pkt)) {
476 rxrpc_krxiod_queue_transport(trans);
477 _leave(" CSUM failed");
481 addr = pkt->nh.iph->saddr;
482 port = pkt->h.uh->source;
484 _net("Rx Received UDP packet from %08x:%04hu",
485 ntohl(addr), ntohs(port));
487 /* unmarshall the Rx parameters and split jumbo packets */
488 ret = rxrpc_incoming_msg(trans, pkt, &msgq);
491 rxrpc_krxiod_queue_transport(trans);
492 _leave(" bad packet");
496 BUG_ON(list_empty(&msgq));
498 msg = list_entry(msgq.next, struct rxrpc_message, link);
500 /* locate the record for the peer from which it
502 ret = rxrpc_peer_lookup(trans, addr, &peer);
504 kdebug("Rx No connections from that peer");
505 rxrpc_trans_immediate_abort(trans, msg, -EINVAL);
509 /* try and find a matching connection */
510 ret = rxrpc_connection_lookup(peer, msg, &msg->conn);
512 kdebug("Rx Unknown Connection");
513 rxrpc_trans_immediate_abort(trans, msg, -EINVAL);
514 rxrpc_put_peer(peer);
517 rxrpc_put_peer(peer);
519 /* deal with the first packet of a new call */
520 if (msg->hdr.flags & RXRPC_CLIENT_INITIATED &&
521 msg->hdr.type == RXRPC_PACKET_TYPE_DATA &&
522 ntohl(msg->hdr.seq) == 1
524 _debug("Rx New server call");
525 rxrpc_trans_receive_new_call(trans, &msgq);
529 /* deal with subsequent packet(s) of call */
530 _debug("Rx Call packet");
531 while (!list_empty(&msgq)) {
532 msg = list_entry(msgq.next, struct rxrpc_message, link);
533 list_del_init(&msg->link);
535 ret = rxrpc_conn_receive_call_packet(msg->conn, NULL, msg);
537 rxrpc_trans_immediate_abort(trans, msg, ret);
538 rxrpc_put_message(msg);
542 rxrpc_put_message(msg);
547 /* dispose of the packets */
549 while (!list_empty(&msgq)) {
550 msg = list_entry(msgq.next, struct rxrpc_message, link);
551 list_del_init(&msg->link);
553 rxrpc_put_message(msg);
560 } /* end rxrpc_trans_receive_packet() */
562 /*****************************************************************************/
564 * accept a new call from a client trying to connect to one of my services
565 * - called in process context
567 static int rxrpc_trans_receive_new_call(struct rxrpc_transport *trans,
568 struct list_head *msgq)
570 struct rxrpc_message *msg;
574 /* only bother with the first packet */
575 msg = list_entry(msgq->next, struct rxrpc_message, link);
576 list_del_init(&msg->link);
577 rxrpc_krxsecd_queue_incoming_call(msg);
578 rxrpc_put_message(msg);
583 } /* end rxrpc_trans_receive_new_call() */
585 /*****************************************************************************/
587 * perform an immediate abort without connection or call structures
589 int rxrpc_trans_immediate_abort(struct rxrpc_transport *trans,
590 struct rxrpc_message *msg,
593 struct rxrpc_header ahdr;
594 struct sockaddr_in sin;
595 struct msghdr msghdr;
600 _enter("%p,%p,%d", trans, msg, error);
602 /* don't abort an abort packet */
603 if (msg->hdr.type == RXRPC_PACKET_TYPE_ABORT) {
608 _error = htonl(-error);
610 /* set up the message to be transmitted */
611 memcpy(&ahdr, &msg->hdr, sizeof(ahdr));
612 ahdr.epoch = msg->hdr.epoch;
613 ahdr.serial = htonl(1);
615 ahdr.type = RXRPC_PACKET_TYPE_ABORT;
616 ahdr.flags = RXRPC_LAST_PACKET;
617 ahdr.flags |= ~msg->hdr.flags & RXRPC_CLIENT_INITIATED;
619 iov[0].iov_len = sizeof(ahdr);
620 iov[0].iov_base = &ahdr;
621 iov[1].iov_len = sizeof(_error);
622 iov[1].iov_base = &_error;
624 len = sizeof(ahdr) + sizeof(_error);
626 memset(&sin,0,sizeof(sin));
627 sin.sin_family = AF_INET;
628 sin.sin_port = msg->pkt->h.uh->source;
629 sin.sin_addr.s_addr = msg->pkt->nh.iph->saddr;
631 msghdr.msg_name = &sin;
632 msghdr.msg_namelen = sizeof(sin);
633 msghdr.msg_control = NULL;
634 msghdr.msg_controllen = 0;
635 msghdr.msg_flags = MSG_DONTWAIT;
637 _net("Sending message type %d of %d bytes to %08x:%d",
640 ntohl(sin.sin_addr.s_addr),
641 ntohs(sin.sin_port));
643 /* send the message */
644 ret = kernel_sendmsg(trans->socket, &msghdr, iov, 2, len);
646 _leave(" = %d", ret);
648 } /* end rxrpc_trans_immediate_abort() */
650 /*****************************************************************************/
652 * receive an ICMP error report and percolate it to all connections
653 * heading to the affected host or port
655 static void rxrpc_trans_receive_error_report(struct rxrpc_transport *trans)
657 struct rxrpc_connection *conn;
658 struct sockaddr_in sin;
659 struct rxrpc_peer *peer;
660 struct list_head connq, *_p;
661 struct errormsg emsg;
669 trans->error_rcvd = 0;
671 /* try and receive an error message */
673 msg.msg_namelen = sizeof(sin);
674 msg.msg_control = &emsg;
675 msg.msg_controllen = sizeof(emsg);
678 err = kernel_recvmsg(trans->socket, &msg, NULL, 0, 0,
679 MSG_ERRQUEUE | MSG_DONTWAIT | MSG_TRUNC);
681 if (err == -EAGAIN) {
687 printk("%s: unable to recv an error report: %d\n",
693 msg.msg_controllen = (char *) msg.msg_control - (char *) &emsg;
695 if (msg.msg_controllen < sizeof(emsg.cmsg) ||
696 msg.msg_namelen < sizeof(sin)) {
697 printk("%s: short control message"
698 " (nlen=%u clen=%Zu fl=%x)\n",
706 _net("Rx Received control message"
707 " { len=%Zu level=%u type=%u }",
709 emsg.cmsg.cmsg_level,
710 emsg.cmsg.cmsg_type);
712 if (sin.sin_family != AF_INET) {
713 printk("Rx Ignoring error report with non-INET address"
719 _net("Rx Received message pertaining to host addr=%x port=%hu",
720 ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
722 if (emsg.cmsg.cmsg_level != SOL_IP ||
723 emsg.cmsg.cmsg_type != IP_RECVERR) {
724 printk("Rx Ignoring unknown error report"
725 " { level=%u type=%u }",
726 emsg.cmsg.cmsg_level,
727 emsg.cmsg.cmsg_type);
731 if (msg.msg_controllen < sizeof(emsg.cmsg) + sizeof(emsg.ee)) {
732 printk("%s: short error message (%Zu)\n",
733 __FUNCTION__, msg.msg_controllen);
740 switch (emsg.ee.ee_origin) {
741 case SO_EE_ORIGIN_ICMP:
743 switch (emsg.ee.ee_type) {
744 case ICMP_DEST_UNREACH:
745 switch (emsg.ee.ee_code) {
746 case ICMP_NET_UNREACH:
747 _net("Rx Received ICMP Network Unreachable");
751 case ICMP_HOST_UNREACH:
752 _net("Rx Received ICMP Host Unreachable");
756 case ICMP_PORT_UNREACH:
757 _net("Rx Received ICMP Port Unreachable");
760 case ICMP_NET_UNKNOWN:
761 _net("Rx Received ICMP Unknown Network");
765 case ICMP_HOST_UNKNOWN:
766 _net("Rx Received ICMP Unknown Host");
771 _net("Rx Received ICMP DestUnreach { code=%u }",
773 err = emsg.ee.ee_errno;
778 case ICMP_TIME_EXCEEDED:
779 _net("Rx Received ICMP TTL Exceeded");
780 err = emsg.ee.ee_errno;
784 _proto("Rx Received ICMP error { type=%u code=%u }",
785 emsg.ee.ee_type, emsg.ee.ee_code);
786 err = emsg.ee.ee_errno;
791 case SO_EE_ORIGIN_LOCAL:
792 _proto("Rx Received local error { error=%d }",
795 err = emsg.ee.ee_errno;
798 case SO_EE_ORIGIN_NONE:
799 case SO_EE_ORIGIN_ICMP6:
801 _proto("Rx Received error report { orig=%u }",
804 err = emsg.ee.ee_errno;
808 /* find all the connections between this transport and the
809 * affected destination */
810 INIT_LIST_HEAD(&connq);
812 if (rxrpc_peer_lookup(trans, sin.sin_addr.s_addr,
814 read_lock(&peer->conn_lock);
815 list_for_each(_p, &peer->conn_active) {
816 conn = list_entry(_p, struct rxrpc_connection,
818 if (port && conn->addr.sin_port != port)
820 if (!list_empty(&conn->err_link))
823 rxrpc_get_connection(conn);
824 list_add_tail(&conn->err_link, &connq);
826 read_unlock(&peer->conn_lock);
828 /* service all those connections */
829 while (!list_empty(&connq)) {
830 conn = list_entry(connq.next,
831 struct rxrpc_connection,
833 list_del(&conn->err_link);
835 rxrpc_conn_handle_error(conn, local, err);
837 rxrpc_put_connection(conn);
840 rxrpc_put_peer(peer);
846 } /* end rxrpc_trans_receive_error_report() */