6fb8880601
See the following descriptions for details. Proposed on tech-kern and tech-net Overview -------- We protect the routing table with a rwock and protect rtcaches with another rwlock. Each rtentry is protected from being freed or updated via reference counting and psref. Global rwlocks -------------- There are two rwlocks; one for the routing table (rt_lock) and the other for rtcaches (rtcache_lock). rtcache_lock covers all existing rtcaches; there may have room for optimizations (future work). The locking order is rtcache_lock first and rt_lock is next. rtentry references ------------------ References to an rtentry is managed with reference counting and psref. Either of the two mechanisms is used depending on where a rtentry is obtained. Reference counting is used when we obtain a rtentry from the routing table directly via rtalloc1 and rtrequest{,1} while psref is used when we obtain a rtentry from a rtcache via rtcache_* APIs. In both cases, a caller can sleep/block with holding an obtained rtentry. The reasons why we use two different mechanisms are (i) only using reference counting hurts the performance due to atomic instructions (rtcache case) (ii) ease of implementation; applying psref to APIs such rtaloc1 and rtrequest{,1} requires additional works (adding a local variable and an argument). We will finally migrate to use only psref but we can do it when we have a lockless routing table alternative. Reference counting for rtentry ------------------------------ rt_refcnt now doesn't count permanent references such as for rt_timers and rtcaches, instead it is used only for temporal references when obtaining a rtentry via rtalloc1 and rtrequest{,1}. We can do so because destroying a rtentry always involves removing references of rt_timers and rtcaches to the rtentry and we don't need to track such references. This also makes it easy to wait for readers to release references on deleting or updating a rtentry, i.e., we can simply wait until the reference counter is 0 or 1. (If there are permanent references the counter can be arbitrary.) rt_ref increments a reference counter of a rtentry and rt_unref decrements it. rt_ref is called inside APIs (rtalloc1 and rtrequest{,1} so users don't need to care about it while users must call rt_unref to an obtained rtentry after using it. rtfree is removed and we use rt_unref and rt_free instead. rt_unref now just decrements the counter of a given rtentry and rt_free just tries to destroy a given rtentry. See the next section for destructions of rtentries by rt_free. Destructions of rtentries ------------------------- We destroy a rtentry only when we call rtrequst{,1}(RTM_DELETE); the original implementation can destroy in any rtfree where it's the last reference. If we use reference counting or psref, it's easy to understand if the place that a rtentry is destroyed is fixed. rt_free waits for references to a given rtentry to be released before actually destroying the rtentry. rt_free uses a condition variable (cv_wait) (and psref_target_destroy for psref) to wait. Unfortunately rtrequst{,1}(RTM_DELETE) can be called in softint that we cannot use cv_wait. In that case, we have to defer the destruction to a workqueue. rtentry#rt_cv, rtentry#rt_psref and global variables (see rt_free_global) are added to conduct the procedure. Updates of rtentries -------------------- One difficulty to use refcnt/psref instead of rwlock for rtentry is updates of rtentries. We need an additional mechanism to prevent readers from seeing inconsistency of a rtentry being updated. We introduce RTF_UPDATING flag to rtentries that are updating. While the flag is set to a rtentry, users cannot acquire the rtentry. By doing so, we avoid users to see inconsistent rtentries. There are two options when a user tries to acquire a rtentry with the RTF_UPDATING flag; if a user runs in softint context the user fails to acquire a rtentry (NULL is returned). Otherwise a user waits until the update completes by waiting on cv. The procedure of a updater is simpler to destruction of a rtentry. Wait on cv (and psref) and after all readers left, proceed with the update. Global variables (see rt_update_global) are added to conduct the procedure. Currently we apply the mechanism to only RTM_CHANGE in rtsock.c. We would have to apply other codes. See "Known issues" section. psref for rtentry ----------------- When we obtain a rtentry from a rtcache via rtcache_* APIs, psref is used to reference to the rtentry. rtcache_ref acquires a reference to a rtentry with psref and rtcache_unref releases the reference after using it. rtcache_ref is called inside rtcache_* APIs and users don't need to take care of it while users must call rtcache_unref to release the reference. struct psref and int bound that is needed for psref is embedded into struct route. By doing so we don't need to add local variables and additional argument to APIs. However this adds another constraint to psref other than reference counting one's; holding a reference of an rtentry via a rtcache is allowed by just one caller at the same time. So we must not acquire a rtentry via a rtcache twice and avoid a recursive use of a rtcache. And also a rtcache must be arranged to be used by a LWP/softint at the same time somehow. For IP forwarding case, we have per-CPU rtcaches used in softint so the constraint is guaranteed. For a h rtcache of a PCB case, the constraint is guaranteed by the solock of each PCB. Any other cases (pf, ipf, stf and ipsec) are currently guaranteed by only the existence of the global locks (softnet_lock and/or KERNEL_LOCK). If we've found the cases that we cannot guarantee the constraint, we would need to introduce other rtcache APIs that use simple reference counting. psref of rtcache is created with IPL_SOFTNET and so rtcache shouldn't used at an IPL higher than IPL_SOFTNET. Note that rtcache_free is used to invalidate a given rtcache. We don't need another care by my change; just keep them as they are. Performance impact ------------------ When NET_MPSAFE is disabled the performance drop is 3% while when it's enabled the drop is increased to 11%. The difference comes from that currently we don't take any global locks and don't use psref if NET_MPSAFE is disabled. We can optimize the performance of the case of NET_MPSAFE on by reducing lookups of rtcache that uses psref; currently we do two lookups but we should be able to trim one of two. This is a future work. Known issues ------------ There are two known issues to be solved; one is that a caller of rtrequest(RTM_ADD) may change rtentry (see rtinit). We need to prevent new references during the update. Or we may be able to remove the code (perhaps, need more investigations). The other is rtredirect that updates a rtentry. We need to apply our update mechanism, however it's not easy because rtredirect is called in softint and we cannot apply our mechanism simply. One solution is to defer rtredirect to a workqueue but it requires some code restructuring.
698 lines
16 KiB
C
698 lines
16 KiB
C
/* $NetBSD: if_mpls.c,v 1.29 2016/12/12 03:55:57 ozaki-r Exp $ */
|
|
|
|
/*
|
|
* Copyright (c) 2010 The NetBSD Foundation, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
* by Mihai Chelaru <kefren@NetBSD.org>
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#include <sys/cdefs.h>
|
|
__KERNEL_RCSID(0, "$NetBSD: if_mpls.c,v 1.29 2016/12/12 03:55:57 ozaki-r Exp $");
|
|
|
|
#ifdef _KERNEL_OPT
|
|
#include "opt_inet.h"
|
|
#include "opt_mpls.h"
|
|
#endif
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/errno.h>
|
|
#include <sys/malloc.h>
|
|
#include <sys/mbuf.h>
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <net/bpf.h>
|
|
#include <net/if.h>
|
|
#include <net/if_types.h>
|
|
#include <net/netisr.h>
|
|
#include <net/route.h>
|
|
#include <sys/device.h>
|
|
#include <sys/module.h>
|
|
#include <sys/atomic.h>
|
|
|
|
#ifdef INET
|
|
#include <netinet/in.h>
|
|
#include <netinet/in_systm.h>
|
|
#include <netinet/in_var.h>
|
|
#include <netinet/ip.h>
|
|
#include <netinet/ip_var.h>
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
#include <netinet/ip6.h>
|
|
#include <netinet6/in6_var.h>
|
|
#include <netinet6/ip6_var.h>
|
|
#endif
|
|
|
|
#include <netmpls/mpls.h>
|
|
#include <netmpls/mpls_var.h>
|
|
|
|
#include "if_mpls.h"
|
|
|
|
#include "ioconf.h"
|
|
|
|
#define TRIM_LABEL do { \
|
|
m_adj(m, sizeof(union mpls_shim)); \
|
|
if (m->m_len < sizeof(union mpls_shim) && \
|
|
(m = m_pullup(m, sizeof(union mpls_shim))) == NULL) \
|
|
goto done; \
|
|
dst.smpls_addr.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr); \
|
|
} while (/* CONSTCOND */ 0)
|
|
|
|
|
|
static int mpls_clone_create(struct if_clone *, int);
|
|
static int mpls_clone_destroy(struct ifnet *);
|
|
|
|
static struct if_clone mpls_if_cloner =
|
|
IF_CLONE_INITIALIZER("mpls", mpls_clone_create, mpls_clone_destroy);
|
|
|
|
|
|
static void mpls_input(struct ifnet *, struct mbuf *);
|
|
static int mpls_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
|
|
const struct rtentry *);
|
|
static int mpls_ioctl(struct ifnet *, u_long, void *);
|
|
static int mpls_send_frame(struct mbuf *, struct ifnet *,
|
|
const struct rtentry *);
|
|
static int mpls_lse(struct mbuf *);
|
|
|
|
#ifdef INET
|
|
static int mpls_unlabel_inet(struct mbuf *);
|
|
static struct mbuf *mpls_label_inet(struct mbuf *, union mpls_shim *, uint);
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
static int mpls_unlabel_inet6(struct mbuf *);
|
|
static struct mbuf *mpls_label_inet6(struct mbuf *, union mpls_shim *, uint);
|
|
#endif
|
|
|
|
static struct mbuf *mpls_prepend_shim(struct mbuf *, union mpls_shim *);
|
|
|
|
extern int mpls_defttl, mpls_mapttl_inet, mpls_mapttl_inet6, mpls_icmp_respond,
|
|
mpls_forwarding, mpls_frame_accept, mpls_mapprec_inet, mpls_mapclass_inet6,
|
|
mpls_rfc4182;
|
|
|
|
static u_int mpls_count;
|
|
/* ARGSUSED */
|
|
void
|
|
mplsattach(int count)
|
|
{
|
|
/*
|
|
* Nothing to do here, initialization is handled by the
|
|
* module initialization code in mplsinit() below).
|
|
*/
|
|
}
|
|
|
|
static void
|
|
mplsinit(void)
|
|
{
|
|
if_clone_attach(&mpls_if_cloner);
|
|
}
|
|
|
|
static int
|
|
mplsdetach(void)
|
|
{
|
|
int error = 0;
|
|
|
|
if (mpls_count != 0)
|
|
error = EBUSY;
|
|
|
|
if (error == 0)
|
|
if_clone_detach(&mpls_if_cloner);
|
|
|
|
return error;
|
|
}
|
|
|
|
static int
|
|
mpls_clone_create(struct if_clone *ifc, int unit)
|
|
{
|
|
struct mpls_softc *sc;
|
|
|
|
atomic_inc_uint(&mpls_count);
|
|
sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
|
|
|
|
if_initname(&sc->sc_if, ifc->ifc_name, unit);
|
|
sc->sc_if.if_softc = sc;
|
|
sc->sc_if.if_type = IFT_MPLS;
|
|
sc->sc_if.if_addrlen = 0;
|
|
sc->sc_if.if_hdrlen = sizeof(union mpls_shim);
|
|
sc->sc_if.if_dlt = DLT_NULL;
|
|
sc->sc_if.if_mtu = 1500;
|
|
sc->sc_if.if_flags = 0;
|
|
sc->sc_if._if_input = mpls_input;
|
|
sc->sc_if.if_output = mpls_output;
|
|
sc->sc_if.if_ioctl = mpls_ioctl;
|
|
|
|
if_attach(&sc->sc_if);
|
|
if_alloc_sadl(&sc->sc_if);
|
|
bpf_attach(&sc->sc_if, DLT_NULL, sizeof(uint32_t));
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
mpls_clone_destroy(struct ifnet *ifp)
|
|
{
|
|
int s;
|
|
|
|
bpf_detach(ifp);
|
|
|
|
s = splnet();
|
|
if_detach(ifp);
|
|
splx(s);
|
|
|
|
free(ifp->if_softc, M_DEVBUF);
|
|
atomic_dec_uint(&mpls_count);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
mpls_input(struct ifnet *ifp, struct mbuf *m)
|
|
{
|
|
#if 0
|
|
/*
|
|
* TODO - kefren
|
|
* I'd love to unshim the packet, guess family
|
|
* and pass it to bpf
|
|
*/
|
|
bpf_mtap_af(ifp, AF_MPLS, m);
|
|
#endif
|
|
|
|
mpls_lse(m);
|
|
}
|
|
|
|
void
|
|
mplsintr(void)
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
for (;;) {
|
|
IFQ_LOCK(&mplsintrq);
|
|
IF_DEQUEUE(&mplsintrq, m);
|
|
IFQ_UNLOCK(&mplsintrq);
|
|
|
|
if (!m)
|
|
return;
|
|
|
|
if (((m->m_flags & M_PKTHDR) == 0) ||
|
|
(m->m_pkthdr.rcvif_index == 0))
|
|
panic("mplsintr(): no pkthdr or rcvif");
|
|
|
|
#ifdef MBUFTRACE
|
|
m_claimm(m, &mpls_owner);
|
|
#endif
|
|
mpls_input(m_get_rcvif_NOMPSAFE(m), m);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* prepend shim and deliver
|
|
*/
|
|
static int
|
|
mpls_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
|
|
const struct rtentry *rt)
|
|
{
|
|
union mpls_shim mh, *pms;
|
|
struct rtentry *rt1;
|
|
int err;
|
|
uint psize = sizeof(struct sockaddr_mpls);
|
|
|
|
KASSERT(KERNEL_LOCKED_P());
|
|
|
|
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
|
|
m_freem(m);
|
|
return ENETDOWN;
|
|
}
|
|
|
|
if (rt_gettag(rt) == NULL || rt_gettag(rt)->sa_family != AF_MPLS) {
|
|
m_freem(m);
|
|
return EINVAL;
|
|
}
|
|
|
|
bpf_mtap_af(ifp, dst->sa_family, m);
|
|
|
|
memset(&mh, 0, sizeof(mh));
|
|
mh.s_addr = MPLS_GETSADDR(rt);
|
|
mh.shim.bos = 1;
|
|
mh.shim.exp = 0;
|
|
mh.shim.ttl = mpls_defttl;
|
|
|
|
pms = &((struct sockaddr_mpls*)rt_gettag(rt))->smpls_addr;
|
|
|
|
while (psize <= rt_gettag(rt)->sa_len - sizeof(mh)) {
|
|
pms++;
|
|
if (mh.shim.label != MPLS_LABEL_IMPLNULL &&
|
|
((m = mpls_prepend_shim(m, &mh)) == NULL))
|
|
return ENOBUFS;
|
|
memset(&mh, 0, sizeof(mh));
|
|
mh.s_addr = ntohl(pms->s_addr);
|
|
mh.shim.bos = mh.shim.exp = 0;
|
|
mh.shim.ttl = mpls_defttl;
|
|
psize += sizeof(mh);
|
|
}
|
|
|
|
switch(dst->sa_family) {
|
|
#ifdef INET
|
|
case AF_INET:
|
|
m = mpls_label_inet(m, &mh, psize - sizeof(struct sockaddr_mpls));
|
|
break;
|
|
#endif
|
|
#ifdef INET6
|
|
case AF_INET6:
|
|
m = mpls_label_inet6(m, &mh, psize - sizeof(struct sockaddr_mpls));
|
|
break;
|
|
#endif
|
|
default:
|
|
m = mpls_prepend_shim(m, &mh);
|
|
break;
|
|
}
|
|
|
|
if (m == NULL) {
|
|
IF_DROP(&ifp->if_snd);
|
|
ifp->if_oerrors++;
|
|
return ENOBUFS;
|
|
}
|
|
|
|
ifp->if_opackets++;
|
|
ifp->if_obytes += m->m_pkthdr.len;
|
|
|
|
if ((rt1=rtalloc1(rt->rt_gateway, 1)) == NULL) {
|
|
m_freem(m);
|
|
return EHOSTUNREACH;
|
|
}
|
|
|
|
err = mpls_send_frame(m, rt1->rt_ifp, rt);
|
|
rt_unref(rt1);
|
|
return err;
|
|
}
|
|
|
|
static int
|
|
mpls_ioctl(struct ifnet *ifp, u_long cmd, void *data)
|
|
{
|
|
int error = 0, s = splnet();
|
|
struct ifreq *ifr = data;
|
|
|
|
switch(cmd) {
|
|
case SIOCINITIFADDR:
|
|
ifp->if_flags |= IFF_UP | IFF_RUNNING;
|
|
break;
|
|
case SIOCSIFMTU:
|
|
if (ifr != NULL && ifr->ifr_mtu < 576) {
|
|
error = EINVAL;
|
|
break;
|
|
}
|
|
/* FALLTHROUGH */
|
|
case SIOCGIFMTU:
|
|
if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
|
|
error = 0;
|
|
break;
|
|
case SIOCSIFFLAGS:
|
|
if ((error = ifioctl_common(ifp, cmd, data)) != 0)
|
|
break;
|
|
if (ifp->if_flags & IFF_UP)
|
|
ifp->if_flags |= IFF_RUNNING;
|
|
break;
|
|
default:
|
|
error = ifioctl_common(ifp, cmd, data);
|
|
break;
|
|
}
|
|
splx(s);
|
|
return error;
|
|
}
|
|
|
|
/*
|
|
* MPLS Label Switch Engine
|
|
*/
|
|
static int
|
|
mpls_lse(struct mbuf *m)
|
|
{
|
|
struct sockaddr_mpls dst;
|
|
union mpls_shim tshim, *htag;
|
|
struct rtentry *rt = NULL;
|
|
int error = ENOBUFS;
|
|
uint psize = sizeof(struct sockaddr_mpls);
|
|
bool push_back_alert = false;
|
|
|
|
if (m->m_len < sizeof(union mpls_shim) &&
|
|
(m = m_pullup(m, sizeof(union mpls_shim))) == NULL)
|
|
goto done;
|
|
|
|
dst.smpls_len = sizeof(struct sockaddr_mpls);
|
|
dst.smpls_family = AF_MPLS;
|
|
dst.smpls_addr.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
|
|
|
|
/* Check if we're accepting MPLS Frames */
|
|
error = EINVAL;
|
|
if (!mpls_frame_accept)
|
|
goto done;
|
|
|
|
/* TTL decrement */
|
|
if ((m = mpls_ttl_dec(m)) == NULL)
|
|
goto done;
|
|
|
|
/* RFC 4182 */
|
|
if (mpls_rfc4182 != 0)
|
|
while((dst.smpls_addr.shim.label == MPLS_LABEL_IPV4NULL ||
|
|
dst.smpls_addr.shim.label == MPLS_LABEL_IPV6NULL) &&
|
|
__predict_false(dst.smpls_addr.shim.bos == 0))
|
|
TRIM_LABEL;
|
|
|
|
/* RFC 3032 Section 2.1 Page 4 */
|
|
if (__predict_false(dst.smpls_addr.shim.label == MPLS_LABEL_RTALERT) &&
|
|
dst.smpls_addr.shim.bos == 0) {
|
|
TRIM_LABEL;
|
|
push_back_alert = true;
|
|
}
|
|
|
|
if (dst.smpls_addr.shim.label <= MPLS_LABEL_RESMAX) {
|
|
/* Don't swap reserved labels */
|
|
switch (dst.smpls_addr.shim.label) {
|
|
#ifdef INET
|
|
case MPLS_LABEL_IPV4NULL:
|
|
/* Pop shim and push mbuf to IP stack */
|
|
if (dst.smpls_addr.shim.bos)
|
|
error = mpls_unlabel_inet(m);
|
|
break;
|
|
#endif
|
|
#ifdef INET6
|
|
case MPLS_LABEL_IPV6NULL:
|
|
/* Pop shim and push mbuf to IPv6 stack */
|
|
if (dst.smpls_addr.shim.bos)
|
|
error = mpls_unlabel_inet6(m);
|
|
break;
|
|
#endif
|
|
case MPLS_LABEL_RTALERT: /* Yeah, I'm all alerted */
|
|
case MPLS_LABEL_IMPLNULL: /* This is logical only */
|
|
default: /* Rest are not allowed */
|
|
break;
|
|
}
|
|
goto done;
|
|
}
|
|
|
|
/* Check if we should do MPLS forwarding */
|
|
error = EHOSTUNREACH;
|
|
if (!mpls_forwarding)
|
|
goto done;
|
|
|
|
/* Get a route to dst */
|
|
dst.smpls_addr.shim.ttl =
|
|
dst.smpls_addr.shim.bos =
|
|
dst.smpls_addr.shim.exp = 0;
|
|
dst.smpls_addr.s_addr = htonl(dst.smpls_addr.s_addr);
|
|
if ((rt = rtalloc1((const struct sockaddr*)&dst, 1)) == NULL)
|
|
goto done;
|
|
|
|
/* MPLS packet with no MPLS tagged route ? */
|
|
if ((rt->rt_flags & RTF_GATEWAY) == 0 ||
|
|
rt_gettag(rt) == NULL ||
|
|
rt_gettag(rt)->sa_family != AF_MPLS)
|
|
goto done;
|
|
|
|
tshim.s_addr = MPLS_GETSADDR(rt);
|
|
|
|
/* Swap labels */
|
|
if ((m->m_len < sizeof(union mpls_shim)) &&
|
|
(m = m_pullup(m, sizeof(union mpls_shim))) == 0) {
|
|
error = ENOBUFS;
|
|
goto done;
|
|
}
|
|
|
|
/* Replace only the label */
|
|
htag = mtod(m, union mpls_shim *);
|
|
htag->s_addr = ntohl(htag->s_addr);
|
|
htag->shim.label = tshim.shim.label;
|
|
htag->s_addr = htonl(htag->s_addr);
|
|
|
|
/* check if there is anything more to prepend */
|
|
htag = &((struct sockaddr_mpls*)rt_gettag(rt))->smpls_addr;
|
|
while (psize <= rt_gettag(rt)->sa_len - sizeof(tshim)) {
|
|
htag++;
|
|
memset(&tshim, 0, sizeof(tshim));
|
|
tshim.s_addr = ntohl(htag->s_addr);
|
|
tshim.shim.bos = tshim.shim.exp = 0;
|
|
tshim.shim.ttl = mpls_defttl;
|
|
if (tshim.shim.label != MPLS_LABEL_IMPLNULL &&
|
|
((m = mpls_prepend_shim(m, &tshim)) == NULL))
|
|
return ENOBUFS;
|
|
psize += sizeof(tshim);
|
|
}
|
|
|
|
if (__predict_false(push_back_alert == true)) {
|
|
/* re-add the router alert label */
|
|
memset(&tshim, 0, sizeof(tshim));
|
|
tshim.s_addr = MPLS_LABEL_RTALERT;
|
|
tshim.shim.bos = tshim.shim.exp = 0;
|
|
tshim.shim.ttl = mpls_defttl;
|
|
if ((m = mpls_prepend_shim(m, &tshim)) == NULL)
|
|
return ENOBUFS;
|
|
}
|
|
|
|
if ((rt->rt_flags & RTF_GATEWAY) == 0) {
|
|
error = EHOSTUNREACH;
|
|
goto done;
|
|
}
|
|
|
|
rt->rt_use++;
|
|
error = mpls_send_frame(m, rt->rt_ifp, rt);
|
|
|
|
done:
|
|
if (error != 0 && m != NULL)
|
|
m_freem(m);
|
|
if (rt != NULL)
|
|
rt_unref(rt);
|
|
|
|
return error;
|
|
}
|
|
|
|
static int
|
|
mpls_send_frame(struct mbuf *m, struct ifnet *ifp, const struct rtentry *rt)
|
|
{
|
|
union mpls_shim msh;
|
|
int ret;
|
|
|
|
msh.s_addr = MPLS_GETSADDR(rt);
|
|
if (msh.shim.label == MPLS_LABEL_IMPLNULL ||
|
|
(m->m_flags & (M_MCAST | M_BCAST))) {
|
|
m_adj(m, sizeof(union mpls_shim));
|
|
m->m_pkthdr.csum_flags = 0;
|
|
}
|
|
|
|
switch(ifp->if_type) {
|
|
/* only these are supported for now */
|
|
case IFT_ETHER:
|
|
case IFT_TUNNEL:
|
|
case IFT_LOOP:
|
|
#ifdef INET
|
|
ret = ip_if_output(ifp, m, rt->rt_gateway, rt);
|
|
#else
|
|
ret = if_output_lock(ifp, ifp, m, rt->rt_gateway, rt);
|
|
#endif
|
|
return ret;
|
|
break;
|
|
default:
|
|
return ENETUNREACH;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
#ifdef INET
|
|
static int
|
|
mpls_unlabel_inet(struct mbuf *m)
|
|
{
|
|
struct ip *iph;
|
|
union mpls_shim *ms;
|
|
int iphlen;
|
|
|
|
if (mpls_mapttl_inet || mpls_mapprec_inet) {
|
|
|
|
/* get shim info */
|
|
ms = mtod(m, union mpls_shim *);
|
|
ms->s_addr = ntohl(ms->s_addr);
|
|
|
|
/* and get rid of it */
|
|
m_adj(m, sizeof(union mpls_shim));
|
|
|
|
/* get ip header */
|
|
if (m->m_len < sizeof (struct ip) &&
|
|
(m = m_pullup(m, sizeof(struct ip))) == NULL)
|
|
return ENOBUFS;
|
|
iph = mtod(m, struct ip *);
|
|
iphlen = iph->ip_hl << 2;
|
|
|
|
/* get it all */
|
|
if (m->m_len < iphlen) {
|
|
if ((m = m_pullup(m, iphlen)) == NULL)
|
|
return ENOBUFS;
|
|
iph = mtod(m, struct ip *);
|
|
}
|
|
|
|
/* check ipsum */
|
|
if (in_cksum(m, iphlen) != 0) {
|
|
m_freem(m);
|
|
return EINVAL;
|
|
}
|
|
|
|
/* set IP ttl from MPLS ttl */
|
|
if (mpls_mapttl_inet)
|
|
iph->ip_ttl = ms->shim.ttl;
|
|
|
|
/* set IP Precedence from MPLS Exp */
|
|
if (mpls_mapprec_inet) {
|
|
iph->ip_tos = (iph->ip_tos << 3) >> 3;
|
|
iph->ip_tos |= ms->shim.exp << 5;
|
|
}
|
|
|
|
/* reset ipsum because we modified TTL and TOS */
|
|
iph->ip_sum = 0;
|
|
iph->ip_sum = in_cksum(m, iphlen);
|
|
} else
|
|
m_adj(m, sizeof(union mpls_shim));
|
|
|
|
/* Put it on IP queue */
|
|
if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
|
|
m_freem(m);
|
|
return ENOBUFS;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Prepend MPLS label
|
|
*/
|
|
static struct mbuf *
|
|
mpls_label_inet(struct mbuf *m, union mpls_shim *ms, uint offset)
|
|
{
|
|
struct ip iphdr;
|
|
|
|
if (mpls_mapttl_inet || mpls_mapprec_inet) {
|
|
if ((m->m_len < sizeof(struct ip)) &&
|
|
(m = m_pullup(m, offset + sizeof(struct ip))) == 0)
|
|
return NULL; /* XXX */
|
|
m_copydata(m, offset, sizeof(struct ip), &iphdr);
|
|
|
|
/* Map TTL */
|
|
if (mpls_mapttl_inet)
|
|
ms->shim.ttl = iphdr.ip_ttl;
|
|
|
|
/* Copy IP precedence to EXP */
|
|
if (mpls_mapprec_inet)
|
|
ms->shim.exp = ((u_int8_t)iphdr.ip_tos) >> 5;
|
|
}
|
|
|
|
if ((m = mpls_prepend_shim(m, ms)) == NULL)
|
|
return NULL;
|
|
|
|
return m;
|
|
}
|
|
|
|
#endif /* INET */
|
|
|
|
#ifdef INET6
|
|
|
|
static int
|
|
mpls_unlabel_inet6(struct mbuf *m)
|
|
{
|
|
struct ip6_hdr *ip6hdr;
|
|
union mpls_shim ms;
|
|
|
|
/* TODO: mapclass */
|
|
if (mpls_mapttl_inet6) {
|
|
ms.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
|
|
m_adj(m, sizeof(union mpls_shim));
|
|
|
|
if (m->m_len < sizeof (struct ip6_hdr) &&
|
|
(m = m_pullup(m, sizeof(struct ip6_hdr))) == 0)
|
|
return ENOBUFS;
|
|
ip6hdr = mtod(m, struct ip6_hdr *);
|
|
|
|
/* Because we just decremented this in mpls_lse */
|
|
ip6hdr->ip6_hlim = ms.shim.ttl + 1;
|
|
} else
|
|
m_adj(m, sizeof(union mpls_shim));
|
|
|
|
/* Put it back on IPv6 queue. */
|
|
if (__predict_false(!pktq_enqueue(ip6_pktq, m, 0))) {
|
|
m_freem(m);
|
|
return ENOBUFS;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static struct mbuf *
|
|
mpls_label_inet6(struct mbuf *m, union mpls_shim *ms, uint offset)
|
|
{
|
|
struct ip6_hdr ip6h;
|
|
|
|
if (mpls_mapttl_inet6 || mpls_mapclass_inet6) {
|
|
if (m->m_len < sizeof(struct ip6_hdr) &&
|
|
(m = m_pullup(m, offset + sizeof(struct ip6_hdr))) == 0)
|
|
return NULL;
|
|
m_copydata(m, offset, sizeof(struct ip6_hdr), &ip6h);
|
|
|
|
if (mpls_mapttl_inet6)
|
|
ms->shim.ttl = ip6h.ip6_hlim;
|
|
|
|
if (mpls_mapclass_inet6)
|
|
ms->shim.exp = ip6h.ip6_vfc << 1 >> 5;
|
|
}
|
|
|
|
if ((m = mpls_prepend_shim(m, ms)) == NULL)
|
|
return NULL;
|
|
|
|
return m;
|
|
}
|
|
|
|
#endif /* INET6 */
|
|
|
|
static struct mbuf *
|
|
mpls_prepend_shim(struct mbuf *m, union mpls_shim *ms)
|
|
{
|
|
union mpls_shim *shim;
|
|
|
|
M_PREPEND(m, sizeof(*ms), M_DONTWAIT);
|
|
if (m == NULL)
|
|
return NULL;
|
|
|
|
if (m->m_len < sizeof(union mpls_shim) &&
|
|
(m = m_pullup(m, sizeof(union mpls_shim))) == 0)
|
|
return NULL;
|
|
|
|
shim = mtod(m, union mpls_shim *);
|
|
|
|
memcpy(shim, ms, sizeof(*shim));
|
|
shim->s_addr = htonl(shim->s_addr);
|
|
|
|
return m;
|
|
}
|
|
|
|
/*
|
|
* Module infrastructure
|
|
*/
|
|
#include "if_module.h"
|
|
|
|
IF_MODULE(MODULE_CLASS_DRIVER, mpls, "")
|