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