2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
14 * 3. Neither the name of The DragonFly Project nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific, prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $DragonFly: src/sys/netproto/mpls/mpls_input.c,v 1.4 2008/09/24 14:26:39 sephe Exp $
34 #include <sys/globaldata.h>
35 #include <sys/kernel.h>
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
41 #include <net/if_var.h>
42 #include <net/netisr.h>
43 #include <net/route.h>
45 #include <sys/thread2.h>
46 #include <sys/mplock2.h>
48 #include <netproto/mpls/mpls.h>
49 #include <netproto/mpls/mpls_var.h>
51 struct mpls_stats mplsstats_percpu[MAXCPU];
52 struct route mplsforward_rt[MAXCPU];
54 int mplsforwarding = 1;
56 SYSCTL_INT(_net_mpls, OID_AUTO, forwarding, CTLFLAG_RW,
57 &mplsforwarding, 0, "Enable MPLS forwarding between interfaces");
60 static void mpls_input_handler(struct netmsg *);
61 static void mpls_forward(struct mbuf *);
71 * Initialize MPLS statistics counters for each CPU.
75 for (cpu = 0; cpu < ncpus; ++cpu) {
76 bzero(&mplsstats_percpu[cpu], sizeof(struct mpls_stats));
79 bzero(&mplsstat, sizeof(struct mpls_stats));
82 netisr_register(NETISR_MPLS, mpls_input_handler, mpls_cpufn);
86 mpls_input_handler(struct netmsg *msg0)
88 struct mbuf *m = ((struct netmsg_packet *)msg0)->nm_packet;
96 mpls_input(struct mbuf *m)
98 struct mpls *mpls = NULL;
103 mplsstat.mplss_total++;
105 /* length checks already performed at mpls_demux() */
106 KASSERT(m->m_pkthdr.len >= sizeof(struct mpls),
107 ("mpls_input: mpls header too small"));
110 if (m->m_len < sizeof(struct mpls)) {
111 m = m_pullup(m, sizeof(struct mpls));
113 mplsstat.mplss_toosmall++;
118 mpls = mtod(m, struct mpls*);
119 label = MPLS_LABEL(ntohl(mpls->mpls_shim));
123 * Label 0: represents "IPv4 Explicit NULL Label".
125 if (MPLS_STACK(ntohl(mpls->mpls_shim))) {
126 /* Decapsulate the ip datagram from the mpls frame. */
127 m_adj(m, sizeof(struct mpls));
128 netisr_queue(NETISR_IP, m);
131 goto again; /* If not the bottom label, per RFC4182. */
135 * Label 1: represents "Router Alert Label" and is valid
136 * anywhere except at the bottom of the stack.
142 * Label 2: represents "IPv6 Explicit NULL Label".
144 if (MPLS_STACK(ntohl(mpls->mpls_shim))) {
145 /* Decapsulate the ip datagram from the mpls frame. */
146 m_adj(m, sizeof(struct mpls));
147 netisr_queue(NETISR_IPV6, m);
150 goto again; /* If not the bottom label, per RFC4182. */
154 * Label 3: represents the "Implicit NULL Label" and must not
155 * appear on the wire.
160 * Labels 4 - 15: reserved, drop them.
163 mplsstat.mplss_reserved++;
167 if (mplsforwarding) {
171 mplsstat.mplss_cantforward++;
177 mplsstat.mplss_invalid++;
182 mpls_forward(struct mbuf *m)
184 struct sockaddr_mpls *smpls;
186 struct route *cache_rt = &mplsforward_rt[mycpuid];
189 struct sockaddr *dst;
192 KASSERT(m->m_len >= sizeof(struct mpls),
193 ("mpls_forward: mpls header not in one mbuf"));
195 mpls = mtod(m, struct mpls *);
196 label = MPLS_LABEL(ntohl(mpls->mpls_shim));
198 smpls = (struct sockaddr_mpls *) &cache_rt->ro_dst;
199 if (cache_rt->ro_rt == NULL || smpls->smpls_label != label) {
200 if (cache_rt->ro_rt != NULL) {
201 RTFREE(cache_rt->ro_rt);
202 cache_rt->ro_rt = NULL;
204 smpls->smpls_family = AF_MPLS;
205 smpls->smpls_len = sizeof(struct sockaddr_mpls);
206 smpls->smpls_label = htonl(label);
208 if (cache_rt->ro_rt == NULL) {
209 /* route not found */
214 ifp = cache_rt->ro_rt->rt_ifp;
215 dst = cache_rt->ro_rt->rt_gateway;
216 error = mpls_output(m, cache_rt->ro_rt);
219 error = (*ifp->if_output)(ifp, m, dst, cache_rt->ro_rt);
222 mplsstat.mplss_forwarded++;