xref: /original-bsd/sys/netinet/raw_ip.c (revision c05f5e42)
1 /*	raw_ip.c	4.5	82/03/05	*/
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/ip.h"
11 #include "../net/ip_var.h"
12 #include "../net/raw_cb.h"
13 #include "../errno.h"
14 
15 /*
16  * Raw interface to IP protocol.
17  */
18 
19 static struct sockaddr_in ripdst = { PF_INET };
20 static struct sockaddr_in ripsrc = { PF_INET };
21 static struct sockproto ripproto = { AF_INET };
22 
23 /*
24  * Setup generic address and protocol structures
25  * for raw_input routine, then pass them along with
26  * mbuf chain.
27  */
28 rip_input(m)
29 	struct mbuf *m;
30 {
31 	register struct ip *ip = mtod(m, struct ip *);
32 	struct sockaddr_in sin;
33 	struct sockproto sp;
34 
35 COUNT(RIP_INPUT);
36 	ripproto.sp_protocol = ip->ip_p;
37 	ripdst.sin_addr = ip->ip_dst;
38 	ripsrc.sin_addr = ip->ip_src;
39 	raw_input(m, &ripproto, &ripdst, &ripsrc);
40 }
41 
42 /*ARGSUSED*/
43 rip_ctlinput(m)
44 	struct mbuf *m;
45 {
46 COUNT(RIP_CTLINPUT);
47 }
48 
49 /*
50  * Generate IP header and pass packet to ip_output.
51  * Tack on options user may have setup with control call.
52  */
53 rip_output(m0, so)
54 	struct mbuf *m0;
55 	struct socket *so;
56 {
57 	register struct mbuf *m;
58 	register struct ip *ip;
59 	register int len = 0;
60 	register struct rawcb *rp = sotorawcb(so);
61 
62 COUNT(RIP_OUTPUT);
63 	/*
64 	 * Calculate data length and get an mbuf
65 	 * for IP header.
66 	 */
67 	for (m = m0; m; m = m->m_next)
68 		len += m->m_len;
69 	m = m_get(M_DONTWAIT);
70 	if (m == 0) {
71 		m_freem(m);
72 		return;
73 	}
74 
75 	/*
76 	 * Fill in IP header as needed.
77 	 */
78 	m->m_off = MMAXOFF - sizeof(struct ip);
79 	m->m_len = sizeof(struct ip);
80 	m->m_next = m0;
81 	ip = mtod(m, struct ip *);
82 	ip->ip_p = so->so_proto->pr_protocol;
83 	ip->ip_len = sizeof(struct ip) + len;
84 	ip->ip_dst =
85 		((struct sockaddr_in *)&rp->rcb_addr)->sin_addr;
86 	ip->ip_src =
87 		((struct sockaddr_in *)&so->so_addr)->sin_addr;
88 	ip->ip_ttl = MAXTTL;
89 	return (ip_output(m, 0));
90 }
91 
92 /*
93  * Intercept control operations related to
94  * handling of IP options.  Otherwise,
95  * just pass things on to the raw_usrreq
96  * routine for setup and tear down of
97  * raw control block data structures.
98  */
99 rip_usrreq(so, req, m, addr)
100 	struct socket *so;
101 	int req;
102 	struct mbuf *m;
103 	caddr_t addr;
104 {
105 	register struct rawcb *rp = sotorawcb(so);
106 
107 COUNT(RAW_USRREQ);
108 	if (rp == 0 && req != PRU_ATTACH)
109 		return (EINVAL);
110 
111 	switch (req) {
112 
113 	/*
114 	 * SHOULD HAVE CONTROL TO SET PROTOCOL NUMBER (e.g. GGP)
115 	 */
116 	case PRU_CONTROL:
117 		return (EOPNOTSUPP);
118 	}
119 	return (raw_usrreq(so, req, m, addr));
120 }
121