1 /* $OpenBSD: mpls_output.c,v 1.15 2011/04/04 17:44:43 henning Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Claudio Jeker <claudio@openbsd.org> 5 * Copyright (c) 2008 Michele Marchetto <michele@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/param.h> 21 #include <sys/mbuf.h> 22 #include <sys/systm.h> 23 #include <sys/socket.h> 24 25 #include <net/if.h> 26 #include <net/route.h> 27 28 #include <netmpls/mpls.h> 29 30 #ifdef INET 31 #include <netinet/in.h> 32 #include <netinet/in_systm.h> 33 #include <netinet/ip.h> 34 #endif 35 36 #ifdef INET6 37 #include <netinet/ip6.h> 38 #endif 39 40 extern int mpls_inkloop; 41 42 #ifdef MPLS_DEBUG 43 #define MPLS_LABEL_GET(l) ((ntohl((l) & MPLS_LABEL_MASK)) >> MPLS_LABEL_OFFSET) 44 #endif 45 46 void mpls_do_cksum(struct mbuf *); 47 u_int8_t mpls_getttl(struct mbuf *, sa_family_t); 48 49 int 50 mpls_output(struct ifnet *ifp0, struct mbuf *m, struct sockaddr *dst, 51 struct rtentry *rt0) 52 { 53 struct ifnet *ifp = ifp0; 54 struct sockaddr_mpls *smpls; 55 struct sockaddr_mpls sa_mpls; 56 struct shim_hdr *shim; 57 struct rtentry *rt = rt0; 58 struct rt_mpls *rt_mpls; 59 int i, error; 60 u_int8_t ttl; 61 62 if (rt0 == NULL || (dst->sa_family != AF_INET && 63 dst->sa_family != AF_INET6 && dst->sa_family != AF_MPLS)) { 64 if (!ISSET(ifp->if_xflags, IFXF_MPLS)) 65 return (ifp->if_output(ifp, m, dst, rt)); 66 else 67 return (ifp->if_ll_output(ifp, m, dst, rt)); 68 } 69 70 /* need to calculate checksums now if necessary */ 71 mpls_do_cksum(m); 72 73 /* initialize sockaddr_mpls */ 74 bzero(&sa_mpls, sizeof(sa_mpls)); 75 smpls = &sa_mpls; 76 smpls->smpls_family = AF_MPLS; 77 smpls->smpls_len = sizeof(*smpls); 78 79 ttl = mpls_getttl(m, dst->sa_family); 80 81 for (i = 0; i < mpls_inkloop; i++) { 82 rt_mpls = (struct rt_mpls *)rt->rt_llinfo; 83 if (rt_mpls == NULL || (rt->rt_flags & RTF_MPLS) == 0) { 84 /* no MPLS information for this entry */ 85 if (!ISSET(ifp->if_xflags, IFXF_MPLS)) { 86 #ifdef MPLS_DEBUG 87 printf("MPLS_DEBUG: interface not mpls enabled\n"); 88 #endif 89 error = ENETUNREACH; 90 goto bad; 91 } 92 93 return (ifp->if_ll_output(ifp0, m, dst, rt0)); 94 } 95 96 switch (rt_mpls->mpls_operation) { 97 case MPLS_OP_PUSH: 98 m = mpls_shim_push(m, rt_mpls); 99 break; 100 case MPLS_OP_POP: 101 m = mpls_shim_pop(m); 102 break; 103 case MPLS_OP_SWAP: 104 m = mpls_shim_swap(m, rt_mpls); 105 break; 106 default: 107 error = EINVAL; 108 goto bad; 109 } 110 111 if (m == NULL) { 112 error = ENOBUFS; 113 goto bad; 114 } 115 116 /* refetch label */ 117 shim = mtod(m, struct shim_hdr *); 118 /* mark first label with BOS flag */ 119 if (rt0 == rt && dst->sa_family != AF_MPLS) 120 shim->shim_label |= MPLS_BOS_MASK; 121 122 ifp = rt->rt_ifp; 123 if (ifp != NULL) 124 break; 125 126 smpls->smpls_label = shim->shim_label & MPLS_LABEL_MASK; 127 rt = rtalloc1(smplstosa(smpls), RT_REPORT, 0); 128 if (rt == NULL) { 129 /* no entry for this label */ 130 #ifdef MPLS_DEBUG 131 printf("MPLS_DEBUG: label %d not found\n", 132 MPLS_LABEL_GET(shim->shim_label)); 133 #endif 134 error = EHOSTUNREACH; 135 goto bad; 136 } 137 rt->rt_use++; 138 rt->rt_refcnt--; 139 } 140 141 /* write back TTL */ 142 shim->shim_label &= ~MPLS_TTL_MASK; 143 shim->shim_label |= htonl(ttl); 144 145 #ifdef MPLS_DEBUG 146 printf("MPLS: sending on %s outshim %x outlabel %d\n", 147 ifp->if_xname, ntohl(shim->shim_label), 148 MPLS_LABEL_GET(rt_mpls->mpls_label)); 149 #endif 150 151 /* Output iface is not MPLS-enabled */ 152 if (!ISSET(ifp->if_xflags, IFXF_MPLS)) { 153 #ifdef MPLS_DEBUG 154 printf("MPLS_DEBUG: interface not mpls enabled\n"); 155 #endif 156 error = ENETUNREACH; 157 goto bad; 158 } 159 160 /* reset broadcast and multicast flags, this is a P2P tunnel */ 161 m->m_flags &= ~(M_BCAST | M_MCAST); 162 163 smpls->smpls_label = shim->shim_label & MPLS_LABEL_MASK; 164 return (ifp->if_ll_output(ifp, m, smplstosa(smpls), rt)); 165 bad: 166 if (m) 167 m_freem(m); 168 return (error); 169 } 170 171 void 172 mpls_do_cksum(struct mbuf *m) 173 { 174 #ifdef INET 175 struct ip *ip; 176 u_int16_t hlen; 177 178 in_proto_cksum_out(m, NULL); 179 180 if (m->m_pkthdr.csum_flags & M_IPV4_CSUM_OUT) { 181 ip = mtod(m, struct ip *); 182 hlen = ip->ip_hl << 2; 183 ip->ip_sum = in_cksum(m, hlen); 184 m->m_pkthdr.csum_flags &= ~M_IPV4_CSUM_OUT; 185 } 186 #endif 187 } 188 189 u_int8_t 190 mpls_getttl(struct mbuf *m, sa_family_t af) 191 { 192 struct shim_hdr *shim; 193 struct ip *ip; 194 #ifdef INET6 195 struct ip6_hdr *ip6hdr; 196 #endif 197 u_int8_t ttl = mpls_defttl; 198 199 /* If the AF is MPLS then inherit the TTL from the present label. */ 200 if (af == AF_MPLS) { 201 shim = mtod(m, struct shim_hdr *); 202 ttl = ntohl(shim->shim_label & MPLS_TTL_MASK); 203 return (ttl); 204 } 205 /* Else extract TTL from the encapsualted packet. */ 206 switch (*mtod(m, u_char *) >> 4) { 207 case IPVERSION: 208 if (!mpls_mapttl_ip) 209 break; 210 if (m->m_len < sizeof(*ip)) 211 break; /* impossible */ 212 ip = mtod(m, struct ip *); 213 ttl = ip->ip_ttl; 214 break; 215 #ifdef INET6 216 case IPV6_VERSION >> 4: 217 if (!mpls_mapttl_ip6) 218 break; 219 if (m->m_len < sizeof(struct ip6_hdr)) 220 break; /* impossible */ 221 ip6hdr = mtod(m, struct ip6_hdr *); 222 ttl = ip6hdr->ip6_hlim; 223 break; 224 #endif 225 default: 226 break; 227 } 228 return (ttl); 229 } 230