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