]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/can/gw.c
can: gw: add a variable limit for CAN frame routings
[mirror_ubuntu-bionic-kernel.git] / net / can / gw.c
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 *
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/rtnetlink.h>
59 #include <net/net_namespace.h>
60 #include <net/sock.h>
61
62 #define CAN_GW_VERSION "20130117"
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 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) ")");
81
82 static HLIST_HEAD(cgw_list);
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;
113 };
114
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 struct cf_mod 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 u16 flags;
149 };
150
151 /* modification functions that are invoked in the hot path in can_can_gw_rcv */
152
153 #define MODFUNC(func, op) static void func(struct can_frame *cf, \
154 struct cf_mod *mod) { op ; }
155
156 MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
157 MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc)
158 MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
159 MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
160 MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
161 MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
162 MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
163 MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
164 MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
165 MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
166 MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
167 MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
168
169 static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
170 {
171 /*
172 * Copy the struct members separately to ensure that no uninitialized
173 * data are copied in the 3 bytes hole of the struct. This is needed
174 * to make easy compares of the data in the struct cf_mod.
175 */
176
177 dst->can_id = src->can_id;
178 dst->can_dlc = src->can_dlc;
179 *(u64 *)dst->data = *(u64 *)src->data;
180 }
181
182 static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re)
183 {
184 /*
185 * absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
186 * relative to received dlc -1 .. -8 :
187 * e.g. for received dlc = 8
188 * -1 => index = 7 (data[7])
189 * -3 => index = 5 (data[5])
190 * -8 => index = 0 (data[0])
191 */
192
193 if (fr > -9 && fr < 8 &&
194 to > -9 && to < 8 &&
195 re > -9 && re < 8)
196 return 0;
197 else
198 return -EINVAL;
199 }
200
201 static inline int calc_idx(int idx, int rx_dlc)
202 {
203 if (idx < 0)
204 return rx_dlc + idx;
205 else
206 return idx;
207 }
208
209 static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
210 {
211 int from = calc_idx(xor->from_idx, cf->can_dlc);
212 int to = calc_idx(xor->to_idx, cf->can_dlc);
213 int res = calc_idx(xor->result_idx, cf->can_dlc);
214 u8 val = xor->init_xor_val;
215 int i;
216
217 if (from < 0 || to < 0 || res < 0)
218 return;
219
220 if (from <= to) {
221 for (i = from; i <= to; i++)
222 val ^= cf->data[i];
223 } else {
224 for (i = from; i >= to; i--)
225 val ^= cf->data[i];
226 }
227
228 cf->data[res] = val;
229 }
230
231 static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
232 {
233 u8 val = xor->init_xor_val;
234 int i;
235
236 for (i = xor->from_idx; i <= xor->to_idx; i++)
237 val ^= cf->data[i];
238
239 cf->data[xor->result_idx] = val;
240 }
241
242 static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
243 {
244 u8 val = xor->init_xor_val;
245 int i;
246
247 for (i = xor->from_idx; i >= xor->to_idx; i--)
248 val ^= cf->data[i];
249
250 cf->data[xor->result_idx] = val;
251 }
252
253 static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
254 {
255 int from = calc_idx(crc8->from_idx, cf->can_dlc);
256 int to = calc_idx(crc8->to_idx, cf->can_dlc);
257 int res = calc_idx(crc8->result_idx, cf->can_dlc);
258 u8 crc = crc8->init_crc_val;
259 int i;
260
261 if (from < 0 || to < 0 || res < 0)
262 return;
263
264 if (from <= to) {
265 for (i = crc8->from_idx; i <= crc8->to_idx; i++)
266 crc = crc8->crctab[crc^cf->data[i]];
267 } else {
268 for (i = crc8->from_idx; i >= crc8->to_idx; i--)
269 crc = crc8->crctab[crc^cf->data[i]];
270 }
271
272 switch (crc8->profile) {
273
274 case CGW_CRC8PRF_1U8:
275 crc = crc8->crctab[crc^crc8->profile_data[0]];
276 break;
277
278 case CGW_CRC8PRF_16U8:
279 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
280 break;
281
282 case CGW_CRC8PRF_SFFID_XOR:
283 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
284 (cf->can_id >> 8 & 0xFF)];
285 break;
286
287 }
288
289 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
290 }
291
292 static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
293 {
294 u8 crc = crc8->init_crc_val;
295 int i;
296
297 for (i = crc8->from_idx; i <= crc8->to_idx; i++)
298 crc = crc8->crctab[crc^cf->data[i]];
299
300 switch (crc8->profile) {
301
302 case CGW_CRC8PRF_1U8:
303 crc = crc8->crctab[crc^crc8->profile_data[0]];
304 break;
305
306 case CGW_CRC8PRF_16U8:
307 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
308 break;
309
310 case CGW_CRC8PRF_SFFID_XOR:
311 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
312 (cf->can_id >> 8 & 0xFF)];
313 break;
314 }
315
316 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
317 }
318
319 static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
320 {
321 u8 crc = crc8->init_crc_val;
322 int i;
323
324 for (i = crc8->from_idx; i >= crc8->to_idx; i--)
325 crc = crc8->crctab[crc^cf->data[i]];
326
327 switch (crc8->profile) {
328
329 case CGW_CRC8PRF_1U8:
330 crc = crc8->crctab[crc^crc8->profile_data[0]];
331 break;
332
333 case CGW_CRC8PRF_16U8:
334 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
335 break;
336
337 case CGW_CRC8PRF_SFFID_XOR:
338 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
339 (cf->can_id >> 8 & 0xFF)];
340 break;
341 }
342
343 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
344 }
345
346 /* the receive & process & send function */
347 static void can_can_gw_rcv(struct sk_buff *skb, void *data)
348 {
349 struct cgw_job *gwj = (struct cgw_job *)data;
350 struct can_frame *cf;
351 struct sk_buff *nskb;
352 int modidx = 0;
353
354 /*
355 * Do not handle CAN frames routed more than 'max_hops' times.
356 * In general we should never catch this delimiter which is intended
357 * to cover a misconfiguration protection (e.g. circular CAN routes).
358 *
359 * The Controller Area Network controllers only accept CAN frames with
360 * correct CRCs - which are not visible in the controller registers.
361 * According to skbuff.h documentation the csum_start element for IP
362 * checksums is undefined/unsued when ip_summed == CHECKSUM_UNNECESSARY.
363 * Only CAN skbs can be processed here which already have this property.
364 */
365
366 #define cgw_hops(skb) ((skb)->csum_start)
367
368 BUG_ON(skb->ip_summed != CHECKSUM_UNNECESSARY);
369
370 if (cgw_hops(skb) >= max_hops)
371 return;
372
373 if (!(gwj->dst.dev->flags & IFF_UP)) {
374 gwj->dropped_frames++;
375 return;
376 }
377
378 /* is sending the skb back to the incoming interface not allowed? */
379 if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) &&
380 skb_headroom(skb) == sizeof(struct can_skb_priv) &&
381 (((struct can_skb_priv *)(skb->head))->ifindex ==
382 gwj->dst.dev->ifindex))
383 return;
384
385 /*
386 * clone the given skb, which has not been done in can_rcv()
387 *
388 * When there is at least one modification function activated,
389 * we need to copy the skb as we want to modify skb->data.
390 */
391 if (gwj->mod.modfunc[0])
392 nskb = skb_copy(skb, GFP_ATOMIC);
393 else
394 nskb = skb_clone(skb, GFP_ATOMIC);
395
396 if (!nskb) {
397 gwj->dropped_frames++;
398 return;
399 }
400
401 /* put the incremented hop counter in the cloned skb */
402 cgw_hops(nskb) = cgw_hops(skb) + 1;
403 nskb->dev = gwj->dst.dev;
404
405 /* pointer to modifiable CAN frame */
406 cf = (struct can_frame *)nskb->data;
407
408 /* perform preprocessed modification functions if there are any */
409 while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
410 (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
411
412 /* check for checksum updates when the CAN frame has been modified */
413 if (modidx) {
414 if (gwj->mod.csumfunc.crc8)
415 (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
416
417 if (gwj->mod.csumfunc.xor)
418 (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
419 }
420
421 /* clear the skb timestamp if not configured the other way */
422 if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
423 nskb->tstamp.tv64 = 0;
424
425 /* send to netdevice */
426 if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
427 gwj->dropped_frames++;
428 else
429 gwj->handled_frames++;
430 }
431
432 static inline int cgw_register_filter(struct cgw_job *gwj)
433 {
434 return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id,
435 gwj->ccgw.filter.can_mask, can_can_gw_rcv,
436 gwj, "gw");
437 }
438
439 static inline void cgw_unregister_filter(struct cgw_job *gwj)
440 {
441 can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id,
442 gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
443 }
444
445 static int cgw_notifier(struct notifier_block *nb,
446 unsigned long msg, void *data)
447 {
448 struct net_device *dev = (struct net_device *)data;
449
450 if (!net_eq(dev_net(dev), &init_net))
451 return NOTIFY_DONE;
452 if (dev->type != ARPHRD_CAN)
453 return NOTIFY_DONE;
454
455 if (msg == NETDEV_UNREGISTER) {
456
457 struct cgw_job *gwj = NULL;
458 struct hlist_node *n, *nx;
459
460 ASSERT_RTNL();
461
462 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
463
464 if (gwj->src.dev == dev || gwj->dst.dev == dev) {
465 hlist_del(&gwj->list);
466 cgw_unregister_filter(gwj);
467 kfree(gwj);
468 }
469 }
470 }
471
472 return NOTIFY_DONE;
473 }
474
475 static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
476 u32 pid, u32 seq, int flags)
477 {
478 struct cgw_frame_mod mb;
479 struct rtcanmsg *rtcan;
480 struct nlmsghdr *nlh;
481
482 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags);
483 if (!nlh)
484 return -EMSGSIZE;
485
486 rtcan = nlmsg_data(nlh);
487 rtcan->can_family = AF_CAN;
488 rtcan->gwtype = gwj->gwtype;
489 rtcan->flags = gwj->flags;
490
491 /* add statistics if available */
492
493 if (gwj->handled_frames) {
494 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
495 goto cancel;
496 }
497
498 if (gwj->dropped_frames) {
499 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
500 goto cancel;
501 }
502
503 /* check non default settings of attributes */
504
505 if (gwj->mod.modtype.and) {
506 memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
507 mb.modtype = gwj->mod.modtype.and;
508 if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
509 goto cancel;
510 }
511
512 if (gwj->mod.modtype.or) {
513 memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
514 mb.modtype = gwj->mod.modtype.or;
515 if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
516 goto cancel;
517 }
518
519 if (gwj->mod.modtype.xor) {
520 memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
521 mb.modtype = gwj->mod.modtype.xor;
522 if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
523 goto cancel;
524 }
525
526 if (gwj->mod.modtype.set) {
527 memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
528 mb.modtype = gwj->mod.modtype.set;
529 if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
530 goto cancel;
531 }
532
533 if (gwj->mod.csumfunc.crc8) {
534 if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
535 &gwj->mod.csum.crc8) < 0)
536 goto cancel;
537 }
538
539 if (gwj->mod.csumfunc.xor) {
540 if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
541 &gwj->mod.csum.xor) < 0)
542 goto cancel;
543 }
544
545 if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
546
547 if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
548 if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
549 &gwj->ccgw.filter) < 0)
550 goto cancel;
551 }
552
553 if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
554 goto cancel;
555
556 if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
557 goto cancel;
558 }
559
560 return nlmsg_end(skb, nlh);
561
562 cancel:
563 nlmsg_cancel(skb, nlh);
564 return -EMSGSIZE;
565 }
566
567 /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
568 static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
569 {
570 struct cgw_job *gwj = NULL;
571 struct hlist_node *n;
572 int idx = 0;
573 int s_idx = cb->args[0];
574
575 rcu_read_lock();
576 hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) {
577 if (idx < s_idx)
578 goto cont;
579
580 if (cgw_put_job(skb, gwj, RTM_NEWROUTE, NETLINK_CB(cb->skb).portid,
581 cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0)
582 break;
583 cont:
584 idx++;
585 }
586 rcu_read_unlock();
587
588 cb->args[0] = idx;
589
590 return skb->len;
591 }
592
593 static const struct nla_policy cgw_policy[CGW_MAX+1] = {
594 [CGW_MOD_AND] = { .len = sizeof(struct cgw_frame_mod) },
595 [CGW_MOD_OR] = { .len = sizeof(struct cgw_frame_mod) },
596 [CGW_MOD_XOR] = { .len = sizeof(struct cgw_frame_mod) },
597 [CGW_MOD_SET] = { .len = sizeof(struct cgw_frame_mod) },
598 [CGW_CS_XOR] = { .len = sizeof(struct cgw_csum_xor) },
599 [CGW_CS_CRC8] = { .len = sizeof(struct cgw_csum_crc8) },
600 [CGW_SRC_IF] = { .type = NLA_U32 },
601 [CGW_DST_IF] = { .type = NLA_U32 },
602 [CGW_FILTER] = { .len = sizeof(struct can_filter) },
603 };
604
605 /* check for common and gwtype specific attributes */
606 static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
607 u8 gwtype, void *gwtypeattr)
608 {
609 struct nlattr *tb[CGW_MAX+1];
610 struct cgw_frame_mod mb;
611 int modidx = 0;
612 int err = 0;
613
614 /* initialize modification & checksum data space */
615 memset(mod, 0, sizeof(*mod));
616
617 err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX,
618 cgw_policy);
619 if (err < 0)
620 return err;
621
622 /* check for AND/OR/XOR/SET modifications */
623
624 if (tb[CGW_MOD_AND]) {
625 nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
626
627 canframecpy(&mod->modframe.and, &mb.cf);
628 mod->modtype.and = mb.modtype;
629
630 if (mb.modtype & CGW_MOD_ID)
631 mod->modfunc[modidx++] = mod_and_id;
632
633 if (mb.modtype & CGW_MOD_DLC)
634 mod->modfunc[modidx++] = mod_and_dlc;
635
636 if (mb.modtype & CGW_MOD_DATA)
637 mod->modfunc[modidx++] = mod_and_data;
638 }
639
640 if (tb[CGW_MOD_OR]) {
641 nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
642
643 canframecpy(&mod->modframe.or, &mb.cf);
644 mod->modtype.or = mb.modtype;
645
646 if (mb.modtype & CGW_MOD_ID)
647 mod->modfunc[modidx++] = mod_or_id;
648
649 if (mb.modtype & CGW_MOD_DLC)
650 mod->modfunc[modidx++] = mod_or_dlc;
651
652 if (mb.modtype & CGW_MOD_DATA)
653 mod->modfunc[modidx++] = mod_or_data;
654 }
655
656 if (tb[CGW_MOD_XOR]) {
657 nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
658
659 canframecpy(&mod->modframe.xor, &mb.cf);
660 mod->modtype.xor = mb.modtype;
661
662 if (mb.modtype & CGW_MOD_ID)
663 mod->modfunc[modidx++] = mod_xor_id;
664
665 if (mb.modtype & CGW_MOD_DLC)
666 mod->modfunc[modidx++] = mod_xor_dlc;
667
668 if (mb.modtype & CGW_MOD_DATA)
669 mod->modfunc[modidx++] = mod_xor_data;
670 }
671
672 if (tb[CGW_MOD_SET]) {
673 nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
674
675 canframecpy(&mod->modframe.set, &mb.cf);
676 mod->modtype.set = mb.modtype;
677
678 if (mb.modtype & CGW_MOD_ID)
679 mod->modfunc[modidx++] = mod_set_id;
680
681 if (mb.modtype & CGW_MOD_DLC)
682 mod->modfunc[modidx++] = mod_set_dlc;
683
684 if (mb.modtype & CGW_MOD_DATA)
685 mod->modfunc[modidx++] = mod_set_data;
686 }
687
688 /* check for checksum operations after CAN frame modifications */
689 if (modidx) {
690
691 if (tb[CGW_CS_CRC8]) {
692 struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]);
693
694 err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
695 c->result_idx);
696 if (err)
697 return err;
698
699 nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
700 CGW_CS_CRC8_LEN);
701
702 /*
703 * select dedicated processing function to reduce
704 * runtime operations in receive hot path.
705 */
706 if (c->from_idx < 0 || c->to_idx < 0 ||
707 c->result_idx < 0)
708 mod->csumfunc.crc8 = cgw_csum_crc8_rel;
709 else if (c->from_idx <= c->to_idx)
710 mod->csumfunc.crc8 = cgw_csum_crc8_pos;
711 else
712 mod->csumfunc.crc8 = cgw_csum_crc8_neg;
713 }
714
715 if (tb[CGW_CS_XOR]) {
716 struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]);
717
718 err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
719 c->result_idx);
720 if (err)
721 return err;
722
723 nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
724 CGW_CS_XOR_LEN);
725
726 /*
727 * select dedicated processing function to reduce
728 * runtime operations in receive hot path.
729 */
730 if (c->from_idx < 0 || c->to_idx < 0 ||
731 c->result_idx < 0)
732 mod->csumfunc.xor = cgw_csum_xor_rel;
733 else if (c->from_idx <= c->to_idx)
734 mod->csumfunc.xor = cgw_csum_xor_pos;
735 else
736 mod->csumfunc.xor = cgw_csum_xor_neg;
737 }
738 }
739
740 if (gwtype == CGW_TYPE_CAN_CAN) {
741
742 /* check CGW_TYPE_CAN_CAN specific attributes */
743
744 struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
745 memset(ccgw, 0, sizeof(*ccgw));
746
747 /* check for can_filter in attributes */
748 if (tb[CGW_FILTER])
749 nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
750 sizeof(struct can_filter));
751
752 err = -ENODEV;
753
754 /* specifying two interfaces is mandatory */
755 if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
756 return err;
757
758 ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]);
759 ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]);
760
761 /* both indices set to 0 for flushing all routing entries */
762 if (!ccgw->src_idx && !ccgw->dst_idx)
763 return 0;
764
765 /* only one index set to 0 is an error */
766 if (!ccgw->src_idx || !ccgw->dst_idx)
767 return err;
768 }
769
770 /* add the checks for other gwtypes here */
771
772 return 0;
773 }
774
775 static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
776 void *arg)
777 {
778 struct rtcanmsg *r;
779 struct cgw_job *gwj;
780 int err = 0;
781
782 if (!capable(CAP_NET_ADMIN))
783 return -EPERM;
784
785 if (nlmsg_len(nlh) < sizeof(*r))
786 return -EINVAL;
787
788 r = nlmsg_data(nlh);
789 if (r->can_family != AF_CAN)
790 return -EPFNOSUPPORT;
791
792 /* so far we only support CAN -> CAN routings */
793 if (r->gwtype != CGW_TYPE_CAN_CAN)
794 return -EINVAL;
795
796 gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
797 if (!gwj)
798 return -ENOMEM;
799
800 gwj->handled_frames = 0;
801 gwj->dropped_frames = 0;
802 gwj->flags = r->flags;
803 gwj->gwtype = r->gwtype;
804
805 err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
806 if (err < 0)
807 goto out;
808
809 err = -ENODEV;
810
811 /* ifindex == 0 is not allowed for job creation */
812 if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
813 goto out;
814
815 gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
816
817 if (!gwj->src.dev)
818 goto out;
819
820 /* check for CAN netdev not using header_ops - see gw_rcv() */
821 if (gwj->src.dev->type != ARPHRD_CAN || gwj->src.dev->header_ops)
822 goto put_src_out;
823
824 gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
825
826 if (!gwj->dst.dev)
827 goto put_src_out;
828
829 /* check for CAN netdev not using header_ops - see gw_rcv() */
830 if (gwj->dst.dev->type != ARPHRD_CAN || gwj->dst.dev->header_ops)
831 goto put_src_dst_out;
832
833 ASSERT_RTNL();
834
835 err = cgw_register_filter(gwj);
836 if (!err)
837 hlist_add_head_rcu(&gwj->list, &cgw_list);
838
839 put_src_dst_out:
840 dev_put(gwj->dst.dev);
841 put_src_out:
842 dev_put(gwj->src.dev);
843 out:
844 if (err)
845 kmem_cache_free(cgw_cache, gwj);
846
847 return err;
848 }
849
850 static void cgw_remove_all_jobs(void)
851 {
852 struct cgw_job *gwj = NULL;
853 struct hlist_node *n, *nx;
854
855 ASSERT_RTNL();
856
857 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
858 hlist_del(&gwj->list);
859 cgw_unregister_filter(gwj);
860 kfree(gwj);
861 }
862 }
863
864 static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
865 {
866 struct cgw_job *gwj = NULL;
867 struct hlist_node *n, *nx;
868 struct rtcanmsg *r;
869 struct cf_mod mod;
870 struct can_can_gw ccgw;
871 int err = 0;
872
873 if (!capable(CAP_NET_ADMIN))
874 return -EPERM;
875
876 if (nlmsg_len(nlh) < sizeof(*r))
877 return -EINVAL;
878
879 r = nlmsg_data(nlh);
880 if (r->can_family != AF_CAN)
881 return -EPFNOSUPPORT;
882
883 /* so far we only support CAN -> CAN routings */
884 if (r->gwtype != CGW_TYPE_CAN_CAN)
885 return -EINVAL;
886
887 err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw);
888 if (err < 0)
889 return err;
890
891 /* two interface indices both set to 0 => remove all entries */
892 if (!ccgw.src_idx && !ccgw.dst_idx) {
893 cgw_remove_all_jobs();
894 return 0;
895 }
896
897 err = -EINVAL;
898
899 ASSERT_RTNL();
900
901 /* remove only the first matching entry */
902 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
903
904 if (gwj->flags != r->flags)
905 continue;
906
907 if (memcmp(&gwj->mod, &mod, sizeof(mod)))
908 continue;
909
910 /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
911 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
912 continue;
913
914 hlist_del(&gwj->list);
915 cgw_unregister_filter(gwj);
916 kfree(gwj);
917 err = 0;
918 break;
919 }
920
921 return err;
922 }
923
924 static __init int cgw_module_init(void)
925 {
926 /* sanitize given module parameter */
927 max_hops = clamp_t(unsigned int, max_hops, CGW_MIN_HOPS, CGW_MAX_HOPS);
928
929 pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
930 max_hops);
931
932 cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
933 0, 0, NULL);
934
935 if (!cgw_cache)
936 return -ENOMEM;
937
938 /* set notifier */
939 notifier.notifier_call = cgw_notifier;
940 register_netdevice_notifier(&notifier);
941
942 if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs, NULL)) {
943 unregister_netdevice_notifier(&notifier);
944 kmem_cache_destroy(cgw_cache);
945 return -ENOBUFS;
946 }
947
948 /* Only the first call to __rtnl_register can fail */
949 __rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL, NULL);
950 __rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL, NULL);
951
952 return 0;
953 }
954
955 static __exit void cgw_module_exit(void)
956 {
957 rtnl_unregister_all(PF_CAN);
958
959 unregister_netdevice_notifier(&notifier);
960
961 rtnl_lock();
962 cgw_remove_all_jobs();
963 rtnl_unlock();
964
965 rcu_barrier(); /* Wait for completion of call_rcu()'s */
966
967 kmem_cache_destroy(cgw_cache);
968 }
969
970 module_init(cgw_module_init);
971 module_exit(cgw_module_exit);