Home | History | Annotate | Line # | Download | only in net
if_mpls.c revision 1.32
      1 /*	$NetBSD: if_mpls.c,v 1.32 2017/12/09 10:30:30 maxv 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.32 2017/12/09 10:30:30 maxv 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 #define TRIM_LABEL do { \
     78 	m_adj(m, sizeof(union mpls_shim)); \
     79 	if (m->m_len < sizeof(union mpls_shim) && \
     80 	    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL) \
     81 		goto done; \
     82 	dst.smpls_addr.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr); \
     83 	} while (/* CONSTCOND */ 0)
     84 
     85 
     86 static int mpls_clone_create(struct if_clone *, int);
     87 static int mpls_clone_destroy(struct ifnet *);
     88 
     89 static struct if_clone mpls_if_cloner =
     90 	IF_CLONE_INITIALIZER("mpls", mpls_clone_create, mpls_clone_destroy);
     91 
     92 
     93 static void mpls_input(struct ifnet *, struct mbuf *);
     94 static int mpls_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
     95     const struct rtentry *);
     96 static int mpls_ioctl(struct ifnet *, u_long, void *);
     97 static int mpls_send_frame(struct mbuf *, struct ifnet *,
     98     const struct rtentry *);
     99 static int mpls_lse(struct mbuf *);
    100 
    101 #ifdef INET
    102 static struct mbuf *mpls_unlabel_inet(struct mbuf *, int *error);
    103 static struct mbuf *mpls_label_inet(struct mbuf *, union mpls_shim *, uint);
    104 #endif
    105 
    106 #ifdef INET6
    107 static struct mbuf *mpls_unlabel_inet6(struct mbuf *, int *error);
    108 static struct mbuf *mpls_label_inet6(struct mbuf *, union mpls_shim *, uint);
    109 #endif
    110 
    111 static struct mbuf *mpls_prepend_shim(struct mbuf *, union mpls_shim *);
    112 
    113 extern int mpls_defttl, mpls_mapttl_inet, mpls_mapttl_inet6, mpls_icmp_respond,
    114     mpls_forwarding, mpls_frame_accept, mpls_mapprec_inet, mpls_mapclass_inet6,
    115     mpls_rfc4182;
    116 
    117 static u_int mpls_count;
    118 /* ARGSUSED */
    119 void
    120 mplsattach(int count)
    121 {
    122 	/*
    123 	 * Nothing to do here, initialization is handled by the
    124 	 * module initialization code in mplsinit() below).
    125 	 */
    126 }
    127 
    128 static void
    129 mplsinit(void)
    130 {
    131 	if_clone_attach(&mpls_if_cloner);
    132 }
    133 
    134 static int
    135 mplsdetach(void)
    136 {
    137 	int error = 0;
    138 
    139 	if (mpls_count != 0)
    140 		error = EBUSY;
    141 
    142 	if (error == 0)
    143 		if_clone_detach(&mpls_if_cloner);
    144 
    145 	return error;
    146 }
    147 
    148 static int
    149 mpls_clone_create(struct if_clone *ifc, int unit)
    150 {
    151 	struct mpls_softc *sc;
    152 	int rv;
    153 
    154 	atomic_inc_uint(&mpls_count);
    155 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
    156 
    157 	if_initname(&sc->sc_if, ifc->ifc_name, unit);
    158 	sc->sc_if.if_softc = sc;
    159 	sc->sc_if.if_type = IFT_MPLS;
    160 	sc->sc_if.if_addrlen = 0;
    161 	sc->sc_if.if_hdrlen = sizeof(union mpls_shim);
    162 	sc->sc_if.if_dlt = DLT_NULL;
    163 	sc->sc_if.if_mtu = 1500;
    164 	sc->sc_if.if_flags = 0;
    165 	sc->sc_if._if_input = mpls_input;
    166 	sc->sc_if.if_output = mpls_output;
    167 	sc->sc_if.if_ioctl = mpls_ioctl;
    168 
    169 	rv = if_attach(&sc->sc_if);
    170 	if (rv != 0) {
    171 		free(sc, M_DEVBUF);
    172 		atomic_dec_uint(&mpls_count);
    173 		return rv;
    174 	}
    175 	if_alloc_sadl(&sc->sc_if);
    176 	bpf_attach(&sc->sc_if, DLT_NULL, sizeof(uint32_t));
    177 	return 0;
    178 }
    179 
    180 static int
    181 mpls_clone_destroy(struct ifnet *ifp)
    182 {
    183 	int s;
    184 
    185 	bpf_detach(ifp);
    186 
    187 	s = splnet();
    188 	if_detach(ifp);
    189 	splx(s);
    190 
    191 	free(ifp->if_softc, M_DEVBUF);
    192 	atomic_dec_uint(&mpls_count);
    193 	return 0;
    194 }
    195 
    196 static void
    197 mpls_input(struct ifnet *ifp, struct mbuf *m)
    198 {
    199 #if 0
    200 	/*
    201 	 * TODO - kefren
    202 	 * I'd love to unshim the packet, guess family
    203 	 * and pass it to bpf
    204 	 */
    205 	bpf_mtap_af(ifp, AF_MPLS, m);
    206 #endif
    207 
    208 	mpls_lse(m);
    209 }
    210 
    211 void
    212 mplsintr(void)
    213 {
    214 	struct mbuf *m;
    215 
    216 	for (;;) {
    217 		IFQ_LOCK(&mplsintrq);
    218 		IF_DEQUEUE(&mplsintrq, m);
    219 		IFQ_UNLOCK(&mplsintrq);
    220 
    221 		if (!m)
    222 			return;
    223 
    224 		if (((m->m_flags & M_PKTHDR) == 0) ||
    225 		    (m->m_pkthdr.rcvif_index == 0))
    226 			panic("mplsintr(): no pkthdr or rcvif");
    227 
    228 #ifdef MBUFTRACE
    229 		m_claimm(m, &mpls_owner);
    230 #endif
    231 		mpls_input(m_get_rcvif_NOMPSAFE(m), m);
    232 	}
    233 }
    234 
    235 /*
    236  * prepend shim and deliver
    237  */
    238 static int
    239 mpls_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    240     const struct rtentry *rt)
    241 {
    242 	union mpls_shim mh, *pms;
    243 	struct rtentry *rt1;
    244 	int err;
    245 	uint psize = sizeof(struct sockaddr_mpls);
    246 
    247 	KASSERT(KERNEL_LOCKED_P());
    248 
    249 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
    250 		m_freem(m);
    251 		return ENETDOWN;
    252 	}
    253 
    254 	if (rt_gettag(rt) == NULL || rt_gettag(rt)->sa_family != AF_MPLS) {
    255 		m_freem(m);
    256 		return EINVAL;
    257 	}
    258 
    259 	bpf_mtap_af(ifp, dst->sa_family, m);
    260 
    261 	memset(&mh, 0, sizeof(mh));
    262 	mh.s_addr = MPLS_GETSADDR(rt);
    263 	mh.shim.bos = 1;
    264 	mh.shim.exp = 0;
    265 	mh.shim.ttl = mpls_defttl;
    266 
    267 	pms = &((struct sockaddr_mpls*)rt_gettag(rt))->smpls_addr;
    268 
    269 	while (psize <= rt_gettag(rt)->sa_len - sizeof(mh)) {
    270 		pms++;
    271 		if (mh.shim.label != MPLS_LABEL_IMPLNULL &&
    272 		    ((m = mpls_prepend_shim(m, &mh)) == NULL))
    273 			return ENOBUFS;
    274 		memset(&mh, 0, sizeof(mh));
    275 		mh.s_addr = ntohl(pms->s_addr);
    276 		mh.shim.bos = mh.shim.exp = 0;
    277 		mh.shim.ttl = mpls_defttl;
    278 		psize += sizeof(mh);
    279 	}
    280 
    281 	switch (dst->sa_family) {
    282 #ifdef INET
    283 	case AF_INET:
    284 		m = mpls_label_inet(m, &mh, psize - sizeof(struct sockaddr_mpls));
    285 		break;
    286 #endif
    287 #ifdef INET6
    288 	case AF_INET6:
    289 		m = mpls_label_inet6(m, &mh, psize - sizeof(struct sockaddr_mpls));
    290 		break;
    291 #endif
    292 	default:
    293 		m = mpls_prepend_shim(m, &mh);
    294 		break;
    295 	}
    296 
    297 	if (m == NULL) {
    298 		IF_DROP(&ifp->if_snd);
    299 		ifp->if_oerrors++;
    300 		return ENOBUFS;
    301 	}
    302 
    303 	ifp->if_opackets++;
    304 	ifp->if_obytes += m->m_pkthdr.len;
    305 
    306 	if ((rt1=rtalloc1(rt->rt_gateway, 1)) == NULL) {
    307 		m_freem(m);
    308 		return EHOSTUNREACH;
    309 	}
    310 
    311 	err = mpls_send_frame(m, rt1->rt_ifp, rt);
    312 	rt_unref(rt1);
    313 	return err;
    314 }
    315 
    316 static int
    317 mpls_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    318 {
    319 	int error = 0, s = splnet();
    320 	struct ifreq *ifr = data;
    321 
    322 	switch(cmd) {
    323 	case SIOCINITIFADDR:
    324 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
    325 		break;
    326 	case SIOCSIFMTU:
    327 		if (ifr != NULL && ifr->ifr_mtu < 576) {
    328 			error = EINVAL;
    329 			break;
    330 		}
    331 		/* FALLTHROUGH */
    332 	case SIOCGIFMTU:
    333 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    334 			error = 0;
    335 		break;
    336 	case SIOCSIFFLAGS:
    337 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    338 			break;
    339 		if (ifp->if_flags & IFF_UP)
    340 			ifp->if_flags |= IFF_RUNNING;
    341 		break;
    342 	default:
    343 		error = ifioctl_common(ifp, cmd, data);
    344 		break;
    345 	}
    346 	splx(s);
    347 	return error;
    348 }
    349 
    350 /*
    351  * MPLS Label Switch Engine
    352  */
    353 static int
    354 mpls_lse(struct mbuf *m)
    355 {
    356 	struct sockaddr_mpls dst;
    357 	union mpls_shim tshim, *htag;
    358 	struct rtentry *rt = NULL;
    359 	int error = ENOBUFS;
    360 	uint psize = sizeof(struct sockaddr_mpls);
    361 	bool push_back_alert = false;
    362 
    363 	/* If we're not accepting MPLS frames, leave now. */
    364 	if (!mpls_frame_accept) {
    365 		error = EINVAL;
    366 		goto done;
    367 	}
    368 
    369 	if (m->m_len < sizeof(union mpls_shim) &&
    370 	    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL)
    371 		goto done;
    372 
    373 	dst.smpls_len = sizeof(struct sockaddr_mpls);
    374 	dst.smpls_family = AF_MPLS;
    375 	dst.smpls_addr.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
    376 
    377 	error = EINVAL;
    378 
    379 	/* TTL decrement */
    380 	if ((m = mpls_ttl_dec(m)) == NULL)
    381 		goto done;
    382 
    383 	/* RFC 4182 */
    384 	if (mpls_rfc4182 != 0) {
    385 		while ((dst.smpls_addr.shim.label == MPLS_LABEL_IPV4NULL ||
    386 		    dst.smpls_addr.shim.label == MPLS_LABEL_IPV6NULL) &&
    387 		    __predict_false(dst.smpls_addr.shim.bos == 0))
    388 			TRIM_LABEL;
    389 	}
    390 
    391 	/* RFC 3032 Section 2.1 Page 4 */
    392 	if (__predict_false(dst.smpls_addr.shim.label == MPLS_LABEL_RTALERT) &&
    393 	    dst.smpls_addr.shim.bos == 0) {
    394 		TRIM_LABEL;
    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 =
    432 	    dst.smpls_addr.shim.bos =
    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 = mtod(m, union mpls_shim *);
    546 		ms->s_addr = ntohl(ms->s_addr);
    547 
    548 		/* and get rid of it */
    549 		m_adj(m, sizeof(union mpls_shim));
    550 
    551 		/* get ip header */
    552 		if (m->m_len < sizeof(struct ip) &&
    553 		    (m = m_pullup(m, sizeof(struct ip))) == NULL) {
    554 			*error = ENOBUFS;
    555 			return NULL;
    556 		}
    557 
    558 		iph = mtod(m, struct ip *);
    559 		iphlen = iph->ip_hl << 2;
    560 
    561 		/* get it all */
    562 		if (m->m_len < iphlen) {
    563 			if ((m = m_pullup(m, iphlen)) == NULL) {
    564 				*error = ENOBUFS;
    565 				return NULL;
    566 			}
    567 			iph = mtod(m, struct ip *);
    568 		}
    569 
    570 		/* check ipsum */
    571 		if (in_cksum(m, iphlen) != 0) {
    572 			m_freem(m);
    573 			*error = EINVAL;
    574 			return NULL;
    575 		}
    576 
    577 		/* set IP ttl from MPLS ttl */
    578 		if (mpls_mapttl_inet)
    579 			iph->ip_ttl = ms->shim.ttl;
    580 
    581 		/* set IP Precedence from MPLS Exp */
    582 		if (mpls_mapprec_inet) {
    583 			iph->ip_tos = (iph->ip_tos << 3) >> 3;
    584 			iph->ip_tos |= ms->shim.exp << 5;
    585 		}
    586 
    587 		/* reset ipsum because we modified TTL and TOS */
    588 		iph->ip_sum = 0;
    589 		iph->ip_sum = in_cksum(m, iphlen);
    590 	} else {
    591 		m_adj(m, sizeof(union mpls_shim));
    592 	}
    593 
    594 	/* Put it on IP queue */
    595 	if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
    596 		m_freem(m);
    597 		*error = ENOBUFS;
    598 		return NULL;
    599 	}
    600 
    601 	*error = 0;
    602 	return m;
    603 }
    604 
    605 /*
    606  * Prepend MPLS label
    607  */
    608 static struct mbuf *
    609 mpls_label_inet(struct mbuf *m, union mpls_shim *ms, uint offset)
    610 {
    611 	struct ip iphdr;
    612 
    613 	if (mpls_mapttl_inet || mpls_mapprec_inet) {
    614 		if ((m->m_len < sizeof(struct ip)) &&
    615 		    (m = m_pullup(m, offset + sizeof(struct ip))) == 0)
    616 			return NULL; /* XXX */
    617 		m_copydata(m, offset, sizeof(struct ip), &iphdr);
    618 
    619 		/* Map TTL */
    620 		if (mpls_mapttl_inet)
    621 			ms->shim.ttl = iphdr.ip_ttl;
    622 
    623 		/* Copy IP precedence to EXP */
    624 		if (mpls_mapprec_inet)
    625 			ms->shim.exp = ((u_int8_t)iphdr.ip_tos) >> 5;
    626 	}
    627 
    628 	if ((m = mpls_prepend_shim(m, ms)) == NULL)
    629 		return NULL;
    630 
    631 	return m;
    632 }
    633 #endif	/* INET */
    634 
    635 #ifdef INET6
    636 static struct mbuf *
    637 mpls_unlabel_inet6(struct mbuf *m, int *error)
    638 {
    639 	struct ip6_hdr *ip6hdr;
    640 	union mpls_shim ms;
    641 
    642 	/* TODO: mapclass */
    643 	if (mpls_mapttl_inet6) {
    644 		ms.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
    645 		m_adj(m, sizeof(union mpls_shim));
    646 
    647 		if (m->m_len < sizeof(struct ip6_hdr) &&
    648 		    (m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
    649 			*error = ENOBUFS;
    650 			return NULL;
    651 		}
    652 		ip6hdr = mtod(m, struct ip6_hdr *);
    653 
    654 		/* Because we just decremented this in mpls_lse */
    655 		ip6hdr->ip6_hlim = ms.shim.ttl + 1;
    656 	} else {
    657 		m_adj(m, sizeof(union mpls_shim));
    658 	}
    659 
    660 	/* Put it back on IPv6 queue. */
    661 	if (__predict_false(!pktq_enqueue(ip6_pktq, m, 0))) {
    662 		m_freem(m);
    663 		*error = ENOBUFS;
    664 		return NULL;
    665 	}
    666 
    667 	*error = 0;
    668 	return m;
    669 }
    670 
    671 static struct mbuf *
    672 mpls_label_inet6(struct mbuf *m, union mpls_shim *ms, uint offset)
    673 {
    674 	struct ip6_hdr ip6h;
    675 
    676 	if (mpls_mapttl_inet6 || mpls_mapclass_inet6) {
    677 		if (m->m_len < sizeof(struct ip6_hdr) &&
    678 		    (m = m_pullup(m, offset + sizeof(struct ip6_hdr))) == 0)
    679 			return NULL;
    680 		m_copydata(m, offset, sizeof(struct ip6_hdr), &ip6h);
    681 
    682 		if (mpls_mapttl_inet6)
    683 			ms->shim.ttl = ip6h.ip6_hlim;
    684 
    685 		if (mpls_mapclass_inet6)
    686 			ms->shim.exp = ip6h.ip6_vfc << 1 >> 5;
    687 	}
    688 
    689 	if ((m = mpls_prepend_shim(m, ms)) == NULL)
    690 		return NULL;
    691 
    692 	return m;
    693 }
    694 #endif	/* INET6 */
    695 
    696 static struct mbuf *
    697 mpls_prepend_shim(struct mbuf *m, union mpls_shim *ms)
    698 {
    699 	union mpls_shim *shim;
    700 
    701 	M_PREPEND(m, sizeof(*ms), M_DONTWAIT);
    702 	if (m == NULL)
    703 		return NULL;
    704 
    705 	if (m->m_len < sizeof(union mpls_shim) &&
    706 	    (m = m_pullup(m, sizeof(union mpls_shim))) == 0)
    707 		return NULL;
    708 
    709 	shim = mtod(m, union mpls_shim *);
    710 
    711 	memcpy(shim, ms, sizeof(*shim));
    712 	shim->s_addr = htonl(shim->s_addr);
    713 
    714 	return m;
    715 }
    716 
    717 /*
    718  * Module infrastructure
    719  */
    720 #include "if_module.h"
    721 
    722 IF_MODULE(MODULE_CLASS_DRIVER, mpls, "")
    723