Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys Reduce code duplication.
details: https://anonhg.NetBSD.org/src/rev/f1f9f1fa6bfb
branches: trunk
changeset: 343573:f1f9f1fa6bfb
user: rtr <rtr%NetBSD.org@localhost>
date: Mon Feb 15 14:59:03 2016 +0000
description:
Reduce code duplication.
Split creation of IPv4-Mapped IPv6 addresses into its own function
and use it.
No functional change intended. As posted to tech-net@
diffstat:
sys/netinet/in_pcb.c | 9 +++------
sys/netinet/tcp_input.c | 21 +++++----------------
sys/netinet/tcp_subr.c | 15 +++++----------
sys/netinet/tcp_usrreq.c | 7 +++++--
sys/netinet/udp_usrreq.c | 12 ++++--------
sys/netinet6/in6.c | 22 ++++++++++++++++------
sys/netinet6/in6.h | 3 ++-
7 files changed, 40 insertions(+), 49 deletions(-)
diffs (254 lines):
diff -r cb67cef6f912 -r f1f9f1fa6bfb sys/netinet/in_pcb.c
--- a/sys/netinet/in_pcb.c Mon Feb 15 13:25:12 2016 +0000
+++ b/sys/netinet/in_pcb.c Mon Feb 15 14:59:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: in_pcb.c,v 1.162 2015/08/24 22:21:26 pooka Exp $ */
+/* $NetBSD: in_pcb.c,v 1.163 2016/02/15 14:59:03 rtr Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -93,7 +93,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.162 2015/08/24 22:21:26 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.163 2016/02/15 14:59:03 rtr Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
@@ -347,10 +347,7 @@
return (EACCES);
#ifdef INET6
- memset(&mapped, 0, sizeof(mapped));
- mapped.s6_addr16[5] = 0xffff;
- memcpy(&mapped.s6_addr32[3], &sin->sin_addr,
- sizeof(mapped.s6_addr32[3]));
+ in6_in_2_v4mapin6(&sin->sin_addr, &mapped);
t6 = in6_pcblookup_port(table, &mapped, sin->sin_port, wild, &vestige);
if (t6 && (reuseport & t6->in6p_socket->so_options) == 0)
return (EADDRINUSE);
diff -r cb67cef6f912 -r f1f9f1fa6bfb sys/netinet/tcp_input.c
--- a/sys/netinet/tcp_input.c Mon Feb 15 13:25:12 2016 +0000
+++ b/sys/netinet/tcp_input.c Mon Feb 15 14:59:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tcp_input.c,v 1.344 2015/08/24 22:21:26 pooka Exp $ */
+/* $NetBSD: tcp_input.c,v 1.345 2016/02/15 14:59:03 rtr Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -148,7 +148,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.344 2015/08/24 22:21:26 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.345 2016/02/15 14:59:03 rtr Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
@@ -1424,12 +1424,8 @@
struct in6_addr s, d;
/* mapped addr case */
- memset(&s, 0, sizeof(s));
- s.s6_addr16[5] = htons(0xffff);
- bcopy(&ip->ip_src, &s.s6_addr32[3], sizeof(ip->ip_src));
- memset(&d, 0, sizeof(d));
- d.s6_addr16[5] = htons(0xffff);
- bcopy(&ip->ip_dst, &d.s6_addr32[3], sizeof(ip->ip_dst));
+ in6_in_2_v4mapin6(&ip->ip_src, &s);
+ in6_in_2_v4mapin6(&ip->ip_dst, &d);
in6p = in6_pcblookup_connect(&tcbtable, &s,
th->th_sport, &d, th->th_dport,
0, &vestige);
@@ -4086,14 +4082,7 @@
memcpy(&sin6, src, src->sa_len);
if (src->sa_family == AF_INET) {
/* IPv4 packet to AF_INET6 socket */
- memset(&sin6, 0, sizeof(sin6));
- sin6.sin6_family = AF_INET6;
- sin6.sin6_len = sizeof(sin6);
- sin6.sin6_port = ((struct sockaddr_in *)src)->sin_port;
- sin6.sin6_addr.s6_addr16[5] = htons(0xffff);
- bcopy(&((struct sockaddr_in *)src)->sin_addr,
- &sin6.sin6_addr.s6_addr32[3],
- sizeof(sin6.sin6_addr.s6_addr32[3]));
+ in6_sin_2_v4mapsin6((struct sockaddr_in *)src, &sin6);
}
if (in6_pcbconnect(in6p, &sin6, NULL)) {
goto resetandabort;
diff -r cb67cef6f912 -r f1f9f1fa6bfb sys/netinet/tcp_subr.c
--- a/sys/netinet/tcp_subr.c Mon Feb 15 13:25:12 2016 +0000
+++ b/sys/netinet/tcp_subr.c Mon Feb 15 14:59:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tcp_subr.c,v 1.264 2015/09/07 01:56:50 ozaki-r Exp $ */
+/* $NetBSD: tcp_subr.c,v 1.265 2016/02/15 14:59:03 rtr Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -91,7 +91,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tcp_subr.c,v 1.264 2015/09/07 01:56:50 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tcp_subr.c,v 1.265 2016/02/15 14:59:03 rtr Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
@@ -1603,11 +1603,8 @@
*/
th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
#ifdef INET6
- memset(&src6, 0, sizeof(src6));
- memset(&dst6, 0, sizeof(dst6));
- src6.s6_addr16[5] = dst6.s6_addr16[5] = 0xffff;
- memcpy(&src6.s6_addr32[3], &ip->ip_src, sizeof(struct in_addr));
- memcpy(&dst6.s6_addr32[3], &ip->ip_dst, sizeof(struct in_addr));
+ in6_in_2_v4mapin6(&ip->ip_src, &src6);
+ in6_in_2_v4mapin6(&ip->ip_dst, &dst6);
#endif
if ((inp = in_pcblookup_connect(&tcbtable, ip->ip_dst,
th->th_dport, ip->ip_src, th->th_sport, 0)) != NULL)
@@ -1752,9 +1749,7 @@
in_pcbnotifyall(&tcbtable, faddr, EMSGSIZE, tcp_mtudisc);
#ifdef INET6
- memset(&in6, 0, sizeof(in6));
- in6.s6_addr16[5] = 0xffff;
- memcpy(&in6.s6_addr32[3], &faddr, sizeof(struct in_addr));
+ in6_in_2_v4mapin6(&faddr, &in6);
tcp6_mtudisc_callback(&in6);
#endif
}
diff -r cb67cef6f912 -r f1f9f1fa6bfb sys/netinet/tcp_usrreq.c
--- a/sys/netinet/tcp_usrreq.c Mon Feb 15 13:25:12 2016 +0000
+++ b/sys/netinet/tcp_usrreq.c Mon Feb 15 14:59:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tcp_usrreq.c,v 1.210 2016/02/14 23:47:57 rtr Exp $ */
+/* $NetBSD: tcp_usrreq.c,v 1.211 2016/02/15 14:59:03 rtr Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -99,7 +99,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.210 2016/02/14 23:47:57 rtr Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.211 2016/02/15 14:59:03 rtr Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
@@ -136,6 +136,9 @@
#include <netinet/in_offload.h>
#ifdef INET6
+#ifndef INET
+#include <netinet/in.h>
+#endif
#include <netinet/ip6.h>
#include <netinet6/in6_pcb.h>
#include <netinet6/ip6_var.h>
diff -r cb67cef6f912 -r f1f9f1fa6bfb sys/netinet/udp_usrreq.c
--- a/sys/netinet/udp_usrreq.c Mon Feb 15 13:25:12 2016 +0000
+++ b/sys/netinet/udp_usrreq.c Mon Feb 15 14:59:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: udp_usrreq.c,v 1.223 2016/01/20 22:01:18 riastradh Exp $ */
+/* $NetBSD: udp_usrreq.c,v 1.224 2016/02/15 14:59:03 rtr Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -66,7 +66,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.223 2016/01/20 22:01:18 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.224 2016/02/15 14:59:03 rtr Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
@@ -405,16 +405,12 @@
memset(&src6, 0, sizeof(src6));
src6.sin6_family = AF_INET6;
src6.sin6_len = sizeof(struct sockaddr_in6);
- src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
- memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
- sizeof(ip->ip_src));
+ in6_in_2_v4mapin6(&ip->ip_src, &src6.sin6_addr);
src6.sin6_port = uh->uh_sport;
memset(&dst6, 0, sizeof(dst6));
dst6.sin6_family = AF_INET6;
dst6.sin6_len = sizeof(struct sockaddr_in6);
- dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
- memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
- sizeof(ip->ip_dst));
+ in6_in_2_v4mapin6(&ip->ip_dst, &dst6.sin6_addr);
dst6.sin6_port = uh->uh_dport;
n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
diff -r cb67cef6f912 -r f1f9f1fa6bfb sys/netinet6/in6.c
--- a/sys/netinet6/in6.c Mon Feb 15 13:25:12 2016 +0000
+++ b/sys/netinet6/in6.c Mon Feb 15 14:59:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: in6.c,v 1.194 2015/12/12 23:34:25 christos Exp $ */
+/* $NetBSD: in6.c,v 1.195 2016/02/15 14:59:03 rtr Exp $ */
/* $KAME: in6.c,v 1.198 2001/07/18 09:12:38 itojun Exp $ */
/*
@@ -62,7 +62,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: in6.c,v 1.194 2015/12/12 23:34:25 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: in6.c,v 1.195 2016/02/15 14:59:03 rtr Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
@@ -2553,6 +2553,19 @@
}
/*
+ * Convert IPv4 address stored in struct in_addr to IPv4-Mapped IPv6 address
+ * stored in struct in6_addr as defined in RFC 4921 section 2.5.5.2.
+ */
+void
+in6_in_2_v4mapin6(const struct in_addr *in, struct in6_addr *in6)
+{
+ in6->s6_addr32[0] = 0;
+ in6->s6_addr32[1] = 0;
+ in6->s6_addr32[2] = IPV6_ADDR_INT32_SMP;
+ in6->s6_addr32[3] = in->s_addr;
+}
+
+/*
* Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
* v4 mapped addr or v4 compat addr
*/
@@ -2574,10 +2587,7 @@
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_family = AF_INET6;
sin6->sin6_port = sin->sin_port;
- sin6->sin6_addr.s6_addr32[0] = 0;
- sin6->sin6_addr.s6_addr32[1] = 0;
- sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
- sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
+ in6_in_2_v4mapin6(&sin->sin_addr, &sin6->sin6_addr);
}
/* Convert sockaddr_in6 into sockaddr_in. */
diff -r cb67cef6f912 -r f1f9f1fa6bfb sys/netinet6/in6.h
--- a/sys/netinet6/in6.h Mon Feb 15 13:25:12 2016 +0000
+++ b/sys/netinet6/in6.h Mon Feb 15 14:59:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: in6.h,v 1.86 2015/12/12 23:34:25 christos Exp $ */
+/* $NetBSD: in6.h,v 1.87 2016/02/15 14:59:03 rtr Exp $ */
/* $KAME: in6.h,v 1.83 2001/03/29 02:55:07 jinmei Exp $ */
/*
@@ -768,6 +768,7 @@
__BEGIN_DECLS
struct cmsghdr;
+void in6_in_2_v4mapin6(const struct in_addr *, struct in6_addr *);
void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *);
void in6_sin_2_v4mapsin6(const struct sockaddr_in *, struct sockaddr_in6 *);
void in6_sin6_2_sin_in_sock(struct sockaddr *);
Home |
Main Index |
Thread Index |
Old Index