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