xref: /csrg-svn/sys/net/raw_usrreq.c (revision 8636)
1 /*	raw_usrreq.c	4.20	82/10/17	*/
2 
3 #include "../h/param.h"
4 #include "../h/mbuf.h"
5 #include "../h/protosw.h"
6 #include "../h/socket.h"
7 #include "../h/socketvar.h"
8 #include "../vax/mtpr.h"
9 #include "../net/if.h"
10 #include "../net/netisr.h"
11 #include "../net/raw_cb.h"
12 #include <errno.h>
13 
14 int	rawqmaxlen = IFQ_MAXLEN;
15 
16 /*
17  * Initialize raw connection block q.
18  */
19 raw_init()
20 {
21 
22 	rawcb.rcb_next = rawcb.rcb_prev = &rawcb;
23 	rawintrq.ifq_maxlen = IFQ_MAXLEN;
24 }
25 
26 /*
27  * Raw protocol interface.
28  */
29 raw_input(m0, proto, src, dst)
30 	struct mbuf *m0;
31 	struct sockproto *proto;
32 	struct sockaddr *src, *dst;
33 {
34 	register struct mbuf *m;
35 	struct raw_header *rh;
36 	int s;
37 
38 	/*
39 	 * Rip off an mbuf for a generic header.
40 	 */
41 	m = m_get(M_DONTWAIT);
42 	if (m == 0) {
43 		m_freem(m0);
44 		return;
45 	}
46 	m->m_next = m0;
47 	m->m_len = sizeof(struct raw_header);
48 	rh = mtod(m, struct raw_header *);
49 	rh->raw_dst = *dst;
50 	rh->raw_src = *src;
51 	rh->raw_proto = *proto;
52 
53 	/*
54 	 * Header now contains enough info to decide
55 	 * which socket to place packet in (if any).
56 	 * Queue it up for the raw protocol process
57 	 * running at software interrupt level.
58 	 */
59 	s = splimp();
60 	if (IF_QFULL(&rawintrq))
61 		m_freem(m);
62 	else
63 		IF_ENQUEUE(&rawintrq, m);
64 	splx(s);
65 	schednetisr(NETISR_RAW);
66 }
67 
68 /*
69  * Raw protocol input routine.  Process packets entered
70  * into the queue at interrupt time.  Find the socket
71  * associated with the packet(s) and move them over.  If
72  * nothing exists for this packet, drop it.
73  */
74 rawintr()
75 {
76 	int s;
77 	struct mbuf *m;
78 	register struct rawcb *rp;
79 	register struct protosw *lproto;
80 	register struct raw_header *rh;
81 	struct socket *last;
82 
83 next:
84 	s = splimp();
85 	IF_DEQUEUE(&rawintrq, m);
86 	splx(s);
87 	if (m == 0)
88 		return;
89 	rh = mtod(m, struct raw_header *);
90 	last = 0;
91 	for (rp = rawcb.rcb_next; rp != &rawcb; rp = rp->rcb_next) {
92 		lproto = rp->rcb_socket->so_proto;
93 		if (lproto->pr_family != rh->raw_proto.sp_family)
94 			continue;
95 		if (lproto->pr_protocol &&
96 		    lproto->pr_protocol != rh->raw_proto.sp_protocol)
97 			continue;
98 		/*
99 		 * We assume the lower level routines have
100 		 * placed the address in a canonical format
101 		 * suitable for a structure comparison.
102 		 */
103 #define equal(a1, a2) \
104 	(bcmp((caddr_t)&(a1), (caddr_t)&(a2), sizeof (struct sockaddr)) == 0)
105 		if ((rp->rcb_flags & RAW_LADDR) &&
106 		    !equal(rp->rcb_laddr, rh->raw_dst))
107 			continue;
108 		if ((rp->rcb_flags & RAW_FADDR) &&
109 		    !equal(rp->rcb_faddr, rh->raw_src))
110 			continue;
111 		if (last) {
112 			struct mbuf *n;
113 			if ((n = m_copy(m->m_next, 0, (int)M_COPYALL)) == 0)
114 				goto nospace;
115 			if (sbappendaddr(&last->so_rcv, &rh->raw_src, n)==0) {
116 				/* should notify about lost packet */
117 				m_freem(n);
118 				goto nospace;
119 			}
120 			sorwakeup(last);
121 		}
122 nospace:
123 		last = rp->rcb_socket;
124 	}
125 	if (last) {
126 		m = m_free(m);		/* header */
127 		if (sbappendaddr(&last->so_rcv, &rh->raw_src, m) == 0)
128 			goto drop;
129 		sorwakeup(last);
130 		goto next;
131 	}
132 drop:
133 	m_freem(m);
134 	goto next;
135 }
136 
137 /*ARGSUSED*/
138 raw_ctlinput(cmd, arg)
139 	int cmd;
140 	caddr_t arg;
141 {
142 
143 	if (cmd < 0 || cmd > PRC_NCMDS)
144 		return;
145 	/* INCOMPLETE */
146 }
147 
148 /*ARGSUSED*/
149 raw_usrreq(so, req, m, nam, opt)
150 	struct socket *so;
151 	int req;
152 	struct mbuf *m, *nam;
153 	struct socketopt *opt;
154 {
155 	register struct rawcb *rp = sotorawcb(so);
156 	int error = 0;
157 
158 	if (rp == 0 && req != PRU_ATTACH)
159 		return (EINVAL);
160 
161 	switch (req) {
162 
163 	/*
164 	 * Allocate a raw control block and fill in the
165 	 * necessary info to allow packets to be routed to
166 	 * the appropriate raw interface routine.
167 	 */
168 	case PRU_ATTACH:
169 		if ((so->so_state & SS_PRIV) == 0)
170 			return (EACCES);
171 		if (rp)
172 			return (EINVAL);
173 		error = raw_attach(so);
174 		break;
175 
176 	/*
177 	 * Destroy state just before socket deallocation.
178 	 * Flush data or not depending on the options.
179 	 */
180 	case PRU_DETACH:
181 		if (rp == 0)
182 			return (ENOTCONN);
183 		raw_detach(rp);
184 		break;
185 
186 	/*
187 	 * If a socket isn't bound to a single address,
188 	 * the raw input routine will hand it anything
189 	 * within that protocol family (assuming there's
190 	 * nothing else around it should go to).
191 	 */
192 	case PRU_CONNECT:
193 		if (rp->rcb_flags & RAW_FADDR)
194 			return (EISCONN);
195 		raw_connaddr(rp, nam);
196 		soisconnected(so);
197 		break;
198 
199 	case PRU_DISCONNECT:
200 		if ((rp->rcb_flags & RAW_FADDR) == 0)
201 			return (ENOTCONN);
202 		raw_disconnect(rp);
203 		soisdisconnected(so);
204 		break;
205 
206 	/*
207 	 * Mark the connection as being incapable of further input.
208 	 */
209 	case PRU_SHUTDOWN:
210 		socantsendmore(so);
211 		break;
212 
213 	/*
214 	 * Ship a packet out.  The appropriate raw output
215 	 * routine handles any massaging necessary.
216 	 */
217 	case PRU_SEND:
218 		if (nam) {
219 			if (rp->rcb_flags & RAW_FADDR)
220 				return (EISCONN);
221 			raw_connaddr(rp, nam);
222 		} else if ((rp->rcb_flags & RAW_FADDR) == 0)
223 			return (ENOTCONN);
224 		error = (*so->so_proto->pr_output)(m, so);
225 		if (nam)
226 			rp->rcb_flags &= ~RAW_FADDR;
227 		break;
228 
229 	case PRU_ABORT:
230 		raw_disconnect(rp);
231 		sofree(so);
232 		soisdisconnected(so);
233 		break;
234 
235 	/*
236 	 * Not supported.
237 	 */
238 	case PRU_ACCEPT:
239 	case PRU_RCVD:
240 	case PRU_CONTROL:
241 	case PRU_SENSE:
242 	case PRU_RCVOOB:
243 	case PRU_SENDOOB:
244 		error = EOPNOTSUPP;
245 		break;
246 
247 	case PRU_SOCKADDR:
248 		bcopy((caddr_t)&rp->rcb_laddr, mtod(nam, struct sockaddr *),
249 		    sizeof (struct sockaddr));
250 		nam->m_len = sizeof (struct sockaddr);
251 		break;
252 
253 	default:
254 		panic("raw_usrreq");
255 	}
256 	return (error);
257 }
258