route6.c revision 195699
1284778Sdelphij/*-
2267843Sdelphij * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3267843Sdelphij * All rights reserved.
4284778Sdelphij *
5267843Sdelphij * Redistribution and use in source and binary forms, with or without
6267843Sdelphij * modification, are permitted provided that the following conditions
7267843Sdelphij * are met:
8267843Sdelphij * 1. Redistributions of source code must retain the above copyright
9267843Sdelphij *    notice, this list of conditions and the following disclaimer.
10267843Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
11267843Sdelphij *    notice, this list of conditions and the following disclaimer in the
12267843Sdelphij *    documentation and/or other materials provided with the distribution.
13267843Sdelphij * 3. Neither the name of the project nor the names of its contributors
14267843Sdelphij *    may be used to endorse or promote products derived from this software
15267843Sdelphij *    without specific prior written permission.
16267843Sdelphij *
17267843Sdelphij * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18267843Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19267843Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20284778Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21284778Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22284778Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23284778Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24284778Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25284778Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26284778Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27284778Sdelphij * SUCH DAMAGE.
28284778Sdelphij *
29284778Sdelphij *	$KAME: route6.c,v 1.24 2001/03/14 03:07:05 itojun Exp $
30284778Sdelphij */
31267843Sdelphij
32267843Sdelphij#include <sys/cdefs.h>
33267843Sdelphij__FBSDID("$FreeBSD: head/sys/netinet6/route6.c 195699 2009-07-14 22:48:30Z rwatson $");
34267843Sdelphij
35267843Sdelphij#include "opt_inet.h"
36267843Sdelphij#include "opt_inet6.h"
37267843Sdelphij
38267843Sdelphij#include <sys/param.h>
39267843Sdelphij#include <sys/mbuf.h>
40267843Sdelphij#include <sys/socket.h>
41267843Sdelphij#include <sys/systm.h>
42267843Sdelphij#include <sys/queue.h>
43267843Sdelphij#include <sys/vimage.h>
44267843Sdelphij
45267843Sdelphij#include <net/if.h>
46267843Sdelphij
47267843Sdelphij#include <netinet/in.h>
48267843Sdelphij#include <netinet6/in6_var.h>
49267843Sdelphij#include <netinet/ip6.h>
50267843Sdelphij#include <netinet6/ip6_var.h>
51267843Sdelphij#include <netinet6/scope6_var.h>
52267843Sdelphij
53267843Sdelphij#include <netinet/icmp6.h>
54267843Sdelphij
55267843Sdelphij/*
56267843Sdelphij * proto - is unused
57267843Sdelphij */
58267843Sdelphij
59267843Sdelphijint
60267843Sdelphijroute6_input(struct mbuf **mp, int *offp, int proto)
61267843Sdelphij{
62267843Sdelphij	struct ip6_hdr *ip6;
63267843Sdelphij	struct mbuf *m = *mp;
64267843Sdelphij	struct ip6_rthdr *rh;
65267843Sdelphij	int off = *offp, rhlen;
66267843Sdelphij	struct ip6aux *ip6a;
67267843Sdelphij
68267843Sdelphij	ip6a = ip6_findaux(m);
69267843Sdelphij	if (ip6a) {
70267843Sdelphij		/* XXX reject home-address option before rthdr */
71267843Sdelphij		if (ip6a->ip6a_flags & IP6A_SWAP) {
72267843Sdelphij			V_ip6stat.ip6s_badoptions++;
73267843Sdelphij			m_freem(m);
74267843Sdelphij			return IPPROTO_DONE;
75267843Sdelphij		}
76267843Sdelphij	}
77267843Sdelphij
78267843Sdelphij#ifndef PULLDOWN_TEST
79267843Sdelphij	IP6_EXTHDR_CHECK(m, off, sizeof(*rh), IPPROTO_DONE);
80267843Sdelphij	ip6 = mtod(m, struct ip6_hdr *);
81267843Sdelphij	rh = (struct ip6_rthdr *)((caddr_t)ip6 + off);
82267843Sdelphij#else
83267843Sdelphij	ip6 = mtod(m, struct ip6_hdr *);
84267843Sdelphij	IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, sizeof(*rh));
85267843Sdelphij	if (rh == NULL) {
86267843Sdelphij		V_ip6stat.ip6s_tooshort++;
87267843Sdelphij		return IPPROTO_DONE;
88267843Sdelphij	}
89267843Sdelphij#endif
90267843Sdelphij
91267843Sdelphij	/*
92267843Sdelphij	 * While this switch may look gratuitous, leave it in
93267843Sdelphij	 * in favour of RH2 implementations, etc.
94267843Sdelphij	 */
95267843Sdelphij	switch (rh->ip6r_type) {
96267843Sdelphij	default:
97267843Sdelphij		/* Unknown routing header type. */
98267843Sdelphij		if (rh->ip6r_segleft == 0) {
99267843Sdelphij			rhlen = (rh->ip6r_len + 1) << 3;
100267843Sdelphij			break;	/* Final dst. Just ignore the header. */
101330569Sgordon		}
102267843Sdelphij		V_ip6stat.ip6s_badoptions++;
103267843Sdelphij		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
104267843Sdelphij			    (caddr_t)&rh->ip6r_type - (caddr_t)ip6);
105267843Sdelphij		return (IPPROTO_DONE);
106267843Sdelphij	}
107267843Sdelphij
108267843Sdelphij	*offp += rhlen;
109267843Sdelphij	return (rh->ip6r_nxt);
110267843Sdelphij}
111267843Sdelphij