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