ipsopt.c revision 24583
1/*
2 * (C)opyright 1995 by Darren Reed.
3 *
4 * This code may be freely distributed as long as it retains this notice
5 * and is not changed in any way.  The author accepts no responsibility
6 * for the use of this software.  I hate legaleese, don't you ?
7 */
8#if !defined(lint) && defined(LIBC_SCCS)
9static	char	sccsid[] = "@(#)ipsopt.c	1.2 1/11/96 (C)1995 Darren Reed";
10#endif
11#include <stdio.h>
12#include <string.h>
13#include <sys/types.h>
14#include <sys/time.h>
15#include <sys/socket.h>
16#include <netinet/in.h>
17#include <netinet/in_systm.h>
18#include <netinet/ip.h>
19#include "ip_compat.h"
20
21
22#ifndef	__P
23# ifdef	__STDC__
24#  define	__P(x)	x
25# else
26#  define	__P(x)	()
27# endif
28#endif
29
30
31struct ipopt_names {
32	int	on_value;
33	int	on_bit;
34	int	on_siz;
35	char	*on_name;
36};
37
38struct ipopt_names ionames[] = {
39	{ IPOPT_EOL,	0x01,	1, "eol" },
40	{ IPOPT_NOP,	0x02,	1, "nop" },
41	{ IPOPT_RR,	0x04,	7, "rr" },	/* 1 route */
42	{ IPOPT_TS,	0x08,	8, "ts" },	/* 1 TS */
43	{ IPOPT_SECURITY, 0x08,	11, "sec-level" },
44	{ IPOPT_LSRR,	0x10,	7, "lsrr" },	/* 1 route */
45	{ IPOPT_SATID,	0x20,	4, "satid" },
46	{ IPOPT_SSRR,	0x40,	7, "ssrr" },	/* 1 route */
47	{ 0, 0, 0, NULL }	/* must be last */
48};
49
50struct	ipopt_names secnames[] = {
51	{ IPOPT_SECUR_UNCLASS,	0x0100,	0, "unclass" },
52	{ IPOPT_SECUR_CONFID,	0x0200,	0, "confid" },
53	{ IPOPT_SECUR_EFTO,	0x0400,	0, "efto" },
54	{ IPOPT_SECUR_MMMM,	0x0800,	0, "mmmm" },
55	{ IPOPT_SECUR_RESTR,	0x1000,	0, "restr" },
56	{ IPOPT_SECUR_SECRET,	0x2000,	0, "secret" },
57	{ IPOPT_SECUR_TOPSECRET, 0x4000,0, "topsecret" },
58	{ 0, 0, 0, NULL }	/* must be last */
59};
60
61
62u_short seclevel __P((char *));
63u_long optname __P((char *, char *));
64
65
66u_short seclevel(slevel)
67char *slevel;
68{
69	struct ipopt_names *so;
70
71	for (so = secnames; so->on_name; so++)
72		if (!strcasecmp(slevel, so->on_name))
73			break;
74
75	if (!so->on_name) {
76		fprintf(stderr, "no such security level: %s\n", slevel);
77		return 0;
78	}
79	return so->on_value;
80}
81
82
83u_long optname(cp, op)
84char *cp, *op;
85{
86	struct ipopt_names *io;
87	u_short lvl;
88	u_long msk = 0;
89	char *s, *t;
90	int len = 0;
91
92	for (s = strtok(cp, ","); s; s = strtok(NULL, ",")) {
93		if ((t = strchr(s, '=')))
94			*t++ = '\0';
95		for (io = ionames; io->on_name; io++) {
96			if (strcasecmp(s, io->on_name) || (msk & io->on_bit))
97				continue;
98			if ((len + io->on_siz) > 48) {
99				fprintf(stderr, "options too long\n");
100				return 0;
101			}
102			len += io->on_siz;
103			*op++ = io->on_value;
104			if (io->on_siz > 1) {
105				*op++ = io->on_siz;
106				*op++ = IPOPT_MINOFF;
107
108				if (t && !strcasecmp(s, "sec-level")) {
109					lvl = seclevel(t);
110					bcopy(&lvl, op, sizeof(lvl));
111				}
112				op += io->on_siz - 3;
113			}
114			msk |= io->on_bit;
115			break;
116		}
117		if (!io->on_name) {
118			fprintf(stderr, "unknown IP option name %s\n", s);
119			return 0;
120		}
121	}
122	*op++ = IPOPT_EOL;
123	len++;
124	return len;
125}
126