1*21770Skarels /* raw_ip.c 6.5 85/06/02 */ 25123Swnj 317062Sbloom #include "param.h" 417062Sbloom #include "mbuf.h" 517062Sbloom #include "socket.h" 617062Sbloom #include "protosw.h" 717062Sbloom #include "socketvar.h" 817062Sbloom #include "errno.h" 910894Ssam 106509Ssam #include "../net/if.h" 1113455Ssam #include "../net/route.h" 1210894Ssam #include "../net/raw_cb.h" 1310894Ssam 1417062Sbloom #include "in.h" 1517062Sbloom #include "in_systm.h" 1617062Sbloom #include "ip.h" 1717062Sbloom #include "ip_var.h" 185123Swnj 195123Swnj /* 205612Swnj * Raw interface to IP protocol. 215123Swnj */ 225612Swnj 2313455Ssam struct sockaddr_in ripdst = { AF_INET }; 2413455Ssam struct sockaddr_in ripsrc = { AF_INET }; 2513455Ssam struct sockproto ripproto = { PF_INET }; 265612Swnj /* 275612Swnj * Setup generic address and protocol structures 285612Swnj * for raw_input routine, then pass them along with 295612Swnj * mbuf chain. 305612Swnj */ 315123Swnj rip_input(m) 325123Swnj struct mbuf *m; 335123Swnj { 345612Swnj register struct ip *ip = mtod(m, struct ip *); 355123Swnj 365612Swnj ripproto.sp_protocol = ip->ip_p; 375646Ssam ripdst.sin_addr = ip->ip_dst; 385646Ssam ripsrc.sin_addr = ip->ip_src; 396529Ssam raw_input(m, &ripproto, (struct sockaddr *)&ripsrc, 406529Ssam (struct sockaddr *)&ripdst); 415123Swnj } 425123Swnj 435612Swnj /* 445612Swnj * Generate IP header and pass packet to ip_output. 455612Swnj * Tack on options user may have setup with control call. 465612Swnj */ 475612Swnj rip_output(m0, so) 485612Swnj struct mbuf *m0; 495612Swnj struct socket *so; 505123Swnj { 515612Swnj register struct mbuf *m; 525612Swnj register struct ip *ip; 536509Ssam int len = 0, error; 546509Ssam struct rawcb *rp = sotorawcb(so); 557156Swnj struct sockaddr_in *sin; 565123Swnj 575612Swnj /* 585612Swnj * Calculate data length and get an mbuf 595612Swnj * for IP header. 605612Swnj */ 615612Swnj for (m = m0; m; m = m->m_next) 625612Swnj len += m->m_len; 639642Ssam m = m_get(M_DONTWAIT, MT_HEADER); 645612Swnj if (m == 0) { 656509Ssam error = ENOBUFS; 666509Ssam goto bad; 675612Swnj } 685612Swnj 695612Swnj /* 705612Swnj * Fill in IP header as needed. 715612Swnj */ 725612Swnj m->m_off = MMAXOFF - sizeof(struct ip); 735612Swnj m->m_len = sizeof(struct ip); 745612Swnj m->m_next = m0; 755612Swnj ip = mtod(m, struct ip *); 7616798Skarels ip->ip_tos = 0; 7715714Skarels ip->ip_off = 0; 78*21770Skarels ip->ip_p = rp->rcb_proto.sp_protocol; 795612Swnj ip->ip_len = sizeof(struct ip) + len; 807156Swnj if (rp->rcb_flags & RAW_LADDR) { 817156Swnj sin = (struct sockaddr_in *)&rp->rcb_laddr; 827156Swnj if (sin->sin_family != AF_INET) { 836509Ssam error = EAFNOSUPPORT; 846509Ssam goto bad; 856509Ssam } 867156Swnj ip->ip_src.s_addr = sin->sin_addr.s_addr; 877156Swnj } else 887156Swnj ip->ip_src.s_addr = 0; 897156Swnj ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr; 905612Swnj ip->ip_ttl = MAXTTL; 9113455Ssam return (ip_output(m, (struct mbuf *)0, &rp->rcb_route, 9212418Ssam IP_ROUTETOIF|IP_ALLOWBROADCAST)); 936509Ssam bad: 946509Ssam m_freem(m); 956509Ssam return (error); 965123Swnj } 97