Phonet: basic routing support
Subsystems (3)
cover Phonet: basic routing support
Hello,
This patchset provides rudimentary support for routing Phonet packets. Configuration is done with the common rtnetlink infrastructure.
This is useful when there is more than one Phonet interface in the same namespace,
e.g. a serial bus to a cellular modem and a USB gadget function to a PC.
Comments welcome.
include/net/phonet/phonet.h | 1 include/net/phonet/pn_dev.h | 6 ++ net/phonet/af_phonet.c | 55 ++++++++++++++++-- net/phonet/pn_dev.c | 131 +++++++++++++++++++++++++++++++++++++++++--- net/phonet/pn_netlink.c | 130 +++++++++++++++++++++++++++++++++++++++++++ net/phonet/socket.c | 21 +++++++ 6 files changed, 333 insertions(+), 11 deletions(-)
Signature · 1 line
Rémi Denis-Courmont
PATCH 1/5 Phonet: deliver broadcast packets to broadcast sockets
From: Rémi Denis-Courmont <[email protected]>
--- include/net/phonet/phonet.h | 1 + net/phonet/af_phonet.c | 6 ++++++ net/phonet/socket.c | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 0 deletions(-)
include/net/phonet/phonet.h +1 -0
@@ -47,6 +47,7 @@ static inline struct pn_sock *pn_sk(struct sock *sk)extern const struct proto_ops phonet_dgram_ops;struct sock *pn_find_sock_by_sa (struct net *net,const struct sockaddr_pn *sa); +void pn_deliver_sock_broadcast (struct net *net,struct sk_buff *skb);void phonet_get_local_port_range (int *min,int *max);void pn_sock_hash (struct sock *sk);void pn_sock_unhash (struct sock *sk);
net/phonet/af_phonet.c +6 -0
@@ -369,6 +369,12 @@ static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,pn_skb_get_dst_sockaddr (skb, &sa); +/* check if this is broadcasted */ +if (pn_sockaddr_get_addr (&sa) ==PNADDR_BROADCAST ) { +pn_deliver_sock_broadcast (net, skb); + goto out; + } +/* check if we are the destination */ if (phonet_address_lookup (net,pn_sockaddr_get_addr (&sa)) ==0 ) {/* Phonet packet input */
net/phonet/socket.c +21 -0
@@ -94,7 +94,28 @@ struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn)spin_unlock_bh (&pnsocks.lock);return rval; +} + +/* Deliver a broadcast packet (only in bottom-half) */ +void pn_deliver_sock_broadcast (struct net *net,struct sk_buff *skb) +{ +struct hlist_node *node; +struct sock *sknode; + +spin_lock (&pnsocks.lock); +sk_for_each (sknode, node, &pnsocks.hlist) { +struct sk_buff *clone; + +if (!net_eq (sock_net (sknode), net)) +continue ; +if (!sock_flag (sknode,SOCK_BROADCAST )) +continue ; + clone =skb_clone (skb,GFP_ATOMIC ); +if (clone) +sk_receive_skb (sknode, clone,0 ); + } +spin_unlock (&pnsocks.lock); }void pn_sock_hash (struct sock *sk)
Signature · 1 line
1.6.0.4
PATCH 2/5 Phonet: routing table backend
From: Rémi Denis-Courmont <[email protected]>
The Phonet "universe" only has 64 addresses, so we keep a trivial flat routing table.
--- include/net/phonet/pn_dev.h | 5 ++ net/phonet/pn_dev.c | 100 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 99 insertions(+), 6 deletions(-)
include/net/phonet/pn_dev.h +5 -0
@@ -47,6 +47,11 @@ u8 phonet_address_get(struct net_device *dev, u8 addr);int phonet_address_lookup (struct net *net,u8 addr);void phonet_address_notify (int event,struct net_device *dev,u8 addr); +int phonet_route_add (struct net_device *dev,u8 daddr); +int phonet_route_del (struct net_device *dev,u8 daddr); +struct net_device *phonet_route_get (struct net *net,u8 daddr); +struct net_device *phonet_route_output (struct net *net,u8 daddr); +#define PN_NO_ADDR 0xffextern const struct file_operations pn_sock_seq_fops;
net/phonet/pn_dev.c +94 -6
140 lines not on this page · raw message
Signature · 1 line
1.6.0.4
PATCH 3/5 Phonet: routing table Netlink interface
From: Rémi Denis-Courmont <[email protected]>
--- include/net/phonet/pn_dev.h | 1 + net/phonet/pn_dev.c | 31 ++++++++++ net/phonet/pn_netlink.c | 130 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 0 deletions(-)
include/net/phonet/pn_dev.h +1 -0
@@ -49,6 +49,7 @@ void phonet_address_notify(int event, struct net_device *dev, u8 addr);int phonet_route_add (struct net_device *dev,u8 daddr);int phonet_route_del (struct net_device *dev,u8 daddr); +void rtm_phonet_notify (int event,struct net_device *dev,u8 dst);struct net_device *phonet_route_get (struct net *net,u8 daddr);struct net_device *phonet_route_output (struct net *net,u8 daddr);
net/phonet/pn_dev.c +31 -0
@@ -240,6 +240,27 @@ static int phonet_device_autoconf(struct net_device *dev)return 0 ; } +static void phonet_route_autodel (struct net_device *dev) +{ +struct phonet_net *pnn =net_generic (dev_net (dev), phonet_net_id); +unsigned i; +DECLARE_BITMAP (deleted,64 ); + +/* Remove left-over Phonet routes */ +bitmap_zero (deleted,64 ); +spin_lock_bh (&pnn->routes.lock); +for (i =0 ; i <64 ; i++) +if (dev == pnn->routes.table[i]) { +set_bit (i, deleted); + pnn->routes.table[i] =NULL ; +dev_put (dev); + } +spin_unlock_bh (&pnn->routes.lock); +for (i =find_first_bit (deleted,64 ); i <64 ; + i =find_next_bit (deleted,64 , i +1 )) +rtm_phonet_notify (RTM_DELROUTE , dev, i); +} +/* notify Phonet of device events */ static int phonet_device_notify (struct notifier_block *me,unsigned long what,void *arg)
@@ -253,6 +274,7 @@ static int phonet_device_notify(struct notifier_block *me, unsigned long what,break ;case NETDEV_UNREGISTER :phonet_device_destroy (dev); +phonet_route_autodel (dev);break ; }return 0 ;
@@ -287,10 +309,19 @@ static void phonet_exit_net(struct net *net) {struct phonet_net *pnn =net_generic (net, phonet_net_id);struct net_device *dev; +unsigned i;rtnl_lock ();for_each_netdev (net, dev)phonet_device_destroy (dev); + +for (i =0 ; i <64 ; i++) { + dev = pnn->routes.table[i]; +if (dev) { +rtm_phonet_notify (RTM_DELROUTE , dev, i); +dev_put (dev); + } + }rtnl_unlock ();proc_net_remove (net,"phonet" );
net/phonet/pn_netlink.c +130 -0
147 lines not on this page · raw message
Signature · 1 line
1.6.0.4
PATCH 4/5 Phonet: route outgoing packets
From: Rémi Denis-Courmont <[email protected]>
--- net/phonet/af_phonet.c | 17 ++++++++++++----- 1 files changed, 12 insertions(+), 5 deletions(-)
net/phonet/af_phonet.c +12 -5
@@ -190,9 +190,8 @@ static int pn_send(struct sk_buff *skb, struct net_device *dev, skb->priority =0 ; skb->dev = dev; -if (pn_addr (src) ==pn_addr (dst)) { +if (skb->pkt_type ==PACKET_LOOPBACK ) {skb_reset_mac_header (skb); - skb->pkt_type =PACKET_LOOPBACK ;skb_orphan (skb);if (irq)netif_rx (skb);
@@ -222,6 +221,9 @@ static int pn_raw_send(const void *data, int len, struct net_device *dev,if (skb ==NULL )return -ENOMEM ; +if (phonet_address_lookup (dev_net (dev),pn_addr (dst)) ==0 ) + skb->pkt_type =PACKET_LOOPBACK ; +skb_reserve (skb,MAX_PHONET_HEADER );__skb_put (skb, len);skb_copy_to_linear_data (skb, data, len);
@@ -235,6 +237,7 @@ static int pn_raw_send(const void *data, int len, struct net_device *dev,int pn_skb_send (struct sock *sk,struct sk_buff *skb,const struct sockaddr_pn *target) { +struct net *net =sock_net (sk);struct net_device *dev;struct pn_sock *pn =pn_sk (sk);int err;
@@ -243,9 +246,13 @@ int pn_skb_send(struct sock *sk, struct sk_buff *skb, err = -EHOSTUNREACH ;if (sk->sk_bound_dev_if) - dev =dev_get_by_index (sock_net (sk), sk->sk_bound_dev_if); -else - dev =phonet_device_get (sock_net (sk)); + dev =dev_get_by_index (net, sk->sk_bound_dev_if); +else if (phonet_address_lookup (net, daddr) ==0 ) { + dev =phonet_device_get (net); + skb->pkt_type =PACKET_LOOPBACK ; + }else + dev =phonet_route_output (net, daddr); +if (!dev || !(dev->flags &IFF_UP )) goto drop;
Signature · 1 line
1.6.0.4
PATCH 5/5 Phonet: forward incoming packets
From: Rémi Denis-Courmont <[email protected]>
--- net/phonet/af_phonet.c | 32 ++++++++++++++++++++++++++++++++ 1 files changed, 32 insertions(+), 0 deletions(-)
net/phonet/af_phonet.c +32 -0
@@ -394,6 +394,38 @@ static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,send_obj_unreachable (skb);send_reset_indications (skb); } + }else if (unlikely (skb->pkt_type ==PACKET_LOOPBACK )) + goto out;/* Race between address deletion and loopback */ +else { +/* Phonet packet routing */ +struct net_device *out_dev; + + out_dev =phonet_route_output (net,pn_sockaddr_get_addr (&sa)); +if (!out_dev) { +LIMIT_NETDEBUG (KERN_WARNING "No Phonet route to %02X\n" , +pn_sockaddr_get_addr (&sa)); + goto out; + } + +__skb_push (skb,sizeof (struct phonethdr )); + skb->dev = out_dev; +if (out_dev == dev) { +LIMIT_NETDEBUG (KERN_ERR "Phonet loop to %02X on %s\n" , +pn_sockaddr_get_addr (&sa), dev->name); + goto out_dev; + } +/* Some drivers (e.g. TUN) do not allocate HW header space */ +if (skb_cow_head (skb, out_dev->hard_header_len)) + goto out_dev; + +if (dev_hard_header (skb, out_dev,ETH_P_PHONET ,NULL ,NULL , + skb->len) <0 ) + goto out_dev; +dev_queue_xmit (skb); +dev_put (out_dev); +return NET_RX_SUCCESS ; +out_dev: +dev_put (out_dev); } out:
Signature · 1 line
1.6.0.4
David Miller
maintainerFrom: Rémi Denis-Courmont <[email protected]>
Date: Wed, 14 Oct 2009 12:47:34 +0200
Quoted 7 lines
This patchset provides rudimentary support for routing Phonet packets. Configuration is done with the common rtnetlink infrastructure.
This is useful when there is more than one Phonet interface in the same namespace,
e.g. a serial bus to a cellular modem and a USB gadget function to a PC.
Looks good, applied to net-next-2.6, thanks.
Quoted 4 lines · David Miller
From: Rémi Denis-Courmont <[email protected]>
Date: Wed, 14 Oct 2009 12:47:34 +0200Quoted 1 line
Quoted 8 lines
Quoted 6 lines
This patchset provides rudimentary support for routing Phonet packets. Configuration is done with the common rtnetlink infrastructure.
This is useful when there is more than one Phonet interface in the same namespace,
e.g. a serial bus to a cellular modem and a USB gadget function to a PC.Looks good, applied to net-next-2.6, thanks.
Hmmrr or not. I took an old buggy patchset. Trivial fix follows. Sorry.
Signature · 1 line
Rémi Denis-Courmont
PATCH 1/1 Phonet: hold socket before giving it to sk_deliver_skb()
From: Rémi Denis-Courmont <[email protected]>
--- net/phonet/socket.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
net/phonet/socket.c +3 -1
@@ -112,8 +112,10 @@ void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb)continue ; clone =skb_clone (skb,GFP_ATOMIC ); -if (clone) +if (clone) { +sock_hold (sknode);sk_receive_skb (sknode, clone,0 ); + } }spin_unlock (&pnsocks.lock); }
Signature · 1 line
1.6.0.4
Re: [PATCH] Phonet: hold socket before giving it to sk_deliver_skb()
Quoted 13 lines · Rémi Denis-Courmont
From: Rémi Denis-Courmont <[email protected]>
Signed-off-by Rémi Denis-Courmont <[email protected]>--- net/phonet/socket.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)net/phonet/socket.c +0
-0@@ -112,8 +112,10 @@ void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb)continue ;
net/phonet/socket.c void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb) · line 121
Quoted 10 lines
net/phonet/socket.c +3
-1clone =skb_clone (skb,GFP_ATOMIC ); -if (clone) +if (clone) { +sock_hold (sknode);sk_receive_skb (sknode, clone,0 ); + } }spin_unlock (&pnsocks.lock); }
Indeed sk_receive_skb() does a sock_put(sk)
Re: [PATCH] Phonet: hold socket before giving it to sk_deliver_skb()
In reply to Rémi Denis-CourmontFrom: Rémi Denis-Courmont <[email protected]>
Date: Thu, 15 Oct 2009 16:00:00 +0300
Quoted 3 lines
From: Rémi Denis-Courmont <[email protected]>
Signed-off-by Rémi Denis-Courmont <[email protected]>
Applied.