1*18414Skarels /*	raw_imp.c	6.4	85/03/19	*/
25637Sroot 
317069Sbloom #include "param.h"
417069Sbloom #include "mbuf.h"
517069Sbloom #include "socket.h"
617069Sbloom #include "protosw.h"
717069Sbloom #include "socketvar.h"
817069Sbloom #include "errno.h"
910900Ssam 
1010900Ssam #include "../net/if.h"
1113453Ssam #include "../net/route.h"
1210900Ssam #include "../net/raw_cb.h"
1310900Ssam 
148401Swnj #include "../netinet/in.h"
158401Swnj #include "../netinet/in_systm.h"
16*18414Skarels #include "../netinet/in_var.h"
1717069Sbloom #include "if_imp.h"
185637Sroot 
195637Sroot /*
205637Sroot  * Raw interface to IMP.
215637Sroot  */
225637Sroot 
235637Sroot /*
245637Sroot  * Generate IMP leader and pass packet to impoutput.
255637Sroot  * The user must create a skeletal leader in order to
265637Sroot  * communicate message type, message subtype, etc.
275637Sroot  * We fill in holes where needed and verify parameters
285637Sroot  * supplied by user.
295637Sroot  */
305847Sroot rimp_output(m, so)
315637Sroot 	register struct mbuf *m;
325637Sroot 	struct socket *so;
335637Sroot {
345637Sroot 	struct mbuf *n;
356509Ssam 	int len, error = 0;
365647Ssam 	register struct imp_leader *ip;
375637Sroot 	register struct sockaddr_in *sin;
385637Sroot 	register struct rawcb *rp = sotorawcb(so);
39*18414Skarels 	struct in_ifaddr *ia;
405772Swnj 	struct control_leader *cp;
415637Sroot 
425637Sroot 	/*
435637Sroot 	 * Verify user has supplied necessary space
445637Sroot 	 * for the leader and check parameters in it.
455637Sroot 	 */
465772Swnj 	if ((m->m_off > MMAXOFF || m->m_len < sizeof(struct control_leader)) &&
476509Ssam 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0) {
486509Ssam 		error = EMSGSIZE;	/* XXX */
496509Ssam 		goto bad;
506509Ssam 	}
515772Swnj 	cp = mtod(m, struct control_leader *);
525772Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
535772Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
546509Ssam 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0) {
556509Ssam 			error = EMSGSIZE;	/* XXX */
566509Ssam 			goto bad;
576509Ssam 		}
585647Ssam 	ip = mtod(m, struct imp_leader *);
596509Ssam 	if (ip->il_format != IMP_NFF) {
606509Ssam 		error = EMSGSIZE;		/* XXX */
615637Sroot 		goto bad;
626509Ssam 	}
635870Sroot #ifdef notdef
645647Ssam 	if (ip->il_link != IMPLINK_IP &&
656509Ssam 	    (ip->il_link<IMPLINK_LOWEXPER || ip->il_link>IMPLINK_HIGHEXPER)) {
666509Ssam 		error = EPERM;
675637Sroot 		goto bad;
686509Ssam 	}
695870Sroot #endif
705637Sroot 
715637Sroot 	/*
725637Sroot 	 * Fill in IMP leader -- impoutput refrains from rebuilding
735637Sroot 	 * the leader when it sees the protocol family PF_IMPLINK.
745637Sroot 	 * (message size calculated by walking through mbuf's)
755637Sroot 	 */
765637Sroot 	for (len = 0, n = m; n; n = n->m_next)
775637Sroot 		len += n->m_len;
786161Ssam 	ip->il_length = htons((u_short)(len << 3));
796509Ssam 	sin = (struct sockaddr_in *)&rp->rcb_faddr;
80*18414Skarels 	imp_addr_to_leader(ip, sin->sin_addr.s_addr);	/* BRL */
816339Ssam 	/* no routing here */
82*18414Skarels 	ia = in_iaonnetof(in_netof(sin->sin_addr));
83*18414Skarels 	if (ia)
84*18414Skarels 		return (impoutput(ia->ia_ifp, m, (struct sockaddr *)sin));
856509Ssam 	error = ENETUNREACH;
865637Sroot bad:
875637Sroot 	m_freem(m);
886509Ssam 	return (error);
895637Sroot }
90