Home | History | Annotate | Line # | Download | only in net
if_mpls.c revision 1.3.4.2
      1  1.3.4.2  yamt /*	$NetBSD: if_mpls.c,v 1.3.4.2 2010/08/11 22:54:54 yamt Exp $ */
      2  1.3.4.2  yamt 
      3  1.3.4.2  yamt /*
      4  1.3.4.2  yamt  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.3.4.2  yamt  * All rights reserved.
      6  1.3.4.2  yamt  *
      7  1.3.4.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3.4.2  yamt  * by Mihai Chelaru <kefren (at) NetBSD.org>
      9  1.3.4.2  yamt  *
     10  1.3.4.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.3.4.2  yamt  * modification, are permitted provided that the following conditions
     12  1.3.4.2  yamt  * are met:
     13  1.3.4.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.3.4.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.3.4.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3.4.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.3.4.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.3.4.2  yamt  *
     19  1.3.4.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.3.4.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.3.4.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.3.4.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.3.4.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.3.4.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.3.4.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.3.4.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.3.4.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.3.4.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.3.4.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.3.4.2  yamt  */
     31  1.3.4.2  yamt 
     32  1.3.4.2  yamt #include <sys/cdefs.h>
     33  1.3.4.2  yamt __KERNEL_RCSID(0, "$NetBSD: if_mpls.c,v 1.3.4.2 2010/08/11 22:54:54 yamt Exp $");
     34  1.3.4.2  yamt 
     35  1.3.4.2  yamt #include "opt_inet.h"
     36  1.3.4.2  yamt #include "opt_mpls.h"
     37  1.3.4.2  yamt 
     38  1.3.4.2  yamt #include <sys/param.h>
     39  1.3.4.2  yamt 
     40  1.3.4.2  yamt #include <sys/errno.h>
     41  1.3.4.2  yamt #include <sys/malloc.h>
     42  1.3.4.2  yamt #include <sys/mbuf.h>
     43  1.3.4.2  yamt #include <sys/sysctl.h>
     44  1.3.4.2  yamt 
     45  1.3.4.2  yamt #include <net/bpf.h>
     46  1.3.4.2  yamt #include <net/if.h>
     47  1.3.4.2  yamt #include <net/if_types.h>
     48  1.3.4.2  yamt #include <net/netisr.h>
     49  1.3.4.2  yamt #include <net/route.h>
     50  1.3.4.2  yamt 
     51  1.3.4.2  yamt #ifdef INET
     52  1.3.4.2  yamt #include <netinet/in.h>
     53  1.3.4.2  yamt #include <netinet/in_systm.h>
     54  1.3.4.2  yamt #include <netinet/in_var.h>
     55  1.3.4.2  yamt #include <netinet/ip.h>
     56  1.3.4.2  yamt #endif
     57  1.3.4.2  yamt 
     58  1.3.4.2  yamt #ifdef INET6
     59  1.3.4.2  yamt #include <netinet/ip6.h>
     60  1.3.4.2  yamt #include <netinet6/in6_var.h>
     61  1.3.4.2  yamt #include <netinet6/ip6_var.h>
     62  1.3.4.2  yamt #endif
     63  1.3.4.2  yamt 
     64  1.3.4.2  yamt #include <netmpls/mpls.h>
     65  1.3.4.2  yamt #include <netmpls/mpls_var.h>
     66  1.3.4.2  yamt 
     67  1.3.4.2  yamt #include "if_mpls.h"
     68  1.3.4.2  yamt 
     69  1.3.4.2  yamt void ifmplsattach(int);
     70  1.3.4.2  yamt 
     71  1.3.4.2  yamt static int mpls_clone_create(struct if_clone *, int);
     72  1.3.4.2  yamt static int mpls_clone_destroy(struct ifnet *);
     73  1.3.4.2  yamt 
     74  1.3.4.2  yamt static struct if_clone mpls_if_cloner =
     75  1.3.4.2  yamt 	IF_CLONE_INITIALIZER("mpls", mpls_clone_create, mpls_clone_destroy);
     76  1.3.4.2  yamt 
     77  1.3.4.2  yamt 
     78  1.3.4.2  yamt static void mpls_input(struct ifnet *, struct mbuf *);
     79  1.3.4.2  yamt static int mpls_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
     80  1.3.4.2  yamt 	struct rtentry *);
     81  1.3.4.2  yamt static int mpls_ioctl(struct ifnet *, u_long, void *);
     82  1.3.4.2  yamt static int mpls_send_frame(struct mbuf *, struct ifnet *, struct rtentry *);
     83  1.3.4.2  yamt static int mpls_lse(struct mbuf *);
     84  1.3.4.2  yamt 
     85  1.3.4.2  yamt #ifdef INET
     86  1.3.4.2  yamt static int mpls_unlabel_inet(struct mbuf *);
     87  1.3.4.2  yamt static struct mbuf *mpls_label_inet(struct mbuf *, union mpls_shim *);
     88  1.3.4.2  yamt #endif
     89  1.3.4.2  yamt 
     90  1.3.4.2  yamt #ifdef INET6
     91  1.3.4.2  yamt static int mpls_unlabel_inet6(struct mbuf *);
     92  1.3.4.2  yamt static struct mbuf *mpls_label_inet6(struct mbuf *, union mpls_shim *);
     93  1.3.4.2  yamt #endif
     94  1.3.4.2  yamt 
     95  1.3.4.2  yamt static struct mbuf *mpls_prepend_shim(struct mbuf *, union mpls_shim *);
     96  1.3.4.2  yamt 
     97  1.3.4.2  yamt extern int mpls_defttl, mpls_mapttl_inet, mpls_mapttl_inet6, mpls_icmp_respond,
     98  1.3.4.2  yamt 	mpls_forwarding, mpls_accept, mpls_mapprec_inet, mpls_mapclass_inet6;
     99  1.3.4.2  yamt 
    100  1.3.4.2  yamt /* ARGSUSED */
    101  1.3.4.2  yamt void
    102  1.3.4.2  yamt ifmplsattach(int count)
    103  1.3.4.2  yamt {
    104  1.3.4.2  yamt 	if_clone_attach(&mpls_if_cloner);
    105  1.3.4.2  yamt }
    106  1.3.4.2  yamt 
    107  1.3.4.2  yamt static int
    108  1.3.4.2  yamt mpls_clone_create(struct if_clone *ifc, int unit)
    109  1.3.4.2  yamt {
    110  1.3.4.2  yamt 	struct mpls_softc *sc;
    111  1.3.4.2  yamt 
    112  1.3.4.2  yamt 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
    113  1.3.4.2  yamt 
    114  1.3.4.2  yamt 	if_initname(&sc->sc_if, ifc->ifc_name, unit);
    115  1.3.4.2  yamt 	sc->sc_if.if_softc = sc;
    116  1.3.4.2  yamt 	sc->sc_if.if_type = IFT_MPLS;
    117  1.3.4.2  yamt 	sc->sc_if.if_addrlen = 0;
    118  1.3.4.2  yamt 	sc->sc_if.if_hdrlen = sizeof(union mpls_shim);
    119  1.3.4.2  yamt 	sc->sc_if.if_dlt = DLT_NULL;
    120  1.3.4.2  yamt 	sc->sc_if.if_mtu = 1500;
    121  1.3.4.2  yamt 	sc->sc_if.if_flags = 0;
    122  1.3.4.2  yamt 	sc->sc_if.if_input = mpls_input;
    123  1.3.4.2  yamt 	sc->sc_if.if_output = mpls_output;
    124  1.3.4.2  yamt 	sc->sc_if.if_ioctl = mpls_ioctl;
    125  1.3.4.2  yamt 
    126  1.3.4.2  yamt 	if_attach(&sc->sc_if);
    127  1.3.4.2  yamt 	if_alloc_sadl(&sc->sc_if);
    128  1.3.4.2  yamt 	bpf_attach(&sc->sc_if, DLT_NULL, sizeof(uint32_t));
    129  1.3.4.2  yamt 	return 0;
    130  1.3.4.2  yamt }
    131  1.3.4.2  yamt 
    132  1.3.4.2  yamt static int
    133  1.3.4.2  yamt mpls_clone_destroy(struct ifnet *ifp)
    134  1.3.4.2  yamt {
    135  1.3.4.2  yamt 	int s;
    136  1.3.4.2  yamt 
    137  1.3.4.2  yamt 	bpf_detach(ifp);
    138  1.3.4.2  yamt 
    139  1.3.4.2  yamt 	s = splnet();
    140  1.3.4.2  yamt 	if_detach(ifp);
    141  1.3.4.2  yamt 	splx(s);
    142  1.3.4.2  yamt 
    143  1.3.4.2  yamt 	free(ifp->if_softc, M_DEVBUF);
    144  1.3.4.2  yamt 	return 0;
    145  1.3.4.2  yamt }
    146  1.3.4.2  yamt 
    147  1.3.4.2  yamt static void
    148  1.3.4.2  yamt mpls_input(struct ifnet *ifp, struct mbuf *m)
    149  1.3.4.2  yamt {
    150  1.3.4.2  yamt #if 0
    151  1.3.4.2  yamt 	/*
    152  1.3.4.2  yamt 	 * TODO - kefren
    153  1.3.4.2  yamt 	 * I'd love to unshim the packet, guess family
    154  1.3.4.2  yamt 	 * and pass it to bpf
    155  1.3.4.2  yamt 	 */
    156  1.3.4.2  yamt 	bpf_mtap_af(ifp, AF_MPLS, m);
    157  1.3.4.2  yamt #endif
    158  1.3.4.2  yamt 
    159  1.3.4.2  yamt 	mpls_lse(m);
    160  1.3.4.2  yamt }
    161  1.3.4.2  yamt 
    162  1.3.4.2  yamt void
    163  1.3.4.2  yamt mplsintr(void)
    164  1.3.4.2  yamt {
    165  1.3.4.2  yamt 	struct mbuf *m;
    166  1.3.4.2  yamt 	int s;
    167  1.3.4.2  yamt 
    168  1.3.4.2  yamt 	while (!IF_IS_EMPTY(&mplsintrq)) {
    169  1.3.4.2  yamt 		s = splnet();
    170  1.3.4.2  yamt 		IF_DEQUEUE(&mplsintrq, m);
    171  1.3.4.2  yamt 		splx(s);
    172  1.3.4.2  yamt 
    173  1.3.4.2  yamt 		if (!m)
    174  1.3.4.2  yamt 			return;
    175  1.3.4.2  yamt 
    176  1.3.4.2  yamt 		if (((m->m_flags & M_PKTHDR) == 0) ||
    177  1.3.4.2  yamt 		    (m->m_pkthdr.rcvif == 0))
    178  1.3.4.2  yamt 			panic("mplsintr(): no pkthdr or rcvif");
    179  1.3.4.2  yamt 
    180  1.3.4.2  yamt #ifdef MBUFTRACE
    181  1.3.4.2  yamt 		m_claimm(m, &mpls_owner);
    182  1.3.4.2  yamt #endif
    183  1.3.4.2  yamt 		mpls_input(m->m_pkthdr.rcvif, m);
    184  1.3.4.2  yamt 	}
    185  1.3.4.2  yamt }
    186  1.3.4.2  yamt 
    187  1.3.4.2  yamt /*
    188  1.3.4.2  yamt  * prepend shim and deliver
    189  1.3.4.2  yamt  */
    190  1.3.4.2  yamt static int
    191  1.3.4.2  yamt mpls_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, struct rtentry *rt)
    192  1.3.4.2  yamt {
    193  1.3.4.2  yamt 	union mpls_shim mh;
    194  1.3.4.2  yamt 	struct rtentry *rt1;
    195  1.3.4.2  yamt 	int err;
    196  1.3.4.2  yamt 
    197  1.3.4.2  yamt 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
    198  1.3.4.2  yamt 		m_freem(m);
    199  1.3.4.2  yamt 		return ENETDOWN;
    200  1.3.4.2  yamt 	}
    201  1.3.4.2  yamt 
    202  1.3.4.2  yamt 	if (rt_gettag(rt) == NULL || rt_gettag(rt)->sa_family != AF_MPLS) {
    203  1.3.4.2  yamt 		m_freem(m);
    204  1.3.4.2  yamt 		return EINVAL;
    205  1.3.4.2  yamt 	}
    206  1.3.4.2  yamt 
    207  1.3.4.2  yamt 	bpf_mtap_af(ifp, dst->sa_family, m);
    208  1.3.4.2  yamt 
    209  1.3.4.2  yamt 	mh.s_addr=MPLS_GETSADDR(rt);
    210  1.3.4.2  yamt 	mh.shim.bos=1;
    211  1.3.4.2  yamt 	mh.shim.exp=0;
    212  1.3.4.2  yamt 	mh.shim.ttl=mpls_defttl;
    213  1.3.4.2  yamt 
    214  1.3.4.2  yamt 	switch(dst->sa_family) {
    215  1.3.4.2  yamt #ifdef INET
    216  1.3.4.2  yamt 	case AF_INET:
    217  1.3.4.2  yamt 		m = mpls_label_inet(m, &mh);
    218  1.3.4.2  yamt 		break;
    219  1.3.4.2  yamt #endif
    220  1.3.4.2  yamt #ifdef INET6
    221  1.3.4.2  yamt 	case AF_INET6:
    222  1.3.4.2  yamt 		m = mpls_label_inet6(m, &mh);
    223  1.3.4.2  yamt 		break;
    224  1.3.4.2  yamt #endif
    225  1.3.4.2  yamt 	default:
    226  1.3.4.2  yamt 		m = mpls_prepend_shim(m, &mh);
    227  1.3.4.2  yamt 		break;
    228  1.3.4.2  yamt 	}
    229  1.3.4.2  yamt 
    230  1.3.4.2  yamt 	if (m == NULL) {
    231  1.3.4.2  yamt 		IF_DROP(&ifp->if_snd);
    232  1.3.4.2  yamt 		ifp->if_oerrors++;
    233  1.3.4.2  yamt 		return ENOBUFS;
    234  1.3.4.2  yamt 	}
    235  1.3.4.2  yamt 
    236  1.3.4.2  yamt 	ifp->if_opackets++;
    237  1.3.4.2  yamt 	ifp->if_obytes += m->m_pkthdr.len;
    238  1.3.4.2  yamt 
    239  1.3.4.2  yamt 	if ((rt1=rtalloc1(rt->rt_gateway, 1)) == NULL) {
    240  1.3.4.2  yamt 		m_freem(m);
    241  1.3.4.2  yamt 		return EHOSTUNREACH;
    242  1.3.4.2  yamt 	}
    243  1.3.4.2  yamt 
    244  1.3.4.2  yamt 	err = mpls_send_frame(m, rt1->rt_ifp, rt);
    245  1.3.4.2  yamt 	RTFREE(rt1);
    246  1.3.4.2  yamt 	return err;
    247  1.3.4.2  yamt }
    248  1.3.4.2  yamt 
    249  1.3.4.2  yamt static int
    250  1.3.4.2  yamt mpls_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    251  1.3.4.2  yamt {
    252  1.3.4.2  yamt 	int error = 0, s = splnet();
    253  1.3.4.2  yamt 	struct ifreq *ifr = data;
    254  1.3.4.2  yamt 
    255  1.3.4.2  yamt 	switch(cmd) {
    256  1.3.4.2  yamt 	case SIOCINITIFADDR:
    257  1.3.4.2  yamt 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
    258  1.3.4.2  yamt 		break;
    259  1.3.4.2  yamt 	case SIOCSIFMTU:
    260  1.3.4.2  yamt 		if (ifr != NULL && ifr->ifr_mtu < 576) {
    261  1.3.4.2  yamt 			error = EINVAL;
    262  1.3.4.2  yamt 			break;
    263  1.3.4.2  yamt 		}
    264  1.3.4.2  yamt 		/* FALLTHROUGH */
    265  1.3.4.2  yamt 	case SIOCGIFMTU:
    266  1.3.4.2  yamt 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    267  1.3.4.2  yamt 			error = 0;
    268  1.3.4.2  yamt 		break;
    269  1.3.4.2  yamt 	case SIOCSIFFLAGS:
    270  1.3.4.2  yamt 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    271  1.3.4.2  yamt 			break;
    272  1.3.4.2  yamt 		if (ifp->if_flags & IFF_UP)
    273  1.3.4.2  yamt 			ifp->if_flags |= IFF_RUNNING;
    274  1.3.4.2  yamt 		break;
    275  1.3.4.2  yamt 	default:
    276  1.3.4.2  yamt 		error = ifioctl_common(ifp, cmd, data);
    277  1.3.4.2  yamt 		break;
    278  1.3.4.2  yamt 	}
    279  1.3.4.2  yamt 	splx(s);
    280  1.3.4.2  yamt 	return error;
    281  1.3.4.2  yamt }
    282  1.3.4.2  yamt 
    283  1.3.4.2  yamt /*
    284  1.3.4.2  yamt  * MPLS Label Switch Engine
    285  1.3.4.2  yamt  */
    286  1.3.4.2  yamt static int
    287  1.3.4.2  yamt mpls_lse(struct mbuf *m)
    288  1.3.4.2  yamt {
    289  1.3.4.2  yamt 	struct sockaddr_mpls dst;
    290  1.3.4.2  yamt 	union mpls_shim tshim, *htag;
    291  1.3.4.2  yamt 	struct rtentry *rt = NULL;
    292  1.3.4.2  yamt 	int error = ENOBUFS;
    293  1.3.4.2  yamt 
    294  1.3.4.2  yamt 	if (m->m_len < sizeof(union mpls_shim) &&
    295  1.3.4.2  yamt 	    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL)
    296  1.3.4.2  yamt 		goto done;
    297  1.3.4.2  yamt 
    298  1.3.4.2  yamt 	dst.smpls_len = sizeof(struct sockaddr_mpls);
    299  1.3.4.2  yamt 	dst.smpls_family = AF_MPLS;
    300  1.3.4.2  yamt 	dst.smpls_addr.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
    301  1.3.4.2  yamt 
    302  1.3.4.2  yamt 	/* Check if we're accepting MPLS Frames */
    303  1.3.4.2  yamt 	error = EINVAL;
    304  1.3.4.2  yamt 	if (!mpls_accept)
    305  1.3.4.2  yamt 		goto done;
    306  1.3.4.2  yamt 
    307  1.3.4.2  yamt 	/* TTL decrement */
    308  1.3.4.2  yamt 	if ((m = mpls_ttl_dec(m)) == NULL)
    309  1.3.4.2  yamt 		goto done;
    310  1.3.4.2  yamt 
    311  1.3.4.2  yamt 	if (dst.smpls_addr.shim.label <= MPLS_LABEL_RESMAX) {
    312  1.3.4.2  yamt 		/* Don't swap reserved labels */
    313  1.3.4.2  yamt 		switch (dst.smpls_addr.shim.label) {
    314  1.3.4.2  yamt #ifdef INET
    315  1.3.4.2  yamt 		case MPLS_LABEL_IPV4NULL:
    316  1.3.4.2  yamt 			/* Pop shim and push mbuf to IP stack */
    317  1.3.4.2  yamt 			if (dst.smpls_addr.shim.bos)
    318  1.3.4.2  yamt 				error = mpls_unlabel_inet(m);
    319  1.3.4.2  yamt 			break;
    320  1.3.4.2  yamt #endif
    321  1.3.4.2  yamt #ifdef INET6
    322  1.3.4.2  yamt 		case MPLS_LABEL_IPV6NULL:
    323  1.3.4.2  yamt 			/* Pop shim and push mbuf to IPv6 stack */
    324  1.3.4.2  yamt 			if (dst.smpls_addr.shim.bos)
    325  1.3.4.2  yamt 				error = mpls_unlabel_inet6(m);
    326  1.3.4.2  yamt 			break;
    327  1.3.4.2  yamt #endif
    328  1.3.4.2  yamt 		case MPLS_LABEL_RTALERT:	/* Yeah, I'm all alerted */
    329  1.3.4.2  yamt 		case MPLS_LABEL_IMPLNULL:	/* This is logical only */
    330  1.3.4.2  yamt 		default:			/* Rest are not allowed */
    331  1.3.4.2  yamt 			break;
    332  1.3.4.2  yamt 		}
    333  1.3.4.2  yamt 		goto done;
    334  1.3.4.2  yamt 	}
    335  1.3.4.2  yamt 
    336  1.3.4.2  yamt 	/* Check if we should do MPLS forwarding */
    337  1.3.4.2  yamt 	error = EHOSTUNREACH;
    338  1.3.4.2  yamt 	if (!mpls_forwarding)
    339  1.3.4.2  yamt 		goto done;
    340  1.3.4.2  yamt 
    341  1.3.4.2  yamt 	/* Get a route to dst */
    342  1.3.4.2  yamt 	dst.smpls_addr.shim.ttl =
    343  1.3.4.2  yamt 	    dst.smpls_addr.shim.bos =
    344  1.3.4.2  yamt 	    dst.smpls_addr.shim.exp = 0;
    345  1.3.4.2  yamt 	dst.smpls_addr.s_addr = htonl(dst.smpls_addr.s_addr);
    346  1.3.4.2  yamt 	if ((rt = rtalloc1((const struct sockaddr*)&dst, 1)) == NULL)
    347  1.3.4.2  yamt 		goto done;
    348  1.3.4.2  yamt 
    349  1.3.4.2  yamt 	/* MPLS packet with no MPLS tagged route ? */
    350  1.3.4.2  yamt 	if ((rt->rt_flags & RTF_GATEWAY) == 0 ||
    351  1.3.4.2  yamt 	     rt_gettag(rt) == NULL ||
    352  1.3.4.2  yamt 	     rt_gettag(rt)->sa_family != AF_MPLS)
    353  1.3.4.2  yamt 		goto done;
    354  1.3.4.2  yamt 
    355  1.3.4.2  yamt 	tshim.s_addr = MPLS_GETSADDR(rt);
    356  1.3.4.2  yamt 
    357  1.3.4.2  yamt 	/* Swap labels */
    358  1.3.4.2  yamt 	if ((m->m_len < sizeof(union mpls_shim)) &&
    359  1.3.4.2  yamt 	    (m = m_pullup(m, sizeof(union mpls_shim))) == 0) {
    360  1.3.4.2  yamt 		error = ENOBUFS;
    361  1.3.4.2  yamt 		goto done;
    362  1.3.4.2  yamt 	}
    363  1.3.4.2  yamt 
    364  1.3.4.2  yamt 	/* Replace only the label */
    365  1.3.4.2  yamt 	htag = mtod(m, union mpls_shim *);
    366  1.3.4.2  yamt 	htag->s_addr = ntohl(htag->s_addr);
    367  1.3.4.2  yamt 	htag->shim.label = tshim.shim.label;
    368  1.3.4.2  yamt 	htag->s_addr = htonl(htag->s_addr);
    369  1.3.4.2  yamt 
    370  1.3.4.2  yamt 	error = mpls_send_frame(m, rt->rt_ifp, rt);
    371  1.3.4.2  yamt 
    372  1.3.4.2  yamt done:
    373  1.3.4.2  yamt 	if (error != 0 && m != NULL)
    374  1.3.4.2  yamt 		m_freem(m);
    375  1.3.4.2  yamt 	if (rt != NULL)
    376  1.3.4.2  yamt 		RTFREE(rt);
    377  1.3.4.2  yamt 
    378  1.3.4.2  yamt 	return error;
    379  1.3.4.2  yamt }
    380  1.3.4.2  yamt 
    381  1.3.4.2  yamt static int
    382  1.3.4.2  yamt mpls_send_frame(struct mbuf *m, struct ifnet *ifp, struct rtentry *rt)
    383  1.3.4.2  yamt {
    384  1.3.4.2  yamt 	union mpls_shim msh;
    385  1.3.4.2  yamt 
    386  1.3.4.2  yamt 	if ((rt->rt_flags & RTF_GATEWAY) == 0)
    387  1.3.4.2  yamt 		return EHOSTUNREACH;
    388  1.3.4.2  yamt 
    389  1.3.4.2  yamt 	rt->rt_use++;
    390  1.3.4.2  yamt 
    391  1.3.4.2  yamt 	msh.s_addr = MPLS_GETSADDR(rt);
    392  1.3.4.2  yamt 	if (msh.shim.label == MPLS_LABEL_IMPLNULL) {
    393  1.3.4.2  yamt 		m_adj(m, sizeof(union mpls_shim));
    394  1.3.4.2  yamt 		m->m_pkthdr.csum_flags = 0;
    395  1.3.4.2  yamt 	}
    396  1.3.4.2  yamt 
    397  1.3.4.2  yamt 	switch(ifp->if_type) {
    398  1.3.4.2  yamt 	/* only these two are supported for now */
    399  1.3.4.2  yamt 	case IFT_ETHER:
    400  1.3.4.2  yamt 	case IFT_TUNNEL:
    401  1.3.4.2  yamt 		return (*ifp->if_output)(ifp, m, rt->rt_gateway, rt);
    402  1.3.4.2  yamt 	case IFT_LOOP:
    403  1.3.4.2  yamt 		break;
    404  1.3.4.2  yamt 	default:
    405  1.3.4.2  yamt 		return ENETUNREACH;
    406  1.3.4.2  yamt 	}
    407  1.3.4.2  yamt 	return 0;
    408  1.3.4.2  yamt }
    409  1.3.4.2  yamt 
    410  1.3.4.2  yamt 
    411  1.3.4.2  yamt 
    412  1.3.4.2  yamt #ifdef INET
    413  1.3.4.2  yamt static int
    414  1.3.4.2  yamt mpls_unlabel_inet(struct mbuf *m)
    415  1.3.4.2  yamt {
    416  1.3.4.2  yamt 	int s, iphlen;
    417  1.3.4.2  yamt 	struct ip *iph;
    418  1.3.4.2  yamt 	union mpls_shim *ms;
    419  1.3.4.2  yamt 	struct ifqueue *inq;
    420  1.3.4.2  yamt 
    421  1.3.4.2  yamt 	if (mpls_mapttl_inet || mpls_mapprec_inet) {
    422  1.3.4.2  yamt 
    423  1.3.4.2  yamt 		/* get shim info */
    424  1.3.4.2  yamt 		ms = mtod(m, union mpls_shim *);
    425  1.3.4.2  yamt 		ms->s_addr = ntohl(ms->s_addr);
    426  1.3.4.2  yamt 
    427  1.3.4.2  yamt 		/* and get rid of it */
    428  1.3.4.2  yamt 		m_adj(m, sizeof(union mpls_shim));
    429  1.3.4.2  yamt 
    430  1.3.4.2  yamt 		/* get ip header */
    431  1.3.4.2  yamt 		if (m->m_len < sizeof (struct ip) &&
    432  1.3.4.2  yamt 		    (m = m_pullup(m, sizeof(struct ip))) == NULL)
    433  1.3.4.2  yamt 			return ENOBUFS;
    434  1.3.4.2  yamt 		iph = mtod(m, struct ip *);
    435  1.3.4.2  yamt 		iphlen = iph->ip_hl << 2;
    436  1.3.4.2  yamt 
    437  1.3.4.2  yamt 		/* get it all */
    438  1.3.4.2  yamt 		if (m->m_len < iphlen) {
    439  1.3.4.2  yamt 			if ((m = m_pullup(m, iphlen)) == NULL)
    440  1.3.4.2  yamt 				return ENOBUFS;
    441  1.3.4.2  yamt 			iph = mtod(m, struct ip *);
    442  1.3.4.2  yamt 		}
    443  1.3.4.2  yamt 
    444  1.3.4.2  yamt 		/* check ipsum */
    445  1.3.4.2  yamt 		if (in_cksum(m, iphlen) != 0) {
    446  1.3.4.2  yamt 			m_freem(m);
    447  1.3.4.2  yamt 			return EINVAL;
    448  1.3.4.2  yamt 		}
    449  1.3.4.2  yamt 
    450  1.3.4.2  yamt 		/* set IP ttl from MPLS ttl */
    451  1.3.4.2  yamt 		if (mpls_mapttl_inet)
    452  1.3.4.2  yamt 			iph->ip_ttl = ms->shim.ttl;
    453  1.3.4.2  yamt 
    454  1.3.4.2  yamt 		/* set IP Precedence from MPLS Exp */
    455  1.3.4.2  yamt 		if (mpls_mapprec_inet) {
    456  1.3.4.2  yamt 			iph->ip_tos = (iph->ip_tos << 3) >> 3;
    457  1.3.4.2  yamt 			iph->ip_tos |= ms->shim.exp << 5;
    458  1.3.4.2  yamt 		}
    459  1.3.4.2  yamt 
    460  1.3.4.2  yamt 		/* reset ipsum because we modified TTL and TOS */
    461  1.3.4.2  yamt 		iph->ip_sum = 0;
    462  1.3.4.2  yamt 		iph->ip_sum = in_cksum(m, iphlen);
    463  1.3.4.2  yamt 	} else
    464  1.3.4.2  yamt 		m_adj(m, sizeof(union mpls_shim));
    465  1.3.4.2  yamt 
    466  1.3.4.2  yamt 	/* Put it on IP queue */
    467  1.3.4.2  yamt 	inq = &ipintrq;
    468  1.3.4.2  yamt 	s = splnet();
    469  1.3.4.2  yamt 	if (IF_QFULL(inq)) {
    470  1.3.4.2  yamt 		IF_DROP(inq);
    471  1.3.4.2  yamt 		splx(s);
    472  1.3.4.2  yamt 		m_freem(m);
    473  1.3.4.2  yamt 		return ENOBUFS;
    474  1.3.4.2  yamt 	}
    475  1.3.4.2  yamt 	IF_ENQUEUE(inq, m);
    476  1.3.4.2  yamt 	splx(s);
    477  1.3.4.2  yamt 	schednetisr(NETISR_IP);
    478  1.3.4.2  yamt 
    479  1.3.4.2  yamt 	return 0;
    480  1.3.4.2  yamt }
    481  1.3.4.2  yamt 
    482  1.3.4.2  yamt /*
    483  1.3.4.2  yamt  * Prepend MPLS label
    484  1.3.4.2  yamt  */
    485  1.3.4.2  yamt static struct mbuf *
    486  1.3.4.2  yamt mpls_label_inet(struct mbuf *m, union mpls_shim *ms)
    487  1.3.4.2  yamt {
    488  1.3.4.2  yamt 	struct ip *iphdr;
    489  1.3.4.2  yamt 
    490  1.3.4.2  yamt 	if (mpls_mapttl_inet || mpls_mapprec_inet) {
    491  1.3.4.2  yamt 		if ((m->m_len < sizeof(struct ip)) &&
    492  1.3.4.2  yamt 		    (m = m_pullup(m, sizeof(struct ip))) == 0)
    493  1.3.4.2  yamt 			return NULL;
    494  1.3.4.2  yamt 		iphdr = mtod(m, struct ip *);
    495  1.3.4.2  yamt 
    496  1.3.4.2  yamt 		/* Map TTL */
    497  1.3.4.2  yamt 		if (mpls_mapttl_inet)
    498  1.3.4.2  yamt 			ms->shim.ttl = iphdr->ip_ttl;
    499  1.3.4.2  yamt 
    500  1.3.4.2  yamt 		/* Copy IP precedence to EXP */
    501  1.3.4.2  yamt 		if (mpls_mapprec_inet)
    502  1.3.4.2  yamt 			ms->shim.exp = ((u_int8_t)iphdr->ip_tos) >> 5;
    503  1.3.4.2  yamt 	}
    504  1.3.4.2  yamt 
    505  1.3.4.2  yamt 	if ((m = mpls_prepend_shim(m, ms)) == NULL)
    506  1.3.4.2  yamt 		return NULL;
    507  1.3.4.2  yamt 
    508  1.3.4.2  yamt 	return m;
    509  1.3.4.2  yamt }
    510  1.3.4.2  yamt 
    511  1.3.4.2  yamt #endif	/* INET */
    512  1.3.4.2  yamt 
    513  1.3.4.2  yamt #ifdef INET6
    514  1.3.4.2  yamt 
    515  1.3.4.2  yamt static int
    516  1.3.4.2  yamt mpls_unlabel_inet6(struct mbuf *m)
    517  1.3.4.2  yamt {
    518  1.3.4.2  yamt 	struct ip6_hdr *ip6hdr;
    519  1.3.4.2  yamt 	union mpls_shim ms;
    520  1.3.4.2  yamt 	struct ifqueue *inq;
    521  1.3.4.2  yamt 	int s;
    522  1.3.4.2  yamt 
    523  1.3.4.2  yamt 	/* TODO: mapclass */
    524  1.3.4.2  yamt 	if (mpls_mapttl_inet6) {
    525  1.3.4.2  yamt 		ms.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
    526  1.3.4.2  yamt 		m_adj(m, sizeof(union mpls_shim));
    527  1.3.4.2  yamt 
    528  1.3.4.2  yamt 		if (m->m_len < sizeof (struct ip6_hdr) &&
    529  1.3.4.2  yamt 		    (m = m_pullup(m, sizeof(struct ip6_hdr))) == 0)
    530  1.3.4.2  yamt 			return ENOBUFS;
    531  1.3.4.2  yamt 		ip6hdr = mtod(m, struct ip6_hdr *);
    532  1.3.4.2  yamt 
    533  1.3.4.2  yamt 		/* Because we just decremented this in mpls_lse */
    534  1.3.4.2  yamt 		ip6hdr->ip6_hlim = ms.shim.ttl + 1;
    535  1.3.4.2  yamt 	} else
    536  1.3.4.2  yamt 		m_adj(m, sizeof(union mpls_shim));
    537  1.3.4.2  yamt 
    538  1.3.4.2  yamt 	/* Put it back on IPv6 stack */
    539  1.3.4.2  yamt 	schednetisr(NETISR_IPV6);
    540  1.3.4.2  yamt 	inq = &ip6intrq;
    541  1.3.4.2  yamt 	s = splnet();
    542  1.3.4.2  yamt 	if (IF_QFULL(inq)) {
    543  1.3.4.2  yamt 		IF_DROP(inq);
    544  1.3.4.2  yamt 		splx(s);
    545  1.3.4.2  yamt 		m_freem(m);
    546  1.3.4.2  yamt 		return ENOBUFS;
    547  1.3.4.2  yamt 	}
    548  1.3.4.2  yamt 
    549  1.3.4.2  yamt 	IF_ENQUEUE(inq, m);
    550  1.3.4.2  yamt 	splx(s);
    551  1.3.4.2  yamt 
    552  1.3.4.2  yamt 	return 0;
    553  1.3.4.2  yamt }
    554  1.3.4.2  yamt 
    555  1.3.4.2  yamt static struct mbuf *
    556  1.3.4.2  yamt mpls_label_inet6(struct mbuf *m, union mpls_shim *ms)
    557  1.3.4.2  yamt {
    558  1.3.4.2  yamt 	struct ip6_hdr *ip6h;
    559  1.3.4.2  yamt 
    560  1.3.4.2  yamt 	if (mpls_mapttl_inet6 || mpls_mapclass_inet6) {
    561  1.3.4.2  yamt 		if (m->m_len < sizeof(struct ip6_hdr) &&
    562  1.3.4.2  yamt 		    (m = m_pullup(m, sizeof(struct ip6_hdr))) == 0)
    563  1.3.4.2  yamt 			return NULL;
    564  1.3.4.2  yamt 		ip6h = mtod(m, struct ip6_hdr *);
    565  1.3.4.2  yamt 
    566  1.3.4.2  yamt 		if (mpls_mapttl_inet6)
    567  1.3.4.2  yamt 			ms->shim.ttl = ip6h->ip6_hlim;
    568  1.3.4.2  yamt 
    569  1.3.4.2  yamt 		if (mpls_mapclass_inet6)
    570  1.3.4.2  yamt 			ms->shim.exp = ip6h->ip6_vfc << 1 >> 5;
    571  1.3.4.2  yamt 	}
    572  1.3.4.2  yamt 
    573  1.3.4.2  yamt 	if ((m = mpls_prepend_shim(m, ms)) == NULL)
    574  1.3.4.2  yamt 		return NULL;
    575  1.3.4.2  yamt 
    576  1.3.4.2  yamt 	return m;
    577  1.3.4.2  yamt }
    578  1.3.4.2  yamt 
    579  1.3.4.2  yamt #endif	/* INET6 */
    580  1.3.4.2  yamt 
    581  1.3.4.2  yamt static struct mbuf *
    582  1.3.4.2  yamt mpls_prepend_shim(struct mbuf *m, union mpls_shim *ms)
    583  1.3.4.2  yamt {
    584  1.3.4.2  yamt 	union mpls_shim *shim;
    585  1.3.4.2  yamt 
    586  1.3.4.2  yamt 	M_PREPEND(m, sizeof(*ms), M_DONTWAIT);
    587  1.3.4.2  yamt 	if (m == NULL)
    588  1.3.4.2  yamt 		return NULL;
    589  1.3.4.2  yamt 
    590  1.3.4.2  yamt 	if (m->m_len < sizeof(union mpls_shim) &&
    591  1.3.4.2  yamt 	    (m = m_pullup(m, sizeof(union mpls_shim))) == 0)
    592  1.3.4.2  yamt 		return NULL;
    593  1.3.4.2  yamt 
    594  1.3.4.2  yamt 	shim = mtod(m, union mpls_shim *);
    595  1.3.4.2  yamt 
    596  1.3.4.2  yamt 	memcpy(shim, ms, sizeof(*shim));
    597  1.3.4.2  yamt 	shim->s_addr = htonl(shim->s_addr);
    598  1.3.4.2  yamt 
    599  1.3.4.2  yamt 	return m;
    600  1.3.4.2  yamt }
    601