| 1 |
/* |
| 2 |
* System-dependent procedures for pppd under Solaris 2. |
| 3 |
* |
| 4 |
* Parts re-written by Adi Masputra <[email protected]>, based on |
| 5 |
* the original sys-svr4.c |
| 6 |
* |
| 7 |
* Copyright (c) 2000 by Sun Microsystems, Inc. |
| 8 |
* All rights reserved. |
| 9 |
* |
| 10 |
* Permission to use, copy, modify, and distribute this software and its |
| 11 |
* documentation is hereby granted, provided that the above copyright |
| 12 |
* notice appears in all copies. |
| 13 |
* |
| 14 |
* SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF |
| 15 |
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 16 |
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 17 |
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR |
| 18 |
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR |
| 19 |
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES |
| 20 |
* |
| 21 |
* Copyright (c) 1995-2024 Paul Mackerras. All rights reserved. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* |
| 27 |
* 1. Redistributions of source code must retain the above copyright |
| 28 |
* notice, this list of conditions and the following disclaimer. |
| 29 |
* |
| 30 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 31 |
* notice, this list of conditions and the following disclaimer in |
| 32 |
* the documentation and/or other materials provided with the |
| 33 |
* distribution. |
| 34 |
* |
| 35 |
* THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO |
| 36 |
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 37 |
* AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 38 |
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 39 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN |
| 40 |
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
| 41 |
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 42 |
* |
| 43 |
* Derived from main.c and pppd.h, which are: |
| 44 |
* |
| 45 |
* Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved. |
| 46 |
* |
| 47 |
* Redistribution and use in source and binary forms, with or without |
| 48 |
* modification, are permitted provided that the following conditions |
| 49 |
* are met: |
| 50 |
* |
| 51 |
* 1. Redistributions of source code must retain the above copyright |
| 52 |
* notice, this list of conditions and the following disclaimer. |
| 53 |
* |
| 54 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 55 |
* notice, this list of conditions and the following disclaimer in |
| 56 |
* the documentation and/or other materials provided with the |
| 57 |
* distribution. |
| 58 |
* |
| 59 |
* 3. The name "Carnegie Mellon University" must not be used to |
| 60 |
* endorse or promote products derived from this software without |
| 61 |
* prior written permission. For permission or any legal |
| 62 |
* details, please contact |
| 63 |
* Office of Technology Transfer |
| 64 |
* Carnegie Mellon University |
| 65 |
* 5000 Forbes Avenue |
| 66 |
* Pittsburgh, PA 15213-3890 |
| 67 |
* (412) 268-4387, fax: (412) 268-7395 |
| 68 |
* [email protected] |
| 69 |
* |
| 70 |
* 4. Redistributions of any form whatsoever must retain the following |
| 71 |
* acknowledgment: |
| 72 |
* "This product includes software developed by Computing Services |
| 73 |
* at Carnegie Mellon University (http://www.cmu.edu/computing/)." |
| 74 |
* |
| 75 |
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 76 |
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 77 |
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE |
| 78 |
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 79 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN |
| 80 |
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
| 81 |
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 82 |
*/ |
| 83 |
|
| 84 |
#ifdef HAVE_CONFIG_H |
| 85 |
#include "config.h" |
| 86 |
#endif |
| 87 |
|
| 88 |
#include <limits.h> |
| 89 |
#include <stdio.h> |
| 90 |
#include <stddef.h> |
| 91 |
#include <stdlib.h> |
| 92 |
#include <string.h> |
| 93 |
#include <ctype.h> |
| 94 |
#include <errno.h> |
| 95 |
#include <fcntl.h> |
| 96 |
#include <unistd.h> |
| 97 |
#include <termios.h> |
| 98 |
#include <signal.h> |
| 99 |
#include <utmpx.h> |
| 100 |
#include <stropts.h> |
| 101 |
#include <sys/types.h> |
| 102 |
#include <sys/ioccom.h> |
| 103 |
#include <sys/stream.h> |
| 104 |
#include <sys/stropts.h> |
| 105 |
#include <sys/socket.h> |
| 106 |
#include <sys/sockio.h> |
| 107 |
#include <sys/sysmacros.h> |
| 108 |
#include <sys/systeminfo.h> |
| 109 |
#include <sys/dlpi.h> |
| 110 |
#include <sys/stat.h> |
| 111 |
#include <sys/mkdev.h> |
| 112 |
#include <sys/time.h> |
| 113 |
#include <net/if.h> |
| 114 |
#include <net/if_arp.h> |
| 115 |
#include <net/route.h> |
| 116 |
#include <net/ppp_defs.h> |
| 117 |
#include <net/pppio.h> |
| 118 |
#include <netinet/in.h> |
| 119 |
#include <sys/tihdr.h> |
| 120 |
#include <sys/tiuser.h> |
| 121 |
#include <inet/common.h> |
| 122 |
#include <inet/mib2.h> |
| 123 |
#include <sys/ethernet.h> |
| 124 |
|
| 125 |
#ifdef PPP_WITH_FILTER |
| 126 |
#include <pcap.h> |
| 127 |
#endif |
| 128 |
|
| 129 |
#include "pppd-private.h" |
| 130 |
#include "fsm.h" |
| 131 |
#include "lcp.h" |
| 132 |
#include "ipcp.h" |
| 133 |
#include "ccp.h" |
| 134 |
#include "eui64.h" |
| 135 |
|
| 136 |
#if !defined(PPP_DEV_NAME) |
| 137 |
#define PPP_DEV_NAME "/dev/" PPP_DRV_NAME |
| 138 |
#endif /* !defined(PPP_DEV_NAME) */ |
| 139 |
|
| 140 |
#if !defined(AHDLC_MOD_NAME) |
| 141 |
#define AHDLC_MOD_NAME "spppasyn" |
| 142 |
#endif /* !defined(AHDLC_MOD_NAME) */ |
| 143 |
|
| 144 |
#if !defined(COMP_MOD_NAME) |
| 145 |
#define COMP_MOD_NAME "spppcomp" |
| 146 |
#endif /* !defined(COMP_MOD_NAME) */ |
| 147 |
|
| 148 |
#if !defined(IP_MOD_NAME) |
| 149 |
#define IP_MOD_NAME "ip" |
| 150 |
#endif /* !defined(IP_MOD_NAME) */ |
| 151 |
|
| 152 |
#define UDP_DEV_NAME "/dev/udp" |
| 153 |
#define UDP6_DEV_NAME "/dev/udp6" |
| 154 |
|
| 155 |
/* |
| 156 |
* "/dev/udp" is used as a multiplexor to PLINK the interface stream |
| 157 |
* under. It is used in place of "/dev/ip" since STREAMS will not let |
| 158 |
* a driver be PLINK'ed under itself, and "/dev/ip" is typically the |
| 159 |
* driver at the bottom of the tunneling interfaces stream. |
| 160 |
*/ |
| 161 |
static char *mux_dev_name = UDP_DEV_NAME; |
| 162 |
static int pppfd; |
| 163 |
static int fdmuxid = -1; |
| 164 |
static int ipfd; |
| 165 |
static int ipmuxid = -1; |
| 166 |
static int ip6fd; /* IP file descriptor */ |
| 167 |
static int ip6muxid = -1; /* Multiplexer file descriptor */ |
| 168 |
static int if6_is_up = 0; /* IPv6 interface has been marked up */ |
| 169 |
|
| 170 |
#define IN6_SOCKADDR_FROM_EUI64(s, eui64) do { \ |
| 171 |
(s)->sin6_family = AF_INET6; \ |
| 172 |
(s)->sin6_addr.s6_addr32[0] = htonl(0xfe800000); \ |
| 173 |
eui64_copy(eui64, (s)->sin6_addr.s6_addr32[2]); \ |
| 174 |
} while(0) |
| 175 |
|
| 176 |
#define _IN6_LLX_FROM_EUI64(l, s, eui64, as) do { \ |
| 177 |
s->sin6_addr.s6_addr32[0] = htonl(as); \ |
| 178 |
eui64_copy(eui64, s->sin6_addr.s6_addr32[2]); \ |
| 179 |
s->sin6_family = AF_INET6; \ |
| 180 |
l.lifr_addr.ss_family = AF_INET6; \ |
| 181 |
l.lifr_addrlen = 64; \ |
| 182 |
l.lifr_addr = laddr; \ |
| 183 |
} while (0) |
| 184 |
|
| 185 |
#define _IN6A_LLX_FROM_EUI64(s, eui64, as) do { \ |
| 186 |
s->s6_addr32[0] = htonl(as); \ |
| 187 |
eui64_copy(eui64, s->s6_addr32[2]); \ |
| 188 |
} while (0) |
| 189 |
|
| 190 |
#define IN6_LLADDR_FROM_EUI64(l, s, eui64) \ |
| 191 |
_IN6_LLX_FROM_EUI64(l, s, eui64, 0xfe800000) |
| 192 |
|
| 193 |
#define IN6_LLTOKEN_FROM_EUI64(l, s, eui64) \ |
| 194 |
_IN6_LLX_FROM_EUI64(l, s, eui64, 0) |
| 195 |
|
| 196 |
#define IN6A_LLADDR_FROM_EUI64(s, eui64) \ |
| 197 |
_IN6A_LLX_FROM_EUI64(s, eui64, 0xfe800000) |
| 198 |
|
| 199 |
static int restore_term; |
| 200 |
static struct termios inittermios; |
| 201 |
static struct winsize wsinfo; /* Initial window size info */ |
| 202 |
static pid_t tty_sid; /* original session ID for terminal */ |
| 203 |
|
| 204 |
extern u_char inpacket_buf[]; /* borrowed from main.c */ |
| 205 |
|
| 206 |
#define MAX_POLLFDS 32 |
| 207 |
static struct pollfd pollfds[MAX_POLLFDS]; |
| 208 |
static int n_pollfds; |
| 209 |
|
| 210 |
static int link_mtu, link_mru; |
| 211 |
|
| 212 |
#define NMODULES 32 |
| 213 |
static int tty_nmodules; |
| 214 |
static char tty_modules[NMODULES][FMNAMESZ+1]; |
| 215 |
static int tty_npushed; |
| 216 |
|
| 217 |
static int if_is_up; /* Interface has been marked up */ |
| 218 |
static u_int32_t remote_addr; /* IP address of peer */ |
| 219 |
static u_int32_t default_route_gateway; /* Gateway for default route added */ |
| 220 |
static eui64_t default_route_gateway6; /* Gateway for default IPv6 route added */ |
| 221 |
static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */ |
| 222 |
|
| 223 |
/* Prototypes for procedures local to this file. */ |
| 224 |
static int translate_speed(int); |
| 225 |
static int baud_rate_of(int); |
| 226 |
static int get_ether_addr(u_int32_t, struct sockaddr *); |
| 227 |
static int get_hw_addr(char *, u_int32_t, struct sockaddr *); |
| 228 |
static int get_hw_addr_dlpi(char *, struct sockaddr *); |
| 229 |
static int dlpi_attach(int, int); |
| 230 |
static int dlpi_info_req(int); |
| 231 |
static int dlpi_get_reply(int, union DL_primitives *, int, size_t); |
| 232 |
static int strioctl(int, int, void *, int, int); |
| 233 |
|
| 234 |
/* |
| 235 |
* sifppa - Sets interface ppa |
| 236 |
* |
| 237 |
* without setting the ppa, ip module will return EINVAL upon setting the |
| 238 |
* interface UP (SIOCSxIFFLAGS). This is because ip module in 2.8 expects |
| 239 |
* two DLPI_INFO_REQ to be sent down to the driver (below ip) before |
| 240 |
* IFF_UP can be set. Plumbing the device causes one DLPI_INFO_REQ to |
| 241 |
* be sent down, and the second DLPI_INFO_REQ is sent upon receiving |
| 242 |
* IF_UNITSEL (old) or SIOCSLIFNAME (new) ioctls. Such setting of the ppa |
| 243 |
* is required because the ppp DLPI provider advertises itself as |
| 244 |
* a DLPI style 2 type, which requires a point of attachment to be |
| 245 |
* specified. The only way the user can specify a point of attachment |
| 246 |
* is via SIOCSLIFNAME or IF_UNITSEL. |
| 247 |
* |
| 248 |
* Such changes in the behavior of ip module was made to meet new or |
| 249 |
* evolving standards requirements. |
| 250 |
* |
| 251 |
*/ |
| 252 |
static int |
| 253 |
sifppa(fd, ppa) |
| 254 |
int fd; |
| 255 |
int ppa; |
| 256 |
{ |
| 257 |
return (int)ioctl(fd, IF_UNITSEL, (char *)&ppa); |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
* get_first_ether_hwaddr - get the hardware address for the first |
| 262 |
* ethernet-style interface on this system. |
| 263 |
*/ |
| 264 |
int |
| 265 |
get_first_ether_hwaddr(u_char *addr) |
| 266 |
{ |
| 267 |
struct lifnum lifn; |
| 268 |
struct lifconf lifc; |
| 269 |
struct lifreq *plifreq; |
| 270 |
struct lifreq lifr; |
| 271 |
int fd, num_ifs, i, found; |
| 272 |
uint_t fl, req_size; |
| 273 |
char *req; |
| 274 |
|
| 275 |
fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 276 |
if (fd < 0) { |
| 277 |
return -1; |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* Find out how many interfaces are running |
| 282 |
*/ |
| 283 |
lifn.lifn_family = AF_UNSPEC; |
| 284 |
lifn.lifn_flags = LIFC_NOXMIT; |
| 285 |
if (ioctl(fd, SIOCGLIFNUM, &lifn) < 0) { |
| 286 |
close(fd); |
| 287 |
error("could not determine number of interfaces: %m"); |
| 288 |
return -1; |
| 289 |
} |
| 290 |
|
| 291 |
num_ifs = lifn.lifn_count; |
| 292 |
req_size = num_ifs * sizeof(struct lifreq); |
| 293 |
req = malloc(req_size); |
| 294 |
if (req == NULL) { |
| 295 |
close(fd); |
| 296 |
error("out of memory"); |
| 297 |
return -1; |
| 298 |
} |
| 299 |
|
| 300 |
/* |
| 301 |
* Get interface configuration info for all interfaces |
| 302 |
*/ |
| 303 |
lifc.lifc_family = AF_UNSPEC; |
| 304 |
lifc.lifc_flags = LIFC_NOXMIT; |
| 305 |
lifc.lifc_len = req_size; |
| 306 |
lifc.lifc_buf = req; |
| 307 |
if (ioctl(fd, SIOCGLIFCONF, &lifc) < 0) { |
| 308 |
close(fd); |
| 309 |
free(req); |
| 310 |
error("SIOCGLIFCONF: %m"); |
| 311 |
return -1; |
| 312 |
} |
| 313 |
|
| 314 |
/* |
| 315 |
* And traverse each interface to look specifically for the first |
| 316 |
* occurence of an Ethernet interface which has been marked up |
| 317 |
*/ |
| 318 |
plifreq = lifc.lifc_req; |
| 319 |
found = 0; |
| 320 |
for (i = lifc.lifc_len / sizeof(struct lifreq); i > 0; i--, plifreq++) { |
| 321 |
|
| 322 |
if (strchr(plifreq->lifr_name, ':') != NULL) |
| 323 |
continue; |
| 324 |
|
| 325 |
memset(&lifr, 0, sizeof(lifr)); |
| 326 |
strncpy(lifr.lifr_name, plifreq->lifr_name, sizeof(lifr.lifr_name)); |
| 327 |
if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) { |
| 328 |
error("SIOCGLIFFLAGS: %m"); |
| 329 |
break; |
| 330 |
} |
| 331 |
fl = lifr.lifr_flags; |
| 332 |
|
| 333 |
if ((fl & (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP)) |
| 334 |
!= (IFF_UP | IFF_BROADCAST)) |
| 335 |
continue; |
| 336 |
|
| 337 |
if (get_if_hwaddr(addr, lifr.lifr_name) < 0) |
| 338 |
continue; |
| 339 |
|
| 340 |
found = 1; |
| 341 |
break; |
| 342 |
} |
| 343 |
free(req); |
| 344 |
close(fd); |
| 345 |
|
| 346 |
if (found) |
| 347 |
return 0; |
| 348 |
else |
| 349 |
return -1; |
| 350 |
} |
| 351 |
|
| 352 |
/* |
| 353 |
* get_if_hwaddr - get the hardware address for the specified |
| 354 |
* network interface device. |
| 355 |
*/ |
| 356 |
int |
| 357 |
get_if_hwaddr(u_char *addr, char *if_name) |
| 358 |
{ |
| 359 |
struct sockaddr s_eth_addr; |
| 360 |
struct ether_addr *eth_addr = (struct ether_addr *)&s_eth_addr.sa_data; |
| 361 |
|
| 362 |
if (if_name == NULL) |
| 363 |
return -1; |
| 364 |
|
| 365 |
/* |
| 366 |
* Send DL_INFO_REQ to the driver to solicit its MAC address |
| 367 |
*/ |
| 368 |
if (!get_hw_addr_dlpi(if_name, &s_eth_addr)) { |
| 369 |
error("could not obtain hardware address for %s", if_name); |
| 370 |
return -1; |
| 371 |
} |
| 372 |
|
| 373 |
memcpy(addr, eth_addr->ether_addr_octet, 6); |
| 374 |
return 1; |
| 375 |
} |
| 376 |
|
| 377 |
/* |
| 378 |
* slifname - Sets interface ppa and flags |
| 379 |
* |
| 380 |
* in addition to the comments stated in sifppa(), IFF_IPV6 bit must |
| 381 |
* be set in order to declare this as an IPv6 interface |
| 382 |
*/ |
| 383 |
static int |
| 384 |
slifname(int fd, int ppa) |
| 385 |
{ |
| 386 |
struct lifreq lifr; |
| 387 |
int ret; |
| 388 |
|
| 389 |
memset(&lifr, 0, sizeof(lifr)); |
| 390 |
ret = ioctl(fd, SIOCGLIFFLAGS, &lifr); |
| 391 |
if (ret < 0) |
| 392 |
goto slifname_done; |
| 393 |
|
| 394 |
lifr.lifr_flags |= IFF_IPV6; |
| 395 |
lifr.lifr_flags &= ~(IFF_BROADCAST | IFF_IPV4); |
| 396 |
lifr.lifr_ppa = ppa; |
| 397 |
strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); |
| 398 |
|
| 399 |
ret = ioctl(fd, SIOCSLIFNAME, &lifr); |
| 400 |
|
| 401 |
slifname_done: |
| 402 |
return ret; |
| 403 |
} |
| 404 |
|
| 405 |
/* |
| 406 |
* sys_init - System-dependent initialization. |
| 407 |
*/ |
| 408 |
void |
| 409 |
sys_init(void) |
| 410 |
{ |
| 411 |
int ifd, x; |
| 412 |
struct ifreq ifr; |
| 413 |
int i6fd; |
| 414 |
struct lifreq lifr; |
| 415 |
|
| 416 |
ipfd = open(mux_dev_name, O_RDWR, 0); |
| 417 |
if (ipfd < 0) |
| 418 |
fatal("Couldn't open IP device: %m"); |
| 419 |
|
| 420 |
ip6fd = open(UDP6_DEV_NAME, O_RDWR, 0); |
| 421 |
if (ip6fd < 0) |
| 422 |
fatal("Couldn't open IP device (2): %m"); |
| 423 |
|
| 424 |
if (default_device && !notty) |
| 425 |
tty_sid = getsid((pid_t)0); |
| 426 |
|
| 427 |
pppfd = open(PPP_DEV_NAME, O_RDWR | O_NONBLOCK, 0); |
| 428 |
if (pppfd < 0) |
| 429 |
fatal("Can't open %s: %m", PPP_DEV_NAME); |
| 430 |
if (kdebugflag & 1) { |
| 431 |
x = PPPDBG_LOG + PPPDBG_DRIVER; |
| 432 |
strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0); |
| 433 |
} |
| 434 |
|
| 435 |
/* Assign a new PPA and get its unit number. */ |
| 436 |
if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) |
| 437 |
fatal("Can't create new PPP interface: %m"); |
| 438 |
|
| 439 |
/* |
| 440 |
* Since sys_init() is called prior to ifname being set in main(), |
| 441 |
* we need to get the ifname now, otherwise slifname(), and others, |
| 442 |
* will fail, or maybe, I should move them to a later point ? |
| 443 |
* <[email protected]> |
| 444 |
*/ |
| 445 |
sprintf(ifname, PPP_DRV_NAME "%d", ifunit); |
| 446 |
|
| 447 |
/* |
| 448 |
* Open the ppp device again and link it under the ip multiplexor. |
| 449 |
* IP will assign a unit number which hopefully is the same as ifunit. |
| 450 |
* I don't know any way to be certain they will be the same. :-( |
| 451 |
*/ |
| 452 |
ifd = open(PPP_DEV_NAME, O_RDWR, 0); |
| 453 |
if (ifd < 0) |
| 454 |
fatal("Can't open %s (2): %m", PPP_DEV_NAME); |
| 455 |
if (kdebugflag & 1) { |
| 456 |
x = PPPDBG_LOG + PPPDBG_DRIVER; |
| 457 |
strioctl(ifd, PPPIO_DEBUG, &x, sizeof(int), 0); |
| 458 |
} |
| 459 |
|
| 460 |
i6fd = open(PPP_DEV_NAME, O_RDWR, 0); |
| 461 |
if (i6fd < 0) { |
| 462 |
close(ifd); |
| 463 |
fatal("Can't open %s (3): %m", PPP_DEV_NAME); |
| 464 |
} |
| 465 |
if (kdebugflag & 1) { |
| 466 |
x = PPPDBG_LOG + PPPDBG_DRIVER; |
| 467 |
strioctl(i6fd, PPPIO_DEBUG, &x, sizeof(int), 0); |
| 468 |
} |
| 469 |
|
| 470 |
if (ioctl(ifd, I_PUSH, IP_MOD_NAME) < 0) { |
| 471 |
close(ifd); |
| 472 |
close(i6fd); |
| 473 |
fatal("Can't push IP module: %m"); |
| 474 |
} |
| 475 |
|
| 476 |
/* |
| 477 |
* Assign ppa according to the unit number returned by ppp device |
| 478 |
* after plumbing is completed above. |
| 479 |
*/ |
| 480 |
if (sifppa(ifd, ifunit) < 0) { |
| 481 |
close (ifd); |
| 482 |
close(i6fd); |
| 483 |
fatal("Can't set ppa for unit %d: %m", ifunit); |
| 484 |
} |
| 485 |
|
| 486 |
/* |
| 487 |
* An IPv6 interface is created anyway, even when the user does not |
| 488 |
* explicitly enable it. Note that the interface will be marked |
| 489 |
* IPv6 during slifname(). |
| 490 |
*/ |
| 491 |
if (ioctl(i6fd, I_PUSH, IP_MOD_NAME) < 0) { |
| 492 |
close(ifd); |
| 493 |
close(i6fd); |
| 494 |
fatal("Can't push IP module (2): %m"); |
| 495 |
} |
| 496 |
|
| 497 |
/* |
| 498 |
* Assign ppa according to the unit number returned by ppp device |
| 499 |
* after plumbing is completed above. In addition, mark the interface |
| 500 |
* as an IPv6 interface. |
| 501 |
*/ |
| 502 |
if (slifname(i6fd, ifunit) < 0) { |
| 503 |
close(ifd); |
| 504 |
close(i6fd); |
| 505 |
fatal("Can't set ifname for unit %d: %m", ifunit); |
| 506 |
} |
| 507 |
|
| 508 |
ipmuxid = ioctl(ipfd, I_PLINK, ifd); |
| 509 |
close(ifd); |
| 510 |
if (ipmuxid < 0) { |
| 511 |
close(i6fd); |
| 512 |
fatal("Can't I_PLINK PPP device to IP: %m"); |
| 513 |
} |
| 514 |
|
| 515 |
memset(&ifr, 0, sizeof(ifr)); |
| 516 |
sprintf(ifr.ifr_name, "%s", ifname); |
| 517 |
ifr.ifr_ip_muxid = ipmuxid; |
| 518 |
|
| 519 |
/* |
| 520 |
* In Sol 8 and later, STREAMS dynamic module plumbing feature exists. |
| 521 |
* This is so that an arbitrary module can be inserted, or deleted, |
| 522 |
* between ip module and the device driver without tearing down the |
| 523 |
* existing stream. Such feature requires the mux ids, which is set |
| 524 |
* by SIOCSIFMUXID (or SIOCLSIFMUXID). |
| 525 |
*/ |
| 526 |
if (ioctl(ipfd, SIOCSIFMUXID, &ifr) < 0) { |
| 527 |
ioctl(ipfd, I_PUNLINK, ipmuxid); |
| 528 |
close(i6fd); |
| 529 |
fatal("SIOCSIFMUXID: %m"); |
| 530 |
} |
| 531 |
|
| 532 |
ip6muxid = ioctl(ip6fd, I_PLINK, i6fd); |
| 533 |
close(i6fd); |
| 534 |
if (ip6muxid < 0) { |
| 535 |
ioctl(ipfd, I_PUNLINK, ipmuxid); |
| 536 |
fatal("Can't I_PLINK PPP device to IP (2): %m"); |
| 537 |
} |
| 538 |
|
| 539 |
memset(&lifr, 0, sizeof(lifr)); |
| 540 |
sprintf(lifr.lifr_name, "%s", ifname); |
| 541 |
lifr.lifr_ip_muxid = ip6muxid; |
| 542 |
|
| 543 |
/* |
| 544 |
* Let IP know of the mux id [see comment for SIOCSIFMUXID above] |
| 545 |
*/ |
| 546 |
if (ioctl(ip6fd, SIOCSLIFMUXID, &lifr) < 0) { |
| 547 |
ioctl(ipfd, I_PUNLINK, ipmuxid); |
| 548 |
ioctl(ip6fd, I_PUNLINK, ip6muxid); |
| 549 |
fatal("Can't link PPP device to IP (2): %m"); |
| 550 |
} |
| 551 |
|
| 552 |
n_pollfds = 0; |
| 553 |
} |
| 554 |
|
| 555 |
/* |
| 556 |
* sys_cleanup - restore any system state we modified before exiting: |
| 557 |
* mark the interface down, delete default route and/or proxy arp entry. |
| 558 |
* This should call die() because it's called from die(). |
| 559 |
*/ |
| 560 |
void |
| 561 |
sys_cleanup(void) |
| 562 |
{ |
| 563 |
struct ifreq ifr; |
| 564 |
struct lifreq lifr; |
| 565 |
|
| 566 |
if (if6_is_up) |
| 567 |
sif6down(0); |
| 568 |
if (if_is_up) |
| 569 |
sifdown(0); |
| 570 |
if (default_route_gateway) |
| 571 |
cifdefaultroute(0, default_route_gateway, default_route_gateway); |
| 572 |
if (default_route_gateway6.e32[0] != 0 || default_route_gateway6.e32[1] != 0) |
| 573 |
cif6defaultroute(0, default_route_gateway6, default_route_gateway6); |
| 574 |
if (proxy_arp_addr) |
| 575 |
cifproxyarp(0, proxy_arp_addr); |
| 576 |
|
| 577 |
/* |
| 578 |
* Make sure we ask ip what the muxid, because 'ifconfig modlist' will |
| 579 |
* unlink and re-link the modules, causing the muxid to change. |
| 580 |
*/ |
| 581 |
memset(&ifr, 0, sizeof(ifr)); |
| 582 |
sprintf(ifr.ifr_name, "%s", ifname); |
| 583 |
if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) { |
| 584 |
error("SIOCGIFFLAGS: %m"); |
| 585 |
return; |
| 586 |
} |
| 587 |
|
| 588 |
if (ioctl(ipfd, SIOCGIFMUXID, &ifr) < 0) { |
| 589 |
error("SIOCGIFMUXID: %m"); |
| 590 |
return; |
| 591 |
} |
| 592 |
|
| 593 |
ipmuxid = ifr.ifr_ip_muxid; |
| 594 |
|
| 595 |
if (ioctl(ipfd, I_PUNLINK, ipmuxid) < 0) { |
| 596 |
error("Can't I_PUNLINK PPP from IP: %m"); |
| 597 |
return; |
| 598 |
} |
| 599 |
|
| 600 |
/* |
| 601 |
* Make sure we ask ip what the muxid, because 'ifconfig modlist' will |
| 602 |
* unlink and re-link the modules, causing the muxid to change. |
| 603 |
*/ |
| 604 |
memset(&lifr, 0, sizeof(lifr)); |
| 605 |
sprintf(lifr.lifr_name, "%s", ifname); |
| 606 |
if (ioctl(ip6fd, SIOCGLIFFLAGS, &lifr) < 0) { |
| 607 |
error("SIOCGLIFFLAGS: %m"); |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
if (ioctl(ip6fd, SIOCGLIFMUXID, &lifr) < 0) { |
| 612 |
error("SIOCGLIFMUXID: %m"); |
| 613 |
return; |
| 614 |
} |
| 615 |
|
| 616 |
ip6muxid = lifr.lifr_ip_muxid; |
| 617 |
|
| 618 |
if (ioctl(ip6fd, I_PUNLINK, ip6muxid) < 0) { |
| 619 |
error("Can't I_PUNLINK PPP from IP (2): %m"); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
/* |
| 624 |
* sys_close - Clean up in a child process before execing. |
| 625 |
*/ |
| 626 |
void |
| 627 |
ppp_sys_close(void) |
| 628 |
{ |
| 629 |
close(ipfd); |
| 630 |
close(ip6fd); |
| 631 |
if (pppfd >= 0) |
| 632 |
close(pppfd); |
| 633 |
} |
| 634 |
|
| 635 |
/* |
| 636 |
* sys_check_options - check the options that the user specified |
| 637 |
*/ |
| 638 |
int |
| 639 |
sys_check_options(void) |
| 640 |
{ |
| 641 |
return 1; |
| 642 |
} |
| 643 |
|
| 644 |
/* |
| 645 |
* ppp_check_kernel_support - check whether the system has any ppp interfaces |
| 646 |
*/ |
| 647 |
int |
| 648 |
ppp_check_kernel_support(void) |
| 649 |
{ |
| 650 |
struct stat buf; |
| 651 |
|
| 652 |
return stat(PPP_DEV_NAME, &buf) >= 0; |
| 653 |
} |
| 654 |
|
| 655 |
/* |
| 656 |
* any_compressions - see if compression is enabled or not |
| 657 |
* |
| 658 |
* In the STREAMS implementation of kernel-portion pppd, |
| 659 |
* the comp STREAMS module performs the ACFC, PFC, as well |
| 660 |
* CCP and VJ compressions. However, if the user has explicitly |
| 661 |
* declare to not enable them from the command line, there is |
| 662 |
* no point of having the comp module be pushed on the stream. |
| 663 |
*/ |
| 664 |
static int |
| 665 |
any_compressions(void) |
| 666 |
{ |
| 667 |
if ((!lcp_wantoptions[0].neg_accompression) && |
| 668 |
(!lcp_wantoptions[0].neg_pcompression) && |
| 669 |
(!ccp_protent.enabled_flag) && |
| 670 |
(!ipcp_wantoptions[0].neg_vj)) { |
| 671 |
return 0; |
| 672 |
} |
| 673 |
return 1; |
| 674 |
} |
| 675 |
|
| 676 |
/* |
| 677 |
* tty_establish_ppp - Turn the serial port into a ppp interface. |
| 678 |
*/ |
| 679 |
int |
| 680 |
tty_establish_ppp(int fd) |
| 681 |
{ |
| 682 |
int i; |
| 683 |
|
| 684 |
/* Pop any existing modules off the tty stream. */ |
| 685 |
for (i = 0;; ++i) |
| 686 |
if (ioctl(fd, I_LOOK, tty_modules[i]) < 0 |
| 687 |
|| strcmp(tty_modules[i], "ptem") == 0 |
| 688 |
|| ioctl(fd, I_POP, 0) < 0) |
| 689 |
break; |
| 690 |
tty_nmodules = i; |
| 691 |
|
| 692 |
/* Push the async hdlc module and the compressor module. */ |
| 693 |
tty_npushed = 0; |
| 694 |
|
| 695 |
if(!ppp_sync_serial()) { |
| 696 |
if (ioctl(fd, I_PUSH, AHDLC_MOD_NAME) < 0) { |
| 697 |
error("Couldn't push PPP Async HDLC module: %m"); |
| 698 |
return -1; |
| 699 |
} |
| 700 |
++tty_npushed; |
| 701 |
} |
| 702 |
if (kdebugflag & 4) { |
| 703 |
i = PPPDBG_LOG + PPPDBG_AHDLC; |
| 704 |
strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0); |
| 705 |
} |
| 706 |
/* |
| 707 |
* There's no need to push comp module if we don't intend |
| 708 |
* to compress anything |
| 709 |
*/ |
| 710 |
if (any_compressions()) { |
| 711 |
if (ioctl(fd, I_PUSH, COMP_MOD_NAME) < 0) |
| 712 |
error("Couldn't push PPP compression module: %m"); |
| 713 |
else |
| 714 |
++tty_npushed; |
| 715 |
} |
| 716 |
|
| 717 |
if (kdebugflag & 2) { |
| 718 |
i = PPPDBG_LOG; |
| 719 |
if (any_compressions()) |
| 720 |
i += PPPDBG_COMP; |
| 721 |
strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0); |
| 722 |
} |
| 723 |
|
| 724 |
/* Link the serial port under the PPP multiplexor. */ |
| 725 |
if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) { |
| 726 |
error("Can't link tty to PPP mux: %m"); |
| 727 |
return -1; |
| 728 |
} |
| 729 |
|
| 730 |
return pppfd; |
| 731 |
} |
| 732 |
|
| 733 |
/* |
| 734 |
* tty_disestablish_ppp - Restore the serial port to normal operation. |
| 735 |
* It attempts to reconstruct the stream with the previously popped |
| 736 |
* modules. This shouldn't call die() because it's called from die(). |
| 737 |
*/ |
| 738 |
void |
| 739 |
tty_disestablish_ppp(int fd) |
| 740 |
{ |
| 741 |
int i; |
| 742 |
|
| 743 |
if (fdmuxid >= 0) { |
| 744 |
if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) { |
| 745 |
if (!hungup) |
| 746 |
error("Can't unlink tty from PPP mux: %m"); |
| 747 |
} |
| 748 |
fdmuxid = -1; |
| 749 |
|
| 750 |
if (!hungup) { |
| 751 |
while (tty_npushed > 0 && ioctl(fd, I_POP, 0) >= 0) |
| 752 |
--tty_npushed; |
| 753 |
for (i = tty_nmodules - 1; i >= 0; --i) |
| 754 |
if (ioctl(fd, I_PUSH, tty_modules[i]) < 0) |
| 755 |
error("Couldn't restore tty module %s: %m", |
| 756 |
tty_modules[i]); |
| 757 |
} |
| 758 |
if (hungup && default_device && tty_sid > 0) { |
| 759 |
/* |
| 760 |
* If we have received a hangup, we need to send a SIGHUP |
| 761 |
* to the terminal's controlling process. The reason is |
| 762 |
* that the original stream head for the terminal hasn't |
| 763 |
* seen the M_HANGUP message (it went up through the ppp |
| 764 |
* driver to the stream head for our fd to /dev/ppp). |
| 765 |
*/ |
| 766 |
kill(tty_sid, SIGHUP); |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
/* |
| 772 |
* Check whether the link seems not to be 8-bit clean. |
| 773 |
*/ |
| 774 |
void |
| 775 |
clean_check(void) |
| 776 |
{ |
| 777 |
int x; |
| 778 |
char *s; |
| 779 |
|
| 780 |
if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0) |
| 781 |
return; |
| 782 |
s = NULL; |
| 783 |
switch (~x) { |
| 784 |
case RCV_B7_0: |
| 785 |
s = "bit 7 set to 1"; |
| 786 |
break; |
| 787 |
case RCV_B7_1: |
| 788 |
s = "bit 7 set to 0"; |
| 789 |
break; |
| 790 |
case RCV_EVNP: |
| 791 |
s = "odd parity"; |
| 792 |
break; |
| 793 |
case RCV_ODDP: |
| 794 |
s = "even parity"; |
| 795 |
break; |
| 796 |
} |
| 797 |
if (s != NULL) { |
| 798 |
warn("Serial link is not 8-bit clean:"); |
| 799 |
warn("All received characters had %s", s); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
/* |
| 804 |
* List of valid speeds. |
| 805 |
*/ |
| 806 |
struct speed { |
| 807 |
int speed_int, speed_val; |
| 808 |
} speeds[] = { |
| 809 |
#ifdef B50 |
| 810 |
{ 50, B50 }, |
| 811 |
#endif |
| 812 |
#ifdef B75 |
| 813 |
{ 75, B75 }, |
| 814 |
#endif |
| 815 |
#ifdef B110 |
| 816 |
{ 110, B110 }, |
| 817 |
#endif |
| 818 |
#ifdef B134 |
| 819 |
{ 134, B134 }, |
| 820 |
#endif |
| 821 |
#ifdef B150 |
| 822 |
{ 150, B150 }, |
| 823 |
#endif |
| 824 |
#ifdef B200 |
| 825 |
{ 200, B200 }, |
| 826 |
#endif |
| 827 |
#ifdef B300 |
| 828 |
{ 300, B300 }, |
| 829 |
#endif |
| 830 |
#ifdef B600 |
| 831 |
{ 600, B600 }, |
| 832 |
#endif |
| 833 |
#ifdef B1200 |
| 834 |
{ 1200, B1200 }, |
| 835 |
#endif |
| 836 |
#ifdef B1800 |
| 837 |
{ 1800, B1800 }, |
| 838 |
#endif |
| 839 |
#ifdef B2000 |
| 840 |
{ 2000, B2000 }, |
| 841 |
#endif |
| 842 |
#ifdef B2400 |
| 843 |
{ 2400, B2400 }, |
| 844 |
#endif |
| 845 |
#ifdef B3600 |
| 846 |
{ 3600, B3600 }, |
| 847 |
#endif |
| 848 |
#ifdef B4800 |
| 849 |
{ 4800, B4800 }, |
| 850 |
#endif |
| 851 |
#ifdef B7200 |
| 852 |
{ 7200, B7200 }, |
| 853 |
#endif |
| 854 |
#ifdef B9600 |
| 855 |
{ 9600, B9600 }, |
| 856 |
#endif |
| 857 |
#ifdef B19200 |
| 858 |
{ 19200, B19200 }, |
| 859 |
#endif |
| 860 |
#ifdef B38400 |
| 861 |
{ 38400, B38400 }, |
| 862 |
#endif |
| 863 |
#ifdef EXTA |
| 864 |
{ 19200, EXTA }, |
| 865 |
#endif |
| 866 |
#ifdef EXTB |
| 867 |
{ 38400, EXTB }, |
| 868 |
#endif |
| 869 |
#ifdef B57600 |
| 870 |
{ 57600, B57600 }, |
| 871 |
#endif |
| 872 |
#ifdef B76800 |
| 873 |
{ 76800, B76800 }, |
| 874 |
#endif |
| 875 |
#ifdef B115200 |
| 876 |
{ 115200, B115200 }, |
| 877 |
#endif |
| 878 |
#ifdef B153600 |
| 879 |
{ 153600, B153600 }, |
| 880 |
#endif |
| 881 |
#ifdef B230400 |
| 882 |
{ 230400, B230400 }, |
| 883 |
#endif |
| 884 |
#ifdef B307200 |
| 885 |
{ 307200, B307200 }, |
| 886 |
#endif |
| 887 |
#ifdef B460800 |
| 888 |
{ 460800, B460800 }, |
| 889 |
#endif |
| 890 |
{ 0, 0 } |
| 891 |
}; |
| 892 |
|
| 893 |
/* |
| 894 |
* Translate from bits/second to a speed_t. |
| 895 |
*/ |
| 896 |
static int |
| 897 |
translate_speed(int bps) |
| 898 |
{ |
| 899 |
struct speed *speedp; |
| 900 |
|
| 901 |
if (bps == 0) |
| 902 |
return 0; |
| 903 |
for (speedp = speeds; speedp->speed_int; speedp++) |
| 904 |
if (bps == speedp->speed_int) |
| 905 |
return speedp->speed_val; |
| 906 |
warn("speed %d not supported", bps); |
| 907 |
return 0; |
| 908 |
} |
| 909 |
|
| 910 |
/* |
| 911 |
* Translate from a speed_t to bits/second. |
| 912 |
*/ |
| 913 |
static int |
| 914 |
baud_rate_of(int speed) |
| 915 |
{ |
| 916 |
struct speed *speedp; |
| 917 |
|
| 918 |
if (speed == 0) |
| 919 |
return 0; |
| 920 |
for (speedp = speeds; speedp->speed_int; speedp++) |
| 921 |
if (speed == speedp->speed_val) |
| 922 |
return speedp->speed_int; |
| 923 |
return 0; |
| 924 |
} |
| 925 |
|
| 926 |
/* |
| 927 |
* set_up_tty: Set up the serial port on `fd' for 8 bits, no parity, |
| 928 |
* at the requested speed, etc. If `local' is true, set CLOCAL |
| 929 |
* regardless of whether the modem option was specified. |
| 930 |
*/ |
| 931 |
void |
| 932 |
set_up_tty(int fd, int local) |
| 933 |
{ |
| 934 |
int speed; |
| 935 |
struct termios tios; |
| 936 |
|
| 937 |
if (!ppp_sync_serial() && tcgetattr(fd, &tios) < 0) |
| 938 |
fatal("tcgetattr: %m"); |
| 939 |
|
| 940 |
if (!restore_term) { |
| 941 |
inittermios = tios; |
| 942 |
if (!ppp_sync_serial()) |
| 943 |
ioctl(fd, TIOCGWINSZ, &wsinfo); |
| 944 |
} |
| 945 |
|
| 946 |
tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL); |
| 947 |
if (crtscts > 0) |
| 948 |
tios.c_cflag |= CRTSCTS; |
| 949 |
else if (crtscts < 0) |
| 950 |
tios.c_cflag &= ~CRTSCTS; |
| 951 |
|
| 952 |
if (stop_bits >= 2) |
| 953 |
tios.c_cflag |= CSTOPB; |
| 954 |
|
| 955 |
tios.c_cflag |= CS8 | CREAD | HUPCL; |
| 956 |
if (local || !modem) |
| 957 |
tios.c_cflag |= CLOCAL; |
| 958 |
tios.c_iflag = IGNBRK | IGNPAR; |
| 959 |
tios.c_oflag = 0; |
| 960 |
tios.c_lflag = 0; |
| 961 |
tios.c_cc[VMIN] = 1; |
| 962 |
tios.c_cc[VTIME] = 0; |
| 963 |
|
| 964 |
if (crtscts == -2) { |
| 965 |
tios.c_iflag |= IXON | IXOFF; |
| 966 |
tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */ |
| 967 |
tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */ |
| 968 |
} |
| 969 |
|
| 970 |
speed = translate_speed(inspeed); |
| 971 |
if (speed) { |
| 972 |
cfsetospeed(&tios, speed); |
| 973 |
cfsetispeed(&tios, speed); |
| 974 |
} else { |
| 975 |
speed = cfgetospeed(&tios); |
| 976 |
/* |
| 977 |
* We can't proceed if the serial port speed is 0, |
| 978 |
* since that implies that the serial port is disabled. |
| 979 |
*/ |
| 980 |
if ((speed == B0) && !ppp_sync_serial()) |
| 981 |
fatal("Baud rate for %s is 0; need explicit baud rate", devnam); |
| 982 |
} |
| 983 |
|
| 984 |
if (!ppp_sync_serial() && tcsetattr(fd, TCSAFLUSH, &tios) < 0) |
| 985 |
fatal("tcsetattr: %m"); |
| 986 |
|
| 987 |
baud_rate = inspeed = baud_rate_of(speed); |
| 988 |
if (!ppp_sync_serial()) |
| 989 |
restore_term = 1; |
| 990 |
} |
| 991 |
|
| 992 |
/* |
| 993 |
* restore_tty - restore the terminal to the saved settings. |
| 994 |
*/ |
| 995 |
void |
| 996 |
restore_tty(int fd) |
| 997 |
{ |
| 998 |
if (restore_term) { |
| 999 |
if (!default_device) { |
| 1000 |
/* |
| 1001 |
* Turn off echoing, because otherwise we can get into |
| 1002 |
* a loop with the tty and the modem echoing to each other. |
| 1003 |
* We presume we are the sole user of this tty device, so |
| 1004 |
* when we close it, it will revert to its defaults anyway. |
| 1005 |
*/ |
| 1006 |
inittermios.c_lflag &= ~(ECHO | ECHONL); |
| 1007 |
} |
| 1008 |
if (!ppp_sync_serial() && tcsetattr(fd, TCSAFLUSH, &inittermios) < 0) |
| 1009 |
if (!hungup && errno != ENXIO) |
| 1010 |
warn("tcsetattr: %m"); |
| 1011 |
if (!ppp_sync_serial()) |
| 1012 |
ioctl(fd, TIOCSWINSZ, &wsinfo); |
| 1013 |
restore_term = 0; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
/* |
| 1018 |
* setdtr - control the DTR line on the serial port. |
| 1019 |
* This is called from die(), so it shouldn't call die(). |
| 1020 |
*/ |
| 1021 |
void |
| 1022 |
setdtr(int fd, int on) |
| 1023 |
{ |
| 1024 |
int modembits = TIOCM_DTR; |
| 1025 |
|
| 1026 |
ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); |
| 1027 |
} |
| 1028 |
|
| 1029 |
/* |
| 1030 |
* open_loopback - open the device we use for getting packets |
| 1031 |
* in demand mode. Under Solaris 2, we use our existing fd |
| 1032 |
* to the ppp driver. |
| 1033 |
*/ |
| 1034 |
int |
| 1035 |
open_ppp_loopback(void) |
| 1036 |
{ |
| 1037 |
return pppfd; |
| 1038 |
} |
| 1039 |
|
| 1040 |
/* |
| 1041 |
* output - Output PPP packet. |
| 1042 |
*/ |
| 1043 |
void |
| 1044 |
output(int unit, u_char *p, int len) |
| 1045 |
{ |
| 1046 |
struct strbuf data; |
| 1047 |
int retries; |
| 1048 |
struct pollfd pfd; |
| 1049 |
|
| 1050 |
dump_packet("sent", p, len); |
| 1051 |
if (snoop_send_hook) snoop_send_hook(p, len); |
| 1052 |
|
| 1053 |
data.len = len; |
| 1054 |
data.buf = (caddr_t) p; |
| 1055 |
retries = 4; |
| 1056 |
while (putmsg(pppfd, NULL, &data, 0) < 0) { |
| 1057 |
if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) { |
| 1058 |
if (errno != ENXIO) |
| 1059 |
error("Couldn't send packet: %m"); |
| 1060 |
break; |
| 1061 |
} |
| 1062 |
pfd.fd = pppfd; |
| 1063 |
pfd.events = POLLOUT; |
| 1064 |
poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */ |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
/* |
| 1070 |
* wait_input - wait until there is data available, |
| 1071 |
* for the length of time specified by *timo (indefinite |
| 1072 |
* if timo is NULL). |
| 1073 |
*/ |
| 1074 |
void |
| 1075 |
wait_input(struct timeval *timo) |
| 1076 |
{ |
| 1077 |
int t; |
| 1078 |
|
| 1079 |
t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000; |
| 1080 |
if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR) |
| 1081 |
fatal("poll: %m"); |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* |
| 1085 |
* add_fd - add an fd to the set that wait_input waits for. |
| 1086 |
*/ |
| 1087 |
void add_fd(int fd) |
| 1088 |
{ |
| 1089 |
int n; |
| 1090 |
|
| 1091 |
for (n = 0; n < n_pollfds; ++n) |
| 1092 |
if (pollfds[n].fd == fd) |
| 1093 |
return; |
| 1094 |
if (n_pollfds < MAX_POLLFDS) { |
| 1095 |
pollfds[n_pollfds].fd = fd; |
| 1096 |
pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP; |
| 1097 |
++n_pollfds; |
| 1098 |
} else |
| 1099 |
error("Too many inputs!"); |
| 1100 |
} |
| 1101 |
|
| 1102 |
/* |
| 1103 |
* remove_fd - remove an fd from the set that wait_input waits for. |
| 1104 |
*/ |
| 1105 |
void remove_fd(int fd) |
| 1106 |
{ |
| 1107 |
int n; |
| 1108 |
|
| 1109 |
for (n = 0; n < n_pollfds; ++n) { |
| 1110 |
if (pollfds[n].fd == fd) { |
| 1111 |
while (++n < n_pollfds) |
| 1112 |
pollfds[n-1] = pollfds[n]; |
| 1113 |
--n_pollfds; |
| 1114 |
break; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
/* |
| 1120 |
* read_packet - get a PPP packet from the serial device. |
| 1121 |
*/ |
| 1122 |
int |
| 1123 |
read_packet(u_char *buf) |
| 1124 |
{ |
| 1125 |
struct strbuf ctrl, data; |
| 1126 |
int flags, len; |
| 1127 |
unsigned char ctrlbuf[sizeof(union DL_primitives) + 64]; |
| 1128 |
|
| 1129 |
for (;;) { |
| 1130 |
data.maxlen = PPP_MRU + PPP_HDRLEN; |
| 1131 |
data.buf = (caddr_t) buf; |
| 1132 |
ctrl.maxlen = sizeof(ctrlbuf); |
| 1133 |
ctrl.buf = (caddr_t) ctrlbuf; |
| 1134 |
flags = 0; |
| 1135 |
len = getmsg(pppfd, &ctrl, &data, &flags); |
| 1136 |
if (len < 0) { |
| 1137 |
if (errno == EAGAIN || errno == EINTR) |
| 1138 |
return -1; |
| 1139 |
fatal("Error reading packet: %m"); |
| 1140 |
} |
| 1141 |
|
| 1142 |
if (ctrl.len <= 0) |
| 1143 |
return data.len; |
| 1144 |
|
| 1145 |
/* |
| 1146 |
* Got a M_PROTO or M_PCPROTO message. Interpret it |
| 1147 |
* as a DLPI primitive?? |
| 1148 |
*/ |
| 1149 |
if (debug && ((union DL_primitives *)ctrlbuf)->dl_primitive != 0x53505050) |
| 1150 |
dbglog("got dlpi prim 0x%x, len=%d", |
| 1151 |
((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len); |
| 1152 |
|
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
/* |
| 1157 |
* get_loop_output - get outgoing packets from the ppp device, |
| 1158 |
* and detect when we want to bring the real link up. |
| 1159 |
* Return value is 1 if we need to bring up the link, 0 otherwise. |
| 1160 |
*/ |
| 1161 |
int |
| 1162 |
get_loop_output(void) |
| 1163 |
{ |
| 1164 |
int len; |
| 1165 |
int rv = 0; |
| 1166 |
|
| 1167 |
while ((len = read_packet(inpacket_buf)) > 0) { |
| 1168 |
if (loop_frame(inpacket_buf, len)) |
| 1169 |
rv = 1; |
| 1170 |
} |
| 1171 |
return rv; |
| 1172 |
} |
| 1173 |
|
| 1174 |
/* |
| 1175 |
* ppp_set_mtu - set the MTU on the PPP network interface. |
| 1176 |
*/ |
| 1177 |
void |
| 1178 |
ppp_set_mtu(int unit, int mtu) |
| 1179 |
{ |
| 1180 |
struct ifreq ifr; |
| 1181 |
struct lifreq lifr; |
| 1182 |
int fd; |
| 1183 |
|
| 1184 |
memset(&ifr, 0, sizeof(ifr)); |
| 1185 |
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 1186 |
ifr.ifr_metric = mtu; |
| 1187 |
if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) { |
| 1188 |
error("Couldn't set IP MTU (%s): %m", ifr.ifr_name); |
| 1189 |
} |
| 1190 |
|
| 1191 |
fd = socket(AF_INET6, SOCK_DGRAM, 0); |
| 1192 |
if (fd < 0) |
| 1193 |
error("Couldn't open IPv6 socket: %m"); |
| 1194 |
|
| 1195 |
memset(&lifr, 0, sizeof(lifr)); |
| 1196 |
strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); |
| 1197 |
lifr.lifr_mtu = mtu; |
| 1198 |
if (ioctl(fd, SIOCSLIFMTU, &lifr) < 0) { |
| 1199 |
close(fd); |
| 1200 |
error("Couldn't set IPv6 MTU (%s): %m", ifr.ifr_name); |
| 1201 |
} |
| 1202 |
close(fd); |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
/* |
| 1208 |
* ppp_get_mtu - get the MTU on the PPP network interface. |
| 1209 |
*/ |
| 1210 |
int |
| 1211 |
ppp_get_mtu(int unit) |
| 1212 |
{ |
| 1213 |
struct ifreq ifr; |
| 1214 |
|
| 1215 |
memset (&ifr, '\0', sizeof (ifr)); |
| 1216 |
strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); |
| 1217 |
|
| 1218 |
if (ioctl(ipfd, SIOCGIFMTU, (caddr_t) &ifr) < 0) { |
| 1219 |
error("ioctl(SIOCGIFMTU): %m (line %d)", __LINE__); |
| 1220 |
return 0; |
| 1221 |
} |
| 1222 |
return ifr.ifr_metric; |
| 1223 |
} |
| 1224 |
|
| 1225 |
/* |
| 1226 |
* tty_send_config - configure the transmit characteristics of |
| 1227 |
* the ppp interface. |
| 1228 |
*/ |
| 1229 |
void |
| 1230 |
tty_send_config(int mtu, u_int32_t asyncmap, int pcomp, int accomp) |
| 1231 |
{ |
| 1232 |
int cf[2]; |
| 1233 |
|
| 1234 |
link_mtu = mtu; |
| 1235 |
if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) { |
| 1236 |
if (hungup && errno == ENXIO) { |
| 1237 |
++error_count; |
| 1238 |
return; |
| 1239 |
} |
| 1240 |
error("Couldn't set MTU: %m"); |
| 1241 |
} |
| 1242 |
if (fdmuxid >= 0) { |
| 1243 |
if (!ppp_sync_serial()) { |
| 1244 |
if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) |
| 1245 |
error("Couldn't set transmit ACCM: %m"); |
| 1246 |
} |
| 1247 |
cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0); |
| 1248 |
cf[1] = COMP_PROT | COMP_AC; |
| 1249 |
if (any_compressions() && |
| 1250 |
strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) |
| 1251 |
error("Couldn't set prot/AC compression: %m"); |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
/* |
| 1256 |
* tty_set_xaccm - set the extended transmit ACCM for the interface. |
| 1257 |
*/ |
| 1258 |
void |
| 1259 |
tty_set_xaccm(ext_accm accm) |
| 1260 |
{ |
| 1261 |
if (ppp_sync_serial()) |
| 1262 |
return; |
| 1263 |
|
| 1264 |
if (fdmuxid >= 0 |
| 1265 |
&& strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) { |
| 1266 |
if (!hungup || errno != ENXIO) |
| 1267 |
warn("Couldn't set extended ACCM: %m"); |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
/* |
| 1272 |
* tty_recv_config - configure the receive-side characteristics of |
| 1273 |
* the ppp interface. |
| 1274 |
*/ |
| 1275 |
void |
| 1276 |
tty_recv_config(int mru, u_int32_t asyncmap, int pcomp, int accomp) |
| 1277 |
{ |
| 1278 |
int cf[2]; |
| 1279 |
|
| 1280 |
link_mru = mru; |
| 1281 |
if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) { |
| 1282 |
if (hungup && errno == ENXIO) { |
| 1283 |
++error_count; |
| 1284 |
return; |
| 1285 |
} |
| 1286 |
error("Couldn't set MRU: %m"); |
| 1287 |
} |
| 1288 |
if (fdmuxid >= 0) { |
| 1289 |
if (!ppp_sync_serial()) { |
| 1290 |
if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) |
| 1291 |
error("Couldn't set receive ACCM: %m"); |
| 1292 |
} |
| 1293 |
cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0); |
| 1294 |
cf[1] = DECOMP_PROT | DECOMP_AC; |
| 1295 |
if (any_compressions() && |
| 1296 |
strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) |
| 1297 |
error("Couldn't set prot/AC decompression: %m"); |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
/* |
| 1302 |
* ccp_test - ask kernel whether a given compression method |
| 1303 |
* is acceptable for use. |
| 1304 |
*/ |
| 1305 |
int |
| 1306 |
ccp_test(int unit, u_char *opt_ptr, int opt_len, int for_transmit) |
| 1307 |
{ |
| 1308 |
if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP), |
| 1309 |
opt_ptr, opt_len, 0) >= 0) |
| 1310 |
return 1; |
| 1311 |
return (errno == ENOSR)? 0: -1; |
| 1312 |
} |
| 1313 |
|
| 1314 |
/* |
| 1315 |
* ccp_flags_set - inform kernel about the current state of CCP. |
| 1316 |
*/ |
| 1317 |
void |
| 1318 |
ccp_flags_set(int unit, int isopen, int isup) |
| 1319 |
{ |
| 1320 |
int cf[2]; |
| 1321 |
|
| 1322 |
cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0); |
| 1323 |
cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR; |
| 1324 |
if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) { |
| 1325 |
if (!hungup || errno != ENXIO) |
| 1326 |
error("Couldn't set kernel CCP state: %m"); |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
/* |
| 1331 |
* get_idle_time - return how long the link has been idle. |
| 1332 |
*/ |
| 1333 |
int |
| 1334 |
get_idle_time(int u, struct ppp_idle *ip) |
| 1335 |
{ |
| 1336 |
return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0; |
| 1337 |
} |
| 1338 |
|
| 1339 |
/* |
| 1340 |
* get_ppp_stats - return statistics for the link. |
| 1341 |
*/ |
| 1342 |
int |
| 1343 |
get_ppp_stats(int u, struct pppd_stats *stats) |
| 1344 |
{ |
| 1345 |
struct ppp_stats64 s; |
| 1346 |
|
| 1347 |
if (!ppp_sync_serial() && |
| 1348 |
strioctl(pppfd, PPPIO_GETSTAT64, &s, 0, sizeof(s)) < 0) { |
| 1349 |
error("Couldn't get link statistics: %m"); |
| 1350 |
return 0; |
| 1351 |
} |
| 1352 |
stats->bytes_in = s.p.ppp_ibytes; |
| 1353 |
stats->bytes_out = s.p.ppp_obytes; |
| 1354 |
stats->pkts_in = s.p.ppp_ipackets; |
| 1355 |
stats->pkts_out = s.p.ppp_opackets; |
| 1356 |
return 1; |
| 1357 |
} |
| 1358 |
|
| 1359 |
/* |
| 1360 |
* ccp_fatal_error - returns 1 if decompression was disabled as a |
| 1361 |
* result of an error detected after decompression of a packet, |
| 1362 |
* 0 otherwise. This is necessary because of patent nonsense. |
| 1363 |
*/ |
| 1364 |
int |
| 1365 |
ccp_fatal_error(int unit) |
| 1366 |
{ |
| 1367 |
int cf[2]; |
| 1368 |
|
| 1369 |
cf[0] = cf[1] = 0; |
| 1370 |
if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) { |
| 1371 |
if (errno != ENXIO && errno != EINVAL) |
| 1372 |
error("Couldn't get compression flags: %m"); |
| 1373 |
return 0; |
| 1374 |
} |
| 1375 |
return cf[0] & CCP_FATALERROR; |
| 1376 |
} |
| 1377 |
|
| 1378 |
/* |
| 1379 |
* sifvjcomp - config tcp header compression |
| 1380 |
*/ |
| 1381 |
int |
| 1382 |
sifvjcomp(int u, int vjcomp, int xcidcomp, int xmaxcid) |
| 1383 |
{ |
| 1384 |
int cf[2]; |
| 1385 |
char maxcid[2]; |
| 1386 |
|
| 1387 |
if (vjcomp) { |
| 1388 |
maxcid[0] = xcidcomp; |
| 1389 |
maxcid[1] = 15; /* XXX should be rmaxcid */ |
| 1390 |
if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) { |
| 1391 |
error("Couldn't initialize VJ compression: %m"); |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */ |
| 1396 |
+ (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0); |
| 1397 |
cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID; |
| 1398 |
if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) { |
| 1399 |
if (vjcomp) |
| 1400 |
error("Couldn't enable VJ compression: %m"); |
| 1401 |
} |
| 1402 |
|
| 1403 |
return 1; |
| 1404 |
} |
| 1405 |
|
| 1406 |
/* |
| 1407 |
* sifup - Config the interface up and enable IP packets to pass. |
| 1408 |
*/ |
| 1409 |
int |
| 1410 |
sifup(int u) |
| 1411 |
{ |
| 1412 |
struct ifreq ifr; |
| 1413 |
|
| 1414 |
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 1415 |
if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) { |
| 1416 |
error("Couldn't mark interface up (get): %m"); |
| 1417 |
return 0; |
| 1418 |
} |
| 1419 |
ifr.ifr_flags |= IFF_UP; |
| 1420 |
if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) { |
| 1421 |
error("Couldn't mark interface up (set): %m"); |
| 1422 |
return 0; |
| 1423 |
} |
| 1424 |
if_is_up = 1; |
| 1425 |
return 1; |
| 1426 |
} |
| 1427 |
|
| 1428 |
/* |
| 1429 |
* sifdown - Config the interface down and disable IP. |
| 1430 |
*/ |
| 1431 |
int |
| 1432 |
sifdown(int u) |
| 1433 |
{ |
| 1434 |
struct ifreq ifr; |
| 1435 |
|
| 1436 |
if (ipmuxid < 0) |
| 1437 |
return 1; |
| 1438 |
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 1439 |
if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) { |
| 1440 |
error("Couldn't mark interface down (get): %m"); |
| 1441 |
return 0; |
| 1442 |
} |
| 1443 |
ifr.ifr_flags &= ~IFF_UP; |
| 1444 |
if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) { |
| 1445 |
error("Couldn't mark interface down (set): %m"); |
| 1446 |
return 0; |
| 1447 |
} |
| 1448 |
if_is_up = 0; |
| 1449 |
return 1; |
| 1450 |
} |
| 1451 |
|
| 1452 |
/* |
| 1453 |
* sifnpmode - Set the mode for handling packets for a given NP. |
| 1454 |
*/ |
| 1455 |
int |
| 1456 |
sifnpmode(int u, int proto, enum NPmode mode) |
| 1457 |
{ |
| 1458 |
int npi[2]; |
| 1459 |
|
| 1460 |
npi[0] = proto; |
| 1461 |
npi[1] = (int) mode; |
| 1462 |
if (strioctl(pppfd, PPPIO_NPMODE, &npi, 2 * sizeof(int), 0) < 0) { |
| 1463 |
error("ioctl(set NP %d mode to %d): %m", proto, mode); |
| 1464 |
return 0; |
| 1465 |
} |
| 1466 |
return 1; |
| 1467 |
} |
| 1468 |
|
| 1469 |
/* |
| 1470 |
* sif6up - Config the IPv6 interface up and enable IPv6 packets to pass. |
| 1471 |
*/ |
| 1472 |
int |
| 1473 |
sif6up(int u) |
| 1474 |
{ |
| 1475 |
struct lifreq lifr; |
| 1476 |
int fd; |
| 1477 |
|
| 1478 |
fd = socket(AF_INET6, SOCK_DGRAM, 0); |
| 1479 |
if (fd < 0) { |
| 1480 |
return 0; |
| 1481 |
} |
| 1482 |
|
| 1483 |
memset(&lifr, 0, sizeof(lifr)); |
| 1484 |
strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); |
| 1485 |
if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) { |
| 1486 |
close(fd); |
| 1487 |
return 0; |
| 1488 |
} |
| 1489 |
|
| 1490 |
lifr.lifr_flags |= IFF_UP; |
| 1491 |
strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); |
| 1492 |
if (ioctl(fd, SIOCSLIFFLAGS, &lifr) < 0) { |
| 1493 |
close(fd); |
| 1494 |
return 0; |
| 1495 |
} |
| 1496 |
|
| 1497 |
if6_is_up = 1; |
| 1498 |
close(fd); |
| 1499 |
return 1; |
| 1500 |
} |
| 1501 |
|
| 1502 |
/* |
| 1503 |
* sifdown - Config the IPv6 interface down and disable IPv6. |
| 1504 |
*/ |
| 1505 |
int |
| 1506 |
sif6down(int u) |
| 1507 |
{ |
| 1508 |
struct lifreq lifr; |
| 1509 |
int fd; |
| 1510 |
|
| 1511 |
fd = socket(AF_INET6, SOCK_DGRAM, 0); |
| 1512 |
if (fd < 0) |
| 1513 |
return 0; |
| 1514 |
|
| 1515 |
memset(&lifr, 0, sizeof(lifr)); |
| 1516 |
strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); |
| 1517 |
if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) { |
| 1518 |
close(fd); |
| 1519 |
return 0; |
| 1520 |
} |
| 1521 |
|
| 1522 |
lifr.lifr_flags &= ~IFF_UP; |
| 1523 |
strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); |
| 1524 |
if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) { |
| 1525 |
close(fd); |
| 1526 |
return 0; |
| 1527 |
} |
| 1528 |
|
| 1529 |
if6_is_up = 0; |
| 1530 |
close(fd); |
| 1531 |
return 1; |
| 1532 |
} |
| 1533 |
|
| 1534 |
/* |
| 1535 |
* sif6addr - Config the interface with an IPv6 link-local address |
| 1536 |
*/ |
| 1537 |
int |
| 1538 |
sif6addr(int u, eui64_t o, eui64_t h) |
| 1539 |
{ |
| 1540 |
struct lifreq lifr; |
| 1541 |
struct sockaddr_storage laddr; |
| 1542 |
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&laddr; |
| 1543 |
int fd; |
| 1544 |
|
| 1545 |
fd = socket(AF_INET6, SOCK_DGRAM, 0); |
| 1546 |
if (fd < 0) |
| 1547 |
return 0; |
| 1548 |
|
| 1549 |
memset(&lifr, 0, sizeof(lifr)); |
| 1550 |
strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); |
| 1551 |
|
| 1552 |
/* |
| 1553 |
* Do this because /dev/ppp responds to DL_PHYS_ADDR_REQ with |
| 1554 |
* zero values, hence the interface token came to be zero too, |
| 1555 |
* and without this, in.ndpd will complain |
| 1556 |
*/ |
| 1557 |
IN6_LLTOKEN_FROM_EUI64(lifr, sin6, o); |
| 1558 |
if (ioctl(fd, SIOCSLIFTOKEN, &lifr) < 0) { |
| 1559 |
close(fd); |
| 1560 |
return 0; |
| 1561 |
} |
| 1562 |
|
| 1563 |
/* |
| 1564 |
* Set the interface address and destination address |
| 1565 |
*/ |
| 1566 |
IN6_LLADDR_FROM_EUI64(lifr, sin6, o); |
| 1567 |
if (ioctl(fd, SIOCSLIFADDR, &lifr) < 0) { |
| 1568 |
close(fd); |
| 1569 |
return 0; |
| 1570 |
} |
| 1571 |
|
| 1572 |
memset(&lifr, 0, sizeof(lifr)); |
| 1573 |
strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); |
| 1574 |
IN6_LLADDR_FROM_EUI64(lifr, sin6, h); |
| 1575 |
if (ioctl(fd, SIOCSLIFDSTADDR, &lifr) < 0) { |
| 1576 |
close(fd); |
| 1577 |
return 0; |
| 1578 |
} |
| 1579 |
|
| 1580 |
return 1; |
| 1581 |
} |
| 1582 |
|
| 1583 |
/* |
| 1584 |
* cif6addr - Remove the IPv6 address from interface |
| 1585 |
*/ |
| 1586 |
int |
| 1587 |
cif6addr(int u, eui64_t o, eui64_t h) |
| 1588 |
{ |
| 1589 |
return 1; |
| 1590 |
} |
| 1591 |
|
| 1592 |
/* |
| 1593 |
* sif6defaultroute - assign a default route through the address given. |
| 1594 |
*/ |
| 1595 |
int |
| 1596 |
sif6defaultroute(int u, eui64_t l, eui64_t g) |
| 1597 |
{ |
| 1598 |
struct { |
| 1599 |
struct rt_msghdr rtm; |
| 1600 |
struct sockaddr_in6 dst; |
| 1601 |
struct sockaddr_in6 gw; |
| 1602 |
} rmsg; |
| 1603 |
static int seq; |
| 1604 |
int rtsock; |
| 1605 |
|
| 1606 |
#if defined(__USLC__) |
| 1607 |
g = l; /* use the local address as gateway */ |
| 1608 |
#endif |
| 1609 |
memset(&rmsg, 0, sizeof(rmsg)); |
| 1610 |
|
| 1611 |
rmsg.rtm.rtm_msglen = sizeof (rmsg); |
| 1612 |
rmsg.rtm.rtm_version = RTM_VERSION; |
| 1613 |
rmsg.rtm.rtm_type = RTM_ADD; |
| 1614 |
rmsg.rtm.rtm_flags = RTF_GATEWAY; |
| 1615 |
rmsg.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; |
| 1616 |
rmsg.rtm.rtm_pid = getpid(); |
| 1617 |
rmsg.rtm.rtm_seq = seq++; |
| 1618 |
|
| 1619 |
rmsg.dst.sin6_family = AF_INET6; |
| 1620 |
|
| 1621 |
rmsg.gw.sin6_family = AF_INET6; |
| 1622 |
IN6_SOCKADDR_FROM_EUI64(&rmsg.gw, g); |
| 1623 |
|
| 1624 |
rtsock = socket(PF_ROUTE, SOCK_RAW, 0); |
| 1625 |
|
| 1626 |
if (rtsock < 0) { |
| 1627 |
error("Can't add default route: %m"); |
| 1628 |
return 0; |
| 1629 |
} |
| 1630 |
|
| 1631 |
if (write(rtsock, &rmsg, sizeof(rmsg)) < 0) |
| 1632 |
error("Can't add default route: %m"); |
| 1633 |
|
| 1634 |
close(rtsock); |
| 1635 |
|
| 1636 |
default_route_gateway6 = g; |
| 1637 |
return 1; |
| 1638 |
} |
| 1639 |
|
| 1640 |
/* |
| 1641 |
* cif6defaultroute - delete a default route through the address given. |
| 1642 |
*/ |
| 1643 |
int |
| 1644 |
cif6defaultroute(int u, eui64_t l, eui64_t g) |
| 1645 |
{ |
| 1646 |
/* No need to do this on Solaris; the kernel deletes the |
| 1647 |
route when the interface goes down. */ |
| 1648 |
memset(&default_route_gateway6, 0, sizeof(default_route_gateway6)); |
| 1649 |
return 1; |
| 1650 |
} |
| 1651 |
|
| 1652 |
#define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr) |
| 1653 |
|
| 1654 |
/* |
| 1655 |
* sifaddr - Config the interface IP addresses and netmask. |
| 1656 |
*/ |
| 1657 |
int |
| 1658 |
sifaddr(int u, u_int32_t o, u_int32_t h, u_int32_t m) |
| 1659 |
{ |
| 1660 |
struct ifreq ifr; |
| 1661 |
int ret = 1; |
| 1662 |
|
| 1663 |
memset(&ifr, 0, sizeof(ifr)); |
| 1664 |
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 1665 |
ifr.ifr_addr.sa_family = AF_INET; |
| 1666 |
INET_ADDR(ifr.ifr_addr) = m; |
| 1667 |
if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) { |
| 1668 |
error("Couldn't set IP netmask: %m"); |
| 1669 |
ret = 0; |
| 1670 |
} |
| 1671 |
ifr.ifr_addr.sa_family = AF_INET; |
| 1672 |
INET_ADDR(ifr.ifr_addr) = o; |
| 1673 |
if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) { |
| 1674 |
error("Couldn't set local IP address: %m"); |
| 1675 |
ret = 0; |
| 1676 |
} |
| 1677 |
|
| 1678 |
/* |
| 1679 |
* On some systems, we have to explicitly set the point-to-point |
| 1680 |
* flag bit before we can set a destination address. |
| 1681 |
*/ |
| 1682 |
if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) >= 0 |
| 1683 |
&& (ifr.ifr_flags & IFF_POINTOPOINT) == 0) { |
| 1684 |
ifr.ifr_flags |= IFF_POINTOPOINT; |
| 1685 |
if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) { |
| 1686 |
error("Couldn't mark interface pt-to-pt: %m"); |
| 1687 |
ret = 0; |
| 1688 |
} |
| 1689 |
} |
| 1690 |
ifr.ifr_dstaddr.sa_family = AF_INET; |
| 1691 |
INET_ADDR(ifr.ifr_dstaddr) = h; |
| 1692 |
if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) { |
| 1693 |
error("Couldn't set remote IP address: %m"); |
| 1694 |
ret = 0; |
| 1695 |
} |
| 1696 |
|
| 1697 |
remote_addr = h; |
| 1698 |
return ret; |
| 1699 |
} |
| 1700 |
|
| 1701 |
/* |
| 1702 |
* cifaddr - Clear the interface IP addresses, and delete routes |
| 1703 |
* through the interface if possible. |
| 1704 |
*/ |
| 1705 |
int |
| 1706 |
cifaddr(int u, u_int32_t o, u_int32_t h) |
| 1707 |
{ |
| 1708 |
#if defined(__USLC__) /* was: #if 0 */ |
| 1709 |
cifroute(unit, ouraddr, hisaddr); |
| 1710 |
if (ipmuxid >= 0) { |
| 1711 |
notice("Removing ppp interface unit"); |
| 1712 |
if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) { |
| 1713 |
error("Can't remove ppp interface unit: %m"); |
| 1714 |
return 0; |
| 1715 |
} |
| 1716 |
ipmuxid = -1; |
| 1717 |
} |
| 1718 |
#endif |
| 1719 |
remote_addr = 0; |
| 1720 |
return 1; |
| 1721 |
} |
| 1722 |
|
| 1723 |
/* |
| 1724 |
* sifdefaultroute - assign a default route through the address given. |
| 1725 |
*/ |
| 1726 |
int |
| 1727 |
sifdefaultroute(int u, u_int32_t l, u_int32_t g, bool replace) |
| 1728 |
{ |
| 1729 |
struct rtentry rt; |
| 1730 |
|
| 1731 |
if (replace) { |
| 1732 |
error("Replacing the default route is not implemented on Solaris yet"); |
| 1733 |
return 0; |
| 1734 |
} |
| 1735 |
|
| 1736 |
#if defined(__USLC__) |
| 1737 |
g = l; /* use the local address as gateway */ |
| 1738 |
#endif |
| 1739 |
memset(&rt, 0, sizeof(rt)); |
| 1740 |
rt.rt_dst.sa_family = AF_INET; |
| 1741 |
INET_ADDR(rt.rt_dst) = 0; |
| 1742 |
rt.rt_gateway.sa_family = AF_INET; |
| 1743 |
INET_ADDR(rt.rt_gateway) = g; |
| 1744 |
rt.rt_flags = RTF_GATEWAY; |
| 1745 |
|
| 1746 |
if (ioctl(ipfd, SIOCADDRT, &rt) < 0) { |
| 1747 |
error("Can't add default route: %m"); |
| 1748 |
return 0; |
| 1749 |
} |
| 1750 |
|
| 1751 |
default_route_gateway = g; |
| 1752 |
return 1; |
| 1753 |
} |
| 1754 |
|
| 1755 |
/* |
| 1756 |
* cifdefaultroute - delete a default route through the address given. |
| 1757 |
*/ |
| 1758 |
int |
| 1759 |
cifdefaultroute(int u, u_int32_t l, u_int32_t g) |
| 1760 |
{ |
| 1761 |
struct rtentry rt; |
| 1762 |
|
| 1763 |
#if defined(__USLC__) |
| 1764 |
g = l; /* use the local address as gateway */ |
| 1765 |
#endif |
| 1766 |
memset(&rt, 0, sizeof(rt)); |
| 1767 |
rt.rt_dst.sa_family = AF_INET; |
| 1768 |
INET_ADDR(rt.rt_dst) = 0; |
| 1769 |
rt.rt_gateway.sa_family = AF_INET; |
| 1770 |
INET_ADDR(rt.rt_gateway) = g; |
| 1771 |
rt.rt_flags = RTF_GATEWAY; |
| 1772 |
|
| 1773 |
if (ioctl(ipfd, SIOCDELRT, &rt) < 0) { |
| 1774 |
error("Can't delete default route: %m"); |
| 1775 |
return 0; |
| 1776 |
} |
| 1777 |
|
| 1778 |
default_route_gateway = 0; |
| 1779 |
return 1; |
| 1780 |
} |
| 1781 |
|
| 1782 |
/* |
| 1783 |
* sifproxyarp - Make a proxy ARP entry for the peer. |
| 1784 |
*/ |
| 1785 |
int |
| 1786 |
sifproxyarp(int unit, u_int32_t hisaddr) |
| 1787 |
{ |
| 1788 |
struct arpreq arpreq; |
| 1789 |
|
| 1790 |
memset(&arpreq, 0, sizeof(arpreq)); |
| 1791 |
if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) |
| 1792 |
return 0; |
| 1793 |
|
| 1794 |
arpreq.arp_pa.sa_family = AF_INET; |
| 1795 |
INET_ADDR(arpreq.arp_pa) = hisaddr; |
| 1796 |
arpreq.arp_flags = ATF_PERM | ATF_PUBL; |
| 1797 |
if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) { |
| 1798 |
error("Couldn't set proxy ARP entry: %m"); |
| 1799 |
return 0; |
| 1800 |
} |
| 1801 |
|
| 1802 |
proxy_arp_addr = hisaddr; |
| 1803 |
return 1; |
| 1804 |
} |
| 1805 |
|
| 1806 |
/* |
| 1807 |
* cifproxyarp - Delete the proxy ARP entry for the peer. |
| 1808 |
*/ |
| 1809 |
int |
| 1810 |
cifproxyarp(int unit, u_int32_t hisaddr) |
| 1811 |
{ |
| 1812 |
struct arpreq arpreq; |
| 1813 |
|
| 1814 |
memset(&arpreq, 0, sizeof(arpreq)); |
| 1815 |
arpreq.arp_pa.sa_family = AF_INET; |
| 1816 |
INET_ADDR(arpreq.arp_pa) = hisaddr; |
| 1817 |
if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) { |
| 1818 |
error("Couldn't delete proxy ARP entry: %m"); |
| 1819 |
return 0; |
| 1820 |
} |
| 1821 |
|
| 1822 |
proxy_arp_addr = 0; |
| 1823 |
return 1; |
| 1824 |
} |
| 1825 |
|
| 1826 |
/* |
| 1827 |
* get_ether_addr - get the hardware address of an interface on the |
| 1828 |
* the same subnet as ipaddr. |
| 1829 |
*/ |
| 1830 |
#define MAX_IFS 32 |
| 1831 |
|
| 1832 |
static int |
| 1833 |
get_ether_addr(u_int32_t ipaddr, struct sockaddr *hwaddr) |
| 1834 |
{ |
| 1835 |
struct ifreq *ifr, *ifend, ifreq; |
| 1836 |
int nif; |
| 1837 |
struct ifconf ifc; |
| 1838 |
u_int32_t ina, mask; |
| 1839 |
|
| 1840 |
/* |
| 1841 |
* Scan through the system's network interfaces. |
| 1842 |
*/ |
| 1843 |
if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0) |
| 1844 |
nif = MAX_IFS; |
| 1845 |
ifc.ifc_len = nif * sizeof(struct ifreq); |
| 1846 |
ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len); |
| 1847 |
if (ifc.ifc_buf == 0) |
| 1848 |
return 0; |
| 1849 |
if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) { |
| 1850 |
warn("Couldn't get system interface list: %m"); |
| 1851 |
free(ifc.ifc_buf); |
| 1852 |
return 0; |
| 1853 |
} |
| 1854 |
ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); |
| 1855 |
for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) { |
| 1856 |
if (ifr->ifr_addr.sa_family != AF_INET) |
| 1857 |
continue; |
| 1858 |
/* |
| 1859 |
* Check that the interface is up, and not point-to-point or loopback. |
| 1860 |
*/ |
| 1861 |
strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); |
| 1862 |
if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0) |
| 1863 |
continue; |
| 1864 |
if ((ifreq.ifr_flags & |
| 1865 |
(IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP)) |
| 1866 |
!= (IFF_UP|IFF_BROADCAST)) |
| 1867 |
continue; |
| 1868 |
/* |
| 1869 |
* Get its netmask and check that it's on the right subnet. |
| 1870 |
*/ |
| 1871 |
if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0) |
| 1872 |
continue; |
| 1873 |
ina = INET_ADDR(ifr->ifr_addr); |
| 1874 |
mask = INET_ADDR(ifreq.ifr_addr); |
| 1875 |
if ((ipaddr & mask) == (ina & mask)) |
| 1876 |
break; |
| 1877 |
} |
| 1878 |
|
| 1879 |
if (ifr >= ifend) { |
| 1880 |
warn("No suitable interface found for proxy ARP"); |
| 1881 |
free(ifc.ifc_buf); |
| 1882 |
return 0; |
| 1883 |
} |
| 1884 |
|
| 1885 |
info("found interface %s for proxy ARP", ifr->ifr_name); |
| 1886 |
if (!get_hw_addr(ifr->ifr_name, ina, hwaddr)) { |
| 1887 |
error("Couldn't get hardware address for %s", ifr->ifr_name); |
| 1888 |
free(ifc.ifc_buf); |
| 1889 |
return 0; |
| 1890 |
} |
| 1891 |
|
| 1892 |
free(ifc.ifc_buf); |
| 1893 |
return 1; |
| 1894 |
} |
| 1895 |
|
| 1896 |
/* |
| 1897 |
* get_hw_addr_dlpi - obtain the hardware address using DLPI |
| 1898 |
*/ |
| 1899 |
static int |
| 1900 |
get_hw_addr_dlpi(char *name, struct sockaddr *hwaddr) |
| 1901 |
{ |
| 1902 |
char *q; |
| 1903 |
int unit, iffd, adrlen; |
| 1904 |
unsigned char *adrp; |
| 1905 |
char ifdev[24]; |
| 1906 |
struct { |
| 1907 |
union DL_primitives prim; |
| 1908 |
char space[64]; |
| 1909 |
} reply; |
| 1910 |
|
| 1911 |
/* |
| 1912 |
* We have to open the device and ask it for its hardware address. |
| 1913 |
* First split apart the device name and unit. |
| 1914 |
*/ |
| 1915 |
slprintf(ifdev, sizeof(ifdev), "/dev/%s", name); |
| 1916 |
for (q = ifdev + strlen(ifdev); --q >= ifdev; ) |
| 1917 |
if (!isdigit(*q)) |
| 1918 |
break; |
| 1919 |
unit = atoi(q+1); |
| 1920 |
q[1] = 0; |
| 1921 |
|
| 1922 |
/* |
| 1923 |
* Open the device and do a DLPI attach and phys_addr_req. |
| 1924 |
*/ |
| 1925 |
iffd = open(ifdev, O_RDWR); |
| 1926 |
if (iffd < 0) { |
| 1927 |
error("Can't open %s: %m", ifdev); |
| 1928 |
return 0; |
| 1929 |
} |
| 1930 |
if (dlpi_attach(iffd, unit) < 0 |
| 1931 |
|| dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0 |
| 1932 |
|| dlpi_info_req(iffd) < 0 |
| 1933 |
|| dlpi_get_reply(iffd, &reply.prim, DL_INFO_ACK, sizeof(reply)) < 0) { |
| 1934 |
close(iffd); |
| 1935 |
return 0; |
| 1936 |
} |
| 1937 |
|
| 1938 |
adrlen = reply.prim.info_ack.dl_addr_length; |
| 1939 |
adrp = (unsigned char *)&reply + reply.prim.info_ack.dl_addr_offset; |
| 1940 |
|
| 1941 |
if (reply.prim.info_ack.dl_sap_length < 0) |
| 1942 |
adrlen += reply.prim.info_ack.dl_sap_length; |
| 1943 |
else |
| 1944 |
adrp += reply.prim.info_ack.dl_sap_length; |
| 1945 |
|
| 1946 |
hwaddr->sa_family = AF_UNSPEC; |
| 1947 |
memcpy(hwaddr->sa_data, adrp, adrlen); |
| 1948 |
|
| 1949 |
return 1; |
| 1950 |
} |
| 1951 |
/* |
| 1952 |
* get_hw_addr - obtain the hardware address for a named interface. |
| 1953 |
*/ |
| 1954 |
static int |
| 1955 |
get_hw_addr(char *name, u_int32_t ina, struct sockaddr *hwaddr) |
| 1956 |
{ |
| 1957 |
/* New way - get the address by doing an arp request. */ |
| 1958 |
int s; |
| 1959 |
struct arpreq req; |
| 1960 |
|
| 1961 |
s = socket(AF_INET, SOCK_DGRAM, 0); |
| 1962 |
if (s < 0) |
| 1963 |
return 0; |
| 1964 |
memset(&req, 0, sizeof(req)); |
| 1965 |
req.arp_pa.sa_family = AF_INET; |
| 1966 |
INET_ADDR(req.arp_pa) = ina; |
| 1967 |
if (ioctl(s, SIOCGARP, &req) < 0) { |
| 1968 |
error("Couldn't get ARP entry for %s: %m", ip_ntoa(ina)); |
| 1969 |
return 0; |
| 1970 |
} |
| 1971 |
*hwaddr = req.arp_ha; |
| 1972 |
hwaddr->sa_family = AF_UNSPEC; |
| 1973 |
|
| 1974 |
return 1; |
| 1975 |
} |
| 1976 |
|
| 1977 |
static int |
| 1978 |
dlpi_attach(int fd, int ppa) |
| 1979 |
{ |
| 1980 |
dl_attach_req_t req; |
| 1981 |
struct strbuf buf; |
| 1982 |
|
| 1983 |
req.dl_primitive = DL_ATTACH_REQ; |
| 1984 |
req.dl_ppa = ppa; |
| 1985 |
buf.len = sizeof(req); |
| 1986 |
buf.buf = (void *) &req; |
| 1987 |
return putmsg(fd, &buf, NULL, RS_HIPRI); |
| 1988 |
} |
| 1989 |
|
| 1990 |
static int |
| 1991 |
dlpi_info_req(int fd) |
| 1992 |
{ |
| 1993 |
dl_info_req_t req; |
| 1994 |
struct strbuf buf; |
| 1995 |
|
| 1996 |
req.dl_primitive = DL_INFO_REQ; |
| 1997 |
buf.len = sizeof(req); |
| 1998 |
buf.buf = (void *) &req; |
| 1999 |
return putmsg(fd, &buf, NULL, RS_HIPRI); |
| 2000 |
} |
| 2001 |
|
| 2002 |
static int |
| 2003 |
dlpi_get_reply(int fd, union DL_primitives *reply, int expected_prim, size_t maxlen) |
| 2004 |
{ |
| 2005 |
struct strbuf buf; |
| 2006 |
int flags, n; |
| 2007 |
struct pollfd pfd; |
| 2008 |
|
| 2009 |
/* |
| 2010 |
* Use poll to wait for a message with a timeout. |
| 2011 |
*/ |
| 2012 |
pfd.fd = fd; |
| 2013 |
pfd.events = POLLIN | POLLPRI; |
| 2014 |
do { |
| 2015 |
n = poll(&pfd, 1, 1000); |
| 2016 |
} while (n == -1 && errno == EINTR && !ppp_signaled(SIGTERM)); |
| 2017 |
if (n <= 0) |
| 2018 |
return -1; |
| 2019 |
|
| 2020 |
/* |
| 2021 |
* Get the reply. |
| 2022 |
*/ |
| 2023 |
buf.maxlen = maxlen; |
| 2024 |
buf.buf = (void *) reply; |
| 2025 |
flags = 0; |
| 2026 |
if (getmsg(fd, &buf, NULL, &flags) < 0) |
| 2027 |
return -1; |
| 2028 |
|
| 2029 |
if (buf.len < sizeof(ulong)) { |
| 2030 |
if (debug) |
| 2031 |
dbglog("dlpi response short (len=%d)\n", buf.len); |
| 2032 |
return -1; |
| 2033 |
} |
| 2034 |
|
| 2035 |
if (reply->dl_primitive == expected_prim) |
| 2036 |
return 0; |
| 2037 |
|
| 2038 |
if (debug) { |
| 2039 |
if (reply->dl_primitive == DL_ERROR_ACK) { |
| 2040 |
dbglog("dlpi error %d (unix errno %d) for prim %x\n", |
| 2041 |
reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno, |
| 2042 |
reply->error_ack.dl_error_primitive); |
| 2043 |
} else { |
| 2044 |
dbglog("dlpi unexpected response prim %x\n", |
| 2045 |
reply->dl_primitive); |
| 2046 |
} |
| 2047 |
} |
| 2048 |
|
| 2049 |
return -1; |
| 2050 |
} |
| 2051 |
|
| 2052 |
/* |
| 2053 |
* Return user specified netmask, modified by any mask we might determine |
| 2054 |
* for address `addr' (in network byte order). |
| 2055 |
* Here we scan through the system's list of interfaces, looking for |
| 2056 |
* any non-point-to-point interfaces which might appear to be on the same |
| 2057 |
* network as `addr'. If we find any, we OR in their netmask to the |
| 2058 |
* user-specified netmask. |
| 2059 |
*/ |
| 2060 |
u_int32_t |
| 2061 |
GetMask(u_int32_t addr) |
| 2062 |
{ |
| 2063 |
u_int32_t mask, nmask, ina; |
| 2064 |
struct ifreq *ifr, *ifend, ifreq; |
| 2065 |
int nif; |
| 2066 |
struct ifconf ifc; |
| 2067 |
|
| 2068 |
addr = ntohl(addr); |
| 2069 |
if (IN_CLASSA(addr)) /* determine network mask for address class */ |
| 2070 |
nmask = IN_CLASSA_NET; |
| 2071 |
else if (IN_CLASSB(addr)) |
| 2072 |
nmask = IN_CLASSB_NET; |
| 2073 |
else |
| 2074 |
nmask = IN_CLASSC_NET; |
| 2075 |
/* class D nets are disallowed by bad_ip_adrs */ |
| 2076 |
mask = netmask | htonl(nmask); |
| 2077 |
|
| 2078 |
/* |
| 2079 |
* Scan through the system's network interfaces. |
| 2080 |
*/ |
| 2081 |
#ifdef SIOCGIFNUM |
| 2082 |
if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0) |
| 2083 |
#endif |
| 2084 |
nif = MAX_IFS; |
| 2085 |
ifc.ifc_len = nif * sizeof(struct ifreq); |
| 2086 |
ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len); |
| 2087 |
if (ifc.ifc_buf == 0) |
| 2088 |
return mask; |
| 2089 |
if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) { |
| 2090 |
warn("Couldn't get system interface list: %m"); |
| 2091 |
free(ifc.ifc_buf); |
| 2092 |
return mask; |
| 2093 |
} |
| 2094 |
ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); |
| 2095 |
for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) { |
| 2096 |
/* |
| 2097 |
* Check the interface's internet address. |
| 2098 |
*/ |
| 2099 |
if (ifr->ifr_addr.sa_family != AF_INET) |
| 2100 |
continue; |
| 2101 |
ina = INET_ADDR(ifr->ifr_addr); |
| 2102 |
if ((ntohl(ina) & nmask) != (addr & nmask)) |
| 2103 |
continue; |
| 2104 |
/* |
| 2105 |
* Check that the interface is up, and not point-to-point or loopback. |
| 2106 |
*/ |
| 2107 |
strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); |
| 2108 |
if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0) |
| 2109 |
continue; |
| 2110 |
if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK)) |
| 2111 |
!= IFF_UP) |
| 2112 |
continue; |
| 2113 |
/* |
| 2114 |
* Get its netmask and OR it into our mask. |
| 2115 |
*/ |
| 2116 |
if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0) |
| 2117 |
continue; |
| 2118 |
mask |= INET_ADDR(ifreq.ifr_addr); |
| 2119 |
} |
| 2120 |
|
| 2121 |
free(ifc.ifc_buf); |
| 2122 |
return mask; |
| 2123 |
} |
| 2124 |
|
| 2125 |
/* |
| 2126 |
* logwtmp - write an accounting record to the /var/adm/wtmp file. |
| 2127 |
*/ |
| 2128 |
void |
| 2129 |
logwtmp(const char *line, const char *name, const char *host) |
| 2130 |
{ |
| 2131 |
static struct utmpx utmpx; |
| 2132 |
|
| 2133 |
if (name[0] != 0) { |
| 2134 |
/* logging in */ |
| 2135 |
strncpy(utmpx.ut_user, name, sizeof(utmpx.ut_user)); |
| 2136 |
strncpy(utmpx.ut_line, line, sizeof(utmpx.ut_line)); |
| 2137 |
strncpy(utmpx.ut_host, host, sizeof(utmpx.ut_host)); |
| 2138 |
if (*host != '\0') { |
| 2139 |
utmpx.ut_syslen = strlen(host) + 1; |
| 2140 |
if (utmpx.ut_syslen > sizeof(utmpx.ut_host)) |
| 2141 |
utmpx.ut_syslen = sizeof(utmpx.ut_host); |
| 2142 |
} |
| 2143 |
utmpx.ut_pid = getpid(); |
| 2144 |
utmpx.ut_type = USER_PROCESS; |
| 2145 |
} else { |
| 2146 |
utmpx.ut_type = DEAD_PROCESS; |
| 2147 |
} |
| 2148 |
gettimeofday(&utmpx.ut_tv, NULL); |
| 2149 |
updwtmpx("/var/adm/wtmpx", &utmpx); |
| 2150 |
} |
| 2151 |
|
| 2152 |
/* |
| 2153 |
* get_host_seed - return the serial number of this machine. |
| 2154 |
*/ |
| 2155 |
int |
| 2156 |
get_host_seed(void) |
| 2157 |
{ |
| 2158 |
char buf[32]; |
| 2159 |
|
| 2160 |
if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) { |
| 2161 |
error("sysinfo: %m"); |
| 2162 |
return 0; |
| 2163 |
} |
| 2164 |
return (int) strtoul(buf, NULL, 16); |
| 2165 |
} |
| 2166 |
|
| 2167 |
static int |
| 2168 |
strioctl(int fd, int cmd, void *ptr, int ilen, int olen) |
| 2169 |
{ |
| 2170 |
struct strioctl str; |
| 2171 |
|
| 2172 |
str.ic_cmd = cmd; |
| 2173 |
str.ic_timout = 0; |
| 2174 |
str.ic_len = ilen; |
| 2175 |
str.ic_dp = ptr; |
| 2176 |
if (ioctl(fd, I_STR, &str) == -1) |
| 2177 |
return -1; |
| 2178 |
if (str.ic_len != olen) |
| 2179 |
dbglog("strioctl: expected %d bytes, got %d for cmd %x\n", |
| 2180 |
olen, str.ic_len, cmd); |
| 2181 |
return 0; |
| 2182 |
} |
| 2183 |
|
| 2184 |
/* |
| 2185 |
* cifroute - delete a route through the addresses given. |
| 2186 |
*/ |
| 2187 |
int |
| 2188 |
cifroute(int u, u_int32_t our, u_int32_t his) |
| 2189 |
{ |
| 2190 |
struct rtentry rt; |
| 2191 |
|
| 2192 |
memset(&rt, 0, sizeof(rt)); |
| 2193 |
rt.rt_dst.sa_family = AF_INET; |
| 2194 |
INET_ADDR(rt.rt_dst) = his; |
| 2195 |
rt.rt_gateway.sa_family = AF_INET; |
| 2196 |
INET_ADDR(rt.rt_gateway) = our; |
| 2197 |
rt.rt_flags = RTF_HOST; |
| 2198 |
|
| 2199 |
if (ioctl(ipfd, SIOCDELRT, &rt) < 0) { |
| 2200 |
error("Can't delete route: %m"); |
| 2201 |
return 0; |
| 2202 |
} |
| 2203 |
|
| 2204 |
return 1; |
| 2205 |
} |
| 2206 |
|
| 2207 |
/* |
| 2208 |
* have_route_to - determine if the system has a route to the specified |
| 2209 |
* IP address. Returns 0 if not, 1 if so, -1 if we can't tell. |
| 2210 |
* `addr' is in network byte order. |
| 2211 |
* For demand mode to work properly, we have to ignore routes |
| 2212 |
* through our own interface. |
| 2213 |
*/ |
| 2214 |
#ifndef T_CURRENT /* needed for Solaris 2.5 */ |
| 2215 |
#define T_CURRENT MI_T_CURRENT |
| 2216 |
#endif |
| 2217 |
|
| 2218 |
int |
| 2219 |
have_route_to(u_int32_t addr) |
| 2220 |
{ |
| 2221 |
int fd, r, flags, i; |
| 2222 |
struct { |
| 2223 |
struct T_optmgmt_req req; |
| 2224 |
struct opthdr hdr; |
| 2225 |
} req; |
| 2226 |
union { |
| 2227 |
struct T_optmgmt_ack ack; |
| 2228 |
unsigned char space[64]; |
| 2229 |
} ack; |
| 2230 |
struct opthdr *rh; |
| 2231 |
struct strbuf cbuf, dbuf; |
| 2232 |
int nroutes; |
| 2233 |
mib2_ipRouteEntry_t routes[8]; |
| 2234 |
mib2_ipRouteEntry_t *rp; |
| 2235 |
|
| 2236 |
fd = open(mux_dev_name, O_RDWR); |
| 2237 |
if (fd < 0) { |
| 2238 |
warn("have_route_to: couldn't open %s: %m", mux_dev_name); |
| 2239 |
return -1; |
| 2240 |
} |
| 2241 |
|
| 2242 |
req.req.PRIM_type = T_OPTMGMT_REQ; |
| 2243 |
req.req.OPT_offset = (char *) &req.hdr - (char *) &req; |
| 2244 |
req.req.OPT_length = sizeof(req.hdr); |
| 2245 |
req.req.MGMT_flags = T_CURRENT; |
| 2246 |
|
| 2247 |
req.hdr.level = MIB2_IP; |
| 2248 |
req.hdr.name = 0; |
| 2249 |
req.hdr.len = 0; |
| 2250 |
|
| 2251 |
cbuf.buf = (char *) &req; |
| 2252 |
cbuf.len = sizeof(req); |
| 2253 |
|
| 2254 |
if (putmsg(fd, &cbuf, NULL, 0) == -1) { |
| 2255 |
warn("have_route_to: putmsg: %m"); |
| 2256 |
close(fd); |
| 2257 |
return -1; |
| 2258 |
} |
| 2259 |
|
| 2260 |
for (;;) { |
| 2261 |
cbuf.buf = (char *) &ack; |
| 2262 |
cbuf.maxlen = sizeof(ack); |
| 2263 |
dbuf.buf = (char *) routes; |
| 2264 |
dbuf.maxlen = sizeof(routes); |
| 2265 |
flags = 0; |
| 2266 |
r = getmsg(fd, &cbuf, &dbuf, &flags); |
| 2267 |
if (r == -1) { |
| 2268 |
warn("have_route_to: getmsg: %m"); |
| 2269 |
close(fd); |
| 2270 |
return -1; |
| 2271 |
} |
| 2272 |
|
| 2273 |
if (cbuf.len < sizeof(struct T_optmgmt_ack) |
| 2274 |
|| ack.ack.PRIM_type != T_OPTMGMT_ACK |
| 2275 |
|| ack.ack.MGMT_flags != T_SUCCESS |
| 2276 |
|| ack.ack.OPT_length < sizeof(struct opthdr)) { |
| 2277 |
dbglog("have_route_to: bad message len=%d prim=%d", |
| 2278 |
cbuf.len, ack.ack.PRIM_type); |
| 2279 |
close(fd); |
| 2280 |
return -1; |
| 2281 |
} |
| 2282 |
|
| 2283 |
rh = (struct opthdr *) ((char *)&ack + ack.ack.OPT_offset); |
| 2284 |
if (rh->level == 0 && rh->name == 0) |
| 2285 |
break; |
| 2286 |
if (rh->level != MIB2_IP || rh->name != MIB2_IP_21) { |
| 2287 |
while (r == MOREDATA) |
| 2288 |
r = getmsg(fd, NULL, &dbuf, &flags); |
| 2289 |
continue; |
| 2290 |
} |
| 2291 |
|
| 2292 |
for (;;) { |
| 2293 |
nroutes = dbuf.len / sizeof(mib2_ipRouteEntry_t); |
| 2294 |
for (rp = routes, i = 0; i < nroutes; ++i, ++rp) { |
| 2295 |
if (rp->ipRouteMask != ~0) { |
| 2296 |
dbglog("have_route_to: dest=%I gw=%I mask=%I", |
| 2297 |
rp->ipRouteDest, rp->ipRouteNextHop, |
| 2298 |
rp->ipRouteMask); |
| 2299 |
if (((addr ^ rp->ipRouteDest) & rp->ipRouteMask) == 0 |
| 2300 |
&& rp->ipRouteNextHop != remote_addr) |
| 2301 |
return 1; |
| 2302 |
} |
| 2303 |
} |
| 2304 |
if (r == 0) |
| 2305 |
break; |
| 2306 |
r = getmsg(fd, NULL, &dbuf, &flags); |
| 2307 |
} |
| 2308 |
} |
| 2309 |
close(fd); |
| 2310 |
return 0; |
| 2311 |
} |
| 2312 |
|
| 2313 |
/* |
| 2314 |
* get_pty - get a pty master/slave pair and chown the slave side to |
| 2315 |
* the uid given. Assumes slave_name points to MAXPATHLEN bytes of space. |
| 2316 |
*/ |
| 2317 |
int |
| 2318 |
get_pty(int *master_fdp, int *slave_fdp, char *slave_name, int uid) |
| 2319 |
{ |
| 2320 |
int mfd, sfd; |
| 2321 |
char *pty_name; |
| 2322 |
|
| 2323 |
mfd = open("/dev/ptmx", O_RDWR); |
| 2324 |
if (mfd < 0) { |
| 2325 |
error("Couldn't open pty master: %m"); |
| 2326 |
return 0; |
| 2327 |
} |
| 2328 |
|
| 2329 |
pty_name = ptsname(mfd); |
| 2330 |
if (pty_name == NULL) { |
| 2331 |
error("Couldn't get name of pty slave"); |
| 2332 |
close(mfd); |
| 2333 |
return 0; |
| 2334 |
} |
| 2335 |
if (chown(pty_name, uid, -1) < 0) |
| 2336 |
warn("Couldn't change owner of pty slave: %m"); |
| 2337 |
if (chmod(pty_name, S_IRUSR | S_IWUSR) < 0) |
| 2338 |
warn("Couldn't change permissions on pty slave: %m"); |
| 2339 |
if (unlockpt(mfd) < 0) |
| 2340 |
warn("Couldn't unlock pty slave: %m"); |
| 2341 |
|
| 2342 |
sfd = open(pty_name, O_RDWR); |
| 2343 |
if (sfd < 0) { |
| 2344 |
error("Couldn't open pty slave %s: %m", pty_name); |
| 2345 |
close(mfd); |
| 2346 |
return 0; |
| 2347 |
} |
| 2348 |
if (ioctl(sfd, I_PUSH, "ptem") < 0) |
| 2349 |
warn("Couldn't push ptem module on pty slave: %m"); |
| 2350 |
|
| 2351 |
dbglog("Using %s", pty_name); |
| 2352 |
strlcpy(slave_name, pty_name, MAXPATHLEN); |
| 2353 |
*master_fdp = mfd; |
| 2354 |
*slave_fdp = sfd; |
| 2355 |
|
| 2356 |
return 1; |
| 2357 |
} |
| 2358 |
|
| 2359 |
/******************************************************************** |
| 2360 |
* |
| 2361 |
* get_time - Get current time, monotonic if possible. |
| 2362 |
*/ |
| 2363 |
int |
| 2364 |
ppp_get_time(struct timeval *tv) |
| 2365 |
{ |
| 2366 |
return gettimeofday(tv, NULL); |
| 2367 |
} |