Debian Codegraph @

tc/p_ip.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * p_ip.c packet editor: IPV4 header
4 *
5 * Authors: J Hadi Salim ([email protected])
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <string.h>
16 #include "utils.h"
17 #include "tc_util.h"
18 #include "m_pedit.h"
19
20 static int
21 parse_ip(int *argc_p, char ***argv_p,
22 struct m_pedit_sel *sel, struct m_pedit_key *tkey)
23 {
24 int res = -1;
25 int argc = *argc_p;
26 char **argv = *argv_p;
27
28 if (argc < 2)
29 return -1;
30
31 tkey->htype = sel->extended ?
32 TCA_PEDIT_KEY_EX_HDR_TYPE_IP4 :
33 TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
34
35 if (strcmp(*argv, "src") == 0) {
36 NEXT_ARG();
37 tkey->off = 12;
38 res = parse_cmd(&argc, &argv, 4, TIPV4, RU32, sel, tkey, 0);
39 goto done;
40 }
41 if (strcmp(*argv, "dst") == 0) {
42 NEXT_ARG();
43 tkey->off = 16;
44 res = parse_cmd(&argc, &argv, 4, TIPV4, RU32, sel, tkey, 0);
45 goto done;
46 }
47 /* jamal - look at these and make them either old or new
48 ** scheme given diffserv
49 ** don't forget the CE bit
50 */
51 if (strcmp(*argv, "tos") == 0 || matches(*argv, "dsfield") == 0) {
52 NEXT_ARG();
53 tkey->off = 1;
54 res = parse_cmd(&argc, &argv, 1, TU32, RU8, sel, tkey, 0);
55 goto done;
56 }
57 if (strcmp(*argv, "ihl") == 0) {
58 NEXT_ARG();
59 tkey->off = 0;
60 res = parse_cmd(&argc, &argv, 1, TU32, 0x0f, sel, tkey, 0);
61 goto done;
62 }
63 if (strcmp(*argv, "ttl") == 0) {
64 NEXT_ARG();
65 tkey->off = 8;
66 res = parse_cmd(&argc, &argv, 1, TU32, RU8, sel, tkey, PEDIT_ALLOW_DEC);
67 goto done;
68 }
69 if (strcmp(*argv, "protocol") == 0) {
70 NEXT_ARG();
71 tkey->off = 9;
72 res = parse_cmd(&argc, &argv, 1, TU32, RU8, sel, tkey, 0);
73 goto done;
74 }
75 /* jamal - fix this */
76 if (matches(*argv, "precedence") == 0) {
77 NEXT_ARG();
78 tkey->off = 1;
79 res = parse_cmd(&argc, &argv, 1, TU32, RU8, sel, tkey, 0);
80 goto done;
81 }
82 /* jamal - validate this at some point */
83 if (strcmp(*argv, "nofrag") == 0) {
84 NEXT_ARG();
85 tkey->off = 6;
86 res = parse_cmd(&argc, &argv, 1, TU32, 0x3F, sel, tkey, 0);
87 goto done;
88 }
89 /* jamal - validate this at some point */
90 if (strcmp(*argv, "firstfrag") == 0) {
91 NEXT_ARG();
92 tkey->off = 6;
93 res = parse_cmd(&argc, &argv, 1, TU32, 0x1F, sel, tkey, 0);
94 goto done;
95 }
96 if (strcmp(*argv, "ce") == 0) {
97 NEXT_ARG();
98 tkey->off = 6;
99 res = parse_cmd(&argc, &argv, 1, TU32, 0x80, sel, tkey, 0);
100 goto done;
101 }
102 if (strcmp(*argv, "df") == 0) {
103 NEXT_ARG();
104 tkey->off = 6;
105 res = parse_cmd(&argc, &argv, 1, TU32, 0x40, sel, tkey, 0);
106 goto done;
107 }
108 if (strcmp(*argv, "mf") == 0) {
109 NEXT_ARG();
110 tkey->off = 6;
111 res = parse_cmd(&argc, &argv, 1, TU32, 0x20, sel, tkey, 0);
112 goto done;
113 }
114
115 if (sel->extended)
116 return -1; /* fields located outside IP header should be
117 * addressed using the relevant header type in
118 * extended pedit kABI
119 */
120
121 if (strcmp(*argv, "dport") == 0) {
122 NEXT_ARG();
123 tkey->off = 22;
124 res = parse_cmd(&argc, &argv, 2, TU32, RU16, sel, tkey, 0);
125 goto done;
126 }
127 if (strcmp(*argv, "sport") == 0) {
128 NEXT_ARG();
129 tkey->off = 20;
130 res = parse_cmd(&argc, &argv, 2, TU32, RU16, sel, tkey, 0);
131 goto done;
132 }
133 if (strcmp(*argv, "icmp_type") == 0) {
134 NEXT_ARG();
135 tkey->off = 20;
136 res = parse_cmd(&argc, &argv, 1, TU32, RU8, sel, tkey, 0);
137 goto done;
138 }
139 if (strcmp(*argv, "icmp_code") == 0) {
140 NEXT_ARG();
141 tkey->off = 20;
142 res = parse_cmd(&argc, &argv, 1, TU32, RU8, sel, tkey, 0);
143 goto done;
144 }
145 return -1;
146
147 done:
148 *argc_p = argc;
149 *argv_p = argv;
150 return res;
151 }
152
153 struct m_pedit_util p_pedit_ip = {
154 .id = "ip",
155 .parse_peopt = parse_ip,
156 };