Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /*- |
2 | * Copyright (c) 1991 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | * | |
43371f85 | 33 | * from: @(#)clnp_raw.c 7.8 (Berkeley) 5/6/91 |
fde1aeb2 | 34 | * $Id: clnp_raw.c,v 1.3 1993/11/25 01:35:46 wollman Exp $ |
15637ed4 RG |
35 | */ |
36 | ||
37 | /*********************************************************** | |
38 | Copyright IBM Corporation 1987 | |
39 | ||
40 | All Rights Reserved | |
41 | ||
42 | Permission to use, copy, modify, and distribute this software and its | |
43 | documentation for any purpose and without fee is hereby granted, | |
44 | provided that the above copyright notice appear in all copies and that | |
45 | both that copyright notice and this permission notice appear in | |
46 | supporting documentation, and that the name of IBM not be | |
47 | used in advertising or publicity pertaining to distribution of the | |
48 | software without specific, written prior permission. | |
49 | ||
50 | IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING | |
51 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL | |
52 | IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR | |
53 | ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
54 | WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | |
55 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
56 | SOFTWARE. | |
57 | ||
58 | ******************************************************************/ | |
59 | ||
60 | /* | |
61 | * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison | |
62 | */ | |
15637ed4 RG |
63 | |
64 | #include "param.h" | |
fde1aeb2 | 65 | #include "systm.h" |
15637ed4 RG |
66 | #include "mbuf.h" |
67 | #include "domain.h" | |
68 | #include "protosw.h" | |
69 | #include "socket.h" | |
70 | #include "socketvar.h" | |
71 | #include "errno.h" | |
72 | #include "time.h" | |
73 | ||
74 | #include "../net/if.h" | |
75 | #include "../net/route.h" | |
76 | #include "../net/raw_cb.h" | |
77 | ||
78 | #include "iso.h" | |
79 | #include "iso_pcb.h" | |
80 | #include "clnp.h" | |
81 | #include "clnp_stat.h" | |
82 | #include "argo_debug.h" | |
83 | ||
84 | #include "tp_user.h"/* XXX -- defines SOL_NETWORK */ | |
85 | ||
86 | struct sockproto rclnp_proto = { PF_ISO, 0 }; | |
87 | /* | |
88 | * FUNCTION: rclnp_input | |
89 | * | |
90 | * PURPOSE: Setup generic address an protocol structures for | |
91 | * raw input routine, then pass them along with the | |
92 | * mbuf chain. | |
93 | * | |
94 | * RETURNS: none | |
95 | * | |
96 | * SIDE EFFECTS: | |
97 | * | |
98 | * NOTES: The protocol field of rclnp_proto is set to zero indicating | |
99 | * no protocol. | |
100 | */ | |
4c45483e | 101 | void |
15637ed4 RG |
102 | rclnp_input(m, src, dst, hdrlen) |
103 | struct mbuf *m; /* ptr to packet */ | |
104 | struct sockaddr_iso *src; /* ptr to src address */ | |
105 | struct sockaddr_iso *dst; /* ptr to dest address */ | |
106 | int hdrlen; /* length (in bytes) of clnp header */ | |
107 | { | |
108 | #ifdef TROLL | |
109 | if (trollctl.tr_ops & TR_CHUCK) { | |
110 | m_freem(m); | |
111 | return; | |
112 | } | |
113 | #endif TROLL | |
114 | ||
115 | if (raw_input(m, &rclnp_proto, (struct sockaddr *)src, | |
116 | (struct sockaddr *)dst) == 0) { | |
117 | clnp_stat.cns_delivered--; | |
118 | clnp_stat.cns_noproto++; | |
119 | } | |
120 | } | |
121 | ||
122 | /* | |
123 | * FUNCTION: rclnp_output | |
124 | * | |
125 | * PURPOSE: Prepare to send a raw clnp packet. Setup src and dest | |
126 | * addresses, count the number of bytes to send, and | |
127 | * call clnp_output. | |
128 | * | |
129 | * RETURNS: success - 0 | |
130 | * failure - an appropriate error code | |
131 | * | |
132 | * SIDE EFFECTS: | |
133 | * | |
134 | * NOTES: | |
135 | */ | |
4c45483e | 136 | int |
15637ed4 RG |
137 | rclnp_output(m0, so) |
138 | struct mbuf *m0; /* packet to send */ | |
139 | struct socket *so; /* socket to send from */ | |
140 | { | |
141 | register struct mbuf *m; /* used to scan a chain */ | |
142 | int len = 0; /* store length of chain here */ | |
143 | struct rawisopcb *rp = sotorawisopcb(so); /* ptr to raw cb */ | |
144 | int error; /* return value of function */ | |
145 | int flags; /* flags for clnp_output */ | |
146 | ||
147 | if (0 == m0->m_flags & M_PKTHDR) | |
148 | return (EINVAL); | |
149 | /* | |
150 | * Set up src address. If user has bound socket to an address, use it. | |
151 | * Otherwise, do not specify src (clnp_output will fill it in). | |
152 | */ | |
153 | if (rp->risop_rcb.rcb_laddr) { | |
154 | if (rp->risop_isop.isop_sladdr.siso_family != AF_ISO) { | |
155 | bad: | |
156 | m_freem(m0); | |
157 | return(EAFNOSUPPORT); | |
158 | } | |
159 | } | |
160 | /* set up dest address */ | |
161 | if (rp->risop_rcb.rcb_faddr == 0) | |
162 | goto bad; | |
163 | rp->risop_isop.isop_sfaddr = | |
164 | *(struct sockaddr_iso *)rp->risop_rcb.rcb_faddr; | |
165 | rp->risop_isop.isop_faddr = &rp->risop_isop.isop_sfaddr; | |
166 | ||
167 | /* get flags and ship it off */ | |
168 | flags = rp->risop_flags & CLNP_VFLAGS; | |
169 | ||
170 | error = clnp_output(m0, &rp->risop_isop, m0->m_pkthdr.len, | |
171 | flags|CLNP_NOCACHE); | |
172 | ||
173 | return (error); | |
174 | } | |
175 | ||
176 | /* | |
177 | * FUNCTION: rclnp_ctloutput | |
178 | * | |
179 | * PURPOSE: Raw clnp socket option processing | |
180 | * All options are stored inside an mbuf. | |
181 | * | |
182 | * RETURNS: success - 0 | |
183 | * failure - unix error code | |
184 | * | |
185 | * SIDE EFFECTS: If the options mbuf does not exist, it the mbuf passed | |
186 | * is used. | |
187 | * | |
188 | * NOTES: | |
189 | */ | |
4c45483e | 190 | int |
15637ed4 RG |
191 | rclnp_ctloutput(op, so, level, optname, m) |
192 | int op; /* type of operation */ | |
193 | struct socket *so; /* ptr to socket */ | |
194 | int level; /* level of option */ | |
195 | int optname; /* name of option */ | |
196 | struct mbuf **m; /* ptr to ptr to option data */ | |
197 | { | |
198 | int error = 0; | |
199 | register struct rawisopcb *rp = sotorawisopcb(so);/* raw cb ptr */ | |
200 | ||
201 | IFDEBUG(D_CTLOUTPUT) | |
202 | printf("rclnp_ctloutput: op = x%x, level = x%x, name = x%x\n", | |
203 | op, level, optname); | |
204 | if (*m != NULL) { | |
205 | printf("rclnp_ctloutput: %d bytes of mbuf data\n", (*m)->m_len); | |
206 | dump_buf(mtod((*m), caddr_t), (*m)->m_len); | |
207 | } | |
208 | ENDDEBUG | |
209 | ||
210 | #ifdef SOL_NETWORK | |
211 | if (level != SOL_NETWORK) | |
212 | error = EINVAL; | |
213 | else switch (op) { | |
214 | #else | |
215 | switch (op) { | |
216 | #endif SOL_NETWORK | |
217 | case PRCO_SETOPT: | |
218 | switch (optname) { | |
219 | case CLNPOPT_FLAGS: { | |
220 | u_short usr_flags; | |
221 | /* | |
222 | * Insure that the data passed has exactly one short in it | |
223 | */ | |
224 | if ((*m == NULL) || ((*m)->m_len != sizeof(short))) { | |
225 | error = EINVAL; | |
226 | break; | |
227 | } | |
228 | ||
229 | /* | |
230 | * Don't allow invalid flags to be set | |
231 | */ | |
232 | usr_flags = (*mtod((*m), short *)); | |
233 | ||
234 | if ((usr_flags & (CLNP_VFLAGS)) != usr_flags) { | |
235 | error = EINVAL; | |
236 | } else | |
237 | rp->risop_flags |= usr_flags; | |
238 | ||
239 | } break; | |
240 | ||
241 | case CLNPOPT_OPTS: | |
242 | if (error = clnp_set_opts(&rp->risop_isop.isop_options, m)) | |
243 | break; | |
244 | rp->risop_isop.isop_optindex = m_get(M_WAIT, MT_SOOPTS); | |
245 | (void) clnp_opt_sanity(rp->risop_isop.isop_options, | |
246 | mtod(rp->risop_isop.isop_options, caddr_t), | |
247 | rp->risop_isop.isop_options->m_len, | |
248 | mtod(rp->risop_isop.isop_optindex, | |
249 | struct clnp_optidx *)); | |
250 | break; | |
251 | } | |
252 | break; | |
253 | ||
254 | case PRCO_GETOPT: | |
255 | #ifdef notdef | |
256 | /* commented out to keep hi C quiet */ | |
257 | switch (optname) { | |
258 | default: | |
259 | error = EINVAL; | |
260 | break; | |
261 | } | |
262 | #endif notdef | |
263 | break; | |
264 | default: | |
265 | error = EINVAL; | |
266 | break; | |
267 | } | |
268 | if (op == PRCO_SETOPT) { | |
269 | /* note: m_freem does not barf is *m is NULL */ | |
270 | m_freem(*m); | |
271 | *m = NULL; | |
272 | } | |
273 | ||
274 | return error; | |
275 | } | |
276 | ||
277 | /*ARGSUSED*/ | |
4c45483e | 278 | int |
15637ed4 RG |
279 | clnp_usrreq(so, req, m, nam, control) |
280 | register struct socket *so; | |
281 | int req; | |
282 | struct mbuf *m, *nam, *control; | |
283 | { | |
284 | register int error = 0; | |
285 | register struct rawisopcb *rp = sotorawisopcb(so); | |
286 | ||
287 | rp = sotorawisopcb(so); | |
288 | switch (req) { | |
289 | ||
290 | case PRU_ATTACH: | |
291 | if (rp) | |
292 | panic("rip_attach"); | |
293 | MALLOC(rp, struct rawisopcb *, sizeof *rp, M_PCB, M_WAITOK); | |
294 | if (rp == 0) | |
295 | return (ENOBUFS); | |
296 | bzero((caddr_t)rp, sizeof *rp); | |
297 | so->so_pcb = (caddr_t)rp; | |
298 | break; | |
299 | ||
300 | case PRU_DETACH: | |
301 | if (rp == 0) | |
302 | panic("rip_detach"); | |
303 | if (rp->risop_isop.isop_options) | |
304 | m_freem(rp->risop_isop.isop_options); | |
305 | if (rp->risop_isop.isop_route.ro_rt) | |
306 | RTFREE(rp->risop_isop.isop_route.ro_rt); | |
307 | if (rp->risop_rcb.rcb_laddr) | |
308 | rp->risop_rcb.rcb_laddr = 0; | |
309 | /* free clnp cached hdr if necessary */ | |
310 | if (rp->risop_isop.isop_clnpcache != NULL) { | |
311 | struct clnp_cache *clcp = | |
312 | mtod(rp->risop_isop.isop_clnpcache, struct clnp_cache *); | |
313 | if (clcp->clc_hdr != NULL) { | |
314 | m_free(clcp->clc_hdr); | |
315 | } | |
316 | m_free(rp->risop_isop.isop_clnpcache); | |
317 | } | |
318 | if (rp->risop_isop.isop_optindex != NULL) | |
319 | m_free(rp->risop_isop.isop_optindex); | |
320 | ||
321 | break; | |
322 | ||
323 | case PRU_BIND: | |
324 | { | |
325 | struct sockaddr_iso *addr = mtod(nam, struct sockaddr_iso *); | |
326 | ||
327 | if (nam->m_len != sizeof(*addr)) | |
328 | return (EINVAL); | |
329 | if ((ifnet == 0) || | |
330 | (addr->siso_family != AF_ISO) || | |
331 | (addr->siso_addr.isoa_len && | |
332 | ifa_ifwithaddr((struct sockaddr *)addr) == 0)) | |
333 | return (EADDRNOTAVAIL); | |
334 | rp->risop_isop.isop_sladdr = *addr; | |
335 | rp->risop_rcb.rcb_laddr = (struct sockaddr *) | |
336 | (rp->risop_isop.isop_laddr = &rp->risop_isop.isop_sladdr); | |
337 | return (0); | |
338 | } | |
339 | case PRU_CONNECT: | |
340 | { | |
341 | struct sockaddr_iso *addr = mtod(nam, struct sockaddr_iso *); | |
342 | ||
343 | if ((nam->m_len > sizeof(*addr)) || (addr->siso_len > sizeof(*addr))) | |
344 | return (EINVAL); | |
345 | if (ifnet == 0) | |
346 | return (EADDRNOTAVAIL); | |
347 | if (addr->siso_family != AF_ISO) | |
348 | rp->risop_isop.isop_sfaddr = *addr; | |
349 | rp->risop_rcb.rcb_faddr = (struct sockaddr *) | |
350 | (rp->risop_isop.isop_faddr = &rp->risop_isop.isop_sfaddr); | |
351 | soisconnected(so); | |
352 | return (0); | |
353 | } | |
354 | } | |
fde1aeb2 | 355 | error = raw_usrreq(so, req, m, nam, control, 0); |
15637ed4 RG |
356 | |
357 | if (error && req == PRU_ATTACH && so->so_pcb) | |
358 | free((caddr_t)rp, M_PCB); | |
359 | return (error); | |
360 | } |