19b42cabeSNuno Antunes /*
29b42cabeSNuno Antunes * Copyright (c) 2007 The DragonFly Project. All rights reserved.
39b42cabeSNuno Antunes *
49b42cabeSNuno Antunes * Redistribution and use in source and binary forms, with or without
59b42cabeSNuno Antunes * modification, are permitted provided that the following conditions
69b42cabeSNuno Antunes * are met:
79b42cabeSNuno Antunes *
89b42cabeSNuno Antunes * 1. Redistributions of source code must retain the above copyright
99b42cabeSNuno Antunes * notice, this list of conditions and the following disclaimer.
109b42cabeSNuno Antunes * 2. Redistributions in binary form must reproduce the above copyright
119b42cabeSNuno Antunes * notice, this list of conditions and the following disclaimer in
129b42cabeSNuno Antunes * the documentation and/or other materials provided with the
139b42cabeSNuno Antunes * distribution.
149b42cabeSNuno Antunes * 3. Neither the name of The DragonFly Project nor the names of its
159b42cabeSNuno Antunes * contributors may be used to endorse or promote products derived
169b42cabeSNuno Antunes * from this software without specific, prior written permission.
179b42cabeSNuno Antunes *
189b42cabeSNuno Antunes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
199b42cabeSNuno Antunes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
209b42cabeSNuno Antunes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
219b42cabeSNuno Antunes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
229b42cabeSNuno Antunes * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
239b42cabeSNuno Antunes * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
249b42cabeSNuno Antunes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
259b42cabeSNuno Antunes * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
269b42cabeSNuno Antunes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
279b42cabeSNuno Antunes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
289b42cabeSNuno Antunes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b42cabeSNuno Antunes * SUCH DAMAGE.
309b42cabeSNuno Antunes */
319b42cabeSNuno Antunes
329b42cabeSNuno Antunes #include <sys/globaldata.h>
339b42cabeSNuno Antunes #include <sys/kernel.h>
349b42cabeSNuno Antunes #include <sys/mbuf.h>
359b42cabeSNuno Antunes #include <sys/param.h>
369b42cabeSNuno Antunes #include <sys/sysctl.h>
379b42cabeSNuno Antunes #include <sys/systm.h>
389b42cabeSNuno Antunes
399b42cabeSNuno Antunes #include <net/if_var.h>
409b42cabeSNuno Antunes #include <net/netisr.h>
419b42cabeSNuno Antunes #include <net/route.h>
429b42cabeSNuno Antunes
43c3c96e44SMatthew Dillon #include <sys/mplock2.h>
44c3c96e44SMatthew Dillon
459b42cabeSNuno Antunes #include <netproto/mpls/mpls.h>
469b42cabeSNuno Antunes #include <netproto/mpls/mpls_var.h>
479b42cabeSNuno Antunes
489b42cabeSNuno Antunes struct mpls_stats mplsstats_percpu[MAXCPU];
499b42cabeSNuno Antunes struct route mplsforward_rt[MAXCPU];
509b42cabeSNuno Antunes
519b42cabeSNuno Antunes int mplsforwarding = 1;
529b42cabeSNuno Antunes /*
539b42cabeSNuno Antunes SYSCTL_INT(_net_mpls, OID_AUTO, forwarding, CTLFLAG_RW,
549b42cabeSNuno Antunes &mplsforwarding, 0, "Enable MPLS forwarding between interfaces");
559b42cabeSNuno Antunes */
569b42cabeSNuno Antunes
5767bf99c4SMatthew Dillon static void mpls_input_handler(netmsg_t);
589b42cabeSNuno Antunes static void mpls_forward(struct mbuf *);
599b42cabeSNuno Antunes
609b42cabeSNuno Antunes void
mpls_init(void)619b42cabeSNuno Antunes mpls_init(void)
629b42cabeSNuno Antunes {
639b42cabeSNuno Antunes int cpu;
649b42cabeSNuno Antunes
659b42cabeSNuno Antunes /*
669b42cabeSNuno Antunes * Initialize MPLS statistics counters for each CPU.
679b42cabeSNuno Antunes *
689b42cabeSNuno Antunes */
699b42cabeSNuno Antunes for (cpu = 0; cpu < ncpus; ++cpu) {
709b42cabeSNuno Antunes bzero(&mplsstats_percpu[cpu], sizeof(struct mpls_stats));
719b42cabeSNuno Antunes }
729b42cabeSNuno Antunes
73*ca86d83eSSepherosa Ziehau netisr_register(NETISR_MPLS, mpls_input_handler, mpls_hashfn);
749b42cabeSNuno Antunes }
759b42cabeSNuno Antunes
769b42cabeSNuno Antunes static void
mpls_input_handler(netmsg_t msg)7767bf99c4SMatthew Dillon mpls_input_handler(netmsg_t msg)
789b42cabeSNuno Antunes {
7967bf99c4SMatthew Dillon struct mbuf *m = msg->packet.nm_packet;
809b42cabeSNuno Antunes
81c3c96e44SMatthew Dillon get_mplock();
829b42cabeSNuno Antunes mpls_input(m);
83c3c96e44SMatthew Dillon rel_mplock();
8467bf99c4SMatthew Dillon /* do not reply, msg embedded in mbuf */
859b42cabeSNuno Antunes }
869b42cabeSNuno Antunes
879b42cabeSNuno Antunes void
mpls_input(struct mbuf * m)889b42cabeSNuno Antunes mpls_input(struct mbuf *m)
899b42cabeSNuno Antunes {
909b42cabeSNuno Antunes struct mpls *mpls = NULL;
919b42cabeSNuno Antunes mpls_label_t label;
929b42cabeSNuno Antunes
939b42cabeSNuno Antunes M_ASSERTPKTHDR(m);
949b42cabeSNuno Antunes
959b42cabeSNuno Antunes mplsstat.mplss_total++;
969b42cabeSNuno Antunes
979b42cabeSNuno Antunes /* length checks already performed at mpls_demux() */
989b42cabeSNuno Antunes KASSERT(m->m_pkthdr.len >= sizeof(struct mpls),
999b42cabeSNuno Antunes ("mpls_input: mpls header too small"));
1009b42cabeSNuno Antunes
1019b42cabeSNuno Antunes again:
1029b42cabeSNuno Antunes if (m->m_len < sizeof(struct mpls)) {
1039b42cabeSNuno Antunes m = m_pullup(m, sizeof(struct mpls));
1049b42cabeSNuno Antunes if (m == NULL) {
1059b42cabeSNuno Antunes mplsstat.mplss_toosmall++;
1069b42cabeSNuno Antunes return;
1079b42cabeSNuno Antunes }
1089b42cabeSNuno Antunes }
1099b42cabeSNuno Antunes
1109b42cabeSNuno Antunes mpls = mtod(m, struct mpls*);
1119b42cabeSNuno Antunes label = MPLS_LABEL(ntohl(mpls->mpls_shim));
1129b42cabeSNuno Antunes switch (label) {
1139b42cabeSNuno Antunes case 0:
1149b42cabeSNuno Antunes /*
1159b42cabeSNuno Antunes * Label 0: represents "IPv4 Explicit NULL Label".
1169b42cabeSNuno Antunes */
1179b42cabeSNuno Antunes if (MPLS_STACK(ntohl(mpls->mpls_shim))) {
1189b42cabeSNuno Antunes /* Decapsulate the ip datagram from the mpls frame. */
1199b42cabeSNuno Antunes m_adj(m, sizeof(struct mpls));
120c3c96e44SMatthew Dillon netisr_queue(NETISR_IP, m);
1219b42cabeSNuno Antunes return;
1229b42cabeSNuno Antunes }
1239b42cabeSNuno Antunes goto again; /* If not the bottom label, per RFC4182. */
1249b42cabeSNuno Antunes
1259b42cabeSNuno Antunes case 1:
1269b42cabeSNuno Antunes /*
1279b42cabeSNuno Antunes * Label 1: represents "Router Alert Label" and is valid
1289b42cabeSNuno Antunes * anywhere except at the bottom of the stack.
1299b42cabeSNuno Antunes */
1309b42cabeSNuno Antunes break;
1319b42cabeSNuno Antunes
1329b42cabeSNuno Antunes case 2:
1339b42cabeSNuno Antunes /*
1349b42cabeSNuno Antunes * Label 2: represents "IPv6 Explicit NULL Label".
1359b42cabeSNuno Antunes */
1369b42cabeSNuno Antunes if (MPLS_STACK(ntohl(mpls->mpls_shim))) {
1379b42cabeSNuno Antunes /* Decapsulate the ip datagram from the mpls frame. */
1389b42cabeSNuno Antunes m_adj(m, sizeof(struct mpls));
139c3c96e44SMatthew Dillon netisr_queue(NETISR_IPV6, m);
1409b42cabeSNuno Antunes return;
1419b42cabeSNuno Antunes }
1429b42cabeSNuno Antunes goto again; /* If not the bottom label, per RFC4182. */
1439b42cabeSNuno Antunes
1449b42cabeSNuno Antunes case 3:
1459b42cabeSNuno Antunes /*
1469b42cabeSNuno Antunes * Label 3: represents the "Implicit NULL Label" and must not
1479b42cabeSNuno Antunes * appear on the wire.
1489b42cabeSNuno Antunes */
1499b42cabeSNuno Antunes break;
1509b42cabeSNuno Antunes default:
1519b42cabeSNuno Antunes /*
1529b42cabeSNuno Antunes * Labels 4 - 15: reserved, drop them.
1539b42cabeSNuno Antunes */
1549b42cabeSNuno Antunes if (label <= 15) {
1559b42cabeSNuno Antunes mplsstat.mplss_reserved++;
1569b42cabeSNuno Antunes m_freem(m);
1579b42cabeSNuno Antunes return;
1589b42cabeSNuno Antunes }
1599b42cabeSNuno Antunes if (mplsforwarding) {
1609b42cabeSNuno Antunes mpls_forward(m);
1619b42cabeSNuno Antunes return;
1629b42cabeSNuno Antunes } else {
1639b42cabeSNuno Antunes mplsstat.mplss_cantforward++;
1649b42cabeSNuno Antunes m_freem(m);
1659b42cabeSNuno Antunes return;
1669b42cabeSNuno Antunes }
1679b42cabeSNuno Antunes }
1689b42cabeSNuno Antunes
1699b42cabeSNuno Antunes mplsstat.mplss_invalid++;
1709b42cabeSNuno Antunes m_freem(m);
1719b42cabeSNuno Antunes }
1729b42cabeSNuno Antunes
1739b42cabeSNuno Antunes static void
mpls_forward(struct mbuf * m)1749b42cabeSNuno Antunes mpls_forward(struct mbuf *m)
1759b42cabeSNuno Antunes {
1769b42cabeSNuno Antunes struct sockaddr_mpls *smpls;
1779b42cabeSNuno Antunes struct mpls *mpls;
1789b42cabeSNuno Antunes struct route *cache_rt = &mplsforward_rt[mycpuid];
1799b42cabeSNuno Antunes mpls_label_t label;
1809b42cabeSNuno Antunes struct ifnet *ifp;
1819b42cabeSNuno Antunes struct sockaddr *dst;
182cb8d752cSNuno Antunes int error;
1839b42cabeSNuno Antunes
1849b42cabeSNuno Antunes KASSERT(m->m_len >= sizeof(struct mpls),
185cb8d752cSNuno Antunes ("mpls_forward: mpls header not in one mbuf"));
1869b42cabeSNuno Antunes
1879b42cabeSNuno Antunes mpls = mtod(m, struct mpls *);
1889b42cabeSNuno Antunes label = MPLS_LABEL(ntohl(mpls->mpls_shim));
1899b42cabeSNuno Antunes
1909b42cabeSNuno Antunes smpls = (struct sockaddr_mpls *) &cache_rt->ro_dst;
1919b42cabeSNuno Antunes if (cache_rt->ro_rt == NULL || smpls->smpls_label != label) {
1929b42cabeSNuno Antunes if (cache_rt->ro_rt != NULL) {
1939b42cabeSNuno Antunes RTFREE(cache_rt->ro_rt);
1949b42cabeSNuno Antunes cache_rt->ro_rt = NULL;
1959b42cabeSNuno Antunes }
1969b42cabeSNuno Antunes smpls->smpls_family = AF_MPLS;
1979b42cabeSNuno Antunes smpls->smpls_len = sizeof(struct sockaddr_mpls);
1989b42cabeSNuno Antunes smpls->smpls_label = htonl(label);
1999b42cabeSNuno Antunes rtalloc(cache_rt);
2009b42cabeSNuno Antunes if (cache_rt->ro_rt == NULL) {
2019b42cabeSNuno Antunes /* route not found */
2029b42cabeSNuno Antunes return;
2039b42cabeSNuno Antunes }
2049b42cabeSNuno Antunes }
2059b42cabeSNuno Antunes
2069b42cabeSNuno Antunes ifp = cache_rt->ro_rt->rt_ifp;
2079b42cabeSNuno Antunes dst = cache_rt->ro_rt->rt_gateway;
208cb8d752cSNuno Antunes error = mpls_output(m, cache_rt->ro_rt);
209cb8d752cSNuno Antunes if (error)
210cb8d752cSNuno Antunes goto bad;
211cb8d752cSNuno Antunes error = (*ifp->if_output)(ifp, m, dst, cache_rt->ro_rt);
212cb8d752cSNuno Antunes if (error)
213cb8d752cSNuno Antunes goto bad;
2149b42cabeSNuno Antunes mplsstat.mplss_forwarded++;
215cb8d752cSNuno Antunes
216cb8d752cSNuno Antunes return;
217cb8d752cSNuno Antunes bad:
218cb8d752cSNuno Antunes m_freem(m);
2199b42cabeSNuno Antunes }
220