Commit 77bed404 authored by Phil Sutter's avatar Phil Sutter Committed by Stephen Hemminger

tc: pedit: Fix for big-endian systems

This was tricky to get right:
- The 'stride' value used for 8 and 16 bit values must behave inverse to
  the value's intra word offset to work correctly with big-endian data
  act_pedit is editing.
- The 'm' array's values are in host byte order, so they have to be
  converted as well (and the ordering was just inverse, for some
  reason).
- The only sane way of getting this right is to manipulate value/mask in
  host byte order and convert the output.
- TIPV4 (i.e. 'munge ip src/dst') had it's own pitfall: the address
  parser converts to network byte order automatically. This patch fixes
  this by converting it back before calling pack_key32, which is a hack
  but at least does not require to implement a completely separate code
  flow.
Signed-off-by: default avatarPhil Sutter <phil@nwl.cc>
Signed-off-by: default avatarStephen Hemminger <stephen@networkplumber.org>
parent 952f89de
...@@ -156,7 +156,7 @@ int ...@@ -156,7 +156,7 @@ int
pack_key16(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) pack_key16(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
{ {
int ind, stride; int ind, stride;
__u32 m[4] = {0xFFFF0000, 0xFF0000FF, 0x0000FFFF}; __u32 m[4] = {0x0000FFFF, 0xFF0000FF, 0xFFFF0000};
if (tkey->val > 0xFFFF || tkey->mask > 0xFFFF) { if (tkey->val > 0xFFFF || tkey->mask > 0xFFFF) {
fprintf(stderr, "pack_key16 bad value\n"); fprintf(stderr, "pack_key16 bad value\n");
...@@ -170,9 +170,9 @@ pack_key16(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) ...@@ -170,9 +170,9 @@ pack_key16(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
return -1; return -1;
} }
stride = 8 * ind; stride = 8 * (2 - ind);
tkey->val = htons(tkey->val & retain) << stride; tkey->val = htonl((tkey->val & retain) << stride);
tkey->mask = (htons(tkey->mask | ~retain) << stride) | m[ind]; tkey->mask = htonl(((tkey->mask | ~retain) << stride) | m[ind]);
tkey->off &= ~3; tkey->off &= ~3;
...@@ -186,7 +186,7 @@ int ...@@ -186,7 +186,7 @@ int
pack_key8(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) pack_key8(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
{ {
int ind, stride; int ind, stride;
__u32 m[4] = {0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, 0x00FFFFFF}; __u32 m[4] = {0x00FFFFFF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00};
if (tkey->val > 0xFF || tkey->mask > 0xFF) { if (tkey->val > 0xFF || tkey->mask > 0xFF) {
fprintf(stderr, "pack_key8 bad value (val %x mask %x\n", tkey->val, tkey->mask); fprintf(stderr, "pack_key8 bad value (val %x mask %x\n", tkey->val, tkey->mask);
...@@ -195,9 +195,9 @@ pack_key8(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) ...@@ -195,9 +195,9 @@ pack_key8(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
ind = tkey->off & 3; ind = tkey->off & 3;
stride = 8 * ind; stride = 8 * (3 - ind);
tkey->val = (tkey->val & retain) << stride; tkey->val = htonl((tkey->val & retain) << stride);
tkey->mask = ((tkey->mask | ~retain) << stride) | m[ind]; tkey->mask = htonl(((tkey->mask | ~retain) << stride) | m[ind]);
tkey->off &= ~3; tkey->off &= ~3;
...@@ -283,6 +283,9 @@ parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct ...@@ -283,6 +283,9 @@ parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct
tkey->val = val; tkey->val = val;
tkey->mask = mask; tkey->mask = mask;
if (type == TIPV4)
tkey->val = ntohl(tkey->val);
if (len == 1) { if (len == 1) {
res = pack_key8(retain, sel, tkey); res = pack_key8(retain, sel, tkey);
goto done; goto done;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment