]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netiso/clnp_options.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1991, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)clnp_options.c 8.1 (Berkeley) 6/10/93
57 /***********************************************************
58 Copyright IBM Corporation 1987
62 Permission to use, copy, modify, and distribute this software and its
63 documentation for any purpose and without fee is hereby granted,
64 provided that the above copyright notice appear in all copies and that
65 both that copyright notice and this permission notice appear in
66 supporting documentation, and that the name of IBM not be
67 used in advertising or publicity pertaining to distribution of the
68 software without specific, written prior permission.
70 IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
71 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
72 IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
73 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
74 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
75 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
78 ******************************************************************/
81 * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
86 #include <sys/param.h>
88 #include <sys/domain.h>
89 #include <sys/protosw.h>
90 #include <sys/socket.h>
91 #include <sys/socketvar.h>
92 #include <sys/errno.h>
95 #include <net/route.h>
97 #include <netiso/iso.h>
98 #include <netiso/clnp.h>
99 #include <netiso/clnp_stat.h>
100 #include <netiso/argo_debug.h>
103 * FUNCTION: clnp_update_srcrt
105 * PURPOSE: Process src rt option accompanying a clnp datagram.
106 * - bump src route ptr if src routing and
107 * we appear current in src route list.
113 * NOTES: If source routing has been terminated, do nothing.
115 clnp_update_srcrt(options
, oidx
)
116 struct mbuf
*options
; /* ptr to options mbuf */
117 struct clnp_optidx
*oidx
; /* ptr to option index */
119 u_char len
; /* length of current address */
120 struct iso_addr isoa
; /* copy current address into here */
122 if (CLNPSRCRT_TERM(oidx
, options
)) {
124 printf("clnp_update_srcrt: src rt terminated\n");
129 len
= CLNPSRCRT_CLEN(oidx
, options
);
130 bcopy(CLNPSRCRT_CADDR(oidx
, options
), (caddr_t
)&isoa
, len
);
134 printf("clnp_update_srcrt: current src rt: %s\n",
135 clnp_iso_addrp(&isoa
));
138 if (clnp_ours(&isoa
)) {
140 printf("clnp_update_srcrt: updating src rt\n");
143 /* update pointer to next src route */
144 len
++; /* count length byte too! */
145 CLNPSRCRT_OFF(oidx
, options
) += len
;
150 * FUNCTION: clnp_dooptions
152 * PURPOSE: Process options accompanying a clnp datagram.
153 * Processing includes
154 * - log our address if recording route
162 clnp_dooptions(options
, oidx
, ifp
, isoa
)
163 struct mbuf
*options
; /* ptr to options mbuf */
164 struct clnp_optidx
*oidx
; /* ptr to option index */
165 struct ifnet
*ifp
; /* ptr to interface pkt is leaving on */
166 struct iso_addr
*isoa
; /* ptr to our address for this ifp */
169 * If record route is specified, move all
170 * existing records over, and insert the address of
173 if (oidx
->cni_recrtp
) {
174 char *opt
; /* ptr to beginning of recrt option */
175 u_char off
; /* offset from opt of first free byte */
176 char *rec_start
; /* beginning of new rt recorded */
178 opt
= CLNP_OFFTOOPT(options
, oidx
->cni_recrtp
);
180 rec_start
= opt
+ off
- 1;
183 printf("clnp_dooptions: record route: option x%x for %d bytes\n",
184 opt
, oidx
->cni_recrt_len
);
185 printf("\tfree slot offset x%x\n", off
);
186 printf("clnp_dooptions: recording %s\n", clnp_iso_addrp(isoa
));
187 printf("clnp_dooptions: option dump:\n");
188 dump_buf(opt
, oidx
->cni_recrt_len
);
191 /* proceed only if recording has not been terminated */
193 int new_addrlen
= isoa
->isoa_len
+ 1;
195 * if there is insufficient room to store the next address,
196 * then terminate recording. Plus 1 on isoa_len is for the
199 if (oidx
->cni_recrt_len
- (off
- 1) < new_addrlen
) {
200 *(opt
+ 1) = 0xff; /* terminate recording */
203 printf("clnp_dooptions: new addr at x%x for %d\n",
204 rec_start
, new_addrlen
);
207 bcopy((caddr_t
)isoa
, rec_start
, new_addrlen
);
209 /* update offset field */
210 *(opt
+ 1) += new_addrlen
;
213 printf("clnp_dooptions: new option dump:\n");
214 dump_buf(opt
, oidx
->cni_recrt_len
);
222 * FUNCTION: clnp_set_opts
224 * PURPOSE: Check the data mbuf passed for option sanity. If it is
225 * ok, then set the options ptr to address the data mbuf.
226 * If an options mbuf exists, free it. This implies that
227 * any old options will be lost. If data is NULL, simply
228 * free any old options.
230 * RETURNS: unix error code
236 clnp_set_opts(options
, data
)
237 struct mbuf
**options
; /* target for option information */
238 struct mbuf
**data
; /* source of option information */
240 int error
= 0; /* error return value */
241 struct clnp_optidx dummy
; /* dummy index - not used */
244 * remove any existing options
246 if (*options
!= NULL
) {
253 * Insure that the options are reasonable.
255 * Also, we do not support security, priority,
256 * nor do we allow one to send an ER option
258 * The QOS parameter is checked for the DECBIT.
260 if ((clnp_opt_sanity(*data
, mtod(*data
, caddr_t
), (*data
)->m_len
,
262 (dummy
.cni_securep
) ||
263 (dummy
.cni_priorp
) ||
264 (dummy
.cni_er_reason
!= ER_INVALREAS
)) {
268 *data
= NULL
; /* so caller won't free mbuf @ *data */
275 * FUNCTION: clnp_opt_sanity
277 * PURPOSE: Check the options (beginning at opts for len bytes) for
278 * sanity. In addition, fill in the option index structure
279 * in with information about each option discovered.
281 * RETURNS: success (options check out) - 0
282 * failure - an ER pdu error code describing failure
286 * NOTES: Each pointer field of the option index is filled in with
287 * the offset from the beginning of the mbuf data, not the
290 clnp_opt_sanity(m
, opts
, len
, oidx
)
291 struct mbuf
*m
; /* mbuf options reside in */
292 caddr_t opts
; /* ptr to buffer containing options */
293 int len
; /* length of buffer */
294 struct clnp_optidx
*oidx
; /* RETURN: filled in with option idx info */
296 u_char opcode
; /* code of particular option */
297 u_char oplen
; /* length of a particular option */
298 caddr_t opts_end
; /* ptr to end of options */
299 u_char pad
= 0, secure
= 0, srcrt
= 0, recrt
= 0, qos
= 0, prior
= 0;
300 /* flags for catching duplicate options */
303 printf("clnp_opt_sanity: checking %d bytes of data:\n", len
);
307 /* clear option index field if passed */
308 bzero((caddr_t
)oidx
, sizeof(struct clnp_optidx
));
311 * We need to indicate whether the ER option is present. This is done
312 * by overloading the er_reason field to also indicate presense of
313 * the option along with the option value. I would like ER_INVALREAS
314 * to have value 0, but alas, 0 is a valid er reason...
316 oidx
->cni_er_reason
= ER_INVALREAS
;
318 opts_end
= opts
+ len
;
319 while (opts
< opts_end
) {
320 /* must have at least 2 bytes per option (opcode and len) */
321 if (opts
+ 2 > opts_end
)
322 return(GEN_INCOMPLETE
);
327 printf("clnp_opt_sanity: opcode is %x and oplen %d\n",
329 printf("clnp_opt_sanity: clnpoval_SRCRT is %x\n", CLNPOVAL_SRCRT
);
333 printf("CLNPOVAL_PAD\n");
335 case CLNPOVAL_SECURE
: {
336 printf("CLNPOVAL_SECURE\n");
338 case CLNPOVAL_SRCRT
: {
339 printf("CLNPOVAL_SRCRT\n");
341 case CLNPOVAL_RECRT
: {
342 printf("CLNPOVAL_RECRT\n");
345 printf("CLNPOVAL_QOS\n");
347 case CLNPOVAL_PRIOR
: {
348 printf("CLNPOVAL_PRIOR\n");
350 case CLNPOVAL_ERREAS
: {
351 printf("CLNPOVAL_ERREAS\n");
354 printf("UKNOWN option %x\n", opcode
);
358 /* don't allow crazy length values */
359 if (opts
+ oplen
> opts_end
)
360 return(GEN_INCOMPLETE
);
365 * Padding: increment pointer by length of padding
367 if (pad
++) /* duplicate ? */
372 case CLNPOVAL_SECURE
: {
373 u_char format
= *opts
;
375 if (secure
++) /* duplicate ? */
378 * Security: high 2 bits of first octet indicate format
379 * (00 in high bits is reserved).
380 * Remaining bits must be 0. Remaining octets indicate
383 if (((format
& 0x3f) > 0) || /* low 6 bits set ? */
384 ((format
& 0xc0) == 0)) /* high 2 bits zero ? */
385 return(GEN_HDRSYNTAX
);
387 oidx
->cni_securep
= CLNP_OPTTOOFF(m
, opts
);
388 oidx
->cni_secure_len
= oplen
;
392 case CLNPOVAL_SRCRT
: {
393 u_char type
, offset
; /* type of rt, offset of start */
394 caddr_t route_end
; /* address of end of route option */
397 printf("clnp_opt_sanity: SRC RT\n");
400 if (srcrt
++) /* duplicate ? */
403 * source route: There must be 2 bytes following the length
404 * field: type and offset. The type must be either
405 * partial route or complete route. The offset field must
406 * be within the option. A single exception is made, however.
407 * The offset may be 1 greater than the length. This case
408 * occurs when the last source route record is consumed.
409 * In this case, we ignore the source route option.
410 * RAH? You should be able to set offset to 'ff' like in record
412 * Following this is a series of address fields.
413 * Each address field is composed of a (length, address) pair.
414 * Insure that the offset and each address length is reasonable
416 route_end
= opts
+ oplen
;
418 if (opts
+ 2 > route_end
)
419 return(SRCRT_SYNTAX
);
425 /* type must be partial or complete */
426 if (!((type
== CLNPOVAL_PARTRT
) || (type
== CLNPOVAL_COMPRT
)))
427 return(SRCRT_SYNTAX
);
429 oidx
->cni_srcrt_s
= CLNP_OPTTOOFF(m
, opts
);
430 oidx
->cni_srcrt_len
= oplen
;
432 opts
+= offset
-1; /*set opts to first addr in rt */
435 * Offset must be reasonable:
436 * less than end of options, or equal to end of options
438 if (opts
>= route_end
) {
439 if (opts
== route_end
) {
441 printf("clnp_opt_sanity: end of src route info\n");
445 return(SRCRT_SYNTAX
);
448 while (opts
< route_end
) {
449 u_char addrlen
= *opts
++;
450 if (opts
+ addrlen
> route_end
)
451 return(SRCRT_SYNTAX
);
455 case CLNPOVAL_RECRT
: {
456 u_char type
, offset
; /* type of rt, offset of start */
457 caddr_t record_end
; /* address of end of record option */
459 if (recrt
++) /* duplicate ? */
462 * record route: after the length field, expect a
463 * type and offset. Type must be partial or complete.
464 * Offset indicates where to start recording. Insure it
465 * is within the option. All ones for offset means
466 * recording is terminated.
468 record_end
= opts
+ oplen
;
470 oidx
->cni_recrtp
= CLNP_OPTTOOFF(m
, opts
);
471 oidx
->cni_recrt_len
= oplen
;
473 if (opts
+ 2 > record_end
)
474 return(GEN_INCOMPLETE
);
479 /* type must be partial or complete */
480 if (!((type
== CLNPOVAL_PARTRT
) || (type
== CLNPOVAL_COMPRT
)))
481 return(GEN_HDRSYNTAX
);
483 /* offset must be reasonable */
484 if ((offset
< 0xff) && (opts
+ offset
> record_end
))
485 return(GEN_HDRSYNTAX
);
489 u_char format
= *opts
;
491 if (qos
++) /* duplicate ? */
494 * qos: high 2 bits of first octet indicate format
495 * (00 in high bits is reserved).
496 * Remaining bits must be 0 (unless format indicates
497 * globally unique qos, in which case remaining bits indicate
498 * qos (except bit 6 which is reserved)). Otherwise,
499 * remaining octets indicate actual qos.
501 if (((format
& 0xc0) == 0) || /* high 2 bits zero ? */
502 (((format
& 0xc0) != CLNPOVAL_GLOBAL
) &&
503 ((format
& 0x3f) > 0))) /* not global,low bits used ? */
504 return(GEN_HDRSYNTAX
);
506 oidx
->cni_qos_formatp
= CLNP_OPTTOOFF(m
, opts
);
507 oidx
->cni_qos_len
= oplen
;
512 case CLNPOVAL_PRIOR
: {
513 if (prior
++) /* duplicate ? */
516 * priority: value must be one byte long
519 return(GEN_HDRSYNTAX
);
521 oidx
->cni_priorp
= CLNP_OPTTOOFF(m
, opts
);
526 case CLNPOVAL_ERREAS
: {
528 * er reason: value must be two bytes long
531 return(GEN_HDRSYNTAX
);
533 oidx
->cni_er_reason
= *opts
;
540 printf("clnp_opt_sanity: UNKNOWN OPTION 0x%x\n", opcode
);
542 return(DISC_UNSUPPOPT
);
547 printf("clnp_opt_sanity: return(0)\n", opcode
);