xref: /csrg-svn/sys/netinet/raw_ip.c (revision 5612)
1 /*	raw_ip.c	4.2	82/01/24	*/
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 "/usr/include/errno.h"
14 
15 /*
16  * Raw interface to IP protocol.
17  */
18 
19 static struct sockaddr_in ripaddr = { PF_INET };
20 static struct sockproto ripproto = { AF_INET };
21 
22 /*
23  * Setup generic address and protocol structures
24  * for raw_input routine, then pass them along with
25  * mbuf chain.
26  */
27 rip_input(m)
28 	struct mbuf *m;
29 {
30 	register struct ip *ip = mtod(m, struct ip *);
31 	struct sockaddr_in sin;
32 	struct sockproto sp;
33 
34 COUNT(RIP_INPUT);
35 	ripproto.sp_protocol = ip->ip_p;
36 	ripaddr.sin_addr = ip->ip_dst;
37 	raw_input(m, ripproto, ripaddr);
38 }
39 
40 /*ARGSUSED*/
41 rip_ctlinput(m)
42 	struct mbuf *m;
43 {
44 COUNT(RIP_CTLINPUT);
45 }
46 
47 /*
48  * Generate IP header and pass packet to ip_output.
49  * Tack on options user may have setup with control call.
50  */
51 rip_output(m0, so)
52 	struct mbuf *m0;
53 	struct socket *so;
54 {
55 	register struct mbuf *m;
56 	register struct ip *ip;
57 	register int len = 0;
58 	register struct rawcb *rp = sotorawcb(so);
59 
60 COUNT(RIP_OUTPUT);
61 	if (so->so_options & SO_DEBUG)
62 		printf("rip_output\n");
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 		(void) 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 printf("ip=<p=%d,len=%d,dst=%x,src=%x>\n",ip->ip_p,ip->ip_len,ip->ip_dst,ip->ip_src);
90 	return (ip_output(m, 0));
91 }
92 
93 /*
94  * Intercept control operations related to
95  * handling of IP options.  Otherwise,
96  * just pass things on to the raw_usrreq
97  * routine for setup and tear down of
98  * raw control block data structures.
99  */
100 rip_usrreq(so, req, m, addr)
101 	struct socket *so;
102 	int req;
103 	struct mbuf *m;
104 	caddr_t addr;
105 {
106 	register struct rawcb *rp = sotorawcb(so);
107 
108 COUNT(RAW_USRREQ);
109 	if (rp == 0 && req != PRU_ATTACH)
110 		return (EINVAL);
111 
112 	switch (req) {
113 
114 	case PRU_CONTROL:
115 		return (EOPNOTSUPP);
116 	}
117 	return (raw_usrreq(so, req, m, addr));
118 }
119