1 /* ip_output.c 1.30 82/03/30 */ 2 3 #include "../h/param.h" 4 #include "../h/mbuf.h" 5 #include "../h/mtpr.h" 6 #include "../h/socket.h" 7 #include "../h/socketvar.h" 8 #include "../net/in.h" 9 #include "../net/in_systm.h" 10 #include "../net/if.h" 11 #include "../net/ip.h" 12 #include "../net/ip_var.h" 13 #include "../net/route.h" 14 15 ip_output(m, opt, ro, allowbroadcast) 16 struct mbuf *m; 17 struct mbuf *opt; 18 struct route *ro; 19 int allowbroadcast; 20 { 21 register struct ip *ip = mtod(m, struct ip *); 22 register struct ifnet *ifp; 23 int len, hlen = sizeof (struct ip), off; 24 struct route iproute; 25 struct sockaddr *dst; 26 27 COUNT(IP_OUTPUT); 28 if (opt) /* XXX */ 29 (void) m_free(opt); /* XXX */ 30 /* 31 * Fill in IP header. 32 */ 33 ip->ip_v = IPVERSION; 34 ip->ip_hl = hlen >> 2; 35 ip->ip_off &= IP_DF; 36 ip->ip_id = htons(ip_id++); 37 38 /* 39 * Find interface for this packet in the routing 40 * table. Note each interface has placed itself 41 * in there at boot time, so calls to rtalloc 42 * degenerate to if_ifonnetof(ip->ip_dst.s_net). 43 */ 44 if (ro == 0) { 45 ro = &iproute; 46 bzero((caddr_t)ro, sizeof (*ro)); 47 } 48 if (ro->ro_rt == 0) { 49 ro->ro_dst.sa_family = AF_INET; 50 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst; 51 rtalloc(ro); 52 if (ro != &iproute) 53 ro->ro_rt->rt_refcnt++; 54 } 55 if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) { 56 printf("no route to %x\n", ip->ip_dst.s_addr); 57 goto bad; 58 } 59 dst = ro->ro_rt->rt_flags&RTF_DIRECT ? 60 (struct sockaddr *)&ro->ro_dst : &ro->ro_rt->rt_gateway; 61 if (!allowbroadcast && (ifp->if_flags & IFF_BROADCAST)) { 62 struct sockaddr_in *sin; 63 64 sin = (struct sockaddr_in *)&ifp->if_broadaddr; 65 if (sin->sin_addr.s_addr == ip->ip_dst.s_addr) 66 goto bad; 67 } 68 69 /* 70 * If small enough for interface, can just send directly. 71 */ 72 if (ip->ip_len <= ifp->if_mtu) { 73 #if vax 74 ip->ip_len = htons((u_short)ip->ip_len); 75 ip->ip_off = htons((u_short)ip->ip_off); 76 #endif 77 ip->ip_sum = 0; 78 ip->ip_sum = in_cksum(m, hlen); 79 ro->ro_rt->rt_use++; 80 return ((*ifp->if_output)(ifp, m, dst)); 81 } 82 83 /* 84 * Too large for interface; fragment if possible. 85 * Must be able to put at least 8 bytes per fragment. 86 */ 87 if (ip->ip_off & IP_DF) 88 goto bad; 89 len = (ifp->if_mtu - hlen) &~ 7; 90 if (len < 8) 91 goto bad; 92 93 /* 94 * Discard IP header from logical mbuf for m_copy's sake. 95 * Loop through length of segment, make a copy of each 96 * part and output. 97 */ 98 m->m_len -= sizeof (struct ip); 99 m->m_off += sizeof (struct ip); 100 for (off = 0; off < ip->ip_len-hlen; off += len) { 101 struct mbuf *mh = m_get(M_DONTWAIT); 102 struct ip *mhip; 103 104 if (mh == 0) 105 goto bad; 106 mh->m_off = MMAXOFF - hlen; 107 mhip = mtod(mh, struct ip *); 108 *mhip = *ip; 109 if (hlen > sizeof (struct ip)) { 110 int olen = ip_optcopy(ip, mhip, off); 111 mh->m_len = sizeof (struct ip) + olen; 112 } else 113 mh->m_len = sizeof (struct ip); 114 mhip->ip_off = off >> 3; 115 if (off + len >= ip->ip_len-hlen) 116 len = mhip->ip_len = ip->ip_len - hlen - off; 117 else { 118 mhip->ip_len = len; 119 mhip->ip_off |= IP_MF; 120 } 121 mhip->ip_len += sizeof (struct ip); 122 #if vax 123 mhip->ip_len = htons((u_short)mhip->ip_len); 124 #endif 125 mh->m_next = m_copy(m, off, len); 126 if (mh->m_next == 0) { 127 (void) m_free(mh); 128 goto bad; 129 } 130 #if vax 131 mhip->ip_off = htons((u_short)mhip->ip_off); 132 #endif 133 mhip->ip_sum = 0; 134 mhip->ip_sum = in_cksum(mh, hlen); 135 ro->ro_rt->rt_use++; 136 if ((*ifp->if_output)(ifp, mh, dst) == 0) 137 goto bad; 138 } 139 m_freem(m); 140 return (1); 141 bad: 142 m_freem(m); 143 return (0); 144 } 145 146 /* 147 * Copy options from ip to jp. 148 * If off is 0 all options are copied 149 * otherwise copy selectively. 150 */ 151 ip_optcopy(ip, jp, off) 152 struct ip *ip, *jp; 153 int off; 154 { 155 register u_char *cp, *dp; 156 int opt, optlen, cnt; 157 158 COUNT(IP_OPTCOPY); 159 cp = (u_char *)(ip + 1); 160 dp = (u_char *)(jp + 1); 161 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 162 for (; cnt > 0; cnt -= optlen, cp += optlen) { 163 opt = cp[0]; 164 if (opt == IPOPT_EOL) 165 break; 166 if (opt == IPOPT_NOP) 167 optlen = 1; 168 else 169 optlen = cp[1]; 170 if (optlen > cnt) /* XXX */ 171 optlen = cnt; /* XXX */ 172 if (off == 0 || IPOPT_COPIED(opt)) { 173 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 174 dp += optlen; 175 } 176 } 177 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 178 *dp++ = IPOPT_EOL; 179 return (optlen); 180 } 181