ip_output.c revision 14195
1139790Simp/* 266633Sdfr * Copyright (c) 1982, 1986, 1988, 1990, 1993 366633Sdfr * The Regents of the University of California. All rights reserved. 466633Sdfr * 566633Sdfr * Redistribution and use in source and binary forms, with or without 666633Sdfr * modification, are permitted provided that the following conditions 766633Sdfr * are met: 866633Sdfr * 1. Redistributions of source code must retain the above copyright 966633Sdfr * notice, this list of conditions and the following disclaimer. 1066633Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1166633Sdfr * notice, this list of conditions and the following disclaimer in the 1266633Sdfr * documentation and/or other materials provided with the distribution. 1366633Sdfr * 3. All advertising materials mentioning features or use of this software 1474031Sdfr * must display the following acknowledgement: 1566633Sdfr * This product includes software developed by the University of 1666633Sdfr * California, Berkeley and its contributors. 1766633Sdfr * 4. Neither the name of the University nor the names of its contributors 1866633Sdfr * may be used to endorse or promote products derived from this software 1966633Sdfr * without specific prior written permission. 2066633Sdfr * 2177448Sjhb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2277448Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2366633Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2477448Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2577448Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2666633Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2781198Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2866633Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2966633Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3066633Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3166633Sdfr * SUCH DAMAGE. 3266633Sdfr * 3366633Sdfr * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 3466633Sdfr * $Id: ip_output.c,v 1.27 1995/12/19 21:24:19 wollman Exp $ 3595761Smarcel */ 36113140Smarcel 3766633Sdfr#include <sys/param.h> 3874031Sdfr#include <sys/systm.h> 3974031Sdfr#include <sys/malloc.h> 4074031Sdfr#include <sys/mbuf.h> 4174031Sdfr#include <sys/errno.h> 4266633Sdfr#include <sys/protosw.h> 4366633Sdfr#include <sys/socket.h> 4466633Sdfr#include <sys/socketvar.h> 4566633Sdfr#include <sys/queue.h> 4666633Sdfr 4766633Sdfr#include <net/if.h> 4866633Sdfr#include <net/route.h> 4966633Sdfr 5066633Sdfr#include <netinet/in.h> 5166633Sdfr#include <netinet/in_systm.h> 5266633Sdfr#include <netinet/ip.h> 5366633Sdfr#include <netinet/in_pcb.h> 5466633Sdfr#include <netinet/in_var.h> 5566633Sdfr#include <netinet/ip_var.h> 5666633Sdfr 5766633Sdfr#include <netinet/ip_fw.h> 5866633Sdfr 5966633Sdfr#ifdef vax 6066633Sdfr#include <machine/mtpr.h> 6166633Sdfr#endif 6266633Sdfr 6366633Sdfru_short ip_id; 6466633Sdfr 6566633Sdfrstatic struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *)); 6666633Sdfrstatic void ip_mloopback 6766633Sdfr __P((struct ifnet *, struct mbuf *, struct sockaddr_in *)); 6866633Sdfrstatic int ip_getmoptions 6966633Sdfr __P((int, struct ip_moptions *, struct mbuf **)); 7066633Sdfrstatic int ip_optcopy __P((struct ip *, struct ip *)); 7166633Sdfrstatic int ip_pcbopts __P((struct mbuf **, struct mbuf *)); 7285304Sobrienstatic int ip_setmoptions 7366633Sdfr __P((int, struct ip_moptions **, struct mbuf *)); 7466633Sdfr 75151897Srwatson/* 7666633Sdfr * IP output. The packet in mbuf chain m contains a skeletal IP 7766633Sdfr * header (with len, off, ttl, proto, tos, src, dst). 7866633Sdfr * The mbuf chain containing the packet will be freed. 7966633Sdfr * The mbuf opt, if present, will not be freed. 8066633Sdfr */ 8166633Sdfrint 8266633Sdfrip_output(m0, opt, ro, flags, imo) 8366633Sdfr struct mbuf *m0; 8466633Sdfr struct mbuf *opt; 85125975Sphk struct route *ro; 86130585Sphk int flags; 8766633Sdfr struct ip_moptions *imo; 8866633Sdfr{ 8966633Sdfr register struct ip *ip, *mhip; 9066633Sdfr register struct ifnet *ifp; 9166633Sdfr register struct mbuf *m = m0; 9266633Sdfr register int hlen = sizeof (struct ip); 9366633Sdfr int len, off, error = 0; 9466633Sdfr /* 9566633Sdfr * It might seem obvious at first glance that one could easily 9666633Sdfr * make a one-behind cache out of this by simply making `iproute' 9766633Sdfr * static and eliminating the bzero() below. However, this turns 9866633Sdfr * out not to work, for two reasons: 9966633Sdfr * 10067018Sdfr * 1) This routine needs to be reentrant. It can be called 10166633Sdfr * recursively from encapsulating network interfaces, and it 102112051Smarcel * is always called recursively from ip_mforward(). 10366633Sdfr * 10466633Sdfr * 2) You turn out not to gain much. There is already a one- 10566633Sdfr * behind cache implemented for the specific case of forwarding, 106112946Sphk * and sends on a connected socket will use a route associated 10766633Sdfr * with the PCB. The only cases left are sends on unconnected 10866633Sdfr * and raw sockets, and if these cases are really significant, 10966633Sdfr * something is seriously wrong. 11066633Sdfr */ 11166633Sdfr struct route iproute; 11266633Sdfr struct sockaddr_in *dst; 11366633Sdfr struct in_ifaddr *ia; 11466633Sdfr 11566633Sdfr#ifdef DIAGNOSTIC 116136809Sphk if ((m->m_flags & M_PKTHDR) == 0) 11766633Sdfr panic("ip_output no HDR"); 11866633Sdfr#endif 11966633Sdfr if (opt) { 12066633Sdfr m = ip_insertoptions(m, opt, &len); 12167018Sdfr hlen = len; 12267018Sdfr } 12367018Sdfr ip = mtod(m, struct ip *); 12467018Sdfr /* 12567018Sdfr * Fill in IP header. 12667018Sdfr */ 12767018Sdfr if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 12867018Sdfr ip->ip_v = IPVERSION; 12967018Sdfr ip->ip_off &= IP_DF; 13067018Sdfr ip->ip_id = htons(ip_id++); 13167018Sdfr ip->ip_hl = hlen >> 2; 132112051Smarcel ipstat.ips_localout++; 133112051Smarcel } else { 13467018Sdfr hlen = ip->ip_hl << 2; 13567018Sdfr } 13667018Sdfr /* 13767018Sdfr * Route packet. 13867018Sdfr */ 13967018Sdfr if (ro == 0) { 14067018Sdfr ro = &iproute; 14166633Sdfr bzero((caddr_t)ro, sizeof (*ro)); 142111979Sphk } 14366633Sdfr dst = (struct sockaddr_in *)&ro->ro_dst; 14466633Sdfr /* 14566633Sdfr * If there is a cached route, 14666633Sdfr * check that it is to the same destination 14766633Sdfr * and is still up. If not, free it and try again. 14866633Sdfr */ 14966633Sdfr if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 15066633Sdfr dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 15166633Sdfr RTFREE(ro->ro_rt); 15266633Sdfr ro->ro_rt = (struct rtentry *)0; 15366633Sdfr } 15483964Sdfr if (ro->ro_rt == 0) { 15566633Sdfr dst->sin_family = AF_INET; 15683964Sdfr dst->sin_len = sizeof(*dst); 15783964Sdfr dst->sin_addr = ip->ip_dst; 15883964Sdfr } 15983964Sdfr /* 16083964Sdfr * If routing to interface only, 16166633Sdfr * short circuit routing lookup. 16266633Sdfr */ 16366633Sdfr#define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) 16466633Sdfr#define sintosa(sin) ((struct sockaddr *)(sin)) 16566633Sdfr if (flags & IP_ROUTETOIF) { 16666633Sdfr if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 && 16766633Sdfr (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) { 168184205Sdes ipstat.ips_noroute++; 16966633Sdfr error = ENETUNREACH; 17066633Sdfr goto bad; 17166633Sdfr } 172110229Sphk ifp = ia->ia_ifp; 173125975Sphk ip->ip_ttl = 1; 174125975Sphk } else { 175125975Sphk /* 176125975Sphk * If this is the case, we probably don't want to allocate 177125975Sphk * a protocol-cloned route since we didn't get one from the 178125975Sphk * ULP. This lets TCP do its thing, while not burdening 179125975Sphk * forwarding or ICMP with the overhead of cloning a route. 180125975Sphk * Of course, we still want to do any cloning requested by 181125975Sphk * the link layer, as this is probably required in all cases 182125975Sphk * for correct operation (as it is for ARP). 183125975Sphk */ 184125975Sphk if (ro->ro_rt == 0) 18583964Sdfr rtalloc_ign(ro, RTF_PRCLONING); 18666633Sdfr if (ro->ro_rt == 0) { 187178028Smarcel ipstat.ips_noroute++; 18866633Sdfr error = EHOSTUNREACH; 18966633Sdfr goto bad; 19066633Sdfr } 19166633Sdfr ia = ifatoia(ro->ro_rt->rt_ifa); 19266633Sdfr ifp = ro->ro_rt->rt_ifp; 19366633Sdfr ro->ro_rt->rt_use++; 19466633Sdfr if (ro->ro_rt->rt_flags & RTF_GATEWAY) 19566633Sdfr dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 19666633Sdfr } 197177253Srwatson if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 198 struct in_multi *inm; 199 200 m->m_flags |= M_MCAST; 201 /* 202 * IP destination address is multicast. Make sure "dst" 203 * still points to the address in "ro". (It may have been 204 * changed to point to a gateway address, above.) 205 */ 206 dst = (struct sockaddr_in *)&ro->ro_dst; 207 /* 208 * See if the caller provided any multicast options 209 */ 210 if (imo != NULL) { 211 ip->ip_ttl = imo->imo_multicast_ttl; 212 if (imo->imo_multicast_ifp != NULL) 213 ifp = imo->imo_multicast_ifp; 214 if (imo->imo_multicast_vif != -1) 215 ip->ip_src.s_addr = 216 ip_mcast_src(imo->imo_multicast_vif); 217 } else 218 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 219 /* 220 * Confirm that the outgoing interface supports multicast. 221 */ 222 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 223 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 224 ipstat.ips_noroute++; 225 error = ENETUNREACH; 226 goto bad; 227 } 228 } 229 /* 230 * If source address not specified yet, use address 231 * of outgoing interface. 232 */ 233 if (ip->ip_src.s_addr == INADDR_ANY) { 234 register struct in_ifaddr *ia; 235 236 for (ia = in_ifaddr; ia; ia = ia->ia_next) 237 if (ia->ia_ifp == ifp) { 238 ip->ip_src = IA_SIN(ia)->sin_addr; 239 break; 240 } 241 } 242 243 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 244 if (inm != NULL && 245 (imo == NULL || imo->imo_multicast_loop)) { 246 /* 247 * If we belong to the destination multicast group 248 * on the outgoing interface, and the caller did not 249 * forbid loopback, loop back a copy. 250 */ 251 ip_mloopback(ifp, m, dst); 252 } 253 else { 254 /* 255 * If we are acting as a multicast router, perform 256 * multicast forwarding as if the packet had just 257 * arrived on the interface to which we are about 258 * to send. The multicast forwarding function 259 * recursively calls this function, using the 260 * IP_FORWARDING flag to prevent infinite recursion. 261 * 262 * Multicasts that are looped back by ip_mloopback(), 263 * above, will be forwarded by the ip_input() routine, 264 * if necessary. 265 */ 266 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 267 /* 268 * Check if rsvp daemon is running. If not, don't 269 * set ip_moptions. This ensures that the packet 270 * is multicast and not just sent down one link 271 * as prescribed by rsvpd. 272 */ 273 if (!rsvp_on) 274 imo = NULL; 275 if (ip_mforward(ip, ifp, m, imo) != 0) { 276 m_freem(m); 277 goto done; 278 } 279 } 280 } 281 282 /* 283 * Multicasts with a time-to-live of zero may be looped- 284 * back, above, but must not be transmitted on a network. 285 * Also, multicasts addressed to the loopback interface 286 * are not sent -- the above call to ip_mloopback() will 287 * loop back a copy if this host actually belongs to the 288 * destination group on the loopback interface. 289 */ 290 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 291 m_freem(m); 292 goto done; 293 } 294 295 goto sendit; 296 } 297#ifndef notdef 298 /* 299 * If source address not specified yet, use address 300 * of outgoing interface. 301 */ 302 if (ip->ip_src.s_addr == INADDR_ANY) 303 ip->ip_src = IA_SIN(ia)->sin_addr; 304#endif 305 /* 306 * Verify that we have any chance at all of being able to queue 307 * the packet or packet fragments 308 */ 309 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 310 ifp->if_snd.ifq_maxlen) { 311 error = ENOBUFS; 312 goto bad; 313 } 314 315 /* 316 * Look for broadcast address and 317 * and verify user is allowed to send 318 * such a packet. 319 */ 320 if (in_broadcast(dst->sin_addr, ifp)) { 321 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 322 error = EADDRNOTAVAIL; 323 goto bad; 324 } 325 if ((flags & IP_ALLOWBROADCAST) == 0) { 326 error = EACCES; 327 goto bad; 328 } 329 /* don't allow broadcast messages to be fragmented */ 330 if ((u_short)ip->ip_len > ifp->if_mtu) { 331 error = EMSGSIZE; 332 goto bad; 333 } 334 m->m_flags |= M_BCAST; 335 } else 336 m->m_flags &= ~M_BCAST; 337 338sendit: 339 /* 340 * If small enough for interface, can just send directly. 341 */ 342 if ((u_short)ip->ip_len <= ifp->if_mtu) { 343 ip->ip_len = htons((u_short)ip->ip_len); 344 ip->ip_off = htons((u_short)ip->ip_off); 345 ip->ip_sum = 0; 346 ip->ip_sum = in_cksum(m, hlen); 347 error = (*ifp->if_output)(ifp, m, 348 (struct sockaddr *)dst, ro->ro_rt); 349 goto done; 350 } 351 /* 352 * Too large for interface; fragment if possible. 353 * Must be able to put at least 8 bytes per fragment. 354 */ 355 if (ip->ip_off & IP_DF) { 356 error = EMSGSIZE; 357#if 1 358 /* 359 * This case can happen if the user changed the MTU 360 * of an interface after enabling IP on it. Because 361 * most netifs don't keep track of routes pointing to 362 * them, there is no way for one to update all its 363 * routes when the MTU is changed. 364 */ 365 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) 366 && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) 367 && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 368 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 369 } 370#endif 371 ipstat.ips_cantfrag++; 372 goto bad; 373 } 374 len = (ifp->if_mtu - hlen) &~ 7; 375 if (len < 8) { 376 error = EMSGSIZE; 377 goto bad; 378 } 379 380 { 381 int mhlen, firstlen = len; 382 struct mbuf **mnext = &m->m_nextpkt; 383 384 /* 385 * Loop through length of segment after first fragment, 386 * make new header and copy data of each part and link onto chain. 387 */ 388 m0 = m; 389 mhlen = sizeof (struct ip); 390 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { 391 MGETHDR(m, M_DONTWAIT, MT_HEADER); 392 if (m == 0) { 393 error = ENOBUFS; 394 ipstat.ips_odropped++; 395 goto sendorfree; 396 } 397 m->m_data += max_linkhdr; 398 mhip = mtod(m, struct ip *); 399 *mhip = *ip; 400 if (hlen > sizeof (struct ip)) { 401 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 402 mhip->ip_hl = mhlen >> 2; 403 } 404 m->m_len = mhlen; 405 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); 406 if (ip->ip_off & IP_MF) 407 mhip->ip_off |= IP_MF; 408 if (off + len >= (u_short)ip->ip_len) 409 len = (u_short)ip->ip_len - off; 410 else 411 mhip->ip_off |= IP_MF; 412 mhip->ip_len = htons((u_short)(len + mhlen)); 413 m->m_next = m_copy(m0, off, len); 414 if (m->m_next == 0) { 415 (void) m_free(m); 416 error = ENOBUFS; /* ??? */ 417 ipstat.ips_odropped++; 418 goto sendorfree; 419 } 420 m->m_pkthdr.len = mhlen + len; 421 m->m_pkthdr.rcvif = (struct ifnet *)0; 422 mhip->ip_off = htons((u_short)mhip->ip_off); 423 mhip->ip_sum = 0; 424 mhip->ip_sum = in_cksum(m, mhlen); 425 *mnext = m; 426 mnext = &m->m_nextpkt; 427 ipstat.ips_ofragments++; 428 } 429 /* 430 * Update first fragment by trimming what's been copied out 431 * and updating header, then send each fragment (in order). 432 */ 433 m = m0; 434 m_adj(m, hlen + firstlen - (u_short)ip->ip_len); 435 m->m_pkthdr.len = hlen + firstlen; 436 ip->ip_len = htons((u_short)m->m_pkthdr.len); 437 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF)); 438 ip->ip_sum = 0; 439 ip->ip_sum = in_cksum(m, hlen); 440sendorfree: 441 for (m = m0; m; m = m0) { 442 m0 = m->m_nextpkt; 443 m->m_nextpkt = 0; 444 if (error == 0) 445 error = (*ifp->if_output)(ifp, m, 446 (struct sockaddr *)dst, ro->ro_rt); 447 else 448 m_freem(m); 449 } 450 451 if (error == 0) 452 ipstat.ips_fragmented++; 453 } 454done: 455 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) 456 RTFREE(ro->ro_rt); 457 /* 458 * Count outgoing packet,here we count both our packets and 459 * those we forward. 460 * Here we want to convert ip_len to host byte order when counting 461 * so we set 3rd arg to 1. 462 * This is locally generated packet so it has not 463 * incoming interface. 464 */ 465 if (ip_acct_cnt_ptr!=NULL) 466 (*ip_acct_cnt_ptr)(ip,NULL,ip_acct_chain,1); 467 468 return (error); 469bad: 470 m_freem(m0); 471 goto done; 472} 473 474/* 475 * Insert IP options into preformed packet. 476 * Adjust IP destination as required for IP source routing, 477 * as indicated by a non-zero in_addr at the start of the options. 478 */ 479static struct mbuf * 480ip_insertoptions(m, opt, phlen) 481 register struct mbuf *m; 482 struct mbuf *opt; 483 int *phlen; 484{ 485 register struct ipoption *p = mtod(opt, struct ipoption *); 486 struct mbuf *n; 487 register struct ip *ip = mtod(m, struct ip *); 488 unsigned optlen; 489 490 optlen = opt->m_len - sizeof(p->ipopt_dst); 491 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) 492 return (m); /* XXX should fail */ 493 if (p->ipopt_dst.s_addr) 494 ip->ip_dst = p->ipopt_dst; 495 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 496 MGETHDR(n, M_DONTWAIT, MT_HEADER); 497 if (n == 0) 498 return (m); 499 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 500 m->m_len -= sizeof(struct ip); 501 m->m_data += sizeof(struct ip); 502 n->m_next = m; 503 m = n; 504 m->m_len = optlen + sizeof(struct ip); 505 m->m_data += max_linkhdr; 506 (void)memcpy(mtod(m, void *), ip, sizeof(struct ip)); 507 } else { 508 m->m_data -= optlen; 509 m->m_len += optlen; 510 m->m_pkthdr.len += optlen; 511 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 512 } 513 ip = mtod(m, struct ip *); 514 (void)memcpy(ip + 1, p->ipopt_list, (unsigned)optlen); 515 *phlen = sizeof(struct ip) + optlen; 516 ip->ip_len += optlen; 517 return (m); 518} 519 520/* 521 * Copy options from ip to jp, 522 * omitting those not copied during fragmentation. 523 */ 524static int 525ip_optcopy(ip, jp) 526 struct ip *ip, *jp; 527{ 528 register u_char *cp, *dp; 529 int opt, optlen, cnt; 530 531 cp = (u_char *)(ip + 1); 532 dp = (u_char *)(jp + 1); 533 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 534 for (; cnt > 0; cnt -= optlen, cp += optlen) { 535 opt = cp[0]; 536 if (opt == IPOPT_EOL) 537 break; 538 if (opt == IPOPT_NOP) { 539 /* Preserve for IP mcast tunnel's LSRR alignment. */ 540 *dp++ = IPOPT_NOP; 541 optlen = 1; 542 continue; 543 } else 544 optlen = cp[IPOPT_OLEN]; 545 /* bogus lengths should have been caught by ip_dooptions */ 546 if (optlen > cnt) 547 optlen = cnt; 548 if (IPOPT_COPIED(opt)) { 549 (void)memcpy(dp, cp, (unsigned)optlen); 550 dp += optlen; 551 } 552 } 553 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 554 *dp++ = IPOPT_EOL; 555 return (optlen); 556} 557 558/* 559 * IP socket option processing. 560 */ 561int 562ip_ctloutput(op, so, level, optname, mp) 563 int op; 564 struct socket *so; 565 int level, optname; 566 struct mbuf **mp; 567{ 568 register struct inpcb *inp = sotoinpcb(so); 569 register struct mbuf *m = *mp; 570 register int optval = 0; 571 int error = 0; 572 573 if (level != IPPROTO_IP) { 574 error = EINVAL; 575 if (op == PRCO_SETOPT && *mp) 576 (void) m_free(*mp); 577 } else switch (op) { 578 579 case PRCO_SETOPT: 580 switch (optname) { 581 case IP_OPTIONS: 582#ifdef notyet 583 case IP_RETOPTS: 584 return (ip_pcbopts(optname, &inp->inp_options, m)); 585#else 586 return (ip_pcbopts(&inp->inp_options, m)); 587#endif 588 589 case IP_TOS: 590 case IP_TTL: 591 case IP_RECVOPTS: 592 case IP_RECVRETOPTS: 593 case IP_RECVDSTADDR: 594 if (m == 0 || m->m_len != sizeof(int)) 595 error = EINVAL; 596 else { 597 optval = *mtod(m, int *); 598 switch (optname) { 599 600 case IP_TOS: 601 inp->inp_ip.ip_tos = optval; 602 break; 603 604 case IP_TTL: 605 inp->inp_ip.ip_ttl = optval; 606 break; 607#define OPTSET(bit) \ 608 if (optval) \ 609 inp->inp_flags |= bit; \ 610 else \ 611 inp->inp_flags &= ~bit; 612 613 case IP_RECVOPTS: 614 OPTSET(INP_RECVOPTS); 615 break; 616 617 case IP_RECVRETOPTS: 618 OPTSET(INP_RECVRETOPTS); 619 break; 620 621 case IP_RECVDSTADDR: 622 OPTSET(INP_RECVDSTADDR); 623 break; 624 } 625 } 626 break; 627#undef OPTSET 628 629 case IP_MULTICAST_IF: 630 case IP_MULTICAST_VIF: 631 case IP_MULTICAST_TTL: 632 case IP_MULTICAST_LOOP: 633 case IP_ADD_MEMBERSHIP: 634 case IP_DROP_MEMBERSHIP: 635 error = ip_setmoptions(optname, &inp->inp_moptions, m); 636 break; 637 638 case IP_PORTRANGE: 639 if (m == 0 || m->m_len != sizeof(int)) 640 error = EINVAL; 641 else { 642 optval = *mtod(m, int *); 643 644 switch (optval) { 645 646 case IP_PORTRANGE_DEFAULT: 647 inp->inp_flags &= ~(INP_LOWPORT); 648 inp->inp_flags &= ~(INP_HIGHPORT); 649 break; 650 651 case IP_PORTRANGE_HIGH: 652 inp->inp_flags &= ~(INP_LOWPORT); 653 inp->inp_flags |= INP_HIGHPORT; 654 break; 655 656 case IP_PORTRANGE_LOW: 657 inp->inp_flags &= ~(INP_HIGHPORT); 658 inp->inp_flags |= INP_LOWPORT; 659 break; 660 661 default: 662 error = EINVAL; 663 break; 664 } 665 } 666 667 default: 668 error = ENOPROTOOPT; 669 break; 670 } 671 if (m) 672 (void)m_free(m); 673 break; 674 675 case PRCO_GETOPT: 676 switch (optname) { 677 case IP_OPTIONS: 678 case IP_RETOPTS: 679 *mp = m = m_get(M_WAIT, MT_SOOPTS); 680 if (inp->inp_options) { 681 m->m_len = inp->inp_options->m_len; 682 (void)memcpy(mtod(m, void *), 683 mtod(inp->inp_options, void *), (unsigned)m->m_len); 684 } else 685 m->m_len = 0; 686 break; 687 688 case IP_TOS: 689 case IP_TTL: 690 case IP_RECVOPTS: 691 case IP_RECVRETOPTS: 692 case IP_RECVDSTADDR: 693 *mp = m = m_get(M_WAIT, MT_SOOPTS); 694 m->m_len = sizeof(int); 695 switch (optname) { 696 697 case IP_TOS: 698 optval = inp->inp_ip.ip_tos; 699 break; 700 701 case IP_TTL: 702 optval = inp->inp_ip.ip_ttl; 703 break; 704 705#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 706 707 case IP_RECVOPTS: 708 optval = OPTBIT(INP_RECVOPTS); 709 break; 710 711 case IP_RECVRETOPTS: 712 optval = OPTBIT(INP_RECVRETOPTS); 713 break; 714 715 case IP_RECVDSTADDR: 716 optval = OPTBIT(INP_RECVDSTADDR); 717 break; 718 } 719 *mtod(m, int *) = optval; 720 break; 721 722 case IP_MULTICAST_IF: 723 case IP_MULTICAST_VIF: 724 case IP_MULTICAST_TTL: 725 case IP_MULTICAST_LOOP: 726 case IP_ADD_MEMBERSHIP: 727 case IP_DROP_MEMBERSHIP: 728 error = ip_getmoptions(optname, inp->inp_moptions, mp); 729 break; 730 731 case IP_PORTRANGE: 732 *mp = m = m_get(M_WAIT, MT_SOOPTS); 733 m->m_len = sizeof(int); 734 735 if (inp->inp_flags & INP_HIGHPORT) 736 optval = IP_PORTRANGE_HIGH; 737 else if (inp->inp_flags & INP_LOWPORT) 738 optval = IP_PORTRANGE_LOW; 739 else 740 optval = 0; 741 742 *mtod(m, int *) = optval; 743 break; 744 745 default: 746 error = ENOPROTOOPT; 747 break; 748 } 749 break; 750 } 751 return (error); 752} 753 754/* 755 * Set up IP options in pcb for insertion in output packets. 756 * Store in mbuf with pointer in pcbopt, adding pseudo-option 757 * with destination address if source routed. 758 */ 759static int 760#ifdef notyet 761ip_pcbopts(optname, pcbopt, m) 762 int optname; 763#else 764ip_pcbopts(pcbopt, m) 765#endif 766 struct mbuf **pcbopt; 767 register struct mbuf *m; 768{ 769 register cnt, optlen; 770 register u_char *cp; 771 u_char opt; 772 773 /* turn off any old options */ 774 if (*pcbopt) 775 (void)m_free(*pcbopt); 776 *pcbopt = 0; 777 if (m == (struct mbuf *)0 || m->m_len == 0) { 778 /* 779 * Only turning off any previous options. 780 */ 781 if (m) 782 (void)m_free(m); 783 return (0); 784 } 785 786#ifndef vax 787 if (m->m_len % sizeof(long)) 788 goto bad; 789#endif 790 /* 791 * IP first-hop destination address will be stored before 792 * actual options; move other options back 793 * and clear it when none present. 794 */ 795 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 796 goto bad; 797 cnt = m->m_len; 798 m->m_len += sizeof(struct in_addr); 799 cp = mtod(m, u_char *) + sizeof(struct in_addr); 800 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 801 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 802 803 for (; cnt > 0; cnt -= optlen, cp += optlen) { 804 opt = cp[IPOPT_OPTVAL]; 805 if (opt == IPOPT_EOL) 806 break; 807 if (opt == IPOPT_NOP) 808 optlen = 1; 809 else { 810 optlen = cp[IPOPT_OLEN]; 811 if (optlen <= IPOPT_OLEN || optlen > cnt) 812 goto bad; 813 } 814 switch (opt) { 815 816 default: 817 break; 818 819 case IPOPT_LSRR: 820 case IPOPT_SSRR: 821 /* 822 * user process specifies route as: 823 * ->A->B->C->D 824 * D must be our final destination (but we can't 825 * check that since we may not have connected yet). 826 * A is first hop destination, which doesn't appear in 827 * actual IP option, but is stored before the options. 828 */ 829 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 830 goto bad; 831 m->m_len -= sizeof(struct in_addr); 832 cnt -= sizeof(struct in_addr); 833 optlen -= sizeof(struct in_addr); 834 cp[IPOPT_OLEN] = optlen; 835 /* 836 * Move first hop before start of options. 837 */ 838 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 839 sizeof(struct in_addr)); 840 /* 841 * Then copy rest of options back 842 * to close up the deleted entry. 843 */ 844 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 845 sizeof(struct in_addr)), 846 (caddr_t)&cp[IPOPT_OFFSET+1], 847 (unsigned)cnt + sizeof(struct in_addr)); 848 break; 849 } 850 } 851 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 852 goto bad; 853 *pcbopt = m; 854 return (0); 855 856bad: 857 (void)m_free(m); 858 return (EINVAL); 859} 860 861/* 862 * Set the IP multicast options in response to user setsockopt(). 863 */ 864static int 865ip_setmoptions(optname, imop, m) 866 int optname; 867 struct ip_moptions **imop; 868 struct mbuf *m; 869{ 870 register int error = 0; 871 u_char loop; 872 register int i; 873 struct in_addr addr; 874 register struct ip_mreq *mreq; 875 register struct ifnet *ifp; 876 register struct ip_moptions *imo = *imop; 877 struct route ro; 878 register struct sockaddr_in *dst; 879 int s; 880 881 if (imo == NULL) { 882 /* 883 * No multicast option buffer attached to the pcb; 884 * allocate one and initialize to default values. 885 */ 886 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 887 M_WAITOK); 888 889 if (imo == NULL) 890 return (ENOBUFS); 891 *imop = imo; 892 imo->imo_multicast_ifp = NULL; 893 imo->imo_multicast_vif = -1; 894 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 895 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 896 imo->imo_num_memberships = 0; 897 } 898 899 switch (optname) { 900 /* store an index number for the vif you wanna use in the send */ 901 case IP_MULTICAST_VIF: 902 if (!legal_vif_num) { 903 error = EOPNOTSUPP; 904 break; 905 } 906 if (m == NULL || m->m_len != sizeof(int)) { 907 error = EINVAL; 908 break; 909 } 910 i = *(mtod(m, int *)); 911 if (!legal_vif_num(i) && (i != -1)) { 912 error = EINVAL; 913 break; 914 } 915 imo->imo_multicast_vif = i; 916 break; 917 918 case IP_MULTICAST_IF: 919 /* 920 * Select the interface for outgoing multicast packets. 921 */ 922 if (m == NULL || m->m_len != sizeof(struct in_addr)) { 923 error = EINVAL; 924 break; 925 } 926 addr = *(mtod(m, struct in_addr *)); 927 /* 928 * INADDR_ANY is used to remove a previous selection. 929 * When no interface is selected, a default one is 930 * chosen every time a multicast packet is sent. 931 */ 932 if (addr.s_addr == INADDR_ANY) { 933 imo->imo_multicast_ifp = NULL; 934 break; 935 } 936 /* 937 * The selected interface is identified by its local 938 * IP address. Find the interface and confirm that 939 * it supports multicasting. 940 */ 941 s = splimp(); 942 INADDR_TO_IFP(addr, ifp); 943 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 944 error = EADDRNOTAVAIL; 945 break; 946 } 947 imo->imo_multicast_ifp = ifp; 948 splx(s); 949 break; 950 951 case IP_MULTICAST_TTL: 952 /* 953 * Set the IP time-to-live for outgoing multicast packets. 954 */ 955 if (m == NULL || m->m_len != 1) { 956 error = EINVAL; 957 break; 958 } 959 imo->imo_multicast_ttl = *(mtod(m, u_char *)); 960 break; 961 962 case IP_MULTICAST_LOOP: 963 /* 964 * Set the loopback flag for outgoing multicast packets. 965 * Must be zero or one. 966 */ 967 if (m == NULL || m->m_len != 1 || 968 (loop = *(mtod(m, u_char *))) > 1) { 969 error = EINVAL; 970 break; 971 } 972 imo->imo_multicast_loop = loop; 973 break; 974 975 case IP_ADD_MEMBERSHIP: 976 /* 977 * Add a multicast group membership. 978 * Group must be a valid IP multicast address. 979 */ 980 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 981 error = EINVAL; 982 break; 983 } 984 mreq = mtod(m, struct ip_mreq *); 985 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) { 986 error = EINVAL; 987 break; 988 } 989 s = splimp(); 990 /* 991 * If no interface address was provided, use the interface of 992 * the route to the given multicast address. 993 */ 994 if (mreq->imr_interface.s_addr == INADDR_ANY) { 995 bzero((caddr_t)&ro, sizeof(ro)); 996 dst = (struct sockaddr_in *)&ro.ro_dst; 997 dst->sin_len = sizeof(*dst); 998 dst->sin_family = AF_INET; 999 dst->sin_addr = mreq->imr_multiaddr; 1000 rtalloc(&ro); 1001 if (ro.ro_rt == NULL) { 1002 error = EADDRNOTAVAIL; 1003 splx(s); 1004 break; 1005 } 1006 ifp = ro.ro_rt->rt_ifp; 1007 rtfree(ro.ro_rt); 1008 } 1009 else { 1010 INADDR_TO_IFP(mreq->imr_interface, ifp); 1011 } 1012 1013 /* 1014 * See if we found an interface, and confirm that it 1015 * supports multicast. 1016 */ 1017 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1018 error = EADDRNOTAVAIL; 1019 splx(s); 1020 break; 1021 } 1022 /* 1023 * See if the membership already exists or if all the 1024 * membership slots are full. 1025 */ 1026 for (i = 0; i < imo->imo_num_memberships; ++i) { 1027 if (imo->imo_membership[i]->inm_ifp == ifp && 1028 imo->imo_membership[i]->inm_addr.s_addr 1029 == mreq->imr_multiaddr.s_addr) 1030 break; 1031 } 1032 if (i < imo->imo_num_memberships) { 1033 error = EADDRINUSE; 1034 splx(s); 1035 break; 1036 } 1037 if (i == IP_MAX_MEMBERSHIPS) { 1038 error = ETOOMANYREFS; 1039 splx(s); 1040 break; 1041 } 1042 /* 1043 * Everything looks good; add a new record to the multicast 1044 * address list for the given interface. 1045 */ 1046 if ((imo->imo_membership[i] = 1047 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) { 1048 error = ENOBUFS; 1049 splx(s); 1050 break; 1051 } 1052 ++imo->imo_num_memberships; 1053 splx(s); 1054 break; 1055 1056 case IP_DROP_MEMBERSHIP: 1057 /* 1058 * Drop a multicast group membership. 1059 * Group must be a valid IP multicast address. 1060 */ 1061 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 1062 error = EINVAL; 1063 break; 1064 } 1065 mreq = mtod(m, struct ip_mreq *); 1066 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) { 1067 error = EINVAL; 1068 break; 1069 } 1070 1071 s = splimp(); 1072 /* 1073 * If an interface address was specified, get a pointer 1074 * to its ifnet structure. 1075 */ 1076 if (mreq->imr_interface.s_addr == INADDR_ANY) 1077 ifp = NULL; 1078 else { 1079 INADDR_TO_IFP(mreq->imr_interface, ifp); 1080 if (ifp == NULL) { 1081 error = EADDRNOTAVAIL; 1082 splx(s); 1083 break; 1084 } 1085 } 1086 /* 1087 * Find the membership in the membership array. 1088 */ 1089 for (i = 0; i < imo->imo_num_memberships; ++i) { 1090 if ((ifp == NULL || 1091 imo->imo_membership[i]->inm_ifp == ifp) && 1092 imo->imo_membership[i]->inm_addr.s_addr == 1093 mreq->imr_multiaddr.s_addr) 1094 break; 1095 } 1096 if (i == imo->imo_num_memberships) { 1097 error = EADDRNOTAVAIL; 1098 splx(s); 1099 break; 1100 } 1101 /* 1102 * Give up the multicast address record to which the 1103 * membership points. 1104 */ 1105 in_delmulti(imo->imo_membership[i]); 1106 /* 1107 * Remove the gap in the membership array. 1108 */ 1109 for (++i; i < imo->imo_num_memberships; ++i) 1110 imo->imo_membership[i-1] = imo->imo_membership[i]; 1111 --imo->imo_num_memberships; 1112 splx(s); 1113 break; 1114 1115 default: 1116 error = EOPNOTSUPP; 1117 break; 1118 } 1119 1120 /* 1121 * If all options have default values, no need to keep the mbuf. 1122 */ 1123 if (imo->imo_multicast_ifp == NULL && 1124 imo->imo_multicast_vif == -1 && 1125 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1126 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1127 imo->imo_num_memberships == 0) { 1128 free(*imop, M_IPMOPTS); 1129 *imop = NULL; 1130 } 1131 1132 return (error); 1133} 1134 1135/* 1136 * Return the IP multicast options in response to user getsockopt(). 1137 */ 1138static int 1139ip_getmoptions(optname, imo, mp) 1140 int optname; 1141 register struct ip_moptions *imo; 1142 register struct mbuf **mp; 1143{ 1144 u_char *ttl; 1145 u_char *loop; 1146 struct in_addr *addr; 1147 struct in_ifaddr *ia; 1148 1149 *mp = m_get(M_WAIT, MT_SOOPTS); 1150 1151 switch (optname) { 1152 1153 case IP_MULTICAST_VIF: 1154 if (imo != NULL) 1155 *(mtod(*mp, int *)) = imo->imo_multicast_vif; 1156 else 1157 *(mtod(*mp, int *)) = -1; 1158 (*mp)->m_len = sizeof(int); 1159 return(0); 1160 1161 case IP_MULTICAST_IF: 1162 addr = mtod(*mp, struct in_addr *); 1163 (*mp)->m_len = sizeof(struct in_addr); 1164 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1165 addr->s_addr = INADDR_ANY; 1166 else { 1167 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1168 addr->s_addr = (ia == NULL) ? INADDR_ANY 1169 : IA_SIN(ia)->sin_addr.s_addr; 1170 } 1171 return (0); 1172 1173 case IP_MULTICAST_TTL: 1174 ttl = mtod(*mp, u_char *); 1175 (*mp)->m_len = 1; 1176 *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL 1177 : imo->imo_multicast_ttl; 1178 return (0); 1179 1180 case IP_MULTICAST_LOOP: 1181 loop = mtod(*mp, u_char *); 1182 (*mp)->m_len = 1; 1183 *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP 1184 : imo->imo_multicast_loop; 1185 return (0); 1186 1187 default: 1188 return (EOPNOTSUPP); 1189 } 1190} 1191 1192/* 1193 * Discard the IP multicast options. 1194 */ 1195void 1196ip_freemoptions(imo) 1197 register struct ip_moptions *imo; 1198{ 1199 register int i; 1200 1201 if (imo != NULL) { 1202 for (i = 0; i < imo->imo_num_memberships; ++i) 1203 in_delmulti(imo->imo_membership[i]); 1204 free(imo, M_IPMOPTS); 1205 } 1206} 1207 1208/* 1209 * Routine called from ip_output() to loop back a copy of an IP multicast 1210 * packet to the input queue of a specified interface. Note that this 1211 * calls the output routine of the loopback "driver", but with an interface 1212 * pointer that might NOT be a loopback interface -- evil, but easier than 1213 * replicating that code here. 1214 */ 1215static void 1216ip_mloopback(ifp, m, dst) 1217 struct ifnet *ifp; 1218 register struct mbuf *m; 1219 register struct sockaddr_in *dst; 1220{ 1221 register struct ip *ip; 1222 struct mbuf *copym; 1223 1224 copym = m_copy(m, 0, M_COPYALL); 1225 if (copym != NULL) { 1226 /* 1227 * We don't bother to fragment if the IP length is greater 1228 * than the interface's MTU. Can this possibly matter? 1229 */ 1230 ip = mtod(copym, struct ip *); 1231 ip->ip_len = htons((u_short)ip->ip_len); 1232 ip->ip_off = htons((u_short)ip->ip_off); 1233 ip->ip_sum = 0; 1234 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 1235 (void) looutput(ifp, copym, (struct sockaddr *)dst, NULL); 1236 } 1237} 1238