1*16545Skarels /* ip_output.c 6.4 84/05/25 */ 24571Swnj 34496Swnj #include "../h/param.h" 44662Swnj #include "../h/mbuf.h" 510893Ssam #include "../h/errno.h" 64662Swnj #include "../h/socket.h" 74924Swnj #include "../h/socketvar.h" 810893Ssam 910893Ssam #include "../net/if.h" 1010893Ssam #include "../net/route.h" 1110893Ssam 128400Swnj #include "../netinet/in.h" 138400Swnj #include "../netinet/in_systm.h" 148400Swnj #include "../netinet/ip.h" 158400Swnj #include "../netinet/ip_var.h" 164496Swnj 1712460Ssam #ifdef vax 1812460Ssam #include "../vax/mtpr.h" 1912460Ssam #endif 2010893Ssam 2112417Ssam ip_output(m, opt, ro, flags) 224924Swnj struct mbuf *m; 235085Swnj struct mbuf *opt; 246339Ssam struct route *ro; 2512417Ssam int flags; 264496Swnj { 274924Swnj register struct ip *ip = mtod(m, struct ip *); 285085Swnj register struct ifnet *ifp; 296505Ssam int len, hlen = sizeof (struct ip), off, error = 0; 306339Ssam struct route iproute; 316351Ssam struct sockaddr *dst; 324496Swnj 335219Swnj if (opt) /* XXX */ 345242Sroot (void) m_free(opt); /* XXX */ 354924Swnj /* 364924Swnj * Fill in IP header. 374924Swnj */ 3812417Ssam if ((flags & IP_FORWARDING) == 0) { 3912417Ssam ip->ip_v = IPVERSION; 4012417Ssam ip->ip_off &= IP_DF; 4112417Ssam ip->ip_id = htons(ip_id++); 42*16545Skarels ip->ip_hl = hlen >> 2; 43*16545Skarels } else 44*16545Skarels ip->ip_id = htons(ip->ip_id); 454496Swnj 464545Swnj /* 477155Swnj * Route packet. 485085Swnj */ 496339Ssam if (ro == 0) { 506339Ssam ro = &iproute; 516339Ssam bzero((caddr_t)ro, sizeof (*ro)); 525085Swnj } 537155Swnj dst = &ro->ro_dst; 546339Ssam if (ro->ro_rt == 0) { 556368Ssam ro->ro_dst.sa_family = AF_INET; 566368Ssam ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst; 577155Swnj /* 5812417Ssam * If routing to interface only, 5912417Ssam * short circuit routing lookup. 607155Swnj */ 6112417Ssam if (flags & IP_ROUTETOIF) { 628638Sroot ifp = if_ifonnetof(in_netof(ip->ip_dst)); 6312417Ssam if (ifp == 0) { 6412417Ssam error = ENETUNREACH; 6512417Ssam goto bad; 6612417Ssam } 677155Swnj goto gotif; 687155Swnj } 696368Ssam rtalloc(ro); 70*16545Skarels } else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) { 7115716Skarels /* 7215716Skarels * The old route has gone away; try for a new one. 7315716Skarels */ 7415716Skarels rtfree(ro->ro_rt); 7515716Skarels rtalloc(ro); 766339Ssam } 7712417Ssam if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) { 7812417Ssam error = ENETUNREACH; 7912417Ssam goto bad; 8012417Ssam } 817155Swnj ro->ro_rt->rt_use++; 8215276Ssam if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST)) 837155Swnj dst = &ro->ro_rt->rt_gateway; 847155Swnj gotif: 8510402Ssam #ifndef notdef 867155Swnj /* 877155Swnj * If source address not specified yet, use address 887155Swnj * of outgoing interface. 897155Swnj */ 9010402Ssam if (in_lnaof(ip->ip_src) == INADDR_ANY) 917155Swnj ip->ip_src.s_addr = 927155Swnj ((struct sockaddr_in *)&ifp->if_addr)->sin_addr.s_addr; 9310402Ssam #endif 946505Ssam 957155Swnj /* 9610402Ssam * Look for broadcast address and 9710402Ssam * and verify user is allowed to send 9810146Ssam * such a packet. 997155Swnj */ 10012762Ssam if (in_lnaof(((struct sockaddr_in *)dst)->sin_addr) == INADDR_ANY) { 10110146Ssam if ((ifp->if_flags & IFF_BROADCAST) == 0) { 10210146Ssam error = EADDRNOTAVAIL; 10310146Ssam goto bad; 10410146Ssam } 10512417Ssam if ((flags & IP_ALLOWBROADCAST) == 0) { 1067155Swnj error = EACCES; 1076339Ssam goto bad; 1086505Ssam } 10910146Ssam /* don't allow broadcast messages to be fragmented */ 11010146Ssam if (ip->ip_len > ifp->if_mtu) { 11110146Ssam error = EMSGSIZE; 11210146Ssam goto bad; 11310146Ssam } 1146339Ssam } 1156339Ssam 1165085Swnj /* 1174924Swnj * If small enough for interface, can just send directly. 1184545Swnj */ 1195085Swnj if (ip->ip_len <= ifp->if_mtu) { 1205085Swnj ip->ip_len = htons((u_short)ip->ip_len); 1215085Swnj ip->ip_off = htons((u_short)ip->ip_off); 1225085Swnj ip->ip_sum = 0; 1235085Swnj ip->ip_sum = in_cksum(m, hlen); 1247155Swnj error = (*ifp->if_output)(ifp, m, dst); 1257155Swnj goto done; 1264908Swnj } 1274924Swnj 1284924Swnj /* 1294924Swnj * Too large for interface; fragment if possible. 1304924Swnj * Must be able to put at least 8 bytes per fragment. 1314924Swnj */ 1326505Ssam if (ip->ip_off & IP_DF) { 1336505Ssam error = EMSGSIZE; 1344924Swnj goto bad; 1356505Ssam } 1365085Swnj len = (ifp->if_mtu - hlen) &~ 7; 1376505Ssam if (len < 8) { 1386505Ssam error = EMSGSIZE; 1394924Swnj goto bad; 1406505Ssam } 1414924Swnj 1424924Swnj /* 1434924Swnj * Discard IP header from logical mbuf for m_copy's sake. 1444924Swnj * Loop through length of segment, make a copy of each 1454924Swnj * part and output. 1464924Swnj */ 1474924Swnj m->m_len -= sizeof (struct ip); 1484924Swnj m->m_off += sizeof (struct ip); 1495892Sroot for (off = 0; off < ip->ip_len-hlen; off += len) { 15010012Ssam struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER); 1514924Swnj struct ip *mhip; 1524924Swnj 1536505Ssam if (mh == 0) { 1546505Ssam error = ENOBUFS; 1554924Swnj goto bad; 1566505Ssam } 1574924Swnj mh->m_off = MMAXOFF - hlen; 1584924Swnj mhip = mtod(mh, struct ip *); 1594924Swnj *mhip = *ip; 1604952Swnj if (hlen > sizeof (struct ip)) { 1614924Swnj int olen = ip_optcopy(ip, mhip, off); 1624924Swnj mh->m_len = sizeof (struct ip) + olen; 1634924Swnj } else 1644924Swnj mh->m_len = sizeof (struct ip); 1655770Swnj mhip->ip_off = off >> 3; 166*16545Skarels if (ip->ip_off & IP_MF) 167*16545Skarels mhip->ip_off |= IP_MF; 1685892Sroot if (off + len >= ip->ip_len-hlen) 1695892Sroot len = mhip->ip_len = ip->ip_len - hlen - off; 1704924Swnj else { 1714924Swnj mhip->ip_len = len; 1724924Swnj mhip->ip_off |= IP_MF; 1734496Swnj } 1745770Swnj mhip->ip_len += sizeof (struct ip); 1755770Swnj mhip->ip_len = htons((u_short)mhip->ip_len); 1764924Swnj mh->m_next = m_copy(m, off, len); 1774924Swnj if (mh->m_next == 0) { 1784967Swnj (void) m_free(mh); 1796505Ssam error = ENOBUFS; /* ??? */ 1804924Swnj goto bad; 1814674Swnj } 1825892Sroot mhip->ip_off = htons((u_short)mhip->ip_off); 1835892Sroot mhip->ip_sum = 0; 1845892Sroot mhip->ip_sum = in_cksum(mh, hlen); 1856505Ssam if (error = (*ifp->if_output)(ifp, mh, dst)) 1866505Ssam break; 1874924Swnj } 1887155Swnj m_freem(m); 1897155Swnj goto done; 1907155Swnj 1914924Swnj bad: 1924924Swnj m_freem(m); 1937155Swnj done: 19412417Ssam if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) 1957155Swnj RTFREE(ro->ro_rt); 1966505Ssam return (error); 1974924Swnj } 1984924Swnj 1994924Swnj /* 2004924Swnj * Copy options from ip to jp. 2014952Swnj * If off is 0 all options are copied 2024924Swnj * otherwise copy selectively. 2034924Swnj */ 2044924Swnj ip_optcopy(ip, jp, off) 2054924Swnj struct ip *ip, *jp; 2064924Swnj int off; 2074924Swnj { 2084924Swnj register u_char *cp, *dp; 2094924Swnj int opt, optlen, cnt; 2104924Swnj 2114924Swnj cp = (u_char *)(ip + 1); 2124924Swnj dp = (u_char *)(jp + 1); 2134924Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 2144924Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 2154924Swnj opt = cp[0]; 2164924Swnj if (opt == IPOPT_EOL) 2174924Swnj break; 2184924Swnj if (opt == IPOPT_NOP) 2194924Swnj optlen = 1; 2204924Swnj else 2214924Swnj optlen = cp[1]; 2224924Swnj if (optlen > cnt) /* XXX */ 2234924Swnj optlen = cnt; /* XXX */ 2244924Swnj if (off == 0 || IPOPT_COPIED(opt)) { 2254952Swnj bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 2264924Swnj dp += optlen; 2274674Swnj } 2284545Swnj } 2294924Swnj for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 2304924Swnj *dp++ = IPOPT_EOL; 2314924Swnj return (optlen); 2324496Swnj } 233