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