3 * Linux INET6 implementation
6 * Pedro Roque <pedro_m@yahoo.com>
7 * Ian P. Morris <I.P.Morris@soton.ac.uk>
9 * $Id: ip6_input.c,v 1.19 2000/12/13 18:31:50 davem Exp $
11 * Based in linux/net/ipv4/ip_input.c
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
23 #include <linux/sched.h>
24 #include <linux/net.h>
25 #include <linux/netdevice.h>
26 #include <linux/in6.h>
27 #include <linux/icmpv6.h>
29 #include <linux/netfilter.h>
30 #include <linux/netfilter_ipv6.h>
36 #include <net/protocol.h>
37 #include <net/transp_v6.h>
38 #include <net/rawv6.h>
39 #include <net/ndisc.h>
40 #include <net/ip6_route.h>
41 #include <net/addrconf.h>
45 static inline int ip6_rcv_finish( struct sk_buff *skb)
50 return skb->dst->input(skb);
53 int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
58 if (skb->pkt_type == PACKET_OTHERHOST)
61 IP6_INC_STATS_BH(Ip6InReceives);
63 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
66 /* Store incoming device index. When the packet will
67 be queued, we cannot refer to skb->dev anymore.
69 ((struct inet6_skb_parm *)skb->cb)->iif = dev->ifindex;
71 if (skb->len < sizeof(struct ipv6hdr))
74 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
79 if (hdr->version != 6)
82 pkt_len = ntohs(hdr->payload_len);
84 /* pkt_len may be zero if Jumbo payload option is present */
85 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
86 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
88 if (pkt_len + sizeof(struct ipv6hdr) < skb->len) {
89 if (__pskb_trim(skb, pkt_len + sizeof(struct ipv6hdr)))
92 if (skb->ip_summed == CHECKSUM_HW)
93 skb->ip_summed = CHECKSUM_NONE;
97 if (hdr->nexthdr == NEXTHDR_HOP) {
98 skb->h.raw = (u8*)(hdr+1);
99 if (ipv6_parse_hopopts(skb, offsetof(struct ipv6hdr, nexthdr)) < 0) {
100 IP6_INC_STATS_BH(Ip6InHdrErrors);
106 return NF_HOOK(PF_INET6,NF_IP6_PRE_ROUTING, skb, dev, NULL, ip6_rcv_finish);
108 IP6_INC_STATS_BH(Ip6InTruncatedPkts);
110 IP6_INC_STATS_BH(Ip6InHdrErrors);
118 * Deliver the packet to the host
122 static inline int ip6_input_finish(struct sk_buff *skb)
124 struct ipv6hdr *hdr = skb->nh.ipv6h;
125 struct inet6_protocol *ipprot;
132 skb->h.raw = skb->nh.raw + sizeof(struct ipv6hdr);
135 * Parse extension headers
138 nexthdr = hdr->nexthdr;
139 nhoff = offsetof(struct ipv6hdr, nexthdr);
141 /* Skip hop-by-hop options, they are already parsed. */
142 if (nexthdr == NEXTHDR_HOP) {
143 nhoff = sizeof(struct ipv6hdr);
144 nexthdr = skb->h.raw[0];
145 skb->h.raw += (skb->h.raw[1]+1)<<3;
148 /* This check is sort of optimization.
149 It would be stupid to detect for optional headers,
150 which are missing with probability of 200%
152 if (nexthdr != IPPROTO_TCP && nexthdr != IPPROTO_UDP) {
153 nhoff = ipv6_parse_exthdrs(&skb, nhoff);
156 nexthdr = skb->nh.raw[nhoff];
160 if (!pskb_pull(skb, skb->h.raw - skb->data))
163 if (skb->ip_summed == CHECKSUM_HW)
164 skb->csum = csum_sub(skb->csum,
165 csum_partial(skb->nh.raw, skb->h.raw-skb->nh.raw, 0));
167 raw_sk = raw_v6_htable[nexthdr&(MAX_INET_PROTOS-1)];
169 raw_sk = ipv6_raw_deliver(skb, nexthdr);
171 hash = nexthdr & (MAX_INET_PROTOS - 1);
172 for (ipprot = (struct inet6_protocol *) inet6_protos[hash];
174 ipprot = (struct inet6_protocol *) ipprot->next) {
175 struct sk_buff *buff = skb;
177 if (ipprot->protocol != nexthdr)
180 if (ipprot->copy || raw_sk)
181 buff = skb_clone(skb, GFP_ATOMIC);
184 ipprot->handler(buff);
189 rawv6_rcv(raw_sk, skb);
195 * not found: send ICMP parameter problem back
198 IP6_INC_STATS_BH(Ip6InUnknownProtos);
199 icmpv6_param_prob(skb, ICMPV6_UNK_NEXTHDR, nhoff);
210 int ip6_input(struct sk_buff *skb)
212 return NF_HOOK(PF_INET6,NF_IP6_LOCAL_IN, skb, skb->dev, NULL, ip6_input_finish);
215 int ip6_mc_input(struct sk_buff *skb)
221 IP6_INC_STATS_BH(Ip6InMcastPkts);
224 if (ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, &hdr->saddr))
228 * IPv6 multicast router mode isnt currently supported.
231 if (ipv6_config.multicast_route) {
234 addr_type = ipv6_addr_type(&hdr->daddr);
236 if (!(addr_type & (IPV6_ADDR_LOOPBACK | IPV6_ADDR_LINKLOCAL))) {
237 struct sk_buff *skb2;
238 struct dst_entry *dst;
243 skb2 = skb_clone(skb, GFP_ATOMIC);