Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: pep-gprs.c |
| 3 | * |
| 4 | * GPRS over Phonet pipe end point socket |
| 5 | * |
| 6 | * Copyright (C) 2008 Nokia Corporation. |
| 7 | * |
| 8 | * Author: Rémi Denis-Courmont <remi.denis-courmont@nokia.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * version 2 as published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 22 | * 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/if_ether.h> |
| 28 | #include <linux/if_arp.h> |
| 29 | #include <net/sock.h> |
| 30 | |
| 31 | #include <linux/if_phonet.h> |
| 32 | #include <net/tcp_states.h> |
| 33 | #include <net/phonet/gprs.h> |
| 34 | |
| 35 | #define GPRS_DEFAULT_MTU 1400 |
| 36 | |
| 37 | struct gprs_dev { |
| 38 | struct sock *sk; |
| 39 | void (*old_state_change)(struct sock *); |
| 40 | void (*old_data_ready)(struct sock *, int); |
| 41 | void (*old_write_space)(struct sock *); |
| 42 | |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 43 | struct net_device *dev; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Harvey Harrison | 5c7f033 | 2008-11-06 23:10:50 -0800 | [diff] [blame] | 46 | static __be16 gprs_type_trans(struct sk_buff *skb) |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 47 | { |
| 48 | const u8 *pvfc; |
| 49 | u8 buf; |
| 50 | |
| 51 | pvfc = skb_header_pointer(skb, 0, 1, &buf); |
| 52 | if (!pvfc) |
Harvey Harrison | 5c7f033 | 2008-11-06 23:10:50 -0800 | [diff] [blame] | 53 | return htons(0); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 54 | /* Look at IP version field */ |
| 55 | switch (*pvfc >> 4) { |
| 56 | case 4: |
| 57 | return htons(ETH_P_IP); |
| 58 | case 6: |
| 59 | return htons(ETH_P_IPV6); |
| 60 | } |
Harvey Harrison | 5c7f033 | 2008-11-06 23:10:50 -0800 | [diff] [blame] | 61 | return htons(0); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Rémi Denis-Courmont | 893873f | 2008-12-17 15:48:50 -0800 | [diff] [blame] | 64 | static void gprs_writeable(struct gprs_dev *gp) |
| 65 | { |
| 66 | struct net_device *dev = gp->dev; |
| 67 | |
| 68 | if (pep_writeable(gp->sk)) |
| 69 | netif_wake_queue(dev); |
| 70 | } |
| 71 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 72 | /* |
| 73 | * Socket callbacks |
| 74 | */ |
| 75 | |
| 76 | static void gprs_state_change(struct sock *sk) |
| 77 | { |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 78 | struct gprs_dev *gp = sk->sk_user_data; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 79 | |
| 80 | if (sk->sk_state == TCP_CLOSE_WAIT) { |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 81 | struct net_device *dev = gp->dev; |
| 82 | |
| 83 | netif_stop_queue(dev); |
| 84 | netif_carrier_off(dev); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 88 | static int gprs_recv(struct gprs_dev *gp, struct sk_buff *skb) |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 89 | { |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 90 | struct net_device *dev = gp->dev; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 91 | int err = 0; |
Harvey Harrison | 5c7f033 | 2008-11-06 23:10:50 -0800 | [diff] [blame] | 92 | __be16 protocol = gprs_type_trans(skb); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 93 | |
| 94 | if (!protocol) { |
| 95 | err = -EINVAL; |
| 96 | goto drop; |
| 97 | } |
| 98 | |
| 99 | if (likely(skb_headroom(skb) & 3)) { |
| 100 | struct sk_buff *rskb, *fs; |
| 101 | int flen = 0; |
| 102 | |
| 103 | /* Phonet Pipe data header is misaligned (3 bytes), |
| 104 | * so wrap the IP packet as a single fragment of an head-less |
| 105 | * socket buffer. The network stack will pull what it needs, |
| 106 | * but at least, the whole IP payload is not memcpy'd. */ |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 107 | rskb = netdev_alloc_skb(dev, 0); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 108 | if (!rskb) { |
| 109 | err = -ENOBUFS; |
| 110 | goto drop; |
| 111 | } |
| 112 | skb_shinfo(rskb)->frag_list = skb; |
| 113 | rskb->len += skb->len; |
| 114 | rskb->data_len += rskb->len; |
| 115 | rskb->truesize += rskb->len; |
| 116 | |
| 117 | /* Avoid nested fragments */ |
| 118 | for (fs = skb_shinfo(skb)->frag_list; fs; fs = fs->next) |
| 119 | flen += fs->len; |
| 120 | skb->next = skb_shinfo(skb)->frag_list; |
| 121 | skb_shinfo(skb)->frag_list = NULL; |
| 122 | skb->len -= flen; |
| 123 | skb->data_len -= flen; |
| 124 | skb->truesize -= flen; |
| 125 | |
| 126 | skb = rskb; |
| 127 | } |
| 128 | |
| 129 | skb->protocol = protocol; |
| 130 | skb_reset_mac_header(skb); |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 131 | skb->dev = dev; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 132 | |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 133 | if (likely(dev->flags & IFF_UP)) { |
| 134 | dev->stats.rx_packets++; |
| 135 | dev->stats.rx_bytes += skb->len; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 136 | netif_rx(skb); |
| 137 | skb = NULL; |
| 138 | } else |
| 139 | err = -ENODEV; |
| 140 | |
| 141 | drop: |
| 142 | if (skb) { |
| 143 | dev_kfree_skb(skb); |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 144 | dev->stats.rx_dropped++; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 145 | } |
| 146 | return err; |
| 147 | } |
| 148 | |
| 149 | static void gprs_data_ready(struct sock *sk, int len) |
| 150 | { |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 151 | struct gprs_dev *gp = sk->sk_user_data; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 152 | struct sk_buff *skb; |
| 153 | |
| 154 | while ((skb = pep_read(sk)) != NULL) { |
| 155 | skb_orphan(skb); |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 156 | gprs_recv(gp, skb); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | static void gprs_write_space(struct sock *sk) |
| 161 | { |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 162 | struct gprs_dev *gp = sk->sk_user_data; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 163 | |
Rémi Denis-Courmont | 893873f | 2008-12-17 15:48:50 -0800 | [diff] [blame] | 164 | if (netif_running(gp->dev)) |
| 165 | gprs_writeable(gp); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Network device callbacks |
| 170 | */ |
| 171 | |
Rémi Denis-Courmont | 4798a2b | 2008-12-15 00:53:57 -0800 | [diff] [blame] | 172 | static int gprs_open(struct net_device *dev) |
| 173 | { |
| 174 | struct gprs_dev *gp = netdev_priv(dev); |
| 175 | |
Rémi Denis-Courmont | 893873f | 2008-12-17 15:48:50 -0800 | [diff] [blame] | 176 | gprs_writeable(gp); |
Rémi Denis-Courmont | 4798a2b | 2008-12-15 00:53:57 -0800 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int gprs_close(struct net_device *dev) |
| 181 | { |
Rémi Denis-Courmont | 4798a2b | 2008-12-15 00:53:57 -0800 | [diff] [blame] | 182 | netif_stop_queue(dev); |
Rémi Denis-Courmont | 4798a2b | 2008-12-15 00:53:57 -0800 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 186 | static int gprs_xmit(struct sk_buff *skb, struct net_device *dev) |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 187 | { |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 188 | struct gprs_dev *gp = netdev_priv(dev); |
Rémi Denis-Courmont | 893873f | 2008-12-17 15:48:50 -0800 | [diff] [blame] | 189 | struct sock *sk = gp->sk; |
| 190 | int len, err; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 191 | |
| 192 | switch (skb->protocol) { |
| 193 | case htons(ETH_P_IP): |
| 194 | case htons(ETH_P_IPV6): |
| 195 | break; |
| 196 | default: |
| 197 | dev_kfree_skb(skb); |
| 198 | return 0; |
| 199 | } |
| 200 | |
Rémi Denis-Courmont | 893873f | 2008-12-17 15:48:50 -0800 | [diff] [blame] | 201 | skb_orphan(skb); |
| 202 | skb_set_owner_w(skb, sk); |
| 203 | len = skb->len; |
| 204 | err = pep_write(sk, skb); |
| 205 | if (err) { |
| 206 | LIMIT_NETDEBUG(KERN_WARNING"%s: TX error (%d)\n", |
| 207 | dev->name, err); |
| 208 | dev->stats.tx_aborted_errors++; |
| 209 | dev->stats.tx_errors++; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 210 | dev_kfree_skb(skb); |
Rémi Denis-Courmont | 893873f | 2008-12-17 15:48:50 -0800 | [diff] [blame] | 211 | } else { |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 212 | dev->stats.tx_packets++; |
Rémi Denis-Courmont | 893873f | 2008-12-17 15:48:50 -0800 | [diff] [blame] | 213 | dev->stats.tx_bytes += len; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Rémi Denis-Courmont | 893873f | 2008-12-17 15:48:50 -0800 | [diff] [blame] | 216 | if (!pep_writeable(sk)) |
| 217 | netif_stop_queue(dev); |
| 218 | return 0; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 221 | static int gprs_set_mtu(struct net_device *dev, int new_mtu) |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 222 | { |
| 223 | if ((new_mtu < 576) || (new_mtu > (PHONET_MAX_MTU - 11))) |
| 224 | return -EINVAL; |
| 225 | |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 226 | dev->mtu = new_mtu; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
Stephen Hemminger | ab638e6 | 2009-01-07 17:24:34 -0800 | [diff] [blame] | 230 | static const struct net_device_ops gprs_netdev_ops = { |
| 231 | .ndo_open = gprs_open, |
| 232 | .ndo_stop = gprs_close, |
| 233 | .ndo_start_xmit = gprs_xmit, |
| 234 | .ndo_change_mtu = gprs_set_mtu, |
| 235 | }; |
| 236 | |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 237 | static void gprs_setup(struct net_device *dev) |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 238 | { |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 239 | dev->features = NETIF_F_FRAGLIST; |
Rémi Denis-Courmont | 57c81ff | 2008-12-17 15:47:48 -0800 | [diff] [blame] | 240 | dev->type = ARPHRD_PHONET_PIPE; |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 241 | dev->flags = IFF_POINTOPOINT | IFF_NOARP; |
| 242 | dev->mtu = GPRS_DEFAULT_MTU; |
| 243 | dev->hard_header_len = 0; |
| 244 | dev->addr_len = 0; |
| 245 | dev->tx_queue_len = 10; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 246 | |
Stephen Hemminger | ab638e6 | 2009-01-07 17:24:34 -0800 | [diff] [blame] | 247 | dev->netdev_ops = &gprs_netdev_ops; |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 248 | dev->destructor = free_netdev; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* |
| 252 | * External interface |
| 253 | */ |
| 254 | |
| 255 | /* |
| 256 | * Attach a GPRS interface to a datagram socket. |
| 257 | * Returns the interface index on success, negative error code on error. |
| 258 | */ |
| 259 | int gprs_attach(struct sock *sk) |
| 260 | { |
| 261 | static const char ifname[] = "gprs%d"; |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 262 | struct gprs_dev *gp; |
| 263 | struct net_device *dev; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 264 | int err; |
| 265 | |
| 266 | if (unlikely(sk->sk_type == SOCK_STREAM)) |
| 267 | return -EINVAL; /* need packet boundaries */ |
| 268 | |
| 269 | /* Create net device */ |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 270 | dev = alloc_netdev(sizeof(*gp), ifname, gprs_setup); |
| 271 | if (!dev) |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 272 | return -ENOMEM; |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 273 | gp = netdev_priv(dev); |
Rémi Denis-Courmont | 893873f | 2008-12-17 15:48:50 -0800 | [diff] [blame] | 274 | gp->sk = sk; |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 275 | gp->dev = dev; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 276 | |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 277 | netif_stop_queue(dev); |
| 278 | err = register_netdev(dev); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 279 | if (err) { |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 280 | free_netdev(dev); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 281 | return err; |
| 282 | } |
| 283 | |
| 284 | lock_sock(sk); |
| 285 | if (unlikely(sk->sk_user_data)) { |
| 286 | err = -EBUSY; |
| 287 | goto out_rel; |
| 288 | } |
| 289 | if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) || |
| 290 | sock_flag(sk, SOCK_DEAD))) { |
| 291 | err = -EINVAL; |
| 292 | goto out_rel; |
| 293 | } |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 294 | sk->sk_user_data = gp; |
| 295 | gp->old_state_change = sk->sk_state_change; |
| 296 | gp->old_data_ready = sk->sk_data_ready; |
| 297 | gp->old_write_space = sk->sk_write_space; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 298 | sk->sk_state_change = gprs_state_change; |
| 299 | sk->sk_data_ready = gprs_data_ready; |
| 300 | sk->sk_write_space = gprs_write_space; |
| 301 | release_sock(sk); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 302 | sock_hold(sk); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 303 | |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 304 | printk(KERN_DEBUG"%s: attached\n", dev->name); |
| 305 | return dev->ifindex; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 306 | |
| 307 | out_rel: |
| 308 | release_sock(sk); |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 309 | unregister_netdev(dev); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 310 | return err; |
| 311 | } |
| 312 | |
| 313 | void gprs_detach(struct sock *sk) |
| 314 | { |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 315 | struct gprs_dev *gp = sk->sk_user_data; |
| 316 | struct net_device *dev = gp->dev; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 317 | |
| 318 | lock_sock(sk); |
| 319 | sk->sk_user_data = NULL; |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 320 | sk->sk_state_change = gp->old_state_change; |
| 321 | sk->sk_data_ready = gp->old_data_ready; |
| 322 | sk->sk_write_space = gp->old_write_space; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 323 | release_sock(sk); |
| 324 | |
Rémi Denis-Courmont | 09a2c3c | 2008-12-16 01:18:31 -0800 | [diff] [blame] | 325 | printk(KERN_DEBUG"%s: detached\n", dev->name); |
| 326 | unregister_netdev(dev); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 327 | sock_put(sk); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 328 | } |