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