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