Subject: Re: sockets in AF_LINK family ? (patches attached)
To: None <tech-net@NetBSD.org>
From: Dheeraj S <dheeraj@ece.gatech.edu>
List: tech-net
Date: 03/25/2005 19:30:53
--OBd5C1Lgu00Gd/Tn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Fri, Mar 25, 2005 at 01:13:34PM -0600, David Young wrote:
>>snipped<<
> Dheeraj,
> 
> It looks to me like you are using IP subroutines (in_xxx) and IP protocol
> control blocks (PCBs, inpcb).  I do not expect for that to work.  Also,
> it is important not to have any arbitrary dependencies on the IP part
> of the stack.
> 
> Dave
> 

David,
 I have attached, a new set of patches. without any dependency on the
IP subroutines this time. Does this seem the right approach to support
a larger set of functionality ?

truely
dheeraj

PS: the way i am verifying it to work is have a simple ioctl to change the
link-level address. Please suggest if i should have a different test case.

-- 
"Nature wants us to react, return blow for blow, cheating for cheating, lie for
lie, and then it requires a Divine power not to hit-back, keep control and 
remain unattached, and act with prudence." 

--OBd5C1Lgu00Gd/Tn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="patch-conf-files.txt"

Index: conf/files
===================================================================
RCS file: /cvsroot/src/sys/conf/files,v
retrieving revision 1.713
diff -u -r1.713 files
--- conf/files	18 Mar 2005 11:11:50 -0000	1.713
+++ conf/files	26 Mar 2005 00:20:59 -0000
@@ -137,6 +137,7 @@
 include "netns/files.netns"
 include "netsmb/files.netsmb"
 include "net/files.pf"
+include "net80211/files.net80211"
 
 defflag				IPX		# IPX network stack
 defflag				PFIL_HOOKS	# pfil(9)
@@ -1326,17 +1327,9 @@
 file	net/raw_usrreq.c
 file	net/route.c
 file	net/rtsock.c
+file	net/dlsock.c
 file	net/slcompress.c		sl | ppp | strip | (irip & irip_vj)
 file	net/zlib.c			(ppp & ppp_deflate) | ipsec | opencrypto
-file	net80211/ieee80211.c		wlan
-file	net80211/ieee80211_compat.c	wlan
-file	net80211/ieee80211_crypto.c	wlan
-file	net80211/ieee80211_input.c	wlan
-file	net80211/ieee80211_ioctl.c	wlan
-file	net80211/ieee80211_node.c	wlan
-file	net80211/ieee80211_output.c	wlan
-file	net80211/ieee80211_proto.c	wlan
-file	net80211/ieee80211_rssadapt.c	wlan
 file	netinet/if_arp.c		arp | netatalk		needs-flag
 file	netinet/if_atm.c		atm
 file	netinet/in_gif.c		gif & inet

--OBd5C1Lgu00Gd/Tn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="patch-dlsock_c.txt"

--- /dev/null	2005-03-25 18:17:24.000000000 -0500
+++ net/dlsock.c	2005-03-25 18:15:50.000000000 -0500
@@ -0,0 +1,164 @@
+/*
+	Copyright  Dheeraj Reddy. dheeraj@ece.gatech.edu
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/domain.h>
+#include <sys/protosw.h>
+
+#include <net/if.h>
+
+#include <net/dlsock.h>
+
+DOMAIN_DEFINE(dltdomain);
+
+struct sockproto dlt_proto = {PF_LINK, };
+
+struct  dltcbhead dltpcb = LIST_HEAD_INITIALIZER(dltpcb);
+
+u_long  dlt_sendspace = DLTSNDQ;
+u_long  dlt_recvspace = DLTRCVQ;
+
+
+void	dlt_init (void)
+{
+	aprint_normal("%s\n", __FUNCTION__);
+	LIST_INIT(&dltpcb);
+}
+
+int     dlt_attach(struct socket* sock, int proto)
+{
+	struct dltpcb *dltp = sotodltpcb(sock);
+	int retval;
+
+	/* dltp is allocated in _usrreq() */
+	if (dltp == 0)
+		return ENOBUFS;
+
+	if ((retval = soreserve(sock, dlt_sendspace, dlt_recvspace)) != 0)
+		return retval;
+	dltp->dlt_socket = sock;
+	dltp->dlt_proto.sp_family = sock->so_proto->pr_domain->dom_family;
+	dltp->dlt_proto.sp_protocol = proto;
+	LIST_INSERT_HEAD(&dltpcb, dltp, dltpcb_list);
+	return 0;
+}
+
+void    dlt_detach(struct dltpcb* dltp)
+{
+	struct socket *sock = dltp->dlt_socket;
+
+	sock->so_pcb = 0;
+	sofree(sock);
+	LIST_REMOVE(dltp, dltpcb_list);
+
+	free((caddr_t)dltp, M_PCB);
+}
+
+void	dlt_input (struct mbuf *m, ...)
+{
+	/* Just drop for now */
+	aprint_normal("%s\n", __FUNCTION__);
+	m_freem(m);
+}
+
+void*    dlt_ctlinput (int cmd, struct sockaddr* saddr, void* d)
+{
+	aprint_normal("%s\n", __FUNCTION__);
+	return (void*)0;
+}
+
+
+int	dlt_ctloutput(int cmd, struct socket *sock, int i,
+		      int j, struct mbuf** m)
+{
+	aprint_normal("%s\n", __FUNCTION__);
+	return 0;
+}
+
+void     dlt_setsockaddr(struct dltpcb* dltp, struct mbuf* nam)
+{
+	nam->m_len = dltp->dlt_laddr->sa_len;
+	bcopy(dltp->dlt_laddr, mtod(nam, caddr_t), (size_t)nam->m_len);
+}
+
+void    dlt_setpeeraddr(struct dltpcb* dltp, struct mbuf* nam)
+{
+	nam->m_len = dltp->dlt_daddr->sa_len;
+	bcopy(dltp->dlt_daddr, mtod(nam, caddr_t), (size_t)nam->m_len);
+}
+
+int	dlt_usrreq(struct socket *so, int req, struct mbuf *cmd, 
+		struct mbuf *data_, struct mbuf *ifp_, struct proc *p)
+{
+	struct dltpcb *dltp = sotodltpcb(so);
+	struct ifnet *ifp = (struct ifnet*)(ifp_);
+	int retval = 0;
+	int s;
+
+	if (req == PRU_ATTACH) {
+		MALLOC(dltp, struct dltpcb*, sizeof(struct dltpcb),
+		       M_PCB, M_WAITOK);
+		if ((so->so_pcb = dltp) != NULL)
+			memset(so->so_pcb, 0, sizeof(struct dltpcb));
+	}
+	
+	s = splsoftnet();	
+
+	switch (req) {
+	
+	case PRU_ATTACH:
+		retval = dlt_attach(so, (int)(long)(data_));
+		if (retval) {
+			free((caddr_t)dltp, M_PCB);
+		}
+		break;
+
+	case PRU_DETACH:
+		dlt_detach(dltp);
+		break;
+
+	case PRU_CONTROL:
+
+		retval = ifp->if_ioctl(ifp, (u_long)cmd, (caddr_t)data_);
+		break;
+
+	case PRU_SOCKADDR:
+
+		dlt_setsockaddr(dltp, data_);
+		break;
+	case PRU_PEERADDR:
+
+		dlt_setpeeraddr(dltp, data_);
+		break;
+		
+	default:
+
+		retval = EOPNOTSUPP;
+	}
+		
+	splx(s);
+	
+	return retval;
+}
+
+int	dlt_output (struct mbuf *m, ...)
+{
+	return (EOPNOTSUPP);
+}
+
+const struct protosw dltsw[] = {
+{
+	SOCK_DGRAM,	&dltdomain,	0,		PR_ATOMIC|PR_ADDR,
+	dlt_input,	dlt_output,	dlt_ctlinput,	dlt_ctloutput,
+	dlt_usrreq,
+	dlt_init,	0,		0,	0,
+} };
+
+struct domain dltdomain = {
+	PF_LINK, "dlt", 0, 0, 0,
+	dltsw, &dltsw[sizeof(dltsw)/sizeof(dltsw[0])]
+};

--OBd5C1Lgu00Gd/Tn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="patch-dlsock_h.txt"

--- /dev/null	2005-03-25 18:17:24.000000000 -0500
+++ net/dlsock.h	2005-03-25 17:44:00.000000000 -0500
@@ -0,0 +1,47 @@
+/*
+  Dheeraj Reddy (dheeraj@ece.gatech.edu)
+*/
+
+#ifndef _NET_DL_SOCK_H_
+#define _NET_DL_SOCK_H_
+
+/*
+  Data Link socket control block to link a socket with a DLI
+  
+*/
+
+struct dltpcb {
+	LIST_ENTRY(dltpcb) dltpcb_list;
+	struct socket *dlt_socket;
+	struct sockaddr *dlt_daddr;
+	struct sockaddr *dlt_laddr;
+	struct sockproto dlt_proto;
+};
+
+#define sotodltpcb(so)       ((struct dltpcb *)(so)->so_pcb)
+
+#define DLTSNDQ  8192
+#define DLTRCVQ  8192
+
+LIST_HEAD(dltcbhead, dltpcb);
+
+extern  struct dltcbhead dltpcb;
+
+
+int     dlt_attach (struct socket*, int);
+void    dlt_detach (struct dltpcb*);
+
+void*    dlt_ctlinput(int, struct sockaddr*, void*);
+int     dlt_ctloutput(int, struct socket*, int, int, struct mbuf**);
+int     dlt_usrreq (struct socket*, int, struct mbuf*, struct mbuf*,
+		    struct mbuf*, struct proc*);
+void    dlt_init (void);
+
+void	dlt_input (struct mbuf *, ...);
+int	dlt_output (struct mbuf *, ...);
+
+/* void    dlt_disconnect(struct dltpcb*); */
+void    dlt_setsockaddr (struct dltpcb*, struct mbuf*);
+void    dlt_setpeeraddr (struct dltpcb*, struct mbuf*);
+
+#endif

--OBd5C1Lgu00Gd/Tn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="patch-net-make.txt"

Index: net/Makefile
===================================================================
RCS file: /cvsroot/src/sys/net/Makefile,v
retrieving revision 1.17
diff -u -r1.17 Makefile
--- net/Makefile	8 Jan 2005 22:28:51 -0000	1.17
+++ net/Makefile	26 Mar 2005 00:20:23 -0000
@@ -8,7 +8,7 @@
 	if_ppp.h if_pppvar.h if_pppoe.h if_slvar.h if_sppp.h if_stf.h \
 	if_stripvar.h if_tap.h if_token.h if_tun.h if_types.h if_vlanvar.h \
 	netisr.h pfil.h pfkeyv2.h pfvar.h ppp-comp.h ppp_defs.h radix.h \
-	raw_cb.h route.h slcompress.h slip.h zlib.h
+	raw_cb.h route.h dlsock.h slcompress.h slip.h zlib.h
 
 .include <bsd.kinc.mk>
 

--OBd5C1Lgu00Gd/Tn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="patch-net80211.txt"

--- /dev/null	2005-03-25 18:17:24.000000000 -0500
+++ net80211/files.net80211	2005-03-24 19:57:45.000000000 -0500
@@ -0,0 +1,10 @@
+
+file	net80211/ieee80211.c		wlan
+file	net80211/ieee80211_compat.c	wlan
+file	net80211/ieee80211_crypto.c	wlan
+file	net80211/ieee80211_input.c	wlan
+file	net80211/ieee80211_ioctl.c	wlan
+file	net80211/ieee80211_node.c	wlan
+file	net80211/ieee80211_output.c	wlan
+file	net80211/ieee80211_proto.c	wlan
+file	net80211/ieee80211_rssadapt.c	wlan

--OBd5C1Lgu00Gd/Tn--