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