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