From f02cb2e83cda2e7c843af31d86dd656fd741d8ab Mon Sep 17 00:00:00 2001 From: bluhm Date: Mon, 10 Sep 2018 16:14:07 +0000 Subject: [PATCH] Instead of calculating the mbuf packet header length here and there, put the algorithm into a new function m_calchdrlen(). Also set an uninitialized m_len to 0 in NFS code. OK claudio@ --- share/man/man9/mbuf.9 | 8 +++++++- sys/kern/uipc_mbuf.c | 14 +++++++++++++- sys/net/pf_norm.c | 23 +++-------------------- sys/netinet/ip_input.c | 11 ++--------- sys/netinet6/frag6.c | 11 ++--------- sys/nfs/krpc_subr.c | 10 ++-------- sys/nfs/nfs_socket.c | 13 +++---------- sys/sys/mbuf.h | 3 ++- 8 files changed, 34 insertions(+), 59 deletions(-) diff --git a/share/man/man9/mbuf.9 b/share/man/man9/mbuf.9 index c8a9946de0c..15b1b701368 100644 --- a/share/man/man9/mbuf.9 +++ b/share/man/man9/mbuf.9 @@ -1,4 +1,4 @@ -.\" $OpenBSD: mbuf.9,v 1.113 2018/09/10 13:52:37 jmc Exp $ +.\" $OpenBSD: mbuf.9,v 1.114 2018/09/10 16:14:07 bluhm Exp $ .\" .\" Copyright (c) 2001 Jean-Jacques Bernard-Gundol .\" All rights reserved. @@ -37,6 +37,7 @@ .Nm m_gethdr , .Nm m_removehdr , .Nm m_resethdr , +.Nm m_calchdrlen , .Nm MGETHDR , .Nm m_prepend , .Nm M_PREPEND , @@ -83,6 +84,8 @@ .Fn m_removehdr "struct mbuf *m" .Ft void .Fn m_resethdr "struct mbuf *m" +.Ft void +.Fn m_calchdrlen "struct mbuf *m" .Ft struct mbuf * .Fn m_gethdr "int how" "int type" .Fn MGETHDR "struct mbuf *m" "int how" "int type" @@ -502,6 +505,9 @@ Delete all data and all tags attached to an .Fa mbuf . Keep the data and mbuf chain, initialize the packet header. +.It Fn m_calchdrlen "struct mbuf *m" +Set the packet header length to the sum of all length values in the +mbuf chain. .It Fn m_gethdr "int how" "int type" Return a pointer to an mbuf of the type specified after initializing it to contain a packet header. diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c index 2dda4b56292..4349c02e882 100644 --- a/sys/kern/uipc_mbuf.c +++ b/sys/kern/uipc_mbuf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_mbuf.c,v 1.257 2018/09/10 12:47:02 bluhm Exp $ */ +/* $OpenBSD: uipc_mbuf.c,v 1.258 2018/09/10 16:14:07 bluhm Exp $ */ /* $NetBSD: uipc_mbuf.c,v 1.15.4.1 1996/06/13 17:11:44 cgd Exp $ */ /* @@ -327,6 +327,18 @@ m_resethdr(struct mbuf *m) m->m_pkthdr.ph_loopcnt = loopcnt; } +void +m_calchdrlen(struct mbuf *m) +{ + struct mbuf *n; + int plen = 0; + + KASSERT(m->m_flags & M_PKTHDR); + for (n = m; n; n = n->m_next) + plen += n->m_len; + m->m_pkthdr.len = plen; +} + struct mbuf * m_getclr(int nowait, int type) { diff --git a/sys/net/pf_norm.c b/sys/net/pf_norm.c index d60dd43442b..20503b82b57 100644 --- a/sys/net/pf_norm.c +++ b/sys/net/pf_norm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pf_norm.c,v 1.215 2018/09/10 12:47:02 bluhm Exp $ */ +/* $OpenBSD: pf_norm.c,v 1.216 2018/09/10 16:14:07 bluhm Exp $ */ /* * Copyright 2001 Niels Provos @@ -805,16 +805,7 @@ pf_reassemble(struct mbuf **m0, int dir, u_short *reason) hdrlen = frent->fe_hdrlen; m = *m0 = pf_join_fragment(frag); frag = NULL; - - { - int plen = 0; - - KASSERT(m->m_flags & M_PKTHDR); - for (m = *m0; m; m = m->m_next) - plen += m->m_len; - m = *m0; - m->m_pkthdr.len = plen; - } + m_calchdrlen(m); ip = mtod(m, struct ip *); ip->ip_len = htons(hdrlen + total); @@ -907,15 +898,7 @@ pf_reassemble6(struct mbuf **m0, struct ip6_frag *fraghdr, if (frag6_deletefraghdr(m, hdrlen) != 0) goto fail; - { - int plen = 0; - - KASSERT(m->m_flags & M_PKTHDR); - for (m = *m0; m; m = m->m_next) - plen += m->m_len; - m = *m0; - m->m_pkthdr.len = plen; - } + m_calchdrlen(m); if ((mtag = m_tag_get(PACKET_TAG_PF_REASSEMBLED, sizeof(struct pf_fragment_tag), M_NOWAIT)) == NULL) diff --git a/sys/netinet/ip_input.c b/sys/netinet/ip_input.c index 3e9bab33b36..9d248132686 100644 --- a/sys/netinet/ip_input.c +++ b/sys/netinet/ip_input.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_input.c,v 1.339 2018/09/10 12:47:02 bluhm Exp $ */ +/* $OpenBSD: ip_input.c,v 1.340 2018/09/10 16:14:07 bluhm Exp $ */ /* $NetBSD: ip_input.c,v 1.30 1996/03/16 23:53:58 christos Exp $ */ /* @@ -964,14 +964,7 @@ insert: pool_put(&ipq_pool, fp); m->m_len += (ip->ip_hl << 2); m->m_data -= (ip->ip_hl << 2); - { - int plen = 0; - - KASSERT(m->m_flags & M_PKTHDR); - for (t = m; t; t = t->m_next) - plen += t->m_len; - m->m_pkthdr.len = plen; - } + m_calchdrlen(m); return (m); dropfrag: diff --git a/sys/netinet6/frag6.c b/sys/netinet6/frag6.c index 5e8721f15e2..aae4f0c7c9a 100644 --- a/sys/netinet6/frag6.c +++ b/sys/netinet6/frag6.c @@ -1,4 +1,4 @@ -/* $OpenBSD: frag6.c,v 1.84 2018/09/10 12:47:02 bluhm Exp $ */ +/* $OpenBSD: frag6.c,v 1.85 2018/09/10 16:14:08 bluhm Exp $ */ /* $KAME: frag6.c,v 1.40 2002/05/27 21:40:31 itojun Exp $ */ /* @@ -431,14 +431,7 @@ frag6_input(struct mbuf **mp, int *offp, int proto, int af) pool_put(&ip6q_pool, q6); - { - int plen = 0; - - KASSERT(m->m_flags & M_PKTHDR); - for (t = m; t; t = t->m_next) - plen += t->m_len; - m->m_pkthdr.len = plen; - } + m_calchdrlen(m); /* * Restore NXT to the original. diff --git a/sys/nfs/krpc_subr.c b/sys/nfs/krpc_subr.c index 5c3c62ecba4..eee2f371201 100644 --- a/sys/nfs/krpc_subr.c +++ b/sys/nfs/krpc_subr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: krpc_subr.c,v 1.34 2018/07/30 12:22:14 mpi Exp $ */ +/* $OpenBSD: krpc_subr.c,v 1.35 2018/09/10 16:14:08 bluhm Exp $ */ /* $NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $ */ /* @@ -339,13 +339,7 @@ krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func, /* * Setup packet header */ - len = 0; - m = mhead; - while (m) { - len += m->m_len; - m = m->m_next; - } - mhead->m_pkthdr.len = len; + m_calchdrlen(mhead); mhead->m_pkthdr.ph_ifidx = 0; /* diff --git a/sys/nfs/nfs_socket.c b/sys/nfs/nfs_socket.c index 33d6ba14d76..7b2c59b5668 100644 --- a/sys/nfs/nfs_socket.c +++ b/sys/nfs/nfs_socket.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nfs_socket.c,v 1.130 2018/07/30 12:22:14 mpi Exp $ */ +/* $OpenBSD: nfs_socket.c,v 1.131 2018/09/10 16:14:08 bluhm Exp $ */ /* $NetBSD: nfs_socket.c,v 1.27 1996/04/15 20:20:00 thorpej Exp $ */ /* @@ -855,7 +855,6 @@ nfs_request(struct vnode *vp, int procnum, struct nfsm_info *infop) int t1, i, error = 0; int trylater_delay; struct nfsreq *rep; - int mrest_len; struct nfsm_info info; rep = pool_get(&nfsreqpl, PR_WAITOK); @@ -864,17 +863,11 @@ nfs_request(struct vnode *vp, int procnum, struct nfsm_info *infop) rep->r_procp = infop->nmi_procp; rep->r_procnum = procnum; - mrest_len = 0; - m = infop->nmi_mreq; - while (m) { - mrest_len += m->m_len; - m = m->m_next; - } - /* empty mbuf for AUTH_UNIX header */ rep->r_mreq = m_gethdr(M_WAIT, MT_DATA); rep->r_mreq->m_next = infop->nmi_mreq; - rep->r_mreq->m_pkthdr.len = mrest_len; + rep->r_mreq->m_len = 0; + m_calchdrlen(rep->r_mreq); trylater_delay = NFS_MINTIMEO; diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h index 8a9df640faf..2513c484922 100644 --- a/sys/sys/mbuf.h +++ b/sys/sys/mbuf.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mbuf.h,v 1.237 2018/09/10 12:47:02 bluhm Exp $ */ +/* $OpenBSD: mbuf.h,v 1.238 2018/09/10 16:14:08 bluhm Exp $ */ /* $NetBSD: mbuf.h,v 1.19 1996/02/09 18:25:14 christos Exp $ */ /* @@ -443,6 +443,7 @@ struct mbuf *m_gethdr(int, int); struct mbuf *m_inithdr(struct mbuf *); void m_removehdr(struct mbuf *); void m_resethdr(struct mbuf *); +void m_calchdrlen(struct mbuf *); int m_defrag(struct mbuf *, int); struct mbuf *m_prepend(struct mbuf *, int, int); struct mbuf *m_pulldown(struct mbuf *, int, int, int *); -- 2.20.1