xref: /csrg-svn/sys/netinet/raw_ip.c (revision 15714)
1*15714Skarels /*	raw_ip.c	6.2	83/12/15	*/
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"
1113455Ssam #include "../net/route.h"
1210894Ssam #include "../net/raw_cb.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 
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 *);
76*15714Skarels 	ip->ip_off = 0;
775612Swnj 	ip->ip_p = so->so_proto->pr_protocol;
785612Swnj 	ip->ip_len = sizeof(struct ip) + len;
797156Swnj 	if (rp->rcb_flags & RAW_LADDR) {
807156Swnj 		sin = (struct sockaddr_in *)&rp->rcb_laddr;
817156Swnj 		if (sin->sin_family != AF_INET) {
826509Ssam 			error = EAFNOSUPPORT;
836509Ssam 			goto bad;
846509Ssam 		}
857156Swnj 		ip->ip_src.s_addr = sin->sin_addr.s_addr;
867156Swnj 	} else
877156Swnj 		ip->ip_src.s_addr = 0;
887156Swnj 	ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr;
895612Swnj 	ip->ip_ttl = MAXTTL;
9013455Ssam 	return (ip_output(m, (struct mbuf *)0, &rp->rcb_route,
9112418Ssam 	   IP_ROUTETOIF|IP_ALLOWBROADCAST));
926509Ssam bad:
936509Ssam 	m_freem(m);
946509Ssam 	return (error);
955123Swnj }
96