]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/can/dev.h
can: can_dropped_invalid_skb(): ensure an initialized headroom in outgoing CAN sk_buffs
[mirror_ubuntu-bionic-kernel.git] / include / linux / can / dev.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
39549eef
WG
2/*
3 * linux/can/dev.h
4 *
5 * Definitions for the CAN network device driver interface
6 *
7 * Copyright (C) 2006 Andrey Volkov <avolkov@varma-el.com>
8 * Varma Electronics Oy
9 *
10 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
11 *
39549eef
WG
12 */
13
42193e3e
OH
14#ifndef _CAN_DEV_H
15#define _CAN_DEV_H
39549eef 16
829e0015 17#include <linux/can.h>
39549eef 18#include <linux/can/error.h>
996a953d 19#include <linux/can/led.h>
2785968c 20#include <linux/can/netlink.h>
3c406959 21#include <linux/can/skb.h>
2785968c 22#include <linux/netdevice.h>
39549eef
WG
23
24/*
25 * CAN mode
26 */
27enum can_mode {
28 CAN_MODE_STOP = 0,
29 CAN_MODE_START,
30 CAN_MODE_SLEEP
31};
32
33/*
34 * CAN common private data
35 */
39549eef 36struct can_priv {
9abefcb1 37 struct net_device *dev;
39549eef
WG
38 struct can_device_stats can_stats;
39
9859ccd2
OH
40 struct can_bittiming bittiming, data_bittiming;
41 const struct can_bittiming_const *bittiming_const,
42 *data_bittiming_const;
12a6075c
OH
43 const u16 *termination_const;
44 unsigned int termination_const_cnt;
45 u16 termination;
431af779
MKB
46 const u32 *bitrate_const;
47 unsigned int bitrate_const_cnt;
48 const u32 *data_bitrate_const;
49 unsigned int data_bitrate_const_cnt;
39549eef
WG
50 struct can_clock clock;
51
52 enum can_state state;
bb208f14
OH
53
54 /* CAN controller features - see include/uapi/linux/can/netlink.h */
55 u32 ctrlmode; /* current options setting */
56 u32 ctrlmode_supported; /* options that can be modified by netlink */
57 u32 ctrlmode_static; /* static enabled options for driver/hardware */
39549eef
WG
58
59 int restart_ms;
9abefcb1 60 struct delayed_work restart_work;
39549eef 61
39549eef 62 int (*do_set_bittiming)(struct net_device *dev);
9859ccd2 63 int (*do_set_data_bittiming)(struct net_device *dev);
39549eef 64 int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
12a6075c 65 int (*do_set_termination)(struct net_device *dev, u16 term);
39549eef
WG
66 int (*do_get_state)(const struct net_device *dev,
67 enum can_state *state);
52c793f2
WG
68 int (*do_get_berr_counter)(const struct net_device *dev,
69 struct can_berr_counter *bec);
a6e4bc53
WG
70
71 unsigned int echo_skb_max;
72 struct sk_buff **echo_skb;
996a953d
FB
73
74#ifdef CONFIG_CAN_LEDS
75 struct led_trigger *tx_led_trig;
76 char tx_led_trig_name[CAN_LED_NAME_SZ];
77 struct led_trigger *rx_led_trig;
78 char rx_led_trig_name[CAN_LED_NAME_SZ];
c54eb70e
YY
79 struct led_trigger *rxtx_led_trig;
80 char rxtx_led_trig_name[CAN_LED_NAME_SZ];
996a953d 81#endif
39549eef
WG
82};
83
c7cd606f
OH
84/*
85 * get_can_dlc(value) - helper macro to cast a given data length code (dlc)
86 * to __u8 and ensure the dlc value to be max. 8 bytes.
87 *
88 * To be used in the CAN netdriver receive path to ensure conformance with
89 * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
90 */
1e0625fa
OH
91#define get_can_dlc(i) (min_t(__u8, (i), CAN_MAX_DLC))
92#define get_canfd_dlc(i) (min_t(__u8, (i), CANFD_MAX_DLC))
c7cd606f 93
3c406959
OH
94/* Check for outgoing skbs that have not been created by the CAN subsystem */
95static inline bool can_skb_headroom_valid(struct net_device *dev,
96 struct sk_buff *skb)
97{
98 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
99 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
100 return false;
101
102 /* af_packet does not apply CAN skb specific settings */
103 if (skb->ip_summed == CHECKSUM_NONE) {
104 /* init headroom */
105 can_skb_prv(skb)->ifindex = dev->ifindex;
106 can_skb_prv(skb)->skbcnt = 0;
107
108 skb->ip_summed = CHECKSUM_UNNECESSARY;
109
110 /* preform proper loopback on capable devices */
111 if (dev->flags & IFF_ECHO)
112 skb->pkt_type = PACKET_LOOPBACK;
113 else
114 skb->pkt_type = PACKET_HOST;
115
116 skb_reset_mac_header(skb);
117 skb_reset_network_header(skb);
118 skb_reset_transport_header(skb);
119 }
120
121 return true;
122}
123
3ccd4c61 124/* Drop a given socketbuffer if it does not contain a valid CAN frame. */
d6fbaea5 125static inline bool can_dropped_invalid_skb(struct net_device *dev,
3ccd4c61
OH
126 struct sk_buff *skb)
127{
1e0625fa
OH
128 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
129
130 if (skb->protocol == htons(ETH_P_CAN)) {
131 if (unlikely(skb->len != CAN_MTU ||
132 cfd->len > CAN_MAX_DLEN))
133 goto inval_skb;
134 } else if (skb->protocol == htons(ETH_P_CANFD)) {
135 if (unlikely(skb->len != CANFD_MTU ||
136 cfd->len > CANFD_MAX_DLEN))
137 goto inval_skb;
138 } else
139 goto inval_skb;
3ccd4c61 140
3c406959
OH
141 if (!can_skb_headroom_valid(dev, skb))
142 goto inval_skb;
143
d6fbaea5 144 return false;
1e0625fa
OH
145
146inval_skb:
147 kfree_skb(skb);
148 dev->stats.tx_dropped++;
d6fbaea5 149 return true;
3ccd4c61
OH
150}
151
98e69016
DA
152static inline bool can_is_canfd_skb(const struct sk_buff *skb)
153{
154 /* the CAN specific type of skb is identified by its data length */
155 return skb->len == CANFD_MTU;
156}
157
bb208f14
OH
158/* helper to define static CAN controller features at device creation time */
159static inline void can_set_static_ctrlmode(struct net_device *dev,
160 u32 static_mode)
161{
162 struct can_priv *priv = netdev_priv(dev);
163
164 /* alloc_candev() succeeded => netdev_priv() is valid at this point */
165 priv->ctrlmode = static_mode;
166 priv->ctrlmode_static = static_mode;
167
168 /* override MTU which was set by default in can_setup()? */
169 if (static_mode & CAN_CTRLMODE_FD)
170 dev->mtu = CANFD_MTU;
171}
172
1e0625fa
OH
173/* get data length from can_dlc with sanitized can_dlc */
174u8 can_dlc2len(u8 can_dlc);
175
176/* map the sanitized data length to an appropriate data length code */
177u8 can_len2dlc(u8 len);
178
a6e4bc53 179struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max);
39549eef
WG
180void free_candev(struct net_device *dev);
181
bf03a537
KVD
182/* a candev safe wrapper around netdev_priv */
183struct can_priv *safe_candev_priv(struct net_device *dev);
184
39549eef
WG
185int open_candev(struct net_device *dev);
186void close_candev(struct net_device *dev);
bc05a894 187int can_change_mtu(struct net_device *dev, int new_mtu);
39549eef
WG
188
189int register_candev(struct net_device *dev);
190void unregister_candev(struct net_device *dev);
191
192int can_restart_now(struct net_device *dev);
193void can_bus_off(struct net_device *dev);
194
bac78aab
AY
195void can_change_state(struct net_device *dev, struct can_frame *cf,
196 enum can_state tx_state, enum can_state rx_state);
197
a6e4bc53
WG
198void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
199 unsigned int idx);
ec4a2746 200struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr);
cf5046b3 201unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
a6e4bc53 202void can_free_echo_skb(struct net_device *dev, unsigned int idx);
39549eef 203
7b6856a0 204struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
cb2518ca
SG
205struct sk_buff *alloc_canfd_skb(struct net_device *dev,
206 struct canfd_frame **cfd);
7b6856a0
WG
207struct sk_buff *alloc_can_err_skb(struct net_device *dev,
208 struct can_frame **cf);
209
42193e3e 210#endif /* !_CAN_DEV_H */