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