1/*
2 * m_pedit.c		packet editor: IPV4/6 header
3 *
4 *		This program is free software; you can distribute it and/or
5 *		modify it under the terms of the GNU General Public License
6 *		as published by the Free Software Foundation; either version
7 *		2 of the License, or (at your option) any later version.
8 *
9 * Authors:  J Hadi Salim (hadi@cyberus.ca)
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
17#include <fcntl.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22#include "utils.h"
23#include "tc_util.h"
24#include "m_pedit.h"
25
26static int
27parse_ip(int *argc_p, char ***argv_p,struct tc_pedit_sel *sel,struct tc_pedit_key *tkey)
28{
29	int res = -1;
30	int argc = *argc_p;
31	char **argv = *argv_p;
32
33	if (argc < 2)
34		return -1;
35
36	if (strcmp(*argv, "src") == 0) {
37		NEXT_ARG();
38		tkey->off = 12;
39		res = parse_cmd(&argc, &argv, 4, TIPV4,RU32,sel,tkey);
40		goto done;
41	}
42	if (strcmp(*argv, "dst") == 0) {
43		NEXT_ARG();
44		tkey->off = 16;
45		res = parse_cmd(&argc, &argv, 4, TIPV4,RU32,sel,tkey);
46		goto done;
47	}
48	/* jamal - look at these and make them either old or new
49	** scheme given diffserv
50	** dont forget the CE bit
51	*/
52	if (strcmp(*argv, "tos") == 0 || matches(*argv, "dsfield") == 0) {
53		NEXT_ARG();
54		tkey->off = 1;
55		res = parse_cmd(&argc, &argv,  1, TU32,RU8,sel,tkey);
56		goto done;
57	}
58	if (strcmp(*argv, "ihl") == 0) {
59		NEXT_ARG();
60		tkey->off = 0;
61		res = parse_cmd(&argc, &argv, 1, TU32,RU8,sel,tkey);
62		goto done;
63	}
64	if (strcmp(*argv, "protocol") == 0) {
65		NEXT_ARG();
66		tkey->off = 9;
67		res = parse_cmd(&argc, &argv, 1, TU32,RU8,sel,tkey);
68		goto done;
69	}
70	/* jamal - fix this */
71	if (matches(*argv, "precedence") == 0) {
72		NEXT_ARG();
73		tkey->off = 1;
74		res = parse_cmd(&argc, &argv, 1, TU32,RU8,sel,tkey);
75		goto done;
76	}
77	/* jamal - validate this at some point */
78	if (strcmp(*argv, "nofrag") == 0) {
79		NEXT_ARG();
80		tkey->off = 6;
81		res = parse_cmd(&argc, &argv, 1, TU32,0x3F,sel,tkey);
82		goto done;
83	}
84	/* jamal - validate this at some point */
85	if (strcmp(*argv, "firstfrag") == 0) {
86		NEXT_ARG();
87		tkey->off = 6;
88		res = parse_cmd(&argc, &argv, 1, TU32,0x1F,sel,tkey);
89		goto done;
90	}
91	if (strcmp(*argv, "ce") == 0) {
92		NEXT_ARG();
93		tkey->off = 6;
94		res = parse_cmd(&argc, &argv, 1, TU32,0x80,sel,tkey);
95		goto done;
96	}
97	if (strcmp(*argv, "df") == 0) {
98		NEXT_ARG();
99		tkey->off = 6;
100		res = parse_cmd(&argc, &argv, 1, TU32,0x40,sel,tkey);
101		goto done;
102	}
103	if (strcmp(*argv, "mf") == 0) {
104		NEXT_ARG();
105		tkey->off = 6;
106		res = parse_cmd(&argc, &argv, 1, TU32,0x20,sel,tkey);
107		goto done;
108	}
109	if (strcmp(*argv, "dport") == 0) {
110		NEXT_ARG();
111		tkey->off = 22;
112		res = parse_cmd(&argc, &argv, 2, TU32,RU16,sel,tkey);
113		goto done;
114	}
115	if (strcmp(*argv, "sport") == 0) {
116		NEXT_ARG();
117		tkey->off = 20;
118		res = parse_cmd(&argc, &argv, 2, TU32,RU16,sel,tkey);
119		goto done;
120	}
121	if (strcmp(*argv, "icmp_type") == 0) {
122		NEXT_ARG();
123		tkey->off = 20;
124		res = parse_cmd(&argc, &argv, 1, TU32,RU8,sel,tkey);
125		goto done;
126	}
127	if (strcmp(*argv, "icmp_code") == 0) {
128		NEXT_ARG();
129		tkey->off = 20;
130		res = parse_cmd(&argc, &argv, 1, TU32,RU8,sel,tkey);
131		goto done;
132	}
133	return -1;
134
135      done:
136	*argc_p = argc;
137	*argv_p = argv;
138	return res;
139}
140
141static int
142parse_ip6(int *argc_p, char ***argv_p,struct tc_pedit_sel *sel,struct tc_pedit_key *tkey)
143{
144	int res = -1;
145	return res;
146}
147
148struct m_pedit_util p_pedit_ip = {
149	NULL,
150	"ip",
151	parse_ip,
152};
153
154
155struct m_pedit_util p_pedit_ip6 = {
156	NULL,
157	"ip6",
158	parse_ip6,
159};
160