Module Name: src Committed By: msaitoh Date: Mon Oct 23 09:32:55 UTC 2017
Modified Files: src/sys/net: if_srt.c if_stf.c Log Message: - If if_attach() failed in the attach function, free resources and return. - KNF To generate a diff of this commit: cvs rdiff -u -r1.26 -r1.27 src/sys/net/if_srt.c cvs rdiff -u -r1.101 -r1.102 src/sys/net/if_stf.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/net/if_srt.c diff -u src/sys/net/if_srt.c:1.26 src/sys/net/if_srt.c:1.27 --- src/sys/net/if_srt.c:1.26 Tue Feb 14 03:05:06 2017 +++ src/sys/net/if_srt.c Mon Oct 23 09:32:55 2017 @@ -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 @@ update_mtu(struct srt_softc *sc) 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 @@ static int 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 @@ srt_clone_create(struct if_clone *cl, in 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 @@ srt_clone_destroy(struct ifnet *ifp) } 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 @@ srt_ioctl(dev_t dev, u_long cmd, void *d 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 @@ srt_ioctl(dev_t dev, u_long cmd, void *d 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 @@ srt_ioctl(dev_t dev, u_long cmd, void *d } 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 @@ srt_ioctl(dev_t dev, u_long cmd, void *d 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 @@ srt_ioctl(dev_t dev, u_long cmd, void *d *(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; Index: src/sys/net/if_stf.c diff -u src/sys/net/if_stf.c:1.101 src/sys/net/if_stf.c:1.102 --- src/sys/net/if_stf.c:1.101 Mon Dec 12 03:55:57 2016 +++ src/sys/net/if_stf.c Mon Oct 23 09:32:55 2017 @@ -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 @@ stf_clone_create(struct if_clone *ifc, i /* 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 @@ stf_clone_create(struct if_clone *ifc, i 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 @@ stf_clone_create(struct if_clone *ifc, i 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 @@ stf_clone_destroy(struct ifnet *ifp) rtcache_free(&sc->sc_ro); free(sc, M_DEVBUF); - return (0); + return 0; } static int