sys-solaris.c 65 KB

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