Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * gw.c - CAN frame Gateway/Router/Bridge with netlink interface |
| 3 | * |
| 4 | * Copyright (c) 2011 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 | * |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 40 | */ |
| 41 | |
| 42 | #include <linux/module.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/types.h> |
Oliver Hartkopp | be286ba | 2013-01-17 18:43:44 +0100 | [diff] [blame] | 45 | #include <linux/kernel.h> |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 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> |
Oliver Hartkopp | d904d3e | 2013-01-17 18:43:41 +0100 | [diff] [blame] | 56 | #include <linux/can/skb.h> |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 57 | #include <linux/can/gw.h> |
| 58 | #include <net/rtnetlink.h> |
| 59 | #include <net/net_namespace.h> |
| 60 | #include <net/sock.h> |
| 61 | |
Oliver Hartkopp | be286ba | 2013-01-17 18:43:44 +0100 | [diff] [blame] | 62 | #define CAN_GW_VERSION "20130117" |
| 63 | #define CAN_GW_NAME "can-gw" |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 64 | |
| 65 | MODULE_DESCRIPTION("PF_CAN netlink gateway"); |
| 66 | MODULE_LICENSE("Dual BSD/GPL"); |
| 67 | MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); |
Oliver Hartkopp | be286ba | 2013-01-17 18:43:44 +0100 | [diff] [blame] | 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 int max_hops __read_mostly = CGW_DEFAULT_HOPS; |
| 75 | module_param(max_hops, uint, S_IRUGO); |
| 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) ")"); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 81 | |
Daniel Baluta | a75afd4 | 2012-03-26 02:05:50 +0300 | [diff] [blame] | 82 | static HLIST_HEAD(cgw_list); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 83 | static struct notifier_block notifier; |
| 84 | |
| 85 | static struct kmem_cache *cgw_cache __read_mostly; |
| 86 | |
| 87 | /* structure that contains the (on-the-fly) CAN frame modifications */ |
| 88 | struct cf_mod { |
| 89 | struct { |
| 90 | struct can_frame and; |
| 91 | struct can_frame or; |
| 92 | struct can_frame xor; |
| 93 | struct can_frame set; |
| 94 | } modframe; |
| 95 | struct { |
| 96 | u8 and; |
| 97 | u8 or; |
| 98 | u8 xor; |
| 99 | u8 set; |
| 100 | } modtype; |
| 101 | void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf, |
| 102 | struct cf_mod *mod); |
| 103 | |
| 104 | /* CAN frame checksum calculation after CAN frame modifications */ |
| 105 | struct { |
| 106 | struct cgw_csum_xor xor; |
| 107 | struct cgw_csum_crc8 crc8; |
| 108 | } csum; |
| 109 | struct { |
| 110 | void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor); |
| 111 | void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8); |
| 112 | } csumfunc; |
Oliver Hartkopp | dd895d7 | 2015-06-09 08:05:10 +0200 | [diff] [blame] | 113 | u32 uid; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | * So far we just support CAN -> CAN routing and frame modifications. |
| 119 | * |
| 120 | * The internal can_can_gw structure contains data and attributes for |
| 121 | * a CAN -> CAN gateway job. |
| 122 | */ |
| 123 | struct can_can_gw { |
| 124 | struct can_filter filter; |
| 125 | int src_idx; |
| 126 | int dst_idx; |
| 127 | }; |
| 128 | |
| 129 | /* list entry for CAN gateways jobs */ |
| 130 | struct cgw_job { |
| 131 | struct hlist_node list; |
| 132 | struct rcu_head rcu; |
| 133 | u32 handled_frames; |
| 134 | u32 dropped_frames; |
Oliver Hartkopp | e6afa00 | 2013-01-17 18:43:46 +0100 | [diff] [blame] | 135 | u32 deleted_frames; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 136 | struct cf_mod mod; |
| 137 | union { |
| 138 | /* CAN frame data source */ |
| 139 | struct net_device *dev; |
| 140 | } src; |
| 141 | union { |
| 142 | /* CAN frame data destination */ |
| 143 | struct net_device *dev; |
| 144 | } dst; |
| 145 | union { |
| 146 | struct can_can_gw ccgw; |
| 147 | /* tbc */ |
| 148 | }; |
| 149 | u8 gwtype; |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 150 | u8 limit_hops; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 151 | u16 flags; |
| 152 | }; |
| 153 | |
| 154 | /* modification functions that are invoked in the hot path in can_can_gw_rcv */ |
| 155 | |
| 156 | #define MODFUNC(func, op) static void func(struct can_frame *cf, \ |
| 157 | struct cf_mod *mod) { op ; } |
| 158 | |
| 159 | MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id) |
| 160 | MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc) |
| 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_dlc, cf->can_dlc |= mod->modframe.or.can_dlc) |
| 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_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc) |
| 167 | MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data) |
| 168 | MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id) |
| 169 | MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc) |
| 170 | MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data) |
| 171 | |
| 172 | static inline void canframecpy(struct can_frame *dst, struct can_frame *src) |
| 173 | { |
| 174 | /* |
| 175 | * Copy the struct members separately to ensure that no uninitialized |
| 176 | * data are copied in the 3 bytes hole of the struct. This is needed |
| 177 | * to make easy compares of the data in the struct cf_mod. |
| 178 | */ |
| 179 | |
| 180 | dst->can_id = src->can_id; |
| 181 | dst->can_dlc = src->can_dlc; |
| 182 | *(u64 *)dst->data = *(u64 *)src->data; |
| 183 | } |
| 184 | |
| 185 | static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re) |
| 186 | { |
| 187 | /* |
| 188 | * absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0] |
| 189 | * relative to received dlc -1 .. -8 : |
| 190 | * e.g. for received dlc = 8 |
| 191 | * -1 => index = 7 (data[7]) |
| 192 | * -3 => index = 5 (data[5]) |
| 193 | * -8 => index = 0 (data[0]) |
| 194 | */ |
| 195 | |
| 196 | if (fr > -9 && fr < 8 && |
| 197 | to > -9 && to < 8 && |
| 198 | re > -9 && re < 8) |
| 199 | return 0; |
| 200 | else |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | |
| 204 | static inline int calc_idx(int idx, int rx_dlc) |
| 205 | { |
| 206 | if (idx < 0) |
| 207 | return rx_dlc + idx; |
| 208 | else |
| 209 | return idx; |
| 210 | } |
| 211 | |
| 212 | static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor) |
| 213 | { |
| 214 | int from = calc_idx(xor->from_idx, cf->can_dlc); |
| 215 | int to = calc_idx(xor->to_idx, cf->can_dlc); |
| 216 | int res = calc_idx(xor->result_idx, cf->can_dlc); |
| 217 | u8 val = xor->init_xor_val; |
| 218 | int i; |
| 219 | |
| 220 | if (from < 0 || to < 0 || res < 0) |
| 221 | return; |
| 222 | |
| 223 | if (from <= to) { |
| 224 | for (i = from; i <= to; i++) |
| 225 | val ^= cf->data[i]; |
| 226 | } else { |
| 227 | for (i = from; i >= to; i--) |
| 228 | val ^= cf->data[i]; |
| 229 | } |
| 230 | |
| 231 | cf->data[res] = val; |
| 232 | } |
| 233 | |
| 234 | static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor) |
| 235 | { |
| 236 | u8 val = xor->init_xor_val; |
| 237 | int i; |
| 238 | |
| 239 | for (i = xor->from_idx; i <= xor->to_idx; i++) |
| 240 | val ^= cf->data[i]; |
| 241 | |
| 242 | cf->data[xor->result_idx] = val; |
| 243 | } |
| 244 | |
| 245 | static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor) |
| 246 | { |
| 247 | u8 val = xor->init_xor_val; |
| 248 | int i; |
| 249 | |
| 250 | for (i = xor->from_idx; i >= xor->to_idx; i--) |
| 251 | val ^= cf->data[i]; |
| 252 | |
| 253 | cf->data[xor->result_idx] = val; |
| 254 | } |
| 255 | |
| 256 | static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8) |
| 257 | { |
| 258 | int from = calc_idx(crc8->from_idx, cf->can_dlc); |
| 259 | int to = calc_idx(crc8->to_idx, cf->can_dlc); |
| 260 | int res = calc_idx(crc8->result_idx, cf->can_dlc); |
| 261 | u8 crc = crc8->init_crc_val; |
| 262 | int i; |
| 263 | |
| 264 | if (from < 0 || to < 0 || res < 0) |
| 265 | return; |
| 266 | |
| 267 | if (from <= to) { |
| 268 | for (i = crc8->from_idx; i <= crc8->to_idx; i++) |
| 269 | crc = crc8->crctab[crc^cf->data[i]]; |
| 270 | } else { |
| 271 | for (i = crc8->from_idx; i >= crc8->to_idx; i--) |
| 272 | crc = crc8->crctab[crc^cf->data[i]]; |
| 273 | } |
| 274 | |
| 275 | switch (crc8->profile) { |
| 276 | |
| 277 | case CGW_CRC8PRF_1U8: |
| 278 | crc = crc8->crctab[crc^crc8->profile_data[0]]; |
| 279 | break; |
| 280 | |
| 281 | case CGW_CRC8PRF_16U8: |
| 282 | crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]]; |
| 283 | break; |
| 284 | |
| 285 | case CGW_CRC8PRF_SFFID_XOR: |
| 286 | crc = crc8->crctab[crc^(cf->can_id & 0xFF)^ |
| 287 | (cf->can_id >> 8 & 0xFF)]; |
| 288 | break; |
| 289 | |
| 290 | } |
| 291 | |
| 292 | cf->data[crc8->result_idx] = crc^crc8->final_xor_val; |
| 293 | } |
| 294 | |
| 295 | static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8) |
| 296 | { |
| 297 | u8 crc = crc8->init_crc_val; |
| 298 | int i; |
| 299 | |
| 300 | for (i = crc8->from_idx; i <= crc8->to_idx; i++) |
| 301 | crc = crc8->crctab[crc^cf->data[i]]; |
| 302 | |
| 303 | switch (crc8->profile) { |
| 304 | |
| 305 | case CGW_CRC8PRF_1U8: |
| 306 | crc = crc8->crctab[crc^crc8->profile_data[0]]; |
| 307 | break; |
| 308 | |
| 309 | case CGW_CRC8PRF_16U8: |
| 310 | crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]]; |
| 311 | break; |
| 312 | |
| 313 | case CGW_CRC8PRF_SFFID_XOR: |
| 314 | crc = crc8->crctab[crc^(cf->can_id & 0xFF)^ |
| 315 | (cf->can_id >> 8 & 0xFF)]; |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | cf->data[crc8->result_idx] = crc^crc8->final_xor_val; |
| 320 | } |
| 321 | |
| 322 | static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8) |
| 323 | { |
| 324 | u8 crc = crc8->init_crc_val; |
| 325 | int i; |
| 326 | |
| 327 | for (i = crc8->from_idx; i >= crc8->to_idx; i--) |
| 328 | crc = crc8->crctab[crc^cf->data[i]]; |
| 329 | |
| 330 | switch (crc8->profile) { |
| 331 | |
| 332 | case CGW_CRC8PRF_1U8: |
| 333 | crc = crc8->crctab[crc^crc8->profile_data[0]]; |
| 334 | break; |
| 335 | |
| 336 | case CGW_CRC8PRF_16U8: |
| 337 | crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]]; |
| 338 | break; |
| 339 | |
| 340 | case CGW_CRC8PRF_SFFID_XOR: |
| 341 | crc = crc8->crctab[crc^(cf->can_id & 0xFF)^ |
| 342 | (cf->can_id >> 8 & 0xFF)]; |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | cf->data[crc8->result_idx] = crc^crc8->final_xor_val; |
| 347 | } |
| 348 | |
| 349 | /* the receive & process & send function */ |
| 350 | static void can_can_gw_rcv(struct sk_buff *skb, void *data) |
| 351 | { |
| 352 | struct cgw_job *gwj = (struct cgw_job *)data; |
| 353 | struct can_frame *cf; |
| 354 | struct sk_buff *nskb; |
| 355 | int modidx = 0; |
| 356 | |
Oliver Hartkopp | be286ba | 2013-01-17 18:43:44 +0100 | [diff] [blame] | 357 | /* |
| 358 | * Do not handle CAN frames routed more than 'max_hops' times. |
| 359 | * In general we should never catch this delimiter which is intended |
| 360 | * to cover a misconfiguration protection (e.g. circular CAN routes). |
| 361 | * |
| 362 | * The Controller Area Network controllers only accept CAN frames with |
| 363 | * correct CRCs - which are not visible in the controller registers. |
| 364 | * According to skbuff.h documentation the csum_start element for IP |
Jeremiah Mahler | 069f845 | 2014-12-05 09:54:38 -0800 | [diff] [blame] | 365 | * checksums is undefined/unused when ip_summed == CHECKSUM_UNNECESSARY. |
Oliver Hartkopp | be286ba | 2013-01-17 18:43:44 +0100 | [diff] [blame] | 366 | * Only CAN skbs can be processed here which already have this property. |
| 367 | */ |
| 368 | |
| 369 | #define cgw_hops(skb) ((skb)->csum_start) |
| 370 | |
| 371 | BUG_ON(skb->ip_summed != CHECKSUM_UNNECESSARY); |
| 372 | |
Oliver Hartkopp | e6afa00 | 2013-01-17 18:43:46 +0100 | [diff] [blame] | 373 | if (cgw_hops(skb) >= max_hops) { |
| 374 | /* indicate deleted frames due to misconfiguration */ |
| 375 | gwj->deleted_frames++; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 376 | return; |
Oliver Hartkopp | e6afa00 | 2013-01-17 18:43:46 +0100 | [diff] [blame] | 377 | } |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 378 | |
| 379 | if (!(gwj->dst.dev->flags & IFF_UP)) { |
| 380 | gwj->dropped_frames++; |
| 381 | return; |
| 382 | } |
| 383 | |
Oliver Hartkopp | d904d3e | 2013-01-17 18:43:41 +0100 | [diff] [blame] | 384 | /* is sending the skb back to the incoming interface not allowed? */ |
| 385 | if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) && |
Oliver Hartkopp | 2bf3440 | 2013-01-28 08:33:33 +0000 | [diff] [blame] | 386 | can_skb_prv(skb)->ifindex == gwj->dst.dev->ifindex) |
Oliver Hartkopp | d904d3e | 2013-01-17 18:43:41 +0100 | [diff] [blame] | 387 | return; |
| 388 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 389 | /* |
| 390 | * clone the given skb, which has not been done in can_rcv() |
| 391 | * |
| 392 | * When there is at least one modification function activated, |
| 393 | * we need to copy the skb as we want to modify skb->data. |
| 394 | */ |
| 395 | if (gwj->mod.modfunc[0]) |
| 396 | nskb = skb_copy(skb, GFP_ATOMIC); |
| 397 | else |
| 398 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 399 | |
| 400 | if (!nskb) { |
| 401 | gwj->dropped_frames++; |
| 402 | return; |
| 403 | } |
| 404 | |
Oliver Hartkopp | be286ba | 2013-01-17 18:43:44 +0100 | [diff] [blame] | 405 | /* put the incremented hop counter in the cloned skb */ |
| 406 | cgw_hops(nskb) = cgw_hops(skb) + 1; |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 407 | |
| 408 | /* first processing of this CAN frame -> adjust to private hop limit */ |
| 409 | if (gwj->limit_hops && cgw_hops(nskb) == 1) |
| 410 | cgw_hops(nskb) = max_hops - gwj->limit_hops + 1; |
| 411 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 412 | nskb->dev = gwj->dst.dev; |
| 413 | |
| 414 | /* pointer to modifiable CAN frame */ |
| 415 | cf = (struct can_frame *)nskb->data; |
| 416 | |
| 417 | /* perform preprocessed modification functions if there are any */ |
| 418 | while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx]) |
| 419 | (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod); |
| 420 | |
Oliver Hartkopp | d379b33 | 2019-01-04 15:55:26 +0100 | [diff] [blame] | 421 | /* Has the CAN frame been modified? */ |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 422 | if (modidx) { |
Oliver Hartkopp | d379b33 | 2019-01-04 15:55:26 +0100 | [diff] [blame] | 423 | /* get available space for the processed CAN frame type */ |
| 424 | int max_len = nskb->len - offsetof(struct can_frame, data); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 425 | |
Oliver Hartkopp | d379b33 | 2019-01-04 15:55:26 +0100 | [diff] [blame] | 426 | /* dlc may have changed, make sure it fits to the CAN frame */ |
| 427 | if (cf->can_dlc > max_len) |
| 428 | goto out_delete; |
| 429 | |
| 430 | /* check for checksum updates in classic CAN length only */ |
| 431 | if (gwj->mod.csumfunc.crc8) { |
| 432 | if (cf->can_dlc > 8) |
| 433 | goto out_delete; |
| 434 | |
| 435 | (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8); |
| 436 | } |
| 437 | |
| 438 | if (gwj->mod.csumfunc.xor) { |
| 439 | if (cf->can_dlc > 8) |
| 440 | goto out_delete; |
| 441 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 442 | (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor); |
Oliver Hartkopp | d379b33 | 2019-01-04 15:55:26 +0100 | [diff] [blame] | 443 | } |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* clear the skb timestamp if not configured the other way */ |
| 447 | if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP)) |
| 448 | nskb->tstamp.tv64 = 0; |
| 449 | |
| 450 | /* send to netdevice */ |
| 451 | if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO)) |
| 452 | gwj->dropped_frames++; |
| 453 | else |
| 454 | gwj->handled_frames++; |
Oliver Hartkopp | d379b33 | 2019-01-04 15:55:26 +0100 | [diff] [blame] | 455 | |
| 456 | return; |
| 457 | |
| 458 | out_delete: |
| 459 | /* delete frame due to misconfiguration */ |
| 460 | gwj->deleted_frames++; |
| 461 | kfree_skb(nskb); |
| 462 | return; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static inline int cgw_register_filter(struct cgw_job *gwj) |
| 466 | { |
| 467 | return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id, |
| 468 | gwj->ccgw.filter.can_mask, can_can_gw_rcv, |
Eric Dumazet | adf86d5 | 2017-01-27 08:11:44 -0800 | [diff] [blame] | 469 | gwj, "gw", NULL); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static inline void cgw_unregister_filter(struct cgw_job *gwj) |
| 473 | { |
| 474 | can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id, |
| 475 | gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj); |
| 476 | } |
| 477 | |
| 478 | static int cgw_notifier(struct notifier_block *nb, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 479 | unsigned long msg, void *ptr) |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 480 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 481 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 482 | |
| 483 | if (!net_eq(dev_net(dev), &init_net)) |
| 484 | return NOTIFY_DONE; |
| 485 | if (dev->type != ARPHRD_CAN) |
| 486 | return NOTIFY_DONE; |
| 487 | |
| 488 | if (msg == NETDEV_UNREGISTER) { |
| 489 | |
| 490 | struct cgw_job *gwj = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 491 | struct hlist_node *nx; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 492 | |
| 493 | ASSERT_RTNL(); |
| 494 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 495 | hlist_for_each_entry_safe(gwj, nx, &cgw_list, list) { |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 496 | |
| 497 | if (gwj->src.dev == dev || gwj->dst.dev == dev) { |
| 498 | hlist_del(&gwj->list); |
| 499 | cgw_unregister_filter(gwj); |
Wei Yongjun | 3480a21 | 2013-04-09 14:16:04 +0800 | [diff] [blame] | 500 | kmem_cache_free(cgw_cache, gwj); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | return NOTIFY_DONE; |
| 506 | } |
| 507 | |
Thomas Graf | 1da0faa3 | 2012-07-05 14:19:57 +0200 | [diff] [blame] | 508 | static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type, |
| 509 | u32 pid, u32 seq, int flags) |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 510 | { |
| 511 | struct cgw_frame_mod mb; |
| 512 | struct rtcanmsg *rtcan; |
Thomas Graf | 1da0faa3 | 2012-07-05 14:19:57 +0200 | [diff] [blame] | 513 | struct nlmsghdr *nlh; |
| 514 | |
| 515 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 516 | if (!nlh) |
| 517 | return -EMSGSIZE; |
| 518 | |
| 519 | rtcan = nlmsg_data(nlh); |
| 520 | rtcan->can_family = AF_CAN; |
| 521 | rtcan->gwtype = gwj->gwtype; |
| 522 | rtcan->flags = gwj->flags; |
| 523 | |
| 524 | /* add statistics if available */ |
| 525 | |
| 526 | if (gwj->handled_frames) { |
| 527 | if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0) |
| 528 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | if (gwj->dropped_frames) { |
| 532 | if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0) |
| 533 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Oliver Hartkopp | e6afa00 | 2013-01-17 18:43:46 +0100 | [diff] [blame] | 536 | if (gwj->deleted_frames) { |
| 537 | if (nla_put_u32(skb, CGW_DELETED, gwj->deleted_frames) < 0) |
| 538 | goto cancel; |
| 539 | } |
| 540 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 541 | /* check non default settings of attributes */ |
| 542 | |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 543 | if (gwj->limit_hops) { |
| 544 | if (nla_put_u8(skb, CGW_LIM_HOPS, gwj->limit_hops) < 0) |
| 545 | goto cancel; |
| 546 | } |
| 547 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 548 | if (gwj->mod.modtype.and) { |
| 549 | memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf)); |
| 550 | mb.modtype = gwj->mod.modtype.and; |
| 551 | if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0) |
| 552 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | if (gwj->mod.modtype.or) { |
| 556 | memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf)); |
| 557 | mb.modtype = gwj->mod.modtype.or; |
| 558 | if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0) |
| 559 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | if (gwj->mod.modtype.xor) { |
| 563 | memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf)); |
| 564 | mb.modtype = gwj->mod.modtype.xor; |
| 565 | if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0) |
| 566 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | if (gwj->mod.modtype.set) { |
| 570 | memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf)); |
| 571 | mb.modtype = gwj->mod.modtype.set; |
| 572 | if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0) |
| 573 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Oliver Hartkopp | dd895d7 | 2015-06-09 08:05:10 +0200 | [diff] [blame] | 576 | if (gwj->mod.uid) { |
| 577 | if (nla_put_u32(skb, CGW_MOD_UID, gwj->mod.uid) < 0) |
| 578 | goto cancel; |
| 579 | } |
| 580 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 581 | if (gwj->mod.csumfunc.crc8) { |
| 582 | if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN, |
| 583 | &gwj->mod.csum.crc8) < 0) |
| 584 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | if (gwj->mod.csumfunc.xor) { |
| 588 | if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN, |
| 589 | &gwj->mod.csum.xor) < 0) |
| 590 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (gwj->gwtype == CGW_TYPE_CAN_CAN) { |
| 594 | |
| 595 | if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) { |
| 596 | if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter), |
| 597 | &gwj->ccgw.filter) < 0) |
| 598 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0) |
| 602 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 603 | |
| 604 | if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0) |
| 605 | goto cancel; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 608 | nlmsg_end(skb, nlh); |
| 609 | return 0; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 610 | |
| 611 | cancel: |
| 612 | nlmsg_cancel(skb, nlh); |
| 613 | return -EMSGSIZE; |
| 614 | } |
| 615 | |
| 616 | /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */ |
| 617 | static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb) |
| 618 | { |
| 619 | struct cgw_job *gwj = NULL; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 620 | int idx = 0; |
| 621 | int s_idx = cb->args[0]; |
| 622 | |
| 623 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 624 | hlist_for_each_entry_rcu(gwj, &cgw_list, list) { |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 625 | if (idx < s_idx) |
| 626 | goto cont; |
| 627 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 628 | if (cgw_put_job(skb, gwj, RTM_NEWROUTE, NETLINK_CB(cb->skb).portid, |
Thomas Graf | 1da0faa3 | 2012-07-05 14:19:57 +0200 | [diff] [blame] | 629 | cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0) |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 630 | break; |
| 631 | cont: |
| 632 | idx++; |
| 633 | } |
| 634 | rcu_read_unlock(); |
| 635 | |
| 636 | cb->args[0] = idx; |
| 637 | |
| 638 | return skb->len; |
| 639 | } |
| 640 | |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 641 | static const struct nla_policy cgw_policy[CGW_MAX+1] = { |
| 642 | [CGW_MOD_AND] = { .len = sizeof(struct cgw_frame_mod) }, |
| 643 | [CGW_MOD_OR] = { .len = sizeof(struct cgw_frame_mod) }, |
| 644 | [CGW_MOD_XOR] = { .len = sizeof(struct cgw_frame_mod) }, |
| 645 | [CGW_MOD_SET] = { .len = sizeof(struct cgw_frame_mod) }, |
| 646 | [CGW_CS_XOR] = { .len = sizeof(struct cgw_csum_xor) }, |
| 647 | [CGW_CS_CRC8] = { .len = sizeof(struct cgw_csum_crc8) }, |
| 648 | [CGW_SRC_IF] = { .type = NLA_U32 }, |
| 649 | [CGW_DST_IF] = { .type = NLA_U32 }, |
| 650 | [CGW_FILTER] = { .len = sizeof(struct can_filter) }, |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 651 | [CGW_LIM_HOPS] = { .type = NLA_U8 }, |
Oliver Hartkopp | dd895d7 | 2015-06-09 08:05:10 +0200 | [diff] [blame] | 652 | [CGW_MOD_UID] = { .type = NLA_U32 }, |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 653 | }; |
| 654 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 655 | /* check for common and gwtype specific attributes */ |
| 656 | static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod, |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 657 | u8 gwtype, void *gwtypeattr, u8 *limhops) |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 658 | { |
| 659 | struct nlattr *tb[CGW_MAX+1]; |
| 660 | struct cgw_frame_mod mb; |
| 661 | int modidx = 0; |
| 662 | int err = 0; |
| 663 | |
| 664 | /* initialize modification & checksum data space */ |
| 665 | memset(mod, 0, sizeof(*mod)); |
| 666 | |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 667 | err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, |
| 668 | cgw_policy); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 669 | if (err < 0) |
| 670 | return err; |
| 671 | |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 672 | if (tb[CGW_LIM_HOPS]) { |
| 673 | *limhops = nla_get_u8(tb[CGW_LIM_HOPS]); |
| 674 | |
| 675 | if (*limhops < 1 || *limhops > max_hops) |
| 676 | return -EINVAL; |
| 677 | } |
| 678 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 679 | /* check for AND/OR/XOR/SET modifications */ |
| 680 | |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 681 | if (tb[CGW_MOD_AND]) { |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 682 | nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN); |
| 683 | |
| 684 | canframecpy(&mod->modframe.and, &mb.cf); |
| 685 | mod->modtype.and = mb.modtype; |
| 686 | |
| 687 | if (mb.modtype & CGW_MOD_ID) |
| 688 | mod->modfunc[modidx++] = mod_and_id; |
| 689 | |
| 690 | if (mb.modtype & CGW_MOD_DLC) |
| 691 | mod->modfunc[modidx++] = mod_and_dlc; |
| 692 | |
| 693 | if (mb.modtype & CGW_MOD_DATA) |
| 694 | mod->modfunc[modidx++] = mod_and_data; |
| 695 | } |
| 696 | |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 697 | if (tb[CGW_MOD_OR]) { |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 698 | nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN); |
| 699 | |
| 700 | canframecpy(&mod->modframe.or, &mb.cf); |
| 701 | mod->modtype.or = mb.modtype; |
| 702 | |
| 703 | if (mb.modtype & CGW_MOD_ID) |
| 704 | mod->modfunc[modidx++] = mod_or_id; |
| 705 | |
| 706 | if (mb.modtype & CGW_MOD_DLC) |
| 707 | mod->modfunc[modidx++] = mod_or_dlc; |
| 708 | |
| 709 | if (mb.modtype & CGW_MOD_DATA) |
| 710 | mod->modfunc[modidx++] = mod_or_data; |
| 711 | } |
| 712 | |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 713 | if (tb[CGW_MOD_XOR]) { |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 714 | nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN); |
| 715 | |
| 716 | canframecpy(&mod->modframe.xor, &mb.cf); |
| 717 | mod->modtype.xor = mb.modtype; |
| 718 | |
| 719 | if (mb.modtype & CGW_MOD_ID) |
| 720 | mod->modfunc[modidx++] = mod_xor_id; |
| 721 | |
| 722 | if (mb.modtype & CGW_MOD_DLC) |
| 723 | mod->modfunc[modidx++] = mod_xor_dlc; |
| 724 | |
| 725 | if (mb.modtype & CGW_MOD_DATA) |
| 726 | mod->modfunc[modidx++] = mod_xor_data; |
| 727 | } |
| 728 | |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 729 | if (tb[CGW_MOD_SET]) { |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 730 | nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN); |
| 731 | |
| 732 | canframecpy(&mod->modframe.set, &mb.cf); |
| 733 | mod->modtype.set = mb.modtype; |
| 734 | |
| 735 | if (mb.modtype & CGW_MOD_ID) |
| 736 | mod->modfunc[modidx++] = mod_set_id; |
| 737 | |
| 738 | if (mb.modtype & CGW_MOD_DLC) |
| 739 | mod->modfunc[modidx++] = mod_set_dlc; |
| 740 | |
| 741 | if (mb.modtype & CGW_MOD_DATA) |
| 742 | mod->modfunc[modidx++] = mod_set_data; |
| 743 | } |
| 744 | |
| 745 | /* check for checksum operations after CAN frame modifications */ |
| 746 | if (modidx) { |
| 747 | |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 748 | if (tb[CGW_CS_CRC8]) { |
Thomas Graf | 5d91efa | 2012-07-05 14:19:58 +0200 | [diff] [blame] | 749 | struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 750 | |
| 751 | err = cgw_chk_csum_parms(c->from_idx, c->to_idx, |
| 752 | c->result_idx); |
| 753 | if (err) |
| 754 | return err; |
| 755 | |
| 756 | nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8], |
| 757 | CGW_CS_CRC8_LEN); |
| 758 | |
| 759 | /* |
| 760 | * select dedicated processing function to reduce |
| 761 | * runtime operations in receive hot path. |
| 762 | */ |
| 763 | if (c->from_idx < 0 || c->to_idx < 0 || |
| 764 | c->result_idx < 0) |
| 765 | mod->csumfunc.crc8 = cgw_csum_crc8_rel; |
| 766 | else if (c->from_idx <= c->to_idx) |
| 767 | mod->csumfunc.crc8 = cgw_csum_crc8_pos; |
| 768 | else |
| 769 | mod->csumfunc.crc8 = cgw_csum_crc8_neg; |
| 770 | } |
| 771 | |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 772 | if (tb[CGW_CS_XOR]) { |
Thomas Graf | 5d91efa | 2012-07-05 14:19:58 +0200 | [diff] [blame] | 773 | struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 774 | |
| 775 | err = cgw_chk_csum_parms(c->from_idx, c->to_idx, |
| 776 | c->result_idx); |
| 777 | if (err) |
| 778 | return err; |
| 779 | |
| 780 | nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR], |
| 781 | CGW_CS_XOR_LEN); |
| 782 | |
| 783 | /* |
| 784 | * select dedicated processing function to reduce |
| 785 | * runtime operations in receive hot path. |
| 786 | */ |
| 787 | if (c->from_idx < 0 || c->to_idx < 0 || |
| 788 | c->result_idx < 0) |
| 789 | mod->csumfunc.xor = cgw_csum_xor_rel; |
| 790 | else if (c->from_idx <= c->to_idx) |
| 791 | mod->csumfunc.xor = cgw_csum_xor_pos; |
| 792 | else |
| 793 | mod->csumfunc.xor = cgw_csum_xor_neg; |
| 794 | } |
Oliver Hartkopp | dd895d7 | 2015-06-09 08:05:10 +0200 | [diff] [blame] | 795 | |
| 796 | if (tb[CGW_MOD_UID]) { |
| 797 | nla_memcpy(&mod->uid, tb[CGW_MOD_UID], sizeof(u32)); |
| 798 | } |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | if (gwtype == CGW_TYPE_CAN_CAN) { |
| 802 | |
| 803 | /* check CGW_TYPE_CAN_CAN specific attributes */ |
| 804 | |
| 805 | struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr; |
| 806 | memset(ccgw, 0, sizeof(*ccgw)); |
| 807 | |
| 808 | /* check for can_filter in attributes */ |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 809 | if (tb[CGW_FILTER]) |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 810 | nla_memcpy(&ccgw->filter, tb[CGW_FILTER], |
| 811 | sizeof(struct can_filter)); |
| 812 | |
| 813 | err = -ENODEV; |
| 814 | |
| 815 | /* specifying two interfaces is mandatory */ |
| 816 | if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF]) |
| 817 | return err; |
| 818 | |
Thomas Graf | 732d35f | 2012-07-05 14:19:56 +0200 | [diff] [blame] | 819 | ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]); |
| 820 | ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 821 | |
| 822 | /* both indices set to 0 for flushing all routing entries */ |
| 823 | if (!ccgw->src_idx && !ccgw->dst_idx) |
| 824 | return 0; |
| 825 | |
| 826 | /* only one index set to 0 is an error */ |
| 827 | if (!ccgw->src_idx || !ccgw->dst_idx) |
| 828 | return err; |
| 829 | } |
| 830 | |
| 831 | /* add the checks for other gwtypes here */ |
| 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 836 | static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh) |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 837 | { |
| 838 | struct rtcanmsg *r; |
| 839 | struct cgw_job *gwj; |
Oliver Hartkopp | dd895d7 | 2015-06-09 08:05:10 +0200 | [diff] [blame] | 840 | struct cf_mod mod; |
| 841 | struct can_can_gw ccgw; |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 842 | u8 limhops = 0; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 843 | int err = 0; |
| 844 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 845 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 846 | return -EPERM; |
| 847 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 848 | if (nlmsg_len(nlh) < sizeof(*r)) |
| 849 | return -EINVAL; |
| 850 | |
| 851 | r = nlmsg_data(nlh); |
| 852 | if (r->can_family != AF_CAN) |
| 853 | return -EPFNOSUPPORT; |
| 854 | |
| 855 | /* so far we only support CAN -> CAN routings */ |
| 856 | if (r->gwtype != CGW_TYPE_CAN_CAN) |
| 857 | return -EINVAL; |
| 858 | |
Oliver Hartkopp | dd895d7 | 2015-06-09 08:05:10 +0200 | [diff] [blame] | 859 | err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops); |
| 860 | if (err < 0) |
| 861 | return err; |
| 862 | |
| 863 | if (mod.uid) { |
| 864 | |
| 865 | ASSERT_RTNL(); |
| 866 | |
| 867 | /* check for updating an existing job with identical uid */ |
| 868 | hlist_for_each_entry(gwj, &cgw_list, list) { |
| 869 | |
| 870 | if (gwj->mod.uid != mod.uid) |
| 871 | continue; |
| 872 | |
| 873 | /* interfaces & filters must be identical */ |
| 874 | if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw))) |
| 875 | return -EINVAL; |
| 876 | |
| 877 | /* update modifications with disabled softirq & quit */ |
| 878 | local_bh_disable(); |
| 879 | memcpy(&gwj->mod, &mod, sizeof(mod)); |
| 880 | local_bh_enable(); |
| 881 | return 0; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | /* ifindex == 0 is not allowed for job creation */ |
| 886 | if (!ccgw.src_idx || !ccgw.dst_idx) |
| 887 | return -ENODEV; |
| 888 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 889 | gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL); |
| 890 | if (!gwj) |
| 891 | return -ENOMEM; |
| 892 | |
| 893 | gwj->handled_frames = 0; |
| 894 | gwj->dropped_frames = 0; |
Oliver Hartkopp | e6afa00 | 2013-01-17 18:43:46 +0100 | [diff] [blame] | 895 | gwj->deleted_frames = 0; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 896 | gwj->flags = r->flags; |
| 897 | gwj->gwtype = r->gwtype; |
Oliver Hartkopp | dd895d7 | 2015-06-09 08:05:10 +0200 | [diff] [blame] | 898 | gwj->limit_hops = limhops; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 899 | |
Oliver Hartkopp | dd895d7 | 2015-06-09 08:05:10 +0200 | [diff] [blame] | 900 | /* insert already parsed information */ |
| 901 | memcpy(&gwj->mod, &mod, sizeof(mod)); |
| 902 | memcpy(&gwj->ccgw, &ccgw, sizeof(ccgw)); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 903 | |
| 904 | err = -ENODEV; |
| 905 | |
Ying Xue | 5af28de | 2014-01-15 10:23:44 +0800 | [diff] [blame] | 906 | gwj->src.dev = __dev_get_by_index(&init_net, gwj->ccgw.src_idx); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 907 | |
| 908 | if (!gwj->src.dev) |
| 909 | goto out; |
| 910 | |
Oliver Hartkopp | c0ebbdd | 2013-12-04 19:52:36 +0100 | [diff] [blame] | 911 | if (gwj->src.dev->type != ARPHRD_CAN) |
Ying Xue | 5af28de | 2014-01-15 10:23:44 +0800 | [diff] [blame] | 912 | goto out; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 913 | |
Ying Xue | 5af28de | 2014-01-15 10:23:44 +0800 | [diff] [blame] | 914 | gwj->dst.dev = __dev_get_by_index(&init_net, gwj->ccgw.dst_idx); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 915 | |
| 916 | if (!gwj->dst.dev) |
Ying Xue | 5af28de | 2014-01-15 10:23:44 +0800 | [diff] [blame] | 917 | goto out; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 918 | |
Oliver Hartkopp | c0ebbdd | 2013-12-04 19:52:36 +0100 | [diff] [blame] | 919 | if (gwj->dst.dev->type != ARPHRD_CAN) |
Ying Xue | 5af28de | 2014-01-15 10:23:44 +0800 | [diff] [blame] | 920 | goto out; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 921 | |
| 922 | ASSERT_RTNL(); |
| 923 | |
| 924 | err = cgw_register_filter(gwj); |
| 925 | if (!err) |
| 926 | hlist_add_head_rcu(&gwj->list, &cgw_list); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 927 | out: |
| 928 | if (err) |
| 929 | kmem_cache_free(cgw_cache, gwj); |
| 930 | |
| 931 | return err; |
| 932 | } |
| 933 | |
| 934 | static void cgw_remove_all_jobs(void) |
| 935 | { |
| 936 | struct cgw_job *gwj = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 937 | struct hlist_node *nx; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 938 | |
| 939 | ASSERT_RTNL(); |
| 940 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 941 | hlist_for_each_entry_safe(gwj, nx, &cgw_list, list) { |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 942 | hlist_del(&gwj->list); |
| 943 | cgw_unregister_filter(gwj); |
Wei Yongjun | 3480a21 | 2013-04-09 14:16:04 +0800 | [diff] [blame] | 944 | kmem_cache_free(cgw_cache, gwj); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 948 | static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh) |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 949 | { |
| 950 | struct cgw_job *gwj = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 951 | struct hlist_node *nx; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 952 | struct rtcanmsg *r; |
| 953 | struct cf_mod mod; |
| 954 | struct can_can_gw ccgw; |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 955 | u8 limhops = 0; |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 956 | int err = 0; |
| 957 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 958 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 959 | return -EPERM; |
| 960 | |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 961 | if (nlmsg_len(nlh) < sizeof(*r)) |
| 962 | return -EINVAL; |
| 963 | |
| 964 | r = nlmsg_data(nlh); |
| 965 | if (r->can_family != AF_CAN) |
| 966 | return -EPFNOSUPPORT; |
| 967 | |
| 968 | /* so far we only support CAN -> CAN routings */ |
| 969 | if (r->gwtype != CGW_TYPE_CAN_CAN) |
| 970 | return -EINVAL; |
| 971 | |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 972 | err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 973 | if (err < 0) |
| 974 | return err; |
| 975 | |
| 976 | /* two interface indices both set to 0 => remove all entries */ |
| 977 | if (!ccgw.src_idx && !ccgw.dst_idx) { |
| 978 | cgw_remove_all_jobs(); |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | err = -EINVAL; |
| 983 | |
| 984 | ASSERT_RTNL(); |
| 985 | |
| 986 | /* remove only the first matching entry */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 987 | hlist_for_each_entry_safe(gwj, nx, &cgw_list, list) { |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 988 | |
| 989 | if (gwj->flags != r->flags) |
| 990 | continue; |
| 991 | |
Oliver Hartkopp | 391ac12 | 2013-08-26 15:05:36 +0200 | [diff] [blame] | 992 | if (gwj->limit_hops != limhops) |
| 993 | continue; |
| 994 | |
Oliver Hartkopp | dd895d7 | 2015-06-09 08:05:10 +0200 | [diff] [blame] | 995 | /* we have a match when uid is enabled and identical */ |
| 996 | if (gwj->mod.uid || mod.uid) { |
| 997 | if (gwj->mod.uid != mod.uid) |
| 998 | continue; |
| 999 | } else { |
| 1000 | /* no uid => check for identical modifications */ |
| 1001 | if (memcmp(&gwj->mod, &mod, sizeof(mod))) |
| 1002 | continue; |
| 1003 | } |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 1004 | |
| 1005 | /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */ |
| 1006 | if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw))) |
| 1007 | continue; |
| 1008 | |
| 1009 | hlist_del(&gwj->list); |
| 1010 | cgw_unregister_filter(gwj); |
Wei Yongjun | 3480a21 | 2013-04-09 14:16:04 +0800 | [diff] [blame] | 1011 | kmem_cache_free(cgw_cache, gwj); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 1012 | err = 0; |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | return err; |
| 1017 | } |
| 1018 | |
| 1019 | static __init int cgw_module_init(void) |
| 1020 | { |
Oliver Hartkopp | be286ba | 2013-01-17 18:43:44 +0100 | [diff] [blame] | 1021 | /* sanitize given module parameter */ |
| 1022 | max_hops = clamp_t(unsigned int, max_hops, CGW_MIN_HOPS, CGW_MAX_HOPS); |
| 1023 | |
| 1024 | pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n", |
| 1025 | max_hops); |
Oliver Hartkopp | c1aabdf | 2011-09-01 04:23:23 +0000 | [diff] [blame] | 1026 | |
| 1027 | cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job), |
| 1028 | 0, 0, NULL); |
| 1029 | |
| 1030 | if (!cgw_cache) |
| 1031 | return -ENOMEM; |
| 1032 | |
| 1033 | /* set notifier */ |
| 1034 | notifier.notifier_call = cgw_notifier; |
| 1035 | register_netdevice_notifier(¬ifier); |
| 1036 | |
| 1037 | if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs, NULL)) { |
| 1038 | unregister_netdevice_notifier(¬ifier); |
| 1039 | kmem_cache_destroy(cgw_cache); |
| 1040 | return -ENOBUFS; |
| 1041 | } |
| 1042 | |
| 1043 | /* Only the first call to __rtnl_register can fail */ |
| 1044 | __rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL, NULL); |
| 1045 | __rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL, NULL); |
| 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | static __exit void cgw_module_exit(void) |
| 1051 | { |
| 1052 | rtnl_unregister_all(PF_CAN); |
| 1053 | |
| 1054 | unregister_netdevice_notifier(¬ifier); |
| 1055 | |
| 1056 | rtnl_lock(); |
| 1057 | cgw_remove_all_jobs(); |
| 1058 | rtnl_unlock(); |
| 1059 | |
| 1060 | rcu_barrier(); /* Wait for completion of call_rcu()'s */ |
| 1061 | |
| 1062 | kmem_cache_destroy(cgw_cache); |
| 1063 | } |
| 1064 | |
| 1065 | module_init(cgw_module_init); |
| 1066 | module_exit(cgw_module_exit); |