Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/net - If if_attach() failed in the attach function, free...
details: https://anonhg.NetBSD.org/src/rev/ac9d7a580ebb
branches: trunk
changeset: 357017:ac9d7a580ebb
user: msaitoh <msaitoh%NetBSD.org@localhost>
date: Mon Oct 23 09:32:55 2017 +0000
description:
- If if_attach() failed in the attach function, free resources and return.
- KNF
diffstat:
sys/net/if_srt.c | 36 ++++++++++++++++++++++--------------
sys/net/if_stf.c | 23 ++++++++++++++++-------
2 files changed, 38 insertions(+), 21 deletions(-)
diffs (189 lines):
diff -r 7c0a53a2e4a1 -r ac9d7a580ebb sys/net/if_srt.c
--- a/sys/net/if_srt.c Mon Oct 23 09:32:33 2017 +0000
+++ b/sys/net/if_srt.c Mon Oct 23 09:32:55 2017 +0000
@@ -1,8 +1,8 @@
-/* $NetBSD: if_srt.c,v 1.26 2017/02/14 03:05:06 ozaki-r Exp $ */
+/* $NetBSD: if_srt.c,v 1.27 2017/10/23 09:32:55 msaitoh Exp $ */
/* This file is in the public domain. */
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_srt.c,v 1.26 2017/02/14 03:05:06 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_srt.c,v 1.27 2017/10/23 09:32:55 msaitoh Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
@@ -88,7 +88,7 @@
if (sc->flags & SSF_MTULOCK)
return;
mtu = 65535;
- for (i=sc->nrt-1;i>=0;i--) {
+ for (i = sc->nrt-1; i>=0; i--) {
r = sc->rts[i];
if (r->u.dstifp->if_mtu < mtu)
mtu = r->u.dstifp->if_mtu;
@@ -249,12 +249,13 @@
srt_clone_create(struct if_clone *cl, int unit)
{
struct srt_softc *sc;
+ int rv;
if (unit < 0 || unit > SRT_MAXUNIT)
return ENXIO;
if (softcv[unit])
return EBUSY;
- sc = malloc(sizeof(struct srt_softc), M_DEVBUF, M_WAITOK|M_ZERO);
+ sc = malloc(sizeof(struct srt_softc), M_DEVBUF, M_WAITOK | M_ZERO);
sc->unit = unit;
sc->nrt = 0;
sc->rts = 0;
@@ -268,7 +269,13 @@
sc->intf.if_ioctl = &srt_if_ioctl;
sc->intf.if_output = &srt_if_output;
sc->intf.if_dlt = DLT_RAW;
- if_attach(&sc->intf);
+ rv = if_attach(&sc->intf);
+ if (rv != 0) {
+ aprint_error("%s: if_initialize failed(%d)\n",
+ sc->intf.if_xname, rv);
+ free(sc, M_DEVBUF);
+ return rv;
+ }
if_alloc_sadl(&sc->intf);
#ifdef BPFILTER_NOW_AVAILABLE
bpf_attach(&sc->intf, 0, 0);
@@ -295,16 +302,16 @@
}
if (softcv[sc->unit] != sc) {
panic("srt_clone_destroy: bad backpointer ([%d]=%p not %p)\n",
- sc->unit,(void *)softcv[sc->unit],(void *)sc);
+ sc->unit, (void *)softcv[sc->unit], (void *)sc);
}
softcv[sc->unit] = 0;
- free(sc,M_DEVBUF);
+ free(sc, M_DEVBUF);
atomic_inc_uint(&srt_count);
return 0;
}
struct if_clone srt_clone =
- IF_CLONE_INITIALIZER("srt",&srt_clone_create,&srt_clone_destroy);
+ IF_CLONE_INITIALIZER("srt", &srt_clone_create, &srt_clone_destroy);
void
srtattach(int n)
@@ -408,8 +415,9 @@
dr->af = scr->af;
dr->srcmatch = scr->srcmatch;
dr->srcmask = scr->srcmask;
- strlcpy(&dr->u.dstifn[0],&scr->u.dstifp->if_xname[0],IFNAMSIZ);
- memcpy(&dr->dst,&scr->dst,scr->dst.sa.sa_len);
+ strlcpy(&dr->u.dstifn[0], &scr->u.dstifp->if_xname[0],
+ IFNAMSIZ);
+ memcpy(&dr->dst, &scr->dst, scr->dst.sa.sa_len);
return 0;
case SRT_SETRT:
if (! (flag & FWRITE))
@@ -417,7 +425,7 @@
dr = (struct srt_rt *) data;
if (dr->inx > sc->nrt)
return EDOM;
- strlcpy(&nbuf[0],&dr->u.dstifn[0],IFNAMSIZ);
+ strlcpy(&nbuf[0], &dr->u.dstifn[0], IFNAMSIZ);
nbuf[IFNAMSIZ-1] = '\0';
if (dr->dst.sa.sa_family != dr->af)
return EIO;
@@ -460,7 +468,7 @@
}
scr = sc->rts[dr->inx];
if (scr == 0) {
- scr = malloc(sizeof(struct srt_rt),M_DEVBUF,M_WAITOK);
+ scr = malloc(sizeof(struct srt_rt), M_DEVBUF,M_WAITOK);
if (scr == 0)
return ENOBUFS;
scr->inx = dr->inx;
@@ -488,7 +496,7 @@
sc->nrt--;
if (i < sc->nrt) {
memcpy(sc->rts+i, sc->rts+i+1,
- (sc->nrt-i)*sizeof(*sc->rts));
+ (sc->nrt-i) * sizeof(*sc->rts));
}
if (sc->nrt == 0) {
free(sc->rts, M_DEVBUF);
@@ -510,7 +518,7 @@
*(unsigned int *)data = sc->flags | global_flags;
return 0;
case SRT_SGFLAGS:
- if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD))
+ if ((flag & (FWRITE | FREAD)) != (FWRITE | FREAD))
return EBADF;
o = sc->flags | global_flags;
n = *(unsigned int *)data & SSF_UCHG;
diff -r 7c0a53a2e4a1 -r ac9d7a580ebb sys/net/if_stf.c
--- a/sys/net/if_stf.c Mon Oct 23 09:32:33 2017 +0000
+++ b/sys/net/if_stf.c Mon Oct 23 09:32:55 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: if_stf.c,v 1.101 2016/12/12 03:55:57 ozaki-r Exp $ */
+/* $NetBSD: if_stf.c,v 1.102 2017/10/23 09:32:55 msaitoh Exp $ */
/* $KAME: if_stf.c,v 1.62 2001/06/07 22:32:16 itojun Exp $ */
/*
@@ -75,7 +75,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_stf.c,v 1.101 2016/12/12 03:55:57 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_stf.c,v 1.102 2017/10/23 09:32:55 msaitoh Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
@@ -220,7 +220,7 @@
/* Only one stf interface is allowed. */
encap_lock_exit();
free(sc, M_DEVBUF);
- return (EEXIST);
+ return EEXIST;
}
sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
@@ -229,7 +229,7 @@
if (sc->encap_cookie == NULL) {
printf("%s: unable to attach encap\n", if_name(&sc->sc_if));
free(sc, M_DEVBUF);
- return (EIO); /* XXX */
+ return EIO; /* XXX */
}
sc->sc_if.if_mtu = STF_MTU;
@@ -238,11 +238,20 @@
sc->sc_if.if_output = stf_output;
sc->sc_if.if_type = IFT_STF;
sc->sc_if.if_dlt = DLT_NULL;
- if_attach(&sc->sc_if);
+ error = if_attach(&sc->sc_if);
+ if (error != 0) {
+ aprint_error("%s: if_initialize failed(%d)\n",
+ if_name(&sc->sc_if), error);
+ encap_lock_enter();
+ encap_detach(sc->encap_cookie);
+ encap_lock_exit();
+ free(sc, M_DEVBUF);
+ return error;
+ }
if_alloc_sadl(&sc->sc_if);
bpf_attach(&sc->sc_if, DLT_NULL, sizeof(u_int));
LIST_INSERT_HEAD(&stf_softc_list, sc, sc_list);
- return (0);
+ return 0;
}
static int
@@ -259,7 +268,7 @@
rtcache_free(&sc->sc_ro);
free(sc, M_DEVBUF);
- return (0);
+ return 0;
}
static int
Home |
Main Index |
Thread Index |
Old Index