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