chnl_net.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. #undef pr_debug
  30. #define pr_debug pr_warn
  31. /*This list is protected by the rtnl lock. */
  32. static LIST_HEAD(chnl_net_list);
  33. MODULE_LICENSE("GPL");
  34. MODULE_ALIAS_RTNL_LINK("caif");
  35. enum caif_states {
  36. CAIF_CONNECTED = 1,
  37. CAIF_CONNECTING,
  38. CAIF_DISCONNECTED,
  39. CAIF_SHUTDOWN
  40. };
  41. struct chnl_net {
  42. struct cflayer chnl;
  43. struct net_device_stats stats;
  44. struct caif_connect_request conn_req;
  45. struct list_head list_field;
  46. struct net_device *netdev;
  47. char name[256];
  48. wait_queue_head_t netmgmt_wq;
  49. /* Flow status to remember and control the transmission. */
  50. bool flowenabled;
  51. enum caif_states state;
  52. };
  53. static void robust_list_del(struct list_head *delete_node)
  54. {
  55. struct list_head *list_node;
  56. struct list_head *n;
  57. ASSERT_RTNL();
  58. list_for_each_safe(list_node, n, &chnl_net_list) {
  59. if (list_node == delete_node) {
  60. list_del(list_node);
  61. return;
  62. }
  63. }
  64. WARN_ON(1);
  65. }
  66. static int chnl_recv_cb(struct cflayer *layr, struct cfpkt *pkt)
  67. {
  68. struct sk_buff *skb;
  69. struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
  70. int pktlen;
  71. int err = 0;
  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. skb->protocol = htons(ETH_P_IP);
  83. /* If we change the header in loop mode, the checksum is corrupted. */
  84. if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
  85. skb->ip_summed = CHECKSUM_UNNECESSARY;
  86. else
  87. skb->ip_summed = CHECKSUM_NONE;
  88. if (in_interrupt())
  89. netif_rx(skb);
  90. else
  91. netif_rx_ni(skb);
  92. /* Update statistics. */
  93. priv->netdev->stats.rx_packets++;
  94. priv->netdev->stats.rx_bytes += pktlen;
  95. return err;
  96. }
  97. static int delete_device(struct chnl_net *dev)
  98. {
  99. ASSERT_RTNL();
  100. if (dev->netdev)
  101. unregister_netdevice(dev->netdev);
  102. return 0;
  103. }
  104. static void close_work(struct work_struct *work)
  105. {
  106. struct chnl_net *dev = NULL;
  107. struct list_head *list_node;
  108. struct list_head *_tmp;
  109. /* May be called with or without RTNL lock held */
  110. int islocked = rtnl_is_locked();
  111. if (!islocked)
  112. rtnl_lock();
  113. list_for_each_safe(list_node, _tmp, &chnl_net_list) {
  114. dev = list_entry(list_node, struct chnl_net, list_field);
  115. if (dev->state == CAIF_SHUTDOWN)
  116. dev_close(dev->netdev);
  117. }
  118. if (!islocked)
  119. rtnl_unlock();
  120. }
  121. static DECLARE_WORK(close_worker, close_work);
  122. static void chnl_flowctrl_cb(struct cflayer *layr, enum caif_ctrlcmd flow,
  123. int phyid)
  124. {
  125. struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
  126. pr_debug("NET flowctrl func called flow: %s\n",
  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_warn("Size of skb exceeded MTU\n");
  175. return -ENOSPC;
  176. }
  177. if (!priv->flowenabled) {
  178. pr_debug("dropping packets flow off\n");
  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. int llifindex, headroom, tailroom, mtu;
  203. struct net_device *lldev;
  204. ASSERT_RTNL();
  205. priv = netdev_priv(dev);
  206. if (!priv) {
  207. pr_debug("chnl_net_open: no priv\n");
  208. return -ENODEV;
  209. }
  210. if (priv->state != CAIF_CONNECTING) {
  211. priv->state = CAIF_CONNECTING;
  212. result = caif_connect_client(&priv->conn_req, &priv->chnl,
  213. &llifindex, &headroom, &tailroom);
  214. if (result != 0) {
  215. pr_debug("err: "
  216. "Unable to register and open device,"
  217. " Err:%d\n",
  218. result);
  219. goto error;
  220. }
  221. lldev = dev_get_by_index(dev_net(dev), llifindex);
  222. if (lldev == NULL) {
  223. pr_debug("no interface?\n");
  224. result = -ENODEV;
  225. goto error;
  226. }
  227. dev->needed_tailroom = tailroom + lldev->needed_tailroom;
  228. dev->hard_header_len = headroom + lldev->hard_header_len +
  229. lldev->needed_tailroom;
  230. /*
  231. * MTU, head-room etc is not know before we have a
  232. * CAIF link layer device available. MTU calculation may
  233. * override initial RTNL configuration.
  234. * MTU is minimum of current mtu, link layer mtu pluss
  235. * CAIF head and tail, and PDP GPRS contexts max MTU.
  236. */
  237. mtu = min_t(int, dev->mtu, lldev->mtu - (headroom + tailroom));
  238. mtu = min_t(int, GPRS_PDP_MTU, mtu);
  239. dev_set_mtu(dev, mtu);
  240. dev_put(lldev);
  241. if (mtu < 100) {
  242. pr_warn("CAIF Interface MTU too small (%d)\n", mtu);
  243. result = -ENODEV;
  244. goto error;
  245. }
  246. }
  247. rtnl_unlock(); /* Release RTNL lock during connect wait */
  248. result = wait_event_interruptible_timeout(priv->netmgmt_wq,
  249. priv->state != CAIF_CONNECTING,
  250. CONNECT_TIMEOUT);
  251. rtnl_lock();
  252. if (result == -ERESTARTSYS) {
  253. pr_debug("wait_event_interruptible woken by a signal\n");
  254. result = -ERESTARTSYS;
  255. goto error;
  256. }
  257. if (result == 0) {
  258. pr_debug("connect timeout\n");
  259. caif_disconnect_client(&priv->chnl);
  260. priv->state = CAIF_DISCONNECTED;
  261. pr_debug("state disconnected\n");
  262. result = -ETIMEDOUT;
  263. goto error;
  264. }
  265. if (priv->state != CAIF_CONNECTED) {
  266. pr_debug("connect failed\n");
  267. result = -ECONNREFUSED;
  268. goto error;
  269. }
  270. pr_debug("CAIF Netdevice connected\n");
  271. return 0;
  272. error:
  273. caif_disconnect_client(&priv->chnl);
  274. priv->state = CAIF_DISCONNECTED;
  275. pr_debug("state disconnected\n");
  276. return result;
  277. }
  278. static int chnl_net_stop(struct net_device *dev)
  279. {
  280. struct chnl_net *priv;
  281. ASSERT_RTNL();
  282. priv = netdev_priv(dev);
  283. priv->state = CAIF_DISCONNECTED;
  284. caif_disconnect_client(&priv->chnl);
  285. return 0;
  286. }
  287. static int chnl_net_init(struct net_device *dev)
  288. {
  289. struct chnl_net *priv;
  290. ASSERT_RTNL();
  291. priv = netdev_priv(dev);
  292. strncpy(priv->name, dev->name, sizeof(priv->name));
  293. return 0;
  294. }
  295. static void chnl_net_uninit(struct net_device *dev)
  296. {
  297. struct chnl_net *priv;
  298. ASSERT_RTNL();
  299. priv = netdev_priv(dev);
  300. robust_list_del(&priv->list_field);
  301. }
  302. static const struct net_device_ops netdev_ops = {
  303. .ndo_open = chnl_net_open,
  304. .ndo_stop = chnl_net_stop,
  305. .ndo_init = chnl_net_init,
  306. .ndo_uninit = chnl_net_uninit,
  307. .ndo_start_xmit = chnl_net_start_xmit,
  308. };
  309. static void ipcaif_net_setup(struct net_device *dev)
  310. {
  311. struct chnl_net *priv;
  312. dev->netdev_ops = &netdev_ops;
  313. dev->destructor = free_netdev;
  314. dev->flags |= IFF_NOARP;
  315. dev->flags |= IFF_POINTOPOINT;
  316. dev->mtu = GPRS_PDP_MTU;
  317. dev->tx_queue_len = CAIF_NET_DEFAULT_QUEUE_LEN;
  318. priv = netdev_priv(dev);
  319. priv->chnl.receive = chnl_recv_cb;
  320. priv->chnl.ctrlcmd = chnl_flowctrl_cb;
  321. priv->netdev = dev;
  322. priv->conn_req.protocol = CAIFPROTO_DATAGRAM;
  323. priv->conn_req.link_selector = CAIF_LINK_HIGH_BANDW;
  324. priv->conn_req.priority = CAIF_PRIO_LOW;
  325. /* Insert illegal value */
  326. priv->conn_req.sockaddr.u.dgm.connection_id = -1;
  327. priv->flowenabled = false;
  328. ASSERT_RTNL();
  329. init_waitqueue_head(&priv->netmgmt_wq);
  330. list_add(&priv->list_field, &chnl_net_list);
  331. }
  332. static int ipcaif_fill_info(struct sk_buff *skb, const struct net_device *dev)
  333. {
  334. struct chnl_net *priv;
  335. u8 loop;
  336. priv = netdev_priv(dev);
  337. NLA_PUT_U32(skb, IFLA_CAIF_IPV4_CONNID,
  338. priv->conn_req.sockaddr.u.dgm.connection_id);
  339. NLA_PUT_U32(skb, IFLA_CAIF_IPV6_CONNID,
  340. priv->conn_req.sockaddr.u.dgm.connection_id);
  341. loop = priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP;
  342. NLA_PUT_U8(skb, IFLA_CAIF_LOOPBACK, loop);
  343. return 0;
  344. nla_put_failure:
  345. return -EMSGSIZE;
  346. }
  347. static void caif_netlink_parms(struct nlattr *data[],
  348. struct caif_connect_request *conn_req)
  349. {
  350. if (!data) {
  351. pr_warn("no params data found\n");
  352. return;
  353. }
  354. if (data[IFLA_CAIF_IPV4_CONNID])
  355. conn_req->sockaddr.u.dgm.connection_id =
  356. nla_get_u32(data[IFLA_CAIF_IPV4_CONNID]);
  357. if (data[IFLA_CAIF_IPV6_CONNID])
  358. conn_req->sockaddr.u.dgm.connection_id =
  359. nla_get_u32(data[IFLA_CAIF_IPV6_CONNID]);
  360. if (data[IFLA_CAIF_LOOPBACK]) {
  361. if (nla_get_u8(data[IFLA_CAIF_LOOPBACK]))
  362. conn_req->protocol = CAIFPROTO_DATAGRAM_LOOP;
  363. else
  364. conn_req->protocol = CAIFPROTO_DATAGRAM;
  365. }
  366. }
  367. static int ipcaif_newlink(struct net *src_net, struct net_device *dev,
  368. struct nlattr *tb[], struct nlattr *data[])
  369. {
  370. int ret;
  371. struct chnl_net *caifdev;
  372. ASSERT_RTNL();
  373. caifdev = netdev_priv(dev);
  374. caif_netlink_parms(data, &caifdev->conn_req);
  375. dev_net_set(caifdev->netdev, src_net);
  376. ret = register_netdevice(dev);
  377. if (ret)
  378. pr_warn("device rtml registration failed\n");
  379. return ret;
  380. }
  381. static int ipcaif_changelink(struct net_device *dev, struct nlattr *tb[],
  382. struct nlattr *data[])
  383. {
  384. struct chnl_net *caifdev;
  385. ASSERT_RTNL();
  386. caifdev = netdev_priv(dev);
  387. caif_netlink_parms(data, &caifdev->conn_req);
  388. netdev_state_change(dev);
  389. return 0;
  390. }
  391. static size_t ipcaif_get_size(const struct net_device *dev)
  392. {
  393. return
  394. /* IFLA_CAIF_IPV4_CONNID */
  395. nla_total_size(4) +
  396. /* IFLA_CAIF_IPV6_CONNID */
  397. nla_total_size(4) +
  398. /* IFLA_CAIF_LOOPBACK */
  399. nla_total_size(2) +
  400. 0;
  401. }
  402. static const struct nla_policy ipcaif_policy[IFLA_CAIF_MAX + 1] = {
  403. [IFLA_CAIF_IPV4_CONNID] = { .type = NLA_U32 },
  404. [IFLA_CAIF_IPV6_CONNID] = { .type = NLA_U32 },
  405. [IFLA_CAIF_LOOPBACK] = { .type = NLA_U8 }
  406. };
  407. static struct rtnl_link_ops ipcaif_link_ops __read_mostly = {
  408. .kind = "caif",
  409. .priv_size = sizeof(struct chnl_net),
  410. .setup = ipcaif_net_setup,
  411. .maxtype = IFLA_CAIF_MAX,
  412. .policy = ipcaif_policy,
  413. .newlink = ipcaif_newlink,
  414. .changelink = ipcaif_changelink,
  415. .get_size = ipcaif_get_size,
  416. .fill_info = ipcaif_fill_info,
  417. };
  418. static int __init chnl_init_module(void)
  419. {
  420. return rtnl_link_register(&ipcaif_link_ops);
  421. }
  422. static void __exit chnl_exit_module(void)
  423. {
  424. struct chnl_net *dev = NULL;
  425. struct list_head *list_node;
  426. struct list_head *_tmp;
  427. rtnl_link_unregister(&ipcaif_link_ops);
  428. rtnl_lock();
  429. list_for_each_safe(list_node, _tmp, &chnl_net_list) {
  430. dev = list_entry(list_node, struct chnl_net, list_field);
  431. list_del(list_node);
  432. delete_device(dev);
  433. }
  434. rtnl_unlock();
  435. }
  436. module_init(chnl_init_module);
  437. module_exit(chnl_exit_module);