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 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
10 #include <linux/version.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/if_ether.h>
16 #include <linux/moduleparam.h>
18 #include <linux/sched.h>
19 #include <linux/sockios.h>
20 #include <linux/caif/if_caif.h>
21 #include <net/rtnetlink.h>
22 #include <net/caif/caif_layer.h>
23 #include <net/caif/cfcnfg.h>
24 #include <net/caif/cfpkt.h>
25 #include <net/caif/caif_dev.h>
27 /* GPRS PDP connection has MTU to 1500 */
28 #define GPRS_PDP_MTU 1500
29 /* 5 sec. connect timeout */
30 #define CONNECT_TIMEOUT (5 * HZ)
31 #define CAIF_NET_DEFAULT_QUEUE_LEN 500
34 #define pr_debug pr_warn
36 /*This list is protected by the rtnl lock. */
37 static LIST_HEAD(chnl_net_list);
39 MODULE_LICENSE("GPL");
40 MODULE_ALIAS_RTNL_LINK("caif");
51 struct net_device_stats stats;
52 struct caif_connect_request conn_req;
53 struct list_head list_field;
54 struct net_device *netdev;
56 wait_queue_head_t netmgmt_wq;
57 /* Flow status to remember and control the transmission. */
59 enum caif_states state;
62 static void robust_list_del(struct list_head *delete_node)
64 struct list_head *list_node;
67 list_for_each_safe(list_node, n, &chnl_net_list) {
68 if (list_node == delete_node) {
76 static int chnl_recv_cb(struct cflayer *layr, struct cfpkt *pkt)
79 struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
83 priv = container_of(layr, struct chnl_net, chnl);
88 /* Get length of CAIF packet. */
89 pktlen = cfpkt_getlen(pkt);
91 skb = (struct sk_buff *) cfpkt_tonative(pkt);
92 /* Pass some minimum information and
93 * send the packet to the net stack.
95 skb->dev = priv->netdev;
96 skb->protocol = htons(ETH_P_IP);
98 /* If we change the header in loop mode, the checksum is corrupted. */
99 if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
100 skb->ip_summed = CHECKSUM_UNNECESSARY;
102 skb->ip_summed = CHECKSUM_NONE;
109 /* Update statistics. */
110 priv->netdev->stats.rx_packets++;
111 priv->netdev->stats.rx_bytes += pktlen;
116 static int delete_device(struct chnl_net *dev)
120 unregister_netdevice(dev->netdev);
124 static void close_work(struct work_struct *work)
126 struct chnl_net *dev = NULL;
127 struct list_head *list_node;
128 struct list_head *_tmp;
129 /* May be called with or without RTNL lock held */
130 int islocked = rtnl_is_locked();
133 list_for_each_safe(list_node, _tmp, &chnl_net_list) {
134 dev = list_entry(list_node, struct chnl_net, list_field);
135 if (dev->state == CAIF_SHUTDOWN)
136 dev_close(dev->netdev);
141 static DECLARE_WORK(close_worker, close_work);
143 static void chnl_flowctrl_cb(struct cflayer *layr, enum caif_ctrlcmd flow,
146 struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
147 pr_debug("NET flowctrl func called flow: %s\n",
148 flow == CAIF_CTRLCMD_FLOW_ON_IND ? "ON" :
149 flow == CAIF_CTRLCMD_INIT_RSP ? "INIT" :
150 flow == CAIF_CTRLCMD_FLOW_OFF_IND ? "OFF" :
151 flow == CAIF_CTRLCMD_DEINIT_RSP ? "CLOSE/DEINIT" :
152 flow == CAIF_CTRLCMD_INIT_FAIL_RSP ? "OPEN_FAIL" :
153 flow == CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND ?
154 "REMOTE_SHUTDOWN" : "UKNOWN CTRL COMMAND");
159 case CAIF_CTRLCMD_FLOW_OFF_IND:
160 priv->flowenabled = false;
161 netif_stop_queue(priv->netdev);
163 case CAIF_CTRLCMD_DEINIT_RSP:
164 priv->state = CAIF_DISCONNECTED;
166 case CAIF_CTRLCMD_INIT_FAIL_RSP:
167 priv->state = CAIF_DISCONNECTED;
168 wake_up_interruptible(&priv->netmgmt_wq);
170 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
171 priv->state = CAIF_SHUTDOWN;
172 netif_tx_disable(priv->netdev);
173 schedule_work(&close_worker);
175 case CAIF_CTRLCMD_FLOW_ON_IND:
176 priv->flowenabled = true;
177 netif_wake_queue(priv->netdev);
179 case CAIF_CTRLCMD_INIT_RSP:
180 priv->state = CAIF_CONNECTED;
181 priv->flowenabled = true;
182 netif_wake_queue(priv->netdev);
183 wake_up_interruptible(&priv->netmgmt_wq);
190 static int chnl_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
192 struct chnl_net *priv;
193 struct cfpkt *pkt = NULL;
196 /* Get our private data. */
197 priv = netdev_priv(dev);
199 if (skb->len > priv->netdev->mtu) {
200 pr_warn("Size of skb exceeded MTU\n");
204 if (!priv->flowenabled) {
205 pr_debug("dropping packets flow off\n");
206 return NETDEV_TX_BUSY;
209 if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
210 swap(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
212 /* Store original SKB length. */
215 pkt = cfpkt_fromnative(CAIF_DIR_OUT, (void *) skb);
217 /* Send the packet down the stack. */
218 result = priv->chnl.dn->transmit(priv->chnl.dn, pkt);
220 if (result == -EAGAIN)
221 result = NETDEV_TX_BUSY;
225 /* Update statistics. */
226 dev->stats.tx_packets++;
227 dev->stats.tx_bytes += len;
232 static int chnl_net_open(struct net_device *dev)
234 struct chnl_net *priv = NULL;
236 int llifindex, headroom, tailroom, mtu;
237 struct net_device *lldev;
239 priv = netdev_priv(dev);
241 pr_debug("chnl_net_open: no priv\n");
245 if (priv->state != CAIF_CONNECTING) {
246 priv->state = CAIF_CONNECTING;
247 result = caif_connect_client(&priv->conn_req, &priv->chnl,
248 &llifindex, &headroom, &tailroom);
251 "Unable to register and open device,"
257 lldev = dev_get_by_index(dev_net(dev), llifindex);
260 pr_debug("no interface?\n");
265 dev->needed_tailroom = tailroom + lldev->needed_tailroom;
266 dev->hard_header_len = headroom + lldev->hard_header_len +
267 lldev->needed_tailroom;
270 * MTU, head-room etc is not know before we have a
271 * CAIF link layer device available. MTU calculation may
272 * override initial RTNL configuration.
273 * MTU is minimum of current mtu, link layer mtu pluss
274 * CAIF head and tail, and PDP GPRS contexts max MTU.
276 mtu = min_t(int, dev->mtu, lldev->mtu - (headroom + tailroom));
277 mtu = min_t(int, GPRS_PDP_MTU, mtu);
278 dev_set_mtu(dev, mtu);
282 pr_warn("CAIF Interface MTU too small (%d)\n", mtu);
288 rtnl_unlock(); /* Release RTNL lock during connect wait */
290 result = wait_event_interruptible_timeout(priv->netmgmt_wq,
291 priv->state != CAIF_CONNECTING,
296 if (result == -ERESTARTSYS) {
297 pr_debug("wait_event_interruptible woken by a signal\n");
298 result = -ERESTARTSYS;
303 pr_debug("connect timeout\n");
304 caif_disconnect_client(&priv->chnl);
305 priv->state = CAIF_DISCONNECTED;
306 pr_debug("state disconnected\n");
311 if (priv->state != CAIF_CONNECTED) {
312 pr_debug("connect failed\n");
313 result = -ECONNREFUSED;
316 pr_debug("CAIF Netdevice connected\n");
320 caif_disconnect_client(&priv->chnl);
321 priv->state = CAIF_DISCONNECTED;
322 pr_debug("state disconnected\n");
327 static int chnl_net_stop(struct net_device *dev)
329 struct chnl_net *priv;
332 priv = netdev_priv(dev);
333 priv->state = CAIF_DISCONNECTED;
334 caif_disconnect_client(&priv->chnl);
338 static int chnl_net_init(struct net_device *dev)
340 struct chnl_net *priv;
342 priv = netdev_priv(dev);
343 strncpy(priv->name, dev->name, sizeof(priv->name));
347 static void chnl_net_uninit(struct net_device *dev)
349 struct chnl_net *priv;
351 priv = netdev_priv(dev);
352 robust_list_del(&priv->list_field);
355 static const struct net_device_ops netdev_ops = {
356 .ndo_open = chnl_net_open,
357 .ndo_stop = chnl_net_stop,
358 .ndo_init = chnl_net_init,
359 .ndo_uninit = chnl_net_uninit,
360 .ndo_start_xmit = chnl_net_start_xmit,
363 static void ipcaif_net_setup(struct net_device *dev)
365 struct chnl_net *priv;
366 dev->netdev_ops = &netdev_ops;
367 dev->destructor = free_netdev;
368 dev->flags |= IFF_NOARP;
369 dev->flags |= IFF_POINTOPOINT;
370 dev->mtu = GPRS_PDP_MTU;
371 dev->tx_queue_len = CAIF_NET_DEFAULT_QUEUE_LEN;
373 priv = netdev_priv(dev);
374 priv->chnl.receive = chnl_recv_cb;
375 priv->chnl.ctrlcmd = chnl_flowctrl_cb;
377 priv->conn_req.protocol = CAIFPROTO_DATAGRAM;
378 priv->conn_req.link_selector = CAIF_LINK_HIGH_BANDW;
379 priv->conn_req.priority = CAIF_PRIO_LOW;
380 /* Insert illegal value */
381 priv->conn_req.sockaddr.u.dgm.connection_id = -1;
382 priv->flowenabled = false;
385 init_waitqueue_head(&priv->netmgmt_wq);
386 list_add(&priv->list_field, &chnl_net_list);
390 static int ipcaif_fill_info(struct sk_buff *skb, const struct net_device *dev)
392 struct chnl_net *priv;
394 priv = netdev_priv(dev);
395 NLA_PUT_U32(skb, IFLA_CAIF_IPV4_CONNID,
396 priv->conn_req.sockaddr.u.dgm.connection_id);
397 NLA_PUT_U32(skb, IFLA_CAIF_IPV6_CONNID,
398 priv->conn_req.sockaddr.u.dgm.connection_id);
399 loop = priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP;
400 NLA_PUT_U8(skb, IFLA_CAIF_LOOPBACK, loop);
409 static void caif_netlink_parms(struct nlattr *data[],
410 struct caif_connect_request *conn_req)
413 pr_warn("no params data found\n");
416 if (data[IFLA_CAIF_IPV4_CONNID])
417 conn_req->sockaddr.u.dgm.connection_id =
418 nla_get_u32(data[IFLA_CAIF_IPV4_CONNID]);
419 if (data[IFLA_CAIF_IPV6_CONNID])
420 conn_req->sockaddr.u.dgm.connection_id =
421 nla_get_u32(data[IFLA_CAIF_IPV6_CONNID]);
422 if (data[IFLA_CAIF_LOOPBACK]) {
423 if (nla_get_u8(data[IFLA_CAIF_LOOPBACK]))
424 conn_req->protocol = CAIFPROTO_DATAGRAM_LOOP;
426 conn_req->protocol = CAIFPROTO_DATAGRAM;
430 static int ipcaif_newlink(struct net *src_net, struct net_device *dev,
431 struct nlattr *tb[], struct nlattr *data[])
434 struct chnl_net *caifdev;
436 caifdev = netdev_priv(dev);
437 caif_netlink_parms(data, &caifdev->conn_req);
438 dev_net_set(caifdev->netdev, src_net);
440 ret = register_netdevice(dev);
442 pr_warn("device rtml registration failed\n");
446 static int ipcaif_changelink(struct net_device *dev, struct nlattr *tb[],
447 struct nlattr *data[])
449 struct chnl_net *caifdev;
451 caifdev = netdev_priv(dev);
452 caif_netlink_parms(data, &caifdev->conn_req);
453 netdev_state_change(dev);
457 static size_t ipcaif_get_size(const struct net_device *dev)
460 /* IFLA_CAIF_IPV4_CONNID */
462 /* IFLA_CAIF_IPV6_CONNID */
464 /* IFLA_CAIF_LOOPBACK */
469 static const struct nla_policy ipcaif_policy[IFLA_CAIF_MAX + 1] = {
470 [IFLA_CAIF_IPV4_CONNID] = { .type = NLA_U32 },
471 [IFLA_CAIF_IPV6_CONNID] = { .type = NLA_U32 },
472 [IFLA_CAIF_LOOPBACK] = { .type = NLA_U8 }
476 static struct rtnl_link_ops ipcaif_link_ops __read_mostly = {
478 .priv_size = sizeof(struct chnl_net),
479 .setup = ipcaif_net_setup,
480 .maxtype = IFLA_CAIF_MAX,
481 .policy = ipcaif_policy,
482 .newlink = ipcaif_newlink,
483 .changelink = ipcaif_changelink,
484 .get_size = ipcaif_get_size,
485 .fill_info = ipcaif_fill_info,
489 static int __init chnl_init_module(void)
491 return rtnl_link_register(&ipcaif_link_ops);
494 static void __exit chnl_exit_module(void)
496 struct chnl_net *dev = NULL;
497 struct list_head *list_node;
498 struct list_head *_tmp;
499 rtnl_link_unregister(&ipcaif_link_ops);
501 list_for_each_safe(list_node, _tmp, &chnl_net_list) {
502 dev = list_entry(list_node, struct chnl_net, list_field);
509 module_init(chnl_init_module);
510 module_exit(chnl_exit_module);