2 * Copyright (C) ST-Ericsson AB 2010
3 * Authors: Sjur Brendeland
5 * License terms: GNU General Public License (GPL) version 2
8 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
11 #include <linux/hardirq.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/cfpkt.h>
24 #include <net/caif/caif_dev.h>
26 /* GPRS PDP connection has MTU to 1500 */
27 #define GPRS_PDP_MTU 1500
28 /* 5 sec. connect timeout */
29 #define CONNECT_TIMEOUT (5 * HZ)
30 #define CAIF_NET_DEFAULT_QUEUE_LEN 500
31 #define UNDEF_CONNID 0xffffffff
33 /*This list is protected by the rtnl lock. */
34 static LIST_HEAD(chnl_net_list);
36 MODULE_LICENSE("GPL");
37 MODULE_ALIAS_RTNL_LINK("caif");
48 struct net_device_stats stats;
49 struct caif_connect_request conn_req;
50 struct list_head list_field;
51 struct net_device *netdev;
53 wait_queue_head_t netmgmt_wq;
54 /* Flow status to remember and control the transmission. */
56 enum caif_states state;
59 static int chnl_recv_cb(struct cflayer *layr, struct cfpkt *pkt)
62 struct chnl_net *priv;
67 priv = container_of(layr, struct chnl_net, chnl);
71 skb = (struct sk_buff *) cfpkt_tonative(pkt);
73 /* Get length of CAIF packet. */
76 /* Pass some minimum information and
77 * send the packet to the net stack.
79 skb->dev = priv->netdev;
81 /* check the version of IP */
82 ip_version = skb_header_pointer(skb, 0, 1, &buf);
88 switch (*ip_version >> 4) {
90 skb->protocol = htons(ETH_P_IP);
93 skb->protocol = htons(ETH_P_IPV6);
97 priv->netdev->stats.rx_errors++;
101 /* If we change the header in loop mode, the checksum is corrupted. */
102 if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
103 skb->ip_summed = CHECKSUM_UNNECESSARY;
105 skb->ip_summed = CHECKSUM_NONE;
112 /* Update statistics. */
113 priv->netdev->stats.rx_packets++;
114 priv->netdev->stats.rx_bytes += pktlen;
119 static int delete_device(struct chnl_net *dev)
123 unregister_netdevice(dev->netdev);
127 static void close_work(struct work_struct *work)
129 struct chnl_net *dev = NULL;
130 struct list_head *list_node;
131 struct list_head *_tmp;
134 list_for_each_safe(list_node, _tmp, &chnl_net_list) {
135 dev = list_entry(list_node, struct chnl_net, list_field);
136 if (dev->state == CAIF_SHUTDOWN)
137 dev_close(dev->netdev);
141 static DECLARE_WORK(close_worker, close_work);
143 static void chnl_hold(struct cflayer *lyr)
145 struct chnl_net *priv = container_of(lyr, struct chnl_net, chnl);
146 dev_hold(priv->netdev);
149 static void chnl_put(struct cflayer *lyr)
151 struct chnl_net *priv = container_of(lyr, struct chnl_net, chnl);
152 dev_put(priv->netdev);
155 static void chnl_flowctrl_cb(struct cflayer *layr, enum caif_ctrlcmd flow,
158 struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
159 pr_debug("NET flowctrl func called flow: %s\n",
160 flow == CAIF_CTRLCMD_FLOW_ON_IND ? "ON" :
161 flow == CAIF_CTRLCMD_INIT_RSP ? "INIT" :
162 flow == CAIF_CTRLCMD_FLOW_OFF_IND ? "OFF" :
163 flow == CAIF_CTRLCMD_DEINIT_RSP ? "CLOSE/DEINIT" :
164 flow == CAIF_CTRLCMD_INIT_FAIL_RSP ? "OPEN_FAIL" :
165 flow == CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND ?
166 "REMOTE_SHUTDOWN" : "UKNOWN CTRL COMMAND");
171 case CAIF_CTRLCMD_FLOW_OFF_IND:
172 priv->flowenabled = false;
173 netif_stop_queue(priv->netdev);
175 case CAIF_CTRLCMD_DEINIT_RSP:
176 priv->state = CAIF_DISCONNECTED;
178 case CAIF_CTRLCMD_INIT_FAIL_RSP:
179 priv->state = CAIF_DISCONNECTED;
180 wake_up_interruptible(&priv->netmgmt_wq);
182 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
183 priv->state = CAIF_SHUTDOWN;
184 netif_tx_disable(priv->netdev);
185 schedule_work(&close_worker);
187 case CAIF_CTRLCMD_FLOW_ON_IND:
188 priv->flowenabled = true;
189 netif_wake_queue(priv->netdev);
191 case CAIF_CTRLCMD_INIT_RSP:
192 caif_client_register_refcnt(&priv->chnl, chnl_hold, chnl_put);
193 priv->state = CAIF_CONNECTED;
194 priv->flowenabled = true;
195 netif_wake_queue(priv->netdev);
196 wake_up_interruptible(&priv->netmgmt_wq);
203 static int chnl_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
205 struct chnl_net *priv;
206 struct cfpkt *pkt = NULL;
209 /* Get our private data. */
210 priv = netdev_priv(dev);
212 if (skb->len > priv->netdev->mtu) {
213 pr_warn("Size of skb exceeded MTU\n");
215 dev->stats.tx_errors++;
219 if (!priv->flowenabled) {
220 pr_debug("dropping packets flow off\n");
222 dev->stats.tx_dropped++;
226 if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
227 swap(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
229 /* Store original SKB length. */
232 pkt = cfpkt_fromnative(CAIF_DIR_OUT, (void *) skb);
234 /* Send the packet down the stack. */
235 result = priv->chnl.dn->transmit(priv->chnl.dn, pkt);
237 dev->stats.tx_dropped++;
241 /* Update statistics. */
242 dev->stats.tx_packets++;
243 dev->stats.tx_bytes += len;
248 static int chnl_net_open(struct net_device *dev)
250 struct chnl_net *priv = NULL;
252 int llifindex, headroom, tailroom, mtu;
253 struct net_device *lldev;
255 priv = netdev_priv(dev);
257 pr_debug("chnl_net_open: no priv\n");
261 if (priv->state != CAIF_CONNECTING) {
262 priv->state = CAIF_CONNECTING;
263 result = caif_connect_client(dev_net(dev), &priv->conn_req,
264 &priv->chnl, &llifindex,
265 &headroom, &tailroom);
268 "Unable to register and open device,"
274 lldev = __dev_get_by_index(dev_net(dev), llifindex);
277 pr_debug("no interface?\n");
282 dev->needed_tailroom = tailroom + lldev->needed_tailroom;
283 dev->hard_header_len = headroom + lldev->hard_header_len +
284 lldev->needed_tailroom;
287 * MTU, head-room etc is not know before we have a
288 * CAIF link layer device available. MTU calculation may
289 * override initial RTNL configuration.
290 * MTU is minimum of current mtu, link layer mtu pluss
291 * CAIF head and tail, and PDP GPRS contexts max MTU.
293 mtu = min_t(int, dev->mtu, lldev->mtu - (headroom + tailroom));
294 mtu = min_t(int, GPRS_PDP_MTU, mtu);
295 dev_set_mtu(dev, mtu);
298 pr_warn("CAIF Interface MTU too small (%d)\n", mtu);
304 rtnl_unlock(); /* Release RTNL lock during connect wait */
306 result = wait_event_interruptible_timeout(priv->netmgmt_wq,
307 priv->state != CAIF_CONNECTING,
312 if (result == -ERESTARTSYS) {
313 pr_debug("wait_event_interruptible woken by a signal\n");
314 result = -ERESTARTSYS;
319 pr_debug("connect timeout\n");
320 caif_disconnect_client(dev_net(dev), &priv->chnl);
321 priv->state = CAIF_DISCONNECTED;
322 pr_debug("state disconnected\n");
327 if (priv->state != CAIF_CONNECTED) {
328 pr_debug("connect failed\n");
329 result = -ECONNREFUSED;
332 pr_debug("CAIF Netdevice connected\n");
336 caif_disconnect_client(dev_net(dev), &priv->chnl);
337 priv->state = CAIF_DISCONNECTED;
338 pr_debug("state disconnected\n");
343 static int chnl_net_stop(struct net_device *dev)
345 struct chnl_net *priv;
348 priv = netdev_priv(dev);
349 priv->state = CAIF_DISCONNECTED;
350 caif_disconnect_client(dev_net(dev), &priv->chnl);
354 static int chnl_net_init(struct net_device *dev)
356 struct chnl_net *priv;
358 priv = netdev_priv(dev);
359 strncpy(priv->name, dev->name, sizeof(priv->name));
360 INIT_LIST_HEAD(&priv->list_field);
364 static void chnl_net_uninit(struct net_device *dev)
366 struct chnl_net *priv;
368 priv = netdev_priv(dev);
369 list_del_init(&priv->list_field);
372 static const struct net_device_ops netdev_ops = {
373 .ndo_open = chnl_net_open,
374 .ndo_stop = chnl_net_stop,
375 .ndo_init = chnl_net_init,
376 .ndo_uninit = chnl_net_uninit,
377 .ndo_start_xmit = chnl_net_start_xmit,
380 static void chnl_net_destructor(struct net_device *dev)
382 struct chnl_net *priv = netdev_priv(dev);
383 caif_free_client(&priv->chnl);
387 static void ipcaif_net_setup(struct net_device *dev)
389 struct chnl_net *priv;
390 dev->netdev_ops = &netdev_ops;
391 dev->destructor = chnl_net_destructor;
392 dev->flags |= IFF_NOARP;
393 dev->flags |= IFF_POINTOPOINT;
394 dev->mtu = GPRS_PDP_MTU;
395 dev->tx_queue_len = CAIF_NET_DEFAULT_QUEUE_LEN;
397 priv = netdev_priv(dev);
398 priv->chnl.receive = chnl_recv_cb;
399 priv->chnl.ctrlcmd = chnl_flowctrl_cb;
401 priv->conn_req.protocol = CAIFPROTO_DATAGRAM;
402 priv->conn_req.link_selector = CAIF_LINK_HIGH_BANDW;
403 priv->conn_req.priority = CAIF_PRIO_LOW;
404 /* Insert illegal value */
405 priv->conn_req.sockaddr.u.dgm.connection_id = UNDEF_CONNID;
406 priv->flowenabled = false;
408 init_waitqueue_head(&priv->netmgmt_wq);
412 static int ipcaif_fill_info(struct sk_buff *skb, const struct net_device *dev)
414 struct chnl_net *priv;
416 priv = netdev_priv(dev);
417 if (nla_put_u32(skb, IFLA_CAIF_IPV4_CONNID,
418 priv->conn_req.sockaddr.u.dgm.connection_id) ||
419 nla_put_u32(skb, IFLA_CAIF_IPV6_CONNID,
420 priv->conn_req.sockaddr.u.dgm.connection_id))
421 goto nla_put_failure;
422 loop = priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP;
423 if (nla_put_u8(skb, IFLA_CAIF_LOOPBACK, loop))
424 goto nla_put_failure;
431 static void caif_netlink_parms(struct nlattr *data[],
432 struct caif_connect_request *conn_req)
435 pr_warn("no params data found\n");
438 if (data[IFLA_CAIF_IPV4_CONNID])
439 conn_req->sockaddr.u.dgm.connection_id =
440 nla_get_u32(data[IFLA_CAIF_IPV4_CONNID]);
441 if (data[IFLA_CAIF_IPV6_CONNID])
442 conn_req->sockaddr.u.dgm.connection_id =
443 nla_get_u32(data[IFLA_CAIF_IPV6_CONNID]);
444 if (data[IFLA_CAIF_LOOPBACK]) {
445 if (nla_get_u8(data[IFLA_CAIF_LOOPBACK]))
446 conn_req->protocol = CAIFPROTO_DATAGRAM_LOOP;
448 conn_req->protocol = CAIFPROTO_DATAGRAM;
452 static int ipcaif_newlink(struct net *src_net, struct net_device *dev,
453 struct nlattr *tb[], struct nlattr *data[])
456 struct chnl_net *caifdev;
458 caifdev = netdev_priv(dev);
459 caif_netlink_parms(data, &caifdev->conn_req);
461 ret = register_netdevice(dev);
463 pr_warn("device rtml registration failed\n");
465 list_add(&caifdev->list_field, &chnl_net_list);
467 /* Use ifindex as connection id, and use loopback channel default. */
468 if (caifdev->conn_req.sockaddr.u.dgm.connection_id == UNDEF_CONNID) {
469 caifdev->conn_req.sockaddr.u.dgm.connection_id = dev->ifindex;
470 caifdev->conn_req.protocol = CAIFPROTO_DATAGRAM_LOOP;
475 static int ipcaif_changelink(struct net_device *dev, struct nlattr *tb[],
476 struct nlattr *data[])
478 struct chnl_net *caifdev;
480 caifdev = netdev_priv(dev);
481 caif_netlink_parms(data, &caifdev->conn_req);
482 netdev_state_change(dev);
486 static size_t ipcaif_get_size(const struct net_device *dev)
489 /* IFLA_CAIF_IPV4_CONNID */
491 /* IFLA_CAIF_IPV6_CONNID */
493 /* IFLA_CAIF_LOOPBACK */
498 static const struct nla_policy ipcaif_policy[IFLA_CAIF_MAX + 1] = {
499 [IFLA_CAIF_IPV4_CONNID] = { .type = NLA_U32 },
500 [IFLA_CAIF_IPV6_CONNID] = { .type = NLA_U32 },
501 [IFLA_CAIF_LOOPBACK] = { .type = NLA_U8 }
505 static struct rtnl_link_ops ipcaif_link_ops __read_mostly = {
507 .priv_size = sizeof(struct chnl_net),
508 .setup = ipcaif_net_setup,
509 .maxtype = IFLA_CAIF_MAX,
510 .policy = ipcaif_policy,
511 .newlink = ipcaif_newlink,
512 .changelink = ipcaif_changelink,
513 .get_size = ipcaif_get_size,
514 .fill_info = ipcaif_fill_info,
518 static int __init chnl_init_module(void)
520 return rtnl_link_register(&ipcaif_link_ops);
523 static void __exit chnl_exit_module(void)
525 struct chnl_net *dev = NULL;
526 struct list_head *list_node;
527 struct list_head *_tmp;
528 rtnl_link_unregister(&ipcaif_link_ops);
530 list_for_each_safe(list_node, _tmp, &chnl_net_list) {
531 dev = list_entry(list_node, struct chnl_net, list_field);
532 list_del_init(list_node);
538 module_init(chnl_init_module);
539 module_exit(chnl_exit_module);