chnl_net.c 13 KB

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