xref: /dragonfly/sys/netproto/mpls/mpls_input.c (revision cfd1aba3)
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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
13  *    distribution.
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.
17  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/globaldata.h>
33 #include <sys/kernel.h>
34 #include <sys/mbuf.h>
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
38 
39 #include <net/if_var.h>
40 #include <net/netisr.h>
41 #include <net/route.h>
42 
43 #include <sys/thread2.h>
44 #include <sys/mplock2.h>
45 
46 #include <netproto/mpls/mpls.h>
47 #include <netproto/mpls/mpls_var.h>
48 
49 struct mpls_stats	mplsstats_percpu[MAXCPU];
50 struct route		mplsforward_rt[MAXCPU];
51 
52 int mplsforwarding = 1;
53 /*
54 SYSCTL_INT(_net_mpls, OID_AUTO, forwarding, CTLFLAG_RW,
55     &mplsforwarding, 0, "Enable MPLS forwarding between interfaces");
56 */
57 
58 static void	mpls_input_handler(netmsg_t);
59 static void	mpls_forward(struct mbuf *);
60 
61 void
62 mpls_init(void)
63 {
64 	int cpu;
65 
66 	/*
67 	 * Initialize MPLS statistics counters for each CPU.
68 	 *
69 	 */
70 	for (cpu = 0; cpu < ncpus; ++cpu) {
71 		bzero(&mplsstats_percpu[cpu], sizeof(struct mpls_stats));
72 	}
73 
74 	netisr_register(NETISR_MPLS, mpls_input_handler, mpls_hashfn);
75 }
76 
77 static void
78 mpls_input_handler(netmsg_t msg)
79 {
80         struct mbuf *m = msg->packet.nm_packet;
81 
82 	get_mplock();
83         mpls_input(m);
84 	rel_mplock();
85 	/* do not reply, msg embedded in mbuf */
86 }
87 
88 void
89 mpls_input(struct mbuf *m)
90 {
91 	struct mpls *mpls = NULL;
92 	mpls_label_t label;
93 
94 	M_ASSERTPKTHDR(m);
95 
96 	mplsstat.mplss_total++;
97 
98 	/* length checks already performed at mpls_demux() */
99 	KASSERT(m->m_pkthdr.len >= sizeof(struct mpls),
100 	    ("mpls_input: mpls header too small"));
101 
102 again:
103 	if (m->m_len < sizeof(struct mpls)) {
104 		m = m_pullup(m, sizeof(struct mpls));
105 		if (m == NULL) {
106 			mplsstat.mplss_toosmall++;
107 			return;
108 		}
109 	}
110 
111 	mpls = mtod(m, struct mpls*);
112 	label = MPLS_LABEL(ntohl(mpls->mpls_shim));
113 	switch (label) {
114 	case 0:
115 		/*
116 		 * Label 0: represents "IPv4 Explicit NULL Label".
117 		 */
118 		if (MPLS_STACK(ntohl(mpls->mpls_shim))) {
119 			/* Decapsulate the ip datagram from the mpls frame. */
120 			m_adj(m, sizeof(struct mpls));
121 			netisr_queue(NETISR_IP, m);
122 			return;
123 		}
124 		goto again; /* If not the bottom label, per RFC4182. */
125 
126 	case 1:
127 		/*
128 		 * Label 1: represents "Router Alert Label" and is valid
129 		 * anywhere except at the bottom of the stack.
130 		 */
131 		break;
132 
133 	case 2:
134 		/*
135 		 * Label 2: represents "IPv6 Explicit NULL Label".
136 		 */
137 		if (MPLS_STACK(ntohl(mpls->mpls_shim))) {
138 			/* Decapsulate the ip datagram from the mpls frame. */
139 			m_adj(m, sizeof(struct mpls));
140 			netisr_queue(NETISR_IPV6, m);
141 			return;
142 		}
143 		goto again; /* If not the bottom label, per RFC4182. */
144 
145 	case 3:
146 		/*
147 		 * Label 3: represents the "Implicit NULL Label" and must not
148 		 * appear on the wire.
149 		 */
150 		break;
151 	default:
152 		/*
153 		 * Labels 4 - 15: reserved, drop them.
154 		 */
155 		if (label <= 15) {
156 			mplsstat.mplss_reserved++;
157 			m_freem(m);
158 			return;
159 		}
160 		if (mplsforwarding) {
161 			mpls_forward(m);
162 			return;
163 		} else {
164 			mplsstat.mplss_cantforward++;
165 			m_freem(m);
166 			return;
167 		}
168 	}
169 
170 	mplsstat.mplss_invalid++;
171 	m_freem(m);
172 }
173 
174 static void
175 mpls_forward(struct mbuf *m)
176 {
177 	struct sockaddr_mpls *smpls;
178 	struct mpls *mpls;
179 	struct route *cache_rt = &mplsforward_rt[mycpuid];
180 	mpls_label_t label;
181 	struct ifnet *ifp;
182 	struct sockaddr *dst;
183 	int error;
184 
185 	KASSERT(m->m_len >= sizeof(struct mpls),
186 	    ("mpls_forward: mpls header not in one mbuf"));
187 
188 	mpls = mtod(m, struct mpls *);
189 	label = MPLS_LABEL(ntohl(mpls->mpls_shim));
190 
191 	smpls = (struct sockaddr_mpls *) &cache_rt->ro_dst;
192 	if (cache_rt->ro_rt == NULL || smpls->smpls_label != label) {
193 		if (cache_rt->ro_rt != NULL) {
194 			RTFREE(cache_rt->ro_rt);
195 			cache_rt->ro_rt = NULL;
196 		}
197 		smpls->smpls_family = AF_MPLS;
198 		smpls->smpls_len = sizeof(struct sockaddr_mpls);
199 		smpls->smpls_label = htonl(label);
200 		rtalloc(cache_rt);
201 		if (cache_rt->ro_rt == NULL) {
202 			/* route not found */
203 			return;
204 		}
205 	}
206 
207 	ifp = cache_rt->ro_rt->rt_ifp;
208 	dst = cache_rt->ro_rt->rt_gateway;
209 	error = mpls_output(m, cache_rt->ro_rt);
210 	if (error)
211 		goto bad;
212 	error = (*ifp->if_output)(ifp, m, dst, cache_rt->ro_rt);
213 	if (error)
214 		goto bad;
215 	mplsstat.mplss_forwarded++;
216 
217 	return;
218 bad:
219 	m_freem(m);
220 }
221