xref: /csrg-svn/sys/netinet/raw_ip.c (revision 8401)
1*8401Swnj /*	raw_ip.c	4.14	82/10/09	*/
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"
9*8401Swnj #include "../netinet/in.h"
10*8401Swnj #include "../netinet/in_systm.h"
11*8401Swnj #include "../netinet/ip.h"
12*8401Swnj #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);
536509Ssam 	struct ifnet *ifp;
547156Swnj 	struct sockaddr_in *sin;
555123Swnj 
565612Swnj 	/*
575612Swnj 	 * Calculate data length and get an mbuf
585612Swnj 	 * for IP header.
595612Swnj 	 */
605612Swnj 	for (m = m0; m; m = m->m_next)
615612Swnj 		len += m->m_len;
625612Swnj 	m = m_get(M_DONTWAIT);
635612Swnj 	if (m == 0) {
646509Ssam 		error = ENOBUFS;
656509Ssam 		goto bad;
665612Swnj 	}
675612Swnj 
685612Swnj 	/*
695612Swnj 	 * Fill in IP header as needed.
705612Swnj 	 */
715612Swnj 	m->m_off = MMAXOFF - sizeof(struct ip);
725612Swnj 	m->m_len = sizeof(struct ip);
735612Swnj 	m->m_next = m0;
745612Swnj 	ip = mtod(m, struct ip *);
755612Swnj 	ip->ip_p = so->so_proto->pr_protocol;
765612Swnj 	ip->ip_len = sizeof(struct ip) + len;
777156Swnj 	if (rp->rcb_flags & RAW_LADDR) {
787156Swnj 		sin = (struct sockaddr_in *)&rp->rcb_laddr;
797156Swnj 		if (sin->sin_family != AF_INET) {
806509Ssam 			error = EAFNOSUPPORT;
816509Ssam 			goto bad;
826509Ssam 		}
837156Swnj 		ip->ip_src.s_addr = sin->sin_addr.s_addr;
847156Swnj 	} else
857156Swnj 		ip->ip_src.s_addr = 0;
867156Swnj 	ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr;
875612Swnj 	ip->ip_ttl = MAXTTL;
887156Swnj 	return (ip_output(m, (struct mbuf *)0, &routetoif, 1));
896509Ssam bad:
906509Ssam 	m_freem(m);
916509Ssam 	return (error);
925123Swnj }
93