chnl_net.c 13 KB

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