📄 ip_output.c
字号:
ip_mloopback(ifp, m, dst, NULL); } else { /* * If we are acting as a multicast router, perform * multicast forwarding as if the packet had just * arrived on the interface to which we are about * to send. The multicast forwarding function * recursively calls this function, using the * IP_FORWARDING flag to prevent infinite recursion. * * Multicasts that are looped back by ip_mloopback(), * above, will be forwarded by the ip_input() routine, * if necessary. */ if ((_mCastRouteFwdHook != NULL) && ((flags & IP_FORWARDING) == 0)) { if ((*_mCastRouteFwdHook)(m, ifp, ip, NULL) != 0) { m_freem(m); goto done; } } } /* * Multicasts with a time-to-live of zero may be looped- * back, above, but must not be transmitted on a network. * Also, multicasts addressed to the loopback interface * are not sent -- the above call to ip_mloopback() will * loop back a copy if this host actually belongs to the * destination group on the loopback interface. */ if (ip->ip_ttl == 0 || ifp == loif) { m_freem(m); goto done; } /* * Use the predefined MTU size for outgoing packets. The * point-to-multipoint devices which permit a different * value do not support multicast addresses. */ mtu = ifp->if_mtu; goto sendit; }#ifndef notdef /* * If source address not specified yet, use address * of outgoing interface. */ if (ip->ip_src.s_addr == INADDR_ANY) ip->ip_src = IA_SIN(ia)->sin_addr;#ifdef WV_INSTRUMENTATION#ifdef INCLUDE_WVNET /* * Update the source destination address if not previously * specified, since it will be unavailable at the "done:" label. */ srcAddr = ip->ip_src.s_addr;#endif#endif#endif /* * Get the MTU size. Point-to-multipoint devices allow a separate * MTU for each destination address. All other devices use a * predefined size. */ if (ifp->if_type == IFT_PMP) { /* * The SIOCGMTU operation reads the address from the * argument and returns the corresponding MTU size. */ mtuQuery.family = AF_INET; mtuQuery.dstIpAddr = dst->sin_addr.s_addr; error = ifp->if_ioctl (ifp, SIOCGMTU, (caddr_t)&mtuQuery); if (error) { error = EHOSTUNREACH; goto bad; } mtu = mtuQuery.mtu; } else mtu = ifp->if_mtu; /* * Look for broadcast address and * and verify user is allowed to send * such a packet. */ if (in_broadcast(dst->sin_addr, ifp)) { if ((ifp->if_flags & IFF_BROADCAST) == 0) {#ifdef WV_INSTRUMENTATION#ifdef INCLUDE_WVNET /* WV_NET_CRITICAL event */ WV_NET_ADDROUT_EVENT_2 (NET_CORE_EVENT, WV_NET_CRITICAL, 21, 2, ip->ip_src.s_addr, ip->ip_dst.s_addr, WV_NETEVENT_IPOUT_BADFLAGS, WV_NET_SEND, dst->sin_addr.s_addr, ifp)#endif /* INCLUDE_WVNET */#endif error = EADDRNOTAVAIL; goto bad; } if ((flags & IP_ALLOWBROADCAST) == 0) {#ifdef WV_INSTRUMENTATION#ifdef INCLUDE_WVNET /* WV_NET_CRITICAL event */ WV_NET_ADDROUT_EVENT_2 (NET_CORE_EVENT, WV_NET_CRITICAL, 21, 2, ip->ip_src.s_addr, ip->ip_dst.s_addr, WV_NETEVENT_IPOUT_BADFLAGS, WV_NET_SEND, dst->sin_addr.s_addr, 0)#endif /* INCLUDE_WVNET */#endif error = EACCES; goto bad; } /* allow broadcast messages to be fragmented if flag set */ if (((u_short)ip->ip_len > mtu) && !(_ipCfgFlags & IP_DO_LARGE_BCAST)) {#ifdef WV_INSTRUMENTATION#ifdef INCLUDE_WVNET /* WV_NET_CRITICAL event */ WV_NET_ADDROUT_EVENT_2 (NET_CORE_EVENT, WV_NET_CRITICAL, 22, 3, ip->ip_src.s_addr, ip->ip_dst.s_addr, WV_NETEVENT_IPOUT_CANTFRAG, WV_NET_SEND, dst->sin_addr.s_addr, ifp)#endif /* INCLUDE_WVNET */#endif error = EMSGSIZE; goto bad; } m->m_flags |= M_BCAST; } else m->m_flags &= ~M_BCAST;sendit: if (_func_ipsecOutput != NULL) { /* Deliver the packet to ipsec for further processing/filtering. */ error = _func_ipsecOutput (&m0, opt, ro, flags, imo, dst, ia); m = m0; ip = mtod(m, struct ip *); if (error != 0) { /* If there is an error we should have already freed the packet */ if (error == ERROR) { /* Unless the error is very specific we do not propagate the error; so we reset it */ error = OK; } goto done; } } /* * If small enough for interface, can just send directly. */ if ((u_short)ip->ip_len <= mtu) {#ifdef WV_INSTRUMENTATION#ifdef INCLUDE_WVNET /* WV_NET_INFO event */ WV_NET_ADDROUT_EVENT_4 (NET_CORE_EVENT, WV_NET_INFO, 54, 10, ip->ip_src.s_addr, ip->ip_dst.s_addr, WV_NETEVENT_IPOUT_SENDALL, WV_NET_SEND, ip->ip_src.s_addr, ip->ip_dst.s_addr, (u_long)ip->ip_len, mtu)#endif /* INCLUDE_WVNET */#endif ip->ip_len = htons((u_short)ip->ip_len); ip->ip_off = htons((u_short)ip->ip_off); ip->ip_sum = 0; if (_ipCfgFlags & IP_DO_CHECKSUM_SND) ip->ip_sum = in_cksum(m, hlen); error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, ro->ro_rt); goto done; } /* * Too large for interface; fragment if possible. * Must be able to put at least 8 bytes per fragment. */ if (ip->ip_off & IP_DF) {#ifdef WV_INSTRUMENTATION#ifdef INCLUDE_WVNET /* WV_NET_CRITICAL event */ WV_NET_ADDROUT_EVENT_2 (NET_CORE_EVENT, WV_NET_CRITICAL, 22, 3, ip->ip_src.s_addr, ip->ip_dst.s_addr, WV_NETEVENT_IPOUT_CANTFRAG, WV_NET_SEND, dst->sin_addr.s_addr, ifp)#endif /* INCLUDE_WVNET */#endif error = EMSGSIZE; /* * If the interface is using a new (smaller) MTU size than * originally assigned, update the current path MTU estimate * (if any) to the new (largest possible) value. */ if (ro->ro_rt && (ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) && (ro->ro_rt->rt_rmx.rmx_mtu > mtu)) { ro->ro_rt->rt_rmx.rmx_mtu = mtu; }#ifdef VIRTUAL_STACK _ipstat.ips_cantfrag++;#else ipstat.ips_cantfrag++;#endif /* VIRTUAL_STACK */ goto bad; } len = (mtu - hlen) &~ 7; if (len < 8) {#ifdef WV_INSTRUMENTATION#ifdef INCLUDE_WVNET /* WV_NET_CRITICAL event */ WV_NET_ADDROUT_EVENT_2 (NET_CORE_EVENT, WV_NET_CRITICAL, 23, 4, ip->ip_src.s_addr, ip->ip_dst.s_addr, WV_NETEVENT_IPOUT_SHORTMSG, WV_NET_SEND, dst->sin_addr.s_addr, ifp)#endif /* INCLUDE_WVNET */#endif error = EMSGSIZE; goto bad; } { int mhlen, firstlen = len; struct mbuf **mnext = &m->m_nextpkt;#ifdef WV_INSTRUMENTATION#ifdef INCLUDE_WVNET /* WV_NET_INFO event */ WV_NET_ADDROUT_EVENT_4 (NET_CORE_EVENT, WV_NET_INFO, 55, 11, ip->ip_src.s_addr, ip->ip_dst.s_addr, WV_NETEVENT_IPOUT_SENDFRAG, WV_NET_SEND, dst->sin_addr.s_addr, (u_long)ip->ip_len, hlen, mtu);#endif /* INCLUDE_WVNET */#endif /* * Loop through length of segment after first fragment, * make new header and copy data of each part and link onto chain. */ m0 = m; mhlen = sizeof (struct ip); for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { m= mHdrClGet(M_DONTWAIT, MT_HEADER, CL_SIZE_128, TRUE); if (m == 0) { error = ENOBUFS;#ifdef VIRTUAL_STACK _ipstat.ips_odropped++;#else ipstat.ips_odropped++;#endif /* VIRTUAL_STACK */ goto sendorfree; } m->m_flags = m0->m_flags; m->m_data += max_linkhdr; mhip = mtod(m, struct ip *); *mhip = *ip; if (hlen > sizeof (struct ip)) { mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); mhip->ip_hl = mhlen >> 2; } m->m_len = mhlen; mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); if (ip->ip_off & IP_MF) mhip->ip_off |= IP_MF; if (off + len >= (u_short)ip->ip_len) len = (u_short)ip->ip_len - off; else mhip->ip_off |= IP_MF; mhip->ip_len = htons((u_short)(len + mhlen)); m->m_next = m_copy(m0, off, len); if (m->m_next == 0) { (void) m_free(m); error = ENOBUFS; /* ??? */#ifdef VIRTUAL_STACK _ipstat.ips_odropped++;#else ipstat.ips_odropped++;#endif /* VIRTUAL_STACK */ goto sendorfree; } m->m_pkthdr.len = mhlen + len; m->m_pkthdr.rcvif = (struct ifnet *)0; mhip->ip_off = htons((u_short)mhip->ip_off); mhip->ip_sum = 0; if (_ipCfgFlags & IP_DO_CHECKSUM_SND) mhip->ip_sum = in_cksum(m, mhlen); *mnext = m; mnext = &m->m_nextpkt;#ifdef VIRTUAL_STACK _ipstat.ips_ofragments++;#else ipstat.ips_ofragments++;#endif /* VIRTUAL_STACK */ } /* * Update first fragment by trimming what's been copied out * and updating header, then send each fragment (in order). */ m = m0; m_adj(m, hlen + firstlen - (u_short)ip->ip_len); m->m_pkthdr.len = hlen + firstlen; ip->ip_len = htons((u_short)m->m_pkthdr.len); ip->ip_off = htons((u_short)(ip->ip_off | IP_MF)); ip->ip_sum = 0; if (_ipCfgFlags & IP_DO_CHECKSUM_SND) ip->ip_sum = in_cksum(m, hlen);sendorfree: for (m = m0; m; m = m0) { m0 = m->m_nextpkt; m->m_nextpkt = 0; if (error == 0) error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, ro->ro_rt); else m_freem(m); } if (error == 0)#ifdef VIRTUAL_STACK _ipstat.ips_fragmented++;#else ipstat.ips_fragmented++;#endif /* VIRTUAL_STACK */ }done:#ifdef WV_INSTRUMENTATION#ifdef INCLUDE_WVNET /* WV_NET_ERROR event */ WV_NET_ADDROUT_EVENT_4 (NET_CORE_EVENT, WV_NET_ERROR, 31, 9, srcAddr, dstAddr, WV_NETEVENT_IPOUT_FINISH, WV_NET_SEND, error, ifp, srcAddr, dstAddr)#endif /* INCLUDE_WVNET */#endif if ((ro == &iproute) && ((flags & IP_ROUTETOIF) == 0) && ro->ro_rt) { RTFREE(ro->ro_rt); } return (error);bad: m_freem(m); goto done;}/* * Insert IP options into preformed packet. * Adjust IP destination as required for IP source routing, * as indicated by a non-zero in_addr at the start of the options. */static struct mbuf *ip_insertoptions(m, opt, phlen) register struct mbuf *m; struct mbuf *opt; int *phlen;{ register struct ipoption *p = mtod(opt, struct ipoption *); struct mbuf *n; register struct ip *ip = mtod(m, struct ip *); unsigned optlen; optlen = opt->m_len - sizeof(p->ipopt_dst); if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) return (m); /* XXX should fail */ if (p->ipopt_dst.s_addr) ip->ip_dst = p->ipopt_dst;#if 0 /* XXX changed for default cluster support vinai * The reason why always new clusters are allocted, * is that, data could be overwritten since clusters are shared. */ if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {#endif n = mHdrClGet(M_DONTWAIT, MT_HEADER, CL_SIZE_128, TRUE); if (n == 0) return (m); n->m_pkthdr.len = m->m_pkthdr.len + optlen; m->m_len -= sizeof(struct ip); m->m_data += sizeof(struct ip); n->m_next = m; m = n; m->m_len = optlen + sizeof(struct ip); m->m_data += max_linkhdr; bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));#if 0 /* XXX changed for default cluster support vinai */ } else { m->m_data -= optlen; m->m_len += optlen; m->m_pkthdr.len += optlen; ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); }#endif ip = mtod(m, struct ip *); bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen); *phlen = sizeof(struct ip) + optlen;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -