1 /*	raw_pup.c	4.12	82/06/20	*/
2 
3 #include "../h/param.h"
4 #include "../h/mbuf.h"
5 #include "../h/socket.h"
6 #include "../h/protosw.h"
7 #include "../h/socketvar.h"
8 #include "../net/in.h"
9 #include "../net/in_systm.h"
10 #include "../net/pup.h"
11 #include "../net/raw_cb.h"
12 #include "../net/if.h"
13 #include "../errno.h"
14 
15 /*
16  * Raw PUP protocol interface.
17  */
18 
19 /*
20  * Encapsulate packet in PUP header which is supplied by the
21  * user.  This is done to allow user to specify PUP identifier.
22  */
23 rpup_output(m, so)
24 	register struct mbuf *m;
25 	struct socket *so;
26 {
27 	register struct rawcb *rp = sotorawcb(so);
28 	register struct pup_header *pup;
29 	int len, error = 0;
30 	struct mbuf *n;
31 	struct sockaddr_pup *dst;
32 	struct ifnet *ifp;
33 
34 	/*
35 	 * Verify user has supplied necessary space
36 	 * for the header and check parameters in it.
37 	 */
38 	if ((m->m_off > MMAXOFF || m->m_len < sizeof(struct pup_header)) &&
39 	    (m = m_pullup(m, sizeof(struct pup_header))) == 0) {
40 		error = EMSGSIZE;	/* XXX */
41 		goto bad;
42 	}
43 	pup = mtod(m, struct pup_header *);
44 	if (pup->pup_type == 0) {
45 		error = EPERM;		/* XXX */
46 		goto bad;
47 	}
48 	if (pup->pup_tcontrol && (pup->pup_tcontrol & ~PUP_TRACE)) {
49 		error = EPERM;		/* XXX */
50 		goto bad;
51 	}
52 	for (len = 0, n = m; n; n = n->m_next)
53 		len += n->m_len;
54 	pup->pup_length = len;
55 #if vax || pdp11
56 	pup->pup_length = htons(pup->pup_length);
57 #endif
58 	/* assume user generates PUP checksum. */
59 	dst = (struct sockaddr_pup *)&rp->rcb_faddr;
60 	pup->pup_dport = dst->spup_addr;
61 	ifp = if_ifonnetof(pup->pup_dnet);
62 	if (ifp) {
63 		if (rp->rcb_flags & RAW_LADDR) {
64 			register struct sockaddr_pup *src;
65 
66 			src = (struct sockaddr_pup *)&rp->rcb_laddr;
67 			pup->pup_sport = src->spup_addr;
68 		} else {
69 			pup->pup_snet = ifp->if_net;
70 			pup->pup_shost = ifp->if_host[0];
71 			/* socket is specified by user */
72 		}
73 		return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst));
74 	}
75 	error = ENETUNREACH;
76 bad:
77 	m_freem(m);
78 	return (error);
79 }
80