1 
   2 /*
   3  * CDDL HEADER START
   4  *
   5  * The contents of this file are subject to the terms of the
   6  * Common Development and Distribution License (the "License").
   7  * You may not use this file except in compliance with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 
  23 /*
  24  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  25  * Use is subject to license terms.
  26  *
  27  * Copyright 2017 Jason King.
  28  * Copyright (c) 2017, Joyent, Inc.
  29  */
  30 
  31 #ifndef _DEFS_H
  32 #define _DEFS_H
  33 
  34 #include <sys/types.h>
  35 #include <sys/socket.h>
  36 #include <sys/debug.h>
  37 #include <ikedoor.h>
  38 #include <cryptoutil.h>
  39 #include <security/cryptoki.h>
  40 #include <stdio.h>
  41 #include <assert.h>
  42 #include <umem.h>
  43 #include <bunyan.h>
  44 #include <libintl.h>
  45 
  46 #ifdef  __cplusplus
  47 extern "C" {
  48 #endif
  49 
  50 #ifndef SOCKADDR_U_T
  51 #define SOCKADDR_U_T
  52 typedef union sockaddr_u_s {
  53         struct sockaddr_storage *sau_ss;
  54         struct sockaddr_in      *sau_sin;
  55         struct sockaddr_in6     *sau_sin6;
  56 } sockaddr_u_t;
  57 #endif /* SOCKADDR_U_T */
  58 
  59 /* Parsed-out PF_KEY message. */
  60 typedef struct parsedmsg_s {
  61         struct parsedmsg_s *pmsg_next;
  62         sadb_msg_t *pmsg_samsg;
  63         sadb_ext_t *pmsg_exts[SADB_EXT_MAX + 2]; /* 2 for alignment */
  64         sockaddr_u_t pmsg_sau;
  65         sockaddr_u_t pmsg_dau;
  66         sockaddr_u_t pmsg_isau;
  67         sockaddr_u_t pmsg_idau;
  68         sockaddr_u_t pmsg_nlau;
  69         sockaddr_u_t pmsg_nrau;
  70 } parsedmsg_t;
  71 
  72 #define pmsg_sss pmsg_sau.sau_ss
  73 #define pmsg_ssin pmsg_sau.sau_sin
  74 #define pmsg_ssin6 pmsg_sau.sau_sin6
  75 #define pmsg_dss pmsg_dau.sau_ss
  76 #define pmsg_dsin pmsg_dau.sau_sin
  77 #define pmsg_dsin6 pmsg_dau.sau_sin6
  78 #define pmsg_isss pmsg_isau.sau_ss
  79 #define pmsg_issin pmsg_isau.sau_sin
  80 #define pmsg_issin6 pmsg_isau.sau_sin6
  81 #define pmsg_idss pmsg_idau.sau_ss
  82 #define pmsg_idsin pmsg_idau.sau_sin
  83 #define pmsg_idsin6 pmsg_idau.sau_sin6
  84 #define pmsg_nlss pmsg_nlau.sau_ss
  85 #define pmsg_nlsin pmsg_nlau.sau_sin
  86 #define pmsg_nlsin6 pmsg_nlau.sau_sin6
  87 #define pmsg_nrss pmsg_nrau.sau_ss
  88 #define pmsg_nrsin pmsg_rnau.sau_sin
  89 #define pmsg_nrsin6 pmsg_nrau.sau_sin6
  90 
  91 typedef void (*pfreq_cb_t)(sadb_msg_t *, void *);
  92 typedef struct algindex {
  93         const char *desc;
  94         int doi_num;
  95 } algindex_t;
  96 
  97 /*
  98  * Compare two AF_INET{,6} sockaddrs (no port).  Assume sockaddr_storage
  99  * pointers are passed, and also verifies the address families match and
 100  * are either AF_INET or AF_INET6.
 101  */
 102 #define SA_ADDR_EQ(sa1, sa2)                                            \
 103         (((sa1)->ss_family == (sa2)->ss_family) &&                        \
 104             ((((sa1)->ss_family == AF_INET) &&                               \
 105                 ((struct sockaddr_in *)(sa1))->sin_addr.s_addr ==    \
 106                 ((struct sockaddr_in *)(sa2))->sin_addr.s_addr) ||   \
 107                 (((sa1)->ss_family == AF_INET6) &&                   \
 108                 IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)(sa1))->sin6_addr,\
 109                     &((struct sockaddr_in6 *)(sa2))->sin6_addr))))
 110 
 111 /*
 112  * Compare two AF_INET{,6} sockaddr ports.  Exploit the identical offsets for
 113  * sin_port/sin6_port.  (Does not check sockaddr families a priori.)
 114  */
 115 #define SA_PORT_EQ(sa1, sa2) (((struct sockaddr_in *)(sa1))->sin_port == \
 116             ((struct sockaddr_in *)(sa2))->sin_port)
 117 
 118 /*
 119  * Compare two AF_INET{,6} sockaddrs (including ports).  Exploit the
 120  * identical offsets for sin_port/sin6_port.
 121  */
 122 #define SA_FULL_EQ(sa1, sa2) (SA_ADDR_EQ(sa1, sa2) && SA_PORT_EQ(sa1, sa2))
 123 
 124 #define INVALID(var) assfail("Invalid value of " # var, __FILE__, __LINE__)
 125 #define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
 126 
 127 /*
 128  * Simple wrapper for pthread calls that should never fail under
 129  * normal conditions.
 130  */
 131 #define PTH(fn) do {                                                    \
 132         int __pthread_rc = fn;                                          \
 133         if (__pthread_rc < 0)                                                \
 134                 assfail(#fn " call failed", __FILE__, __LINE__);        \
 135 _NOTE(CONSTCOND) } while (0)
 136 
 137 /* BEGIN CSTYLED */
 138 #define STDERR(_lvl, _log, _msg, ...)                   \
 139         (void) bunyan_##_lvl((_log), (_msg),            \
 140         BUNYAN_T_STRING, "err", strerror(errno),        \
 141         BUNYAN_T_INT32, "errno", (int32_t)(errno),      \
 142         BUNYAN_T_STRING, "func", __func__,              \
 143         BUNYAN_T_STRING, "file", __FILE__,              \
 144         BUNYAN_T_INT32, "line", __LINE__,               \
 145         ## __VA_ARGS__,                                 \
 146         BUNYAN_T_END)
 147 /* END CSTYLED */
 148 
 149 static inline uint32_t
 150 ss_port(const struct sockaddr_storage *ss)
 151 {
 152         sockaddr_u_t sau;
 153         sau.sau_ss = (struct sockaddr_storage *)ss;
 154         switch (ss->ss_family) {
 155         case AF_INET:
 156                 return ((uint32_t)sau.sau_sin->sin_port);
 157         case AF_INET6:
 158                 return ((uint32_t)sau.sau_sin6->sin6_port);
 159         default:
 160                 INVALID("ss->ss_family");
 161                 /*NOTREACHED*/
 162                 return (NULL);
 163         }
 164 }
 165 
 166 static inline const void *
 167 ss_addr(const struct sockaddr_storage *ss)
 168 {
 169         sockaddr_u_t sau;
 170         sau.sau_ss = (struct sockaddr_storage *)ss;
 171         switch (ss->ss_family) {
 172         case AF_INET:
 173                 return (&sau.sau_sin->sin_addr);
 174         case AF_INET6:
 175                 return (&sau.sau_sin6->sin6_addr);
 176         default:
 177                 INVALID("ss->ss_family");
 178                 /*NOTREACHED*/
 179                 return (NULL);
 180         }
 181 }
 182 
 183 static inline int
 184 ss_bunyan(const struct sockaddr_storage *ss)
 185 {
 186         switch (ss->ss_family) {
 187         case AF_INET:
 188                 return (BUNYAN_T_IP);
 189         case AF_INET6:
 190                 return (BUNYAN_T_IP6);
 191         default:
 192                 INVALID("ss->ss_family");
 193                 /*NOTREACHED*/
 194                 return (BUNYAN_T_END);
 195         }
 196 }
 197 
 198 /* BEGIN CSTYLED */
 199 #define NETLOG(_level, _log, _msg, _src, _dest, ...)    \
 200         (void) bunyan_##_level((_log), (_msg),          \
 201         BUNYAN_T_STRING, "func", __func__,              \
 202         BUNYAN_T_STRING, "file", __FILE__,              \
 203         BUNYAN_T_INT32, "line", __LINE__,               \
 204         ss_bunyan(_src), "src", ss_addr(_src),          \
 205         BUNYAN_T_UINT32, "srcport", ss_port(_src),      \
 206         ss_bunyan(_dest), "dest", ss_addr(_dest),       \
 207         BUNYAN_T_UINT32, "destport", ss_port(_dest),    \
 208         ## __VA_ARGS__,                                 \
 209         BUNYAN_T_END)
 210 /* END CSTYLED */
 211 
 212 typedef enum event {
 213         EVENT_NONE,
 214         EVENT_SIGNAL
 215 } event_t;
 216 
 217 extern char *my_fmri;
 218 extern bunyan_logger_t *log;
 219 extern int port;
 220 
 221 void schedule_socket(int, void(*)(int, void *));
 222 
 223 typedef int (*bunyan_logfn_t)(bunyan_logger_t *, const char *, ...);
 224 bunyan_logfn_t getlog(bunyan_level_t);
 225 
 226 #ifdef  __cplusplus
 227 }
 228 #endif
 229 
 230 #endif  /* _DEFS_H */