Module Name:    src
Committed By:   minskim
Date:           Thu Jul 16 04:09:51 UTC 2009

Modified Files:
        src/share/man/man4: ip.4
        src/sys/netinet: in.h in_pcb.h ip_input.c ip_output.c

Log Message:
Add the IP_RECVTTL option support.

If the IP_RECVTTL option is enabled on a SOCK_DGRAM socket, the
recvmsg(2) call will return the TTL of the received datagram.  The
msg_control field in the msghdr structure points to a buffer that
contains a cmsghdr structure followed by the TTL value.

Modeled after FreeBSD implementation.


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/share/man/man4/ip.4
cvs rdiff -u -r1.83 -r1.84 src/sys/netinet/in.h
cvs rdiff -u -r1.45 -r1.46 src/sys/netinet/in_pcb.h
cvs rdiff -u -r1.281 -r1.282 src/sys/netinet/ip_input.c
cvs rdiff -u -r1.203 -r1.204 src/sys/netinet/ip_output.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/share/man/man4/ip.4
diff -u src/share/man/man4/ip.4:1.23 src/share/man/man4/ip.4:1.24
--- src/share/man/man4/ip.4:1.23	Tue Sep 23 14:58:05 2008
+++ src/share/man/man4/ip.4	Thu Jul 16 04:09:51 2009
@@ -1,4 +1,4 @@
-.\"	$NetBSD: ip.4,v 1.23 2008/09/23 14:58:05 briggs Exp $
+.\"	$NetBSD: ip.4,v 1.24 2009/07/16 04:09:51 minskim Exp $
 .\"
 .\" Copyright (c) 1983, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)ip.4	8.2 (Berkeley) 11/30/93
 .\"
-.Dd September 23, 2008
+.Dd July 16, 2009
 .Dt IP 4
 .Os
 .Sh NAME
@@ -165,6 +165,26 @@
 cmsg_level = IPPROTO_IP
 cmsg_type = IP_RECVIF
 .Ed
+.Pp
+If the
+.Dv IP_RECVTTL
+option is enabled on a
+.Dv SOCK_DGRAM
+socket, the
+.Xr recvmsg 2
+call will return the
+.Tn TTL
+of the received datagram.
+The msg_control field in the msghdr structure points to a buffer
+that contains a cmsghdr structure followed by the
+.Tn TTL
+value.
+The cmsghdr fields have the following values:
+.Bd -literal
+cmsg_len = sizeof(uint8_t)
+cmsg_level = IPPROTO_IP
+cmsg_type = IP_RECVTTL
+.Ed
 .Ss MULTICAST OPTIONS
 .Tn IP
 multicasting is supported only on

Index: src/sys/netinet/in.h
diff -u src/sys/netinet/in.h:1.83 src/sys/netinet/in.h:1.84
--- src/sys/netinet/in.h:1.83	Fri Jan 25 21:12:14 2008
+++ src/sys/netinet/in.h	Thu Jul 16 04:09:51 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: in.h,v 1.83 2008/01/25 21:12:14 joerg Exp $	*/
+/*	$NetBSD: in.h,v 1.84 2009/07/16 04:09:51 minskim Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1990, 1993
@@ -282,6 +282,7 @@
 #if 1 /*IPSEC*/
 #define	IP_IPSEC_POLICY		22 /* struct; get/set security policy */
 #endif
+#define	IP_RECVTTL		23   /* bool; receive IP TTL w/dgram */
 
 /*
  * Defaults and limits for options

Index: src/sys/netinet/in_pcb.h
diff -u src/sys/netinet/in_pcb.h:1.45 src/sys/netinet/in_pcb.h:1.46
--- src/sys/netinet/in_pcb.h:1.45	Sun Dec 16 18:39:57 2007
+++ src/sys/netinet/in_pcb.h	Thu Jul 16 04:09:51 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: in_pcb.h,v 1.45 2007/12/16 18:39:57 elad Exp $	*/
+/*	$NetBSD: in_pcb.h,v 1.46 2009/07/16 04:09:51 minskim Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -108,8 +108,6 @@
 /* XXX should move to an UDP control block */
 #define INP_ESPINUDP		0x100	/* ESP over UDP for NAT-T */
 #define INP_ESPINUDP_NON_IKE	0x200	/* ESP over UDP for NAT-T */
-#define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
-				INP_RECVIF)
 #define INP_ESPINUDP_ALL	(INP_ESPINUDP|INP_ESPINUDP_NON_IKE)
 #define INP_NOHEADER		0x400	/* Kernel removes IP header
 					 * before feeding a packet
@@ -118,6 +116,9 @@
 					 * not supply an IP header.
 					 * Cancels INP_HDRINCL.
 					 */
+#define	INP_RECVTTL		0x800	/* receive incoming IP TTL */
+#define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
+				INP_RECVIF|INP_RECVTTL)
 
 #define	sotoinpcb(so)		((struct inpcb *)(so)->so_pcb)
 

Index: src/sys/netinet/ip_input.c
diff -u src/sys/netinet/ip_input.c:1.281 src/sys/netinet/ip_input.c:1.282
--- src/sys/netinet/ip_input.c:1.281	Sat Apr 18 14:58:05 2009
+++ src/sys/netinet/ip_input.c	Thu Jul 16 04:09:51 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip_input.c,v 1.281 2009/04/18 14:58:05 tsutsui Exp $	*/
+/*	$NetBSD: ip_input.c,v 1.282 2009/07/16 04:09:51 minskim Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -91,7 +91,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.281 2009/04/18 14:58:05 tsutsui Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.282 2009/07/16 04:09:51 minskim Exp $");
 
 #include "opt_inet.h"
 #include "opt_compat_netbsd.h"
@@ -2118,6 +2118,12 @@
 		if (*mp)
 			mp = &(*mp)->m_next;
 	}
+	if (inp->inp_flags & INP_RECVTTL) {
+		*mp = sbcreatecontrol((void *) &ip->ip_ttl,
+		    sizeof(uint8_t), IP_RECVTTL, IPPROTO_IP);
+		if (*mp)
+			mp = &(*mp)->m_next;
+	}
 }
 
 /*

Index: src/sys/netinet/ip_output.c
diff -u src/sys/netinet/ip_output.c:1.203 src/sys/netinet/ip_output.c:1.204
--- src/sys/netinet/ip_output.c:1.203	Wed Jul  1 14:47:54 2009
+++ src/sys/netinet/ip_output.c	Thu Jul 16 04:09:51 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip_output.c,v 1.203 2009/07/01 14:47:54 martin Exp $	*/
+/*	$NetBSD: ip_output.c,v 1.204 2009/07/16 04:09:51 minskim Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -91,7 +91,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.203 2009/07/01 14:47:54 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.204 2009/07/16 04:09:51 minskim Exp $");
 
 #include "opt_pfil_hooks.h"
 #include "opt_inet.h"
@@ -1227,6 +1227,7 @@
 		case IP_RECVRETOPTS:
 		case IP_RECVDSTADDR:
 		case IP_RECVIF:
+		case IP_RECVTTL:
 			error = sockopt_getint(sopt, &optval);
 			if (error)
 				break;
@@ -1260,6 +1261,10 @@
 			case IP_RECVIF:
 				OPTSET(INP_RECVIF);
 				break;
+
+			case IP_RECVTTL:
+				OPTSET(INP_RECVTTL);
+				break;
 			}
 		break;
 #undef OPTSET
@@ -1334,6 +1339,7 @@
 		case IP_RECVRETOPTS:
 		case IP_RECVDSTADDR:
 		case IP_RECVIF:
+		case IP_RECVTTL:
 		case IP_ERRORMTU:
 			switch (sopt->sopt_name) {
 			case IP_TOS:
@@ -1365,6 +1371,10 @@
 			case IP_RECVIF:
 				optval = OPTBIT(INP_RECVIF);
 				break;
+
+			case IP_RECVTTL:
+				optval = OPTBIT(INP_RECVTTL);
+				break;
 			}
 			error = sockopt_setint(sopt, optval);
 			break;

Reply via email to