1*7156Swnj /* raw_ip.c 4.12 82/06/12 */ 25123Swnj 35123Swnj #include "../h/param.h" 45123Swnj #include "../h/mbuf.h" 55123Swnj #include "../h/socket.h" 65612Swnj #include "../h/protosw.h" 75123Swnj #include "../h/socketvar.h" 86509Ssam #include "../net/if.h" 95123Swnj #include "../net/in.h" 105123Swnj #include "../net/in_systm.h" 115612Swnj #include "../net/ip.h" 125612Swnj #include "../net/ip_var.h" 135612Swnj #include "../net/raw_cb.h" 14*7156Swnj #include "../net/route.h" 156509Ssam #include <errno.h> 165123Swnj 175123Swnj /* 185612Swnj * Raw interface to IP protocol. 195123Swnj */ 205612Swnj 216339Ssam static struct sockaddr_in ripdst = { AF_INET }; 226339Ssam static struct sockaddr_in ripsrc = { AF_INET }; 236339Ssam static struct sockproto ripproto = { PF_INET }; 245612Swnj /* 255612Swnj * Setup generic address and protocol structures 265612Swnj * for raw_input routine, then pass them along with 275612Swnj * mbuf chain. 285612Swnj */ 295123Swnj rip_input(m) 305123Swnj struct mbuf *m; 315123Swnj { 325612Swnj register struct ip *ip = mtod(m, struct ip *); 335123Swnj 345123Swnj COUNT(RIP_INPUT); 355612Swnj ripproto.sp_protocol = ip->ip_p; 365646Ssam ripdst.sin_addr = ip->ip_dst; 375646Ssam ripsrc.sin_addr = ip->ip_src; 386529Ssam raw_input(m, &ripproto, (struct sockaddr *)&ripsrc, 396529Ssam (struct sockaddr *)&ripdst); 405123Swnj } 415123Swnj 425612Swnj /* 435612Swnj * Generate IP header and pass packet to ip_output. 445612Swnj * Tack on options user may have setup with control call. 455612Swnj */ 465612Swnj rip_output(m0, so) 475612Swnj struct mbuf *m0; 485612Swnj struct socket *so; 495123Swnj { 505612Swnj register struct mbuf *m; 515612Swnj register struct ip *ip; 526509Ssam int len = 0, error; 536509Ssam struct rawcb *rp = sotorawcb(so); 546509Ssam struct ifnet *ifp; 55*7156Swnj struct sockaddr_in *sin; 565123Swnj 575123Swnj COUNT(RIP_OUTPUT); 585612Swnj /* 595612Swnj * Calculate data length and get an mbuf 605612Swnj * for IP header. 615612Swnj */ 625612Swnj for (m = m0; m; m = m->m_next) 635612Swnj len += m->m_len; 645612Swnj m = m_get(M_DONTWAIT); 655612Swnj if (m == 0) { 666509Ssam error = ENOBUFS; 676509Ssam goto bad; 685612Swnj } 695612Swnj 705612Swnj /* 715612Swnj * Fill in IP header as needed. 725612Swnj */ 735612Swnj m->m_off = MMAXOFF - sizeof(struct ip); 745612Swnj m->m_len = sizeof(struct ip); 755612Swnj m->m_next = m0; 765612Swnj ip = mtod(m, struct ip *); 775612Swnj ip->ip_p = so->so_proto->pr_protocol; 785612Swnj ip->ip_len = sizeof(struct ip) + len; 79*7156Swnj if (rp->rcb_flags & RAW_LADDR) { 80*7156Swnj sin = (struct sockaddr_in *)&rp->rcb_laddr; 81*7156Swnj if (sin->sin_family != AF_INET) { 826509Ssam error = EAFNOSUPPORT; 836509Ssam goto bad; 846509Ssam } 85*7156Swnj ip->ip_src.s_addr = sin->sin_addr.s_addr; 86*7156Swnj } else 87*7156Swnj ip->ip_src.s_addr = 0; 88*7156Swnj ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr; 895612Swnj ip->ip_ttl = MAXTTL; 90*7156Swnj return (ip_output(m, (struct mbuf *)0, &routetoif, 1)); 916509Ssam bad: 926509Ssam m_freem(m); 936509Ssam return (error); 945123Swnj } 95