xref: /openbsd/sys/netmpls/mpls.h (revision 898184e3)
1 /*	$OpenBSD: mpls.h,v 1.25 2010/09/08 08:00:56 claudio Exp $	*/
2 
3 /*
4  * Copyright (C) 1999, 2000 and 2001 AYAME Project, WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, ORCONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef _NETMPLS_MPLS_H_
34 #define _NETMPLS_MPLS_H_
35 
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #include <sys/proc.h>
39 #include <sys/queue.h>
40 
41 #include <net/if.h>
42 #include <net/if_dl.h>
43 
44 /*
45  * Structure of a SHIM header.
46  */
47 #define MPLS_LABEL_MAX		((1 << 20) - 1)
48 
49 struct shim_hdr {
50 	u_int32_t shim_label;	/* 20 bit label, 4 bit exp & BoS, 8 bit TTL */
51 };
52 
53 #define MPLS_HDRLEN	sizeof(struct shim_hdr)
54 
55 /*
56  * By byte-swapping the constants, we avoid ever having to byte-swap IP
57  * addresses inside the kernel.  Unfortunately, user-level programs rely
58  * on these macros not doing byte-swapping.
59  */
60 
61 #ifdef _KERNEL
62 #define __MADDR(x)     ((u_int32_t)htonl((u_int32_t)(x)))
63 #else
64 #define __MADDR(x)     ((u_int32_t)(x))
65 #endif
66 
67 #define MPLS_LABEL_MASK		__MADDR(0xfffff000U)
68 #define MPLS_LABEL_OFFSET	12
69 #define MPLS_EXP_MASK		__MADDR(0x00000e00U)
70 #define MPLS_EXP_OFFSET		9
71 #define MPLS_BOS_MASK		__MADDR(0x00000100U)
72 #define MPLS_BOS_OFFSET		8
73 #define MPLS_TTL_MASK		__MADDR(0x000000ffU)
74 
75 #define MPLS_BOS_ISSET(l)	(((l) & MPLS_BOS_MASK) == MPLS_BOS_MASK)
76 
77 /* Reserved lavel values (RFC3032) */
78 #define MPLS_LABEL_IPV4NULL	0               /* IPv4 Explicit NULL Label */
79 #define MPLS_LABEL_RTALERT	1               /* Router Alert Label       */
80 #define MPLS_LABEL_IPV6NULL	2               /* IPv6 Explicit NULL Label */
81 #define MPLS_LABEL_IMPLNULL	3               /* Implicit NULL Label      */
82 /*      MPLS_LABEL_RESERVED	4-15 */		/* Values 4-15 are reserved */
83 #define MPLS_LABEL_RESERVED_MAX 15
84 
85 /*
86  * Socket address
87  */
88 
89 struct sockaddr_mpls {
90 	u_int8_t	smpls_len;		/* length */
91 	u_int8_t	smpls_family;		/* AF_MPLS */
92 	u_int16_t	smpls_pad0;
93 	u_int32_t	smpls_label;		/* MPLS label */
94 	u_int32_t	smpls_pad1[2];
95 };
96 
97 struct rt_mpls {
98 	u_int32_t	mpls_label;
99 	u_int8_t	mpls_operation;
100 	u_int8_t	mpls_exp;
101 };
102 
103 #define MPLS_OP_LOCAL		0x0
104 #define MPLS_OP_POP		0x1
105 #define MPLS_OP_PUSH		0x2
106 #define MPLS_OP_SWAP		0x4
107 
108 #define MPLS_INKERNEL_LOOP_MAX	16
109 
110 #define satosmpls(sa)		((struct sockaddr_mpls *)(sa))
111 #define smplstosa(smpls)	((struct sockaddr *)(smpls))
112 
113 /*
114  * Names for MPLS sysctl objects
115  */
116 #define MPLSCTL_ENABLE			1
117 #define	MPLSCTL_DEFTTL			2
118 #define MPLSCTL_IFQUEUE			3
119 #define	MPLSCTL_MAXINKLOOP		4
120 #define MPLSCTL_MAPTTL_IP		5
121 #define MPLSCTL_MAPTTL_IP6		6
122 #define MPLSCTL_MAXID			7
123 
124 #define MPLSCTL_NAMES { \
125 	{ NULL, 0 }, \
126 	{ NULL, 0 }, \
127 	{ "ttl", CTLTYPE_INT }, \
128 	{ "ifq", CTLTYPE_NODE },\
129 	{ "maxloop_inkernel", CTLTYPE_INT }, \
130 	{ "mapttl_ip", CTLTYPE_INT }, \
131 	{ "mapttl_ip6", CTLTYPE_INT } \
132 }
133 
134 #define MPLSCTL_VARS { \
135 	NULL, \
136 	NULL, \
137 	&mpls_defttl, \
138 	NULL, \
139 	&mpls_inkloop, \
140 	&mpls_mapttl_ip, \
141 	&mpls_mapttl_ip6 \
142 }
143 
144 #endif
145 
146 #ifdef _KERNEL
147 
148 struct mpe_softc {
149 	struct ifnet		sc_if;		/* the interface */
150 	int			sc_unit;
151 	struct shim_hdr		sc_shim;
152 	LIST_ENTRY(mpe_softc)	sc_list;
153 };
154 
155 #define MPE_HDRLEN	sizeof(struct shim_hdr)
156 #define MPE_MTU		1500
157 #define MPE_MTU_MIN	256
158 #define MPE_MTU_MAX	8192
159 
160 void	mpe_input(struct mbuf *, struct ifnet *, struct sockaddr_mpls *,
161 	    u_int8_t);
162 void	mpe_input6(struct mbuf *, struct ifnet *, struct sockaddr_mpls *,
163 	    u_int8_t);
164 
165 extern int mpls_raw_usrreq(struct socket *, int, struct mbuf *,
166 			struct mbuf *, struct mbuf *, struct proc *);
167 
168 extern struct ifqueue	mplsintrq;	/* MPLS input queue */
169 extern int		mplsqmaxlen;	/* MPLS input queue length */
170 extern int		mpls_defttl;
171 extern int		mpls_mapttl_ip;
172 extern int		mpls_mapttl_ip6;
173 
174 
175 void	mpls_init(void);
176 void	mplsintr(void);
177 
178 struct mbuf	*mpls_shim_pop(struct mbuf *);
179 struct mbuf	*mpls_shim_swap(struct mbuf *, struct rt_mpls *);
180 struct mbuf	*mpls_shim_push(struct mbuf *, struct rt_mpls *);
181 
182 int		 mpls_sysctl(int *, u_int, void *, size_t *, void *, size_t);
183 void		 mpls_input(struct mbuf *);
184 int		 mpls_output(struct ifnet *, struct mbuf *, struct sockaddr *,
185 		    struct rtentry *);
186 
187 #endif /* _KERNEL */
188