Skip to content

Commit 90e8efb

Browse files
committed
....
1 parent 97b0b80 commit 90e8efb

3 files changed

Lines changed: 72 additions & 6 deletions

File tree

ipthelper/xshared.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ int command_default(struct iptables_command_state *cs,
248248
m->x6_options,
249249
&m->option_offset);
250250
else
251-
gl->opts = xtables_merge_options(gl->orig_opts,
252-
gl->opts,
253-
m->extra_opts,
254-
&m->option_offset);
251+
gl->opts = xs_merge_options(gl->orig_opts,
252+
gl->opts,
253+
m->extra_opts,
254+
&m->option_offset);
255255
if (gl->opts == NULL)
256256
xtables_error(OTHER_PROBLEM, "can't alloc memory!");
257257
xs_validate_new_longopts(gl->opts, merge_start,
@@ -661,7 +661,7 @@ void command_match(struct iptables_command_state *cs)
661661
m->x6_options, &m->option_offset);
662662
merged = true;
663663
} else if (m->extra_opts != NULL) {
664-
opts = xtables_merge_options(xt_params->orig_opts, opts,
664+
opts = xs_merge_options(xt_params->orig_opts, opts,
665665
m->extra_opts, &m->option_offset);
666666
merged = true;
667667
}
@@ -735,7 +735,7 @@ void command_jump(struct iptables_command_state *cs)
735735
&cs->target->option_offset);
736736
merged = true;
737737
} else if (cs->target->extra_opts != NULL) {
738-
opts = xtables_merge_options(xt_params->orig_opts, opts,
738+
opts = xs_merge_options(xt_params->orig_opts, opts,
739739
cs->target->extra_opts,
740740
&cs->target->option_offset);
741741
merged = true;

ipthelper/xshared.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,9 @@ void print_ifaces(const char *iniface, const char *outiface, uint8_t invflags,
179179
void command_match(struct iptables_command_state *cs);
180180
const char *xt_parse_target(const char *targetname);
181181
void command_jump(struct iptables_command_state *cs);
182+
struct option *xs_merge_options(struct option *orig_opts,
183+
struct option *oldopts,
184+
const struct option *newopts,
185+
unsigned int *option_offset);
182186

183187
#endif /* IPTABLES_XSHARED_H */

ipthelper/xtoptions.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,68 @@ xtables_options_xfrm(struct option *orig_opts, struct option *oldopts,
128128
return merge;
129129
}
130130

131+
struct option *
132+
xs_merge_options(struct option *orig_opts, struct option *oldopts,
133+
const struct option *newopts, unsigned int *option_offset)
134+
{
135+
unsigned int num_orig = 0, num_old = 0, num_new = 0, i;
136+
struct option *merge;
137+
struct option *mp;
138+
struct option *oldbase = oldopts;
139+
140+
if (newopts == NULL || newopts[0].name == NULL)
141+
return oldopts;
142+
143+
if (orig_opts != NULL)
144+
for (; orig_opts[num_orig].name != NULL; ++num_orig)
145+
;
146+
147+
if (oldopts != NULL)
148+
for (num_old = 0; oldopts[num_old].name != NULL; ++num_old)
149+
;
150+
151+
for (; newopts[num_new].name != NULL; ++num_new)
152+
;
153+
154+
if (oldopts != NULL && num_old >= num_orig) {
155+
oldopts += num_orig;
156+
num_old -= num_orig;
157+
} else {
158+
oldopts = NULL;
159+
num_old = 0;
160+
}
161+
162+
merge = malloc(sizeof(*merge) * (num_orig + num_new + num_old + 1));
163+
if (merge == NULL)
164+
return NULL;
165+
166+
if (num_orig != 0)
167+
memcpy(merge, orig_opts, sizeof(*merge) * num_orig);
168+
mp = merge + num_orig;
169+
170+
xt_params->option_offset += XT_OPTION_OFFSET_SCALE;
171+
*option_offset = xt_params->option_offset;
172+
173+
for (i = 0; i < num_new; ++i, ++mp) {
174+
mp->name = newopts[i].name;
175+
mp->has_arg = newopts[i].has_arg;
176+
mp->flag = newopts[i].flag;
177+
mp->val = newopts[i].val + *option_offset;
178+
}
179+
180+
if (oldopts != NULL && num_old != 0) {
181+
memcpy(mp, oldopts, sizeof(*mp) * num_old);
182+
mp += num_old;
183+
}
184+
185+
memset(mp, 0, sizeof(*mp));
186+
187+
if (oldbase != NULL && oldbase != xt_params->orig_opts)
188+
free(oldbase);
189+
190+
return merge;
191+
}
192+
131193
/**
132194
* Give the upper limit for a certain type.
133195
*/

0 commit comments

Comments
 (0)