1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) 2 /* gw.c - CAN frame Gateway/Router/Bridge with netlink interface 3 * 4 * Copyright (c) 2019 Volkswagen Group Electronic Research 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of Volkswagen nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * Alternatively, provided that this notice is retained in full, this 20 * software may be distributed under the terms of the GNU General 21 * Public License ("GPL") version 2, in which case the provisions of the 22 * GPL apply INSTEAD OF those given above. 23 * 24 * The provided data structures and external interfaces from this code 25 * are not restricted to be used by modules with a GPL compatible license. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 38 * DAMAGE. 39 * 40 */ 41 42 #include <linux/module.h> 43 #include <linux/init.h> 44 #include <linux/types.h> 45 #include <linux/kernel.h> 46 #include <linux/list.h> 47 #include <linux/spinlock.h> 48 #include <linux/rcupdate.h> 49 #include <linux/rculist.h> 50 #include <linux/net.h> 51 #include <linux/netdevice.h> 52 #include <linux/if_arp.h> 53 #include <linux/skbuff.h> 54 #include <linux/can.h> 55 #include <linux/can/core.h> 56 #include <linux/can/skb.h> 57 #include <linux/can/gw.h> 58 #include <net/can.h> 59 #include <net/rtnetlink.h> 60 #include <net/net_namespace.h> 61 #include <net/sock.h> 62 63 #define CAN_GW_NAME "can-gw" 64 65 MODULE_DESCRIPTION("PF_CAN netlink gateway"); 66 MODULE_LICENSE("Dual BSD/GPL"); 67 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); 68 MODULE_ALIAS(CAN_GW_NAME); 69 70 #define CGW_MIN_HOPS 1 71 #define CGW_MAX_HOPS 6 72 #define CGW_DEFAULT_HOPS 1 73 74 static unsigned char max_hops __read_mostly = CGW_DEFAULT_HOPS; 75 module_param(max_hops, byte, 0444); 76 MODULE_PARM_DESC(max_hops, 77 "maximum " CAN_GW_NAME " routing hops for CAN frames " 78 "(valid values: " __stringify(CGW_MIN_HOPS) "-" 79 __stringify(CGW_MAX_HOPS) " hops, " 80 "default: " __stringify(CGW_DEFAULT_HOPS) ")"); 81 82 static struct notifier_block notifier; 83 static struct kmem_cache *cgw_cache __read_mostly; 84 85 /* structure that contains the (on-the-fly) CAN frame modifications */ 86 struct cf_mod { 87 struct { 88 struct canfd_frame and; 89 struct canfd_frame or; 90 struct canfd_frame xor; 91 struct canfd_frame set; 92 } modframe; 93 struct { 94 u8 and; 95 u8 or; 96 u8 xor; 97 u8 set; 98 } modtype; 99 void (*modfunc[MAX_MODFUNCTIONS])(struct canfd_frame *cf, 100 struct cf_mod *mod); 101 102 /* CAN frame checksum calculation after CAN frame modifications */ 103 struct { 104 struct cgw_csum_xor xor; 105 struct cgw_csum_crc8 crc8; 106 } csum; 107 struct { 108 void (*xor)(struct canfd_frame *cf, 109 struct cgw_csum_xor *xor); 110 void (*crc8)(struct canfd_frame *cf, 111 struct cgw_csum_crc8 *crc8); 112 } csumfunc; 113 u32 uid; 114 }; 115 116 /* So far we just support CAN -> CAN routing and frame modifications. 117 * 118 * The internal can_can_gw structure contains data and attributes for 119 * a CAN -> CAN gateway job. 120 */ 121 struct can_can_gw { 122 struct can_filter filter; 123 int src_idx; 124 int dst_idx; 125 }; 126 127 /* list entry for CAN gateways jobs */ 128 struct cgw_job { 129 struct hlist_node list; 130 struct rcu_head rcu; 131 u32 handled_frames; 132 u32 dropped_frames; 133 u32 deleted_frames; 134 struct cf_mod __rcu *cf_mod; 135 union { 136 /* CAN frame data source */ 137 struct net_device *dev; 138 } src; 139 union { 140 /* CAN frame data destination */ 141 struct net_device *dev; 142 } dst; 143 union { 144 struct can_can_gw ccgw; 145 /* tbc */ 146 }; 147 u8 gwtype; 148 u8 limit_hops; 149 u16 flags; 150 }; 151 152 /* modification functions that are invoked in the hot path in can_can_gw_rcv */ 153 154 #define MODFUNC(func, op) static void func(struct canfd_frame *cf, \ 155 struct cf_mod *mod) { op ; } 156 157 MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id) 158 MODFUNC(mod_and_len, cf->len &= mod->modframe.and.len) 159 MODFUNC(mod_and_flags, cf->flags &= mod->modframe.and.flags) 160 MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data) 161 MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id) 162 MODFUNC(mod_or_len, cf->len |= mod->modframe.or.len) 163 MODFUNC(mod_or_flags, cf->flags |= mod->modframe.or.flags) 164 MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data) 165 MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id) 166 MODFUNC(mod_xor_len, cf->len ^= mod->modframe.xor.len) 167 MODFUNC(mod_xor_flags, cf->flags ^= mod->modframe.xor.flags) 168 MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data) 169 MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id) 170 MODFUNC(mod_set_len, cf->len = mod->modframe.set.len) 171 MODFUNC(mod_set_flags, cf->flags = mod->modframe.set.flags) 172 MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data) 173 174 static void mod_and_fddata(struct canfd_frame *cf, struct cf_mod *mod) 175 { 176 int i; 177 178 for (i = 0; i < CANFD_MAX_DLEN; i += 8) 179 *(u64 *)(cf->data + i) &= *(u64 *)(mod->modframe.and.data + i); 180 } 181 182 static void mod_or_fddata(struct canfd_frame *cf, struct cf_mod *mod) 183 { 184 int i; 185 186 for (i = 0; i < CANFD_MAX_DLEN; i += 8) 187 *(u64 *)(cf->data + i) |= *(u64 *)(mod->modframe.or.data + i); 188 } 189 190 static void mod_xor_fddata(struct canfd_frame *cf, struct cf_mod *mod) 191 { 192 int i; 193 194 for (i = 0; i < CANFD_MAX_DLEN; i += 8) 195 *(u64 *)(cf->data + i) ^= *(u64 *)(mod->modframe.xor.data + i); 196 } 197 198 static void mod_set_fddata(struct canfd_frame *cf, struct cf_mod *mod) 199 { 200 memcpy(cf->data, mod->modframe.set.data, CANFD_MAX_DLEN); 201 } 202 203 /* retrieve valid CC DLC value and store it into 'len' */ 204 static void mod_retrieve_ccdlc(struct canfd_frame *cf) 205 { 206 struct can_frame *ccf = (struct can_frame *)cf; 207 208 /* len8_dlc is only valid if len == CAN_MAX_DLEN */ 209 if (ccf->len != CAN_MAX_DLEN) 210 return; 211 212 /* do we have a valid len8_dlc value from 9 .. 15 ? */ 213 if (ccf->len8_dlc > CAN_MAX_DLEN && ccf->len8_dlc <= CAN_MAX_RAW_DLC) 214 ccf->len = ccf->len8_dlc; 215 } 216 217 /* convert valid CC DLC value in 'len' into struct can_frame elements */ 218 static void mod_store_ccdlc(struct canfd_frame *cf) 219 { 220 struct can_frame *ccf = (struct can_frame *)cf; 221 222 /* clear potential leftovers */ 223 ccf->len8_dlc = 0; 224 225 /* plain data length 0 .. 8 - that was easy */ 226 if (ccf->len <= CAN_MAX_DLEN) 227 return; 228 229 /* potentially broken values are caught in can_can_gw_rcv() */ 230 if (ccf->len > CAN_MAX_RAW_DLC) 231 return; 232 233 /* we have a valid dlc value from 9 .. 15 in ccf->len */ 234 ccf->len8_dlc = ccf->len; 235 ccf->len = CAN_MAX_DLEN; 236 } 237 238 static void mod_and_ccdlc(struct canfd_frame *cf, struct cf_mod *mod) 239 { 240 mod_retrieve_ccdlc(cf); 241 mod_and_len(cf, mod); 242 mod_store_ccdlc(cf); 243 } 244 245 static void mod_or_ccdlc(struct canfd_frame *cf, struct cf_mod *mod) 246 { 247 mod_retrieve_ccdlc(cf); 248 mod_or_len(cf, mod); 249 mod_store_ccdlc(cf); 250 } 251 252 static void mod_xor_ccdlc(struct canfd_frame *cf, struct cf_mod *mod) 253 { 254 mod_retrieve_ccdlc(cf); 255 mod_xor_len(cf, mod); 256 mod_store_ccdlc(cf); 257 } 258 259 static void mod_set_ccdlc(struct canfd_frame *cf, struct cf_mod *mod) 260 { 261 mod_set_len(cf, mod); 262 mod_store_ccdlc(cf); 263 } 264 265 static void canframecpy(struct canfd_frame *dst, struct can_frame *src) 266 { 267 /* Copy the struct members separately to ensure that no uninitialized 268 * data are copied in the 3 bytes hole of the struct. This is needed 269 * to make easy compares of the data in the struct cf_mod. 270 */ 271 272 dst->can_id = src->can_id; 273 dst->len = src->len; 274 *(u64 *)dst->data = *(u64 *)src->data; 275 } 276 277 static void canfdframecpy(struct canfd_frame *dst, struct canfd_frame *src) 278 { 279 /* Copy the struct members separately to ensure that no uninitialized 280 * data are copied in the 2 bytes hole of the struct. This is needed 281 * to make easy compares of the data in the struct cf_mod. 282 */ 283 284 dst->can_id = src->can_id; 285 dst->flags = src->flags; 286 dst->len = src->len; 287 memcpy(dst->data, src->data, CANFD_MAX_DLEN); 288 } 289 290 static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re, struct rtcanmsg *r) 291 { 292 s8 dlen = CAN_MAX_DLEN; 293 294 if (r->flags & CGW_FLAGS_CAN_FD) 295 dlen = CANFD_MAX_DLEN; 296 297 /* absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0] 298 * relative to received dlc -1 .. -8 : 299 * e.g. for received dlc = 8 300 * -1 => index = 7 (data[7]) 301 * -3 => index = 5 (data[5]) 302 * -8 => index = 0 (data[0]) 303 */ 304 305 if (fr >= -dlen && fr < dlen && 306 to >= -dlen && to < dlen && 307 re >= -dlen && re < dlen) 308 return 0; 309 else 310 return -EINVAL; 311 } 312 313 static inline int calc_idx(int idx, int rx_len) 314 { 315 if (idx < 0) 316 return rx_len + idx; 317 else 318 return idx; 319 } 320 321 static void cgw_csum_xor_rel(struct canfd_frame *cf, struct cgw_csum_xor *xor) 322 { 323 int from = calc_idx(xor->from_idx, cf->len); 324 int to = calc_idx(xor->to_idx, cf->len); 325 int res = calc_idx(xor->result_idx, cf->len); 326 u8 val = xor->init_xor_val; 327 int i; 328 329 if (from < 0 || to < 0 || res < 0) 330 return; 331 332 if (from <= to) { 333 for (i = from; i <= to; i++) 334 val ^= cf->data[i]; 335 } else { 336 for (i = from; i >= to; i--) 337 val ^= cf->data[i]; 338 } 339 340 cf->data[res] = val; 341 } 342 343 static void cgw_csum_xor_pos(struct canfd_frame *cf, struct cgw_csum_xor *xor) 344 { 345 u8 val = xor->init_xor_val; 346 int i; 347 348 for (i = xor->from_idx; i <= xor->to_idx; i++) 349 val ^= cf->data[i]; 350 351 cf->data[xor->result_idx] = val; 352 } 353 354 static void cgw_csum_xor_neg(struct canfd_frame *cf, struct cgw_csum_xor *xor) 355 { 356 u8 val = xor->init_xor_val; 357 int i; 358 359 for (i = xor->from_idx; i >= xor->to_idx; i--) 360 val ^= cf->data[i]; 361 362 cf->data[xor->result_idx] = val; 363 } 364 365 static void cgw_csum_crc8_rel(struct canfd_frame *cf, 366 struct cgw_csum_crc8 *crc8) 367 { 368 int from = calc_idx(crc8->from_idx, cf->len); 369 int to = calc_idx(crc8->to_idx, cf->len); 370 int res = calc_idx(crc8->result_idx, cf->len); 371 u8 crc = crc8->init_crc_val; 372 int i; 373 374 if (from < 0 || to < 0 || res < 0) 375 return; 376 377 if (from <= to) { 378 for (i = from; i <= to; i++) 379 crc = crc8->crctab[crc ^ cf->data[i]]; 380 } else { 381 for (i = from; i >= to; i--) 382 crc = crc8->crctab[crc ^ cf->data[i]]; 383 } 384 385 switch (crc8->profile) { 386 case CGW_CRC8PRF_1U8: 387 crc = crc8->crctab[crc ^ crc8->profile_data[0]]; 388 break; 389 390 case CGW_CRC8PRF_16U8: 391 crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]]; 392 break; 393 394 case CGW_CRC8PRF_SFFID_XOR: 395 crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^ 396 (cf->can_id >> 8 & 0xFF)]; 397 break; 398 } 399 400 cf->data[res] = crc ^ crc8->final_xor_val; 401 } 402 403 static void cgw_csum_crc8_pos(struct canfd_frame *cf, 404 struct cgw_csum_crc8 *crc8) 405 { 406 u8 crc = crc8->init_crc_val; 407 int i; 408 409 for (i = crc8->from_idx; i <= crc8->to_idx; i++) 410 crc = crc8->crctab[crc ^ cf->data[i]]; 411 412 switch (crc8->profile) { 413 case CGW_CRC8PRF_1U8: 414 crc = crc8->crctab[crc ^ crc8->profile_data[0]]; 415 break; 416 417 case CGW_CRC8PRF_16U8: 418 crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]]; 419 break; 420 421 case CGW_CRC8PRF_SFFID_XOR: 422 crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^ 423 (cf->can_id >> 8 & 0xFF)]; 424 break; 425 } 426 427 cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val; 428 } 429 430 static void cgw_csum_crc8_neg(struct canfd_frame *cf, 431 struct cgw_csum_crc8 *crc8) 432 { 433 u8 crc = crc8->init_crc_val; 434 int i; 435 436 for (i = crc8->from_idx; i >= crc8->to_idx; i--) 437 crc = crc8->crctab[crc ^ cf->data[i]]; 438 439 switch (crc8->profile) { 440 case CGW_CRC8PRF_1U8: 441 crc = crc8->crctab[crc ^ crc8->profile_data[0]]; 442 break; 443 444 case CGW_CRC8PRF_16U8: 445 crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]]; 446 break; 447 448 case CGW_CRC8PRF_SFFID_XOR: 449 crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^ 450 (cf->can_id >> 8 & 0xFF)]; 451 break; 452 } 453 454 cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val; 455 } 456 457 /* the receive & process & send function */ 458 static void can_can_gw_rcv(struct sk_buff *skb, void *data) 459 { 460 struct cgw_job *gwj = (struct cgw_job *)data; 461 struct canfd_frame *cf; 462 struct sk_buff *nskb; 463 struct can_skb_ext *csx, *ncsx; 464 struct cf_mod *mod; 465 int modidx = 0; 466 467 /* process strictly Classic CAN or CAN FD frames */ 468 if (gwj->flags & CGW_FLAGS_CAN_FD) { 469 if (!can_is_canfd_skb(skb)) 470 return; 471 } else { 472 if (!can_is_can_skb(skb)) 473 return; 474 } 475 476 csx = can_skb_ext_find(skb); 477 if (!csx) 478 return; 479 480 /* Do not handle CAN frames routed more than 'max_hops' times. 481 * In general we should never catch this delimiter which is intended 482 * to cover a misconfiguration protection (e.g. circular CAN routes). 483 */ 484 if (csx->can_gw_hops >= max_hops) { 485 /* indicate deleted frames due to misconfiguration */ 486 gwj->deleted_frames++; 487 return; 488 } 489 490 if (!(gwj->dst.dev->flags & IFF_UP)) { 491 gwj->dropped_frames++; 492 return; 493 } 494 495 /* is sending the skb back to the incoming interface not allowed? */ 496 if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) && 497 csx->can_iif == gwj->dst.dev->ifindex) 498 return; 499 500 /* clone the given skb, which has not been done in can_rcv() 501 * 502 * When there is at least one modification function activated, 503 * we need to copy the skb as we want to modify skb->data. 504 */ 505 mod = rcu_dereference(gwj->cf_mod); 506 if (mod->modfunc[0]) 507 nskb = skb_copy(skb, GFP_ATOMIC); 508 else 509 nskb = skb_clone(skb, GFP_ATOMIC); 510 511 if (!nskb) { 512 gwj->dropped_frames++; 513 return; 514 } 515 516 /* the cloned/copied nskb points to the skb extension of the original 517 * skb with an increased refcount. skb_ext_add() creates a copy to 518 * separate the skb extension data to modify the can_gw_hops. 519 */ 520 ncsx = skb_ext_add(nskb, SKB_EXT_CAN); 521 if (!ncsx) { 522 kfree_skb(nskb); 523 gwj->dropped_frames++; 524 return; 525 } 526 527 /* put the incremented hop counter in the cloned skb */ 528 ncsx->can_gw_hops = csx->can_gw_hops + 1; 529 530 /* force a new CAN UID generation for the routed frame */ 531 ncsx->can_skb_uid = 0; 532 533 /* first processing of this CAN frame -> adjust to private hop limit */ 534 if (gwj->limit_hops && ncsx->can_gw_hops == 1) 535 ncsx->can_gw_hops = max_hops - gwj->limit_hops + 1; 536 537 nskb->dev = gwj->dst.dev; 538 539 /* pointer to modifiable CAN frame */ 540 cf = (struct canfd_frame *)nskb->data; 541 542 /* perform preprocessed modification functions if there are any */ 543 while (modidx < MAX_MODFUNCTIONS && mod->modfunc[modidx]) 544 (*mod->modfunc[modidx++])(cf, mod); 545 546 /* Has the CAN frame been modified? */ 547 if (modidx) { 548 /* get available space for the processed CAN frame type */ 549 int max_len = nskb->len - offsetof(struct canfd_frame, data); 550 551 /* dlc may have changed, make sure it fits to the CAN frame */ 552 if (cf->len > max_len) { 553 /* delete frame due to misconfiguration */ 554 gwj->deleted_frames++; 555 kfree_skb(nskb); 556 return; 557 } 558 559 /* check for checksum updates */ 560 if (mod->csumfunc.crc8) 561 (*mod->csumfunc.crc8)(cf, &mod->csum.crc8); 562 563 if (mod->csumfunc.xor) 564 (*mod->csumfunc.xor)(cf, &mod->csum.xor); 565 } 566 567 /* clear the skb timestamp if not configured the other way */ 568 if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP)) 569 nskb->tstamp = 0; 570 571 /* send to netdevice */ 572 if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO)) 573 gwj->dropped_frames++; 574 else 575 gwj->handled_frames++; 576 } 577 578 static inline int cgw_register_filter(struct net *net, struct cgw_job *gwj) 579 { 580 return can_rx_register(net, gwj->src.dev, gwj->ccgw.filter.can_id, 581 gwj->ccgw.filter.can_mask, can_can_gw_rcv, 582 gwj, "gw", NULL); 583 } 584 585 static inline void cgw_unregister_filter(struct net *net, struct cgw_job *gwj) 586 { 587 can_rx_unregister(net, gwj->src.dev, gwj->ccgw.filter.can_id, 588 gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj); 589 } 590 591 static void cgw_job_free_rcu(struct rcu_head *rcu_head) 592 { 593 struct cgw_job *gwj = container_of(rcu_head, struct cgw_job, rcu); 594 595 /* cgw_job::cf_mod is always accessed from the same cgw_job object within 596 * the same RCU read section. Once cgw_job is scheduled for removal, 597 * cf_mod can also be removed without mandating an additional grace period. 598 */ 599 kfree(rcu_access_pointer(gwj->cf_mod)); 600 kmem_cache_free(cgw_cache, gwj); 601 } 602 603 /* Return cgw_job::cf_mod with RTNL protected section */ 604 static struct cf_mod *cgw_job_cf_mod(struct cgw_job *gwj) 605 { 606 return rcu_dereference_protected(gwj->cf_mod, rtnl_is_locked()); 607 } 608 609 static int cgw_notifier(struct notifier_block *nb, 610 unsigned long msg, void *ptr) 611 { 612 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 613 struct net *net = dev_net(dev); 614 615 if (dev->type != ARPHRD_CAN) 616 return NOTIFY_DONE; 617 618 if (msg == NETDEV_UNREGISTER) { 619 struct cgw_job *gwj = NULL; 620 struct hlist_node *nx; 621 622 ASSERT_RTNL(); 623 624 hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) { 625 if (gwj->src.dev == dev || gwj->dst.dev == dev) { 626 hlist_del(&gwj->list); 627 cgw_unregister_filter(net, gwj); 628 call_rcu(&gwj->rcu, cgw_job_free_rcu); 629 } 630 } 631 } 632 633 return NOTIFY_DONE; 634 } 635 636 static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type, 637 u32 pid, u32 seq, int flags) 638 { 639 struct rtcanmsg *rtcan; 640 struct nlmsghdr *nlh; 641 struct cf_mod *mod; 642 643 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags); 644 if (!nlh) 645 return -EMSGSIZE; 646 647 rtcan = nlmsg_data(nlh); 648 rtcan->can_family = AF_CAN; 649 rtcan->gwtype = gwj->gwtype; 650 rtcan->flags = gwj->flags; 651 652 /* add statistics if available */ 653 654 if (gwj->handled_frames) { 655 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0) 656 goto cancel; 657 } 658 659 if (gwj->dropped_frames) { 660 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0) 661 goto cancel; 662 } 663 664 if (gwj->deleted_frames) { 665 if (nla_put_u32(skb, CGW_DELETED, gwj->deleted_frames) < 0) 666 goto cancel; 667 } 668 669 /* check non default settings of attributes */ 670 671 if (gwj->limit_hops) { 672 if (nla_put_u8(skb, CGW_LIM_HOPS, gwj->limit_hops) < 0) 673 goto cancel; 674 } 675 676 mod = cgw_job_cf_mod(gwj); 677 if (gwj->flags & CGW_FLAGS_CAN_FD) { 678 struct cgw_fdframe_mod mb; 679 680 if (mod->modtype.and) { 681 memcpy(&mb.cf, &mod->modframe.and, sizeof(mb.cf)); 682 mb.modtype = mod->modtype.and; 683 if (nla_put(skb, CGW_FDMOD_AND, sizeof(mb), &mb) < 0) 684 goto cancel; 685 } 686 687 if (mod->modtype.or) { 688 memcpy(&mb.cf, &mod->modframe.or, sizeof(mb.cf)); 689 mb.modtype = mod->modtype.or; 690 if (nla_put(skb, CGW_FDMOD_OR, sizeof(mb), &mb) < 0) 691 goto cancel; 692 } 693 694 if (mod->modtype.xor) { 695 memcpy(&mb.cf, &mod->modframe.xor, sizeof(mb.cf)); 696 mb.modtype = mod->modtype.xor; 697 if (nla_put(skb, CGW_FDMOD_XOR, sizeof(mb), &mb) < 0) 698 goto cancel; 699 } 700 701 if (mod->modtype.set) { 702 memcpy(&mb.cf, &mod->modframe.set, sizeof(mb.cf)); 703 mb.modtype = mod->modtype.set; 704 if (nla_put(skb, CGW_FDMOD_SET, sizeof(mb), &mb) < 0) 705 goto cancel; 706 } 707 } else { 708 struct cgw_frame_mod mb; 709 710 if (mod->modtype.and) { 711 memcpy(&mb.cf, &mod->modframe.and, sizeof(mb.cf)); 712 mb.modtype = mod->modtype.and; 713 if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0) 714 goto cancel; 715 } 716 717 if (mod->modtype.or) { 718 memcpy(&mb.cf, &mod->modframe.or, sizeof(mb.cf)); 719 mb.modtype = mod->modtype.or; 720 if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0) 721 goto cancel; 722 } 723 724 if (mod->modtype.xor) { 725 memcpy(&mb.cf, &mod->modframe.xor, sizeof(mb.cf)); 726 mb.modtype = mod->modtype.xor; 727 if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0) 728 goto cancel; 729 } 730 731 if (mod->modtype.set) { 732 memcpy(&mb.cf, &mod->modframe.set, sizeof(mb.cf)); 733 mb.modtype = mod->modtype.set; 734 if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0) 735 goto cancel; 736 } 737 } 738 739 if (mod->uid) { 740 if (nla_put_u32(skb, CGW_MOD_UID, mod->uid) < 0) 741 goto cancel; 742 } 743 744 if (mod->csumfunc.crc8) { 745 if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN, 746 &mod->csum.crc8) < 0) 747 goto cancel; 748 } 749 750 if (mod->csumfunc.xor) { 751 if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN, 752 &mod->csum.xor) < 0) 753 goto cancel; 754 } 755 756 if (gwj->gwtype == CGW_TYPE_CAN_CAN) { 757 if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) { 758 if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter), 759 &gwj->ccgw.filter) < 0) 760 goto cancel; 761 } 762 763 if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0) 764 goto cancel; 765 766 if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0) 767 goto cancel; 768 } 769 770 nlmsg_end(skb, nlh); 771 return 0; 772 773 cancel: 774 nlmsg_cancel(skb, nlh); 775 return -EMSGSIZE; 776 } 777 778 /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */ 779 static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb) 780 { 781 struct net *net = sock_net(skb->sk); 782 struct cgw_job *gwj = NULL; 783 int idx = 0; 784 int s_idx = cb->args[0]; 785 786 rcu_read_lock(); 787 hlist_for_each_entry_rcu(gwj, &net->can.cgw_list, list) { 788 if (idx < s_idx) 789 goto cont; 790 791 if (cgw_put_job(skb, gwj, RTM_NEWROUTE, 792 NETLINK_CB(cb->skb).portid, 793 cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0) 794 break; 795 cont: 796 idx++; 797 } 798 rcu_read_unlock(); 799 800 cb->args[0] = idx; 801 802 return skb->len; 803 } 804 805 static const struct nla_policy cgw_policy[CGW_MAX + 1] = { 806 [CGW_MOD_AND] = { .len = sizeof(struct cgw_frame_mod) }, 807 [CGW_MOD_OR] = { .len = sizeof(struct cgw_frame_mod) }, 808 [CGW_MOD_XOR] = { .len = sizeof(struct cgw_frame_mod) }, 809 [CGW_MOD_SET] = { .len = sizeof(struct cgw_frame_mod) }, 810 [CGW_CS_XOR] = { .len = sizeof(struct cgw_csum_xor) }, 811 [CGW_CS_CRC8] = { .len = sizeof(struct cgw_csum_crc8) }, 812 [CGW_SRC_IF] = { .type = NLA_U32 }, 813 [CGW_DST_IF] = { .type = NLA_U32 }, 814 [CGW_FILTER] = { .len = sizeof(struct can_filter) }, 815 [CGW_LIM_HOPS] = { .type = NLA_U8 }, 816 [CGW_MOD_UID] = { .type = NLA_U32 }, 817 [CGW_FDMOD_AND] = { .len = sizeof(struct cgw_fdframe_mod) }, 818 [CGW_FDMOD_OR] = { .len = sizeof(struct cgw_fdframe_mod) }, 819 [CGW_FDMOD_XOR] = { .len = sizeof(struct cgw_fdframe_mod) }, 820 [CGW_FDMOD_SET] = { .len = sizeof(struct cgw_fdframe_mod) }, 821 }; 822 823 /* check for common and gwtype specific attributes */ 824 static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod, 825 u8 gwtype, void *gwtypeattr, u8 *limhops) 826 { 827 struct nlattr *tb[CGW_MAX + 1]; 828 struct rtcanmsg *r = nlmsg_data(nlh); 829 int modidx = 0; 830 int err = 0; 831 832 /* initialize modification & checksum data space */ 833 memset(mod, 0, sizeof(*mod)); 834 835 err = nlmsg_parse_deprecated(nlh, sizeof(struct rtcanmsg), tb, 836 CGW_MAX, cgw_policy, NULL); 837 if (err < 0) 838 return err; 839 840 if (tb[CGW_LIM_HOPS]) { 841 *limhops = nla_get_u8(tb[CGW_LIM_HOPS]); 842 843 if (*limhops < 1 || *limhops > max_hops) 844 return -EINVAL; 845 } 846 847 /* check for AND/OR/XOR/SET modifications */ 848 if (r->flags & CGW_FLAGS_CAN_FD) { 849 struct cgw_fdframe_mod mb; 850 851 if (tb[CGW_FDMOD_AND]) { 852 nla_memcpy(&mb, tb[CGW_FDMOD_AND], CGW_FDMODATTR_LEN); 853 854 canfdframecpy(&mod->modframe.and, &mb.cf); 855 mod->modtype.and = mb.modtype; 856 857 if (mb.modtype & CGW_MOD_ID) 858 mod->modfunc[modidx++] = mod_and_id; 859 860 if (mb.modtype & CGW_MOD_LEN) 861 mod->modfunc[modidx++] = mod_and_len; 862 863 if (mb.modtype & CGW_MOD_FLAGS) 864 mod->modfunc[modidx++] = mod_and_flags; 865 866 if (mb.modtype & CGW_MOD_DATA) 867 mod->modfunc[modidx++] = mod_and_fddata; 868 } 869 870 if (tb[CGW_FDMOD_OR]) { 871 nla_memcpy(&mb, tb[CGW_FDMOD_OR], CGW_FDMODATTR_LEN); 872 873 canfdframecpy(&mod->modframe.or, &mb.cf); 874 mod->modtype.or = mb.modtype; 875 876 if (mb.modtype & CGW_MOD_ID) 877 mod->modfunc[modidx++] = mod_or_id; 878 879 if (mb.modtype & CGW_MOD_LEN) 880 mod->modfunc[modidx++] = mod_or_len; 881 882 if (mb.modtype & CGW_MOD_FLAGS) 883 mod->modfunc[modidx++] = mod_or_flags; 884 885 if (mb.modtype & CGW_MOD_DATA) 886 mod->modfunc[modidx++] = mod_or_fddata; 887 } 888 889 if (tb[CGW_FDMOD_XOR]) { 890 nla_memcpy(&mb, tb[CGW_FDMOD_XOR], CGW_FDMODATTR_LEN); 891 892 canfdframecpy(&mod->modframe.xor, &mb.cf); 893 mod->modtype.xor = mb.modtype; 894 895 if (mb.modtype & CGW_MOD_ID) 896 mod->modfunc[modidx++] = mod_xor_id; 897 898 if (mb.modtype & CGW_MOD_LEN) 899 mod->modfunc[modidx++] = mod_xor_len; 900 901 if (mb.modtype & CGW_MOD_FLAGS) 902 mod->modfunc[modidx++] = mod_xor_flags; 903 904 if (mb.modtype & CGW_MOD_DATA) 905 mod->modfunc[modidx++] = mod_xor_fddata; 906 } 907 908 if (tb[CGW_FDMOD_SET]) { 909 nla_memcpy(&mb, tb[CGW_FDMOD_SET], CGW_FDMODATTR_LEN); 910 911 canfdframecpy(&mod->modframe.set, &mb.cf); 912 mod->modtype.set = mb.modtype; 913 914 if (mb.modtype & CGW_MOD_ID) 915 mod->modfunc[modidx++] = mod_set_id; 916 917 if (mb.modtype & CGW_MOD_LEN) 918 mod->modfunc[modidx++] = mod_set_len; 919 920 if (mb.modtype & CGW_MOD_FLAGS) 921 mod->modfunc[modidx++] = mod_set_flags; 922 923 if (mb.modtype & CGW_MOD_DATA) 924 mod->modfunc[modidx++] = mod_set_fddata; 925 } 926 } else { 927 struct cgw_frame_mod mb; 928 929 if (tb[CGW_MOD_AND]) { 930 nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN); 931 932 canframecpy(&mod->modframe.and, &mb.cf); 933 mod->modtype.and = mb.modtype; 934 935 if (mb.modtype & CGW_MOD_ID) 936 mod->modfunc[modidx++] = mod_and_id; 937 938 if (mb.modtype & CGW_MOD_DLC) 939 mod->modfunc[modidx++] = mod_and_ccdlc; 940 941 if (mb.modtype & CGW_MOD_DATA) 942 mod->modfunc[modidx++] = mod_and_data; 943 } 944 945 if (tb[CGW_MOD_OR]) { 946 nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN); 947 948 canframecpy(&mod->modframe.or, &mb.cf); 949 mod->modtype.or = mb.modtype; 950 951 if (mb.modtype & CGW_MOD_ID) 952 mod->modfunc[modidx++] = mod_or_id; 953 954 if (mb.modtype & CGW_MOD_DLC) 955 mod->modfunc[modidx++] = mod_or_ccdlc; 956 957 if (mb.modtype & CGW_MOD_DATA) 958 mod->modfunc[modidx++] = mod_or_data; 959 } 960 961 if (tb[CGW_MOD_XOR]) { 962 nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN); 963 964 canframecpy(&mod->modframe.xor, &mb.cf); 965 mod->modtype.xor = mb.modtype; 966 967 if (mb.modtype & CGW_MOD_ID) 968 mod->modfunc[modidx++] = mod_xor_id; 969 970 if (mb.modtype & CGW_MOD_DLC) 971 mod->modfunc[modidx++] = mod_xor_ccdlc; 972 973 if (mb.modtype & CGW_MOD_DATA) 974 mod->modfunc[modidx++] = mod_xor_data; 975 } 976 977 if (tb[CGW_MOD_SET]) { 978 nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN); 979 980 canframecpy(&mod->modframe.set, &mb.cf); 981 mod->modtype.set = mb.modtype; 982 983 if (mb.modtype & CGW_MOD_ID) 984 mod->modfunc[modidx++] = mod_set_id; 985 986 if (mb.modtype & CGW_MOD_DLC) 987 mod->modfunc[modidx++] = mod_set_ccdlc; 988 989 if (mb.modtype & CGW_MOD_DATA) 990 mod->modfunc[modidx++] = mod_set_data; 991 } 992 } 993 994 /* check for checksum operations after CAN frame modifications */ 995 if (modidx) { 996 if (tb[CGW_CS_CRC8]) { 997 struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]); 998 999 err = cgw_chk_csum_parms(c->from_idx, c->to_idx, 1000 c->result_idx, r); 1001 if (err) 1002 return err; 1003 1004 nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8], 1005 CGW_CS_CRC8_LEN); 1006 1007 /* select dedicated processing function to reduce 1008 * runtime operations in receive hot path. 1009 */ 1010 if (c->from_idx < 0 || c->to_idx < 0 || 1011 c->result_idx < 0) 1012 mod->csumfunc.crc8 = cgw_csum_crc8_rel; 1013 else if (c->from_idx <= c->to_idx) 1014 mod->csumfunc.crc8 = cgw_csum_crc8_pos; 1015 else 1016 mod->csumfunc.crc8 = cgw_csum_crc8_neg; 1017 } 1018 1019 if (tb[CGW_CS_XOR]) { 1020 struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]); 1021 1022 err = cgw_chk_csum_parms(c->from_idx, c->to_idx, 1023 c->result_idx, r); 1024 if (err) 1025 return err; 1026 1027 nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR], 1028 CGW_CS_XOR_LEN); 1029 1030 /* select dedicated processing function to reduce 1031 * runtime operations in receive hot path. 1032 */ 1033 if (c->from_idx < 0 || c->to_idx < 0 || 1034 c->result_idx < 0) 1035 mod->csumfunc.xor = cgw_csum_xor_rel; 1036 else if (c->from_idx <= c->to_idx) 1037 mod->csumfunc.xor = cgw_csum_xor_pos; 1038 else 1039 mod->csumfunc.xor = cgw_csum_xor_neg; 1040 } 1041 1042 if (tb[CGW_MOD_UID]) 1043 nla_memcpy(&mod->uid, tb[CGW_MOD_UID], sizeof(u32)); 1044 } 1045 1046 if (gwtype == CGW_TYPE_CAN_CAN) { 1047 /* check CGW_TYPE_CAN_CAN specific attributes */ 1048 struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr; 1049 1050 memset(ccgw, 0, sizeof(*ccgw)); 1051 1052 /* check for can_filter in attributes */ 1053 if (tb[CGW_FILTER]) 1054 nla_memcpy(&ccgw->filter, tb[CGW_FILTER], 1055 sizeof(struct can_filter)); 1056 1057 err = -ENODEV; 1058 1059 /* specifying two interfaces is mandatory */ 1060 if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF]) 1061 return err; 1062 1063 ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]); 1064 ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]); 1065 1066 /* both indices set to 0 for flushing all routing entries */ 1067 if (!ccgw->src_idx && !ccgw->dst_idx) 1068 return 0; 1069 1070 /* only one index set to 0 is an error */ 1071 if (!ccgw->src_idx || !ccgw->dst_idx) 1072 return err; 1073 } 1074 1075 /* add the checks for other gwtypes here */ 1076 1077 return 0; 1078 } 1079 1080 static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh, 1081 struct netlink_ext_ack *extack) 1082 { 1083 struct net *net = sock_net(skb->sk); 1084 struct rtcanmsg *r; 1085 struct cgw_job *gwj; 1086 struct cf_mod *mod; 1087 struct can_can_gw ccgw; 1088 u8 limhops = 0; 1089 int err = 0; 1090 1091 if (!netlink_capable(skb, CAP_NET_ADMIN)) 1092 return -EPERM; 1093 1094 if (nlmsg_len(nlh) < sizeof(*r)) 1095 return -EINVAL; 1096 1097 r = nlmsg_data(nlh); 1098 if (r->can_family != AF_CAN) 1099 return -EPFNOSUPPORT; 1100 1101 /* so far we only support CAN -> CAN routings */ 1102 if (r->gwtype != CGW_TYPE_CAN_CAN) 1103 return -EINVAL; 1104 1105 mod = kmalloc_obj(*mod); 1106 if (!mod) 1107 return -ENOMEM; 1108 1109 err = cgw_parse_attr(nlh, mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops); 1110 if (err < 0) 1111 goto out_free_cf; 1112 1113 if (mod->uid) { 1114 ASSERT_RTNL(); 1115 1116 /* check for updating an existing job with identical uid */ 1117 hlist_for_each_entry(gwj, &net->can.cgw_list, list) { 1118 struct cf_mod *old_cf; 1119 1120 old_cf = cgw_job_cf_mod(gwj); 1121 if (old_cf->uid != mod->uid) 1122 continue; 1123 1124 /* interfaces & filters must be identical */ 1125 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw))) { 1126 err = -EINVAL; 1127 goto out_free_cf; 1128 } 1129 1130 rcu_assign_pointer(gwj->cf_mod, mod); 1131 kfree_rcu_mightsleep(old_cf); 1132 return 0; 1133 } 1134 } 1135 1136 /* ifindex == 0 is not allowed for job creation */ 1137 if (!ccgw.src_idx || !ccgw.dst_idx) { 1138 err = -ENODEV; 1139 goto out_free_cf; 1140 } 1141 1142 gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL); 1143 if (!gwj) { 1144 err = -ENOMEM; 1145 goto out_free_cf; 1146 } 1147 1148 gwj->handled_frames = 0; 1149 gwj->dropped_frames = 0; 1150 gwj->deleted_frames = 0; 1151 gwj->flags = r->flags; 1152 gwj->gwtype = r->gwtype; 1153 gwj->limit_hops = limhops; 1154 1155 /* insert already parsed information */ 1156 RCU_INIT_POINTER(gwj->cf_mod, mod); 1157 memcpy(&gwj->ccgw, &ccgw, sizeof(ccgw)); 1158 1159 err = -ENODEV; 1160 1161 gwj->src.dev = __dev_get_by_index(net, gwj->ccgw.src_idx); 1162 1163 if (!gwj->src.dev) 1164 goto out; 1165 1166 if (gwj->src.dev->type != ARPHRD_CAN) 1167 goto out; 1168 1169 gwj->dst.dev = __dev_get_by_index(net, gwj->ccgw.dst_idx); 1170 1171 if (!gwj->dst.dev) 1172 goto out; 1173 1174 if (gwj->dst.dev->type != ARPHRD_CAN) 1175 goto out; 1176 1177 /* is sending the skb back to the incoming interface intended? */ 1178 if (gwj->src.dev == gwj->dst.dev && 1179 !(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK)) { 1180 err = -EINVAL; 1181 goto out; 1182 } 1183 1184 ASSERT_RTNL(); 1185 1186 err = cgw_register_filter(net, gwj); 1187 if (!err) 1188 hlist_add_head_rcu(&gwj->list, &net->can.cgw_list); 1189 out: 1190 if (err) { 1191 kmem_cache_free(cgw_cache, gwj); 1192 out_free_cf: 1193 kfree(mod); 1194 } 1195 return err; 1196 } 1197 1198 static void cgw_remove_all_jobs(struct net *net) 1199 { 1200 struct cgw_job *gwj = NULL; 1201 struct hlist_node *nx; 1202 1203 ASSERT_RTNL(); 1204 1205 hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) { 1206 hlist_del(&gwj->list); 1207 cgw_unregister_filter(net, gwj); 1208 call_rcu(&gwj->rcu, cgw_job_free_rcu); 1209 } 1210 } 1211 1212 static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, 1213 struct netlink_ext_ack *extack) 1214 { 1215 struct net *net = sock_net(skb->sk); 1216 struct cgw_job *gwj = NULL; 1217 struct hlist_node *nx; 1218 struct rtcanmsg *r; 1219 struct cf_mod mod; 1220 struct can_can_gw ccgw; 1221 u8 limhops = 0; 1222 int err = 0; 1223 1224 if (!netlink_capable(skb, CAP_NET_ADMIN)) 1225 return -EPERM; 1226 1227 if (nlmsg_len(nlh) < sizeof(*r)) 1228 return -EINVAL; 1229 1230 r = nlmsg_data(nlh); 1231 if (r->can_family != AF_CAN) 1232 return -EPFNOSUPPORT; 1233 1234 /* so far we only support CAN -> CAN routings */ 1235 if (r->gwtype != CGW_TYPE_CAN_CAN) 1236 return -EINVAL; 1237 1238 err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops); 1239 if (err < 0) 1240 return err; 1241 1242 /* two interface indices both set to 0 => remove all entries */ 1243 if (!ccgw.src_idx && !ccgw.dst_idx) { 1244 cgw_remove_all_jobs(net); 1245 return 0; 1246 } 1247 1248 err = -EINVAL; 1249 1250 ASSERT_RTNL(); 1251 1252 /* remove only the first matching entry */ 1253 hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) { 1254 struct cf_mod *cf_mod; 1255 1256 if (gwj->flags != r->flags) 1257 continue; 1258 1259 if (gwj->limit_hops != limhops) 1260 continue; 1261 1262 cf_mod = cgw_job_cf_mod(gwj); 1263 /* we have a match when uid is enabled and identical */ 1264 if (cf_mod->uid || mod.uid) { 1265 if (cf_mod->uid != mod.uid) 1266 continue; 1267 } else { 1268 /* no uid => check for identical modifications */ 1269 if (memcmp(cf_mod, &mod, sizeof(mod))) 1270 continue; 1271 } 1272 1273 /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */ 1274 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw))) 1275 continue; 1276 1277 hlist_del(&gwj->list); 1278 cgw_unregister_filter(net, gwj); 1279 call_rcu(&gwj->rcu, cgw_job_free_rcu); 1280 err = 0; 1281 break; 1282 } 1283 1284 return err; 1285 } 1286 1287 static int __net_init cangw_pernet_init(struct net *net) 1288 { 1289 INIT_HLIST_HEAD(&net->can.cgw_list); 1290 return 0; 1291 } 1292 1293 static void __net_exit cangw_pernet_exit_batch(struct list_head *net_list) 1294 { 1295 struct net *net; 1296 1297 rtnl_lock(); 1298 list_for_each_entry(net, net_list, exit_list) 1299 cgw_remove_all_jobs(net); 1300 rtnl_unlock(); 1301 } 1302 1303 static struct pernet_operations cangw_pernet_ops = { 1304 .init = cangw_pernet_init, 1305 .exit_batch = cangw_pernet_exit_batch, 1306 }; 1307 1308 static const struct rtnl_msg_handler cgw_rtnl_msg_handlers[] __initconst_or_module = { 1309 {.owner = THIS_MODULE, .protocol = PF_CAN, .msgtype = RTM_NEWROUTE, 1310 .doit = cgw_create_job}, 1311 {.owner = THIS_MODULE, .protocol = PF_CAN, .msgtype = RTM_DELROUTE, 1312 .doit = cgw_remove_job}, 1313 {.owner = THIS_MODULE, .protocol = PF_CAN, .msgtype = RTM_GETROUTE, 1314 .dumpit = cgw_dump_jobs}, 1315 }; 1316 1317 static __init int cgw_module_init(void) 1318 { 1319 int ret; 1320 1321 /* sanitize given module parameter */ 1322 max_hops = clamp_t(unsigned int, max_hops, CGW_MIN_HOPS, CGW_MAX_HOPS); 1323 1324 pr_info("can: netlink gateway - max_hops=%d\n", max_hops); 1325 1326 ret = register_pernet_subsys(&cangw_pernet_ops); 1327 if (ret) 1328 return ret; 1329 1330 ret = -ENOMEM; 1331 cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job), 1332 0, 0, NULL); 1333 if (!cgw_cache) 1334 goto out_cache_create; 1335 1336 /* set notifier */ 1337 notifier.notifier_call = cgw_notifier; 1338 ret = register_netdevice_notifier(¬ifier); 1339 if (ret) 1340 goto out_register_notifier; 1341 1342 ret = rtnl_register_many(cgw_rtnl_msg_handlers); 1343 if (ret) 1344 goto out_rtnl_register; 1345 1346 return 0; 1347 1348 out_rtnl_register: 1349 unregister_netdevice_notifier(¬ifier); 1350 out_register_notifier: 1351 kmem_cache_destroy(cgw_cache); 1352 out_cache_create: 1353 unregister_pernet_subsys(&cangw_pernet_ops); 1354 1355 return ret; 1356 } 1357 1358 static __exit void cgw_module_exit(void) 1359 { 1360 rtnl_unregister_all(PF_CAN); 1361 1362 unregister_netdevice_notifier(¬ifier); 1363 1364 unregister_pernet_subsys(&cangw_pernet_ops); 1365 rcu_barrier(); /* Wait for completion of call_rcu()'s */ 1366 1367 kmem_cache_destroy(cgw_cache); 1368 } 1369 1370 module_init(cgw_module_init); 1371 module_exit(cgw_module_exit); 1372