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