GNU Linux-libre 5.4.207-gnu1
[releases.git] / net / can / gw.c
1 // SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
2 /* gw.c - CAN frame Gateway/Router/Bridge with netlink interface
3  *
4  * Copyright (c) 2019 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of Volkswagen nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * Alternatively, provided that this notice is retained in full, this
20  * software may be distributed under the terms of the GNU General
21  * Public License ("GPL") version 2, in which case the provisions of the
22  * GPL apply INSTEAD OF those given above.
23  *
24  * The provided data structures and external interfaces from this code
25  * are not restricted to be used by modules with a GPL compatible license.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  *
40  */
41
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/list.h>
47 #include <linux/spinlock.h>
48 #include <linux/rcupdate.h>
49 #include <linux/rculist.h>
50 #include <linux/net.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_arp.h>
53 #include <linux/skbuff.h>
54 #include <linux/can.h>
55 #include <linux/can/core.h>
56 #include <linux/can/skb.h>
57 #include <linux/can/gw.h>
58 #include <net/rtnetlink.h>
59 #include <net/net_namespace.h>
60 #include <net/sock.h>
61
62 #define CAN_GW_VERSION "20190810"
63 #define CAN_GW_NAME "can-gw"
64
65 MODULE_DESCRIPTION("PF_CAN netlink gateway");
66 MODULE_LICENSE("Dual BSD/GPL");
67 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
68 MODULE_ALIAS(CAN_GW_NAME);
69
70 #define CGW_MIN_HOPS 1
71 #define CGW_MAX_HOPS 6
72 #define CGW_DEFAULT_HOPS 1
73
74 static unsigned int max_hops __read_mostly = CGW_DEFAULT_HOPS;
75 module_param(max_hops, uint, 0444);
76 MODULE_PARM_DESC(max_hops,
77                  "maximum " CAN_GW_NAME " routing hops for CAN frames "
78                  "(valid values: " __stringify(CGW_MIN_HOPS) "-"
79                  __stringify(CGW_MAX_HOPS) " hops, "
80                  "default: " __stringify(CGW_DEFAULT_HOPS) ")");
81
82 static struct notifier_block notifier;
83 static struct kmem_cache *cgw_cache __read_mostly;
84
85 /* structure that contains the (on-the-fly) CAN frame modifications */
86 struct cf_mod {
87         struct {
88                 struct canfd_frame and;
89                 struct canfd_frame or;
90                 struct canfd_frame xor;
91                 struct canfd_frame set;
92         } modframe;
93         struct {
94                 u8 and;
95                 u8 or;
96                 u8 xor;
97                 u8 set;
98         } modtype;
99         void (*modfunc[MAX_MODFUNCTIONS])(struct canfd_frame *cf,
100                                           struct cf_mod *mod);
101
102         /* CAN frame checksum calculation after CAN frame modifications */
103         struct {
104                 struct cgw_csum_xor xor;
105                 struct cgw_csum_crc8 crc8;
106         } csum;
107         struct {
108                 void (*xor)(struct canfd_frame *cf,
109                             struct cgw_csum_xor *xor);
110                 void (*crc8)(struct canfd_frame *cf,
111                              struct cgw_csum_crc8 *crc8);
112         } csumfunc;
113         u32 uid;
114 };
115
116 /* So far we just support CAN -> CAN routing and frame modifications.
117  *
118  * The internal can_can_gw structure contains data and attributes for
119  * a CAN -> CAN gateway job.
120  */
121 struct can_can_gw {
122         struct can_filter filter;
123         int src_idx;
124         int dst_idx;
125 };
126
127 /* list entry for CAN gateways jobs */
128 struct cgw_job {
129         struct hlist_node list;
130         struct rcu_head rcu;
131         u32 handled_frames;
132         u32 dropped_frames;
133         u32 deleted_frames;
134         struct cf_mod mod;
135         union {
136                 /* CAN frame data source */
137                 struct net_device *dev;
138         } src;
139         union {
140                 /* CAN frame data destination */
141                 struct net_device *dev;
142         } dst;
143         union {
144                 struct can_can_gw ccgw;
145                 /* tbc */
146         };
147         u8 gwtype;
148         u8 limit_hops;
149         u16 flags;
150 };
151
152 /* modification functions that are invoked in the hot path in can_can_gw_rcv */
153
154 #define MODFUNC(func, op) static void func(struct canfd_frame *cf, \
155                                            struct cf_mod *mod) { op ; }
156
157 MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
158 MODFUNC(mod_and_len, cf->len &= mod->modframe.and.len)
159 MODFUNC(mod_and_flags, cf->flags &= mod->modframe.and.flags)
160 MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
161 MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
162 MODFUNC(mod_or_len, cf->len |= mod->modframe.or.len)
163 MODFUNC(mod_or_flags, cf->flags |= mod->modframe.or.flags)
164 MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
165 MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
166 MODFUNC(mod_xor_len, cf->len ^= mod->modframe.xor.len)
167 MODFUNC(mod_xor_flags, cf->flags ^= mod->modframe.xor.flags)
168 MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
169 MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
170 MODFUNC(mod_set_len, cf->len = mod->modframe.set.len)
171 MODFUNC(mod_set_flags, cf->flags = mod->modframe.set.flags)
172 MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
173
174 static void mod_and_fddata(struct canfd_frame *cf, struct cf_mod *mod)
175 {
176         int i;
177
178         for (i = 0; i < CANFD_MAX_DLEN; i += 8)
179                 *(u64 *)(cf->data + i) &= *(u64 *)(mod->modframe.and.data + i);
180 }
181
182 static void mod_or_fddata(struct canfd_frame *cf, struct cf_mod *mod)
183 {
184         int i;
185
186         for (i = 0; i < CANFD_MAX_DLEN; i += 8)
187                 *(u64 *)(cf->data + i) |= *(u64 *)(mod->modframe.or.data + i);
188 }
189
190 static void mod_xor_fddata(struct canfd_frame *cf, struct cf_mod *mod)
191 {
192         int i;
193
194         for (i = 0; i < CANFD_MAX_DLEN; i += 8)
195                 *(u64 *)(cf->data + i) ^= *(u64 *)(mod->modframe.xor.data + i);
196 }
197
198 static void mod_set_fddata(struct canfd_frame *cf, struct cf_mod *mod)
199 {
200         memcpy(cf->data, mod->modframe.set.data, CANFD_MAX_DLEN);
201 }
202
203 static void canframecpy(struct canfd_frame *dst, struct can_frame *src)
204 {
205         /* Copy the struct members separately to ensure that no uninitialized
206          * data are copied in the 3 bytes hole of the struct. This is needed
207          * to make easy compares of the data in the struct cf_mod.
208          */
209
210         dst->can_id = src->can_id;
211         dst->len = src->can_dlc;
212         *(u64 *)dst->data = *(u64 *)src->data;
213 }
214
215 static void canfdframecpy(struct canfd_frame *dst, struct canfd_frame *src)
216 {
217         /* Copy the struct members separately to ensure that no uninitialized
218          * data are copied in the 2 bytes hole of the struct. This is needed
219          * to make easy compares of the data in the struct cf_mod.
220          */
221
222         dst->can_id = src->can_id;
223         dst->flags = src->flags;
224         dst->len = src->len;
225         memcpy(dst->data, src->data, CANFD_MAX_DLEN);
226 }
227
228 static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re, struct rtcanmsg *r)
229 {
230         s8 dlen = CAN_MAX_DLEN;
231
232         if (r->flags & CGW_FLAGS_CAN_FD)
233                 dlen = CANFD_MAX_DLEN;
234
235         /* absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
236          * relative to received dlc -1 .. -8 :
237          * e.g. for received dlc = 8
238          * -1 => index = 7 (data[7])
239          * -3 => index = 5 (data[5])
240          * -8 => index = 0 (data[0])
241          */
242
243         if (fr >= -dlen && fr < dlen &&
244             to >= -dlen && to < dlen &&
245             re >= -dlen && re < dlen)
246                 return 0;
247         else
248                 return -EINVAL;
249 }
250
251 static inline int calc_idx(int idx, int rx_len)
252 {
253         if (idx < 0)
254                 return rx_len + idx;
255         else
256                 return idx;
257 }
258
259 static void cgw_csum_xor_rel(struct canfd_frame *cf, struct cgw_csum_xor *xor)
260 {
261         int from = calc_idx(xor->from_idx, cf->len);
262         int to = calc_idx(xor->to_idx, cf->len);
263         int res = calc_idx(xor->result_idx, cf->len);
264         u8 val = xor->init_xor_val;
265         int i;
266
267         if (from < 0 || to < 0 || res < 0)
268                 return;
269
270         if (from <= to) {
271                 for (i = from; i <= to; i++)
272                         val ^= cf->data[i];
273         } else {
274                 for (i = from; i >= to; i--)
275                         val ^= cf->data[i];
276         }
277
278         cf->data[res] = val;
279 }
280
281 static void cgw_csum_xor_pos(struct canfd_frame *cf, struct cgw_csum_xor *xor)
282 {
283         u8 val = xor->init_xor_val;
284         int i;
285
286         for (i = xor->from_idx; i <= xor->to_idx; i++)
287                 val ^= cf->data[i];
288
289         cf->data[xor->result_idx] = val;
290 }
291
292 static void cgw_csum_xor_neg(struct canfd_frame *cf, struct cgw_csum_xor *xor)
293 {
294         u8 val = xor->init_xor_val;
295         int i;
296
297         for (i = xor->from_idx; i >= xor->to_idx; i--)
298                 val ^= cf->data[i];
299
300         cf->data[xor->result_idx] = val;
301 }
302
303 static void cgw_csum_crc8_rel(struct canfd_frame *cf,
304                               struct cgw_csum_crc8 *crc8)
305 {
306         int from = calc_idx(crc8->from_idx, cf->len);
307         int to = calc_idx(crc8->to_idx, cf->len);
308         int res = calc_idx(crc8->result_idx, cf->len);
309         u8 crc = crc8->init_crc_val;
310         int i;
311
312         if (from < 0 || to < 0 || res < 0)
313                 return;
314
315         if (from <= to) {
316                 for (i = crc8->from_idx; i <= crc8->to_idx; i++)
317                         crc = crc8->crctab[crc ^ cf->data[i]];
318         } else {
319                 for (i = crc8->from_idx; i >= crc8->to_idx; i--)
320                         crc = crc8->crctab[crc ^ cf->data[i]];
321         }
322
323         switch (crc8->profile) {
324         case CGW_CRC8PRF_1U8:
325                 crc = crc8->crctab[crc ^ crc8->profile_data[0]];
326                 break;
327
328         case  CGW_CRC8PRF_16U8:
329                 crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]];
330                 break;
331
332         case CGW_CRC8PRF_SFFID_XOR:
333                 crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^
334                                    (cf->can_id >> 8 & 0xFF)];
335                 break;
336         }
337
338         cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val;
339 }
340
341 static void cgw_csum_crc8_pos(struct canfd_frame *cf,
342                               struct cgw_csum_crc8 *crc8)
343 {
344         u8 crc = crc8->init_crc_val;
345         int i;
346
347         for (i = crc8->from_idx; i <= crc8->to_idx; i++)
348                 crc = crc8->crctab[crc ^ cf->data[i]];
349
350         switch (crc8->profile) {
351         case CGW_CRC8PRF_1U8:
352                 crc = crc8->crctab[crc ^ crc8->profile_data[0]];
353                 break;
354
355         case  CGW_CRC8PRF_16U8:
356                 crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]];
357                 break;
358
359         case CGW_CRC8PRF_SFFID_XOR:
360                 crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^
361                                    (cf->can_id >> 8 & 0xFF)];
362                 break;
363         }
364
365         cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val;
366 }
367
368 static void cgw_csum_crc8_neg(struct canfd_frame *cf,
369                               struct cgw_csum_crc8 *crc8)
370 {
371         u8 crc = crc8->init_crc_val;
372         int i;
373
374         for (i = crc8->from_idx; i >= crc8->to_idx; i--)
375                 crc = crc8->crctab[crc ^ cf->data[i]];
376
377         switch (crc8->profile) {
378         case CGW_CRC8PRF_1U8:
379                 crc = crc8->crctab[crc ^ crc8->profile_data[0]];
380                 break;
381
382         case  CGW_CRC8PRF_16U8:
383                 crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]];
384                 break;
385
386         case CGW_CRC8PRF_SFFID_XOR:
387                 crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^
388                                    (cf->can_id >> 8 & 0xFF)];
389                 break;
390         }
391
392         cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val;
393 }
394
395 /* the receive & process & send function */
396 static void can_can_gw_rcv(struct sk_buff *skb, void *data)
397 {
398         struct cgw_job *gwj = (struct cgw_job *)data;
399         struct canfd_frame *cf;
400         struct sk_buff *nskb;
401         int modidx = 0;
402
403         /* process strictly Classic CAN or CAN FD frames */
404         if (gwj->flags & CGW_FLAGS_CAN_FD) {
405                 if (skb->len != CANFD_MTU)
406                         return;
407         } else {
408                 if (skb->len != CAN_MTU)
409                         return;
410         }
411
412         /* Do not handle CAN frames routed more than 'max_hops' times.
413          * In general we should never catch this delimiter which is intended
414          * to cover a misconfiguration protection (e.g. circular CAN routes).
415          *
416          * The Controller Area Network controllers only accept CAN frames with
417          * correct CRCs - which are not visible in the controller registers.
418          * According to skbuff.h documentation the csum_start element for IP
419          * checksums is undefined/unused when ip_summed == CHECKSUM_UNNECESSARY.
420          * Only CAN skbs can be processed here which already have this property.
421          */
422
423 #define cgw_hops(skb) ((skb)->csum_start)
424
425         BUG_ON(skb->ip_summed != CHECKSUM_UNNECESSARY);
426
427         if (cgw_hops(skb) >= max_hops) {
428                 /* indicate deleted frames due to misconfiguration */
429                 gwj->deleted_frames++;
430                 return;
431         }
432
433         if (!(gwj->dst.dev->flags & IFF_UP)) {
434                 gwj->dropped_frames++;
435                 return;
436         }
437
438         /* is sending the skb back to the incoming interface not allowed? */
439         if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) &&
440             can_skb_prv(skb)->ifindex == gwj->dst.dev->ifindex)
441                 return;
442
443         /* clone the given skb, which has not been done in can_rcv()
444          *
445          * When there is at least one modification function activated,
446          * we need to copy the skb as we want to modify skb->data.
447          */
448         if (gwj->mod.modfunc[0])
449                 nskb = skb_copy(skb, GFP_ATOMIC);
450         else
451                 nskb = skb_clone(skb, GFP_ATOMIC);
452
453         if (!nskb) {
454                 gwj->dropped_frames++;
455                 return;
456         }
457
458         /* put the incremented hop counter in the cloned skb */
459         cgw_hops(nskb) = cgw_hops(skb) + 1;
460
461         /* first processing of this CAN frame -> adjust to private hop limit */
462         if (gwj->limit_hops && cgw_hops(nskb) == 1)
463                 cgw_hops(nskb) = max_hops - gwj->limit_hops + 1;
464
465         nskb->dev = gwj->dst.dev;
466
467         /* pointer to modifiable CAN frame */
468         cf = (struct canfd_frame *)nskb->data;
469
470         /* perform preprocessed modification functions if there are any */
471         while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
472                 (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
473
474         /* Has the CAN frame been modified? */
475         if (modidx) {
476                 /* get available space for the processed CAN frame type */
477                 int max_len = nskb->len - offsetof(struct canfd_frame, data);
478
479                 /* dlc may have changed, make sure it fits to the CAN frame */
480                 if (cf->len > max_len) {
481                         /* delete frame due to misconfiguration */
482                         gwj->deleted_frames++;
483                         kfree_skb(nskb);
484                         return;
485                 }
486
487                 /* check for checksum updates */
488                 if (gwj->mod.csumfunc.crc8)
489                         (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
490
491                 if (gwj->mod.csumfunc.xor)
492                         (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
493         }
494
495         /* clear the skb timestamp if not configured the other way */
496         if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
497                 nskb->tstamp = 0;
498
499         /* send to netdevice */
500         if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
501                 gwj->dropped_frames++;
502         else
503                 gwj->handled_frames++;
504 }
505
506 static inline int cgw_register_filter(struct net *net, struct cgw_job *gwj)
507 {
508         return can_rx_register(net, gwj->src.dev, gwj->ccgw.filter.can_id,
509                                gwj->ccgw.filter.can_mask, can_can_gw_rcv,
510                                gwj, "gw", NULL);
511 }
512
513 static inline void cgw_unregister_filter(struct net *net, struct cgw_job *gwj)
514 {
515         can_rx_unregister(net, gwj->src.dev, gwj->ccgw.filter.can_id,
516                           gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
517 }
518
519 static int cgw_notifier(struct notifier_block *nb,
520                         unsigned long msg, void *ptr)
521 {
522         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
523         struct net *net = dev_net(dev);
524
525         if (dev->type != ARPHRD_CAN)
526                 return NOTIFY_DONE;
527
528         if (msg == NETDEV_UNREGISTER) {
529                 struct cgw_job *gwj = NULL;
530                 struct hlist_node *nx;
531
532                 ASSERT_RTNL();
533
534                 hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
535                         if (gwj->src.dev == dev || gwj->dst.dev == dev) {
536                                 hlist_del(&gwj->list);
537                                 cgw_unregister_filter(net, gwj);
538                                 synchronize_rcu();
539                                 kmem_cache_free(cgw_cache, gwj);
540                         }
541                 }
542         }
543
544         return NOTIFY_DONE;
545 }
546
547 static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
548                        u32 pid, u32 seq, int flags)
549 {
550         struct rtcanmsg *rtcan;
551         struct nlmsghdr *nlh;
552
553         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags);
554         if (!nlh)
555                 return -EMSGSIZE;
556
557         rtcan = nlmsg_data(nlh);
558         rtcan->can_family = AF_CAN;
559         rtcan->gwtype = gwj->gwtype;
560         rtcan->flags = gwj->flags;
561
562         /* add statistics if available */
563
564         if (gwj->handled_frames) {
565                 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
566                         goto cancel;
567         }
568
569         if (gwj->dropped_frames) {
570                 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
571                         goto cancel;
572         }
573
574         if (gwj->deleted_frames) {
575                 if (nla_put_u32(skb, CGW_DELETED, gwj->deleted_frames) < 0)
576                         goto cancel;
577         }
578
579         /* check non default settings of attributes */
580
581         if (gwj->limit_hops) {
582                 if (nla_put_u8(skb, CGW_LIM_HOPS, gwj->limit_hops) < 0)
583                         goto cancel;
584         }
585
586         if (gwj->flags & CGW_FLAGS_CAN_FD) {
587                 struct cgw_fdframe_mod mb;
588
589                 if (gwj->mod.modtype.and) {
590                         memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
591                         mb.modtype = gwj->mod.modtype.and;
592                         if (nla_put(skb, CGW_FDMOD_AND, sizeof(mb), &mb) < 0)
593                                 goto cancel;
594                 }
595
596                 if (gwj->mod.modtype.or) {
597                         memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
598                         mb.modtype = gwj->mod.modtype.or;
599                         if (nla_put(skb, CGW_FDMOD_OR, sizeof(mb), &mb) < 0)
600                                 goto cancel;
601                 }
602
603                 if (gwj->mod.modtype.xor) {
604                         memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
605                         mb.modtype = gwj->mod.modtype.xor;
606                         if (nla_put(skb, CGW_FDMOD_XOR, sizeof(mb), &mb) < 0)
607                                 goto cancel;
608                 }
609
610                 if (gwj->mod.modtype.set) {
611                         memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
612                         mb.modtype = gwj->mod.modtype.set;
613                         if (nla_put(skb, CGW_FDMOD_SET, sizeof(mb), &mb) < 0)
614                                 goto cancel;
615                 }
616         } else {
617                 struct cgw_frame_mod mb;
618
619                 if (gwj->mod.modtype.and) {
620                         memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
621                         mb.modtype = gwj->mod.modtype.and;
622                         if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
623                                 goto cancel;
624                 }
625
626                 if (gwj->mod.modtype.or) {
627                         memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
628                         mb.modtype = gwj->mod.modtype.or;
629                         if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
630                                 goto cancel;
631                 }
632
633                 if (gwj->mod.modtype.xor) {
634                         memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
635                         mb.modtype = gwj->mod.modtype.xor;
636                         if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
637                                 goto cancel;
638                 }
639
640                 if (gwj->mod.modtype.set) {
641                         memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
642                         mb.modtype = gwj->mod.modtype.set;
643                         if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
644                                 goto cancel;
645                 }
646         }
647
648         if (gwj->mod.uid) {
649                 if (nla_put_u32(skb, CGW_MOD_UID, gwj->mod.uid) < 0)
650                         goto cancel;
651         }
652
653         if (gwj->mod.csumfunc.crc8) {
654                 if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
655                             &gwj->mod.csum.crc8) < 0)
656                         goto cancel;
657         }
658
659         if (gwj->mod.csumfunc.xor) {
660                 if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
661                             &gwj->mod.csum.xor) < 0)
662                         goto cancel;
663         }
664
665         if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
666                 if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
667                         if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
668                                     &gwj->ccgw.filter) < 0)
669                                 goto cancel;
670                 }
671
672                 if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
673                         goto cancel;
674
675                 if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
676                         goto cancel;
677         }
678
679         nlmsg_end(skb, nlh);
680         return 0;
681
682 cancel:
683         nlmsg_cancel(skb, nlh);
684         return -EMSGSIZE;
685 }
686
687 /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
688 static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
689 {
690         struct net *net = sock_net(skb->sk);
691         struct cgw_job *gwj = NULL;
692         int idx = 0;
693         int s_idx = cb->args[0];
694
695         rcu_read_lock();
696         hlist_for_each_entry_rcu(gwj, &net->can.cgw_list, list) {
697                 if (idx < s_idx)
698                         goto cont;
699
700                 if (cgw_put_job(skb, gwj, RTM_NEWROUTE,
701                                 NETLINK_CB(cb->skb).portid,
702                                 cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0)
703                         break;
704 cont:
705                 idx++;
706         }
707         rcu_read_unlock();
708
709         cb->args[0] = idx;
710
711         return skb->len;
712 }
713
714 static const struct nla_policy cgw_policy[CGW_MAX + 1] = {
715         [CGW_MOD_AND]   = { .len = sizeof(struct cgw_frame_mod) },
716         [CGW_MOD_OR]    = { .len = sizeof(struct cgw_frame_mod) },
717         [CGW_MOD_XOR]   = { .len = sizeof(struct cgw_frame_mod) },
718         [CGW_MOD_SET]   = { .len = sizeof(struct cgw_frame_mod) },
719         [CGW_CS_XOR]    = { .len = sizeof(struct cgw_csum_xor) },
720         [CGW_CS_CRC8]   = { .len = sizeof(struct cgw_csum_crc8) },
721         [CGW_SRC_IF]    = { .type = NLA_U32 },
722         [CGW_DST_IF]    = { .type = NLA_U32 },
723         [CGW_FILTER]    = { .len = sizeof(struct can_filter) },
724         [CGW_LIM_HOPS]  = { .type = NLA_U8 },
725         [CGW_MOD_UID]   = { .type = NLA_U32 },
726         [CGW_FDMOD_AND] = { .len = sizeof(struct cgw_fdframe_mod) },
727         [CGW_FDMOD_OR]  = { .len = sizeof(struct cgw_fdframe_mod) },
728         [CGW_FDMOD_XOR] = { .len = sizeof(struct cgw_fdframe_mod) },
729         [CGW_FDMOD_SET] = { .len = sizeof(struct cgw_fdframe_mod) },
730 };
731
732 /* check for common and gwtype specific attributes */
733 static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
734                           u8 gwtype, void *gwtypeattr, u8 *limhops)
735 {
736         struct nlattr *tb[CGW_MAX + 1];
737         struct rtcanmsg *r = nlmsg_data(nlh);
738         int modidx = 0;
739         int err = 0;
740
741         /* initialize modification & checksum data space */
742         memset(mod, 0, sizeof(*mod));
743
744         err = nlmsg_parse_deprecated(nlh, sizeof(struct rtcanmsg), tb,
745                                      CGW_MAX, cgw_policy, NULL);
746         if (err < 0)
747                 return err;
748
749         if (tb[CGW_LIM_HOPS]) {
750                 *limhops = nla_get_u8(tb[CGW_LIM_HOPS]);
751
752                 if (*limhops < 1 || *limhops > max_hops)
753                         return -EINVAL;
754         }
755
756         /* check for AND/OR/XOR/SET modifications */
757         if (r->flags & CGW_FLAGS_CAN_FD) {
758                 struct cgw_fdframe_mod mb;
759
760                 if (tb[CGW_FDMOD_AND]) {
761                         nla_memcpy(&mb, tb[CGW_FDMOD_AND], CGW_FDMODATTR_LEN);
762
763                         canfdframecpy(&mod->modframe.and, &mb.cf);
764                         mod->modtype.and = mb.modtype;
765
766                         if (mb.modtype & CGW_MOD_ID)
767                                 mod->modfunc[modidx++] = mod_and_id;
768
769                         if (mb.modtype & CGW_MOD_LEN)
770                                 mod->modfunc[modidx++] = mod_and_len;
771
772                         if (mb.modtype & CGW_MOD_FLAGS)
773                                 mod->modfunc[modidx++] = mod_and_flags;
774
775                         if (mb.modtype & CGW_MOD_DATA)
776                                 mod->modfunc[modidx++] = mod_and_fddata;
777                 }
778
779                 if (tb[CGW_FDMOD_OR]) {
780                         nla_memcpy(&mb, tb[CGW_FDMOD_OR], CGW_FDMODATTR_LEN);
781
782                         canfdframecpy(&mod->modframe.or, &mb.cf);
783                         mod->modtype.or = mb.modtype;
784
785                         if (mb.modtype & CGW_MOD_ID)
786                                 mod->modfunc[modidx++] = mod_or_id;
787
788                         if (mb.modtype & CGW_MOD_LEN)
789                                 mod->modfunc[modidx++] = mod_or_len;
790
791                         if (mb.modtype & CGW_MOD_FLAGS)
792                                 mod->modfunc[modidx++] = mod_or_flags;
793
794                         if (mb.modtype & CGW_MOD_DATA)
795                                 mod->modfunc[modidx++] = mod_or_fddata;
796                 }
797
798                 if (tb[CGW_FDMOD_XOR]) {
799                         nla_memcpy(&mb, tb[CGW_FDMOD_XOR], CGW_FDMODATTR_LEN);
800
801                         canfdframecpy(&mod->modframe.xor, &mb.cf);
802                         mod->modtype.xor = mb.modtype;
803
804                         if (mb.modtype & CGW_MOD_ID)
805                                 mod->modfunc[modidx++] = mod_xor_id;
806
807                         if (mb.modtype & CGW_MOD_LEN)
808                                 mod->modfunc[modidx++] = mod_xor_len;
809
810                         if (mb.modtype & CGW_MOD_FLAGS)
811                                 mod->modfunc[modidx++] = mod_xor_flags;
812
813                         if (mb.modtype & CGW_MOD_DATA)
814                                 mod->modfunc[modidx++] = mod_xor_fddata;
815                 }
816
817                 if (tb[CGW_FDMOD_SET]) {
818                         nla_memcpy(&mb, tb[CGW_FDMOD_SET], CGW_FDMODATTR_LEN);
819
820                         canfdframecpy(&mod->modframe.set, &mb.cf);
821                         mod->modtype.set = mb.modtype;
822
823                         if (mb.modtype & CGW_MOD_ID)
824                                 mod->modfunc[modidx++] = mod_set_id;
825
826                         if (mb.modtype & CGW_MOD_LEN)
827                                 mod->modfunc[modidx++] = mod_set_len;
828
829                         if (mb.modtype & CGW_MOD_FLAGS)
830                                 mod->modfunc[modidx++] = mod_set_flags;
831
832                         if (mb.modtype & CGW_MOD_DATA)
833                                 mod->modfunc[modidx++] = mod_set_fddata;
834                 }
835         } else {
836                 struct cgw_frame_mod mb;
837
838                 if (tb[CGW_MOD_AND]) {
839                         nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
840
841                         canframecpy(&mod->modframe.and, &mb.cf);
842                         mod->modtype.and = mb.modtype;
843
844                         if (mb.modtype & CGW_MOD_ID)
845                                 mod->modfunc[modidx++] = mod_and_id;
846
847                         if (mb.modtype & CGW_MOD_LEN)
848                                 mod->modfunc[modidx++] = mod_and_len;
849
850                         if (mb.modtype & CGW_MOD_DATA)
851                                 mod->modfunc[modidx++] = mod_and_data;
852                 }
853
854                 if (tb[CGW_MOD_OR]) {
855                         nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
856
857                         canframecpy(&mod->modframe.or, &mb.cf);
858                         mod->modtype.or = mb.modtype;
859
860                         if (mb.modtype & CGW_MOD_ID)
861                                 mod->modfunc[modidx++] = mod_or_id;
862
863                         if (mb.modtype & CGW_MOD_LEN)
864                                 mod->modfunc[modidx++] = mod_or_len;
865
866                         if (mb.modtype & CGW_MOD_DATA)
867                                 mod->modfunc[modidx++] = mod_or_data;
868                 }
869
870                 if (tb[CGW_MOD_XOR]) {
871                         nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
872
873                         canframecpy(&mod->modframe.xor, &mb.cf);
874                         mod->modtype.xor = mb.modtype;
875
876                         if (mb.modtype & CGW_MOD_ID)
877                                 mod->modfunc[modidx++] = mod_xor_id;
878
879                         if (mb.modtype & CGW_MOD_LEN)
880                                 mod->modfunc[modidx++] = mod_xor_len;
881
882                         if (mb.modtype & CGW_MOD_DATA)
883                                 mod->modfunc[modidx++] = mod_xor_data;
884                 }
885
886                 if (tb[CGW_MOD_SET]) {
887                         nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
888
889                         canframecpy(&mod->modframe.set, &mb.cf);
890                         mod->modtype.set = mb.modtype;
891
892                         if (mb.modtype & CGW_MOD_ID)
893                                 mod->modfunc[modidx++] = mod_set_id;
894
895                         if (mb.modtype & CGW_MOD_LEN)
896                                 mod->modfunc[modidx++] = mod_set_len;
897
898                         if (mb.modtype & CGW_MOD_DATA)
899                                 mod->modfunc[modidx++] = mod_set_data;
900                 }
901         }
902
903         /* check for checksum operations after CAN frame modifications */
904         if (modidx) {
905                 if (tb[CGW_CS_CRC8]) {
906                         struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]);
907
908                         err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
909                                                  c->result_idx, r);
910                         if (err)
911                                 return err;
912
913                         nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
914                                    CGW_CS_CRC8_LEN);
915
916                         /* select dedicated processing function to reduce
917                          * runtime operations in receive hot path.
918                          */
919                         if (c->from_idx < 0 || c->to_idx < 0 ||
920                             c->result_idx < 0)
921                                 mod->csumfunc.crc8 = cgw_csum_crc8_rel;
922                         else if (c->from_idx <= c->to_idx)
923                                 mod->csumfunc.crc8 = cgw_csum_crc8_pos;
924                         else
925                                 mod->csumfunc.crc8 = cgw_csum_crc8_neg;
926                 }
927
928                 if (tb[CGW_CS_XOR]) {
929                         struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]);
930
931                         err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
932                                                  c->result_idx, r);
933                         if (err)
934                                 return err;
935
936                         nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
937                                    CGW_CS_XOR_LEN);
938
939                         /* select dedicated processing function to reduce
940                          * runtime operations in receive hot path.
941                          */
942                         if (c->from_idx < 0 || c->to_idx < 0 ||
943                             c->result_idx < 0)
944                                 mod->csumfunc.xor = cgw_csum_xor_rel;
945                         else if (c->from_idx <= c->to_idx)
946                                 mod->csumfunc.xor = cgw_csum_xor_pos;
947                         else
948                                 mod->csumfunc.xor = cgw_csum_xor_neg;
949                 }
950
951                 if (tb[CGW_MOD_UID])
952                         nla_memcpy(&mod->uid, tb[CGW_MOD_UID], sizeof(u32));
953         }
954
955         if (gwtype == CGW_TYPE_CAN_CAN) {
956                 /* check CGW_TYPE_CAN_CAN specific attributes */
957                 struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
958
959                 memset(ccgw, 0, sizeof(*ccgw));
960
961                 /* check for can_filter in attributes */
962                 if (tb[CGW_FILTER])
963                         nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
964                                    sizeof(struct can_filter));
965
966                 err = -ENODEV;
967
968                 /* specifying two interfaces is mandatory */
969                 if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
970                         return err;
971
972                 ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]);
973                 ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]);
974
975                 /* both indices set to 0 for flushing all routing entries */
976                 if (!ccgw->src_idx && !ccgw->dst_idx)
977                         return 0;
978
979                 /* only one index set to 0 is an error */
980                 if (!ccgw->src_idx || !ccgw->dst_idx)
981                         return err;
982         }
983
984         /* add the checks for other gwtypes here */
985
986         return 0;
987 }
988
989 static int cgw_create_job(struct sk_buff *skb,  struct nlmsghdr *nlh,
990                           struct netlink_ext_ack *extack)
991 {
992         struct net *net = sock_net(skb->sk);
993         struct rtcanmsg *r;
994         struct cgw_job *gwj;
995         struct cf_mod mod;
996         struct can_can_gw ccgw;
997         u8 limhops = 0;
998         int err = 0;
999
1000         if (!netlink_capable(skb, CAP_NET_ADMIN))
1001                 return -EPERM;
1002
1003         if (nlmsg_len(nlh) < sizeof(*r))
1004                 return -EINVAL;
1005
1006         r = nlmsg_data(nlh);
1007         if (r->can_family != AF_CAN)
1008                 return -EPFNOSUPPORT;
1009
1010         /* so far we only support CAN -> CAN routings */
1011         if (r->gwtype != CGW_TYPE_CAN_CAN)
1012                 return -EINVAL;
1013
1014         err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
1015         if (err < 0)
1016                 return err;
1017
1018         if (mod.uid) {
1019                 ASSERT_RTNL();
1020
1021                 /* check for updating an existing job with identical uid */
1022                 hlist_for_each_entry(gwj, &net->can.cgw_list, list) {
1023                         if (gwj->mod.uid != mod.uid)
1024                                 continue;
1025
1026                         /* interfaces & filters must be identical */
1027                         if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
1028                                 return -EINVAL;
1029
1030                         /* update modifications with disabled softirq & quit */
1031                         local_bh_disable();
1032                         memcpy(&gwj->mod, &mod, sizeof(mod));
1033                         local_bh_enable();
1034                         return 0;
1035                 }
1036         }
1037
1038         /* ifindex == 0 is not allowed for job creation */
1039         if (!ccgw.src_idx || !ccgw.dst_idx)
1040                 return -ENODEV;
1041
1042         gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
1043         if (!gwj)
1044                 return -ENOMEM;
1045
1046         gwj->handled_frames = 0;
1047         gwj->dropped_frames = 0;
1048         gwj->deleted_frames = 0;
1049         gwj->flags = r->flags;
1050         gwj->gwtype = r->gwtype;
1051         gwj->limit_hops = limhops;
1052
1053         /* insert already parsed information */
1054         memcpy(&gwj->mod, &mod, sizeof(mod));
1055         memcpy(&gwj->ccgw, &ccgw, sizeof(ccgw));
1056
1057         err = -ENODEV;
1058
1059         gwj->src.dev = __dev_get_by_index(net, gwj->ccgw.src_idx);
1060
1061         if (!gwj->src.dev)
1062                 goto out;
1063
1064         if (gwj->src.dev->type != ARPHRD_CAN)
1065                 goto out;
1066
1067         gwj->dst.dev = __dev_get_by_index(net, gwj->ccgw.dst_idx);
1068
1069         if (!gwj->dst.dev)
1070                 goto out;
1071
1072         if (gwj->dst.dev->type != ARPHRD_CAN)
1073                 goto out;
1074
1075         ASSERT_RTNL();
1076
1077         err = cgw_register_filter(net, gwj);
1078         if (!err)
1079                 hlist_add_head_rcu(&gwj->list, &net->can.cgw_list);
1080 out:
1081         if (err)
1082                 kmem_cache_free(cgw_cache, gwj);
1083
1084         return err;
1085 }
1086
1087 static void cgw_remove_all_jobs(struct net *net)
1088 {
1089         struct cgw_job *gwj = NULL;
1090         struct hlist_node *nx;
1091
1092         ASSERT_RTNL();
1093
1094         hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
1095                 hlist_del(&gwj->list);
1096                 cgw_unregister_filter(net, gwj);
1097                 synchronize_rcu();
1098                 kmem_cache_free(cgw_cache, gwj);
1099         }
1100 }
1101
1102 static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh,
1103                           struct netlink_ext_ack *extack)
1104 {
1105         struct net *net = sock_net(skb->sk);
1106         struct cgw_job *gwj = NULL;
1107         struct hlist_node *nx;
1108         struct rtcanmsg *r;
1109         struct cf_mod mod;
1110         struct can_can_gw ccgw;
1111         u8 limhops = 0;
1112         int err = 0;
1113
1114         if (!netlink_capable(skb, CAP_NET_ADMIN))
1115                 return -EPERM;
1116
1117         if (nlmsg_len(nlh) < sizeof(*r))
1118                 return -EINVAL;
1119
1120         r = nlmsg_data(nlh);
1121         if (r->can_family != AF_CAN)
1122                 return -EPFNOSUPPORT;
1123
1124         /* so far we only support CAN -> CAN routings */
1125         if (r->gwtype != CGW_TYPE_CAN_CAN)
1126                 return -EINVAL;
1127
1128         err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
1129         if (err < 0)
1130                 return err;
1131
1132         /* two interface indices both set to 0 => remove all entries */
1133         if (!ccgw.src_idx && !ccgw.dst_idx) {
1134                 cgw_remove_all_jobs(net);
1135                 return 0;
1136         }
1137
1138         err = -EINVAL;
1139
1140         ASSERT_RTNL();
1141
1142         /* remove only the first matching entry */
1143         hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
1144                 if (gwj->flags != r->flags)
1145                         continue;
1146
1147                 if (gwj->limit_hops != limhops)
1148                         continue;
1149
1150                 /* we have a match when uid is enabled and identical */
1151                 if (gwj->mod.uid || mod.uid) {
1152                         if (gwj->mod.uid != mod.uid)
1153                                 continue;
1154                 } else {
1155                         /* no uid => check for identical modifications */
1156                         if (memcmp(&gwj->mod, &mod, sizeof(mod)))
1157                                 continue;
1158                 }
1159
1160                 /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
1161                 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
1162                         continue;
1163
1164                 hlist_del(&gwj->list);
1165                 cgw_unregister_filter(net, gwj);
1166                 synchronize_rcu();
1167                 kmem_cache_free(cgw_cache, gwj);
1168                 err = 0;
1169                 break;
1170         }
1171
1172         return err;
1173 }
1174
1175 static int __net_init cangw_pernet_init(struct net *net)
1176 {
1177         INIT_HLIST_HEAD(&net->can.cgw_list);
1178         return 0;
1179 }
1180
1181 static void __net_exit cangw_pernet_exit(struct net *net)
1182 {
1183         rtnl_lock();
1184         cgw_remove_all_jobs(net);
1185         rtnl_unlock();
1186 }
1187
1188 static struct pernet_operations cangw_pernet_ops = {
1189         .init = cangw_pernet_init,
1190         .exit = cangw_pernet_exit,
1191 };
1192
1193 static __init int cgw_module_init(void)
1194 {
1195         int ret;
1196
1197         /* sanitize given module parameter */
1198         max_hops = clamp_t(unsigned int, max_hops, CGW_MIN_HOPS, CGW_MAX_HOPS);
1199
1200         pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
1201                 max_hops);
1202
1203         ret = register_pernet_subsys(&cangw_pernet_ops);
1204         if (ret)
1205                 return ret;
1206
1207         ret = -ENOMEM;
1208         cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
1209                                       0, 0, NULL);
1210         if (!cgw_cache)
1211                 goto out_cache_create;
1212
1213         /* set notifier */
1214         notifier.notifier_call = cgw_notifier;
1215         ret = register_netdevice_notifier(&notifier);
1216         if (ret)
1217                 goto out_register_notifier;
1218
1219         ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
1220                                    NULL, cgw_dump_jobs, 0);
1221         if (ret)
1222                 goto out_rtnl_register1;
1223
1224         ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
1225                                    cgw_create_job, NULL, 0);
1226         if (ret)
1227                 goto out_rtnl_register2;
1228         ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
1229                                    cgw_remove_job, NULL, 0);
1230         if (ret)
1231                 goto out_rtnl_register3;
1232
1233         return 0;
1234
1235 out_rtnl_register3:
1236         rtnl_unregister(PF_CAN, RTM_NEWROUTE);
1237 out_rtnl_register2:
1238         rtnl_unregister(PF_CAN, RTM_GETROUTE);
1239 out_rtnl_register1:
1240         unregister_netdevice_notifier(&notifier);
1241 out_register_notifier:
1242         kmem_cache_destroy(cgw_cache);
1243 out_cache_create:
1244         unregister_pernet_subsys(&cangw_pernet_ops);
1245
1246         return ret;
1247 }
1248
1249 static __exit void cgw_module_exit(void)
1250 {
1251         rtnl_unregister_all(PF_CAN);
1252
1253         unregister_netdevice_notifier(&notifier);
1254
1255         unregister_pernet_subsys(&cangw_pernet_ops);
1256         rcu_barrier(); /* Wait for completion of call_rcu()'s */
1257
1258         kmem_cache_destroy(cgw_cache);
1259 }
1260
1261 module_init(cgw_module_init);
1262 module_exit(cgw_module_exit);