[PATCH] remove many unneeded #includes of sched.h
[linux-2.6.git] / net / rxrpc / transport.c
1 /* transport.c: Rx Transport routines
2  *
3  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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.
10  */
11
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>
22 #include <linux/in.h>
23 #include <linux/in6.h>
24 #include <linux/icmp.h>
25 #include <linux/skbuff.h>
26 #include <net/sock.h>
27 #include <net/ip.h>
28 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
29 #include <linux/ipv6.h> /* this should _really_ be in errqueue.h.. */
30 #endif
31 #include <linux/errqueue.h>
32 #include <asm/uaccess.h>
33 #include "internal.h"
34
35 struct errormsg {
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 */
39 };
40
41 static DEFINE_SPINLOCK(rxrpc_transports_lock);
42 static struct list_head rxrpc_transports = LIST_HEAD_INIT(rxrpc_transports);
43
44 __RXACCT_DECL(atomic_t rxrpc_transport_count);
45 LIST_HEAD(rxrpc_proc_transports);
46 DECLARE_RWSEM(rxrpc_proc_transports_sem);
47
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);
53
54 /*****************************************************************************/
55 /*
56  * create a new transport endpoint using the specified UDP port
57  */
58 int rxrpc_create_transport(unsigned short port,
59                            struct rxrpc_transport **_trans)
60 {
61         struct rxrpc_transport *trans;
62         struct sockaddr_in sin;
63         mm_segment_t oldfs;
64         struct sock *sock;
65         int ret, opt;
66
67         _enter("%hu", port);
68
69         trans = kzalloc(sizeof(struct rxrpc_transport), GFP_KERNEL);
70         if (!trans)
71                 return -ENOMEM;
72
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);
84         trans->port = port;
85
86         /* create a UDP socket to be my actual transport endpoint */
87         ret = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &trans->socket);
88         if (ret < 0)
89                 goto error;
90
91         /* use the specified port */
92         if (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,
98                                                sizeof(sin));
99                 if (ret < 0)
100                         goto error;
101         }
102
103         opt = 1;
104         oldfs = get_fs();
105         set_fs(KERNEL_DS);
106         ret = trans->socket->ops->setsockopt(trans->socket, SOL_IP, IP_RECVERR,
107                                              (char *) &opt, sizeof(opt));
108         set_fs(oldfs);
109
110         spin_lock(&rxrpc_transports_lock);
111         list_add(&trans->link, &rxrpc_transports);
112         spin_unlock(&rxrpc_transports_lock);
113
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;
119
120         down_write(&rxrpc_proc_transports_sem);
121         list_add_tail(&trans->proc_link, &rxrpc_proc_transports);
122         up_write(&rxrpc_proc_transports_sem);
123
124         __RXACCT(atomic_inc(&rxrpc_transport_count));
125
126         *_trans = trans;
127         _leave(" = 0 (%p)", trans);
128         return 0;
129
130  error:
131         /* finish cleaning up the transport (not really needed here, but...) */
132         if (trans->socket)
133                 trans->socket->ops->shutdown(trans->socket, 2);
134
135         /* close the socket */
136         if (trans->socket) {
137                 trans->socket->sk->sk_user_data = NULL;
138                 sock_release(trans->socket);
139                 trans->socket = NULL;
140         }
141
142         kfree(trans);
143
144
145         _leave(" = %d", ret);
146         return ret;
147 } /* end rxrpc_create_transport() */
148
149 /*****************************************************************************/
150 /*
151  * destroy a transport endpoint
152  */
153 void rxrpc_put_transport(struct rxrpc_transport *trans)
154 {
155         _enter("%p{u=%d p=%hu}",
156                trans, atomic_read(&trans->usage), trans->port);
157
158         BUG_ON(atomic_read(&trans->usage) <= 0);
159
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);
165                 _leave("");
166                 return;
167         }
168
169         list_del(&trans->link);
170         spin_unlock(&rxrpc_transports_lock);
171
172         /* finish cleaning up the transport */
173         if (trans->socket)
174                 trans->socket->ops->shutdown(trans->socket, 2);
175
176         rxrpc_krxsecd_clear_transport(trans);
177         rxrpc_krxiod_dequeue_transport(trans);
178
179         /* discard all peer information */
180         rxrpc_peer_clearall(trans);
181
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));
186
187         /* close the socket */
188         if (trans->socket) {
189                 trans->socket->sk->sk_user_data = NULL;
190                 sock_release(trans->socket);
191                 trans->socket = NULL;
192         }
193
194         kfree(trans);
195
196         _leave("");
197 } /* end rxrpc_put_transport() */
198
199 /*****************************************************************************/
200 /*
201  * add a service to a transport to be listened upon
202  */
203 int rxrpc_add_service(struct rxrpc_transport *trans,
204                       struct rxrpc_service *newsrv)
205 {
206         struct rxrpc_service *srv;
207         struct list_head *_p;
208         int ret = -EEXIST;
209
210         _enter("%p{%hu},%p{%hu}",
211                trans, trans->port, newsrv, newsrv->service_id);
212
213         /* verify that the service ID is not already present */
214         spin_lock(&trans->lock);
215
216         list_for_each(_p, &trans->services) {
217                 srv = list_entry(_p, struct rxrpc_service, link);
218                 if (srv->service_id == newsrv->service_id)
219                         goto out;
220         }
221
222         /* okay - add the transport to the list */
223         list_add_tail(&newsrv->link, &trans->services);
224         rxrpc_get_transport(trans);
225         ret = 0;
226
227  out:
228         spin_unlock(&trans->lock);
229
230         _leave("= %d", ret);
231         return ret;
232 } /* end rxrpc_add_service() */
233
234 /*****************************************************************************/
235 /*
236  * remove a service from a transport
237  */
238 void rxrpc_del_service(struct rxrpc_transport *trans, struct rxrpc_service *srv)
239 {
240         _enter("%p{%hu},%p{%hu}", trans, trans->port, srv, srv->service_id);
241
242         spin_lock(&trans->lock);
243         list_del(&srv->link);
244         spin_unlock(&trans->lock);
245
246         rxrpc_put_transport(trans);
247
248         _leave("");
249 } /* end rxrpc_del_service() */
250
251 /*****************************************************************************/
252 /*
253  * INET callback when data has been received on the socket.
254  */
255 static void rxrpc_data_ready(struct sock *sk, int count)
256 {
257         struct rxrpc_transport *trans;
258
259         _enter("%p{t=%p},%d", sk, sk->sk_user_data, count);
260
261         /* queue the transport for attention by krxiod */
262         trans = (struct rxrpc_transport *) sk->sk_user_data;
263         if (trans)
264                 rxrpc_krxiod_queue_transport(trans);
265
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);
269
270         _leave("");
271 } /* end rxrpc_data_ready() */
272
273 /*****************************************************************************/
274 /*
275  * INET callback when an ICMP error packet is received
276  * - sk->err is error (EHOSTUNREACH, EPROTO or EMSGSIZE)
277  */
278 static void rxrpc_error_report(struct sock *sk)
279 {
280         struct rxrpc_transport *trans;
281
282         _enter("%p{t=%p}", sk, sk->sk_user_data);
283
284         /* queue the transport for attention by krxiod */
285         trans = (struct rxrpc_transport *) sk->sk_user_data;
286         if (trans) {
287                 trans->error_rcvd = 1;
288                 rxrpc_krxiod_queue_transport(trans);
289         }
290
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);
294
295         _leave("");
296 } /* end rxrpc_error_report() */
297
298 /*****************************************************************************/
299 /*
300  * split a message up, allocating message records and filling them in
301  * from the contents of a socket buffer
302  */
303 static int rxrpc_incoming_msg(struct rxrpc_transport *trans,
304                               struct sk_buff *pkt,
305                               struct list_head *msgq)
306 {
307         struct rxrpc_message *msg;
308         int ret;
309
310         _enter("");
311
312         msg = kzalloc(sizeof(struct rxrpc_message), GFP_KERNEL);
313         if (!msg) {
314                 _leave(" = -ENOMEM");
315                 return -ENOMEM;
316         }
317
318         atomic_set(&msg->usage, 1);
319         list_add_tail(&msg->link,msgq);
320
321         /* dig out the Rx routing parameters */
322         if (skb_copy_bits(pkt, sizeof(struct udphdr),
323                           &msg->hdr, sizeof(msg->hdr)) < 0) {
324                 ret = -EBADMSG;
325                 goto error;
326         }
327
328         msg->trans = trans;
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);
333                 if (pkt->sk)
334                         sock_enable_timestamp(pkt->sk);
335         }
336         msg->seq = ntohl(msg->hdr.seq);
337
338         /* attach the packet */
339         skb_get(pkt);
340         msg->pkt = pkt;
341
342         msg->offset = sizeof(struct udphdr) + sizeof(struct rxrpc_header);
343         msg->dsize = msg->pkt->len - msg->offset;
344
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],
352              msg->hdr.flags,
353              ntohs(msg->hdr.serviceId),
354              msg->hdr.securityIndex);
355
356         __RXACCT(atomic_inc(&rxrpc_message_count));
357
358         /* split off jumbo packets */
359         while (msg->hdr.type == RXRPC_PACKET_TYPE_DATA &&
360                msg->hdr.flags & RXRPC_JUMBO_PACKET
361                ) {
362                 struct rxrpc_jumbo_header jumbo;
363                 struct rxrpc_message *jumbomsg = msg;
364
365                 _debug("split jumbo packet");
366
367                 /* quick sanity check */
368                 ret = -EBADMSG;
369                 if (msg->dsize <
370                     RXRPC_JUMBO_DATALEN + sizeof(struct rxrpc_jumbo_header))
371                         goto error;
372                 if (msg->hdr.flags & RXRPC_LAST_PACKET)
373                         goto error;
374
375                 /* dig out the secondary header */
376                 if (skb_copy_bits(pkt, msg->offset + RXRPC_JUMBO_DATALEN,
377                                   &jumbo, sizeof(jumbo)) < 0)
378                         goto error;
379
380                 /* allocate a new message record */
381                 ret = -ENOMEM;
382                 msg = kmemdup(jumbomsg, sizeof(struct rxrpc_message), GFP_KERNEL);
383                 if (!msg)
384                         goto error;
385
386                 list_add_tail(&msg->link, msgq);
387
388                 /* adjust the jumbo packet */
389                 jumbomsg->dsize = RXRPC_JUMBO_DATALEN;
390
391                 /* attach the packet here too */
392                 skb_get(pkt);
393
394                 /* adjust the parameters */
395                 msg->seq++;
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;
404
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],
413                      msg->hdr.flags,
414                      ntohs(msg->hdr.serviceId),
415                      msg->hdr.securityIndex);
416
417                 __RXACCT(atomic_inc(&rxrpc_message_count));
418         }
419
420         _leave(" = 0 #%d", atomic_read(&rxrpc_message_count));
421         return 0;
422
423  error:
424         while (!list_empty(msgq)) {
425                 msg = list_entry(msgq->next, struct rxrpc_message, link);
426                 list_del_init(&msg->link);
427
428                 rxrpc_put_message(msg);
429         }
430
431         _leave(" = %d", ret);
432         return ret;
433 } /* end rxrpc_incoming_msg() */
434
435 /*****************************************************************************/
436 /*
437  * accept a new call
438  * - called from krxiod in process context
439  */
440 void rxrpc_trans_receive_packet(struct rxrpc_transport *trans)
441 {
442         struct rxrpc_message *msg;
443         struct rxrpc_peer *peer;
444         struct sk_buff *pkt;
445         int ret;
446         __be32 addr;
447         __be16 port;
448
449         LIST_HEAD(msgq);
450
451         _enter("%p{%d}", trans, trans->port);
452
453         for (;;) {
454                 /* deal with outstanting errors first */
455                 if (trans->error_rcvd)
456                         rxrpc_trans_receive_error_report(trans);
457
458                 /* attempt to receive a packet */
459                 pkt = skb_recv_datagram(trans->socket->sk, 0, 1, &ret);
460                 if (!pkt) {
461                         if (ret == -EAGAIN) {
462                                 _leave(" EAGAIN");
463                                 return;
464                         }
465
466                         /* an icmp error may have occurred */
467                         rxrpc_krxiod_queue_transport(trans);
468                         _leave(" error %d\n", ret);
469                         return;
470                 }
471
472                 /* we'll probably need to checksum it (didn't call
473                  * sock_recvmsg) */
474                 if (skb_checksum_complete(pkt)) {
475                         kfree_skb(pkt);
476                         rxrpc_krxiod_queue_transport(trans);
477                         _leave(" CSUM failed");
478                         return;
479                 }
480
481                 addr = pkt->nh.iph->saddr;
482                 port = pkt->h.uh->source;
483
484                 _net("Rx Received UDP packet from %08x:%04hu",
485                      ntohl(addr), ntohs(port));
486
487                 /* unmarshall the Rx parameters and split jumbo packets */
488                 ret = rxrpc_incoming_msg(trans, pkt, &msgq);
489                 if (ret < 0) {
490                         kfree_skb(pkt);
491                         rxrpc_krxiod_queue_transport(trans);
492                         _leave(" bad packet");
493                         return;
494                 }
495
496                 BUG_ON(list_empty(&msgq));
497
498                 msg = list_entry(msgq.next, struct rxrpc_message, link);
499
500                 /* locate the record for the peer from which it
501                  * originated */
502                 ret = rxrpc_peer_lookup(trans, addr, &peer);
503                 if (ret < 0) {
504                         kdebug("Rx No connections from that peer");
505                         rxrpc_trans_immediate_abort(trans, msg, -EINVAL);
506                         goto finished_msg;
507                 }
508
509                 /* try and find a matching connection */
510                 ret = rxrpc_connection_lookup(peer, msg, &msg->conn);
511                 if (ret < 0) {
512                         kdebug("Rx Unknown Connection");
513                         rxrpc_trans_immediate_abort(trans, msg, -EINVAL);
514                         rxrpc_put_peer(peer);
515                         goto finished_msg;
516                 }
517                 rxrpc_put_peer(peer);
518
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
523                     ) {
524                         _debug("Rx New server call");
525                         rxrpc_trans_receive_new_call(trans, &msgq);
526                         goto finished_msg;
527                 }
528
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);
534
535                         ret = rxrpc_conn_receive_call_packet(msg->conn, NULL, msg);
536                         if (ret < 0) {
537                                 rxrpc_trans_immediate_abort(trans, msg, ret);
538                                 rxrpc_put_message(msg);
539                                 goto finished_msg;
540                         }
541
542                         rxrpc_put_message(msg);
543                 }
544
545                 goto finished_msg;
546
547                 /* dispose of the packets */
548         finished_msg:
549                 while (!list_empty(&msgq)) {
550                         msg = list_entry(msgq.next, struct rxrpc_message, link);
551                         list_del_init(&msg->link);
552
553                         rxrpc_put_message(msg);
554                 }
555                 kfree_skb(pkt);
556         }
557
558         _leave("");
559
560 } /* end rxrpc_trans_receive_packet() */
561
562 /*****************************************************************************/
563 /*
564  * accept a new call from a client trying to connect to one of my services
565  * - called in process context
566  */
567 static int rxrpc_trans_receive_new_call(struct rxrpc_transport *trans,
568                                         struct list_head *msgq)
569 {
570         struct rxrpc_message *msg;
571
572         _enter("");
573
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);
579
580         _leave(" = 0");
581
582         return 0;
583 } /* end rxrpc_trans_receive_new_call() */
584
585 /*****************************************************************************/
586 /*
587  * perform an immediate abort without connection or call structures
588  */
589 int rxrpc_trans_immediate_abort(struct rxrpc_transport *trans,
590                                 struct rxrpc_message *msg,
591                                 int error)
592 {
593         struct rxrpc_header ahdr;
594         struct sockaddr_in sin;
595         struct msghdr msghdr;
596         struct kvec iov[2];
597         __be32 _error;
598         int len, ret;
599
600         _enter("%p,%p,%d", trans, msg, error);
601
602         /* don't abort an abort packet */
603         if (msg->hdr.type == RXRPC_PACKET_TYPE_ABORT) {
604                 _leave(" = 0");
605                 return 0;
606         }
607
608         _error = htonl(-error);
609
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);
614         ahdr.seq        = 0;
615         ahdr.type       = RXRPC_PACKET_TYPE_ABORT;
616         ahdr.flags      = RXRPC_LAST_PACKET;
617         ahdr.flags      |= ~msg->hdr.flags & RXRPC_CLIENT_INITIATED;
618
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;
623
624         len = sizeof(ahdr) + sizeof(_error);
625
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;
630
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;
636
637         _net("Sending message type %d of %d bytes to %08x:%d",
638              ahdr.type,
639              len,
640              ntohl(sin.sin_addr.s_addr),
641              ntohs(sin.sin_port));
642
643         /* send the message */
644         ret = kernel_sendmsg(trans->socket, &msghdr, iov, 2, len);
645
646         _leave(" = %d", ret);
647         return ret;
648 } /* end rxrpc_trans_immediate_abort() */
649
650 /*****************************************************************************/
651 /*
652  * receive an ICMP error report and percolate it to all connections
653  * heading to the affected host or port
654  */
655 static void rxrpc_trans_receive_error_report(struct rxrpc_transport *trans)
656 {
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;
662         struct msghdr msg;
663         __be16 port;
664         int local, err;
665
666         _enter("%p", trans);
667
668         for (;;) {
669                 trans->error_rcvd = 0;
670
671                 /* try and receive an error message */
672                 msg.msg_name    = &sin;
673                 msg.msg_namelen = sizeof(sin);
674                 msg.msg_control = &emsg;
675                 msg.msg_controllen = sizeof(emsg);
676                 msg.msg_flags   = 0;
677
678                 err = kernel_recvmsg(trans->socket, &msg, NULL, 0, 0,
679                                    MSG_ERRQUEUE | MSG_DONTWAIT | MSG_TRUNC);
680
681                 if (err == -EAGAIN) {
682                         _leave("");
683                         return;
684                 }
685
686                 if (err < 0) {
687                         printk("%s: unable to recv an error report: %d\n",
688                                __FUNCTION__, err);
689                         _leave("");
690                         return;
691                 }
692
693                 msg.msg_controllen = (char *) msg.msg_control - (char *) &emsg;
694
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",
699                                __FUNCTION__,
700                                msg.msg_namelen,
701                                msg.msg_controllen,
702                                msg.msg_flags);
703                         continue;
704                 }
705
706                 _net("Rx Received control message"
707                      " { len=%Zu level=%u type=%u }",
708                      emsg.cmsg.cmsg_len,
709                      emsg.cmsg.cmsg_level,
710                      emsg.cmsg.cmsg_type);
711
712                 if (sin.sin_family != AF_INET) {
713                         printk("Rx Ignoring error report with non-INET address"
714                                " (fam=%u)",
715                                sin.sin_family);
716                         continue;
717                 }
718
719                 _net("Rx Received message pertaining to host addr=%x port=%hu",
720                      ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
721
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);
728                         continue;
729                 }
730
731                 if (msg.msg_controllen < sizeof(emsg.cmsg) + sizeof(emsg.ee)) {
732                         printk("%s: short error message (%Zu)\n",
733                                __FUNCTION__, msg.msg_controllen);
734                         _leave("");
735                         return;
736                 }
737
738                 port = sin.sin_port;
739
740                 switch (emsg.ee.ee_origin) {
741                 case SO_EE_ORIGIN_ICMP:
742                         local = 0;
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");
748                                         port = 0;
749                                         err = -ENETUNREACH;
750                                         break;
751                                 case ICMP_HOST_UNREACH:
752                                         _net("Rx Received ICMP Host Unreachable");
753                                         port = 0;
754                                         err = -EHOSTUNREACH;
755                                         break;
756                                 case ICMP_PORT_UNREACH:
757                                         _net("Rx Received ICMP Port Unreachable");
758                                         err = -ECONNREFUSED;
759                                         break;
760                                 case ICMP_NET_UNKNOWN:
761                                         _net("Rx Received ICMP Unknown Network");
762                                         port = 0;
763                                         err = -ENETUNREACH;
764                                         break;
765                                 case ICMP_HOST_UNKNOWN:
766                                         _net("Rx Received ICMP Unknown Host");
767                                         port = 0;
768                                         err = -EHOSTUNREACH;
769                                         break;
770                                 default:
771                                         _net("Rx Received ICMP DestUnreach { code=%u }",
772                                              emsg.ee.ee_code);
773                                         err = emsg.ee.ee_errno;
774                                         break;
775                                 }
776                                 break;
777
778                         case ICMP_TIME_EXCEEDED:
779                                 _net("Rx Received ICMP TTL Exceeded");
780                                 err = emsg.ee.ee_errno;
781                                 break;
782
783                         default:
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;
787                                 break;
788                         }
789                         break;
790
791                 case SO_EE_ORIGIN_LOCAL:
792                         _proto("Rx Received local error { error=%d }",
793                                emsg.ee.ee_errno);
794                         local = 1;
795                         err = emsg.ee.ee_errno;
796                         break;
797
798                 case SO_EE_ORIGIN_NONE:
799                 case SO_EE_ORIGIN_ICMP6:
800                 default:
801                         _proto("Rx Received error report { orig=%u }",
802                                emsg.ee.ee_origin);
803                         local = 0;
804                         err = emsg.ee.ee_errno;
805                         break;
806                 }
807
808                 /* find all the connections between this transport and the
809                  * affected destination */
810                 INIT_LIST_HEAD(&connq);
811
812                 if (rxrpc_peer_lookup(trans, sin.sin_addr.s_addr,
813                                       &peer) == 0) {
814                         read_lock(&peer->conn_lock);
815                         list_for_each(_p, &peer->conn_active) {
816                                 conn = list_entry(_p, struct rxrpc_connection,
817                                                   link);
818                                 if (port && conn->addr.sin_port != port)
819                                         continue;
820                                 if (!list_empty(&conn->err_link))
821                                         continue;
822
823                                 rxrpc_get_connection(conn);
824                                 list_add_tail(&conn->err_link, &connq);
825                         }
826                         read_unlock(&peer->conn_lock);
827
828                         /* service all those connections */
829                         while (!list_empty(&connq)) {
830                                 conn = list_entry(connq.next,
831                                                   struct rxrpc_connection,
832                                                   err_link);
833                                 list_del(&conn->err_link);
834
835                                 rxrpc_conn_handle_error(conn, local, err);
836
837                                 rxrpc_put_connection(conn);
838                         }
839
840                         rxrpc_put_peer(peer);
841                 }
842         }
843
844         _leave("");
845         return;
846 } /* end rxrpc_trans_receive_error_report() */