isotp.c 45 KB

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