2929static const unsigned int mctp_message_maxlen = 64 * 1024 ;
3030static const unsigned long mctp_key_lifetime = 6 * CONFIG_HZ ;
3131
32+ static void mctp_flow_prepare_output (struct sk_buff * skb , struct mctp_dev * dev );
33+
3234/* route output callbacks */
3335static int mctp_route_discard (struct mctp_route * route , struct sk_buff * skb )
3436{
@@ -152,8 +154,19 @@ static struct mctp_sk_key *mctp_key_alloc(struct mctp_sock *msk,
152154
153155void mctp_key_unref (struct mctp_sk_key * key )
154156{
155- if (refcount_dec_and_test (& key -> refs ))
156- kfree (key );
157+ unsigned long flags ;
158+
159+ if (!refcount_dec_and_test (& key -> refs ))
160+ return ;
161+
162+ /* even though no refs exist here, the lock allows us to stay
163+ * consistent with the locking requirement of mctp_dev_release_key
164+ */
165+ spin_lock_irqsave (& key -> lock , flags );
166+ mctp_dev_release_key (key -> dev , key );
167+ spin_unlock_irqrestore (& key -> lock , flags );
168+
169+ kfree (key );
157170}
158171
159172static int mctp_key_add (struct mctp_sk_key * key , struct mctp_sock * msk )
@@ -204,6 +217,7 @@ static void __mctp_key_unlock_drop(struct mctp_sk_key *key, struct net *net,
204217 key -> reasm_head = NULL ;
205218 key -> reasm_dead = true;
206219 key -> valid = false;
220+ mctp_dev_release_key (key -> dev , key );
207221 spin_unlock_irqrestore (& key -> lock , flags );
208222
209223 spin_lock_irqsave (& net -> mctp .keys_lock , flags );
@@ -222,6 +236,40 @@ static void __mctp_key_unlock_drop(struct mctp_sk_key *key, struct net *net,
222236
223237}
224238
239+ #ifdef CONFIG_MCTP_FLOWS
240+ static void mctp_skb_set_flow (struct sk_buff * skb , struct mctp_sk_key * key )
241+ {
242+ struct mctp_flow * flow ;
243+
244+ flow = skb_ext_add (skb , SKB_EXT_MCTP );
245+ if (!flow )
246+ return ;
247+
248+ refcount_inc (& key -> refs );
249+ flow -> key = key ;
250+ }
251+
252+ static void mctp_flow_prepare_output (struct sk_buff * skb , struct mctp_dev * dev )
253+ {
254+ struct mctp_sk_key * key ;
255+ struct mctp_flow * flow ;
256+
257+ flow = skb_ext_find (skb , SKB_EXT_MCTP );
258+ if (!flow )
259+ return ;
260+
261+ key = flow -> key ;
262+
263+ if (WARN_ON (key -> dev && key -> dev != dev ))
264+ return ;
265+
266+ mctp_dev_set_key (dev , key );
267+ }
268+ #else
269+ static void mctp_skb_set_flow (struct sk_buff * skb , struct mctp_sk_key * key ) {}
270+ static void mctp_flow_prepare_output (struct sk_buff * skb , struct mctp_dev * dev ) {}
271+ #endif
272+
225273static int mctp_frag_queue (struct mctp_sk_key * key , struct sk_buff * skb )
226274{
227275 struct mctp_hdr * hdr = mctp_hdr (skb );
@@ -465,6 +513,8 @@ static int mctp_route_output(struct mctp_route *route, struct sk_buff *skb)
465513 return - EHOSTUNREACH ;
466514 }
467515
516+ mctp_flow_prepare_output (skb , route -> dev );
517+
468518 rc = dev_queue_xmit (skb );
469519 if (rc )
470520 rc = net_xmit_errno (rc );
@@ -803,6 +853,7 @@ int mctp_local_output(struct sock *sk, struct mctp_route *rt,
803853 rc = PTR_ERR (key );
804854 goto out_release ;
805855 }
856+ mctp_skb_set_flow (skb , key );
806857 /* done with the key in this scope */
807858 mctp_key_unref (key );
808859 tag |= MCTP_HDR_FLAG_TO ;
0 commit comments