xref: /openbmc/linux/net/can/gw.c (revision e6afa00a)
1c1aabdf3SOliver Hartkopp /*
2c1aabdf3SOliver Hartkopp  * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
3c1aabdf3SOliver Hartkopp  *
4c1aabdf3SOliver Hartkopp  * Copyright (c) 2011 Volkswagen Group Electronic Research
5c1aabdf3SOliver Hartkopp  * All rights reserved.
6c1aabdf3SOliver Hartkopp  *
7c1aabdf3SOliver Hartkopp  * Redistribution and use in source and binary forms, with or without
8c1aabdf3SOliver Hartkopp  * modification, are permitted provided that the following conditions
9c1aabdf3SOliver Hartkopp  * are met:
10c1aabdf3SOliver Hartkopp  * 1. Redistributions of source code must retain the above copyright
11c1aabdf3SOliver Hartkopp  *    notice, this list of conditions and the following disclaimer.
12c1aabdf3SOliver Hartkopp  * 2. Redistributions in binary form must reproduce the above copyright
13c1aabdf3SOliver Hartkopp  *    notice, this list of conditions and the following disclaimer in the
14c1aabdf3SOliver Hartkopp  *    documentation and/or other materials provided with the distribution.
15c1aabdf3SOliver Hartkopp  * 3. Neither the name of Volkswagen nor the names of its contributors
16c1aabdf3SOliver Hartkopp  *    may be used to endorse or promote products derived from this software
17c1aabdf3SOliver Hartkopp  *    without specific prior written permission.
18c1aabdf3SOliver Hartkopp  *
19c1aabdf3SOliver Hartkopp  * Alternatively, provided that this notice is retained in full, this
20c1aabdf3SOliver Hartkopp  * software may be distributed under the terms of the GNU General
21c1aabdf3SOliver Hartkopp  * Public License ("GPL") version 2, in which case the provisions of the
22c1aabdf3SOliver Hartkopp  * GPL apply INSTEAD OF those given above.
23c1aabdf3SOliver Hartkopp  *
24c1aabdf3SOliver Hartkopp  * The provided data structures and external interfaces from this code
25c1aabdf3SOliver Hartkopp  * are not restricted to be used by modules with a GPL compatible license.
26c1aabdf3SOliver Hartkopp  *
27c1aabdf3SOliver Hartkopp  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28c1aabdf3SOliver Hartkopp  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29c1aabdf3SOliver Hartkopp  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30c1aabdf3SOliver Hartkopp  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31c1aabdf3SOliver Hartkopp  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32c1aabdf3SOliver Hartkopp  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33c1aabdf3SOliver Hartkopp  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34c1aabdf3SOliver Hartkopp  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35c1aabdf3SOliver Hartkopp  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36c1aabdf3SOliver Hartkopp  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37c1aabdf3SOliver Hartkopp  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38c1aabdf3SOliver Hartkopp  * DAMAGE.
39c1aabdf3SOliver Hartkopp  *
40c1aabdf3SOliver Hartkopp  */
41c1aabdf3SOliver Hartkopp 
42c1aabdf3SOliver Hartkopp #include <linux/module.h>
43c1aabdf3SOliver Hartkopp #include <linux/init.h>
44c1aabdf3SOliver Hartkopp #include <linux/types.h>
45be286bafSOliver Hartkopp #include <linux/kernel.h>
46c1aabdf3SOliver Hartkopp #include <linux/list.h>
47c1aabdf3SOliver Hartkopp #include <linux/spinlock.h>
48c1aabdf3SOliver Hartkopp #include <linux/rcupdate.h>
49c1aabdf3SOliver Hartkopp #include <linux/rculist.h>
50c1aabdf3SOliver Hartkopp #include <linux/net.h>
51c1aabdf3SOliver Hartkopp #include <linux/netdevice.h>
52c1aabdf3SOliver Hartkopp #include <linux/if_arp.h>
53c1aabdf3SOliver Hartkopp #include <linux/skbuff.h>
54c1aabdf3SOliver Hartkopp #include <linux/can.h>
55c1aabdf3SOliver Hartkopp #include <linux/can/core.h>
56d904d3edSOliver Hartkopp #include <linux/can/skb.h>
57c1aabdf3SOliver Hartkopp #include <linux/can/gw.h>
58c1aabdf3SOliver Hartkopp #include <net/rtnetlink.h>
59c1aabdf3SOliver Hartkopp #include <net/net_namespace.h>
60c1aabdf3SOliver Hartkopp #include <net/sock.h>
61c1aabdf3SOliver Hartkopp 
62be286bafSOliver Hartkopp #define CAN_GW_VERSION "20130117"
63be286bafSOliver Hartkopp #define CAN_GW_NAME "can-gw"
64c1aabdf3SOliver Hartkopp 
65c1aabdf3SOliver Hartkopp MODULE_DESCRIPTION("PF_CAN netlink gateway");
66c1aabdf3SOliver Hartkopp MODULE_LICENSE("Dual BSD/GPL");
67c1aabdf3SOliver Hartkopp MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
68be286bafSOliver Hartkopp MODULE_ALIAS(CAN_GW_NAME);
69be286bafSOliver Hartkopp 
70be286bafSOliver Hartkopp #define CGW_MIN_HOPS 1
71be286bafSOliver Hartkopp #define CGW_MAX_HOPS 6
72be286bafSOliver Hartkopp #define CGW_DEFAULT_HOPS 1
73be286bafSOliver Hartkopp 
74be286bafSOliver Hartkopp static unsigned int max_hops __read_mostly = CGW_DEFAULT_HOPS;
75be286bafSOliver Hartkopp module_param(max_hops, uint, S_IRUGO);
76be286bafSOliver Hartkopp MODULE_PARM_DESC(max_hops,
77be286bafSOliver Hartkopp 		 "maximum " CAN_GW_NAME " routing hops for CAN frames "
78be286bafSOliver Hartkopp 		 "(valid values: " __stringify(CGW_MIN_HOPS) "-"
79be286bafSOliver Hartkopp 		 __stringify(CGW_MAX_HOPS) " hops, "
80be286bafSOliver Hartkopp 		 "default: " __stringify(CGW_DEFAULT_HOPS) ")");
81c1aabdf3SOliver Hartkopp 
82a75afd47SDaniel Baluta static HLIST_HEAD(cgw_list);
83c1aabdf3SOliver Hartkopp static struct notifier_block notifier;
84c1aabdf3SOliver Hartkopp 
85c1aabdf3SOliver Hartkopp static struct kmem_cache *cgw_cache __read_mostly;
86c1aabdf3SOliver Hartkopp 
87c1aabdf3SOliver Hartkopp /* structure that contains the (on-the-fly) CAN frame modifications */
88c1aabdf3SOliver Hartkopp struct cf_mod {
89c1aabdf3SOliver Hartkopp 	struct {
90c1aabdf3SOliver Hartkopp 		struct can_frame and;
91c1aabdf3SOliver Hartkopp 		struct can_frame or;
92c1aabdf3SOliver Hartkopp 		struct can_frame xor;
93c1aabdf3SOliver Hartkopp 		struct can_frame set;
94c1aabdf3SOliver Hartkopp 	} modframe;
95c1aabdf3SOliver Hartkopp 	struct {
96c1aabdf3SOliver Hartkopp 		u8 and;
97c1aabdf3SOliver Hartkopp 		u8 or;
98c1aabdf3SOliver Hartkopp 		u8 xor;
99c1aabdf3SOliver Hartkopp 		u8 set;
100c1aabdf3SOliver Hartkopp 	} modtype;
101c1aabdf3SOliver Hartkopp 	void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
102c1aabdf3SOliver Hartkopp 					  struct cf_mod *mod);
103c1aabdf3SOliver Hartkopp 
104c1aabdf3SOliver Hartkopp 	/* CAN frame checksum calculation after CAN frame modifications */
105c1aabdf3SOliver Hartkopp 	struct {
106c1aabdf3SOliver Hartkopp 		struct cgw_csum_xor xor;
107c1aabdf3SOliver Hartkopp 		struct cgw_csum_crc8 crc8;
108c1aabdf3SOliver Hartkopp 	} csum;
109c1aabdf3SOliver Hartkopp 	struct {
110c1aabdf3SOliver Hartkopp 		void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
111c1aabdf3SOliver Hartkopp 		void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
112c1aabdf3SOliver Hartkopp 	} csumfunc;
113c1aabdf3SOliver Hartkopp };
114c1aabdf3SOliver Hartkopp 
115c1aabdf3SOliver Hartkopp 
116c1aabdf3SOliver Hartkopp /*
117c1aabdf3SOliver Hartkopp  * So far we just support CAN -> CAN routing and frame modifications.
118c1aabdf3SOliver Hartkopp  *
119c1aabdf3SOliver Hartkopp  * The internal can_can_gw structure contains data and attributes for
120c1aabdf3SOliver Hartkopp  * a CAN -> CAN gateway job.
121c1aabdf3SOliver Hartkopp  */
122c1aabdf3SOliver Hartkopp struct can_can_gw {
123c1aabdf3SOliver Hartkopp 	struct can_filter filter;
124c1aabdf3SOliver Hartkopp 	int src_idx;
125c1aabdf3SOliver Hartkopp 	int dst_idx;
126c1aabdf3SOliver Hartkopp };
127c1aabdf3SOliver Hartkopp 
128c1aabdf3SOliver Hartkopp /* list entry for CAN gateways jobs */
129c1aabdf3SOliver Hartkopp struct cgw_job {
130c1aabdf3SOliver Hartkopp 	struct hlist_node list;
131c1aabdf3SOliver Hartkopp 	struct rcu_head rcu;
132c1aabdf3SOliver Hartkopp 	u32 handled_frames;
133c1aabdf3SOliver Hartkopp 	u32 dropped_frames;
134e6afa00aSOliver Hartkopp 	u32 deleted_frames;
135c1aabdf3SOliver Hartkopp 	struct cf_mod mod;
136c1aabdf3SOliver Hartkopp 	union {
137c1aabdf3SOliver Hartkopp 		/* CAN frame data source */
138c1aabdf3SOliver Hartkopp 		struct net_device *dev;
139c1aabdf3SOliver Hartkopp 	} src;
140c1aabdf3SOliver Hartkopp 	union {
141c1aabdf3SOliver Hartkopp 		/* CAN frame data destination */
142c1aabdf3SOliver Hartkopp 		struct net_device *dev;
143c1aabdf3SOliver Hartkopp 	} dst;
144c1aabdf3SOliver Hartkopp 	union {
145c1aabdf3SOliver Hartkopp 		struct can_can_gw ccgw;
146c1aabdf3SOliver Hartkopp 		/* tbc */
147c1aabdf3SOliver Hartkopp 	};
148c1aabdf3SOliver Hartkopp 	u8 gwtype;
149c1aabdf3SOliver Hartkopp 	u16 flags;
150c1aabdf3SOliver Hartkopp };
151c1aabdf3SOliver Hartkopp 
152c1aabdf3SOliver Hartkopp /* modification functions that are invoked in the hot path in can_can_gw_rcv */
153c1aabdf3SOliver Hartkopp 
154c1aabdf3SOliver Hartkopp #define MODFUNC(func, op) static void func(struct can_frame *cf, \
155c1aabdf3SOliver Hartkopp 					   struct cf_mod *mod) { op ; }
156c1aabdf3SOliver Hartkopp 
157c1aabdf3SOliver Hartkopp MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
158c1aabdf3SOliver Hartkopp MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc)
159c1aabdf3SOliver Hartkopp MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
160c1aabdf3SOliver Hartkopp MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
161c1aabdf3SOliver Hartkopp MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
162c1aabdf3SOliver Hartkopp MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
163c1aabdf3SOliver Hartkopp MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
164c1aabdf3SOliver Hartkopp MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
165c1aabdf3SOliver Hartkopp MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
166c1aabdf3SOliver Hartkopp MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
167c1aabdf3SOliver Hartkopp MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
168c1aabdf3SOliver Hartkopp MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
169c1aabdf3SOliver Hartkopp 
170c1aabdf3SOliver Hartkopp static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
171c1aabdf3SOliver Hartkopp {
172c1aabdf3SOliver Hartkopp 	/*
173c1aabdf3SOliver Hartkopp 	 * Copy the struct members separately to ensure that no uninitialized
174c1aabdf3SOliver Hartkopp 	 * data are copied in the 3 bytes hole of the struct. This is needed
175c1aabdf3SOliver Hartkopp 	 * to make easy compares of the data in the struct cf_mod.
176c1aabdf3SOliver Hartkopp 	 */
177c1aabdf3SOliver Hartkopp 
178c1aabdf3SOliver Hartkopp 	dst->can_id = src->can_id;
179c1aabdf3SOliver Hartkopp 	dst->can_dlc = src->can_dlc;
180c1aabdf3SOliver Hartkopp 	*(u64 *)dst->data = *(u64 *)src->data;
181c1aabdf3SOliver Hartkopp }
182c1aabdf3SOliver Hartkopp 
183c1aabdf3SOliver Hartkopp static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re)
184c1aabdf3SOliver Hartkopp {
185c1aabdf3SOliver Hartkopp 	/*
186c1aabdf3SOliver Hartkopp 	 * absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
187c1aabdf3SOliver Hartkopp 	 * relative to received dlc -1 .. -8 :
188c1aabdf3SOliver Hartkopp 	 * e.g. for received dlc = 8
189c1aabdf3SOliver Hartkopp 	 * -1 => index = 7 (data[7])
190c1aabdf3SOliver Hartkopp 	 * -3 => index = 5 (data[5])
191c1aabdf3SOliver Hartkopp 	 * -8 => index = 0 (data[0])
192c1aabdf3SOliver Hartkopp 	 */
193c1aabdf3SOliver Hartkopp 
194c1aabdf3SOliver Hartkopp 	if (fr > -9 && fr < 8 &&
195c1aabdf3SOliver Hartkopp 	    to > -9 && to < 8 &&
196c1aabdf3SOliver Hartkopp 	    re > -9 && re < 8)
197c1aabdf3SOliver Hartkopp 		return 0;
198c1aabdf3SOliver Hartkopp 	else
199c1aabdf3SOliver Hartkopp 		return -EINVAL;
200c1aabdf3SOliver Hartkopp }
201c1aabdf3SOliver Hartkopp 
202c1aabdf3SOliver Hartkopp static inline int calc_idx(int idx, int rx_dlc)
203c1aabdf3SOliver Hartkopp {
204c1aabdf3SOliver Hartkopp 	if (idx < 0)
205c1aabdf3SOliver Hartkopp 		return rx_dlc + idx;
206c1aabdf3SOliver Hartkopp 	else
207c1aabdf3SOliver Hartkopp 		return idx;
208c1aabdf3SOliver Hartkopp }
209c1aabdf3SOliver Hartkopp 
210c1aabdf3SOliver Hartkopp static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
211c1aabdf3SOliver Hartkopp {
212c1aabdf3SOliver Hartkopp 	int from = calc_idx(xor->from_idx, cf->can_dlc);
213c1aabdf3SOliver Hartkopp 	int to = calc_idx(xor->to_idx, cf->can_dlc);
214c1aabdf3SOliver Hartkopp 	int res = calc_idx(xor->result_idx, cf->can_dlc);
215c1aabdf3SOliver Hartkopp 	u8 val = xor->init_xor_val;
216c1aabdf3SOliver Hartkopp 	int i;
217c1aabdf3SOliver Hartkopp 
218c1aabdf3SOliver Hartkopp 	if (from < 0 || to < 0 || res < 0)
219c1aabdf3SOliver Hartkopp 		return;
220c1aabdf3SOliver Hartkopp 
221c1aabdf3SOliver Hartkopp 	if (from <= to) {
222c1aabdf3SOliver Hartkopp 		for (i = from; i <= to; i++)
223c1aabdf3SOliver Hartkopp 			val ^= cf->data[i];
224c1aabdf3SOliver Hartkopp 	} else {
225c1aabdf3SOliver Hartkopp 		for (i = from; i >= to; i--)
226c1aabdf3SOliver Hartkopp 			val ^= cf->data[i];
227c1aabdf3SOliver Hartkopp 	}
228c1aabdf3SOliver Hartkopp 
229c1aabdf3SOliver Hartkopp 	cf->data[res] = val;
230c1aabdf3SOliver Hartkopp }
231c1aabdf3SOliver Hartkopp 
232c1aabdf3SOliver Hartkopp static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
233c1aabdf3SOliver Hartkopp {
234c1aabdf3SOliver Hartkopp 	u8 val = xor->init_xor_val;
235c1aabdf3SOliver Hartkopp 	int i;
236c1aabdf3SOliver Hartkopp 
237c1aabdf3SOliver Hartkopp 	for (i = xor->from_idx; i <= xor->to_idx; i++)
238c1aabdf3SOliver Hartkopp 		val ^= cf->data[i];
239c1aabdf3SOliver Hartkopp 
240c1aabdf3SOliver Hartkopp 	cf->data[xor->result_idx] = val;
241c1aabdf3SOliver Hartkopp }
242c1aabdf3SOliver Hartkopp 
243c1aabdf3SOliver Hartkopp static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
244c1aabdf3SOliver Hartkopp {
245c1aabdf3SOliver Hartkopp 	u8 val = xor->init_xor_val;
246c1aabdf3SOliver Hartkopp 	int i;
247c1aabdf3SOliver Hartkopp 
248c1aabdf3SOliver Hartkopp 	for (i = xor->from_idx; i >= xor->to_idx; i--)
249c1aabdf3SOliver Hartkopp 		val ^= cf->data[i];
250c1aabdf3SOliver Hartkopp 
251c1aabdf3SOliver Hartkopp 	cf->data[xor->result_idx] = val;
252c1aabdf3SOliver Hartkopp }
253c1aabdf3SOliver Hartkopp 
254c1aabdf3SOliver Hartkopp static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
255c1aabdf3SOliver Hartkopp {
256c1aabdf3SOliver Hartkopp 	int from = calc_idx(crc8->from_idx, cf->can_dlc);
257c1aabdf3SOliver Hartkopp 	int to = calc_idx(crc8->to_idx, cf->can_dlc);
258c1aabdf3SOliver Hartkopp 	int res = calc_idx(crc8->result_idx, cf->can_dlc);
259c1aabdf3SOliver Hartkopp 	u8 crc = crc8->init_crc_val;
260c1aabdf3SOliver Hartkopp 	int i;
261c1aabdf3SOliver Hartkopp 
262c1aabdf3SOliver Hartkopp 	if (from < 0 || to < 0 || res < 0)
263c1aabdf3SOliver Hartkopp 		return;
264c1aabdf3SOliver Hartkopp 
265c1aabdf3SOliver Hartkopp 	if (from <= to) {
266c1aabdf3SOliver Hartkopp 		for (i = crc8->from_idx; i <= crc8->to_idx; i++)
267c1aabdf3SOliver Hartkopp 			crc = crc8->crctab[crc^cf->data[i]];
268c1aabdf3SOliver Hartkopp 	} else {
269c1aabdf3SOliver Hartkopp 		for (i = crc8->from_idx; i >= crc8->to_idx; i--)
270c1aabdf3SOliver Hartkopp 			crc = crc8->crctab[crc^cf->data[i]];
271c1aabdf3SOliver Hartkopp 	}
272c1aabdf3SOliver Hartkopp 
273c1aabdf3SOliver Hartkopp 	switch (crc8->profile) {
274c1aabdf3SOliver Hartkopp 
275c1aabdf3SOliver Hartkopp 	case CGW_CRC8PRF_1U8:
276c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^crc8->profile_data[0]];
277c1aabdf3SOliver Hartkopp 		break;
278c1aabdf3SOliver Hartkopp 
279c1aabdf3SOliver Hartkopp 	case  CGW_CRC8PRF_16U8:
280c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
281c1aabdf3SOliver Hartkopp 		break;
282c1aabdf3SOliver Hartkopp 
283c1aabdf3SOliver Hartkopp 	case CGW_CRC8PRF_SFFID_XOR:
284c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
285c1aabdf3SOliver Hartkopp 				   (cf->can_id >> 8 & 0xFF)];
286c1aabdf3SOliver Hartkopp 		break;
287c1aabdf3SOliver Hartkopp 
288c1aabdf3SOliver Hartkopp 	}
289c1aabdf3SOliver Hartkopp 
290c1aabdf3SOliver Hartkopp 	cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
291c1aabdf3SOliver Hartkopp }
292c1aabdf3SOliver Hartkopp 
293c1aabdf3SOliver Hartkopp static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
294c1aabdf3SOliver Hartkopp {
295c1aabdf3SOliver Hartkopp 	u8 crc = crc8->init_crc_val;
296c1aabdf3SOliver Hartkopp 	int i;
297c1aabdf3SOliver Hartkopp 
298c1aabdf3SOliver Hartkopp 	for (i = crc8->from_idx; i <= crc8->to_idx; i++)
299c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^cf->data[i]];
300c1aabdf3SOliver Hartkopp 
301c1aabdf3SOliver Hartkopp 	switch (crc8->profile) {
302c1aabdf3SOliver Hartkopp 
303c1aabdf3SOliver Hartkopp 	case CGW_CRC8PRF_1U8:
304c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^crc8->profile_data[0]];
305c1aabdf3SOliver Hartkopp 		break;
306c1aabdf3SOliver Hartkopp 
307c1aabdf3SOliver Hartkopp 	case  CGW_CRC8PRF_16U8:
308c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
309c1aabdf3SOliver Hartkopp 		break;
310c1aabdf3SOliver Hartkopp 
311c1aabdf3SOliver Hartkopp 	case CGW_CRC8PRF_SFFID_XOR:
312c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
313c1aabdf3SOliver Hartkopp 				   (cf->can_id >> 8 & 0xFF)];
314c1aabdf3SOliver Hartkopp 		break;
315c1aabdf3SOliver Hartkopp 	}
316c1aabdf3SOliver Hartkopp 
317c1aabdf3SOliver Hartkopp 	cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
318c1aabdf3SOliver Hartkopp }
319c1aabdf3SOliver Hartkopp 
320c1aabdf3SOliver Hartkopp static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
321c1aabdf3SOliver Hartkopp {
322c1aabdf3SOliver Hartkopp 	u8 crc = crc8->init_crc_val;
323c1aabdf3SOliver Hartkopp 	int i;
324c1aabdf3SOliver Hartkopp 
325c1aabdf3SOliver Hartkopp 	for (i = crc8->from_idx; i >= crc8->to_idx; i--)
326c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^cf->data[i]];
327c1aabdf3SOliver Hartkopp 
328c1aabdf3SOliver Hartkopp 	switch (crc8->profile) {
329c1aabdf3SOliver Hartkopp 
330c1aabdf3SOliver Hartkopp 	case CGW_CRC8PRF_1U8:
331c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^crc8->profile_data[0]];
332c1aabdf3SOliver Hartkopp 		break;
333c1aabdf3SOliver Hartkopp 
334c1aabdf3SOliver Hartkopp 	case  CGW_CRC8PRF_16U8:
335c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
336c1aabdf3SOliver Hartkopp 		break;
337c1aabdf3SOliver Hartkopp 
338c1aabdf3SOliver Hartkopp 	case CGW_CRC8PRF_SFFID_XOR:
339c1aabdf3SOliver Hartkopp 		crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
340c1aabdf3SOliver Hartkopp 				   (cf->can_id >> 8 & 0xFF)];
341c1aabdf3SOliver Hartkopp 		break;
342c1aabdf3SOliver Hartkopp 	}
343c1aabdf3SOliver Hartkopp 
344c1aabdf3SOliver Hartkopp 	cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
345c1aabdf3SOliver Hartkopp }
346c1aabdf3SOliver Hartkopp 
347c1aabdf3SOliver Hartkopp /* the receive & process & send function */
348c1aabdf3SOliver Hartkopp static void can_can_gw_rcv(struct sk_buff *skb, void *data)
349c1aabdf3SOliver Hartkopp {
350c1aabdf3SOliver Hartkopp 	struct cgw_job *gwj = (struct cgw_job *)data;
351c1aabdf3SOliver Hartkopp 	struct can_frame *cf;
352c1aabdf3SOliver Hartkopp 	struct sk_buff *nskb;
353c1aabdf3SOliver Hartkopp 	int modidx = 0;
354c1aabdf3SOliver Hartkopp 
355be286bafSOliver Hartkopp 	/*
356be286bafSOliver Hartkopp 	 * Do not handle CAN frames routed more than 'max_hops' times.
357be286bafSOliver Hartkopp 	 * In general we should never catch this delimiter which is intended
358be286bafSOliver Hartkopp 	 * to cover a misconfiguration protection (e.g. circular CAN routes).
359be286bafSOliver Hartkopp 	 *
360be286bafSOliver Hartkopp 	 * The Controller Area Network controllers only accept CAN frames with
361be286bafSOliver Hartkopp 	 * correct CRCs - which are not visible in the controller registers.
362be286bafSOliver Hartkopp 	 * According to skbuff.h documentation the csum_start element for IP
363be286bafSOliver Hartkopp 	 * checksums is undefined/unsued when ip_summed == CHECKSUM_UNNECESSARY.
364be286bafSOliver Hartkopp 	 * Only CAN skbs can be processed here which already have this property.
365be286bafSOliver Hartkopp 	 */
366be286bafSOliver Hartkopp 
367be286bafSOliver Hartkopp #define cgw_hops(skb) ((skb)->csum_start)
368be286bafSOliver Hartkopp 
369be286bafSOliver Hartkopp 	BUG_ON(skb->ip_summed != CHECKSUM_UNNECESSARY);
370be286bafSOliver Hartkopp 
371e6afa00aSOliver Hartkopp 	if (cgw_hops(skb) >= max_hops) {
372e6afa00aSOliver Hartkopp 		/* indicate deleted frames due to misconfiguration */
373e6afa00aSOliver Hartkopp 		gwj->deleted_frames++;
374c1aabdf3SOliver Hartkopp 		return;
375e6afa00aSOliver Hartkopp 	}
376c1aabdf3SOliver Hartkopp 
377c1aabdf3SOliver Hartkopp 	if (!(gwj->dst.dev->flags & IFF_UP)) {
378c1aabdf3SOliver Hartkopp 		gwj->dropped_frames++;
379c1aabdf3SOliver Hartkopp 		return;
380c1aabdf3SOliver Hartkopp 	}
381c1aabdf3SOliver Hartkopp 
382d904d3edSOliver Hartkopp 	/* is sending the skb back to the incoming interface not allowed? */
383d904d3edSOliver Hartkopp 	if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) &&
384d904d3edSOliver Hartkopp 	    skb_headroom(skb) == sizeof(struct can_skb_priv) &&
385d904d3edSOliver Hartkopp 	    (((struct can_skb_priv *)(skb->head))->ifindex ==
386d904d3edSOliver Hartkopp 	     gwj->dst.dev->ifindex))
387d904d3edSOliver Hartkopp 		return;
388d904d3edSOliver Hartkopp 
389c1aabdf3SOliver Hartkopp 	/*
390c1aabdf3SOliver Hartkopp 	 * clone the given skb, which has not been done in can_rcv()
391c1aabdf3SOliver Hartkopp 	 *
392c1aabdf3SOliver Hartkopp 	 * When there is at least one modification function activated,
393c1aabdf3SOliver Hartkopp 	 * we need to copy the skb as we want to modify skb->data.
394c1aabdf3SOliver Hartkopp 	 */
395c1aabdf3SOliver Hartkopp 	if (gwj->mod.modfunc[0])
396c1aabdf3SOliver Hartkopp 		nskb = skb_copy(skb, GFP_ATOMIC);
397c1aabdf3SOliver Hartkopp 	else
398c1aabdf3SOliver Hartkopp 		nskb = skb_clone(skb, GFP_ATOMIC);
399c1aabdf3SOliver Hartkopp 
400c1aabdf3SOliver Hartkopp 	if (!nskb) {
401c1aabdf3SOliver Hartkopp 		gwj->dropped_frames++;
402c1aabdf3SOliver Hartkopp 		return;
403c1aabdf3SOliver Hartkopp 	}
404c1aabdf3SOliver Hartkopp 
405be286bafSOliver Hartkopp 	/* put the incremented hop counter in the cloned skb */
406be286bafSOliver Hartkopp 	cgw_hops(nskb) = cgw_hops(skb) + 1;
407c1aabdf3SOliver Hartkopp 	nskb->dev = gwj->dst.dev;
408c1aabdf3SOliver Hartkopp 
409c1aabdf3SOliver Hartkopp 	/* pointer to modifiable CAN frame */
410c1aabdf3SOliver Hartkopp 	cf = (struct can_frame *)nskb->data;
411c1aabdf3SOliver Hartkopp 
412c1aabdf3SOliver Hartkopp 	/* perform preprocessed modification functions if there are any */
413c1aabdf3SOliver Hartkopp 	while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
414c1aabdf3SOliver Hartkopp 		(*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
415c1aabdf3SOliver Hartkopp 
416c1aabdf3SOliver Hartkopp 	/* check for checksum updates when the CAN frame has been modified */
417c1aabdf3SOliver Hartkopp 	if (modidx) {
418c1aabdf3SOliver Hartkopp 		if (gwj->mod.csumfunc.crc8)
419c1aabdf3SOliver Hartkopp 			(*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
420c1aabdf3SOliver Hartkopp 
421c1aabdf3SOliver Hartkopp 		if (gwj->mod.csumfunc.xor)
422c1aabdf3SOliver Hartkopp 			(*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
423c1aabdf3SOliver Hartkopp 	}
424c1aabdf3SOliver Hartkopp 
425c1aabdf3SOliver Hartkopp 	/* clear the skb timestamp if not configured the other way */
426c1aabdf3SOliver Hartkopp 	if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
427c1aabdf3SOliver Hartkopp 		nskb->tstamp.tv64 = 0;
428c1aabdf3SOliver Hartkopp 
429c1aabdf3SOliver Hartkopp 	/* send to netdevice */
430c1aabdf3SOliver Hartkopp 	if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
431c1aabdf3SOliver Hartkopp 		gwj->dropped_frames++;
432c1aabdf3SOliver Hartkopp 	else
433c1aabdf3SOliver Hartkopp 		gwj->handled_frames++;
434c1aabdf3SOliver Hartkopp }
435c1aabdf3SOliver Hartkopp 
436c1aabdf3SOliver Hartkopp static inline int cgw_register_filter(struct cgw_job *gwj)
437c1aabdf3SOliver Hartkopp {
438c1aabdf3SOliver Hartkopp 	return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id,
439c1aabdf3SOliver Hartkopp 			       gwj->ccgw.filter.can_mask, can_can_gw_rcv,
440c1aabdf3SOliver Hartkopp 			       gwj, "gw");
441c1aabdf3SOliver Hartkopp }
442c1aabdf3SOliver Hartkopp 
443c1aabdf3SOliver Hartkopp static inline void cgw_unregister_filter(struct cgw_job *gwj)
444c1aabdf3SOliver Hartkopp {
445c1aabdf3SOliver Hartkopp 	can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id,
446c1aabdf3SOliver Hartkopp 			  gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
447c1aabdf3SOliver Hartkopp }
448c1aabdf3SOliver Hartkopp 
449c1aabdf3SOliver Hartkopp static int cgw_notifier(struct notifier_block *nb,
450c1aabdf3SOliver Hartkopp 			unsigned long msg, void *data)
451c1aabdf3SOliver Hartkopp {
452c1aabdf3SOliver Hartkopp 	struct net_device *dev = (struct net_device *)data;
453c1aabdf3SOliver Hartkopp 
454c1aabdf3SOliver Hartkopp 	if (!net_eq(dev_net(dev), &init_net))
455c1aabdf3SOliver Hartkopp 		return NOTIFY_DONE;
456c1aabdf3SOliver Hartkopp 	if (dev->type != ARPHRD_CAN)
457c1aabdf3SOliver Hartkopp 		return NOTIFY_DONE;
458c1aabdf3SOliver Hartkopp 
459c1aabdf3SOliver Hartkopp 	if (msg == NETDEV_UNREGISTER) {
460c1aabdf3SOliver Hartkopp 
461c1aabdf3SOliver Hartkopp 		struct cgw_job *gwj = NULL;
462c1aabdf3SOliver Hartkopp 		struct hlist_node *n, *nx;
463c1aabdf3SOliver Hartkopp 
464c1aabdf3SOliver Hartkopp 		ASSERT_RTNL();
465c1aabdf3SOliver Hartkopp 
466c1aabdf3SOliver Hartkopp 		hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
467c1aabdf3SOliver Hartkopp 
468c1aabdf3SOliver Hartkopp 			if (gwj->src.dev == dev || gwj->dst.dev == dev) {
469c1aabdf3SOliver Hartkopp 				hlist_del(&gwj->list);
470c1aabdf3SOliver Hartkopp 				cgw_unregister_filter(gwj);
471c1aabdf3SOliver Hartkopp 				kfree(gwj);
472c1aabdf3SOliver Hartkopp 			}
473c1aabdf3SOliver Hartkopp 		}
474c1aabdf3SOliver Hartkopp 	}
475c1aabdf3SOliver Hartkopp 
476c1aabdf3SOliver Hartkopp 	return NOTIFY_DONE;
477c1aabdf3SOliver Hartkopp }
478c1aabdf3SOliver Hartkopp 
4791da0faa3SThomas Graf static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
4801da0faa3SThomas Graf 		       u32 pid, u32 seq, int flags)
481c1aabdf3SOliver Hartkopp {
482c1aabdf3SOliver Hartkopp 	struct cgw_frame_mod mb;
483c1aabdf3SOliver Hartkopp 	struct rtcanmsg *rtcan;
4841da0faa3SThomas Graf 	struct nlmsghdr *nlh;
4851da0faa3SThomas Graf 
4861da0faa3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags);
487c1aabdf3SOliver Hartkopp 	if (!nlh)
488c1aabdf3SOliver Hartkopp 		return -EMSGSIZE;
489c1aabdf3SOliver Hartkopp 
490c1aabdf3SOliver Hartkopp 	rtcan = nlmsg_data(nlh);
491c1aabdf3SOliver Hartkopp 	rtcan->can_family = AF_CAN;
492c1aabdf3SOliver Hartkopp 	rtcan->gwtype = gwj->gwtype;
493c1aabdf3SOliver Hartkopp 	rtcan->flags = gwj->flags;
494c1aabdf3SOliver Hartkopp 
495c1aabdf3SOliver Hartkopp 	/* add statistics if available */
496c1aabdf3SOliver Hartkopp 
497c1aabdf3SOliver Hartkopp 	if (gwj->handled_frames) {
498c1aabdf3SOliver Hartkopp 		if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
499c1aabdf3SOliver Hartkopp 			goto cancel;
500c1aabdf3SOliver Hartkopp 	}
501c1aabdf3SOliver Hartkopp 
502c1aabdf3SOliver Hartkopp 	if (gwj->dropped_frames) {
503c1aabdf3SOliver Hartkopp 		if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
504c1aabdf3SOliver Hartkopp 			goto cancel;
505c1aabdf3SOliver Hartkopp 	}
506c1aabdf3SOliver Hartkopp 
507e6afa00aSOliver Hartkopp 	if (gwj->deleted_frames) {
508e6afa00aSOliver Hartkopp 		if (nla_put_u32(skb, CGW_DELETED, gwj->deleted_frames) < 0)
509e6afa00aSOliver Hartkopp 			goto cancel;
510e6afa00aSOliver Hartkopp 	}
511e6afa00aSOliver Hartkopp 
512c1aabdf3SOliver Hartkopp 	/* check non default settings of attributes */
513c1aabdf3SOliver Hartkopp 
514c1aabdf3SOliver Hartkopp 	if (gwj->mod.modtype.and) {
515c1aabdf3SOliver Hartkopp 		memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
516c1aabdf3SOliver Hartkopp 		mb.modtype = gwj->mod.modtype.and;
517c1aabdf3SOliver Hartkopp 		if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
518c1aabdf3SOliver Hartkopp 			goto cancel;
519c1aabdf3SOliver Hartkopp 	}
520c1aabdf3SOliver Hartkopp 
521c1aabdf3SOliver Hartkopp 	if (gwj->mod.modtype.or) {
522c1aabdf3SOliver Hartkopp 		memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
523c1aabdf3SOliver Hartkopp 		mb.modtype = gwj->mod.modtype.or;
524c1aabdf3SOliver Hartkopp 		if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
525c1aabdf3SOliver Hartkopp 			goto cancel;
526c1aabdf3SOliver Hartkopp 	}
527c1aabdf3SOliver Hartkopp 
528c1aabdf3SOliver Hartkopp 	if (gwj->mod.modtype.xor) {
529c1aabdf3SOliver Hartkopp 		memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
530c1aabdf3SOliver Hartkopp 		mb.modtype = gwj->mod.modtype.xor;
531c1aabdf3SOliver Hartkopp 		if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
532c1aabdf3SOliver Hartkopp 			goto cancel;
533c1aabdf3SOliver Hartkopp 	}
534c1aabdf3SOliver Hartkopp 
535c1aabdf3SOliver Hartkopp 	if (gwj->mod.modtype.set) {
536c1aabdf3SOliver Hartkopp 		memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
537c1aabdf3SOliver Hartkopp 		mb.modtype = gwj->mod.modtype.set;
538c1aabdf3SOliver Hartkopp 		if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
539c1aabdf3SOliver Hartkopp 			goto cancel;
540c1aabdf3SOliver Hartkopp 	}
541c1aabdf3SOliver Hartkopp 
542c1aabdf3SOliver Hartkopp 	if (gwj->mod.csumfunc.crc8) {
543c1aabdf3SOliver Hartkopp 		if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
544c1aabdf3SOliver Hartkopp 			    &gwj->mod.csum.crc8) < 0)
545c1aabdf3SOliver Hartkopp 			goto cancel;
546c1aabdf3SOliver Hartkopp 	}
547c1aabdf3SOliver Hartkopp 
548c1aabdf3SOliver Hartkopp 	if (gwj->mod.csumfunc.xor) {
549c1aabdf3SOliver Hartkopp 		if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
550c1aabdf3SOliver Hartkopp 			    &gwj->mod.csum.xor) < 0)
551c1aabdf3SOliver Hartkopp 			goto cancel;
552c1aabdf3SOliver Hartkopp 	}
553c1aabdf3SOliver Hartkopp 
554c1aabdf3SOliver Hartkopp 	if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
555c1aabdf3SOliver Hartkopp 
556c1aabdf3SOliver Hartkopp 		if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
557c1aabdf3SOliver Hartkopp 			if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
558c1aabdf3SOliver Hartkopp 				    &gwj->ccgw.filter) < 0)
559c1aabdf3SOliver Hartkopp 				goto cancel;
560c1aabdf3SOliver Hartkopp 		}
561c1aabdf3SOliver Hartkopp 
562c1aabdf3SOliver Hartkopp 		if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
563c1aabdf3SOliver Hartkopp 			goto cancel;
564c1aabdf3SOliver Hartkopp 
565c1aabdf3SOliver Hartkopp 		if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
566c1aabdf3SOliver Hartkopp 			goto cancel;
567c1aabdf3SOliver Hartkopp 	}
568c1aabdf3SOliver Hartkopp 
5696eaf53caSThomas Graf 	return nlmsg_end(skb, nlh);
570c1aabdf3SOliver Hartkopp 
571c1aabdf3SOliver Hartkopp cancel:
572c1aabdf3SOliver Hartkopp 	nlmsg_cancel(skb, nlh);
573c1aabdf3SOliver Hartkopp 	return -EMSGSIZE;
574c1aabdf3SOliver Hartkopp }
575c1aabdf3SOliver Hartkopp 
576c1aabdf3SOliver Hartkopp /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
577c1aabdf3SOliver Hartkopp static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
578c1aabdf3SOliver Hartkopp {
579c1aabdf3SOliver Hartkopp 	struct cgw_job *gwj = NULL;
580c1aabdf3SOliver Hartkopp 	struct hlist_node *n;
581c1aabdf3SOliver Hartkopp 	int idx = 0;
582c1aabdf3SOliver Hartkopp 	int s_idx = cb->args[0];
583c1aabdf3SOliver Hartkopp 
584c1aabdf3SOliver Hartkopp 	rcu_read_lock();
585c1aabdf3SOliver Hartkopp 	hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) {
586c1aabdf3SOliver Hartkopp 		if (idx < s_idx)
587c1aabdf3SOliver Hartkopp 			goto cont;
588c1aabdf3SOliver Hartkopp 
58915e47304SEric W. Biederman 		if (cgw_put_job(skb, gwj, RTM_NEWROUTE, NETLINK_CB(cb->skb).portid,
5901da0faa3SThomas Graf 		    cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0)
591c1aabdf3SOliver Hartkopp 			break;
592c1aabdf3SOliver Hartkopp cont:
593c1aabdf3SOliver Hartkopp 		idx++;
594c1aabdf3SOliver Hartkopp 	}
595c1aabdf3SOliver Hartkopp 	rcu_read_unlock();
596c1aabdf3SOliver Hartkopp 
597c1aabdf3SOliver Hartkopp 	cb->args[0] = idx;
598c1aabdf3SOliver Hartkopp 
599c1aabdf3SOliver Hartkopp 	return skb->len;
600c1aabdf3SOliver Hartkopp }
601c1aabdf3SOliver Hartkopp 
602732d35fdSThomas Graf static const struct nla_policy cgw_policy[CGW_MAX+1] = {
603732d35fdSThomas Graf 	[CGW_MOD_AND]	= { .len = sizeof(struct cgw_frame_mod) },
604732d35fdSThomas Graf 	[CGW_MOD_OR]	= { .len = sizeof(struct cgw_frame_mod) },
605732d35fdSThomas Graf 	[CGW_MOD_XOR]	= { .len = sizeof(struct cgw_frame_mod) },
606732d35fdSThomas Graf 	[CGW_MOD_SET]	= { .len = sizeof(struct cgw_frame_mod) },
607732d35fdSThomas Graf 	[CGW_CS_XOR]	= { .len = sizeof(struct cgw_csum_xor) },
608732d35fdSThomas Graf 	[CGW_CS_CRC8]	= { .len = sizeof(struct cgw_csum_crc8) },
609732d35fdSThomas Graf 	[CGW_SRC_IF]	= { .type = NLA_U32 },
610732d35fdSThomas Graf 	[CGW_DST_IF]	= { .type = NLA_U32 },
611732d35fdSThomas Graf 	[CGW_FILTER]	= { .len = sizeof(struct can_filter) },
612732d35fdSThomas Graf };
613732d35fdSThomas Graf 
614c1aabdf3SOliver Hartkopp /* check for common and gwtype specific attributes */
615c1aabdf3SOliver Hartkopp static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
616c1aabdf3SOliver Hartkopp 			  u8 gwtype, void *gwtypeattr)
617c1aabdf3SOliver Hartkopp {
618c1aabdf3SOliver Hartkopp 	struct nlattr *tb[CGW_MAX+1];
619c1aabdf3SOliver Hartkopp 	struct cgw_frame_mod mb;
620c1aabdf3SOliver Hartkopp 	int modidx = 0;
621c1aabdf3SOliver Hartkopp 	int err = 0;
622c1aabdf3SOliver Hartkopp 
623c1aabdf3SOliver Hartkopp 	/* initialize modification & checksum data space */
624c1aabdf3SOliver Hartkopp 	memset(mod, 0, sizeof(*mod));
625c1aabdf3SOliver Hartkopp 
626732d35fdSThomas Graf 	err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX,
627732d35fdSThomas Graf 			  cgw_policy);
628c1aabdf3SOliver Hartkopp 	if (err < 0)
629c1aabdf3SOliver Hartkopp 		return err;
630c1aabdf3SOliver Hartkopp 
631c1aabdf3SOliver Hartkopp 	/* check for AND/OR/XOR/SET modifications */
632c1aabdf3SOliver Hartkopp 
633732d35fdSThomas Graf 	if (tb[CGW_MOD_AND]) {
634c1aabdf3SOliver Hartkopp 		nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
635c1aabdf3SOliver Hartkopp 
636c1aabdf3SOliver Hartkopp 		canframecpy(&mod->modframe.and, &mb.cf);
637c1aabdf3SOliver Hartkopp 		mod->modtype.and = mb.modtype;
638c1aabdf3SOliver Hartkopp 
639c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_ID)
640c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_and_id;
641c1aabdf3SOliver Hartkopp 
642c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_DLC)
643c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_and_dlc;
644c1aabdf3SOliver Hartkopp 
645c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_DATA)
646c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_and_data;
647c1aabdf3SOliver Hartkopp 	}
648c1aabdf3SOliver Hartkopp 
649732d35fdSThomas Graf 	if (tb[CGW_MOD_OR]) {
650c1aabdf3SOliver Hartkopp 		nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
651c1aabdf3SOliver Hartkopp 
652c1aabdf3SOliver Hartkopp 		canframecpy(&mod->modframe.or, &mb.cf);
653c1aabdf3SOliver Hartkopp 		mod->modtype.or = mb.modtype;
654c1aabdf3SOliver Hartkopp 
655c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_ID)
656c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_or_id;
657c1aabdf3SOliver Hartkopp 
658c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_DLC)
659c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_or_dlc;
660c1aabdf3SOliver Hartkopp 
661c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_DATA)
662c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_or_data;
663c1aabdf3SOliver Hartkopp 	}
664c1aabdf3SOliver Hartkopp 
665732d35fdSThomas Graf 	if (tb[CGW_MOD_XOR]) {
666c1aabdf3SOliver Hartkopp 		nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
667c1aabdf3SOliver Hartkopp 
668c1aabdf3SOliver Hartkopp 		canframecpy(&mod->modframe.xor, &mb.cf);
669c1aabdf3SOliver Hartkopp 		mod->modtype.xor = mb.modtype;
670c1aabdf3SOliver Hartkopp 
671c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_ID)
672c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_xor_id;
673c1aabdf3SOliver Hartkopp 
674c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_DLC)
675c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_xor_dlc;
676c1aabdf3SOliver Hartkopp 
677c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_DATA)
678c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_xor_data;
679c1aabdf3SOliver Hartkopp 	}
680c1aabdf3SOliver Hartkopp 
681732d35fdSThomas Graf 	if (tb[CGW_MOD_SET]) {
682c1aabdf3SOliver Hartkopp 		nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
683c1aabdf3SOliver Hartkopp 
684c1aabdf3SOliver Hartkopp 		canframecpy(&mod->modframe.set, &mb.cf);
685c1aabdf3SOliver Hartkopp 		mod->modtype.set = mb.modtype;
686c1aabdf3SOliver Hartkopp 
687c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_ID)
688c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_set_id;
689c1aabdf3SOliver Hartkopp 
690c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_DLC)
691c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_set_dlc;
692c1aabdf3SOliver Hartkopp 
693c1aabdf3SOliver Hartkopp 		if (mb.modtype & CGW_MOD_DATA)
694c1aabdf3SOliver Hartkopp 			mod->modfunc[modidx++] = mod_set_data;
695c1aabdf3SOliver Hartkopp 	}
696c1aabdf3SOliver Hartkopp 
697c1aabdf3SOliver Hartkopp 	/* check for checksum operations after CAN frame modifications */
698c1aabdf3SOliver Hartkopp 	if (modidx) {
699c1aabdf3SOliver Hartkopp 
700732d35fdSThomas Graf 		if (tb[CGW_CS_CRC8]) {
7015d91efa8SThomas Graf 			struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]);
702c1aabdf3SOliver Hartkopp 
703c1aabdf3SOliver Hartkopp 			err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
704c1aabdf3SOliver Hartkopp 						 c->result_idx);
705c1aabdf3SOliver Hartkopp 			if (err)
706c1aabdf3SOliver Hartkopp 				return err;
707c1aabdf3SOliver Hartkopp 
708c1aabdf3SOliver Hartkopp 			nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
709c1aabdf3SOliver Hartkopp 				   CGW_CS_CRC8_LEN);
710c1aabdf3SOliver Hartkopp 
711c1aabdf3SOliver Hartkopp 			/*
712c1aabdf3SOliver Hartkopp 			 * select dedicated processing function to reduce
713c1aabdf3SOliver Hartkopp 			 * runtime operations in receive hot path.
714c1aabdf3SOliver Hartkopp 			 */
715c1aabdf3SOliver Hartkopp 			if (c->from_idx < 0 || c->to_idx < 0 ||
716c1aabdf3SOliver Hartkopp 			    c->result_idx < 0)
717c1aabdf3SOliver Hartkopp 				mod->csumfunc.crc8 = cgw_csum_crc8_rel;
718c1aabdf3SOliver Hartkopp 			else if (c->from_idx <= c->to_idx)
719c1aabdf3SOliver Hartkopp 				mod->csumfunc.crc8 = cgw_csum_crc8_pos;
720c1aabdf3SOliver Hartkopp 			else
721c1aabdf3SOliver Hartkopp 				mod->csumfunc.crc8 = cgw_csum_crc8_neg;
722c1aabdf3SOliver Hartkopp 		}
723c1aabdf3SOliver Hartkopp 
724732d35fdSThomas Graf 		if (tb[CGW_CS_XOR]) {
7255d91efa8SThomas Graf 			struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]);
726c1aabdf3SOliver Hartkopp 
727c1aabdf3SOliver Hartkopp 			err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
728c1aabdf3SOliver Hartkopp 						 c->result_idx);
729c1aabdf3SOliver Hartkopp 			if (err)
730c1aabdf3SOliver Hartkopp 				return err;
731c1aabdf3SOliver Hartkopp 
732c1aabdf3SOliver Hartkopp 			nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
733c1aabdf3SOliver Hartkopp 				   CGW_CS_XOR_LEN);
734c1aabdf3SOliver Hartkopp 
735c1aabdf3SOliver Hartkopp 			/*
736c1aabdf3SOliver Hartkopp 			 * select dedicated processing function to reduce
737c1aabdf3SOliver Hartkopp 			 * runtime operations in receive hot path.
738c1aabdf3SOliver Hartkopp 			 */
739c1aabdf3SOliver Hartkopp 			if (c->from_idx < 0 || c->to_idx < 0 ||
740c1aabdf3SOliver Hartkopp 			    c->result_idx < 0)
741c1aabdf3SOliver Hartkopp 				mod->csumfunc.xor = cgw_csum_xor_rel;
742c1aabdf3SOliver Hartkopp 			else if (c->from_idx <= c->to_idx)
743c1aabdf3SOliver Hartkopp 				mod->csumfunc.xor = cgw_csum_xor_pos;
744c1aabdf3SOliver Hartkopp 			else
745c1aabdf3SOliver Hartkopp 				mod->csumfunc.xor = cgw_csum_xor_neg;
746c1aabdf3SOliver Hartkopp 		}
747c1aabdf3SOliver Hartkopp 	}
748c1aabdf3SOliver Hartkopp 
749c1aabdf3SOliver Hartkopp 	if (gwtype == CGW_TYPE_CAN_CAN) {
750c1aabdf3SOliver Hartkopp 
751c1aabdf3SOliver Hartkopp 		/* check CGW_TYPE_CAN_CAN specific attributes */
752c1aabdf3SOliver Hartkopp 
753c1aabdf3SOliver Hartkopp 		struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
754c1aabdf3SOliver Hartkopp 		memset(ccgw, 0, sizeof(*ccgw));
755c1aabdf3SOliver Hartkopp 
756c1aabdf3SOliver Hartkopp 		/* check for can_filter in attributes */
757732d35fdSThomas Graf 		if (tb[CGW_FILTER])
758c1aabdf3SOliver Hartkopp 			nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
759c1aabdf3SOliver Hartkopp 				   sizeof(struct can_filter));
760c1aabdf3SOliver Hartkopp 
761c1aabdf3SOliver Hartkopp 		err = -ENODEV;
762c1aabdf3SOliver Hartkopp 
763c1aabdf3SOliver Hartkopp 		/* specifying two interfaces is mandatory */
764c1aabdf3SOliver Hartkopp 		if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
765c1aabdf3SOliver Hartkopp 			return err;
766c1aabdf3SOliver Hartkopp 
767732d35fdSThomas Graf 		ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]);
768732d35fdSThomas Graf 		ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]);
769c1aabdf3SOliver Hartkopp 
770c1aabdf3SOliver Hartkopp 		/* both indices set to 0 for flushing all routing entries */
771c1aabdf3SOliver Hartkopp 		if (!ccgw->src_idx && !ccgw->dst_idx)
772c1aabdf3SOliver Hartkopp 			return 0;
773c1aabdf3SOliver Hartkopp 
774c1aabdf3SOliver Hartkopp 		/* only one index set to 0 is an error */
775c1aabdf3SOliver Hartkopp 		if (!ccgw->src_idx || !ccgw->dst_idx)
776c1aabdf3SOliver Hartkopp 			return err;
777c1aabdf3SOliver Hartkopp 	}
778c1aabdf3SOliver Hartkopp 
779c1aabdf3SOliver Hartkopp 	/* add the checks for other gwtypes here */
780c1aabdf3SOliver Hartkopp 
781c1aabdf3SOliver Hartkopp 	return 0;
782c1aabdf3SOliver Hartkopp }
783c1aabdf3SOliver Hartkopp 
784c1aabdf3SOliver Hartkopp static int cgw_create_job(struct sk_buff *skb,  struct nlmsghdr *nlh,
785c1aabdf3SOliver Hartkopp 			  void *arg)
786c1aabdf3SOliver Hartkopp {
787c1aabdf3SOliver Hartkopp 	struct rtcanmsg *r;
788c1aabdf3SOliver Hartkopp 	struct cgw_job *gwj;
789c1aabdf3SOliver Hartkopp 	int err = 0;
790c1aabdf3SOliver Hartkopp 
791dfc47ef8SEric W. Biederman 	if (!capable(CAP_NET_ADMIN))
792dfc47ef8SEric W. Biederman 		return -EPERM;
793dfc47ef8SEric W. Biederman 
794c1aabdf3SOliver Hartkopp 	if (nlmsg_len(nlh) < sizeof(*r))
795c1aabdf3SOliver Hartkopp 		return -EINVAL;
796c1aabdf3SOliver Hartkopp 
797c1aabdf3SOliver Hartkopp 	r = nlmsg_data(nlh);
798c1aabdf3SOliver Hartkopp 	if (r->can_family != AF_CAN)
799c1aabdf3SOliver Hartkopp 		return -EPFNOSUPPORT;
800c1aabdf3SOliver Hartkopp 
801c1aabdf3SOliver Hartkopp 	/* so far we only support CAN -> CAN routings */
802c1aabdf3SOliver Hartkopp 	if (r->gwtype != CGW_TYPE_CAN_CAN)
803c1aabdf3SOliver Hartkopp 		return -EINVAL;
804c1aabdf3SOliver Hartkopp 
805c1aabdf3SOliver Hartkopp 	gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
806c1aabdf3SOliver Hartkopp 	if (!gwj)
807c1aabdf3SOliver Hartkopp 		return -ENOMEM;
808c1aabdf3SOliver Hartkopp 
809c1aabdf3SOliver Hartkopp 	gwj->handled_frames = 0;
810c1aabdf3SOliver Hartkopp 	gwj->dropped_frames = 0;
811e6afa00aSOliver Hartkopp 	gwj->deleted_frames = 0;
812c1aabdf3SOliver Hartkopp 	gwj->flags = r->flags;
813c1aabdf3SOliver Hartkopp 	gwj->gwtype = r->gwtype;
814c1aabdf3SOliver Hartkopp 
815c1aabdf3SOliver Hartkopp 	err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
816c1aabdf3SOliver Hartkopp 	if (err < 0)
817c1aabdf3SOliver Hartkopp 		goto out;
818c1aabdf3SOliver Hartkopp 
819c1aabdf3SOliver Hartkopp 	err = -ENODEV;
820c1aabdf3SOliver Hartkopp 
821c1aabdf3SOliver Hartkopp 	/* ifindex == 0 is not allowed for job creation */
822c1aabdf3SOliver Hartkopp 	if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
823c1aabdf3SOliver Hartkopp 		goto out;
824c1aabdf3SOliver Hartkopp 
825c1aabdf3SOliver Hartkopp 	gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
826c1aabdf3SOliver Hartkopp 
827c1aabdf3SOliver Hartkopp 	if (!gwj->src.dev)
828c1aabdf3SOliver Hartkopp 		goto out;
829c1aabdf3SOliver Hartkopp 
830c1aabdf3SOliver Hartkopp 	/* check for CAN netdev not using header_ops - see gw_rcv() */
831c1aabdf3SOliver Hartkopp 	if (gwj->src.dev->type != ARPHRD_CAN || gwj->src.dev->header_ops)
832c1aabdf3SOliver Hartkopp 		goto put_src_out;
833c1aabdf3SOliver Hartkopp 
834c1aabdf3SOliver Hartkopp 	gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
835c1aabdf3SOliver Hartkopp 
836c1aabdf3SOliver Hartkopp 	if (!gwj->dst.dev)
837c1aabdf3SOliver Hartkopp 		goto put_src_out;
838c1aabdf3SOliver Hartkopp 
839c1aabdf3SOliver Hartkopp 	/* check for CAN netdev not using header_ops - see gw_rcv() */
840c1aabdf3SOliver Hartkopp 	if (gwj->dst.dev->type != ARPHRD_CAN || gwj->dst.dev->header_ops)
841c1aabdf3SOliver Hartkopp 		goto put_src_dst_out;
842c1aabdf3SOliver Hartkopp 
843c1aabdf3SOliver Hartkopp 	ASSERT_RTNL();
844c1aabdf3SOliver Hartkopp 
845c1aabdf3SOliver Hartkopp 	err = cgw_register_filter(gwj);
846c1aabdf3SOliver Hartkopp 	if (!err)
847c1aabdf3SOliver Hartkopp 		hlist_add_head_rcu(&gwj->list, &cgw_list);
848c1aabdf3SOliver Hartkopp 
849c1aabdf3SOliver Hartkopp put_src_dst_out:
850c1aabdf3SOliver Hartkopp 	dev_put(gwj->dst.dev);
851c1aabdf3SOliver Hartkopp put_src_out:
852c1aabdf3SOliver Hartkopp 	dev_put(gwj->src.dev);
853c1aabdf3SOliver Hartkopp out:
854c1aabdf3SOliver Hartkopp 	if (err)
855c1aabdf3SOliver Hartkopp 		kmem_cache_free(cgw_cache, gwj);
856c1aabdf3SOliver Hartkopp 
857c1aabdf3SOliver Hartkopp 	return err;
858c1aabdf3SOliver Hartkopp }
859c1aabdf3SOliver Hartkopp 
860c1aabdf3SOliver Hartkopp static void cgw_remove_all_jobs(void)
861c1aabdf3SOliver Hartkopp {
862c1aabdf3SOliver Hartkopp 	struct cgw_job *gwj = NULL;
863c1aabdf3SOliver Hartkopp 	struct hlist_node *n, *nx;
864c1aabdf3SOliver Hartkopp 
865c1aabdf3SOliver Hartkopp 	ASSERT_RTNL();
866c1aabdf3SOliver Hartkopp 
867c1aabdf3SOliver Hartkopp 	hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
868c1aabdf3SOliver Hartkopp 		hlist_del(&gwj->list);
869c1aabdf3SOliver Hartkopp 		cgw_unregister_filter(gwj);
870c1aabdf3SOliver Hartkopp 		kfree(gwj);
871c1aabdf3SOliver Hartkopp 	}
872c1aabdf3SOliver Hartkopp }
873c1aabdf3SOliver Hartkopp 
874c1aabdf3SOliver Hartkopp static int cgw_remove_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
875c1aabdf3SOliver Hartkopp {
876c1aabdf3SOliver Hartkopp 	struct cgw_job *gwj = NULL;
877c1aabdf3SOliver Hartkopp 	struct hlist_node *n, *nx;
878c1aabdf3SOliver Hartkopp 	struct rtcanmsg *r;
879c1aabdf3SOliver Hartkopp 	struct cf_mod mod;
880c1aabdf3SOliver Hartkopp 	struct can_can_gw ccgw;
881c1aabdf3SOliver Hartkopp 	int err = 0;
882c1aabdf3SOliver Hartkopp 
883dfc47ef8SEric W. Biederman 	if (!capable(CAP_NET_ADMIN))
884dfc47ef8SEric W. Biederman 		return -EPERM;
885dfc47ef8SEric W. Biederman 
886c1aabdf3SOliver Hartkopp 	if (nlmsg_len(nlh) < sizeof(*r))
887c1aabdf3SOliver Hartkopp 		return -EINVAL;
888c1aabdf3SOliver Hartkopp 
889c1aabdf3SOliver Hartkopp 	r = nlmsg_data(nlh);
890c1aabdf3SOliver Hartkopp 	if (r->can_family != AF_CAN)
891c1aabdf3SOliver Hartkopp 		return -EPFNOSUPPORT;
892c1aabdf3SOliver Hartkopp 
893c1aabdf3SOliver Hartkopp 	/* so far we only support CAN -> CAN routings */
894c1aabdf3SOliver Hartkopp 	if (r->gwtype != CGW_TYPE_CAN_CAN)
895c1aabdf3SOliver Hartkopp 		return -EINVAL;
896c1aabdf3SOliver Hartkopp 
897c1aabdf3SOliver Hartkopp 	err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw);
898c1aabdf3SOliver Hartkopp 	if (err < 0)
899c1aabdf3SOliver Hartkopp 		return err;
900c1aabdf3SOliver Hartkopp 
901c1aabdf3SOliver Hartkopp 	/* two interface indices both set to 0 => remove all entries */
902c1aabdf3SOliver Hartkopp 	if (!ccgw.src_idx && !ccgw.dst_idx) {
903c1aabdf3SOliver Hartkopp 		cgw_remove_all_jobs();
904c1aabdf3SOliver Hartkopp 		return 0;
905c1aabdf3SOliver Hartkopp 	}
906c1aabdf3SOliver Hartkopp 
907c1aabdf3SOliver Hartkopp 	err = -EINVAL;
908c1aabdf3SOliver Hartkopp 
909c1aabdf3SOliver Hartkopp 	ASSERT_RTNL();
910c1aabdf3SOliver Hartkopp 
911c1aabdf3SOliver Hartkopp 	/* remove only the first matching entry */
912c1aabdf3SOliver Hartkopp 	hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
913c1aabdf3SOliver Hartkopp 
914c1aabdf3SOliver Hartkopp 		if (gwj->flags != r->flags)
915c1aabdf3SOliver Hartkopp 			continue;
916c1aabdf3SOliver Hartkopp 
917c1aabdf3SOliver Hartkopp 		if (memcmp(&gwj->mod, &mod, sizeof(mod)))
918c1aabdf3SOliver Hartkopp 			continue;
919c1aabdf3SOliver Hartkopp 
920c1aabdf3SOliver Hartkopp 		/* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
921c1aabdf3SOliver Hartkopp 		if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
922c1aabdf3SOliver Hartkopp 			continue;
923c1aabdf3SOliver Hartkopp 
924c1aabdf3SOliver Hartkopp 		hlist_del(&gwj->list);
925c1aabdf3SOliver Hartkopp 		cgw_unregister_filter(gwj);
926c1aabdf3SOliver Hartkopp 		kfree(gwj);
927c1aabdf3SOliver Hartkopp 		err = 0;
928c1aabdf3SOliver Hartkopp 		break;
929c1aabdf3SOliver Hartkopp 	}
930c1aabdf3SOliver Hartkopp 
931c1aabdf3SOliver Hartkopp 	return err;
932c1aabdf3SOliver Hartkopp }
933c1aabdf3SOliver Hartkopp 
934c1aabdf3SOliver Hartkopp static __init int cgw_module_init(void)
935c1aabdf3SOliver Hartkopp {
936be286bafSOliver Hartkopp 	/* sanitize given module parameter */
937be286bafSOliver Hartkopp 	max_hops = clamp_t(unsigned int, max_hops, CGW_MIN_HOPS, CGW_MAX_HOPS);
938be286bafSOliver Hartkopp 
939be286bafSOliver Hartkopp 	pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
940be286bafSOliver Hartkopp 		max_hops);
941c1aabdf3SOliver Hartkopp 
942c1aabdf3SOliver Hartkopp 	cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
943c1aabdf3SOliver Hartkopp 				      0, 0, NULL);
944c1aabdf3SOliver Hartkopp 
945c1aabdf3SOliver Hartkopp 	if (!cgw_cache)
946c1aabdf3SOliver Hartkopp 		return -ENOMEM;
947c1aabdf3SOliver Hartkopp 
948c1aabdf3SOliver Hartkopp 	/* set notifier */
949c1aabdf3SOliver Hartkopp 	notifier.notifier_call = cgw_notifier;
950c1aabdf3SOliver Hartkopp 	register_netdevice_notifier(&notifier);
951c1aabdf3SOliver Hartkopp 
952c1aabdf3SOliver Hartkopp 	if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs, NULL)) {
953c1aabdf3SOliver Hartkopp 		unregister_netdevice_notifier(&notifier);
954c1aabdf3SOliver Hartkopp 		kmem_cache_destroy(cgw_cache);
955c1aabdf3SOliver Hartkopp 		return -ENOBUFS;
956c1aabdf3SOliver Hartkopp 	}
957c1aabdf3SOliver Hartkopp 
958c1aabdf3SOliver Hartkopp 	/* Only the first call to __rtnl_register can fail */
959c1aabdf3SOliver Hartkopp 	__rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL, NULL);
960c1aabdf3SOliver Hartkopp 	__rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL, NULL);
961c1aabdf3SOliver Hartkopp 
962c1aabdf3SOliver Hartkopp 	return 0;
963c1aabdf3SOliver Hartkopp }
964c1aabdf3SOliver Hartkopp 
965c1aabdf3SOliver Hartkopp static __exit void cgw_module_exit(void)
966c1aabdf3SOliver Hartkopp {
967c1aabdf3SOliver Hartkopp 	rtnl_unregister_all(PF_CAN);
968c1aabdf3SOliver Hartkopp 
969c1aabdf3SOliver Hartkopp 	unregister_netdevice_notifier(&notifier);
970c1aabdf3SOliver Hartkopp 
971c1aabdf3SOliver Hartkopp 	rtnl_lock();
972c1aabdf3SOliver Hartkopp 	cgw_remove_all_jobs();
973c1aabdf3SOliver Hartkopp 	rtnl_unlock();
974c1aabdf3SOliver Hartkopp 
975c1aabdf3SOliver Hartkopp 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
976c1aabdf3SOliver Hartkopp 
977c1aabdf3SOliver Hartkopp 	kmem_cache_destroy(cgw_cache);
978c1aabdf3SOliver Hartkopp }
979c1aabdf3SOliver Hartkopp 
980c1aabdf3SOliver Hartkopp module_init(cgw_module_init);
981c1aabdf3SOliver Hartkopp module_exit(cgw_module_exit);
982