[PATCH] PCI: start paying attention to a lot of pci function return values
[linux-2.6] / net / ipv6 / datagram.c
1 /*
2  *      common UDP/RAW code
3  *      Linux INET6 implementation 
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>     
7  *
8  *      $Id: datagram.c,v 1.24 2002/02/01 22:01:04 davem Exp $
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/errno.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/interrupt.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
23 #include <linux/in6.h>
24 #include <linux/ipv6.h>
25 #include <linux/route.h>
26
27 #include <net/ipv6.h>
28 #include <net/ndisc.h>
29 #include <net/addrconf.h>
30 #include <net/transp_v6.h>
31 #include <net/ip6_route.h>
32 #include <net/tcp_states.h>
33
34 #include <linux/errqueue.h>
35 #include <asm/uaccess.h>
36
37 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
38 {
39         struct sockaddr_in6     *usin = (struct sockaddr_in6 *) uaddr;
40         struct inet_sock        *inet = inet_sk(sk);
41         struct ipv6_pinfo       *np = inet6_sk(sk);
42         struct in6_addr         *daddr, *final_p = NULL, final;
43         struct dst_entry        *dst;
44         struct flowi            fl;
45         struct ip6_flowlabel    *flowlabel = NULL;
46         int                     addr_type;
47         int                     err;
48
49         if (usin->sin6_family == AF_INET) {
50                 if (__ipv6_only_sock(sk))
51                         return -EAFNOSUPPORT;
52                 err = ip4_datagram_connect(sk, uaddr, addr_len);
53                 goto ipv4_connected;
54         }
55
56         if (addr_len < SIN6_LEN_RFC2133)
57                 return -EINVAL;
58
59         if (usin->sin6_family != AF_INET6) 
60                 return -EAFNOSUPPORT;
61
62         memset(&fl, 0, sizeof(fl));
63         if (np->sndflow) {
64                 fl.fl6_flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
65                 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
66                         flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
67                         if (flowlabel == NULL)
68                                 return -EINVAL;
69                         ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
70                 }
71         }
72
73         addr_type = ipv6_addr_type(&usin->sin6_addr);
74
75         if (addr_type == IPV6_ADDR_ANY) {
76                 /*
77                  *      connect to self
78                  */
79                 usin->sin6_addr.s6_addr[15] = 0x01;
80         }
81
82         daddr = &usin->sin6_addr;
83
84         if (addr_type == IPV6_ADDR_MAPPED) {
85                 struct sockaddr_in sin;
86
87                 if (__ipv6_only_sock(sk)) {
88                         err = -ENETUNREACH;
89                         goto out;
90                 }
91                 sin.sin_family = AF_INET;
92                 sin.sin_addr.s_addr = daddr->s6_addr32[3];
93                 sin.sin_port = usin->sin6_port;
94
95                 err = ip4_datagram_connect(sk, 
96                                            (struct sockaddr*) &sin, 
97                                            sizeof(sin));
98
99 ipv4_connected:
100                 if (err)
101                         goto out;
102                 
103                 ipv6_addr_set(&np->daddr, 0, 0, htonl(0x0000ffff), inet->daddr);
104
105                 if (ipv6_addr_any(&np->saddr)) {
106                         ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000ffff),
107                                       inet->saddr);
108                 }
109
110                 if (ipv6_addr_any(&np->rcv_saddr)) {
111                         ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000ffff),
112                                       inet->rcv_saddr);
113                 }
114                 goto out;
115         }
116
117         if (addr_type&IPV6_ADDR_LINKLOCAL) {
118                 if (addr_len >= sizeof(struct sockaddr_in6) &&
119                     usin->sin6_scope_id) {
120                         if (sk->sk_bound_dev_if &&
121                             sk->sk_bound_dev_if != usin->sin6_scope_id) {
122                                 err = -EINVAL;
123                                 goto out;
124                         }
125                         sk->sk_bound_dev_if = usin->sin6_scope_id;
126                         if (!sk->sk_bound_dev_if &&
127                             (addr_type & IPV6_ADDR_MULTICAST))
128                                 fl.oif = np->mcast_oif;
129                 }
130
131                 /* Connect to link-local address requires an interface */
132                 if (!sk->sk_bound_dev_if) {
133                         err = -EINVAL;
134                         goto out;
135                 }
136         }
137
138         ipv6_addr_copy(&np->daddr, daddr);
139         np->flow_label = fl.fl6_flowlabel;
140
141         inet->dport = usin->sin6_port;
142
143         /*
144          *      Check for a route to destination an obtain the
145          *      destination cache for it.
146          */
147
148         fl.proto = sk->sk_protocol;
149         ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
150         ipv6_addr_copy(&fl.fl6_src, &np->saddr);
151         fl.oif = sk->sk_bound_dev_if;
152         fl.fl_ip_dport = inet->dport;
153         fl.fl_ip_sport = inet->sport;
154
155         if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST))
156                 fl.oif = np->mcast_oif;
157
158         if (flowlabel) {
159                 if (flowlabel->opt && flowlabel->opt->srcrt) {
160                         struct rt0_hdr *rt0 = (struct rt0_hdr *) flowlabel->opt->srcrt;
161                         ipv6_addr_copy(&final, &fl.fl6_dst);
162                         ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
163                         final_p = &final;
164                 }
165         } else if (np->opt && np->opt->srcrt) {
166                 struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
167                 ipv6_addr_copy(&final, &fl.fl6_dst);
168                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
169                 final_p = &final;
170         }
171
172         err = ip6_dst_lookup(sk, &dst, &fl);
173         if (err)
174                 goto out;
175         if (final_p)
176                 ipv6_addr_copy(&fl.fl6_dst, final_p);
177
178         if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
179                 dst_release(dst);
180                 goto out;
181         }
182
183         /* source address lookup done in ip6_dst_lookup */
184
185         if (ipv6_addr_any(&np->saddr))
186                 ipv6_addr_copy(&np->saddr, &fl.fl6_src);
187
188         if (ipv6_addr_any(&np->rcv_saddr)) {
189                 ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src);
190                 inet->rcv_saddr = LOOPBACK4_IPV6;
191         }
192
193         ip6_dst_store(sk, dst,
194                       ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
195                       &np->daddr : NULL);
196
197         sk->sk_state = TCP_ESTABLISHED;
198 out:
199         fl6_sock_release(flowlabel);
200         return err;
201 }
202
203 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, 
204                      u16 port, u32 info, u8 *payload)
205 {
206         struct ipv6_pinfo *np  = inet6_sk(sk);
207         struct icmp6hdr *icmph = (struct icmp6hdr *)skb->h.raw;
208         struct sock_exterr_skb *serr;
209
210         if (!np->recverr)
211                 return;
212
213         skb = skb_clone(skb, GFP_ATOMIC);
214         if (!skb)
215                 return;
216
217         serr = SKB_EXT_ERR(skb);
218         serr->ee.ee_errno = err;
219         serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
220         serr->ee.ee_type = icmph->icmp6_type; 
221         serr->ee.ee_code = icmph->icmp6_code;
222         serr->ee.ee_pad = 0;
223         serr->ee.ee_info = info;
224         serr->ee.ee_data = 0;
225         serr->addr_offset = (u8*)&(((struct ipv6hdr*)(icmph+1))->daddr) - skb->nh.raw;
226         serr->port = port;
227
228         skb->h.raw = payload;
229         __skb_pull(skb, payload - skb->data);
230
231         if (sock_queue_err_skb(sk, skb))
232                 kfree_skb(skb);
233 }
234
235 void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info)
236 {
237         struct ipv6_pinfo *np = inet6_sk(sk);
238         struct sock_exterr_skb *serr;
239         struct ipv6hdr *iph;
240         struct sk_buff *skb;
241
242         if (!np->recverr)
243                 return;
244
245         skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
246         if (!skb)
247                 return;
248
249         iph = (struct ipv6hdr*)skb_put(skb, sizeof(struct ipv6hdr));
250         skb->nh.ipv6h = iph;
251         ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
252
253         serr = SKB_EXT_ERR(skb);
254         serr->ee.ee_errno = err;
255         serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
256         serr->ee.ee_type = 0; 
257         serr->ee.ee_code = 0;
258         serr->ee.ee_pad = 0;
259         serr->ee.ee_info = info;
260         serr->ee.ee_data = 0;
261         serr->addr_offset = (u8*)&iph->daddr - skb->nh.raw;
262         serr->port = fl->fl_ip_dport;
263
264         skb->h.raw = skb->tail;
265         __skb_pull(skb, skb->tail - skb->data);
266
267         if (sock_queue_err_skb(sk, skb))
268                 kfree_skb(skb);
269 }
270
271 /* 
272  *      Handle MSG_ERRQUEUE
273  */
274 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
275 {
276         struct ipv6_pinfo *np = inet6_sk(sk);
277         struct sock_exterr_skb *serr;
278         struct sk_buff *skb, *skb2;
279         struct sockaddr_in6 *sin;
280         struct {
281                 struct sock_extended_err ee;
282                 struct sockaddr_in6      offender;
283         } errhdr;
284         int err;
285         int copied;
286
287         err = -EAGAIN;
288         skb = skb_dequeue(&sk->sk_error_queue);
289         if (skb == NULL)
290                 goto out;
291
292         copied = skb->len;
293         if (copied > len) {
294                 msg->msg_flags |= MSG_TRUNC;
295                 copied = len;
296         }
297         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
298         if (err)
299                 goto out_free_skb;
300
301         sock_recv_timestamp(msg, sk, skb);
302
303         serr = SKB_EXT_ERR(skb);
304
305         sin = (struct sockaddr_in6 *)msg->msg_name;
306         if (sin) {
307                 sin->sin6_family = AF_INET6;
308                 sin->sin6_flowinfo = 0;
309                 sin->sin6_port = serr->port; 
310                 sin->sin6_scope_id = 0;
311                 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) {
312                         ipv6_addr_copy(&sin->sin6_addr,
313                           (struct in6_addr *)(skb->nh.raw + serr->addr_offset));
314                         if (np->sndflow)
315                                 sin->sin6_flowinfo = *(u32*)(skb->nh.raw + serr->addr_offset - 24) & IPV6_FLOWINFO_MASK;
316                         if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
317                                 sin->sin6_scope_id = IP6CB(skb)->iif;
318                 } else {
319                         ipv6_addr_set(&sin->sin6_addr, 0, 0,
320                                       htonl(0xffff),
321                                       *(u32*)(skb->nh.raw + serr->addr_offset));
322                 }
323         }
324
325         memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
326         sin = &errhdr.offender;
327         sin->sin6_family = AF_UNSPEC;
328         if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
329                 sin->sin6_family = AF_INET6;
330                 sin->sin6_flowinfo = 0;
331                 sin->sin6_scope_id = 0;
332                 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) {
333                         ipv6_addr_copy(&sin->sin6_addr, &skb->nh.ipv6h->saddr);
334                         if (np->rxopt.all)
335                                 datagram_recv_ctl(sk, msg, skb);
336                         if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
337                                 sin->sin6_scope_id = IP6CB(skb)->iif;
338                 } else {
339                         struct inet_sock *inet = inet_sk(sk);
340
341                         ipv6_addr_set(&sin->sin6_addr, 0, 0,
342                                       htonl(0xffff),
343                                       skb->nh.iph->saddr);
344                         if (inet->cmsg_flags)
345                                 ip_cmsg_recv(msg, skb);
346                 }
347         }
348
349         put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
350
351         /* Now we could try to dump offended packet options */
352
353         msg->msg_flags |= MSG_ERRQUEUE;
354         err = copied;
355
356         /* Reset and regenerate socket error */
357         spin_lock_bh(&sk->sk_error_queue.lock);
358         sk->sk_err = 0;
359         if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
360                 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
361                 spin_unlock_bh(&sk->sk_error_queue.lock);
362                 sk->sk_error_report(sk);
363         } else {
364                 spin_unlock_bh(&sk->sk_error_queue.lock);
365         }
366
367 out_free_skb:   
368         kfree_skb(skb);
369 out:
370         return err;
371 }
372
373
374
375 int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
376 {
377         struct ipv6_pinfo *np = inet6_sk(sk);
378         struct inet6_skb_parm *opt = IP6CB(skb);
379
380         if (np->rxopt.bits.rxinfo) {
381                 struct in6_pktinfo src_info;
382
383                 src_info.ipi6_ifindex = opt->iif;
384                 ipv6_addr_copy(&src_info.ipi6_addr, &skb->nh.ipv6h->daddr);
385                 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
386         }
387
388         if (np->rxopt.bits.rxhlim) {
389                 int hlim = skb->nh.ipv6h->hop_limit;
390                 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
391         }
392
393         if (np->rxopt.bits.rxflow && (*(u32*)skb->nh.raw & IPV6_FLOWINFO_MASK)) {
394                 u32 flowinfo = *(u32*)skb->nh.raw & IPV6_FLOWINFO_MASK;
395                 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
396         }
397         if (np->rxopt.bits.hopopts && opt->hop) {
398                 u8 *ptr = skb->nh.raw + opt->hop;
399                 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
400         }
401         if (np->rxopt.bits.dstopts && opt->dst0) {
402                 u8 *ptr = skb->nh.raw + opt->dst0;
403                 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, (ptr[1]+1)<<3, ptr);
404         }
405         if (np->rxopt.bits.srcrt && opt->srcrt) {
406                 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(skb->nh.raw + opt->srcrt);
407                 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
408         }
409         if (np->rxopt.bits.dstopts && opt->dst1) {
410                 u8 *ptr = skb->nh.raw + opt->dst1;
411                 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, (ptr[1]+1)<<3, ptr);
412         }
413         return 0;
414 }
415
416 int datagram_send_ctl(struct msghdr *msg, struct flowi *fl,
417                       struct ipv6_txoptions *opt,
418                       int *hlimit)
419 {
420         struct in6_pktinfo *src_info;
421         struct cmsghdr *cmsg;
422         struct ipv6_rt_hdr *rthdr;
423         struct ipv6_opt_hdr *hdr;
424         int len;
425         int err = 0;
426
427         for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
428                 int addr_type;
429                 struct net_device *dev = NULL;
430
431                 if (!CMSG_OK(msg, cmsg)) {
432                         err = -EINVAL;
433                         goto exit_f;
434                 }
435
436                 if (cmsg->cmsg_level != SOL_IPV6)
437                         continue;
438
439                 switch (cmsg->cmsg_type) {
440                 case IPV6_PKTINFO:
441                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
442                                 err = -EINVAL;
443                                 goto exit_f;
444                         }
445
446                         src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
447                         
448                         if (src_info->ipi6_ifindex) {
449                                 if (fl->oif && src_info->ipi6_ifindex != fl->oif)
450                                         return -EINVAL;
451                                 fl->oif = src_info->ipi6_ifindex;
452                         }
453
454                         addr_type = ipv6_addr_type(&src_info->ipi6_addr);
455
456                         if (addr_type == IPV6_ADDR_ANY)
457                                 break;
458                         
459                         if (addr_type & IPV6_ADDR_LINKLOCAL) {
460                                 if (!src_info->ipi6_ifindex)
461                                         return -EINVAL;
462                                 else {
463                                         dev = dev_get_by_index(src_info->ipi6_ifindex);
464                                         if (!dev)
465                                                 return -ENODEV;
466                                 }
467                         }
468                         if (!ipv6_chk_addr(&src_info->ipi6_addr, dev, 0)) {
469                                 if (dev)
470                                         dev_put(dev);
471                                 err = -EINVAL;
472                                 goto exit_f;
473                         }
474                         if (dev)
475                                 dev_put(dev);
476
477                         ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr);
478                         break;
479
480                 case IPV6_FLOWINFO:
481                         if (cmsg->cmsg_len < CMSG_LEN(4)) {
482                                 err = -EINVAL;
483                                 goto exit_f;
484                         }
485
486                         if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) {
487                                 if ((fl->fl6_flowlabel^*(u32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
488                                         err = -EINVAL;
489                                         goto exit_f;
490                                 }
491                         }
492                         fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(u32 *)CMSG_DATA(cmsg);
493                         break;
494
495                 case IPV6_HOPOPTS:
496                         if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
497                                 err = -EINVAL;
498                                 goto exit_f;
499                         }
500
501                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
502                         len = ((hdr->hdrlen + 1) << 3);
503                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
504                                 err = -EINVAL;
505                                 goto exit_f;
506                         }
507                         if (!capable(CAP_NET_RAW)) {
508                                 err = -EPERM;
509                                 goto exit_f;
510                         }
511                         opt->opt_nflen += len;
512                         opt->hopopt = hdr;
513                         break;
514
515                 case IPV6_DSTOPTS:
516                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
517                                 err = -EINVAL;
518                                 goto exit_f;
519                         }
520
521                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
522                         len = ((hdr->hdrlen + 1) << 3);
523                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
524                                 err = -EINVAL;
525                                 goto exit_f;
526                         }
527                         if (!capable(CAP_NET_RAW)) {
528                                 err = -EPERM;
529                                 goto exit_f;
530                         }
531                         if (opt->dst1opt) {
532                                 err = -EINVAL;
533                                 goto exit_f;
534                         }
535                         opt->opt_flen += len;
536                         opt->dst1opt = hdr;
537                         break;
538
539                 case IPV6_RTHDR:
540                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
541                                 err = -EINVAL;
542                                 goto exit_f;
543                         }
544
545                         rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
546
547                         /*
548                          *      TYPE 0
549                          */
550                         if (rthdr->type) {
551                                 err = -EINVAL;
552                                 goto exit_f;
553                         }
554
555                         len = ((rthdr->hdrlen + 1) << 3);
556
557                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
558                                 err = -EINVAL;
559                                 goto exit_f;
560                         }
561
562                         /* segments left must also match */
563                         if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
564                                 err = -EINVAL;
565                                 goto exit_f;
566                         }
567
568                         opt->opt_nflen += len;
569                         opt->srcrt = rthdr;
570
571                         if (opt->dst1opt) {
572                                 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
573
574                                 opt->opt_nflen += dsthdrlen;
575                                 opt->dst0opt = opt->dst1opt;
576                                 opt->dst1opt = NULL;
577                                 opt->opt_flen -= dsthdrlen;
578                         }
579
580                         break;
581
582                 case IPV6_HOPLIMIT:
583                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
584                                 err = -EINVAL;
585                                 goto exit_f;
586                         }
587
588                         *hlimit = *(int *)CMSG_DATA(cmsg);
589                         break;
590
591                 default:
592                         LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
593                                        cmsg->cmsg_type);
594                         err = -EINVAL;
595                         break;
596                 };
597         }
598
599 exit_f:
600         return err;
601 }