Home | History | Annotate | Line # | Download | only in netmpls
mpls_proto.c revision 1.7
      1 /*	$NetBSD: mpls_proto.c,v 1.7 2014/05/18 14:46:16 rmind Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Mihai Chelaru <kefren (at) NetBSD.org>
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: mpls_proto.c,v 1.7 2014/05/18 14:46:16 rmind Exp $");
     34 
     35 #include "opt_inet.h"
     36 #include "opt_mbuftrace.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/socket.h>
     40 #include <sys/protosw.h>
     41 #include <sys/domain.h>
     42 #include <sys/socketvar.h>
     43 #include <sys/sysctl.h>
     44 
     45 #include <net/route.h>
     46 
     47 #include <netmpls/mpls.h>
     48 #include <netmpls/mpls_var.h>
     49 
     50 struct ifqueue mplsintrq;
     51 
     52 static int mpls_usrreq(struct socket *, int, struct mbuf *, struct mbuf *,
     53 	struct mbuf *, struct lwp *);
     54 static void sysctl_net_mpls_setup(struct sysctllog **);
     55 
     56 #ifdef MBUFTRACE
     57 struct mowner mpls_owner = MOWNER_INIT("MPLS", "");
     58 #endif
     59 
     60 int mpls_defttl = 255;
     61 int mpls_mapttl_inet = 1;
     62 int mpls_mapttl_inet6 = 1;
     63 int mpls_icmp_respond = 0;
     64 int mpls_forwarding = 0;
     65 int mpls_accept = 0;
     66 int mpls_mapprec_inet = 1;
     67 int mpls_mapclass_inet6 = 1;
     68 int mpls_rfc4182 = 1;
     69 
     70 void mpls_init(void)
     71 {
     72 #ifdef MBUFTRACE
     73 	MOWNER_ATTACH(&mpls_owner);
     74 #endif
     75 	memset(&mplsintrq, 0, sizeof(mplsintrq));
     76 	mplsintrq.ifq_maxlen = 256;
     77 
     78 	sysctl_net_mpls_setup(NULL);
     79 }
     80 
     81 static int
     82 mpls_usrreq(struct socket *so, int req, struct mbuf *m,
     83         struct mbuf *nam, struct mbuf *control, struct lwp *l)
     84 {
     85 	int error = EOPNOTSUPP;
     86 
     87 	if ((req == PRU_ATTACH) &&
     88 	    (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0)) {
     89 		int s = splsoftnet();
     90 		error = soreserve(so, 8192, 8192);
     91 		splx(s);
     92 	}
     93 
     94 	return error;
     95 }
     96 
     97 /*
     98  * Sysctl for MPLS variables.
     99  */
    100 static void
    101 sysctl_net_mpls_setup(struct sysctllog **clog)
    102 {
    103 
    104         sysctl_createv(clog, 0, NULL, NULL,
    105                        CTLFLAG_PERMANENT,
    106                        CTLTYPE_NODE, "mpls", NULL,
    107                        NULL, 0, NULL, 0,
    108                        CTL_NET, PF_MPLS, CTL_EOL);
    109 
    110         sysctl_createv(clog, 0, NULL, NULL,
    111                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    112                        CTLTYPE_INT, "ttl",
    113                        SYSCTL_DESCR("Default TTL"),
    114                        NULL, 0, &mpls_defttl, 0,
    115                        CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    116 	sysctl_createv(clog, 0, NULL, NULL,
    117 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    118 		       CTLTYPE_INT, "forwarding",
    119 		       SYSCTL_DESCR("MPLS forwarding"),
    120 		       NULL, 0, &mpls_forwarding, 0,
    121 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    122 	sysctl_createv(clog, 0, NULL, NULL,
    123 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    124 		       CTLTYPE_INT, "accept",
    125 		       SYSCTL_DESCR("Accept MPLS Frames"),
    126 		       NULL, 0, &mpls_accept, 0,
    127 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    128 	sysctl_createv(clog, 0, NULL, NULL,
    129 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    130 		       CTLTYPE_INT, "ifq_len",
    131 		       SYSCTL_DESCR("MPLS queue length"),
    132 		       NULL, 0, &mplsintrq.ifq_maxlen, 0,
    133 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    134 	sysctl_createv(clog, 0, NULL, NULL,
    135 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    136 		       CTLTYPE_INT, "rfc4182",
    137 		       SYSCTL_DESCR("RFC 4182 conformance"),
    138 		       NULL, 0, &mpls_rfc4182, 0,
    139 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    140 #ifdef INET
    141 	sysctl_createv(clog, 0, NULL, NULL,
    142 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    143 		       CTLTYPE_INT, "inet_mapttl",
    144 		       SYSCTL_DESCR("Map IP TTL"),
    145 		       NULL, 0, &mpls_mapttl_inet, 0,
    146 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    147 	sysctl_createv(clog, 0, NULL, NULL,
    148 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    149 		       CTLTYPE_INT, "inet_map_prec",
    150 		       SYSCTL_DESCR("Map IP Prec"),
    151 		       NULL, 0, &mpls_mapprec_inet, 0,
    152 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    153 	sysctl_createv(clog, 0, NULL, NULL,
    154 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    155 		       CTLTYPE_INT, "icmp_respond",
    156 		       SYSCTL_DESCR("Emit ICMP packets on errors"),
    157 		       NULL, 0, &mpls_icmp_respond, 0,
    158 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    159 #endif
    160 #ifdef INET6
    161 	sysctl_createv(clog, 0, NULL, NULL,
    162 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    163 		       CTLTYPE_INT, "inet6_mapttl",
    164 		       SYSCTL_DESCR("Map IP6 TTL"),
    165 		       NULL, 0, &mpls_mapttl_inet6, 0,
    166 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    167 	sysctl_createv(clog, 0, NULL, NULL,
    168 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    169 		       CTLTYPE_INT, "inet6_map_prec",
    170 		       SYSCTL_DESCR("Map IP6 class"),
    171 		       NULL, 0, &mpls_mapclass_inet6, 0,
    172 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    173 #endif
    174 }
    175 
    176 DOMAIN_DEFINE(mplsdomain);
    177 
    178 PR_WRAP_USRREQ(mpls_usrreq)
    179 
    180 #define	mpls_usrreq	mpls_usrreq_wrapper
    181 
    182 static const struct pr_usrreqs mpls_usrreqs = {
    183 	.pr_generic	= mpls_usrreq,
    184 };
    185 
    186 const struct protosw mplssw[] = {
    187 	{	.pr_domain = &mplsdomain,
    188 		.pr_init = mpls_init,
    189 	},
    190 	{
    191 		.pr_type = SOCK_DGRAM,
    192 		.pr_domain = &mplsdomain,
    193 		.pr_flags = PR_ATOMIC | PR_ADDR,
    194 		.pr_usrreqs = &mpls_usrreqs,
    195 	},
    196 	{
    197 		.pr_type = SOCK_RAW,
    198 		.pr_domain = &mplsdomain,
    199 		.pr_flags = PR_ATOMIC | PR_ADDR,
    200 		.pr_usrreqs = &mpls_usrreqs,
    201 	},
    202 };
    203 
    204 struct domain mplsdomain = {
    205 	.dom_family = PF_MPLS,
    206 	.dom_name = "MPLS",
    207 	.dom_init = NULL,
    208 	.dom_externalize = NULL,
    209 	.dom_dispose = NULL,
    210 	.dom_protosw = mplssw,
    211 	.dom_protoswNPROTOSW = &mplssw[__arraycount(mplssw)],
    212 	.dom_rtattach = rt_inithead,
    213 	.dom_rtoffset = offsetof(struct sockaddr_mpls, smpls_addr) << 3,
    214 	.dom_maxrtkey = sizeof(union mpls_shim),
    215 	.dom_ifattach = NULL,
    216 	.dom_ifdetach = NULL,
    217 	.dom_ifqueues = { &mplsintrq, NULL },
    218 	.dom_link = { NULL },
    219 	.dom_mowner = MOWNER_INIT("MPLS", ""),
    220 	.dom_sa_cmpofs = offsetof(struct sockaddr_mpls, smpls_addr),
    221 	.dom_sa_cmplen = sizeof(union mpls_shim),
    222 	.dom_rtcache = LIST_HEAD_INITIALIZER(mplsdomain.dom_rtcache)
    223 };
    224