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 /* GPRS PDP connection has MTU to 1500 */
27 /* 5 sec. connect timeout */
28 #define CONNECT_TIMEOUT (5 * HZ)
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");
49 struct net_device_stats stats
;
50 struct caif_connect_request conn_req
;
51 struct list_head list_field
;
52 struct net_device
*netdev
;
54 wait_queue_head_t netmgmt_wq
;
55 /* Flow status to remember and control the transmission. */
57 enum caif_states state
;
60 static void robust_list_del(struct list_head
*delete_node
)
62 struct list_head
*list_node
;
65 list_for_each_safe(list_node
, n
, &chnl_net_list
) {
66 if (list_node
== delete_node
) {
74 static int chnl_recv_cb(struct cflayer
*layr
, struct cfpkt
*pkt
)
77 struct chnl_net
*priv
= container_of(layr
, struct chnl_net
, chnl
);
81 priv
= container_of(layr
, struct chnl_net
, chnl
);
86 /* Get length of CAIF packet. */
87 pktlen
= cfpkt_getlen(pkt
);
89 skb
= (struct sk_buff
*) cfpkt_tonative(pkt
);
90 /* Pass some minimum information and
91 * send the packet to the net stack.
93 skb
->dev
= priv
->netdev
;
94 skb
->protocol
= htons(ETH_P_IP
);
96 /* If we change the header in loop mode, the checksum is corrupted. */
97 if (priv
->conn_req
.protocol
== CAIFPROTO_DATAGRAM_LOOP
)
98 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
100 skb
->ip_summed
= CHECKSUM_NONE
;
107 /* Update statistics. */
108 priv
->netdev
->stats
.rx_packets
++;
109 priv
->netdev
->stats
.rx_bytes
+= pktlen
;
114 static int delete_device(struct chnl_net
*dev
)
118 unregister_netdevice(dev
->netdev
);
122 static void close_work(struct work_struct
*work
)
124 struct chnl_net
*dev
= NULL
;
125 struct list_head
*list_node
;
126 struct list_head
*_tmp
;
127 /* May be called with or without RTNL lock held */
128 int islocked
= rtnl_is_locked();
131 list_for_each_safe(list_node
, _tmp
, &chnl_net_list
) {
132 dev
= list_entry(list_node
, struct chnl_net
, list_field
);
133 if (dev
->state
== CAIF_SHUTDOWN
)
134 dev_close(dev
->netdev
);
139 static DECLARE_WORK(close_worker
, close_work
);
141 static void chnl_flowctrl_cb(struct cflayer
*layr
, enum caif_ctrlcmd flow
,
144 struct chnl_net
*priv
= container_of(layr
, struct chnl_net
, chnl
);
145 pr_debug("CAIF: %s(): NET flowctrl func called flow: %s\n",
147 flow
== CAIF_CTRLCMD_FLOW_ON_IND
? "ON" :
148 flow
== CAIF_CTRLCMD_INIT_RSP
? "INIT" :
149 flow
== CAIF_CTRLCMD_FLOW_OFF_IND
? "OFF" :
150 flow
== CAIF_CTRLCMD_DEINIT_RSP
? "CLOSE/DEINIT" :
151 flow
== CAIF_CTRLCMD_INIT_FAIL_RSP
? "OPEN_FAIL" :
152 flow
== CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
?
153 "REMOTE_SHUTDOWN" : "UKNOWN CTRL COMMAND");
158 case CAIF_CTRLCMD_FLOW_OFF_IND
:
159 priv
->flowenabled
= false;
160 netif_stop_queue(priv
->netdev
);
162 case CAIF_CTRLCMD_DEINIT_RSP
:
163 priv
->state
= CAIF_DISCONNECTED
;
165 case CAIF_CTRLCMD_INIT_FAIL_RSP
:
166 priv
->state
= CAIF_DISCONNECTED
;
167 wake_up_interruptible(&priv
->netmgmt_wq
);
169 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
170 priv
->state
= CAIF_SHUTDOWN
;
171 netif_tx_disable(priv
->netdev
);
172 schedule_work(&close_worker
);
174 case CAIF_CTRLCMD_FLOW_ON_IND
:
175 priv
->flowenabled
= true;
176 netif_wake_queue(priv
->netdev
);
178 case CAIF_CTRLCMD_INIT_RSP
:
179 priv
->state
= CAIF_CONNECTED
;
180 priv
->flowenabled
= true;
181 netif_wake_queue(priv
->netdev
);
182 wake_up_interruptible(&priv
->netmgmt_wq
);
189 static int chnl_net_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
191 struct chnl_net
*priv
;
192 struct cfpkt
*pkt
= NULL
;
195 /* Get our private data. */
196 priv
= netdev_priv(dev
);
198 if (skb
->len
> priv
->netdev
->mtu
) {
199 pr_warning("CAIF: %s(): Size of skb exceeded MTU\n", __func__
);
203 if (!priv
->flowenabled
) {
204 pr_debug("CAIF: %s(): dropping packets flow off\n", __func__
);
205 return NETDEV_TX_BUSY
;
208 if (priv
->conn_req
.protocol
== CAIFPROTO_DATAGRAM_LOOP
)
209 swap(ip_hdr(skb
)->saddr
, ip_hdr(skb
)->daddr
);
211 /* Store original SKB length. */
214 pkt
= cfpkt_fromnative(CAIF_DIR_OUT
, (void *) skb
);
216 /* Send the packet down the stack. */
217 result
= priv
->chnl
.dn
->transmit(priv
->chnl
.dn
, pkt
);
219 if (result
== -EAGAIN
)
220 result
= NETDEV_TX_BUSY
;
224 /* Update statistics. */
225 dev
->stats
.tx_packets
++;
226 dev
->stats
.tx_bytes
+= len
;
231 static int chnl_net_open(struct net_device
*dev
)
233 struct chnl_net
*priv
= NULL
;
236 priv
= netdev_priv(dev
);
238 pr_debug("CAIF: %s(): chnl_net_open: no priv\n", __func__
);
242 if (priv
->state
!= CAIF_CONNECTING
) {
243 priv
->state
= CAIF_CONNECTING
;
244 result
= caif_connect_client(&priv
->conn_req
, &priv
->chnl
);
246 priv
->state
= CAIF_DISCONNECTED
;
247 pr_debug("CAIF: %s(): err: "
248 "Unable to register and open device,"
256 result
= wait_event_interruptible_timeout(priv
->netmgmt_wq
,
257 priv
->state
!= CAIF_CONNECTING
,
260 if (result
== -ERESTARTSYS
) {
261 pr_debug("CAIF: %s(): wait_event_interruptible"
262 " woken by a signal\n", __func__
);
266 pr_debug("CAIF: %s(): connect timeout\n", __func__
);
267 caif_disconnect_client(&priv
->chnl
);
268 priv
->state
= CAIF_DISCONNECTED
;
269 pr_debug("CAIF: %s(): state disconnected\n", __func__
);
273 if (priv
->state
!= CAIF_CONNECTED
) {
274 pr_debug("CAIF: %s(): connect failed\n", __func__
);
275 return -ECONNREFUSED
;
277 pr_debug("CAIF: %s(): CAIF Netdevice connected\n", __func__
);
281 static int chnl_net_stop(struct net_device
*dev
)
283 struct chnl_net
*priv
;
286 priv
= netdev_priv(dev
);
287 priv
->state
= CAIF_DISCONNECTED
;
288 caif_disconnect_client(&priv
->chnl
);
292 static int chnl_net_init(struct net_device
*dev
)
294 struct chnl_net
*priv
;
296 priv
= netdev_priv(dev
);
297 strncpy(priv
->name
, dev
->name
, sizeof(priv
->name
));
301 static void chnl_net_uninit(struct net_device
*dev
)
303 struct chnl_net
*priv
;
305 priv
= netdev_priv(dev
);
306 robust_list_del(&priv
->list_field
);
309 static const struct net_device_ops netdev_ops
= {
310 .ndo_open
= chnl_net_open
,
311 .ndo_stop
= chnl_net_stop
,
312 .ndo_init
= chnl_net_init
,
313 .ndo_uninit
= chnl_net_uninit
,
314 .ndo_start_xmit
= chnl_net_start_xmit
,
317 static void ipcaif_net_setup(struct net_device
*dev
)
319 struct chnl_net
*priv
;
320 dev
->netdev_ops
= &netdev_ops
;
321 dev
->destructor
= free_netdev
;
322 dev
->flags
|= IFF_NOARP
;
323 dev
->flags
|= IFF_POINTOPOINT
;
324 dev
->needed_headroom
= CAIF_NEEDED_HEADROOM
;
325 dev
->needed_tailroom
= CAIF_NEEDED_TAILROOM
;
327 dev
->tx_queue_len
= CAIF_NET_DEFAULT_QUEUE_LEN
;
329 priv
= netdev_priv(dev
);
330 priv
->chnl
.receive
= chnl_recv_cb
;
331 priv
->chnl
.ctrlcmd
= chnl_flowctrl_cb
;
333 priv
->conn_req
.protocol
= CAIFPROTO_DATAGRAM
;
334 priv
->conn_req
.link_selector
= CAIF_LINK_HIGH_BANDW
;
335 priv
->conn_req
.priority
= CAIF_PRIO_LOW
;
336 /* Insert illegal value */
337 priv
->conn_req
.sockaddr
.u
.dgm
.connection_id
= -1;
338 priv
->flowenabled
= false;
341 init_waitqueue_head(&priv
->netmgmt_wq
);
342 list_add(&priv
->list_field
, &chnl_net_list
);
346 static int ipcaif_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
348 struct chnl_net
*priv
;
350 priv
= netdev_priv(dev
);
351 NLA_PUT_U32(skb
, IFLA_CAIF_IPV4_CONNID
,
352 priv
->conn_req
.sockaddr
.u
.dgm
.connection_id
);
353 NLA_PUT_U32(skb
, IFLA_CAIF_IPV6_CONNID
,
354 priv
->conn_req
.sockaddr
.u
.dgm
.connection_id
);
355 loop
= priv
->conn_req
.protocol
== CAIFPROTO_DATAGRAM_LOOP
;
356 NLA_PUT_U8(skb
, IFLA_CAIF_LOOPBACK
, loop
);
365 static void caif_netlink_parms(struct nlattr
*data
[],
366 struct caif_connect_request
*conn_req
)
369 pr_warning("CAIF: %s: no params data found\n", __func__
);
372 if (data
[IFLA_CAIF_IPV4_CONNID
])
373 conn_req
->sockaddr
.u
.dgm
.connection_id
=
374 nla_get_u32(data
[IFLA_CAIF_IPV4_CONNID
]);
375 if (data
[IFLA_CAIF_IPV6_CONNID
])
376 conn_req
->sockaddr
.u
.dgm
.connection_id
=
377 nla_get_u32(data
[IFLA_CAIF_IPV6_CONNID
]);
378 if (data
[IFLA_CAIF_LOOPBACK
]) {
379 if (nla_get_u8(data
[IFLA_CAIF_LOOPBACK
]))
380 conn_req
->protocol
= CAIFPROTO_DATAGRAM_LOOP
;
382 conn_req
->protocol
= CAIFPROTO_DATAGRAM
;
386 static int ipcaif_newlink(struct net
*src_net
, struct net_device
*dev
,
387 struct nlattr
*tb
[], struct nlattr
*data
[])
390 struct chnl_net
*caifdev
;
392 caifdev
= netdev_priv(dev
);
393 caif_netlink_parms(data
, &caifdev
->conn_req
);
394 dev_net_set(caifdev
->netdev
, src_net
);
396 ret
= register_netdevice(dev
);
398 pr_warning("CAIF: %s(): device rtml registration failed\n",
403 static int ipcaif_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
404 struct nlattr
*data
[])
406 struct chnl_net
*caifdev
;
408 caifdev
= netdev_priv(dev
);
409 caif_netlink_parms(data
, &caifdev
->conn_req
);
410 netdev_state_change(dev
);
414 static size_t ipcaif_get_size(const struct net_device
*dev
)
417 /* IFLA_CAIF_IPV4_CONNID */
419 /* IFLA_CAIF_IPV6_CONNID */
421 /* IFLA_CAIF_LOOPBACK */
426 static const struct nla_policy ipcaif_policy
[IFLA_CAIF_MAX
+ 1] = {
427 [IFLA_CAIF_IPV4_CONNID
] = { .type
= NLA_U32
},
428 [IFLA_CAIF_IPV6_CONNID
] = { .type
= NLA_U32
},
429 [IFLA_CAIF_LOOPBACK
] = { .type
= NLA_U8
}
433 static struct rtnl_link_ops ipcaif_link_ops __read_mostly
= {
435 .priv_size
= sizeof(struct chnl_net
),
436 .setup
= ipcaif_net_setup
,
437 .maxtype
= IFLA_CAIF_MAX
,
438 .policy
= ipcaif_policy
,
439 .newlink
= ipcaif_newlink
,
440 .changelink
= ipcaif_changelink
,
441 .get_size
= ipcaif_get_size
,
442 .fill_info
= ipcaif_fill_info
,
446 static int __init
chnl_init_module(void)
448 return rtnl_link_register(&ipcaif_link_ops
);
451 static void __exit
chnl_exit_module(void)
453 struct chnl_net
*dev
= NULL
;
454 struct list_head
*list_node
;
455 struct list_head
*_tmp
;
456 rtnl_link_unregister(&ipcaif_link_ops
);
458 list_for_each_safe(list_node
, _tmp
, &chnl_net_list
) {
459 dev
= list_entry(list_node
, struct chnl_net
, list_field
);
466 module_init(chnl_init_module
);
467 module_exit(chnl_exit_module
);