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>
23 #include <netinet/ip_var.h>
24 #include <netinet/tcp.h>
25 #include <arpa/inet.h>
38 struct ipopt_names ionames[] = {
39 { IPOPT_EOL, 0x01, 1, "eol" },
40 { IPOPT_NOP, 0x02, 1, "nop" },
41 { IPOPT_RR, 0x04, 3, "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 */
50 struct 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 */
62 u_short ipseclevel(slevel)
65 struct ipopt_names *so;
67 for (so = secnames; so->on_name; so++)
68 if (!strcasecmp(slevel, so->on_name))
72 fprintf(stderr, "no such security level: %s\n", slevel);
79 int addipopt(op, io, len, class)
81 struct ipopt_names *io;
86 int olen = len, srr = 0;
91 if ((len + io->on_siz) > 48) {
92 fprintf(stderr, "options too long\n");
99 * Allow option to specify RR buffer length in bytes.
101 if (io->on_value == IPOPT_RR) {
102 val = (class && *class) ? atoi(class) : 4;
103 *op++ = val + io->on_siz;
107 if (io->on_value == IPOPT_TS)
108 *op++ = IPOPT_MINOFF + 1;
110 *op++ = IPOPT_MINOFF;
112 while (class && *class) {
114 switch (io->on_value)
116 case IPOPT_SECURITY :
117 lvl = ipseclevel(class);
122 if ((t = strchr(class, ',')))
124 ipadr.s_addr = inet_addr(class);
126 bcopy((char *)&ipadr, op, sizeof(ipadr));
131 bcopy((char *)&val, op, 2);
140 s[IPOPT_OLEN] = IPOPT_MINOFF - 1 + 4 * srr;
141 if (io->on_value == IPOPT_RR)
144 op += io->on_siz - 3;
150 u_32_t buildopts(cp, op, len)
154 struct ipopt_names *io;
157 int inc, lastop = -1;
159 for (s = strtok(cp, ","); s; s = strtok(NULL, ",")) {
160 if ((t = strchr(s, '=')))
162 for (io = ionames; io->on_name; io++) {
163 if (strcasecmp(s, io->on_name) || (msk & io->on_bit))
165 lastop = io->on_value;
166 if ((inc = addipopt(op, io, len, t))) {
174 fprintf(stderr, "unknown IP option name %s\n", s);
181 *op++ = ((len & 3) == 3) ? IPOPT_EOL : IPOPT_NOP;
185 if (lastop != IPOPT_EOL) {
186 if (lastop == IPOPT_NOP)
187 *(op - 1) = IPOPT_EOL;