ip_output.c revision 5543
1104349Sphk/* 2104349Sphk * Copyright (c) 1982, 1986, 1988, 1990, 1993 3104349Sphk * The Regents of the University of California. All rights reserved. 4104349Sphk * 5104349Sphk * Redistribution and use in source and binary forms, with or without 6104349Sphk * modification, are permitted provided that the following conditions 7104349Sphk * are met: 8104349Sphk * 1. Redistributions of source code must retain the above copyright 9104349Sphk * notice, this list of conditions and the following disclaimer. 10104349Sphk * 2. Redistributions in binary form must reproduce the above copyright 11104349Sphk * notice, this list of conditions and the following disclaimer in the 12104349Sphk * documentation and/or other materials provided with the distribution. 13104349Sphk * 3. All advertising materials mentioning features or use of this software 14104349Sphk * must display the following acknowledgement: 15104349Sphk * This product includes software developed by the University of 16104349Sphk * California, Berkeley and its contributors. 17104349Sphk * 4. Neither the name of the University nor the names of its contributors 18104349Sphk * may be used to endorse or promote products derived from this software 19104349Sphk * without specific prior written permission. 20104349Sphk * 21104349Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22104349Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23104349Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24104349Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25104349Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26104349Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27104349Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28104349Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29104349Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30104349Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31104349Sphk * SUCH DAMAGE. 32104349Sphk * 33178848Scokane * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 34247296Sdelphij * $Id: ip_output.c,v 1.11 1994/12/13 23:08:12 wollman Exp $ 35104349Sphk */ 36104349Sphk 37104349Sphk#include <sys/param.h> 38104349Sphk#include <sys/systm.h> 39104349Sphk#include <sys/malloc.h> 40104349Sphk#include <sys/mbuf.h> 41104349Sphk#include <sys/errno.h> 42104349Sphk#include <sys/protosw.h> 43104349Sphk#include <sys/socket.h> 44104349Sphk#include <sys/socketvar.h> 45178848Scokane 46104349Sphk#include <net/if.h> 47104349Sphk#include <net/route.h> 48178848Scokane 49104349Sphk#include <netinet/in.h> 50247296Sdelphij#include <netinet/in_systm.h> 51104349Sphk#include <netinet/ip.h> 52247296Sdelphij#include <netinet/in_pcb.h> 53104349Sphk#include <netinet/in_var.h> 54247296Sdelphij#include <netinet/ip_var.h> 55104349Sphk 56104349Sphk#include <netinet/ip_fw.h> 57247296Sdelphij 58247296Sdelphij#ifdef vax 59247296Sdelphij#include <machine/mtpr.h> 60247296Sdelphij#endif 61247296Sdelphij 62104349Sphku_short ip_id; 63178848Scokane 64104349Sphkstatic struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *)); 65178848Scokanestatic void ip_mloopback 66178848Scokane __P((struct ifnet *, struct mbuf *, struct sockaddr_in *)); 67104349Sphk 68104349Sphk/* 69247296Sdelphij * IP output. The packet in mbuf chain m contains a skeletal IP 70104349Sphk * header (with len, off, ttl, proto, tos, src, dst). 71104349Sphk * The mbuf chain containing the packet will be freed. 72104349Sphk * The mbuf opt, if present, will not be freed. 73247296Sdelphij */ 74247296Sdelphijint 75104349Sphkip_output(m0, opt, ro, flags, imo) 76178848Scokane struct mbuf *m0; 77104349Sphk struct mbuf *opt; 78178848Scokane struct route *ro; 79104349Sphk int flags; 80247296Sdelphij struct ip_moptions *imo; 81178848Scokane{ 82247296Sdelphij register struct ip *ip, *mhip; 83178848Scokane register struct ifnet *ifp; 84104349Sphk register struct mbuf *m = m0; 85247296Sdelphij register int hlen = sizeof (struct ip); 86247296Sdelphij int len, off, error = 0; 87178848Scokane struct route iproute; 88178848Scokane struct sockaddr_in *dst; 89247296Sdelphij struct in_ifaddr *ia; 90104349Sphk 91104349Sphk#ifdef DIAGNOSTIC 92247296Sdelphij if ((m->m_flags & M_PKTHDR) == 0) 93178848Scokane panic("ip_output no HDR"); 94104349Sphk#endif 95104349Sphk if (opt) { 96178848Scokane m = ip_insertoptions(m, opt, &len); 97178848Scokane hlen = len; 98178848Scokane } 99247296Sdelphij ip = mtod(m, struct ip *); 100104349Sphk /* 101104349Sphk * Fill in IP header. 102104349Sphk */ 103104349Sphk if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 104104349Sphk ip->ip_v = IPVERSION; 105104349Sphk ip->ip_off &= IP_DF; 106104349Sphk ip->ip_id = htons(ip_id++); 107104349Sphk ip->ip_hl = hlen >> 2; 108178848Scokane ipstat.ips_localout++; 109104349Sphk } else { 110104349Sphk hlen = ip->ip_hl << 2; 111104349Sphk } 112104349Sphk /* 113178848Scokane * Route packet. 114104349Sphk */ 115178848Scokane if (ro == 0) { 116104349Sphk ro = &iproute; 117104349Sphk bzero((caddr_t)ro, sizeof (*ro)); 118104349Sphk } 119104349Sphk dst = (struct sockaddr_in *)&ro->ro_dst; 120104349Sphk /* 121178848Scokane * If there is a cached route, 122178848Scokane * check that it is to the same destination 123104349Sphk * and is still up. If not, free it and try again. 124104349Sphk */ 125104349Sphk if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 126178848Scokane dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 127104349Sphk RTFREE(ro->ro_rt); 128104349Sphk ro->ro_rt = (struct rtentry *)0; 129104349Sphk } 130104349Sphk if (ro->ro_rt == 0) { 131104349Sphk dst->sin_family = AF_INET; 132247296Sdelphij dst->sin_len = sizeof(*dst); 133247296Sdelphij dst->sin_addr = ip->ip_dst; 134247296Sdelphij } 135104349Sphk /* 136178848Scokane * If routing to interface only, 137104349Sphk * short circuit routing lookup. 138104349Sphk */ 139178848Scokane#define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) 140104349Sphk#define sintosa(sin) ((struct sockaddr *)(sin)) 141104349Sphk if (flags & IP_ROUTETOIF) { 142104349Sphk if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 && 143104349Sphk (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) { 144178848Scokane ipstat.ips_noroute++; 145104349Sphk error = ENETUNREACH; 146104349Sphk goto bad; 147247296Sdelphij } 148247296Sdelphij ifp = ia->ia_ifp; 149247296Sdelphij ip->ip_ttl = 1; 150247296Sdelphij } else { 151247296Sdelphij /* 152247296Sdelphij * If this is the case, we probably don't want to allocate 153104349Sphk * a protocol-cloned route since we didn't get one from the 154104349Sphk * ULP. This lets TCP do its thing, while not burdening 155247296Sdelphij * forwarding or ICMP with the overhead of cloning a route. 156247296Sdelphij * Of course, we still want to do any cloning requested by 157104349Sphk * the link layer, as this is probably required in all cases 158104349Sphk * for correct operation (as it is for ARP). 159247296Sdelphij */ 160247296Sdelphij if (ro->ro_rt == 0) 161104349Sphk rtalloc_ign(ro, RTF_PRCLONING); 162104349Sphk if (ro->ro_rt == 0) { 163247296Sdelphij ipstat.ips_noroute++; 164247296Sdelphij error = EHOSTUNREACH; 165247296Sdelphij goto bad; 166247296Sdelphij } 167247296Sdelphij ia = ifatoia(ro->ro_rt->rt_ifa); 168247296Sdelphij ifp = ro->ro_rt->rt_ifp; 169247296Sdelphij ro->ro_rt->rt_use++; 170247296Sdelphij if (ro->ro_rt->rt_flags & RTF_GATEWAY) 171104349Sphk dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 172247296Sdelphij } 173247296Sdelphij if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 174247296Sdelphij struct in_multi *inm; 175247296Sdelphij extern struct ifnet loif; 176247296Sdelphij 177247296Sdelphij m->m_flags |= M_MCAST; 178247296Sdelphij /* 179104349Sphk * IP destination address is multicast. Make sure "dst" 180104349Sphk * still points to the address in "ro". (It may have been 181247296Sdelphij * changed to point to a gateway address, above.) 182104349Sphk */ 183104349Sphk dst = (struct sockaddr_in *)&ro->ro_dst; 184104349Sphk /* 185104349Sphk * See if the caller provided any multicast options 186247296Sdelphij */ 187104349Sphk if (imo != NULL) { 188104349Sphk ip->ip_ttl = imo->imo_multicast_ttl; 189247296Sdelphij if (imo->imo_multicast_ifp != NULL) 190104349Sphk ifp = imo->imo_multicast_ifp; 191247296Sdelphij } else 192178848Scokane ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 193247296Sdelphij /* 194104349Sphk * Confirm that the outgoing interface supports multicast. 195104349Sphk */ 196104349Sphk if ((ifp->if_flags & IFF_MULTICAST) == 0) { 197104349Sphk ipstat.ips_noroute++; 198104349Sphk error = ENETUNREACH; 199104349Sphk goto bad; 200104349Sphk } 201104349Sphk /* 202 * If source address not specified yet, use address 203 * of outgoing interface. 204 */ 205 if (ip->ip_src.s_addr == INADDR_ANY) { 206 register struct in_ifaddr *ia; 207 208 for (ia = in_ifaddr; ia; ia = ia->ia_next) 209 if (ia->ia_ifp == ifp) { 210 ip->ip_src = IA_SIN(ia)->sin_addr; 211 break; 212 } 213 } 214 215 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 216 if (inm != NULL && 217 (imo == NULL || imo->imo_multicast_loop)) { 218 /* 219 * If we belong to the destination multicast group 220 * on the outgoing interface, and the caller did not 221 * forbid loopback, loop back a copy. 222 */ 223 ip_mloopback(ifp, m, dst); 224 } 225 else { 226 /* 227 * If we are acting as a multicast router, perform 228 * multicast forwarding as if the packet had just 229 * arrived on the interface to which we are about 230 * to send. The multicast forwarding function 231 * recursively calls this function, using the 232 * IP_FORWARDING flag to prevent infinite recursion. 233 * 234 * Multicasts that are looped back by ip_mloopback(), 235 * above, will be forwarded by the ip_input() routine, 236 * if necessary. 237 */ 238 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 239 /* 240 * Check if rsvp daemon is running. If not, don't 241 * set ip_moptions. This ensures that the packet 242 * is multicast and not just sent down one link 243 * as prescribed by rsvpd. 244 */ 245 if (ip_rsvpd == NULL) 246 imo = NULL; 247 if (ip_mforward(ip, ifp, m, imo) != 0) { 248 m_freem(m); 249 goto done; 250 } 251 } 252 } 253 254 /* 255 * Multicasts with a time-to-live of zero may be looped- 256 * back, above, but must not be transmitted on a network. 257 * Also, multicasts addressed to the loopback interface 258 * are not sent -- the above call to ip_mloopback() will 259 * loop back a copy if this host actually belongs to the 260 * destination group on the loopback interface. 261 */ 262 if (ip->ip_ttl == 0 || ifp == &loif) { 263 m_freem(m); 264 goto done; 265 } 266 267 goto sendit; 268 } 269#ifndef notdef 270 /* 271 * If source address not specified yet, use address 272 * of outgoing interface. 273 */ 274 if (ip->ip_src.s_addr == INADDR_ANY) 275 ip->ip_src = IA_SIN(ia)->sin_addr; 276#endif 277 /* 278 * Verify that we have any chance at all of being able to queue 279 * the packet or packet fragments 280 */ 281 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 282 ifp->if_snd.ifq_maxlen) { 283 error = ENOBUFS; 284 goto bad; 285 } 286 287 /* 288 * Look for broadcast address and 289 * and verify user is allowed to send 290 * such a packet. 291 */ 292 if (in_broadcast(dst->sin_addr, ifp)) { 293 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 294 error = EADDRNOTAVAIL; 295 goto bad; 296 } 297 if ((flags & IP_ALLOWBROADCAST) == 0) { 298 error = EACCES; 299 goto bad; 300 } 301 /* don't allow broadcast messages to be fragmented */ 302 if ((u_short)ip->ip_len > ifp->if_mtu) { 303 error = EMSGSIZE; 304 goto bad; 305 } 306 m->m_flags |= M_BCAST; 307 } else 308 m->m_flags &= ~M_BCAST; 309 310sendit: 311 /* 312 * If small enough for interface, can just send directly. 313 */ 314 if ((u_short)ip->ip_len <= ifp->if_mtu) { 315 ip->ip_len = htons((u_short)ip->ip_len); 316 ip->ip_off = htons((u_short)ip->ip_off); 317 ip->ip_sum = 0; 318 ip->ip_sum = in_cksum(m, hlen); 319 error = (*ifp->if_output)(ifp, m, 320 (struct sockaddr *)dst, ro->ro_rt); 321 goto done; 322 } 323 /* 324 * Too large for interface; fragment if possible. 325 * Must be able to put at least 8 bytes per fragment. 326 */ 327 if (ip->ip_off & IP_DF) { 328 error = EMSGSIZE; 329 ipstat.ips_cantfrag++; 330 goto bad; 331 } 332 len = (ifp->if_mtu - hlen) &~ 7; 333 if (len < 8) { 334 error = EMSGSIZE; 335 goto bad; 336 } 337 338 { 339 int mhlen, firstlen = len; 340 struct mbuf **mnext = &m->m_nextpkt; 341 342 /* 343 * Loop through length of segment after first fragment, 344 * make new header and copy data of each part and link onto chain. 345 */ 346 m0 = m; 347 mhlen = sizeof (struct ip); 348 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { 349 MGETHDR(m, M_DONTWAIT, MT_HEADER); 350 if (m == 0) { 351 error = ENOBUFS; 352 ipstat.ips_odropped++; 353 goto sendorfree; 354 } 355 m->m_data += max_linkhdr; 356 mhip = mtod(m, struct ip *); 357 *mhip = *ip; 358 if (hlen > sizeof (struct ip)) { 359 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 360 mhip->ip_hl = mhlen >> 2; 361 } 362 m->m_len = mhlen; 363 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); 364 if (ip->ip_off & IP_MF) 365 mhip->ip_off |= IP_MF; 366 if (off + len >= (u_short)ip->ip_len) 367 len = (u_short)ip->ip_len - off; 368 else 369 mhip->ip_off |= IP_MF; 370 mhip->ip_len = htons((u_short)(len + mhlen)); 371 m->m_next = m_copy(m0, off, len); 372 if (m->m_next == 0) { 373 (void) m_free(m); 374 error = ENOBUFS; /* ??? */ 375 ipstat.ips_odropped++; 376 goto sendorfree; 377 } 378 m->m_pkthdr.len = mhlen + len; 379 m->m_pkthdr.rcvif = (struct ifnet *)0; 380 mhip->ip_off = htons((u_short)mhip->ip_off); 381 mhip->ip_sum = 0; 382 mhip->ip_sum = in_cksum(m, mhlen); 383 *mnext = m; 384 mnext = &m->m_nextpkt; 385 ipstat.ips_ofragments++; 386 } 387 /* 388 * Update first fragment by trimming what's been copied out 389 * and updating header, then send each fragment (in order). 390 */ 391 m = m0; 392 m_adj(m, hlen + firstlen - (u_short)ip->ip_len); 393 m->m_pkthdr.len = hlen + firstlen; 394 ip->ip_len = htons((u_short)m->m_pkthdr.len); 395 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF)); 396 ip->ip_sum = 0; 397 ip->ip_sum = in_cksum(m, hlen); 398sendorfree: 399 for (m = m0; m; m = m0) { 400 m0 = m->m_nextpkt; 401 m->m_nextpkt = 0; 402 if (error == 0) 403 error = (*ifp->if_output)(ifp, m, 404 (struct sockaddr *)dst, ro->ro_rt); 405 else 406 m_freem(m); 407 } 408 409 if (error == 0) 410 ipstat.ips_fragmented++; 411 } 412done: 413 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) 414 RTFREE(ro->ro_rt); 415 /* 416 * Count outgoing packet,here we count both our packets and 417 * those we forward. 418 * Here we want to convert ip_len to host byte order when counting 419 * so we set 3rd arg to 1. 420 * This is locally generated packet so it has not 421 * incoming interface. 422 */ 423 if (ip_acct_cnt_ptr!=NULL) 424 (*ip_acct_cnt_ptr)(ip,NULL,ip_acct_chain,1); 425 426 return (error); 427bad: 428 m_freem(m0); 429 goto done; 430} 431 432/* 433 * Insert IP options into preformed packet. 434 * Adjust IP destination as required for IP source routing, 435 * as indicated by a non-zero in_addr at the start of the options. 436 */ 437static struct mbuf * 438ip_insertoptions(m, opt, phlen) 439 register struct mbuf *m; 440 struct mbuf *opt; 441 int *phlen; 442{ 443 register struct ipoption *p = mtod(opt, struct ipoption *); 444 struct mbuf *n; 445 register struct ip *ip = mtod(m, struct ip *); 446 unsigned optlen; 447 448 optlen = opt->m_len - sizeof(p->ipopt_dst); 449 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) 450 return (m); /* XXX should fail */ 451 if (p->ipopt_dst.s_addr) 452 ip->ip_dst = p->ipopt_dst; 453 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 454 MGETHDR(n, M_DONTWAIT, MT_HEADER); 455 if (n == 0) 456 return (m); 457 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 458 m->m_len -= sizeof(struct ip); 459 m->m_data += sizeof(struct ip); 460 n->m_next = m; 461 m = n; 462 m->m_len = optlen + sizeof(struct ip); 463 m->m_data += max_linkhdr; 464 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 465 } else { 466 m->m_data -= optlen; 467 m->m_len += optlen; 468 m->m_pkthdr.len += optlen; 469 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 470 } 471 ip = mtod(m, struct ip *); 472 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen); 473 *phlen = sizeof(struct ip) + optlen; 474 ip->ip_len += optlen; 475 return (m); 476} 477 478/* 479 * Copy options from ip to jp, 480 * omitting those not copied during fragmentation. 481 */ 482int 483ip_optcopy(ip, jp) 484 struct ip *ip, *jp; 485{ 486 register u_char *cp, *dp; 487 int opt, optlen, cnt; 488 489 cp = (u_char *)(ip + 1); 490 dp = (u_char *)(jp + 1); 491 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 492 for (; cnt > 0; cnt -= optlen, cp += optlen) { 493 opt = cp[0]; 494 if (opt == IPOPT_EOL) 495 break; 496 if (opt == IPOPT_NOP) { 497 /* Preserve for IP mcast tunnel's LSRR alignment. */ 498 *dp++ = IPOPT_NOP; 499 optlen = 1; 500 continue; 501 } else 502 optlen = cp[IPOPT_OLEN]; 503 /* bogus lengths should have been caught by ip_dooptions */ 504 if (optlen > cnt) 505 optlen = cnt; 506 if (IPOPT_COPIED(opt)) { 507 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 508 dp += optlen; 509 } 510 } 511 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 512 *dp++ = IPOPT_EOL; 513 return (optlen); 514} 515 516/* 517 * IP socket option processing. 518 */ 519int 520ip_ctloutput(op, so, level, optname, mp) 521 int op; 522 struct socket *so; 523 int level, optname; 524 struct mbuf **mp; 525{ 526 register struct inpcb *inp = sotoinpcb(so); 527 register struct mbuf *m = *mp; 528 register int optval = 0; 529 int error = 0; 530 531 if (level != IPPROTO_IP) { 532 error = EINVAL; 533 if (op == PRCO_SETOPT && *mp) 534 (void) m_free(*mp); 535 } else switch (op) { 536 537 case PRCO_SETOPT: 538 switch (optname) { 539 case IP_OPTIONS: 540#ifdef notyet 541 case IP_RETOPTS: 542 return (ip_pcbopts(optname, &inp->inp_options, m)); 543#else 544 return (ip_pcbopts(&inp->inp_options, m)); 545#endif 546 547 case IP_TOS: 548 case IP_TTL: 549 case IP_RECVOPTS: 550 case IP_RECVRETOPTS: 551 case IP_RECVDSTADDR: 552 if (m->m_len != sizeof(int)) 553 error = EINVAL; 554 else { 555 optval = *mtod(m, int *); 556 switch (optname) { 557 558 case IP_TOS: 559 inp->inp_ip.ip_tos = optval; 560 break; 561 562 case IP_TTL: 563 inp->inp_ip.ip_ttl = optval; 564 break; 565#define OPTSET(bit) \ 566 if (optval) \ 567 inp->inp_flags |= bit; \ 568 else \ 569 inp->inp_flags &= ~bit; 570 571 case IP_RECVOPTS: 572 OPTSET(INP_RECVOPTS); 573 break; 574 575 case IP_RECVRETOPTS: 576 OPTSET(INP_RECVRETOPTS); 577 break; 578 579 case IP_RECVDSTADDR: 580 OPTSET(INP_RECVDSTADDR); 581 break; 582 } 583 } 584 break; 585#undef OPTSET 586 587 case IP_MULTICAST_IF: 588 case IP_MULTICAST_VIF: 589 case IP_MULTICAST_TTL: 590 case IP_MULTICAST_LOOP: 591 case IP_ADD_MEMBERSHIP: 592 case IP_DROP_MEMBERSHIP: 593 error = ip_setmoptions(optname, &inp->inp_moptions, m); 594 break; 595 596 default: 597 error = ENOPROTOOPT; 598 break; 599 } 600 if (m) 601 (void)m_free(m); 602 break; 603 604 case PRCO_GETOPT: 605 switch (optname) { 606 case IP_OPTIONS: 607 case IP_RETOPTS: 608 *mp = m = m_get(M_WAIT, MT_SOOPTS); 609 if (inp->inp_options) { 610 m->m_len = inp->inp_options->m_len; 611 bcopy(mtod(inp->inp_options, caddr_t), 612 mtod(m, caddr_t), (unsigned)m->m_len); 613 } else 614 m->m_len = 0; 615 break; 616 617 case IP_TOS: 618 case IP_TTL: 619 case IP_RECVOPTS: 620 case IP_RECVRETOPTS: 621 case IP_RECVDSTADDR: 622 *mp = m = m_get(M_WAIT, MT_SOOPTS); 623 m->m_len = sizeof(int); 624 switch (optname) { 625 626 case IP_TOS: 627 optval = inp->inp_ip.ip_tos; 628 break; 629 630 case IP_TTL: 631 optval = inp->inp_ip.ip_ttl; 632 break; 633 634#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 635 636 case IP_RECVOPTS: 637 optval = OPTBIT(INP_RECVOPTS); 638 break; 639 640 case IP_RECVRETOPTS: 641 optval = OPTBIT(INP_RECVRETOPTS); 642 break; 643 644 case IP_RECVDSTADDR: 645 optval = OPTBIT(INP_RECVDSTADDR); 646 break; 647 } 648 *mtod(m, int *) = optval; 649 break; 650 651 case IP_MULTICAST_IF: 652 case IP_MULTICAST_VIF: 653 case IP_MULTICAST_TTL: 654 case IP_MULTICAST_LOOP: 655 case IP_ADD_MEMBERSHIP: 656 case IP_DROP_MEMBERSHIP: 657 error = ip_getmoptions(optname, inp->inp_moptions, mp); 658 break; 659 660 default: 661 error = ENOPROTOOPT; 662 break; 663 } 664 break; 665 } 666 return (error); 667} 668 669/* 670 * Set up IP options in pcb for insertion in output packets. 671 * Store in mbuf with pointer in pcbopt, adding pseudo-option 672 * with destination address if source routed. 673 */ 674int 675#ifdef notyet 676ip_pcbopts(optname, pcbopt, m) 677 int optname; 678#else 679ip_pcbopts(pcbopt, m) 680#endif 681 struct mbuf **pcbopt; 682 register struct mbuf *m; 683{ 684 register cnt, optlen; 685 register u_char *cp; 686 u_char opt; 687 688 /* turn off any old options */ 689 if (*pcbopt) 690 (void)m_free(*pcbopt); 691 *pcbopt = 0; 692 if (m == (struct mbuf *)0 || m->m_len == 0) { 693 /* 694 * Only turning off any previous options. 695 */ 696 if (m) 697 (void)m_free(m); 698 return (0); 699 } 700 701#ifndef vax 702 if (m->m_len % sizeof(long)) 703 goto bad; 704#endif 705 /* 706 * IP first-hop destination address will be stored before 707 * actual options; move other options back 708 * and clear it when none present. 709 */ 710 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 711 goto bad; 712 cnt = m->m_len; 713 m->m_len += sizeof(struct in_addr); 714 cp = mtod(m, u_char *) + sizeof(struct in_addr); 715 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 716 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 717 718 for (; cnt > 0; cnt -= optlen, cp += optlen) { 719 opt = cp[IPOPT_OPTVAL]; 720 if (opt == IPOPT_EOL) 721 break; 722 if (opt == IPOPT_NOP) 723 optlen = 1; 724 else { 725 optlen = cp[IPOPT_OLEN]; 726 if (optlen <= IPOPT_OLEN || optlen > cnt) 727 goto bad; 728 } 729 switch (opt) { 730 731 default: 732 break; 733 734 case IPOPT_LSRR: 735 case IPOPT_SSRR: 736 /* 737 * user process specifies route as: 738 * ->A->B->C->D 739 * D must be our final destination (but we can't 740 * check that since we may not have connected yet). 741 * A is first hop destination, which doesn't appear in 742 * actual IP option, but is stored before the options. 743 */ 744 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 745 goto bad; 746 m->m_len -= sizeof(struct in_addr); 747 cnt -= sizeof(struct in_addr); 748 optlen -= sizeof(struct in_addr); 749 cp[IPOPT_OLEN] = optlen; 750 /* 751 * Move first hop before start of options. 752 */ 753 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 754 sizeof(struct in_addr)); 755 /* 756 * Then copy rest of options back 757 * to close up the deleted entry. 758 */ 759 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 760 sizeof(struct in_addr)), 761 (caddr_t)&cp[IPOPT_OFFSET+1], 762 (unsigned)cnt + sizeof(struct in_addr)); 763 break; 764 } 765 } 766 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 767 goto bad; 768 *pcbopt = m; 769 return (0); 770 771bad: 772 (void)m_free(m); 773 return (EINVAL); 774} 775 776/* 777 * Set the IP multicast options in response to user setsockopt(). 778 */ 779int 780ip_setmoptions(optname, imop, m) 781 int optname; 782 struct ip_moptions **imop; 783 struct mbuf *m; 784{ 785 register int error = 0; 786 u_char loop; 787 register int i; 788 struct in_addr addr; 789 register struct ip_mreq *mreq; 790 register struct ifnet *ifp; 791 register struct ip_moptions *imo = *imop; 792 struct route ro; 793 register struct sockaddr_in *dst; 794 795 if (imo == NULL) { 796 /* 797 * No multicast option buffer attached to the pcb; 798 * allocate one and initialize to default values. 799 */ 800 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 801 M_WAITOK); 802 803 if (imo == NULL) 804 return (ENOBUFS); 805 *imop = imo; 806 imo->imo_multicast_ifp = NULL; 807 imo->imo_multicast_vif = 0; 808 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 809 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 810 imo->imo_num_memberships = 0; 811 } 812 813 switch (optname) { 814 extern int (*legal_vif_num)(int); 815 /* store an index number for the vif you wanna use in the send */ 816 case IP_MULTICAST_VIF: 817 if (!legal_vif_num) { 818 error = EOPNOTSUPP; 819 break; 820 } 821 if (m == NULL || m->m_len != sizeof(int)) { 822 error = EINVAL; 823 break; 824 } 825 i = *(mtod(m, int *)); 826 if (!legal_vif_num(i)) { 827 error = EINVAL; 828 break; 829 } 830 imo->imo_multicast_vif = i; 831 break; 832 833 case IP_MULTICAST_IF: 834 /* 835 * Select the interface for outgoing multicast packets. 836 */ 837 if (m == NULL || m->m_len != sizeof(struct in_addr)) { 838 error = EINVAL; 839 break; 840 } 841 addr = *(mtod(m, struct in_addr *)); 842 /* 843 * INADDR_ANY is used to remove a previous selection. 844 * When no interface is selected, a default one is 845 * chosen every time a multicast packet is sent. 846 */ 847 if (addr.s_addr == INADDR_ANY) { 848 imo->imo_multicast_ifp = NULL; 849 break; 850 } 851 /* 852 * The selected interface is identified by its local 853 * IP address. Find the interface and confirm that 854 * it supports multicasting. 855 */ 856 INADDR_TO_IFP(addr, ifp); 857 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 858 error = EADDRNOTAVAIL; 859 break; 860 } 861 imo->imo_multicast_ifp = ifp; 862 break; 863 864 case IP_MULTICAST_TTL: 865 /* 866 * Set the IP time-to-live for outgoing multicast packets. 867 */ 868 if (m == NULL || m->m_len != 1) { 869 error = EINVAL; 870 break; 871 } 872 imo->imo_multicast_ttl = *(mtod(m, u_char *)); 873 break; 874 875 case IP_MULTICAST_LOOP: 876 /* 877 * Set the loopback flag for outgoing multicast packets. 878 * Must be zero or one. 879 */ 880 if (m == NULL || m->m_len != 1 || 881 (loop = *(mtod(m, u_char *))) > 1) { 882 error = EINVAL; 883 break; 884 } 885 imo->imo_multicast_loop = loop; 886 break; 887 888 case IP_ADD_MEMBERSHIP: 889 /* 890 * Add a multicast group membership. 891 * Group must be a valid IP multicast address. 892 */ 893 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 894 error = EINVAL; 895 break; 896 } 897 mreq = mtod(m, struct ip_mreq *); 898 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) { 899 error = EINVAL; 900 break; 901 } 902 /* 903 * If no interface address was provided, use the interface of 904 * the route to the given multicast address. 905 */ 906 if (mreq->imr_interface.s_addr == INADDR_ANY) { 907 ro.ro_rt = NULL; 908 dst = (struct sockaddr_in *)&ro.ro_dst; 909 dst->sin_len = sizeof(*dst); 910 dst->sin_family = AF_INET; 911 dst->sin_addr = mreq->imr_multiaddr; 912 rtalloc(&ro); 913 if (ro.ro_rt == NULL) { 914 error = EADDRNOTAVAIL; 915 break; 916 } 917 ifp = ro.ro_rt->rt_ifp; 918 rtfree(ro.ro_rt); 919 } 920 else { 921 INADDR_TO_IFP(mreq->imr_interface, ifp); 922 } 923 /* 924 * See if we found an interface, and confirm that it 925 * supports multicast. 926 */ 927 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 928 error = EADDRNOTAVAIL; 929 break; 930 } 931 /* 932 * See if the membership already exists or if all the 933 * membership slots are full. 934 */ 935 for (i = 0; i < imo->imo_num_memberships; ++i) { 936 if (imo->imo_membership[i]->inm_ifp == ifp && 937 imo->imo_membership[i]->inm_addr.s_addr 938 == mreq->imr_multiaddr.s_addr) 939 break; 940 } 941 if (i < imo->imo_num_memberships) { 942 error = EADDRINUSE; 943 break; 944 } 945 if (i == IP_MAX_MEMBERSHIPS) { 946 error = ETOOMANYREFS; 947 break; 948 } 949 /* 950 * Everything looks good; add a new record to the multicast 951 * address list for the given interface. 952 */ 953 if ((imo->imo_membership[i] = 954 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) { 955 error = ENOBUFS; 956 break; 957 } 958 ++imo->imo_num_memberships; 959 break; 960 961 case IP_DROP_MEMBERSHIP: 962 /* 963 * Drop a multicast group membership. 964 * Group must be a valid IP multicast address. 965 */ 966 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 967 error = EINVAL; 968 break; 969 } 970 mreq = mtod(m, struct ip_mreq *); 971 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) { 972 error = EINVAL; 973 break; 974 } 975 /* 976 * If an interface address was specified, get a pointer 977 * to its ifnet structure. 978 */ 979 if (mreq->imr_interface.s_addr == INADDR_ANY) 980 ifp = NULL; 981 else { 982 INADDR_TO_IFP(mreq->imr_interface, ifp); 983 if (ifp == NULL) { 984 error = EADDRNOTAVAIL; 985 break; 986 } 987 } 988 /* 989 * Find the membership in the membership array. 990 */ 991 for (i = 0; i < imo->imo_num_memberships; ++i) { 992 if ((ifp == NULL || 993 imo->imo_membership[i]->inm_ifp == ifp) && 994 imo->imo_membership[i]->inm_addr.s_addr == 995 mreq->imr_multiaddr.s_addr) 996 break; 997 } 998 if (i == imo->imo_num_memberships) { 999 error = EADDRNOTAVAIL; 1000 break; 1001 } 1002 /* 1003 * Give up the multicast address record to which the 1004 * membership points. 1005 */ 1006 in_delmulti(imo->imo_membership[i]); 1007 /* 1008 * Remove the gap in the membership array. 1009 */ 1010 for (++i; i < imo->imo_num_memberships; ++i) 1011 imo->imo_membership[i-1] = imo->imo_membership[i]; 1012 --imo->imo_num_memberships; 1013 break; 1014 1015 default: 1016 error = EOPNOTSUPP; 1017 break; 1018 } 1019 1020 /* 1021 * If all options have default values, no need to keep the mbuf. 1022 */ 1023 if (imo->imo_multicast_ifp == NULL && 1024 imo->imo_multicast_vif == 0 && 1025 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1026 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1027 imo->imo_num_memberships == 0) { 1028 free(*imop, M_IPMOPTS); 1029 *imop = NULL; 1030 } 1031 1032 return (error); 1033} 1034 1035/* 1036 * Return the IP multicast options in response to user getsockopt(). 1037 */ 1038int 1039ip_getmoptions(optname, imo, mp) 1040 int optname; 1041 register struct ip_moptions *imo; 1042 register struct mbuf **mp; 1043{ 1044 u_char *ttl; 1045 u_char *loop; 1046 struct in_addr *addr; 1047 struct in_ifaddr *ia; 1048 1049 *mp = m_get(M_WAIT, MT_SOOPTS); 1050 1051 switch (optname) { 1052 1053 case IP_MULTICAST_VIF: 1054 if (imo != NULL) 1055 *(mtod(*mp, int *)) = imo->imo_multicast_vif; 1056 else 1057 *(mtod(*mp, int *)) = 7890; 1058 (*mp)->m_len = sizeof(int); 1059 return(0); 1060 1061 case IP_MULTICAST_IF: 1062 addr = mtod(*mp, struct in_addr *); 1063 (*mp)->m_len = sizeof(struct in_addr); 1064 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1065 addr->s_addr = INADDR_ANY; 1066 else { 1067 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1068 addr->s_addr = (ia == NULL) ? INADDR_ANY 1069 : IA_SIN(ia)->sin_addr.s_addr; 1070 } 1071 return (0); 1072 1073 case IP_MULTICAST_TTL: 1074 ttl = mtod(*mp, u_char *); 1075 (*mp)->m_len = 1; 1076 *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL 1077 : imo->imo_multicast_ttl; 1078 return (0); 1079 1080 case IP_MULTICAST_LOOP: 1081 loop = mtod(*mp, u_char *); 1082 (*mp)->m_len = 1; 1083 *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP 1084 : imo->imo_multicast_loop; 1085 return (0); 1086 1087 default: 1088 return (EOPNOTSUPP); 1089 } 1090} 1091 1092/* 1093 * Discard the IP multicast options. 1094 */ 1095void 1096ip_freemoptions(imo) 1097 register struct ip_moptions *imo; 1098{ 1099 register int i; 1100 1101 if (imo != NULL) { 1102 for (i = 0; i < imo->imo_num_memberships; ++i) 1103 in_delmulti(imo->imo_membership[i]); 1104 free(imo, M_IPMOPTS); 1105 } 1106} 1107 1108/* 1109 * Routine called from ip_output() to loop back a copy of an IP multicast 1110 * packet to the input queue of a specified interface. Note that this 1111 * calls the output routine of the loopback "driver", but with an interface 1112 * pointer that might NOT be &loif -- easier than replicating that code here. 1113 */ 1114static void 1115ip_mloopback(ifp, m, dst) 1116 struct ifnet *ifp; 1117 register struct mbuf *m; 1118 register struct sockaddr_in *dst; 1119{ 1120 register struct ip *ip; 1121 struct mbuf *copym; 1122 1123 copym = m_copy(m, 0, M_COPYALL); 1124 if (copym != NULL) { 1125 /* 1126 * We don't bother to fragment if the IP length is greater 1127 * than the interface's MTU. Can this possibly matter? 1128 */ 1129 ip = mtod(copym, struct ip *); 1130 ip->ip_len = htons((u_short)ip->ip_len); 1131 ip->ip_off = htons((u_short)ip->ip_off); 1132 ip->ip_sum = 0; 1133 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 1134 (void) looutput(ifp, copym, (struct sockaddr *)dst, NULL); 1135 } 1136} 1137