Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. | |
3 | * 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 | * | |
38e82238 | 33 | * from: @(#)ip_output.c 7.23 (Berkeley) 11/12/90 |
fde1aeb2 | 34 | * $Id: ip_output.c,v 1.4 1993/11/25 01:35:09 wollman Exp $ |
15637ed4 RG |
35 | */ |
36 | ||
37 | #include "param.h" | |
fde1aeb2 | 38 | #include "systm.h" |
15637ed4 RG |
39 | #include "malloc.h" |
40 | #include "mbuf.h" | |
41 | #include "errno.h" | |
42 | #include "protosw.h" | |
43 | #include "socket.h" | |
44 | #include "socketvar.h" | |
45 | ||
46 | #include "../net/if.h" | |
47 | #include "../net/route.h" | |
48 | ||
49 | #include "in.h" | |
50 | #include "in_systm.h" | |
51 | #include "ip.h" | |
52 | #include "in_pcb.h" | |
53 | #include "in_var.h" | |
54 | #include "ip_var.h" | |
55 | ||
56 | #ifdef vax | |
57 | #include "machine/mtpr.h" | |
58 | #endif | |
59 | ||
60 | struct mbuf *ip_insertoptions(); | |
61 | ||
62 | /* | |
63 | * IP output. The packet in mbuf chain m contains a skeletal IP | |
64 | * header (with len, off, ttl, proto, tos, src, dst). | |
65 | * The mbuf chain containing the packet will be freed. | |
66 | * The mbuf opt, if present, will not be freed. | |
67 | */ | |
4c45483e | 68 | int |
15637ed4 RG |
69 | ip_output(m0, opt, ro, flags) |
70 | struct mbuf *m0; | |
71 | struct mbuf *opt; | |
72 | struct route *ro; | |
73 | int flags; | |
74 | { | |
75 | register struct ip *ip, *mhip; | |
76 | register struct ifnet *ifp; | |
77 | register struct mbuf *m = m0; | |
78 | register int hlen = sizeof (struct ip); | |
79 | int len, off, error = 0; | |
80 | struct route iproute; | |
81 | struct sockaddr_in *dst; | |
82 | struct in_ifaddr *ia; | |
83 | ||
84 | #ifdef DIAGNOSTIC | |
85 | if ((m->m_flags & M_PKTHDR) == 0) | |
86 | panic("ip_output no HDR"); | |
87 | #endif | |
88 | if (opt) { | |
89 | m = ip_insertoptions(m, opt, &len); | |
90 | hlen = len; | |
91 | } | |
92 | ip = mtod(m, struct ip *); | |
93 | /* | |
94 | * Fill in IP header. | |
95 | */ | |
96 | if ((flags & IP_FORWARDING) == 0) { | |
97 | ip->ip_v = IPVERSION; | |
98 | ip->ip_off &= IP_DF; | |
99 | ip->ip_id = htons(ip_id++); | |
100 | ip->ip_hl = hlen >> 2; | |
101 | } else { | |
102 | hlen = ip->ip_hl << 2; | |
103 | ipstat.ips_localout++; | |
104 | } | |
105 | /* | |
106 | * Route packet. | |
107 | */ | |
108 | if (ro == 0) { | |
109 | ro = &iproute; | |
110 | bzero((caddr_t)ro, sizeof (*ro)); | |
111 | } | |
112 | dst = (struct sockaddr_in *)&ro->ro_dst; | |
113 | /* | |
114 | * If there is a cached route, | |
115 | * check that it is to the same destination | |
116 | * and is still up. If not, free it and try again. | |
117 | */ | |
118 | if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || | |
119 | dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { | |
120 | RTFREE(ro->ro_rt); | |
121 | ro->ro_rt = (struct rtentry *)0; | |
122 | } | |
123 | if (ro->ro_rt == 0) { | |
124 | dst->sin_family = AF_INET; | |
125 | dst->sin_len = sizeof(*dst); | |
126 | dst->sin_addr = ip->ip_dst; | |
127 | } | |
128 | /* | |
129 | * If routing to interface only, | |
130 | * short circuit routing lookup. | |
131 | */ | |
132 | if (flags & IP_ROUTETOIF) { | |
133 | ||
134 | ia = (struct in_ifaddr *)ifa_ifwithdstaddr((struct sockaddr *)dst); | |
135 | if (ia == 0) | |
136 | ia = in_iaonnetof(in_netof(ip->ip_dst)); | |
137 | if (ia == 0) { | |
138 | error = ENETUNREACH; | |
139 | goto bad; | |
140 | } | |
141 | ifp = ia->ia_ifp; | |
142 | } else { | |
143 | if (ro->ro_rt == 0) | |
144 | rtalloc(ro); | |
145 | if (ro->ro_rt == 0) { | |
146 | error = EHOSTUNREACH; | |
147 | goto bad; | |
148 | } | |
149 | ia = (struct in_ifaddr *)ro->ro_rt->rt_ifa; | |
150 | ifp = ro->ro_rt->rt_ifp; | |
151 | ro->ro_rt->rt_use++; | |
152 | if (ro->ro_rt->rt_flags & RTF_GATEWAY) | |
153 | dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; | |
154 | } | |
155 | #ifndef notdef | |
156 | /* | |
157 | * If source address not specified yet, use address | |
158 | * of outgoing interface. | |
159 | */ | |
160 | if (ip->ip_src.s_addr == INADDR_ANY) | |
161 | ip->ip_src = IA_SIN(ia)->sin_addr; | |
162 | #endif | |
b8d8ba78 DG |
163 | |
164 | /* | |
165 | * Verify that we have any chance at all of being able to queue | |
166 | * the packet or packet fragments | |
167 | */ | |
168 | if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= | |
169 | ifp->if_snd.ifq_maxlen) { | |
170 | error = ENOBUFS; | |
171 | goto bad; | |
172 | } | |
173 | ||
15637ed4 RG |
174 | /* |
175 | * Look for broadcast address and | |
176 | * and verify user is allowed to send | |
177 | * such a packet. | |
178 | */ | |
179 | if (in_broadcast(dst->sin_addr)) { | |
180 | if ((ifp->if_flags & IFF_BROADCAST) == 0) { | |
181 | error = EADDRNOTAVAIL; | |
182 | goto bad; | |
183 | } | |
184 | if ((flags & IP_ALLOWBROADCAST) == 0) { | |
185 | error = EACCES; | |
186 | goto bad; | |
187 | } | |
188 | /* don't allow broadcast messages to be fragmented */ | |
189 | if ((u_short)ip->ip_len > ifp->if_mtu) { | |
190 | error = EMSGSIZE; | |
191 | goto bad; | |
192 | } | |
193 | m->m_flags |= M_BCAST; | |
194 | } | |
195 | ||
196 | /* | |
197 | * If small enough for interface, can just send directly. | |
198 | */ | |
199 | if ((u_short)ip->ip_len <= ifp->if_mtu) { | |
200 | ip->ip_len = htons((u_short)ip->ip_len); | |
201 | ip->ip_off = htons((u_short)ip->ip_off); | |
202 | ip->ip_sum = 0; | |
203 | ip->ip_sum = in_cksum(m, hlen); | |
204 | error = (*ifp->if_output)(ifp, m, | |
205 | (struct sockaddr *)dst, ro->ro_rt); | |
206 | goto done; | |
207 | } | |
208 | ipstat.ips_fragmented++; | |
209 | /* | |
210 | * Too large for interface; fragment if possible. | |
211 | * Must be able to put at least 8 bytes per fragment. | |
212 | */ | |
213 | if (ip->ip_off & IP_DF) { | |
214 | error = EMSGSIZE; | |
215 | goto bad; | |
216 | } | |
217 | len = (ifp->if_mtu - hlen) &~ 7; | |
218 | if (len < 8) { | |
219 | error = EMSGSIZE; | |
220 | goto bad; | |
221 | } | |
222 | ||
223 | { | |
224 | int mhlen, firstlen = len; | |
225 | struct mbuf **mnext = &m->m_nextpkt; | |
226 | ||
227 | /* | |
228 | * Loop through length of segment after first fragment, | |
229 | * make new header and copy data of each part and link onto chain. | |
230 | */ | |
231 | m0 = m; | |
232 | mhlen = sizeof (struct ip); | |
233 | for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { | |
234 | MGETHDR(m, M_DONTWAIT, MT_HEADER); | |
235 | if (m == 0) { | |
236 | error = ENOBUFS; | |
237 | goto sendorfree; | |
238 | } | |
239 | m->m_data += max_linkhdr; | |
240 | mhip = mtod(m, struct ip *); | |
241 | *mhip = *ip; | |
242 | if (hlen > sizeof (struct ip)) { | |
243 | mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); | |
244 | mhip->ip_hl = mhlen >> 2; | |
245 | } | |
246 | m->m_len = mhlen; | |
247 | mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); | |
248 | if (ip->ip_off & IP_MF) | |
249 | mhip->ip_off |= IP_MF; | |
250 | if (off + len >= (u_short)ip->ip_len) | |
251 | len = (u_short)ip->ip_len - off; | |
252 | else | |
253 | mhip->ip_off |= IP_MF; | |
254 | mhip->ip_len = htons((u_short)(len + mhlen)); | |
255 | m->m_next = m_copy(m0, off, len); | |
256 | if (m->m_next == 0) { | |
257 | error = ENOBUFS; /* ??? */ | |
258 | goto sendorfree; | |
259 | } | |
260 | m->m_pkthdr.len = mhlen + len; | |
261 | m->m_pkthdr.rcvif = (struct ifnet *)0; | |
262 | mhip->ip_off = htons((u_short)mhip->ip_off); | |
263 | mhip->ip_sum = 0; | |
264 | mhip->ip_sum = in_cksum(m, mhlen); | |
265 | *mnext = m; | |
266 | mnext = &m->m_nextpkt; | |
267 | ipstat.ips_ofragments++; | |
268 | } | |
269 | /* | |
270 | * Update first fragment by trimming what's been copied out | |
271 | * and updating header, then send each fragment (in order). | |
272 | */ | |
273 | m = m0; | |
274 | m_adj(m, hlen + firstlen - (u_short)ip->ip_len); | |
275 | m->m_pkthdr.len = hlen + firstlen; | |
276 | ip->ip_len = htons((u_short)m->m_pkthdr.len); | |
277 | ip->ip_off = htons((u_short)(ip->ip_off | IP_MF)); | |
278 | ip->ip_sum = 0; | |
279 | ip->ip_sum = in_cksum(m, hlen); | |
280 | sendorfree: | |
281 | for (m = m0; m; m = m0) { | |
282 | m0 = m->m_nextpkt; | |
283 | m->m_nextpkt = 0; | |
284 | if (error == 0) | |
285 | error = (*ifp->if_output)(ifp, m, | |
286 | (struct sockaddr *)dst, ro->ro_rt); | |
287 | else | |
288 | m_freem(m); | |
289 | } | |
290 | } | |
291 | done: | |
292 | if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) | |
293 | RTFREE(ro->ro_rt); | |
294 | return (error); | |
295 | bad: | |
296 | m_freem(m0); | |
297 | goto done; | |
298 | } | |
299 | ||
300 | /* | |
301 | * Insert IP options into preformed packet. | |
302 | * Adjust IP destination as required for IP source routing, | |
303 | * as indicated by a non-zero in_addr at the start of the options. | |
304 | */ | |
305 | struct mbuf * | |
306 | ip_insertoptions(m, opt, phlen) | |
307 | register struct mbuf *m; | |
308 | struct mbuf *opt; | |
309 | int *phlen; | |
310 | { | |
311 | register struct ipoption *p = mtod(opt, struct ipoption *); | |
312 | struct mbuf *n; | |
313 | register struct ip *ip = mtod(m, struct ip *); | |
314 | unsigned optlen; | |
315 | ||
316 | optlen = opt->m_len - sizeof(p->ipopt_dst); | |
317 | if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) | |
318 | return (m); /* XXX should fail */ | |
319 | if (p->ipopt_dst.s_addr) | |
320 | ip->ip_dst = p->ipopt_dst; | |
321 | if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { | |
322 | MGETHDR(n, M_DONTWAIT, MT_HEADER); | |
323 | if (n == 0) | |
324 | return (m); | |
325 | n->m_pkthdr.len = m->m_pkthdr.len + optlen; | |
326 | m->m_len -= sizeof(struct ip); | |
327 | m->m_data += sizeof(struct ip); | |
328 | n->m_next = m; | |
329 | m = n; | |
330 | m->m_len = optlen + sizeof(struct ip); | |
331 | m->m_data += max_linkhdr; | |
332 | bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); | |
333 | } else { | |
334 | m->m_data -= optlen; | |
335 | m->m_len += optlen; | |
336 | m->m_pkthdr.len += optlen; | |
337 | ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); | |
338 | } | |
339 | ip = mtod(m, struct ip *); | |
340 | bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen); | |
341 | *phlen = sizeof(struct ip) + optlen; | |
342 | ip->ip_len += optlen; | |
343 | return (m); | |
344 | } | |
345 | ||
346 | /* | |
347 | * Copy options from ip to jp, | |
348 | * omitting those not copied during fragmentation. | |
349 | */ | |
4c45483e | 350 | int |
15637ed4 RG |
351 | ip_optcopy(ip, jp) |
352 | struct ip *ip, *jp; | |
353 | { | |
354 | register u_char *cp, *dp; | |
355 | int opt, optlen, cnt; | |
356 | ||
357 | cp = (u_char *)(ip + 1); | |
358 | dp = (u_char *)(jp + 1); | |
359 | cnt = (ip->ip_hl << 2) - sizeof (struct ip); | |
360 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
361 | opt = cp[0]; | |
362 | if (opt == IPOPT_EOL) | |
363 | break; | |
364 | if (opt == IPOPT_NOP) | |
365 | optlen = 1; | |
366 | else | |
367 | optlen = cp[IPOPT_OLEN]; | |
368 | /* bogus lengths should have been caught by ip_dooptions */ | |
369 | if (optlen > cnt) | |
370 | optlen = cnt; | |
371 | if (IPOPT_COPIED(opt)) { | |
372 | bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); | |
373 | dp += optlen; | |
374 | } | |
375 | } | |
376 | for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) | |
377 | *dp++ = IPOPT_EOL; | |
378 | return (optlen); | |
379 | } | |
380 | ||
381 | /* | |
382 | * IP socket option processing. | |
383 | */ | |
4c45483e | 384 | int |
15637ed4 RG |
385 | ip_ctloutput(op, so, level, optname, mp) |
386 | int op; | |
387 | struct socket *so; | |
388 | int level, optname; | |
389 | struct mbuf **mp; | |
390 | { | |
391 | register struct inpcb *inp = sotoinpcb(so); | |
392 | register struct mbuf *m = *mp; | |
4c45483e | 393 | register int optval = 0; |
15637ed4 RG |
394 | int error = 0; |
395 | ||
396 | if (level != IPPROTO_IP) | |
397 | error = EINVAL; | |
398 | else switch (op) { | |
399 | ||
400 | case PRCO_SETOPT: | |
401 | switch (optname) { | |
402 | case IP_OPTIONS: | |
403 | #ifdef notyet | |
404 | case IP_RETOPTS: | |
405 | return (ip_pcbopts(optname, &inp->inp_options, m)); | |
406 | #else | |
407 | return (ip_pcbopts(&inp->inp_options, m)); | |
408 | #endif | |
409 | ||
410 | case IP_TOS: | |
411 | case IP_TTL: | |
412 | case IP_RECVOPTS: | |
413 | case IP_RECVRETOPTS: | |
414 | case IP_RECVDSTADDR: | |
415 | if (m->m_len != sizeof(int)) | |
416 | error = EINVAL; | |
417 | else { | |
418 | optval = *mtod(m, int *); | |
419 | switch (optname) { | |
420 | ||
421 | case IP_TOS: | |
422 | inp->inp_ip.ip_tos = optval; | |
423 | break; | |
424 | ||
425 | case IP_TTL: | |
426 | inp->inp_ip.ip_ttl = optval; | |
427 | break; | |
428 | #define OPTSET(bit) \ | |
429 | if (optval) \ | |
430 | inp->inp_flags |= bit; \ | |
431 | else \ | |
432 | inp->inp_flags &= ~bit; | |
433 | ||
434 | case IP_RECVOPTS: | |
435 | OPTSET(INP_RECVOPTS); | |
436 | break; | |
437 | ||
438 | case IP_RECVRETOPTS: | |
439 | OPTSET(INP_RECVRETOPTS); | |
440 | break; | |
441 | ||
442 | case IP_RECVDSTADDR: | |
443 | OPTSET(INP_RECVDSTADDR); | |
444 | break; | |
445 | } | |
446 | } | |
447 | break; | |
448 | #undef OPTSET | |
449 | ||
450 | default: | |
451 | error = EINVAL; | |
452 | break; | |
453 | } | |
454 | if (m) | |
455 | (void)m_free(m); | |
456 | break; | |
457 | ||
458 | case PRCO_GETOPT: | |
459 | switch (optname) { | |
460 | case IP_OPTIONS: | |
461 | case IP_RETOPTS: | |
462 | *mp = m = m_get(M_WAIT, MT_SOOPTS); | |
463 | if (inp->inp_options) { | |
464 | m->m_len = inp->inp_options->m_len; | |
465 | bcopy(mtod(inp->inp_options, caddr_t), | |
466 | mtod(m, caddr_t), (unsigned)m->m_len); | |
467 | } else | |
468 | m->m_len = 0; | |
469 | break; | |
470 | ||
471 | case IP_TOS: | |
472 | case IP_TTL: | |
473 | case IP_RECVOPTS: | |
474 | case IP_RECVRETOPTS: | |
475 | case IP_RECVDSTADDR: | |
476 | *mp = m = m_get(M_WAIT, MT_SOOPTS); | |
477 | m->m_len = sizeof(int); | |
478 | switch (optname) { | |
479 | ||
480 | case IP_TOS: | |
481 | optval = inp->inp_ip.ip_tos; | |
482 | break; | |
483 | ||
484 | case IP_TTL: | |
485 | optval = inp->inp_ip.ip_ttl; | |
486 | break; | |
487 | ||
488 | #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) | |
489 | ||
490 | case IP_RECVOPTS: | |
491 | optval = OPTBIT(INP_RECVOPTS); | |
492 | break; | |
493 | ||
494 | case IP_RECVRETOPTS: | |
495 | optval = OPTBIT(INP_RECVRETOPTS); | |
496 | break; | |
497 | ||
498 | case IP_RECVDSTADDR: | |
499 | optval = OPTBIT(INP_RECVDSTADDR); | |
500 | break; | |
501 | } | |
502 | *mtod(m, int *) = optval; | |
503 | break; | |
504 | ||
505 | default: | |
506 | error = EINVAL; | |
507 | break; | |
508 | } | |
509 | break; | |
510 | } | |
511 | return (error); | |
512 | } | |
513 | ||
514 | /* | |
515 | * Set up IP options in pcb for insertion in output packets. | |
516 | * Store in mbuf with pointer in pcbopt, adding pseudo-option | |
517 | * with destination address if source routed. | |
518 | */ | |
4c45483e | 519 | int |
15637ed4 RG |
520 | #ifdef notyet |
521 | ip_pcbopts(optname, pcbopt, m) | |
522 | int optname; | |
523 | #else | |
524 | ip_pcbopts(pcbopt, m) | |
525 | #endif | |
526 | struct mbuf **pcbopt; | |
527 | register struct mbuf *m; | |
528 | { | |
529 | register cnt, optlen; | |
530 | register u_char *cp; | |
531 | u_char opt; | |
532 | ||
533 | /* turn off any old options */ | |
534 | if (*pcbopt) | |
535 | (void)m_free(*pcbopt); | |
536 | *pcbopt = 0; | |
537 | if (m == (struct mbuf *)0 || m->m_len == 0) { | |
538 | /* | |
539 | * Only turning off any previous options. | |
540 | */ | |
541 | if (m) | |
542 | (void)m_free(m); | |
543 | return (0); | |
544 | } | |
545 | ||
546 | #ifndef vax | |
547 | if (m->m_len % sizeof(long)) | |
548 | goto bad; | |
549 | #endif | |
550 | /* | |
551 | * IP first-hop destination address will be stored before | |
552 | * actual options; move other options back | |
553 | * and clear it when none present. | |
554 | */ | |
555 | if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) | |
556 | goto bad; | |
557 | cnt = m->m_len; | |
558 | m->m_len += sizeof(struct in_addr); | |
559 | cp = mtod(m, u_char *) + sizeof(struct in_addr); | |
560 | ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); | |
561 | bzero(mtod(m, caddr_t), sizeof(struct in_addr)); | |
562 | ||
563 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
564 | opt = cp[IPOPT_OPTVAL]; | |
565 | if (opt == IPOPT_EOL) | |
566 | break; | |
567 | if (opt == IPOPT_NOP) | |
568 | optlen = 1; | |
569 | else { | |
570 | optlen = cp[IPOPT_OLEN]; | |
571 | if (optlen <= IPOPT_OLEN || optlen > cnt) | |
572 | goto bad; | |
573 | } | |
574 | switch (opt) { | |
575 | ||
576 | default: | |
577 | break; | |
578 | ||
579 | case IPOPT_LSRR: | |
580 | case IPOPT_SSRR: | |
581 | /* | |
582 | * user process specifies route as: | |
583 | * ->A->B->C->D | |
584 | * D must be our final destination (but we can't | |
585 | * check that since we may not have connected yet). | |
586 | * A is first hop destination, which doesn't appear in | |
587 | * actual IP option, but is stored before the options. | |
588 | */ | |
589 | if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) | |
590 | goto bad; | |
591 | m->m_len -= sizeof(struct in_addr); | |
592 | cnt -= sizeof(struct in_addr); | |
593 | optlen -= sizeof(struct in_addr); | |
594 | cp[IPOPT_OLEN] = optlen; | |
595 | /* | |
596 | * Move first hop before start of options. | |
597 | */ | |
598 | bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), | |
599 | sizeof(struct in_addr)); | |
600 | /* | |
601 | * Then copy rest of options back | |
602 | * to close up the deleted entry. | |
603 | */ | |
604 | ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + | |
605 | sizeof(struct in_addr)), | |
606 | (caddr_t)&cp[IPOPT_OFFSET+1], | |
607 | (unsigned)cnt + sizeof(struct in_addr)); | |
608 | break; | |
609 | } | |
610 | } | |
611 | if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) | |
612 | goto bad; | |
613 | *pcbopt = m; | |
614 | return (0); | |
615 | ||
616 | bad: | |
617 | (void)m_free(m); | |
618 | return (EINVAL); | |
619 | } |