xref: /openbsd-src/sys/netmpls/mpls_output.c (revision 2ec318bfd9d2fdfebdb703c1db477637fd7ccd42)
1 /* $OpenBSD: mpls_output.c,v 1.17 2014/07/22 11:06:10 mpi 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/ip.h>
33 #endif
34 
35 #ifdef INET6
36 #include <netinet/ip6.h>
37 #endif
38 
39 #ifdef MPLS_DEBUG
40 #define MPLS_LABEL_GET(l)	((ntohl((l) & MPLS_LABEL_MASK)) >> MPLS_LABEL_OFFSET)
41 #endif
42 
43 void		mpls_do_cksum(struct mbuf *);
44 u_int8_t	mpls_getttl(struct mbuf *, sa_family_t);
45 
46 int
47 mpls_output(struct ifnet *ifp0, struct mbuf *m, struct sockaddr *dst,
48     struct rtentry *rt0)
49 {
50 	struct ifnet		*ifp = ifp0;
51 	struct sockaddr_mpls	*smpls;
52 	struct sockaddr_mpls	 sa_mpls;
53 	struct shim_hdr		*shim;
54 	struct rtentry		*rt = rt0;
55 	struct rt_mpls		*rt_mpls;
56 	int			 i, error;
57 	u_int8_t		 ttl;
58 
59 	if (rt0 == NULL || (dst->sa_family != AF_INET &&
60 	    dst->sa_family != AF_INET6 && dst->sa_family != AF_MPLS)) {
61 		if (!ISSET(ifp->if_xflags, IFXF_MPLS))
62 			return (ifp->if_output(ifp, m, dst, rt));
63 		else
64 			return (ifp->if_ll_output(ifp, m, dst, rt));
65 	}
66 
67 	/* need to calculate checksums now if necessary */
68 	mpls_do_cksum(m);
69 
70 	/* initialize sockaddr_mpls */
71 	bzero(&sa_mpls, sizeof(sa_mpls));
72 	smpls = &sa_mpls;
73 	smpls->smpls_family = AF_MPLS;
74 	smpls->smpls_len = sizeof(*smpls);
75 
76 	ttl = mpls_getttl(m, dst->sa_family);
77 
78 	for (i = 0; i < mpls_inkloop; i++) {
79 		rt_mpls = (struct rt_mpls *)rt->rt_llinfo;
80 		if (rt_mpls == NULL || (rt->rt_flags & RTF_MPLS) == 0) {
81 			/* no MPLS information for this entry */
82 			if (!ISSET(ifp->if_xflags, IFXF_MPLS)) {
83 #ifdef MPLS_DEBUG
84 				printf("MPLS_DEBUG: interface not mpls enabled\n");
85 #endif
86 				error = ENETUNREACH;
87 				goto bad;
88 			}
89 
90 			return (ifp->if_ll_output(ifp0, m, dst, rt0));
91 		}
92 
93 		switch (rt_mpls->mpls_operation) {
94 		case MPLS_OP_PUSH:
95 			m = mpls_shim_push(m, rt_mpls);
96 			break;
97 		case MPLS_OP_POP:
98 			m = mpls_shim_pop(m);
99 			break;
100 		case MPLS_OP_SWAP:
101 			m = mpls_shim_swap(m, rt_mpls);
102 			break;
103 		default:
104 			error = EINVAL;
105 			goto bad;
106 		}
107 
108 		if (m == NULL) {
109 			error = ENOBUFS;
110 			goto bad;
111 		}
112 
113 		/* refetch label */
114 		shim = mtod(m, struct shim_hdr *);
115 		/* mark first label with BOS flag */
116 		if (rt0 == rt && dst->sa_family != AF_MPLS)
117 			shim->shim_label |= MPLS_BOS_MASK;
118 
119 		ifp = rt->rt_ifp;
120 		if (ifp != NULL)
121 			break;
122 
123 		smpls->smpls_label = shim->shim_label & MPLS_LABEL_MASK;
124 		rt = rtalloc1(smplstosa(smpls), RT_REPORT, 0);
125 		if (rt == NULL) {
126 			/* no entry for this label */
127 #ifdef MPLS_DEBUG
128 			printf("MPLS_DEBUG: label %d not found\n",
129 			    MPLS_LABEL_GET(shim->shim_label));
130 #endif
131 			error = EHOSTUNREACH;
132 			goto bad;
133 		}
134 		rt->rt_use++;
135 		rt->rt_refcnt--;
136 	}
137 
138 	/* write back TTL */
139 	shim->shim_label &= ~MPLS_TTL_MASK;
140 	shim->shim_label |= htonl(ttl);
141 
142 #ifdef MPLS_DEBUG
143 	printf("MPLS: sending on %s outshim %x outlabel %d\n",
144 	    ifp->if_xname, ntohl(shim->shim_label),
145 	    MPLS_LABEL_GET(rt_mpls->mpls_label));
146 #endif
147 
148 	/* Output iface is not MPLS-enabled */
149 	if (!ISSET(ifp->if_xflags, IFXF_MPLS)) {
150 #ifdef MPLS_DEBUG
151 		printf("MPLS_DEBUG: interface not mpls enabled\n");
152 #endif
153 		error = ENETUNREACH;
154 		goto bad;
155 	}
156 
157 	/* reset broadcast and multicast flags, this is a P2P tunnel */
158 	m->m_flags &= ~(M_BCAST | M_MCAST);
159 
160 	smpls->smpls_label = shim->shim_label & MPLS_LABEL_MASK;
161 	return (ifp->if_ll_output(ifp, m, smplstosa(smpls), rt));
162 bad:
163 	if (m)
164 		m_freem(m);
165 	return (error);
166 }
167 
168 void
169 mpls_do_cksum(struct mbuf *m)
170 {
171 #ifdef INET
172 	struct ip *ip;
173 	u_int16_t hlen;
174 
175 	in_proto_cksum_out(m, NULL);
176 
177 	if (m->m_pkthdr.csum_flags & M_IPV4_CSUM_OUT) {
178 		ip = mtod(m, struct ip *);
179 		hlen = ip->ip_hl << 2;
180 		ip->ip_sum = in_cksum(m, hlen);
181 		m->m_pkthdr.csum_flags &= ~M_IPV4_CSUM_OUT;
182 	}
183 #endif
184 }
185 
186 u_int8_t
187 mpls_getttl(struct mbuf *m, sa_family_t af)
188 {
189 	struct shim_hdr *shim;
190 	struct ip *ip;
191 #ifdef INET6
192 	struct ip6_hdr *ip6hdr;
193 #endif
194 	u_int8_t ttl = mpls_defttl;
195 
196 	/* If the AF is MPLS then inherit the TTL from the present label. */
197 	if (af == AF_MPLS) {
198 		shim = mtod(m, struct shim_hdr *);
199 		ttl = ntohl(shim->shim_label & MPLS_TTL_MASK);
200 		return (ttl);
201 	}
202 	/* Else extract TTL from the encapsualted packet. */
203 	switch (*mtod(m, u_char *) >> 4) {
204 	case IPVERSION:
205 		if (!mpls_mapttl_ip)
206 			break;
207 		if (m->m_len < sizeof(*ip))
208 			break;			/* impossible */
209 		ip = mtod(m, struct ip *);
210 		ttl = ip->ip_ttl;
211 		break;
212 #ifdef INET6
213 	case IPV6_VERSION >> 4:
214 		if (!mpls_mapttl_ip6)
215 			break;
216 		if (m->m_len < sizeof(struct ip6_hdr))
217 			break;			/* impossible */
218 		ip6hdr = mtod(m, struct ip6_hdr *);
219 		ttl = ip6hdr->ip6_hlim;
220 		break;
221 #endif
222 	default:
223 		break;
224 	}
225 	return (ttl);
226 }
227