Blame view

net/can/gw.c 25.7 KB
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  /*
   * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
   *
   * Copyright (c) 2011 Volkswagen Group Electronic Research
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in the
   *    documentation and/or other materials provided with the distribution.
   * 3. Neither the name of Volkswagen nor the names of its contributors
   *    may be used to endorse or promote products derived from this software
   *    without specific prior written permission.
   *
   * Alternatively, provided that this notice is retained in full, this
   * software may be distributed under the terms of the GNU General
   * Public License ("GPL") version 2, in which case the provisions of the
   * GPL apply INSTEAD OF those given above.
   *
   * The provided data structures and external interfaces from this code
   * are not restricted to be used by modules with a GPL compatible license.
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
   * DAMAGE.
   *
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
40
41
42
43
44
   */
  
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/types.h>
be286bafe   Oliver Hartkopp   can: gw: add a va...
45
  #include <linux/kernel.h>
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
46
47
48
49
50
51
52
53
54
55
  #include <linux/list.h>
  #include <linux/spinlock.h>
  #include <linux/rcupdate.h>
  #include <linux/rculist.h>
  #include <linux/net.h>
  #include <linux/netdevice.h>
  #include <linux/if_arp.h>
  #include <linux/skbuff.h>
  #include <linux/can.h>
  #include <linux/can/core.h>
d904d3edc   Oliver Hartkopp   can: gw: make rou...
56
  #include <linux/can/skb.h>
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
57
58
59
60
  #include <linux/can/gw.h>
  #include <net/rtnetlink.h>
  #include <net/net_namespace.h>
  #include <net/sock.h>
be286bafe   Oliver Hartkopp   can: gw: add a va...
61
62
  #define CAN_GW_VERSION "20130117"
  #define CAN_GW_NAME "can-gw"
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
63
64
65
66
  
  MODULE_DESCRIPTION("PF_CAN netlink gateway");
  MODULE_LICENSE("Dual BSD/GPL");
  MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
be286bafe   Oliver Hartkopp   can: gw: add a va...
67
68
69
70
71
72
73
74
75
76
77
78
79
  MODULE_ALIAS(CAN_GW_NAME);
  
  #define CGW_MIN_HOPS 1
  #define CGW_MAX_HOPS 6
  #define CGW_DEFAULT_HOPS 1
  
  static unsigned int max_hops __read_mostly = CGW_DEFAULT_HOPS;
  module_param(max_hops, uint, S_IRUGO);
  MODULE_PARM_DESC(max_hops,
  		 "maximum " CAN_GW_NAME " routing hops for CAN frames "
  		 "(valid values: " __stringify(CGW_MIN_HOPS) "-"
  		 __stringify(CGW_MAX_HOPS) " hops, "
  		 "default: " __stringify(CGW_DEFAULT_HOPS) ")");
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
80

a75afd477   Daniel Baluta   can: fix sparse w...
81
  static HLIST_HEAD(cgw_list);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  static struct notifier_block notifier;
  
  static struct kmem_cache *cgw_cache __read_mostly;
  
  /* structure that contains the (on-the-fly) CAN frame modifications */
  struct cf_mod {
  	struct {
  		struct can_frame and;
  		struct can_frame or;
  		struct can_frame xor;
  		struct can_frame set;
  	} modframe;
  	struct {
  		u8 and;
  		u8 or;
  		u8 xor;
  		u8 set;
  	} modtype;
  	void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
  					  struct cf_mod *mod);
  
  	/* CAN frame checksum calculation after CAN frame modifications */
  	struct {
  		struct cgw_csum_xor xor;
  		struct cgw_csum_crc8 crc8;
  	} csum;
  	struct {
  		void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
  		void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
  	} csumfunc;
dd895d7f2   Oliver Hartkopp   can: cangw: intro...
112
  	u32 uid;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  };
  
  
  /*
   * So far we just support CAN -> CAN routing and frame modifications.
   *
   * The internal can_can_gw structure contains data and attributes for
   * a CAN -> CAN gateway job.
   */
  struct can_can_gw {
  	struct can_filter filter;
  	int src_idx;
  	int dst_idx;
  };
  
  /* list entry for CAN gateways jobs */
  struct cgw_job {
  	struct hlist_node list;
  	struct rcu_head rcu;
  	u32 handled_frames;
  	u32 dropped_frames;
e6afa00a1   Oliver Hartkopp   can: gw: indicate...
134
  	u32 deleted_frames;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  	struct cf_mod mod;
  	union {
  		/* CAN frame data source */
  		struct net_device *dev;
  	} src;
  	union {
  		/* CAN frame data destination */
  		struct net_device *dev;
  	} dst;
  	union {
  		struct can_can_gw ccgw;
  		/* tbc */
  	};
  	u8 gwtype;
391ac1282   Oliver Hartkopp   can: gw: add a pe...
149
  	u8 limit_hops;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  	u16 flags;
  };
  
  /* modification functions that are invoked in the hot path in can_can_gw_rcv */
  
  #define MODFUNC(func, op) static void func(struct can_frame *cf, \
  					   struct cf_mod *mod) { op ; }
  
  MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
  MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc)
  MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
  MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
  MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
  MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
  MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
  MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
  MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
  MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
  MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
  MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
  
  static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
  {
  	/*
  	 * Copy the struct members separately to ensure that no uninitialized
  	 * data are copied in the 3 bytes hole of the struct. This is needed
  	 * to make easy compares of the data in the struct cf_mod.
  	 */
  
  	dst->can_id = src->can_id;
  	dst->can_dlc = src->can_dlc;
  	*(u64 *)dst->data = *(u64 *)src->data;
  }
  
  static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re)
  {
  	/*
  	 * absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
  	 * relative to received dlc -1 .. -8 :
  	 * e.g. for received dlc = 8
  	 * -1 => index = 7 (data[7])
  	 * -3 => index = 5 (data[5])
  	 * -8 => index = 0 (data[0])
  	 */
  
  	if (fr > -9 && fr < 8 &&
  	    to > -9 && to < 8 &&
  	    re > -9 && re < 8)
  		return 0;
  	else
  		return -EINVAL;
  }
  
  static inline int calc_idx(int idx, int rx_dlc)
  {
  	if (idx < 0)
  		return rx_dlc + idx;
  	else
  		return idx;
  }
  
  static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
  {
  	int from = calc_idx(xor->from_idx, cf->can_dlc);
  	int to = calc_idx(xor->to_idx, cf->can_dlc);
  	int res = calc_idx(xor->result_idx, cf->can_dlc);
  	u8 val = xor->init_xor_val;
  	int i;
  
  	if (from < 0 || to < 0 || res < 0)
  		return;
  
  	if (from <= to) {
  		for (i = from; i <= to; i++)
  			val ^= cf->data[i];
  	} else {
  		for (i = from; i >= to; i--)
  			val ^= cf->data[i];
  	}
  
  	cf->data[res] = val;
  }
  
  static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
  {
  	u8 val = xor->init_xor_val;
  	int i;
  
  	for (i = xor->from_idx; i <= xor->to_idx; i++)
  		val ^= cf->data[i];
  
  	cf->data[xor->result_idx] = val;
  }
  
  static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
  {
  	u8 val = xor->init_xor_val;
  	int i;
  
  	for (i = xor->from_idx; i >= xor->to_idx; i--)
  		val ^= cf->data[i];
  
  	cf->data[xor->result_idx] = val;
  }
  
  static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  {
  	int from = calc_idx(crc8->from_idx, cf->can_dlc);
  	int to = calc_idx(crc8->to_idx, cf->can_dlc);
  	int res = calc_idx(crc8->result_idx, cf->can_dlc);
  	u8 crc = crc8->init_crc_val;
  	int i;
  
  	if (from < 0 || to < 0 || res < 0)
  		return;
  
  	if (from <= to) {
  		for (i = crc8->from_idx; i <= crc8->to_idx; i++)
  			crc = crc8->crctab[crc^cf->data[i]];
  	} else {
  		for (i = crc8->from_idx; i >= crc8->to_idx; i--)
  			crc = crc8->crctab[crc^cf->data[i]];
  	}
  
  	switch (crc8->profile) {
  
  	case CGW_CRC8PRF_1U8:
  		crc = crc8->crctab[crc^crc8->profile_data[0]];
  		break;
  
  	case  CGW_CRC8PRF_16U8:
  		crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  		break;
  
  	case CGW_CRC8PRF_SFFID_XOR:
  		crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  				   (cf->can_id >> 8 & 0xFF)];
  		break;
  
  	}
  
  	cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  }
  
  static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  {
  	u8 crc = crc8->init_crc_val;
  	int i;
  
  	for (i = crc8->from_idx; i <= crc8->to_idx; i++)
  		crc = crc8->crctab[crc^cf->data[i]];
  
  	switch (crc8->profile) {
  
  	case CGW_CRC8PRF_1U8:
  		crc = crc8->crctab[crc^crc8->profile_data[0]];
  		break;
  
  	case  CGW_CRC8PRF_16U8:
  		crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  		break;
  
  	case CGW_CRC8PRF_SFFID_XOR:
  		crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  				   (cf->can_id >> 8 & 0xFF)];
  		break;
  	}
  
  	cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  }
  
  static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  {
  	u8 crc = crc8->init_crc_val;
  	int i;
  
  	for (i = crc8->from_idx; i >= crc8->to_idx; i--)
  		crc = crc8->crctab[crc^cf->data[i]];
  
  	switch (crc8->profile) {
  
  	case CGW_CRC8PRF_1U8:
  		crc = crc8->crctab[crc^crc8->profile_data[0]];
  		break;
  
  	case  CGW_CRC8PRF_16U8:
  		crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  		break;
  
  	case CGW_CRC8PRF_SFFID_XOR:
  		crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  				   (cf->can_id >> 8 & 0xFF)];
  		break;
  	}
  
  	cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  }
  
  /* the receive & process & send function */
  static void can_can_gw_rcv(struct sk_buff *skb, void *data)
  {
  	struct cgw_job *gwj = (struct cgw_job *)data;
  	struct can_frame *cf;
  	struct sk_buff *nskb;
  	int modidx = 0;
be286bafe   Oliver Hartkopp   can: gw: add a va...
355
356
357
358
359
360
361
362
  	/*
  	 * Do not handle CAN frames routed more than 'max_hops' times.
  	 * In general we should never catch this delimiter which is intended
  	 * to cover a misconfiguration protection (e.g. circular CAN routes).
  	 *
  	 * The Controller Area Network controllers only accept CAN frames with
  	 * correct CRCs - which are not visible in the controller registers.
  	 * According to skbuff.h documentation the csum_start element for IP
069f8457a   Jeremiah Mahler   can: fix spelling...
363
  	 * checksums is undefined/unused when ip_summed == CHECKSUM_UNNECESSARY.
be286bafe   Oliver Hartkopp   can: gw: add a va...
364
365
366
367
368
369
  	 * Only CAN skbs can be processed here which already have this property.
  	 */
  
  #define cgw_hops(skb) ((skb)->csum_start)
  
  	BUG_ON(skb->ip_summed != CHECKSUM_UNNECESSARY);
e6afa00a1   Oliver Hartkopp   can: gw: indicate...
370
371
372
  	if (cgw_hops(skb) >= max_hops) {
  		/* indicate deleted frames due to misconfiguration */
  		gwj->deleted_frames++;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
373
  		return;
e6afa00a1   Oliver Hartkopp   can: gw: indicate...
374
  	}
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
375
376
377
378
379
  
  	if (!(gwj->dst.dev->flags & IFF_UP)) {
  		gwj->dropped_frames++;
  		return;
  	}
d904d3edc   Oliver Hartkopp   can: gw: make rou...
380
381
  	/* is sending the skb back to the incoming interface not allowed? */
  	if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) &&
2bf3440d7   Oliver Hartkopp   can: rework skb r...
382
  	    can_skb_prv(skb)->ifindex == gwj->dst.dev->ifindex)
d904d3edc   Oliver Hartkopp   can: gw: make rou...
383
  		return;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  	/*
  	 * clone the given skb, which has not been done in can_rcv()
  	 *
  	 * When there is at least one modification function activated,
  	 * we need to copy the skb as we want to modify skb->data.
  	 */
  	if (gwj->mod.modfunc[0])
  		nskb = skb_copy(skb, GFP_ATOMIC);
  	else
  		nskb = skb_clone(skb, GFP_ATOMIC);
  
  	if (!nskb) {
  		gwj->dropped_frames++;
  		return;
  	}
be286bafe   Oliver Hartkopp   can: gw: add a va...
399
400
  	/* put the incremented hop counter in the cloned skb */
  	cgw_hops(nskb) = cgw_hops(skb) + 1;
391ac1282   Oliver Hartkopp   can: gw: add a pe...
401
402
403
404
  
  	/* first processing of this CAN frame -> adjust to private hop limit */
  	if (gwj->limit_hops && cgw_hops(nskb) == 1)
  		cgw_hops(nskb) = max_hops - gwj->limit_hops + 1;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
  	nskb->dev = gwj->dst.dev;
  
  	/* pointer to modifiable CAN frame */
  	cf = (struct can_frame *)nskb->data;
  
  	/* perform preprocessed modification functions if there are any */
  	while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
  		(*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
  
  	/* check for checksum updates when the CAN frame has been modified */
  	if (modidx) {
  		if (gwj->mod.csumfunc.crc8)
  			(*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
  
  		if (gwj->mod.csumfunc.xor)
  			(*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
  	}
  
  	/* clear the skb timestamp if not configured the other way */
  	if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
  		nskb->tstamp.tv64 = 0;
  
  	/* send to netdevice */
  	if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
  		gwj->dropped_frames++;
  	else
  		gwj->handled_frames++;
  }
  
  static inline int cgw_register_filter(struct cgw_job *gwj)
  {
  	return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id,
  			       gwj->ccgw.filter.can_mask, can_can_gw_rcv,
adf86d59b   Eric Dumazet   can: Fix kernel p...
438
  			       gwj, "gw", NULL);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
439
440
441
442
443
444
445
446
447
  }
  
  static inline void cgw_unregister_filter(struct cgw_job *gwj)
  {
  	can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id,
  			  gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
  }
  
  static int cgw_notifier(struct notifier_block *nb,
351638e7d   Jiri Pirko   net: pass info st...
448
  			unsigned long msg, void *ptr)
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
449
  {
351638e7d   Jiri Pirko   net: pass info st...
450
  	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
451
452
453
454
455
456
457
458
459
  
  	if (!net_eq(dev_net(dev), &init_net))
  		return NOTIFY_DONE;
  	if (dev->type != ARPHRD_CAN)
  		return NOTIFY_DONE;
  
  	if (msg == NETDEV_UNREGISTER) {
  
  		struct cgw_job *gwj = NULL;
b67bfe0d4   Sasha Levin   hlist: drop the n...
460
  		struct hlist_node *nx;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
461
462
  
  		ASSERT_RTNL();
b67bfe0d4   Sasha Levin   hlist: drop the n...
463
  		hlist_for_each_entry_safe(gwj, nx, &cgw_list, list) {
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
464
465
466
467
  
  			if (gwj->src.dev == dev || gwj->dst.dev == dev) {
  				hlist_del(&gwj->list);
  				cgw_unregister_filter(gwj);
3480a2125   Wei Yongjun   can: gw: use kmem...
468
  				kmem_cache_free(cgw_cache, gwj);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
469
470
471
472
473
474
  			}
  		}
  	}
  
  	return NOTIFY_DONE;
  }
1da0faa38   Thomas Graf   can: gw: Properly...
475
476
  static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
  		       u32 pid, u32 seq, int flags)
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
477
478
479
  {
  	struct cgw_frame_mod mb;
  	struct rtcanmsg *rtcan;
1da0faa38   Thomas Graf   can: gw: Properly...
480
481
482
  	struct nlmsghdr *nlh;
  
  	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
483
484
485
486
487
488
489
490
491
492
493
494
495
  	if (!nlh)
  		return -EMSGSIZE;
  
  	rtcan = nlmsg_data(nlh);
  	rtcan->can_family = AF_CAN;
  	rtcan->gwtype = gwj->gwtype;
  	rtcan->flags = gwj->flags;
  
  	/* add statistics if available */
  
  	if (gwj->handled_frames) {
  		if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
  			goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
496
497
498
499
500
  	}
  
  	if (gwj->dropped_frames) {
  		if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
  			goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
501
  	}
e6afa00a1   Oliver Hartkopp   can: gw: indicate...
502
503
504
505
  	if (gwj->deleted_frames) {
  		if (nla_put_u32(skb, CGW_DELETED, gwj->deleted_frames) < 0)
  			goto cancel;
  	}
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
506
  	/* check non default settings of attributes */
391ac1282   Oliver Hartkopp   can: gw: add a pe...
507
508
509
510
  	if (gwj->limit_hops) {
  		if (nla_put_u8(skb, CGW_LIM_HOPS, gwj->limit_hops) < 0)
  			goto cancel;
  	}
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
511
512
513
514
515
  	if (gwj->mod.modtype.and) {
  		memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
  		mb.modtype = gwj->mod.modtype.and;
  		if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
  			goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
516
517
518
519
520
521
522
  	}
  
  	if (gwj->mod.modtype.or) {
  		memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
  		mb.modtype = gwj->mod.modtype.or;
  		if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
  			goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
523
524
525
526
527
528
529
  	}
  
  	if (gwj->mod.modtype.xor) {
  		memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
  		mb.modtype = gwj->mod.modtype.xor;
  		if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
  			goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
530
531
532
533
534
535
536
  	}
  
  	if (gwj->mod.modtype.set) {
  		memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
  		mb.modtype = gwj->mod.modtype.set;
  		if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
  			goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
537
  	}
dd895d7f2   Oliver Hartkopp   can: cangw: intro...
538
539
540
541
  	if (gwj->mod.uid) {
  		if (nla_put_u32(skb, CGW_MOD_UID, gwj->mod.uid) < 0)
  			goto cancel;
  	}
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
542
543
544
545
  	if (gwj->mod.csumfunc.crc8) {
  		if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
  			    &gwj->mod.csum.crc8) < 0)
  			goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
546
547
548
549
550
551
  	}
  
  	if (gwj->mod.csumfunc.xor) {
  		if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
  			    &gwj->mod.csum.xor) < 0)
  			goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
552
553
554
555
556
557
558
559
  	}
  
  	if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
  
  		if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
  			if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
  				    &gwj->ccgw.filter) < 0)
  				goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
560
561
562
563
  		}
  
  		if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
  			goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
564
565
566
  
  		if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
  			goto cancel;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
567
  	}
053c095a8   Johannes Berg   netlink: make nlm...
568
569
  	nlmsg_end(skb, nlh);
  	return 0;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
570
571
572
573
574
575
576
577
578
579
  
  cancel:
  	nlmsg_cancel(skb, nlh);
  	return -EMSGSIZE;
  }
  
  /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
  static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
  {
  	struct cgw_job *gwj = NULL;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
580
581
582
583
  	int idx = 0;
  	int s_idx = cb->args[0];
  
  	rcu_read_lock();
b67bfe0d4   Sasha Levin   hlist: drop the n...
584
  	hlist_for_each_entry_rcu(gwj, &cgw_list, list) {
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
585
586
  		if (idx < s_idx)
  			goto cont;
15e473046   Eric W. Biederman   netlink: Rename p...
587
  		if (cgw_put_job(skb, gwj, RTM_NEWROUTE, NETLINK_CB(cb->skb).portid,
1da0faa38   Thomas Graf   can: gw: Properly...
588
  		    cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0)
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
589
590
591
592
593
594
595
596
597
598
  			break;
  cont:
  		idx++;
  	}
  	rcu_read_unlock();
  
  	cb->args[0] = idx;
  
  	return skb->len;
  }
732d35fd0   Thomas Graf   can: gw: Use nla_...
599
600
601
602
603
604
605
606
607
608
  static const struct nla_policy cgw_policy[CGW_MAX+1] = {
  	[CGW_MOD_AND]	= { .len = sizeof(struct cgw_frame_mod) },
  	[CGW_MOD_OR]	= { .len = sizeof(struct cgw_frame_mod) },
  	[CGW_MOD_XOR]	= { .len = sizeof(struct cgw_frame_mod) },
  	[CGW_MOD_SET]	= { .len = sizeof(struct cgw_frame_mod) },
  	[CGW_CS_XOR]	= { .len = sizeof(struct cgw_csum_xor) },
  	[CGW_CS_CRC8]	= { .len = sizeof(struct cgw_csum_crc8) },
  	[CGW_SRC_IF]	= { .type = NLA_U32 },
  	[CGW_DST_IF]	= { .type = NLA_U32 },
  	[CGW_FILTER]	= { .len = sizeof(struct can_filter) },
391ac1282   Oliver Hartkopp   can: gw: add a pe...
609
  	[CGW_LIM_HOPS]	= { .type = NLA_U8 },
dd895d7f2   Oliver Hartkopp   can: cangw: intro...
610
  	[CGW_MOD_UID]	= { .type = NLA_U32 },
732d35fd0   Thomas Graf   can: gw: Use nla_...
611
  };
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
612
613
  /* check for common and gwtype specific attributes */
  static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
391ac1282   Oliver Hartkopp   can: gw: add a pe...
614
  			  u8 gwtype, void *gwtypeattr, u8 *limhops)
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
615
616
617
618
619
620
621
622
  {
  	struct nlattr *tb[CGW_MAX+1];
  	struct cgw_frame_mod mb;
  	int modidx = 0;
  	int err = 0;
  
  	/* initialize modification & checksum data space */
  	memset(mod, 0, sizeof(*mod));
732d35fd0   Thomas Graf   can: gw: Use nla_...
623
624
  	err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX,
  			  cgw_policy);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
625
626
  	if (err < 0)
  		return err;
391ac1282   Oliver Hartkopp   can: gw: add a pe...
627
628
629
630
631
632
  	if (tb[CGW_LIM_HOPS]) {
  		*limhops = nla_get_u8(tb[CGW_LIM_HOPS]);
  
  		if (*limhops < 1 || *limhops > max_hops)
  			return -EINVAL;
  	}
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
633
  	/* check for AND/OR/XOR/SET modifications */
732d35fd0   Thomas Graf   can: gw: Use nla_...
634
  	if (tb[CGW_MOD_AND]) {
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
635
636
637
638
639
640
641
642
643
644
645
646
647
648
  		nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
  
  		canframecpy(&mod->modframe.and, &mb.cf);
  		mod->modtype.and = mb.modtype;
  
  		if (mb.modtype & CGW_MOD_ID)
  			mod->modfunc[modidx++] = mod_and_id;
  
  		if (mb.modtype & CGW_MOD_DLC)
  			mod->modfunc[modidx++] = mod_and_dlc;
  
  		if (mb.modtype & CGW_MOD_DATA)
  			mod->modfunc[modidx++] = mod_and_data;
  	}
732d35fd0   Thomas Graf   can: gw: Use nla_...
649
  	if (tb[CGW_MOD_OR]) {
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
650
651
652
653
654
655
656
657
658
659
660
661
662
663
  		nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
  
  		canframecpy(&mod->modframe.or, &mb.cf);
  		mod->modtype.or = mb.modtype;
  
  		if (mb.modtype & CGW_MOD_ID)
  			mod->modfunc[modidx++] = mod_or_id;
  
  		if (mb.modtype & CGW_MOD_DLC)
  			mod->modfunc[modidx++] = mod_or_dlc;
  
  		if (mb.modtype & CGW_MOD_DATA)
  			mod->modfunc[modidx++] = mod_or_data;
  	}
732d35fd0   Thomas Graf   can: gw: Use nla_...
664
  	if (tb[CGW_MOD_XOR]) {
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
  		nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
  
  		canframecpy(&mod->modframe.xor, &mb.cf);
  		mod->modtype.xor = mb.modtype;
  
  		if (mb.modtype & CGW_MOD_ID)
  			mod->modfunc[modidx++] = mod_xor_id;
  
  		if (mb.modtype & CGW_MOD_DLC)
  			mod->modfunc[modidx++] = mod_xor_dlc;
  
  		if (mb.modtype & CGW_MOD_DATA)
  			mod->modfunc[modidx++] = mod_xor_data;
  	}
732d35fd0   Thomas Graf   can: gw: Use nla_...
679
  	if (tb[CGW_MOD_SET]) {
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
  		nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
  
  		canframecpy(&mod->modframe.set, &mb.cf);
  		mod->modtype.set = mb.modtype;
  
  		if (mb.modtype & CGW_MOD_ID)
  			mod->modfunc[modidx++] = mod_set_id;
  
  		if (mb.modtype & CGW_MOD_DLC)
  			mod->modfunc[modidx++] = mod_set_dlc;
  
  		if (mb.modtype & CGW_MOD_DATA)
  			mod->modfunc[modidx++] = mod_set_data;
  	}
  
  	/* check for checksum operations after CAN frame modifications */
  	if (modidx) {
732d35fd0   Thomas Graf   can: gw: Use nla_...
697
  		if (tb[CGW_CS_CRC8]) {
5d91efa8d   Thomas Graf   can: gw: Remove p...
698
  			struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
  
  			err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  						 c->result_idx);
  			if (err)
  				return err;
  
  			nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
  				   CGW_CS_CRC8_LEN);
  
  			/*
  			 * select dedicated processing function to reduce
  			 * runtime operations in receive hot path.
  			 */
  			if (c->from_idx < 0 || c->to_idx < 0 ||
  			    c->result_idx < 0)
  				mod->csumfunc.crc8 = cgw_csum_crc8_rel;
  			else if (c->from_idx <= c->to_idx)
  				mod->csumfunc.crc8 = cgw_csum_crc8_pos;
  			else
  				mod->csumfunc.crc8 = cgw_csum_crc8_neg;
  		}
732d35fd0   Thomas Graf   can: gw: Use nla_...
720
  		if (tb[CGW_CS_XOR]) {
5d91efa8d   Thomas Graf   can: gw: Remove p...
721
  			struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
  
  			err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  						 c->result_idx);
  			if (err)
  				return err;
  
  			nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
  				   CGW_CS_XOR_LEN);
  
  			/*
  			 * select dedicated processing function to reduce
  			 * runtime operations in receive hot path.
  			 */
  			if (c->from_idx < 0 || c->to_idx < 0 ||
  			    c->result_idx < 0)
  				mod->csumfunc.xor = cgw_csum_xor_rel;
  			else if (c->from_idx <= c->to_idx)
  				mod->csumfunc.xor = cgw_csum_xor_pos;
  			else
  				mod->csumfunc.xor = cgw_csum_xor_neg;
  		}
dd895d7f2   Oliver Hartkopp   can: cangw: intro...
743
744
745
746
  
  		if (tb[CGW_MOD_UID]) {
  			nla_memcpy(&mod->uid, tb[CGW_MOD_UID], sizeof(u32));
  		}
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
747
748
749
750
751
752
753
754
755
756
  	}
  
  	if (gwtype == CGW_TYPE_CAN_CAN) {
  
  		/* check CGW_TYPE_CAN_CAN specific attributes */
  
  		struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
  		memset(ccgw, 0, sizeof(*ccgw));
  
  		/* check for can_filter in attributes */
732d35fd0   Thomas Graf   can: gw: Use nla_...
757
  		if (tb[CGW_FILTER])
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
758
759
760
761
762
763
764
765
  			nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
  				   sizeof(struct can_filter));
  
  		err = -ENODEV;
  
  		/* specifying two interfaces is mandatory */
  		if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
  			return err;
732d35fd0   Thomas Graf   can: gw: Use nla_...
766
767
  		ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]);
  		ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
768
769
770
771
772
773
774
775
776
777
778
779
780
781
  
  		/* both indices set to 0 for flushing all routing entries */
  		if (!ccgw->src_idx && !ccgw->dst_idx)
  			return 0;
  
  		/* only one index set to 0 is an error */
  		if (!ccgw->src_idx || !ccgw->dst_idx)
  			return err;
  	}
  
  	/* add the checks for other gwtypes here */
  
  	return 0;
  }
661d2967b   Thomas Graf   rtnetlink: Remove...
782
  static int cgw_create_job(struct sk_buff *skb,  struct nlmsghdr *nlh)
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
783
784
785
  {
  	struct rtcanmsg *r;
  	struct cgw_job *gwj;
dd895d7f2   Oliver Hartkopp   can: cangw: intro...
786
787
  	struct cf_mod mod;
  	struct can_can_gw ccgw;
391ac1282   Oliver Hartkopp   can: gw: add a pe...
788
  	u8 limhops = 0;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
789
  	int err = 0;
90f62cf30   Eric W. Biederman   net: Use netlink_...
790
  	if (!netlink_capable(skb, CAP_NET_ADMIN))
dfc47ef86   Eric W. Biederman   net: Push capable...
791
  		return -EPERM;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
792
793
794
795
796
797
798
799
800
801
  	if (nlmsg_len(nlh) < sizeof(*r))
  		return -EINVAL;
  
  	r = nlmsg_data(nlh);
  	if (r->can_family != AF_CAN)
  		return -EPFNOSUPPORT;
  
  	/* so far we only support CAN -> CAN routings */
  	if (r->gwtype != CGW_TYPE_CAN_CAN)
  		return -EINVAL;
dd895d7f2   Oliver Hartkopp   can: cangw: intro...
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
  	err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
  	if (err < 0)
  		return err;
  
  	if (mod.uid) {
  
  		ASSERT_RTNL();
  
  		/* check for updating an existing job with identical uid */
  		hlist_for_each_entry(gwj, &cgw_list, list) {
  
  			if (gwj->mod.uid != mod.uid)
  				continue;
  
  			/* interfaces & filters must be identical */
  			if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
  				return -EINVAL;
  
  			/* update modifications with disabled softirq & quit */
  			local_bh_disable();
  			memcpy(&gwj->mod, &mod, sizeof(mod));
  			local_bh_enable();
  			return 0;
  		}
  	}
  
  	/* ifindex == 0 is not allowed for job creation */
  	if (!ccgw.src_idx || !ccgw.dst_idx)
  		return -ENODEV;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
831
832
833
834
835
836
  	gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
  	if (!gwj)
  		return -ENOMEM;
  
  	gwj->handled_frames = 0;
  	gwj->dropped_frames = 0;
e6afa00a1   Oliver Hartkopp   can: gw: indicate...
837
  	gwj->deleted_frames = 0;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
838
839
  	gwj->flags = r->flags;
  	gwj->gwtype = r->gwtype;
dd895d7f2   Oliver Hartkopp   can: cangw: intro...
840
  	gwj->limit_hops = limhops;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
841

dd895d7f2   Oliver Hartkopp   can: cangw: intro...
842
843
844
  	/* insert already parsed information */
  	memcpy(&gwj->mod, &mod, sizeof(mod));
  	memcpy(&gwj->ccgw, &ccgw, sizeof(ccgw));
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
845
846
  
  	err = -ENODEV;
5af28de35   Ying Xue   can: use __dev_ge...
847
  	gwj->src.dev = __dev_get_by_index(&init_net, gwj->ccgw.src_idx);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
848
849
850
  
  	if (!gwj->src.dev)
  		goto out;
c0ebbdd6b   Oliver Hartkopp   can: gw: remove o...
851
  	if (gwj->src.dev->type != ARPHRD_CAN)
5af28de35   Ying Xue   can: use __dev_ge...
852
  		goto out;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
853

5af28de35   Ying Xue   can: use __dev_ge...
854
  	gwj->dst.dev = __dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
855
856
  
  	if (!gwj->dst.dev)
5af28de35   Ying Xue   can: use __dev_ge...
857
  		goto out;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
858

c0ebbdd6b   Oliver Hartkopp   can: gw: remove o...
859
  	if (gwj->dst.dev->type != ARPHRD_CAN)
5af28de35   Ying Xue   can: use __dev_ge...
860
  		goto out;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
861
862
863
864
865
866
  
  	ASSERT_RTNL();
  
  	err = cgw_register_filter(gwj);
  	if (!err)
  		hlist_add_head_rcu(&gwj->list, &cgw_list);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
867
868
869
870
871
872
873
874
875
876
  out:
  	if (err)
  		kmem_cache_free(cgw_cache, gwj);
  
  	return err;
  }
  
  static void cgw_remove_all_jobs(void)
  {
  	struct cgw_job *gwj = NULL;
b67bfe0d4   Sasha Levin   hlist: drop the n...
877
  	struct hlist_node *nx;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
878
879
  
  	ASSERT_RTNL();
b67bfe0d4   Sasha Levin   hlist: drop the n...
880
  	hlist_for_each_entry_safe(gwj, nx, &cgw_list, list) {
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
881
882
  		hlist_del(&gwj->list);
  		cgw_unregister_filter(gwj);
3480a2125   Wei Yongjun   can: gw: use kmem...
883
  		kmem_cache_free(cgw_cache, gwj);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
884
885
  	}
  }
391ac1282   Oliver Hartkopp   can: gw: add a pe...
886
  static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh)
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
887
888
  {
  	struct cgw_job *gwj = NULL;
b67bfe0d4   Sasha Levin   hlist: drop the n...
889
  	struct hlist_node *nx;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
890
891
892
  	struct rtcanmsg *r;
  	struct cf_mod mod;
  	struct can_can_gw ccgw;
391ac1282   Oliver Hartkopp   can: gw: add a pe...
893
  	u8 limhops = 0;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
894
  	int err = 0;
90f62cf30   Eric W. Biederman   net: Use netlink_...
895
  	if (!netlink_capable(skb, CAP_NET_ADMIN))
dfc47ef86   Eric W. Biederman   net: Push capable...
896
  		return -EPERM;
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
897
898
899
900
901
902
903
904
905
906
  	if (nlmsg_len(nlh) < sizeof(*r))
  		return -EINVAL;
  
  	r = nlmsg_data(nlh);
  	if (r->can_family != AF_CAN)
  		return -EPFNOSUPPORT;
  
  	/* so far we only support CAN -> CAN routings */
  	if (r->gwtype != CGW_TYPE_CAN_CAN)
  		return -EINVAL;
391ac1282   Oliver Hartkopp   can: gw: add a pe...
907
  	err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
908
909
910
911
912
913
914
915
916
917
918
919
920
921
  	if (err < 0)
  		return err;
  
  	/* two interface indices both set to 0 => remove all entries */
  	if (!ccgw.src_idx && !ccgw.dst_idx) {
  		cgw_remove_all_jobs();
  		return 0;
  	}
  
  	err = -EINVAL;
  
  	ASSERT_RTNL();
  
  	/* remove only the first matching entry */
b67bfe0d4   Sasha Levin   hlist: drop the n...
922
  	hlist_for_each_entry_safe(gwj, nx, &cgw_list, list) {
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
923
924
925
  
  		if (gwj->flags != r->flags)
  			continue;
391ac1282   Oliver Hartkopp   can: gw: add a pe...
926
927
  		if (gwj->limit_hops != limhops)
  			continue;
dd895d7f2   Oliver Hartkopp   can: cangw: intro...
928
929
930
931
932
933
934
935
936
  		/* we have a match when uid is enabled and identical */
  		if (gwj->mod.uid || mod.uid) {
  			if (gwj->mod.uid != mod.uid)
  				continue;
  		} else {
  			/* no uid => check for identical modifications */
  			if (memcmp(&gwj->mod, &mod, sizeof(mod)))
  				continue;
  		}
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
937
938
939
940
941
942
943
  
  		/* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
  		if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
  			continue;
  
  		hlist_del(&gwj->list);
  		cgw_unregister_filter(gwj);
3480a2125   Wei Yongjun   can: gw: use kmem...
944
  		kmem_cache_free(cgw_cache, gwj);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
945
946
947
948
949
950
951
952
953
  		err = 0;
  		break;
  	}
  
  	return err;
  }
  
  static __init int cgw_module_init(void)
  {
be286bafe   Oliver Hartkopp   can: gw: add a va...
954
955
956
957
958
959
  	/* sanitize given module parameter */
  	max_hops = clamp_t(unsigned int, max_hops, CGW_MIN_HOPS, CGW_MAX_HOPS);
  
  	pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d
  ",
  		max_hops);
c1aabdf37   Oliver Hartkopp   can-gw: add netli...
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  
  	cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
  				      0, 0, NULL);
  
  	if (!cgw_cache)
  		return -ENOMEM;
  
  	/* set notifier */
  	notifier.notifier_call = cgw_notifier;
  	register_netdevice_notifier(&notifier);
  
  	if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs, NULL)) {
  		unregister_netdevice_notifier(&notifier);
  		kmem_cache_destroy(cgw_cache);
  		return -ENOBUFS;
  	}
  
  	/* Only the first call to __rtnl_register can fail */
  	__rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL, NULL);
  	__rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL, NULL);
  
  	return 0;
  }
  
  static __exit void cgw_module_exit(void)
  {
  	rtnl_unregister_all(PF_CAN);
  
  	unregister_netdevice_notifier(&notifier);
  
  	rtnl_lock();
  	cgw_remove_all_jobs();
  	rtnl_unlock();
  
  	rcu_barrier(); /* Wait for completion of call_rcu()'s */
  
  	kmem_cache_destroy(cgw_cache);
  }
  
  module_init(cgw_module_init);
  module_exit(cgw_module_exit);