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