1*9642Ssam /* raw_ip.c 4.16 82/12/14 */ 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" 98401Swnj #include "../netinet/in.h" 108401Swnj #include "../netinet/in_systm.h" 118401Swnj #include "../netinet/ip.h" 128401Swnj #include "../netinet/ip_var.h" 135612Swnj #include "../net/raw_cb.h" 147156Swnj #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 345612Swnj ripproto.sp_protocol = ip->ip_p; 355646Ssam ripdst.sin_addr = ip->ip_dst; 365646Ssam ripsrc.sin_addr = ip->ip_src; 376529Ssam raw_input(m, &ripproto, (struct sockaddr *)&ripsrc, 386529Ssam (struct sockaddr *)&ripdst); 395123Swnj } 405123Swnj 415612Swnj /* 425612Swnj * Generate IP header and pass packet to ip_output. 435612Swnj * Tack on options user may have setup with control call. 445612Swnj */ 455612Swnj rip_output(m0, so) 465612Swnj struct mbuf *m0; 475612Swnj struct socket *so; 485123Swnj { 495612Swnj register struct mbuf *m; 505612Swnj register struct ip *ip; 516509Ssam int len = 0, error; 526509Ssam struct rawcb *rp = sotorawcb(so); 537156Swnj struct sockaddr_in *sin; 545123Swnj 555612Swnj /* 565612Swnj * Calculate data length and get an mbuf 575612Swnj * for IP header. 585612Swnj */ 595612Swnj for (m = m0; m; m = m->m_next) 605612Swnj len += m->m_len; 61*9642Ssam m = m_get(M_DONTWAIT, MT_HEADER); 625612Swnj if (m == 0) { 636509Ssam error = ENOBUFS; 646509Ssam goto bad; 655612Swnj } 665612Swnj 675612Swnj /* 685612Swnj * Fill in IP header as needed. 695612Swnj */ 705612Swnj m->m_off = MMAXOFF - sizeof(struct ip); 715612Swnj m->m_len = sizeof(struct ip); 725612Swnj m->m_next = m0; 735612Swnj ip = mtod(m, struct ip *); 745612Swnj ip->ip_p = so->so_proto->pr_protocol; 755612Swnj ip->ip_len = sizeof(struct ip) + len; 767156Swnj if (rp->rcb_flags & RAW_LADDR) { 777156Swnj sin = (struct sockaddr_in *)&rp->rcb_laddr; 787156Swnj if (sin->sin_family != AF_INET) { 796509Ssam error = EAFNOSUPPORT; 806509Ssam goto bad; 816509Ssam } 827156Swnj ip->ip_src.s_addr = sin->sin_addr.s_addr; 837156Swnj } else 847156Swnj ip->ip_src.s_addr = 0; 857156Swnj ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr; 865612Swnj ip->ip_ttl = MAXTTL; 877156Swnj return (ip_output(m, (struct mbuf *)0, &routetoif, 1)); 886509Ssam bad: 896509Ssam m_freem(m); 906509Ssam return (error); 915123Swnj } 92