chnl_net.c 12 KB

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