]>
Commit | Line | Data |
---|---|---|
f0cbd3ec FB |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1988, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | * | |
33 | * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 | |
34 | * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp | |
35 | */ | |
36 | ||
37 | /* | |
38 | * Changes and additions relating to SLiRP are | |
39 | * Copyright (c) 1995 Danny Gasparovski. | |
5fafdf24 | 40 | * |
f0cbd3ec FB |
41 | * Please read the file COPYRIGHT for the |
42 | * terms and conditions of the copyright. | |
43 | */ | |
44 | ||
45 | #include <slirp.h> | |
429d0a3d | 46 | #include <osdep.h> |
f0cbd3ec FB |
47 | #include "ip_icmp.h" |
48 | ||
31a60e22 | 49 | #ifdef LOG_ENABLED |
f0cbd3ec | 50 | struct ipstat ipstat; |
31a60e22 BS |
51 | #endif |
52 | ||
f0cbd3ec FB |
53 | struct ipq ipq; |
54 | ||
429d0a3d | 55 | static struct ip *ip_reass(register struct ip *ip, |
9634d903 BS |
56 | register struct ipq *fp); |
57 | static void ip_freef(struct ipq *fp); | |
58 | static void ip_enq(register struct ipasfrag *p, | |
59 | register struct ipasfrag *prev); | |
60 | static void ip_deq(register struct ipasfrag *p); | |
61 | ||
f0cbd3ec FB |
62 | /* |
63 | * IP initialization: fill in IP protocol switch table. | |
64 | * All protocols not implemented in kernel go to raw IP protocol handler. | |
65 | */ | |
66 | void | |
67 | ip_init() | |
68 | { | |
429d0a3d | 69 | ipq.ip_link.next = ipq.ip_link.prev = &ipq.ip_link; |
f0cbd3ec FB |
70 | ip_id = tt.tv_sec & 0xffff; |
71 | udp_init(); | |
72 | tcp_init(); | |
f0cbd3ec FB |
73 | } |
74 | ||
75 | /* | |
76 | * Ip input routine. Checksum and byte swap header. If fragmented | |
77 | * try to reassemble. Process options. Pass to next level. | |
78 | */ | |
79 | void | |
80 | ip_input(m) | |
81 | struct mbuf *m; | |
82 | { | |
83 | register struct ip *ip; | |
84 | int hlen; | |
5fafdf24 | 85 | |
f0cbd3ec FB |
86 | DEBUG_CALL("ip_input"); |
87 | DEBUG_ARG("m = %lx", (long)m); | |
88 | DEBUG_ARG("m_len = %d", m->m_len); | |
89 | ||
31a60e22 | 90 | STAT(ipstat.ips_total++); |
5fafdf24 | 91 | |
f0cbd3ec | 92 | if (m->m_len < sizeof (struct ip)) { |
31a60e22 | 93 | STAT(ipstat.ips_toosmall++); |
f0cbd3ec FB |
94 | return; |
95 | } | |
5fafdf24 | 96 | |
f0cbd3ec | 97 | ip = mtod(m, struct ip *); |
5fafdf24 | 98 | |
f0cbd3ec | 99 | if (ip->ip_v != IPVERSION) { |
31a60e22 | 100 | STAT(ipstat.ips_badvers++); |
f0cbd3ec FB |
101 | goto bad; |
102 | } | |
103 | ||
104 | hlen = ip->ip_hl << 2; | |
105 | if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */ | |
31a60e22 | 106 | STAT(ipstat.ips_badhlen++); /* or packet too short */ |
f0cbd3ec FB |
107 | goto bad; |
108 | } | |
109 | ||
110 | /* keep ip header intact for ICMP reply | |
5fafdf24 TS |
111 | * ip->ip_sum = cksum(m, hlen); |
112 | * if (ip->ip_sum) { | |
f0cbd3ec FB |
113 | */ |
114 | if(cksum(m,hlen)) { | |
31a60e22 | 115 | STAT(ipstat.ips_badsum++); |
f0cbd3ec FB |
116 | goto bad; |
117 | } | |
118 | ||
119 | /* | |
120 | * Convert fields to host representation. | |
121 | */ | |
122 | NTOHS(ip->ip_len); | |
123 | if (ip->ip_len < hlen) { | |
31a60e22 | 124 | STAT(ipstat.ips_badlen++); |
f0cbd3ec FB |
125 | goto bad; |
126 | } | |
127 | NTOHS(ip->ip_id); | |
128 | NTOHS(ip->ip_off); | |
129 | ||
130 | /* | |
131 | * Check that the amount of data in the buffers | |
132 | * is as at least much as the IP header would have us expect. | |
133 | * Trim mbufs if longer than we expect. | |
134 | * Drop packet if shorter than we expect. | |
135 | */ | |
136 | if (m->m_len < ip->ip_len) { | |
31a60e22 | 137 | STAT(ipstat.ips_tooshort++); |
f0cbd3ec FB |
138 | goto bad; |
139 | } | |
a9ba3a85 AL |
140 | |
141 | if (slirp_restrict) { | |
142 | if (memcmp(&ip->ip_dst.s_addr, &special_addr, 3)) { | |
143 | if (ip->ip_dst.s_addr == 0xffffffff && ip->ip_p != IPPROTO_UDP) | |
144 | goto bad; | |
145 | } else { | |
146 | int host = ntohl(ip->ip_dst.s_addr) & 0xff; | |
147 | struct ex_list *ex_ptr; | |
148 | ||
149 | if (host == 0xff) | |
150 | goto bad; | |
151 | ||
152 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) | |
153 | if (ex_ptr->ex_addr == host) | |
154 | break; | |
155 | ||
156 | if (!ex_ptr) | |
157 | goto bad; | |
158 | } | |
159 | } | |
160 | ||
f0cbd3ec FB |
161 | /* Should drop packet if mbuf too long? hmmm... */ |
162 | if (m->m_len > ip->ip_len) | |
163 | m_adj(m, ip->ip_len - m->m_len); | |
164 | ||
165 | /* check ip_ttl for a correct ICMP reply */ | |
166 | if(ip->ip_ttl==0 || ip->ip_ttl==1) { | |
167 | icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl"); | |
168 | goto bad; | |
169 | } | |
170 | ||
171 | /* | |
172 | * Process options and, if not destined for us, | |
173 | * ship it on. ip_dooptions returns 1 when an | |
174 | * error was detected (causing an icmp message | |
175 | * to be sent and the original packet to be freed). | |
176 | */ | |
177 | /* We do no IP options */ | |
178 | /* if (hlen > sizeof (struct ip) && ip_dooptions(m)) | |
179 | * goto next; | |
180 | */ | |
181 | /* | |
182 | * If offset or IP_MF are set, must reassemble. | |
183 | * Otherwise, nothing need be done. | |
184 | * (We could look in the reassembly queue to see | |
185 | * if the packet was previously fragmented, | |
186 | * but it's not worth the time; just let them time out.) | |
5fafdf24 | 187 | * |
f0cbd3ec FB |
188 | * XXX This should fail, don't fragment yet |
189 | */ | |
190 | if (ip->ip_off &~ IP_DF) { | |
191 | register struct ipq *fp; | |
429d0a3d | 192 | struct qlink *l; |
f0cbd3ec FB |
193 | /* |
194 | * Look for queue of fragments | |
195 | * of this datagram. | |
196 | */ | |
429d0a3d BS |
197 | for (l = ipq.ip_link.next; l != &ipq.ip_link; l = l->next) { |
198 | fp = container_of(l, struct ipq, ip_link); | |
199 | if (ip->ip_id == fp->ipq_id && | |
200 | ip->ip_src.s_addr == fp->ipq_src.s_addr && | |
201 | ip->ip_dst.s_addr == fp->ipq_dst.s_addr && | |
202 | ip->ip_p == fp->ipq_p) | |
f0cbd3ec | 203 | goto found; |
429d0a3d BS |
204 | } |
205 | fp = NULL; | |
f0cbd3ec FB |
206 | found: |
207 | ||
208 | /* | |
209 | * Adjust ip_len to not reflect header, | |
210 | * set ip_mff if more fragments are expected, | |
211 | * convert offset of this to bytes. | |
212 | */ | |
213 | ip->ip_len -= hlen; | |
214 | if (ip->ip_off & IP_MF) | |
429d0a3d | 215 | ip->ip_tos |= 1; |
5fafdf24 | 216 | else |
429d0a3d | 217 | ip->ip_tos &= ~1; |
f0cbd3ec FB |
218 | |
219 | ip->ip_off <<= 3; | |
220 | ||
221 | /* | |
222 | * If datagram marked as having more fragments | |
223 | * or if this is not the first fragment, | |
224 | * attempt reassembly; if it succeeds, proceed. | |
225 | */ | |
429d0a3d | 226 | if (ip->ip_tos & 1 || ip->ip_off) { |
31a60e22 | 227 | STAT(ipstat.ips_fragments++); |
429d0a3d | 228 | ip = ip_reass(ip, fp); |
f0cbd3ec FB |
229 | if (ip == 0) |
230 | return; | |
31a60e22 | 231 | STAT(ipstat.ips_reassembled++); |
f0cbd3ec FB |
232 | m = dtom(ip); |
233 | } else | |
234 | if (fp) | |
235 | ip_freef(fp); | |
236 | ||
237 | } else | |
238 | ip->ip_len -= hlen; | |
239 | ||
240 | /* | |
241 | * Switch out to protocol's input routine. | |
242 | */ | |
31a60e22 | 243 | STAT(ipstat.ips_delivered++); |
f0cbd3ec FB |
244 | switch (ip->ip_p) { |
245 | case IPPROTO_TCP: | |
246 | tcp_input(m, hlen, (struct socket *)NULL); | |
247 | break; | |
248 | case IPPROTO_UDP: | |
249 | udp_input(m, hlen); | |
250 | break; | |
251 | case IPPROTO_ICMP: | |
252 | icmp_input(m, hlen); | |
253 | break; | |
254 | default: | |
31a60e22 | 255 | STAT(ipstat.ips_noproto++); |
f0cbd3ec FB |
256 | m_free(m); |
257 | } | |
258 | return; | |
259 | bad: | |
260 | m_freem(m); | |
261 | return; | |
262 | } | |
263 | ||
429d0a3d BS |
264 | #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink))) |
265 | #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink))) | |
f0cbd3ec FB |
266 | /* |
267 | * Take incoming datagram fragment and try to | |
268 | * reassemble it into whole datagram. If a chain for | |
269 | * reassembly of this datagram already exists, then it | |
270 | * is given as fp; otherwise have to make a chain. | |
271 | */ | |
9634d903 | 272 | static struct ip * |
429d0a3d | 273 | ip_reass(register struct ip *ip, register struct ipq *fp) |
f0cbd3ec FB |
274 | { |
275 | register struct mbuf *m = dtom(ip); | |
276 | register struct ipasfrag *q; | |
277 | int hlen = ip->ip_hl << 2; | |
278 | int i, next; | |
5fafdf24 | 279 | |
f0cbd3ec FB |
280 | DEBUG_CALL("ip_reass"); |
281 | DEBUG_ARG("ip = %lx", (long)ip); | |
282 | DEBUG_ARG("fp = %lx", (long)fp); | |
283 | DEBUG_ARG("m = %lx", (long)m); | |
284 | ||
285 | /* | |
286 | * Presence of header sizes in mbufs | |
287 | * would confuse code below. | |
288 | * Fragment m_data is concatenated. | |
289 | */ | |
290 | m->m_data += hlen; | |
291 | m->m_len -= hlen; | |
292 | ||
293 | /* | |
294 | * If first fragment to arrive, create a reassembly queue. | |
295 | */ | |
296 | if (fp == 0) { | |
297 | struct mbuf *t; | |
298 | if ((t = m_get()) == NULL) goto dropfrag; | |
299 | fp = mtod(t, struct ipq *); | |
429d0a3d | 300 | insque(&fp->ip_link, &ipq.ip_link); |
f0cbd3ec FB |
301 | fp->ipq_ttl = IPFRAGTTL; |
302 | fp->ipq_p = ip->ip_p; | |
303 | fp->ipq_id = ip->ip_id; | |
429d0a3d BS |
304 | fp->frag_link.next = fp->frag_link.prev = &fp->frag_link; |
305 | fp->ipq_src = ip->ip_src; | |
306 | fp->ipq_dst = ip->ip_dst; | |
f0cbd3ec FB |
307 | q = (struct ipasfrag *)fp; |
308 | goto insert; | |
309 | } | |
5fafdf24 | 310 | |
f0cbd3ec FB |
311 | /* |
312 | * Find a segment which begins after this one does. | |
313 | */ | |
429d0a3d BS |
314 | for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link; |
315 | q = q->ipf_next) | |
316 | if (q->ipf_off > ip->ip_off) | |
f0cbd3ec FB |
317 | break; |
318 | ||
319 | /* | |
320 | * If there is a preceding segment, it may provide some of | |
321 | * our data already. If so, drop the data from the incoming | |
322 | * segment. If it provides all of our data, drop us. | |
323 | */ | |
429d0a3d BS |
324 | if (q->ipf_prev != &fp->frag_link) { |
325 | struct ipasfrag *pq = q->ipf_prev; | |
326 | i = pq->ipf_off + pq->ipf_len - ip->ip_off; | |
f0cbd3ec FB |
327 | if (i > 0) { |
328 | if (i >= ip->ip_len) | |
329 | goto dropfrag; | |
330 | m_adj(dtom(ip), i); | |
331 | ip->ip_off += i; | |
332 | ip->ip_len -= i; | |
333 | } | |
334 | } | |
335 | ||
336 | /* | |
337 | * While we overlap succeeding segments trim them or, | |
338 | * if they are completely covered, dequeue them. | |
339 | */ | |
429d0a3d BS |
340 | while (q != (struct ipasfrag*)&fp->frag_link && |
341 | ip->ip_off + ip->ip_len > q->ipf_off) { | |
342 | i = (ip->ip_off + ip->ip_len) - q->ipf_off; | |
343 | if (i < q->ipf_len) { | |
344 | q->ipf_len -= i; | |
345 | q->ipf_off += i; | |
f0cbd3ec FB |
346 | m_adj(dtom(q), i); |
347 | break; | |
348 | } | |
429d0a3d BS |
349 | q = q->ipf_next; |
350 | m_freem(dtom(q->ipf_prev)); | |
351 | ip_deq(q->ipf_prev); | |
f0cbd3ec FB |
352 | } |
353 | ||
354 | insert: | |
355 | /* | |
356 | * Stick new segment in its place; | |
357 | * check for complete reassembly. | |
358 | */ | |
429d0a3d | 359 | ip_enq(iptofrag(ip), q->ipf_prev); |
f0cbd3ec | 360 | next = 0; |
429d0a3d BS |
361 | for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; |
362 | q = q->ipf_next) { | |
363 | if (q->ipf_off != next) | |
f0cbd3ec | 364 | return (0); |
429d0a3d | 365 | next += q->ipf_len; |
f0cbd3ec | 366 | } |
429d0a3d | 367 | if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1) |
f0cbd3ec FB |
368 | return (0); |
369 | ||
370 | /* | |
371 | * Reassembly is complete; concatenate fragments. | |
372 | */ | |
429d0a3d | 373 | q = fp->frag_link.next; |
f0cbd3ec FB |
374 | m = dtom(q); |
375 | ||
376 | q = (struct ipasfrag *) q->ipf_next; | |
429d0a3d BS |
377 | while (q != (struct ipasfrag*)&fp->frag_link) { |
378 | struct mbuf *t = dtom(q); | |
f0cbd3ec | 379 | q = (struct ipasfrag *) q->ipf_next; |
fedc54ad | 380 | m_cat(m, t); |
f0cbd3ec FB |
381 | } |
382 | ||
383 | /* | |
384 | * Create header for new ip packet by | |
385 | * modifying header of first packet; | |
386 | * dequeue and discard fragment reassembly header. | |
387 | * Make header visible. | |
388 | */ | |
429d0a3d | 389 | q = fp->frag_link.next; |
f0cbd3ec FB |
390 | |
391 | /* | |
392 | * If the fragments concatenated to an mbuf that's | |
393 | * bigger than the total size of the fragment, then and | |
394 | * m_ext buffer was alloced. But fp->ipq_next points to | |
395 | * the old buffer (in the mbuf), so we must point ip | |
396 | * into the new buffer. | |
397 | */ | |
398 | if (m->m_flags & M_EXT) { | |
399 | int delta; | |
400 | delta = (char *)ip - m->m_dat; | |
429d0a3d | 401 | q = (struct ipasfrag *)(m->m_ext + delta); |
f0cbd3ec FB |
402 | } |
403 | ||
5fafdf24 | 404 | /* DEBUG_ARG("ip = %lx", (long)ip); |
f0cbd3ec FB |
405 | * ip=(struct ipasfrag *)m->m_data; */ |
406 | ||
429d0a3d | 407 | ip = fragtoip(q); |
f0cbd3ec | 408 | ip->ip_len = next; |
429d0a3d BS |
409 | ip->ip_tos &= ~1; |
410 | ip->ip_src = fp->ipq_src; | |
411 | ip->ip_dst = fp->ipq_dst; | |
412 | remque(&fp->ip_link); | |
f0cbd3ec | 413 | (void) m_free(dtom(fp)); |
f0cbd3ec FB |
414 | m->m_len += (ip->ip_hl << 2); |
415 | m->m_data -= (ip->ip_hl << 2); | |
416 | ||
429d0a3d | 417 | return ip; |
f0cbd3ec FB |
418 | |
419 | dropfrag: | |
31a60e22 | 420 | STAT(ipstat.ips_fragdropped++); |
f0cbd3ec FB |
421 | m_freem(m); |
422 | return (0); | |
423 | } | |
424 | ||
425 | /* | |
426 | * Free a fragment reassembly header and all | |
427 | * associated datagrams. | |
428 | */ | |
9634d903 BS |
429 | static void |
430 | ip_freef(struct ipq *fp) | |
f0cbd3ec FB |
431 | { |
432 | register struct ipasfrag *q, *p; | |
433 | ||
429d0a3d BS |
434 | for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) { |
435 | p = q->ipf_next; | |
f0cbd3ec FB |
436 | ip_deq(q); |
437 | m_freem(dtom(q)); | |
438 | } | |
429d0a3d | 439 | remque(&fp->ip_link); |
f0cbd3ec FB |
440 | (void) m_free(dtom(fp)); |
441 | } | |
442 | ||
443 | /* | |
444 | * Put an ip fragment on a reassembly chain. | |
445 | * Like insque, but pointers in middle of structure. | |
446 | */ | |
9634d903 BS |
447 | static void |
448 | ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev) | |
f0cbd3ec FB |
449 | { |
450 | DEBUG_CALL("ip_enq"); | |
451 | DEBUG_ARG("prev = %lx", (long)prev); | |
429d0a3d | 452 | p->ipf_prev = prev; |
f0cbd3ec | 453 | p->ipf_next = prev->ipf_next; |
429d0a3d BS |
454 | ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p; |
455 | prev->ipf_next = p; | |
f0cbd3ec FB |
456 | } |
457 | ||
458 | /* | |
459 | * To ip_enq as remque is to insque. | |
460 | */ | |
9634d903 BS |
461 | static void |
462 | ip_deq(register struct ipasfrag *p) | |
f0cbd3ec FB |
463 | { |
464 | ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next; | |
465 | ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev; | |
466 | } | |
467 | ||
468 | /* | |
469 | * IP timer processing; | |
470 | * if a timer expires on a reassembly | |
471 | * queue, discard it. | |
472 | */ | |
473 | void | |
474 | ip_slowtimo() | |
475 | { | |
429d0a3d | 476 | struct qlink *l; |
5fafdf24 | 477 | |
f0cbd3ec | 478 | DEBUG_CALL("ip_slowtimo"); |
5fafdf24 | 479 | |
429d0a3d BS |
480 | l = ipq.ip_link.next; |
481 | ||
482 | if (l == 0) | |
f0cbd3ec FB |
483 | return; |
484 | ||
429d0a3d BS |
485 | while (l != &ipq.ip_link) { |
486 | struct ipq *fp = container_of(l, struct ipq, ip_link); | |
487 | l = l->next; | |
488 | if (--fp->ipq_ttl == 0) { | |
31a60e22 | 489 | STAT(ipstat.ips_fragtimeout++); |
429d0a3d | 490 | ip_freef(fp); |
f0cbd3ec FB |
491 | } |
492 | } | |
493 | } | |
494 | ||
495 | /* | |
496 | * Do option processing on a datagram, | |
497 | * possibly discarding it if bad options are encountered, | |
498 | * or forwarding it if source-routed. | |
499 | * Returns 1 if packet has been forwarded/freed, | |
500 | * 0 if the packet should be processed further. | |
501 | */ | |
502 | ||
503 | #ifdef notdef | |
504 | ||
505 | int | |
506 | ip_dooptions(m) | |
507 | struct mbuf *m; | |
508 | { | |
509 | register struct ip *ip = mtod(m, struct ip *); | |
510 | register u_char *cp; | |
511 | register struct ip_timestamp *ipt; | |
512 | register struct in_ifaddr *ia; | |
513 | /* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */ | |
514 | int opt, optlen, cnt, off, code, type, forward = 0; | |
515 | struct in_addr *sin, dst; | |
516 | typedef u_int32_t n_time; | |
517 | n_time ntime; | |
518 | ||
519 | dst = ip->ip_dst; | |
520 | cp = (u_char *)(ip + 1); | |
521 | cnt = (ip->ip_hl << 2) - sizeof (struct ip); | |
522 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
523 | opt = cp[IPOPT_OPTVAL]; | |
524 | if (opt == IPOPT_EOL) | |
525 | break; | |
526 | if (opt == IPOPT_NOP) | |
527 | optlen = 1; | |
528 | else { | |
529 | optlen = cp[IPOPT_OLEN]; | |
530 | if (optlen <= 0 || optlen > cnt) { | |
531 | code = &cp[IPOPT_OLEN] - (u_char *)ip; | |
532 | goto bad; | |
533 | } | |
534 | } | |
535 | switch (opt) { | |
536 | ||
537 | default: | |
538 | break; | |
539 | ||
540 | /* | |
541 | * Source routing with record. | |
542 | * Find interface with current destination address. | |
543 | * If none on this machine then drop if strictly routed, | |
544 | * or do nothing if loosely routed. | |
545 | * Record interface address and bring up next address | |
546 | * component. If strictly routed make sure next | |
547 | * address is on directly accessible net. | |
548 | */ | |
549 | case IPOPT_LSRR: | |
550 | case IPOPT_SSRR: | |
551 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { | |
552 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
553 | goto bad; | |
554 | } | |
555 | ipaddr.sin_addr = ip->ip_dst; | |
556 | ia = (struct in_ifaddr *) | |
557 | ifa_ifwithaddr((struct sockaddr *)&ipaddr); | |
558 | if (ia == 0) { | |
559 | if (opt == IPOPT_SSRR) { | |
560 | type = ICMP_UNREACH; | |
561 | code = ICMP_UNREACH_SRCFAIL; | |
562 | goto bad; | |
563 | } | |
564 | /* | |
565 | * Loose routing, and not at next destination | |
566 | * yet; nothing to do except forward. | |
567 | */ | |
568 | break; | |
569 | } | |
570 | off--; / * 0 origin * / | |
571 | if (off > optlen - sizeof(struct in_addr)) { | |
572 | /* | |
573 | * End of source route. Should be for us. | |
574 | */ | |
575 | save_rte(cp, ip->ip_src); | |
576 | break; | |
577 | } | |
578 | /* | |
579 | * locate outgoing interface | |
580 | */ | |
581 | bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, | |
582 | sizeof(ipaddr.sin_addr)); | |
583 | if (opt == IPOPT_SSRR) { | |
584 | #define INA struct in_ifaddr * | |
585 | #define SA struct sockaddr * | |
586 | if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) | |
587 | ia = (INA)ifa_ifwithnet((SA)&ipaddr); | |
588 | } else | |
589 | ia = ip_rtaddr(ipaddr.sin_addr); | |
590 | if (ia == 0) { | |
591 | type = ICMP_UNREACH; | |
592 | code = ICMP_UNREACH_SRCFAIL; | |
593 | goto bad; | |
594 | } | |
595 | ip->ip_dst = ipaddr.sin_addr; | |
596 | bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), | |
597 | (caddr_t)(cp + off), sizeof(struct in_addr)); | |
598 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); | |
599 | /* | |
600 | * Let ip_intr's mcast routing check handle mcast pkts | |
601 | */ | |
602 | forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); | |
603 | break; | |
604 | ||
605 | case IPOPT_RR: | |
606 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { | |
607 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
608 | goto bad; | |
609 | } | |
610 | /* | |
611 | * If no space remains, ignore. | |
612 | */ | |
613 | off--; * 0 origin * | |
614 | if (off > optlen - sizeof(struct in_addr)) | |
615 | break; | |
616 | bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, | |
617 | sizeof(ipaddr.sin_addr)); | |
618 | /* | |
619 | * locate outgoing interface; if we're the destination, | |
620 | * use the incoming interface (should be same). | |
621 | */ | |
622 | if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && | |
623 | (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { | |
624 | type = ICMP_UNREACH; | |
625 | code = ICMP_UNREACH_HOST; | |
626 | goto bad; | |
627 | } | |
628 | bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), | |
629 | (caddr_t)(cp + off), sizeof(struct in_addr)); | |
630 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); | |
631 | break; | |
632 | ||
633 | case IPOPT_TS: | |
634 | code = cp - (u_char *)ip; | |
635 | ipt = (struct ip_timestamp *)cp; | |
636 | if (ipt->ipt_len < 5) | |
637 | goto bad; | |
638 | if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) { | |
639 | if (++ipt->ipt_oflw == 0) | |
640 | goto bad; | |
641 | break; | |
642 | } | |
643 | sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); | |
644 | switch (ipt->ipt_flg) { | |
645 | ||
646 | case IPOPT_TS_TSONLY: | |
647 | break; | |
648 | ||
649 | case IPOPT_TS_TSANDADDR: | |
650 | if (ipt->ipt_ptr + sizeof(n_time) + | |
651 | sizeof(struct in_addr) > ipt->ipt_len) | |
652 | goto bad; | |
653 | ipaddr.sin_addr = dst; | |
654 | ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr, | |
655 | m->m_pkthdr.rcvif); | |
656 | if (ia == 0) | |
657 | continue; | |
658 | bcopy((caddr_t)&IA_SIN(ia)->sin_addr, | |
659 | (caddr_t)sin, sizeof(struct in_addr)); | |
660 | ipt->ipt_ptr += sizeof(struct in_addr); | |
661 | break; | |
662 | ||
663 | case IPOPT_TS_PRESPEC: | |
664 | if (ipt->ipt_ptr + sizeof(n_time) + | |
665 | sizeof(struct in_addr) > ipt->ipt_len) | |
666 | goto bad; | |
667 | bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, | |
668 | sizeof(struct in_addr)); | |
669 | if (ifa_ifwithaddr((SA)&ipaddr) == 0) | |
670 | continue; | |
671 | ipt->ipt_ptr += sizeof(struct in_addr); | |
672 | break; | |
673 | ||
674 | default: | |
675 | goto bad; | |
676 | } | |
677 | ntime = iptime(); | |
678 | bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, | |
679 | sizeof(n_time)); | |
680 | ipt->ipt_ptr += sizeof(n_time); | |
681 | } | |
682 | } | |
683 | if (forward) { | |
684 | ip_forward(m, 1); | |
685 | return (1); | |
686 | } | |
687 | } | |
688 | } | |
689 | return (0); | |
690 | bad: | |
691 | /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */ | |
692 | ||
693 | /* Not yet */ | |
694 | icmp_error(m, type, code, 0, 0); | |
695 | ||
31a60e22 | 696 | STAT(ipstat.ips_badoptions++); |
f0cbd3ec FB |
697 | return (1); |
698 | } | |
699 | ||
700 | #endif /* notdef */ | |
701 | ||
702 | /* | |
703 | * Strip out IP options, at higher | |
704 | * level protocol in the kernel. | |
705 | * Second argument is buffer to which options | |
706 | * will be moved, and return value is their length. | |
707 | * (XXX) should be deleted; last arg currently ignored. | |
708 | */ | |
709 | void | |
710 | ip_stripoptions(m, mopt) | |
711 | register struct mbuf *m; | |
712 | struct mbuf *mopt; | |
713 | { | |
714 | register int i; | |
715 | struct ip *ip = mtod(m, struct ip *); | |
716 | register caddr_t opts; | |
717 | int olen; | |
718 | ||
719 | olen = (ip->ip_hl<<2) - sizeof (struct ip); | |
720 | opts = (caddr_t)(ip + 1); | |
721 | i = m->m_len - (sizeof (struct ip) + olen); | |
722 | memcpy(opts, opts + olen, (unsigned)i); | |
723 | m->m_len -= olen; | |
5fafdf24 | 724 | |
f0cbd3ec FB |
725 | ip->ip_hl = sizeof(struct ip) >> 2; |
726 | } |