chnl_net.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson AB 2010
  4. * Authors: Sjur Brendeland
  5. * Daniel Martensson
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
  8. #include <linux/fs.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/if_ether.h>
  13. #include <linux/ip.h>
  14. #include <linux/sched.h>
  15. #include <linux/sockios.h>
  16. #include <linux/caif/if_caif.h>
  17. #include <net/rtnetlink.h>
  18. #include <net/caif/caif_layer.h>
  19. #include <net/caif/cfpkt.h>
  20. #include <net/caif/caif_dev.h>
  21. /* GPRS PDP connection has MTU to 1500 */
  22. #define GPRS_PDP_MTU 1500
  23. /* 5 sec. connect timeout */
  24. #define CONNECT_TIMEOUT (5 * HZ)
  25. #define CAIF_NET_DEFAULT_QUEUE_LEN 500
  26. #define UNDEF_CONNID 0xffffffff
  27. /*This list is protected by the rtnl lock. */
  28. static LIST_HEAD(chnl_net_list);
  29. MODULE_LICENSE("GPL");
  30. MODULE_ALIAS_RTNL_LINK("caif");
  31. enum caif_states {
  32. CAIF_CONNECTED = 1,
  33. CAIF_CONNECTING,
  34. CAIF_DISCONNECTED,
  35. CAIF_SHUTDOWN
  36. };
  37. struct chnl_net {
  38. struct cflayer chnl;
  39. struct caif_connect_request conn_req;
  40. struct list_head list_field;
  41. struct net_device *netdev;
  42. char name[256];
  43. wait_queue_head_t netmgmt_wq;
  44. /* Flow status to remember and control the transmission. */
  45. bool flowenabled;
  46. enum caif_states state;
  47. };
  48. static int chnl_recv_cb(struct cflayer *layr, struct cfpkt *pkt)
  49. {
  50. struct sk_buff *skb;
  51. struct chnl_net *priv;
  52. int pktlen;
  53. const u8 *ip_version;
  54. u8 buf;
  55. priv = container_of(layr, struct chnl_net, chnl);
  56. if (!priv)
  57. return -EINVAL;
  58. skb = (struct sk_buff *) cfpkt_tonative(pkt);
  59. /* Get length of CAIF packet. */
  60. pktlen = skb->len;
  61. /* Pass some minimum information and
  62. * send the packet to the net stack.
  63. */
  64. skb->dev = priv->netdev;
  65. /* check the version of IP */
  66. ip_version = skb_header_pointer(skb, 0, 1, &buf);
  67. if (!ip_version) {
  68. kfree_skb(skb);
  69. return -EINVAL;
  70. }
  71. switch (*ip_version >> 4) {
  72. case 4:
  73. skb->protocol = htons(ETH_P_IP);
  74. break;
  75. case 6:
  76. skb->protocol = htons(ETH_P_IPV6);
  77. break;
  78. default:
  79. kfree_skb(skb);
  80. priv->netdev->stats.rx_errors++;
  81. return -EINVAL;
  82. }
  83. /* If we change the header in loop mode, the checksum is corrupted. */
  84. if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
  85. skb->ip_summed = CHECKSUM_UNNECESSARY;
  86. else
  87. skb->ip_summed = CHECKSUM_NONE;
  88. if (in_interrupt())
  89. netif_rx(skb);
  90. else
  91. netif_rx_ni(skb);
  92. /* Update statistics. */
  93. priv->netdev->stats.rx_packets++;
  94. priv->netdev->stats.rx_bytes += pktlen;
  95. return 0;
  96. }
  97. static int delete_device(struct chnl_net *dev)
  98. {
  99. ASSERT_RTNL();
  100. if (dev->netdev)
  101. unregister_netdevice(dev->netdev);
  102. return 0;
  103. }
  104. static void close_work(struct work_struct *work)
  105. {
  106. struct chnl_net *dev = NULL;
  107. struct list_head *list_node;
  108. struct list_head *_tmp;
  109. rtnl_lock();
  110. list_for_each_safe(list_node, _tmp, &chnl_net_list) {
  111. dev = list_entry(list_node, struct chnl_net, list_field);
  112. if (dev->state == CAIF_SHUTDOWN)
  113. dev_close(dev->netdev);
  114. }
  115. rtnl_unlock();
  116. }
  117. static DECLARE_WORK(close_worker, close_work);
  118. static void chnl_hold(struct cflayer *lyr)
  119. {
  120. struct chnl_net *priv = container_of(lyr, struct chnl_net, chnl);
  121. dev_hold(priv->netdev);
  122. }
  123. static void chnl_put(struct cflayer *lyr)
  124. {
  125. struct chnl_net *priv = container_of(lyr, struct chnl_net, chnl);
  126. dev_put(priv->netdev);
  127. }
  128. static void chnl_flowctrl_cb(struct cflayer *layr, enum caif_ctrlcmd flow,
  129. int phyid)
  130. {
  131. struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
  132. pr_debug("NET flowctrl func called flow: %s\n",
  133. flow == CAIF_CTRLCMD_FLOW_ON_IND ? "ON" :
  134. flow == CAIF_CTRLCMD_INIT_RSP ? "INIT" :
  135. flow == CAIF_CTRLCMD_FLOW_OFF_IND ? "OFF" :
  136. flow == CAIF_CTRLCMD_DEINIT_RSP ? "CLOSE/DEINIT" :
  137. flow == CAIF_CTRLCMD_INIT_FAIL_RSP ? "OPEN_FAIL" :
  138. flow == CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND ?
  139. "REMOTE_SHUTDOWN" : "UNKNOWN CTRL COMMAND");
  140. switch (flow) {
  141. case CAIF_CTRLCMD_FLOW_OFF_IND:
  142. priv->flowenabled = false;
  143. netif_stop_queue(priv->netdev);
  144. break;
  145. case CAIF_CTRLCMD_DEINIT_RSP:
  146. priv->state = CAIF_DISCONNECTED;
  147. break;
  148. case CAIF_CTRLCMD_INIT_FAIL_RSP:
  149. priv->state = CAIF_DISCONNECTED;
  150. wake_up_interruptible(&priv->netmgmt_wq);
  151. break;
  152. case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
  153. priv->state = CAIF_SHUTDOWN;
  154. netif_tx_disable(priv->netdev);
  155. schedule_work(&close_worker);
  156. break;
  157. case CAIF_CTRLCMD_FLOW_ON_IND:
  158. priv->flowenabled = true;
  159. netif_wake_queue(priv->netdev);
  160. break;
  161. case CAIF_CTRLCMD_INIT_RSP:
  162. caif_client_register_refcnt(&priv->chnl, chnl_hold, chnl_put);
  163. priv->state = CAIF_CONNECTED;
  164. priv->flowenabled = true;
  165. netif_wake_queue(priv->netdev);
  166. wake_up_interruptible(&priv->netmgmt_wq);
  167. break;
  168. default:
  169. break;
  170. }
  171. }
  172. static netdev_tx_t chnl_net_start_xmit(struct sk_buff *skb,
  173. struct net_device *dev)
  174. {
  175. struct chnl_net *priv;
  176. struct cfpkt *pkt = NULL;
  177. int len;
  178. int result = -1;
  179. /* Get our private data. */
  180. priv = netdev_priv(dev);
  181. if (skb->len > priv->netdev->mtu) {
  182. pr_warn("Size of skb exceeded MTU\n");
  183. kfree_skb(skb);
  184. dev->stats.tx_errors++;
  185. return NETDEV_TX_OK;
  186. }
  187. if (!priv->flowenabled) {
  188. pr_debug("dropping packets flow off\n");
  189. kfree_skb(skb);
  190. dev->stats.tx_dropped++;
  191. return NETDEV_TX_OK;
  192. }
  193. if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
  194. swap(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
  195. /* Store original SKB length. */
  196. len = skb->len;
  197. pkt = cfpkt_fromnative(CAIF_DIR_OUT, (void *) skb);
  198. /* Send the packet down the stack. */
  199. result = priv->chnl.dn->transmit(priv->chnl.dn, pkt);
  200. if (result) {
  201. dev->stats.tx_dropped++;
  202. return NETDEV_TX_OK;
  203. }
  204. /* Update statistics. */
  205. dev->stats.tx_packets++;
  206. dev->stats.tx_bytes += len;
  207. return NETDEV_TX_OK;
  208. }
  209. static int chnl_net_open(struct net_device *dev)
  210. {
  211. struct chnl_net *priv = NULL;
  212. int result = -1;
  213. int llifindex, headroom, tailroom, mtu;
  214. struct net_device *lldev;
  215. ASSERT_RTNL();
  216. priv = netdev_priv(dev);
  217. if (!priv) {
  218. pr_debug("chnl_net_open: no priv\n");
  219. return -ENODEV;
  220. }
  221. if (priv->state != CAIF_CONNECTING) {
  222. priv->state = CAIF_CONNECTING;
  223. result = caif_connect_client(dev_net(dev), &priv->conn_req,
  224. &priv->chnl, &llifindex,
  225. &headroom, &tailroom);
  226. if (result != 0) {
  227. pr_debug("err: "
  228. "Unable to register and open device,"
  229. " Err:%d\n",
  230. result);
  231. goto error;
  232. }
  233. lldev = __dev_get_by_index(dev_net(dev), llifindex);
  234. if (lldev == NULL) {
  235. pr_debug("no interface?\n");
  236. result = -ENODEV;
  237. goto error;
  238. }
  239. dev->needed_tailroom = tailroom + lldev->needed_tailroom;
  240. dev->hard_header_len = headroom + lldev->hard_header_len +
  241. lldev->needed_tailroom;
  242. /*
  243. * MTU, head-room etc is not know before we have a
  244. * CAIF link layer device available. MTU calculation may
  245. * override initial RTNL configuration.
  246. * MTU is minimum of current mtu, link layer mtu pluss
  247. * CAIF head and tail, and PDP GPRS contexts max MTU.
  248. */
  249. mtu = min_t(int, dev->mtu, lldev->mtu - (headroom + tailroom));
  250. mtu = min_t(int, GPRS_PDP_MTU, mtu);
  251. dev_set_mtu(dev, mtu);
  252. if (mtu < 100) {
  253. pr_warn("CAIF Interface MTU too small (%d)\n", mtu);
  254. result = -ENODEV;
  255. goto error;
  256. }
  257. }
  258. rtnl_unlock(); /* Release RTNL lock during connect wait */
  259. result = wait_event_interruptible_timeout(priv->netmgmt_wq,
  260. priv->state != CAIF_CONNECTING,
  261. CONNECT_TIMEOUT);
  262. rtnl_lock();
  263. if (result == -ERESTARTSYS) {
  264. pr_debug("wait_event_interruptible woken by a signal\n");
  265. result = -ERESTARTSYS;
  266. goto error;
  267. }
  268. if (result == 0) {
  269. pr_debug("connect timeout\n");
  270. caif_disconnect_client(dev_net(dev), &priv->chnl);
  271. priv->state = CAIF_DISCONNECTED;
  272. pr_debug("state disconnected\n");
  273. result = -ETIMEDOUT;
  274. goto error;
  275. }
  276. if (priv->state != CAIF_CONNECTED) {
  277. pr_debug("connect failed\n");
  278. result = -ECONNREFUSED;
  279. goto error;
  280. }
  281. pr_debug("CAIF Netdevice connected\n");
  282. return 0;
  283. error:
  284. caif_disconnect_client(dev_net(dev), &priv->chnl);
  285. priv->state = CAIF_DISCONNECTED;
  286. pr_debug("state disconnected\n");
  287. return result;
  288. }
  289. static int chnl_net_stop(struct net_device *dev)
  290. {
  291. struct chnl_net *priv;
  292. ASSERT_RTNL();
  293. priv = netdev_priv(dev);
  294. priv->state = CAIF_DISCONNECTED;
  295. caif_disconnect_client(dev_net(dev), &priv->chnl);
  296. return 0;
  297. }
  298. static int chnl_net_init(struct net_device *dev)
  299. {
  300. struct chnl_net *priv;
  301. ASSERT_RTNL();
  302. priv = netdev_priv(dev);
  303. strncpy(priv->name, dev->name, sizeof(priv->name));
  304. INIT_LIST_HEAD(&priv->list_field);
  305. return 0;
  306. }
  307. static void chnl_net_uninit(struct net_device *dev)
  308. {
  309. struct chnl_net *priv;
  310. ASSERT_RTNL();
  311. priv = netdev_priv(dev);
  312. list_del_init(&priv->list_field);
  313. }
  314. static const struct net_device_ops netdev_ops = {
  315. .ndo_open = chnl_net_open,
  316. .ndo_stop = chnl_net_stop,
  317. .ndo_init = chnl_net_init,
  318. .ndo_uninit = chnl_net_uninit,
  319. .ndo_start_xmit = chnl_net_start_xmit,
  320. };
  321. static void chnl_net_destructor(struct net_device *dev)
  322. {
  323. struct chnl_net *priv = netdev_priv(dev);
  324. caif_free_client(&priv->chnl);
  325. }
  326. static void ipcaif_net_setup(struct net_device *dev)
  327. {
  328. struct chnl_net *priv;
  329. dev->netdev_ops = &netdev_ops;
  330. dev->needs_free_netdev = true;
  331. dev->priv_destructor = chnl_net_destructor;
  332. dev->flags |= IFF_NOARP;
  333. dev->flags |= IFF_POINTOPOINT;
  334. dev->mtu = GPRS_PDP_MTU;
  335. dev->tx_queue_len = CAIF_NET_DEFAULT_QUEUE_LEN;
  336. priv = netdev_priv(dev);
  337. priv->chnl.receive = chnl_recv_cb;
  338. priv->chnl.ctrlcmd = chnl_flowctrl_cb;
  339. priv->netdev = dev;
  340. priv->conn_req.protocol = CAIFPROTO_DATAGRAM;
  341. priv->conn_req.link_selector = CAIF_LINK_HIGH_BANDW;
  342. priv->conn_req.priority = CAIF_PRIO_LOW;
  343. /* Insert illegal value */
  344. priv->conn_req.sockaddr.u.dgm.connection_id = UNDEF_CONNID;
  345. priv->flowenabled = false;
  346. init_waitqueue_head(&priv->netmgmt_wq);
  347. }
  348. static int ipcaif_fill_info(struct sk_buff *skb, const struct net_device *dev)
  349. {
  350. struct chnl_net *priv;
  351. u8 loop;
  352. priv = netdev_priv(dev);
  353. if (nla_put_u32(skb, IFLA_CAIF_IPV4_CONNID,
  354. priv->conn_req.sockaddr.u.dgm.connection_id) ||
  355. nla_put_u32(skb, IFLA_CAIF_IPV6_CONNID,
  356. priv->conn_req.sockaddr.u.dgm.connection_id))
  357. goto nla_put_failure;
  358. loop = priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP;
  359. if (nla_put_u8(skb, IFLA_CAIF_LOOPBACK, loop))
  360. goto nla_put_failure;
  361. return 0;
  362. nla_put_failure:
  363. return -EMSGSIZE;
  364. }
  365. static void caif_netlink_parms(struct nlattr *data[],
  366. struct caif_connect_request *conn_req)
  367. {
  368. if (!data) {
  369. pr_warn("no params data found\n");
  370. return;
  371. }
  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;
  381. else
  382. conn_req->protocol = CAIFPROTO_DATAGRAM;
  383. }
  384. }
  385. static int ipcaif_newlink(struct net *src_net, struct net_device *dev,
  386. struct nlattr *tb[], struct nlattr *data[],
  387. struct netlink_ext_ack *extack)
  388. {
  389. int ret;
  390. struct chnl_net *caifdev;
  391. ASSERT_RTNL();
  392. caifdev = netdev_priv(dev);
  393. caif_netlink_parms(data, &caifdev->conn_req);
  394. ret = register_netdevice(dev);
  395. if (ret)
  396. pr_warn("device rtml registration failed\n");
  397. else
  398. list_add(&caifdev->list_field, &chnl_net_list);
  399. /* Use ifindex as connection id, and use loopback channel default. */
  400. if (caifdev->conn_req.sockaddr.u.dgm.connection_id == UNDEF_CONNID) {
  401. caifdev->conn_req.sockaddr.u.dgm.connection_id = dev->ifindex;
  402. caifdev->conn_req.protocol = CAIFPROTO_DATAGRAM_LOOP;
  403. }
  404. return ret;
  405. }
  406. static int ipcaif_changelink(struct net_device *dev, struct nlattr *tb[],
  407. struct nlattr *data[],
  408. struct netlink_ext_ack *extack)
  409. {
  410. struct chnl_net *caifdev;
  411. ASSERT_RTNL();
  412. caifdev = netdev_priv(dev);
  413. caif_netlink_parms(data, &caifdev->conn_req);
  414. netdev_state_change(dev);
  415. return 0;
  416. }
  417. static size_t ipcaif_get_size(const struct net_device *dev)
  418. {
  419. return
  420. /* IFLA_CAIF_IPV4_CONNID */
  421. nla_total_size(4) +
  422. /* IFLA_CAIF_IPV6_CONNID */
  423. nla_total_size(4) +
  424. /* IFLA_CAIF_LOOPBACK */
  425. nla_total_size(2) +
  426. 0;
  427. }
  428. static const struct nla_policy ipcaif_policy[IFLA_CAIF_MAX + 1] = {
  429. [IFLA_CAIF_IPV4_CONNID] = { .type = NLA_U32 },
  430. [IFLA_CAIF_IPV6_CONNID] = { .type = NLA_U32 },
  431. [IFLA_CAIF_LOOPBACK] = { .type = NLA_U8 }
  432. };
  433. static struct rtnl_link_ops ipcaif_link_ops __read_mostly = {
  434. .kind = "caif",
  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,
  443. };
  444. static int __init chnl_init_module(void)
  445. {
  446. return rtnl_link_register(&ipcaif_link_ops);
  447. }
  448. static void __exit chnl_exit_module(void)
  449. {
  450. struct chnl_net *dev = NULL;
  451. struct list_head *list_node;
  452. struct list_head *_tmp;
  453. rtnl_link_unregister(&ipcaif_link_ops);
  454. rtnl_lock();
  455. list_for_each_safe(list_node, _tmp, &chnl_net_list) {
  456. dev = list_entry(list_node, struct chnl_net, list_field);
  457. list_del_init(list_node);
  458. delete_device(dev);
  459. }
  460. rtnl_unlock();
  461. }
  462. module_init(chnl_init_module);
  463. module_exit(chnl_exit_module);