^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* raw.c - Raw sockets for protocol family CAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * modification, are permitted provided that the following conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * 1. Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * notice, this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * 2. Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * notice, this list of conditions and the following disclaimer in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * documentation and/or other materials provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * 3. Neither the name of Volkswagen nor the names of its contributors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * may be used to endorse or promote products derived from this software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * without specific prior written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Alternatively, provided that this notice is retained in full, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * software may be distributed under the terms of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Public License ("GPL") version 2, in which case the provisions of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * GPL apply INSTEAD OF those given above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * The provided data structures and external interfaces from this code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * are not restricted to be used by modules with a GPL compatible license.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/can.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/can/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/can/skb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/can/raw.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) MODULE_DESCRIPTION("PF_CAN raw protocol");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) MODULE_LICENSE("Dual BSD/GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) MODULE_ALIAS("can-proto-1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define RAW_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define MASK_ALL 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* A raw socket has a list of can_filters attached to it, each receiving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * the CAN frames matching that filter. If the filter list is empty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * no CAN frames will be received by the socket. The default after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * opening the socket, is to have one filter which receives all frames.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * The filter list is allocated dynamically with the exception of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * list containing only one item. This common case is optimized by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * storing the single filter in dfilter, to avoid using dynamic memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct uniqframe {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int skbcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) const struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned int join_rx_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct raw_sock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct sock sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int bound;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct list_head notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int loopback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int recv_own_msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int fd_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int join_filters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int count; /* number of active filters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct can_filter dfilter; /* default/single filter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct can_filter *filter; /* pointer to filter(s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) can_err_mask_t err_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct uniqframe __percpu *uniq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static LIST_HEAD(raw_notifier_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static DEFINE_SPINLOCK(raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static struct raw_sock *raw_busy_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* Return pointer to store the extra msg flags for raw_recvmsg().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * We use the space of one unsigned int beyond the 'struct sockaddr_can'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * in skb->cb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static inline unsigned int *raw_flags(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) sizeof(unsigned int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* return pointer after struct sockaddr_can */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static inline struct raw_sock *raw_sk(const struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return (struct raw_sock *)sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static void raw_rcv(struct sk_buff *oskb, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct sock *sk = (struct sock *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct raw_sock *ro = raw_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct sockaddr_can *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned int *pflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* check the received tx sock reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!ro->recv_own_msgs && oskb->sk == sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* do not pass non-CAN2.0 frames to a legacy socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!ro->fd_frames && oskb->len != CAN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* eliminate multiple filter matches for the same skb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (this_cpu_ptr(ro->uniq)->skb == oskb &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (ro->join_filters) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) this_cpu_inc(ro->uniq->join_rx_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* drop frame until all enabled filters matched */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) this_cpu_ptr(ro->uniq)->skb = oskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) this_cpu_ptr(ro->uniq)->join_rx_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* drop first frame to check all enabled filters? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (ro->join_filters && ro->count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* clone the given skb to be able to enqueue it into the rcv queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) skb = skb_clone(oskb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* Put the datagram to the queue so that raw_recvmsg() can get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * it from there. We need to pass the interface index to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * raw_recvmsg(). We pass a whole struct sockaddr_can in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * skb->cb containing the interface index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) sock_skb_cb_check_size(sizeof(struct sockaddr_can));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) addr = (struct sockaddr_can *)skb->cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) memset(addr, 0, sizeof(*addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) addr->can_family = AF_CAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) addr->can_ifindex = skb->dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /* add CAN specific message flags for raw_recvmsg() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) pflags = raw_flags(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *pflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (oskb->sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *pflags |= MSG_DONTROUTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (oskb->sk == sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) *pflags |= MSG_CONFIRM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (sock_queue_rcv_skb(sk, skb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static int raw_enable_filters(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct sock *sk, struct can_filter *filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) err = can_rx_register(net, dev, filter[i].can_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) filter[i].can_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) raw_rcv, sk, "raw", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /* clean up successfully registered filters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) while (--i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) can_rx_unregister(net, dev, filter[i].can_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) filter[i].can_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) raw_rcv, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int raw_enable_errfilter(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct sock *sk, can_err_mask_t err_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (err_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) err = can_rx_register(net, dev, 0, err_mask | CAN_ERR_FLAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) raw_rcv, sk, "raw", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static void raw_disable_filters(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct sock *sk, struct can_filter *filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) for (i = 0; i < count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) can_rx_unregister(net, dev, filter[i].can_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) filter[i].can_mask, raw_rcv, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static inline void raw_disable_errfilter(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) can_err_mask_t err_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (err_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) can_rx_unregister(net, dev, 0, err_mask | CAN_ERR_FLAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) raw_rcv, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static inline void raw_disable_allfilters(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) struct raw_sock *ro = raw_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) raw_disable_filters(net, dev, sk, ro->filter, ro->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) raw_disable_errfilter(net, dev, sk, ro->err_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int raw_enable_allfilters(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct raw_sock *ro = raw_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) err = raw_enable_filters(net, dev, sk, ro->filter, ro->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) err = raw_enable_errfilter(net, dev, sk, ro->err_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) raw_disable_filters(net, dev, sk, ro->filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) ro->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static void raw_notify(struct raw_sock *ro, unsigned long msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct sock *sk = &ro->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (!net_eq(dev_net(dev), sock_net(sk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (ro->ifindex != dev->ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) switch (msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) case NETDEV_UNREGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /* remove current filters & unregister */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (ro->bound)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) raw_disable_allfilters(dev_net(dev), dev, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (ro->count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) kfree(ro->filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ro->ifindex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) ro->bound = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) ro->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) sk->sk_err = ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!sock_flag(sk, SOCK_DEAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) case NETDEV_DOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) sk->sk_err = ENETDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!sock_flag(sk, SOCK_DEAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static int raw_notifier(struct notifier_block *nb, unsigned long msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct net_device *dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (dev->type != ARPHRD_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (unlikely(raw_busy_notifier)) /* Check for reentrant bug. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) spin_lock(&raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) list_for_each_entry(raw_busy_notifier, &raw_notifier_list, notifier) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) spin_unlock(&raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) raw_notify(raw_busy_notifier, msg, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) spin_lock(&raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) raw_busy_notifier = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) spin_unlock(&raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static int raw_init(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct raw_sock *ro = raw_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) ro->bound = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) ro->ifindex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /* set default filter to single entry dfilter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) ro->dfilter.can_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) ro->dfilter.can_mask = MASK_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) ro->filter = &ro->dfilter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) ro->count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /* set default loopback behaviour */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ro->loopback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) ro->recv_own_msgs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ro->fd_frames = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) ro->join_filters = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /* alloc_percpu provides zero'ed memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ro->uniq = alloc_percpu(struct uniqframe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (unlikely(!ro->uniq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* set notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) spin_lock(&raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) list_add_tail(&ro->notifier, &raw_notifier_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) spin_unlock(&raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static int raw_release(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct raw_sock *ro;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ro = raw_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) spin_lock(&raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) while (raw_busy_notifier == ro) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) spin_unlock(&raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) schedule_timeout_uninterruptible(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) spin_lock(&raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) list_del(&ro->notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) spin_unlock(&raw_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* remove current filters & unregister */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (ro->bound) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (ro->ifindex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) dev = dev_get_by_index(sock_net(sk), ro->ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) raw_disable_allfilters(dev_net(dev), dev, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) raw_disable_allfilters(sock_net(sk), NULL, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (ro->count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) kfree(ro->filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ro->ifindex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ro->bound = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) ro->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) free_percpu(ro->uniq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) sock_orphan(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) sock->sk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) struct raw_sock *ro = raw_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) int notify_enetdown = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (len < RAW_MIN_NAMELEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (addr->can_family != AF_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (ro->bound && addr->can_ifindex == ro->ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (addr->can_ifindex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) dev = dev_get_by_index(sock_net(sk), addr->can_ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (dev->type != ARPHRD_CAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (!(dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) notify_enetdown = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) ifindex = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) /* filters set by default/setsockopt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) err = raw_enable_allfilters(sock_net(sk), dev, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) ifindex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /* filters set by default/setsockopt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) err = raw_enable_allfilters(sock_net(sk), NULL, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (ro->bound) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /* unregister old filters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (ro->ifindex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) dev = dev_get_by_index(sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) ro->ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) raw_disable_allfilters(dev_net(dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) dev, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) raw_disable_allfilters(sock_net(sk), NULL, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) ro->ifindex = ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) ro->bound = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (notify_enetdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) sk->sk_err = ENETDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (!sock_flag(sk, SOCK_DEAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) int peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct raw_sock *ro = raw_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) memset(addr, 0, RAW_MIN_NAMELEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) addr->can_family = AF_CAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) addr->can_ifindex = ro->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return RAW_MIN_NAMELEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static int raw_setsockopt(struct socket *sock, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) struct raw_sock *ro = raw_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct can_filter sfilter; /* single filter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct net_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) can_err_mask_t err_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (level != SOL_CAN_RAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) case CAN_RAW_FILTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (optlen % sizeof(struct can_filter) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) count = optlen / sizeof(struct can_filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (count > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /* filter does not fit into dfilter => alloc space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) filter = memdup_sockptr(optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (IS_ERR(filter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return PTR_ERR(filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) } else if (count == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (copy_from_sockptr(&sfilter, optval, sizeof(sfilter)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (ro->bound && ro->ifindex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) dev = dev_get_by_index(sock_net(sk), ro->ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) kfree(filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) goto out_fil;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (ro->bound) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /* (try to) register the new filters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (count == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) err = raw_enable_filters(sock_net(sk), dev, sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) &sfilter, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) err = raw_enable_filters(sock_net(sk), dev, sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) filter, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) kfree(filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) goto out_fil;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /* remove old filter registrations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) raw_disable_filters(sock_net(sk), dev, sk, ro->filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) ro->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) /* remove old filter space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (ro->count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) kfree(ro->filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) /* link new filters to the socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (count == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) /* copy filter data for single filter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) ro->dfilter = sfilter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) filter = &ro->dfilter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) ro->filter = filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) ro->count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) out_fil:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) case CAN_RAW_ERR_FILTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (optlen != sizeof(err_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (copy_from_sockptr(&err_mask, optval, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) err_mask &= CAN_ERR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (ro->bound && ro->ifindex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) dev = dev_get_by_index(sock_net(sk), ro->ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) /* remove current error mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (ro->bound) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /* (try to) register the new err_mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) err = raw_enable_errfilter(sock_net(sk), dev, sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) err_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) /* remove old err_mask registration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) raw_disable_errfilter(sock_net(sk), dev, sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) ro->err_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) /* link new err_mask to the socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) ro->err_mask = err_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) case CAN_RAW_LOOPBACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (optlen != sizeof(ro->loopback))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (copy_from_sockptr(&ro->loopback, optval, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) case CAN_RAW_RECV_OWN_MSGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (optlen != sizeof(ro->recv_own_msgs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (copy_from_sockptr(&ro->recv_own_msgs, optval, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) case CAN_RAW_FD_FRAMES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (optlen != sizeof(ro->fd_frames))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) if (copy_from_sockptr(&ro->fd_frames, optval, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) case CAN_RAW_JOIN_FILTERS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (optlen != sizeof(ro->join_filters))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) if (copy_from_sockptr(&ro->join_filters, optval, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) static int raw_getsockopt(struct socket *sock, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) char __user *optval, int __user *optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) struct raw_sock *ro = raw_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) void *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) if (level != SOL_CAN_RAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (get_user(len, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) case CAN_RAW_FILTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) if (ro->count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) int fsize = ro->count * sizeof(struct can_filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (len > fsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) len = fsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) if (copy_to_user(optval, ro->filter, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) err = put_user(len, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) case CAN_RAW_ERR_FILTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) if (len > sizeof(can_err_mask_t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) len = sizeof(can_err_mask_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) val = &ro->err_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) case CAN_RAW_LOOPBACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (len > sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) len = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) val = &ro->loopback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) case CAN_RAW_RECV_OWN_MSGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) if (len > sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) len = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) val = &ro->recv_own_msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) case CAN_RAW_FD_FRAMES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (len > sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) len = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) val = &ro->fd_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) case CAN_RAW_JOIN_FILTERS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (len > sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) len = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) val = &ro->join_filters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (put_user(len, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) if (copy_to_user(optval, val, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) struct raw_sock *ro = raw_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) int ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (msg->msg_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) if (msg->msg_namelen < RAW_MIN_NAMELEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (addr->can_family != AF_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) ifindex = addr->can_ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) ifindex = ro->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) dev = dev_get_by_index(sock_net(sk), ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) if (ro->fd_frames && dev->mtu == CANFD_MTU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (unlikely(size != CANFD_MTU && size != CAN_MTU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) if (unlikely(size != CAN_MTU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) msg->msg_flags & MSG_DONTWAIT, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) can_skb_reserve(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) can_skb_prv(skb)->ifindex = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) can_skb_prv(skb)->skbcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) err = memcpy_from_msg(skb_put(skb, size), msg, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) skb_setup_tx_timestamp(skb, sk->sk_tsflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) skb->sk = sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) skb->priority = sk->sk_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) err = can_send(skb, ro->loopback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) goto send_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) put_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) send_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) int noblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) noblock = flags & MSG_DONTWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) flags &= ~MSG_DONTWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) if (flags & MSG_ERRQUEUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) return sock_recv_errqueue(sk, msg, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) SOL_CAN_RAW, SCM_CAN_RAW_ERRQUEUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) skb = skb_recv_datagram(sk, flags, noblock, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (size < skb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) msg->msg_flags |= MSG_TRUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) size = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) err = memcpy_to_msg(msg, skb->data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) skb_free_datagram(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) sock_recv_ts_and_drops(msg, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) if (msg->msg_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) __sockaddr_check_size(RAW_MIN_NAMELEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) msg->msg_namelen = RAW_MIN_NAMELEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) /* assign the flags that have been recorded in raw_rcv() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) msg->msg_flags |= *(raw_flags(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) skb_free_datagram(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) static int raw_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) /* no ioctls for socket layer -> hand it down to NIC layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) static const struct proto_ops raw_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) .family = PF_CAN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) .release = raw_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) .bind = raw_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) .connect = sock_no_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) .accept = sock_no_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) .getname = raw_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) .poll = datagram_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) .ioctl = raw_sock_no_ioctlcmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) .gettstamp = sock_gettstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) .listen = sock_no_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) .shutdown = sock_no_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) .setsockopt = raw_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) .getsockopt = raw_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) .sendmsg = raw_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) .recvmsg = raw_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) .sendpage = sock_no_sendpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) static struct proto raw_proto __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) .name = "CAN_RAW",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) .obj_size = sizeof(struct raw_sock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) .init = raw_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) static const struct can_proto raw_can_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) .type = SOCK_RAW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) .protocol = CAN_RAW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) .ops = &raw_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) .prot = &raw_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) static struct notifier_block canraw_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) .notifier_call = raw_notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) static __init int raw_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) pr_info("can: raw protocol\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) err = can_proto_register(&raw_can_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) pr_err("can: registration of raw protocol failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) register_netdevice_notifier(&canraw_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) static __exit void raw_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) can_proto_unregister(&raw_can_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) unregister_netdevice_notifier(&canraw_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) module_init(raw_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) module_exit(raw_module_exit);