xref: /freebsd/sbin/ipf/ipsend/ipsopt.c (revision 41edb306f05651fcaf6c74f9e3557f59f80292e1)
1*41edb306SCy Schubert /*	$FreeBSD$	*/
2*41edb306SCy Schubert 
3*41edb306SCy Schubert /*
4*41edb306SCy Schubert  * Copyright (C) 2012 by Darren Reed.
5*41edb306SCy Schubert  *
6*41edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
7*41edb306SCy Schubert  *
8*41edb306SCy Schubert  */
9*41edb306SCy Schubert #if !defined(lint)
10*41edb306SCy Schubert static const char sccsid[] = "@(#)ipsopt.c	1.2 1/11/96 (C)1995 Darren Reed";
11*41edb306SCy Schubert static const char rcsid[] = "@(#)$Id$";
12*41edb306SCy Schubert #endif
13*41edb306SCy Schubert #include <sys/param.h>
14*41edb306SCy Schubert #include <sys/types.h>
15*41edb306SCy Schubert #include <sys/time.h>
16*41edb306SCy Schubert #include <sys/socket.h>
17*41edb306SCy Schubert #include <netinet/in.h>
18*41edb306SCy Schubert #include <netinet/in_systm.h>
19*41edb306SCy Schubert #include <netinet/ip.h>
20*41edb306SCy Schubert #include <stdio.h>
21*41edb306SCy Schubert #include <string.h>
22*41edb306SCy Schubert #include <stdlib.h>
23*41edb306SCy Schubert #include <netinet/ip_var.h>
24*41edb306SCy Schubert #include <netinet/tcp.h>
25*41edb306SCy Schubert #include <arpa/inet.h>
26*41edb306SCy Schubert #include "ipsend.h"
27*41edb306SCy Schubert 
28*41edb306SCy Schubert 
29*41edb306SCy Schubert #ifndef	__P
30*41edb306SCy Schubert #  define	__P(x)	x
31*41edb306SCy Schubert #endif
32*41edb306SCy Schubert 
33*41edb306SCy Schubert 
34*41edb306SCy Schubert struct ipopt_names ionames[] = {
35*41edb306SCy Schubert 	{ IPOPT_EOL,	0x01,	1, "eol" },
36*41edb306SCy Schubert 	{ IPOPT_NOP,	0x02,	1, "nop" },
37*41edb306SCy Schubert 	{ IPOPT_RR,	0x04,	3, "rr" },	/* 1 route */
38*41edb306SCy Schubert 	{ IPOPT_TS,	0x08,	8, "ts" },	/* 1 TS */
39*41edb306SCy Schubert 	{ IPOPT_SECURITY, 0x08,	11, "sec-level" },
40*41edb306SCy Schubert 	{ IPOPT_LSRR,	0x10,	7, "lsrr" },	/* 1 route */
41*41edb306SCy Schubert 	{ IPOPT_SATID,	0x20,	4, "satid" },
42*41edb306SCy Schubert 	{ IPOPT_SSRR,	0x40,	7, "ssrr" },	/* 1 route */
43*41edb306SCy Schubert 	{ 0, 0, 0, NULL }	/* must be last */
44*41edb306SCy Schubert };
45*41edb306SCy Schubert 
46*41edb306SCy Schubert struct	ipopt_names secnames[] = {
47*41edb306SCy Schubert 	{ IPOPT_SECUR_UNCLASS,	0x0100,	0, "unclass" },
48*41edb306SCy Schubert 	{ IPOPT_SECUR_CONFID,	0x0200,	0, "confid" },
49*41edb306SCy Schubert 	{ IPOPT_SECUR_EFTO,	0x0400,	0, "efto" },
50*41edb306SCy Schubert 	{ IPOPT_SECUR_MMMM,	0x0800,	0, "mmmm" },
51*41edb306SCy Schubert 	{ IPOPT_SECUR_RESTR,	0x1000,	0, "restr" },
52*41edb306SCy Schubert 	{ IPOPT_SECUR_SECRET,	0x2000,	0, "secret" },
53*41edb306SCy Schubert 	{ IPOPT_SECUR_TOPSECRET, 0x4000,0, "topsecret" },
54*41edb306SCy Schubert 	{ 0, 0, 0, NULL }	/* must be last */
55*41edb306SCy Schubert };
56*41edb306SCy Schubert 
57*41edb306SCy Schubert 
58*41edb306SCy Schubert u_short ipseclevel(slevel)
59*41edb306SCy Schubert 	char *slevel;
60*41edb306SCy Schubert {
61*41edb306SCy Schubert 	struct ipopt_names *so;
62*41edb306SCy Schubert 
63*41edb306SCy Schubert 	for (so = secnames; so->on_name; so++)
64*41edb306SCy Schubert 		if (!strcasecmp(slevel, so->on_name))
65*41edb306SCy Schubert 			break;
66*41edb306SCy Schubert 
67*41edb306SCy Schubert 	if (!so->on_name) {
68*41edb306SCy Schubert 		fprintf(stderr, "no such security level: %s\n", slevel);
69*41edb306SCy Schubert 		return 0;
70*41edb306SCy Schubert 	}
71*41edb306SCy Schubert 	return so->on_value;
72*41edb306SCy Schubert }
73*41edb306SCy Schubert 
74*41edb306SCy Schubert 
75*41edb306SCy Schubert int addipopt(op, io, len, class)
76*41edb306SCy Schubert 	char *op;
77*41edb306SCy Schubert 	struct ipopt_names *io;
78*41edb306SCy Schubert 	int len;
79*41edb306SCy Schubert 	char *class;
80*41edb306SCy Schubert {
81*41edb306SCy Schubert 	struct in_addr ipadr;
82*41edb306SCy Schubert 	int olen = len, srr = 0;
83*41edb306SCy Schubert 	u_short val;
84*41edb306SCy Schubert 	u_char lvl;
85*41edb306SCy Schubert 	char *s = op, *t;
86*41edb306SCy Schubert 
87*41edb306SCy Schubert 	if ((len + io->on_siz) > 48) {
88*41edb306SCy Schubert 		fprintf(stderr, "options too long\n");
89*41edb306SCy Schubert 		return 0;
90*41edb306SCy Schubert 	}
91*41edb306SCy Schubert 	len += io->on_siz;
92*41edb306SCy Schubert 	*op++ = io->on_value;
93*41edb306SCy Schubert 	if (io->on_siz > 1) {
94*41edb306SCy Schubert 		/*
95*41edb306SCy Schubert 		 * Allow option to specify RR buffer length in bytes.
96*41edb306SCy Schubert 		 */
97*41edb306SCy Schubert 		if (io->on_value == IPOPT_RR) {
98*41edb306SCy Schubert 			val = (class && *class) ? atoi(class) : 4;
99*41edb306SCy Schubert 			*op++ = val + io->on_siz;
100*41edb306SCy Schubert 			len += val;
101*41edb306SCy Schubert 		} else
102*41edb306SCy Schubert 			*op++ = io->on_siz;
103*41edb306SCy Schubert 		if (io->on_value == IPOPT_TS)
104*41edb306SCy Schubert 			*op++ = IPOPT_MINOFF + 1;
105*41edb306SCy Schubert 		else
106*41edb306SCy Schubert 			*op++ = IPOPT_MINOFF;
107*41edb306SCy Schubert 
108*41edb306SCy Schubert 		while (class && *class) {
109*41edb306SCy Schubert 			t = NULL;
110*41edb306SCy Schubert 			switch (io->on_value)
111*41edb306SCy Schubert 			{
112*41edb306SCy Schubert 			case IPOPT_SECURITY :
113*41edb306SCy Schubert 				lvl = ipseclevel(class);
114*41edb306SCy Schubert 				*(op - 1) = lvl;
115*41edb306SCy Schubert 				break;
116*41edb306SCy Schubert 			case IPOPT_LSRR :
117*41edb306SCy Schubert 			case IPOPT_SSRR :
118*41edb306SCy Schubert 				if ((t = strchr(class, ',')))
119*41edb306SCy Schubert 					*t = '\0';
120*41edb306SCy Schubert 				ipadr.s_addr = inet_addr(class);
121*41edb306SCy Schubert 				srr++;
122*41edb306SCy Schubert 				bcopy((char *)&ipadr, op, sizeof(ipadr));
123*41edb306SCy Schubert 				op += sizeof(ipadr);
124*41edb306SCy Schubert 				break;
125*41edb306SCy Schubert 			case IPOPT_SATID :
126*41edb306SCy Schubert 				val = atoi(class);
127*41edb306SCy Schubert 				bcopy((char *)&val, op, 2);
128*41edb306SCy Schubert 				break;
129*41edb306SCy Schubert 			}
130*41edb306SCy Schubert 
131*41edb306SCy Schubert 			if (t)
132*41edb306SCy Schubert 				*t++ = ',';
133*41edb306SCy Schubert 			class = t;
134*41edb306SCy Schubert 		}
135*41edb306SCy Schubert 		if (srr)
136*41edb306SCy Schubert 			s[IPOPT_OLEN] = IPOPT_MINOFF - 1 + 4 * srr;
137*41edb306SCy Schubert 		if (io->on_value == IPOPT_RR)
138*41edb306SCy Schubert 			op += val;
139*41edb306SCy Schubert 		else
140*41edb306SCy Schubert 			op += io->on_siz - 3;
141*41edb306SCy Schubert 	}
142*41edb306SCy Schubert 	return len - olen;
143*41edb306SCy Schubert }
144*41edb306SCy Schubert 
145*41edb306SCy Schubert 
146*41edb306SCy Schubert u_32_t buildopts(cp, op, len)
147*41edb306SCy Schubert 	char *cp, *op;
148*41edb306SCy Schubert 	int len;
149*41edb306SCy Schubert {
150*41edb306SCy Schubert 	struct ipopt_names *io;
151*41edb306SCy Schubert 	u_32_t msk = 0;
152*41edb306SCy Schubert 	char *s, *t;
153*41edb306SCy Schubert 	int inc, lastop = -1;
154*41edb306SCy Schubert 
155*41edb306SCy Schubert 	for (s = strtok(cp, ","); s; s = strtok(NULL, ",")) {
156*41edb306SCy Schubert 		if ((t = strchr(s, '=')))
157*41edb306SCy Schubert 			*t++ = '\0';
158*41edb306SCy Schubert 		for (io = ionames; io->on_name; io++) {
159*41edb306SCy Schubert 			if (strcasecmp(s, io->on_name) || (msk & io->on_bit))
160*41edb306SCy Schubert 				continue;
161*41edb306SCy Schubert 			lastop = io->on_value;
162*41edb306SCy Schubert 			if ((inc = addipopt(op, io, len, t))) {
163*41edb306SCy Schubert 				op += inc;
164*41edb306SCy Schubert 				len += inc;
165*41edb306SCy Schubert 			}
166*41edb306SCy Schubert 			msk |= io->on_bit;
167*41edb306SCy Schubert 			break;
168*41edb306SCy Schubert 		}
169*41edb306SCy Schubert 		if (!io->on_name) {
170*41edb306SCy Schubert 			fprintf(stderr, "unknown IP option name %s\n", s);
171*41edb306SCy Schubert 			return 0;
172*41edb306SCy Schubert 		}
173*41edb306SCy Schubert 	}
174*41edb306SCy Schubert 
175*41edb306SCy Schubert 	if (len & 3) {
176*41edb306SCy Schubert 		while (len & 3) {
177*41edb306SCy Schubert 			*op++ = ((len & 3) == 3) ? IPOPT_EOL : IPOPT_NOP;
178*41edb306SCy Schubert 			len++;
179*41edb306SCy Schubert 		}
180*41edb306SCy Schubert 	} else {
181*41edb306SCy Schubert 		if (lastop != IPOPT_EOL) {
182*41edb306SCy Schubert 			if (lastop == IPOPT_NOP)
183*41edb306SCy Schubert 				*(op - 1) = IPOPT_EOL;
184*41edb306SCy Schubert 			else {
185*41edb306SCy Schubert 				*op++ = IPOPT_NOP;
186*41edb306SCy Schubert 				*op++ = IPOPT_NOP;
187*41edb306SCy Schubert 				*op++ = IPOPT_NOP;
188*41edb306SCy Schubert 				*op = IPOPT_EOL;
189*41edb306SCy Schubert 				len += 4;
190*41edb306SCy Schubert 			}
191*41edb306SCy Schubert 		}
192*41edb306SCy Schubert 	}
193*41edb306SCy Schubert 	return len;
194*41edb306SCy Schubert }
195