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