Home | History | Annotate | Line # | Download | only in netmpls
mpls_proto.c revision 1.5
      1 /*	$NetBSD: mpls_proto.c,v 1.5 2013/07/23 11:11:55 kefren 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.5 2013/07/23 11:11:55 kefren 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 DOMAIN_DEFINE(mplsdomain);
     82 
     83 const struct protosw mplssw[] = {
     84 	{	.pr_domain = &mplsdomain,
     85 		.pr_init = mpls_init,
     86 	},
     87 	{
     88 		.pr_type = SOCK_DGRAM,
     89 		.pr_domain = &mplsdomain,
     90 		.pr_flags = PR_ATOMIC | PR_ADDR,
     91 		.pr_usrreq = mpls_usrreq,
     92 	},
     93 	{
     94 		.pr_type = SOCK_RAW,
     95 		.pr_domain = &mplsdomain,
     96 		.pr_flags = PR_ATOMIC | PR_ADDR,
     97 		.pr_usrreq = mpls_usrreq,
     98 	},
     99 };
    100 
    101 struct domain mplsdomain = {
    102 	.dom_family = PF_MPLS,
    103 	.dom_name = "MPLS",
    104 	.dom_init = NULL,
    105 	.dom_externalize = NULL,
    106 	.dom_dispose = NULL,
    107 	.dom_protosw = mplssw,
    108 	.dom_protoswNPROTOSW = &mplssw[__arraycount(mplssw)],
    109 	.dom_rtattach = rt_inithead,
    110 	.dom_rtoffset = offsetof(struct sockaddr_mpls, smpls_addr) << 3,
    111 	.dom_maxrtkey = sizeof(union mpls_shim),
    112 	.dom_ifattach = NULL,
    113 	.dom_ifdetach = NULL,
    114 	.dom_ifqueues = { &mplsintrq, NULL },
    115 	.dom_link = { NULL },
    116 	.dom_mowner = MOWNER_INIT("MPLS", ""),
    117 	.dom_sa_cmpofs = offsetof(struct sockaddr_mpls, smpls_addr),
    118 	.dom_sa_cmplen = sizeof(union mpls_shim),
    119 	.dom_rtcache = LIST_HEAD_INITIALIZER(mplsdomain.dom_rtcache)
    120 };
    121 
    122 static int
    123 mpls_usrreq(struct socket *so, int req, struct mbuf *m,
    124         struct mbuf *nam, struct mbuf *control, struct lwp *l)
    125 {
    126 	int error = EOPNOTSUPP;
    127 
    128 	if ((req == PRU_ATTACH) &&
    129 	    (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0)) {
    130 		int s = splsoftnet();
    131 		error = soreserve(so, 8192, 8192);
    132 		splx(s);
    133 	}
    134 
    135 	return error;
    136 }
    137 
    138 /*
    139  * Sysctl for MPLS variables.
    140  */
    141 static void
    142 sysctl_net_mpls_setup(struct sysctllog **clog)
    143 {
    144 
    145         sysctl_createv(clog, 0, NULL, NULL,
    146                        CTLFLAG_PERMANENT,
    147                        CTLTYPE_NODE, "net", NULL,
    148                        NULL, 0, NULL, 0,
    149                        CTL_NET, CTL_EOL);
    150         sysctl_createv(clog, 0, NULL, NULL,
    151                        CTLFLAG_PERMANENT,
    152                        CTLTYPE_NODE, "mpls", NULL,
    153                        NULL, 0, NULL, 0,
    154                        CTL_NET, PF_MPLS, CTL_EOL);
    155 
    156         sysctl_createv(clog, 0, NULL, NULL,
    157                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    158                        CTLTYPE_INT, "ttl",
    159                        SYSCTL_DESCR("Default TTL"),
    160                        NULL, 0, &mpls_defttl, 0,
    161                        CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    162 	sysctl_createv(clog, 0, NULL, NULL,
    163 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    164 		       CTLTYPE_INT, "forwarding",
    165 		       SYSCTL_DESCR("MPLS forwarding"),
    166 		       NULL, 0, &mpls_forwarding, 0,
    167 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    168 	sysctl_createv(clog, 0, NULL, NULL,
    169 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    170 		       CTLTYPE_INT, "accept",
    171 		       SYSCTL_DESCR("Accept MPLS Frames"),
    172 		       NULL, 0, &mpls_accept, 0,
    173 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    174 	sysctl_createv(clog, 0, NULL, NULL,
    175 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    176 		       CTLTYPE_INT, "ifq_len",
    177 		       SYSCTL_DESCR("MPLS queue length"),
    178 		       NULL, 0, &mplsintrq.ifq_maxlen, 0,
    179 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    180 	sysctl_createv(clog, 0, NULL, NULL,
    181 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    182 		       CTLTYPE_INT, "rfc4182",
    183 		       SYSCTL_DESCR("RFC 4182 conformance"),
    184 		       NULL, 0, &mpls_rfc4182, 0,
    185 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    186 #ifdef INET
    187 	sysctl_createv(clog, 0, NULL, NULL,
    188 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    189 		       CTLTYPE_INT, "inet_mapttl",
    190 		       SYSCTL_DESCR("Map IP TTL"),
    191 		       NULL, 0, &mpls_mapttl_inet, 0,
    192 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    193 	sysctl_createv(clog, 0, NULL, NULL,
    194 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    195 		       CTLTYPE_INT, "inet_map_prec",
    196 		       SYSCTL_DESCR("Map IP Prec"),
    197 		       NULL, 0, &mpls_mapprec_inet, 0,
    198 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    199 	sysctl_createv(clog, 0, NULL, NULL,
    200 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    201 		       CTLTYPE_INT, "icmp_respond",
    202 		       SYSCTL_DESCR("Emit ICMP packets on errors"),
    203 		       NULL, 0, &mpls_icmp_respond, 0,
    204 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    205 #endif
    206 #ifdef INET6
    207 	sysctl_createv(clog, 0, NULL, NULL,
    208 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    209 		       CTLTYPE_INT, "inet6_mapttl",
    210 		       SYSCTL_DESCR("Map IP6 TTL"),
    211 		       NULL, 0, &mpls_mapttl_inet6, 0,
    212 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    213 	sysctl_createv(clog, 0, NULL, NULL,
    214 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    215 		       CTLTYPE_INT, "inet6_map_prec",
    216 		       SYSCTL_DESCR("Map IP6 class"),
    217 		       NULL, 0, &mpls_mapclass_inet6, 0,
    218 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
    219 #endif
    220 }
    221