xref: /openbmc/linux/net/can/j1939/socket.c (revision fd5e9fccbd504c5179ab57ff695c610bca8809d6)
19d71dd0cSThe j1939 authors // SPDX-License-Identifier: GPL-2.0
29d71dd0cSThe j1939 authors // Copyright (c) 2010-2011 EIA Electronics,
39d71dd0cSThe j1939 authors //                         Pieter Beyens <pieter.beyens@eia.be>
49d71dd0cSThe j1939 authors // Copyright (c) 2010-2011 EIA Electronics,
59d71dd0cSThe j1939 authors //                         Kurt Van Dijck <kurt.van.dijck@eia.be>
69d71dd0cSThe j1939 authors // Copyright (c) 2018 Protonic,
79d71dd0cSThe j1939 authors //                         Robin van der Gracht <robin@protonic.nl>
89d71dd0cSThe j1939 authors // Copyright (c) 2017-2019 Pengutronix,
99d71dd0cSThe j1939 authors //                         Marc Kleine-Budde <kernel@pengutronix.de>
109d71dd0cSThe j1939 authors // Copyright (c) 2017-2019 Pengutronix,
119d71dd0cSThe j1939 authors //                         Oleksij Rempel <kernel@pengutronix.de>
129d71dd0cSThe j1939 authors 
139d71dd0cSThe j1939 authors #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
149d71dd0cSThe j1939 authors 
154e096a18SOleksij Rempel #include <linux/can/can-ml.h>
169d71dd0cSThe j1939 authors #include <linux/can/core.h>
179d71dd0cSThe j1939 authors #include <linux/can/skb.h>
189d71dd0cSThe j1939 authors #include <linux/errqueue.h>
199d71dd0cSThe j1939 authors #include <linux/if_arp.h>
209d71dd0cSThe j1939 authors 
219d71dd0cSThe j1939 authors #include "j1939-priv.h"
229d71dd0cSThe j1939 authors 
239d71dd0cSThe j1939 authors #define J1939_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_addr.j1939)
249d71dd0cSThe j1939 authors 
259d71dd0cSThe j1939 authors /* conversion function between struct sock::sk_priority from linux and
269d71dd0cSThe j1939 authors  * j1939 priority field
279d71dd0cSThe j1939 authors  */
j1939_prio(u32 sk_priority)289d71dd0cSThe j1939 authors static inline priority_t j1939_prio(u32 sk_priority)
299d71dd0cSThe j1939 authors {
309d71dd0cSThe j1939 authors 	sk_priority = min(sk_priority, 7U);
319d71dd0cSThe j1939 authors 
329d71dd0cSThe j1939 authors 	return 7 - sk_priority;
339d71dd0cSThe j1939 authors }
349d71dd0cSThe j1939 authors 
j1939_to_sk_priority(priority_t prio)359d71dd0cSThe j1939 authors static inline u32 j1939_to_sk_priority(priority_t prio)
369d71dd0cSThe j1939 authors {
379d71dd0cSThe j1939 authors 	return 7 - prio;
389d71dd0cSThe j1939 authors }
399d71dd0cSThe j1939 authors 
409d71dd0cSThe j1939 authors /* function to see if pgn is to be evaluated */
j1939_pgn_is_valid(pgn_t pgn)419d71dd0cSThe j1939 authors static inline bool j1939_pgn_is_valid(pgn_t pgn)
429d71dd0cSThe j1939 authors {
439d71dd0cSThe j1939 authors 	return pgn <= J1939_PGN_MAX;
449d71dd0cSThe j1939 authors }
459d71dd0cSThe j1939 authors 
469d71dd0cSThe j1939 authors /* test function to avoid non-zero DA placeholder for pdu1 pgn's */
j1939_pgn_is_clean_pdu(pgn_t pgn)479d71dd0cSThe j1939 authors static inline bool j1939_pgn_is_clean_pdu(pgn_t pgn)
489d71dd0cSThe j1939 authors {
499d71dd0cSThe j1939 authors 	if (j1939_pgn_is_pdu1(pgn))
509d71dd0cSThe j1939 authors 		return !(pgn & 0xff);
519d71dd0cSThe j1939 authors 	else
529d71dd0cSThe j1939 authors 		return true;
539d71dd0cSThe j1939 authors }
549d71dd0cSThe j1939 authors 
j1939_sock_pending_add(struct sock * sk)559d71dd0cSThe j1939 authors static inline void j1939_sock_pending_add(struct sock *sk)
569d71dd0cSThe j1939 authors {
579d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sk);
589d71dd0cSThe j1939 authors 
599d71dd0cSThe j1939 authors 	atomic_inc(&jsk->skb_pending);
609d71dd0cSThe j1939 authors }
619d71dd0cSThe j1939 authors 
j1939_sock_pending_get(struct sock * sk)629d71dd0cSThe j1939 authors static int j1939_sock_pending_get(struct sock *sk)
639d71dd0cSThe j1939 authors {
649d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sk);
659d71dd0cSThe j1939 authors 
669d71dd0cSThe j1939 authors 	return atomic_read(&jsk->skb_pending);
679d71dd0cSThe j1939 authors }
689d71dd0cSThe j1939 authors 
j1939_sock_pending_del(struct sock * sk)699d71dd0cSThe j1939 authors void j1939_sock_pending_del(struct sock *sk)
709d71dd0cSThe j1939 authors {
719d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sk);
729d71dd0cSThe j1939 authors 
739d71dd0cSThe j1939 authors 	/* atomic_dec_return returns the new value */
749d71dd0cSThe j1939 authors 	if (!atomic_dec_return(&jsk->skb_pending))
759d71dd0cSThe j1939 authors 		wake_up(&jsk->waitq);	/* no pending SKB's */
769d71dd0cSThe j1939 authors }
779d71dd0cSThe j1939 authors 
j1939_jsk_add(struct j1939_priv * priv,struct j1939_sock * jsk)789d71dd0cSThe j1939 authors static void j1939_jsk_add(struct j1939_priv *priv, struct j1939_sock *jsk)
799d71dd0cSThe j1939 authors {
809d71dd0cSThe j1939 authors 	jsk->state |= J1939_SOCK_BOUND;
819d71dd0cSThe j1939 authors 	j1939_priv_get(priv);
829d71dd0cSThe j1939 authors 
8326dfe112SZiqi Zhao 	write_lock_bh(&priv->j1939_socks_lock);
849d71dd0cSThe j1939 authors 	list_add_tail(&jsk->list, &priv->j1939_socks);
8526dfe112SZiqi Zhao 	write_unlock_bh(&priv->j1939_socks_lock);
869d71dd0cSThe j1939 authors }
879d71dd0cSThe j1939 authors 
j1939_jsk_del(struct j1939_priv * priv,struct j1939_sock * jsk)889d71dd0cSThe j1939 authors static void j1939_jsk_del(struct j1939_priv *priv, struct j1939_sock *jsk)
899d71dd0cSThe j1939 authors {
9026dfe112SZiqi Zhao 	write_lock_bh(&priv->j1939_socks_lock);
919d71dd0cSThe j1939 authors 	list_del_init(&jsk->list);
9226dfe112SZiqi Zhao 	write_unlock_bh(&priv->j1939_socks_lock);
939d71dd0cSThe j1939 authors 
949d71dd0cSThe j1939 authors 	j1939_priv_put(priv);
959d71dd0cSThe j1939 authors 	jsk->state &= ~J1939_SOCK_BOUND;
969d71dd0cSThe j1939 authors }
979d71dd0cSThe j1939 authors 
j1939_sk_queue_session(struct j1939_session * session)989d71dd0cSThe j1939 authors static bool j1939_sk_queue_session(struct j1939_session *session)
999d71dd0cSThe j1939 authors {
1009d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(session->sk);
1019d71dd0cSThe j1939 authors 	bool empty;
1029d71dd0cSThe j1939 authors 
1039d71dd0cSThe j1939 authors 	spin_lock_bh(&jsk->sk_session_queue_lock);
1049d71dd0cSThe j1939 authors 	empty = list_empty(&jsk->sk_session_queue);
1059d71dd0cSThe j1939 authors 	j1939_session_get(session);
1069d71dd0cSThe j1939 authors 	list_add_tail(&session->sk_session_queue_entry, &jsk->sk_session_queue);
1079d71dd0cSThe j1939 authors 	spin_unlock_bh(&jsk->sk_session_queue_lock);
1089d71dd0cSThe j1939 authors 	j1939_sock_pending_add(&jsk->sk);
1099d71dd0cSThe j1939 authors 
1109d71dd0cSThe j1939 authors 	return empty;
1119d71dd0cSThe j1939 authors }
1129d71dd0cSThe j1939 authors 
1139d71dd0cSThe j1939 authors static struct
j1939_sk_get_incomplete_session(struct j1939_sock * jsk)1149d71dd0cSThe j1939 authors j1939_session *j1939_sk_get_incomplete_session(struct j1939_sock *jsk)
1159d71dd0cSThe j1939 authors {
1169d71dd0cSThe j1939 authors 	struct j1939_session *session = NULL;
1179d71dd0cSThe j1939 authors 
1189d71dd0cSThe j1939 authors 	spin_lock_bh(&jsk->sk_session_queue_lock);
1199d71dd0cSThe j1939 authors 	if (!list_empty(&jsk->sk_session_queue)) {
1209d71dd0cSThe j1939 authors 		session = list_last_entry(&jsk->sk_session_queue,
1219d71dd0cSThe j1939 authors 					  struct j1939_session,
1229d71dd0cSThe j1939 authors 					  sk_session_queue_entry);
1239d71dd0cSThe j1939 authors 		if (session->total_queued_size == session->total_message_size)
1249d71dd0cSThe j1939 authors 			session = NULL;
1259d71dd0cSThe j1939 authors 		else
1269d71dd0cSThe j1939 authors 			j1939_session_get(session);
1279d71dd0cSThe j1939 authors 	}
1289d71dd0cSThe j1939 authors 	spin_unlock_bh(&jsk->sk_session_queue_lock);
1299d71dd0cSThe j1939 authors 
1309d71dd0cSThe j1939 authors 	return session;
1319d71dd0cSThe j1939 authors }
1329d71dd0cSThe j1939 authors 
j1939_sk_queue_drop_all(struct j1939_priv * priv,struct j1939_sock * jsk,int err)1339d71dd0cSThe j1939 authors static void j1939_sk_queue_drop_all(struct j1939_priv *priv,
1349d71dd0cSThe j1939 authors 				    struct j1939_sock *jsk, int err)
1359d71dd0cSThe j1939 authors {
1369d71dd0cSThe j1939 authors 	struct j1939_session *session, *tmp;
1379d71dd0cSThe j1939 authors 
1389d71dd0cSThe j1939 authors 	netdev_dbg(priv->ndev, "%s: err: %i\n", __func__, err);
1399d71dd0cSThe j1939 authors 	spin_lock_bh(&jsk->sk_session_queue_lock);
1409d71dd0cSThe j1939 authors 	list_for_each_entry_safe(session, tmp, &jsk->sk_session_queue,
1419d71dd0cSThe j1939 authors 				 sk_session_queue_entry) {
1429d71dd0cSThe j1939 authors 		list_del_init(&session->sk_session_queue_entry);
1439d71dd0cSThe j1939 authors 		session->err = err;
1449d71dd0cSThe j1939 authors 		j1939_session_put(session);
1459d71dd0cSThe j1939 authors 	}
1469d71dd0cSThe j1939 authors 	spin_unlock_bh(&jsk->sk_session_queue_lock);
1479d71dd0cSThe j1939 authors }
1489d71dd0cSThe j1939 authors 
j1939_sk_queue_activate_next_locked(struct j1939_session * session)1499d71dd0cSThe j1939 authors static void j1939_sk_queue_activate_next_locked(struct j1939_session *session)
1509d71dd0cSThe j1939 authors {
1519d71dd0cSThe j1939 authors 	struct j1939_sock *jsk;
1529d71dd0cSThe j1939 authors 	struct j1939_session *first;
1539d71dd0cSThe j1939 authors 	int err;
1549d71dd0cSThe j1939 authors 
1559d71dd0cSThe j1939 authors 	/* RX-Session don't have a socket (yet) */
1569d71dd0cSThe j1939 authors 	if (!session->sk)
1579d71dd0cSThe j1939 authors 		return;
1589d71dd0cSThe j1939 authors 
1599d71dd0cSThe j1939 authors 	jsk = j1939_sk(session->sk);
1609d71dd0cSThe j1939 authors 	lockdep_assert_held(&jsk->sk_session_queue_lock);
1619d71dd0cSThe j1939 authors 
1629d71dd0cSThe j1939 authors 	err = session->err;
1639d71dd0cSThe j1939 authors 
1649d71dd0cSThe j1939 authors 	first = list_first_entry_or_null(&jsk->sk_session_queue,
1659d71dd0cSThe j1939 authors 					 struct j1939_session,
1669d71dd0cSThe j1939 authors 					 sk_session_queue_entry);
1679d71dd0cSThe j1939 authors 
1689d71dd0cSThe j1939 authors 	/* Some else has already activated the next session */
1699d71dd0cSThe j1939 authors 	if (first != session)
1709d71dd0cSThe j1939 authors 		return;
1719d71dd0cSThe j1939 authors 
1729d71dd0cSThe j1939 authors activate_next:
1739d71dd0cSThe j1939 authors 	list_del_init(&first->sk_session_queue_entry);
1749d71dd0cSThe j1939 authors 	j1939_session_put(first);
1759d71dd0cSThe j1939 authors 	first = list_first_entry_or_null(&jsk->sk_session_queue,
1769d71dd0cSThe j1939 authors 					 struct j1939_session,
1779d71dd0cSThe j1939 authors 					 sk_session_queue_entry);
1789d71dd0cSThe j1939 authors 	if (!first)
1799d71dd0cSThe j1939 authors 		return;
1809d71dd0cSThe j1939 authors 
1818ef49f7fSFedor Pchelkin 	if (j1939_session_activate(first)) {
1828ef49f7fSFedor Pchelkin 		netdev_warn_once(first->priv->ndev,
1838ef49f7fSFedor Pchelkin 				 "%s: 0x%p: Identical session is already activated.\n",
1848ef49f7fSFedor Pchelkin 				 __func__, first);
1859d71dd0cSThe j1939 authors 		first->err = -EBUSY;
1869d71dd0cSThe j1939 authors 		goto activate_next;
1879d71dd0cSThe j1939 authors 	} else {
1889d71dd0cSThe j1939 authors 		/* Give receiver some time (arbitrary chosen) to recover */
1899d71dd0cSThe j1939 authors 		int time_ms = 0;
1909d71dd0cSThe j1939 authors 
1919d71dd0cSThe j1939 authors 		if (err)
1928032bf12SJason A. Donenfeld 			time_ms = 10 + get_random_u32_below(16);
1939d71dd0cSThe j1939 authors 
1949d71dd0cSThe j1939 authors 		j1939_tp_schedule_txtimer(first, time_ms);
1959d71dd0cSThe j1939 authors 	}
1969d71dd0cSThe j1939 authors }
1979d71dd0cSThe j1939 authors 
j1939_sk_queue_activate_next(struct j1939_session * session)1989d71dd0cSThe j1939 authors void j1939_sk_queue_activate_next(struct j1939_session *session)
1999d71dd0cSThe j1939 authors {
2009d71dd0cSThe j1939 authors 	struct j1939_sock *jsk;
2019d71dd0cSThe j1939 authors 
2029d71dd0cSThe j1939 authors 	if (!session->sk)
2039d71dd0cSThe j1939 authors 		return;
2049d71dd0cSThe j1939 authors 
2059d71dd0cSThe j1939 authors 	jsk = j1939_sk(session->sk);
2069d71dd0cSThe j1939 authors 
2079d71dd0cSThe j1939 authors 	spin_lock_bh(&jsk->sk_session_queue_lock);
2089d71dd0cSThe j1939 authors 	j1939_sk_queue_activate_next_locked(session);
2099d71dd0cSThe j1939 authors 	spin_unlock_bh(&jsk->sk_session_queue_lock);
2109d71dd0cSThe j1939 authors }
2119d71dd0cSThe j1939 authors 
j1939_sk_match_dst(struct j1939_sock * jsk,const struct j1939_sk_buff_cb * skcb)2129d71dd0cSThe j1939 authors static bool j1939_sk_match_dst(struct j1939_sock *jsk,
2139d71dd0cSThe j1939 authors 			       const struct j1939_sk_buff_cb *skcb)
2149d71dd0cSThe j1939 authors {
2159d71dd0cSThe j1939 authors 	if ((jsk->state & J1939_SOCK_PROMISC))
2169d71dd0cSThe j1939 authors 		return true;
2179d71dd0cSThe j1939 authors 
2189d71dd0cSThe j1939 authors 	/* Destination address filter */
2199d71dd0cSThe j1939 authors 	if (jsk->addr.src_name && skcb->addr.dst_name) {
2209d71dd0cSThe j1939 authors 		if (jsk->addr.src_name != skcb->addr.dst_name)
2219d71dd0cSThe j1939 authors 			return false;
2229d71dd0cSThe j1939 authors 	} else {
2239d71dd0cSThe j1939 authors 		/* receive (all sockets) if
2249d71dd0cSThe j1939 authors 		 * - all packages that match our bind() address
2259d71dd0cSThe j1939 authors 		 * - all broadcast on a socket if SO_BROADCAST
2269d71dd0cSThe j1939 authors 		 *   is set
2279d71dd0cSThe j1939 authors 		 */
2289d71dd0cSThe j1939 authors 		if (j1939_address_is_unicast(skcb->addr.da)) {
2299d71dd0cSThe j1939 authors 			if (jsk->addr.sa != skcb->addr.da)
2309d71dd0cSThe j1939 authors 				return false;
2319d71dd0cSThe j1939 authors 		} else if (!sock_flag(&jsk->sk, SOCK_BROADCAST)) {
2329d71dd0cSThe j1939 authors 			/* receiving broadcast without SO_BROADCAST
2339d71dd0cSThe j1939 authors 			 * flag is not allowed
2349d71dd0cSThe j1939 authors 			 */
2359d71dd0cSThe j1939 authors 			return false;
2369d71dd0cSThe j1939 authors 		}
2379d71dd0cSThe j1939 authors 	}
2389d71dd0cSThe j1939 authors 
2399d71dd0cSThe j1939 authors 	/* Source address filter */
2409d71dd0cSThe j1939 authors 	if (jsk->state & J1939_SOCK_CONNECTED) {
2419d71dd0cSThe j1939 authors 		/* receive (all sockets) if
2429d71dd0cSThe j1939 authors 		 * - all packages that match our connect() name or address
2439d71dd0cSThe j1939 authors 		 */
2449d71dd0cSThe j1939 authors 		if (jsk->addr.dst_name && skcb->addr.src_name) {
2459d71dd0cSThe j1939 authors 			if (jsk->addr.dst_name != skcb->addr.src_name)
2469d71dd0cSThe j1939 authors 				return false;
2479d71dd0cSThe j1939 authors 		} else {
2489d71dd0cSThe j1939 authors 			if (jsk->addr.da != skcb->addr.sa)
2499d71dd0cSThe j1939 authors 				return false;
2509d71dd0cSThe j1939 authors 		}
2519d71dd0cSThe j1939 authors 	}
2529d71dd0cSThe j1939 authors 
2539d71dd0cSThe j1939 authors 	/* PGN filter */
2549d71dd0cSThe j1939 authors 	if (j1939_pgn_is_valid(jsk->pgn_rx_filter) &&
2559d71dd0cSThe j1939 authors 	    jsk->pgn_rx_filter != skcb->addr.pgn)
2569d71dd0cSThe j1939 authors 		return false;
2579d71dd0cSThe j1939 authors 
2589d71dd0cSThe j1939 authors 	return true;
2599d71dd0cSThe j1939 authors }
2609d71dd0cSThe j1939 authors 
2619d71dd0cSThe j1939 authors /* matches skb control buffer (addr) with a j1939 filter */
j1939_sk_match_filter(struct j1939_sock * jsk,const struct j1939_sk_buff_cb * skcb)2629d71dd0cSThe j1939 authors static bool j1939_sk_match_filter(struct j1939_sock *jsk,
2639d71dd0cSThe j1939 authors 				  const struct j1939_sk_buff_cb *skcb)
2649d71dd0cSThe j1939 authors {
265f84e7534SOleksij Rempel 	const struct j1939_filter *f;
266f84e7534SOleksij Rempel 	int nfilter;
267f84e7534SOleksij Rempel 
268f84e7534SOleksij Rempel 	spin_lock_bh(&jsk->filters_lock);
269f84e7534SOleksij Rempel 
270f84e7534SOleksij Rempel 	f = jsk->filters;
271f84e7534SOleksij Rempel 	nfilter = jsk->nfilters;
2729d71dd0cSThe j1939 authors 
2739d71dd0cSThe j1939 authors 	if (!nfilter)
2749d71dd0cSThe j1939 authors 		/* receive all when no filters are assigned */
275f84e7534SOleksij Rempel 		goto filter_match_found;
2769d71dd0cSThe j1939 authors 
2779d71dd0cSThe j1939 authors 	for (; nfilter; ++f, --nfilter) {
2789d71dd0cSThe j1939 authors 		if ((skcb->addr.pgn & f->pgn_mask) != f->pgn)
2799d71dd0cSThe j1939 authors 			continue;
2809d71dd0cSThe j1939 authors 		if ((skcb->addr.sa & f->addr_mask) != f->addr)
2819d71dd0cSThe j1939 authors 			continue;
2829d71dd0cSThe j1939 authors 		if ((skcb->addr.src_name & f->name_mask) != f->name)
2839d71dd0cSThe j1939 authors 			continue;
284f84e7534SOleksij Rempel 		goto filter_match_found;
2859d71dd0cSThe j1939 authors 	}
286f84e7534SOleksij Rempel 
287f84e7534SOleksij Rempel 	spin_unlock_bh(&jsk->filters_lock);
2889d71dd0cSThe j1939 authors 	return false;
289f84e7534SOleksij Rempel 
290f84e7534SOleksij Rempel filter_match_found:
291f84e7534SOleksij Rempel 	spin_unlock_bh(&jsk->filters_lock);
292f84e7534SOleksij Rempel 	return true;
2939d71dd0cSThe j1939 authors }
2949d71dd0cSThe j1939 authors 
j1939_sk_recv_match_one(struct j1939_sock * jsk,const struct j1939_sk_buff_cb * skcb)2959d71dd0cSThe j1939 authors static bool j1939_sk_recv_match_one(struct j1939_sock *jsk,
2969d71dd0cSThe j1939 authors 				    const struct j1939_sk_buff_cb *skcb)
2979d71dd0cSThe j1939 authors {
2989d71dd0cSThe j1939 authors 	if (!(jsk->state & J1939_SOCK_BOUND))
2999d71dd0cSThe j1939 authors 		return false;
3009d71dd0cSThe j1939 authors 
3019d71dd0cSThe j1939 authors 	if (!j1939_sk_match_dst(jsk, skcb))
3029d71dd0cSThe j1939 authors 		return false;
3039d71dd0cSThe j1939 authors 
3049d71dd0cSThe j1939 authors 	if (!j1939_sk_match_filter(jsk, skcb))
3059d71dd0cSThe j1939 authors 		return false;
3069d71dd0cSThe j1939 authors 
3079d71dd0cSThe j1939 authors 	return true;
3089d71dd0cSThe j1939 authors }
3099d71dd0cSThe j1939 authors 
j1939_sk_recv_one(struct j1939_sock * jsk,struct sk_buff * oskb)3109d71dd0cSThe j1939 authors static void j1939_sk_recv_one(struct j1939_sock *jsk, struct sk_buff *oskb)
3119d71dd0cSThe j1939 authors {
3129d71dd0cSThe j1939 authors 	const struct j1939_sk_buff_cb *oskcb = j1939_skb_to_cb(oskb);
3139d71dd0cSThe j1939 authors 	struct j1939_sk_buff_cb *skcb;
3149d71dd0cSThe j1939 authors 	struct sk_buff *skb;
3159d71dd0cSThe j1939 authors 
3169d71dd0cSThe j1939 authors 	if (oskb->sk == &jsk->sk)
3179d71dd0cSThe j1939 authors 		return;
3189d71dd0cSThe j1939 authors 
3199d71dd0cSThe j1939 authors 	if (!j1939_sk_recv_match_one(jsk, oskcb))
3209d71dd0cSThe j1939 authors 		return;
3219d71dd0cSThe j1939 authors 
3229d71dd0cSThe j1939 authors 	skb = skb_clone(oskb, GFP_ATOMIC);
3239d71dd0cSThe j1939 authors 	if (!skb) {
3249d71dd0cSThe j1939 authors 		pr_warn("skb clone failed\n");
3259d71dd0cSThe j1939 authors 		return;
3269d71dd0cSThe j1939 authors 	}
3279d71dd0cSThe j1939 authors 	can_skb_set_owner(skb, oskb->sk);
3289d71dd0cSThe j1939 authors 
3299d71dd0cSThe j1939 authors 	skcb = j1939_skb_to_cb(skb);
3309d71dd0cSThe j1939 authors 	skcb->msg_flags &= ~(MSG_DONTROUTE);
3319d71dd0cSThe j1939 authors 	if (skb->sk)
3329d71dd0cSThe j1939 authors 		skcb->msg_flags |= MSG_DONTROUTE;
3339d71dd0cSThe j1939 authors 
3349d71dd0cSThe j1939 authors 	if (sock_queue_rcv_skb(&jsk->sk, skb) < 0)
3359d71dd0cSThe j1939 authors 		kfree_skb(skb);
3369d71dd0cSThe j1939 authors }
3379d71dd0cSThe j1939 authors 
j1939_sk_recv_match(struct j1939_priv * priv,struct j1939_sk_buff_cb * skcb)3389d71dd0cSThe j1939 authors bool j1939_sk_recv_match(struct j1939_priv *priv, struct j1939_sk_buff_cb *skcb)
3399d71dd0cSThe j1939 authors {
3409d71dd0cSThe j1939 authors 	struct j1939_sock *jsk;
3419d71dd0cSThe j1939 authors 	bool match = false;
3429d71dd0cSThe j1939 authors 
34326dfe112SZiqi Zhao 	read_lock_bh(&priv->j1939_socks_lock);
3449d71dd0cSThe j1939 authors 	list_for_each_entry(jsk, &priv->j1939_socks, list) {
3459d71dd0cSThe j1939 authors 		match = j1939_sk_recv_match_one(jsk, skcb);
3469d71dd0cSThe j1939 authors 		if (match)
3479d71dd0cSThe j1939 authors 			break;
3489d71dd0cSThe j1939 authors 	}
34926dfe112SZiqi Zhao 	read_unlock_bh(&priv->j1939_socks_lock);
3509d71dd0cSThe j1939 authors 
3519d71dd0cSThe j1939 authors 	return match;
3529d71dd0cSThe j1939 authors }
3539d71dd0cSThe j1939 authors 
j1939_sk_recv(struct j1939_priv * priv,struct sk_buff * skb)3549d71dd0cSThe j1939 authors void j1939_sk_recv(struct j1939_priv *priv, struct sk_buff *skb)
3559d71dd0cSThe j1939 authors {
3569d71dd0cSThe j1939 authors 	struct j1939_sock *jsk;
3579d71dd0cSThe j1939 authors 
35826dfe112SZiqi Zhao 	read_lock_bh(&priv->j1939_socks_lock);
3599d71dd0cSThe j1939 authors 	list_for_each_entry(jsk, &priv->j1939_socks, list) {
3609d71dd0cSThe j1939 authors 		j1939_sk_recv_one(jsk, skb);
3619d71dd0cSThe j1939 authors 	}
36226dfe112SZiqi Zhao 	read_unlock_bh(&priv->j1939_socks_lock);
3639d71dd0cSThe j1939 authors }
3649d71dd0cSThe j1939 authors 
j1939_sk_sock_destruct(struct sock * sk)36525fe97cbSOleksij Rempel static void j1939_sk_sock_destruct(struct sock *sk)
36625fe97cbSOleksij Rempel {
36725fe97cbSOleksij Rempel 	struct j1939_sock *jsk = j1939_sk(sk);
36825fe97cbSOleksij Rempel 
36904bdec2bSgushengxian 	/* This function will be called by the generic networking code, when
37025fe97cbSOleksij Rempel 	 * the socket is ultimately closed (sk->sk_destruct).
37125fe97cbSOleksij Rempel 	 *
37225fe97cbSOleksij Rempel 	 * The race between
37325fe97cbSOleksij Rempel 	 * - processing a received CAN frame
37425fe97cbSOleksij Rempel 	 *   (can_receive -> j1939_can_recv)
37525fe97cbSOleksij Rempel 	 *   and accessing j1939_priv
37625fe97cbSOleksij Rempel 	 * ... and ...
37725fe97cbSOleksij Rempel 	 * - closing a socket
37825fe97cbSOleksij Rempel 	 *   (j1939_can_rx_unregister -> can_rx_unregister)
37925fe97cbSOleksij Rempel 	 *   and calling the final j1939_priv_put()
38025fe97cbSOleksij Rempel 	 *
38125fe97cbSOleksij Rempel 	 * is avoided by calling the final j1939_priv_put() from this
38225fe97cbSOleksij Rempel 	 * RCU deferred cleanup call.
38325fe97cbSOleksij Rempel 	 */
38425fe97cbSOleksij Rempel 	if (jsk->priv) {
38525fe97cbSOleksij Rempel 		j1939_priv_put(jsk->priv);
38625fe97cbSOleksij Rempel 		jsk->priv = NULL;
38725fe97cbSOleksij Rempel 	}
38825fe97cbSOleksij Rempel 
38925fe97cbSOleksij Rempel 	/* call generic CAN sock destruct */
39025fe97cbSOleksij Rempel 	can_sock_destruct(sk);
39125fe97cbSOleksij Rempel }
39225fe97cbSOleksij Rempel 
j1939_sk_init(struct sock * sk)3939d71dd0cSThe j1939 authors static int j1939_sk_init(struct sock *sk)
3949d71dd0cSThe j1939 authors {
3959d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sk);
3969d71dd0cSThe j1939 authors 
3979d71dd0cSThe j1939 authors 	/* Ensure that "sk" is first member in "struct j1939_sock", so that we
3989d71dd0cSThe j1939 authors 	 * can skip it during memset().
3999d71dd0cSThe j1939 authors 	 */
4009d71dd0cSThe j1939 authors 	BUILD_BUG_ON(offsetof(struct j1939_sock, sk) != 0);
4019d71dd0cSThe j1939 authors 	memset((void *)jsk + sizeof(jsk->sk), 0x0,
4029d71dd0cSThe j1939 authors 	       sizeof(*jsk) - sizeof(jsk->sk));
4039d71dd0cSThe j1939 authors 
4049d71dd0cSThe j1939 authors 	INIT_LIST_HEAD(&jsk->list);
4059d71dd0cSThe j1939 authors 	init_waitqueue_head(&jsk->waitq);
4069d71dd0cSThe j1939 authors 	jsk->sk.sk_priority = j1939_to_sk_priority(6);
4079d71dd0cSThe j1939 authors 	jsk->sk.sk_reuse = 1; /* per default */
4089d71dd0cSThe j1939 authors 	jsk->addr.sa = J1939_NO_ADDR;
4099d71dd0cSThe j1939 authors 	jsk->addr.da = J1939_NO_ADDR;
4109d71dd0cSThe j1939 authors 	jsk->addr.pgn = J1939_NO_PGN;
4119d71dd0cSThe j1939 authors 	jsk->pgn_rx_filter = J1939_NO_PGN;
4129d71dd0cSThe j1939 authors 	atomic_set(&jsk->skb_pending, 0);
4139d71dd0cSThe j1939 authors 	spin_lock_init(&jsk->sk_session_queue_lock);
4149d71dd0cSThe j1939 authors 	INIT_LIST_HEAD(&jsk->sk_session_queue);
415f84e7534SOleksij Rempel 	spin_lock_init(&jsk->filters_lock);
41622c696feSOleksij Rempel 
41722c696feSOleksij Rempel 	/* j1939_sk_sock_destruct() depends on SOCK_RCU_FREE flag */
41822c696feSOleksij Rempel 	sock_set_flag(sk, SOCK_RCU_FREE);
41925fe97cbSOleksij Rempel 	sk->sk_destruct = j1939_sk_sock_destruct;
420b43e3a82SOleksij Rempel 	sk->sk_protocol = CAN_J1939;
4219d71dd0cSThe j1939 authors 
4229d71dd0cSThe j1939 authors 	return 0;
4239d71dd0cSThe j1939 authors }
4249d71dd0cSThe j1939 authors 
j1939_sk_sanity_check(struct sockaddr_can * addr,int len)4259d71dd0cSThe j1939 authors static int j1939_sk_sanity_check(struct sockaddr_can *addr, int len)
4269d71dd0cSThe j1939 authors {
4279d71dd0cSThe j1939 authors 	if (!addr)
4289d71dd0cSThe j1939 authors 		return -EDESTADDRREQ;
4299d71dd0cSThe j1939 authors 	if (len < J1939_MIN_NAMELEN)
4309d71dd0cSThe j1939 authors 		return -EINVAL;
4319d71dd0cSThe j1939 authors 	if (addr->can_family != AF_CAN)
4329d71dd0cSThe j1939 authors 		return -EINVAL;
4339d71dd0cSThe j1939 authors 	if (!addr->can_ifindex)
4349d71dd0cSThe j1939 authors 		return -ENODEV;
4359d71dd0cSThe j1939 authors 	if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn) &&
4369d71dd0cSThe j1939 authors 	    !j1939_pgn_is_clean_pdu(addr->can_addr.j1939.pgn))
4379d71dd0cSThe j1939 authors 		return -EINVAL;
4389d71dd0cSThe j1939 authors 
4399d71dd0cSThe j1939 authors 	return 0;
4409d71dd0cSThe j1939 authors }
4419d71dd0cSThe j1939 authors 
j1939_sk_bind(struct socket * sock,struct sockaddr * uaddr,int len)4429d71dd0cSThe j1939 authors static int j1939_sk_bind(struct socket *sock, struct sockaddr *uaddr, int len)
4439d71dd0cSThe j1939 authors {
4449d71dd0cSThe j1939 authors 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
4459d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sock->sk);
44600d4e14dSOleksij Rempel 	struct j1939_priv *priv;
44700d4e14dSOleksij Rempel 	struct sock *sk;
44800d4e14dSOleksij Rempel 	struct net *net;
4499d71dd0cSThe j1939 authors 	int ret = 0;
4509d71dd0cSThe j1939 authors 
4519d71dd0cSThe j1939 authors 	ret = j1939_sk_sanity_check(addr, len);
4529d71dd0cSThe j1939 authors 	if (ret)
4539d71dd0cSThe j1939 authors 		return ret;
4549d71dd0cSThe j1939 authors 
4559d71dd0cSThe j1939 authors 	lock_sock(sock->sk);
4569d71dd0cSThe j1939 authors 
45700d4e14dSOleksij Rempel 	priv = jsk->priv;
45800d4e14dSOleksij Rempel 	sk = sock->sk;
45900d4e14dSOleksij Rempel 	net = sock_net(sk);
46000d4e14dSOleksij Rempel 
4619d71dd0cSThe j1939 authors 	/* Already bound to an interface? */
4629d71dd0cSThe j1939 authors 	if (jsk->state & J1939_SOCK_BOUND) {
4639d71dd0cSThe j1939 authors 		/* A re-bind() to a different interface is not
4649d71dd0cSThe j1939 authors 		 * supported.
4659d71dd0cSThe j1939 authors 		 */
4669d71dd0cSThe j1939 authors 		if (jsk->ifindex != addr->can_ifindex) {
4679d71dd0cSThe j1939 authors 			ret = -EINVAL;
4689d71dd0cSThe j1939 authors 			goto out_release_sock;
4699d71dd0cSThe j1939 authors 		}
4709d71dd0cSThe j1939 authors 
4719d71dd0cSThe j1939 authors 		/* drop old references */
4729d71dd0cSThe j1939 authors 		j1939_jsk_del(priv, jsk);
4739d71dd0cSThe j1939 authors 		j1939_local_ecu_put(priv, jsk->addr.src_name, jsk->addr.sa);
4749d71dd0cSThe j1939 authors 	} else {
4754e096a18SOleksij Rempel 		struct can_ml_priv *can_ml;
4769d71dd0cSThe j1939 authors 		struct net_device *ndev;
4779d71dd0cSThe j1939 authors 
4789d71dd0cSThe j1939 authors 		ndev = dev_get_by_index(net, addr->can_ifindex);
4799d71dd0cSThe j1939 authors 		if (!ndev) {
4809d71dd0cSThe j1939 authors 			ret = -ENODEV;
4819d71dd0cSThe j1939 authors 			goto out_release_sock;
4829d71dd0cSThe j1939 authors 		}
4839d71dd0cSThe j1939 authors 
4844e096a18SOleksij Rempel 		can_ml = can_get_ml_priv(ndev);
4854e096a18SOleksij Rempel 		if (!can_ml) {
486af804b78SOleksij Rempel 			dev_put(ndev);
487af804b78SOleksij Rempel 			ret = -ENODEV;
488af804b78SOleksij Rempel 			goto out_release_sock;
489af804b78SOleksij Rempel 		}
490af804b78SOleksij Rempel 
49108c487d8SZhang Changzhong 		if (!(ndev->flags & IFF_UP)) {
49208c487d8SZhang Changzhong 			dev_put(ndev);
49308c487d8SZhang Changzhong 			ret = -ENETDOWN;
49408c487d8SZhang Changzhong 			goto out_release_sock;
49508c487d8SZhang Changzhong 		}
49608c487d8SZhang Changzhong 
4979d71dd0cSThe j1939 authors 		priv = j1939_netdev_start(ndev);
4989d71dd0cSThe j1939 authors 		dev_put(ndev);
4999d71dd0cSThe j1939 authors 		if (IS_ERR(priv)) {
5009d71dd0cSThe j1939 authors 			ret = PTR_ERR(priv);
5019d71dd0cSThe j1939 authors 			goto out_release_sock;
5029d71dd0cSThe j1939 authors 		}
5039d71dd0cSThe j1939 authors 
5049d71dd0cSThe j1939 authors 		jsk->ifindex = addr->can_ifindex;
50525fe97cbSOleksij Rempel 
50625fe97cbSOleksij Rempel 		/* the corresponding j1939_priv_put() is called via
50725fe97cbSOleksij Rempel 		 * sk->sk_destruct, which points to j1939_sk_sock_destruct()
50825fe97cbSOleksij Rempel 		 */
50925fe97cbSOleksij Rempel 		j1939_priv_get(priv);
51025fe97cbSOleksij Rempel 		jsk->priv = priv;
5119d71dd0cSThe j1939 authors 	}
5129d71dd0cSThe j1939 authors 
5139d71dd0cSThe j1939 authors 	/* set default transmit pgn */
5149d71dd0cSThe j1939 authors 	if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn))
5159d71dd0cSThe j1939 authors 		jsk->pgn_rx_filter = addr->can_addr.j1939.pgn;
5169d71dd0cSThe j1939 authors 	jsk->addr.src_name = addr->can_addr.j1939.name;
5179d71dd0cSThe j1939 authors 	jsk->addr.sa = addr->can_addr.j1939.addr;
5189d71dd0cSThe j1939 authors 
5199d71dd0cSThe j1939 authors 	/* get new references */
5209d71dd0cSThe j1939 authors 	ret = j1939_local_ecu_get(priv, jsk->addr.src_name, jsk->addr.sa);
5219d71dd0cSThe j1939 authors 	if (ret) {
5229d71dd0cSThe j1939 authors 		j1939_netdev_stop(priv);
5239d71dd0cSThe j1939 authors 		goto out_release_sock;
5249d71dd0cSThe j1939 authors 	}
5259d71dd0cSThe j1939 authors 
5269d71dd0cSThe j1939 authors 	j1939_jsk_add(priv, jsk);
5279d71dd0cSThe j1939 authors 
5289d71dd0cSThe j1939 authors  out_release_sock: /* fall through */
5299d71dd0cSThe j1939 authors 	release_sock(sock->sk);
5309d71dd0cSThe j1939 authors 
5319d71dd0cSThe j1939 authors 	return ret;
5329d71dd0cSThe j1939 authors }
5339d71dd0cSThe j1939 authors 
j1939_sk_connect(struct socket * sock,struct sockaddr * uaddr,int len,int flags)5349d71dd0cSThe j1939 authors static int j1939_sk_connect(struct socket *sock, struct sockaddr *uaddr,
5359d71dd0cSThe j1939 authors 			    int len, int flags)
5369d71dd0cSThe j1939 authors {
5379d71dd0cSThe j1939 authors 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
5389d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sock->sk);
5399d71dd0cSThe j1939 authors 	int ret = 0;
5409d71dd0cSThe j1939 authors 
5419d71dd0cSThe j1939 authors 	ret = j1939_sk_sanity_check(addr, len);
5429d71dd0cSThe j1939 authors 	if (ret)
5439d71dd0cSThe j1939 authors 		return ret;
5449d71dd0cSThe j1939 authors 
5459d71dd0cSThe j1939 authors 	lock_sock(sock->sk);
5469d71dd0cSThe j1939 authors 
5479d71dd0cSThe j1939 authors 	/* bind() before connect() is mandatory */
5489d71dd0cSThe j1939 authors 	if (!(jsk->state & J1939_SOCK_BOUND)) {
5499d71dd0cSThe j1939 authors 		ret = -EINVAL;
5509d71dd0cSThe j1939 authors 		goto out_release_sock;
5519d71dd0cSThe j1939 authors 	}
5529d71dd0cSThe j1939 authors 
5539d71dd0cSThe j1939 authors 	/* A connect() to a different interface is not supported. */
5549d71dd0cSThe j1939 authors 	if (jsk->ifindex != addr->can_ifindex) {
5559d71dd0cSThe j1939 authors 		ret = -EINVAL;
5569d71dd0cSThe j1939 authors 		goto out_release_sock;
5579d71dd0cSThe j1939 authors 	}
5589d71dd0cSThe j1939 authors 
5599d71dd0cSThe j1939 authors 	if (!addr->can_addr.j1939.name &&
5609d71dd0cSThe j1939 authors 	    addr->can_addr.j1939.addr == J1939_NO_ADDR &&
5619d71dd0cSThe j1939 authors 	    !sock_flag(&jsk->sk, SOCK_BROADCAST)) {
5629d71dd0cSThe j1939 authors 		/* broadcast, but SO_BROADCAST not set */
5639d71dd0cSThe j1939 authors 		ret = -EACCES;
5649d71dd0cSThe j1939 authors 		goto out_release_sock;
5659d71dd0cSThe j1939 authors 	}
5669d71dd0cSThe j1939 authors 
5679d71dd0cSThe j1939 authors 	jsk->addr.dst_name = addr->can_addr.j1939.name;
5689d71dd0cSThe j1939 authors 	jsk->addr.da = addr->can_addr.j1939.addr;
5699d71dd0cSThe j1939 authors 
5709d71dd0cSThe j1939 authors 	if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn))
5719d71dd0cSThe j1939 authors 		jsk->addr.pgn = addr->can_addr.j1939.pgn;
5729d71dd0cSThe j1939 authors 
5739d71dd0cSThe j1939 authors 	jsk->state |= J1939_SOCK_CONNECTED;
5749d71dd0cSThe j1939 authors 
5759d71dd0cSThe j1939 authors  out_release_sock: /* fall through */
5769d71dd0cSThe j1939 authors 	release_sock(sock->sk);
5779d71dd0cSThe j1939 authors 
5789d71dd0cSThe j1939 authors 	return ret;
5799d71dd0cSThe j1939 authors }
5809d71dd0cSThe j1939 authors 
j1939_sk_sock2sockaddr_can(struct sockaddr_can * addr,const struct j1939_sock * jsk,int peer)5819d71dd0cSThe j1939 authors static void j1939_sk_sock2sockaddr_can(struct sockaddr_can *addr,
5829d71dd0cSThe j1939 authors 				       const struct j1939_sock *jsk, int peer)
5839d71dd0cSThe j1939 authors {
58438ba8b92SEric Dumazet 	/* There are two holes (2 bytes and 3 bytes) to clear to avoid
58538ba8b92SEric Dumazet 	 * leaking kernel information to user space.
58638ba8b92SEric Dumazet 	 */
58738ba8b92SEric Dumazet 	memset(addr, 0, J1939_MIN_NAMELEN);
58838ba8b92SEric Dumazet 
5899d71dd0cSThe j1939 authors 	addr->can_family = AF_CAN;
5909d71dd0cSThe j1939 authors 	addr->can_ifindex = jsk->ifindex;
5919d71dd0cSThe j1939 authors 	addr->can_addr.j1939.pgn = jsk->addr.pgn;
5929d71dd0cSThe j1939 authors 	if (peer) {
5939d71dd0cSThe j1939 authors 		addr->can_addr.j1939.name = jsk->addr.dst_name;
5949d71dd0cSThe j1939 authors 		addr->can_addr.j1939.addr = jsk->addr.da;
5959d71dd0cSThe j1939 authors 	} else {
5969d71dd0cSThe j1939 authors 		addr->can_addr.j1939.name = jsk->addr.src_name;
5979d71dd0cSThe j1939 authors 		addr->can_addr.j1939.addr = jsk->addr.sa;
5989d71dd0cSThe j1939 authors 	}
5999d71dd0cSThe j1939 authors }
6009d71dd0cSThe j1939 authors 
j1939_sk_getname(struct socket * sock,struct sockaddr * uaddr,int peer)6019d71dd0cSThe j1939 authors static int j1939_sk_getname(struct socket *sock, struct sockaddr *uaddr,
6029d71dd0cSThe j1939 authors 			    int peer)
6039d71dd0cSThe j1939 authors {
6049d71dd0cSThe j1939 authors 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
6059d71dd0cSThe j1939 authors 	struct sock *sk = sock->sk;
6069d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sk);
6079d71dd0cSThe j1939 authors 	int ret = 0;
6089d71dd0cSThe j1939 authors 
6099d71dd0cSThe j1939 authors 	lock_sock(sk);
6109d71dd0cSThe j1939 authors 
6119d71dd0cSThe j1939 authors 	if (peer && !(jsk->state & J1939_SOCK_CONNECTED)) {
6129d71dd0cSThe j1939 authors 		ret = -EADDRNOTAVAIL;
6139d71dd0cSThe j1939 authors 		goto failure;
6149d71dd0cSThe j1939 authors 	}
6159d71dd0cSThe j1939 authors 
6169d71dd0cSThe j1939 authors 	j1939_sk_sock2sockaddr_can(addr, jsk, peer);
6179d71dd0cSThe j1939 authors 	ret = J1939_MIN_NAMELEN;
6189d71dd0cSThe j1939 authors 
6199d71dd0cSThe j1939 authors  failure:
6209d71dd0cSThe j1939 authors 	release_sock(sk);
6219d71dd0cSThe j1939 authors 
6229d71dd0cSThe j1939 authors 	return ret;
6239d71dd0cSThe j1939 authors }
6249d71dd0cSThe j1939 authors 
j1939_sk_release(struct socket * sock)6259d71dd0cSThe j1939 authors static int j1939_sk_release(struct socket *sock)
6269d71dd0cSThe j1939 authors {
6279d71dd0cSThe j1939 authors 	struct sock *sk = sock->sk;
6289d71dd0cSThe j1939 authors 	struct j1939_sock *jsk;
6299d71dd0cSThe j1939 authors 
6309d71dd0cSThe j1939 authors 	if (!sk)
6319d71dd0cSThe j1939 authors 		return 0;
6329d71dd0cSThe j1939 authors 
6339d71dd0cSThe j1939 authors 	lock_sock(sk);
634fd81ebfeSOleksij Rempel 	jsk = j1939_sk(sk);
6359d71dd0cSThe j1939 authors 
6369d71dd0cSThe j1939 authors 	if (jsk->state & J1939_SOCK_BOUND) {
6379d71dd0cSThe j1939 authors 		struct j1939_priv *priv = jsk->priv;
6389d71dd0cSThe j1939 authors 
6399d71dd0cSThe j1939 authors 		if (wait_event_interruptible(jsk->waitq,
6409d71dd0cSThe j1939 authors 					     !j1939_sock_pending_get(&jsk->sk))) {
6419d71dd0cSThe j1939 authors 			j1939_cancel_active_session(priv, sk);
6429d71dd0cSThe j1939 authors 			j1939_sk_queue_drop_all(priv, jsk, ESHUTDOWN);
6439d71dd0cSThe j1939 authors 		}
6449d71dd0cSThe j1939 authors 
6459d71dd0cSThe j1939 authors 		j1939_jsk_del(priv, jsk);
6469d71dd0cSThe j1939 authors 
6479d71dd0cSThe j1939 authors 		j1939_local_ecu_put(priv, jsk->addr.src_name,
6489d71dd0cSThe j1939 authors 				    jsk->addr.sa);
6499d71dd0cSThe j1939 authors 
6509d71dd0cSThe j1939 authors 		j1939_netdev_stop(priv);
6519d71dd0cSThe j1939 authors 	}
6529d71dd0cSThe j1939 authors 
653896daf72SOleksij Rempel 	kfree(jsk->filters);
6549d71dd0cSThe j1939 authors 	sock_orphan(sk);
6559d71dd0cSThe j1939 authors 	sock->sk = NULL;
6569d71dd0cSThe j1939 authors 
6579d71dd0cSThe j1939 authors 	release_sock(sk);
6589d71dd0cSThe j1939 authors 	sock_put(sk);
6599d71dd0cSThe j1939 authors 
6609d71dd0cSThe j1939 authors 	return 0;
6619d71dd0cSThe j1939 authors }
6629d71dd0cSThe j1939 authors 
j1939_sk_setsockopt_flag(struct j1939_sock * jsk,sockptr_t optval,unsigned int optlen,int flag)663a7b75c5aSChristoph Hellwig static int j1939_sk_setsockopt_flag(struct j1939_sock *jsk, sockptr_t optval,
6649d71dd0cSThe j1939 authors 				    unsigned int optlen, int flag)
6659d71dd0cSThe j1939 authors {
6669d71dd0cSThe j1939 authors 	int tmp;
6679d71dd0cSThe j1939 authors 
6689d71dd0cSThe j1939 authors 	if (optlen != sizeof(tmp))
6699d71dd0cSThe j1939 authors 		return -EINVAL;
670a7b75c5aSChristoph Hellwig 	if (copy_from_sockptr(&tmp, optval, optlen))
6719d71dd0cSThe j1939 authors 		return -EFAULT;
6729d71dd0cSThe j1939 authors 	lock_sock(&jsk->sk);
6739d71dd0cSThe j1939 authors 	if (tmp)
6749d71dd0cSThe j1939 authors 		jsk->state |= flag;
6759d71dd0cSThe j1939 authors 	else
6769d71dd0cSThe j1939 authors 		jsk->state &= ~flag;
6779d71dd0cSThe j1939 authors 	release_sock(&jsk->sk);
6789d71dd0cSThe j1939 authors 	return tmp;
6799d71dd0cSThe j1939 authors }
6809d71dd0cSThe j1939 authors 
j1939_sk_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)6819d71dd0cSThe j1939 authors static int j1939_sk_setsockopt(struct socket *sock, int level, int optname,
682a7b75c5aSChristoph Hellwig 			       sockptr_t optval, unsigned int optlen)
6839d71dd0cSThe j1939 authors {
6849d71dd0cSThe j1939 authors 	struct sock *sk = sock->sk;
6859d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sk);
6869d71dd0cSThe j1939 authors 	int tmp, count = 0, ret = 0;
6879d71dd0cSThe j1939 authors 	struct j1939_filter *filters = NULL, *ofilters;
6889d71dd0cSThe j1939 authors 
6899d71dd0cSThe j1939 authors 	if (level != SOL_CAN_J1939)
6909d71dd0cSThe j1939 authors 		return -EINVAL;
6919d71dd0cSThe j1939 authors 
6929d71dd0cSThe j1939 authors 	switch (optname) {
6939d71dd0cSThe j1939 authors 	case SO_J1939_FILTER:
694aaf473d0SNorbert Slusarek 		if (!sockptr_is_null(optval) && optlen != 0) {
6959d71dd0cSThe j1939 authors 			struct j1939_filter *f;
6969d71dd0cSThe j1939 authors 			int c;
6979d71dd0cSThe j1939 authors 
6989d71dd0cSThe j1939 authors 			if (optlen % sizeof(*filters) != 0)
6999d71dd0cSThe j1939 authors 				return -EINVAL;
7009d71dd0cSThe j1939 authors 
7019d71dd0cSThe j1939 authors 			if (optlen > J1939_FILTER_MAX *
7029d71dd0cSThe j1939 authors 			    sizeof(struct j1939_filter))
7039d71dd0cSThe j1939 authors 				return -EINVAL;
7049d71dd0cSThe j1939 authors 
7059d71dd0cSThe j1939 authors 			count = optlen / sizeof(*filters);
706a7b75c5aSChristoph Hellwig 			filters = memdup_sockptr(optval, optlen);
7079d71dd0cSThe j1939 authors 			if (IS_ERR(filters))
7089d71dd0cSThe j1939 authors 				return PTR_ERR(filters);
7099d71dd0cSThe j1939 authors 
7109d71dd0cSThe j1939 authors 			for (f = filters, c = count; c; f++, c--) {
7119d71dd0cSThe j1939 authors 				f->name &= f->name_mask;
7129d71dd0cSThe j1939 authors 				f->pgn &= f->pgn_mask;
7139d71dd0cSThe j1939 authors 				f->addr &= f->addr_mask;
7149d71dd0cSThe j1939 authors 			}
7159d71dd0cSThe j1939 authors 		}
7169d71dd0cSThe j1939 authors 
7179d71dd0cSThe j1939 authors 		lock_sock(&jsk->sk);
718f84e7534SOleksij Rempel 		spin_lock_bh(&jsk->filters_lock);
7199d71dd0cSThe j1939 authors 		ofilters = jsk->filters;
7209d71dd0cSThe j1939 authors 		jsk->filters = filters;
7219d71dd0cSThe j1939 authors 		jsk->nfilters = count;
722f84e7534SOleksij Rempel 		spin_unlock_bh(&jsk->filters_lock);
7239d71dd0cSThe j1939 authors 		release_sock(&jsk->sk);
7249d71dd0cSThe j1939 authors 		kfree(ofilters);
7259d71dd0cSThe j1939 authors 		return 0;
7269d71dd0cSThe j1939 authors 	case SO_J1939_PROMISC:
7279d71dd0cSThe j1939 authors 		return j1939_sk_setsockopt_flag(jsk, optval, optlen,
7289d71dd0cSThe j1939 authors 						J1939_SOCK_PROMISC);
7299d71dd0cSThe j1939 authors 	case SO_J1939_ERRQUEUE:
7309d71dd0cSThe j1939 authors 		ret = j1939_sk_setsockopt_flag(jsk, optval, optlen,
7319d71dd0cSThe j1939 authors 					       J1939_SOCK_ERRQUEUE);
7329d71dd0cSThe j1939 authors 		if (ret < 0)
7339d71dd0cSThe j1939 authors 			return ret;
7349d71dd0cSThe j1939 authors 
7359d71dd0cSThe j1939 authors 		if (!(jsk->state & J1939_SOCK_ERRQUEUE))
7369d71dd0cSThe j1939 authors 			skb_queue_purge(&sk->sk_error_queue);
7379d71dd0cSThe j1939 authors 		return ret;
7389d71dd0cSThe j1939 authors 	case SO_J1939_SEND_PRIO:
7399d71dd0cSThe j1939 authors 		if (optlen != sizeof(tmp))
7409d71dd0cSThe j1939 authors 			return -EINVAL;
741a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&tmp, optval, optlen))
7429d71dd0cSThe j1939 authors 			return -EFAULT;
7439d71dd0cSThe j1939 authors 		if (tmp < 0 || tmp > 7)
7449d71dd0cSThe j1939 authors 			return -EDOM;
7459d71dd0cSThe j1939 authors 		if (tmp < 2 && !capable(CAP_NET_ADMIN))
7469d71dd0cSThe j1939 authors 			return -EPERM;
7479d71dd0cSThe j1939 authors 		lock_sock(&jsk->sk);
7489d71dd0cSThe j1939 authors 		jsk->sk.sk_priority = j1939_to_sk_priority(tmp);
7499d71dd0cSThe j1939 authors 		release_sock(&jsk->sk);
7509d71dd0cSThe j1939 authors 		return 0;
7519d71dd0cSThe j1939 authors 	default:
7529d71dd0cSThe j1939 authors 		return -ENOPROTOOPT;
7539d71dd0cSThe j1939 authors 	}
7549d71dd0cSThe j1939 authors }
7559d71dd0cSThe j1939 authors 
j1939_sk_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)7569d71dd0cSThe j1939 authors static int j1939_sk_getsockopt(struct socket *sock, int level, int optname,
7579d71dd0cSThe j1939 authors 			       char __user *optval, int __user *optlen)
7589d71dd0cSThe j1939 authors {
7599d71dd0cSThe j1939 authors 	struct sock *sk = sock->sk;
7609d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sk);
7619d71dd0cSThe j1939 authors 	int ret, ulen;
7629d71dd0cSThe j1939 authors 	/* set defaults for using 'int' properties */
7639d71dd0cSThe j1939 authors 	int tmp = 0;
7649d71dd0cSThe j1939 authors 	int len = sizeof(tmp);
7659d71dd0cSThe j1939 authors 	void *val = &tmp;
7669d71dd0cSThe j1939 authors 
7679d71dd0cSThe j1939 authors 	if (level != SOL_CAN_J1939)
7689d71dd0cSThe j1939 authors 		return -EINVAL;
7699d71dd0cSThe j1939 authors 	if (get_user(ulen, optlen))
7709d71dd0cSThe j1939 authors 		return -EFAULT;
7719d71dd0cSThe j1939 authors 	if (ulen < 0)
7729d71dd0cSThe j1939 authors 		return -EINVAL;
7739d71dd0cSThe j1939 authors 
7749d71dd0cSThe j1939 authors 	lock_sock(&jsk->sk);
7759d71dd0cSThe j1939 authors 	switch (optname) {
7769d71dd0cSThe j1939 authors 	case SO_J1939_PROMISC:
7779d71dd0cSThe j1939 authors 		tmp = (jsk->state & J1939_SOCK_PROMISC) ? 1 : 0;
7789d71dd0cSThe j1939 authors 		break;
7799d71dd0cSThe j1939 authors 	case SO_J1939_ERRQUEUE:
7809d71dd0cSThe j1939 authors 		tmp = (jsk->state & J1939_SOCK_ERRQUEUE) ? 1 : 0;
7819d71dd0cSThe j1939 authors 		break;
7829d71dd0cSThe j1939 authors 	case SO_J1939_SEND_PRIO:
7839d71dd0cSThe j1939 authors 		tmp = j1939_prio(jsk->sk.sk_priority);
7849d71dd0cSThe j1939 authors 		break;
7859d71dd0cSThe j1939 authors 	default:
7869d71dd0cSThe j1939 authors 		ret = -ENOPROTOOPT;
7879d71dd0cSThe j1939 authors 		goto no_copy;
7889d71dd0cSThe j1939 authors 	}
7899d71dd0cSThe j1939 authors 
7909d71dd0cSThe j1939 authors 	/* copy to user, based on 'len' & 'val'
7919d71dd0cSThe j1939 authors 	 * but most sockopt's are 'int' properties, and have 'len' & 'val'
7929d71dd0cSThe j1939 authors 	 * left unchanged, but instead modified 'tmp'
7939d71dd0cSThe j1939 authors 	 */
7949d71dd0cSThe j1939 authors 	if (len > ulen)
7959d71dd0cSThe j1939 authors 		ret = -EFAULT;
7969d71dd0cSThe j1939 authors 	else if (put_user(len, optlen))
7979d71dd0cSThe j1939 authors 		ret = -EFAULT;
7989d71dd0cSThe j1939 authors 	else if (copy_to_user(optval, val, len))
7999d71dd0cSThe j1939 authors 		ret = -EFAULT;
8009d71dd0cSThe j1939 authors 	else
8019d71dd0cSThe j1939 authors 		ret = 0;
8029d71dd0cSThe j1939 authors  no_copy:
8039d71dd0cSThe j1939 authors 	release_sock(&jsk->sk);
8049d71dd0cSThe j1939 authors 	return ret;
8059d71dd0cSThe j1939 authors }
8069d71dd0cSThe j1939 authors 
j1939_sk_recvmsg(struct socket * sock,struct msghdr * msg,size_t size,int flags)8079d71dd0cSThe j1939 authors static int j1939_sk_recvmsg(struct socket *sock, struct msghdr *msg,
8089d71dd0cSThe j1939 authors 			    size_t size, int flags)
8099d71dd0cSThe j1939 authors {
8109d71dd0cSThe j1939 authors 	struct sock *sk = sock->sk;
8119d71dd0cSThe j1939 authors 	struct sk_buff *skb;
8129d71dd0cSThe j1939 authors 	struct j1939_sk_buff_cb *skcb;
8139d71dd0cSThe j1939 authors 	int ret = 0;
8149d71dd0cSThe j1939 authors 
8151db080cbSOliver Hartkopp 	if (flags & ~(MSG_DONTWAIT | MSG_ERRQUEUE | MSG_CMSG_COMPAT))
8169d71dd0cSThe j1939 authors 		return -EINVAL;
8179d71dd0cSThe j1939 authors 
8189d71dd0cSThe j1939 authors 	if (flags & MSG_ERRQUEUE)
8199d71dd0cSThe j1939 authors 		return sock_recv_errqueue(sock->sk, msg, size, SOL_CAN_J1939,
8209d71dd0cSThe j1939 authors 					  SCM_J1939_ERRQUEUE);
8219d71dd0cSThe j1939 authors 
822f4b41f06SOliver Hartkopp 	skb = skb_recv_datagram(sk, flags, &ret);
8239d71dd0cSThe j1939 authors 	if (!skb)
8249d71dd0cSThe j1939 authors 		return ret;
8259d71dd0cSThe j1939 authors 
8269d71dd0cSThe j1939 authors 	if (size < skb->len)
8279d71dd0cSThe j1939 authors 		msg->msg_flags |= MSG_TRUNC;
8289d71dd0cSThe j1939 authors 	else
8299d71dd0cSThe j1939 authors 		size = skb->len;
8309d71dd0cSThe j1939 authors 
8319d71dd0cSThe j1939 authors 	ret = memcpy_to_msg(msg, skb->data, size);
8329d71dd0cSThe j1939 authors 	if (ret < 0) {
8339d71dd0cSThe j1939 authors 		skb_free_datagram(sk, skb);
8349d71dd0cSThe j1939 authors 		return ret;
8359d71dd0cSThe j1939 authors 	}
8369d71dd0cSThe j1939 authors 
8379d71dd0cSThe j1939 authors 	skcb = j1939_skb_to_cb(skb);
8389d71dd0cSThe j1939 authors 	if (j1939_address_is_valid(skcb->addr.da))
8399d71dd0cSThe j1939 authors 		put_cmsg(msg, SOL_CAN_J1939, SCM_J1939_DEST_ADDR,
8409d71dd0cSThe j1939 authors 			 sizeof(skcb->addr.da), &skcb->addr.da);
8419d71dd0cSThe j1939 authors 
8429d71dd0cSThe j1939 authors 	if (skcb->addr.dst_name)
8439d71dd0cSThe j1939 authors 		put_cmsg(msg, SOL_CAN_J1939, SCM_J1939_DEST_NAME,
8449d71dd0cSThe j1939 authors 			 sizeof(skcb->addr.dst_name), &skcb->addr.dst_name);
8459d71dd0cSThe j1939 authors 
8469d71dd0cSThe j1939 authors 	put_cmsg(msg, SOL_CAN_J1939, SCM_J1939_PRIO,
8479d71dd0cSThe j1939 authors 		 sizeof(skcb->priority), &skcb->priority);
8489d71dd0cSThe j1939 authors 
8499d71dd0cSThe j1939 authors 	if (msg->msg_name) {
8509d71dd0cSThe j1939 authors 		struct sockaddr_can *paddr = msg->msg_name;
8519d71dd0cSThe j1939 authors 
8529d71dd0cSThe j1939 authors 		msg->msg_namelen = J1939_MIN_NAMELEN;
8539d71dd0cSThe j1939 authors 		memset(msg->msg_name, 0, msg->msg_namelen);
8549d71dd0cSThe j1939 authors 		paddr->can_family = AF_CAN;
8559d71dd0cSThe j1939 authors 		paddr->can_ifindex = skb->skb_iif;
8569d71dd0cSThe j1939 authors 		paddr->can_addr.j1939.name = skcb->addr.src_name;
8579d71dd0cSThe j1939 authors 		paddr->can_addr.j1939.addr = skcb->addr.sa;
8589d71dd0cSThe j1939 authors 		paddr->can_addr.j1939.pgn = skcb->addr.pgn;
8599d71dd0cSThe j1939 authors 	}
8609d71dd0cSThe j1939 authors 
8616fd1d51cSErin MacNeil 	sock_recv_cmsgs(msg, sk, skb);
8629d71dd0cSThe j1939 authors 	msg->msg_flags |= skcb->msg_flags;
8639d71dd0cSThe j1939 authors 	skb_free_datagram(sk, skb);
8649d71dd0cSThe j1939 authors 
8659d71dd0cSThe j1939 authors 	return size;
8669d71dd0cSThe j1939 authors }
8679d71dd0cSThe j1939 authors 
j1939_sk_alloc_skb(struct net_device * ndev,struct sock * sk,struct msghdr * msg,size_t size,int * errcode)8689d71dd0cSThe j1939 authors static struct sk_buff *j1939_sk_alloc_skb(struct net_device *ndev,
8699d71dd0cSThe j1939 authors 					  struct sock *sk,
8709d71dd0cSThe j1939 authors 					  struct msghdr *msg, size_t size,
8719d71dd0cSThe j1939 authors 					  int *errcode)
8729d71dd0cSThe j1939 authors {
8739d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sk);
8749d71dd0cSThe j1939 authors 	struct j1939_sk_buff_cb *skcb;
8759d71dd0cSThe j1939 authors 	struct sk_buff *skb;
8769d71dd0cSThe j1939 authors 	int ret;
8779d71dd0cSThe j1939 authors 
8789d71dd0cSThe j1939 authors 	skb = sock_alloc_send_skb(sk,
8799d71dd0cSThe j1939 authors 				  size +
8809d71dd0cSThe j1939 authors 				  sizeof(struct can_frame) -
8819d71dd0cSThe j1939 authors 				  sizeof(((struct can_frame *)NULL)->data) +
8829d71dd0cSThe j1939 authors 				  sizeof(struct can_skb_priv),
8839d71dd0cSThe j1939 authors 				  msg->msg_flags & MSG_DONTWAIT, &ret);
8849d71dd0cSThe j1939 authors 	if (!skb)
8859d71dd0cSThe j1939 authors 		goto failure;
8869d71dd0cSThe j1939 authors 
8879d71dd0cSThe j1939 authors 	can_skb_reserve(skb);
8889d71dd0cSThe j1939 authors 	can_skb_prv(skb)->ifindex = ndev->ifindex;
8899d71dd0cSThe j1939 authors 	can_skb_prv(skb)->skbcnt = 0;
8909d71dd0cSThe j1939 authors 	skb_reserve(skb, offsetof(struct can_frame, data));
8919d71dd0cSThe j1939 authors 
8929d71dd0cSThe j1939 authors 	ret = memcpy_from_msg(skb_put(skb, size), msg, size);
8939d71dd0cSThe j1939 authors 	if (ret < 0)
8949d71dd0cSThe j1939 authors 		goto free_skb;
8959d71dd0cSThe j1939 authors 
8969d71dd0cSThe j1939 authors 	skb->dev = ndev;
8979d71dd0cSThe j1939 authors 
8989d71dd0cSThe j1939 authors 	skcb = j1939_skb_to_cb(skb);
8999d71dd0cSThe j1939 authors 	memset(skcb, 0, sizeof(*skcb));
9009d71dd0cSThe j1939 authors 	skcb->addr = jsk->addr;
9019d71dd0cSThe j1939 authors 	skcb->priority = j1939_prio(sk->sk_priority);
9029d71dd0cSThe j1939 authors 
9039d71dd0cSThe j1939 authors 	if (msg->msg_name) {
9049d71dd0cSThe j1939 authors 		struct sockaddr_can *addr = msg->msg_name;
9059d71dd0cSThe j1939 authors 
9069d71dd0cSThe j1939 authors 		if (addr->can_addr.j1939.name ||
9079d71dd0cSThe j1939 authors 		    addr->can_addr.j1939.addr != J1939_NO_ADDR) {
9089d71dd0cSThe j1939 authors 			skcb->addr.dst_name = addr->can_addr.j1939.name;
9099d71dd0cSThe j1939 authors 			skcb->addr.da = addr->can_addr.j1939.addr;
9109d71dd0cSThe j1939 authors 		}
9119d71dd0cSThe j1939 authors 		if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn))
9129d71dd0cSThe j1939 authors 			skcb->addr.pgn = addr->can_addr.j1939.pgn;
9139d71dd0cSThe j1939 authors 	}
9149d71dd0cSThe j1939 authors 
9159d71dd0cSThe j1939 authors 	*errcode = ret;
9169d71dd0cSThe j1939 authors 	return skb;
9179d71dd0cSThe j1939 authors 
9189d71dd0cSThe j1939 authors free_skb:
9199d71dd0cSThe j1939 authors 	kfree_skb(skb);
9209d71dd0cSThe j1939 authors failure:
9219d71dd0cSThe j1939 authors 	*errcode = ret;
9229d71dd0cSThe j1939 authors 	return NULL;
9239d71dd0cSThe j1939 authors }
9249d71dd0cSThe j1939 authors 
j1939_sk_opt_stats_get_size(enum j1939_sk_errqueue_type type)9255b9272e9SOleksij Rempel static size_t j1939_sk_opt_stats_get_size(enum j1939_sk_errqueue_type type)
9269d71dd0cSThe j1939 authors {
9275b9272e9SOleksij Rempel 	switch (type) {
9285b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_RX_RTS:
9295b9272e9SOleksij Rempel 		return
9305b9272e9SOleksij Rempel 			nla_total_size(sizeof(u32)) + /* J1939_NLA_TOTAL_SIZE */
9315b9272e9SOleksij Rempel 			nla_total_size(sizeof(u32)) + /* J1939_NLA_PGN */
9325b9272e9SOleksij Rempel 			nla_total_size(sizeof(u64)) + /* J1939_NLA_SRC_NAME */
9335b9272e9SOleksij Rempel 			nla_total_size(sizeof(u64)) + /* J1939_NLA_DEST_NAME */
9345b9272e9SOleksij Rempel 			nla_total_size(sizeof(u8)) +  /* J1939_NLA_SRC_ADDR */
9355b9272e9SOleksij Rempel 			nla_total_size(sizeof(u8)) +  /* J1939_NLA_DEST_ADDR */
9365b9272e9SOleksij Rempel 			0;
9375b9272e9SOleksij Rempel 	default:
9389d71dd0cSThe j1939 authors 		return
9399d71dd0cSThe j1939 authors 			nla_total_size(sizeof(u32)) + /* J1939_NLA_BYTES_ACKED */
9409d71dd0cSThe j1939 authors 			0;
9419d71dd0cSThe j1939 authors 	}
9425b9272e9SOleksij Rempel }
9439d71dd0cSThe j1939 authors 
9449d71dd0cSThe j1939 authors static struct sk_buff *
j1939_sk_get_timestamping_opt_stats(struct j1939_session * session,enum j1939_sk_errqueue_type type)9455b9272e9SOleksij Rempel j1939_sk_get_timestamping_opt_stats(struct j1939_session *session,
9465b9272e9SOleksij Rempel 				    enum j1939_sk_errqueue_type type)
9479d71dd0cSThe j1939 authors {
9489d71dd0cSThe j1939 authors 	struct sk_buff *stats;
9499d71dd0cSThe j1939 authors 	u32 size;
9509d71dd0cSThe j1939 authors 
9515b9272e9SOleksij Rempel 	stats = alloc_skb(j1939_sk_opt_stats_get_size(type), GFP_ATOMIC);
9529d71dd0cSThe j1939 authors 	if (!stats)
9539d71dd0cSThe j1939 authors 		return NULL;
9549d71dd0cSThe j1939 authors 
9559d71dd0cSThe j1939 authors 	if (session->skcb.addr.type == J1939_SIMPLE)
9569d71dd0cSThe j1939 authors 		size = session->total_message_size;
9579d71dd0cSThe j1939 authors 	else
9589d71dd0cSThe j1939 authors 		size = min(session->pkt.tx_acked * 7,
9599d71dd0cSThe j1939 authors 			   session->total_message_size);
9609d71dd0cSThe j1939 authors 
9615b9272e9SOleksij Rempel 	switch (type) {
9625b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_RX_RTS:
9635b9272e9SOleksij Rempel 		nla_put_u32(stats, J1939_NLA_TOTAL_SIZE,
9645b9272e9SOleksij Rempel 			    session->total_message_size);
9655b9272e9SOleksij Rempel 		nla_put_u32(stats, J1939_NLA_PGN,
9665b9272e9SOleksij Rempel 			    session->skcb.addr.pgn);
9675b9272e9SOleksij Rempel 		nla_put_u64_64bit(stats, J1939_NLA_SRC_NAME,
9685b9272e9SOleksij Rempel 				  session->skcb.addr.src_name, J1939_NLA_PAD);
9695b9272e9SOleksij Rempel 		nla_put_u64_64bit(stats, J1939_NLA_DEST_NAME,
9705b9272e9SOleksij Rempel 				  session->skcb.addr.dst_name, J1939_NLA_PAD);
9715b9272e9SOleksij Rempel 		nla_put_u8(stats, J1939_NLA_SRC_ADDR,
9725b9272e9SOleksij Rempel 			   session->skcb.addr.sa);
9735b9272e9SOleksij Rempel 		nla_put_u8(stats, J1939_NLA_DEST_ADDR,
9745b9272e9SOleksij Rempel 			   session->skcb.addr.da);
9755b9272e9SOleksij Rempel 		break;
9765b9272e9SOleksij Rempel 	default:
9779d71dd0cSThe j1939 authors 		nla_put_u32(stats, J1939_NLA_BYTES_ACKED, size);
9785b9272e9SOleksij Rempel 	}
9799d71dd0cSThe j1939 authors 
9809d71dd0cSThe j1939 authors 	return stats;
9819d71dd0cSThe j1939 authors }
9829d71dd0cSThe j1939 authors 
__j1939_sk_errqueue(struct j1939_session * session,struct sock * sk,enum j1939_sk_errqueue_type type)9835b9272e9SOleksij Rempel static void __j1939_sk_errqueue(struct j1939_session *session, struct sock *sk,
9849d71dd0cSThe j1939 authors 				enum j1939_sk_errqueue_type type)
9859d71dd0cSThe j1939 authors {
9869d71dd0cSThe j1939 authors 	struct j1939_priv *priv = session->priv;
9879d71dd0cSThe j1939 authors 	struct j1939_sock *jsk;
9889d71dd0cSThe j1939 authors 	struct sock_exterr_skb *serr;
9899d71dd0cSThe j1939 authors 	struct sk_buff *skb;
9909d71dd0cSThe j1939 authors 	char *state = "UNK";
991e3390b30SEric Dumazet 	u32 tsflags;
9929d71dd0cSThe j1939 authors 	int err;
9939d71dd0cSThe j1939 authors 
9949d71dd0cSThe j1939 authors 	jsk = j1939_sk(sk);
9959d71dd0cSThe j1939 authors 
9969d71dd0cSThe j1939 authors 	if (!(jsk->state & J1939_SOCK_ERRQUEUE))
9979d71dd0cSThe j1939 authors 		return;
9989d71dd0cSThe j1939 authors 
999e3390b30SEric Dumazet 	tsflags = READ_ONCE(sk->sk_tsflags);
10005b9272e9SOleksij Rempel 	switch (type) {
10015b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_TX_ACK:
1002e3390b30SEric Dumazet 		if (!(tsflags & SOF_TIMESTAMPING_TX_ACK))
10035b9272e9SOleksij Rempel 			return;
10045b9272e9SOleksij Rempel 		break;
10055b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_TX_SCHED:
1006e3390b30SEric Dumazet 		if (!(tsflags & SOF_TIMESTAMPING_TX_SCHED))
10075b9272e9SOleksij Rempel 			return;
10085b9272e9SOleksij Rempel 		break;
10095b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_TX_ABORT:
10105b9272e9SOleksij Rempel 		break;
10115b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_RX_RTS:
10125b9272e9SOleksij Rempel 		fallthrough;
10135b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_RX_DPO:
10145b9272e9SOleksij Rempel 		fallthrough;
10155b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_RX_ABORT:
1016e3390b30SEric Dumazet 		if (!(tsflags & SOF_TIMESTAMPING_RX_SOFTWARE))
10175b9272e9SOleksij Rempel 			return;
10185b9272e9SOleksij Rempel 		break;
10195b9272e9SOleksij Rempel 	default:
10205b9272e9SOleksij Rempel 		netdev_err(priv->ndev, "Unknown errqueue type %i\n", type);
10215b9272e9SOleksij Rempel 	}
10225b9272e9SOleksij Rempel 
10235b9272e9SOleksij Rempel 	skb = j1939_sk_get_timestamping_opt_stats(session, type);
10249d71dd0cSThe j1939 authors 	if (!skb)
10259d71dd0cSThe j1939 authors 		return;
10269d71dd0cSThe j1939 authors 
10279d71dd0cSThe j1939 authors 	skb->tstamp = ktime_get_real();
10289d71dd0cSThe j1939 authors 
10299d71dd0cSThe j1939 authors 	BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
10309d71dd0cSThe j1939 authors 
10319d71dd0cSThe j1939 authors 	serr = SKB_EXT_ERR(skb);
10329d71dd0cSThe j1939 authors 	memset(serr, 0, sizeof(*serr));
10339d71dd0cSThe j1939 authors 	switch (type) {
1034cd85d3aeSOleksij Rempel 	case J1939_ERRQUEUE_TX_ACK:
10359d71dd0cSThe j1939 authors 		serr->ee.ee_errno = ENOMSG;
10369d71dd0cSThe j1939 authors 		serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
10379d71dd0cSThe j1939 authors 		serr->ee.ee_info = SCM_TSTAMP_ACK;
10385b9272e9SOleksij Rempel 		state = "TX ACK";
10399d71dd0cSThe j1939 authors 		break;
1040cd85d3aeSOleksij Rempel 	case J1939_ERRQUEUE_TX_SCHED:
10419d71dd0cSThe j1939 authors 		serr->ee.ee_errno = ENOMSG;
10429d71dd0cSThe j1939 authors 		serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
10439d71dd0cSThe j1939 authors 		serr->ee.ee_info = SCM_TSTAMP_SCHED;
10445b9272e9SOleksij Rempel 		state = "TX SCH";
10459d71dd0cSThe j1939 authors 		break;
1046cd85d3aeSOleksij Rempel 	case J1939_ERRQUEUE_TX_ABORT:
10479d71dd0cSThe j1939 authors 		serr->ee.ee_errno = session->err;
10489d71dd0cSThe j1939 authors 		serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
10499d71dd0cSThe j1939 authors 		serr->ee.ee_info = J1939_EE_INFO_TX_ABORT;
10505b9272e9SOleksij Rempel 		state = "TX ABT";
10519d71dd0cSThe j1939 authors 		break;
10525b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_RX_RTS:
10535b9272e9SOleksij Rempel 		serr->ee.ee_errno = ENOMSG;
10545b9272e9SOleksij Rempel 		serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
10555b9272e9SOleksij Rempel 		serr->ee.ee_info = J1939_EE_INFO_RX_RTS;
10565b9272e9SOleksij Rempel 		state = "RX RTS";
10575b9272e9SOleksij Rempel 		break;
10585b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_RX_DPO:
10595b9272e9SOleksij Rempel 		serr->ee.ee_errno = ENOMSG;
10605b9272e9SOleksij Rempel 		serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
10615b9272e9SOleksij Rempel 		serr->ee.ee_info = J1939_EE_INFO_RX_DPO;
10625b9272e9SOleksij Rempel 		state = "RX DPO";
10635b9272e9SOleksij Rempel 		break;
10645b9272e9SOleksij Rempel 	case J1939_ERRQUEUE_RX_ABORT:
10655b9272e9SOleksij Rempel 		serr->ee.ee_errno = session->err;
10665b9272e9SOleksij Rempel 		serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
10675b9272e9SOleksij Rempel 		serr->ee.ee_info = J1939_EE_INFO_RX_ABORT;
10685b9272e9SOleksij Rempel 		state = "RX ABT";
10695b9272e9SOleksij Rempel 		break;
10709d71dd0cSThe j1939 authors 	}
10719d71dd0cSThe j1939 authors 
10729d71dd0cSThe j1939 authors 	serr->opt_stats = true;
1073e3390b30SEric Dumazet 	if (tsflags & SOF_TIMESTAMPING_OPT_ID)
10749d71dd0cSThe j1939 authors 		serr->ee.ee_data = session->tskey;
10759d71dd0cSThe j1939 authors 
10769d71dd0cSThe j1939 authors 	netdev_dbg(session->priv->ndev, "%s: 0x%p tskey: %i, state: %s\n",
10779d71dd0cSThe j1939 authors 		   __func__, session, session->tskey, state);
10789d71dd0cSThe j1939 authors 	err = sock_queue_err_skb(sk, skb);
10799d71dd0cSThe j1939 authors 
10809d71dd0cSThe j1939 authors 	if (err)
10819d71dd0cSThe j1939 authors 		kfree_skb(skb);
10829d71dd0cSThe j1939 authors };
10839d71dd0cSThe j1939 authors 
j1939_sk_errqueue(struct j1939_session * session,enum j1939_sk_errqueue_type type)10845b9272e9SOleksij Rempel void j1939_sk_errqueue(struct j1939_session *session,
10855b9272e9SOleksij Rempel 		       enum j1939_sk_errqueue_type type)
10865b9272e9SOleksij Rempel {
10875b9272e9SOleksij Rempel 	struct j1939_priv *priv = session->priv;
10885b9272e9SOleksij Rempel 	struct j1939_sock *jsk;
10895b9272e9SOleksij Rempel 
10905b9272e9SOleksij Rempel 	if (session->sk) {
10915b9272e9SOleksij Rempel 		/* send TX notifications to the socket of origin  */
10925b9272e9SOleksij Rempel 		__j1939_sk_errqueue(session, session->sk, type);
10935b9272e9SOleksij Rempel 		return;
10945b9272e9SOleksij Rempel 	}
10955b9272e9SOleksij Rempel 
10965b9272e9SOleksij Rempel 	/* spread RX notifications to all sockets subscribed to this session */
109726dfe112SZiqi Zhao 	read_lock_bh(&priv->j1939_socks_lock);
10985b9272e9SOleksij Rempel 	list_for_each_entry(jsk, &priv->j1939_socks, list) {
10995b9272e9SOleksij Rempel 		if (j1939_sk_recv_match_one(jsk, &session->skcb))
11005b9272e9SOleksij Rempel 			__j1939_sk_errqueue(session, &jsk->sk, type);
11015b9272e9SOleksij Rempel 	}
110226dfe112SZiqi Zhao 	read_unlock_bh(&priv->j1939_socks_lock);
11035b9272e9SOleksij Rempel };
11045b9272e9SOleksij Rempel 
j1939_sk_send_loop_abort(struct sock * sk,int err)11059d71dd0cSThe j1939 authors void j1939_sk_send_loop_abort(struct sock *sk, int err)
11069d71dd0cSThe j1939 authors {
11072a84aea8SOleksij Rempel 	struct j1939_sock *jsk = j1939_sk(sk);
11082a84aea8SOleksij Rempel 
11092a84aea8SOleksij Rempel 	if (jsk->state & J1939_SOCK_ERRQUEUE)
11102a84aea8SOleksij Rempel 		return;
11112a84aea8SOleksij Rempel 
11129d71dd0cSThe j1939 authors 	sk->sk_err = err;
11139d71dd0cSThe j1939 authors 
1114e3ae2365SAlexander Aring 	sk_error_report(sk);
11159d71dd0cSThe j1939 authors }
11169d71dd0cSThe j1939 authors 
j1939_sk_send_loop(struct j1939_priv * priv,struct sock * sk,struct msghdr * msg,size_t size)11179d71dd0cSThe j1939 authors static int j1939_sk_send_loop(struct j1939_priv *priv,  struct sock *sk,
11189d71dd0cSThe j1939 authors 			      struct msghdr *msg, size_t size)
11199d71dd0cSThe j1939 authors 
11209d71dd0cSThe j1939 authors {
11219d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sk);
11229d71dd0cSThe j1939 authors 	struct j1939_session *session = j1939_sk_get_incomplete_session(jsk);
11239d71dd0cSThe j1939 authors 	struct sk_buff *skb;
11249d71dd0cSThe j1939 authors 	size_t segment_size, todo_size;
11259d71dd0cSThe j1939 authors 	int ret = 0;
11269d71dd0cSThe j1939 authors 
11279d71dd0cSThe j1939 authors 	if (session &&
11289d71dd0cSThe j1939 authors 	    session->total_message_size != session->total_queued_size + size) {
11299d71dd0cSThe j1939 authors 		j1939_session_put(session);
11309d71dd0cSThe j1939 authors 		return -EIO;
11319d71dd0cSThe j1939 authors 	}
11329d71dd0cSThe j1939 authors 
11339d71dd0cSThe j1939 authors 	todo_size = size;
11349d71dd0cSThe j1939 authors 
1135*2a6ea31dSAlexander Hölzl 	do {
11369d71dd0cSThe j1939 authors 		struct j1939_sk_buff_cb *skcb;
11379d71dd0cSThe j1939 authors 
11389d71dd0cSThe j1939 authors 		segment_size = min_t(size_t, J1939_MAX_TP_PACKET_SIZE,
11399d71dd0cSThe j1939 authors 				     todo_size);
11409d71dd0cSThe j1939 authors 
11419d71dd0cSThe j1939 authors 		/* Allocate skb for one segment */
11429d71dd0cSThe j1939 authors 		skb = j1939_sk_alloc_skb(priv->ndev, sk, msg, segment_size,
11439d71dd0cSThe j1939 authors 					 &ret);
11449d71dd0cSThe j1939 authors 		if (ret)
11459d71dd0cSThe j1939 authors 			break;
11469d71dd0cSThe j1939 authors 
11479d71dd0cSThe j1939 authors 		skcb = j1939_skb_to_cb(skb);
11489d71dd0cSThe j1939 authors 
11499d71dd0cSThe j1939 authors 		if (!session) {
11509d71dd0cSThe j1939 authors 			/* at this point the size should be full size
11519d71dd0cSThe j1939 authors 			 * of the session
11529d71dd0cSThe j1939 authors 			 */
11539d71dd0cSThe j1939 authors 			skcb->offset = 0;
11549d71dd0cSThe j1939 authors 			session = j1939_tp_send(priv, skb, size);
11559d71dd0cSThe j1939 authors 			if (IS_ERR(session)) {
11569d71dd0cSThe j1939 authors 				ret = PTR_ERR(session);
11579d71dd0cSThe j1939 authors 				goto kfree_skb;
11589d71dd0cSThe j1939 authors 			}
11599d71dd0cSThe j1939 authors 			if (j1939_sk_queue_session(session)) {
11609d71dd0cSThe j1939 authors 				/* try to activate session if we a
11619d71dd0cSThe j1939 authors 				 * fist in the queue
11629d71dd0cSThe j1939 authors 				 */
11639d71dd0cSThe j1939 authors 				if (!j1939_session_activate(session)) {
11649d71dd0cSThe j1939 authors 					j1939_tp_schedule_txtimer(session, 0);
11659d71dd0cSThe j1939 authors 				} else {
11669d71dd0cSThe j1939 authors 					ret = -EBUSY;
11679d71dd0cSThe j1939 authors 					session->err = ret;
11689d71dd0cSThe j1939 authors 					j1939_sk_queue_drop_all(priv, jsk,
11699d71dd0cSThe j1939 authors 								EBUSY);
11709d71dd0cSThe j1939 authors 					break;
11719d71dd0cSThe j1939 authors 				}
11729d71dd0cSThe j1939 authors 			}
11739d71dd0cSThe j1939 authors 		} else {
11749d71dd0cSThe j1939 authors 			skcb->offset = session->total_queued_size;
11759d71dd0cSThe j1939 authors 			j1939_session_skb_queue(session, skb);
11769d71dd0cSThe j1939 authors 		}
11779d71dd0cSThe j1939 authors 
11789d71dd0cSThe j1939 authors 		todo_size -= segment_size;
11799d71dd0cSThe j1939 authors 		session->total_queued_size += segment_size;
1180*2a6ea31dSAlexander Hölzl 	} while (todo_size);
11819d71dd0cSThe j1939 authors 
11829d71dd0cSThe j1939 authors 	switch (ret) {
11839d71dd0cSThe j1939 authors 	case 0: /* OK */
11849d71dd0cSThe j1939 authors 		if (todo_size)
11859d71dd0cSThe j1939 authors 			netdev_warn(priv->ndev,
11869d71dd0cSThe j1939 authors 				    "no error found and not completely queued?! %zu\n",
11879d71dd0cSThe j1939 authors 				    todo_size);
11889d71dd0cSThe j1939 authors 		ret = size;
11899d71dd0cSThe j1939 authors 		break;
11909d71dd0cSThe j1939 authors 	case -ERESTARTSYS:
11919d71dd0cSThe j1939 authors 		ret = -EINTR;
1192df561f66SGustavo A. R. Silva 		fallthrough;
11939d71dd0cSThe j1939 authors 	case -EAGAIN: /* OK */
11949d71dd0cSThe j1939 authors 		if (todo_size != size)
11959d71dd0cSThe j1939 authors 			ret = size - todo_size;
11969d71dd0cSThe j1939 authors 		break;
11979d71dd0cSThe j1939 authors 	default: /* ERROR */
11989d71dd0cSThe j1939 authors 		break;
11999d71dd0cSThe j1939 authors 	}
12009d71dd0cSThe j1939 authors 
12019d71dd0cSThe j1939 authors 	if (session)
12029d71dd0cSThe j1939 authors 		j1939_session_put(session);
12039d71dd0cSThe j1939 authors 
12049d71dd0cSThe j1939 authors 	return ret;
12059d71dd0cSThe j1939 authors 
12069d71dd0cSThe j1939 authors  kfree_skb:
12079d71dd0cSThe j1939 authors 	kfree_skb(skb);
12089d71dd0cSThe j1939 authors 	return ret;
12099d71dd0cSThe j1939 authors }
12109d71dd0cSThe j1939 authors 
j1939_sk_sendmsg(struct socket * sock,struct msghdr * msg,size_t size)12119d71dd0cSThe j1939 authors static int j1939_sk_sendmsg(struct socket *sock, struct msghdr *msg,
12129d71dd0cSThe j1939 authors 			    size_t size)
12139d71dd0cSThe j1939 authors {
12149d71dd0cSThe j1939 authors 	struct sock *sk = sock->sk;
12159d71dd0cSThe j1939 authors 	struct j1939_sock *jsk = j1939_sk(sk);
1216fd81ebfeSOleksij Rempel 	struct j1939_priv *priv;
12179d71dd0cSThe j1939 authors 	int ifindex;
12189d71dd0cSThe j1939 authors 	int ret;
12199d71dd0cSThe j1939 authors 
1220fd81ebfeSOleksij Rempel 	lock_sock(sock->sk);
12219d71dd0cSThe j1939 authors 	/* various socket state tests */
1222fd81ebfeSOleksij Rempel 	if (!(jsk->state & J1939_SOCK_BOUND)) {
1223fd81ebfeSOleksij Rempel 		ret = -EBADFD;
1224fd81ebfeSOleksij Rempel 		goto sendmsg_done;
1225fd81ebfeSOleksij Rempel 	}
12269d71dd0cSThe j1939 authors 
1227fd81ebfeSOleksij Rempel 	priv = jsk->priv;
12289d71dd0cSThe j1939 authors 	ifindex = jsk->ifindex;
12299d71dd0cSThe j1939 authors 
1230fd81ebfeSOleksij Rempel 	if (!jsk->addr.src_name && jsk->addr.sa == J1939_NO_ADDR) {
12319d71dd0cSThe j1939 authors 		/* no source address assigned yet */
1232fd81ebfeSOleksij Rempel 		ret = -EBADFD;
1233fd81ebfeSOleksij Rempel 		goto sendmsg_done;
1234fd81ebfeSOleksij Rempel 	}
12359d71dd0cSThe j1939 authors 
12369d71dd0cSThe j1939 authors 	/* deal with provided destination address info */
12379d71dd0cSThe j1939 authors 	if (msg->msg_name) {
12389d71dd0cSThe j1939 authors 		struct sockaddr_can *addr = msg->msg_name;
12399d71dd0cSThe j1939 authors 
1240fd81ebfeSOleksij Rempel 		if (msg->msg_namelen < J1939_MIN_NAMELEN) {
1241fd81ebfeSOleksij Rempel 			ret = -EINVAL;
1242fd81ebfeSOleksij Rempel 			goto sendmsg_done;
1243fd81ebfeSOleksij Rempel 		}
12449d71dd0cSThe j1939 authors 
1245fd81ebfeSOleksij Rempel 		if (addr->can_family != AF_CAN) {
1246fd81ebfeSOleksij Rempel 			ret = -EINVAL;
1247fd81ebfeSOleksij Rempel 			goto sendmsg_done;
1248fd81ebfeSOleksij Rempel 		}
12499d71dd0cSThe j1939 authors 
1250fd81ebfeSOleksij Rempel 		if (addr->can_ifindex && addr->can_ifindex != ifindex) {
1251fd81ebfeSOleksij Rempel 			ret = -EBADFD;
1252fd81ebfeSOleksij Rempel 			goto sendmsg_done;
1253fd81ebfeSOleksij Rempel 		}
12549d71dd0cSThe j1939 authors 
12559d71dd0cSThe j1939 authors 		if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn) &&
1256fd81ebfeSOleksij Rempel 		    !j1939_pgn_is_clean_pdu(addr->can_addr.j1939.pgn)) {
1257fd81ebfeSOleksij Rempel 			ret = -EINVAL;
1258fd81ebfeSOleksij Rempel 			goto sendmsg_done;
1259fd81ebfeSOleksij Rempel 		}
12609d71dd0cSThe j1939 authors 
12619d71dd0cSThe j1939 authors 		if (!addr->can_addr.j1939.name &&
12629d71dd0cSThe j1939 authors 		    addr->can_addr.j1939.addr == J1939_NO_ADDR &&
1263fd81ebfeSOleksij Rempel 		    !sock_flag(sk, SOCK_BROADCAST)) {
12649d71dd0cSThe j1939 authors 			/* broadcast, but SO_BROADCAST not set */
1265fd81ebfeSOleksij Rempel 			ret = -EACCES;
1266fd81ebfeSOleksij Rempel 			goto sendmsg_done;
1267fd81ebfeSOleksij Rempel 		}
12689d71dd0cSThe j1939 authors 	} else {
12699d71dd0cSThe j1939 authors 		if (!jsk->addr.dst_name && jsk->addr.da == J1939_NO_ADDR &&
1270fd81ebfeSOleksij Rempel 		    !sock_flag(sk, SOCK_BROADCAST)) {
12719d71dd0cSThe j1939 authors 			/* broadcast, but SO_BROADCAST not set */
1272fd81ebfeSOleksij Rempel 			ret = -EACCES;
1273fd81ebfeSOleksij Rempel 			goto sendmsg_done;
1274fd81ebfeSOleksij Rempel 		}
12759d71dd0cSThe j1939 authors 	}
12769d71dd0cSThe j1939 authors 
12779d71dd0cSThe j1939 authors 	ret = j1939_sk_send_loop(priv, sk, msg, size);
12789d71dd0cSThe j1939 authors 
1279fd81ebfeSOleksij Rempel sendmsg_done:
1280fd81ebfeSOleksij Rempel 	release_sock(sock->sk);
1281fd81ebfeSOleksij Rempel 
12829d71dd0cSThe j1939 authors 	return ret;
12839d71dd0cSThe j1939 authors }
12849d71dd0cSThe j1939 authors 
j1939_sk_netdev_event_netdown(struct j1939_priv * priv)12859d71dd0cSThe j1939 authors void j1939_sk_netdev_event_netdown(struct j1939_priv *priv)
12869d71dd0cSThe j1939 authors {
12879d71dd0cSThe j1939 authors 	struct j1939_sock *jsk;
12889d71dd0cSThe j1939 authors 	int error_code = ENETDOWN;
12899d71dd0cSThe j1939 authors 
129026dfe112SZiqi Zhao 	read_lock_bh(&priv->j1939_socks_lock);
12919d71dd0cSThe j1939 authors 	list_for_each_entry(jsk, &priv->j1939_socks, list) {
12929d71dd0cSThe j1939 authors 		jsk->sk.sk_err = error_code;
12939d71dd0cSThe j1939 authors 		if (!sock_flag(&jsk->sk, SOCK_DEAD))
1294e3ae2365SAlexander Aring 			sk_error_report(&jsk->sk);
12959d71dd0cSThe j1939 authors 
12969d71dd0cSThe j1939 authors 		j1939_sk_queue_drop_all(priv, jsk, error_code);
12979d71dd0cSThe j1939 authors 	}
129826dfe112SZiqi Zhao 	read_unlock_bh(&priv->j1939_socks_lock);
12999d71dd0cSThe j1939 authors }
13009d71dd0cSThe j1939 authors 
j1939_sk_no_ioctlcmd(struct socket * sock,unsigned int cmd,unsigned long arg)13019d71dd0cSThe j1939 authors static int j1939_sk_no_ioctlcmd(struct socket *sock, unsigned int cmd,
13029d71dd0cSThe j1939 authors 				unsigned long arg)
13039d71dd0cSThe j1939 authors {
13049d71dd0cSThe j1939 authors 	/* no ioctls for socket layer -> hand it down to NIC layer */
13059d71dd0cSThe j1939 authors 	return -ENOIOCTLCMD;
13069d71dd0cSThe j1939 authors }
13079d71dd0cSThe j1939 authors 
13089d71dd0cSThe j1939 authors static const struct proto_ops j1939_ops = {
13099d71dd0cSThe j1939 authors 	.family = PF_CAN,
13109d71dd0cSThe j1939 authors 	.release = j1939_sk_release,
13119d71dd0cSThe j1939 authors 	.bind = j1939_sk_bind,
13129d71dd0cSThe j1939 authors 	.connect = j1939_sk_connect,
13139d71dd0cSThe j1939 authors 	.socketpair = sock_no_socketpair,
13149d71dd0cSThe j1939 authors 	.accept = sock_no_accept,
13159d71dd0cSThe j1939 authors 	.getname = j1939_sk_getname,
13169d71dd0cSThe j1939 authors 	.poll = datagram_poll,
13179d71dd0cSThe j1939 authors 	.ioctl = j1939_sk_no_ioctlcmd,
13189d71dd0cSThe j1939 authors 	.listen = sock_no_listen,
13199d71dd0cSThe j1939 authors 	.shutdown = sock_no_shutdown,
13209d71dd0cSThe j1939 authors 	.setsockopt = j1939_sk_setsockopt,
13219d71dd0cSThe j1939 authors 	.getsockopt = j1939_sk_getsockopt,
13229d71dd0cSThe j1939 authors 	.sendmsg = j1939_sk_sendmsg,
13239d71dd0cSThe j1939 authors 	.recvmsg = j1939_sk_recvmsg,
13249d71dd0cSThe j1939 authors 	.mmap = sock_no_mmap,
13259d71dd0cSThe j1939 authors };
13269d71dd0cSThe j1939 authors 
13279d71dd0cSThe j1939 authors static struct proto j1939_proto __read_mostly = {
13289d71dd0cSThe j1939 authors 	.name = "CAN_J1939",
13299d71dd0cSThe j1939 authors 	.owner = THIS_MODULE,
13309d71dd0cSThe j1939 authors 	.obj_size = sizeof(struct j1939_sock),
13319d71dd0cSThe j1939 authors 	.init = j1939_sk_init,
13329d71dd0cSThe j1939 authors };
13339d71dd0cSThe j1939 authors 
13349d71dd0cSThe j1939 authors const struct can_proto j1939_can_proto = {
13359d71dd0cSThe j1939 authors 	.type = SOCK_DGRAM,
13369d71dd0cSThe j1939 authors 	.protocol = CAN_J1939,
13379d71dd0cSThe j1939 authors 	.ops = &j1939_ops,
13389d71dd0cSThe j1939 authors 	.prot = &j1939_proto,
13399d71dd0cSThe j1939 authors };
1340