xref: /csrg-svn/sys/netinet/raw_ip.c (revision 7156)
1 /*	raw_ip.c	4.12	82/06/12	*/
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/if.h"
9 #include "../net/in.h"
10 #include "../net/in_systm.h"
11 #include "../net/ip.h"
12 #include "../net/ip_var.h"
13 #include "../net/raw_cb.h"
14 #include "../net/route.h"
15 #include <errno.h>
16 
17 /*
18  * Raw interface to IP protocol.
19  */
20 
21 static struct sockaddr_in ripdst = { AF_INET };
22 static struct sockaddr_in ripsrc = { AF_INET };
23 static struct sockproto ripproto = { PF_INET };
24 /*
25  * Setup generic address and protocol structures
26  * for raw_input routine, then pass them along with
27  * mbuf chain.
28  */
29 rip_input(m)
30 	struct mbuf *m;
31 {
32 	register struct ip *ip = mtod(m, struct ip *);
33 
34 COUNT(RIP_INPUT);
35 	ripproto.sp_protocol = ip->ip_p;
36 	ripdst.sin_addr = ip->ip_dst;
37 	ripsrc.sin_addr = ip->ip_src;
38 	raw_input(m, &ripproto, (struct sockaddr *)&ripsrc,
39 	  (struct sockaddr *)&ripdst);
40 }
41 
42 /*
43  * Generate IP header and pass packet to ip_output.
44  * Tack on options user may have setup with control call.
45  */
46 rip_output(m0, so)
47 	struct mbuf *m0;
48 	struct socket *so;
49 {
50 	register struct mbuf *m;
51 	register struct ip *ip;
52 	int len = 0, error;
53 	struct rawcb *rp = sotorawcb(so);
54 	struct ifnet *ifp;
55 	struct sockaddr_in *sin;
56 
57 COUNT(RIP_OUTPUT);
58 	/*
59 	 * Calculate data length and get an mbuf
60 	 * for IP header.
61 	 */
62 	for (m = m0; m; m = m->m_next)
63 		len += m->m_len;
64 	m = m_get(M_DONTWAIT);
65 	if (m == 0) {
66 		error = ENOBUFS;
67 		goto bad;
68 	}
69 
70 	/*
71 	 * Fill in IP header as needed.
72 	 */
73 	m->m_off = MMAXOFF - sizeof(struct ip);
74 	m->m_len = sizeof(struct ip);
75 	m->m_next = m0;
76 	ip = mtod(m, struct ip *);
77 	ip->ip_p = so->so_proto->pr_protocol;
78 	ip->ip_len = sizeof(struct ip) + len;
79 	if (rp->rcb_flags & RAW_LADDR) {
80 		sin = (struct sockaddr_in *)&rp->rcb_laddr;
81 		if (sin->sin_family != AF_INET) {
82 			error = EAFNOSUPPORT;
83 			goto bad;
84 		}
85 		ip->ip_src.s_addr = sin->sin_addr.s_addr;
86 	} else
87 		ip->ip_src.s_addr = 0;
88 	ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr;
89 	ip->ip_ttl = MAXTTL;
90 	return (ip_output(m, (struct mbuf *)0, &routetoif, 1));
91 bad:
92 	m_freem(m);
93 	return (error);
94 }
95