1*23745Skarels /* 2*23745Skarels * Copyright (c) 1982 Regents of the University of California. 3*23745Skarels * All rights reserved. The Berkeley software License Agreement 4*23745Skarels * specifies the terms and conditions for redistribution. 5*23745Skarels * 6*23745Skarels * @(#)ip_output.c 6.8 (Berkeley) 06/24/85 7*23745Skarels */ 84571Swnj 917061Sbloom #include "param.h" 1017061Sbloom #include "mbuf.h" 1117061Sbloom #include "errno.h" 1217061Sbloom #include "socket.h" 1317061Sbloom #include "socketvar.h" 1410893Ssam 1510893Ssam #include "../net/if.h" 1610893Ssam #include "../net/route.h" 1710893Ssam 1817061Sbloom #include "in.h" 1917061Sbloom #include "in_systm.h" 2018375Skarels #include "in_var.h" 2117061Sbloom #include "ip.h" 2217061Sbloom #include "ip_var.h" 234496Swnj 2412460Ssam #ifdef vax 2512460Ssam #include "../vax/mtpr.h" 2612460Ssam #endif 2710893Ssam 2812417Ssam ip_output(m, opt, ro, flags) 294924Swnj struct mbuf *m; 305085Swnj struct mbuf *opt; 316339Ssam struct route *ro; 3212417Ssam int flags; 334496Swnj { 344924Swnj register struct ip *ip = mtod(m, struct ip *); 355085Swnj register struct ifnet *ifp; 366505Ssam int len, hlen = sizeof (struct ip), off, error = 0; 376339Ssam struct route iproute; 3816602Ssam struct sockaddr_in *dst; 394496Swnj 405219Swnj if (opt) /* XXX */ 415242Sroot (void) m_free(opt); /* XXX */ 424924Swnj /* 434924Swnj * Fill in IP header. 444924Swnj */ 4512417Ssam if ((flags & IP_FORWARDING) == 0) { 4612417Ssam ip->ip_v = IPVERSION; 4712417Ssam ip->ip_off &= IP_DF; 4812417Ssam ip->ip_id = htons(ip_id++); 4916545Skarels ip->ip_hl = hlen >> 2; 5018375Skarels } 514496Swnj 524545Swnj /* 537155Swnj * Route packet. 545085Swnj */ 556339Ssam if (ro == 0) { 566339Ssam ro = &iproute; 576339Ssam bzero((caddr_t)ro, sizeof (*ro)); 585085Swnj } 5916602Ssam dst = (struct sockaddr_in *)&ro->ro_dst; 606339Ssam if (ro->ro_rt == 0) { 6116602Ssam dst->sin_family = AF_INET; 6216602Ssam dst->sin_addr = ip->ip_dst; 637155Swnj /* 6412417Ssam * If routing to interface only, 6512417Ssam * short circuit routing lookup. 667155Swnj */ 6712417Ssam if (flags & IP_ROUTETOIF) { 6818375Skarels struct in_ifaddr *ia; 6918375Skarels ia = in_iaonnetof(in_netof(ip->ip_dst)); 7018375Skarels if (ia == 0) { 7112417Ssam error = ENETUNREACH; 7212417Ssam goto bad; 7312417Ssam } 7418375Skarels ifp = ia->ia_ifp; 757155Swnj goto gotif; 767155Swnj } 776368Ssam rtalloc(ro); 7816545Skarels } else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) { 7915716Skarels /* 8015716Skarels * The old route has gone away; try for a new one. 8115716Skarels */ 8215716Skarels rtfree(ro->ro_rt); 8318375Skarels ro->ro_rt = NULL; 8415716Skarels rtalloc(ro); 856339Ssam } 8612417Ssam if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) { 8712417Ssam error = ENETUNREACH; 8812417Ssam goto bad; 8912417Ssam } 907155Swnj ro->ro_rt->rt_use++; 9115276Ssam if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST)) 9216602Ssam dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway; 937155Swnj gotif: 94*23745Skarels #ifndef notdef 957155Swnj /* 96*23745Skarels * If source address not specified yet, use address 97*23745Skarels * of outgoing interface. 98*23745Skarels */ 99*23745Skarels if (ip->ip_src.s_addr == INADDR_ANY) { 100*23745Skarels register struct in_ifaddr *ia; 101*23745Skarels 102*23745Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) 103*23745Skarels if (ia->ia_ifp == ifp) { 104*23745Skarels ip->ip_src = IA_SIN(ia)->sin_addr; 105*23745Skarels break; 106*23745Skarels } 107*23745Skarels } 108*23745Skarels #endif 109*23745Skarels /* 11010402Ssam * Look for broadcast address and 11110402Ssam * and verify user is allowed to send 11210146Ssam * such a packet. 1137155Swnj */ 11418375Skarels if (in_broadcast(dst->sin_addr)) { 11510146Ssam if ((ifp->if_flags & IFF_BROADCAST) == 0) { 11610146Ssam error = EADDRNOTAVAIL; 11710146Ssam goto bad; 11810146Ssam } 11912417Ssam if ((flags & IP_ALLOWBROADCAST) == 0) { 1207155Swnj error = EACCES; 1216339Ssam goto bad; 1226505Ssam } 12310146Ssam /* don't allow broadcast messages to be fragmented */ 12410146Ssam if (ip->ip_len > ifp->if_mtu) { 12510146Ssam error = EMSGSIZE; 12610146Ssam goto bad; 12710146Ssam } 1286339Ssam } 1296339Ssam 1305085Swnj /* 1314924Swnj * If small enough for interface, can just send directly. 1324545Swnj */ 1335085Swnj if (ip->ip_len <= ifp->if_mtu) { 1345085Swnj ip->ip_len = htons((u_short)ip->ip_len); 1355085Swnj ip->ip_off = htons((u_short)ip->ip_off); 1365085Swnj ip->ip_sum = 0; 1375085Swnj ip->ip_sum = in_cksum(m, hlen); 13816602Ssam error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst); 1397155Swnj goto done; 1404908Swnj } 1414924Swnj 1424924Swnj /* 1434924Swnj * Too large for interface; fragment if possible. 1444924Swnj * Must be able to put at least 8 bytes per fragment. 1454924Swnj */ 1466505Ssam if (ip->ip_off & IP_DF) { 1476505Ssam error = EMSGSIZE; 1484924Swnj goto bad; 1496505Ssam } 1505085Swnj len = (ifp->if_mtu - hlen) &~ 7; 1516505Ssam if (len < 8) { 1526505Ssam error = EMSGSIZE; 1534924Swnj goto bad; 1546505Ssam } 1554924Swnj 1564924Swnj /* 1574924Swnj * Discard IP header from logical mbuf for m_copy's sake. 1584924Swnj * Loop through length of segment, make a copy of each 1594924Swnj * part and output. 1604924Swnj */ 1614924Swnj m->m_len -= sizeof (struct ip); 1624924Swnj m->m_off += sizeof (struct ip); 1635892Sroot for (off = 0; off < ip->ip_len-hlen; off += len) { 16410012Ssam struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER); 1654924Swnj struct ip *mhip; 1664924Swnj 1676505Ssam if (mh == 0) { 1686505Ssam error = ENOBUFS; 1694924Swnj goto bad; 1706505Ssam } 1714924Swnj mh->m_off = MMAXOFF - hlen; 1724924Swnj mhip = mtod(mh, struct ip *); 1734924Swnj *mhip = *ip; 1744952Swnj if (hlen > sizeof (struct ip)) { 1754924Swnj int olen = ip_optcopy(ip, mhip, off); 1764924Swnj mh->m_len = sizeof (struct ip) + olen; 1774924Swnj } else 1784924Swnj mh->m_len = sizeof (struct ip); 1795770Swnj mhip->ip_off = off >> 3; 18016545Skarels if (ip->ip_off & IP_MF) 18116545Skarels mhip->ip_off |= IP_MF; 1825892Sroot if (off + len >= ip->ip_len-hlen) 1835892Sroot len = mhip->ip_len = ip->ip_len - hlen - off; 1844924Swnj else { 1854924Swnj mhip->ip_len = len; 1864924Swnj mhip->ip_off |= IP_MF; 1874496Swnj } 1885770Swnj mhip->ip_len += sizeof (struct ip); 1895770Swnj mhip->ip_len = htons((u_short)mhip->ip_len); 1904924Swnj mh->m_next = m_copy(m, off, len); 1914924Swnj if (mh->m_next == 0) { 1924967Swnj (void) m_free(mh); 1936505Ssam error = ENOBUFS; /* ??? */ 1944924Swnj goto bad; 1954674Swnj } 1965892Sroot mhip->ip_off = htons((u_short)mhip->ip_off); 1975892Sroot mhip->ip_sum = 0; 1985892Sroot mhip->ip_sum = in_cksum(mh, hlen); 19916602Ssam if (error = (*ifp->if_output)(ifp, mh, (struct sockaddr *)dst)) 2006505Ssam break; 2014924Swnj } 2024924Swnj bad: 2034924Swnj m_freem(m); 2047155Swnj done: 20512417Ssam if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) 2067155Swnj RTFREE(ro->ro_rt); 2076505Ssam return (error); 2084924Swnj } 2094924Swnj 2104924Swnj /* 2114924Swnj * Copy options from ip to jp. 2124952Swnj * If off is 0 all options are copied 2134924Swnj * otherwise copy selectively. 2144924Swnj */ 2154924Swnj ip_optcopy(ip, jp, off) 2164924Swnj struct ip *ip, *jp; 2174924Swnj int off; 2184924Swnj { 2194924Swnj register u_char *cp, *dp; 2204924Swnj int opt, optlen, cnt; 2214924Swnj 2224924Swnj cp = (u_char *)(ip + 1); 2234924Swnj dp = (u_char *)(jp + 1); 2244924Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 2254924Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 2264924Swnj opt = cp[0]; 2274924Swnj if (opt == IPOPT_EOL) 2284924Swnj break; 2294924Swnj if (opt == IPOPT_NOP) 2304924Swnj optlen = 1; 2314924Swnj else 2324924Swnj optlen = cp[1]; 2334924Swnj if (optlen > cnt) /* XXX */ 2344924Swnj optlen = cnt; /* XXX */ 2354924Swnj if (off == 0 || IPOPT_COPIED(opt)) { 2364952Swnj bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 2374924Swnj dp += optlen; 2384674Swnj } 2394545Swnj } 2404924Swnj for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 2414924Swnj *dp++ = IPOPT_EOL; 2424924Swnj return (optlen); 2434496Swnj } 244