1*12460Ssam /* ip_output.c 1.48 83/05/15 */ 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 17*12460Ssam #ifdef vax 18*12460Ssam #include "../vax/mtpr.h" 19*12460Ssam #endif 2010893Ssam 217155Swnj int ipnorouteprint = 0; 227155Swnj 2312417Ssam ip_output(m, opt, ro, flags) 244924Swnj struct mbuf *m; 255085Swnj struct mbuf *opt; 266339Ssam struct route *ro; 2712417Ssam int flags; 284496Swnj { 294924Swnj register struct ip *ip = mtod(m, struct ip *); 305085Swnj register struct ifnet *ifp; 316505Ssam int len, hlen = sizeof (struct ip), off, error = 0; 326339Ssam struct route iproute; 336351Ssam struct sockaddr *dst; 344496Swnj 355219Swnj if (opt) /* XXX */ 365242Sroot (void) m_free(opt); /* XXX */ 374924Swnj /* 384924Swnj * Fill in IP header. 394924Swnj */ 404924Swnj ip->ip_hl = hlen >> 2; 4112417Ssam if ((flags & IP_FORWARDING) == 0) { 4212417Ssam ip->ip_v = IPVERSION; 4312417Ssam ip->ip_off &= IP_DF; 4412417Ssam ip->ip_id = htons(ip_id++); 4512417Ssam } 464496Swnj 474545Swnj /* 487155Swnj * Route packet. 495085Swnj */ 506339Ssam if (ro == 0) { 516339Ssam ro = &iproute; 526339Ssam bzero((caddr_t)ro, sizeof (*ro)); 535085Swnj } 547155Swnj dst = &ro->ro_dst; 556339Ssam if (ro->ro_rt == 0) { 566368Ssam ro->ro_dst.sa_family = AF_INET; 576368Ssam ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst; 587155Swnj /* 5912417Ssam * If routing to interface only, 6012417Ssam * short circuit routing lookup. 617155Swnj */ 6212417Ssam if (flags & IP_ROUTETOIF) { 638638Sroot ifp = if_ifonnetof(in_netof(ip->ip_dst)); 6412417Ssam if (ifp == 0) { 6512417Ssam error = ENETUNREACH; 6612417Ssam goto bad; 6712417Ssam } 687155Swnj goto gotif; 697155Swnj } 706368Ssam rtalloc(ro); 716339Ssam } 7212417Ssam if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) { 7312417Ssam error = ENETUNREACH; 7412417Ssam goto bad; 7512417Ssam } 767155Swnj ro->ro_rt->rt_use++; 777155Swnj if (ro->ro_rt->rt_flags & RTF_GATEWAY) 787155Swnj dst = &ro->ro_rt->rt_gateway; 797155Swnj gotif: 8010402Ssam #ifndef notdef 817155Swnj /* 827155Swnj * If source address not specified yet, use address 837155Swnj * of outgoing interface. 847155Swnj */ 8510402Ssam if (in_lnaof(ip->ip_src) == INADDR_ANY) 867155Swnj ip->ip_src.s_addr = 877155Swnj ((struct sockaddr_in *)&ifp->if_addr)->sin_addr.s_addr; 8810402Ssam #endif 896505Ssam 907155Swnj /* 9110402Ssam * Look for broadcast address and 9210402Ssam * and verify user is allowed to send 9310146Ssam * such a packet. 947155Swnj */ 9510146Ssam if (in_lnaof(dst) == INADDR_ANY) { 9610146Ssam if ((ifp->if_flags & IFF_BROADCAST) == 0) { 9710146Ssam error = EADDRNOTAVAIL; 9810146Ssam goto bad; 9910146Ssam } 10012417Ssam if ((flags & IP_ALLOWBROADCAST) == 0) { 1017155Swnj error = EACCES; 1026339Ssam goto bad; 1036505Ssam } 10410146Ssam /* don't allow broadcast messages to be fragmented */ 10510146Ssam if (ip->ip_len > ifp->if_mtu) { 10610146Ssam error = EMSGSIZE; 10710146Ssam goto bad; 10810146Ssam } 1096339Ssam } 1106339Ssam 1115085Swnj /* 1124924Swnj * If small enough for interface, can just send directly. 1134545Swnj */ 1145085Swnj if (ip->ip_len <= ifp->if_mtu) { 1155085Swnj ip->ip_len = htons((u_short)ip->ip_len); 1165085Swnj ip->ip_off = htons((u_short)ip->ip_off); 1175085Swnj ip->ip_sum = 0; 1185085Swnj ip->ip_sum = in_cksum(m, hlen); 1197155Swnj error = (*ifp->if_output)(ifp, m, dst); 1207155Swnj goto done; 1214908Swnj } 1224924Swnj 1234924Swnj /* 1244924Swnj * Too large for interface; fragment if possible. 1254924Swnj * Must be able to put at least 8 bytes per fragment. 1264924Swnj */ 1276505Ssam if (ip->ip_off & IP_DF) { 1286505Ssam error = EMSGSIZE; 1294924Swnj goto bad; 1306505Ssam } 1315085Swnj len = (ifp->if_mtu - hlen) &~ 7; 1326505Ssam if (len < 8) { 1336505Ssam error = EMSGSIZE; 1344924Swnj goto bad; 1356505Ssam } 1364924Swnj 1374924Swnj /* 1384924Swnj * Discard IP header from logical mbuf for m_copy's sake. 1394924Swnj * Loop through length of segment, make a copy of each 1404924Swnj * part and output. 1414924Swnj */ 1424924Swnj m->m_len -= sizeof (struct ip); 1434924Swnj m->m_off += sizeof (struct ip); 1445892Sroot for (off = 0; off < ip->ip_len-hlen; off += len) { 14510012Ssam struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER); 1464924Swnj struct ip *mhip; 1474924Swnj 1486505Ssam if (mh == 0) { 1496505Ssam error = ENOBUFS; 1504924Swnj goto bad; 1516505Ssam } 1524924Swnj mh->m_off = MMAXOFF - hlen; 1534924Swnj mhip = mtod(mh, struct ip *); 1544924Swnj *mhip = *ip; 1554952Swnj if (hlen > sizeof (struct ip)) { 1564924Swnj int olen = ip_optcopy(ip, mhip, off); 1574924Swnj mh->m_len = sizeof (struct ip) + olen; 1584924Swnj } else 1594924Swnj mh->m_len = sizeof (struct ip); 1605770Swnj mhip->ip_off = off >> 3; 1615892Sroot if (off + len >= ip->ip_len-hlen) 1625892Sroot len = mhip->ip_len = ip->ip_len - hlen - off; 1634924Swnj else { 1644924Swnj mhip->ip_len = len; 1654924Swnj mhip->ip_off |= IP_MF; 1664496Swnj } 1675770Swnj mhip->ip_len += sizeof (struct ip); 1685770Swnj mhip->ip_len = htons((u_short)mhip->ip_len); 1694924Swnj mh->m_next = m_copy(m, off, len); 1704924Swnj if (mh->m_next == 0) { 1714967Swnj (void) m_free(mh); 1726505Ssam error = ENOBUFS; /* ??? */ 1734924Swnj goto bad; 1744674Swnj } 1755892Sroot mhip->ip_off = htons((u_short)mhip->ip_off); 1765892Sroot mhip->ip_sum = 0; 1775892Sroot mhip->ip_sum = in_cksum(mh, hlen); 1786505Ssam if (error = (*ifp->if_output)(ifp, mh, dst)) 1796505Ssam break; 1804924Swnj } 1817155Swnj m_freem(m); 1827155Swnj goto done; 1837155Swnj 1844924Swnj bad: 1854924Swnj m_freem(m); 1867155Swnj done: 18712417Ssam if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) 1887155Swnj RTFREE(ro->ro_rt); 1896505Ssam return (error); 1904924Swnj } 1914924Swnj 1924924Swnj /* 1934924Swnj * Copy options from ip to jp. 1944952Swnj * If off is 0 all options are copied 1954924Swnj * otherwise copy selectively. 1964924Swnj */ 1974924Swnj ip_optcopy(ip, jp, off) 1984924Swnj struct ip *ip, *jp; 1994924Swnj int off; 2004924Swnj { 2014924Swnj register u_char *cp, *dp; 2024924Swnj int opt, optlen, cnt; 2034924Swnj 2044924Swnj cp = (u_char *)(ip + 1); 2054924Swnj dp = (u_char *)(jp + 1); 2064924Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 2074924Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 2084924Swnj opt = cp[0]; 2094924Swnj if (opt == IPOPT_EOL) 2104924Swnj break; 2114924Swnj if (opt == IPOPT_NOP) 2124924Swnj optlen = 1; 2134924Swnj else 2144924Swnj optlen = cp[1]; 2154924Swnj if (optlen > cnt) /* XXX */ 2164924Swnj optlen = cnt; /* XXX */ 2174924Swnj if (off == 0 || IPOPT_COPIED(opt)) { 2184952Swnj bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 2194924Swnj dp += optlen; 2204674Swnj } 2214545Swnj } 2224924Swnj for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 2234924Swnj *dp++ = IPOPT_EOL; 2244924Swnj return (optlen); 2254496Swnj } 226