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
|
/* $OpenBSD: mpls_input.c,v 1.12 2008/05/10 01:56:32 claudio Exp $ */
/*
* Copyright (c) 2008 Claudio Jeker <claudio@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 "mpe.h"
#include <sys/param.h>
#include <sys/mbuf.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netmpls/mpls.h>
struct ifqueue mplsintrq;
int mplsqmaxlen = IFQ_MAXLEN;
extern int mpls_inkloop;
#ifdef MPLS_DEBUG
#define MPLS_LABEL_GET(l) ((ntohl((l) & MPLS_LABEL_MASK)) >> MPLS_LABEL_OFFSET)
#define MPLS_TTL_GET(l) (ntohl((l) & MPLS_TTL_MASK))
#endif
void
mpls_init(void)
{
mplsintrq.ifq_maxlen = mplsqmaxlen;
}
void
mplsintr(void)
{
struct mbuf *m;
int s;
for (;;) {
/* Get next datagram of input queue */
s = splnet();
IF_DEQUEUE(&mplsintrq, m);
splx(s);
if (m == NULL)
return;
#ifdef DIAGNOSTIC
if ((m->m_flags & M_PKTHDR) == 0)
panic("ipintr no HDR");
#endif
mpls_input(m);
}
}
void
mpls_input(struct mbuf *m)
{
struct ifnet *ifp = m->m_pkthdr.rcvif;
struct sockaddr_mpls *smpls;
struct sockaddr_mpls sa_mpls;
struct shim_hdr *shim;
struct rtentry *rt = NULL;
u_int32_t ttl;
int i, hasbos;
if (!mpls_enable) {
m_freem(m);
return;
}
/* drop all broadcast and multicast packets */
if (m->m_flags & (M_BCAST | M_MCAST)) {
m_freem(m);
return;
}
if (m->m_len < sizeof(*shim))
if ((m = m_pullup(m, sizeof(*shim))) == NULL)
return;
shim = mtod(m, struct shim_hdr *);
#ifdef MPLS_DEBUG
printf("mpls_input: iface %s label=%d, ttl=%d BoS %d\n",
ifp->if_xname, MPLS_LABEL_GET(shim->shim_label),
MPLS_TTL_GET(shim->shim_label),
MPLS_BOS_ISSET(shim->shim_label));
#endif /* MPLS_DEBUG */
/* check and decrement TTL */
ttl = ntohl(shim->shim_label & MPLS_TTL_MASK);
if (ttl <= 1) {
/* TTL exceeded */
/*
* XXX if possible hand packet up to network layer so that an
* ICMP TTL exceeded can be sent back.
*/
m_freem(m);
return;
}
ttl = htonl(ttl - 1);
for (i = 0; i < mpls_inkloop; i++) {
bzero(&sa_mpls, sizeof(sa_mpls));
smpls = &sa_mpls;
smpls->smpls_family = AF_MPLS;
smpls->smpls_len = sizeof(*smpls);
smpls->smpls_in_ifindex = ifp->if_index;
smpls->smpls_in_label = shim->shim_label & MPLS_LABEL_MASK;
printf("smpls af %d len %d in_label %d in_ifindex %d\n",
smpls->smpls_family, smpls->smpls_len,
MPLS_LABEL_GET(smpls->smpls_in_label),
smpls->smpls_in_ifindex);
rt = rtalloc1(smplstosa(smpls),1, 0);
if (rt == NULL) {
/* no entry for this label */
#ifdef MPLS_DEBUG
printf("MPLS_DEBUG: label not found\n");
#endif
m_freem(m);
goto done;
}
rt->rt_use++;
smpls = satosmpls(rt_key(rt));
#ifdef MPLS_DEBUG
printf("route af %d len %d in_label %d in_ifindex %d\n",
smpls->smpls_family, smpls->smpls_len,
MPLS_LABEL_GET(smpls->smpls_in_label),
smpls->smpls_in_ifindex);
printf("\top %d out_label %d out_ifindex %d\n",
smpls->smpls_operation,
MPLS_LABEL_GET(smpls->smpls_out_label),
smpls->smpls_out_ifindex);
#endif
switch (smpls->smpls_operation) {
case MPLS_OP_POP:
hasbos = MPLS_BOS_ISSET(shim->shim_label);
m = mpls_shim_pop(m);
if (hasbos) {
#if NMPE > 0
if (rt->rt_ifp->if_type == IFT_MPLS) {
mpe_input(m, rt->rt_ifp, smpls, ttl);
goto done;
}
#endif
/* last label but we have no clue so drop */
m_freem(m);
goto done;
}
break;
case MPLS_OP_PUSH:
m = mpls_shim_push(m, smpls);
break;
case MPLS_OP_SWAP:
m = mpls_shim_swap(m, smpls);
break;
default:
m_freem(m);
goto done;
}
if (m == NULL)
goto done;
/* refetch label */
shim = mtod(m, struct shim_hdr *);
ifp = rt->rt_ifp;
if (smpls->smpls_out_ifindex)
break;
RTFREE(rt);
rt = NULL;
}
/* write back TTL */
shim->shim_label = (shim->shim_label & ~MPLS_TTL_MASK) | ttl;
#ifdef MPLS_DEBUG
printf("MPLS: sending on %s outlabel %x dst af %d in %d out %d\n",
ifp->if_xname, ntohl(shim->shim_label), smpls->smpls_family,
MPLS_LABEL_GET(smpls->smpls_in_label),
MPLS_LABEL_GET(smpls->smpls_out_label));
#endif
(*ifp->if_output)(ifp, m, smplstosa(smpls), rt);
done:
if (rt)
RTFREE(rt);
}
|