Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) ST-Ericsson AB 2010
0004  * Authors: Sjur Brendeland
0005  *      Daniel Martensson
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
0009 
0010 #include <linux/fs.h>
0011 #include <linux/init.h>
0012 #include <linux/module.h>
0013 #include <linux/netdevice.h>
0014 #include <linux/if_ether.h>
0015 #include <linux/ip.h>
0016 #include <linux/sched.h>
0017 #include <linux/sockios.h>
0018 #include <linux/caif/if_caif.h>
0019 #include <net/rtnetlink.h>
0020 #include <net/caif/caif_layer.h>
0021 #include <net/caif/cfpkt.h>
0022 #include <net/caif/caif_dev.h>
0023 
0024 /* GPRS PDP connection has MTU to 1500 */
0025 #define GPRS_PDP_MTU 1500
0026 /* 5 sec. connect timeout */
0027 #define CONNECT_TIMEOUT (5 * HZ)
0028 #define CAIF_NET_DEFAULT_QUEUE_LEN 500
0029 #define UNDEF_CONNID 0xffffffff
0030 
0031 /*This list is protected by the rtnl lock. */
0032 static LIST_HEAD(chnl_net_list);
0033 
0034 MODULE_LICENSE("GPL");
0035 MODULE_ALIAS_RTNL_LINK("caif");
0036 
0037 enum caif_states {
0038     CAIF_CONNECTED      = 1,
0039     CAIF_CONNECTING,
0040     CAIF_DISCONNECTED,
0041     CAIF_SHUTDOWN
0042 };
0043 
0044 struct chnl_net {
0045     struct cflayer chnl;
0046     struct caif_connect_request conn_req;
0047     struct list_head list_field;
0048     struct net_device *netdev;
0049     char name[256];
0050     wait_queue_head_t netmgmt_wq;
0051     /* Flow status to remember and control the transmission. */
0052     bool flowenabled;
0053     enum caif_states state;
0054 };
0055 
0056 static int chnl_recv_cb(struct cflayer *layr, struct cfpkt *pkt)
0057 {
0058     struct sk_buff *skb;
0059     struct chnl_net *priv;
0060     int pktlen;
0061     const u8 *ip_version;
0062     u8 buf;
0063 
0064     priv = container_of(layr, struct chnl_net, chnl);
0065 
0066     skb = (struct sk_buff *) cfpkt_tonative(pkt);
0067 
0068     /* Get length of CAIF packet. */
0069     pktlen = skb->len;
0070 
0071     /* Pass some minimum information and
0072      * send the packet to the net stack.
0073      */
0074     skb->dev = priv->netdev;
0075 
0076     /* check the version of IP */
0077     ip_version = skb_header_pointer(skb, 0, 1, &buf);
0078     if (!ip_version) {
0079         kfree_skb(skb);
0080         return -EINVAL;
0081     }
0082 
0083     switch (*ip_version >> 4) {
0084     case 4:
0085         skb->protocol = htons(ETH_P_IP);
0086         break;
0087     case 6:
0088         skb->protocol = htons(ETH_P_IPV6);
0089         break;
0090     default:
0091         kfree_skb(skb);
0092         priv->netdev->stats.rx_errors++;
0093         return -EINVAL;
0094     }
0095 
0096     /* If we change the header in loop mode, the checksum is corrupted. */
0097     if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
0098         skb->ip_summed = CHECKSUM_UNNECESSARY;
0099     else
0100         skb->ip_summed = CHECKSUM_NONE;
0101 
0102     netif_rx(skb);
0103 
0104     /* Update statistics. */
0105     priv->netdev->stats.rx_packets++;
0106     priv->netdev->stats.rx_bytes += pktlen;
0107 
0108     return 0;
0109 }
0110 
0111 static int delete_device(struct chnl_net *dev)
0112 {
0113     ASSERT_RTNL();
0114     if (dev->netdev)
0115         unregister_netdevice(dev->netdev);
0116     return 0;
0117 }
0118 
0119 static void close_work(struct work_struct *work)
0120 {
0121     struct chnl_net *dev = NULL;
0122     struct list_head *list_node;
0123     struct list_head *_tmp;
0124 
0125     rtnl_lock();
0126     list_for_each_safe(list_node, _tmp, &chnl_net_list) {
0127         dev = list_entry(list_node, struct chnl_net, list_field);
0128         if (dev->state == CAIF_SHUTDOWN)
0129             dev_close(dev->netdev);
0130     }
0131     rtnl_unlock();
0132 }
0133 static DECLARE_WORK(close_worker, close_work);
0134 
0135 static void chnl_hold(struct cflayer *lyr)
0136 {
0137     struct chnl_net *priv = container_of(lyr, struct chnl_net, chnl);
0138     dev_hold(priv->netdev);
0139 }
0140 
0141 static void chnl_put(struct cflayer *lyr)
0142 {
0143     struct chnl_net *priv = container_of(lyr, struct chnl_net, chnl);
0144     dev_put(priv->netdev);
0145 }
0146 
0147 static void chnl_flowctrl_cb(struct cflayer *layr, enum caif_ctrlcmd flow,
0148                  int phyid)
0149 {
0150     struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
0151     pr_debug("NET flowctrl func called flow: %s\n",
0152         flow == CAIF_CTRLCMD_FLOW_ON_IND ? "ON" :
0153         flow == CAIF_CTRLCMD_INIT_RSP ? "INIT" :
0154         flow == CAIF_CTRLCMD_FLOW_OFF_IND ? "OFF" :
0155         flow == CAIF_CTRLCMD_DEINIT_RSP ? "CLOSE/DEINIT" :
0156         flow == CAIF_CTRLCMD_INIT_FAIL_RSP ? "OPEN_FAIL" :
0157         flow == CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND ?
0158          "REMOTE_SHUTDOWN" : "UNKNOWN CTRL COMMAND");
0159 
0160 
0161 
0162     switch (flow) {
0163     case CAIF_CTRLCMD_FLOW_OFF_IND:
0164         priv->flowenabled = false;
0165         netif_stop_queue(priv->netdev);
0166         break;
0167     case CAIF_CTRLCMD_DEINIT_RSP:
0168         priv->state = CAIF_DISCONNECTED;
0169         break;
0170     case CAIF_CTRLCMD_INIT_FAIL_RSP:
0171         priv->state = CAIF_DISCONNECTED;
0172         wake_up_interruptible(&priv->netmgmt_wq);
0173         break;
0174     case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
0175         priv->state = CAIF_SHUTDOWN;
0176         netif_tx_disable(priv->netdev);
0177         schedule_work(&close_worker);
0178         break;
0179     case CAIF_CTRLCMD_FLOW_ON_IND:
0180         priv->flowenabled = true;
0181         netif_wake_queue(priv->netdev);
0182         break;
0183     case CAIF_CTRLCMD_INIT_RSP:
0184         caif_client_register_refcnt(&priv->chnl, chnl_hold, chnl_put);
0185         priv->state = CAIF_CONNECTED;
0186         priv->flowenabled = true;
0187         netif_wake_queue(priv->netdev);
0188         wake_up_interruptible(&priv->netmgmt_wq);
0189         break;
0190     default:
0191         break;
0192     }
0193 }
0194 
0195 static netdev_tx_t chnl_net_start_xmit(struct sk_buff *skb,
0196                        struct net_device *dev)
0197 {
0198     struct chnl_net *priv;
0199     struct cfpkt *pkt = NULL;
0200     int len;
0201     int result = -1;
0202     /* Get our private data. */
0203     priv = netdev_priv(dev);
0204 
0205     if (skb->len > priv->netdev->mtu) {
0206         pr_warn("Size of skb exceeded MTU\n");
0207         kfree_skb(skb);
0208         dev->stats.tx_errors++;
0209         return NETDEV_TX_OK;
0210     }
0211 
0212     if (!priv->flowenabled) {
0213         pr_debug("dropping packets flow off\n");
0214         kfree_skb(skb);
0215         dev->stats.tx_dropped++;
0216         return NETDEV_TX_OK;
0217     }
0218 
0219     if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
0220         swap(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
0221 
0222     /* Store original SKB length. */
0223     len = skb->len;
0224 
0225     pkt = cfpkt_fromnative(CAIF_DIR_OUT, (void *) skb);
0226 
0227     /* Send the packet down the stack. */
0228     result = priv->chnl.dn->transmit(priv->chnl.dn, pkt);
0229     if (result) {
0230         dev->stats.tx_dropped++;
0231         return NETDEV_TX_OK;
0232     }
0233 
0234     /* Update statistics. */
0235     dev->stats.tx_packets++;
0236     dev->stats.tx_bytes += len;
0237 
0238     return NETDEV_TX_OK;
0239 }
0240 
0241 static int chnl_net_open(struct net_device *dev)
0242 {
0243     struct chnl_net *priv = NULL;
0244     int result = -1;
0245     int llifindex, headroom, tailroom, mtu;
0246     struct net_device *lldev;
0247     ASSERT_RTNL();
0248     priv = netdev_priv(dev);
0249     if (!priv) {
0250         pr_debug("chnl_net_open: no priv\n");
0251         return -ENODEV;
0252     }
0253 
0254     if (priv->state != CAIF_CONNECTING) {
0255         priv->state = CAIF_CONNECTING;
0256         result = caif_connect_client(dev_net(dev), &priv->conn_req,
0257                         &priv->chnl, &llifindex,
0258                         &headroom, &tailroom);
0259         if (result != 0) {
0260                 pr_debug("err: "
0261                      "Unable to register and open device,"
0262                      " Err:%d\n",
0263                      result);
0264                 goto error;
0265         }
0266 
0267         lldev = __dev_get_by_index(dev_net(dev), llifindex);
0268 
0269         if (lldev == NULL) {
0270             pr_debug("no interface?\n");
0271             result = -ENODEV;
0272             goto error;
0273         }
0274 
0275         dev->needed_tailroom = tailroom + lldev->needed_tailroom;
0276         dev->hard_header_len = headroom + lldev->hard_header_len +
0277             lldev->needed_tailroom;
0278 
0279         /*
0280          * MTU, head-room etc is not know before we have a
0281          * CAIF link layer device available. MTU calculation may
0282          * override initial RTNL configuration.
0283          * MTU is minimum of current mtu, link layer mtu pluss
0284          * CAIF head and tail, and PDP GPRS contexts max MTU.
0285          */
0286         mtu = min_t(int, dev->mtu, lldev->mtu - (headroom + tailroom));
0287         mtu = min_t(int, GPRS_PDP_MTU, mtu);
0288         dev_set_mtu(dev, mtu);
0289 
0290         if (mtu < 100) {
0291             pr_warn("CAIF Interface MTU too small (%d)\n", mtu);
0292             result = -ENODEV;
0293             goto error;
0294         }
0295     }
0296 
0297     rtnl_unlock();  /* Release RTNL lock during connect wait */
0298 
0299     result = wait_event_interruptible_timeout(priv->netmgmt_wq,
0300                         priv->state != CAIF_CONNECTING,
0301                         CONNECT_TIMEOUT);
0302 
0303     rtnl_lock();
0304 
0305     if (result == -ERESTARTSYS) {
0306         pr_debug("wait_event_interruptible woken by a signal\n");
0307         result = -ERESTARTSYS;
0308         goto error;
0309     }
0310 
0311     if (result == 0) {
0312         pr_debug("connect timeout\n");
0313         caif_disconnect_client(dev_net(dev), &priv->chnl);
0314         priv->state = CAIF_DISCONNECTED;
0315         pr_debug("state disconnected\n");
0316         result = -ETIMEDOUT;
0317         goto error;
0318     }
0319 
0320     if (priv->state != CAIF_CONNECTED) {
0321         pr_debug("connect failed\n");
0322         result = -ECONNREFUSED;
0323         goto error;
0324     }
0325     pr_debug("CAIF Netdevice connected\n");
0326     return 0;
0327 
0328 error:
0329     caif_disconnect_client(dev_net(dev), &priv->chnl);
0330     priv->state = CAIF_DISCONNECTED;
0331     pr_debug("state disconnected\n");
0332     return result;
0333 
0334 }
0335 
0336 static int chnl_net_stop(struct net_device *dev)
0337 {
0338     struct chnl_net *priv;
0339 
0340     ASSERT_RTNL();
0341     priv = netdev_priv(dev);
0342     priv->state = CAIF_DISCONNECTED;
0343     caif_disconnect_client(dev_net(dev), &priv->chnl);
0344     return 0;
0345 }
0346 
0347 static int chnl_net_init(struct net_device *dev)
0348 {
0349     struct chnl_net *priv;
0350     ASSERT_RTNL();
0351     priv = netdev_priv(dev);
0352     strncpy(priv->name, dev->name, sizeof(priv->name));
0353     INIT_LIST_HEAD(&priv->list_field);
0354     return 0;
0355 }
0356 
0357 static void chnl_net_uninit(struct net_device *dev)
0358 {
0359     struct chnl_net *priv;
0360     ASSERT_RTNL();
0361     priv = netdev_priv(dev);
0362     list_del_init(&priv->list_field);
0363 }
0364 
0365 static const struct net_device_ops netdev_ops = {
0366     .ndo_open = chnl_net_open,
0367     .ndo_stop = chnl_net_stop,
0368     .ndo_init = chnl_net_init,
0369     .ndo_uninit = chnl_net_uninit,
0370     .ndo_start_xmit = chnl_net_start_xmit,
0371 };
0372 
0373 static void chnl_net_destructor(struct net_device *dev)
0374 {
0375     struct chnl_net *priv = netdev_priv(dev);
0376     caif_free_client(&priv->chnl);
0377 }
0378 
0379 static void ipcaif_net_setup(struct net_device *dev)
0380 {
0381     struct chnl_net *priv;
0382     dev->netdev_ops = &netdev_ops;
0383     dev->needs_free_netdev = true;
0384     dev->priv_destructor = chnl_net_destructor;
0385     dev->flags |= IFF_NOARP;
0386     dev->flags |= IFF_POINTOPOINT;
0387     dev->mtu = GPRS_PDP_MTU;
0388     dev->tx_queue_len = CAIF_NET_DEFAULT_QUEUE_LEN;
0389 
0390     priv = netdev_priv(dev);
0391     priv->chnl.receive = chnl_recv_cb;
0392     priv->chnl.ctrlcmd = chnl_flowctrl_cb;
0393     priv->netdev = dev;
0394     priv->conn_req.protocol = CAIFPROTO_DATAGRAM;
0395     priv->conn_req.link_selector = CAIF_LINK_HIGH_BANDW;
0396     priv->conn_req.priority = CAIF_PRIO_LOW;
0397     /* Insert illegal value */
0398     priv->conn_req.sockaddr.u.dgm.connection_id = UNDEF_CONNID;
0399     priv->flowenabled = false;
0400 
0401     init_waitqueue_head(&priv->netmgmt_wq);
0402 }
0403 
0404 
0405 static int ipcaif_fill_info(struct sk_buff *skb, const struct net_device *dev)
0406 {
0407     struct chnl_net *priv;
0408     u8 loop;
0409     priv = netdev_priv(dev);
0410     if (nla_put_u32(skb, IFLA_CAIF_IPV4_CONNID,
0411             priv->conn_req.sockaddr.u.dgm.connection_id) ||
0412         nla_put_u32(skb, IFLA_CAIF_IPV6_CONNID,
0413             priv->conn_req.sockaddr.u.dgm.connection_id))
0414         goto nla_put_failure;
0415     loop = priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP;
0416     if (nla_put_u8(skb, IFLA_CAIF_LOOPBACK, loop))
0417         goto nla_put_failure;
0418     return 0;
0419 nla_put_failure:
0420     return -EMSGSIZE;
0421 
0422 }
0423 
0424 static void caif_netlink_parms(struct nlattr *data[],
0425                    struct caif_connect_request *conn_req)
0426 {
0427     if (!data) {
0428         pr_warn("no params data found\n");
0429         return;
0430     }
0431     if (data[IFLA_CAIF_IPV4_CONNID])
0432         conn_req->sockaddr.u.dgm.connection_id =
0433             nla_get_u32(data[IFLA_CAIF_IPV4_CONNID]);
0434     if (data[IFLA_CAIF_IPV6_CONNID])
0435         conn_req->sockaddr.u.dgm.connection_id =
0436             nla_get_u32(data[IFLA_CAIF_IPV6_CONNID]);
0437     if (data[IFLA_CAIF_LOOPBACK]) {
0438         if (nla_get_u8(data[IFLA_CAIF_LOOPBACK]))
0439             conn_req->protocol = CAIFPROTO_DATAGRAM_LOOP;
0440         else
0441             conn_req->protocol = CAIFPROTO_DATAGRAM;
0442     }
0443 }
0444 
0445 static int ipcaif_newlink(struct net *src_net, struct net_device *dev,
0446               struct nlattr *tb[], struct nlattr *data[],
0447               struct netlink_ext_ack *extack)
0448 {
0449     int ret;
0450     struct chnl_net *caifdev;
0451     ASSERT_RTNL();
0452     caifdev = netdev_priv(dev);
0453     caif_netlink_parms(data, &caifdev->conn_req);
0454 
0455     ret = register_netdevice(dev);
0456     if (ret)
0457         pr_warn("device rtml registration failed\n");
0458     else
0459         list_add(&caifdev->list_field, &chnl_net_list);
0460 
0461     /* Use ifindex as connection id, and use loopback channel default. */
0462     if (caifdev->conn_req.sockaddr.u.dgm.connection_id == UNDEF_CONNID) {
0463         caifdev->conn_req.sockaddr.u.dgm.connection_id = dev->ifindex;
0464         caifdev->conn_req.protocol = CAIFPROTO_DATAGRAM_LOOP;
0465     }
0466     return ret;
0467 }
0468 
0469 static int ipcaif_changelink(struct net_device *dev, struct nlattr *tb[],
0470                  struct nlattr *data[],
0471                  struct netlink_ext_ack *extack)
0472 {
0473     struct chnl_net *caifdev;
0474     ASSERT_RTNL();
0475     caifdev = netdev_priv(dev);
0476     caif_netlink_parms(data, &caifdev->conn_req);
0477     netdev_state_change(dev);
0478     return 0;
0479 }
0480 
0481 static size_t ipcaif_get_size(const struct net_device *dev)
0482 {
0483     return
0484         /* IFLA_CAIF_IPV4_CONNID */
0485         nla_total_size(4) +
0486         /* IFLA_CAIF_IPV6_CONNID */
0487         nla_total_size(4) +
0488         /* IFLA_CAIF_LOOPBACK */
0489         nla_total_size(2) +
0490         0;
0491 }
0492 
0493 static const struct nla_policy ipcaif_policy[IFLA_CAIF_MAX + 1] = {
0494     [IFLA_CAIF_IPV4_CONNID]       = { .type = NLA_U32 },
0495     [IFLA_CAIF_IPV6_CONNID]       = { .type = NLA_U32 },
0496     [IFLA_CAIF_LOOPBACK]          = { .type = NLA_U8 }
0497 };
0498 
0499 
0500 static struct rtnl_link_ops ipcaif_link_ops __read_mostly = {
0501     .kind       = "caif",
0502     .priv_size  = sizeof(struct chnl_net),
0503     .setup      = ipcaif_net_setup,
0504     .maxtype    = IFLA_CAIF_MAX,
0505     .policy     = ipcaif_policy,
0506     .newlink    = ipcaif_newlink,
0507     .changelink = ipcaif_changelink,
0508     .get_size   = ipcaif_get_size,
0509     .fill_info  = ipcaif_fill_info,
0510 
0511 };
0512 
0513 static int __init chnl_init_module(void)
0514 {
0515     return rtnl_link_register(&ipcaif_link_ops);
0516 }
0517 
0518 static void __exit chnl_exit_module(void)
0519 {
0520     struct chnl_net *dev = NULL;
0521     struct list_head *list_node;
0522     struct list_head *_tmp;
0523     rtnl_link_unregister(&ipcaif_link_ops);
0524     rtnl_lock();
0525     list_for_each_safe(list_node, _tmp, &chnl_net_list) {
0526         dev = list_entry(list_node, struct chnl_net, list_field);
0527         list_del_init(list_node);
0528         delete_device(dev);
0529     }
0530     rtnl_unlock();
0531 }
0532 
0533 module_init(chnl_init_module);
0534 module_exit(chnl_exit_module);