From nobody@FreeBSD.org  Mon Nov  7 17:35:40 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id E84C1106566B
	for <freebsd-gnats-submit@FreeBSD.org>; Mon,  7 Nov 2011 17:35:39 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id CEDDA8FC19
	for <freebsd-gnats-submit@FreeBSD.org>; Mon,  7 Nov 2011 17:35:39 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id pA7HZdrC092296
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 7 Nov 2011 17:35:39 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id pA7HZd3N092295;
	Mon, 7 Nov 2011 17:35:39 GMT
	(envelope-from nobody)
Message-Id: <201111071735.pA7HZd3N092295@red.freebsd.org>
Date: Mon, 7 Nov 2011 17:35:39 GMT
From: Jukka Ukkonen <jau@iki.fi>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Enhancement: 
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         162352
>Category:       kern
>Synopsis:       [patch] Enhancement: add SO_PROTO to socket.h
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kib
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Nov 07 17:40:06 UTC 2011
>Closed-Date:    Sun Mar 11 09:37:47 UTC 2012
>Last-Modified:  Sun Mar 11 09:37:47 UTC 2012
>Originator:     Jukka Ukkonen
>Release:        FreeBSD-9.0-RC1
>Organization:
---
>Environment:
Sorry, the system I tried this on is powered down at this moment.
FreeBSD-9.0-RC1 still covers it all.

>Description:
In FreeBSD there has been getsockname()/getpeername() which have easily
provided the address family of a socket and getsockopt() with SO_TYPE
providing the communication type (stream, datagram, sequential packet,
reliable packet delivery) for the socket.  The thing that has been
missing has been an easy way to find the protocol used.

Now that we have 2 protocols TCP and SCTP for address families inet and
inet6 which can both be stream sockets we really need an easy way to ask
the kernel for the actual protocol.

The attached patch adds a new socket option SO_PROTO (aliased to
SO_PROTOCOL for Linux compatibility) to getsockopt().  I decided to call
the option SO_PROTO due to the fact that we already have a whole lot of
IPPROTO_this_and_that and I simply wanted to follow the tradition in
the naming.

Naturally this option cannot be used for setsockopt().

Assume we have a function which gets an anonymous socket as a call
argument or a program which receives an anonymous socket from an another
program over a UNIX domain socket or inherits one from its parent.

To do anything useful with the socket one would most likely have to know
what are the address family, the communication type, and the protocol
used with that socket.

An additional benefit will be improved portability between FreeBSD and
other UNIX type systems. At least Linux has a similar feature.

>How-To-Repeat:
No problem when the attached patch gets applied to the system.
This is just a very straight forward extension, not a bug as such.

The extension follows the exact same model that has already been used
for SO_TYPE.  So, it should be harmless and very useful.

>Fix:
Try to figure out the protocol used for a previously unknown socket inside
a function without the attached patch and the reasoning for this enhancement
should be obvious.

The patch should work roughly as-is also for older FreeBSD versions, but
notice that I have only tried it on FreeBSD-9.0-RC1.



Patch attached with submission follows:

--- src/sys/sys/socket.h.orig	2011-11-05 23:25:51.000000000 +0200
+++ src/sys/sys/socket.h	2011-11-06 09:21:41.000000000 +0200
@@ -139,6 +139,8 @@
 #define	SO_SETFIB	0x1014		/* use this FIB to route */
 #define	SO_USER_COOKIE	0x1015		/* user cookie (dummynet etc.) */
 #endif
+#define SO_PROTO	0x1016		/* Get socket protocol IPPROTO_xyz. */
+#define SO_PROTOCOL	SO_PROTO	/* Linux compatibility */
 
 /*
  * Structure used for manipulating linger option.
--- src/sys/kern/uipc_socket.c.orig	2011-11-05 23:20:34.000000000 +0200
+++ src/sys/kern/uipc_socket.c	2011-11-05 23:43:01.000000000 +0200
@@ -2730,6 +2730,10 @@
 			optval = so->so_type;
 			goto integer;
 
+		case SO_PROTO:
+			optval = so->so_proto->pr_protocol;
+			goto integer;
+
 		case SO_ERROR:
 			SOCK_LOCK(so);
 			optval = so->so_error;


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->freebsd-net 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Mon Nov 7 21:18:58 UTC 2011 
Responsible-Changed-Why:  
reclassify. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=162352 

From: "Jukka A. Ukkonen" <ext-jukka.1.ukkonen@nokia.com>
To: bug-followup@FreeBSD.org, jau@iki.fi
Cc:  
Subject: Re: kern/162352: [patch] Enhancement: add SO_PROTO to socket.h
Date: Tue, 08 Nov 2011 12:51:07 +0200

 On SunOS/Solaris the socket option with similar semantics
 seems to be called SO_PROTOTYPE.
 Does anyone have some HP-UX or AIX systems or some other
 alternative OS environments to check their naming for similar
 options, assuming they have similar options at all?
 
 --jau
 

From: "Jukka A. Ukkonen" <ext-jukka.1.ukkonen@nokia.com>
To: bug-followup@FreeBSD.org, jau@iki.fi
Cc:  
Subject: Re: kern/162352: [patch] Enhancement: add SO_PROTO to socket.h
Date: Tue, 15 Nov 2011 16:46:55 +0200

 Apparently also HP-UX uses the name SO_PROTOTYPE,
 though, with a somewhat odd twist.
 A comment in <sys/socket.h> implies that in HP-UX
 this option could be used with setsockopt() to push
 another protocol to a raw socket.
 
 --jau
Responsible-Changed-From-To: freebsd-net->kib-net 
Responsible-Changed-By: kib 
Responsible-Changed-When: Sun Feb 26 13:37:14 UTC 2012 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=162352 
Responsible-Changed-From-To: kib-net->kib 
Responsible-Changed-By: kib 
Responsible-Changed-When: Sun Feb 26 13:40:28 UTC 2012 
Responsible-Changed-Why:  
Fix typo. 

Noted by:	brueffer 

http://www.freebsd.org/cgi/query-pr.cgi?pr=162352 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/162352: commit references a PR
Date: Sun, 26 Feb 2012 13:55:58 +0000 (UTC)

 Author: kib
 Date: Sun Feb 26 13:55:43 2012
 New Revision: 232179
 URL: http://svn.freebsd.org/changeset/base/232179
 
 Log:
   Add SO_PROTOCOL/SO_PROTOTYPE socket SOL_SOCKET-level option to get the
   socket protocol number.  This is useful since the socket type can
   be implemented by different protocols in the same protocol family,
   e.g. SOCK_STREAM may be provided by both TCP and SCTP.
   
   Submitted by:	Jukka A. Ukkonen <jau iki fi>
   PR:	  kern/162352
   Discussed with:	bz
   Reviewed by:	glebius
   MFC after:	2 weeks
 
 Modified:
   head/sys/kern/uipc_socket.c
   head/sys/sys/socket.h
 
 Modified: head/sys/kern/uipc_socket.c
 ==============================================================================
 --- head/sys/kern/uipc_socket.c	Sun Feb 26 13:51:05 2012	(r232178)
 +++ head/sys/kern/uipc_socket.c	Sun Feb 26 13:55:43 2012	(r232179)
 @@ -2733,6 +2733,10 @@ integer:
  			optval = so->so_type;
  			goto integer;
  
 +		case SO_PROTOCOL:
 +			optval = so->so_proto->pr_protocol;
 +			goto integer;
 +
  		case SO_ERROR:
  			SOCK_LOCK(so);
  			optval = so->so_error;
 
 Modified: head/sys/sys/socket.h
 ==============================================================================
 --- head/sys/sys/socket.h	Sun Feb 26 13:51:05 2012	(r232178)
 +++ head/sys/sys/socket.h	Sun Feb 26 13:55:43 2012	(r232179)
 @@ -138,6 +138,8 @@ typedef	__uid_t		uid_t;
  #define	SO_LISTENINCQLEN	0x1013	/* socket's incomplete queue length */
  #define	SO_SETFIB	0x1014		/* use this FIB to route */
  #define	SO_USER_COOKIE	0x1015		/* user cookie (dummynet etc.) */
 +#define	SO_PROTOCOL	0x1016		/* get socket protocol (Linux name) */
 +#define	SO_PROTOTYPE	SO_PROTOCOL	/* alias for SO_PROTOCOL (SunOS name) */
  #endif
  
  /*
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/162352: commit references a PR
Date: Sun, 11 Mar 2012 00:49:08 +0000 (UTC)

 Author: kib
 Date: Sun Mar 11 00:48:54 2012
 New Revision: 232805
 URL: http://svn.freebsd.org/changeset/base/232805
 
 Log:
   MFC r232179:
   Add SO_PROTOCOL/SO_PROTOTYPE socket SOL_SOCKET-level option to get the
   socket protocol number.
   
   PR:	kern/162352
 
 Modified:
   stable/9/sys/kern/uipc_socket.c
   stable/9/sys/sys/socket.h
 Directory Properties:
   stable/9/sys/   (props changed)
 
 Modified: stable/9/sys/kern/uipc_socket.c
 ==============================================================================
 --- stable/9/sys/kern/uipc_socket.c	Sun Mar 11 00:43:01 2012	(r232804)
 +++ stable/9/sys/kern/uipc_socket.c	Sun Mar 11 00:48:54 2012	(r232805)
 @@ -2728,6 +2728,10 @@ integer:
  			optval = so->so_type;
  			goto integer;
  
 +		case SO_PROTOCOL:
 +			optval = so->so_proto->pr_protocol;
 +			goto integer;
 +
  		case SO_ERROR:
  			SOCK_LOCK(so);
  			optval = so->so_error;
 
 Modified: stable/9/sys/sys/socket.h
 ==============================================================================
 --- stable/9/sys/sys/socket.h	Sun Mar 11 00:43:01 2012	(r232804)
 +++ stable/9/sys/sys/socket.h	Sun Mar 11 00:48:54 2012	(r232805)
 @@ -138,6 +138,8 @@ typedef	__uid_t		uid_t;
  #define	SO_LISTENINCQLEN	0x1013	/* socket's incomplete queue length */
  #define	SO_SETFIB	0x1014		/* use this FIB to route */
  #define	SO_USER_COOKIE	0x1015		/* user cookie (dummynet etc.) */
 +#define	SO_PROTOCOL	0x1016		/* get socket protocol (Linux name) */
 +#define	SO_PROTOTYPE	SO_PROTOCOL	/* alias for SO_PROTOCOL (SunOS name) */
  #endif
  
  /*
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/162352: commit references a PR
Date: Sun, 11 Mar 2012 09:27:26 +0000 (UTC)

 Author: kib
 Date: Sun Mar 11 09:27:09 2012
 New Revision: 232819
 URL: http://svn.freebsd.org/changeset/base/232819
 
 Log:
   MFC r232179:
   Add SO_PROTOCOL/SO_PROTOTYPE socket SOL_SOCKET-level option to get the
   socket protocol number.
   
   PR:	kern/162352
 
 Modified:
   stable/8/sys/kern/uipc_socket.c
   stable/8/sys/sys/socket.h
 Directory Properties:
   stable/8/sys/   (props changed)
 
 Modified: stable/8/sys/kern/uipc_socket.c
 ==============================================================================
 --- stable/8/sys/kern/uipc_socket.c	Sun Mar 11 09:22:05 2012	(r232818)
 +++ stable/8/sys/kern/uipc_socket.c	Sun Mar 11 09:27:09 2012	(r232819)
 @@ -2713,6 +2713,10 @@ integer:
  			optval = so->so_type;
  			goto integer;
  
 +		case SO_PROTOCOL:
 +			optval = so->so_proto->pr_protocol;
 +			goto integer;
 +
  		case SO_ERROR:
  			SOCK_LOCK(so);
  			optval = so->so_error;
 
 Modified: stable/8/sys/sys/socket.h
 ==============================================================================
 --- stable/8/sys/sys/socket.h	Sun Mar 11 09:22:05 2012	(r232818)
 +++ stable/8/sys/sys/socket.h	Sun Mar 11 09:27:09 2012	(r232819)
 @@ -139,6 +139,8 @@ typedef	__uid_t		uid_t;
  #define	SO_LISTENQLEN	0x1012		/* socket's complete queue length */
  #define	SO_LISTENINCQLEN	0x1013	/* socket's incomplete queue length */
  #define	SO_SETFIB	0x1014		/* use this FIB to route */
 +#define	SO_PROTOCOL	0x1016		/* get socket protocol (Linux name) */
 +#define	SO_PROTOTYPE	SO_PROTOCOL	/* alias for SO_PROTOCOL (SunOS name) */
  #endif
  
  /*
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->closed 
State-Changed-By: kib 
State-Changed-When: Sun Mar 11 09:37:27 UTC 2012 
State-Changed-Why:  
Merged to 9 and 8. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=162352 
>Unformatted:
