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