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