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>
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>
33 #include <asm/checksum.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 spinlock_t rxrpc_transports_lock = SPIN_LOCK_UNLOCKED;
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 = kmalloc(sizeof(struct rxrpc_transport), GFP_KERNEL);
74 memset(trans, 0, sizeof(struct rxrpc_transport));
75 atomic_set(&trans->usage, 1);
76 INIT_LIST_HEAD(&trans->services);
77 INIT_LIST_HEAD(&trans->link);
78 INIT_LIST_HEAD(&trans->krxiodq_link);
79 spin_lock_init(&trans->lock);
80 INIT_LIST_HEAD(&trans->peer_active);
81 INIT_LIST_HEAD(&trans->peer_graveyard);
82 spin_lock_init(&trans->peer_gylock);
83 init_waitqueue_head(&trans->peer_gy_waitq);
84 rwlock_init(&trans->peer_lock);
85 atomic_set(&trans->peer_count, 0);
88 /* create a UDP socket to be my actual transport endpoint */
89 ret = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &trans->socket);
93 /* use the specified port */
95 memset(&sin, 0, sizeof(sin));
96 sin.sin_family = AF_INET;
97 sin.sin_port = htons(port);
98 ret = trans->socket->ops->bind(trans->socket,
99 (struct sockaddr *) &sin,
108 ret = trans->socket->ops->setsockopt(trans->socket, SOL_IP, IP_RECVERR,
109 (char *) &opt, sizeof(opt));
112 spin_lock(&rxrpc_transports_lock);
113 list_add(&trans->link, &rxrpc_transports);
114 spin_unlock(&rxrpc_transports_lock);
116 /* set the socket up */
117 sock = trans->socket->sk;
118 sock->sk_user_data = trans;
119 sock->sk_data_ready = rxrpc_data_ready;
120 sock->sk_error_report = rxrpc_error_report;
122 down_write(&rxrpc_proc_transports_sem);
123 list_add_tail(&trans->proc_link, &rxrpc_proc_transports);
124 up_write(&rxrpc_proc_transports_sem);
126 __RXACCT(atomic_inc(&rxrpc_transport_count));
129 _leave(" = 0 (%p)", trans);
133 /* finish cleaning up the transport (not really needed here, but...) */
135 trans->socket->ops->shutdown(trans->socket, 2);
137 /* close the socket */
139 trans->socket->sk->sk_user_data = NULL;
140 sock_release(trans->socket);
141 trans->socket = NULL;
147 _leave(" = %d", ret);
149 } /* end rxrpc_create_transport() */
151 /*****************************************************************************/
153 * clear the connections on a transport endpoint
155 void rxrpc_clear_transport(struct rxrpc_transport *trans)
157 //struct rxrpc_connection *conn;
159 } /* end rxrpc_clear_transport() */
161 /*****************************************************************************/
163 * destroy a transport endpoint
165 void rxrpc_put_transport(struct rxrpc_transport *trans)
167 _enter("%p{u=%d p=%hu}",
168 trans, atomic_read(&trans->usage), trans->port);
170 BUG_ON(atomic_read(&trans->usage) <= 0);
172 /* to prevent a race, the decrement and the dequeue must be
173 * effectively atomic */
174 spin_lock(&rxrpc_transports_lock);
175 if (likely(!atomic_dec_and_test(&trans->usage))) {
176 spin_unlock(&rxrpc_transports_lock);
181 list_del(&trans->link);
182 spin_unlock(&rxrpc_transports_lock);
184 /* finish cleaning up the transport */
186 trans->socket->ops->shutdown(trans->socket, 2);
188 rxrpc_krxsecd_clear_transport(trans);
189 rxrpc_krxiod_dequeue_transport(trans);
191 /* discard all peer information */
192 rxrpc_peer_clearall(trans);
194 down_write(&rxrpc_proc_transports_sem);
195 list_del(&trans->proc_link);
196 up_write(&rxrpc_proc_transports_sem);
197 __RXACCT(atomic_dec(&rxrpc_transport_count));
199 /* close the socket */
201 trans->socket->sk->sk_user_data = NULL;
202 sock_release(trans->socket);
203 trans->socket = NULL;
209 } /* end rxrpc_put_transport() */
211 /*****************************************************************************/
213 * add a service to a transport to be listened upon
215 int rxrpc_add_service(struct rxrpc_transport *trans,
216 struct rxrpc_service *newsrv)
218 struct rxrpc_service *srv;
219 struct list_head *_p;
222 _enter("%p{%hu},%p{%hu}",
223 trans, trans->port, newsrv, newsrv->service_id);
225 /* verify that the service ID is not already present */
226 spin_lock(&trans->lock);
228 list_for_each(_p, &trans->services) {
229 srv = list_entry(_p, struct rxrpc_service, link);
230 if (srv->service_id == newsrv->service_id)
234 /* okay - add the transport to the list */
235 list_add_tail(&newsrv->link, &trans->services);
236 rxrpc_get_transport(trans);
240 spin_unlock(&trans->lock);
244 } /* end rxrpc_add_service() */
246 /*****************************************************************************/
248 * remove a service from a transport
250 void rxrpc_del_service(struct rxrpc_transport *trans, struct rxrpc_service *srv)
252 _enter("%p{%hu},%p{%hu}", trans, trans->port, srv, srv->service_id);
254 spin_lock(&trans->lock);
255 list_del(&srv->link);
256 spin_unlock(&trans->lock);
258 rxrpc_put_transport(trans);
261 } /* end rxrpc_del_service() */
263 /*****************************************************************************/
265 * INET callback when data has been received on the socket.
267 static void rxrpc_data_ready(struct sock *sk, int count)
269 struct rxrpc_transport *trans;
271 _enter("%p{t=%p},%d", sk, sk->sk_user_data, count);
273 /* queue the transport for attention by krxiod */
274 trans = (struct rxrpc_transport *) sk->sk_user_data;
276 rxrpc_krxiod_queue_transport(trans);
278 /* wake up anyone waiting on the socket */
279 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
280 wake_up_interruptible(sk->sk_sleep);
283 } /* end rxrpc_data_ready() */
285 /*****************************************************************************/
287 * INET callback when an ICMP error packet is received
288 * - sk->err is error (EHOSTUNREACH, EPROTO or EMSGSIZE)
290 static void rxrpc_error_report(struct sock *sk)
292 struct rxrpc_transport *trans;
294 _enter("%p{t=%p}", sk, sk->sk_user_data);
296 /* queue the transport for attention by krxiod */
297 trans = (struct rxrpc_transport *) sk->sk_user_data;
299 trans->error_rcvd = 1;
300 rxrpc_krxiod_queue_transport(trans);
303 /* wake up anyone waiting on the socket */
304 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
305 wake_up_interruptible(sk->sk_sleep);
308 } /* end rxrpc_error_report() */
310 /*****************************************************************************/
312 * split a message up, allocating message records and filling them in
313 * from the contents of a socket buffer
315 static int rxrpc_incoming_msg(struct rxrpc_transport *trans,
317 struct list_head *msgq)
319 struct rxrpc_message *msg;
324 msg = kmalloc(sizeof(struct rxrpc_message), GFP_KERNEL);
326 _leave(" = -ENOMEM");
330 memset(msg, 0, sizeof(*msg));
331 atomic_set(&msg->usage, 1);
332 list_add_tail(&msg->link,msgq);
334 /* dig out the Rx routing parameters */
335 if (skb_copy_bits(pkt, sizeof(struct udphdr),
336 &msg->hdr, sizeof(msg->hdr)) < 0) {
342 msg->state = RXRPC_MSG_RECEIVED;
343 msg->stamp = pkt->stamp;
344 if (msg->stamp.tv_sec == 0) {
345 do_gettimeofday(&msg->stamp);
347 sock_enable_timestamp(pkt->sk);
349 msg->seq = ntohl(msg->hdr.seq);
351 /* attach the packet */
355 msg->offset = sizeof(struct udphdr) + sizeof(struct rxrpc_header);
356 msg->dsize = msg->pkt->len - msg->offset;
358 _net("Rx Received packet from %s (%08x;%08x,%1x,%d,%s,%02x,%d,%d)",
359 msg->hdr.flags & RXRPC_CLIENT_INITIATED ? "client" : "server",
360 ntohl(msg->hdr.epoch),
361 (ntohl(msg->hdr.cid) & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT,
362 ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK,
363 ntohl(msg->hdr.callNumber),
364 rxrpc_pkts[msg->hdr.type],
366 ntohs(msg->hdr.serviceId),
367 msg->hdr.securityIndex);
369 __RXACCT(atomic_inc(&rxrpc_message_count));
371 /* split off jumbo packets */
372 while (msg->hdr.type == RXRPC_PACKET_TYPE_DATA &&
373 msg->hdr.flags & RXRPC_JUMBO_PACKET
375 struct rxrpc_jumbo_header jumbo;
376 struct rxrpc_message *jumbomsg = msg;
378 _debug("split jumbo packet");
380 /* quick sanity check */
383 RXRPC_JUMBO_DATALEN + sizeof(struct rxrpc_jumbo_header))
385 if (msg->hdr.flags & RXRPC_LAST_PACKET)
388 /* dig out the secondary header */
389 if (skb_copy_bits(pkt, msg->offset + RXRPC_JUMBO_DATALEN,
390 &jumbo, sizeof(jumbo)) < 0)
393 /* allocate a new message record */
395 msg = kmalloc(sizeof(struct rxrpc_message), GFP_KERNEL);
399 memcpy(msg, jumbomsg, sizeof(*msg));
400 list_add_tail(&msg->link, msgq);
402 /* adjust the jumbo packet */
403 jumbomsg->dsize = RXRPC_JUMBO_DATALEN;
405 /* attach the packet here too */
408 /* adjust the parameters */
410 msg->hdr.seq = htonl(msg->seq);
411 msg->hdr.serial = htonl(ntohl(msg->hdr.serial) + 1);
412 msg->offset += RXRPC_JUMBO_DATALEN +
413 sizeof(struct rxrpc_jumbo_header);
414 msg->dsize -= RXRPC_JUMBO_DATALEN +
415 sizeof(struct rxrpc_jumbo_header);
416 msg->hdr.flags = jumbo.flags;
417 msg->hdr._rsvd = jumbo._rsvd;
419 _net("Rx Split jumbo packet from %s"
420 " (%08x;%08x,%1x,%d,%s,%02x,%d,%d)",
421 msg->hdr.flags & RXRPC_CLIENT_INITIATED ? "client" : "server",
422 ntohl(msg->hdr.epoch),
423 (ntohl(msg->hdr.cid) & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT,
424 ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK,
425 ntohl(msg->hdr.callNumber),
426 rxrpc_pkts[msg->hdr.type],
428 ntohs(msg->hdr.serviceId),
429 msg->hdr.securityIndex);
431 __RXACCT(atomic_inc(&rxrpc_message_count));
434 _leave(" = 0 #%d", atomic_read(&rxrpc_message_count));
438 while (!list_empty(msgq)) {
439 msg = list_entry(msgq->next, struct rxrpc_message, link);
440 list_del_init(&msg->link);
442 rxrpc_put_message(msg);
445 _leave(" = %d", ret);
447 } /* end rxrpc_incoming_msg() */
449 /*****************************************************************************/
452 * - called from krxiod in process context
454 void rxrpc_trans_receive_packet(struct rxrpc_transport *trans)
456 struct rxrpc_message *msg;
457 struct rxrpc_peer *peer;
465 _enter("%p{%d}", trans, trans->port);
468 /* deal with outstanting errors first */
469 if (trans->error_rcvd)
470 rxrpc_trans_receive_error_report(trans);
472 /* attempt to receive a packet */
473 pkt = skb_recv_datagram(trans->socket->sk, 0, 1, &ret);
475 if (ret == -EAGAIN) {
480 /* an icmp error may have occurred */
481 rxrpc_krxiod_queue_transport(trans);
482 _leave(" error %d\n", ret);
486 /* we'll probably need to checksum it (didn't call
488 if (pkt->ip_summed != CHECKSUM_UNNECESSARY) {
490 csum_fold(skb_checksum(pkt, 0, pkt->len,
493 rxrpc_krxiod_queue_transport(trans);
494 _leave(" CSUM failed");
499 addr = pkt->nh.iph->saddr;
500 port = pkt->h.uh->source;
502 _net("Rx Received UDP packet from %08x:%04hu",
503 ntohl(addr), ntohs(port));
505 /* unmarshall the Rx parameters and split jumbo packets */
506 ret = rxrpc_incoming_msg(trans, pkt, &msgq);
509 rxrpc_krxiod_queue_transport(trans);
510 _leave(" bad packet");
514 BUG_ON(list_empty(&msgq));
516 msg = list_entry(msgq.next, struct rxrpc_message, link);
518 /* locate the record for the peer from which it
520 ret = rxrpc_peer_lookup(trans, addr, &peer);
522 kdebug("Rx No connections from that peer");
523 rxrpc_trans_immediate_abort(trans, msg, -EINVAL);
527 /* try and find a matching connection */
528 ret = rxrpc_connection_lookup(peer, msg, &msg->conn);
530 kdebug("Rx Unknown Connection");
531 rxrpc_trans_immediate_abort(trans, msg, -EINVAL);
532 rxrpc_put_peer(peer);
535 rxrpc_put_peer(peer);
537 /* deal with the first packet of a new call */
538 if (msg->hdr.flags & RXRPC_CLIENT_INITIATED &&
539 msg->hdr.type == RXRPC_PACKET_TYPE_DATA &&
540 ntohl(msg->hdr.seq) == 1
542 _debug("Rx New server call");
543 rxrpc_trans_receive_new_call(trans, &msgq);
547 /* deal with subsequent packet(s) of call */
548 _debug("Rx Call packet");
549 while (!list_empty(&msgq)) {
550 msg = list_entry(msgq.next, struct rxrpc_message, link);
551 list_del_init(&msg->link);
553 ret = rxrpc_conn_receive_call_packet(msg->conn, NULL, msg);
555 rxrpc_trans_immediate_abort(trans, msg, ret);
556 rxrpc_put_message(msg);
560 rxrpc_put_message(msg);
565 /* dispose of the packets */
567 while (!list_empty(&msgq)) {
568 msg = list_entry(msgq.next, struct rxrpc_message, link);
569 list_del_init(&msg->link);
571 rxrpc_put_message(msg);
578 } /* end rxrpc_trans_receive_packet() */
580 /*****************************************************************************/
582 * accept a new call from a client trying to connect to one of my services
583 * - called in process context
585 static int rxrpc_trans_receive_new_call(struct rxrpc_transport *trans,
586 struct list_head *msgq)
588 struct rxrpc_message *msg;
592 /* only bother with the first packet */
593 msg = list_entry(msgq->next, struct rxrpc_message, link);
594 list_del_init(&msg->link);
595 rxrpc_krxsecd_queue_incoming_call(msg);
596 rxrpc_put_message(msg);
601 } /* end rxrpc_trans_receive_new_call() */
603 /*****************************************************************************/
605 * perform an immediate abort without connection or call structures
607 int rxrpc_trans_immediate_abort(struct rxrpc_transport *trans,
608 struct rxrpc_message *msg,
611 struct rxrpc_header ahdr;
612 struct sockaddr_in sin;
613 struct msghdr msghdr;
618 _enter("%p,%p,%d", trans, msg, error);
620 /* don't abort an abort packet */
621 if (msg->hdr.type == RXRPC_PACKET_TYPE_ABORT) {
626 _error = htonl(-error);
628 /* set up the message to be transmitted */
629 memcpy(&ahdr, &msg->hdr, sizeof(ahdr));
630 ahdr.epoch = msg->hdr.epoch;
631 ahdr.serial = htonl(1);
633 ahdr.type = RXRPC_PACKET_TYPE_ABORT;
634 ahdr.flags = RXRPC_LAST_PACKET;
635 ahdr.flags |= ~msg->hdr.flags & RXRPC_CLIENT_INITIATED;
637 iov[0].iov_len = sizeof(ahdr);
638 iov[0].iov_base = &ahdr;
639 iov[1].iov_len = sizeof(_error);
640 iov[1].iov_base = &_error;
642 len = sizeof(ahdr) + sizeof(_error);
644 memset(&sin,0,sizeof(sin));
645 sin.sin_family = AF_INET;
646 sin.sin_port = msg->pkt->h.uh->source;
647 sin.sin_addr.s_addr = msg->pkt->nh.iph->saddr;
649 msghdr.msg_name = &sin;
650 msghdr.msg_namelen = sizeof(sin);
651 msghdr.msg_control = NULL;
652 msghdr.msg_controllen = 0;
653 msghdr.msg_flags = MSG_DONTWAIT;
655 _net("Sending message type %d of %d bytes to %08x:%d",
658 ntohl(sin.sin_addr.s_addr),
659 ntohs(sin.sin_port));
661 /* send the message */
662 ret = kernel_sendmsg(trans->socket, &msghdr, iov, 2, len);
664 _leave(" = %d", ret);
666 } /* end rxrpc_trans_immediate_abort() */
668 /*****************************************************************************/
670 * receive an ICMP error report and percolate it to all connections
671 * heading to the affected host or port
673 static void rxrpc_trans_receive_error_report(struct rxrpc_transport *trans)
675 struct rxrpc_connection *conn;
676 struct sockaddr_in sin;
677 struct rxrpc_peer *peer;
678 struct list_head connq, *_p;
679 struct errormsg emsg;
687 trans->error_rcvd = 0;
689 /* try and receive an error message */
691 msg.msg_namelen = sizeof(sin);
692 msg.msg_control = &emsg;
693 msg.msg_controllen = sizeof(emsg);
696 err = kernel_recvmsg(trans->socket, &msg, NULL, 0, 0,
697 MSG_ERRQUEUE | MSG_DONTWAIT | MSG_TRUNC);
699 if (err == -EAGAIN) {
705 printk("%s: unable to recv an error report: %d\n",
711 msg.msg_controllen = (char *) msg.msg_control - (char *) &emsg;
713 if (msg.msg_controllen < sizeof(emsg.cmsg) ||
714 msg.msg_namelen < sizeof(sin)) {
715 printk("%s: short control message"
716 " (nlen=%u clen=%Zu fl=%x)\n",
724 _net("Rx Received control message"
725 " { len=%Zu level=%u type=%u }",
727 emsg.cmsg.cmsg_level,
728 emsg.cmsg.cmsg_type);
730 if (sin.sin_family != AF_INET) {
731 printk("Rx Ignoring error report with non-INET address"
737 _net("Rx Received message pertaining to host addr=%x port=%hu",
738 ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
740 if (emsg.cmsg.cmsg_level != SOL_IP ||
741 emsg.cmsg.cmsg_type != IP_RECVERR) {
742 printk("Rx Ignoring unknown error report"
743 " { level=%u type=%u }",
744 emsg.cmsg.cmsg_level,
745 emsg.cmsg.cmsg_type);
749 if (msg.msg_controllen < sizeof(emsg.cmsg) + sizeof(emsg.ee)) {
750 printk("%s: short error message (%Zu)\n",
751 __FUNCTION__, msg.msg_controllen);
758 switch (emsg.ee.ee_origin) {
759 case SO_EE_ORIGIN_ICMP:
761 switch (emsg.ee.ee_type) {
762 case ICMP_DEST_UNREACH:
763 switch (emsg.ee.ee_code) {
764 case ICMP_NET_UNREACH:
765 _net("Rx Received ICMP Network Unreachable");
769 case ICMP_HOST_UNREACH:
770 _net("Rx Received ICMP Host Unreachable");
774 case ICMP_PORT_UNREACH:
775 _net("Rx Received ICMP Port Unreachable");
778 case ICMP_NET_UNKNOWN:
779 _net("Rx Received ICMP Unknown Network");
783 case ICMP_HOST_UNKNOWN:
784 _net("Rx Received ICMP Unknown Host");
789 _net("Rx Received ICMP DestUnreach { code=%u }",
791 err = emsg.ee.ee_errno;
796 case ICMP_TIME_EXCEEDED:
797 _net("Rx Received ICMP TTL Exceeded");
798 err = emsg.ee.ee_errno;
802 _proto("Rx Received ICMP error { type=%u code=%u }",
803 emsg.ee.ee_type, emsg.ee.ee_code);
804 err = emsg.ee.ee_errno;
809 case SO_EE_ORIGIN_LOCAL:
810 _proto("Rx Received local error { error=%d }",
813 err = emsg.ee.ee_errno;
816 case SO_EE_ORIGIN_NONE:
817 case SO_EE_ORIGIN_ICMP6:
819 _proto("Rx Received error report { orig=%u }",
822 err = emsg.ee.ee_errno;
826 /* find all the connections between this transport and the
827 * affected destination */
828 INIT_LIST_HEAD(&connq);
830 if (rxrpc_peer_lookup(trans, sin.sin_addr.s_addr,
832 read_lock(&peer->conn_lock);
833 list_for_each(_p, &peer->conn_active) {
834 conn = list_entry(_p, struct rxrpc_connection,
836 if (port && conn->addr.sin_port != port)
838 if (!list_empty(&conn->err_link))
841 rxrpc_get_connection(conn);
842 list_add_tail(&conn->err_link, &connq);
844 read_unlock(&peer->conn_lock);
846 /* service all those connections */
847 while (!list_empty(&connq)) {
848 conn = list_entry(connq.next,
849 struct rxrpc_connection,
851 list_del(&conn->err_link);
853 rxrpc_conn_handle_error(conn, local, err);
855 rxrpc_put_connection(conn);
858 rxrpc_put_peer(peer);
864 } /* end rxrpc_trans_receive_error_report() */