chnl_net.c 12 KB

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