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