chnl_net.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #include <linux/version.h>
  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/moduleparam.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/cfcnfg.h>
  21. #include <net/caif/cfpkt.h>
  22. #include <net/caif/caif_dev.h>
  23. /* GPRS PDP connection has MTU to 1500 */
  24. #define SIZE_MTU 1500
  25. /* 5 sec. connect timeout */
  26. #define CONNECT_TIMEOUT (5 * HZ)
  27. #define CAIF_NET_DEFAULT_QUEUE_LEN 500
  28. #undef pr_debug
  29. #define pr_debug pr_warning
  30. /*This list is protected by the rtnl lock. */
  31. static LIST_HEAD(chnl_net_list);
  32. MODULE_LICENSE("GPL");
  33. MODULE_ALIAS_RTNL_LINK("caif");
  34. enum caif_states {
  35. CAIF_CONNECTED = 1,
  36. CAIF_CONNECTING,
  37. CAIF_DISCONNECTED,
  38. CAIF_SHUTDOWN
  39. };
  40. struct chnl_net {
  41. struct cflayer chnl;
  42. struct net_device_stats stats;
  43. struct caif_connect_request conn_req;
  44. struct list_head list_field;
  45. struct net_device *netdev;
  46. char name[256];
  47. wait_queue_head_t netmgmt_wq;
  48. /* Flow status to remember and control the transmission. */
  49. bool flowenabled;
  50. enum caif_states state;
  51. };
  52. static void robust_list_del(struct list_head *delete_node)
  53. {
  54. struct list_head *list_node;
  55. struct list_head *n;
  56. ASSERT_RTNL();
  57. list_for_each_safe(list_node, n, &chnl_net_list) {
  58. if (list_node == delete_node) {
  59. list_del(list_node);
  60. return;
  61. }
  62. }
  63. WARN_ON(1);
  64. }
  65. static int chnl_recv_cb(struct cflayer *layr, struct cfpkt *pkt)
  66. {
  67. struct sk_buff *skb;
  68. struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
  69. int pktlen;
  70. int err = 0;
  71. priv = container_of(layr, struct chnl_net, chnl);
  72. if (!priv)
  73. return -EINVAL;
  74. /* Get length of CAIF packet. */
  75. pktlen = cfpkt_getlen(pkt);
  76. skb = (struct sk_buff *) cfpkt_tonative(pkt);
  77. /* Pass some minimum information and
  78. * send the packet to the net stack.
  79. */
  80. skb->dev = priv->netdev;
  81. skb->protocol = htons(ETH_P_IP);
  82. /* If we change the header in loop mode, the checksum is corrupted. */
  83. if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
  84. skb->ip_summed = CHECKSUM_UNNECESSARY;
  85. else
  86. skb->ip_summed = CHECKSUM_NONE;
  87. if (in_interrupt())
  88. netif_rx(skb);
  89. else
  90. netif_rx_ni(skb);
  91. /* Update statistics. */
  92. priv->netdev->stats.rx_packets++;
  93. priv->netdev->stats.rx_bytes += pktlen;
  94. return err;
  95. }
  96. static int delete_device(struct chnl_net *dev)
  97. {
  98. ASSERT_RTNL();
  99. if (dev->netdev)
  100. unregister_netdevice(dev->netdev);
  101. return 0;
  102. }
  103. static void close_work(struct work_struct *work)
  104. {
  105. struct chnl_net *dev = NULL;
  106. struct list_head *list_node;
  107. struct list_head *_tmp;
  108. /* May be called with or without RTNL lock held */
  109. int islocked = rtnl_is_locked();
  110. if (!islocked)
  111. rtnl_lock();
  112. list_for_each_safe(list_node, _tmp, &chnl_net_list) {
  113. dev = list_entry(list_node, struct chnl_net, list_field);
  114. if (dev->state == CAIF_SHUTDOWN)
  115. dev_close(dev->netdev);
  116. }
  117. if (!islocked)
  118. rtnl_unlock();
  119. }
  120. static DECLARE_WORK(close_worker, close_work);
  121. static void chnl_flowctrl_cb(struct cflayer *layr, enum caif_ctrlcmd flow,
  122. int phyid)
  123. {
  124. struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
  125. pr_debug("CAIF: %s(): NET flowctrl func called flow: %s\n",
  126. __func__,
  127. flow == CAIF_CTRLCMD_FLOW_ON_IND ? "ON" :
  128. flow == CAIF_CTRLCMD_INIT_RSP ? "INIT" :
  129. flow == CAIF_CTRLCMD_FLOW_OFF_IND ? "OFF" :
  130. flow == CAIF_CTRLCMD_DEINIT_RSP ? "CLOSE/DEINIT" :
  131. flow == CAIF_CTRLCMD_INIT_FAIL_RSP ? "OPEN_FAIL" :
  132. flow == CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND ?
  133. "REMOTE_SHUTDOWN" : "UKNOWN CTRL COMMAND");
  134. switch (flow) {
  135. case CAIF_CTRLCMD_FLOW_OFF_IND:
  136. priv->flowenabled = false;
  137. netif_stop_queue(priv->netdev);
  138. break;
  139. case CAIF_CTRLCMD_DEINIT_RSP:
  140. priv->state = CAIF_DISCONNECTED;
  141. break;
  142. case CAIF_CTRLCMD_INIT_FAIL_RSP:
  143. priv->state = CAIF_DISCONNECTED;
  144. wake_up_interruptible(&priv->netmgmt_wq);
  145. break;
  146. case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
  147. priv->state = CAIF_SHUTDOWN;
  148. netif_tx_disable(priv->netdev);
  149. schedule_work(&close_worker);
  150. break;
  151. case CAIF_CTRLCMD_FLOW_ON_IND:
  152. priv->flowenabled = true;
  153. netif_wake_queue(priv->netdev);
  154. break;
  155. case CAIF_CTRLCMD_INIT_RSP:
  156. priv->state = CAIF_CONNECTED;
  157. priv->flowenabled = true;
  158. netif_wake_queue(priv->netdev);
  159. wake_up_interruptible(&priv->netmgmt_wq);
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. static int chnl_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  166. {
  167. struct chnl_net *priv;
  168. struct cfpkt *pkt = NULL;
  169. int len;
  170. int result = -1;
  171. /* Get our private data. */
  172. priv = netdev_priv(dev);
  173. if (skb->len > priv->netdev->mtu) {
  174. pr_warning("CAIF: %s(): Size of skb exceeded MTU\n", __func__);
  175. return -ENOSPC;
  176. }
  177. if (!priv->flowenabled) {
  178. pr_debug("CAIF: %s(): dropping packets flow off\n", __func__);
  179. return NETDEV_TX_BUSY;
  180. }
  181. if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
  182. swap(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
  183. /* Store original SKB length. */
  184. len = skb->len;
  185. pkt = cfpkt_fromnative(CAIF_DIR_OUT, (void *) skb);
  186. /* Send the packet down the stack. */
  187. result = priv->chnl.dn->transmit(priv->chnl.dn, pkt);
  188. if (result) {
  189. if (result == -EAGAIN)
  190. result = NETDEV_TX_BUSY;
  191. return result;
  192. }
  193. /* Update statistics. */
  194. dev->stats.tx_packets++;
  195. dev->stats.tx_bytes += len;
  196. return NETDEV_TX_OK;
  197. }
  198. static int chnl_net_open(struct net_device *dev)
  199. {
  200. struct chnl_net *priv = NULL;
  201. int result = -1;
  202. ASSERT_RTNL();
  203. priv = netdev_priv(dev);
  204. if (!priv) {
  205. pr_debug("CAIF: %s(): chnl_net_open: no priv\n", __func__);
  206. return -ENODEV;
  207. }
  208. if (priv->state != CAIF_CONNECTING) {
  209. priv->state = CAIF_CONNECTING;
  210. result = caif_connect_client(&priv->conn_req, &priv->chnl);
  211. if (result != 0) {
  212. priv->state = CAIF_DISCONNECTED;
  213. pr_debug("CAIF: %s(): err: "
  214. "Unable to register and open device,"
  215. " Err:%d\n",
  216. __func__,
  217. result);
  218. return result;
  219. }
  220. }
  221. result = wait_event_interruptible_timeout(priv->netmgmt_wq,
  222. priv->state != CAIF_CONNECTING,
  223. CONNECT_TIMEOUT);
  224. if (result == -ERESTARTSYS) {
  225. pr_debug("CAIF: %s(): wait_event_interruptible"
  226. " woken by a signal\n", __func__);
  227. return -ERESTARTSYS;
  228. }
  229. if (result == 0) {
  230. pr_debug("CAIF: %s(): connect timeout\n", __func__);
  231. caif_disconnect_client(&priv->chnl);
  232. priv->state = CAIF_DISCONNECTED;
  233. pr_debug("CAIF: %s(): state disconnected\n", __func__);
  234. return -ETIMEDOUT;
  235. }
  236. if (priv->state != CAIF_CONNECTED) {
  237. pr_debug("CAIF: %s(): connect failed\n", __func__);
  238. return -ECONNREFUSED;
  239. }
  240. pr_debug("CAIF: %s(): CAIF Netdevice connected\n", __func__);
  241. return 0;
  242. }
  243. static int chnl_net_stop(struct net_device *dev)
  244. {
  245. struct chnl_net *priv;
  246. ASSERT_RTNL();
  247. priv = netdev_priv(dev);
  248. priv->state = CAIF_DISCONNECTED;
  249. caif_disconnect_client(&priv->chnl);
  250. return 0;
  251. }
  252. static int chnl_net_init(struct net_device *dev)
  253. {
  254. struct chnl_net *priv;
  255. ASSERT_RTNL();
  256. priv = netdev_priv(dev);
  257. strncpy(priv->name, dev->name, sizeof(priv->name));
  258. return 0;
  259. }
  260. static void chnl_net_uninit(struct net_device *dev)
  261. {
  262. struct chnl_net *priv;
  263. ASSERT_RTNL();
  264. priv = netdev_priv(dev);
  265. robust_list_del(&priv->list_field);
  266. }
  267. static const struct net_device_ops netdev_ops = {
  268. .ndo_open = chnl_net_open,
  269. .ndo_stop = chnl_net_stop,
  270. .ndo_init = chnl_net_init,
  271. .ndo_uninit = chnl_net_uninit,
  272. .ndo_start_xmit = chnl_net_start_xmit,
  273. };
  274. static void ipcaif_net_setup(struct net_device *dev)
  275. {
  276. struct chnl_net *priv;
  277. dev->netdev_ops = &netdev_ops;
  278. dev->destructor = free_netdev;
  279. dev->flags |= IFF_NOARP;
  280. dev->flags |= IFF_POINTOPOINT;
  281. dev->needed_headroom = CAIF_NEEDED_HEADROOM;
  282. dev->needed_tailroom = CAIF_NEEDED_TAILROOM;
  283. dev->mtu = SIZE_MTU;
  284. dev->tx_queue_len = CAIF_NET_DEFAULT_QUEUE_LEN;
  285. priv = netdev_priv(dev);
  286. priv->chnl.receive = chnl_recv_cb;
  287. priv->chnl.ctrlcmd = chnl_flowctrl_cb;
  288. priv->netdev = dev;
  289. priv->conn_req.protocol = CAIFPROTO_DATAGRAM;
  290. priv->conn_req.link_selector = CAIF_LINK_HIGH_BANDW;
  291. priv->conn_req.priority = CAIF_PRIO_LOW;
  292. /* Insert illegal value */
  293. priv->conn_req.sockaddr.u.dgm.connection_id = -1;
  294. priv->flowenabled = false;
  295. ASSERT_RTNL();
  296. init_waitqueue_head(&priv->netmgmt_wq);
  297. list_add(&priv->list_field, &chnl_net_list);
  298. }
  299. static int ipcaif_fill_info(struct sk_buff *skb, const struct net_device *dev)
  300. {
  301. struct chnl_net *priv;
  302. u8 loop;
  303. priv = netdev_priv(dev);
  304. NLA_PUT_U32(skb, IFLA_CAIF_IPV4_CONNID,
  305. priv->conn_req.sockaddr.u.dgm.connection_id);
  306. NLA_PUT_U32(skb, IFLA_CAIF_IPV6_CONNID,
  307. priv->conn_req.sockaddr.u.dgm.connection_id);
  308. loop = priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP;
  309. NLA_PUT_U8(skb, IFLA_CAIF_LOOPBACK, loop);
  310. return 0;
  311. nla_put_failure:
  312. return -EMSGSIZE;
  313. }
  314. static void caif_netlink_parms(struct nlattr *data[],
  315. struct caif_connect_request *conn_req)
  316. {
  317. if (!data) {
  318. pr_warning("CAIF: %s: no params data found\n", __func__);
  319. return;
  320. }
  321. if (data[IFLA_CAIF_IPV4_CONNID])
  322. conn_req->sockaddr.u.dgm.connection_id =
  323. nla_get_u32(data[IFLA_CAIF_IPV4_CONNID]);
  324. if (data[IFLA_CAIF_IPV6_CONNID])
  325. conn_req->sockaddr.u.dgm.connection_id =
  326. nla_get_u32(data[IFLA_CAIF_IPV6_CONNID]);
  327. if (data[IFLA_CAIF_LOOPBACK]) {
  328. if (nla_get_u8(data[IFLA_CAIF_LOOPBACK]))
  329. conn_req->protocol = CAIFPROTO_DATAGRAM_LOOP;
  330. else
  331. conn_req->protocol = CAIFPROTO_DATAGRAM;
  332. }
  333. }
  334. static int ipcaif_newlink(struct net *src_net, struct net_device *dev,
  335. struct nlattr *tb[], struct nlattr *data[])
  336. {
  337. int ret;
  338. struct chnl_net *caifdev;
  339. ASSERT_RTNL();
  340. caifdev = netdev_priv(dev);
  341. caif_netlink_parms(data, &caifdev->conn_req);
  342. dev_net_set(caifdev->netdev, src_net);
  343. ret = register_netdevice(dev);
  344. if (ret)
  345. pr_warning("CAIF: %s(): device rtml registration failed\n",
  346. __func__);
  347. return ret;
  348. }
  349. static int ipcaif_changelink(struct net_device *dev, struct nlattr *tb[],
  350. struct nlattr *data[])
  351. {
  352. struct chnl_net *caifdev;
  353. ASSERT_RTNL();
  354. caifdev = netdev_priv(dev);
  355. caif_netlink_parms(data, &caifdev->conn_req);
  356. netdev_state_change(dev);
  357. return 0;
  358. }
  359. static size_t ipcaif_get_size(const struct net_device *dev)
  360. {
  361. return
  362. /* IFLA_CAIF_IPV4_CONNID */
  363. nla_total_size(4) +
  364. /* IFLA_CAIF_IPV6_CONNID */
  365. nla_total_size(4) +
  366. /* IFLA_CAIF_LOOPBACK */
  367. nla_total_size(2) +
  368. 0;
  369. }
  370. static const struct nla_policy ipcaif_policy[IFLA_CAIF_MAX + 1] = {
  371. [IFLA_CAIF_IPV4_CONNID] = { .type = NLA_U32 },
  372. [IFLA_CAIF_IPV6_CONNID] = { .type = NLA_U32 },
  373. [IFLA_CAIF_LOOPBACK] = { .type = NLA_U8 }
  374. };
  375. static struct rtnl_link_ops ipcaif_link_ops __read_mostly = {
  376. .kind = "caif",
  377. .priv_size = sizeof(struct chnl_net),
  378. .setup = ipcaif_net_setup,
  379. .maxtype = IFLA_CAIF_MAX,
  380. .policy = ipcaif_policy,
  381. .newlink = ipcaif_newlink,
  382. .changelink = ipcaif_changelink,
  383. .get_size = ipcaif_get_size,
  384. .fill_info = ipcaif_fill_info,
  385. };
  386. static int __init chnl_init_module(void)
  387. {
  388. return rtnl_link_register(&ipcaif_link_ops);
  389. }
  390. static void __exit chnl_exit_module(void)
  391. {
  392. struct chnl_net *dev = NULL;
  393. struct list_head *list_node;
  394. struct list_head *_tmp;
  395. rtnl_link_unregister(&ipcaif_link_ops);
  396. rtnl_lock();
  397. list_for_each_safe(list_node, _tmp, &chnl_net_list) {
  398. dev = list_entry(list_node, struct chnl_net, list_field);
  399. list_del(list_node);
  400. delete_device(dev);
  401. }
  402. rtnl_unlock();
  403. }
  404. module_init(chnl_init_module);
  405. module_exit(chnl_exit_module);