ip_output.c revision 1.8 1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * from: @(#)ip_output.c 7.23 (Berkeley) 11/12/90
34 * $Id: ip_output.c,v 1.8 1993/12/18 00:41:59 mycroft Exp $
35 */
36
37 #include <sys/param.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
45 #include <net/if.h>
46 #include <net/route.h>
47
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <netinet/in_pcb.h>
52 #include <netinet/in_var.h>
53 #include <netinet/ip_var.h>
54
55 #ifdef vax
56 #include <machine/mtpr.h>
57 #endif
58
59 struct mbuf *ip_insertoptions();
60 void ip_mloopback __P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
61
62 /*
63 * IP output. The packet in mbuf chain m contains a skeletal IP
64 * header (with len, off, ttl, proto, tos, src, dst).
65 * The mbuf chain containing the packet will be freed.
66 * The mbuf opt, if present, will not be freed.
67 */
68 ip_output(m0, opt, ro, flags
69 #ifdef MULTICAST
70 ,imo
71 #endif
72 )
73 struct mbuf *m0;
74 struct mbuf *opt;
75 struct route *ro;
76 int flags;
77 #ifdef MULTICAST
78 struct ip_moptions *imo;
79 #endif
80 {
81 register struct ip *ip, *mhip;
82 register struct ifnet *ifp;
83 register struct mbuf *m = m0;
84 register int hlen = sizeof (struct ip);
85 int len, off, error = 0;
86 struct route iproute;
87 struct sockaddr_in *dst;
88 struct in_ifaddr *ia;
89
90 #ifdef DIAGNOSTIC
91 if ((m->m_flags & M_PKTHDR) == 0)
92 panic("ip_output no HDR");
93 #endif
94 if (opt) {
95 m = ip_insertoptions(m, opt, &len);
96 hlen = len;
97 }
98 ip = mtod(m, struct ip *);
99 /*
100 * Fill in IP header.
101 */
102 if ((flags & IP_FORWARDING) == 0) {
103 ip->ip_v = IPVERSION;
104 ip->ip_off &= IP_DF;
105 ip->ip_id = htons(ip_id++);
106 ip->ip_hl = hlen >> 2;
107 } else {
108 hlen = ip->ip_hl << 2;
109 ipstat.ips_localout++;
110 }
111 /*
112 * Route packet.
113 */
114 if (ro == 0) {
115 ro = &iproute;
116 bzero((caddr_t)ro, sizeof (*ro));
117 }
118 dst = (struct sockaddr_in *)&ro->ro_dst;
119 /*
120 * If there is a cached route,
121 * check that it is to the same destination
122 * and is still up. If not, free it and try again.
123 */
124 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
125 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
126 RTFREE(ro->ro_rt);
127 ro->ro_rt = (struct rtentry *)0;
128 }
129 if (ro->ro_rt == 0) {
130 dst->sin_family = AF_INET;
131 dst->sin_len = sizeof(*dst);
132 dst->sin_addr = ip->ip_dst;
133 }
134 /*
135 * If routing to interface only,
136 * short circuit routing lookup.
137 */
138 if (flags & IP_ROUTETOIF) {
139
140 ia = (struct in_ifaddr *)ifa_ifwithdstaddr((struct sockaddr *)dst);
141 if (ia == 0)
142 ia = in_iaonnetof(in_netof(ip->ip_dst));
143 if (ia == 0) {
144 error = ENETUNREACH;
145 goto bad;
146 }
147 ifp = ia->ia_ifp;
148 } else {
149 if (ro->ro_rt == 0)
150 rtalloc(ro);
151 if (ro->ro_rt == 0) {
152 error = EHOSTUNREACH;
153 goto bad;
154 }
155 ia = (struct in_ifaddr *)ro->ro_rt->rt_ifa;
156 ifp = ro->ro_rt->rt_ifp;
157 ro->ro_rt->rt_use++;
158 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
159 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
160 }
161 #ifdef MULTICAST
162 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
163 struct in_multi *inm;
164 extern struct ifnet loif;
165 extern struct socket *ip_mrouter;
166
167 m->m_flags |= M_MCAST;
168 /*
169 * IP destination address is multicast. Make sure "dst"
170 * still points to the address in "ro". (It may have been
171 * changed to point to a gateway address, above.)
172 */
173 dst = (struct sockaddr_in *)&ro->ro_dst;
174 /*
175 * See if the caller provided any multicast options
176 */
177 if ((flags & IP_MULTICASTOPTS) && imo != NULL) {
178 ip->ip_ttl = imo->imo_multicast_ttl;
179 if (imo->imo_multicast_ifp != NULL)
180 ifp = imo->imo_multicast_ifp;
181 } else {
182 imo = NULL;
183 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
184 }
185 /*
186 * Confirm that the outgoing interface supports multicast.
187 */
188 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
189 error = ENETUNREACH;
190 goto bad;
191 }
192 /*
193 * If source address not specified yet, use address
194 * of outgoing interface.
195 */
196 if (ip->ip_src.s_addr == INADDR_ANY) {
197 register struct in_ifaddr *ia;
198
199 for (ia = in_ifaddr; ia; ia = ia->ia_next)
200 if (ia->ia_ifp == ifp) {
201 ip->ip_src = IA_SIN(ia)->sin_addr;
202 break;
203 }
204 }
205
206 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
207 if (inm != NULL &&
208 (imo == NULL || imo->imo_multicast_loop)) {
209 /*
210 * If we belong to the destination multicast group
211 * on the outgoing interface, and the caller did not
212 * forbid loopback, loop back a copy.
213 */
214 ip_mloopback(ifp, m, dst);
215 }
216 #ifdef MROUTING
217 else if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
218 /*
219 * If we are acting as a multicast router, perform
220 * multicast forwarding as if the packet had just
221 * arrived on the interface to which we are about
222 * to send. The multicast forwarding function
223 * recursively calls this function, using the
224 * IP_FORWARDING flag to prevent infinite recursion.
225 *
226 * Multicasts that are looped back by ip_mloopback(),
227 * above, will be forwarded by the ip_input() routine,
228 * if necessary.
229 */
230 if (ip_mforward(ip, ifp, m) != 0) {
231 m_freem(m);
232 goto done;
233 }
234 }
235 #endif
236 /*
237 * Multicasts with a time-to-live of zero may be looped-
238 * back, above, but must not be transmitted on a network.
239 * Also, multicasts addressed to the loopback interface
240 * are not sent -- the above call to ip_mloopback() will
241 * loop back a copy if this host actually belongs to the
242 * destination group on the loopback interface.
243 */
244 if (ip->ip_ttl == 0 || ifp == &loif) {
245 m_freem(m);
246 goto done;
247 }
248
249 goto sendit;
250 }
251 #endif
252 #ifndef notdef
253 /*
254 * If source address not specified yet, use address
255 * of outgoing interface.
256 */
257 if (ip->ip_src.s_addr == INADDR_ANY)
258 ip->ip_src = IA_SIN(ia)->sin_addr;
259 #endif
260
261 /*
262 * Verify that we have any chance at all of being able to queue
263 * the packet or packet fragments
264 */
265 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
266 ifp->if_snd.ifq_maxlen) {
267 error = ENOBUFS;
268 goto bad;
269 }
270
271 /*
272 * Look for broadcast address and
273 * and verify user is allowed to send
274 * such a packet.
275 */
276 if (in_broadcast(dst->sin_addr)) {
277 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
278 error = EADDRNOTAVAIL;
279 goto bad;
280 }
281 if ((flags & IP_ALLOWBROADCAST) == 0) {
282 error = EACCES;
283 goto bad;
284 }
285 /* don't allow broadcast messages to be fragmented */
286 if ((u_short)ip->ip_len > ifp->if_mtu) {
287 error = EMSGSIZE;
288 goto bad;
289 }
290 m->m_flags |= M_BCAST;
291 }
292 #ifdef MULTICAST
293 sendit:
294 #endif
295
296 /*
297 * If small enough for interface, can just send directly.
298 */
299 if ((u_short)ip->ip_len <= ifp->if_mtu) {
300 ip->ip_len = htons((u_short)ip->ip_len);
301 ip->ip_off = htons((u_short)ip->ip_off);
302 ip->ip_sum = 0;
303 ip->ip_sum = in_cksum(m, hlen);
304 error = (*ifp->if_output)(ifp, m,
305 (struct sockaddr *)dst, ro->ro_rt);
306 goto done;
307 }
308 ipstat.ips_fragmented++;
309 /*
310 * Too large for interface; fragment if possible.
311 * Must be able to put at least 8 bytes per fragment.
312 */
313 if (ip->ip_off & IP_DF) {
314 error = EMSGSIZE;
315 goto bad;
316 }
317 len = (ifp->if_mtu - hlen) &~ 7;
318 if (len < 8) {
319 error = EMSGSIZE;
320 goto bad;
321 }
322
323 {
324 int mhlen, firstlen = len;
325 struct mbuf **mnext = &m->m_nextpkt;
326
327 /*
328 * Loop through length of segment after first fragment,
329 * make new header and copy data of each part and link onto chain.
330 */
331 m0 = m;
332 mhlen = sizeof (struct ip);
333 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
334 MGETHDR(m, M_DONTWAIT, MT_HEADER);
335 if (m == 0) {
336 error = ENOBUFS;
337 goto sendorfree;
338 }
339 m->m_data += max_linkhdr;
340 mhip = mtod(m, struct ip *);
341 *mhip = *ip;
342 if (hlen > sizeof (struct ip)) {
343 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
344 mhip->ip_hl = mhlen >> 2;
345 }
346 m->m_len = mhlen;
347 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
348 if (ip->ip_off & IP_MF)
349 mhip->ip_off |= IP_MF;
350 if (off + len >= (u_short)ip->ip_len)
351 len = (u_short)ip->ip_len - off;
352 else
353 mhip->ip_off |= IP_MF;
354 mhip->ip_len = htons((u_short)(len + mhlen));
355 m->m_next = m_copy(m0, off, len);
356 if (m->m_next == 0) {
357 error = ENOBUFS; /* ??? */
358 goto sendorfree;
359 }
360 m->m_pkthdr.len = mhlen + len;
361 m->m_pkthdr.rcvif = (struct ifnet *)0;
362 mhip->ip_off = htons((u_short)mhip->ip_off);
363 mhip->ip_sum = 0;
364 mhip->ip_sum = in_cksum(m, mhlen);
365 *mnext = m;
366 mnext = &m->m_nextpkt;
367 ipstat.ips_ofragments++;
368 }
369 /*
370 * Update first fragment by trimming what's been copied out
371 * and updating header, then send each fragment (in order).
372 */
373 m = m0;
374 m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
375 m->m_pkthdr.len = hlen + firstlen;
376 ip->ip_len = htons((u_short)m->m_pkthdr.len);
377 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
378 ip->ip_sum = 0;
379 ip->ip_sum = in_cksum(m, hlen);
380 sendorfree:
381 for (m = m0; m; m = m0) {
382 m0 = m->m_nextpkt;
383 m->m_nextpkt = 0;
384 if (error == 0)
385 error = (*ifp->if_output)(ifp, m,
386 (struct sockaddr *)dst, ro->ro_rt);
387 else
388 m_freem(m);
389 }
390 }
391 done:
392 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
393 RTFREE(ro->ro_rt);
394 return (error);
395 bad:
396 m_freem(m0);
397 goto done;
398 }
399
400 /*
401 * Insert IP options into preformed packet.
402 * Adjust IP destination as required for IP source routing,
403 * as indicated by a non-zero in_addr at the start of the options.
404 */
405 struct mbuf *
406 ip_insertoptions(m, opt, phlen)
407 register struct mbuf *m;
408 struct mbuf *opt;
409 int *phlen;
410 {
411 register struct ipoption *p = mtod(opt, struct ipoption *);
412 struct mbuf *n;
413 register struct ip *ip = mtod(m, struct ip *);
414 unsigned optlen;
415
416 optlen = opt->m_len - sizeof(p->ipopt_dst);
417 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
418 return (m); /* XXX should fail */
419 if (p->ipopt_dst.s_addr)
420 ip->ip_dst = p->ipopt_dst;
421 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
422 MGETHDR(n, M_DONTWAIT, MT_HEADER);
423 if (n == 0)
424 return (m);
425 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
426 m->m_len -= sizeof(struct ip);
427 m->m_data += sizeof(struct ip);
428 n->m_next = m;
429 m = n;
430 m->m_len = optlen + sizeof(struct ip);
431 m->m_data += max_linkhdr;
432 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
433 } else {
434 m->m_data -= optlen;
435 m->m_len += optlen;
436 m->m_pkthdr.len += optlen;
437 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
438 }
439 ip = mtod(m, struct ip *);
440 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
441 *phlen = sizeof(struct ip) + optlen;
442 ip->ip_len += optlen;
443 return (m);
444 }
445
446 /*
447 * Copy options from ip to jp,
448 * omitting those not copied during fragmentation.
449 */
450 ip_optcopy(ip, jp)
451 struct ip *ip, *jp;
452 {
453 register u_char *cp, *dp;
454 int opt, optlen, cnt;
455
456 cp = (u_char *)(ip + 1);
457 dp = (u_char *)(jp + 1);
458 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
459 for (; cnt > 0; cnt -= optlen, cp += optlen) {
460 opt = cp[0];
461 if (opt == IPOPT_EOL)
462 break;
463 if (opt == IPOPT_NOP)
464 optlen = 1;
465 else
466 optlen = cp[IPOPT_OLEN];
467 /* bogus lengths should have been caught by ip_dooptions */
468 if (optlen > cnt)
469 optlen = cnt;
470 if (IPOPT_COPIED(opt)) {
471 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
472 dp += optlen;
473 }
474 }
475 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
476 *dp++ = IPOPT_EOL;
477 return (optlen);
478 }
479
480 /*
481 * IP socket option processing.
482 */
483 ip_ctloutput(op, so, level, optname, mp)
484 int op;
485 struct socket *so;
486 int level, optname;
487 struct mbuf **mp;
488 {
489 register struct inpcb *inp = sotoinpcb(so);
490 register struct mbuf *m = *mp;
491 register int optval;
492 int error = 0;
493
494 #if defined(COMPAT_OLDSOCKOPT) && !defined(MULTICAST)
495 /*
496 * XXX this is a *very* ugly hack, and should go away ASAP...
497 * note that it only affects intput to the function, not
498 * output...
499 */
500 if (op >= 2 && op <= 8)
501 op += 5;
502 #endif
503 if (level != IPPROTO_IP)
504 error = EINVAL;
505 else switch (op) {
506
507 case PRCO_SETOPT:
508 switch (optname) {
509 case IP_OPTIONS:
510 #ifdef notyet
511 case IP_RETOPTS:
512 return (ip_pcbopts(optname, &inp->inp_options, m));
513 #else
514 return (ip_pcbopts(&inp->inp_options, m));
515 #endif
516
517 case IP_TOS:
518 case IP_TTL:
519 case IP_RECVOPTS:
520 case IP_RECVRETOPTS:
521 case IP_RECVDSTADDR:
522 if (m->m_len != sizeof(int))
523 error = EINVAL;
524 else {
525 optval = *mtod(m, int *);
526 switch (optname) {
527
528 case IP_TOS:
529 inp->inp_ip.ip_tos = optval;
530 break;
531
532 case IP_TTL:
533 inp->inp_ip.ip_ttl = optval;
534 break;
535 #define OPTSET(bit) \
536 if (optval) \
537 inp->inp_flags |= bit; \
538 else \
539 inp->inp_flags &= ~bit;
540
541 case IP_RECVOPTS:
542 OPTSET(INP_RECVOPTS);
543 break;
544
545 case IP_RECVRETOPTS:
546 OPTSET(INP_RECVRETOPTS);
547 break;
548
549 case IP_RECVDSTADDR:
550 OPTSET(INP_RECVDSTADDR);
551 break;
552 }
553 }
554 break;
555 #undef OPTSET
556 #ifdef MULTICAST
557 case IP_MULTICAST_IF:
558 case IP_MULTICAST_TTL:
559 case IP_MULTICAST_LOOP:
560 case IP_ADD_MEMBERSHIP:
561 case IP_DROP_MEMBERSHIP:
562 error = ip_setmoptions(optname, &inp->inp_moptions, m);
563 break;
564 #endif
565
566 default:
567 error = EINVAL;
568 break;
569 }
570 if (m)
571 (void)m_free(m);
572 break;
573
574 case PRCO_GETOPT:
575 switch (optname) {
576 case IP_OPTIONS:
577 case IP_RETOPTS:
578 *mp = m = m_get(M_WAIT, MT_SOOPTS);
579 if (inp->inp_options) {
580 m->m_len = inp->inp_options->m_len;
581 bcopy(mtod(inp->inp_options, caddr_t),
582 mtod(m, caddr_t), (unsigned)m->m_len);
583 } else
584 m->m_len = 0;
585 break;
586
587 case IP_TOS:
588 case IP_TTL:
589 case IP_RECVOPTS:
590 case IP_RECVRETOPTS:
591 case IP_RECVDSTADDR:
592 *mp = m = m_get(M_WAIT, MT_SOOPTS);
593 m->m_len = sizeof(int);
594 switch (optname) {
595
596 case IP_TOS:
597 optval = inp->inp_ip.ip_tos;
598 break;
599
600 case IP_TTL:
601 optval = inp->inp_ip.ip_ttl;
602 break;
603
604 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
605
606 case IP_RECVOPTS:
607 optval = OPTBIT(INP_RECVOPTS);
608 break;
609
610 case IP_RECVRETOPTS:
611 optval = OPTBIT(INP_RECVRETOPTS);
612 break;
613
614 case IP_RECVDSTADDR:
615 optval = OPTBIT(INP_RECVDSTADDR);
616 break;
617 }
618 *mtod(m, int *) = optval;
619 break;
620 #ifdef MULTICAST
621 case IP_MULTICAST_IF:
622 case IP_MULTICAST_TTL:
623 case IP_MULTICAST_LOOP:
624 case IP_ADD_MEMBERSHIP:
625 case IP_DROP_MEMBERSHIP:
626 error = ip_getmoptions(optname, &inp->inp_moptions, m);
627 break;
628 #endif
629
630 default:
631 error = EINVAL;
632 break;
633 }
634 break;
635 }
636 return (error);
637 }
638
639 /*
640 * Set up IP options in pcb for insertion in output packets.
641 * Store in mbuf with pointer in pcbopt, adding pseudo-option
642 * with destination address if source routed.
643 */
644 #ifdef notyet
645 ip_pcbopts(optname, pcbopt, m)
646 int optname;
647 #else
648 ip_pcbopts(pcbopt, m)
649 #endif
650 struct mbuf **pcbopt;
651 register struct mbuf *m;
652 {
653 register cnt, optlen;
654 register u_char *cp;
655 u_char opt;
656
657 /* turn off any old options */
658 if (*pcbopt)
659 (void)m_free(*pcbopt);
660 *pcbopt = 0;
661 if (m == (struct mbuf *)0 || m->m_len == 0) {
662 /*
663 * Only turning off any previous options.
664 */
665 if (m)
666 (void)m_free(m);
667 return (0);
668 }
669
670 #ifndef vax
671 if (m->m_len % sizeof(long))
672 goto bad;
673 #endif
674 /*
675 * IP first-hop destination address will be stored before
676 * actual options; move other options back
677 * and clear it when none present.
678 */
679 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
680 goto bad;
681 cnt = m->m_len;
682 m->m_len += sizeof(struct in_addr);
683 cp = mtod(m, u_char *) + sizeof(struct in_addr);
684 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
685 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
686
687 for (; cnt > 0; cnt -= optlen, cp += optlen) {
688 opt = cp[IPOPT_OPTVAL];
689 if (opt == IPOPT_EOL)
690 break;
691 if (opt == IPOPT_NOP)
692 optlen = 1;
693 else {
694 optlen = cp[IPOPT_OLEN];
695 if (optlen <= IPOPT_OLEN || optlen > cnt)
696 goto bad;
697 }
698 switch (opt) {
699
700 default:
701 break;
702
703 case IPOPT_LSRR:
704 case IPOPT_SSRR:
705 /*
706 * user process specifies route as:
707 * ->A->B->C->D
708 * D must be our final destination (but we can't
709 * check that since we may not have connected yet).
710 * A is first hop destination, which doesn't appear in
711 * actual IP option, but is stored before the options.
712 */
713 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
714 goto bad;
715 m->m_len -= sizeof(struct in_addr);
716 cnt -= sizeof(struct in_addr);
717 optlen -= sizeof(struct in_addr);
718 cp[IPOPT_OLEN] = optlen;
719 /*
720 * Move first hop before start of options.
721 */
722 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
723 sizeof(struct in_addr));
724 /*
725 * Then copy rest of options back
726 * to close up the deleted entry.
727 */
728 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
729 sizeof(struct in_addr)),
730 (caddr_t)&cp[IPOPT_OFFSET+1],
731 (unsigned)cnt + sizeof(struct in_addr));
732 break;
733 }
734 }
735 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
736 goto bad;
737 *pcbopt = m;
738 return (0);
739
740 bad:
741 (void)m_free(m);
742 return (EINVAL);
743 }
744
745 #ifdef MULTICAST
746 /*
747 * Set the IP multicast options in response to user setsockopt().
748 */
749 int
750 ip_setmoptions(optname, imop, m)
751 int optname;
752 struct ip_moptions **imop;
753 struct mbuf *m;
754 {
755 register int error = 0;
756 u_char loop;
757 register int i;
758 struct in_addr addr;
759 register struct ip_mreq *mreq;
760 register struct ifnet *ifp;
761 register struct ip_moptions *imo = *imop;
762 struct route ro;
763 register struct sockaddr_in *dst;
764
765 if (imo == NULL) {
766 /*
767 * No multicast option buffer attached to the pcb;
768 * allocate one and initialize to default values.
769 */
770 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
771 M_WAITOK);
772
773 if (imo == NULL)
774 return (ENOBUFS);
775 *imop = imo;
776 imo->imo_multicast_ifp = NULL;
777 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
778 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
779 imo->imo_num_memberships = 0;
780 }
781
782 switch (optname) {
783
784 case IP_MULTICAST_IF:
785 /*
786 * Select the interface for outgoing multicast packets.
787 */
788 if (m == NULL || m->m_len != sizeof(struct in_addr)) {
789 error = EINVAL;
790 break;
791 }
792 addr = *(mtod(m, struct in_addr *));
793 /*
794 * INADDR_ANY is used to remove a previous selection.
795 * When no interface is selected, a default one is
796 * chosen every time a multicast packet is sent.
797 */
798 if (addr.s_addr == INADDR_ANY) {
799 imo->imo_multicast_ifp = NULL;
800 break;
801 }
802 /*
803 * The selected interface is identified by its local
804 * IP address. Find the interface and confirm that
805 * it supports multicasting.
806 */
807 INADDR_TO_IFP(addr, ifp);
808 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
809 error = EADDRNOTAVAIL;
810 break;
811 }
812 imo->imo_multicast_ifp = ifp;
813 break;
814
815 case IP_MULTICAST_TTL:
816 /*
817 * Set the IP time-to-live for outgoing multicast packets.
818 */
819 if (m == NULL || m->m_len != 1) {
820 error = EINVAL;
821 break;
822 }
823 imo->imo_multicast_ttl = *(mtod(m, u_char *));
824 break;
825
826 case IP_MULTICAST_LOOP:
827 /*
828 * Set the loopback flag for outgoing multicast packets.
829 * Must be zero or one.
830 */
831 if (m == NULL || m->m_len != 1 ||
832 (loop = *(mtod(m, u_char *))) > 1) {
833 error = EINVAL;
834 break;
835 }
836 imo->imo_multicast_loop = loop;
837 break;
838
839 case IP_ADD_MEMBERSHIP:
840 /*
841 * Add a multicast group membership.
842 * Group must be a valid IP multicast address.
843 */
844 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
845 error = EINVAL;
846 break;
847 }
848 mreq = mtod(m, struct ip_mreq *);
849 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
850 error = EINVAL;
851 break;
852 }
853 /*
854 * If no interface address was provided, use the interface of
855 * the route to the given multicast address.
856 */
857 if (mreq->imr_interface.s_addr == INADDR_ANY) {
858 ro.ro_rt = NULL;
859 dst = (struct sockaddr_in *)&ro.ro_dst;
860 dst->sin_len = sizeof(*dst);
861 dst->sin_family = AF_INET;
862 dst->sin_addr = mreq->imr_multiaddr;
863 rtalloc(&ro);
864 if (ro.ro_rt == NULL) {
865 error = EADDRNOTAVAIL;
866 break;
867 }
868 ifp = ro.ro_rt->rt_ifp;
869 rtfree(ro.ro_rt);
870 }
871 else {
872 INADDR_TO_IFP(mreq->imr_interface, ifp);
873 }
874 /*
875 * See if we found an interface, and confirm that it
876 * supports multicast.
877 */
878 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
879 error = EADDRNOTAVAIL;
880 break;
881 }
882 /*
883 * See if the membership already exists or if all the
884 * membership slots are full.
885 */
886 for (i = 0; i < imo->imo_num_memberships; ++i) {
887 if (imo->imo_membership[i]->inm_ifp == ifp &&
888 imo->imo_membership[i]->inm_addr.s_addr
889 == mreq->imr_multiaddr.s_addr)
890 break;
891 }
892 if (i < imo->imo_num_memberships) {
893 error = EADDRINUSE;
894 break;
895 }
896 if (i == IP_MAX_MEMBERSHIPS) {
897 error = ETOOMANYREFS;
898 break;
899 }
900 /*
901 * Everything looks good; add a new record to the multicast
902 * address list for the given interface.
903 */
904 if ((imo->imo_membership[i] =
905 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
906 error = ENOBUFS;
907 break;
908 }
909 ++imo->imo_num_memberships;
910 break;
911
912 case IP_DROP_MEMBERSHIP:
913 /*
914 * Drop a multicast group membership.
915 * Group must be a valid IP multicast address.
916 */
917 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
918 error = EINVAL;
919 break;
920 }
921 mreq = mtod(m, struct ip_mreq *);
922 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
923 error = EINVAL;
924 break;
925 }
926 /*
927 * If an interface address was specified, get a pointer
928 * to its ifnet structure.
929 */
930 if (mreq->imr_interface.s_addr == INADDR_ANY)
931 ifp = NULL;
932 else {
933 INADDR_TO_IFP(mreq->imr_interface, ifp);
934 if (ifp == NULL) {
935 error = EADDRNOTAVAIL;
936 break;
937 }
938 }
939 /*
940 * Find the membership in the membership array.
941 */
942 for (i = 0; i < imo->imo_num_memberships; ++i) {
943 if ((ifp == NULL ||
944 imo->imo_membership[i]->inm_ifp == ifp) &&
945 imo->imo_membership[i]->inm_addr.s_addr ==
946 mreq->imr_multiaddr.s_addr)
947 break;
948 }
949 if (i == imo->imo_num_memberships) {
950 error = EADDRNOTAVAIL;
951 break;
952 }
953 /*
954 * Give up the multicast address record to which the
955 * membership points.
956 */
957 in_delmulti(imo->imo_membership[i]);
958 /*
959 * Remove the gap in the membership array.
960 */
961 for (++i; i < imo->imo_num_memberships; ++i)
962 imo->imo_membership[i-1] = imo->imo_membership[i];
963 --imo->imo_num_memberships;
964 break;
965
966 default:
967 error = EOPNOTSUPP;
968 break;
969 }
970
971 /*
972 * If all options have default values, no need to keep the mbuf.
973 */
974 if (imo->imo_multicast_ifp == NULL &&
975 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
976 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
977 imo->imo_num_memberships == 0) {
978 free(*imop, M_IPMOPTS);
979 *imop = NULL;
980 }
981
982 return (error);
983 }
984
985 /*
986 * Return the IP multicast options in response to user getsockopt().
987 */
988 int
989 ip_getmoptions(optname, imo, mp)
990 int optname;
991 register struct ip_moptions *imo;
992 register struct mbuf **mp;
993 {
994 u_char *ttl;
995 u_char *loop;
996 struct in_addr *addr;
997 struct in_ifaddr *ia;
998
999 *mp = m_get(M_WAIT, MT_SOOPTS);
1000
1001 switch (optname) {
1002
1003 case IP_MULTICAST_IF:
1004 addr = mtod(*mp, struct in_addr *);
1005 (*mp)->m_len = sizeof(struct in_addr);
1006 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1007 addr->s_addr = INADDR_ANY;
1008 else {
1009 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1010 addr->s_addr = (ia == NULL) ? INADDR_ANY
1011 : IA_SIN(ia)->sin_addr.s_addr;
1012 }
1013 return (0);
1014
1015 case IP_MULTICAST_TTL:
1016 ttl = mtod(*mp, u_char *);
1017 (*mp)->m_len = 1;
1018 *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1019 : imo->imo_multicast_ttl;
1020 return (0);
1021
1022 case IP_MULTICAST_LOOP:
1023 loop = mtod(*mp, u_char *);
1024 (*mp)->m_len = 1;
1025 *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1026 : imo->imo_multicast_loop;
1027 return (0);
1028
1029 default:
1030 return (EOPNOTSUPP);
1031 }
1032 }
1033
1034 /*
1035 * Discard the IP multicast options.
1036 */
1037 void
1038 ip_freemoptions(imo)
1039 register struct ip_moptions *imo;
1040 {
1041 register int i;
1042
1043 if (imo != NULL) {
1044 for (i = 0; i < imo->imo_num_memberships; ++i)
1045 in_delmulti(imo->imo_membership[i]);
1046 free(imo, M_IPMOPTS);
1047 }
1048 }
1049
1050 /*
1051 * Routine called from ip_output() to loop back a copy of an IP multicast
1052 * packet to the input queue of a specified interface. Note that this
1053 * calls the output routine of the loopback "driver", but with an interface
1054 * pointer that might NOT be &loif -- easier than replicating that code here.
1055 */
1056 void
1057 ip_mloopback(ifp, m, dst)
1058 struct ifnet *ifp;
1059 register struct mbuf *m;
1060 register struct sockaddr_in *dst;
1061 {
1062 register struct ip *ip;
1063 struct mbuf *copym;
1064
1065 copym = m_copy(m, 0, M_COPYALL);
1066 if (copym != NULL) {
1067 /*
1068 * We don't bother to fragment if the IP length is greater
1069 * than the interface's MTU. Can this possibly matter?
1070 */
1071 ip = mtod(copym, struct ip *);
1072 ip->ip_len = htons((u_short)ip->ip_len);
1073 ip->ip_off = htons((u_short)ip->ip_off);
1074 ip->ip_sum = 0;
1075 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1076 (void) looutput(ifp, copym, (struct sockaddr *)dst);
1077 }
1078 }
1079 #endif
1080