1*10893Ssam /* ip_output.c 1.46 83/02/10 */ 24571Swnj 34496Swnj #include "../h/param.h" 44662Swnj #include "../h/mbuf.h" 5*10893Ssam #include "../h/errno.h" 64662Swnj #include "../h/socket.h" 74924Swnj #include "../h/socketvar.h" 8*10893Ssam 9*10893Ssam #include "../net/if.h" 10*10893Ssam #include "../net/route.h" 11*10893Ssam 128400Swnj #include "../netinet/in.h" 138400Swnj #include "../netinet/in_systm.h" 148400Swnj #include "../netinet/ip.h" 158400Swnj #include "../netinet/ip_var.h" 164496Swnj 17*10893Ssam #include "../machine/mtpr.h" 18*10893Ssam 197155Swnj int ipnorouteprint = 0; 207155Swnj 216339Ssam ip_output(m, opt, ro, allowbroadcast) 224924Swnj struct mbuf *m; 235085Swnj struct mbuf *opt; 246339Ssam struct route *ro; 256211Swnj int allowbroadcast; 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 */ 384924Swnj ip->ip_v = IPVERSION; 394924Swnj ip->ip_hl = hlen >> 2; 404924Swnj ip->ip_off &= IP_DF; 415085Swnj ip->ip_id = htons(ip_id++); 424496Swnj 434545Swnj /* 447155Swnj * Route packet. 455085Swnj */ 466339Ssam if (ro == 0) { 476339Ssam ro = &iproute; 486339Ssam bzero((caddr_t)ro, sizeof (*ro)); 495085Swnj } 507155Swnj dst = &ro->ro_dst; 516339Ssam if (ro->ro_rt == 0) { 526368Ssam ro->ro_dst.sa_family = AF_INET; 536368Ssam ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst; 547155Swnj /* 557155Swnj * If routing to interface only, short circuit routing lookup. 567155Swnj */ 577155Swnj if (ro == &routetoif) { 587155Swnj /* check ifp is AF_INET??? */ 598638Sroot ifp = if_ifonnetof(in_netof(ip->ip_dst)); 607155Swnj if (ifp == 0) 617155Swnj goto unreachable; 627155Swnj goto gotif; 637155Swnj } 646368Ssam rtalloc(ro); 656339Ssam } 667155Swnj if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) 677155Swnj goto unreachable; 687155Swnj ro->ro_rt->rt_use++; 697155Swnj if (ro->ro_rt->rt_flags & RTF_GATEWAY) 707155Swnj dst = &ro->ro_rt->rt_gateway; 717155Swnj gotif: 7210402Ssam #ifndef notdef 737155Swnj /* 747155Swnj * If source address not specified yet, use address 757155Swnj * of outgoing interface. 767155Swnj */ 7710402Ssam if (in_lnaof(ip->ip_src) == INADDR_ANY) 787155Swnj ip->ip_src.s_addr = 797155Swnj ((struct sockaddr_in *)&ifp->if_addr)->sin_addr.s_addr; 8010402Ssam #endif 816505Ssam 827155Swnj /* 8310402Ssam * Look for broadcast address and 8410402Ssam * and verify user is allowed to send 8510146Ssam * such a packet. 867155Swnj */ 8710146Ssam if (in_lnaof(dst) == INADDR_ANY) { 8810146Ssam if ((ifp->if_flags & IFF_BROADCAST) == 0) { 8910146Ssam error = EADDRNOTAVAIL; 9010146Ssam goto bad; 9110146Ssam } 9210146Ssam if (!allowbroadcast) { 937155Swnj error = EACCES; 946339Ssam goto bad; 956505Ssam } 9610146Ssam /* don't allow broadcast messages to be fragmented */ 9710146Ssam if (ip->ip_len > ifp->if_mtu) { 9810146Ssam error = EMSGSIZE; 9910146Ssam goto bad; 10010146Ssam } 1016339Ssam } 1026339Ssam 1035085Swnj /* 1044924Swnj * If small enough for interface, can just send directly. 1054545Swnj */ 1065085Swnj if (ip->ip_len <= ifp->if_mtu) { 1075085Swnj ip->ip_len = htons((u_short)ip->ip_len); 1085085Swnj ip->ip_off = htons((u_short)ip->ip_off); 1095085Swnj ip->ip_sum = 0; 1105085Swnj ip->ip_sum = in_cksum(m, hlen); 1117155Swnj error = (*ifp->if_output)(ifp, m, dst); 1127155Swnj goto done; 1134908Swnj } 1144924Swnj 1154924Swnj /* 1164924Swnj * Too large for interface; fragment if possible. 1174924Swnj * Must be able to put at least 8 bytes per fragment. 1184924Swnj */ 1196505Ssam if (ip->ip_off & IP_DF) { 1206505Ssam error = EMSGSIZE; 1214924Swnj goto bad; 1226505Ssam } 1235085Swnj len = (ifp->if_mtu - hlen) &~ 7; 1246505Ssam if (len < 8) { 1256505Ssam error = EMSGSIZE; 1264924Swnj goto bad; 1276505Ssam } 1284924Swnj 1294924Swnj /* 1304924Swnj * Discard IP header from logical mbuf for m_copy's sake. 1314924Swnj * Loop through length of segment, make a copy of each 1324924Swnj * part and output. 1334924Swnj */ 1344924Swnj m->m_len -= sizeof (struct ip); 1354924Swnj m->m_off += sizeof (struct ip); 1365892Sroot for (off = 0; off < ip->ip_len-hlen; off += len) { 13710012Ssam struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER); 1384924Swnj struct ip *mhip; 1394924Swnj 1406505Ssam if (mh == 0) { 1416505Ssam error = ENOBUFS; 1424924Swnj goto bad; 1436505Ssam } 1444924Swnj mh->m_off = MMAXOFF - hlen; 1454924Swnj mhip = mtod(mh, struct ip *); 1464924Swnj *mhip = *ip; 1474952Swnj if (hlen > sizeof (struct ip)) { 1484924Swnj int olen = ip_optcopy(ip, mhip, off); 1494924Swnj mh->m_len = sizeof (struct ip) + olen; 1504924Swnj } else 1514924Swnj mh->m_len = sizeof (struct ip); 1525770Swnj mhip->ip_off = off >> 3; 1535892Sroot if (off + len >= ip->ip_len-hlen) 1545892Sroot len = mhip->ip_len = ip->ip_len - hlen - off; 1554924Swnj else { 1564924Swnj mhip->ip_len = len; 1574924Swnj mhip->ip_off |= IP_MF; 1584496Swnj } 1595770Swnj mhip->ip_len += sizeof (struct ip); 1605770Swnj mhip->ip_len = htons((u_short)mhip->ip_len); 1614924Swnj mh->m_next = m_copy(m, off, len); 1624924Swnj if (mh->m_next == 0) { 1634967Swnj (void) m_free(mh); 1646505Ssam error = ENOBUFS; /* ??? */ 1654924Swnj goto bad; 1664674Swnj } 1675892Sroot mhip->ip_off = htons((u_short)mhip->ip_off); 1685892Sroot mhip->ip_sum = 0; 1695892Sroot mhip->ip_sum = in_cksum(mh, hlen); 1706505Ssam if (error = (*ifp->if_output)(ifp, mh, dst)) 1716505Ssam break; 1724924Swnj } 1737155Swnj m_freem(m); 1747155Swnj goto done; 1757155Swnj 1767155Swnj unreachable: 1777155Swnj if (ipnorouteprint) 1787155Swnj printf("no route to %x (from %x, len %d)\n", 1797155Swnj ip->ip_dst.s_addr, ip->ip_src.s_addr, ip->ip_len); 1807155Swnj error = ENETUNREACH; 1814924Swnj bad: 1824924Swnj m_freem(m); 1837155Swnj done: 1847155Swnj if (ro == &iproute && ro->ro_rt) 1857155Swnj RTFREE(ro->ro_rt); 1866505Ssam return (error); 1874924Swnj } 1884924Swnj 1894924Swnj /* 1904924Swnj * Copy options from ip to jp. 1914952Swnj * If off is 0 all options are copied 1924924Swnj * otherwise copy selectively. 1934924Swnj */ 1944924Swnj ip_optcopy(ip, jp, off) 1954924Swnj struct ip *ip, *jp; 1964924Swnj int off; 1974924Swnj { 1984924Swnj register u_char *cp, *dp; 1994924Swnj int opt, optlen, cnt; 2004924Swnj 2014924Swnj cp = (u_char *)(ip + 1); 2024924Swnj dp = (u_char *)(jp + 1); 2034924Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 2044924Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 2054924Swnj opt = cp[0]; 2064924Swnj if (opt == IPOPT_EOL) 2074924Swnj break; 2084924Swnj if (opt == IPOPT_NOP) 2094924Swnj optlen = 1; 2104924Swnj else 2114924Swnj optlen = cp[1]; 2124924Swnj if (optlen > cnt) /* XXX */ 2134924Swnj optlen = cnt; /* XXX */ 2144924Swnj if (off == 0 || IPOPT_COPIED(opt)) { 2154952Swnj bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 2164924Swnj dp += optlen; 2174674Swnj } 2184545Swnj } 2194924Swnj for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 2204924Swnj *dp++ = IPOPT_EOL; 2214924Swnj return (optlen); 2224496Swnj } 223