4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.h>
28 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
38 * For each iteration through the kernel table, ask for at most NUM_ENTRIES
39 * entries to be returned.
41 #define NUM_ENTRIES 500
44 print_nat_info(ilb_nat_info_t
*info
)
48 char addr
[INET6_ADDRSTRLEN
];
50 if (info
->nat_proto
== IPPROTO_TCP
)
52 else if (info
->nat_proto
== IPPROTO_UDP
)
56 (void) printf("%4s: ", tmp
);
58 if (IN6_IS_ADDR_V4MAPPED(&info
->nat_out_global
)) {
59 IN6_V4MAPPED_TO_IPADDR(&info
->nat_out_global
, addr_v4
);
60 (void) printf("%s.%d > ", inet_ntop(AF_INET
, &addr_v4
, addr
,
61 INET6_ADDRSTRLEN
), ntohs(info
->nat_out_global_port
));
62 IN6_V4MAPPED_TO_IPADDR(&info
->nat_in_global
, addr_v4
);
63 (void) printf("%s.%d >>> ", inet_ntop(AF_INET
, &addr_v4
, addr
,
64 INET6_ADDRSTRLEN
), ntohs(info
->nat_in_global_port
));
66 IN6_V4MAPPED_TO_IPADDR(&info
->nat_out_local
, addr_v4
);
67 (void) printf("%s.%d > ", inet_ntop(AF_INET
, &addr_v4
, addr
,
68 INET6_ADDRSTRLEN
), ntohs(info
->nat_out_local_port
));
69 IN6_V4MAPPED_TO_IPADDR(&info
->nat_in_local
, addr_v4
);
70 (void) printf("%s.%d\n", inet_ntop(AF_INET
, &addr_v4
, addr
,
71 INET6_ADDRSTRLEN
), ntohs(info
->nat_in_local_port
));
73 (void) printf("%s.%d > ", inet_ntop(AF_INET6
,
74 &info
->nat_out_global
, addr
, INET6_ADDRSTRLEN
),
75 ntohs(info
->nat_out_global_port
));
76 (void) printf("%s.%d >>> ", inet_ntop(AF_INET6
,
77 &info
->nat_in_global
, addr
, INET6_ADDRSTRLEN
),
78 ntohs(info
->nat_in_global_port
));
80 (void) printf("%s.%d > ", inet_ntop(AF_INET6
,
81 &info
->nat_out_local
, addr
, INET6_ADDRSTRLEN
),
82 ntohs(info
->nat_out_local_port
));
83 (void) printf("%s.%d\n", inet_ntop(AF_INET6
,
84 &info
->nat_in_local
, addr
, INET6_ADDRSTRLEN
),
85 ntohs(info
->nat_in_local_port
));
90 print_persist_info(ilb_persist_info_t
*info
)
92 char addr
[INET6_ADDRSTRLEN
];
94 (void) printf("%s: ", info
->persist_rule_name
);
95 if (IN6_IS_ADDR_V4MAPPED(&info
->persist_req_addr
)) {
98 IN6_V4MAPPED_TO_IPADDR(&info
->persist_req_addr
, addr_v4
);
99 (void) printf("%s --> ", inet_ntop(AF_INET
, &addr_v4
, addr
,
101 IN6_V4MAPPED_TO_IPADDR(&info
->persist_srv_addr
, addr_v4
);
102 (void) printf("%s\n", inet_ntop(AF_INET
, &addr_v4
, addr
,
105 (void) printf("%s --> ", inet_ntop(AF_INET6
,
106 &info
->persist_req_addr
, addr
, INET6_ADDRSTRLEN
));
107 (void) printf("%s\n", inet_ntop(AF_INET6
,
108 &info
->persist_srv_addr
, addr
, INET6_ADDRSTRLEN
));
112 /* Tell ilbadm_show_info() which table to show. */
119 ilb_nat_info_t
*nbuf
;
120 ilb_persist_info_t
*pbuf
;
124 static ilbadm_status_t
125 ilbadm_show_info(int argc
, char *argv
[], enum which_tbl tbl
)
127 ilb_handle_t h
= ILB_INVALID_HANDLE
;
129 ilb_status_t rclib
= ILB_STATUS_OK
;
130 ilbadm_status_t rc
= ILBADM_OK
;
131 int32_t i
, num_entries
;
137 * If the user does not specify a count, return the whole table.
138 * This requires setting the fourth param to ilb_show_nat/persist()
139 * end to B_FALSE. Otherwise, set end to B_TRUE;
148 num_entries
= atoi(argv
[1]);
149 if (num_entries
< 1) {
161 entry_sz
= sizeof (ilb_nat_info_t
);
163 entry_sz
= sizeof (ilb_persist_info_t
);
164 if ((buf
.buf
= malloc((num_entries
> 0 ? num_entries
: NUM_ENTRIES
) *
165 entry_sz
)) == NULL
) {
170 rclib
= ilb_open(&h
);
171 if (rclib
!= ILB_STATUS_OK
)
175 num
= num_entries
> 0 ? num_entries
: NUM_ENTRIES
;
176 bzero(buf
.buf
, num
* entry_sz
);
179 rclib
= ilb_show_nat(h
, buf
.nbuf
, &num
, &end
);
181 rclib
= ilb_show_persist(h
, buf
.pbuf
, &num
, &end
);
183 if (rclib
!= ILB_STATUS_OK
)
186 for (i
= 0; i
< num
; i
++) {
188 print_nat_info(&buf
.nbuf
[i
]);
190 print_persist_info(&buf
.pbuf
[i
]);
192 if (num_entries
> 0) {
194 if (num_entries
<= 0)
200 if (h
!= ILB_INVALID_HANDLE
)
202 if (rclib
!= ILB_STATUS_OK
) {
203 ilbadm_err(ilb_errstr(rclib
));
206 if ((rc
!= ILBADM_OK
) && (rc
!= ILBADM_LIBERR
))
207 ilbadm_err(ilbadm_errstr(rc
));
213 ilbadm_show_nat(int argc
, char *argv
[])
215 return (ilbadm_show_info(argc
, argv
, show_nat
));
219 ilbadm_show_persist(int argc
, char *argv
[])
221 return (ilbadm_show_info(argc
, argv
, show_persist
));