1*6377Ssam /* ip_output.c 1.30 82/03/30 */ 24571Swnj 34496Swnj #include "../h/param.h" 44662Swnj #include "../h/mbuf.h" 54724Swnj #include "../h/mtpr.h" 64662Swnj #include "../h/socket.h" 74924Swnj #include "../h/socketvar.h" 85085Swnj #include "../net/in.h" 95085Swnj #include "../net/in_systm.h" 105085Swnj #include "../net/if.h" 114802Swnj #include "../net/ip.h" 124899Swnj #include "../net/ip_var.h" 136339Ssam #include "../net/route.h" 144496Swnj 156339Ssam ip_output(m, opt, ro, allowbroadcast) 164924Swnj struct mbuf *m; 175085Swnj struct mbuf *opt; 186339Ssam struct route *ro; 196211Swnj int allowbroadcast; 204496Swnj { 214924Swnj register struct ip *ip = mtod(m, struct ip *); 225085Swnj register struct ifnet *ifp; 236368Ssam int len, hlen = sizeof (struct ip), off; 246339Ssam struct route iproute; 256351Ssam struct sockaddr *dst; 264496Swnj 274496Swnj COUNT(IP_OUTPUT); 285219Swnj if (opt) /* XXX */ 295242Sroot (void) m_free(opt); /* XXX */ 304924Swnj /* 314924Swnj * Fill in IP header. 324924Swnj */ 334924Swnj ip->ip_v = IPVERSION; 344924Swnj ip->ip_hl = hlen >> 2; 354924Swnj ip->ip_off &= IP_DF; 365085Swnj ip->ip_id = htons(ip_id++); 374496Swnj 384545Swnj /* 396351Ssam * Find interface for this packet in the routing 406351Ssam * table. Note each interface has placed itself 41*6377Ssam * in there at boot time, so calls to rtalloc 42*6377Ssam * degenerate to if_ifonnetof(ip->ip_dst.s_net). 435085Swnj */ 446339Ssam if (ro == 0) { 456339Ssam ro = &iproute; 466339Ssam bzero((caddr_t)ro, sizeof (*ro)); 475085Swnj } 486339Ssam if (ro->ro_rt == 0) { 496368Ssam ro->ro_dst.sa_family = AF_INET; 506368Ssam ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst; 516368Ssam rtalloc(ro); 52*6377Ssam if (ro != &iproute) 53*6377Ssam ro->ro_rt->rt_refcnt++; 546339Ssam } 556368Ssam if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) { 566368Ssam printf("no route to %x\n", ip->ip_dst.s_addr); 576211Swnj goto bad; 586368Ssam } 596368Ssam dst = ro->ro_rt->rt_flags&RTF_DIRECT ? 606368Ssam (struct sockaddr *)&ro->ro_dst : &ro->ro_rt->rt_gateway; 616339Ssam if (!allowbroadcast && (ifp->if_flags & IFF_BROADCAST)) { 626339Ssam struct sockaddr_in *sin; 636339Ssam 646339Ssam sin = (struct sockaddr_in *)&ifp->if_broadaddr; 656339Ssam if (sin->sin_addr.s_addr == ip->ip_dst.s_addr) 666339Ssam goto bad; 676339Ssam } 686339Ssam 695085Swnj /* 704924Swnj * If small enough for interface, can just send directly. 714545Swnj */ 725085Swnj if (ip->ip_len <= ifp->if_mtu) { 735219Swnj #if vax 745085Swnj ip->ip_len = htons((u_short)ip->ip_len); 755085Swnj ip->ip_off = htons((u_short)ip->ip_off); 765219Swnj #endif 775085Swnj ip->ip_sum = 0; 785085Swnj ip->ip_sum = in_cksum(m, hlen); 79*6377Ssam ro->ro_rt->rt_use++; 806351Ssam return ((*ifp->if_output)(ifp, m, dst)); 814908Swnj } 824924Swnj 834924Swnj /* 844924Swnj * Too large for interface; fragment if possible. 854924Swnj * Must be able to put at least 8 bytes per fragment. 864924Swnj */ 874924Swnj if (ip->ip_off & IP_DF) 884924Swnj goto bad; 895085Swnj len = (ifp->if_mtu - hlen) &~ 7; 904924Swnj if (len < 8) 914924Swnj goto bad; 924924Swnj 934924Swnj /* 944924Swnj * Discard IP header from logical mbuf for m_copy's sake. 954924Swnj * Loop through length of segment, make a copy of each 964924Swnj * part and output. 974924Swnj */ 984924Swnj m->m_len -= sizeof (struct ip); 994924Swnj m->m_off += sizeof (struct ip); 1005892Sroot for (off = 0; off < ip->ip_len-hlen; off += len) { 1015584Sroot struct mbuf *mh = m_get(M_DONTWAIT); 1024924Swnj struct ip *mhip; 1034924Swnj 1044924Swnj if (mh == 0) 1054924Swnj goto bad; 1064924Swnj mh->m_off = MMAXOFF - hlen; 1074924Swnj mhip = mtod(mh, struct ip *); 1084924Swnj *mhip = *ip; 1094952Swnj if (hlen > sizeof (struct ip)) { 1104924Swnj int olen = ip_optcopy(ip, mhip, off); 1114924Swnj mh->m_len = sizeof (struct ip) + olen; 1124924Swnj } else 1134924Swnj mh->m_len = sizeof (struct ip); 1145770Swnj mhip->ip_off = off >> 3; 1155892Sroot if (off + len >= ip->ip_len-hlen) 1165892Sroot len = mhip->ip_len = ip->ip_len - hlen - off; 1174924Swnj else { 1184924Swnj mhip->ip_len = len; 1194924Swnj mhip->ip_off |= IP_MF; 1204496Swnj } 1215770Swnj mhip->ip_len += sizeof (struct ip); 1225770Swnj #if vax 1235770Swnj mhip->ip_len = htons((u_short)mhip->ip_len); 1245770Swnj #endif 1254924Swnj mh->m_next = m_copy(m, off, len); 1264924Swnj if (mh->m_next == 0) { 1274967Swnj (void) m_free(mh); 1284924Swnj goto bad; 1294674Swnj } 1305770Swnj #if vax 1315892Sroot mhip->ip_off = htons((u_short)mhip->ip_off); 1325770Swnj #endif 1335892Sroot mhip->ip_sum = 0; 1345892Sroot mhip->ip_sum = in_cksum(mh, hlen); 135*6377Ssam ro->ro_rt->rt_use++; 1366351Ssam if ((*ifp->if_output)(ifp, mh, dst) == 0) 1375085Swnj goto bad; 1384924Swnj } 1395085Swnj m_freem(m); 1405085Swnj return (1); 1414924Swnj bad: 1424924Swnj m_freem(m); 1435085Swnj return (0); 1444924Swnj } 1454924Swnj 1464924Swnj /* 1474924Swnj * Copy options from ip to jp. 1484952Swnj * If off is 0 all options are copied 1494924Swnj * otherwise copy selectively. 1504924Swnj */ 1514924Swnj ip_optcopy(ip, jp, off) 1524924Swnj struct ip *ip, *jp; 1534924Swnj int off; 1544924Swnj { 1554924Swnj register u_char *cp, *dp; 1564924Swnj int opt, optlen, cnt; 1574924Swnj 1584952Swnj COUNT(IP_OPTCOPY); 1594924Swnj cp = (u_char *)(ip + 1); 1604924Swnj dp = (u_char *)(jp + 1); 1614924Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1624924Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 1634924Swnj opt = cp[0]; 1644924Swnj if (opt == IPOPT_EOL) 1654924Swnj break; 1664924Swnj if (opt == IPOPT_NOP) 1674924Swnj optlen = 1; 1684924Swnj else 1694924Swnj optlen = cp[1]; 1704924Swnj if (optlen > cnt) /* XXX */ 1714924Swnj optlen = cnt; /* XXX */ 1724924Swnj if (off == 0 || IPOPT_COPIED(opt)) { 1734952Swnj bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 1744924Swnj dp += optlen; 1754674Swnj } 1764545Swnj } 1774924Swnj for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1784924Swnj *dp++ = IPOPT_EOL; 1794924Swnj return (optlen); 1804496Swnj } 181