in_gif.c revision 55009
1/*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/netinet/in_gif.c 55009 1999-12-22 19:13:38Z shin $
30 */
31
32/*
33 * in_gif.c
34 */
35
36#include "opt_mrouting.h"
37#include "opt_inet6.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/socket.h>
42#include <sys/sockio.h>
43#include <sys/mbuf.h>
44#include <sys/errno.h>
45#include <sys/kernel.h>
46#include <sys/sysctl.h>
47#include <sys/protosw.h>
48
49#include <net/if.h>
50#include <net/route.h>
51
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#include <netinet/ip.h>
55#ifdef INET6
56#include <netinet/ip6.h>
57#endif
58#include <netinet/ip_var.h>
59#include <netinet/in_gif.h>
60#include <netinet/ip_ecn.h>
61#ifdef INET6
62#include <netinet6/ip6_ecn.h>
63#endif
64
65#ifdef MROUTING
66#include <netinet/ip_mroute.h>
67#endif /* MROUTING */
68
69#include <net/if_gif.h>
70
71#include "gif.h"
72
73#include <machine/stdarg.h>
74
75#include <net/net_osdep.h>
76
77#if NGIF > 0
78int ip_gif_ttl = GIF_TTL;
79#else
80int ip_gif_ttl = 0;
81#endif
82
83SYSCTL_DECL(_net_inet_ip);
84SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
85	&ip_gif_ttl,	0, "");
86
87int
88in_gif_output(ifp, family, m, rt)
89	struct ifnet	*ifp;
90	int		family;
91	struct mbuf	*m;
92	struct rtentry *rt;
93{
94	register struct gif_softc *sc = (struct gif_softc*)ifp;
95	struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
96	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
97	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
98	struct ip iphdr;	/* capsule IP header, host byte ordered */
99	int proto, error;
100	u_int8_t tos;
101
102	if (sin_src == NULL || sin_dst == NULL ||
103	    sin_src->sin_family != AF_INET ||
104	    sin_dst->sin_family != AF_INET) {
105		m_freem(m);
106		return EAFNOSUPPORT;
107	}
108
109	switch (family) {
110	case AF_INET:
111	    {
112		struct ip *ip;
113
114		proto = IPPROTO_IPV4;
115		if (m->m_len < sizeof(*ip)) {
116			m = m_pullup(m, sizeof(*ip));
117			if (!m)
118				return ENOBUFS;
119		}
120		ip = mtod(m, struct ip *);
121		tos = ip->ip_tos;
122		break;
123	    }
124#ifdef INET6
125	case AF_INET6:
126	    {
127		struct ip6_hdr *ip6;
128		proto = IPPROTO_IPV6;
129		if (m->m_len < sizeof(*ip6)) {
130			m = m_pullup(m, sizeof(*ip6));
131			if (!m)
132				return ENOBUFS;
133		}
134		ip6 = mtod(m, struct ip6_hdr *);
135		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
136		break;
137	    }
138#endif /*INET6*/
139	default:
140#ifdef DIAGNOSTIC
141		printf("in_gif_output: warning: unknown family %d passed\n",
142			family);
143#endif
144		m_freem(m);
145		return EAFNOSUPPORT;
146	}
147
148	bzero(&iphdr, sizeof(iphdr));
149	iphdr.ip_src = sin_src->sin_addr;
150	if (ifp->if_flags & IFF_LINK0) {
151		/* multi-destination mode */
152		if (sin_dst->sin_addr.s_addr != INADDR_ANY)
153			iphdr.ip_dst = sin_dst->sin_addr;
154		else if (rt) {
155			iphdr.ip_dst = ((struct sockaddr_in *)
156					(rt->rt_gateway))->sin_addr;
157		} else {
158			m_freem(m);
159			return ENETUNREACH;
160		}
161	} else {
162		/* bidirectional configured tunnel mode */
163		if (sin_dst->sin_addr.s_addr != INADDR_ANY)
164			iphdr.ip_dst = sin_dst->sin_addr;
165		else {
166			m_freem(m);
167			return ENETUNREACH;
168		}
169	}
170	iphdr.ip_p = proto;
171	/* version will be set in ip_output() */
172	iphdr.ip_ttl = ip_gif_ttl;
173	iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
174	if (ifp->if_flags & IFF_LINK1)
175		ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
176
177	/* prepend new IP header */
178	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
179	if (m && m->m_len < sizeof(struct ip))
180		m = m_pullup(m, sizeof(struct ip));
181	if (m == NULL) {
182		printf("ENOBUFS in in_gif_output %d\n", __LINE__);
183		return ENOBUFS;
184	}
185
186	*(mtod(m, struct ip *)) = iphdr;
187
188	if (dst->sin_family != sin_dst->sin_family ||
189	    dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
190		/* cache route doesn't match */
191		dst->sin_family = sin_dst->sin_family;
192		dst->sin_len = sizeof(struct sockaddr_in);
193		dst->sin_addr = sin_dst->sin_addr;
194		if (sc->gif_ro.ro_rt) {
195			RTFREE(sc->gif_ro.ro_rt);
196			sc->gif_ro.ro_rt = NULL;
197		}
198	}
199
200	if (sc->gif_ro.ro_rt == NULL) {
201		rtalloc(&sc->gif_ro);
202		if (sc->gif_ro.ro_rt == NULL) {
203			m_freem(m);
204			return ENETUNREACH;
205		}
206	}
207
208	error = ip_output(m, 0, &sc->gif_ro, 0, 0);
209	return(error);
210}
211
212void
213in_gif_input(struct mbuf *m, int off, int proto)
214{
215	struct gif_softc *sc;
216	struct ifnet *gifp = NULL;
217	struct ip *ip;
218	int i, af;
219	u_int8_t otos;
220
221	ip = mtod(m, struct ip *);
222
223	/* this code will be soon improved. */
224#define	satosin(sa)	((struct sockaddr_in *)(sa))
225	for (i = 0, sc = gif; i < ngif; i++, sc++) {
226		if (sc->gif_psrc == NULL
227		 || sc->gif_pdst == NULL
228		 || sc->gif_psrc->sa_family != AF_INET
229		 || sc->gif_pdst->sa_family != AF_INET) {
230			continue;
231		}
232
233		if ((sc->gif_if.if_flags & IFF_UP) == 0)
234			continue;
235
236		if ((sc->gif_if.if_flags & IFF_LINK0)
237		 && satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr
238		 && satosin(sc->gif_pdst)->sin_addr.s_addr == INADDR_ANY) {
239			gifp = &sc->gif_if;
240			continue;
241		}
242
243		if (satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr
244		 && satosin(sc->gif_pdst)->sin_addr.s_addr == ip->ip_src.s_addr)
245		{
246			gifp = &sc->gif_if;
247			break;
248		}
249	}
250
251	if (gifp == NULL) {
252#ifdef MROUTING
253		/* for backward compatibility */
254		if (proto == IPPROTO_IPV4) {
255			ipip_input(m, off, proto);
256			return;
257		}
258#endif /*MROUTING*/
259		m_freem(m);
260		ipstat.ips_nogif++;
261		return;
262	}
263
264	otos = ip->ip_tos;
265	m_adj(m, off);
266
267	switch (proto) {
268	case IPPROTO_IPV4:
269	    {
270		struct ip *ip;
271		af = AF_INET;
272		if (m->m_len < sizeof(*ip)) {
273			m = m_pullup(m, sizeof(*ip));
274			if (!m)
275				return;
276		}
277		ip = mtod(m, struct ip *);
278		if (gifp->if_flags & IFF_LINK1)
279			ip_ecn_egress(ECN_ALLOWED, &otos, &ip->ip_tos);
280		break;
281	    }
282#ifdef INET6
283	case IPPROTO_IPV6:
284	    {
285		struct ip6_hdr *ip6;
286		u_int8_t itos;
287		af = AF_INET6;
288		if (m->m_len < sizeof(*ip6)) {
289			m = m_pullup(m, sizeof(*ip6));
290			if (!m)
291				return;
292		}
293		ip6 = mtod(m, struct ip6_hdr *);
294		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
295		if (gifp->if_flags & IFF_LINK1)
296			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
297		ip6->ip6_flow &= ~htonl(0xff << 20);
298		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
299		break;
300	    }
301#endif /* INET6 */
302	default:
303		ipstat.ips_nogif++;
304		m_freem(m);
305		return;
306	}
307	gif_input(m, af, gifp);
308	return;
309}
310