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