Home | History | Annotate | Line # | Download | only in net
if_mpls.c revision 1.38
      1 /*	$NetBSD: if_mpls.c,v 1.38 2022/07/29 15:25:51 skrll Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Mihai Chelaru <kefren (at) NetBSD.org>
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: if_mpls.c,v 1.38 2022/07/29 15:25:51 skrll Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_inet.h"
     37 #include "opt_mpls.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 
     42 #include <sys/errno.h>
     43 #include <sys/malloc.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/sysctl.h>
     46 
     47 #include <net/bpf.h>
     48 #include <net/if.h>
     49 #include <net/if_types.h>
     50 #include <net/netisr.h>
     51 #include <net/route.h>
     52 #include <sys/device.h>
     53 #include <sys/module.h>
     54 #include <sys/atomic.h>
     55 
     56 #ifdef INET
     57 #include <netinet/in.h>
     58 #include <netinet/in_systm.h>
     59 #include <netinet/in_var.h>
     60 #include <netinet/ip.h>
     61 #include <netinet/ip_var.h>
     62 #endif
     63 
     64 #ifdef INET6
     65 #include <netinet/ip6.h>
     66 #include <netinet6/in6_var.h>
     67 #include <netinet6/ip6_var.h>
     68 #endif
     69 
     70 #include <netmpls/mpls.h>
     71 #include <netmpls/mpls_var.h>
     72 
     73 #include "if_mpls.h"
     74 
     75 #include "ioconf.h"
     76 
     77 static int mpls_clone_create(struct if_clone *, int);
     78 static int mpls_clone_destroy(struct ifnet *);
     79 
     80 static struct if_clone mpls_if_cloner =
     81 	IF_CLONE_INITIALIZER("mpls", mpls_clone_create, mpls_clone_destroy);
     82 
     83 static void mpls_input(struct ifnet *, struct mbuf *);
     84 static int mpls_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
     85     const struct rtentry *);
     86 static int mpls_ioctl(struct ifnet *, u_long, void *);
     87 static int mpls_send_frame(struct mbuf *, struct ifnet *,
     88     const struct rtentry *);
     89 static int mpls_lse(struct mbuf *);
     90 
     91 #ifdef INET
     92 static struct mbuf *mpls_unlabel_inet(struct mbuf *, int *error);
     93 static struct mbuf *mpls_label_inet(struct mbuf *, union mpls_shim *, uint);
     94 #endif
     95 
     96 #ifdef INET6
     97 static struct mbuf *mpls_unlabel_inet6(struct mbuf *, int *error);
     98 static struct mbuf *mpls_label_inet6(struct mbuf *, union mpls_shim *, uint);
     99 #endif
    100 
    101 static struct mbuf *mpls_prepend_shim(struct mbuf *, union mpls_shim *);
    102 
    103 extern int mpls_defttl, mpls_mapttl_inet, mpls_mapttl_inet6, mpls_icmp_respond,
    104     mpls_forwarding, mpls_frame_accept, mpls_mapprec_inet, mpls_mapclass_inet6,
    105     mpls_rfc4182;
    106 
    107 static u_int mpls_count;
    108 /* ARGSUSED */
    109 void
    110 mplsattach(int count)
    111 {
    112 	/*
    113 	 * Nothing to do here, initialization is handled by the
    114 	 * module initialization code in mplsinit() below).
    115 	 */
    116 }
    117 
    118 static void
    119 mplsinit(void)
    120 {
    121 	if_clone_attach(&mpls_if_cloner);
    122 }
    123 
    124 static int
    125 mplsdetach(void)
    126 {
    127 	int error = 0;
    128 
    129 	if (mpls_count != 0)
    130 		error = EBUSY;
    131 
    132 	if (error == 0)
    133 		if_clone_detach(&mpls_if_cloner);
    134 
    135 	return error;
    136 }
    137 
    138 static int
    139 mpls_clone_create(struct if_clone *ifc, int unit)
    140 {
    141 	struct mpls_softc *sc;
    142 
    143 	atomic_inc_uint(&mpls_count);
    144 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
    145 
    146 	if_initname(&sc->sc_if, ifc->ifc_name, unit);
    147 	sc->sc_if.if_softc = sc;
    148 	sc->sc_if.if_type = IFT_MPLS;
    149 	sc->sc_if.if_addrlen = 0;
    150 	sc->sc_if.if_hdrlen = sizeof(union mpls_shim);
    151 	sc->sc_if.if_dlt = DLT_NULL;
    152 	sc->sc_if.if_mtu = 1500;
    153 	sc->sc_if.if_flags = 0;
    154 	sc->sc_if._if_input = mpls_input;
    155 	sc->sc_if.if_output = mpls_output;
    156 	sc->sc_if.if_ioctl = mpls_ioctl;
    157 
    158 	if_attach(&sc->sc_if);
    159 	if_alloc_sadl(&sc->sc_if);
    160 	bpf_attach(&sc->sc_if, DLT_NULL, sizeof(uint32_t));
    161 	return 0;
    162 }
    163 
    164 static int
    165 mpls_clone_destroy(struct ifnet *ifp)
    166 {
    167 
    168 	bpf_detach(ifp);
    169 	if_detach(ifp);
    170 
    171 	free(ifp->if_softc, M_DEVBUF);
    172 	atomic_dec_uint(&mpls_count);
    173 	return 0;
    174 }
    175 
    176 static void
    177 mpls_input(struct ifnet *ifp, struct mbuf *m)
    178 {
    179 #if 0
    180 	/*
    181 	 * TODO - kefren
    182 	 * I'd love to unshim the packet, guess family
    183 	 * and pass it to bpf
    184 	 */
    185 	bpf_mtap_af(ifp, AF_MPLS, m, BPF_D_IN);
    186 #endif
    187 
    188 	mpls_lse(m);
    189 }
    190 
    191 void
    192 mplsintr(void)
    193 {
    194 	struct mbuf *m;
    195 
    196 	for (;;) {
    197 		IFQ_LOCK(&mplsintrq);
    198 		IF_DEQUEUE(&mplsintrq, m);
    199 		IFQ_UNLOCK(&mplsintrq);
    200 
    201 		if (!m)
    202 			return;
    203 
    204 		if (((m->m_flags & M_PKTHDR) == 0) ||
    205 		    (m->m_pkthdr.rcvif_index == 0))
    206 			panic("mplsintr(): no pkthdr or rcvif");
    207 
    208 #ifdef MBUFTRACE
    209 		m_claimm(m, &mpls_owner);
    210 #endif
    211 		mpls_input(m_get_rcvif_NOMPSAFE(m), m);
    212 	}
    213 }
    214 
    215 /*
    216  * prepend shim and deliver
    217  */
    218 static int
    219 mpls_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    220     const struct rtentry *rt)
    221 {
    222 	union mpls_shim mh, *pms;
    223 	struct rtentry *rt1;
    224 	int err;
    225 	uint psize = sizeof(struct sockaddr_mpls);
    226 
    227 	KASSERT(KERNEL_LOCKED_P());
    228 
    229 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
    230 		m_freem(m);
    231 		return ENETDOWN;
    232 	}
    233 
    234 	if (rt_gettag(rt) == NULL || rt_gettag(rt)->sa_family != AF_MPLS) {
    235 		m_freem(m);
    236 		return EINVAL;
    237 	}
    238 
    239 	bpf_mtap_af(ifp, dst->sa_family, m, BPF_D_OUT);
    240 
    241 	memset(&mh, 0, sizeof(mh));
    242 	mh.s_addr = MPLS_GETSADDR(rt);
    243 	mh.shim.bos = 1;
    244 	mh.shim.exp = 0;
    245 	mh.shim.ttl = mpls_defttl;
    246 
    247 	pms = &((struct sockaddr_mpls*)rt_gettag(rt))->smpls_addr;
    248 
    249 	while (psize <= rt_gettag(rt)->sa_len - sizeof(mh)) {
    250 		pms++;
    251 		if (mh.shim.label != MPLS_LABEL_IMPLNULL &&
    252 		    ((m = mpls_prepend_shim(m, &mh)) == NULL))
    253 			return ENOBUFS;
    254 		memset(&mh, 0, sizeof(mh));
    255 		mh.s_addr = ntohl(pms->s_addr);
    256 		mh.shim.bos = mh.shim.exp = 0;
    257 		mh.shim.ttl = mpls_defttl;
    258 		psize += sizeof(mh);
    259 	}
    260 
    261 	switch (dst->sa_family) {
    262 #ifdef INET
    263 	case AF_INET:
    264 		m = mpls_label_inet(m, &mh, psize - sizeof(struct sockaddr_mpls));
    265 		break;
    266 #endif
    267 #ifdef INET6
    268 	case AF_INET6:
    269 		m = mpls_label_inet6(m, &mh, psize - sizeof(struct sockaddr_mpls));
    270 		break;
    271 #endif
    272 	default:
    273 		m = mpls_prepend_shim(m, &mh);
    274 		break;
    275 	}
    276 
    277 	if (m == NULL) {
    278 		IF_DROP(&ifp->if_snd);
    279 		if_statinc(ifp, if_oerrors);
    280 		return ENOBUFS;
    281 	}
    282 
    283 	if_statadd2(ifp, if_opackets, 1, if_obytes, m->m_pkthdr.len);
    284 
    285 	if ((rt1 = rtalloc1(rt->rt_gateway, 1)) == NULL) {
    286 		m_freem(m);
    287 		return EHOSTUNREACH;
    288 	}
    289 
    290 	err = mpls_send_frame(m, rt1->rt_ifp, rt);
    291 	rt_unref(rt1);
    292 	return err;
    293 }
    294 
    295 static int
    296 mpls_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    297 {
    298 	int error = 0, s = splnet();
    299 	struct ifreq *ifr = data;
    300 
    301 	switch(cmd) {
    302 	case SIOCINITIFADDR:
    303 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
    304 		break;
    305 	case SIOCSIFMTU:
    306 		if (ifr != NULL && ifr->ifr_mtu < 576) {
    307 			error = EINVAL;
    308 			break;
    309 		}
    310 		/* FALLTHROUGH */
    311 	case SIOCGIFMTU:
    312 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    313 			error = 0;
    314 		break;
    315 	case SIOCSIFFLAGS:
    316 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    317 			break;
    318 		if (ifp->if_flags & IFF_UP)
    319 			ifp->if_flags |= IFF_RUNNING;
    320 		break;
    321 	default:
    322 		error = ifioctl_common(ifp, cmd, data);
    323 		break;
    324 	}
    325 	splx(s);
    326 	return error;
    327 }
    328 
    329 static inline struct mbuf *
    330 mpls_trim_label(struct mbuf *m, union mpls_shim *sh)
    331 {
    332 	m_adj(m, sizeof(union mpls_shim));
    333 
    334 	if (m->m_len < sizeof(union mpls_shim) &&
    335 	    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL)
    336 		return NULL;
    337 
    338 	sh->s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
    339 
    340 	return m;
    341 }
    342 
    343 /*
    344  * MPLS Label Switch Engine
    345  */
    346 static int
    347 mpls_lse(struct mbuf *m)
    348 {
    349 	struct sockaddr_mpls dst;
    350 	union mpls_shim tshim, *htag;
    351 	struct rtentry *rt = NULL;
    352 	int error = ENOBUFS;
    353 	uint psize = sizeof(struct sockaddr_mpls);
    354 	bool push_back_alert = false;
    355 
    356 	/* If we're not accepting MPLS frames, leave now. */
    357 	if (!mpls_frame_accept) {
    358 		error = EINVAL;
    359 		goto done;
    360 	}
    361 
    362 	if (m->m_len < sizeof(union mpls_shim) &&
    363 	    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL)
    364 		goto done;
    365 
    366 	dst.smpls_len = sizeof(struct sockaddr_mpls);
    367 	dst.smpls_family = AF_MPLS;
    368 	dst.smpls_addr.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
    369 
    370 	error = EINVAL;
    371 
    372 	/* TTL decrement */
    373 	if ((m = mpls_ttl_dec(m)) == NULL)
    374 		goto done;
    375 
    376 	/* RFC 4182 */
    377 	if (mpls_rfc4182 != 0) {
    378 		while ((dst.smpls_addr.shim.label == MPLS_LABEL_IPV4NULL ||
    379 		    dst.smpls_addr.shim.label == MPLS_LABEL_IPV6NULL) &&
    380 		    __predict_false(dst.smpls_addr.shim.bos == 0)) {
    381 			m = mpls_trim_label(m, &dst.smpls_addr);
    382 			if (m == NULL) {
    383 				goto done;
    384 			}
    385 		}
    386 	}
    387 
    388 	/* RFC 3032 Section 2.1 Page 4 */
    389 	if (__predict_false(dst.smpls_addr.shim.label == MPLS_LABEL_RTALERT) &&
    390 	    dst.smpls_addr.shim.bos == 0) {
    391 		m = mpls_trim_label(m, &dst.smpls_addr);
    392 		if (m == NULL) {
    393 			goto done;
    394 		}
    395 		push_back_alert = true;
    396 	}
    397 
    398 	if (dst.smpls_addr.shim.label <= MPLS_LABEL_RESMAX) {
    399 		/* Don't swap reserved labels */
    400 		switch (dst.smpls_addr.shim.label) {
    401 #ifdef INET
    402 		case MPLS_LABEL_IPV4NULL:
    403 			/* Pop shim and push mbuf to IP stack */
    404 			if (dst.smpls_addr.shim.bos) {
    405 				m = mpls_unlabel_inet(m, &error);
    406 			}
    407 			break;
    408 #endif
    409 #ifdef INET6
    410 		case MPLS_LABEL_IPV6NULL:
    411 			/* Pop shim and push mbuf to IPv6 stack */
    412 			if (dst.smpls_addr.shim.bos) {
    413 				m = mpls_unlabel_inet6(m, &error);
    414 			}
    415 			break;
    416 #endif
    417 		case MPLS_LABEL_RTALERT:	/* Yeah, I'm all alerted */
    418 		case MPLS_LABEL_IMPLNULL:	/* This is logical only */
    419 		default:			/* Rest are not allowed */
    420 			break;
    421 		}
    422 		goto done;
    423 	}
    424 
    425 	/* Check if we should do MPLS forwarding */
    426 	error = EHOSTUNREACH;
    427 	if (!mpls_forwarding)
    428 		goto done;
    429 
    430 	/* Get a route to dst */
    431 	dst.smpls_addr.shim.ttl = 0;
    432 	dst.smpls_addr.shim.bos = 0;
    433 	dst.smpls_addr.shim.exp = 0;
    434 	dst.smpls_addr.s_addr = htonl(dst.smpls_addr.s_addr);
    435 	if ((rt = rtalloc1((const struct sockaddr*)&dst, 1)) == NULL)
    436 		goto done;
    437 
    438 	/* MPLS packet with no MPLS tagged route ? */
    439 	if ((rt->rt_flags & RTF_GATEWAY) == 0 ||
    440 	     rt_gettag(rt) == NULL ||
    441 	     rt_gettag(rt)->sa_family != AF_MPLS)
    442 		goto done;
    443 
    444 	tshim.s_addr = MPLS_GETSADDR(rt);
    445 
    446 	/* Swap labels */
    447 	if ((m->m_len < sizeof(union mpls_shim)) &&
    448 	    (m = m_pullup(m, sizeof(union mpls_shim))) == 0) {
    449 		error = ENOBUFS;
    450 		goto done;
    451 	}
    452 
    453 	/* Replace only the label */
    454 	htag = mtod(m, union mpls_shim *);
    455 	htag->s_addr = ntohl(htag->s_addr);
    456 	htag->shim.label = tshim.shim.label;
    457 	htag->s_addr = htonl(htag->s_addr);
    458 
    459 	/* check if there is anything more to prepend */
    460 	htag = &((struct sockaddr_mpls*)rt_gettag(rt))->smpls_addr;
    461 	while (psize <= rt_gettag(rt)->sa_len - sizeof(tshim)) {
    462 		htag++;
    463 		memset(&tshim, 0, sizeof(tshim));
    464 		tshim.s_addr = ntohl(htag->s_addr);
    465 		tshim.shim.bos = tshim.shim.exp = 0;
    466 		tshim.shim.ttl = mpls_defttl;
    467 		if (tshim.shim.label != MPLS_LABEL_IMPLNULL &&
    468 		    ((m = mpls_prepend_shim(m, &tshim)) == NULL)) {
    469 			error = ENOBUFS;
    470 			goto done;
    471 		}
    472 		psize += sizeof(tshim);
    473 	}
    474 
    475 	if (__predict_false(push_back_alert == true)) {
    476 		/* re-add the router alert label */
    477 		memset(&tshim, 0, sizeof(tshim));
    478 		tshim.s_addr = MPLS_LABEL_RTALERT;
    479 		tshim.shim.bos = tshim.shim.exp = 0;
    480 		tshim.shim.ttl = mpls_defttl;
    481 		if ((m = mpls_prepend_shim(m, &tshim)) == NULL) {
    482 			error = ENOBUFS;
    483 			goto done;
    484 		}
    485 	}
    486 
    487 	if ((rt->rt_flags & RTF_GATEWAY) == 0) {
    488 		error = EHOSTUNREACH;
    489 		goto done;
    490 	}
    491 
    492 	rt->rt_use++;
    493 	error = mpls_send_frame(m, rt->rt_ifp, rt);
    494 
    495 done:
    496 	if (error != 0 && m != NULL)
    497 		m_freem(m);
    498 	if (rt != NULL)
    499 		rt_unref(rt);
    500 
    501 	return error;
    502 }
    503 
    504 static int
    505 mpls_send_frame(struct mbuf *m, struct ifnet *ifp, const struct rtentry *rt)
    506 {
    507 	union mpls_shim msh;
    508 	int ret;
    509 
    510 	msh.s_addr = MPLS_GETSADDR(rt);
    511 	if (msh.shim.label == MPLS_LABEL_IMPLNULL ||
    512 	    (m->m_flags & (M_MCAST | M_BCAST))) {
    513 		m_adj(m, sizeof(union mpls_shim));
    514 		m->m_pkthdr.csum_flags = 0;
    515 	}
    516 
    517 	switch(ifp->if_type) {
    518 	/* only these are supported for now */
    519 	case IFT_ETHER:
    520 	case IFT_TUNNEL:
    521 	case IFT_LOOP:
    522 #ifdef INET
    523 		ret = ip_if_output(ifp, m, rt->rt_gateway, rt);
    524 #else
    525 		ret = if_output_lock(ifp, ifp, m, rt->rt_gateway, rt);
    526 #endif
    527 		return ret;
    528 		break;
    529 	default:
    530 		return ENETUNREACH;
    531 	}
    532 	return 0;
    533 }
    534 
    535 #ifdef INET
    536 static struct mbuf *
    537 mpls_unlabel_inet(struct mbuf *m, int *error)
    538 {
    539 	struct ip *iph;
    540 	union mpls_shim ms;
    541 	int iphlen;
    542 
    543 	if (mpls_mapttl_inet || mpls_mapprec_inet) {
    544 		/* get shim info */
    545 		ms.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
    546 
    547 		/* and get rid of it */
    548 		m_adj(m, sizeof(union mpls_shim));
    549 
    550 		/* get ip header */
    551 		if (m->m_len < sizeof(struct ip) &&
    552 		    (m = m_pullup(m, sizeof(struct ip))) == NULL) {
    553 			*error = ENOBUFS;
    554 			return NULL;
    555 		}
    556 
    557 		iph = mtod(m, struct ip *);
    558 		iphlen = iph->ip_hl << 2;
    559 
    560 		/* get it all */
    561 		if (m->m_len < iphlen) {
    562 			if ((m = m_pullup(m, iphlen)) == NULL) {
    563 				*error = ENOBUFS;
    564 				return NULL;
    565 			}
    566 			iph = mtod(m, struct ip *);
    567 		}
    568 
    569 		/* check ipsum */
    570 		if (in_cksum(m, iphlen) != 0) {
    571 			m_freem(m);
    572 			*error = EINVAL;
    573 			return NULL;
    574 		}
    575 
    576 		/* set IP ttl from MPLS ttl */
    577 		if (mpls_mapttl_inet)
    578 			iph->ip_ttl = ms.shim.ttl;
    579 
    580 		/* set IP Precedence from MPLS Exp */
    581 		if (mpls_mapprec_inet) {
    582 			iph->ip_tos = (iph->ip_tos << 3) >> 3;
    583 			iph->ip_tos |= ms.shim.exp << 5;
    584 		}
    585 
    586 		/* reset ipsum because we modified TTL and TOS */
    587 		iph->ip_sum = 0;
    588 		iph->ip_sum = in_cksum(m, iphlen);
    589 	} else {
    590 		m_adj(m, sizeof(union mpls_shim));
    591 	}
    592 
    593 	/* Put it on IP queue */
    594 	if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
    595 		m_freem(m);
    596 		*error = ENOBUFS;
    597 		return NULL;
    598 	}
    599 
    600 	*error = 0;
    601 	return m;
    602 }
    603 
    604 /*
    605  * Prepend MPLS label
    606  */
    607 static struct mbuf *
    608 mpls_label_inet(struct mbuf *m, union mpls_shim *ms, uint offset)
    609 {
    610 	struct ip iphdr;
    611 
    612 	if (mpls_mapttl_inet || mpls_mapprec_inet) {
    613 		/* XXX Maybe just check m->m_pkthdr.len instead? */
    614 		if ((m->m_len < offset + sizeof(struct ip)) &&
    615 		    (m = m_pullup(m, offset + sizeof(struct ip))) == 0)
    616 			return NULL;
    617 
    618 		m_copydata(m, offset, sizeof(struct ip), &iphdr);
    619 
    620 		/* Map TTL */
    621 		if (mpls_mapttl_inet)
    622 			ms->shim.ttl = iphdr.ip_ttl;
    623 
    624 		/* Copy IP precedence to EXP */
    625 		if (mpls_mapprec_inet)
    626 			ms->shim.exp = ((u_int8_t)iphdr.ip_tos) >> 5;
    627 	}
    628 
    629 	if ((m = mpls_prepend_shim(m, ms)) == NULL)
    630 		return NULL;
    631 
    632 	return m;
    633 }
    634 #endif	/* INET */
    635 
    636 #ifdef INET6
    637 static struct mbuf *
    638 mpls_unlabel_inet6(struct mbuf *m, int *error)
    639 {
    640 	struct ip6_hdr *ip6hdr;
    641 	union mpls_shim ms;
    642 
    643 	/* TODO: mapclass */
    644 	if (mpls_mapttl_inet6) {
    645 		ms.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
    646 		m_adj(m, sizeof(union mpls_shim));
    647 
    648 		if (m->m_len < sizeof(struct ip6_hdr) &&
    649 		    (m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
    650 			*error = ENOBUFS;
    651 			return NULL;
    652 		}
    653 		ip6hdr = mtod(m, struct ip6_hdr *);
    654 
    655 		/* Because we just decremented this in mpls_lse */
    656 		ip6hdr->ip6_hlim = ms.shim.ttl + 1;
    657 	} else {
    658 		m_adj(m, sizeof(union mpls_shim));
    659 	}
    660 
    661 	/* Put it back on IPv6 queue. */
    662 	if (__predict_false(!pktq_enqueue(ip6_pktq, m, 0))) {
    663 		m_freem(m);
    664 		*error = ENOBUFS;
    665 		return NULL;
    666 	}
    667 
    668 	*error = 0;
    669 	return m;
    670 }
    671 
    672 static struct mbuf *
    673 mpls_label_inet6(struct mbuf *m, union mpls_shim *ms, uint offset)
    674 {
    675 	struct ip6_hdr ip6h;
    676 
    677 	if (mpls_mapttl_inet6 || mpls_mapclass_inet6) {
    678 		/* XXX Maybe just check m->m_pkthdr.len instead? */
    679 		if ((m->m_len < offset + sizeof(struct ip6_hdr)) &&
    680 		    (m = m_pullup(m, offset + sizeof(struct ip6_hdr))) == 0)
    681 			return NULL;
    682 
    683 		m_copydata(m, offset, sizeof(struct ip6_hdr), &ip6h);
    684 
    685 		if (mpls_mapttl_inet6)
    686 			ms->shim.ttl = ip6h.ip6_hlim;
    687 
    688 		if (mpls_mapclass_inet6)
    689 			ms->shim.exp = ip6h.ip6_vfc << 1 >> 5;
    690 	}
    691 
    692 	if ((m = mpls_prepend_shim(m, ms)) == NULL)
    693 		return NULL;
    694 
    695 	return m;
    696 }
    697 #endif	/* INET6 */
    698 
    699 static struct mbuf *
    700 mpls_prepend_shim(struct mbuf *m, union mpls_shim *ms)
    701 {
    702 	union mpls_shim *shim;
    703 
    704 	M_PREPEND(m, sizeof(*ms), M_DONTWAIT);
    705 	if (m == NULL)
    706 		return NULL;
    707 
    708 	if (m->m_len < sizeof(union mpls_shim) &&
    709 	    (m = m_pullup(m, sizeof(union mpls_shim))) == 0)
    710 		return NULL;
    711 
    712 	shim = mtod(m, union mpls_shim *);
    713 
    714 	memcpy(shim, ms, sizeof(*shim));
    715 	shim->s_addr = htonl(shim->s_addr);
    716 
    717 	return m;
    718 }
    719 
    720 /*
    721  * Module infrastructure
    722  */
    723 #include "if_module.h"
    724 
    725 IF_MODULE(MODULE_CLASS_DRIVER, mpls, NULL)
    726