bca8fb0e04d623732d6f90a3845a1e2884cfdbf8
[reactos.git] / reactos / lib / drivers / oskittcp / oskittcp / ip_output.c
1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990, 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_output.c 8.3 (Berkeley) 1/21/94
34 */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/errno.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/queue.h>
45
46 #include <net/if.h>
47 #include <net/route.h>
48
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/in_pcb.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip_var.h>
55
56 #include <netinet/ip_fw.h>
57
58 #ifdef vax
59 #include <machine/mtpr.h>
60 #endif
61 #include <oskittcp.h>
62
63 u_short ip_id;
64
65 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
66 #ifndef __REACTOS__
67 static void ip_mloopback
68 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
69 #endif
70
71 /*
72 * IP output. The packet in mbuf chain m contains a skeletal IP
73 * header (with len, off, ttl, proto, tos, src, dst).
74 * The mbuf chain containing the packet will be freed.
75 * The mbuf opt, if present, will not be freed.
76 */
77 int
78 ip_output(m0, opt, ro, flags, imo)
79 struct mbuf *m0;
80 struct mbuf *opt;
81 struct route *ro;
82 int flags;
83 struct ip_moptions *imo;
84 {
85 register struct ip *ip, *mhip;
86 #ifndef __REACTOS__
87 register struct ifnet *ifp;
88 #endif
89 register struct mbuf *m = m0;
90 register int hlen = sizeof (struct ip);
91 int len = 0, off, error = 0;
92 /*
93 * It might seem obvious at first glance that one could easily
94 * make a one-behind cache out of this by simply making `iproute'
95 * static and eliminating the bzero() below. However, this turns
96 * out not to work, for two reasons:
97 *
98 * 1) This routine needs to be reentrant. It can be called
99 * recursively from encapsulating network interfaces, and it
100 * is always called recursively from ip_mforward().
101 *
102 * 2) You turn out not to gain much. There is already a one-
103 * behind cache implemented for the specific case of forwarding,
104 * and sends on a connected socket will use a route associated
105 * with the PCB. The only cases left are sends on unconnected
106 * and raw sockets, and if these cases are really significant,
107 * something is seriously wrong.
108 */
109 struct route iproute;
110 struct sockaddr_in *dst;
111 struct in_ifaddr *ia = NULL;
112
113 #ifdef DIAGNOSTIC
114 if ((m->m_flags & M_PKTHDR) == 0)
115 panic("ip_output no HDR");
116 #endif
117 if (opt) {
118 m = ip_insertoptions(m, opt, &len);
119 hlen = len;
120 }
121 ip = mtod(m, struct ip *);
122 /*
123 * Fill in IP header.
124 */
125 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
126 ip->ip_v = IPVERSION;
127 ip->ip_off &= IP_DF;
128 ip->ip_id = htons(ip_id++);
129 ip->ip_hl = hlen >> 2;
130 ipstat.ips_localout++;
131 } else {
132 hlen = ip->ip_hl << 2;
133 }
134 /*
135 * Route packet.
136 */
137 if (ro == 0) {
138 ro = &iproute;
139 bzero((caddr_t)ro, sizeof (*ro));
140 }
141 dst = (struct sockaddr_in *)&ro->ro_dst;
142 /*
143 * If there is a cached route,
144 * check that it is to the same destination
145 * and is still up. If not, free it and try again.
146 */
147 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
148 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
149 RTFREE(ro->ro_rt);
150 ro->ro_rt = (struct rtentry *)0;
151 }
152 if (ro->ro_rt == 0) {
153 dst->sin_family = AF_INET;
154 dst->sin_len = sizeof(*dst);
155 dst->sin_addr = ip->ip_dst;
156 }
157 /*
158 * If routing to interface only,
159 * short circuit routing lookup.
160 */
161 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
162 #define sintosa(sin) ((struct sockaddr *)(sin))
163 #ifndef __REACTOS__
164 if (flags & IP_ROUTETOIF) {
165 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
166 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
167 ipstat.ips_noroute++;
168 error = ENETUNREACH;
169 goto bad;
170 }
171
172 ifp = ia->ia_ifp;
173 ip->ip_ttl = 1;
174 } else {
175 /*
176 * If this is the case, we probably don't want to allocate
177 * a protocol-cloned route since we didn't get one from the
178 * ULP. This lets TCP do its thing, while not burdening
179 * forwarding or ICMP with the overhead of cloning a route.
180 * Of course, we still want to do any cloning requested by
181 * the link layer, as this is probably required in all cases
182 * for correct operation (as it is for ARP).
183 */
184
185 if (ro->ro_rt == 0)
186 rtalloc_ign(ro, RTF_PRCLONING);
187 if (ro->ro_rt == 0) {
188 ipstat.ips_noroute++;
189 OS_DbgPrint(OSK_MID_TRACE,("EHOSTUNREACH\n"));
190 error = EHOSTUNREACH;
191 goto bad;
192 }
193 ia = ifatoia(ro->ro_rt->rt_ifa);
194 ifp = ro->ro_rt->rt_ifp;
195 ro->ro_rt->rt_use++;
196 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
197 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
198 }
199
200 #else
201 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
202 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
203 ipstat.ips_noroute++;
204 error = ENETUNREACH;
205 goto bad;
206 }
207 #endif
208
209 #ifndef __REACTOS__
210 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
211 struct in_multi *inm;
212
213 m->m_flags |= M_MCAST;
214 /*
215 * IP destination address is multicast. Make sure "dst"
216 * still points to the address in "ro". (It may have been
217 * changed to point to a gateway address, above.)
218 */
219 dst = (struct sockaddr_in *)&ro->ro_dst;
220 /*
221 * See if the caller provided any multicast options
222 */
223 if (imo != NULL) {
224 ip->ip_ttl = imo->imo_multicast_ttl;
225 if (imo->imo_multicast_ifp != NULL)
226 ifp = imo->imo_multicast_ifp;
227 if (imo->imo_multicast_vif != -1)
228 ip->ip_src.s_addr =
229 ip_mcast_src(imo->imo_multicast_vif);
230 } else
231 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
232 /*
233 * Confirm that the outgoing interface supports multicast.
234 */
235 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
236 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
237 ipstat.ips_noroute++;
238 error = ENETUNREACH;
239 goto bad;
240 }
241 }
242 /*
243 * If source address not specified yet, use address
244 * of outgoing interface.
245 */
246 if (ip->ip_src.s_addr == INADDR_ANY) {
247 register struct in_ifaddr *ia;
248
249 panic("We don't handle this yet\n");
250 for (ia = in_ifaddr; ia; ia = ia->ia_next)
251 if (ia->ia_ifp == ifp) {
252 ip->ip_src = IA_SIN(ia)->sin_addr;
253 break;
254 }
255 }
256
257 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
258 if (inm != NULL &&
259 (imo == NULL || imo->imo_multicast_loop)) {
260 /*
261 * If we belong to the destination multicast group
262 * on the outgoing interface, and the caller did not
263 * forbid loopback, loop back a copy.
264 */
265 ip_mloopback(ifp, m, dst);
266 }
267 else {
268 /*
269 * If we are acting as a multicast router, perform
270 * multicast forwarding as if the packet had just
271 * arrived on the interface to which we are about
272 * to send. The multicast forwarding function
273 * recursively calls this function, using the
274 * IP_FORWARDING flag to prevent infinite recursion.
275 *
276 * Multicasts that are looped back by ip_mloopback(),
277 * above, will be forwarded by the ip_input() routine,
278 * if necessary.
279 */
280 if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
281 /*
282 * Check if rsvp daemon is running. If not, don't
283 * set ip_moptions. This ensures that the packet
284 * is multicast and not just sent down one link
285 * as prescribed by rsvpd.
286 */
287 if (!rsvp_on)
288 imo = NULL;
289 if (ip_mforward(ip, ifp, m, imo) != 0) {
290 m_freem(m);
291 goto done;
292 }
293 }
294 }
295
296 /*
297 * Multicasts with a time-to-live of zero may be looped-
298 * back, above, but must not be transmitted on a network.
299 * Also, multicasts addressed to the loopback interface
300 * are not sent -- the above call to ip_mloopback() will
301 * loop back a copy if this host actually belongs to the
302 * destination group on the loopback interface.
303 */
304 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
305 m_freem(m);
306 goto done;
307 }
308
309 goto sendit;
310 }
311 #endif
312
313 #ifdef __REACTOS__
314 /*
315 * If source address not specified yet, use address
316 * of outgoing interface.
317 */
318 if (ip->ip_src.s_addr == INADDR_ANY)
319 ip->ip_src = IA_SIN(ia)->sin_addr;
320 #endif
321 #ifndef __REACTOS__
322 /*
323 * Verify that we have any chance at all of being able to queue
324 * the packet or packet fragments
325 */
326 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
327 ifp->if_snd.ifq_maxlen) {
328 error = ENOBUFS;
329 goto bad;
330 }
331
332 /*
333 * Look for broadcast address and
334 * and verify user is allowed to send
335 * such a packet.
336 */
337 if (in_broadcast(dst->sin_addr, ifp)) {
338 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
339 error = EADDRNOTAVAIL;
340 goto bad;
341 }
342 if ((flags & IP_ALLOWBROADCAST) == 0) {
343 error = EACCES;
344 goto bad;
345 }
346 /* don't allow broadcast messages to be fragmented */
347 if ((u_short)ip->ip_len > ifp->if_mtu) {
348 error = EMSGSIZE;
349 goto bad;
350 }
351 m->m_flags |= M_BCAST;
352 } else
353 m->m_flags &= ~M_BCAST;
354 #endif
355
356 #ifndef __REACTOS__
357 sendit:
358 /*
359 * Check with the firewall...
360 */
361 if (!(*ip_fw_chk_ptr)(m,ip,ifp,1)) {
362 error = EACCES;
363 goto done;
364 }
365 #endif
366
367 /*
368 * If small enough for interface, can just send directly.
369 */
370
371 /* FIXME: This was ROS-modified code. Where is the original? */
372 if ((u_short)ip->ip_len <= ((struct ifaddr *)ia)->ifa_mtu) {
373 ip->ip_len = htons((u_short)ip->ip_len);
374 ip->ip_off = htons((u_short)ip->ip_off);
375 ip->ip_sum = 0;
376 ip->ip_sum = in_cksum(m, hlen);
377 #ifdef __REACTOS__
378 if( OtcpEvent.PacketSend ) {
379 struct mbuf *new_m;
380 new_m = m_get( M_DONTWAIT, 0 );
381 if ( NULL == new_m ) {
382 error = ENOBUFS;
383 goto done;
384 }
385 MCLGET( new_m, M_DONTWAIT );
386 if (0 == (new_m->m_flags & M_EXT)) {
387 m_free( new_m );
388 error = ENOBUFS;
389 goto done;
390 }
391 m_copydata( m, 0, htons(ip->ip_len), new_m->m_data );
392 new_m->m_len = htons(ip->ip_len);
393 error = OtcpEvent.PacketSend( OtcpEvent.ClientData,
394 (OSK_PCHAR)new_m->m_data, new_m->m_len );
395 m_free( new_m );
396 goto done;
397 }
398 #else
399 error = (*ifp->if_output)(ifp, m,
400 (struct sockaddr *)dst, ro->ro_rt);
401 #endif
402 }
403 /*
404 * Too large for interface; fragment if possible.
405 * Must be able to put at least 8 bytes per fragment.
406 */
407 if (ip->ip_off & IP_DF) {
408 error = EMSGSIZE;
409 #ifndef __REACTOS__
410 #if 1
411 /*
412 * This case can happen if the user changed the MTU
413 * of an interface after enabling IP on it. Because
414 * most netifs don't keep track of routes pointing to
415 * them, there is no way for one to update all its
416 * routes when the MTU is changed.
417 */
418 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
419 && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
420 && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
421 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
422 }
423 #endif
424 #endif
425 ipstat.ips_cantfrag++;
426 goto bad;
427 }
428 #ifndef __REACTOS__
429 len = (ifp->if_mtu - hlen) &~ 7;
430 if (len < 8) {
431 error = EMSGSIZE;
432 goto bad;
433 }
434 #else
435 len = (((struct ifaddr *)ia)->ifa_mtu - hlen) & ~7;
436 #endif
437
438 {
439 int mhlen, firstlen = len;
440 struct mbuf **mnext = &m->m_nextpkt;
441
442 /*
443 * Loop through length of segment after first fragment,
444 * make new header and copy data of each part and link onto chain.
445 */
446 m0 = m;
447 mhlen = sizeof (struct ip);
448 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
449 OS_DbgPrint(OSK_MID_TRACE,("off = %d, len = %d\n", off, len));
450 MGETHDR(m, M_DONTWAIT, MT_HEADER);
451 if (m == 0) {
452 error = ENOBUFS;
453 ipstat.ips_odropped++;
454 goto sendorfree;
455 }
456 m->m_data += max_linkhdr;
457 mhip = mtod(m, struct ip *);
458 *mhip = *ip;
459 if (hlen > sizeof (struct ip)) {
460 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
461 mhip->ip_hl = mhlen >> 2;
462 }
463 m->m_len = mhlen;
464 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
465 if (ip->ip_off & IP_MF)
466 mhip->ip_off |= IP_MF;
467 if (off + len >= (u_short)ip->ip_len)
468 len = (u_short)ip->ip_len - off;
469 else
470 mhip->ip_off |= IP_MF;
471 mhip->ip_len = htons((u_short)(len + mhlen));
472 m->m_next = m_copy(m0, off, len);
473 if (m->m_next == 0) {
474 (void) m_free(m);
475 error = ENOBUFS; /* ??? */
476 ipstat.ips_odropped++;
477 goto sendorfree;
478 }
479 m->m_pkthdr.len = mhlen + len;
480 m->m_pkthdr.rcvif = (struct ifnet *)0;
481 mhip->ip_off = htons((u_short)mhip->ip_off);
482 mhip->ip_sum = 0;
483 mhip->ip_sum = in_cksum(m, mhlen);
484 *mnext = m;
485 mnext = &m->m_nextpkt;
486 ipstat.ips_ofragments++;
487 }
488 /*
489 * Update first fragment by trimming what's been copied out
490 * and updating header, then send each fragment (in order).
491 */
492 m = m0;
493 OS_DbgPrint(OSK_MID_TRACE,("hlen %d firstlen %d ip->ip_len %x\n",
494 hlen, firstlen, ip->ip_len));
495 OS_DbgPrint(OSK_MID_TRACE,("hlen + firstlen - ip->ip_len %d\n",
496 hlen + firstlen - (u_short)ip->ip_len));
497 m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
498 m->m_pkthdr.len = hlen + firstlen;
499 ip->ip_len = htons((u_short)(m->m_pkthdr.len));
500 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
501 ip->ip_sum = 0;
502 ip->ip_sum = in_cksum(m, hlen - sizeof( struct ip ) );
503
504 OS_DbgPrint(OSK_MID_TRACE,("ip->ip_len = %x\n", ip->ip_len));
505
506 sendorfree:
507 for (m = m0; m; m = m0) {
508 m0 = m->m_nextpkt;
509 m->m_nextpkt = 0;
510 #ifndef __REACTOS__
511 if (error == 0)
512 error = (*ifp->if_output)(ifp, m,
513 (struct sockaddr *)dst, ro->ro_rt);
514 else
515 m_freem(m);
516 #else
517 if( error == 0 && OtcpEvent.PacketSend ) {
518 struct mbuf *new_m;
519 MGET( new_m, M_DONTWAIT, 0 );
520 if ( NULL == new_m ) {
521 error = ENOBUFS;
522 goto done;
523 }
524 MCLGET( new_m, M_DONTWAIT );
525 if (0 == (new_m->m_flags & M_EXT)) {
526 m_free( new_m );
527 error = ENOBUFS;
528 goto done;
529 }
530 m_copydata( m, 0, m->m_pkthdr.len, new_m->m_data );
531 new_m->m_len = m->m_pkthdr.len;
532 error = OtcpEvent.PacketSend( OtcpEvent.ClientData,
533 (OSK_PCHAR)new_m->m_data, new_m->m_len );
534 m_free( new_m );
535 }
536
537 OS_DbgPrint(OSK_MID_TRACE,("Error from upper layer: %d\n", error));
538 #endif
539 }
540
541 if (error == 0)
542 ipstat.ips_fragmented++;
543 }
544 done:
545 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
546 RTFREE(ro->ro_rt);
547 }
548
549 return (error);
550 bad:
551 m_freem(m0);
552 goto done;
553 }
554
555 /*
556 * Insert IP options into preformed packet.
557 * Adjust IP destination as required for IP source routing,
558 * as indicated by a non-zero in_addr at the start of the options.
559 *
560 * XXX This routine assumes that the packet has no options in place.
561 */
562 static struct mbuf *
563 ip_insertoptions(m, opt, phlen)
564 register struct mbuf *m;
565 struct mbuf *opt;
566 int *phlen;
567 {
568 register struct ipoption *p = mtod(opt, struct ipoption *);
569 struct mbuf *n;
570 register struct ip *ip = mtod(m, struct ip *);
571 unsigned optlen;
572
573 optlen = opt->m_len - sizeof(p->ipopt_dst);
574 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
575 return (m); /* XXX should fail */
576 if (p->ipopt_dst.s_addr)
577 ip->ip_dst = p->ipopt_dst;
578 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
579 MGETHDR(n, M_DONTWAIT, MT_HEADER);
580 if (n == 0)
581 return (m);
582 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
583 m->m_len -= sizeof(struct ip);
584 m->m_data += sizeof(struct ip);
585 n->m_next = m;
586 m = n;
587 m->m_len = optlen + sizeof(struct ip);
588 m->m_data += max_linkhdr;
589 (void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
590 } else {
591 m->m_data -= optlen;
592 m->m_len += optlen;
593 m->m_pkthdr.len += optlen;
594 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
595 }
596 ip = mtod(m, struct ip *);
597 (void)memcpy(ip + 1, p->ipopt_list, (unsigned)optlen);
598 *phlen = sizeof(struct ip) + optlen;
599 ip->ip_hl = *phlen >> 2;
600 ip->ip_len += optlen;
601 return (m);
602 }
603
604 /*
605 * Copy options from ip to jp,
606 * omitting those not copied during fragmentation.
607 */
608 int
609 ip_optcopy(ip, jp)
610 struct ip *ip, *jp;
611 {
612 register u_char *cp, *dp;
613 int opt, optlen, cnt;
614
615 cp = (u_char *)(ip + 1);
616 dp = (u_char *)(jp + 1);
617 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
618 for (; cnt > 0; cnt -= optlen, cp += optlen) {
619 opt = cp[0];
620 if (opt == IPOPT_EOL)
621 break;
622 if (opt == IPOPT_NOP) {
623 /* Preserve for IP mcast tunnel's LSRR alignment. */
624 *dp++ = IPOPT_NOP;
625 optlen = 1;
626 continue;
627 } else
628 optlen = cp[IPOPT_OLEN];
629 /* bogus lengths should have been caught by ip_dooptions */
630 if (optlen > cnt)
631 optlen = cnt;
632 if (IPOPT_COPIED(opt)) {
633 (void)memcpy(dp, cp, (unsigned)optlen);
634 dp += optlen;
635 }
636 }
637 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
638 *dp++ = IPOPT_EOL;
639 return (optlen);
640 }
641
642 /*
643 * IP socket option processing.
644 */
645 int
646 ip_ctloutput(op, so, level, optname, mp)
647 int op;
648 struct socket *so;
649 int level, optname;
650 struct mbuf **mp;
651 {
652 register struct inpcb *inp = sotoinpcb(so);
653 register struct mbuf *m = *mp;
654 register int optval = 0;
655 int error = 0;
656
657 if (level != IPPROTO_IP) {
658 error = EINVAL;
659 if (op == PRCO_SETOPT && *mp)
660 (void) m_free(*mp);
661 } else switch (op) {
662
663 case PRCO_SETOPT:
664 switch (optname) {
665 case IP_OPTIONS:
666 #ifdef notyet
667 case IP_RETOPTS:
668 return (ip_pcbopts(optname, &inp->inp_options, m));
669 #else
670 return (ip_pcbopts(&inp->inp_options, m));
671 #endif
672
673 case IP_TOS:
674 case IP_TTL:
675 case IP_RECVOPTS:
676 case IP_RECVRETOPTS:
677 case IP_RECVDSTADDR:
678 if (m == 0 || m->m_len != sizeof(int))
679 error = EINVAL;
680 else {
681 optval = *mtod(m, int *);
682 switch (optname) {
683
684 case IP_TOS:
685 inp->inp_ip.ip_tos = optval;
686 break;
687
688 case IP_TTL:
689 inp->inp_ip.ip_ttl = optval;
690 break;
691 #define OPTSET(bit) \
692 if (optval) \
693 inp->inp_flags |= bit; \
694 else \
695 inp->inp_flags &= ~bit;
696
697 case IP_RECVOPTS:
698 OPTSET(INP_RECVOPTS);
699 break;
700
701 case IP_RECVRETOPTS:
702 OPTSET(INP_RECVRETOPTS);
703 break;
704
705 case IP_RECVDSTADDR:
706 OPTSET(INP_RECVDSTADDR);
707 break;
708 }
709 }
710 break;
711 #undef OPTSET
712
713 case IP_MULTICAST_IF:
714 case IP_MULTICAST_VIF:
715 case IP_MULTICAST_TTL:
716 case IP_MULTICAST_LOOP:
717 case IP_ADD_MEMBERSHIP:
718 case IP_DROP_MEMBERSHIP:
719 error = ip_setmoptions(optname, &inp->inp_moptions, m);
720 break;
721
722 default:
723 error = ENOPROTOOPT;
724 break;
725 }
726 if (m)
727 (void)m_free(m);
728 break;
729
730 case PRCO_GETOPT:
731 switch (optname) {
732 case IP_OPTIONS:
733 case IP_RETOPTS:
734 *mp = m = m_get(M_WAIT, MT_SOOPTS);
735 if (inp->inp_options) {
736 m->m_len = inp->inp_options->m_len;
737 (void)memcpy(mtod(m, void *),
738 mtod(inp->inp_options, void *), (unsigned)m->m_len);
739 } else
740 m->m_len = 0;
741 break;
742
743 case IP_TOS:
744 case IP_TTL:
745 case IP_RECVOPTS:
746 case IP_RECVRETOPTS:
747 case IP_RECVDSTADDR:
748 *mp = m = m_get(M_WAIT, MT_SOOPTS);
749 m->m_len = sizeof(int);
750 switch (optname) {
751
752 case IP_TOS:
753 optval = inp->inp_ip.ip_tos;
754 break;
755
756 case IP_TTL:
757 optval = inp->inp_ip.ip_ttl;
758 break;
759
760 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
761
762 case IP_RECVOPTS:
763 optval = OPTBIT(INP_RECVOPTS);
764 break;
765
766 case IP_RECVRETOPTS:
767 optval = OPTBIT(INP_RECVRETOPTS);
768 break;
769
770 case IP_RECVDSTADDR:
771 optval = OPTBIT(INP_RECVDSTADDR);
772 break;
773 }
774 *mtod(m, int *) = optval;
775 break;
776
777 case IP_MULTICAST_IF:
778 case IP_MULTICAST_VIF:
779 case IP_MULTICAST_TTL:
780 case IP_MULTICAST_LOOP:
781 case IP_ADD_MEMBERSHIP:
782 case IP_DROP_MEMBERSHIP:
783 error = ip_getmoptions(optname, inp->inp_moptions, mp);
784 break;
785
786 default:
787 error = ENOPROTOOPT;
788 break;
789 }
790 break;
791 }
792 return (error);
793 }
794
795 /*
796 * Set up IP options in pcb for insertion in output packets.
797 * Store in mbuf with pointer in pcbopt, adding pseudo-option
798 * with destination address if source routed.
799 */
800 int
801 #ifdef notyet
802 ip_pcbopts(optname, pcbopt, m)
803 int optname;
804 #else
805 ip_pcbopts(pcbopt, m)
806 #endif
807 struct mbuf **pcbopt;
808 register struct mbuf *m;
809 {
810 register int cnt, optlen;
811 register u_char *cp;
812 u_char opt;
813
814 /* turn off any old options */
815 if (*pcbopt)
816 (void)m_free(*pcbopt);
817 *pcbopt = 0;
818 if (m == (struct mbuf *)0 || m->m_len == 0) {
819 /*
820 * Only turning off any previous options.
821 */
822 if (m)
823 (void)m_free(m);
824 return (0);
825 }
826
827 #ifndef vax
828 if (m->m_len % sizeof(long))
829 goto bad;
830 #endif
831 /*
832 * IP first-hop destination address will be stored before
833 * actual options; move other options back
834 * and clear it when none present.
835 */
836 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
837 goto bad;
838 cnt = m->m_len;
839 m->m_len += sizeof(struct in_addr);
840 cp = mtod(m, u_char *) + sizeof(struct in_addr);
841 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
842 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
843
844 for (; cnt > 0; cnt -= optlen, cp += optlen) {
845 opt = cp[IPOPT_OPTVAL];
846 if (opt == IPOPT_EOL)
847 break;
848 if (opt == IPOPT_NOP)
849 optlen = 1;
850 else {
851 optlen = cp[IPOPT_OLEN];
852 if (optlen <= IPOPT_OLEN || optlen > cnt)
853 goto bad;
854 }
855 switch (opt) {
856
857 default:
858 break;
859
860 case IPOPT_LSRR:
861 case IPOPT_SSRR:
862 /*
863 * user process specifies route as:
864 * ->A->B->C->D
865 * D must be our final destination (but we can't
866 * check that since we may not have connected yet).
867 * A is first hop destination, which doesn't appear in
868 * actual IP option, but is stored before the options.
869 */
870 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
871 goto bad;
872 m->m_len -= sizeof(struct in_addr);
873 cnt -= sizeof(struct in_addr);
874 optlen -= sizeof(struct in_addr);
875 cp[IPOPT_OLEN] = optlen;
876 /*
877 * Move first hop before start of options.
878 */
879 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
880 sizeof(struct in_addr));
881 /*
882 * Then copy rest of options back
883 * to close up the deleted entry.
884 */
885 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
886 sizeof(struct in_addr)),
887 (caddr_t)&cp[IPOPT_OFFSET+1],
888 (unsigned)cnt + sizeof(struct in_addr));
889 break;
890 }
891 }
892 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
893 goto bad;
894 *pcbopt = m;
895 return (0);
896
897 bad:
898 (void)m_free(m);
899 return (EINVAL);
900 }
901
902 /*
903 * Set the IP multicast options in response to user setsockopt().
904 */
905 int
906 ip_setmoptions(optname, imop, m)
907 int optname;
908 struct ip_moptions **imop;
909 struct mbuf *m;
910 {
911 register int error = 0;
912 #ifndef __REACTOS__
913 u_char loop;
914 register int i;
915 struct in_addr addr;
916 register struct ip_mreq *mreq;
917 register struct ifnet *ifp;
918 #endif
919 register struct ip_moptions *imo = *imop;
920 #ifndef __REACTOS__
921 struct route ro;
922 register struct sockaddr_in *dst;
923 int s;
924 #endif
925
926 if (imo == NULL) {
927 /*
928 * No multicast option buffer attached to the pcb;
929 * allocate one and initialize to default values.
930 */
931 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
932 M_WAITOK);
933
934 if (imo == NULL)
935 return (ENOBUFS);
936 *imop = imo;
937 imo->imo_multicast_ifp = NULL;
938 imo->imo_multicast_vif = -1;
939 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
940 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
941 imo->imo_num_memberships = 0;
942 }
943
944 switch (optname) {
945 #ifndef __REACTOS__
946 /* store an index number for the vif you wanna use in the send */
947 case IP_MULTICAST_VIF:
948 if (!legal_vif_num) {
949 error = EOPNOTSUPP;
950 break;
951 }
952 if (m == NULL || m->m_len != sizeof(int)) {
953 error = EINVAL;
954 break;
955 }
956 i = *(mtod(m, int *));
957 if (!legal_vif_num(i) && (i != -1)) {
958 error = EINVAL;
959 break;
960 }
961 imo->imo_multicast_vif = i;
962 break;
963
964 case IP_MULTICAST_IF:
965 /*
966 * Select the interface for outgoing multicast packets.
967 */
968 if (m == NULL || m->m_len != sizeof(struct in_addr)) {
969 error = EINVAL;
970 break;
971 }
972 addr = *(mtod(m, struct in_addr *));
973 /*
974 * INADDR_ANY is used to remove a previous selection.
975 * When no interface is selected, a default one is
976 * chosen every time a multicast packet is sent.
977 */
978 if (addr.s_addr == INADDR_ANY) {
979 imo->imo_multicast_ifp = NULL;
980 break;
981 }
982 /*
983 * The selected interface is identified by its local
984 * IP address. Find the interface and confirm that
985 * it supports multicasting.
986 */
987 s = splimp();
988 INADDR_TO_IFP(addr, ifp);
989 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
990 error = EADDRNOTAVAIL;
991 break;
992 }
993 imo->imo_multicast_ifp = ifp;
994 splx(s);
995 break;
996
997 case IP_MULTICAST_TTL:
998 /*
999 * Set the IP time-to-live for outgoing multicast packets.
1000 */
1001 if (m == NULL || m->m_len != 1) {
1002 error = EINVAL;
1003 break;
1004 }
1005 imo->imo_multicast_ttl = *(mtod(m, u_char *));
1006 break;
1007
1008 case IP_MULTICAST_LOOP:
1009 /*
1010 * Set the loopback flag for outgoing multicast packets.
1011 * Must be zero or one.
1012 */
1013 if (m == NULL || m->m_len != 1 ||
1014 (loop = *(mtod(m, u_char *))) > 1) {
1015 error = EINVAL;
1016 break;
1017 }
1018 imo->imo_multicast_loop = loop;
1019 break;
1020
1021 case IP_ADD_MEMBERSHIP:
1022 /*
1023 * Add a multicast group membership.
1024 * Group must be a valid IP multicast address.
1025 */
1026 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1027 error = EINVAL;
1028 break;
1029 }
1030 mreq = mtod(m, struct ip_mreq *);
1031 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1032 error = EINVAL;
1033 break;
1034 }
1035 s = splimp();
1036 /*
1037 * If no interface address was provided, use the interface of
1038 * the route to the given multicast address.
1039 */
1040 if (mreq->imr_interface.s_addr == INADDR_ANY) {
1041 bzero((caddr_t)&ro, sizeof(ro));
1042 dst = (struct sockaddr_in *)&ro.ro_dst;
1043 dst->sin_len = sizeof(*dst);
1044 dst->sin_family = AF_INET;
1045 dst->sin_addr = mreq->imr_multiaddr;
1046 rtalloc(&ro);
1047 if (ro.ro_rt == NULL) {
1048 error = EADDRNOTAVAIL;
1049 splx(s);
1050 break;
1051 }
1052 ifp = ro.ro_rt->rt_ifp;
1053 rtfree(ro.ro_rt);
1054 }
1055 else {
1056 INADDR_TO_IFP(mreq->imr_interface, ifp);
1057 }
1058
1059 /*
1060 * See if we found an interface, and confirm that it
1061 * supports multicast.
1062 */
1063 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1064 error = EADDRNOTAVAIL;
1065 splx(s);
1066 break;
1067 }
1068 /*
1069 * See if the membership already exists or if all the
1070 * membership slots are full.
1071 */
1072 for (i = 0; i < imo->imo_num_memberships; ++i) {
1073 if (imo->imo_membership[i]->inm_ifp == ifp &&
1074 imo->imo_membership[i]->inm_addr.s_addr
1075 == mreq->imr_multiaddr.s_addr)
1076 break;
1077 }
1078 if (i < imo->imo_num_memberships) {
1079 error = EADDRINUSE;
1080 splx(s);
1081 break;
1082 }
1083 if (i == IP_MAX_MEMBERSHIPS) {
1084 error = ETOOMANYREFS;
1085 splx(s);
1086 break;
1087 }
1088 /*
1089 * Everything looks good; add a new record to the multicast
1090 * address list for the given interface.
1091 */
1092 if ((imo->imo_membership[i] =
1093 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1094 error = ENOBUFS;
1095 splx(s);
1096 break;
1097 }
1098 ++imo->imo_num_memberships;
1099 splx(s);
1100 break;
1101
1102 case IP_DROP_MEMBERSHIP:
1103 /*
1104 * Drop a multicast group membership.
1105 * Group must be a valid IP multicast address.
1106 */
1107 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1108 error = EINVAL;
1109 break;
1110 }
1111 mreq = mtod(m, struct ip_mreq *);
1112 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1113 error = EINVAL;
1114 break;
1115 }
1116
1117 s = splimp();
1118 /*
1119 * If an interface address was specified, get a pointer
1120 * to its ifnet structure.
1121 */
1122 if (mreq->imr_interface.s_addr == INADDR_ANY)
1123 ifp = NULL;
1124 else {
1125 INADDR_TO_IFP(mreq->imr_interface, ifp);
1126 if (ifp == NULL) {
1127 error = EADDRNOTAVAIL;
1128 splx(s);
1129 break;
1130 }
1131 }
1132 /*
1133 * Find the membership in the membership array.
1134 */
1135 for (i = 0; i < imo->imo_num_memberships; ++i) {
1136 if ((ifp == NULL ||
1137 imo->imo_membership[i]->inm_ifp == ifp) &&
1138 imo->imo_membership[i]->inm_addr.s_addr ==
1139 mreq->imr_multiaddr.s_addr)
1140 break;
1141 }
1142 if (i == imo->imo_num_memberships) {
1143 error = EADDRNOTAVAIL;
1144 splx(s);
1145 break;
1146 }
1147 /*
1148 * Give up the multicast address record to which the
1149 * membership points.
1150 */
1151 in_delmulti(imo->imo_membership[i]);
1152 /*
1153 * Remove the gap in the membership array.
1154 */
1155 for (++i; i < imo->imo_num_memberships; ++i)
1156 imo->imo_membership[i-1] = imo->imo_membership[i];
1157 --imo->imo_num_memberships;
1158 splx(s);
1159 break;
1160 #endif
1161 default:
1162 error = EOPNOTSUPP;
1163 break;
1164 }
1165
1166 /*
1167 * If all options have default values, no need to keep the mbuf.
1168 */
1169 if (imo->imo_multicast_ifp == NULL &&
1170 imo->imo_multicast_vif == -1 &&
1171 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1172 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1173 imo->imo_num_memberships == 0) {
1174 free(*imop, M_IPMOPTS);
1175 *imop = NULL;
1176 }
1177
1178 return (error);
1179 }
1180
1181 /*
1182 * Return the IP multicast options in response to user getsockopt().
1183 */
1184 int
1185 ip_getmoptions(optname, imo, mp)
1186 int optname;
1187 register struct ip_moptions *imo;
1188 register struct mbuf **mp;
1189 {
1190 #ifndef __REACTOS__
1191 u_char *ttl;
1192 u_char *loop;
1193 struct in_addr *addr;
1194 struct in_ifaddr *ia;
1195 #endif
1196
1197 *mp = m_get(M_WAIT, MT_SOOPTS);
1198
1199 switch (optname) {
1200 #ifndef __REACTOS__
1201 case IP_MULTICAST_VIF:
1202 if (imo != NULL)
1203 *(mtod(*mp, int *)) = imo->imo_multicast_vif;
1204 else
1205 *(mtod(*mp, int *)) = -1;
1206 (*mp)->m_len = sizeof(int);
1207 return(0);
1208
1209 case IP_MULTICAST_IF:
1210 addr = mtod(*mp, struct in_addr *);
1211 (*mp)->m_len = sizeof(struct in_addr);
1212 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1213 addr->s_addr = INADDR_ANY;
1214 else {
1215 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1216 addr->s_addr = (ia == NULL) ? INADDR_ANY
1217 : IA_SIN(ia)->sin_addr.s_addr;
1218 }
1219 return (0);
1220
1221 case IP_MULTICAST_TTL:
1222 ttl = mtod(*mp, u_char *);
1223 (*mp)->m_len = 1;
1224 *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1225 : imo->imo_multicast_ttl;
1226 return (0);
1227
1228 case IP_MULTICAST_LOOP:
1229 loop = mtod(*mp, u_char *);
1230 (*mp)->m_len = 1;
1231 *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1232 : imo->imo_multicast_loop;
1233 return (0);
1234 #endif
1235 default:
1236 return (EOPNOTSUPP);
1237 }
1238 }
1239
1240 /*
1241 * Discard the IP multicast options.
1242 */
1243 void
1244 ip_freemoptions(imo)
1245 register struct ip_moptions *imo;
1246 {
1247 register int i;
1248
1249 if (imo != NULL) {
1250 for (i = 0; i < imo->imo_num_memberships; ++i)
1251 in_delmulti(imo->imo_membership[i]);
1252 free(imo, M_IPMOPTS);
1253 }
1254 }
1255
1256 #ifndef __REACTOS__
1257 /*
1258 * Routine called from ip_output() to loop back a copy of an IP multicast
1259 * packet to the input queue of a specified interface. Note that this
1260 * calls the output routine of the loopback "driver", but with an interface
1261 * pointer that might NOT be a loopback interface -- evil, but easier than
1262 * replicating that code here.
1263 */
1264 static void
1265 ip_mloopback(ifp, m, dst)
1266 struct ifnet *ifp;
1267 register struct mbuf *m;
1268 register struct sockaddr_in *dst;
1269 {
1270 register struct ip *ip;
1271 struct mbuf *copym;
1272
1273 copym = m_copy(m, 0, M_COPYALL);
1274 if (copym != NULL) {
1275 /*
1276 * We don't bother to fragment if the IP length is greater
1277 * than the interface's MTU. Can this possibly matter?
1278 */
1279 ip = mtod(copym, struct ip *);
1280 ip->ip_len = htons((u_short)ip->ip_len);
1281 ip->ip_off = htons((u_short)ip->ip_off);
1282 ip->ip_sum = 0;
1283 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1284 (void) looutput(ifp, copym, (struct sockaddr *)dst, NULL);
1285 }
1286 }
1287 #endif