chnl_net.c 13 KB

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