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