2 * Copyright (C) ST-Ericsson AB 2010
3 * Authors: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * Daniel Martensson / Daniel.Martensson@stericsson.com
5 * License terms: GNU General Public License (GPL) version 2
8 #include <linux/version.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/if_ether.h>
14 #include <linux/moduleparam.h>
16 #include <linux/sched.h>
17 #include <linux/sockios.h>
18 #include <linux/caif/if_caif.h>
19 #include <net/rtnetlink.h>
20 #include <net/caif/caif_layer.h>
21 #include <net/caif/cfcnfg.h>
22 #include <net/caif/cfpkt.h>
23 #include <net/caif/caif_dev.h>
25 #define CAIF_CONNECT_TIMEOUT 30
27 #define SIZE_MTU_MAX 4080
28 #define SIZE_MTU_MIN 68
29 #define CAIF_NET_DEFAULT_QUEUE_LEN 500
32 #define pr_debug pr_warning
34 /*This list is protected by the rtnl lock. */
35 static LIST_HEAD(chnl_net_list
);
37 MODULE_LICENSE("GPL");
38 MODULE_ALIAS_RTNL_LINK("caif");
42 struct net_device_stats stats
;
43 struct caif_connect_request conn_req
;
44 struct list_head list_field
;
45 struct net_device
*netdev
;
47 wait_queue_head_t netmgmt_wq
;
48 /* Flow status to remember and control the transmission. */
53 static void robust_list_del(struct list_head
*delete_node
)
55 struct list_head
*list_node
;
58 list_for_each_safe(list_node
, n
, &chnl_net_list
) {
59 if (list_node
== delete_node
) {
66 static int chnl_recv_cb(struct cflayer
*layr
, struct cfpkt
*pkt
)
69 struct chnl_net
*priv
= NULL
;
73 priv
= container_of(layr
, struct chnl_net
, chnl
);
78 /* Get length of CAIF packet. */
79 pktlen
= cfpkt_getlen(pkt
);
81 skb
= (struct sk_buff
*) cfpkt_tonative(pkt
);
82 /* Pass some minimum information and
83 * send the packet to the net stack.
85 skb
->dev
= priv
->netdev
;
86 skb
->protocol
= htons(ETH_P_IP
);
88 /* If we change the header in loop mode, the checksum is corrupted. */
89 if (priv
->conn_req
.protocol
== CAIFPROTO_DATAGRAM_LOOP
)
90 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
92 skb
->ip_summed
= CHECKSUM_NONE
;
94 /* FIXME: Drivers should call this in tasklet context. */
100 /* Update statistics. */
101 priv
->netdev
->stats
.rx_packets
++;
102 priv
->netdev
->stats
.rx_bytes
+= pktlen
;
107 static int delete_device(struct chnl_net
*dev
)
111 unregister_netdevice(dev
->netdev
);
115 static void close_work(struct work_struct
*work
)
117 struct chnl_net
*dev
= NULL
;
118 struct list_head
*list_node
;
119 struct list_head
*_tmp
;
121 list_for_each_safe(list_node
, _tmp
, &chnl_net_list
) {
122 dev
= list_entry(list_node
, struct chnl_net
, list_field
);
123 if (!dev
->pending_close
)
130 static DECLARE_WORK(close_worker
, close_work
);
132 static void chnl_flowctrl_cb(struct cflayer
*layr
, enum caif_ctrlcmd flow
,
135 struct chnl_net
*priv
;
136 pr_debug("CAIF: %s(): NET flowctrl func called flow: %s.\n",
138 flow
== CAIF_CTRLCMD_FLOW_ON_IND
? "ON" :
139 flow
== CAIF_CTRLCMD_INIT_RSP
? "INIT" :
140 flow
== CAIF_CTRLCMD_FLOW_OFF_IND
? "OFF" :
141 flow
== CAIF_CTRLCMD_DEINIT_RSP
? "CLOSE/DEINIT" :
142 flow
== CAIF_CTRLCMD_INIT_FAIL_RSP
? "OPEN_FAIL" :
143 flow
== CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
?
144 "REMOTE_SHUTDOWN" : "UKNOWN CTRL COMMAND");
146 priv
= container_of(layr
, struct chnl_net
, chnl
);
149 case CAIF_CTRLCMD_FLOW_OFF_IND
:
150 case CAIF_CTRLCMD_DEINIT_RSP
:
151 case CAIF_CTRLCMD_INIT_FAIL_RSP
:
152 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
153 priv
->flowenabled
= false;
154 netif_tx_disable(priv
->netdev
);
155 pr_warning("CAIF: %s(): done\n", __func__
);
156 priv
->pending_close
= 1;
157 schedule_work(&close_worker
);
159 case CAIF_CTRLCMD_FLOW_ON_IND
:
160 case CAIF_CTRLCMD_INIT_RSP
:
161 priv
->flowenabled
= true;
162 netif_wake_queue(priv
->netdev
);
163 wake_up_interruptible(&priv
->netmgmt_wq
);
170 static int chnl_net_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
172 struct chnl_net
*priv
;
173 struct cfpkt
*pkt
= NULL
;
176 /* Get our private data. */
177 priv
= netdev_priv(dev
);
179 if (skb
->len
> priv
->netdev
->mtu
) {
180 pr_warning("CAIF: %s(): Size of skb exceeded MTU\n", __func__
);
184 if (!priv
->flowenabled
) {
185 pr_debug("CAIF: %s(): dropping packets flow off\n", __func__
);
186 return NETDEV_TX_BUSY
;
189 if (priv
->conn_req
.protocol
== CAIFPROTO_DATAGRAM_LOOP
)
190 swap(ip_hdr(skb
)->saddr
, ip_hdr(skb
)->daddr
);
192 /* Store original SKB length. */
195 pkt
= cfpkt_fromnative(CAIF_DIR_OUT
, (void *) skb
);
197 pr_debug("CAIF: %s(): transmit inst %s %d,%p\n",
198 __func__
, dev
->name
, priv
->chnl
.dn
->id
, &priv
->chnl
.dn
);
200 /* Send the packet down the stack. */
201 result
= priv
->chnl
.dn
->transmit(priv
->chnl
.dn
, pkt
);
203 if (result
== -EAGAIN
)
204 result
= NETDEV_TX_BUSY
;
208 /* Update statistics. */
209 dev
->stats
.tx_packets
++;
210 dev
->stats
.tx_bytes
+= len
;
215 static int chnl_net_open(struct net_device
*dev
)
217 struct chnl_net
*priv
= NULL
;
221 priv
= netdev_priv(dev
);
222 pr_debug("CAIF: %s(): dev name: %s\n", __func__
, priv
->name
);
225 pr_debug("CAIF: %s(): chnl_net_open: no priv\n", __func__
);
228 result
= caif_connect_client(&priv
->conn_req
, &priv
->chnl
);
230 pr_debug("CAIF: %s(): err: "
231 "Unable to register and open device, Err:%d\n",
236 result
= wait_event_interruptible(priv
->netmgmt_wq
, priv
->flowenabled
);
238 if (result
== -ERESTARTSYS
) {
239 pr_debug("CAIF: %s(): wait_event_interruptible"
240 " woken by a signal\n", __func__
);
243 pr_debug("CAIF: %s(): Flow on recieved\n", __func__
);
248 static int chnl_net_stop(struct net_device
*dev
)
250 struct chnl_net
*priv
;
253 priv
= netdev_priv(dev
);
255 result
= caif_disconnect_client(&priv
->chnl
);
257 pr_debug("CAIF: %s(): chnl_net_stop: err: "
258 "Unable to STOP device, Err:%d\n",
262 result
= wait_event_interruptible(priv
->netmgmt_wq
,
265 if (result
== -ERESTARTSYS
) {
266 pr_debug("CAIF: %s(): wait_event_interruptible woken by"
267 " signal, signal_pending(current) = %d\n",
269 signal_pending(current
));
271 pr_debug("CAIF: %s(): disconnect received\n", __func__
);
278 static int chnl_net_init(struct net_device
*dev
)
280 struct chnl_net
*priv
;
282 priv
= netdev_priv(dev
);
283 strncpy(priv
->name
, dev
->name
, sizeof(priv
->name
));
287 static void chnl_net_uninit(struct net_device
*dev
)
289 struct chnl_net
*priv
;
291 priv
= netdev_priv(dev
);
292 robust_list_del(&priv
->list_field
);
295 static const struct net_device_ops netdev_ops
= {
296 .ndo_open
= chnl_net_open
,
297 .ndo_stop
= chnl_net_stop
,
298 .ndo_init
= chnl_net_init
,
299 .ndo_uninit
= chnl_net_uninit
,
300 .ndo_start_xmit
= chnl_net_start_xmit
,
303 static void ipcaif_net_setup(struct net_device
*dev
)
305 struct chnl_net
*priv
;
306 dev
->netdev_ops
= &netdev_ops
;
307 dev
->destructor
= free_netdev
;
308 dev
->flags
|= IFF_NOARP
;
309 dev
->flags
|= IFF_POINTOPOINT
;
310 dev
->needed_headroom
= CAIF_NEEDED_HEADROOM
;
311 dev
->needed_tailroom
= CAIF_NEEDED_TAILROOM
;
313 dev
->tx_queue_len
= CAIF_NET_DEFAULT_QUEUE_LEN
;
315 priv
= netdev_priv(dev
);
316 priv
->chnl
.receive
= chnl_recv_cb
;
317 priv
->chnl
.ctrlcmd
= chnl_flowctrl_cb
;
319 priv
->conn_req
.protocol
= CAIFPROTO_DATAGRAM
;
320 priv
->conn_req
.link_selector
= CAIF_LINK_HIGH_BANDW
;
321 priv
->conn_req
.priority
= CAIF_PRIO_LOW
;
322 /* Insert illegal value */
323 priv
->conn_req
.sockaddr
.u
.dgm
.connection_id
= -1;
324 priv
->flowenabled
= false;
327 init_waitqueue_head(&priv
->netmgmt_wq
);
328 list_add(&priv
->list_field
, &chnl_net_list
);
332 static int ipcaif_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
334 struct chnl_net
*priv
;
336 priv
= netdev_priv(dev
);
337 NLA_PUT_U32(skb
, IFLA_CAIF_IPV4_CONNID
,
338 priv
->conn_req
.sockaddr
.u
.dgm
.connection_id
);
339 NLA_PUT_U32(skb
, IFLA_CAIF_IPV6_CONNID
,
340 priv
->conn_req
.sockaddr
.u
.dgm
.connection_id
);
341 loop
= priv
->conn_req
.protocol
== CAIFPROTO_DATAGRAM_LOOP
;
342 NLA_PUT_U8(skb
, IFLA_CAIF_LOOPBACK
, loop
);
351 static void caif_netlink_parms(struct nlattr
*data
[],
352 struct caif_connect_request
*conn_req
)
355 pr_warning("CAIF: %s: no params data found\n", __func__
);
358 if (data
[IFLA_CAIF_IPV4_CONNID
])
359 conn_req
->sockaddr
.u
.dgm
.connection_id
=
360 nla_get_u32(data
[IFLA_CAIF_IPV4_CONNID
]);
361 if (data
[IFLA_CAIF_IPV6_CONNID
])
362 conn_req
->sockaddr
.u
.dgm
.connection_id
=
363 nla_get_u32(data
[IFLA_CAIF_IPV6_CONNID
]);
364 if (data
[IFLA_CAIF_LOOPBACK
]) {
365 if (nla_get_u8(data
[IFLA_CAIF_LOOPBACK
]))
366 conn_req
->protocol
= CAIFPROTO_DATAGRAM_LOOP
;
368 conn_req
->protocol
= CAIFPROTO_DATAGRAM
;
372 static int ipcaif_newlink(struct net
*src_net
, struct net_device
*dev
,
373 struct nlattr
*tb
[], struct nlattr
*data
[])
376 struct chnl_net
*caifdev
;
378 caifdev
= netdev_priv(dev
);
379 caif_netlink_parms(data
, &caifdev
->conn_req
);
380 ret
= register_netdevice(dev
);
382 pr_warning("CAIF: %s(): device rtml registration failed\n",
387 static int ipcaif_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
388 struct nlattr
*data
[])
390 struct chnl_net
*caifdev
;
392 caifdev
= netdev_priv(dev
);
393 caif_netlink_parms(data
, &caifdev
->conn_req
);
394 netdev_state_change(dev
);
398 static size_t ipcaif_get_size(const struct net_device
*dev
)
401 /* IFLA_CAIF_IPV4_CONNID */
403 /* IFLA_CAIF_IPV6_CONNID */
405 /* IFLA_CAIF_LOOPBACK */
410 static const struct nla_policy ipcaif_policy
[IFLA_CAIF_MAX
+ 1] = {
411 [IFLA_CAIF_IPV4_CONNID
] = { .type
= NLA_U32
},
412 [IFLA_CAIF_IPV6_CONNID
] = { .type
= NLA_U32
},
413 [IFLA_CAIF_LOOPBACK
] = { .type
= NLA_U8
}
417 static struct rtnl_link_ops ipcaif_link_ops __read_mostly
= {
419 .priv_size
= sizeof(struct chnl_net
),
420 .setup
= ipcaif_net_setup
,
421 .maxtype
= IFLA_CAIF_MAX
,
422 .policy
= ipcaif_policy
,
423 .newlink
= ipcaif_newlink
,
424 .changelink
= ipcaif_changelink
,
425 .get_size
= ipcaif_get_size
,
426 .fill_info
= ipcaif_fill_info
,
430 static int __init
chnl_init_module(void)
432 return rtnl_link_register(&ipcaif_link_ops
);
435 static void __exit
chnl_exit_module(void)
437 struct chnl_net
*dev
= NULL
;
438 struct list_head
*list_node
;
439 struct list_head
*_tmp
;
440 rtnl_link_unregister(&ipcaif_link_ops
);
442 list_for_each_safe(list_node
, _tmp
, &chnl_net_list
) {
443 dev
= list_entry(list_node
, struct chnl_net
, list_field
);
450 module_init(chnl_init_module
);
451 module_exit(chnl_exit_module
);