mpls_ttl.c revision 1.3.4.2 1 1.3.4.2 uebayasi /* $NetBSD: mpls_ttl.c,v 1.3.4.2 2010/08/17 06:47:49 uebayasi Exp $ */
2 1.3.4.2 uebayasi
3 1.3.4.2 uebayasi /*
4 1.3.4.2 uebayasi * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.3.4.2 uebayasi * All rights reserved.
6 1.3.4.2 uebayasi *
7 1.3.4.2 uebayasi * Redistribution and use in source and binary forms, with or without
8 1.3.4.2 uebayasi * modification, are permitted provided that the following conditions
9 1.3.4.2 uebayasi * are met:
10 1.3.4.2 uebayasi * 1. Redistributions of source code must retain the above copyright
11 1.3.4.2 uebayasi * notice, this list of conditions and the following disclaimer.
12 1.3.4.2 uebayasi * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.4.2 uebayasi * notice, this list of conditions and the following disclaimer in the
14 1.3.4.2 uebayasi * documentation and/or other materials provided with the distribution.
15 1.3.4.2 uebayasi * 3. Neither the name of the project nor the names of its contributors
16 1.3.4.2 uebayasi * may be used to endorse or promote products derived from this software
17 1.3.4.2 uebayasi * without specific prior written permission.
18 1.3.4.2 uebayasi *
19 1.3.4.2 uebayasi * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.3.4.2 uebayasi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.3.4.2 uebayasi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.3.4.2 uebayasi * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.3.4.2 uebayasi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.3.4.2 uebayasi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.3.4.2 uebayasi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.3.4.2 uebayasi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.3.4.2 uebayasi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.3.4.2 uebayasi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.3.4.2 uebayasi * SUCH DAMAGE.
30 1.3.4.2 uebayasi */
31 1.3.4.2 uebayasi
32 1.3.4.2 uebayasi /*-
33 1.3.4.2 uebayasi * Copyright (c) 2010 The NetBSD Foundation, Inc.
34 1.3.4.2 uebayasi * All rights reserved.
35 1.3.4.2 uebayasi *
36 1.3.4.2 uebayasi * This code is derived from software contributed to The NetBSD Foundation
37 1.3.4.2 uebayasi * by Public Access Networks Corporation ("Panix"). It was developed under
38 1.3.4.2 uebayasi * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 1.3.4.2 uebayasi *
40 1.3.4.2 uebayasi * This code is derived from software contributed to The NetBSD Foundation
41 1.3.4.2 uebayasi * by Jason R. Thorpe of Zembu Labs, Inc.
42 1.3.4.2 uebayasi *
43 1.3.4.2 uebayasi * This code is derived from software contributed to The NetBSD Foundation
44 1.3.4.2 uebayasi * by Mihai Chelaru <kefren (at) NetBSD.org>
45 1.3.4.2 uebayasi *
46 1.3.4.2 uebayasi * Redistribution and use in source and binary forms, with or without
47 1.3.4.2 uebayasi * modification, are permitted provided that the following conditions
48 1.3.4.2 uebayasi * are met:
49 1.3.4.2 uebayasi * 1. Redistributions of source code must retain the above copyright
50 1.3.4.2 uebayasi * notice, this list of conditions and the following disclaimer.
51 1.3.4.2 uebayasi * 2. Redistributions in binary form must reproduce the above copyright
52 1.3.4.2 uebayasi * notice, this list of conditions and the following disclaimer in the
53 1.3.4.2 uebayasi * documentation and/or other materials provided with the distribution.
54 1.3.4.2 uebayasi *
55 1.3.4.2 uebayasi * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56 1.3.4.2 uebayasi * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 1.3.4.2 uebayasi * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 1.3.4.2 uebayasi * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59 1.3.4.2 uebayasi * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60 1.3.4.2 uebayasi * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61 1.3.4.2 uebayasi * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62 1.3.4.2 uebayasi * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63 1.3.4.2 uebayasi * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64 1.3.4.2 uebayasi * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 1.3.4.2 uebayasi * POSSIBILITY OF SUCH DAMAGE.
66 1.3.4.2 uebayasi */
67 1.3.4.2 uebayasi
68 1.3.4.2 uebayasi /*
69 1.3.4.2 uebayasi * Copyright (c) 1982, 1986, 1988, 1993
70 1.3.4.2 uebayasi * The Regents of the University of California. All rights reserved.
71 1.3.4.2 uebayasi *
72 1.3.4.2 uebayasi * Redistribution and use in source and binary forms, with or without
73 1.3.4.2 uebayasi * modification, are permitted provided that the following conditions
74 1.3.4.2 uebayasi * are met:
75 1.3.4.2 uebayasi * 1. Redistributions of source code must retain the above copyright
76 1.3.4.2 uebayasi * notice, this list of conditions and the following disclaimer.
77 1.3.4.2 uebayasi * 2. Redistributions in binary form must reproduce the above copyright
78 1.3.4.2 uebayasi * notice, this list of conditions and the following disclaimer in the
79 1.3.4.2 uebayasi * documentation and/or other materials provided with the distribution.
80 1.3.4.2 uebayasi * 3. Neither the name of the University nor the names of its contributors
81 1.3.4.2 uebayasi * may be used to endorse or promote products derived from this software
82 1.3.4.2 uebayasi * without specific prior written permission.
83 1.3.4.2 uebayasi *
84 1.3.4.2 uebayasi * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85 1.3.4.2 uebayasi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 1.3.4.2 uebayasi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87 1.3.4.2 uebayasi * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88 1.3.4.2 uebayasi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 1.3.4.2 uebayasi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90 1.3.4.2 uebayasi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91 1.3.4.2 uebayasi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 1.3.4.2 uebayasi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 1.3.4.2 uebayasi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 1.3.4.2 uebayasi * SUCH DAMAGE.
95 1.3.4.2 uebayasi *
96 1.3.4.2 uebayasi * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
97 1.3.4.2 uebayasi */
98 1.3.4.2 uebayasi
99 1.3.4.2 uebayasi #include <sys/cdefs.h>
100 1.3.4.2 uebayasi __KERNEL_RCSID(0, "$NetBSD: mpls_ttl.c,v 1.3.4.2 2010/08/17 06:47:49 uebayasi Exp $");
101 1.3.4.2 uebayasi
102 1.3.4.2 uebayasi #include "opt_inet.h"
103 1.3.4.2 uebayasi #include "opt_mpls.h"
104 1.3.4.2 uebayasi
105 1.3.4.2 uebayasi #include <sys/param.h>
106 1.3.4.2 uebayasi #include <sys/malloc.h>
107 1.3.4.2 uebayasi #include <sys/mbuf.h>
108 1.3.4.2 uebayasi #include <sys/errno.h>
109 1.3.4.2 uebayasi #include <sys/socket.h>
110 1.3.4.2 uebayasi #include <sys/systm.h>
111 1.3.4.2 uebayasi
112 1.3.4.2 uebayasi #include <net/if.h>
113 1.3.4.2 uebayasi #include <net/if_dl.h>
114 1.3.4.2 uebayasi #include <net/route.h>
115 1.3.4.2 uebayasi
116 1.3.4.2 uebayasi #include <netinet/in.h>
117 1.3.4.2 uebayasi #include <netinet/in_systm.h>
118 1.3.4.2 uebayasi #include <netinet/ip.h>
119 1.3.4.2 uebayasi #include <netinet/ip_icmp.h>
120 1.3.4.2 uebayasi #include <netinet/icmp_private.h>
121 1.3.4.2 uebayasi #include <netinet/icmp_var.h>
122 1.3.4.2 uebayasi
123 1.3.4.2 uebayasi #include <netmpls/mpls.h>
124 1.3.4.2 uebayasi #include <netmpls/mpls_var.h>
125 1.3.4.2 uebayasi
126 1.3.4.2 uebayasi #ifdef INET
127 1.3.4.2 uebayasi
128 1.3.4.2 uebayasi /* in netinet/ip_icmp.c */
129 1.3.4.2 uebayasi extern int icmpreturndatabytes;
130 1.3.4.2 uebayasi
131 1.3.4.2 uebayasi /* ICMP Extensions */
132 1.3.4.2 uebayasi
133 1.3.4.2 uebayasi #define ICMP_EXT_VERSION 2
134 1.3.4.2 uebayasi #define MPLS_RETURN_DATA 128
135 1.3.4.2 uebayasi #define ICMP_EXT_OFFSET 128
136 1.3.4.2 uebayasi
137 1.3.4.2 uebayasi struct icmp_ext_cmn_hdr {
138 1.3.4.2 uebayasi #if BYTE_ORDER == BIG_ENDIAN
139 1.3.4.2 uebayasi unsigned char version:4;
140 1.3.4.2 uebayasi unsigned char reserved1:4;
141 1.3.4.2 uebayasi #else
142 1.3.4.2 uebayasi unsigned char reserved1:4;
143 1.3.4.2 uebayasi unsigned char version:4;
144 1.3.4.2 uebayasi #endif
145 1.3.4.2 uebayasi unsigned char reserved2;
146 1.3.4.2 uebayasi unsigned short checksum;
147 1.3.4.2 uebayasi };
148 1.3.4.2 uebayasi
149 1.3.4.2 uebayasi struct icmp_ext_obj_hdr {
150 1.3.4.2 uebayasi u_short length;
151 1.3.4.2 uebayasi u_char class_num;
152 1.3.4.2 uebayasi #define MPLS_STACK_ENTRY_CLASS 1
153 1.3.4.2 uebayasi u_char c_type;
154 1.3.4.2 uebayasi #define MPLS_STACK_ENTRY_C_TYPE 1
155 1.3.4.2 uebayasi };
156 1.3.4.2 uebayasi
157 1.3.4.2 uebayasi struct mpls_extension {
158 1.3.4.2 uebayasi struct icmp_ext_cmn_hdr cmn_hdr;
159 1.3.4.2 uebayasi struct icmp_ext_obj_hdr obj_hdr;
160 1.3.4.2 uebayasi union mpls_shim ms;
161 1.3.4.2 uebayasi } __packed;
162 1.3.4.2 uebayasi
163 1.3.4.2 uebayasi static void mpls_icmp_error(struct mbuf *, int, int, n_long, int,
164 1.3.4.2 uebayasi union mpls_shim *);
165 1.3.4.2 uebayasi static bool ip4_check(struct mbuf *);
166 1.3.4.2 uebayasi
167 1.3.4.2 uebayasi /*
168 1.3.4.2 uebayasi * Reference: http://tools.ietf.org/html/rfc4950
169 1.3.4.2 uebayasi * This should be in sync with icmp_error() in sys/netinet/ip_icmp.c
170 1.3.4.2 uebayasi */
171 1.3.4.2 uebayasi
172 1.3.4.2 uebayasi static void
173 1.3.4.2 uebayasi mpls_icmp_error(struct mbuf *n, int type, int code, n_long dest,
174 1.3.4.2 uebayasi int destmtu, union mpls_shim *shim)
175 1.3.4.2 uebayasi {
176 1.3.4.2 uebayasi struct ip *oip = mtod(n, struct ip *), *nip;
177 1.3.4.2 uebayasi unsigned oiplen = oip->ip_hl << 2;
178 1.3.4.2 uebayasi struct icmp *icp;
179 1.3.4.2 uebayasi struct mbuf *m;
180 1.3.4.2 uebayasi unsigned icmplen, mblen, packetlen;
181 1.3.4.2 uebayasi struct mpls_extension mpls_icmp_ext;
182 1.3.4.2 uebayasi
183 1.3.4.2 uebayasi memset(&mpls_icmp_ext, 0, sizeof(mpls_icmp_ext));
184 1.3.4.2 uebayasi mpls_icmp_ext.cmn_hdr.version = ICMP_EXT_VERSION;
185 1.3.4.2 uebayasi mpls_icmp_ext.cmn_hdr.checksum = 0;
186 1.3.4.2 uebayasi
187 1.3.4.2 uebayasi mpls_icmp_ext.obj_hdr.length = htons(sizeof(union mpls_shim) +
188 1.3.4.2 uebayasi sizeof(struct icmp_ext_obj_hdr));
189 1.3.4.2 uebayasi mpls_icmp_ext.obj_hdr.class_num = MPLS_STACK_ENTRY_CLASS;
190 1.3.4.2 uebayasi mpls_icmp_ext.obj_hdr.c_type = MPLS_STACK_ENTRY_C_TYPE;
191 1.3.4.2 uebayasi
192 1.3.4.2 uebayasi mpls_icmp_ext.ms.s_addr = shim->s_addr;
193 1.3.4.2 uebayasi
194 1.3.4.2 uebayasi #ifdef ICMPPRINTFS
195 1.3.4.2 uebayasi if (icmpprintfs)
196 1.3.4.2 uebayasi printf("mpls_icmp_error(%p, type:%d, code:%d)\n", oip, type,
197 1.3.4.2 uebayasi code);
198 1.3.4.2 uebayasi #endif
199 1.3.4.2 uebayasi if (type != ICMP_REDIRECT)
200 1.3.4.2 uebayasi ICMP_STATINC(ICMP_STAT_ERROR);
201 1.3.4.2 uebayasi /*
202 1.3.4.2 uebayasi * Don't send error if the original packet was encrypted.
203 1.3.4.2 uebayasi * Don't send error if not the first fragment of message.
204 1.3.4.2 uebayasi * Don't error if the old packet protocol was ICMP
205 1.3.4.2 uebayasi * error message, only known informational types.
206 1.3.4.2 uebayasi */
207 1.3.4.2 uebayasi if (n->m_flags & M_DECRYPTED)
208 1.3.4.2 uebayasi goto freeit;
209 1.3.4.2 uebayasi if (oip->ip_off &~ htons(IP_MF|IP_DF))
210 1.3.4.2 uebayasi goto freeit;
211 1.3.4.2 uebayasi if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
212 1.3.4.2 uebayasi n->m_len >= oiplen + ICMP_MINLEN &&
213 1.3.4.2 uebayasi !ICMP_INFOTYPE(((struct icmp *)((char *)oip + oiplen))->icmp_type))
214 1.3.4.2 uebayasi {
215 1.3.4.2 uebayasi ICMP_STATINC(ICMP_STAT_OLDICMP);
216 1.3.4.2 uebayasi goto freeit;
217 1.3.4.2 uebayasi }
218 1.3.4.2 uebayasi /* Don't send error in response to a multicast or broadcast packet */
219 1.3.4.2 uebayasi if (n->m_flags & (M_BCAST|M_MCAST))
220 1.3.4.2 uebayasi goto freeit;
221 1.3.4.2 uebayasi
222 1.3.4.2 uebayasi /*
223 1.3.4.2 uebayasi * First, do a rate limitation check.
224 1.3.4.2 uebayasi */
225 1.3.4.2 uebayasi if (icmp_ratelimit(&oip->ip_src, type, code))
226 1.3.4.2 uebayasi /* XXX stats */
227 1.3.4.2 uebayasi goto freeit;
228 1.3.4.2 uebayasi
229 1.3.4.2 uebayasi /*
230 1.3.4.2 uebayasi * Now, formulate icmp message
231 1.3.4.2 uebayasi */
232 1.3.4.2 uebayasi icmplen = min(ICMP_EXT_OFFSET, ntohs(oip->ip_len));
233 1.3.4.2 uebayasi /*
234 1.3.4.2 uebayasi * Defend against mbuf chains shorter than oip->ip_len - oiplen:
235 1.3.4.2 uebayasi */
236 1.3.4.2 uebayasi mblen = 0;
237 1.3.4.2 uebayasi for (m = n; m && (mblen < icmplen); m = m->m_next)
238 1.3.4.2 uebayasi mblen += m->m_len;
239 1.3.4.2 uebayasi icmplen = min(mblen, icmplen);
240 1.3.4.2 uebayasi
241 1.3.4.2 uebayasi packetlen = sizeof(struct ip) + ICMP_EXT_OFFSET + sizeof(mpls_icmp_ext);
242 1.3.4.2 uebayasi
243 1.3.4.2 uebayasi /*
244 1.3.4.2 uebayasi * As we are not required to return everything we have,
245 1.3.4.2 uebayasi * we return whatever we can return at ease.
246 1.3.4.2 uebayasi *
247 1.3.4.2 uebayasi * Note that ICMP datagrams longer than 576 octets are out of spec
248 1.3.4.2 uebayasi * according to RFC1812; the limit on icmpreturndatabytes below in
249 1.3.4.2 uebayasi * icmp_sysctl will keep things below that limit.
250 1.3.4.2 uebayasi */
251 1.3.4.2 uebayasi
252 1.3.4.2 uebayasi KASSERT (packetlen <= MCLBYTES);
253 1.3.4.2 uebayasi
254 1.3.4.2 uebayasi m = m_gethdr(M_DONTWAIT, MT_HEADER);
255 1.3.4.2 uebayasi if (m && (packetlen > MHLEN)) {
256 1.3.4.2 uebayasi MCLGET(m, M_DONTWAIT);
257 1.3.4.2 uebayasi if ((m->m_flags & M_EXT) == 0) {
258 1.3.4.2 uebayasi m_freem(m);
259 1.3.4.2 uebayasi m = NULL;
260 1.3.4.2 uebayasi }
261 1.3.4.2 uebayasi }
262 1.3.4.2 uebayasi if (m == NULL)
263 1.3.4.2 uebayasi goto freeit;
264 1.3.4.2 uebayasi MCLAIM(m, n->m_owner);
265 1.3.4.2 uebayasi m->m_len = packetlen;
266 1.3.4.2 uebayasi if ((m->m_flags & M_EXT) == 0)
267 1.3.4.2 uebayasi MH_ALIGN(m, m->m_len);
268 1.3.4.2 uebayasi else {
269 1.3.4.2 uebayasi m->m_data += sizeof(struct ip);
270 1.3.4.2 uebayasi m->m_len -= sizeof(struct ip);
271 1.3.4.2 uebayasi }
272 1.3.4.2 uebayasi icp = mtod(m, struct icmp *);
273 1.3.4.2 uebayasi if ((u_int)type > ICMP_MAXTYPE)
274 1.3.4.2 uebayasi panic("icmp error");
275 1.3.4.2 uebayasi ICMP_STATINC(ICMP_STAT_OUTHIST + type);
276 1.3.4.2 uebayasi icp->icmp_type = type;
277 1.3.4.2 uebayasi if (type == ICMP_REDIRECT)
278 1.3.4.2 uebayasi icp->icmp_gwaddr.s_addr = dest;
279 1.3.4.2 uebayasi else {
280 1.3.4.2 uebayasi icp->icmp_void = 0;
281 1.3.4.2 uebayasi /*
282 1.3.4.2 uebayasi * The following assignments assume an overlay with the
283 1.3.4.2 uebayasi * zeroed icmp_void field.
284 1.3.4.2 uebayasi */
285 1.3.4.2 uebayasi if (type == ICMP_PARAMPROB) {
286 1.3.4.2 uebayasi icp->icmp_pptr = code;
287 1.3.4.2 uebayasi code = 0;
288 1.3.4.2 uebayasi } else if (type == ICMP_UNREACH &&
289 1.3.4.2 uebayasi code == ICMP_UNREACH_NEEDFRAG && destmtu)
290 1.3.4.2 uebayasi icp->icmp_nextmtu = htons(destmtu);
291 1.3.4.2 uebayasi }
292 1.3.4.2 uebayasi
293 1.3.4.2 uebayasi icp->icmp_code = code;
294 1.3.4.2 uebayasi
295 1.3.4.2 uebayasi memset(&icp->icmp_ip, 0, ICMP_EXT_OFFSET);
296 1.3.4.2 uebayasi m_copydata(n, 0, icmplen, (char *)&icp->icmp_ip);
297 1.3.4.2 uebayasi
298 1.3.4.2 uebayasi /* Append the extension structure */
299 1.3.4.2 uebayasi memcpy(((char*)&icp->icmp_ip) + ICMP_EXT_OFFSET,
300 1.3.4.2 uebayasi &mpls_icmp_ext, sizeof(mpls_icmp_ext));
301 1.3.4.2 uebayasi
302 1.3.4.2 uebayasi /*
303 1.3.4.2 uebayasi * Now, copy old ip header (without options)
304 1.3.4.2 uebayasi * in front of icmp message.
305 1.3.4.2 uebayasi */
306 1.3.4.2 uebayasi if ((m->m_flags & M_EXT) == 0 &&
307 1.3.4.2 uebayasi m->m_data - sizeof(struct ip) < m->m_pktdat)
308 1.3.4.2 uebayasi panic("icmp len");
309 1.3.4.2 uebayasi m->m_data -= sizeof(struct ip);
310 1.3.4.2 uebayasi m->m_len += sizeof(struct ip);
311 1.3.4.2 uebayasi m->m_pkthdr.len = m->m_len;
312 1.3.4.2 uebayasi m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
313 1.3.4.2 uebayasi nip = mtod(m, struct ip *);
314 1.3.4.2 uebayasi /* ip_v set in ip_output */
315 1.3.4.2 uebayasi nip->ip_hl = sizeof(struct ip) >> 2;
316 1.3.4.2 uebayasi nip->ip_tos = 0;
317 1.3.4.2 uebayasi nip->ip_len = htons(m->m_len);
318 1.3.4.2 uebayasi /* ip_id set in ip_output */
319 1.3.4.2 uebayasi nip->ip_off = htons(0);
320 1.3.4.2 uebayasi /* ip_ttl set in icmp_reflect */
321 1.3.4.2 uebayasi nip->ip_p = IPPROTO_ICMP;
322 1.3.4.2 uebayasi nip->ip_src = oip->ip_src;
323 1.3.4.2 uebayasi nip->ip_dst = oip->ip_dst;
324 1.3.4.2 uebayasi icmp_reflect(m);
325 1.3.4.2 uebayasi
326 1.3.4.2 uebayasi freeit:
327 1.3.4.2 uebayasi m_freem(n);
328 1.3.4.2 uebayasi
329 1.3.4.2 uebayasi }
330 1.3.4.2 uebayasi
331 1.3.4.2 uebayasi static bool
332 1.3.4.2 uebayasi ip4_check(struct mbuf *m)
333 1.3.4.2 uebayasi {
334 1.3.4.2 uebayasi struct ip *iph;
335 1.3.4.2 uebayasi int hlen, len;
336 1.3.4.2 uebayasi
337 1.3.4.2 uebayasi if (m->m_len < sizeof(struct ip) &&
338 1.3.4.2 uebayasi (m = m_pullup(m, sizeof(struct ip))) == NULL)
339 1.3.4.2 uebayasi return false;
340 1.3.4.2 uebayasi
341 1.3.4.2 uebayasi iph = mtod(m, struct ip *);
342 1.3.4.2 uebayasi
343 1.3.4.2 uebayasi if (iph->ip_v != IPVERSION)
344 1.3.4.2 uebayasi goto freeit;
345 1.3.4.2 uebayasi hlen = iph->ip_hl << 2;
346 1.3.4.2 uebayasi if (hlen < sizeof(struct ip))
347 1.3.4.2 uebayasi goto freeit;
348 1.3.4.2 uebayasi if (hlen > m->m_len) {
349 1.3.4.2 uebayasi if ((m = m_pullup(m, hlen)) == NULL)
350 1.3.4.2 uebayasi return false;
351 1.3.4.2 uebayasi iph = mtod(m, struct ip *);
352 1.3.4.2 uebayasi }
353 1.3.4.2 uebayasi if (IN_MULTICAST(iph->ip_src.s_addr) ||
354 1.3.4.2 uebayasi (ntohl(iph->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
355 1.3.4.2 uebayasi (ntohl(iph->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
356 1.3.4.2 uebayasi in_cksum(m, hlen) != 0)
357 1.3.4.2 uebayasi goto freeit;
358 1.3.4.2 uebayasi
359 1.3.4.2 uebayasi len = ntohs(iph->ip_len);
360 1.3.4.2 uebayasi if (len < hlen || m->m_pkthdr.len < len)
361 1.3.4.2 uebayasi goto freeit;
362 1.3.4.2 uebayasi
363 1.3.4.2 uebayasi return true;
364 1.3.4.2 uebayasi freeit:
365 1.3.4.2 uebayasi m_freem(m);
366 1.3.4.2 uebayasi return false;
367 1.3.4.2 uebayasi
368 1.3.4.2 uebayasi }
369 1.3.4.2 uebayasi
370 1.3.4.2 uebayasi #endif /* INET */
371 1.3.4.2 uebayasi
372 1.3.4.2 uebayasi struct mbuf *
373 1.3.4.2 uebayasi mpls_ttl_dec(struct mbuf *m)
374 1.3.4.2 uebayasi {
375 1.3.4.2 uebayasi union mpls_shim *mshim;
376 1.3.4.2 uebayasi #ifdef INET
377 1.3.4.2 uebayasi union mpls_shim top_shim, bossh;
378 1.3.4.2 uebayasi #endif
379 1.3.4.2 uebayasi
380 1.3.4.2 uebayasi if (m->m_len < sizeof(union mpls_shim) &&
381 1.3.4.2 uebayasi (m = m_pullup(m, sizeof(union mpls_shim))) == NULL)
382 1.3.4.2 uebayasi return NULL;
383 1.3.4.2 uebayasi mshim = mtod(m, union mpls_shim *);
384 1.3.4.2 uebayasi mshim->s_addr = ntohl(mshim->s_addr);
385 1.3.4.2 uebayasi mshim->shim.ttl--;
386 1.3.4.2 uebayasi
387 1.3.4.2 uebayasi if (mshim->shim.ttl == 0) {
388 1.3.4.2 uebayasi if (!mpls_icmp_respond) {
389 1.3.4.2 uebayasi m_freem(m);
390 1.3.4.2 uebayasi return NULL;
391 1.3.4.2 uebayasi }
392 1.3.4.2 uebayasi
393 1.3.4.2 uebayasi #ifdef INET
394 1.3.4.2 uebayasi /*
395 1.3.4.2 uebayasi * shim ttl exceeded
396 1.3.4.2 uebayasi * send back ICMP type 11 code 0
397 1.3.4.2 uebayasi */
398 1.3.4.2 uebayasi bossh.s_addr = mshim->s_addr;
399 1.3.4.2 uebayasi top_shim.s_addr = htonl(mshim->s_addr);
400 1.3.4.2 uebayasi m_adj(m, sizeof(union mpls_shim));
401 1.3.4.2 uebayasi
402 1.3.4.2 uebayasi /* Goto BOS */
403 1.3.4.2 uebayasi while(bossh.shim.bos == 0) {
404 1.3.4.2 uebayasi if (m->m_len < sizeof(union mpls_shim) &&
405 1.3.4.2 uebayasi (m = m_pullup(m, sizeof(union mpls_shim))) == NULL) {
406 1.3.4.2 uebayasi m_freem(m);
407 1.3.4.2 uebayasi return NULL;
408 1.3.4.2 uebayasi }
409 1.3.4.2 uebayasi bossh.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
410 1.3.4.2 uebayasi m_adj(m, sizeof(union mpls_shim));
411 1.3.4.2 uebayasi }
412 1.3.4.2 uebayasi
413 1.3.4.2 uebayasi if (ip4_check(m) == true)
414 1.3.4.2 uebayasi mpls_icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
415 1.3.4.2 uebayasi 0, 0, &top_shim);
416 1.3.4.2 uebayasi #else
417 1.3.4.2 uebayasi m_freem(m);
418 1.3.4.2 uebayasi #endif
419 1.3.4.2 uebayasi return NULL;
420 1.3.4.2 uebayasi }
421 1.3.4.2 uebayasi
422 1.3.4.2 uebayasi mshim->s_addr = htonl(mshim->s_addr);
423 1.3.4.2 uebayasi
424 1.3.4.2 uebayasi return m;
425 1.3.4.2 uebayasi }
426