Import 2.3.10pre1
[davej-history.git] / net / ipv6 / ip6_output.c
blob9a635f882758562cd091f8a0dd5405bfedba9439
1 /*
2 * IPv6 output functions
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: ip6_output.c,v 1.20 1999/06/09 10:11:12 davem Exp $
10 * Based on linux/net/ipv4/ip_output.c
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * Changes:
18 * A.N.Kuznetsov : airthmetics in fragmentation.
19 * extension headers are implemented.
20 * route changes now work.
21 * ip6_forward does not confuse sniffers.
22 * etc.
24 * H. von Brand : Added missing #include <linux/string.h>
27 #include <linux/errno.h>
28 #include <linux/types.h>
29 #include <linux/string.h>
30 #include <linux/socket.h>
31 #include <linux/net.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_arp.h>
34 #include <linux/in6.h>
35 #include <linux/route.h>
37 #include <net/sock.h>
38 #include <net/snmp.h>
40 #include <net/ipv6.h>
41 #include <net/ndisc.h>
42 #include <net/protocol.h>
43 #include <net/ip6_route.h>
44 #include <net/addrconf.h>
45 #include <net/rawv6.h>
46 #include <net/icmp.h>
48 static u32 ipv6_fragmentation_id = 1;
50 int ip6_output(struct sk_buff *skb)
52 struct dst_entry *dst = skb->dst;
53 struct device *dev = dst->dev;
54 struct hh_cache *hh = dst->hh;
56 skb->protocol = __constant_htons(ETH_P_IPV6);
57 skb->dev = dev;
59 if (ipv6_addr_is_multicast(&skb->nh.ipv6h->daddr)) {
60 if (!(dev->flags&IFF_LOOPBACK) &&
61 (skb->sk == NULL || skb->sk->net_pinfo.af_inet6.mc_loop) &&
62 ipv6_chk_mcast_addr(dev, &skb->nh.ipv6h->daddr)) {
63 /* Do not check for IFF_ALLMULTI; multicast routing
64 is not supported in any case.
66 dev_loopback_xmit(skb);
68 if (skb->nh.ipv6h->hop_limit == 0) {
69 kfree_skb(skb);
70 return 0;
74 ipv6_statistics.Ip6OutMcastPkts++;
77 if (hh) {
78 read_lock_bh(&hh->hh_lock);
79 memcpy(skb->data - 16, hh->hh_data, 16);
80 read_unlock_bh(&hh->hh_lock);
81 skb_push(skb, hh->hh_len);
82 return hh->hh_output(skb);
83 } else if (dst->neighbour)
84 return dst->neighbour->output(skb);
86 kfree_skb(skb);
87 return -EINVAL;
91 * xmit an sk_buff (used by TCP)
94 int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
95 struct ipv6_txoptions *opt)
97 struct ipv6_pinfo * np = sk ? &sk->net_pinfo.af_inet6 : NULL;
98 struct in6_addr *first_hop = fl->nl_u.ip6_u.daddr;
99 struct dst_entry *dst = skb->dst;
100 struct ipv6hdr *hdr;
101 u8 proto = fl->proto;
102 int seg_len = skb->len;
103 int hlimit;
105 if (opt) {
106 int head_room;
108 /* First: exthdrs may take lots of space (~8K for now)
109 MAX_HEADER is not enough.
111 head_room = opt->opt_nflen + opt->opt_flen;
112 seg_len += head_room;
113 head_room += sizeof(struct ipv6hdr) + ((dst->dev->hard_header_len + 15)&~15);
115 if (skb_headroom(skb) < head_room) {
116 struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
117 kfree(skb);
118 skb = skb2;
119 if (skb == NULL)
120 return -ENOBUFS;
121 if (sk)
122 skb_set_owner_w(skb, sk);
124 if (opt->opt_flen)
125 ipv6_push_frag_opts(skb, opt, &proto);
126 if (opt->opt_nflen)
127 ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
130 hdr = skb->nh.ipv6h = (struct ipv6hdr*)skb_push(skb, sizeof(struct ipv6hdr));
133 * Fill in the IPv6 header
136 *(u32*)hdr = __constant_htonl(0x60000000) | fl->fl6_flowlabel;
137 hlimit = -1;
138 if (np)
139 hlimit = np->hop_limit;
140 if (hlimit < 0)
141 hlimit = ((struct rt6_info*)dst)->rt6i_hoplimit;
143 hdr->payload_len = htons(seg_len);
144 hdr->nexthdr = proto;
145 hdr->hop_limit = hlimit;
147 ipv6_addr_copy(&hdr->saddr, fl->nl_u.ip6_u.saddr);
148 ipv6_addr_copy(&hdr->daddr, first_hop);
150 if (skb->len <= dst->pmtu) {
151 ipv6_statistics.Ip6OutRequests++;
152 dst->output(skb);
153 return 0;
156 printk(KERN_DEBUG "IPv6: sending pkt_too_big to self\n");
157 start_bh_atomic();
158 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, dst->pmtu, skb->dev);
159 end_bh_atomic();
160 kfree_skb(skb);
161 return -EMSGSIZE;
165 * To avoid extra problems ND packets are send through this
166 * routine. It's code duplication but I really want to avoid
167 * extra checks since ipv6_build_header is used by TCP (which
168 * is for us performace critical)
171 int ip6_nd_hdr(struct sock *sk, struct sk_buff *skb, struct device *dev,
172 struct in6_addr *saddr, struct in6_addr *daddr,
173 int proto, int len)
175 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
176 struct ipv6hdr *hdr;
177 int totlen;
179 skb->protocol = __constant_htons(ETH_P_IPV6);
180 skb->dev = dev;
182 totlen = len + sizeof(struct ipv6hdr);
184 hdr = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
185 skb->nh.ipv6h = hdr;
187 *(u32*)hdr = htonl(0x60000000);
189 hdr->payload_len = htons(len);
190 hdr->nexthdr = proto;
191 hdr->hop_limit = np->hop_limit;
193 ipv6_addr_copy(&hdr->saddr, saddr);
194 ipv6_addr_copy(&hdr->daddr, daddr);
196 return 0;
199 static struct ipv6hdr * ip6_bld_1(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
200 int hlimit, unsigned pktlength)
202 struct ipv6hdr *hdr;
204 skb->nh.raw = skb_put(skb, sizeof(struct ipv6hdr));
205 hdr = skb->nh.ipv6h;
207 *(u32*)hdr = fl->fl6_flowlabel | htonl(0x60000000);
209 hdr->payload_len = htons(pktlength - sizeof(struct ipv6hdr));
210 hdr->hop_limit = hlimit;
211 hdr->nexthdr = fl->proto;
213 ipv6_addr_copy(&hdr->saddr, fl->nl_u.ip6_u.saddr);
214 ipv6_addr_copy(&hdr->daddr, fl->nl_u.ip6_u.daddr);
215 return hdr;
218 static __inline__ u8 * ipv6_build_fraghdr(struct sk_buff *skb, u8* prev_hdr, unsigned offset)
220 struct frag_hdr *fhdr;
222 fhdr = (struct frag_hdr *) skb_put(skb, sizeof(struct frag_hdr));
224 fhdr->nexthdr = *prev_hdr;
225 *prev_hdr = NEXTHDR_FRAGMENT;
226 prev_hdr = &fhdr->nexthdr;
228 fhdr->reserved = 0;
229 fhdr->frag_off = htons(offset);
230 fhdr->identification = ipv6_fragmentation_id++;
231 return &fhdr->nexthdr;
234 static int ip6_frag_xmit(struct sock *sk, inet_getfrag_t getfrag,
235 const void *data, struct dst_entry *dst,
236 struct flowi *fl, struct ipv6_txoptions *opt,
237 struct in6_addr *final_dst,
238 int hlimit, int flags, unsigned length, int mtu)
240 struct ipv6hdr *hdr;
241 struct sk_buff *last_skb;
242 u8 *prev_hdr;
243 int unfrag_len;
244 int frag_len;
245 int last_len;
246 int nfrags;
247 int fhdr_dist;
248 int frag_off;
249 int data_off;
250 int err;
253 * Fragmentation
255 * Extension header order:
256 * Hop-by-hop -> Dest0 -> Routing -> Fragment -> Auth -> Dest1 -> rest (...)
258 * We must build the non-fragmented part that
259 * will be in every packet... this also means
260 * that other extension headers (Dest, Auth, etc)
261 * must be considered in the data to be fragmented
264 unfrag_len = sizeof(struct ipv6hdr) + sizeof(struct frag_hdr);
265 last_len = length;
267 if (opt) {
268 unfrag_len += opt->opt_nflen;
269 last_len += opt->opt_flen;
273 * Length of fragmented part on every packet but
274 * the last must be an:
275 * "integer multiple of 8 octects".
278 frag_len = (mtu - unfrag_len) & ~0x7;
280 /* Unfragmentable part exceeds mtu. */
281 if (frag_len <= 0) {
282 ipv6_local_error(sk, EMSGSIZE, fl, mtu);
283 return -EMSGSIZE;
286 nfrags = last_len / frag_len;
289 * We must send from end to start because of
290 * UDP/ICMP checksums. We do a funny trick:
291 * fill the last skb first with the fixed
292 * header (and its data) and then use it
293 * to create the following segments and send it
294 * in the end. If the peer is checking the M_flag
295 * to trigger the reassembly code then this
296 * might be a good idea.
299 frag_off = nfrags * frag_len;
300 last_len -= frag_off;
302 if (last_len == 0) {
303 last_len = frag_len;
304 frag_off -= frag_len;
305 nfrags--;
307 data_off = frag_off;
309 /* And it is implementation problem: for now we assume, that
310 all the exthdrs will fit to the first fragment.
312 if (opt) {
313 if (frag_len < opt->opt_flen) {
314 ipv6_local_error(sk, EMSGSIZE, fl, mtu);
315 return -EMSGSIZE;
317 data_off = frag_off - opt->opt_flen;
320 last_skb = sock_alloc_send_skb(sk, unfrag_len + frag_len +
321 dst->dev->hard_header_len + 15,
322 0, flags & MSG_DONTWAIT, &err);
324 if (last_skb == NULL)
325 return err;
327 last_skb->dst = dst_clone(dst);
329 skb_reserve(last_skb, (dst->dev->hard_header_len + 15) & ~15);
331 hdr = ip6_bld_1(sk, last_skb, fl, hlimit, frag_len+unfrag_len);
332 prev_hdr = &hdr->nexthdr;
334 if (opt && opt->opt_nflen)
335 prev_hdr = ipv6_build_nfrag_opts(last_skb, prev_hdr, opt, final_dst, 0);
337 prev_hdr = ipv6_build_fraghdr(last_skb, prev_hdr, frag_off);
338 fhdr_dist = prev_hdr - last_skb->data;
340 err = getfrag(data, &hdr->saddr, last_skb->tail, data_off, last_len);
342 if (!err) {
343 while (nfrags--) {
344 struct sk_buff *skb;
346 struct frag_hdr *fhdr2;
348 skb = skb_copy(last_skb, sk->allocation);
350 if (skb == NULL) {
351 ipv6_statistics.Ip6FragFails++;
352 kfree_skb(last_skb);
353 return -ENOMEM;
356 frag_off -= frag_len;
357 data_off -= frag_len;
359 fhdr2 = (struct frag_hdr *) (skb->data + fhdr_dist);
361 /* more flag on */
362 fhdr2->frag_off = htons(frag_off | 1);
364 /* Write fragmentable exthdrs to the first chunk */
365 if (nfrags == 0 && opt && opt->opt_flen) {
366 ipv6_build_frag_opts(skb, &fhdr2->nexthdr, opt);
367 frag_len -= opt->opt_flen;
368 data_off = 0;
371 err = getfrag(data, &hdr->saddr,skb_put(skb, frag_len),
372 data_off, frag_len);
374 if (err) {
375 kfree_skb(skb);
376 break;
379 ipv6_statistics.Ip6FragCreates++;
380 ipv6_statistics.Ip6OutRequests++;
381 dst->output(skb);
385 if (err) {
386 ipv6_statistics.Ip6FragFails++;
387 kfree_skb(last_skb);
388 return -EFAULT;
391 hdr->payload_len = htons(unfrag_len + last_len - sizeof(struct ipv6hdr));
394 * update last_skb to reflect the getfrag we did
395 * on start.
398 skb_put(last_skb, last_len);
400 ipv6_statistics.Ip6FragCreates++;
401 ipv6_statistics.Ip6FragOKs++;
402 ipv6_statistics.Ip6OutRequests++;
403 dst->output(last_skb);
405 return 0;
408 int ip6_build_xmit(struct sock *sk, inet_getfrag_t getfrag, const void *data,
409 struct flowi *fl, unsigned length,
410 struct ipv6_txoptions *opt, int hlimit, int flags)
412 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
413 struct in6_addr *final_dst = NULL;
414 struct dst_entry *dst;
415 int err = 0;
416 unsigned int pktlength, jumbolen, mtu;
417 struct in6_addr saddr;
419 if (opt && opt->srcrt) {
420 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
421 final_dst = fl->fl6_dst;
422 fl->fl6_dst = rt0->addr;
425 if (!fl->oif && ipv6_addr_is_multicast(fl->nl_u.ip6_u.daddr))
426 fl->oif = np->mcast_oif;
428 dst = NULL;
429 if (sk->dst_cache) {
430 dst = dst_check(&sk->dst_cache, np->dst_cookie);
431 if (dst) {
432 struct rt6_info *rt = (struct rt6_info*)dst_clone(dst);
434 /* Yes, checking route validity in not connected
435 case is not very simple. Take into account,
436 that we do not support routing by source, TOS,
437 and MSG_DONTROUTE --ANK (980726)
439 1. If route was host route, check that
440 cached destination is current.
441 If it is network route, we still may
442 check its validity using saved pointer
443 to the last used address: daddr_cache.
444 We do not want to save whole address now,
445 (because main consumer of this service
446 is tcp, which has not this problem),
447 so that the last trick works only on connected
448 sockets.
449 2. oif also should be the same.
451 if (((rt->rt6i_dst.plen != 128 ||
452 ipv6_addr_cmp(fl->fl6_dst, &rt->rt6i_dst.addr))
453 && (np->daddr_cache == NULL ||
454 ipv6_addr_cmp(fl->fl6_dst, np->daddr_cache)))
455 || (fl->oif && fl->oif != dst->dev->ifindex)) {
456 dst_release(dst);
457 dst = NULL;
462 if (dst == NULL)
463 dst = ip6_route_output(sk, fl);
465 if (dst->error) {
466 ipv6_statistics.Ip6OutNoRoutes++;
467 dst_release(dst);
468 return -ENETUNREACH;
471 if (fl->fl6_src == NULL) {
472 err = ipv6_get_saddr(dst, fl->fl6_dst, &saddr);
474 if (err) {
475 #if IP6_DEBUG >= 2
476 printk(KERN_DEBUG "ip6_build_xmit: "
477 "no availiable source address\n");
478 #endif
479 goto out;
481 fl->fl6_src = &saddr;
483 pktlength = length;
485 if (hlimit < 0) {
486 if (ipv6_addr_is_multicast(fl->fl6_dst))
487 hlimit = np->mcast_hops;
488 else
489 hlimit = np->hop_limit;
490 if (hlimit < 0)
491 hlimit = ((struct rt6_info*)dst)->rt6i_hoplimit;
494 jumbolen = 0;
496 if (!sk->ip_hdrincl) {
497 pktlength += sizeof(struct ipv6hdr);
498 if (opt)
499 pktlength += opt->opt_flen + opt->opt_nflen;
501 if (pktlength > 0xFFFF + sizeof(struct ipv6hdr)) {
502 /* Jumbo datagram.
503 It is assumed, that in the case of sk->ip_hdrincl
504 jumbo option is supplied by user.
506 pktlength += 8;
507 jumbolen = pktlength - sizeof(struct ipv6hdr);
511 mtu = dst->pmtu;
512 if (np->frag_size < mtu) {
513 if (np->frag_size)
514 mtu = np->frag_size;
515 else if (np->pmtudisc == IPV6_PMTUDISC_DONT)
516 mtu = IPV6_MIN_MTU;
519 /* Critical arithmetic overflow check.
520 FIXME: may gcc optimize it out? --ANK (980726)
522 if (pktlength < length) {
523 ipv6_local_error(sk, EMSGSIZE, fl, mtu);
524 err = -EMSGSIZE;
525 goto out;
528 if (pktlength <= mtu) {
529 struct sk_buff *skb;
530 struct ipv6hdr *hdr;
531 struct device *dev = dst->dev;
533 skb = sock_alloc_send_skb(sk, pktlength + 15 +
534 dev->hard_header_len, 0,
535 flags & MSG_DONTWAIT, &err);
537 if (skb == NULL) {
538 ipv6_statistics.Ip6OutDiscards++;
539 goto out;
542 skb->dst = dst_clone(dst);
544 skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
546 hdr = (struct ipv6hdr *) skb->tail;
547 skb->nh.ipv6h = hdr;
549 if (!sk->ip_hdrincl) {
550 ip6_bld_1(sk, skb, fl, hlimit,
551 jumbolen ? sizeof(struct ipv6hdr) : pktlength);
553 if (opt || jumbolen) {
554 u8 *prev_hdr = &hdr->nexthdr;
555 prev_hdr = ipv6_build_nfrag_opts(skb, prev_hdr, opt, final_dst, jumbolen);
556 if (opt && opt->opt_flen)
557 ipv6_build_frag_opts(skb, prev_hdr, opt);
561 skb_put(skb, length);
562 err = getfrag(data, &hdr->saddr,
563 ((char *) hdr) + (pktlength - length),
564 0, length);
566 if (!err) {
567 ipv6_statistics.Ip6OutRequests++;
568 dst->output(skb);
569 } else {
570 err = -EFAULT;
571 kfree_skb(skb);
573 } else {
574 if (sk->ip_hdrincl || jumbolen ||
575 np->pmtudisc == IPV6_PMTUDISC_DO) {
576 ipv6_local_error(sk, EMSGSIZE, fl, mtu);
577 err = -EMSGSIZE;
578 goto out;
581 err = ip6_frag_xmit(sk, getfrag, data, dst, fl, opt, final_dst, hlimit,
582 flags, length, mtu);
586 * cleanup
588 out:
589 ip6_dst_store(sk, dst, fl->nl_u.ip6_u.daddr == &np->daddr ? &np->daddr : NULL);
590 return err;
593 int ip6_call_ra_chain(struct sk_buff *skb, int sel)
595 struct ip6_ra_chain *ra;
596 struct sock *last = NULL;
598 for (ra = ip6_ra_chain; ra; ra = ra->next) {
599 struct sock *sk = ra->sk;
600 if (sk && ra->sel == sel) {
601 if (last) {
602 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
603 if (skb2)
604 rawv6_rcv(last, skb2, skb2->len);
606 last = sk;
610 if (last) {
611 rawv6_rcv(last, skb, skb->len);
612 return 1;
614 return 0;
617 int ip6_forward(struct sk_buff *skb)
619 struct dst_entry *dst = skb->dst;
620 struct ipv6hdr *hdr = skb->nh.ipv6h;
621 struct inet6_skb_parm *opt =(struct inet6_skb_parm*)skb->cb;
623 if (ipv6_devconf.forwarding == 0 && opt->srcrt == 0)
624 goto drop;
627 * We DO NOT make any processing on
628 * RA packets, pushing them to user level AS IS
629 * without ane WARRANTY that application will be able
630 * to interpret them. The reason is that we
631 * cannot make anything clever here.
633 * We are not end-node, so that if packet contains
634 * AH/ESP, we cannot make anything.
635 * Defragmentation also would be mistake, RA packets
636 * cannot be fragmented, because there is no warranty
637 * that different fragments will go along one path. --ANK
639 if (opt->ra) {
640 u8 *ptr = skb->nh.raw + opt->ra;
641 if (ip6_call_ra_chain(skb, (ptr[2]<<8) + ptr[3]))
642 return 0;
646 * check and decrement ttl
648 if (hdr->hop_limit <= 1) {
649 /* Force OUTPUT device used as source address */
650 skb->dev = dst->dev;
651 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
652 0, skb->dev);
654 kfree_skb(skb);
655 return -ETIMEDOUT;
658 /* IPv6 specs say nothing about it, but it is clear that we cannot
659 send redirects to source routed frames.
661 if (skb->dev == dst->dev && dst->neighbour && opt->srcrt == 0) {
662 struct in6_addr *target = NULL;
663 struct rt6_info *rt;
664 struct neighbour *n = dst->neighbour;
667 * incoming and outgoing devices are the same
668 * send a redirect.
671 rt = (struct rt6_info *) dst;
672 if ((rt->rt6i_flags & RTF_GATEWAY))
673 target = (struct in6_addr*)&n->primary_key;
674 else
675 target = &hdr->daddr;
677 /* Limit redirects both by destination (here)
678 and by source (inside ndisc_send_redirect)
680 if (xrlim_allow(dst, 1*HZ))
681 ndisc_send_redirect(skb, n, target);
682 } else if (ipv6_addr_type(&hdr->saddr)&(IPV6_ADDR_MULTICAST|IPV6_ADDR_LOOPBACK
683 |IPV6_ADDR_LINKLOCAL)) {
684 /* This check is security critical. */
685 goto drop;
688 if (skb->len > dst->pmtu) {
689 /* Again, force OUTPUT device used as source address */
690 skb->dev = dst->dev;
691 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, dst->pmtu, skb->dev);
692 ipv6_statistics.Ip6InTooBigErrors++;
693 kfree_skb(skb);
694 return -EMSGSIZE;
697 if ((skb = skb_cow(skb, dst->dev->hard_header_len)) == NULL)
698 return 0;
700 hdr = skb->nh.ipv6h;
702 /* Mangling hops number delayed to point after skb COW */
704 hdr->hop_limit--;
706 ipv6_statistics.Ip6OutForwDatagrams++;
707 return dst->output(skb);
709 drop:
710 ipv6_statistics.Ip6InAddrErrors++;
711 kfree_skb(skb);
712 return -EINVAL;