xref: /csrg-svn/sys/netinet/raw_ip.c (revision 6509)
1*6509Ssam /*	raw_ip.c	4.10	82/04/10	*/
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"
8*6509Ssam #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"
14*6509Ssam #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;
376161Ssam 	raw_input(m, &ripproto, (struct sockaddr *)&ripdst,
386161Ssam 	  (struct sockaddr *)&ripsrc);
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;
51*6509Ssam 	int len = 0, error;
52*6509Ssam 	struct rawcb *rp = sotorawcb(so);
53*6509Ssam 	struct ifnet *ifp;
54*6509Ssam 	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) {
65*6509Ssam 		error = ENOBUFS;
66*6509Ssam 		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;
78*6509Ssam 	ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr;
79*6509Ssam 	/* local address may not be specified -- XXX */
80*6509Ssam 	ifp = if_ifonnetof(ip->ip_dst.s_net);
81*6509Ssam 	if (ifp == 0) {
82*6509Ssam 		error = ENETUNREACH;
83*6509Ssam 		goto bad;
84*6509Ssam 	}
85*6509Ssam 	if (rp->rcb_flags & RAW_LADDR)
86*6509Ssam 		src = (struct sockaddr_in *)&rp->rcb_laddr;
87*6509Ssam 	else {
88*6509Ssam 		if (ifp->if_addr.sa_family != AF_INET) {
89*6509Ssam 			error = EAFNOSUPPORT;
90*6509Ssam 			goto bad;
91*6509Ssam 		}
92*6509Ssam 		src = (struct sockaddr_in *)&ifp->if_addr;
93*6509Ssam 	}
94*6509Ssam 	ip->ip_src = src->sin_addr;
955612Swnj 	ip->ip_ttl = MAXTTL;
966339Ssam 	return (ip_output(m, (struct mbuf *)0, 0, 1));
97*6509Ssam bad:
98*6509Ssam 	m_freem(m);
99*6509Ssam 	return (error);
1005123Swnj }
101