xref: /csrg-svn/sys/netinet/raw_ip.c (revision 6529)
1*6529Ssam /*	raw_ip.c	4.11	82/04/11	*/
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"
146509Ssam #include <errno.h>
155123Swnj 
165123Swnj /*
175612Swnj  * Raw interface to IP protocol.
185123Swnj  */
195612Swnj 
206339Ssam static struct sockaddr_in ripdst = { AF_INET };
216339Ssam static struct sockaddr_in ripsrc = { AF_INET };
226339Ssam static struct sockproto ripproto = { PF_INET };
235612Swnj /*
245612Swnj  * Setup generic address and protocol structures
255612Swnj  * for raw_input routine, then pass them along with
265612Swnj  * mbuf chain.
275612Swnj  */
285123Swnj rip_input(m)
295123Swnj 	struct mbuf *m;
305123Swnj {
315612Swnj 	register struct ip *ip = mtod(m, struct ip *);
325123Swnj 
335123Swnj COUNT(RIP_INPUT);
345612Swnj 	ripproto.sp_protocol = ip->ip_p;
355646Ssam 	ripdst.sin_addr = ip->ip_dst;
365646Ssam 	ripsrc.sin_addr = ip->ip_src;
37*6529Ssam 	raw_input(m, &ripproto, (struct sockaddr *)&ripsrc,
38*6529Ssam 	  (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;
546509Ssam 	struct sockaddr_in *src;
555123Swnj 
565123Swnj COUNT(RIP_OUTPUT);
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;
635612Swnj 	m = m_get(M_DONTWAIT);
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;
786509Ssam 	ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr;
796509Ssam 	/* local address may not be specified -- XXX */
806509Ssam 	ifp = if_ifonnetof(ip->ip_dst.s_net);
816509Ssam 	if (ifp == 0) {
826509Ssam 		error = ENETUNREACH;
836509Ssam 		goto bad;
846509Ssam 	}
856509Ssam 	if (rp->rcb_flags & RAW_LADDR)
866509Ssam 		src = (struct sockaddr_in *)&rp->rcb_laddr;
876509Ssam 	else {
886509Ssam 		if (ifp->if_addr.sa_family != AF_INET) {
896509Ssam 			error = EAFNOSUPPORT;
906509Ssam 			goto bad;
916509Ssam 		}
926509Ssam 		src = (struct sockaddr_in *)&ifp->if_addr;
936509Ssam 	}
946509Ssam 	ip->ip_src = src->sin_addr;
955612Swnj 	ip->ip_ttl = MAXTTL;
966339Ssam 	return (ip_output(m, (struct mbuf *)0, 0, 1));
976509Ssam bad:
986509Ssam 	m_freem(m);
996509Ssam 	return (error);
1005123Swnj }
101