xref: /openbsd/sys/netmpls/mpls.h (revision 91f110e0)
1 /*	$OpenBSD: mpls.h,v 1.28 2013/04/24 10:20:15 mpi 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/queue.h>
39 
40 #include <net/if.h>
41 #include <net/if_dl.h>
42 
43 /*
44  * Structure of a SHIM header.
45  */
46 #define MPLS_LABEL_MAX		((1 << 20) - 1)
47 
48 struct shim_hdr {
49 	u_int32_t shim_label;	/* 20 bit label, 4 bit exp & BoS, 8 bit TTL */
50 };
51 
52 #define MPLS_HDRLEN	sizeof(struct shim_hdr)
53 
54 /*
55  * By byte-swapping the constants, we avoid ever having to byte-swap IP
56  * addresses inside the kernel.  Unfortunately, user-level programs rely
57  * on these macros not doing byte-swapping.
58  */
59 
60 #ifdef _KERNEL
61 #define __MADDR(x)     ((u_int32_t)htonl((u_int32_t)(x)))
62 #else
63 #define __MADDR(x)     ((u_int32_t)(x))
64 #endif
65 
66 #define MPLS_LABEL_MASK		__MADDR(0xfffff000U)
67 #define MPLS_LABEL_OFFSET	12
68 #define MPLS_EXP_MASK		__MADDR(0x00000e00U)
69 #define MPLS_EXP_OFFSET		9
70 #define MPLS_BOS_MASK		__MADDR(0x00000100U)
71 #define MPLS_BOS_OFFSET		8
72 #define MPLS_TTL_MASK		__MADDR(0x000000ffU)
73 
74 #define MPLS_BOS_ISSET(l)	(((l) & MPLS_BOS_MASK) == MPLS_BOS_MASK)
75 
76 /* Reserved lavel values (RFC3032) */
77 #define MPLS_LABEL_IPV4NULL	0               /* IPv4 Explicit NULL Label */
78 #define MPLS_LABEL_RTALERT	1               /* Router Alert Label       */
79 #define MPLS_LABEL_IPV6NULL	2               /* IPv6 Explicit NULL Label */
80 #define MPLS_LABEL_IMPLNULL	3               /* Implicit NULL Label      */
81 /*      MPLS_LABEL_RESERVED	4-15 */		/* Values 4-15 are reserved */
82 #define MPLS_LABEL_RESERVED_MAX 15
83 
84 /*
85  * Socket address
86  */
87 
88 struct sockaddr_mpls {
89 	u_int8_t	smpls_len;		/* length */
90 	u_int8_t	smpls_family;		/* AF_MPLS */
91 	u_int16_t	smpls_pad0;
92 	u_int32_t	smpls_label;		/* MPLS label */
93 	u_int32_t	smpls_pad1[2];
94 };
95 
96 struct rt_mpls {
97 	u_int32_t	mpls_label;
98 	u_int8_t	mpls_operation;
99 	u_int8_t	mpls_exp;
100 };
101 
102 #define MPLS_OP_LOCAL		0x0
103 #define MPLS_OP_POP		0x1
104 #define MPLS_OP_PUSH		0x2
105 #define MPLS_OP_SWAP		0x4
106 
107 #define MPLS_INKERNEL_LOOP_MAX	16
108 
109 #define satosmpls(sa)		((struct sockaddr_mpls *)(sa))
110 #define smplstosa(smpls)	((struct sockaddr *)(smpls))
111 
112 /*
113  * Names for MPLS sysctl objects
114  */
115 #define MPLSCTL_ENABLE			1
116 #define	MPLSCTL_DEFTTL			2
117 #define MPLSCTL_IFQUEUE			3
118 #define	MPLSCTL_MAXINKLOOP		4
119 #define MPLSCTL_MAPTTL_IP		5
120 #define MPLSCTL_MAPTTL_IP6		6
121 #define MPLSCTL_MAXID			7
122 
123 #define MPLSCTL_NAMES { \
124 	{ NULL, 0 }, \
125 	{ NULL, 0 }, \
126 	{ "ttl", CTLTYPE_INT }, \
127 	{ "ifq", CTLTYPE_NODE },\
128 	{ "maxloop_inkernel", CTLTYPE_INT }, \
129 	{ "mapttl_ip", CTLTYPE_INT }, \
130 	{ "mapttl_ip6", CTLTYPE_INT } \
131 }
132 
133 #define MPLSCTL_VARS { \
134 	NULL, \
135 	NULL, \
136 	&mpls_defttl, \
137 	NULL, \
138 	&mpls_inkloop, \
139 	&mpls_mapttl_ip, \
140 	&mpls_mapttl_ip6 \
141 }
142 
143 #endif
144 
145 #ifdef _KERNEL
146 
147 extern	struct domain mplsdomain;
148 
149 struct mpe_softc {
150 	struct ifnet		sc_if;		/* the interface */
151 	int			sc_unit;
152 	struct shim_hdr		sc_shim;
153 	LIST_ENTRY(mpe_softc)	sc_list;
154 };
155 
156 #define MPE_HDRLEN	sizeof(struct shim_hdr)
157 #define MPE_MTU		1500
158 #define MPE_MTU_MIN	256
159 #define MPE_MTU_MAX	8192
160 
161 void	mpe_input(struct mbuf *, struct ifnet *, struct sockaddr_mpls *,
162 	    u_int8_t);
163 void	mpe_input6(struct mbuf *, struct ifnet *, struct sockaddr_mpls *,
164 	    u_int8_t);
165 
166 extern int mpls_raw_usrreq(struct socket *, int, struct mbuf *,
167 			struct mbuf *, struct mbuf *, struct proc *);
168 
169 extern struct ifqueue	mplsintrq;	/* MPLS input queue */
170 extern int		mpls_defttl;
171 extern int		mpls_mapttl_ip;
172 extern int		mpls_mapttl_ip6;
173 extern int		mpls_inkloop;
174 
175 
176 void	mpls_init(void);
177 void	mplsintr(void);
178 
179 struct mbuf	*mpls_shim_pop(struct mbuf *);
180 struct mbuf	*mpls_shim_swap(struct mbuf *, struct rt_mpls *);
181 struct mbuf	*mpls_shim_push(struct mbuf *, struct rt_mpls *);
182 
183 int		 mpls_sysctl(int *, u_int, void *, size_t *, void *, size_t);
184 void		 mpls_input(struct mbuf *);
185 int		 mpls_output(struct ifnet *, struct mbuf *, struct sockaddr *,
186 		    struct rtentry *);
187 
188 #endif /* _KERNEL */
189