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