102a47617SRémi Denis-Courmont /* 202a47617SRémi Denis-Courmont * File: pep-gprs.c 302a47617SRémi Denis-Courmont * 402a47617SRémi Denis-Courmont * GPRS over Phonet pipe end point socket 502a47617SRémi Denis-Courmont * 602a47617SRémi Denis-Courmont * Copyright (C) 2008 Nokia Corporation. 702a47617SRémi Denis-Courmont * 802a47617SRémi Denis-Courmont * Author: Rémi Denis-Courmont <remi.denis-courmont@nokia.com> 902a47617SRémi Denis-Courmont * 1002a47617SRémi Denis-Courmont * This program is free software; you can redistribute it and/or 1102a47617SRémi Denis-Courmont * modify it under the terms of the GNU General Public License 1202a47617SRémi Denis-Courmont * version 2 as published by the Free Software Foundation. 1302a47617SRémi Denis-Courmont * 1402a47617SRémi Denis-Courmont * This program is distributed in the hope that it will be useful, but 1502a47617SRémi Denis-Courmont * WITHOUT ANY WARRANTY; without even the implied warranty of 1602a47617SRémi Denis-Courmont * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1702a47617SRémi Denis-Courmont * General Public License for more details. 1802a47617SRémi Denis-Courmont * 1902a47617SRémi Denis-Courmont * You should have received a copy of the GNU General Public License 2002a47617SRémi Denis-Courmont * along with this program; if not, write to the Free Software 2102a47617SRémi Denis-Courmont * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 2202a47617SRémi Denis-Courmont * 02110-1301 USA 2302a47617SRémi Denis-Courmont */ 2402a47617SRémi Denis-Courmont 2502a47617SRémi Denis-Courmont #include <linux/kernel.h> 2602a47617SRémi Denis-Courmont #include <linux/netdevice.h> 2702a47617SRémi Denis-Courmont #include <linux/if_ether.h> 2802a47617SRémi Denis-Courmont #include <linux/if_arp.h> 2902a47617SRémi Denis-Courmont #include <net/sock.h> 3002a47617SRémi Denis-Courmont 3102a47617SRémi Denis-Courmont #include <linux/if_phonet.h> 3202a47617SRémi Denis-Courmont #include <net/tcp_states.h> 3302a47617SRémi Denis-Courmont #include <net/phonet/gprs.h> 3402a47617SRémi Denis-Courmont 3502a47617SRémi Denis-Courmont #define GPRS_DEFAULT_MTU 1400 3602a47617SRémi Denis-Courmont 3702a47617SRémi Denis-Courmont struct gprs_dev { 3802a47617SRémi Denis-Courmont struct sock *sk; 3902a47617SRémi Denis-Courmont void (*old_state_change)(struct sock *); 4002a47617SRémi Denis-Courmont void (*old_data_ready)(struct sock *, int); 4102a47617SRémi Denis-Courmont void (*old_write_space)(struct sock *); 4202a47617SRémi Denis-Courmont 4309a2c3c0SRémi Denis-Courmont struct net_device *dev; 4402a47617SRémi Denis-Courmont }; 4502a47617SRémi Denis-Courmont 465c7f0333SHarvey Harrison static __be16 gprs_type_trans(struct sk_buff *skb) 4702a47617SRémi Denis-Courmont { 4802a47617SRémi Denis-Courmont const u8 *pvfc; 4902a47617SRémi Denis-Courmont u8 buf; 5002a47617SRémi Denis-Courmont 5102a47617SRémi Denis-Courmont pvfc = skb_header_pointer(skb, 0, 1, &buf); 5202a47617SRémi Denis-Courmont if (!pvfc) 535c7f0333SHarvey Harrison return htons(0); 5402a47617SRémi Denis-Courmont /* Look at IP version field */ 5502a47617SRémi Denis-Courmont switch (*pvfc >> 4) { 5602a47617SRémi Denis-Courmont case 4: 5702a47617SRémi Denis-Courmont return htons(ETH_P_IP); 5802a47617SRémi Denis-Courmont case 6: 5902a47617SRémi Denis-Courmont return htons(ETH_P_IPV6); 6002a47617SRémi Denis-Courmont } 615c7f0333SHarvey Harrison return htons(0); 6202a47617SRémi Denis-Courmont } 6302a47617SRémi Denis-Courmont 64*893873f3SRémi Denis-Courmont static void gprs_writeable(struct gprs_dev *gp) 65*893873f3SRémi Denis-Courmont { 66*893873f3SRémi Denis-Courmont struct net_device *dev = gp->dev; 67*893873f3SRémi Denis-Courmont 68*893873f3SRémi Denis-Courmont if (pep_writeable(gp->sk)) 69*893873f3SRémi Denis-Courmont netif_wake_queue(dev); 70*893873f3SRémi Denis-Courmont } 71*893873f3SRémi Denis-Courmont 7202a47617SRémi Denis-Courmont /* 7302a47617SRémi Denis-Courmont * Socket callbacks 7402a47617SRémi Denis-Courmont */ 7502a47617SRémi Denis-Courmont 7602a47617SRémi Denis-Courmont static void gprs_state_change(struct sock *sk) 7702a47617SRémi Denis-Courmont { 7809a2c3c0SRémi Denis-Courmont struct gprs_dev *gp = sk->sk_user_data; 7902a47617SRémi Denis-Courmont 8002a47617SRémi Denis-Courmont if (sk->sk_state == TCP_CLOSE_WAIT) { 8109a2c3c0SRémi Denis-Courmont struct net_device *dev = gp->dev; 8209a2c3c0SRémi Denis-Courmont 8309a2c3c0SRémi Denis-Courmont netif_stop_queue(dev); 8409a2c3c0SRémi Denis-Courmont netif_carrier_off(dev); 8502a47617SRémi Denis-Courmont } 8602a47617SRémi Denis-Courmont } 8702a47617SRémi Denis-Courmont 8809a2c3c0SRémi Denis-Courmont static int gprs_recv(struct gprs_dev *gp, struct sk_buff *skb) 8902a47617SRémi Denis-Courmont { 9009a2c3c0SRémi Denis-Courmont struct net_device *dev = gp->dev; 9102a47617SRémi Denis-Courmont int err = 0; 925c7f0333SHarvey Harrison __be16 protocol = gprs_type_trans(skb); 9302a47617SRémi Denis-Courmont 9402a47617SRémi Denis-Courmont if (!protocol) { 9502a47617SRémi Denis-Courmont err = -EINVAL; 9602a47617SRémi Denis-Courmont goto drop; 9702a47617SRémi Denis-Courmont } 9802a47617SRémi Denis-Courmont 9902a47617SRémi Denis-Courmont if (likely(skb_headroom(skb) & 3)) { 10002a47617SRémi Denis-Courmont struct sk_buff *rskb, *fs; 10102a47617SRémi Denis-Courmont int flen = 0; 10202a47617SRémi Denis-Courmont 10302a47617SRémi Denis-Courmont /* Phonet Pipe data header is misaligned (3 bytes), 10402a47617SRémi Denis-Courmont * so wrap the IP packet as a single fragment of an head-less 10502a47617SRémi Denis-Courmont * socket buffer. The network stack will pull what it needs, 10602a47617SRémi Denis-Courmont * but at least, the whole IP payload is not memcpy'd. */ 10709a2c3c0SRémi Denis-Courmont rskb = netdev_alloc_skb(dev, 0); 10802a47617SRémi Denis-Courmont if (!rskb) { 10902a47617SRémi Denis-Courmont err = -ENOBUFS; 11002a47617SRémi Denis-Courmont goto drop; 11102a47617SRémi Denis-Courmont } 11202a47617SRémi Denis-Courmont skb_shinfo(rskb)->frag_list = skb; 11302a47617SRémi Denis-Courmont rskb->len += skb->len; 11402a47617SRémi Denis-Courmont rskb->data_len += rskb->len; 11502a47617SRémi Denis-Courmont rskb->truesize += rskb->len; 11602a47617SRémi Denis-Courmont 11702a47617SRémi Denis-Courmont /* Avoid nested fragments */ 11802a47617SRémi Denis-Courmont for (fs = skb_shinfo(skb)->frag_list; fs; fs = fs->next) 11902a47617SRémi Denis-Courmont flen += fs->len; 12002a47617SRémi Denis-Courmont skb->next = skb_shinfo(skb)->frag_list; 12102a47617SRémi Denis-Courmont skb_shinfo(skb)->frag_list = NULL; 12202a47617SRémi Denis-Courmont skb->len -= flen; 12302a47617SRémi Denis-Courmont skb->data_len -= flen; 12402a47617SRémi Denis-Courmont skb->truesize -= flen; 12502a47617SRémi Denis-Courmont 12602a47617SRémi Denis-Courmont skb = rskb; 12702a47617SRémi Denis-Courmont } 12802a47617SRémi Denis-Courmont 12902a47617SRémi Denis-Courmont skb->protocol = protocol; 13002a47617SRémi Denis-Courmont skb_reset_mac_header(skb); 13109a2c3c0SRémi Denis-Courmont skb->dev = dev; 13202a47617SRémi Denis-Courmont 13309a2c3c0SRémi Denis-Courmont if (likely(dev->flags & IFF_UP)) { 13409a2c3c0SRémi Denis-Courmont dev->stats.rx_packets++; 13509a2c3c0SRémi Denis-Courmont dev->stats.rx_bytes += skb->len; 13602a47617SRémi Denis-Courmont netif_rx(skb); 13702a47617SRémi Denis-Courmont skb = NULL; 13802a47617SRémi Denis-Courmont } else 13902a47617SRémi Denis-Courmont err = -ENODEV; 14002a47617SRémi Denis-Courmont 14102a47617SRémi Denis-Courmont drop: 14202a47617SRémi Denis-Courmont if (skb) { 14302a47617SRémi Denis-Courmont dev_kfree_skb(skb); 14409a2c3c0SRémi Denis-Courmont dev->stats.rx_dropped++; 14502a47617SRémi Denis-Courmont } 14602a47617SRémi Denis-Courmont return err; 14702a47617SRémi Denis-Courmont } 14802a47617SRémi Denis-Courmont 14902a47617SRémi Denis-Courmont static void gprs_data_ready(struct sock *sk, int len) 15002a47617SRémi Denis-Courmont { 15109a2c3c0SRémi Denis-Courmont struct gprs_dev *gp = sk->sk_user_data; 15202a47617SRémi Denis-Courmont struct sk_buff *skb; 15302a47617SRémi Denis-Courmont 15402a47617SRémi Denis-Courmont while ((skb = pep_read(sk)) != NULL) { 15502a47617SRémi Denis-Courmont skb_orphan(skb); 15609a2c3c0SRémi Denis-Courmont gprs_recv(gp, skb); 15702a47617SRémi Denis-Courmont } 15802a47617SRémi Denis-Courmont } 15902a47617SRémi Denis-Courmont 16002a47617SRémi Denis-Courmont static void gprs_write_space(struct sock *sk) 16102a47617SRémi Denis-Courmont { 16209a2c3c0SRémi Denis-Courmont struct gprs_dev *gp = sk->sk_user_data; 16302a47617SRémi Denis-Courmont 164*893873f3SRémi Denis-Courmont if (netif_running(gp->dev)) 165*893873f3SRémi Denis-Courmont gprs_writeable(gp); 16602a47617SRémi Denis-Courmont } 16702a47617SRémi Denis-Courmont 16802a47617SRémi Denis-Courmont /* 16902a47617SRémi Denis-Courmont * Network device callbacks 17002a47617SRémi Denis-Courmont */ 17102a47617SRémi Denis-Courmont 1724798a2b8SRémi Denis-Courmont static int gprs_open(struct net_device *dev) 1734798a2b8SRémi Denis-Courmont { 1744798a2b8SRémi Denis-Courmont struct gprs_dev *gp = netdev_priv(dev); 1754798a2b8SRémi Denis-Courmont 176*893873f3SRémi Denis-Courmont gprs_writeable(gp); 1774798a2b8SRémi Denis-Courmont return 0; 1784798a2b8SRémi Denis-Courmont } 1794798a2b8SRémi Denis-Courmont 1804798a2b8SRémi Denis-Courmont static int gprs_close(struct net_device *dev) 1814798a2b8SRémi Denis-Courmont { 1824798a2b8SRémi Denis-Courmont netif_stop_queue(dev); 1834798a2b8SRémi Denis-Courmont return 0; 1844798a2b8SRémi Denis-Courmont } 1854798a2b8SRémi Denis-Courmont 18609a2c3c0SRémi Denis-Courmont static int gprs_xmit(struct sk_buff *skb, struct net_device *dev) 18702a47617SRémi Denis-Courmont { 18809a2c3c0SRémi Denis-Courmont struct gprs_dev *gp = netdev_priv(dev); 189*893873f3SRémi Denis-Courmont struct sock *sk = gp->sk; 190*893873f3SRémi Denis-Courmont int len, err; 19102a47617SRémi Denis-Courmont 19202a47617SRémi Denis-Courmont switch (skb->protocol) { 19302a47617SRémi Denis-Courmont case htons(ETH_P_IP): 19402a47617SRémi Denis-Courmont case htons(ETH_P_IPV6): 19502a47617SRémi Denis-Courmont break; 19602a47617SRémi Denis-Courmont default: 19702a47617SRémi Denis-Courmont dev_kfree_skb(skb); 19802a47617SRémi Denis-Courmont return 0; 19902a47617SRémi Denis-Courmont } 20002a47617SRémi Denis-Courmont 20102a47617SRémi Denis-Courmont skb_orphan(skb); 20202a47617SRémi Denis-Courmont skb_set_owner_w(skb, sk); 203*893873f3SRémi Denis-Courmont len = skb->len; 20402a47617SRémi Denis-Courmont err = pep_write(sk, skb); 20502a47617SRémi Denis-Courmont if (err) { 20602a47617SRémi Denis-Courmont LIMIT_NETDEBUG(KERN_WARNING"%s: TX error (%d)\n", 20709a2c3c0SRémi Denis-Courmont dev->name, err); 20809a2c3c0SRémi Denis-Courmont dev->stats.tx_aborted_errors++; 20909a2c3c0SRémi Denis-Courmont dev->stats.tx_errors++; 210*893873f3SRémi Denis-Courmont dev_kfree_skb(skb); 211*893873f3SRémi Denis-Courmont } else { 212*893873f3SRémi Denis-Courmont dev->stats.tx_packets++; 213*893873f3SRémi Denis-Courmont dev->stats.tx_bytes += len; 21402a47617SRémi Denis-Courmont } 21502a47617SRémi Denis-Courmont 216*893873f3SRémi Denis-Courmont if (!pep_writeable(sk)) 217*893873f3SRémi Denis-Courmont netif_stop_queue(dev); 218*893873f3SRémi Denis-Courmont return 0; 21902a47617SRémi Denis-Courmont } 22002a47617SRémi Denis-Courmont 22109a2c3c0SRémi Denis-Courmont static int gprs_set_mtu(struct net_device *dev, int new_mtu) 22202a47617SRémi Denis-Courmont { 22302a47617SRémi Denis-Courmont if ((new_mtu < 576) || (new_mtu > (PHONET_MAX_MTU - 11))) 22402a47617SRémi Denis-Courmont return -EINVAL; 22502a47617SRémi Denis-Courmont 22609a2c3c0SRémi Denis-Courmont dev->mtu = new_mtu; 22702a47617SRémi Denis-Courmont return 0; 22802a47617SRémi Denis-Courmont } 22902a47617SRémi Denis-Courmont 23009a2c3c0SRémi Denis-Courmont static void gprs_setup(struct net_device *dev) 23102a47617SRémi Denis-Courmont { 23209a2c3c0SRémi Denis-Courmont dev->features = NETIF_F_FRAGLIST; 23357c81fffSRémi Denis-Courmont dev->type = ARPHRD_PHONET_PIPE; 23409a2c3c0SRémi Denis-Courmont dev->flags = IFF_POINTOPOINT | IFF_NOARP; 23509a2c3c0SRémi Denis-Courmont dev->mtu = GPRS_DEFAULT_MTU; 23609a2c3c0SRémi Denis-Courmont dev->hard_header_len = 0; 23709a2c3c0SRémi Denis-Courmont dev->addr_len = 0; 23809a2c3c0SRémi Denis-Courmont dev->tx_queue_len = 10; 23902a47617SRémi Denis-Courmont 24009a2c3c0SRémi Denis-Courmont dev->destructor = free_netdev; 24109a2c3c0SRémi Denis-Courmont dev->open = gprs_open; 24209a2c3c0SRémi Denis-Courmont dev->stop = gprs_close; 24309a2c3c0SRémi Denis-Courmont dev->hard_start_xmit = gprs_xmit; /* mandatory */ 24409a2c3c0SRémi Denis-Courmont dev->change_mtu = gprs_set_mtu; 24502a47617SRémi Denis-Courmont } 24602a47617SRémi Denis-Courmont 24702a47617SRémi Denis-Courmont /* 24802a47617SRémi Denis-Courmont * External interface 24902a47617SRémi Denis-Courmont */ 25002a47617SRémi Denis-Courmont 25102a47617SRémi Denis-Courmont /* 25202a47617SRémi Denis-Courmont * Attach a GPRS interface to a datagram socket. 25302a47617SRémi Denis-Courmont * Returns the interface index on success, negative error code on error. 25402a47617SRémi Denis-Courmont */ 25502a47617SRémi Denis-Courmont int gprs_attach(struct sock *sk) 25602a47617SRémi Denis-Courmont { 25702a47617SRémi Denis-Courmont static const char ifname[] = "gprs%d"; 25809a2c3c0SRémi Denis-Courmont struct gprs_dev *gp; 25909a2c3c0SRémi Denis-Courmont struct net_device *dev; 26002a47617SRémi Denis-Courmont int err; 26102a47617SRémi Denis-Courmont 26202a47617SRémi Denis-Courmont if (unlikely(sk->sk_type == SOCK_STREAM)) 26302a47617SRémi Denis-Courmont return -EINVAL; /* need packet boundaries */ 26402a47617SRémi Denis-Courmont 26502a47617SRémi Denis-Courmont /* Create net device */ 26609a2c3c0SRémi Denis-Courmont dev = alloc_netdev(sizeof(*gp), ifname, gprs_setup); 26709a2c3c0SRémi Denis-Courmont if (!dev) 26802a47617SRémi Denis-Courmont return -ENOMEM; 26909a2c3c0SRémi Denis-Courmont gp = netdev_priv(dev); 270*893873f3SRémi Denis-Courmont gp->sk = sk; 27109a2c3c0SRémi Denis-Courmont gp->dev = dev; 27202a47617SRémi Denis-Courmont 27309a2c3c0SRémi Denis-Courmont netif_stop_queue(dev); 27409a2c3c0SRémi Denis-Courmont err = register_netdev(dev); 27502a47617SRémi Denis-Courmont if (err) { 27609a2c3c0SRémi Denis-Courmont free_netdev(dev); 27702a47617SRémi Denis-Courmont return err; 27802a47617SRémi Denis-Courmont } 27902a47617SRémi Denis-Courmont 28002a47617SRémi Denis-Courmont lock_sock(sk); 28102a47617SRémi Denis-Courmont if (unlikely(sk->sk_user_data)) { 28202a47617SRémi Denis-Courmont err = -EBUSY; 28302a47617SRémi Denis-Courmont goto out_rel; 28402a47617SRémi Denis-Courmont } 28502a47617SRémi Denis-Courmont if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) || 28602a47617SRémi Denis-Courmont sock_flag(sk, SOCK_DEAD))) { 28702a47617SRémi Denis-Courmont err = -EINVAL; 28802a47617SRémi Denis-Courmont goto out_rel; 28902a47617SRémi Denis-Courmont } 29009a2c3c0SRémi Denis-Courmont sk->sk_user_data = gp; 29109a2c3c0SRémi Denis-Courmont gp->old_state_change = sk->sk_state_change; 29209a2c3c0SRémi Denis-Courmont gp->old_data_ready = sk->sk_data_ready; 29309a2c3c0SRémi Denis-Courmont gp->old_write_space = sk->sk_write_space; 29402a47617SRémi Denis-Courmont sk->sk_state_change = gprs_state_change; 29502a47617SRémi Denis-Courmont sk->sk_data_ready = gprs_data_ready; 29602a47617SRémi Denis-Courmont sk->sk_write_space = gprs_write_space; 29702a47617SRémi Denis-Courmont release_sock(sk); 29802a47617SRémi Denis-Courmont sock_hold(sk); 29902a47617SRémi Denis-Courmont 30009a2c3c0SRémi Denis-Courmont printk(KERN_DEBUG"%s: attached\n", dev->name); 30109a2c3c0SRémi Denis-Courmont return dev->ifindex; 30202a47617SRémi Denis-Courmont 30302a47617SRémi Denis-Courmont out_rel: 30402a47617SRémi Denis-Courmont release_sock(sk); 30509a2c3c0SRémi Denis-Courmont unregister_netdev(dev); 30602a47617SRémi Denis-Courmont return err; 30702a47617SRémi Denis-Courmont } 30802a47617SRémi Denis-Courmont 30902a47617SRémi Denis-Courmont void gprs_detach(struct sock *sk) 31002a47617SRémi Denis-Courmont { 31109a2c3c0SRémi Denis-Courmont struct gprs_dev *gp = sk->sk_user_data; 31209a2c3c0SRémi Denis-Courmont struct net_device *dev = gp->dev; 31302a47617SRémi Denis-Courmont 31402a47617SRémi Denis-Courmont lock_sock(sk); 31502a47617SRémi Denis-Courmont sk->sk_user_data = NULL; 31609a2c3c0SRémi Denis-Courmont sk->sk_state_change = gp->old_state_change; 31709a2c3c0SRémi Denis-Courmont sk->sk_data_ready = gp->old_data_ready; 31809a2c3c0SRémi Denis-Courmont sk->sk_write_space = gp->old_write_space; 31902a47617SRémi Denis-Courmont release_sock(sk); 32002a47617SRémi Denis-Courmont 32109a2c3c0SRémi Denis-Courmont printk(KERN_DEBUG"%s: detached\n", dev->name); 32209a2c3c0SRémi Denis-Courmont unregister_netdev(dev); 32302a47617SRémi Denis-Courmont sock_put(sk); 32402a47617SRémi Denis-Courmont } 325