1*12418Ssam /* raw_ip.c 4.18 83/05/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" 810894Ssam #include "../h/errno.h" 910894Ssam 106509Ssam #include "../net/if.h" 1110894Ssam #include "../net/raw_cb.h" 1210894Ssam #include "../net/route.h" 1310894Ssam 148401Swnj #include "../netinet/in.h" 158401Swnj #include "../netinet/in_systm.h" 168401Swnj #include "../netinet/ip.h" 178401Swnj #include "../netinet/ip_var.h" 185123Swnj 195123Swnj /* 205612Swnj * Raw interface to IP protocol. 215123Swnj */ 225612Swnj 236339Ssam static struct sockaddr_in ripdst = { AF_INET }; 246339Ssam static struct sockaddr_in ripsrc = { AF_INET }; 256339Ssam static 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 *); 765612Swnj ip->ip_p = so->so_proto->pr_protocol; 775612Swnj ip->ip_len = sizeof(struct ip) + len; 787156Swnj if (rp->rcb_flags & RAW_LADDR) { 797156Swnj sin = (struct sockaddr_in *)&rp->rcb_laddr; 807156Swnj if (sin->sin_family != AF_INET) { 816509Ssam error = EAFNOSUPPORT; 826509Ssam goto bad; 836509Ssam } 847156Swnj ip->ip_src.s_addr = sin->sin_addr.s_addr; 857156Swnj } else 867156Swnj ip->ip_src.s_addr = 0; 877156Swnj ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr; 885612Swnj ip->ip_ttl = MAXTTL; 89*12418Ssam return (ip_output(m, (struct mbuf *)0, (struct route *)0, 90*12418Ssam IP_ROUTETOIF|IP_ALLOWBROADCAST)); 916509Ssam bad: 926509Ssam m_freem(m); 936509Ssam return (error); 945123Swnj } 95