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