isotp.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. /* isotp.c - ISO 15765-2 CAN transport protocol for protocol family CAN
  3. *
  4. * This implementation does not provide ISO-TP specific return values to the
  5. * userspace.
  6. *
  7. * - RX path timeout of data reception leads to -ETIMEDOUT
  8. * - RX path SN mismatch leads to -EILSEQ
  9. * - RX path data reception with wrong padding leads to -EBADMSG
  10. * - TX path flowcontrol reception timeout leads to -ECOMM
  11. * - TX path flowcontrol reception overflow leads to -EMSGSIZE
  12. * - TX path flowcontrol reception with wrong layout/padding leads to -EBADMSG
  13. * - when a transfer (tx) is on the run the next write() blocks until it's done
  14. * - use CAN_ISOTP_WAIT_TX_DONE flag to block the caller until the PDU is sent
  15. * - as we have static buffers the check whether the PDU fits into the buffer
  16. * is done at FF reception time (no support for sending 'wait frames')
  17. *
  18. * Copyright (c) 2020 Volkswagen Group Electronic Research
  19. * All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. * 1. Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in the
  28. * documentation and/or other materials provided with the distribution.
  29. * 3. Neither the name of Volkswagen nor the names of its contributors
  30. * may be used to endorse or promote products derived from this software
  31. * without specific prior written permission.
  32. *
  33. * Alternatively, provided that this notice is retained in full, this
  34. * software may be distributed under the terms of the GNU General
  35. * Public License ("GPL") version 2, in which case the provisions of the
  36. * GPL apply INSTEAD OF those given above.
  37. *
  38. * The provided data structures and external interfaces from this code
  39. * are not restricted to be used by modules with a GPL compatible license.
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  42. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  43. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  44. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  45. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  48. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  49. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  50. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  51. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  52. * DAMAGE.
  53. */
  54. #include <linux/module.h>
  55. #include <linux/init.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/spinlock.h>
  58. #include <linux/hrtimer.h>
  59. #include <linux/wait.h>
  60. #include <linux/uio.h>
  61. #include <linux/net.h>
  62. #include <linux/netdevice.h>
  63. #include <linux/socket.h>
  64. #include <linux/if_arp.h>
  65. #include <linux/skbuff.h>
  66. #include <linux/can.h>
  67. #include <linux/can/core.h>
  68. #include <linux/can/skb.h>
  69. #include <linux/can/isotp.h>
  70. #include <linux/slab.h>
  71. #include <net/sock.h>
  72. #include <net/net_namespace.h>
  73. MODULE_DESCRIPTION("PF_CAN ISO 15765-2 transport protocol");
  74. MODULE_LICENSE("Dual BSD/GPL");
  75. MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
  76. MODULE_ALIAS("can-proto-6");
  77. #define ISOTP_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_addr.tp)
  78. #define SINGLE_MASK(id) (((id) & CAN_EFF_FLAG) ? \
  79. (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
  80. (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
  81. /* Since ISO 15765-2:2016 the CAN isotp protocol supports more than 4095
  82. * byte per ISO PDU as the FF_DL can take full 32 bit values (4 Gbyte).
  83. * We would need some good concept to handle this between user space and
  84. * kernel space. For now set the static buffer to something about 8 kbyte
  85. * to be able to test this new functionality.
  86. */
  87. #define DEFAULT_MAX_PDU_SIZE 8300
  88. /* maximum PDU size before ISO 15765-2:2016 extension was 4095 */
  89. #define MAX_12BIT_PDU_SIZE 4095
  90. /* limit the isotp pdu size from the optional module parameter to 1MByte */
  91. #define MAX_PDU_SIZE (1025 * 1024U)
  92. static unsigned int max_pdu_size __read_mostly = DEFAULT_MAX_PDU_SIZE;
  93. module_param(max_pdu_size, uint, 0444);
  94. MODULE_PARM_DESC(max_pdu_size, "maximum isotp pdu size (default "
  95. __stringify(DEFAULT_MAX_PDU_SIZE) ")");
  96. /* N_PCI type values in bits 7-4 of N_PCI bytes */
  97. #define N_PCI_SF 0x00 /* single frame */
  98. #define N_PCI_FF 0x10 /* first frame */
  99. #define N_PCI_CF 0x20 /* consecutive frame */
  100. #define N_PCI_FC 0x30 /* flow control */
  101. #define N_PCI_SZ 1 /* size of the PCI byte #1 */
  102. #define SF_PCI_SZ4 1 /* size of SingleFrame PCI including 4 bit SF_DL */
  103. #define SF_PCI_SZ8 2 /* size of SingleFrame PCI including 8 bit SF_DL */
  104. #define FF_PCI_SZ12 2 /* size of FirstFrame PCI including 12 bit FF_DL */
  105. #define FF_PCI_SZ32 6 /* size of FirstFrame PCI including 32 bit FF_DL */
  106. #define FC_CONTENT_SZ 3 /* flow control content size in byte (FS/BS/STmin) */
  107. #define ISOTP_CHECK_PADDING (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA)
  108. #define ISOTP_ALL_BC_FLAGS (CAN_ISOTP_SF_BROADCAST | CAN_ISOTP_CF_BROADCAST)
  109. /* Flow Status given in FC frame */
  110. #define ISOTP_FC_CTS 0 /* clear to send */
  111. #define ISOTP_FC_WT 1 /* wait */
  112. #define ISOTP_FC_OVFLW 2 /* overflow */
  113. #define ISOTP_FC_TIMEOUT 1 /* 1 sec */
  114. #define ISOTP_ECHO_TIMEOUT 2 /* 2 secs */
  115. enum {
  116. ISOTP_IDLE = 0,
  117. ISOTP_WAIT_FIRST_FC,
  118. ISOTP_WAIT_FC,
  119. ISOTP_WAIT_DATA,
  120. ISOTP_SENDING,
  121. ISOTP_SHUTDOWN,
  122. };
  123. struct tpcon {
  124. u8 *buf;
  125. unsigned int buflen;
  126. unsigned int len;
  127. unsigned int idx;
  128. u32 state;
  129. u8 bs;
  130. u8 sn;
  131. u8 ll_dl;
  132. u8 sbuf[DEFAULT_MAX_PDU_SIZE];
  133. };
  134. struct isotp_sock {
  135. struct sock sk;
  136. int bound;
  137. int ifindex;
  138. canid_t txid;
  139. canid_t rxid;
  140. ktime_t tx_gap;
  141. ktime_t lastrxcf_tstamp;
  142. struct hrtimer rxtimer, txtimer, txfrtimer;
  143. struct can_isotp_options opt;
  144. struct can_isotp_fc_options rxfc, txfc;
  145. struct can_isotp_ll_options ll;
  146. u32 frame_txtime;
  147. u32 force_tx_stmin;
  148. u32 force_rx_stmin;
  149. u32 cfecho; /* consecutive frame echo tag */
  150. struct tpcon rx, tx;
  151. struct list_head notifier;
  152. wait_queue_head_t wait;
  153. spinlock_t rx_lock; /* protect single thread state machine */
  154. };
  155. static LIST_HEAD(isotp_notifier_list);
  156. static DEFINE_SPINLOCK(isotp_notifier_lock);
  157. static struct isotp_sock *isotp_busy_notifier;
  158. static inline struct isotp_sock *isotp_sk(const struct sock *sk)
  159. {
  160. return (struct isotp_sock *)sk;
  161. }
  162. static u32 isotp_bc_flags(struct isotp_sock *so)
  163. {
  164. return so->opt.flags & ISOTP_ALL_BC_FLAGS;
  165. }
  166. static bool isotp_register_rxid(struct isotp_sock *so)
  167. {
  168. /* no broadcast modes => register rx_id for FC frame reception */
  169. return (isotp_bc_flags(so) == 0);
  170. }
  171. static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
  172. {
  173. struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
  174. rxtimer);
  175. struct sock *sk = &so->sk;
  176. if (so->rx.state == ISOTP_WAIT_DATA) {
  177. /* we did not get new data frames in time */
  178. /* report 'connection timed out' */
  179. sk->sk_err = ETIMEDOUT;
  180. if (!sock_flag(sk, SOCK_DEAD))
  181. sk_error_report(sk);
  182. /* reset rx state */
  183. so->rx.state = ISOTP_IDLE;
  184. }
  185. return HRTIMER_NORESTART;
  186. }
  187. static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
  188. {
  189. struct net_device *dev;
  190. struct sk_buff *nskb;
  191. struct canfd_frame *ncf;
  192. struct isotp_sock *so = isotp_sk(sk);
  193. int can_send_ret;
  194. nskb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), gfp_any());
  195. if (!nskb)
  196. return 1;
  197. dev = dev_get_by_index(sock_net(sk), so->ifindex);
  198. if (!dev) {
  199. kfree_skb(nskb);
  200. return 1;
  201. }
  202. can_skb_reserve(nskb);
  203. can_skb_prv(nskb)->ifindex = dev->ifindex;
  204. can_skb_prv(nskb)->skbcnt = 0;
  205. nskb->dev = dev;
  206. can_skb_set_owner(nskb, sk);
  207. ncf = (struct canfd_frame *)nskb->data;
  208. skb_put_zero(nskb, so->ll.mtu);
  209. /* create & send flow control reply */
  210. ncf->can_id = so->txid;
  211. if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
  212. memset(ncf->data, so->opt.txpad_content, CAN_MAX_DLEN);
  213. ncf->len = CAN_MAX_DLEN;
  214. } else {
  215. ncf->len = ae + FC_CONTENT_SZ;
  216. }
  217. ncf->data[ae] = N_PCI_FC | flowstatus;
  218. ncf->data[ae + 1] = so->rxfc.bs;
  219. ncf->data[ae + 2] = so->rxfc.stmin;
  220. if (ae)
  221. ncf->data[0] = so->opt.ext_address;
  222. ncf->flags = so->ll.tx_flags;
  223. can_send_ret = can_send(nskb, 1);
  224. if (can_send_ret)
  225. pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
  226. __func__, ERR_PTR(can_send_ret));
  227. dev_put(dev);
  228. /* reset blocksize counter */
  229. so->rx.bs = 0;
  230. /* reset last CF frame rx timestamp for rx stmin enforcement */
  231. so->lastrxcf_tstamp = ktime_set(0, 0);
  232. /* start rx timeout watchdog */
  233. hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
  234. HRTIMER_MODE_REL_SOFT);
  235. return 0;
  236. }
  237. static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
  238. {
  239. struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
  240. BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
  241. memset(addr, 0, sizeof(*addr));
  242. addr->can_family = AF_CAN;
  243. addr->can_ifindex = skb->dev->ifindex;
  244. if (sock_queue_rcv_skb(sk, skb) < 0)
  245. kfree_skb(skb);
  246. }
  247. static u8 padlen(u8 datalen)
  248. {
  249. static const u8 plen[] = {
  250. 8, 8, 8, 8, 8, 8, 8, 8, 8, /* 0 - 8 */
  251. 12, 12, 12, 12, /* 9 - 12 */
  252. 16, 16, 16, 16, /* 13 - 16 */
  253. 20, 20, 20, 20, /* 17 - 20 */
  254. 24, 24, 24, 24, /* 21 - 24 */
  255. 32, 32, 32, 32, 32, 32, 32, 32, /* 25 - 32 */
  256. 48, 48, 48, 48, 48, 48, 48, 48, /* 33 - 40 */
  257. 48, 48, 48, 48, 48, 48, 48, 48 /* 41 - 48 */
  258. };
  259. if (datalen > 48)
  260. return 64;
  261. return plen[datalen];
  262. }
  263. /* check for length optimization and return 1/true when the check fails */
  264. static int check_optimized(struct canfd_frame *cf, int start_index)
  265. {
  266. /* for CAN_DL <= 8 the start_index is equal to the CAN_DL as the
  267. * padding would start at this point. E.g. if the padding would
  268. * start at cf.data[7] cf->len has to be 7 to be optimal.
  269. * Note: The data[] index starts with zero.
  270. */
  271. if (cf->len <= CAN_MAX_DLEN)
  272. return (cf->len != start_index);
  273. /* This relation is also valid in the non-linear DLC range, where
  274. * we need to take care of the minimal next possible CAN_DL.
  275. * The correct check would be (padlen(cf->len) != padlen(start_index)).
  276. * But as cf->len can only take discrete values from 12, .., 64 at this
  277. * point the padlen(cf->len) is always equal to cf->len.
  278. */
  279. return (cf->len != padlen(start_index));
  280. }
  281. /* check padding and return 1/true when the check fails */
  282. static int check_pad(struct isotp_sock *so, struct canfd_frame *cf,
  283. int start_index, u8 content)
  284. {
  285. int i;
  286. /* no RX_PADDING value => check length of optimized frame length */
  287. if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
  288. if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
  289. return check_optimized(cf, start_index);
  290. /* no valid test against empty value => ignore frame */
  291. return 1;
  292. }
  293. /* check datalength of correctly padded CAN frame */
  294. if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
  295. cf->len != padlen(cf->len))
  296. return 1;
  297. /* check padding content */
  298. if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
  299. for (i = start_index; i < cf->len; i++)
  300. if (cf->data[i] != content)
  301. return 1;
  302. }
  303. return 0;
  304. }
  305. static void isotp_send_cframe(struct isotp_sock *so);
  306. static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
  307. {
  308. struct sock *sk = &so->sk;
  309. if (so->tx.state != ISOTP_WAIT_FC &&
  310. so->tx.state != ISOTP_WAIT_FIRST_FC)
  311. return 0;
  312. hrtimer_cancel(&so->txtimer);
  313. if ((cf->len < ae + FC_CONTENT_SZ) ||
  314. ((so->opt.flags & ISOTP_CHECK_PADDING) &&
  315. check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
  316. /* malformed PDU - report 'not a data message' */
  317. sk->sk_err = EBADMSG;
  318. if (!sock_flag(sk, SOCK_DEAD))
  319. sk_error_report(sk);
  320. so->tx.state = ISOTP_IDLE;
  321. wake_up_interruptible(&so->wait);
  322. return 1;
  323. }
  324. /* get static/dynamic communication params from first/every FC frame */
  325. if (so->tx.state == ISOTP_WAIT_FIRST_FC ||
  326. so->opt.flags & CAN_ISOTP_DYN_FC_PARMS) {
  327. so->txfc.bs = cf->data[ae + 1];
  328. so->txfc.stmin = cf->data[ae + 2];
  329. /* fix wrong STmin values according spec */
  330. if (so->txfc.stmin > 0x7F &&
  331. (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9))
  332. so->txfc.stmin = 0x7F;
  333. so->tx_gap = ktime_set(0, 0);
  334. /* add transmission time for CAN frame N_As */
  335. so->tx_gap = ktime_add_ns(so->tx_gap, so->frame_txtime);
  336. /* add waiting time for consecutive frames N_Cs */
  337. if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
  338. so->tx_gap = ktime_add_ns(so->tx_gap,
  339. so->force_tx_stmin);
  340. else if (so->txfc.stmin < 0x80)
  341. so->tx_gap = ktime_add_ns(so->tx_gap,
  342. so->txfc.stmin * 1000000);
  343. else
  344. so->tx_gap = ktime_add_ns(so->tx_gap,
  345. (so->txfc.stmin - 0xF0)
  346. * 100000);
  347. so->tx.state = ISOTP_WAIT_FC;
  348. }
  349. switch (cf->data[ae] & 0x0F) {
  350. case ISOTP_FC_CTS:
  351. so->tx.bs = 0;
  352. so->tx.state = ISOTP_SENDING;
  353. /* send CF frame and enable echo timeout handling */
  354. hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
  355. HRTIMER_MODE_REL_SOFT);
  356. isotp_send_cframe(so);
  357. break;
  358. case ISOTP_FC_WT:
  359. /* start timer to wait for next FC frame */
  360. hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
  361. HRTIMER_MODE_REL_SOFT);
  362. break;
  363. case ISOTP_FC_OVFLW:
  364. /* overflow on receiver side - report 'message too long' */
  365. sk->sk_err = EMSGSIZE;
  366. if (!sock_flag(sk, SOCK_DEAD))
  367. sk_error_report(sk);
  368. fallthrough;
  369. default:
  370. /* stop this tx job */
  371. so->tx.state = ISOTP_IDLE;
  372. wake_up_interruptible(&so->wait);
  373. }
  374. return 0;
  375. }
  376. static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
  377. struct sk_buff *skb, int len)
  378. {
  379. struct isotp_sock *so = isotp_sk(sk);
  380. struct sk_buff *nskb;
  381. hrtimer_cancel(&so->rxtimer);
  382. so->rx.state = ISOTP_IDLE;
  383. if (!len || len > cf->len - pcilen)
  384. return 1;
  385. if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
  386. check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) {
  387. /* malformed PDU - report 'not a data message' */
  388. sk->sk_err = EBADMSG;
  389. if (!sock_flag(sk, SOCK_DEAD))
  390. sk_error_report(sk);
  391. return 1;
  392. }
  393. nskb = alloc_skb(len, gfp_any());
  394. if (!nskb)
  395. return 1;
  396. memcpy(skb_put(nskb, len), &cf->data[pcilen], len);
  397. nskb->tstamp = skb->tstamp;
  398. nskb->dev = skb->dev;
  399. isotp_rcv_skb(nskb, sk);
  400. return 0;
  401. }
  402. static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
  403. {
  404. struct isotp_sock *so = isotp_sk(sk);
  405. int i;
  406. int off;
  407. int ff_pci_sz;
  408. hrtimer_cancel(&so->rxtimer);
  409. so->rx.state = ISOTP_IDLE;
  410. /* get the used sender LL_DL from the (first) CAN frame data length */
  411. so->rx.ll_dl = padlen(cf->len);
  412. /* the first frame has to use the entire frame up to LL_DL length */
  413. if (cf->len != so->rx.ll_dl)
  414. return 1;
  415. /* get the FF_DL */
  416. so->rx.len = (cf->data[ae] & 0x0F) << 8;
  417. so->rx.len += cf->data[ae + 1];
  418. /* Check for FF_DL escape sequence supporting 32 bit PDU length */
  419. if (so->rx.len) {
  420. ff_pci_sz = FF_PCI_SZ12;
  421. } else {
  422. /* FF_DL = 0 => get real length from next 4 bytes */
  423. so->rx.len = cf->data[ae + 2] << 24;
  424. so->rx.len += cf->data[ae + 3] << 16;
  425. so->rx.len += cf->data[ae + 4] << 8;
  426. so->rx.len += cf->data[ae + 5];
  427. ff_pci_sz = FF_PCI_SZ32;
  428. }
  429. /* take care of a potential SF_DL ESC offset for TX_DL > 8 */
  430. off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
  431. if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
  432. return 1;
  433. /* PDU size > default => try max_pdu_size */
  434. if (so->rx.len > so->rx.buflen && so->rx.buflen < max_pdu_size) {
  435. u8 *newbuf = kmalloc(max_pdu_size, GFP_ATOMIC);
  436. if (newbuf) {
  437. so->rx.buf = newbuf;
  438. so->rx.buflen = max_pdu_size;
  439. }
  440. }
  441. if (so->rx.len > so->rx.buflen) {
  442. /* send FC frame with overflow status */
  443. isotp_send_fc(sk, ae, ISOTP_FC_OVFLW);
  444. return 1;
  445. }
  446. /* copy the first received data bytes */
  447. so->rx.idx = 0;
  448. for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++)
  449. so->rx.buf[so->rx.idx++] = cf->data[i];
  450. /* initial setup for this pdu reception */
  451. so->rx.sn = 1;
  452. so->rx.state = ISOTP_WAIT_DATA;
  453. /* no creation of flow control frames */
  454. if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
  455. return 0;
  456. /* send our first FC frame */
  457. isotp_send_fc(sk, ae, ISOTP_FC_CTS);
  458. return 0;
  459. }
  460. static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
  461. struct sk_buff *skb)
  462. {
  463. struct isotp_sock *so = isotp_sk(sk);
  464. struct sk_buff *nskb;
  465. int i;
  466. if (so->rx.state != ISOTP_WAIT_DATA)
  467. return 0;
  468. /* drop if timestamp gap is less than force_rx_stmin nano secs */
  469. if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
  470. if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
  471. so->force_rx_stmin)
  472. return 0;
  473. so->lastrxcf_tstamp = skb->tstamp;
  474. }
  475. hrtimer_cancel(&so->rxtimer);
  476. /* CFs are never longer than the FF */
  477. if (cf->len > so->rx.ll_dl)
  478. return 1;
  479. /* CFs have usually the LL_DL length */
  480. if (cf->len < so->rx.ll_dl) {
  481. /* this is only allowed for the last CF */
  482. if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ)
  483. return 1;
  484. }
  485. if ((cf->data[ae] & 0x0F) != so->rx.sn) {
  486. /* wrong sn detected - report 'illegal byte sequence' */
  487. sk->sk_err = EILSEQ;
  488. if (!sock_flag(sk, SOCK_DEAD))
  489. sk_error_report(sk);
  490. /* reset rx state */
  491. so->rx.state = ISOTP_IDLE;
  492. return 1;
  493. }
  494. so->rx.sn++;
  495. so->rx.sn %= 16;
  496. for (i = ae + N_PCI_SZ; i < cf->len; i++) {
  497. so->rx.buf[so->rx.idx++] = cf->data[i];
  498. if (so->rx.idx >= so->rx.len)
  499. break;
  500. }
  501. if (so->rx.idx >= so->rx.len) {
  502. /* we are done */
  503. so->rx.state = ISOTP_IDLE;
  504. if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
  505. check_pad(so, cf, i + 1, so->opt.rxpad_content)) {
  506. /* malformed PDU - report 'not a data message' */
  507. sk->sk_err = EBADMSG;
  508. if (!sock_flag(sk, SOCK_DEAD))
  509. sk_error_report(sk);
  510. return 1;
  511. }
  512. nskb = alloc_skb(so->rx.len, gfp_any());
  513. if (!nskb)
  514. return 1;
  515. memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
  516. so->rx.len);
  517. nskb->tstamp = skb->tstamp;
  518. nskb->dev = skb->dev;
  519. isotp_rcv_skb(nskb, sk);
  520. return 0;
  521. }
  522. /* perform blocksize handling, if enabled */
  523. if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
  524. /* start rx timeout watchdog */
  525. hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
  526. HRTIMER_MODE_REL_SOFT);
  527. return 0;
  528. }
  529. /* no creation of flow control frames */
  530. if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
  531. return 0;
  532. /* we reached the specified blocksize so->rxfc.bs */
  533. isotp_send_fc(sk, ae, ISOTP_FC_CTS);
  534. return 0;
  535. }
  536. static void isotp_rcv(struct sk_buff *skb, void *data)
  537. {
  538. struct sock *sk = (struct sock *)data;
  539. struct isotp_sock *so = isotp_sk(sk);
  540. struct canfd_frame *cf;
  541. int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
  542. u8 n_pci_type, sf_dl;
  543. /* Strictly receive only frames with the configured MTU size
  544. * => clear separation of CAN2.0 / CAN FD transport channels
  545. */
  546. if (skb->len != so->ll.mtu)
  547. return;
  548. cf = (struct canfd_frame *)skb->data;
  549. /* if enabled: check reception of my configured extended address */
  550. if (ae && cf->data[0] != so->opt.rx_ext_address)
  551. return;
  552. n_pci_type = cf->data[ae] & 0xF0;
  553. /* Make sure the state changes and data structures stay consistent at
  554. * CAN frame reception time. This locking is not needed in real world
  555. * use cases but the inconsistency can be triggered with syzkaller.
  556. */
  557. spin_lock(&so->rx_lock);
  558. if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
  559. /* check rx/tx path half duplex expectations */
  560. if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
  561. (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
  562. goto out_unlock;
  563. }
  564. switch (n_pci_type) {
  565. case N_PCI_FC:
  566. /* tx path: flow control frame containing the FC parameters */
  567. isotp_rcv_fc(so, cf, ae);
  568. break;
  569. case N_PCI_SF:
  570. /* rx path: single frame
  571. *
  572. * As we do not have a rx.ll_dl configuration, we can only test
  573. * if the CAN frames payload length matches the LL_DL == 8
  574. * requirements - no matter if it's CAN 2.0 or CAN FD
  575. */
  576. /* get the SF_DL from the N_PCI byte */
  577. sf_dl = cf->data[ae] & 0x0F;
  578. if (cf->len <= CAN_MAX_DLEN) {
  579. isotp_rcv_sf(sk, cf, SF_PCI_SZ4 + ae, skb, sf_dl);
  580. } else {
  581. if (can_is_canfd_skb(skb)) {
  582. /* We have a CAN FD frame and CAN_DL is greater than 8:
  583. * Only frames with the SF_DL == 0 ESC value are valid.
  584. *
  585. * If so take care of the increased SF PCI size
  586. * (SF_PCI_SZ8) to point to the message content behind
  587. * the extended SF PCI info and get the real SF_DL
  588. * length value from the formerly first data byte.
  589. */
  590. if (sf_dl == 0)
  591. isotp_rcv_sf(sk, cf, SF_PCI_SZ8 + ae, skb,
  592. cf->data[SF_PCI_SZ4 + ae]);
  593. }
  594. }
  595. break;
  596. case N_PCI_FF:
  597. /* rx path: first frame */
  598. isotp_rcv_ff(sk, cf, ae);
  599. break;
  600. case N_PCI_CF:
  601. /* rx path: consecutive frame */
  602. isotp_rcv_cf(sk, cf, ae, skb);
  603. break;
  604. }
  605. out_unlock:
  606. spin_unlock(&so->rx_lock);
  607. }
  608. static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
  609. int ae, int off)
  610. {
  611. int pcilen = N_PCI_SZ + ae + off;
  612. int space = so->tx.ll_dl - pcilen;
  613. int num = min_t(int, so->tx.len - so->tx.idx, space);
  614. int i;
  615. cf->can_id = so->txid;
  616. cf->len = num + pcilen;
  617. if (num < space) {
  618. if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
  619. /* user requested padding */
  620. cf->len = padlen(cf->len);
  621. memset(cf->data, so->opt.txpad_content, cf->len);
  622. } else if (cf->len > CAN_MAX_DLEN) {
  623. /* mandatory padding for CAN FD frames */
  624. cf->len = padlen(cf->len);
  625. memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT,
  626. cf->len);
  627. }
  628. }
  629. for (i = 0; i < num; i++)
  630. cf->data[pcilen + i] = so->tx.buf[so->tx.idx++];
  631. if (ae)
  632. cf->data[0] = so->opt.ext_address;
  633. }
  634. static void isotp_send_cframe(struct isotp_sock *so)
  635. {
  636. struct sock *sk = &so->sk;
  637. struct sk_buff *skb;
  638. struct net_device *dev;
  639. struct canfd_frame *cf;
  640. int can_send_ret;
  641. int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
  642. dev = dev_get_by_index(sock_net(sk), so->ifindex);
  643. if (!dev)
  644. return;
  645. skb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), GFP_ATOMIC);
  646. if (!skb) {
  647. dev_put(dev);
  648. return;
  649. }
  650. can_skb_reserve(skb);
  651. can_skb_prv(skb)->ifindex = dev->ifindex;
  652. can_skb_prv(skb)->skbcnt = 0;
  653. cf = (struct canfd_frame *)skb->data;
  654. skb_put_zero(skb, so->ll.mtu);
  655. /* create consecutive frame */
  656. isotp_fill_dataframe(cf, so, ae, 0);
  657. /* place consecutive frame N_PCI in appropriate index */
  658. cf->data[ae] = N_PCI_CF | so->tx.sn++;
  659. so->tx.sn %= 16;
  660. so->tx.bs++;
  661. cf->flags = so->ll.tx_flags;
  662. skb->dev = dev;
  663. can_skb_set_owner(skb, sk);
  664. /* cfecho should have been zero'ed by init/isotp_rcv_echo() */
  665. if (so->cfecho)
  666. pr_notice_once("can-isotp: cfecho is %08X != 0\n", so->cfecho);
  667. /* set consecutive frame echo tag */
  668. so->cfecho = *(u32 *)cf->data;
  669. /* send frame with local echo enabled */
  670. can_send_ret = can_send(skb, 1);
  671. if (can_send_ret) {
  672. pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
  673. __func__, ERR_PTR(can_send_ret));
  674. if (can_send_ret == -ENOBUFS)
  675. pr_notice_once("can-isotp: tx queue is full\n");
  676. }
  677. dev_put(dev);
  678. }
  679. static void isotp_create_fframe(struct canfd_frame *cf, struct isotp_sock *so,
  680. int ae)
  681. {
  682. int i;
  683. int ff_pci_sz;
  684. cf->can_id = so->txid;
  685. cf->len = so->tx.ll_dl;
  686. if (ae)
  687. cf->data[0] = so->opt.ext_address;
  688. /* create N_PCI bytes with 12/32 bit FF_DL data length */
  689. if (so->tx.len > MAX_12BIT_PDU_SIZE) {
  690. /* use 32 bit FF_DL notation */
  691. cf->data[ae] = N_PCI_FF;
  692. cf->data[ae + 1] = 0;
  693. cf->data[ae + 2] = (u8)(so->tx.len >> 24) & 0xFFU;
  694. cf->data[ae + 3] = (u8)(so->tx.len >> 16) & 0xFFU;
  695. cf->data[ae + 4] = (u8)(so->tx.len >> 8) & 0xFFU;
  696. cf->data[ae + 5] = (u8)so->tx.len & 0xFFU;
  697. ff_pci_sz = FF_PCI_SZ32;
  698. } else {
  699. /* use 12 bit FF_DL notation */
  700. cf->data[ae] = (u8)(so->tx.len >> 8) | N_PCI_FF;
  701. cf->data[ae + 1] = (u8)so->tx.len & 0xFFU;
  702. ff_pci_sz = FF_PCI_SZ12;
  703. }
  704. /* add first data bytes depending on ae */
  705. for (i = ae + ff_pci_sz; i < so->tx.ll_dl; i++)
  706. cf->data[i] = so->tx.buf[so->tx.idx++];
  707. so->tx.sn = 1;
  708. }
  709. static void isotp_rcv_echo(struct sk_buff *skb, void *data)
  710. {
  711. struct sock *sk = (struct sock *)data;
  712. struct isotp_sock *so = isotp_sk(sk);
  713. struct canfd_frame *cf = (struct canfd_frame *)skb->data;
  714. /* only handle my own local echo CF/SF skb's (no FF!) */
  715. if (skb->sk != sk || so->cfecho != *(u32 *)cf->data)
  716. return;
  717. /* cancel local echo timeout */
  718. hrtimer_cancel(&so->txtimer);
  719. /* local echo skb with consecutive frame has been consumed */
  720. so->cfecho = 0;
  721. if (so->tx.idx >= so->tx.len) {
  722. /* we are done */
  723. so->tx.state = ISOTP_IDLE;
  724. wake_up_interruptible(&so->wait);
  725. return;
  726. }
  727. if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
  728. /* stop and wait for FC with timeout */
  729. so->tx.state = ISOTP_WAIT_FC;
  730. hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
  731. HRTIMER_MODE_REL_SOFT);
  732. return;
  733. }
  734. /* no gap between data frames needed => use burst mode */
  735. if (!so->tx_gap) {
  736. /* enable echo timeout handling */
  737. hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
  738. HRTIMER_MODE_REL_SOFT);
  739. isotp_send_cframe(so);
  740. return;
  741. }
  742. /* start timer to send next consecutive frame with correct delay */
  743. hrtimer_start(&so->txfrtimer, so->tx_gap, HRTIMER_MODE_REL_SOFT);
  744. }
  745. static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)
  746. {
  747. struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
  748. txtimer);
  749. struct sock *sk = &so->sk;
  750. /* don't handle timeouts in IDLE or SHUTDOWN state */
  751. if (so->tx.state == ISOTP_IDLE || so->tx.state == ISOTP_SHUTDOWN)
  752. return HRTIMER_NORESTART;
  753. /* we did not get any flow control or echo frame in time */
  754. /* report 'communication error on send' */
  755. sk->sk_err = ECOMM;
  756. if (!sock_flag(sk, SOCK_DEAD))
  757. sk_error_report(sk);
  758. /* reset tx state */
  759. so->tx.state = ISOTP_IDLE;
  760. wake_up_interruptible(&so->wait);
  761. return HRTIMER_NORESTART;
  762. }
  763. static enum hrtimer_restart isotp_txfr_timer_handler(struct hrtimer *hrtimer)
  764. {
  765. struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
  766. txfrtimer);
  767. /* start echo timeout handling and cover below protocol error */
  768. hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
  769. HRTIMER_MODE_REL_SOFT);
  770. /* cfecho should be consumed by isotp_rcv_echo() here */
  771. if (so->tx.state == ISOTP_SENDING && !so->cfecho)
  772. isotp_send_cframe(so);
  773. return HRTIMER_NORESTART;
  774. }
  775. static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  776. {
  777. struct sock *sk = sock->sk;
  778. struct isotp_sock *so = isotp_sk(sk);
  779. struct sk_buff *skb;
  780. struct net_device *dev;
  781. struct canfd_frame *cf;
  782. int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
  783. int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
  784. s64 hrtimer_sec = ISOTP_ECHO_TIMEOUT;
  785. int off;
  786. int err;
  787. if (!so->bound || so->tx.state == ISOTP_SHUTDOWN)
  788. return -EADDRNOTAVAIL;
  789. while (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE) {
  790. /* we do not support multiple buffers - for now */
  791. if (msg->msg_flags & MSG_DONTWAIT)
  792. return -EAGAIN;
  793. if (so->tx.state == ISOTP_SHUTDOWN)
  794. return -EADDRNOTAVAIL;
  795. /* wait for complete transmission of current pdu */
  796. err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
  797. if (err)
  798. goto err_event_drop;
  799. }
  800. /* PDU size > default => try max_pdu_size */
  801. if (size > so->tx.buflen && so->tx.buflen < max_pdu_size) {
  802. u8 *newbuf = kmalloc(max_pdu_size, GFP_KERNEL);
  803. if (newbuf) {
  804. so->tx.buf = newbuf;
  805. so->tx.buflen = max_pdu_size;
  806. }
  807. }
  808. if (!size || size > so->tx.buflen) {
  809. err = -EINVAL;
  810. goto err_out_drop;
  811. }
  812. /* take care of a potential SF_DL ESC offset for TX_DL > 8 */
  813. off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
  814. /* does the given data fit into a single frame for SF_BROADCAST? */
  815. if ((isotp_bc_flags(so) == CAN_ISOTP_SF_BROADCAST) &&
  816. (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
  817. err = -EINVAL;
  818. goto err_out_drop;
  819. }
  820. err = memcpy_from_msg(so->tx.buf, msg, size);
  821. if (err < 0)
  822. goto err_out_drop;
  823. dev = dev_get_by_index(sock_net(sk), so->ifindex);
  824. if (!dev) {
  825. err = -ENXIO;
  826. goto err_out_drop;
  827. }
  828. skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv),
  829. msg->msg_flags & MSG_DONTWAIT, &err);
  830. if (!skb) {
  831. dev_put(dev);
  832. goto err_out_drop;
  833. }
  834. can_skb_reserve(skb);
  835. can_skb_prv(skb)->ifindex = dev->ifindex;
  836. can_skb_prv(skb)->skbcnt = 0;
  837. so->tx.len = size;
  838. so->tx.idx = 0;
  839. cf = (struct canfd_frame *)skb->data;
  840. skb_put_zero(skb, so->ll.mtu);
  841. /* cfecho should have been zero'ed by init / former isotp_rcv_echo() */
  842. if (so->cfecho)
  843. pr_notice_once("can-isotp: uninit cfecho %08X\n", so->cfecho);
  844. /* check for single frame transmission depending on TX_DL */
  845. if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) {
  846. /* The message size generally fits into a SingleFrame - good.
  847. *
  848. * SF_DL ESC offset optimization:
  849. *
  850. * When TX_DL is greater 8 but the message would still fit
  851. * into a 8 byte CAN frame, we can omit the offset.
  852. * This prevents a protocol caused length extension from
  853. * CAN_DL = 8 to CAN_DL = 12 due to the SF_SL ESC handling.
  854. */
  855. if (size <= CAN_MAX_DLEN - SF_PCI_SZ4 - ae)
  856. off = 0;
  857. isotp_fill_dataframe(cf, so, ae, off);
  858. /* place single frame N_PCI w/o length in appropriate index */
  859. cf->data[ae] = N_PCI_SF;
  860. /* place SF_DL size value depending on the SF_DL ESC offset */
  861. if (off)
  862. cf->data[SF_PCI_SZ4 + ae] = size;
  863. else
  864. cf->data[ae] |= size;
  865. /* set CF echo tag for isotp_rcv_echo() (SF-mode) */
  866. so->cfecho = *(u32 *)cf->data;
  867. } else {
  868. /* send first frame */
  869. isotp_create_fframe(cf, so, ae);
  870. if (isotp_bc_flags(so) == CAN_ISOTP_CF_BROADCAST) {
  871. /* set timer for FC-less operation (STmin = 0) */
  872. if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
  873. so->tx_gap = ktime_set(0, so->force_tx_stmin);
  874. else
  875. so->tx_gap = ktime_set(0, so->frame_txtime);
  876. /* disable wait for FCs due to activated block size */
  877. so->txfc.bs = 0;
  878. /* set CF echo tag for isotp_rcv_echo() (CF-mode) */
  879. so->cfecho = *(u32 *)cf->data;
  880. } else {
  881. /* standard flow control check */
  882. so->tx.state = ISOTP_WAIT_FIRST_FC;
  883. /* start timeout for FC */
  884. hrtimer_sec = ISOTP_FC_TIMEOUT;
  885. /* no CF echo tag for isotp_rcv_echo() (FF-mode) */
  886. so->cfecho = 0;
  887. }
  888. }
  889. hrtimer_start(&so->txtimer, ktime_set(hrtimer_sec, 0),
  890. HRTIMER_MODE_REL_SOFT);
  891. /* send the first or only CAN frame */
  892. cf->flags = so->ll.tx_flags;
  893. skb->dev = dev;
  894. skb->sk = sk;
  895. err = can_send(skb, 1);
  896. dev_put(dev);
  897. if (err) {
  898. pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
  899. __func__, ERR_PTR(err));
  900. /* no transmission -> no timeout monitoring */
  901. hrtimer_cancel(&so->txtimer);
  902. /* reset consecutive frame echo tag */
  903. so->cfecho = 0;
  904. goto err_out_drop;
  905. }
  906. if (wait_tx_done) {
  907. /* wait for complete transmission of current pdu */
  908. err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
  909. if (err)
  910. goto err_event_drop;
  911. err = sock_error(sk);
  912. if (err)
  913. return err;
  914. }
  915. return size;
  916. err_event_drop:
  917. /* got signal: force tx state machine to be idle */
  918. so->tx.state = ISOTP_IDLE;
  919. hrtimer_cancel(&so->txfrtimer);
  920. hrtimer_cancel(&so->txtimer);
  921. err_out_drop:
  922. /* drop this PDU and unlock a potential wait queue */
  923. so->tx.state = ISOTP_IDLE;
  924. wake_up_interruptible(&so->wait);
  925. return err;
  926. }
  927. static int isotp_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  928. int flags)
  929. {
  930. struct sock *sk = sock->sk;
  931. struct sk_buff *skb;
  932. struct isotp_sock *so = isotp_sk(sk);
  933. int ret = 0;
  934. if (flags & ~(MSG_DONTWAIT | MSG_TRUNC | MSG_PEEK | MSG_CMSG_COMPAT))
  935. return -EINVAL;
  936. if (!so->bound)
  937. return -EADDRNOTAVAIL;
  938. skb = skb_recv_datagram(sk, flags, &ret);
  939. if (!skb)
  940. return ret;
  941. if (size < skb->len)
  942. msg->msg_flags |= MSG_TRUNC;
  943. else
  944. size = skb->len;
  945. ret = memcpy_to_msg(msg, skb->data, size);
  946. if (ret < 0)
  947. goto out_err;
  948. sock_recv_cmsgs(msg, sk, skb);
  949. if (msg->msg_name) {
  950. __sockaddr_check_size(ISOTP_MIN_NAMELEN);
  951. msg->msg_namelen = ISOTP_MIN_NAMELEN;
  952. memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
  953. }
  954. /* set length of return value */
  955. ret = (flags & MSG_TRUNC) ? skb->len : size;
  956. out_err:
  957. skb_free_datagram(sk, skb);
  958. return ret;
  959. }
  960. static int isotp_release(struct socket *sock)
  961. {
  962. struct sock *sk = sock->sk;
  963. struct isotp_sock *so;
  964. struct net *net;
  965. if (!sk)
  966. return 0;
  967. so = isotp_sk(sk);
  968. net = sock_net(sk);
  969. /* wait for complete transmission of current pdu */
  970. while (wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE) == 0 &&
  971. cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SHUTDOWN) != ISOTP_IDLE)
  972. ;
  973. /* force state machines to be idle also when a signal occurred */
  974. so->tx.state = ISOTP_SHUTDOWN;
  975. so->rx.state = ISOTP_IDLE;
  976. spin_lock(&isotp_notifier_lock);
  977. while (isotp_busy_notifier == so) {
  978. spin_unlock(&isotp_notifier_lock);
  979. schedule_timeout_uninterruptible(1);
  980. spin_lock(&isotp_notifier_lock);
  981. }
  982. list_del(&so->notifier);
  983. spin_unlock(&isotp_notifier_lock);
  984. lock_sock(sk);
  985. /* remove current filters & unregister */
  986. if (so->bound) {
  987. if (so->ifindex) {
  988. struct net_device *dev;
  989. dev = dev_get_by_index(net, so->ifindex);
  990. if (dev) {
  991. if (isotp_register_rxid(so))
  992. can_rx_unregister(net, dev, so->rxid,
  993. SINGLE_MASK(so->rxid),
  994. isotp_rcv, sk);
  995. can_rx_unregister(net, dev, so->txid,
  996. SINGLE_MASK(so->txid),
  997. isotp_rcv_echo, sk);
  998. dev_put(dev);
  999. synchronize_rcu();
  1000. }
  1001. }
  1002. }
  1003. hrtimer_cancel(&so->txfrtimer);
  1004. hrtimer_cancel(&so->txtimer);
  1005. hrtimer_cancel(&so->rxtimer);
  1006. so->ifindex = 0;
  1007. so->bound = 0;
  1008. if (so->rx.buf != so->rx.sbuf)
  1009. kfree(so->rx.buf);
  1010. if (so->tx.buf != so->tx.sbuf)
  1011. kfree(so->tx.buf);
  1012. sock_orphan(sk);
  1013. sock->sk = NULL;
  1014. release_sock(sk);
  1015. sock_put(sk);
  1016. return 0;
  1017. }
  1018. static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
  1019. {
  1020. struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
  1021. struct sock *sk = sock->sk;
  1022. struct isotp_sock *so = isotp_sk(sk);
  1023. struct net *net = sock_net(sk);
  1024. int ifindex;
  1025. struct net_device *dev;
  1026. canid_t tx_id = addr->can_addr.tp.tx_id;
  1027. canid_t rx_id = addr->can_addr.tp.rx_id;
  1028. int err = 0;
  1029. int notify_enetdown = 0;
  1030. if (len < ISOTP_MIN_NAMELEN)
  1031. return -EINVAL;
  1032. if (addr->can_family != AF_CAN)
  1033. return -EINVAL;
  1034. /* sanitize tx CAN identifier */
  1035. if (tx_id & CAN_EFF_FLAG)
  1036. tx_id &= (CAN_EFF_FLAG | CAN_EFF_MASK);
  1037. else
  1038. tx_id &= CAN_SFF_MASK;
  1039. /* give feedback on wrong CAN-ID value */
  1040. if (tx_id != addr->can_addr.tp.tx_id)
  1041. return -EINVAL;
  1042. /* sanitize rx CAN identifier (if needed) */
  1043. if (isotp_register_rxid(so)) {
  1044. if (rx_id & CAN_EFF_FLAG)
  1045. rx_id &= (CAN_EFF_FLAG | CAN_EFF_MASK);
  1046. else
  1047. rx_id &= CAN_SFF_MASK;
  1048. /* give feedback on wrong CAN-ID value */
  1049. if (rx_id != addr->can_addr.tp.rx_id)
  1050. return -EINVAL;
  1051. }
  1052. if (!addr->can_ifindex)
  1053. return -ENODEV;
  1054. lock_sock(sk);
  1055. if (so->bound) {
  1056. err = -EINVAL;
  1057. goto out;
  1058. }
  1059. /* ensure different CAN IDs when the rx_id is to be registered */
  1060. if (isotp_register_rxid(so) && rx_id == tx_id) {
  1061. err = -EADDRNOTAVAIL;
  1062. goto out;
  1063. }
  1064. dev = dev_get_by_index(net, addr->can_ifindex);
  1065. if (!dev) {
  1066. err = -ENODEV;
  1067. goto out;
  1068. }
  1069. if (dev->type != ARPHRD_CAN) {
  1070. dev_put(dev);
  1071. err = -ENODEV;
  1072. goto out;
  1073. }
  1074. if (dev->mtu < so->ll.mtu) {
  1075. dev_put(dev);
  1076. err = -EINVAL;
  1077. goto out;
  1078. }
  1079. if (!(dev->flags & IFF_UP))
  1080. notify_enetdown = 1;
  1081. ifindex = dev->ifindex;
  1082. if (isotp_register_rxid(so))
  1083. can_rx_register(net, dev, rx_id, SINGLE_MASK(rx_id),
  1084. isotp_rcv, sk, "isotp", sk);
  1085. /* no consecutive frame echo skb in flight */
  1086. so->cfecho = 0;
  1087. /* register for echo skb's */
  1088. can_rx_register(net, dev, tx_id, SINGLE_MASK(tx_id),
  1089. isotp_rcv_echo, sk, "isotpe", sk);
  1090. dev_put(dev);
  1091. /* switch to new settings */
  1092. so->ifindex = ifindex;
  1093. so->rxid = rx_id;
  1094. so->txid = tx_id;
  1095. so->bound = 1;
  1096. out:
  1097. release_sock(sk);
  1098. if (notify_enetdown) {
  1099. sk->sk_err = ENETDOWN;
  1100. if (!sock_flag(sk, SOCK_DEAD))
  1101. sk_error_report(sk);
  1102. }
  1103. return err;
  1104. }
  1105. static int isotp_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
  1106. {
  1107. struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
  1108. struct sock *sk = sock->sk;
  1109. struct isotp_sock *so = isotp_sk(sk);
  1110. if (peer)
  1111. return -EOPNOTSUPP;
  1112. memset(addr, 0, ISOTP_MIN_NAMELEN);
  1113. addr->can_family = AF_CAN;
  1114. addr->can_ifindex = so->ifindex;
  1115. addr->can_addr.tp.rx_id = so->rxid;
  1116. addr->can_addr.tp.tx_id = so->txid;
  1117. return ISOTP_MIN_NAMELEN;
  1118. }
  1119. static int isotp_setsockopt_locked(struct socket *sock, int level, int optname,
  1120. sockptr_t optval, unsigned int optlen)
  1121. {
  1122. struct sock *sk = sock->sk;
  1123. struct isotp_sock *so = isotp_sk(sk);
  1124. int ret = 0;
  1125. if (so->bound)
  1126. return -EISCONN;
  1127. switch (optname) {
  1128. case CAN_ISOTP_OPTS:
  1129. if (optlen != sizeof(struct can_isotp_options))
  1130. return -EINVAL;
  1131. if (copy_from_sockptr(&so->opt, optval, optlen))
  1132. return -EFAULT;
  1133. /* no separate rx_ext_address is given => use ext_address */
  1134. if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
  1135. so->opt.rx_ext_address = so->opt.ext_address;
  1136. /* these broadcast flags are not allowed together */
  1137. if (isotp_bc_flags(so) == ISOTP_ALL_BC_FLAGS) {
  1138. /* CAN_ISOTP_SF_BROADCAST is prioritized */
  1139. so->opt.flags &= ~CAN_ISOTP_CF_BROADCAST;
  1140. /* give user feedback on wrong config attempt */
  1141. ret = -EINVAL;
  1142. }
  1143. /* check for frame_txtime changes (0 => no changes) */
  1144. if (so->opt.frame_txtime) {
  1145. if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO)
  1146. so->frame_txtime = 0;
  1147. else
  1148. so->frame_txtime = so->opt.frame_txtime;
  1149. }
  1150. break;
  1151. case CAN_ISOTP_RECV_FC:
  1152. if (optlen != sizeof(struct can_isotp_fc_options))
  1153. return -EINVAL;
  1154. if (copy_from_sockptr(&so->rxfc, optval, optlen))
  1155. return -EFAULT;
  1156. break;
  1157. case CAN_ISOTP_TX_STMIN:
  1158. if (optlen != sizeof(u32))
  1159. return -EINVAL;
  1160. if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen))
  1161. return -EFAULT;
  1162. break;
  1163. case CAN_ISOTP_RX_STMIN:
  1164. if (optlen != sizeof(u32))
  1165. return -EINVAL;
  1166. if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen))
  1167. return -EFAULT;
  1168. break;
  1169. case CAN_ISOTP_LL_OPTS:
  1170. if (optlen == sizeof(struct can_isotp_ll_options)) {
  1171. struct can_isotp_ll_options ll;
  1172. if (copy_from_sockptr(&ll, optval, optlen))
  1173. return -EFAULT;
  1174. /* check for correct ISO 11898-1 DLC data length */
  1175. if (ll.tx_dl != padlen(ll.tx_dl))
  1176. return -EINVAL;
  1177. if (ll.mtu != CAN_MTU && ll.mtu != CANFD_MTU)
  1178. return -EINVAL;
  1179. if (ll.mtu == CAN_MTU &&
  1180. (ll.tx_dl > CAN_MAX_DLEN || ll.tx_flags != 0))
  1181. return -EINVAL;
  1182. memcpy(&so->ll, &ll, sizeof(ll));
  1183. /* set ll_dl for tx path to similar place as for rx */
  1184. so->tx.ll_dl = ll.tx_dl;
  1185. } else {
  1186. return -EINVAL;
  1187. }
  1188. break;
  1189. default:
  1190. ret = -ENOPROTOOPT;
  1191. }
  1192. return ret;
  1193. }
  1194. static int isotp_setsockopt(struct socket *sock, int level, int optname,
  1195. sockptr_t optval, unsigned int optlen)
  1196. {
  1197. struct sock *sk = sock->sk;
  1198. int ret;
  1199. if (level != SOL_CAN_ISOTP)
  1200. return -EINVAL;
  1201. lock_sock(sk);
  1202. ret = isotp_setsockopt_locked(sock, level, optname, optval, optlen);
  1203. release_sock(sk);
  1204. return ret;
  1205. }
  1206. static int isotp_getsockopt(struct socket *sock, int level, int optname,
  1207. char __user *optval, int __user *optlen)
  1208. {
  1209. struct sock *sk = sock->sk;
  1210. struct isotp_sock *so = isotp_sk(sk);
  1211. int len;
  1212. void *val;
  1213. if (level != SOL_CAN_ISOTP)
  1214. return -EINVAL;
  1215. if (get_user(len, optlen))
  1216. return -EFAULT;
  1217. if (len < 0)
  1218. return -EINVAL;
  1219. switch (optname) {
  1220. case CAN_ISOTP_OPTS:
  1221. len = min_t(int, len, sizeof(struct can_isotp_options));
  1222. val = &so->opt;
  1223. break;
  1224. case CAN_ISOTP_RECV_FC:
  1225. len = min_t(int, len, sizeof(struct can_isotp_fc_options));
  1226. val = &so->rxfc;
  1227. break;
  1228. case CAN_ISOTP_TX_STMIN:
  1229. len = min_t(int, len, sizeof(u32));
  1230. val = &so->force_tx_stmin;
  1231. break;
  1232. case CAN_ISOTP_RX_STMIN:
  1233. len = min_t(int, len, sizeof(u32));
  1234. val = &so->force_rx_stmin;
  1235. break;
  1236. case CAN_ISOTP_LL_OPTS:
  1237. len = min_t(int, len, sizeof(struct can_isotp_ll_options));
  1238. val = &so->ll;
  1239. break;
  1240. default:
  1241. return -ENOPROTOOPT;
  1242. }
  1243. if (put_user(len, optlen))
  1244. return -EFAULT;
  1245. if (copy_to_user(optval, val, len))
  1246. return -EFAULT;
  1247. return 0;
  1248. }
  1249. static void isotp_notify(struct isotp_sock *so, unsigned long msg,
  1250. struct net_device *dev)
  1251. {
  1252. struct sock *sk = &so->sk;
  1253. if (!net_eq(dev_net(dev), sock_net(sk)))
  1254. return;
  1255. if (so->ifindex != dev->ifindex)
  1256. return;
  1257. switch (msg) {
  1258. case NETDEV_UNREGISTER:
  1259. lock_sock(sk);
  1260. /* remove current filters & unregister */
  1261. if (so->bound) {
  1262. if (isotp_register_rxid(so))
  1263. can_rx_unregister(dev_net(dev), dev, so->rxid,
  1264. SINGLE_MASK(so->rxid),
  1265. isotp_rcv, sk);
  1266. can_rx_unregister(dev_net(dev), dev, so->txid,
  1267. SINGLE_MASK(so->txid),
  1268. isotp_rcv_echo, sk);
  1269. }
  1270. so->ifindex = 0;
  1271. so->bound = 0;
  1272. release_sock(sk);
  1273. sk->sk_err = ENODEV;
  1274. if (!sock_flag(sk, SOCK_DEAD))
  1275. sk_error_report(sk);
  1276. break;
  1277. case NETDEV_DOWN:
  1278. sk->sk_err = ENETDOWN;
  1279. if (!sock_flag(sk, SOCK_DEAD))
  1280. sk_error_report(sk);
  1281. break;
  1282. }
  1283. }
  1284. static int isotp_notifier(struct notifier_block *nb, unsigned long msg,
  1285. void *ptr)
  1286. {
  1287. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1288. if (dev->type != ARPHRD_CAN)
  1289. return NOTIFY_DONE;
  1290. if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
  1291. return NOTIFY_DONE;
  1292. if (unlikely(isotp_busy_notifier)) /* Check for reentrant bug. */
  1293. return NOTIFY_DONE;
  1294. spin_lock(&isotp_notifier_lock);
  1295. list_for_each_entry(isotp_busy_notifier, &isotp_notifier_list, notifier) {
  1296. spin_unlock(&isotp_notifier_lock);
  1297. isotp_notify(isotp_busy_notifier, msg, dev);
  1298. spin_lock(&isotp_notifier_lock);
  1299. }
  1300. isotp_busy_notifier = NULL;
  1301. spin_unlock(&isotp_notifier_lock);
  1302. return NOTIFY_DONE;
  1303. }
  1304. static int isotp_init(struct sock *sk)
  1305. {
  1306. struct isotp_sock *so = isotp_sk(sk);
  1307. so->ifindex = 0;
  1308. so->bound = 0;
  1309. so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
  1310. so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
  1311. so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
  1312. so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
  1313. so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
  1314. so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
  1315. so->frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
  1316. so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
  1317. so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
  1318. so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
  1319. so->ll.mtu = CAN_ISOTP_DEFAULT_LL_MTU;
  1320. so->ll.tx_dl = CAN_ISOTP_DEFAULT_LL_TX_DL;
  1321. so->ll.tx_flags = CAN_ISOTP_DEFAULT_LL_TX_FLAGS;
  1322. /* set ll_dl for tx path to similar place as for rx */
  1323. so->tx.ll_dl = so->ll.tx_dl;
  1324. so->rx.state = ISOTP_IDLE;
  1325. so->tx.state = ISOTP_IDLE;
  1326. so->rx.buf = so->rx.sbuf;
  1327. so->tx.buf = so->tx.sbuf;
  1328. so->rx.buflen = ARRAY_SIZE(so->rx.sbuf);
  1329. so->tx.buflen = ARRAY_SIZE(so->tx.sbuf);
  1330. hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
  1331. so->rxtimer.function = isotp_rx_timer_handler;
  1332. hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
  1333. so->txtimer.function = isotp_tx_timer_handler;
  1334. hrtimer_init(&so->txfrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
  1335. so->txfrtimer.function = isotp_txfr_timer_handler;
  1336. init_waitqueue_head(&so->wait);
  1337. spin_lock_init(&so->rx_lock);
  1338. spin_lock(&isotp_notifier_lock);
  1339. list_add_tail(&so->notifier, &isotp_notifier_list);
  1340. spin_unlock(&isotp_notifier_lock);
  1341. return 0;
  1342. }
  1343. static __poll_t isotp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1344. {
  1345. struct sock *sk = sock->sk;
  1346. struct isotp_sock *so = isotp_sk(sk);
  1347. __poll_t mask = datagram_poll(file, sock, wait);
  1348. poll_wait(file, &so->wait, wait);
  1349. /* Check for false positives due to TX state */
  1350. if ((mask & EPOLLWRNORM) && (so->tx.state != ISOTP_IDLE))
  1351. mask &= ~(EPOLLOUT | EPOLLWRNORM);
  1352. return mask;
  1353. }
  1354. static int isotp_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
  1355. unsigned long arg)
  1356. {
  1357. /* no ioctls for socket layer -> hand it down to NIC layer */
  1358. return -ENOIOCTLCMD;
  1359. }
  1360. static const struct proto_ops isotp_ops = {
  1361. .family = PF_CAN,
  1362. .release = isotp_release,
  1363. .bind = isotp_bind,
  1364. .connect = sock_no_connect,
  1365. .socketpair = sock_no_socketpair,
  1366. .accept = sock_no_accept,
  1367. .getname = isotp_getname,
  1368. .poll = isotp_poll,
  1369. .ioctl = isotp_sock_no_ioctlcmd,
  1370. .gettstamp = sock_gettstamp,
  1371. .listen = sock_no_listen,
  1372. .shutdown = sock_no_shutdown,
  1373. .setsockopt = isotp_setsockopt,
  1374. .getsockopt = isotp_getsockopt,
  1375. .sendmsg = isotp_sendmsg,
  1376. .recvmsg = isotp_recvmsg,
  1377. .mmap = sock_no_mmap,
  1378. };
  1379. static struct proto isotp_proto __read_mostly = {
  1380. .name = "CAN_ISOTP",
  1381. .owner = THIS_MODULE,
  1382. .obj_size = sizeof(struct isotp_sock),
  1383. .init = isotp_init,
  1384. };
  1385. static const struct can_proto isotp_can_proto = {
  1386. .type = SOCK_DGRAM,
  1387. .protocol = CAN_ISOTP,
  1388. .ops = &isotp_ops,
  1389. .prot = &isotp_proto,
  1390. };
  1391. static struct notifier_block canisotp_notifier = {
  1392. .notifier_call = isotp_notifier
  1393. };
  1394. static __init int isotp_module_init(void)
  1395. {
  1396. int err;
  1397. max_pdu_size = max_t(unsigned int, max_pdu_size, MAX_12BIT_PDU_SIZE);
  1398. max_pdu_size = min_t(unsigned int, max_pdu_size, MAX_PDU_SIZE);
  1399. pr_info("can: isotp protocol (max_pdu_size %d)\n", max_pdu_size);
  1400. err = can_proto_register(&isotp_can_proto);
  1401. if (err < 0)
  1402. pr_err("can: registration of isotp protocol failed %pe\n", ERR_PTR(err));
  1403. else
  1404. register_netdevice_notifier(&canisotp_notifier);
  1405. return err;
  1406. }
  1407. static __exit void isotp_module_exit(void)
  1408. {
  1409. can_proto_unregister(&isotp_can_proto);
  1410. unregister_netdevice_notifier(&canisotp_notifier);
  1411. }
  1412. module_init(isotp_module_init);
  1413. module_exit(isotp_module_exit);