]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/gw.c
Reduce codesize by using a macro.
[socketcan-devel.git] / kernel / 2.6 / net / can / gw.c
1 /*
2  * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
3  *
4  * Copyright (c) 2002-2010 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  * Send feedback to <socketcan-users@lists.berlios.de>
41  *
42  */
43
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
47 #include <linux/types.h>
48 #include <linux/list.h>
49 #include <linux/spinlock.h>
50 #include <linux/rcupdate.h>
51 #include <linux/rculist.h>
52 #include <linux/net.h>
53 #include <linux/netdevice.h>
54 #include <linux/if_arp.h>
55 #include <linux/skbuff.h>
56 #include <socketcan/can.h>
57 #include <socketcan/can/core.h>
58 #include <socketcan/can/gw.h>
59 #include <net/rtnetlink.h>
60 #include <net/net_namespace.h>
61
62 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
63 RCSID("$Id$");
64
65 #define CAN_GW_VERSION "20100222"
66 static __initdata const char banner[] =
67         KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n";
68
69 MODULE_DESCRIPTION("PF_CAN netlink gateway");
70 MODULE_LICENSE("Dual BSD/GPL");
71 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
72
73 HLIST_HEAD(can_gw_list);
74 static DEFINE_SPINLOCK(can_gw_list_lock);
75 static struct notifier_block notifier;
76
77 static struct kmem_cache *gw_cache __read_mostly;
78
79 #define GW_SK_MAGIC ((void *)(&notifier))
80
81 /*
82  * So far we just support CAN -> CAN routing and frame modifications.
83  *
84  * The internal can_can_gw structure contains optional attributes for
85  * a CAN -> CAN gateway job.
86  */
87 struct can_can_gw {
88         struct can_filter filter;
89         struct {
90                 struct can_frame and;
91                 struct can_frame or;
92                 struct can_frame xor;
93                 struct can_frame set;
94         } modframe;
95         struct {
96                 u8 and;
97                 u8 or;
98                 u8 xor;
99                 u8 set;
100         } modtype;
101         void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
102                                           struct can_can_gw *mod);
103 };
104
105 /* list entry for CAN gateways jobs */
106 struct gw_job {
107         struct hlist_node list;
108         struct rcu_head rcu;
109         struct net_device *src_dev;
110         struct net_device *dst_dev;
111         u32 flags;
112         u32 handled_frames;
113         u32 dropped_frames;
114         union {
115                 struct can_can_gw ccgw;
116                 /* tbc */
117         };
118 };
119
120 /* content of u32 gw_job.flags */
121 #define CAN_TX_ECHO 0x00000001
122 #define CAN_TX_SRC_TSTAMP 0x00000002
123
124 /* modification functions that are invoked in the hot path in gw_rcv */
125
126 #define MODFUNC(func, op) static void func (struct can_frame *cf, \
127                                             struct can_can_gw *mod) { op ; }
128
129 MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
130 MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc)
131 MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
132 MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
133 MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
134 MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
135 MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
136 MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
137 MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
138 MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
139 MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
140 MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
141
142 static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
143 {
144         /*
145          * Copy the struct members separately to ensure that no uninitialized
146          * data are copied in the 3 bytes hole of the struct. This is needed
147          * to make easy compares of the data in the struct can_can_gw.
148          */
149
150         dst->can_id = src->can_id;
151         dst->can_dlc = src->can_dlc;
152         *(u64 *)dst->data = *(u64 *)src->data;
153 }
154
155 /* the receive & process & send function */
156 static void gw_rcv(struct sk_buff *skb, void *data)
157 {
158         struct gw_job *gwj = (struct gw_job *)data;
159         struct can_frame *cf;
160         struct sk_buff *nskb;
161         int modidx = 0;
162
163         /* do not handle already routed frames */
164         if (skb->sk == GW_SK_MAGIC)
165                 return;
166
167         if (!(gwj->dst_dev->flags & IFF_UP)) {
168                 gwj->dropped_frames++;
169                 return;
170         }
171
172         /*
173          * clone the given skb, which has not been done in can_rcv()
174          *
175          * When there is at least one modification function activated,
176          * we need to copy the skb as we want to modify skb->data.
177          */
178         if (gwj->ccgw.modfunc[0])
179                 nskb = skb_copy(skb, GFP_ATOMIC);
180         else
181                 nskb = skb_clone(skb, GFP_ATOMIC);
182
183         if (!nskb) {
184                 gwj->dropped_frames++;
185                 return;
186         }
187
188         /* mark routed frames with a 'special' sk value */
189         nskb->sk = GW_SK_MAGIC;
190         nskb->dev = gwj->dst_dev;
191
192         /* pointer to modifiable CAN frame */
193         cf = (struct can_frame *)nskb->data;
194
195         /* perform preprocessed modification functions if there are any */
196         while (modidx < MAX_MODFUNCTIONS && gwj->ccgw.modfunc[modidx])
197                 (*gwj->ccgw.modfunc[modidx++])(cf, &gwj->ccgw);
198
199         /* clear the skb timestamp if not configured the other way */
200         if (!(gwj->flags & CAN_TX_SRC_TSTAMP))
201                 nskb->tstamp.tv64 = 0;
202
203         /* send to netdevice */
204         if (can_send(nskb, gwj->flags & CAN_TX_ECHO))
205                 gwj->dropped_frames++;
206         else
207                 gwj->handled_frames++;
208 }
209
210 static inline int can_gw_register_filter(struct gw_job *gwj)
211 {
212         return can_rx_register(gwj->src_dev, gwj->ccgw.filter.can_id,
213                                gwj->ccgw.filter.can_mask, gw_rcv, gwj, "gw");
214 }
215
216 static inline void can_gw_unregister_filter(struct gw_job *gwj)
217 {
218         can_rx_unregister(gwj->src_dev, gwj->ccgw.filter.can_id,
219                           gwj->ccgw.filter.can_mask, gw_rcv, gwj);
220 }
221
222 static int gw_notifier(struct notifier_block *nb,
223                         unsigned long msg, void *data)
224 {
225         struct net_device *dev = (struct net_device *)data;
226
227 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
228         if (!net_eq(dev_net(dev), &init_net))
229                 return NOTIFY_DONE;
230 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
231         if (dev->nd_net != &init_net)
232                 return NOTIFY_DONE;
233 #endif
234         if (dev->type != ARPHRD_CAN)
235                 return NOTIFY_DONE;
236
237         if (msg == NETDEV_UNREGISTER) {
238
239                 struct gw_job *gwj = NULL;
240                 struct hlist_node *n, *nx;
241
242                 spin_lock(&can_gw_list_lock);
243
244                 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
245
246                         if (gwj->src_dev == dev || gwj->dst_dev == dev) { 
247                                 hlist_del(&gwj->list);
248                                 can_gw_unregister_filter(gwj);
249                                 kfree(gwj);
250                         }
251                 }
252
253                 spin_unlock(&can_gw_list_lock);
254         }
255
256         return NOTIFY_DONE;
257 }
258
259 static int gw_put_job(struct sk_buff *skb, struct gw_job *gwj)
260 {
261         struct {
262                 struct can_frame cf;
263                 u8 modtype;
264         } __attribute__((packed)) mb;
265
266         struct rtcanmsg *rtcan;
267         struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
268         if (!nlh)
269                 return -EMSGSIZE;
270
271         rtcan = nlmsg_data(nlh);
272         rtcan->can_family = AF_CAN;
273         rtcan->src_ifindex = gwj->src_dev->ifindex;
274         rtcan->dst_ifindex = gwj->dst_dev->ifindex;
275         rtcan->can_txflags = 0;
276
277         if (gwj->flags & CAN_TX_ECHO)
278                 rtcan->can_txflags |= CAN_GW_TXFLAGS_ECHO;
279
280         if (gwj->flags & CAN_TX_SRC_TSTAMP)
281                 rtcan->can_txflags |= CAN_GW_TXFLAGS_SRC_TSTAMP;
282
283         /* check non default settings of attributes */
284         if (gwj->handled_frames) {
285                 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
286                         goto cancel;
287                 else
288                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
289         }
290
291         if (gwj->dropped_frames) {
292                 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
293                         goto cancel;
294                 else
295                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
296         }
297
298         if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
299                 if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
300                             &gwj->ccgw.filter) < 0)
301                         goto cancel;
302                 else
303                         nlh->nlmsg_len += NLA_HDRLEN +
304                                 NLA_ALIGN(sizeof(struct can_filter));
305         }
306
307         if (gwj->ccgw.modtype.and) {
308                 memcpy(&mb.cf, &gwj->ccgw.modframe.and, sizeof(mb.cf));
309                 mb.modtype = gwj->ccgw.modtype.and;
310                 if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
311                         goto cancel;
312                 else
313                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
314         }
315
316         if (gwj->ccgw.modtype.or) {
317                 memcpy(&mb.cf, &gwj->ccgw.modframe.or, sizeof(mb.cf));
318                 mb.modtype = gwj->ccgw.modtype.or;
319                 if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
320                         goto cancel;
321                 else
322                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
323         }
324
325         if (gwj->ccgw.modtype.xor) {
326                 memcpy(&mb.cf, &gwj->ccgw.modframe.xor, sizeof(mb.cf));
327                 mb.modtype = gwj->ccgw.modtype.xor;
328                 if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
329                         goto cancel;
330                 else
331                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
332         }
333
334         if (gwj->ccgw.modtype.set) {
335                 memcpy(&mb.cf, &gwj->ccgw.modframe.set, sizeof(mb.cf));
336                 mb.modtype = gwj->ccgw.modtype.set;
337                 if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
338                         goto cancel;
339                 else
340                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
341         }
342
343         return skb->len;
344
345 cancel:
346         nlmsg_cancel(skb, nlh);
347         return -EMSGSIZE;
348 }
349
350 /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
351 static int gw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
352 {
353         struct gw_job *gwj = NULL;
354         struct hlist_node *n;
355         int idx = 0;
356         int ret = 0;
357
358         rcu_read_lock();
359         hlist_for_each_entry_rcu(gwj, n, &can_gw_list, list) {
360                 if (idx >= cb->args[0]) {
361                         ret = gw_put_job(skb, gwj);
362                         if (ret > 0)
363                                 cb->args[0]++;
364                         break;
365                 }
366                 idx++;
367         }
368         rcu_read_unlock();
369
370         return ret;
371 }
372
373 /* check for attributes / filters for the CAN->CAN gateway */
374 static int can_can_parse_attr(struct nlmsghdr *nlh, struct can_can_gw *ccgw)
375 {
376         struct nlattr *tb[CGW_MAX+1];
377         int modidx = 0;
378         int err = 0;
379
380         struct {
381                 struct can_frame cf;
382                 u8 modtype;
383         } __attribute__((packed)) mb;
384
385         BUILD_BUG_ON(sizeof(mb) != CGW_MODATTR_LEN);
386
387         memset(ccgw, 0, sizeof(*ccgw)); 
388
389         err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
390         if (err < 0)
391                 return err;
392
393         /* check for can_filter in attributes */
394         if (tb[CGW_FILTER] &&
395             nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
396                 nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
397                            sizeof(struct can_filter));
398
399         /* check for AND/OR/XOR/SET modifications */
400         if (tb[CGW_MOD_AND] &&
401             nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
402                 nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
403
404                 canframecpy(&ccgw->modframe.and, &mb.cf);
405                 ccgw->modtype.and = mb.modtype;
406
407                 if (mb.modtype & CGW_MOD_ID)
408                         ccgw->modfunc[modidx++] = mod_and_id;
409
410                 if (mb.modtype & CGW_MOD_DLC)
411                         ccgw->modfunc[modidx++] = mod_and_dlc;
412
413                 if (mb.modtype & CGW_MOD_DATA)
414                         ccgw->modfunc[modidx++] = mod_and_data;
415         }
416
417         if (tb[CGW_MOD_OR] &&
418             nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
419                 nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
420
421                 canframecpy(&ccgw->modframe.or, &mb.cf);
422                 ccgw->modtype.or = mb.modtype;
423
424                 if (mb.modtype & CGW_MOD_ID)
425                         ccgw->modfunc[modidx++] = mod_or_id;
426
427                 if (mb.modtype & CGW_MOD_DLC)
428                         ccgw->modfunc[modidx++] = mod_or_dlc;
429
430                 if (mb.modtype & CGW_MOD_DATA)
431                         ccgw->modfunc[modidx++] = mod_or_data;
432         }
433
434         if (tb[CGW_MOD_XOR] &&
435             nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
436                 nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
437
438                 canframecpy(&ccgw->modframe.xor, &mb.cf);
439                 ccgw->modtype.xor = mb.modtype;
440
441                 if (mb.modtype & CGW_MOD_ID)
442                         ccgw->modfunc[modidx++] = mod_xor_id;
443
444                 if (mb.modtype & CGW_MOD_DLC)
445                         ccgw->modfunc[modidx++] = mod_xor_dlc;
446
447                 if (mb.modtype & CGW_MOD_DATA)
448                         ccgw->modfunc[modidx++] = mod_xor_data;
449         }
450
451         if (tb[CGW_MOD_SET] &&
452             nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
453                 nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
454
455                 canframecpy(&ccgw->modframe.set, &mb.cf);
456                 ccgw->modtype.set = mb.modtype;
457
458                 if (mb.modtype & CGW_MOD_ID)
459                         ccgw->modfunc[modidx++] = mod_set_id;
460
461                 if (mb.modtype & CGW_MOD_DLC)
462                         ccgw->modfunc[modidx++] = mod_set_dlc;
463
464                 if (mb.modtype & CGW_MOD_DATA)
465                         ccgw->modfunc[modidx++] = mod_set_data;
466         }
467
468         return 0;
469 }
470
471 static int gw_create_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
472 {
473         struct rtcanmsg *r;
474         struct gw_job *gwj;
475         int err = 0;
476
477         if (nlmsg_len(nlh) < sizeof(*r))
478                 return -EINVAL;
479
480         r = nlmsg_data(nlh);
481         if (r->can_family != AF_CAN)
482                 return -EPFNOSUPPORT;
483
484         gwj = kmem_cache_alloc(gw_cache, GFP_KERNEL);
485         if (!gwj)
486                 return -ENOMEM;
487
488         gwj->src_dev = dev_get_by_index(&init_net, r->src_ifindex);
489         if (!gwj->src_dev) {
490                 err = -ENODEV;
491                 goto fail;
492         }
493
494         /* for now the source device needs to be a CAN device */
495         if (gwj->src_dev->type != ARPHRD_CAN) {
496                 err = -ENODEV;
497                 goto put_src_fail;
498         }
499
500         gwj->dst_dev = dev_get_by_index(&init_net, r->dst_ifindex);
501         if (!gwj->dst_dev) {
502                 err = -ENODEV;
503                 goto put_src_fail;
504         }
505
506         /* for now the destination device needs to be a CAN device */
507         if (gwj->dst_dev->type != ARPHRD_CAN) {
508                 err = -ENODEV;
509                 goto put_src_dst_fail;
510         }
511
512         gwj->handled_frames = 0;
513         gwj->dropped_frames = 0;
514         gwj->flags = 0;
515
516         if (r->can_txflags & CAN_GW_TXFLAGS_ECHO)
517                 gwj->flags |= CAN_TX_ECHO;
518
519         if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
520                 gwj->flags |= CAN_TX_SRC_TSTAMP;
521
522         err = can_can_parse_attr(nlh, &gwj->ccgw);
523         if (err < 0)
524                 goto put_src_dst_fail;
525
526         spin_lock(&can_gw_list_lock);
527
528         err = can_gw_register_filter(gwj);
529         if (!err)
530                 hlist_add_head_rcu(&gwj->list, &can_gw_list);
531
532         spin_unlock(&can_gw_list_lock);
533         
534         dev_put(gwj->src_dev);
535         dev_put(gwj->dst_dev);
536
537         if (err)
538                 goto fail;
539
540         return 0;
541
542 put_src_dst_fail:
543         dev_put(gwj->dst_dev);
544 put_src_fail:
545         dev_put(gwj->src_dev);
546 fail:
547         kmem_cache_free(gw_cache, gwj);
548         return err;
549 }
550
551 static void gw_remove_all_jobs(void)
552 {
553         struct gw_job *gwj = NULL;
554         struct hlist_node *n, *nx;
555
556         spin_lock(&can_gw_list_lock);
557
558         hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
559                 hlist_del(&gwj->list);
560                 can_gw_unregister_filter(gwj);
561                 kfree(gwj);
562         }
563
564         spin_unlock(&can_gw_list_lock);
565 }
566
567 static int gw_remove_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
568 {
569         struct gw_job *gwj = NULL;
570         struct hlist_node *n, *nx;
571         struct rtcanmsg *r;
572         struct can_can_gw ccgw;
573         u32 flags = 0;
574         int err = 0;
575
576         if (nlmsg_len(nlh) < sizeof(*r))
577                 return -EINVAL;
578
579         r = nlmsg_data(nlh);
580         if (r->can_family != AF_CAN)
581                 return -EPFNOSUPPORT;
582
583         /* if_index set to 0 => remove all entries */
584         if (!r->src_ifindex && !r->dst_ifindex) {
585                 gw_remove_all_jobs();
586                 return 0;
587         }
588
589         if (r->can_txflags & CAN_GW_TXFLAGS_ECHO)
590                 flags |= CAN_TX_ECHO;
591
592         if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
593                 flags |= CAN_TX_SRC_TSTAMP;
594
595         err = can_can_parse_attr(nlh, &ccgw);
596         if (err < 0)
597                 return err;
598
599         err = -EINVAL;
600
601         spin_lock(&can_gw_list_lock);
602
603         /* remove only the first matching entry */
604         hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
605
606                 if (gwj->dst_dev->ifindex != r->dst_ifindex)
607                         continue;
608
609                 if (gwj->src_dev->ifindex != r->src_ifindex)
610                         continue;
611
612                 if (gwj->flags != flags)
613                         continue;
614
615                 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
616                         continue;
617
618                 hlist_del(&gwj->list);
619                 can_gw_unregister_filter(gwj);
620                 kfree(gwj);
621                 err = 0;
622                 break;
623         }
624
625         spin_unlock(&can_gw_list_lock);
626         
627         return err;
628 }
629
630 static __init int gw_module_init(void)
631 {
632         printk(banner);
633
634         gw_cache = kmem_cache_create("can_gw", sizeof(struct gw_job),
635                                       0, 0, NULL);
636
637         if (!gw_cache)
638                 return -ENOMEM;
639
640         /* set notifier */
641         notifier.notifier_call = gw_notifier;
642         register_netdevice_notifier(&notifier);
643
644         if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, gw_dump_jobs)) {
645                 unregister_netdevice_notifier(&notifier);
646                 kmem_cache_destroy(gw_cache);
647                 return -ENOBUFS;
648         }
649
650         /* Only the first call to __rtnl_register can fail */
651         __rtnl_register(PF_CAN, RTM_NEWROUTE, gw_create_job, NULL);
652         __rtnl_register(PF_CAN, RTM_DELROUTE, gw_remove_job, NULL);
653
654         return 0;
655 }
656
657 static __exit void gw_module_exit(void)
658 {
659         rtnl_unregister_all(PF_CAN);
660
661         unregister_netdevice_notifier(&notifier);
662
663         gw_remove_all_jobs();
664
665         rcu_barrier(); /* Wait for completion of call_rcu()'s */
666
667         kmem_cache_destroy(gw_cache);
668 }
669
670 module_init(gw_module_init);
671 module_exit(gw_module_exit);