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