Skip to content

Commit dd895d7

Browse files
hartkoppmarckleinebudde
authored andcommitted
can: cangw: introduce optional uid to reference created routing jobs
Similar to referencing iptables rules by their line number this UID allows to reference created routing jobs, e.g. to alter configured data modifications. The UID is an optional non-zero value which can be provided at routing job creation time. When the UID is set the UID replaces the data modification configuration as job identification attribute e.g. at job removal time. Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent 3d5db5e commit dd895d7

2 files changed

Lines changed: 61 additions & 12 deletions

File tree

‎include/uapi/linux/can/gw.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ enum {
7878
CGW_FILTER, /* specify struct can_filter on source CAN device */
7979
CGW_DELETED, /* number of deleted CAN frames (see max_hops param) */
8080
CGW_LIM_HOPS, /* limit the number of hops of this specific rule */
81+
CGW_MOD_UID, /* user defined identifier for modification updates */
8182
__CGW_MAX
8283
};
8384

@@ -162,6 +163,10 @@ enum {
162163
* load time of the can-gw module). This value is used to reduce the number of
163164
* possible hops for this gateway rule to a value smaller then max_hops.
164165
*
166+
* CGW_MOD_UID (length 4 bytes):
167+
* Optional non-zero user defined routing job identifier to alter existing
168+
* modification settings at runtime.
169+
*
165170
* CGW_CS_XOR (length 4 bytes):
166171
* Set a simple XOR checksum starting with an initial value into
167172
* data[result-idx] using data[start-idx] .. data[end-idx]

‎net/can/gw.c‎

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ struct cf_mod {
110110
void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
111111
void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
112112
} csumfunc;
113+
u32 uid;
113114
};
114115

115116

@@ -548,6 +549,11 @@ static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
548549
goto cancel;
549550
}
550551

552+
if (gwj->mod.uid) {
553+
if (nla_put_u32(skb, CGW_MOD_UID, gwj->mod.uid) < 0)
554+
goto cancel;
555+
}
556+
551557
if (gwj->mod.csumfunc.crc8) {
552558
if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
553559
&gwj->mod.csum.crc8) < 0)
@@ -619,6 +625,7 @@ static const struct nla_policy cgw_policy[CGW_MAX+1] = {
619625
[CGW_DST_IF] = { .type = NLA_U32 },
620626
[CGW_FILTER] = { .len = sizeof(struct can_filter) },
621627
[CGW_LIM_HOPS] = { .type = NLA_U8 },
628+
[CGW_MOD_UID] = { .type = NLA_U32 },
622629
};
623630

624631
/* check for common and gwtype specific attributes */
@@ -761,6 +768,10 @@ static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
761768
else
762769
mod->csumfunc.xor = cgw_csum_xor_neg;
763770
}
771+
772+
if (tb[CGW_MOD_UID]) {
773+
nla_memcpy(&mod->uid, tb[CGW_MOD_UID], sizeof(u32));
774+
}
764775
}
765776

766777
if (gwtype == CGW_TYPE_CAN_CAN) {
@@ -802,6 +813,8 @@ static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh)
802813
{
803814
struct rtcanmsg *r;
804815
struct cgw_job *gwj;
816+
struct cf_mod mod;
817+
struct can_can_gw ccgw;
805818
u8 limhops = 0;
806819
int err = 0;
807820

@@ -819,6 +832,36 @@ static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh)
819832
if (r->gwtype != CGW_TYPE_CAN_CAN)
820833
return -EINVAL;
821834

835+
err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
836+
if (err < 0)
837+
return err;
838+
839+
if (mod.uid) {
840+
841+
ASSERT_RTNL();
842+
843+
/* check for updating an existing job with identical uid */
844+
hlist_for_each_entry(gwj, &cgw_list, list) {
845+
846+
if (gwj->mod.uid != mod.uid)
847+
continue;
848+
849+
/* interfaces & filters must be identical */
850+
if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
851+
return -EINVAL;
852+
853+
/* update modifications with disabled softirq & quit */
854+
local_bh_disable();
855+
memcpy(&gwj->mod, &mod, sizeof(mod));
856+
local_bh_enable();
857+
return 0;
858+
}
859+
}
860+
861+
/* ifindex == 0 is not allowed for job creation */
862+
if (!ccgw.src_idx || !ccgw.dst_idx)
863+
return -ENODEV;
864+
822865
gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
823866
if (!gwj)
824867
return -ENOMEM;
@@ -828,18 +871,14 @@ static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh)
828871
gwj->deleted_frames = 0;
829872
gwj->flags = r->flags;
830873
gwj->gwtype = r->gwtype;
874+
gwj->limit_hops = limhops;
831875

832-
err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw,
833-
&limhops);
834-
if (err < 0)
835-
goto out;
876+
/* insert already parsed information */
877+
memcpy(&gwj->mod, &mod, sizeof(mod));
878+
memcpy(&gwj->ccgw, &ccgw, sizeof(ccgw));
836879

837880
err = -ENODEV;
838881

839-
/* ifindex == 0 is not allowed for job creation */
840-
if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
841-
goto out;
842-
843882
gwj->src.dev = __dev_get_by_index(&init_net, gwj->ccgw.src_idx);
844883

845884
if (!gwj->src.dev)
@@ -856,8 +895,6 @@ static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh)
856895
if (gwj->dst.dev->type != ARPHRD_CAN)
857896
goto out;
858897

859-
gwj->limit_hops = limhops;
860-
861898
ASSERT_RTNL();
862899

863900
err = cgw_register_filter(gwj);
@@ -931,8 +968,15 @@ static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh)
931968
if (gwj->limit_hops != limhops)
932969
continue;
933970

934-
if (memcmp(&gwj->mod, &mod, sizeof(mod)))
935-
continue;
971+
/* we have a match when uid is enabled and identical */
972+
if (gwj->mod.uid || mod.uid) {
973+
if (gwj->mod.uid != mod.uid)
974+
continue;
975+
} else {
976+
/* no uid => check for identical modifications */
977+
if (memcmp(&gwj->mod, &mod, sizeof(mod)))
978+
continue;
979+
}
936980

937981
/* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
938982
if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))

0 commit comments

Comments
 (0)