NFS: Clean up NFSROOT command line parsing
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / caif / chnl_net.c
blob4293e190ec5328f1da7f40065c9eefcdff5d1f66
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 */
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>
25 /* GPRS PDP connection has MTU to 1500 */
26 #define GPRS_PDP_MTU 1500
27 /* 5 sec. connect timeout */
28 #define CONNECT_TIMEOUT (5 * HZ)
29 #define CAIF_NET_DEFAULT_QUEUE_LEN 500
31 #undef pr_debug
32 #define pr_debug pr_warning
34 /*This list is protected by the rtnl lock. */
35 static LIST_HEAD(chnl_net_list);
37 MODULE_LICENSE("GPL");
38 MODULE_ALIAS_RTNL_LINK("caif");
40 enum caif_states {
41 CAIF_CONNECTED = 1,
42 CAIF_CONNECTING,
43 CAIF_DISCONNECTED,
44 CAIF_SHUTDOWN
47 struct chnl_net {
48 struct cflayer chnl;
49 struct net_device_stats stats;
50 struct caif_connect_request conn_req;
51 struct list_head list_field;
52 struct net_device *netdev;
53 char name[256];
54 wait_queue_head_t netmgmt_wq;
55 /* Flow status to remember and control the transmission. */
56 bool flowenabled;
57 enum caif_states state;
60 static void robust_list_del(struct list_head *delete_node)
62 struct list_head *list_node;
63 struct list_head *n;
64 ASSERT_RTNL();
65 list_for_each_safe(list_node, n, &chnl_net_list) {
66 if (list_node == delete_node) {
67 list_del(list_node);
68 return;
71 WARN_ON(1);
74 static int chnl_recv_cb(struct cflayer *layr, struct cfpkt *pkt)
76 struct sk_buff *skb;
77 struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
78 int pktlen;
79 int err = 0;
81 priv = container_of(layr, struct chnl_net, chnl);
83 if (!priv)
84 return -EINVAL;
86 /* Get length of CAIF packet. */
87 pktlen = cfpkt_getlen(pkt);
89 skb = (struct sk_buff *) cfpkt_tonative(pkt);
90 /* Pass some minimum information and
91 * send the packet to the net stack.
93 skb->dev = priv->netdev;
94 skb->protocol = htons(ETH_P_IP);
96 /* If we change the header in loop mode, the checksum is corrupted. */
97 if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
98 skb->ip_summed = CHECKSUM_UNNECESSARY;
99 else
100 skb->ip_summed = CHECKSUM_NONE;
102 if (in_interrupt())
103 netif_rx(skb);
104 else
105 netif_rx_ni(skb);
107 /* Update statistics. */
108 priv->netdev->stats.rx_packets++;
109 priv->netdev->stats.rx_bytes += pktlen;
111 return err;
114 static int delete_device(struct chnl_net *dev)
116 ASSERT_RTNL();
117 if (dev->netdev)
118 unregister_netdevice(dev->netdev);
119 return 0;
122 static void close_work(struct work_struct *work)
124 struct chnl_net *dev = NULL;
125 struct list_head *list_node;
126 struct list_head *_tmp;
127 /* May be called with or without RTNL lock held */
128 int islocked = rtnl_is_locked();
129 if (!islocked)
130 rtnl_lock();
131 list_for_each_safe(list_node, _tmp, &chnl_net_list) {
132 dev = list_entry(list_node, struct chnl_net, list_field);
133 if (dev->state == CAIF_SHUTDOWN)
134 dev_close(dev->netdev);
136 if (!islocked)
137 rtnl_unlock();
139 static DECLARE_WORK(close_worker, close_work);
141 static void chnl_flowctrl_cb(struct cflayer *layr, enum caif_ctrlcmd flow,
142 int phyid)
144 struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
145 pr_debug("CAIF: %s(): NET flowctrl func called flow: %s\n",
146 __func__,
147 flow == CAIF_CTRLCMD_FLOW_ON_IND ? "ON" :
148 flow == CAIF_CTRLCMD_INIT_RSP ? "INIT" :
149 flow == CAIF_CTRLCMD_FLOW_OFF_IND ? "OFF" :
150 flow == CAIF_CTRLCMD_DEINIT_RSP ? "CLOSE/DEINIT" :
151 flow == CAIF_CTRLCMD_INIT_FAIL_RSP ? "OPEN_FAIL" :
152 flow == CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND ?
153 "REMOTE_SHUTDOWN" : "UKNOWN CTRL COMMAND");
157 switch (flow) {
158 case CAIF_CTRLCMD_FLOW_OFF_IND:
159 priv->flowenabled = false;
160 netif_stop_queue(priv->netdev);
161 break;
162 case CAIF_CTRLCMD_DEINIT_RSP:
163 priv->state = CAIF_DISCONNECTED;
164 break;
165 case CAIF_CTRLCMD_INIT_FAIL_RSP:
166 priv->state = CAIF_DISCONNECTED;
167 wake_up_interruptible(&priv->netmgmt_wq);
168 break;
169 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
170 priv->state = CAIF_SHUTDOWN;
171 netif_tx_disable(priv->netdev);
172 schedule_work(&close_worker);
173 break;
174 case CAIF_CTRLCMD_FLOW_ON_IND:
175 priv->flowenabled = true;
176 netif_wake_queue(priv->netdev);
177 break;
178 case CAIF_CTRLCMD_INIT_RSP:
179 priv->state = CAIF_CONNECTED;
180 priv->flowenabled = true;
181 netif_wake_queue(priv->netdev);
182 wake_up_interruptible(&priv->netmgmt_wq);
183 break;
184 default:
185 break;
189 static int chnl_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
191 struct chnl_net *priv;
192 struct cfpkt *pkt = NULL;
193 int len;
194 int result = -1;
195 /* Get our private data. */
196 priv = netdev_priv(dev);
198 if (skb->len > priv->netdev->mtu) {
199 pr_warning("CAIF: %s(): Size of skb exceeded MTU\n", __func__);
200 return -ENOSPC;
203 if (!priv->flowenabled) {
204 pr_debug("CAIF: %s(): dropping packets flow off\n", __func__);
205 return NETDEV_TX_BUSY;
208 if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
209 swap(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
211 /* Store original SKB length. */
212 len = skb->len;
214 pkt = cfpkt_fromnative(CAIF_DIR_OUT, (void *) skb);
216 /* Send the packet down the stack. */
217 result = priv->chnl.dn->transmit(priv->chnl.dn, pkt);
218 if (result) {
219 if (result == -EAGAIN)
220 result = NETDEV_TX_BUSY;
221 return result;
224 /* Update statistics. */
225 dev->stats.tx_packets++;
226 dev->stats.tx_bytes += len;
228 return NETDEV_TX_OK;
231 static int chnl_net_open(struct net_device *dev)
233 struct chnl_net *priv = NULL;
234 int result = -1;
235 int llifindex, headroom, tailroom, mtu;
236 struct net_device *lldev;
237 ASSERT_RTNL();
238 priv = netdev_priv(dev);
239 if (!priv) {
240 pr_debug("CAIF: %s(): chnl_net_open: no priv\n", __func__);
241 return -ENODEV;
244 if (priv->state != CAIF_CONNECTING) {
245 priv->state = CAIF_CONNECTING;
246 result = caif_connect_client(&priv->conn_req, &priv->chnl,
247 &llifindex, &headroom, &tailroom);
248 if (result != 0) {
249 pr_debug("CAIF: %s(): err: "
250 "Unable to register and open device,"
251 " Err:%d\n",
252 __func__,
253 result);
254 goto error;
257 lldev = dev_get_by_index(dev_net(dev), llifindex);
259 if (lldev == NULL) {
260 pr_debug("CAIF: %s(): no interface?\n", __func__);
261 result = -ENODEV;
262 goto error;
265 dev->needed_tailroom = tailroom + lldev->needed_tailroom;
266 dev->hard_header_len = headroom + lldev->hard_header_len +
267 lldev->needed_tailroom;
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.
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);
281 if (mtu < 100) {
282 pr_warning("CAIF: %s(): "
283 "CAIF Interface MTU too small (%d)\n",
284 __func__, mtu);
285 result = -ENODEV;
286 goto error;
290 rtnl_unlock(); /* Release RTNL lock during connect wait */
292 result = wait_event_interruptible_timeout(priv->netmgmt_wq,
293 priv->state != CAIF_CONNECTING,
294 CONNECT_TIMEOUT);
296 rtnl_lock();
298 if (result == -ERESTARTSYS) {
299 pr_debug("CAIF: %s(): wait_event_interruptible"
300 " woken by a signal\n", __func__);
301 result = -ERESTARTSYS;
302 goto error;
305 if (result == 0) {
306 pr_debug("CAIF: %s(): connect timeout\n", __func__);
307 caif_disconnect_client(&priv->chnl);
308 priv->state = CAIF_DISCONNECTED;
309 pr_debug("CAIF: %s(): state disconnected\n", __func__);
310 result = -ETIMEDOUT;
311 goto error;
314 if (priv->state != CAIF_CONNECTED) {
315 pr_debug("CAIF: %s(): connect failed\n", __func__);
316 result = -ECONNREFUSED;
317 goto error;
319 pr_debug("CAIF: %s(): CAIF Netdevice connected\n", __func__);
320 return 0;
322 error:
323 caif_disconnect_client(&priv->chnl);
324 priv->state = CAIF_DISCONNECTED;
325 pr_debug("CAIF: %s(): state disconnected\n", __func__);
326 return result;
330 static int chnl_net_stop(struct net_device *dev)
332 struct chnl_net *priv;
334 ASSERT_RTNL();
335 priv = netdev_priv(dev);
336 priv->state = CAIF_DISCONNECTED;
337 caif_disconnect_client(&priv->chnl);
338 return 0;
341 static int chnl_net_init(struct net_device *dev)
343 struct chnl_net *priv;
344 ASSERT_RTNL();
345 priv = netdev_priv(dev);
346 strncpy(priv->name, dev->name, sizeof(priv->name));
347 return 0;
350 static void chnl_net_uninit(struct net_device *dev)
352 struct chnl_net *priv;
353 ASSERT_RTNL();
354 priv = netdev_priv(dev);
355 robust_list_del(&priv->list_field);
358 static const struct net_device_ops netdev_ops = {
359 .ndo_open = chnl_net_open,
360 .ndo_stop = chnl_net_stop,
361 .ndo_init = chnl_net_init,
362 .ndo_uninit = chnl_net_uninit,
363 .ndo_start_xmit = chnl_net_start_xmit,
366 static void ipcaif_net_setup(struct net_device *dev)
368 struct chnl_net *priv;
369 dev->netdev_ops = &netdev_ops;
370 dev->destructor = free_netdev;
371 dev->flags |= IFF_NOARP;
372 dev->flags |= IFF_POINTOPOINT;
373 dev->mtu = GPRS_PDP_MTU;
374 dev->tx_queue_len = CAIF_NET_DEFAULT_QUEUE_LEN;
376 priv = netdev_priv(dev);
377 priv->chnl.receive = chnl_recv_cb;
378 priv->chnl.ctrlcmd = chnl_flowctrl_cb;
379 priv->netdev = dev;
380 priv->conn_req.protocol = CAIFPROTO_DATAGRAM;
381 priv->conn_req.link_selector = CAIF_LINK_HIGH_BANDW;
382 priv->conn_req.priority = CAIF_PRIO_LOW;
383 /* Insert illegal value */
384 priv->conn_req.sockaddr.u.dgm.connection_id = -1;
385 priv->flowenabled = false;
387 ASSERT_RTNL();
388 init_waitqueue_head(&priv->netmgmt_wq);
389 list_add(&priv->list_field, &chnl_net_list);
393 static int ipcaif_fill_info(struct sk_buff *skb, const struct net_device *dev)
395 struct chnl_net *priv;
396 u8 loop;
397 priv = netdev_priv(dev);
398 NLA_PUT_U32(skb, IFLA_CAIF_IPV4_CONNID,
399 priv->conn_req.sockaddr.u.dgm.connection_id);
400 NLA_PUT_U32(skb, IFLA_CAIF_IPV6_CONNID,
401 priv->conn_req.sockaddr.u.dgm.connection_id);
402 loop = priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP;
403 NLA_PUT_U8(skb, IFLA_CAIF_LOOPBACK, loop);
406 return 0;
407 nla_put_failure:
408 return -EMSGSIZE;
412 static void caif_netlink_parms(struct nlattr *data[],
413 struct caif_connect_request *conn_req)
415 if (!data) {
416 pr_warning("CAIF: %s: no params data found\n", __func__);
417 return;
419 if (data[IFLA_CAIF_IPV4_CONNID])
420 conn_req->sockaddr.u.dgm.connection_id =
421 nla_get_u32(data[IFLA_CAIF_IPV4_CONNID]);
422 if (data[IFLA_CAIF_IPV6_CONNID])
423 conn_req->sockaddr.u.dgm.connection_id =
424 nla_get_u32(data[IFLA_CAIF_IPV6_CONNID]);
425 if (data[IFLA_CAIF_LOOPBACK]) {
426 if (nla_get_u8(data[IFLA_CAIF_LOOPBACK]))
427 conn_req->protocol = CAIFPROTO_DATAGRAM_LOOP;
428 else
429 conn_req->protocol = CAIFPROTO_DATAGRAM;
433 static int ipcaif_newlink(struct net *src_net, struct net_device *dev,
434 struct nlattr *tb[], struct nlattr *data[])
436 int ret;
437 struct chnl_net *caifdev;
438 ASSERT_RTNL();
439 caifdev = netdev_priv(dev);
440 caif_netlink_parms(data, &caifdev->conn_req);
441 dev_net_set(caifdev->netdev, src_net);
443 ret = register_netdevice(dev);
444 if (ret)
445 pr_warning("CAIF: %s(): device rtml registration failed\n",
446 __func__);
447 return ret;
450 static int ipcaif_changelink(struct net_device *dev, struct nlattr *tb[],
451 struct nlattr *data[])
453 struct chnl_net *caifdev;
454 ASSERT_RTNL();
455 caifdev = netdev_priv(dev);
456 caif_netlink_parms(data, &caifdev->conn_req);
457 netdev_state_change(dev);
458 return 0;
461 static size_t ipcaif_get_size(const struct net_device *dev)
463 return
464 /* IFLA_CAIF_IPV4_CONNID */
465 nla_total_size(4) +
466 /* IFLA_CAIF_IPV6_CONNID */
467 nla_total_size(4) +
468 /* IFLA_CAIF_LOOPBACK */
469 nla_total_size(2) +
473 static const struct nla_policy ipcaif_policy[IFLA_CAIF_MAX + 1] = {
474 [IFLA_CAIF_IPV4_CONNID] = { .type = NLA_U32 },
475 [IFLA_CAIF_IPV6_CONNID] = { .type = NLA_U32 },
476 [IFLA_CAIF_LOOPBACK] = { .type = NLA_U8 }
480 static struct rtnl_link_ops ipcaif_link_ops __read_mostly = {
481 .kind = "caif",
482 .priv_size = sizeof(struct chnl_net),
483 .setup = ipcaif_net_setup,
484 .maxtype = IFLA_CAIF_MAX,
485 .policy = ipcaif_policy,
486 .newlink = ipcaif_newlink,
487 .changelink = ipcaif_changelink,
488 .get_size = ipcaif_get_size,
489 .fill_info = ipcaif_fill_info,
493 static int __init chnl_init_module(void)
495 return rtnl_link_register(&ipcaif_link_ops);
498 static void __exit chnl_exit_module(void)
500 struct chnl_net *dev = NULL;
501 struct list_head *list_node;
502 struct list_head *_tmp;
503 rtnl_link_unregister(&ipcaif_link_ops);
504 rtnl_lock();
505 list_for_each_safe(list_node, _tmp, &chnl_net_list) {
506 dev = list_entry(list_node, struct chnl_net, list_field);
507 list_del(list_node);
508 delete_device(dev);
510 rtnl_unlock();
513 module_init(chnl_init_module);
514 module_exit(chnl_exit_module);