xref: /netbsd/sys/net/if_gre.c (revision c4a72b64)
1 /*	$NetBSD: if_gre.c,v 1.42 2002/08/14 00:23:27 itojun Exp $ */
2 
3 /*
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Heiko W.Rupp <hwr@pilhuhn.de>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Encapsulate L3 protocols into IP
41  * See RFC 1701 and 1702 for more details.
42  * If_gre is compatible with Cisco GRE tunnels, so you can
43  * have a NetBSD box as the other end of a tunnel interface of a Cisco
44  * router. See gre(4) for more details.
45  * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
46  */
47 
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.42 2002/08/14 00:23:27 itojun Exp $");
50 
51 #include "opt_inet.h"
52 #include "opt_ns.h"
53 #include "bpfilter.h"
54 
55 #include <sys/param.h>
56 #include <sys/malloc.h>
57 #include <sys/mbuf.h>
58 #include <sys/proc.h>
59 #include <sys/protosw.h>
60 #include <sys/socket.h>
61 #include <sys/ioctl.h>
62 #include <sys/queue.h>
63 #if __NetBSD__
64 #include <sys/systm.h>
65 #endif
66 
67 #include <machine/cpu.h>
68 
69 #include <net/ethertypes.h>
70 #include <net/if.h>
71 #include <net/if_types.h>
72 #include <net/netisr.h>
73 #include <net/route.h>
74 
75 #ifdef INET
76 #include <netinet/in.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/in_var.h>
79 #include <netinet/ip.h>
80 #include <netinet/ip_var.h>
81 #else
82 #error "Huh? if_gre without inet?"
83 #endif
84 
85 #ifdef NS
86 #include <netns/ns.h>
87 #include <netns/ns_if.h>
88 #endif
89 
90 #ifdef NETATALK
91 #include <netatalk/at.h>
92 #include <netatalk/at_var.h>
93 #include <netatalk/at_extern.h>
94 #endif
95 
96 #if NBPFILTER > 0
97 #include <sys/time.h>
98 #include <net/bpf.h>
99 #endif
100 
101 #include <net/if_gre.h>
102 
103 /*
104  * It is not easy to calculate the right value for a GRE MTU.
105  * We leave this task to the admin and use the same default that
106  * other vendors use.
107  */
108 #define GREMTU 1476
109 
110 struct gre_softc_head gre_softc_list;
111 int ip_gre_ttl = GRE_TTL;
112 
113 int	gre_clone_create __P((struct if_clone *, int));
114 void	gre_clone_destroy __P((struct ifnet *));
115 
116 struct if_clone gre_cloner =
117     IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
118 
119 int gre_compute_route(struct gre_softc *sc);
120 
121 void	greattach __P((int));
122 
123 /* ARGSUSED */
124 void
125 greattach(count)
126 	int count;
127 {
128 
129 	LIST_INIT(&gre_softc_list);
130 	if_clone_attach(&gre_cloner);
131 }
132 
133 int
134 gre_clone_create(ifc, unit)
135 	struct if_clone *ifc;
136 	int unit;
137 {
138 	struct gre_softc *sc;
139 
140 	sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
141 	memset(sc, 0, sizeof(struct gre_softc));
142 
143 	sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
144 	sc->sc_if.if_softc = sc;
145 	sc->sc_if.if_type = IFT_OTHER;
146 	sc->sc_if.if_addrlen = 0;
147 	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
148 	sc->sc_if.if_dlt = DLT_NULL;
149 	sc->sc_if.if_mtu = GREMTU;
150 	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
151 	sc->sc_if.if_output = gre_output;
152 	sc->sc_if.if_ioctl = gre_ioctl;
153 	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
154 	sc->g_proto = IPPROTO_GRE;
155 	sc->sc_if.if_flags |= IFF_LINK0;
156 	if_attach(&sc->sc_if);
157 	if_alloc_sadl(&sc->sc_if);
158 #if NBPFILTER > 0
159 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
160 #endif
161 	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
162 	return (0);
163 }
164 
165 void
166 gre_clone_destroy(ifp)
167 	struct ifnet *ifp;
168 {
169 	struct gre_softc *sc = ifp->if_softc;
170 
171 	LIST_REMOVE(sc, sc_list);
172 #if NBPFILTER > 0
173 	bpfdetach(ifp);
174 #endif
175 	if_detach(ifp);
176 	free(sc, M_DEVBUF);
177 }
178 
179 /*
180  * The output routine. Takes a packet and encapsulates it in the protocol
181  * given by sc->g_proto. See also RFC 1701 and RFC 2004
182  */
183 int
184 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
185 	   struct rtentry *rt)
186 {
187 	int error = 0;
188 	struct gre_softc *sc = ifp->if_softc;
189 	struct greip *gh;
190 	struct ip *ip;
191 	u_char osrc;
192 	u_short etype = 0;
193 	struct mobile_h mob_h;
194 
195 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
196 	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
197 		m_freem(m);
198 		error = ENETDOWN;
199 		goto end;
200 	}
201 
202 	gh = NULL;
203 	ip = NULL;
204 	osrc = 0;
205 
206 #if NBPFILTER >0
207 	if (ifp->if_bpf) {
208 		/* see comment of other if_foo.c files */
209 		struct mbuf m0;
210 		u_int32_t af = dst->sa_family;
211 
212 		m0.m_next = m;
213 		m0.m_len = 4;
214 		m0.m_data = (char *)&af;
215 
216 		bpf_mtap(ifp->if_bpf, &m0);
217 	}
218 #endif
219 
220 	m->m_flags &= ~(M_BCAST|M_MCAST);
221 
222 	if (sc->g_proto == IPPROTO_MOBILE) {
223 		if (dst->sa_family == AF_INET) {
224 			struct mbuf *m0;
225 			int msiz;
226 
227 			ip = mtod(m, struct ip *);
228 
229 			memset(&mob_h, 0, MOB_H_SIZ_L);
230 			mob_h.proto = (ip->ip_p) << 8;
231 			mob_h.odst = ip->ip_dst.s_addr;
232 			ip->ip_dst.s_addr = sc->g_dst.s_addr;
233 
234 			/*
235 			 * If the packet comes from our host, we only change
236 			 * the destination address in the IP header.
237 			 * Else we also need to save and change the source
238 			 */
239 			if (in_hosteq(ip->ip_src, sc->g_src)) {
240 				msiz = MOB_H_SIZ_S;
241 			} else {
242 				mob_h.proto |= MOB_H_SBIT;
243 				mob_h.osrc = ip->ip_src.s_addr;
244 				ip->ip_src.s_addr = sc->g_src.s_addr;
245 				msiz = MOB_H_SIZ_L;
246 			}
247 			HTONS(mob_h.proto);
248 			mob_h.hcrc = gre_in_cksum((u_short *)&mob_h, msiz);
249 
250 			if ((m->m_data - msiz) < m->m_pktdat) {
251 				/* need new mbuf */
252 				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
253 				if (m0 == NULL) {
254 					IF_DROP(&ifp->if_snd);
255 					m_freem(m);
256 					error = ENOBUFS;
257 					goto end;
258 				}
259 				m0->m_next = m;
260 				m->m_data += sizeof(struct ip);
261 				m->m_len -= sizeof(struct ip);
262 				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
263 				m0->m_len = msiz + sizeof(struct ip);
264 				m0->m_data += max_linkhdr;
265 				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
266 				       sizeof(struct ip));
267 				m = m0;
268 			} else {  /* we have some space left in the old one */
269 				m->m_data -= msiz;
270 				m->m_len += msiz;
271 				m->m_pkthdr.len += msiz;
272 				memmove(mtod(m, caddr_t), ip,
273 					sizeof(struct ip));
274 			}
275 			ip = mtod(m, struct ip *);
276 			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
277 			ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
278 		} else {  /* AF_INET */
279 			IF_DROP(&ifp->if_snd);
280 			m_freem(m);
281 			error = EINVAL;
282 			goto end;
283 		}
284 	} else if (sc->g_proto == IPPROTO_GRE) {
285 		switch (dst->sa_family) {
286 		case AF_INET:
287 			ip = mtod(m, struct ip *);
288 			etype = ETHERTYPE_IP;
289 			break;
290 #ifdef NETATALK
291 		case AF_APPLETALK:
292 			etype = ETHERTYPE_ATALK;
293 			break;
294 #endif
295 #ifdef NS
296 		case AF_NS:
297 			etype = ETHERTYPE_NS;
298 			break;
299 #endif
300 		default:
301 			IF_DROP(&ifp->if_snd);
302 			m_freem(m);
303 			error = EAFNOSUPPORT;
304 			goto end;
305 		}
306 		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
307 	} else {
308 		IF_DROP(&ifp->if_snd);
309 		m_freem(m);
310 		error = EINVAL;
311 		goto end;
312 	}
313 
314 	if (m == NULL) {	/* impossible */
315 		IF_DROP(&ifp->if_snd);
316 		error = ENOBUFS;
317 		goto end;
318 	}
319 
320 	gh = mtod(m, struct greip *);
321 	if (sc->g_proto == IPPROTO_GRE) {
322 		/* we don't have any GRE flags for now */
323 
324 		memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
325 		gh->gi_ptype = htons(etype);
326 	}
327 
328 	gh->gi_pr = sc->g_proto;
329 	if (sc->g_proto != IPPROTO_MOBILE) {
330 		gh->gi_src = sc->g_src;
331 		gh->gi_dst = sc->g_dst;
332 		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
333 		((struct ip*)gh)->ip_ttl = ip_gre_ttl;
334 		((struct ip*)gh)->ip_tos = ip->ip_tos;
335 		gh->gi_len = m->m_pkthdr.len;
336 	}
337 
338 	ifp->if_opackets++;
339 	ifp->if_obytes += m->m_pkthdr.len;
340 	/* send it off */
341 	error = ip_output(m, NULL, &sc->route, 0, NULL);
342   end:
343 	if (error)
344 		ifp->if_oerrors++;
345 	return (error);
346 }
347 
348 int
349 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
350 {
351 	struct proc *p = curproc;	/* XXX */
352 	struct ifreq *ifr = (struct ifreq *)data;
353 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
354 	struct gre_softc *sc = ifp->if_softc;
355 	int s;
356 	struct sockaddr_in si;
357 	struct sockaddr *sa = NULL;
358 	int error;
359 
360 	error = 0;
361 
362 	s = splnet();
363 	switch (cmd) {
364 	case SIOCSIFADDR:
365 		ifp->if_flags |= IFF_UP;
366 		break;
367 	case SIOCSIFDSTADDR:
368 		break;
369 	case SIOCSIFFLAGS:
370 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
371 			break;
372 		if ((ifr->ifr_flags & IFF_LINK0) != 0)
373 			sc->g_proto = IPPROTO_GRE;
374 		else
375 			sc->g_proto = IPPROTO_MOBILE;
376 		break;
377 	case SIOCSIFMTU:
378 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
379 			break;
380 		if (ifr->ifr_mtu < 576) {
381 			error = EINVAL;
382 			break;
383 		}
384 		ifp->if_mtu = ifr->ifr_mtu;
385 		break;
386 	case SIOCGIFMTU:
387 		ifr->ifr_mtu = sc->sc_if.if_mtu;
388 		break;
389 	case SIOCADDMULTI:
390 	case SIOCDELMULTI:
391 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
392 			break;
393 		if (ifr == 0) {
394 			error = EAFNOSUPPORT;
395 			break;
396 		}
397 		switch (ifr->ifr_addr.sa_family) {
398 #ifdef INET
399 		case AF_INET:
400 			break;
401 #endif
402 		default:
403 			error = EAFNOSUPPORT;
404 			break;
405 		}
406 		break;
407 	case GRESPROTO:
408 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
409 			break;
410 		sc->g_proto = ifr->ifr_flags;
411 		switch (sc->g_proto) {
412 		case IPPROTO_GRE:
413 			ifp->if_flags |= IFF_LINK0;
414 			break;
415 		case IPPROTO_MOBILE:
416 			ifp->if_flags &= ~IFF_LINK0;
417 			break;
418 		default:
419 			error = EPROTONOSUPPORT;
420 			break;
421 		}
422 		break;
423 	case GREGPROTO:
424 		ifr->ifr_flags = sc->g_proto;
425 		break;
426 	case GRESADDRS:
427 	case GRESADDRD:
428 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
429 			break;
430 		/*
431 		 * set tunnel endpoints, compute a less specific route
432 		 * to the remote end and mark if as up
433 		 */
434 		sa = &ifr->ifr_addr;
435 		if (cmd == GRESADDRS)
436 			sc->g_src = (satosin(sa))->sin_addr;
437 		if (cmd == GRESADDRD)
438 			sc->g_dst = (satosin(sa))->sin_addr;
439 	recompute:
440 		if ((sc->g_src.s_addr != INADDR_ANY) &&
441 		    (sc->g_dst.s_addr != INADDR_ANY)) {
442 			if (sc->route.ro_rt != 0) /* free old route */
443 				RTFREE(sc->route.ro_rt);
444 			if (gre_compute_route(sc) == 0)
445 				ifp->if_flags |= IFF_RUNNING;
446 			else
447 				ifp->if_flags &= ~IFF_RUNNING;
448 		}
449 		break;
450 	case GREGADDRS:
451 		memset(&si, 0, sizeof(si));
452 		si.sin_family = AF_INET;
453 		si.sin_len = sizeof(struct sockaddr_in);
454 		si.sin_addr.s_addr = sc->g_src.s_addr;
455 		sa = sintosa(&si);
456 		ifr->ifr_addr = *sa;
457 		break;
458 	case GREGADDRD:
459 		memset(&si, 0, sizeof(si));
460 		si.sin_family = AF_INET;
461 		si.sin_len = sizeof(struct sockaddr_in);
462 		si.sin_addr.s_addr = sc->g_dst.s_addr;
463 		sa = sintosa(&si);
464 		ifr->ifr_addr = *sa;
465 		break;
466 	case SIOCSLIFPHYADDR:
467 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
468 			break;
469 		if (lifr->addr.ss_family != AF_INET ||
470 		    lifr->dstaddr.ss_family != AF_INET) {
471 			error = EAFNOSUPPORT;
472 			break;
473 		}
474 		if (lifr->addr.ss_len != sizeof(si) ||
475 		    lifr->dstaddr.ss_len != sizeof(si)) {
476 			error = EINVAL;
477 			break;
478 		}
479 		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
480 		sc->g_dst =
481 		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
482 		goto recompute;
483 	case SIOCDIFPHYADDR:
484 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
485 			break;
486 		sc->g_src.s_addr = INADDR_ANY;
487 		sc->g_dst.s_addr = INADDR_ANY;
488 		break;
489 	case SIOCGLIFPHYADDR:
490 		if (sc->g_src.s_addr == INADDR_ANY ||
491 		    sc->g_dst.s_addr == INADDR_ANY) {
492 			error = EADDRNOTAVAIL;
493 			break;
494 		}
495 		memset(&si, 0, sizeof(si));
496 		si.sin_family = AF_INET;
497 		si.sin_len = sizeof(struct sockaddr_in);
498 		si.sin_addr.s_addr = sc->g_src.s_addr;
499 		memcpy(&lifr->addr, &si, sizeof(si));
500 		si.sin_addr.s_addr = sc->g_dst.s_addr;
501 		memcpy(&lifr->dstaddr, &si, sizeof(si));
502 		break;
503 	default:
504 		error = EINVAL;
505 		break;
506 	}
507 
508 	splx(s);
509 	return (error);
510 }
511 
512 /*
513  * computes a route to our destination that is not the one
514  * which would be taken by ip_output(), as this one will loop back to
515  * us. If the interface is p2p as  a--->b, then a routing entry exists
516  * If we now send a packet to b (e.g. ping b), this will come down here
517  * gets src=a, dst=b tacked on and would from ip_ouput() sent back to
518  * if_gre.
519  * Goal here is to compute a route to b that is less specific than
520  * a-->b. We know that this one exists as in normal operation we have
521  * at least a default route which matches.
522  */
523 int
524 gre_compute_route(struct gre_softc *sc)
525 {
526 	struct route *ro;
527 	u_int32_t a, b, c;
528 
529 	ro = &sc->route;
530 
531 	memset(ro, 0, sizeof(struct route));
532 	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
533 	ro->ro_dst.sa_family = AF_INET;
534 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
535 
536 	/*
537 	 * toggle last bit, so our interface is not found, but a less
538 	 * specific route. I'd rather like to specify a shorter mask,
539 	 * but this is not possible. Should work though. XXX
540 	 * there is a simpler way ...
541 	 */
542 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
543 		a = ntohl(sc->g_dst.s_addr);
544 		b = a & 0x01;
545 		c = a & 0xfffffffe;
546 		b = b ^ 0x01;
547 		a = b | c;
548 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
549 		    = htonl(a);
550 	}
551 
552 #ifdef DIAGNOSTIC
553 	printf("%s: searching a route to %s", sc->sc_if.if_xname,
554 	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
555 #endif
556 
557 	rtalloc(ro);
558 
559 	/*
560 	 * check if this returned a route at all and this route is no
561 	 * recursion to ourself
562 	 */
563 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
564 #ifdef DIAGNOSTIC
565 		if (ro->ro_rt == NULL)
566 			printf(" - no route found!\n");
567 		else
568 			printf(" - route loops back to ourself!\n");
569 #endif
570 		return EADDRNOTAVAIL;
571 	}
572 
573 	/*
574 	 * now change it back - else ip_output will just drop
575 	 * the route and search one to this interface ...
576 	 */
577 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
578 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
579 
580 #ifdef DIAGNOSTIC
581 	printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname,
582 	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
583 	printf("\n");
584 #endif
585 
586 	return 0;
587 }
588 
589 /*
590  * do a checksum of a buffer - much like in_cksum, which operates on
591  * mbufs.
592  */
593 u_short
594 gre_in_cksum(u_short *p, u_int len)
595 {
596 	u_int sum = 0;
597 	int nwords = len >> 1;
598 
599 	while (nwords-- != 0)
600 		sum += *p++;
601 
602 	if (len & 1) {
603 		union {
604 			u_short w;
605 			u_char c[2];
606 		} u;
607 		u.c[0] = *(u_char *)p;
608 		u.c[1] = 0;
609 		sum += u.w;
610 	}
611 
612 	/* end-around-carry */
613 	sum = (sum >> 16) + (sum & 0xffff);
614 	sum += (sum >> 16);
615 	return (~sum);
616 }
617