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