Diff markup
1 /* 1 /*
2 * af_can.c - Protocol family CAN core module 2 * af_can.c - Protocol family CAN core module
3 * (used by different CAN protocol 3 * (used by different CAN protocol modules)
4 * 4 *
5 * Copyright (c) 2002-2007 Volkswagen Group El 5 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that t 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must reta 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must repr 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials pro 15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Volkswagen nor the n 16 * 3. Neither the name of Volkswagen nor the names of its contributors
17 * may be used to endorse or promote produc 17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permissio 18 * without specific prior written permission.
19 * 19 *
20 * Alternatively, provided that this notice is 20 * Alternatively, provided that this notice is retained in full, this
21 * software may be distributed under the terms 21 * software may be distributed under the terms of the GNU General
22 * Public License ("GPL") version 2, in which 22 * Public License ("GPL") version 2, in which case the provisions of the
23 * GPL apply INSTEAD OF those given above. 23 * GPL apply INSTEAD OF those given above.
24 * 24 *
25 * The provided data structures and external i 25 * The provided data structures and external interfaces from this code
26 * are not restricted to be used by modules wi 26 * are not restricted to be used by modules with a GPL compatible license.
27 * 27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTI 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCH 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIR 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGE 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, S 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE PO 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39 * DAMAGE. 39 * DAMAGE.
40 * 40 *
41 * Send feedback to <socketcan-users@lists.ber 41 * Send feedback to <socketcan-users@lists.berlios.de>
42 * 42 *
43 */ 43 */
44 44
45 #include <linux/module.h> 45 #include <linux/module.h>
46 #include <linux/init.h> 46 #include <linux/init.h>
47 #include <linux/kmod.h> 47 #include <linux/kmod.h>
48 #include <linux/slab.h> 48 #include <linux/slab.h>
49 #include <linux/list.h> 49 #include <linux/list.h>
50 #include <linux/spinlock.h> 50 #include <linux/spinlock.h>
51 #include <linux/rcupdate.h> 51 #include <linux/rcupdate.h>
52 #include <linux/uaccess.h> 52 #include <linux/uaccess.h>
53 #include <linux/net.h> 53 #include <linux/net.h>
54 #include <linux/netdevice.h> 54 #include <linux/netdevice.h>
55 #include <linux/socket.h> 55 #include <linux/socket.h>
56 #include <linux/if_ether.h> 56 #include <linux/if_ether.h>
57 #include <linux/if_arp.h> 57 #include <linux/if_arp.h>
58 #include <linux/skbuff.h> 58 #include <linux/skbuff.h>
59 #include <linux/can.h> 59 #include <linux/can.h>
60 #include <linux/can/core.h> 60 #include <linux/can/core.h>
61 #include <net/net_namespace.h> 61 #include <net/net_namespace.h>
62 #include <net/sock.h> 62 #include <net/sock.h>
63 63
64 #include "af_can.h" 64 #include "af_can.h"
65 65
66 static __initdata const char banner[] = KERN_I 66 static __initdata const char banner[] = KERN_INFO
67 "can: controller area network core (" 67 "can: controller area network core (" CAN_VERSION_STRING ")\n";
68 68
69 MODULE_DESCRIPTION("Controller Area Network PF 69 MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
70 MODULE_LICENSE("Dual BSD/GPL"); 70 MODULE_LICENSE("Dual BSD/GPL");
71 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@vo 71 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
72 "Oliver Hartkopp <oliver.hartkop 72 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
73 73
74 MODULE_ALIAS_NETPROTO(PF_CAN); 74 MODULE_ALIAS_NETPROTO(PF_CAN);
75 75
76 static int stats_timer __read_mostly = 1; 76 static int stats_timer __read_mostly = 1;
77 module_param(stats_timer, int, S_IRUGO); 77 module_param(stats_timer, int, S_IRUGO);
78 MODULE_PARM_DESC(stats_timer, "enable timer fo 78 MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
79 79
80 HLIST_HEAD(can_rx_dev_list); 80 HLIST_HEAD(can_rx_dev_list);
81 static struct dev_rcv_lists can_rx_alldev_list 81 static struct dev_rcv_lists can_rx_alldev_list;
82 static DEFINE_SPINLOCK(can_rcvlists_lock); 82 static DEFINE_SPINLOCK(can_rcvlists_lock);
83 83
84 static struct kmem_cache *rcv_cache __read_mos 84 static struct kmem_cache *rcv_cache __read_mostly;
85 85
86 /* table of registered CAN protocols */ 86 /* table of registered CAN protocols */
87 static struct can_proto *proto_tab[CAN_NPROTO] 87 static struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
88 static DEFINE_SPINLOCK(proto_tab_lock); 88 static DEFINE_SPINLOCK(proto_tab_lock);
89 89
90 struct timer_list can_stattimer; /* timer fo 90 struct timer_list can_stattimer; /* timer for statistics update */
91 struct s_stats can_stats; /* packet s 91 struct s_stats can_stats; /* packet statistics */
92 struct s_pstats can_pstats; /* receive 92 struct s_pstats can_pstats; /* receive list statistics */
93 93
94 /* 94 /*
95 * af_can socket functions 95 * af_can socket functions
96 */ 96 */
97 97
98 static int can_ioctl(struct socket *sock, unsi 98 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
99 { 99 {
100 struct sock *sk = sock->sk; 100 struct sock *sk = sock->sk;
101 101
102 switch (cmd) { 102 switch (cmd) {
103 103
104 case SIOCGSTAMP: 104 case SIOCGSTAMP:
105 return sock_get_timestamp(sk, 105 return sock_get_timestamp(sk, (struct timeval __user *)arg);
106 106
107 default: 107 default:
108 return -ENOIOCTLCMD; 108 return -ENOIOCTLCMD;
109 } 109 }
110 } 110 }
111 111
112 static void can_sock_destruct(struct sock *sk) 112 static void can_sock_destruct(struct sock *sk)
113 { 113 {
114 skb_queue_purge(&sk->sk_receive_queue) 114 skb_queue_purge(&sk->sk_receive_queue);
115 } 115 }
116 116
117 static int can_create(struct net *net, struct 117 static int can_create(struct net *net, struct socket *sock, int protocol)
118 { 118 {
119 struct sock *sk; 119 struct sock *sk;
120 struct can_proto *cp; 120 struct can_proto *cp;
121 int err = 0; 121 int err = 0;
122 122
123 sock->state = SS_UNCONNECTED; 123 sock->state = SS_UNCONNECTED;
124 124
125 if (protocol < 0 || protocol >= CAN_NP 125 if (protocol < 0 || protocol >= CAN_NPROTO)
126 return -EINVAL; 126 return -EINVAL;
127 127
128 if (net != &init_net) 128 if (net != &init_net)
129 return -EAFNOSUPPORT; 129 return -EAFNOSUPPORT;
130 130
131 #ifdef CONFIG_KMOD !! 131 #ifdef CONFIG_MODULES
132 /* try to load protocol module, when C !! 132 /* try to load protocol module kernel is modular */
133 if (!proto_tab[protocol]) { 133 if (!proto_tab[protocol]) {
134 err = request_module("can-prot 134 err = request_module("can-proto-%d", protocol);
135 135
136 /* 136 /*
137 * In case of error we only pr 137 * In case of error we only print a message but don't
138 * return the error code immed 138 * return the error code immediately. Below we will
139 * return -EPROTONOSUPPORT 139 * return -EPROTONOSUPPORT
140 */ 140 */
141 if (err && printk_ratelimit()) 141 if (err && printk_ratelimit())
142 printk(KERN_ERR "can: 142 printk(KERN_ERR "can: request_module "
143 "(can-proto-%d) 143 "(can-proto-%d) failed.\n", protocol);
144 } 144 }
145 #endif 145 #endif
146 146
147 spin_lock(&proto_tab_lock); 147 spin_lock(&proto_tab_lock);
148 cp = proto_tab[protocol]; 148 cp = proto_tab[protocol];
149 if (cp && !try_module_get(cp->prot->ow 149 if (cp && !try_module_get(cp->prot->owner))
150 cp = NULL; 150 cp = NULL;
151 spin_unlock(&proto_tab_lock); 151 spin_unlock(&proto_tab_lock);
152 152
153 /* check for available protocol and co 153 /* check for available protocol and correct usage */
154 154
155 if (!cp) 155 if (!cp)
156 return -EPROTONOSUPPORT; 156 return -EPROTONOSUPPORT;
157 157
158 if (cp->type != sock->type) { 158 if (cp->type != sock->type) {
159 err = -EPROTONOSUPPORT; 159 err = -EPROTONOSUPPORT;
160 goto errout; 160 goto errout;
161 } 161 }
162 162
163 if (cp->capability >= 0 && !capable(cp 163 if (cp->capability >= 0 && !capable(cp->capability)) {
164 err = -EPERM; 164 err = -EPERM;
165 goto errout; 165 goto errout;
166 } 166 }
167 167
168 sock->ops = cp->ops; 168 sock->ops = cp->ops;
169 169
170 sk = sk_alloc(net, PF_CAN, GFP_KERNEL, 170 sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot);
171 if (!sk) { 171 if (!sk) {
172 err = -ENOMEM; 172 err = -ENOMEM;
173 goto errout; 173 goto errout;
174 } 174 }
175 175
176 sock_init_data(sock, sk); 176 sock_init_data(sock, sk);
177 sk->sk_destruct = can_sock_destruct; 177 sk->sk_destruct = can_sock_destruct;
178 178
179 if (sk->sk_prot->init) 179 if (sk->sk_prot->init)
180 err = sk->sk_prot->init(sk); 180 err = sk->sk_prot->init(sk);
181 181
182 if (err) { 182 if (err) {
183 /* release sk on errors */ 183 /* release sk on errors */
184 sock_orphan(sk); 184 sock_orphan(sk);
185 sock_put(sk); 185 sock_put(sk);
186 } 186 }
187 187
188 errout: 188 errout:
189 module_put(cp->prot->owner); 189 module_put(cp->prot->owner);
190 return err; 190 return err;
191 } 191 }
192 192
193 /* 193 /*
194 * af_can tx path 194 * af_can tx path
195 */ 195 */
196 196
197 /** 197 /**
198 * can_send - transmit a CAN frame (optional w 198 * can_send - transmit a CAN frame (optional with local loopback)
199 * @skb: pointer to socket buffer with CAN fra 199 * @skb: pointer to socket buffer with CAN frame in data section
200 * @loop: loopback for listeners on local CAN 200 * @loop: loopback for listeners on local CAN sockets (recommended default!)
201 * 201 *
>> 202 * Due to the loopback this routine must not be called from hardirq context.
>> 203 *
202 * Return: 204 * Return:
203 * 0 on success 205 * 0 on success
204 * -ENETDOWN when the selected interface is d 206 * -ENETDOWN when the selected interface is down
205 * -ENOBUFS on full driver queue (see net_xmi 207 * -ENOBUFS on full driver queue (see net_xmit_errno())
206 * -ENOMEM when local loopback failed at call 208 * -ENOMEM when local loopback failed at calling skb_clone()
207 * -EPERM when trying to send on a non-CAN in 209 * -EPERM when trying to send on a non-CAN interface
>> 210 * -EINVAL when the skb->data does not contain a valid CAN frame
208 */ 211 */
209 int can_send(struct sk_buff *skb, int loop) 212 int can_send(struct sk_buff *skb, int loop)
210 { 213 {
>> 214 struct sk_buff *newskb = NULL;
>> 215 struct can_frame *cf = (struct can_frame *)skb->data;
211 int err; 216 int err;
212 217
>> 218 if (skb->len != sizeof(struct can_frame) || cf->can_dlc > 8) {
>> 219 kfree_skb(skb);
>> 220 return -EINVAL;
>> 221 }
>> 222
213 if (skb->dev->type != ARPHRD_CAN) { 223 if (skb->dev->type != ARPHRD_CAN) {
214 kfree_skb(skb); 224 kfree_skb(skb);
215 return -EPERM; 225 return -EPERM;
216 } 226 }
217 227
218 if (!(skb->dev->flags & IFF_UP)) { 228 if (!(skb->dev->flags & IFF_UP)) {
219 kfree_skb(skb); 229 kfree_skb(skb);
220 return -ENETDOWN; 230 return -ENETDOWN;
221 } 231 }
222 232
223 skb->protocol = htons(ETH_P_CAN); 233 skb->protocol = htons(ETH_P_CAN);
224 skb_reset_network_header(skb); 234 skb_reset_network_header(skb);
225 skb_reset_transport_header(skb); 235 skb_reset_transport_header(skb);
226 236
227 if (loop) { 237 if (loop) {
228 /* local loopback of sent CAN 238 /* local loopback of sent CAN frames */
229 239
230 /* indication for the CAN driv 240 /* indication for the CAN driver: do loopback */
231 skb->pkt_type = PACKET_LOOPBAC 241 skb->pkt_type = PACKET_LOOPBACK;
232 242
233 /* 243 /*
234 * The reference to the origin 244 * The reference to the originating sock may be required
235 * by the receiving socket to 245 * by the receiving socket to check whether the frame is
236 * its own. Example: can_raw s 246 * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
237 * Therefore we have to ensure 247 * Therefore we have to ensure that skb->sk remains the
238 * reference to the originatin 248 * reference to the originating sock by restoring skb->sk
239 * after each skb_clone() or s 249 * after each skb_clone() or skb_orphan() usage.
240 */ 250 */
241 251
242 if (!(skb->dev->flags & IFF_EC 252 if (!(skb->dev->flags & IFF_ECHO)) {
243 /* 253 /*
244 * If the interface is 254 * If the interface is not capable to do loopback
245 * itself, we do it he 255 * itself, we do it here.
246 */ 256 */
247 struct sk_buff *newskb !! 257 newskb = skb_clone(skb, GFP_ATOMIC);
248 <<
249 if (!newskb) { 258 if (!newskb) {
250 kfree_skb(skb) 259 kfree_skb(skb);
251 return -ENOMEM 260 return -ENOMEM;
252 } 261 }
253 262
254 newskb->sk = skb->sk; 263 newskb->sk = skb->sk;
255 newskb->ip_summed = CH 264 newskb->ip_summed = CHECKSUM_UNNECESSARY;
256 newskb->pkt_type = PAC 265 newskb->pkt_type = PACKET_BROADCAST;
257 netif_rx(newskb); <<
258 } 266 }
259 } else { 267 } else {
260 /* indication for the CAN driv 268 /* indication for the CAN driver: no loopback required */
261 skb->pkt_type = PACKET_HOST; 269 skb->pkt_type = PACKET_HOST;
262 } 270 }
263 271
264 /* send to netdevice */ 272 /* send to netdevice */
265 err = dev_queue_xmit(skb); 273 err = dev_queue_xmit(skb);
266 if (err > 0) 274 if (err > 0)
267 err = net_xmit_errno(err); 275 err = net_xmit_errno(err);
268 276
>> 277 if (err) {
>> 278 kfree_skb(newskb);
>> 279 return err;
>> 280 }
>> 281
>> 282 if (newskb)
>> 283 netif_rx_ni(newskb);
>> 284
269 /* update statistics */ 285 /* update statistics */
270 can_stats.tx_frames++; 286 can_stats.tx_frames++;
271 can_stats.tx_frames_delta++; 287 can_stats.tx_frames_delta++;
272 288
273 return err; !! 289 return 0;
274 } 290 }
275 EXPORT_SYMBOL(can_send); 291 EXPORT_SYMBOL(can_send);
276 292
277 /* 293 /*
278 * af_can rx path 294 * af_can rx path
279 */ 295 */
280 296
281 static struct dev_rcv_lists *find_dev_rcv_list 297 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
282 { 298 {
283 struct dev_rcv_lists *d = NULL; 299 struct dev_rcv_lists *d = NULL;
284 struct hlist_node *n; 300 struct hlist_node *n;
285 301
286 /* 302 /*
287 * find receive list for this device 303 * find receive list for this device
288 * 304 *
289 * The hlist_for_each_entry*() macros 305 * The hlist_for_each_entry*() macros curse through the list
290 * using the pointer variable n and se 306 * using the pointer variable n and set d to the containing
291 * struct in each list iteration. The 307 * struct in each list iteration. Therefore, after list
292 * iteration, d is unmodified when the 308 * iteration, d is unmodified when the list is empty, and it
293 * points to last list element, when t 309 * points to last list element, when the list is non-empty
294 * but no match in the loop body is fo 310 * but no match in the loop body is found. I.e. d is *not*
295 * NULL when no match is found. We ca 311 * NULL when no match is found. We can, however, use the
296 * cursor variable n to decide if a ma 312 * cursor variable n to decide if a match was found.
297 */ 313 */
298 314
299 hlist_for_each_entry_rcu(d, n, &can_rx 315 hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
300 if (d->dev == dev) 316 if (d->dev == dev)
301 break; 317 break;
302 } 318 }
303 319
304 return n ? d : NULL; 320 return n ? d : NULL;
305 } 321 }
306 322
>> 323 /**
>> 324 * find_rcv_list - determine optimal filterlist inside device filter struct
>> 325 * @can_id: pointer to CAN identifier of a given can_filter
>> 326 * @mask: pointer to CAN mask of a given can_filter
>> 327 * @d: pointer to the device filter struct
>> 328 *
>> 329 * Description:
>> 330 * Returns the optimal filterlist to reduce the filter handling in the
>> 331 * receive path. This function is called by service functions that need
>> 332 * to register or unregister a can_filter in the filter lists.
>> 333 *
>> 334 * A filter matches in general, when
>> 335 *
>> 336 * <received_can_id> & mask == can_id & mask
>> 337 *
>> 338 * so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
>> 339 * relevant bits for the filter.
>> 340 *
>> 341 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
>> 342 * filter for error frames (CAN_ERR_FLAG bit set in mask). For error frames
>> 343 * there is a special filterlist and a special rx path filter handling.
>> 344 *
>> 345 * Return:
>> 346 * Pointer to optimal filterlist for the given can_id/mask pair.
>> 347 * Constistency checked mask.
>> 348 * Reduced can_id to have a preprocessed filter compare value.
>> 349 */
307 static struct hlist_head *find_rcv_list(canid_ 350 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
308 struct 351 struct dev_rcv_lists *d)
309 { 352 {
310 canid_t inv = *can_id & CAN_INV_FILTER 353 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
311 354
312 /* filter error frames */ !! 355 /* filter for error frames in extra filterlist */
313 if (*mask & CAN_ERR_FLAG) { 356 if (*mask & CAN_ERR_FLAG) {
314 /* clear CAN_ERR_FLAG in list !! 357 /* clear CAN_ERR_FLAG in filter entry */
315 *mask &= CAN_ERR_MASK; 358 *mask &= CAN_ERR_MASK;
316 return &d->rx[RX_ERR]; 359 return &d->rx[RX_ERR];
317 } 360 }
318 361
319 /* ensure valid values in can_mask */ !! 362 /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
320 if (*mask & CAN_EFF_FLAG) !! 363
321 *mask &= (CAN_EFF_MASK | CAN_E !! 364 #define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
322 else !! 365
323 *mask &= (CAN_SFF_MASK | CAN_R !! 366 /* ensure valid values in can_mask for 'SFF only' frame filtering */
>> 367 if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
>> 368 *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
324 369
325 /* reduce condition testing at receive 370 /* reduce condition testing at receive time */
326 *can_id &= *mask; 371 *can_id &= *mask;
327 372
328 /* inverse can_id/can_mask filter */ 373 /* inverse can_id/can_mask filter */
329 if (inv) 374 if (inv)
330 return &d->rx[RX_INV]; 375 return &d->rx[RX_INV];
331 376
332 /* mask == 0 => no condition testing a 377 /* mask == 0 => no condition testing at receive time */
333 if (!(*mask)) 378 if (!(*mask))
334 return &d->rx[RX_ALL]; 379 return &d->rx[RX_ALL];
335 380
336 /* use extra filterset for the subscri !! 381 /* extra filterlists for the subscription of a single non-RTR can_id */
337 if (*can_id & CAN_EFF_FLAG) { !! 382 if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS)
338 if (*mask == (CAN_EFF_MASK | C !! 383 && !(*can_id & CAN_RTR_FLAG)) {
339 /* RFC: a use-case for !! 384
340 return &d->rx[RX_EFF]; !! 385 if (*can_id & CAN_EFF_FLAG) {
>> 386 if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS)) {
>> 387 /* RFC: a future use-case for hash-tables? */
>> 388 return &d->rx[RX_EFF];
>> 389 }
>> 390 } else {
>> 391 if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
>> 392 return &d->rx_sff[*can_id];
341 } 393 }
342 } else { <<
343 if (*mask == CAN_SFF_MASK) <<
344 return &d->rx_sff[*can <<
345 } 394 }
346 395
347 /* default: filter via can_id/can_mask 396 /* default: filter via can_id/can_mask */
348 return &d->rx[RX_FIL]; 397 return &d->rx[RX_FIL];
349 } 398 }
350 399
351 /** 400 /**
352 * can_rx_register - subscribe CAN frames from 401 * can_rx_register - subscribe CAN frames from a specific interface
353 * @dev: pointer to netdevice (NULL => subcrib 402 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
354 * @can_id: CAN identifier (see description) 403 * @can_id: CAN identifier (see description)
355 * @mask: CAN mask (see description) 404 * @mask: CAN mask (see description)
356 * @func: callback function on filter match 405 * @func: callback function on filter match
357 * @data: returned parameter for callback func 406 * @data: returned parameter for callback function
358 * @ident: string for calling module indentifi 407 * @ident: string for calling module indentification
359 * 408 *
360 * Description: 409 * Description:
361 * Invokes the callback function with the rec 410 * Invokes the callback function with the received sk_buff and the given
362 * parameter 'data' on a matching receive fil 411 * parameter 'data' on a matching receive filter. A filter matches, when
363 * 412 *
364 * <received_can_id> & mask == can_id 413 * <received_can_id> & mask == can_id & mask
365 * 414 *
366 * The filter can be inverted (CAN_INV_FILTER 415 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
367 * filter for error frames (CAN_ERR_FLAG bit 416 * filter for error frames (CAN_ERR_FLAG bit set in mask).
368 * 417 *
>> 418 * The provided pointer to the sk_buff is guaranteed to be valid as long as
>> 419 * the callback function is running. The callback function must *not* free
>> 420 * the given sk_buff while processing it's task. When the given sk_buff is
>> 421 * needed after the end of the callback function it must be cloned inside
>> 422 * the callback function with skb_clone().
>> 423 *
369 * Return: 424 * Return:
370 * 0 on success 425 * 0 on success
371 * -ENOMEM on missing cache mem to create sub 426 * -ENOMEM on missing cache mem to create subscription entry
372 * -ENODEV unknown device 427 * -ENODEV unknown device
373 */ 428 */
374 int can_rx_register(struct net_device *dev, ca 429 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
375 void (*func)(struct sk_buf 430 void (*func)(struct sk_buff *, void *), void *data,
376 char *ident) 431 char *ident)
377 { 432 {
378 struct receiver *r; 433 struct receiver *r;
379 struct hlist_head *rl; 434 struct hlist_head *rl;
380 struct dev_rcv_lists *d; 435 struct dev_rcv_lists *d;
381 int err = 0; 436 int err = 0;
382 437
383 /* insert new receiver (dev,canid,mas 438 /* insert new receiver (dev,canid,mask) -> (func,data) */
384 439
385 r = kmem_cache_alloc(rcv_cache, GFP_KE 440 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
386 if (!r) 441 if (!r)
387 return -ENOMEM; 442 return -ENOMEM;
388 443
389 spin_lock(&can_rcvlists_lock); 444 spin_lock(&can_rcvlists_lock);
390 445
391 d = find_dev_rcv_lists(dev); 446 d = find_dev_rcv_lists(dev);
392 if (d) { 447 if (d) {
393 rl = find_rcv_list(&can_id, &m 448 rl = find_rcv_list(&can_id, &mask, d);
394 449
395 r->can_id = can_id; 450 r->can_id = can_id;
396 r->mask = mask; 451 r->mask = mask;
397 r->matches = 0; 452 r->matches = 0;
398 r->func = func; 453 r->func = func;
399 r->data = data; 454 r->data = data;
400 r->ident = ident; 455 r->ident = ident;
401 456
402 hlist_add_head_rcu(&r->list, r 457 hlist_add_head_rcu(&r->list, rl);
403 d->entries++; 458 d->entries++;
404 459
405 can_pstats.rcv_entries++; 460 can_pstats.rcv_entries++;
406 if (can_pstats.rcv_entries_max 461 if (can_pstats.rcv_entries_max < can_pstats.rcv_entries)
407 can_pstats.rcv_entries 462 can_pstats.rcv_entries_max = can_pstats.rcv_entries;
408 } else { 463 } else {
409 kmem_cache_free(rcv_cache, r); 464 kmem_cache_free(rcv_cache, r);
410 err = -ENODEV; 465 err = -ENODEV;
411 } 466 }
412 467
413 spin_unlock(&can_rcvlists_lock); 468 spin_unlock(&can_rcvlists_lock);
414 469
415 return err; 470 return err;
416 } 471 }
417 EXPORT_SYMBOL(can_rx_register); 472 EXPORT_SYMBOL(can_rx_register);
418 473
419 /* 474 /*
420 * can_rx_delete_device - rcu callback for dev 475 * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
421 */ 476 */
422 static void can_rx_delete_device(struct rcu_he 477 static void can_rx_delete_device(struct rcu_head *rp)
423 { 478 {
424 struct dev_rcv_lists *d = container_of 479 struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
425 480
426 kfree(d); 481 kfree(d);
427 } 482 }
428 483
429 /* 484 /*
430 * can_rx_delete_receiver - rcu callback for s 485 * can_rx_delete_receiver - rcu callback for single receiver entry removal
431 */ 486 */
432 static void can_rx_delete_receiver(struct rcu_ 487 static void can_rx_delete_receiver(struct rcu_head *rp)
433 { 488 {
434 struct receiver *r = container_of(rp, 489 struct receiver *r = container_of(rp, struct receiver, rcu);
435 490
436 kmem_cache_free(rcv_cache, r); 491 kmem_cache_free(rcv_cache, r);
437 } 492 }
438 493
439 /** 494 /**
440 * can_rx_unregister - unsubscribe CAN frames 495 * can_rx_unregister - unsubscribe CAN frames from a specific interface
441 * @dev: pointer to netdevice (NULL => unsubcr 496 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
442 * @can_id: CAN identifier 497 * @can_id: CAN identifier
443 * @mask: CAN mask 498 * @mask: CAN mask
444 * @func: callback function on filter match 499 * @func: callback function on filter match
445 * @data: returned parameter for callback func 500 * @data: returned parameter for callback function
446 * 501 *
447 * Description: 502 * Description:
448 * Removes subscription entry depending on gi 503 * Removes subscription entry depending on given (subscription) values.
449 */ 504 */
450 void can_rx_unregister(struct net_device *dev, 505 void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
451 void (*func)(struct sk_ 506 void (*func)(struct sk_buff *, void *), void *data)
452 { 507 {
453 struct receiver *r = NULL; 508 struct receiver *r = NULL;
454 struct hlist_head *rl; 509 struct hlist_head *rl;
455 struct hlist_node *next; 510 struct hlist_node *next;
456 struct dev_rcv_lists *d; 511 struct dev_rcv_lists *d;
457 512
458 spin_lock(&can_rcvlists_lock); 513 spin_lock(&can_rcvlists_lock);
459 514
460 d = find_dev_rcv_lists(dev); 515 d = find_dev_rcv_lists(dev);
461 if (!d) { 516 if (!d) {
462 printk(KERN_ERR "BUG: receive 517 printk(KERN_ERR "BUG: receive list not found for "
463 "dev %s, id %03X, mask 518 "dev %s, id %03X, mask %03X\n",
464 DNAME(dev), can_id, mas 519 DNAME(dev), can_id, mask);
465 goto out; 520 goto out;
466 } 521 }
467 522
468 rl = find_rcv_list(&can_id, &mask, d); 523 rl = find_rcv_list(&can_id, &mask, d);
469 524
470 /* 525 /*
471 * Search the receiver list for the it 526 * Search the receiver list for the item to delete. This should
472 * exist, since no receiver may be unr 527 * exist, since no receiver may be unregistered that hasn't
473 * been registered before. 528 * been registered before.
474 */ 529 */
475 530
476 hlist_for_each_entry_rcu(r, next, rl, 531 hlist_for_each_entry_rcu(r, next, rl, list) {
477 if (r->can_id == can_id && r-> 532 if (r->can_id == can_id && r->mask == mask
478 && r->func == func && r->d 533 && r->func == func && r->data == data)
479 break; 534 break;
480 } 535 }
481 536
482 /* 537 /*
483 * Check for bugs in CAN protocol impl 538 * Check for bugs in CAN protocol implementations:
484 * If no matching list item was found, 539 * If no matching list item was found, the list cursor variable next
485 * will be NULL, while r will point to 540 * will be NULL, while r will point to the last item of the list.
486 */ 541 */
487 542
488 if (!next) { 543 if (!next) {
489 printk(KERN_ERR "BUG: receive 544 printk(KERN_ERR "BUG: receive list entry not found for "
490 "dev %s, id %03X, mask 545 "dev %s, id %03X, mask %03X\n",
491 DNAME(dev), can_id, mas 546 DNAME(dev), can_id, mask);
492 r = NULL; 547 r = NULL;
493 d = NULL; 548 d = NULL;
494 goto out; 549 goto out;
495 } 550 }
496 551
497 hlist_del_rcu(&r->list); 552 hlist_del_rcu(&r->list);
498 d->entries--; 553 d->entries--;
499 554
500 if (can_pstats.rcv_entries > 0) 555 if (can_pstats.rcv_entries > 0)
501 can_pstats.rcv_entries--; 556 can_pstats.rcv_entries--;
502 557
503 /* remove device structure requested b 558 /* remove device structure requested by NETDEV_UNREGISTER */
504 if (d->remove_on_zero_entries && !d->e 559 if (d->remove_on_zero_entries && !d->entries)
505 hlist_del_rcu(&d->list); 560 hlist_del_rcu(&d->list);
506 else 561 else
507 d = NULL; 562 d = NULL;
508 563
509 out: 564 out:
510 spin_unlock(&can_rcvlists_lock); 565 spin_unlock(&can_rcvlists_lock);
511 566
512 /* schedule the receiver item for dele 567 /* schedule the receiver item for deletion */
513 if (r) 568 if (r)
514 call_rcu(&r->rcu, can_rx_delet 569 call_rcu(&r->rcu, can_rx_delete_receiver);
515 570
516 /* schedule the device structure for d 571 /* schedule the device structure for deletion */
517 if (d) 572 if (d)
518 call_rcu(&d->rcu, can_rx_delet 573 call_rcu(&d->rcu, can_rx_delete_device);
519 } 574 }
520 EXPORT_SYMBOL(can_rx_unregister); 575 EXPORT_SYMBOL(can_rx_unregister);
521 576
522 static inline void deliver(struct sk_buff *skb 577 static inline void deliver(struct sk_buff *skb, struct receiver *r)
523 { 578 {
524 struct sk_buff *clone = skb_clone(skb, !! 579 r->func(skb, r->data);
525 !! 580 r->matches++;
526 if (clone) { <<
527 clone->sk = skb->sk; <<
528 r->func(clone, r->data); <<
529 r->matches++; <<
530 } <<
531 } 581 }
532 582
533 static int can_rcv_filter(struct dev_rcv_lists 583 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
534 { 584 {
535 struct receiver *r; 585 struct receiver *r;
536 struct hlist_node *n; 586 struct hlist_node *n;
537 int matches = 0; 587 int matches = 0;
538 struct can_frame *cf = (struct can_fra 588 struct can_frame *cf = (struct can_frame *)skb->data;
539 canid_t can_id = cf->can_id; 589 canid_t can_id = cf->can_id;
540 590
541 if (d->entries == 0) 591 if (d->entries == 0)
542 return 0; 592 return 0;
543 593
544 if (can_id & CAN_ERR_FLAG) { 594 if (can_id & CAN_ERR_FLAG) {
545 /* check for error frame entri 595 /* check for error frame entries only */
546 hlist_for_each_entry_rcu(r, n, 596 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
547 if (can_id & r->mask) 597 if (can_id & r->mask) {
548 deliver(skb, r 598 deliver(skb, r);
549 matches++; 599 matches++;
550 } 600 }
551 } 601 }
552 return matches; 602 return matches;
553 } 603 }
554 604
555 /* check for unfiltered entries */ 605 /* check for unfiltered entries */
556 hlist_for_each_entry_rcu(r, n, &d->rx[ 606 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
557 deliver(skb, r); 607 deliver(skb, r);
558 matches++; 608 matches++;
559 } 609 }
560 610
561 /* check for can_id/mask entries */ 611 /* check for can_id/mask entries */
562 hlist_for_each_entry_rcu(r, n, &d->rx[ 612 hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
563 if ((can_id & r->mask) == r->c 613 if ((can_id & r->mask) == r->can_id) {
564 deliver(skb, r); 614 deliver(skb, r);
565 matches++; 615 matches++;
566 } 616 }
567 } 617 }
568 618
569 /* check for inverted can_id/mask entr 619 /* check for inverted can_id/mask entries */
570 hlist_for_each_entry_rcu(r, n, &d->rx[ 620 hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
571 if ((can_id & r->mask) != r->c 621 if ((can_id & r->mask) != r->can_id) {
572 deliver(skb, r); 622 deliver(skb, r);
573 matches++; 623 matches++;
574 } 624 }
575 } 625 }
576 626
577 /* check CAN_ID specific entries */ !! 627 /* check filterlists for single non-RTR can_ids */
>> 628 if (can_id & CAN_RTR_FLAG)
>> 629 return matches;
>> 630
578 if (can_id & CAN_EFF_FLAG) { 631 if (can_id & CAN_EFF_FLAG) {
579 hlist_for_each_entry_rcu(r, n, 632 hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
580 if (r->can_id == can_i 633 if (r->can_id == can_id) {
581 deliver(skb, r 634 deliver(skb, r);
582 matches++; 635 matches++;
583 } 636 }
584 } 637 }
585 } else { 638 } else {
586 can_id &= CAN_SFF_MASK; 639 can_id &= CAN_SFF_MASK;
587 hlist_for_each_entry_rcu(r, n, 640 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
588 deliver(skb, r); 641 deliver(skb, r);
589 matches++; 642 matches++;
590 } 643 }
591 } 644 }
592 645
593 return matches; 646 return matches;
594 } 647 }
595 648
596 static int can_rcv(struct sk_buff *skb, struct 649 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
597 struct packet_type *pt, str 650 struct packet_type *pt, struct net_device *orig_dev)
598 { 651 {
599 struct dev_rcv_lists *d; 652 struct dev_rcv_lists *d;
>> 653 struct can_frame *cf = (struct can_frame *)skb->data;
600 int matches; 654 int matches;
601 655
602 if (dev->type != ARPHRD_CAN || dev->nd !! 656 if (dev->type != ARPHRD_CAN || !net_eq(dev_net(dev), &init_net)) {
603 kfree_skb(skb); 657 kfree_skb(skb);
604 return 0; 658 return 0;
605 } 659 }
606 660
>> 661 BUG_ON(skb->len != sizeof(struct can_frame) || cf->can_dlc > 8);
>> 662
607 /* update statistics */ 663 /* update statistics */
608 can_stats.rx_frames++; 664 can_stats.rx_frames++;
609 can_stats.rx_frames_delta++; 665 can_stats.rx_frames_delta++;
610 666
611 rcu_read_lock(); 667 rcu_read_lock();
612 668
613 /* deliver the packet to sockets liste 669 /* deliver the packet to sockets listening on all devices */
614 matches = can_rcv_filter(&can_rx_allde 670 matches = can_rcv_filter(&can_rx_alldev_list, skb);
615 671
616 /* find receive list for this device * 672 /* find receive list for this device */
617 d = find_dev_rcv_lists(dev); 673 d = find_dev_rcv_lists(dev);
618 if (d) 674 if (d)
619 matches += can_rcv_filter(d, s 675 matches += can_rcv_filter(d, skb);
620 676
621 rcu_read_unlock(); 677 rcu_read_unlock();
622 678
623 /* free the skbuff allocated by the ne !! 679 /* consume the skbuff allocated by the netdevice driver */
624 kfree_skb(skb); !! 680 consume_skb(skb);
625 681
626 if (matches > 0) { 682 if (matches > 0) {
627 can_stats.matches++; 683 can_stats.matches++;
628 can_stats.matches_delta++; 684 can_stats.matches_delta++;
629 } 685 }
630 686
631 return 0; 687 return 0;
632 } 688 }
633 689
634 /* 690 /*
635 * af_can protocol functions 691 * af_can protocol functions
636 */ 692 */
637 693
638 /** 694 /**
639 * can_proto_register - register CAN transport 695 * can_proto_register - register CAN transport protocol
640 * @cp: pointer to CAN protocol structure 696 * @cp: pointer to CAN protocol structure
641 * 697 *
642 * Return: 698 * Return:
643 * 0 on success 699 * 0 on success
644 * -EINVAL invalid (out of range) protocol nu 700 * -EINVAL invalid (out of range) protocol number
645 * -EBUSY protocol already in use 701 * -EBUSY protocol already in use
646 * -ENOBUF if proto_register() fails 702 * -ENOBUF if proto_register() fails
647 */ 703 */
648 int can_proto_register(struct can_proto *cp) 704 int can_proto_register(struct can_proto *cp)
649 { 705 {
650 int proto = cp->protocol; 706 int proto = cp->protocol;
651 int err = 0; 707 int err = 0;
652 708
653 if (proto < 0 || proto >= CAN_NPROTO) 709 if (proto < 0 || proto >= CAN_NPROTO) {
654 printk(KERN_ERR "can: protocol 710 printk(KERN_ERR "can: protocol number %d out of range\n",
655 proto); 711 proto);
656 return -EINVAL; 712 return -EINVAL;
657 } 713 }
658 714
659 err = proto_register(cp->prot, 0); 715 err = proto_register(cp->prot, 0);
660 if (err < 0) 716 if (err < 0)
661 return err; 717 return err;
662 718
663 spin_lock(&proto_tab_lock); 719 spin_lock(&proto_tab_lock);
664 if (proto_tab[proto]) { 720 if (proto_tab[proto]) {
665 printk(KERN_ERR "can: protocol 721 printk(KERN_ERR "can: protocol %d already registered\n",
666 proto); 722 proto);
667 err = -EBUSY; 723 err = -EBUSY;
668 } else { 724 } else {
669 proto_tab[proto] = cp; 725 proto_tab[proto] = cp;
670 726
671 /* use generic ioctl function 727 /* use generic ioctl function if not defined by module */
672 if (!cp->ops->ioctl) 728 if (!cp->ops->ioctl)
673 cp->ops->ioctl = can_i 729 cp->ops->ioctl = can_ioctl;
674 } 730 }
675 spin_unlock(&proto_tab_lock); 731 spin_unlock(&proto_tab_lock);
676 732
677 if (err < 0) 733 if (err < 0)
678 proto_unregister(cp->prot); 734 proto_unregister(cp->prot);
679 735
680 return err; 736 return err;
681 } 737 }
682 EXPORT_SYMBOL(can_proto_register); 738 EXPORT_SYMBOL(can_proto_register);
683 739
684 /** 740 /**
685 * can_proto_unregister - unregister CAN trans 741 * can_proto_unregister - unregister CAN transport protocol
686 * @cp: pointer to CAN protocol structure 742 * @cp: pointer to CAN protocol structure
687 */ 743 */
688 void can_proto_unregister(struct can_proto *cp 744 void can_proto_unregister(struct can_proto *cp)
689 { 745 {
690 int proto = cp->protocol; 746 int proto = cp->protocol;
691 747
692 spin_lock(&proto_tab_lock); 748 spin_lock(&proto_tab_lock);
693 if (!proto_tab[proto]) { 749 if (!proto_tab[proto]) {
694 printk(KERN_ERR "BUG: can: pro 750 printk(KERN_ERR "BUG: can: protocol %d is not registered\n",
695 proto); 751 proto);
696 } 752 }
697 proto_tab[proto] = NULL; 753 proto_tab[proto] = NULL;
698 spin_unlock(&proto_tab_lock); 754 spin_unlock(&proto_tab_lock);
699 755
700 proto_unregister(cp->prot); 756 proto_unregister(cp->prot);
701 } 757 }
702 EXPORT_SYMBOL(can_proto_unregister); 758 EXPORT_SYMBOL(can_proto_unregister);
703 759
704 /* 760 /*
705 * af_can notifier to create/remove CAN netdev 761 * af_can notifier to create/remove CAN netdevice specific structs
706 */ 762 */
707 static int can_notifier(struct notifier_block 763 static int can_notifier(struct notifier_block *nb, unsigned long msg,
708 void *data) 764 void *data)
709 { 765 {
710 struct net_device *dev = (struct net_d 766 struct net_device *dev = (struct net_device *)data;
711 struct dev_rcv_lists *d; 767 struct dev_rcv_lists *d;
712 768
713 if (dev->nd_net != &init_net) !! 769 if (!net_eq(dev_net(dev), &init_net))
714 return NOTIFY_DONE; 770 return NOTIFY_DONE;
715 771
716 if (dev->type != ARPHRD_CAN) 772 if (dev->type != ARPHRD_CAN)
717 return NOTIFY_DONE; 773 return NOTIFY_DONE;
718 774
719 switch (msg) { 775 switch (msg) {
720 776
721 case NETDEV_REGISTER: 777 case NETDEV_REGISTER:
722 778
723 /* 779 /*
724 * create new dev_rcv_lists fo 780 * create new dev_rcv_lists for this device
725 * 781 *
726 * N.B. zeroing the struct is 782 * N.B. zeroing the struct is the correct initialization
727 * for the embedded hlist_head 783 * for the embedded hlist_head structs.
728 * Another list type, e.g. lis 784 * Another list type, e.g. list_head, would require
729 * explicit initialization. 785 * explicit initialization.
730 */ 786 */
731 787
732 d = kzalloc(sizeof(*d), GFP_KE 788 d = kzalloc(sizeof(*d), GFP_KERNEL);
733 if (!d) { 789 if (!d) {
734 printk(KERN_ERR 790 printk(KERN_ERR
735 "can: allocatio 791 "can: allocation of receive list failed\n");
736 return NOTIFY_DONE; 792 return NOTIFY_DONE;
737 } 793 }
738 d->dev = dev; 794 d->dev = dev;
739 795
740 spin_lock(&can_rcvlists_lock); 796 spin_lock(&can_rcvlists_lock);
741 hlist_add_head_rcu(&d->list, & 797 hlist_add_head_rcu(&d->list, &can_rx_dev_list);
742 spin_unlock(&can_rcvlists_lock 798 spin_unlock(&can_rcvlists_lock);
743 799
744 break; 800 break;
745 801
746 case NETDEV_UNREGISTER: 802 case NETDEV_UNREGISTER:
747 spin_lock(&can_rcvlists_lock); 803 spin_lock(&can_rcvlists_lock);
748 804
749 d = find_dev_rcv_lists(dev); 805 d = find_dev_rcv_lists(dev);
750 if (d) { 806 if (d) {
751 if (d->entries) { 807 if (d->entries) {
752 d->remove_on_z 808 d->remove_on_zero_entries = 1;
753 d = NULL; 809 d = NULL;
754 } else 810 } else
755 hlist_del_rcu( 811 hlist_del_rcu(&d->list);
756 } else 812 } else
757 printk(KERN_ERR "can: 813 printk(KERN_ERR "can: notifier: receive list not "
758 "found for dev 814 "found for dev %s\n", dev->name);
759 815
760 spin_unlock(&can_rcvlists_lock 816 spin_unlock(&can_rcvlists_lock);
761 817
762 if (d) 818 if (d)
763 call_rcu(&d->rcu, can_ 819 call_rcu(&d->rcu, can_rx_delete_device);
764 820
765 break; 821 break;
766 } 822 }
767 823
768 return NOTIFY_DONE; 824 return NOTIFY_DONE;
769 } 825 }
770 826
771 /* 827 /*
772 * af_can module init/exit functions 828 * af_can module init/exit functions
773 */ 829 */
774 830
775 static struct packet_type can_packet __read_mo 831 static struct packet_type can_packet __read_mostly = {
776 .type = __constant_htons(ETH_P_CAN), !! 832 .type = cpu_to_be16(ETH_P_CAN),
777 .dev = NULL, 833 .dev = NULL,
778 .func = can_rcv, 834 .func = can_rcv,
779 }; 835 };
780 836
781 static struct net_proto_family can_family_ops 837 static struct net_proto_family can_family_ops __read_mostly = {
782 .family = PF_CAN, 838 .family = PF_CAN,
783 .create = can_create, 839 .create = can_create,
784 .owner = THIS_MODULE, 840 .owner = THIS_MODULE,
785 }; 841 };
786 842
787 /* notifier block for netdevice event */ 843 /* notifier block for netdevice event */
788 static struct notifier_block can_netdev_notifi 844 static struct notifier_block can_netdev_notifier __read_mostly = {
789 .notifier_call = can_notifier, 845 .notifier_call = can_notifier,
790 }; 846 };
791 847
792 static __init int can_init(void) 848 static __init int can_init(void)
793 { 849 {
794 printk(banner); 850 printk(banner);
795 851
796 rcv_cache = kmem_cache_create("can_rec 852 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
797 0, 0, NU 853 0, 0, NULL);
798 if (!rcv_cache) 854 if (!rcv_cache)
799 return -ENOMEM; 855 return -ENOMEM;
800 856
801 /* 857 /*
802 * Insert can_rx_alldev_list for recep 858 * Insert can_rx_alldev_list for reception on all devices.
803 * This struct is zero initialized whi 859 * This struct is zero initialized which is correct for the
804 * embedded hlist heads, the dev point 860 * embedded hlist heads, the dev pointer, and the entries counter.
805 */ 861 */
806 862
807 spin_lock(&can_rcvlists_lock); 863 spin_lock(&can_rcvlists_lock);
808 hlist_add_head_rcu(&can_rx_alldev_list 864 hlist_add_head_rcu(&can_rx_alldev_list.list, &can_rx_dev_list);
809 spin_unlock(&can_rcvlists_lock); 865 spin_unlock(&can_rcvlists_lock);
810 866
811 if (stats_timer) { 867 if (stats_timer) {
812 /* the statistics are updated 868 /* the statistics are updated every second (timer triggered) */
813 setup_timer(&can_stattimer, ca 869 setup_timer(&can_stattimer, can_stat_update, 0);
814 mod_timer(&can_stattimer, roun 870 mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
815 } else 871 } else
816 can_stattimer.function = NULL; 872 can_stattimer.function = NULL;
817 873
818 can_init_proc(); 874 can_init_proc();
819 875
820 /* protocol register */ 876 /* protocol register */
821 sock_register(&can_family_ops); 877 sock_register(&can_family_ops);
822 register_netdevice_notifier(&can_netde 878 register_netdevice_notifier(&can_netdev_notifier);
823 dev_add_pack(&can_packet); 879 dev_add_pack(&can_packet);
824 880
825 return 0; 881 return 0;
826 } 882 }
827 883
828 static __exit void can_exit(void) 884 static __exit void can_exit(void)
829 { 885 {
830 struct dev_rcv_lists *d; 886 struct dev_rcv_lists *d;
831 struct hlist_node *n, *next; 887 struct hlist_node *n, *next;
832 888
833 if (stats_timer) 889 if (stats_timer)
834 del_timer(&can_stattimer); 890 del_timer(&can_stattimer);
835 891
836 can_remove_proc(); 892 can_remove_proc();
837 893
838 /* protocol unregister */ 894 /* protocol unregister */
839 dev_remove_pack(&can_packet); 895 dev_remove_pack(&can_packet);
840 unregister_netdevice_notifier(&can_net 896 unregister_netdevice_notifier(&can_netdev_notifier);
841 sock_unregister(PF_CAN); 897 sock_unregister(PF_CAN);
842 898
843 /* remove can_rx_dev_list */ 899 /* remove can_rx_dev_list */
844 spin_lock(&can_rcvlists_lock); 900 spin_lock(&can_rcvlists_lock);
845 hlist_del(&can_rx_alldev_list.list); 901 hlist_del(&can_rx_alldev_list.list);
846 hlist_for_each_entry_safe(d, n, next, 902 hlist_for_each_entry_safe(d, n, next, &can_rx_dev_list, list) {
847 hlist_del(&d->list); 903 hlist_del(&d->list);
848 kfree(d); 904 kfree(d);
849 } 905 }
850 spin_unlock(&can_rcvlists_lock); 906 spin_unlock(&can_rcvlists_lock);
851 907
>> 908 rcu_barrier(); /* Wait for completion of call_rcu()'s */
>> 909
852 kmem_cache_destroy(rcv_cache); 910 kmem_cache_destroy(rcv_cache);
853 } 911 }
854 912
855 module_init(can_init); 913 module_init(can_init);
856 module_exit(can_exit); 914 module_exit(can_exit);
857 915
|
This page was automatically generated by the
LXR engine.
|