4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
10 static const char sccsid[] = "@(#)ipsopt.c 1.2 1/11/96 (C)1995 Darren Reed";
11 static const char rcsid[] = "@(#)$Id$";
13 #include <sys/param.h>
14 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <netinet/in_systm.h>
19 #include <netinet/ip.h>
24 #include <netinet/ip_var.h>
26 #include <netinet/tcp.h>
27 #include <arpa/inet.h>
40 struct ipopt_names ionames[] = {
41 { IPOPT_EOL, 0x01, 1, "eol" },
42 { IPOPT_NOP, 0x02, 1, "nop" },
43 { IPOPT_RR, 0x04, 3, "rr" }, /* 1 route */
44 { IPOPT_TS, 0x08, 8, "ts" }, /* 1 TS */
45 { IPOPT_SECURITY, 0x08, 11, "sec-level" },
46 { IPOPT_LSRR, 0x10, 7, "lsrr" }, /* 1 route */
47 { IPOPT_SATID, 0x20, 4, "satid" },
48 { IPOPT_SSRR, 0x40, 7, "ssrr" }, /* 1 route */
49 { 0, 0, 0, NULL } /* must be last */
52 struct ipopt_names secnames[] = {
53 { IPOPT_SECUR_UNCLASS, 0x0100, 0, "unclass" },
54 { IPOPT_SECUR_CONFID, 0x0200, 0, "confid" },
55 { IPOPT_SECUR_EFTO, 0x0400, 0, "efto" },
56 { IPOPT_SECUR_MMMM, 0x0800, 0, "mmmm" },
57 { IPOPT_SECUR_RESTR, 0x1000, 0, "restr" },
58 { IPOPT_SECUR_SECRET, 0x2000, 0, "secret" },
59 { IPOPT_SECUR_TOPSECRET, 0x4000,0, "topsecret" },
60 { 0, 0, 0, NULL } /* must be last */
64 u_short ipseclevel(slevel)
67 struct ipopt_names *so;
69 for (so = secnames; so->on_name; so++)
70 if (!strcasecmp(slevel, so->on_name))
74 fprintf(stderr, "no such security level: %s\n", slevel);
81 int addipopt(op, io, len, class)
83 struct ipopt_names *io;
88 int olen = len, srr = 0;
93 if ((len + io->on_siz) > 48) {
94 fprintf(stderr, "options too long\n");
101 * Allow option to specify RR buffer length in bytes.
103 if (io->on_value == IPOPT_RR) {
104 val = (class && *class) ? atoi(class) : 4;
105 *op++ = val + io->on_siz;
109 if (io->on_value == IPOPT_TS)
110 *op++ = IPOPT_MINOFF + 1;
112 *op++ = IPOPT_MINOFF;
114 while (class && *class) {
116 switch (io->on_value)
118 case IPOPT_SECURITY :
119 lvl = ipseclevel(class);
124 if ((t = strchr(class, ',')))
126 ipadr.s_addr = inet_addr(class);
128 bcopy((char *)&ipadr, op, sizeof(ipadr));
133 bcopy((char *)&val, op, 2);
142 s[IPOPT_OLEN] = IPOPT_MINOFF - 1 + 4 * srr;
143 if (io->on_value == IPOPT_RR)
146 op += io->on_siz - 3;
152 u_32_t buildopts(cp, op, len)
156 struct ipopt_names *io;
159 int inc, lastop = -1;
161 for (s = strtok(cp, ","); s; s = strtok(NULL, ",")) {
162 if ((t = strchr(s, '=')))
164 for (io = ionames; io->on_name; io++) {
165 if (strcasecmp(s, io->on_name) || (msk & io->on_bit))
167 lastop = io->on_value;
168 if ((inc = addipopt(op, io, len, t))) {
176 fprintf(stderr, "unknown IP option name %s\n", s);
183 *op++ = ((len & 3) == 3) ? IPOPT_EOL : IPOPT_NOP;
187 if (lastop != IPOPT_EOL) {
188 if (lastop == IPOPT_NOP)
189 *(op - 1) = IPOPT_EOL;