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
8 #include <linux/version.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>
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
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");
49 struct net_device_stats stats
;
50 struct caif_connect_request conn_req
;
51 struct list_head list_field
;
52 struct net_device
*netdev
;
54 wait_queue_head_t netmgmt_wq
;
55 /* Flow status to remember and control the transmission. */
57 enum caif_states state
;
60 static void robust_list_del(struct list_head
*delete_node
)
62 struct list_head
*list_node
;
65 list_for_each_safe(list_node
, n
, &chnl_net_list
) {
66 if (list_node
== delete_node
) {
74 static int chnl_recv_cb(struct cflayer
*layr
, struct cfpkt
*pkt
)
77 struct chnl_net
*priv
= container_of(layr
, struct chnl_net
, chnl
);
81 priv
= container_of(layr
, struct chnl_net
, chnl
);
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
;
100 skb
->ip_summed
= CHECKSUM_NONE
;
107 /* Update statistics. */
108 priv
->netdev
->stats
.rx_packets
++;
109 priv
->netdev
->stats
.rx_bytes
+= pktlen
;
114 static int delete_device(struct chnl_net
*dev
)
118 unregister_netdevice(dev
->netdev
);
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();
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
);
139 static DECLARE_WORK(close_worker
, close_work
);
141 static void chnl_flowctrl_cb(struct cflayer
*layr
, enum caif_ctrlcmd flow
,
144 struct chnl_net
*priv
= container_of(layr
, struct chnl_net
, chnl
);
145 pr_debug("CAIF: %s(): NET flowctrl func called flow: %s\n",
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");
158 case CAIF_CTRLCMD_FLOW_OFF_IND
:
159 priv
->flowenabled
= false;
160 netif_stop_queue(priv
->netdev
);
162 case CAIF_CTRLCMD_DEINIT_RSP
:
163 priv
->state
= CAIF_DISCONNECTED
;
165 case CAIF_CTRLCMD_INIT_FAIL_RSP
:
166 priv
->state
= CAIF_DISCONNECTED
;
167 wake_up_interruptible(&priv
->netmgmt_wq
);
169 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
170 priv
->state
= CAIF_SHUTDOWN
;
171 netif_tx_disable(priv
->netdev
);
172 schedule_work(&close_worker
);
174 case CAIF_CTRLCMD_FLOW_ON_IND
:
175 priv
->flowenabled
= true;
176 netif_wake_queue(priv
->netdev
);
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
);
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
;
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__
);
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. */
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
);
219 if (result
== -EAGAIN
)
220 result
= NETDEV_TX_BUSY
;
224 /* Update statistics. */
225 dev
->stats
.tx_packets
++;
226 dev
->stats
.tx_bytes
+= len
;
231 static int chnl_net_open(struct net_device
*dev
)
233 struct chnl_net
*priv
= NULL
;
235 int llifindex
, headroom
, tailroom
, mtu
;
236 struct net_device
*lldev
;
238 priv
= netdev_priv(dev
);
240 pr_debug("CAIF: %s(): chnl_net_open: no priv\n", __func__
);
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
);
249 pr_debug("CAIF: %s(): err: "
250 "Unable to register and open device,"
257 lldev
= dev_get_by_index(dev_net(dev
), llifindex
);
260 pr_debug("CAIF: %s(): no interface?\n", __func__
);
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
);
282 pr_warning("CAIF: %s(): "
283 "CAIF Interface MTU too small (%d)\n",
290 rtnl_unlock(); /* Release RTNL lock during connect wait */
292 result
= wait_event_interruptible_timeout(priv
->netmgmt_wq
,
293 priv
->state
!= CAIF_CONNECTING
,
298 if (result
== -ERESTARTSYS
) {
299 pr_debug("CAIF: %s(): wait_event_interruptible"
300 " woken by a signal\n", __func__
);
301 result
= -ERESTARTSYS
;
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__
);
314 if (priv
->state
!= CAIF_CONNECTED
) {
315 pr_debug("CAIF: %s(): connect failed\n", __func__
);
316 result
= -ECONNREFUSED
;
319 pr_debug("CAIF: %s(): CAIF Netdevice connected\n", __func__
);
323 caif_disconnect_client(&priv
->chnl
);
324 priv
->state
= CAIF_DISCONNECTED
;
325 pr_debug("CAIF: %s(): state disconnected\n", __func__
);
330 static int chnl_net_stop(struct net_device
*dev
)
332 struct chnl_net
*priv
;
335 priv
= netdev_priv(dev
);
336 priv
->state
= CAIF_DISCONNECTED
;
337 caif_disconnect_client(&priv
->chnl
);
341 static int chnl_net_init(struct net_device
*dev
)
343 struct chnl_net
*priv
;
345 priv
= netdev_priv(dev
);
346 strncpy(priv
->name
, dev
->name
, sizeof(priv
->name
));
350 static void chnl_net_uninit(struct net_device
*dev
)
352 struct chnl_net
*priv
;
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
;
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;
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
;
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
);
412 static void caif_netlink_parms(struct nlattr
*data
[],
413 struct caif_connect_request
*conn_req
)
416 pr_warning("CAIF: %s: no params data found\n", __func__
);
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
;
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
[])
437 struct chnl_net
*caifdev
;
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
);
445 pr_warning("CAIF: %s(): device rtml registration failed\n",
450 static int ipcaif_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
451 struct nlattr
*data
[])
453 struct chnl_net
*caifdev
;
455 caifdev
= netdev_priv(dev
);
456 caif_netlink_parms(data
, &caifdev
->conn_req
);
457 netdev_state_change(dev
);
461 static size_t ipcaif_get_size(const struct net_device
*dev
)
464 /* IFLA_CAIF_IPV4_CONNID */
466 /* IFLA_CAIF_IPV6_CONNID */
468 /* IFLA_CAIF_LOOPBACK */
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
= {
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
);
505 list_for_each_safe(list_node
, _tmp
, &chnl_net_list
) {
506 dev
= list_entry(list_node
, struct chnl_net
, list_field
);
513 module_init(chnl_init_module
);
514 module_exit(chnl_exit_module
);