sys-linux.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940
  1. /*
  2. * sys-linux.c - System-dependent procedures for setting up
  3. * PPP interfaces on Linux systems
  4. *
  5. * Copyright (c) 1994-2004 Paul Mackerras. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. The name(s) of the authors of this software must not be used to
  15. * endorse or promote products derived from this software without
  16. * prior written permission.
  17. *
  18. * 3. Redistributions of any form whatsoever must retain the following
  19. * acknowledgment:
  20. * "This product includes software developed by Paul Mackerras
  21. * <paulus@samba.org>".
  22. *
  23. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  24. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  25. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  26. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  27. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  28. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  29. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  30. *
  31. * Derived from main.c and pppd.h, which are:
  32. *
  33. * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. *
  39. * 1. Redistributions of source code must retain the above copyright
  40. * notice, this list of conditions and the following disclaimer.
  41. *
  42. * 2. Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. *
  47. * 3. The name "Carnegie Mellon University" must not be used to
  48. * endorse or promote products derived from this software without
  49. * prior written permission. For permission or any legal
  50. * details, please contact
  51. * Office of Technology Transfer
  52. * Carnegie Mellon University
  53. * 5000 Forbes Avenue
  54. * Pittsburgh, PA 15213-3890
  55. * (412) 268-4387, fax: (412) 268-7395
  56. * tech-transfer@andrew.cmu.edu
  57. *
  58. * 4. Redistributions of any form whatsoever must retain the following
  59. * acknowledgment:
  60. * "This product includes software developed by Computing Services
  61. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  62. *
  63. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  64. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  65. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  66. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  67. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  68. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  69. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  70. */
  71. #include <sys/ioctl.h>
  72. #include <sys/types.h>
  73. #include <sys/socket.h>
  74. #include <sys/time.h>
  75. #include <sys/errno.h>
  76. #include <sys/file.h>
  77. #include <sys/stat.h>
  78. #include <sys/utsname.h>
  79. #include <sys/sysmacros.h>
  80. #include <stdio.h>
  81. #include <stdlib.h>
  82. #include <syslog.h>
  83. #include <string.h>
  84. #include <time.h>
  85. #include <memory.h>
  86. #include <utmp.h>
  87. #include <mntent.h>
  88. #include <signal.h>
  89. #include <fcntl.h>
  90. #include <ctype.h>
  91. #include <termios.h>
  92. #include <unistd.h>
  93. /* This is in netdevice.h. However, this compile will fail miserably if
  94. you attempt to include netdevice.h because it has so many references
  95. to __memcpy functions which it should not attempt to do. So, since I
  96. really don't use it, but it must be defined, define it now. */
  97. #ifndef MAX_ADDR_LEN
  98. #define MAX_ADDR_LEN 7
  99. #endif
  100. #if __GLIBC__ >= 2
  101. #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
  102. #include <net/if.h>
  103. #include <net/if_arp.h>
  104. #include <net/route.h>
  105. #include <netinet/if_ether.h>
  106. #else
  107. #include <linux/types.h>
  108. #include <linux/if.h>
  109. #include <linux/if_arp.h>
  110. #include <linux/route.h>
  111. #include <linux/if_ether.h>
  112. #endif
  113. #include <netinet/in.h>
  114. #include <arpa/inet.h>
  115. #include <linux/ppp_defs.h>
  116. #include <linux/if_ppp.h>
  117. #include "pppd.h"
  118. #include "fsm.h"
  119. #include "ipcp.h"
  120. #ifdef IPX_CHANGE
  121. #include "ipxcp.h"
  122. #if __GLIBC__ >= 2 && \
  123. !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
  124. #include <netipx/ipx.h>
  125. #else
  126. #include <linux/ipx.h>
  127. #endif
  128. #endif /* IPX_CHANGE */
  129. #ifdef PPP_FILTER
  130. #include <pcap-bpf.h>
  131. #include <linux/filter.h>
  132. #endif /* PPP_FILTER */
  133. #ifdef LOCKLIB
  134. #include <sys/locks.h>
  135. #endif
  136. #ifdef INET6
  137. #ifndef _LINUX_IN6_H
  138. /*
  139. * This is in linux/include/net/ipv6.h.
  140. */
  141. struct in6_ifreq {
  142. struct in6_addr ifr6_addr;
  143. __u32 ifr6_prefixlen;
  144. unsigned int ifr6_ifindex;
  145. };
  146. #endif
  147. #define IN6_LLADDR_FROM_EUI64(sin6, eui64) do { \
  148. memset(&sin6.s6_addr, 0, sizeof(struct in6_addr)); \
  149. sin6.s6_addr16[0] = htons(0xfe80); \
  150. eui64_copy(eui64, sin6.s6_addr32[2]); \
  151. } while (0)
  152. #endif /* INET6 */
  153. /* We can get an EIO error on an ioctl if the modem has hung up */
  154. #define ok_error(num) ((num)==EIO)
  155. static int tty_disc = N_TTY; /* The TTY discipline */
  156. static int ppp_disc = N_PPP; /* The PPP discpline */
  157. static int initfdflags = -1; /* Initial file descriptor flags for fd */
  158. static int ppp_fd = -1; /* fd which is set to PPP discipline */
  159. static int sock_fd = -1; /* socket for doing interface ioctls */
  160. static int slave_fd = -1; /* pty for old-style demand mode, slave */
  161. static int master_fd = -1; /* pty for old-style demand mode, master */
  162. #ifdef INET6
  163. static int sock6_fd = -1;
  164. #endif /* INET6 */
  165. /*
  166. * For the old-style kernel driver, this is the same as ppp_fd.
  167. * For the new-style driver, it is the fd of an instance of /dev/ppp
  168. * which is attached to the ppp unit and is used for controlling it.
  169. */
  170. int ppp_dev_fd = -1; /* fd for /dev/ppp (new style driver) */
  171. static int chindex; /* channel index (new style driver) */
  172. static fd_set in_fds; /* set of fds that wait_input waits for */
  173. static int max_in_fd; /* highest fd set in in_fds */
  174. static int has_proxy_arp = 0;
  175. static int driver_version = 0;
  176. static int driver_modification = 0;
  177. static int driver_patch = 0;
  178. static int driver_is_old = 0;
  179. static int restore_term = 0; /* 1 => we've munged the terminal */
  180. static struct termios inittermios; /* Initial TTY termios */
  181. int new_style_driver = 0;
  182. static char loop_name[20];
  183. static unsigned char inbuf[512]; /* buffer for chars read from loopback */
  184. static int if_is_up; /* Interface has been marked up */
  185. static int if6_is_up; /* Interface has been marked up for IPv6, to help differentiate */
  186. static int have_default_route; /* Gateway for default route added */
  187. static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
  188. static char proxy_arp_dev[16]; /* Device for proxy arp entry */
  189. static u_int32_t our_old_addr; /* for detecting address changes */
  190. static int dynaddr_set; /* 1 if ip_dynaddr set */
  191. static int looped; /* 1 if using loop */
  192. static int link_mtu; /* mtu for the link (not bundle) */
  193. static struct utsname utsname; /* for the kernel version */
  194. static int kernel_version;
  195. #define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))
  196. #define MAX_IFS 100
  197. #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
  198. #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
  199. IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
  200. #define SIN_ADDR(x) (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr)
  201. /* Prototypes for procedures local to this file. */
  202. static int modify_flags(int fd, int clear_bits, int set_bits);
  203. static int translate_speed (int bps);
  204. static int baud_rate_of (int speed);
  205. static void close_route_table (void);
  206. static int open_route_table (void);
  207. static int read_route_table (struct rtentry *rt);
  208. static int defaultroute_exists (struct rtentry *rt);
  209. static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
  210. char *name, int namelen);
  211. static void decode_version (char *buf, int *version, int *mod, int *patch);
  212. static int set_kdebugflag(int level);
  213. static int ppp_registered(void);
  214. static int make_ppp_unit(void);
  215. static int setifstate (int u, int state);
  216. extern u_char inpacket_buf[]; /* borrowed from main.c */
  217. /*
  218. * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
  219. * if it exists.
  220. */
  221. #define SET_SA_FAMILY(addr, family) \
  222. memset ((char *) &(addr), '\0', sizeof(addr)); \
  223. addr.sa_family = (family);
  224. /*
  225. * Determine if the PPP connection should still be present.
  226. */
  227. extern int hungup;
  228. /* new_fd is the fd of a tty */
  229. static void set_ppp_fd (int new_fd)
  230. {
  231. ppp_fd = new_fd;
  232. if (!new_style_driver)
  233. ppp_dev_fd = new_fd;
  234. }
  235. static int still_ppp(void)
  236. {
  237. if (new_style_driver)
  238. return !hungup && ppp_fd >= 0;
  239. if (!hungup || ppp_fd == slave_fd)
  240. return 1;
  241. if (slave_fd >= 0) {
  242. set_ppp_fd(slave_fd);
  243. return 1;
  244. }
  245. return 0;
  246. }
  247. /*
  248. * modify_flags - set and clear flag bits controlling the kernel
  249. * PPP driver.
  250. */
  251. static int modify_flags(int fd, int clear_bits, int set_bits)
  252. {
  253. int flags;
  254. if (ioctl(fd, PPPIOCGFLAGS, &flags) == -1)
  255. goto err;
  256. flags = (flags & ~clear_bits) | set_bits;
  257. if (ioctl(fd, PPPIOCSFLAGS, &flags) == -1)
  258. goto err;
  259. return 0;
  260. err:
  261. if (errno != EIO)
  262. error("Failed to set PPP kernel option flags: %m");
  263. return -1;
  264. }
  265. /********************************************************************
  266. *
  267. * sys_init - System-dependent initialization.
  268. */
  269. void sys_init(void)
  270. {
  271. /* Get an internet socket for doing socket ioctls. */
  272. sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  273. if (sock_fd < 0)
  274. fatal("Couldn't create IP socket: %m(%d)", errno);
  275. #ifdef INET6
  276. sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0);
  277. if (sock6_fd < 0)
  278. sock6_fd = -errno; /* save errno for later */
  279. #endif
  280. FD_ZERO(&in_fds);
  281. max_in_fd = 0;
  282. }
  283. /********************************************************************
  284. *
  285. * sys_cleanup - restore any system state we modified before exiting:
  286. * mark the interface down, delete default route and/or proxy arp entry.
  287. * This shouldn't call die() because it's called from die().
  288. */
  289. void sys_cleanup(void)
  290. {
  291. /*
  292. * Take down the device
  293. */
  294. if (if_is_up) {
  295. if_is_up = 0;
  296. sifdown(0);
  297. }
  298. if (if6_is_up)
  299. sif6down(0);
  300. /*
  301. * Delete any routes through the device.
  302. */
  303. if (have_default_route)
  304. cifdefaultroute(0, 0, 0);
  305. if (has_proxy_arp)
  306. cifproxyarp(0, proxy_arp_addr);
  307. }
  308. /********************************************************************
  309. *
  310. * sys_close - Clean up in a child process before execing.
  311. */
  312. void
  313. sys_close(void)
  314. {
  315. if (new_style_driver && ppp_dev_fd >= 0)
  316. close(ppp_dev_fd);
  317. if (sock_fd >= 0)
  318. close(sock_fd);
  319. #ifdef INET6
  320. if (sock6_fd >= 0)
  321. close(sock6_fd);
  322. #endif
  323. if (slave_fd >= 0)
  324. close(slave_fd);
  325. if (master_fd >= 0)
  326. close(master_fd);
  327. }
  328. /********************************************************************
  329. *
  330. * set_kdebugflag - Define the debugging level for the kernel
  331. */
  332. static int set_kdebugflag (int requested_level)
  333. {
  334. if (ppp_dev_fd < 0)
  335. return 1;
  336. if (ioctl(ppp_dev_fd, PPPIOCSDEBUG, &requested_level) < 0) {
  337. if ( ! ok_error (errno) )
  338. error("ioctl(PPPIOCSDEBUG): %m (line %d)", __LINE__);
  339. return (0);
  340. }
  341. return (1);
  342. }
  343. /********************************************************************
  344. *
  345. * tty_establish_ppp - Turn the serial port into a ppp interface.
  346. */
  347. int tty_establish_ppp (int tty_fd)
  348. {
  349. int ret_fd;
  350. /*
  351. * Ensure that the tty device is in exclusive mode.
  352. */
  353. if (ioctl(tty_fd, TIOCEXCL, 0) < 0) {
  354. if ( ! ok_error ( errno ))
  355. warn("Couldn't make tty exclusive: %m");
  356. }
  357. /*
  358. * Demand mode - prime the old ppp device to relinquish the unit.
  359. */
  360. if (!new_style_driver && looped
  361. && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0) {
  362. error("ioctl(transfer ppp unit): %m, line %d", __LINE__);
  363. return -1;
  364. }
  365. /*
  366. * Set the current tty to the PPP discpline
  367. */
  368. #ifndef N_SYNC_PPP
  369. #define N_SYNC_PPP 14
  370. #endif
  371. ppp_disc = (new_style_driver && sync_serial)? N_SYNC_PPP: N_PPP;
  372. if (ioctl(tty_fd, TIOCSETD, &ppp_disc) < 0) {
  373. if ( ! ok_error (errno) ) {
  374. error("Couldn't set tty to PPP discipline: %m");
  375. return -1;
  376. }
  377. }
  378. ret_fd = generic_establish_ppp(tty_fd);
  379. #define SC_RCVB (SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP)
  380. #define SC_LOGB (SC_DEBUG | SC_LOG_INPKT | SC_LOG_OUTPKT | SC_LOG_RAWIN \
  381. | SC_LOG_FLUSH)
  382. if (ret_fd >= 0) {
  383. modify_flags(ppp_fd, SC_RCVB | SC_LOGB,
  384. (kdebugflag * SC_DEBUG) & SC_LOGB);
  385. } else {
  386. if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0 && !ok_error(errno))
  387. warn("Couldn't reset tty to normal line discipline: %m");
  388. }
  389. return ret_fd;
  390. }
  391. /********************************************************************
  392. *
  393. * generic_establish_ppp - Turn the fd into a ppp interface.
  394. */
  395. int generic_establish_ppp (int fd)
  396. {
  397. int x;
  398. if (new_style_driver) {
  399. int flags;
  400. /* Open an instance of /dev/ppp and connect the channel to it */
  401. if (ioctl(fd, PPPIOCGCHAN, &chindex) == -1) {
  402. error("Couldn't get channel number: %m");
  403. goto err;
  404. }
  405. dbglog("using channel %d", chindex);
  406. fd = open("/dev/ppp", O_RDWR);
  407. if (fd < 0) {
  408. error("Couldn't reopen /dev/ppp: %m");
  409. goto err;
  410. }
  411. (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
  412. if (ioctl(fd, PPPIOCATTCHAN, &chindex) < 0) {
  413. error("Couldn't attach to channel %d: %m", chindex);
  414. goto err_close;
  415. }
  416. flags = fcntl(fd, F_GETFL);
  417. if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
  418. warn("Couldn't set /dev/ppp (channel) to nonblock: %m");
  419. set_ppp_fd(fd);
  420. if (!looped)
  421. ifunit = -1;
  422. if (!looped && !multilink) {
  423. /*
  424. * Create a new PPP unit.
  425. */
  426. if (make_ppp_unit() < 0)
  427. goto err_close;
  428. }
  429. if (looped)
  430. modify_flags(ppp_dev_fd, SC_LOOP_TRAFFIC, 0);
  431. if (!multilink) {
  432. add_fd(ppp_dev_fd);
  433. if (ioctl(fd, PPPIOCCONNECT, &ifunit) < 0) {
  434. error("Couldn't attach to PPP unit %d: %m", ifunit);
  435. goto err_close;
  436. }
  437. }
  438. } else {
  439. /*
  440. * Old-style driver: find out which interface we were given.
  441. */
  442. set_ppp_fd (fd);
  443. if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {
  444. if (ok_error (errno))
  445. goto err;
  446. fatal("ioctl(PPPIOCGUNIT): %m (line %d)", __LINE__);
  447. }
  448. /* Check that we got the same unit again. */
  449. if (looped && x != ifunit)
  450. fatal("transfer_ppp failed: wanted unit %d, got %d", ifunit, x);
  451. ifunit = x;
  452. /*
  453. * Fetch the initial file flags and reset blocking mode on the file.
  454. */
  455. initfdflags = fcntl(fd, F_GETFL);
  456. if (initfdflags == -1 ||
  457. fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
  458. if ( ! ok_error (errno))
  459. warn("Couldn't set device to non-blocking mode: %m");
  460. }
  461. }
  462. /*
  463. * Enable debug in the driver if requested.
  464. */
  465. if (!looped)
  466. set_kdebugflag (kdebugflag);
  467. looped = 0;
  468. return ppp_fd;
  469. err_close:
  470. close(fd);
  471. err:
  472. return -1;
  473. }
  474. /********************************************************************
  475. *
  476. * tty_disestablish_ppp - Restore the serial port to normal operation.
  477. * This shouldn't call die() because it's called from die().
  478. */
  479. void tty_disestablish_ppp(int tty_fd)
  480. {
  481. if (!hungup) {
  482. /*
  483. * Flush the tty output buffer so that the TIOCSETD doesn't hang.
  484. */
  485. if (tcflush(tty_fd, TCIOFLUSH) < 0)
  486. {
  487. warn("tcflush failed: %m");
  488. goto flushfailed;
  489. }
  490. /*
  491. * Restore the previous line discipline
  492. */
  493. if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0) {
  494. if ( ! ok_error (errno))
  495. error("ioctl(TIOCSETD, N_TTY): %m (line %d)", __LINE__);
  496. }
  497. if (ioctl(tty_fd, TIOCNXCL, 0) < 0) {
  498. if ( ! ok_error (errno))
  499. warn("ioctl(TIOCNXCL): %m (line %d)", __LINE__);
  500. }
  501. /* Reset non-blocking mode on fd. */
  502. if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0) {
  503. if ( ! ok_error (errno))
  504. warn("Couldn't restore device fd flags: %m");
  505. }
  506. }
  507. flushfailed:
  508. initfdflags = -1;
  509. generic_disestablish_ppp(tty_fd);
  510. }
  511. /********************************************************************
  512. *
  513. * generic_disestablish_ppp - Restore device components to normal
  514. * operation, and reconnect the ppp unit to the loopback if in demand
  515. * mode. This shouldn't call die() because it's called from die().
  516. */
  517. void generic_disestablish_ppp(int dev_fd)
  518. {
  519. if (new_style_driver) {
  520. close(ppp_fd);
  521. ppp_fd = -1;
  522. if (demand) {
  523. modify_flags(ppp_dev_fd, 0, SC_LOOP_TRAFFIC);
  524. looped = 1;
  525. } else if (!doing_multilink && ppp_dev_fd >= 0) {
  526. close(ppp_dev_fd);
  527. remove_fd(ppp_dev_fd);
  528. ppp_dev_fd = -1;
  529. }
  530. } else {
  531. /* old-style driver */
  532. if (demand)
  533. set_ppp_fd(slave_fd);
  534. else
  535. ppp_dev_fd = -1;
  536. }
  537. }
  538. /*
  539. * make_ppp_unit - make a new ppp unit for ppp_dev_fd.
  540. * Assumes new_style_driver.
  541. */
  542. static int make_ppp_unit()
  543. {
  544. int x, flags;
  545. if (ppp_dev_fd >= 0) {
  546. dbglog("in make_ppp_unit, already had /dev/ppp open?");
  547. close(ppp_dev_fd);
  548. }
  549. ppp_dev_fd = open("/dev/ppp", O_RDWR);
  550. if (ppp_dev_fd < 0)
  551. fatal("Couldn't open /dev/ppp: %m");
  552. flags = fcntl(ppp_dev_fd, F_GETFL);
  553. if (flags == -1
  554. || fcntl(ppp_dev_fd, F_SETFL, flags | O_NONBLOCK) == -1)
  555. warn("Couldn't set /dev/ppp to nonblock: %m");
  556. ifunit = req_unit;
  557. x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
  558. if (x < 0 && req_unit >= 0 && errno == EEXIST) {
  559. warn("Couldn't allocate PPP unit %d as it is already in use", req_unit);
  560. ifunit = -1;
  561. x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
  562. }
  563. if (x < 0)
  564. error("Couldn't create new ppp unit: %m");
  565. return x;
  566. }
  567. /*
  568. * cfg_bundle - configure the existing bundle.
  569. * Used in demand mode.
  570. */
  571. void cfg_bundle(int mrru, int mtru, int rssn, int tssn)
  572. {
  573. if (!new_style_driver)
  574. return;
  575. /* set the mrru, mtu and flags */
  576. if (ioctl(ppp_dev_fd, PPPIOCSMRRU, &mrru) < 0)
  577. error("Couldn't set MRRU: %m");
  578. modify_flags(ppp_dev_fd, SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ|SC_MULTILINK,
  579. ((rssn? SC_MP_SHORTSEQ: 0) | (tssn? SC_MP_XSHORTSEQ: 0)
  580. | (mrru? SC_MULTILINK: 0)));
  581. /* connect up the channel */
  582. if (ioctl(ppp_fd, PPPIOCCONNECT, &ifunit) < 0)
  583. fatal("Couldn't attach to PPP unit %d: %m", ifunit);
  584. add_fd(ppp_dev_fd);
  585. }
  586. /*
  587. * make_new_bundle - create a new PPP unit (i.e. a bundle)
  588. * and connect our channel to it. This should only get called
  589. * if `multilink' was set at the time establish_ppp was called.
  590. * In demand mode this uses our existing bundle instead of making
  591. * a new one.
  592. */
  593. void make_new_bundle(int mrru, int mtru, int rssn, int tssn)
  594. {
  595. if (!new_style_driver)
  596. return;
  597. /* make us a ppp unit */
  598. if (make_ppp_unit() < 0)
  599. die(1);
  600. /* set the mrru and flags */
  601. cfg_bundle(mrru, mtru, rssn, tssn);
  602. }
  603. /*
  604. * bundle_attach - attach our link to a given PPP unit.
  605. * We assume the unit is controlled by another pppd.
  606. */
  607. int bundle_attach(int ifnum)
  608. {
  609. int master_fd;
  610. if (!new_style_driver)
  611. return -1;
  612. master_fd = open("/dev/ppp", O_RDWR);
  613. if (master_fd < 0)
  614. fatal("Couldn't open /dev/ppp: %m");
  615. if (ioctl(master_fd, PPPIOCATTACH, &ifnum) < 0) {
  616. if (errno == ENXIO) {
  617. close(master_fd);
  618. return 0; /* doesn't still exist */
  619. }
  620. fatal("Couldn't attach to interface unit %d: %m\n", ifnum);
  621. }
  622. if (ioctl(ppp_fd, PPPIOCCONNECT, &ifnum) < 0)
  623. fatal("Couldn't connect to interface unit %d: %m", ifnum);
  624. modify_flags(master_fd, 0, SC_MULTILINK);
  625. close(master_fd);
  626. ifunit = ifnum;
  627. return 1;
  628. }
  629. /*
  630. * destroy_bundle - tell the driver to destroy our bundle.
  631. */
  632. void destroy_bundle(void)
  633. {
  634. if (ppp_dev_fd >= 0) {
  635. close(ppp_dev_fd);
  636. remove_fd(ppp_dev_fd);
  637. ppp_dev_fd = -1;
  638. }
  639. }
  640. /********************************************************************
  641. *
  642. * clean_check - Fetch the flags for the device and generate
  643. * appropriate error messages.
  644. */
  645. void clean_check(void)
  646. {
  647. int x;
  648. char *s;
  649. if (still_ppp()) {
  650. if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
  651. s = NULL;
  652. switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
  653. case SC_RCV_B7_0:
  654. s = "all had bit 7 set to 1";
  655. break;
  656. case SC_RCV_B7_1:
  657. s = "all had bit 7 set to 0";
  658. break;
  659. case SC_RCV_EVNP:
  660. s = "all had odd parity";
  661. break;
  662. case SC_RCV_ODDP:
  663. s = "all had even parity";
  664. break;
  665. }
  666. if (s != NULL) {
  667. warn("Receive serial link is not 8-bit clean:");
  668. warn("Problem: %s", s);
  669. }
  670. }
  671. }
  672. }
  673. /*
  674. * List of valid speeds.
  675. */
  676. struct speed {
  677. int speed_int, speed_val;
  678. } speeds[] = {
  679. #ifdef B50
  680. { 50, B50 },
  681. #endif
  682. #ifdef B75
  683. { 75, B75 },
  684. #endif
  685. #ifdef B110
  686. { 110, B110 },
  687. #endif
  688. #ifdef B134
  689. { 134, B134 },
  690. #endif
  691. #ifdef B150
  692. { 150, B150 },
  693. #endif
  694. #ifdef B200
  695. { 200, B200 },
  696. #endif
  697. #ifdef B300
  698. { 300, B300 },
  699. #endif
  700. #ifdef B600
  701. { 600, B600 },
  702. #endif
  703. #ifdef B1200
  704. { 1200, B1200 },
  705. #endif
  706. #ifdef B1800
  707. { 1800, B1800 },
  708. #endif
  709. #ifdef B2000
  710. { 2000, B2000 },
  711. #endif
  712. #ifdef B2400
  713. { 2400, B2400 },
  714. #endif
  715. #ifdef B3600
  716. { 3600, B3600 },
  717. #endif
  718. #ifdef B4800
  719. { 4800, B4800 },
  720. #endif
  721. #ifdef B7200
  722. { 7200, B7200 },
  723. #endif
  724. #ifdef B9600
  725. { 9600, B9600 },
  726. #endif
  727. #ifdef B19200
  728. { 19200, B19200 },
  729. #endif
  730. #ifdef B38400
  731. { 38400, B38400 },
  732. #endif
  733. #ifdef B57600
  734. { 57600, B57600 },
  735. #endif
  736. #ifdef B76800
  737. { 76800, B76800 },
  738. #endif
  739. #ifdef B115200
  740. { 115200, B115200 },
  741. #endif
  742. #ifdef EXTA
  743. { 19200, EXTA },
  744. #endif
  745. #ifdef EXTB
  746. { 38400, EXTB },
  747. #endif
  748. #ifdef B230400
  749. { 230400, B230400 },
  750. #endif
  751. #ifdef B460800
  752. { 460800, B460800 },
  753. #endif
  754. #ifdef B921600
  755. { 921600, B921600 },
  756. #endif
  757. #ifdef B1000000
  758. { 1000000, B1000000 },
  759. #endif
  760. #ifdef B1152000
  761. { 1152000, B1152000 },
  762. #endif
  763. #ifdef B1500000
  764. { 1500000, B1500000 },
  765. #endif
  766. #ifdef B2000000
  767. { 2000000, B2000000 },
  768. #endif
  769. #ifdef B2500000
  770. { 2500000, B2500000 },
  771. #endif
  772. #ifdef B3000000
  773. { 3000000, B3000000 },
  774. #endif
  775. #ifdef B3500000
  776. { 3500000, B3500000 },
  777. #endif
  778. #ifdef B4000000
  779. { 4000000, B4000000 },
  780. #endif
  781. { 0, 0 }
  782. };
  783. /********************************************************************
  784. *
  785. * Translate from bits/second to a speed_t.
  786. */
  787. static int translate_speed (int bps)
  788. {
  789. struct speed *speedp;
  790. if (bps != 0) {
  791. for (speedp = speeds; speedp->speed_int; speedp++) {
  792. if (bps == speedp->speed_int)
  793. return speedp->speed_val;
  794. }
  795. warn("speed %d not supported", bps);
  796. }
  797. return 0;
  798. }
  799. /********************************************************************
  800. *
  801. * Translate from a speed_t to bits/second.
  802. */
  803. static int baud_rate_of (int speed)
  804. {
  805. struct speed *speedp;
  806. if (speed != 0) {
  807. for (speedp = speeds; speedp->speed_int; speedp++) {
  808. if (speed == speedp->speed_val)
  809. return speedp->speed_int;
  810. }
  811. }
  812. return 0;
  813. }
  814. /********************************************************************
  815. *
  816. * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
  817. * at the requested speed, etc. If `local' is true, set CLOCAL
  818. * regardless of whether the modem option was specified.
  819. */
  820. void set_up_tty(int tty_fd, int local)
  821. {
  822. int speed;
  823. struct termios tios;
  824. setdtr(tty_fd, 1);
  825. if (tcgetattr(tty_fd, &tios) < 0) {
  826. if (!ok_error(errno))
  827. fatal("tcgetattr: %m (line %d)", __LINE__);
  828. return;
  829. }
  830. if (!restore_term)
  831. inittermios = tios;
  832. tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
  833. tios.c_cflag |= CS8 | CREAD | HUPCL;
  834. tios.c_iflag = IGNBRK | IGNPAR;
  835. tios.c_oflag = 0;
  836. tios.c_lflag = 0;
  837. tios.c_cc[VMIN] = 1;
  838. tios.c_cc[VTIME] = 0;
  839. if (local || !modem)
  840. tios.c_cflag ^= (CLOCAL | HUPCL);
  841. switch (crtscts) {
  842. case 1:
  843. tios.c_cflag |= CRTSCTS;
  844. break;
  845. case -2:
  846. tios.c_iflag |= IXON | IXOFF;
  847. tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
  848. tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
  849. break;
  850. case -1:
  851. tios.c_cflag &= ~CRTSCTS;
  852. break;
  853. default:
  854. break;
  855. }
  856. if (stop_bits >= 2)
  857. tios.c_cflag |= CSTOPB;
  858. speed = translate_speed(inspeed);
  859. if (speed) {
  860. cfsetospeed (&tios, speed);
  861. cfsetispeed (&tios, speed);
  862. }
  863. /*
  864. * We can't proceed if the serial port speed is B0,
  865. * since that implies that the serial port is disabled.
  866. */
  867. else {
  868. speed = cfgetospeed(&tios);
  869. if (speed == B0)
  870. fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
  871. }
  872. while (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0 && !ok_error(errno))
  873. if (errno != EINTR)
  874. fatal("tcsetattr: %m (line %d)", __LINE__);
  875. baud_rate = baud_rate_of(speed);
  876. restore_term = 1;
  877. }
  878. /********************************************************************
  879. *
  880. * setdtr - control the DTR line on the serial port.
  881. * This is called from die(), so it shouldn't call die().
  882. */
  883. void setdtr (int tty_fd, int on)
  884. {
  885. int modembits = TIOCM_DTR;
  886. ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
  887. }
  888. /********************************************************************
  889. *
  890. * restore_tty - restore the terminal to the saved settings.
  891. */
  892. void restore_tty (int tty_fd)
  893. {
  894. if (restore_term) {
  895. restore_term = 0;
  896. /*
  897. * Turn off echoing, because otherwise we can get into
  898. * a loop with the tty and the modem echoing to each other.
  899. * We presume we are the sole user of this tty device, so
  900. * when we close it, it will revert to its defaults anyway.
  901. */
  902. if (!default_device)
  903. inittermios.c_lflag &= ~(ECHO | ECHONL);
  904. if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0) {
  905. if (! ok_error (errno))
  906. warn("tcsetattr: %m (line %d)", __LINE__);
  907. }
  908. }
  909. }
  910. /********************************************************************
  911. *
  912. * output - Output PPP packet.
  913. */
  914. void output (int unit, unsigned char *p, int len)
  915. {
  916. int fd = ppp_fd;
  917. int proto;
  918. dump_packet("sent", p, len);
  919. if (snoop_send_hook) snoop_send_hook(p, len);
  920. if (len < PPP_HDRLEN)
  921. return;
  922. if (new_style_driver) {
  923. p += 2;
  924. len -= 2;
  925. proto = (p[0] << 8) + p[1];
  926. if (ppp_dev_fd >= 0 && !(proto >= 0xc000 || proto == PPP_CCPFRAG))
  927. fd = ppp_dev_fd;
  928. }
  929. if (write(fd, p, len) < 0) {
  930. if (errno == EWOULDBLOCK || errno == EAGAIN || errno == ENOBUFS
  931. || errno == ENXIO || errno == EIO || errno == EINTR)
  932. warn("write: warning: %m (%d)", errno);
  933. else
  934. error("write: %m (%d)", errno);
  935. }
  936. }
  937. /********************************************************************
  938. *
  939. * wait_input - wait until there is data available,
  940. * for the length of time specified by *timo (indefinite
  941. * if timo is NULL).
  942. */
  943. void wait_input(struct timeval *timo)
  944. {
  945. fd_set ready, exc;
  946. int n;
  947. ready = in_fds;
  948. exc = in_fds;
  949. n = select(max_in_fd + 1, &ready, NULL, &exc, timo);
  950. if (n < 0 && errno != EINTR)
  951. fatal("select: %m");
  952. }
  953. /*
  954. * add_fd - add an fd to the set that wait_input waits for.
  955. */
  956. void add_fd(int fd)
  957. {
  958. if (fd >= FD_SETSIZE)
  959. fatal("internal error: file descriptor too large (%d)", fd);
  960. FD_SET(fd, &in_fds);
  961. if (fd > max_in_fd)
  962. max_in_fd = fd;
  963. }
  964. /*
  965. * remove_fd - remove an fd from the set that wait_input waits for.
  966. */
  967. void remove_fd(int fd)
  968. {
  969. FD_CLR(fd, &in_fds);
  970. }
  971. /********************************************************************
  972. *
  973. * read_packet - get a PPP packet from the serial device.
  974. */
  975. int read_packet (unsigned char *buf)
  976. {
  977. int len, nr;
  978. len = PPP_MRU + PPP_HDRLEN;
  979. if (new_style_driver) {
  980. *buf++ = PPP_ALLSTATIONS;
  981. *buf++ = PPP_UI;
  982. len -= 2;
  983. }
  984. nr = -1;
  985. if (ppp_fd >= 0) {
  986. nr = read(ppp_fd, buf, len);
  987. if (nr < 0 && errno != EWOULDBLOCK && errno != EAGAIN
  988. && errno != EIO && errno != EINTR)
  989. error("read: %m");
  990. if (nr < 0 && errno == ENXIO)
  991. return 0;
  992. }
  993. if (nr < 0 && new_style_driver && ppp_dev_fd >= 0 && !bundle_eof) {
  994. /* N.B. we read ppp_fd first since LCP packets come in there. */
  995. nr = read(ppp_dev_fd, buf, len);
  996. if (nr < 0 && errno != EWOULDBLOCK && errno != EAGAIN
  997. && errno != EIO && errno != EINTR)
  998. error("read /dev/ppp: %m");
  999. if (nr < 0 && errno == ENXIO)
  1000. nr = 0;
  1001. if (nr == 0 && doing_multilink) {
  1002. remove_fd(ppp_dev_fd);
  1003. bundle_eof = 1;
  1004. }
  1005. }
  1006. if (new_style_driver && ppp_fd < 0 && ppp_dev_fd < 0)
  1007. nr = 0;
  1008. return (new_style_driver && nr > 0)? nr+2: nr;
  1009. }
  1010. /********************************************************************
  1011. *
  1012. * get_loop_output - get outgoing packets from the ppp device,
  1013. * and detect when we want to bring the real link up.
  1014. * Return value is 1 if we need to bring up the link, 0 otherwise.
  1015. */
  1016. int
  1017. get_loop_output(void)
  1018. {
  1019. int rv = 0;
  1020. int n;
  1021. if (new_style_driver) {
  1022. while ((n = read_packet(inpacket_buf)) > 0)
  1023. if (loop_frame(inpacket_buf, n))
  1024. rv = 1;
  1025. return rv;
  1026. }
  1027. while ((n = read(master_fd, inbuf, sizeof(inbuf))) > 0)
  1028. if (loop_chars(inbuf, n))
  1029. rv = 1;
  1030. if (n == 0)
  1031. fatal("eof on loopback");
  1032. if (errno != EWOULDBLOCK && errno != EAGAIN)
  1033. fatal("read from loopback: %m(%d)", errno);
  1034. return rv;
  1035. }
  1036. /*
  1037. * netif_set_mtu - set the MTU on the PPP network interface.
  1038. */
  1039. void
  1040. netif_set_mtu(int unit, int mtu)
  1041. {
  1042. struct ifreq ifr;
  1043. memset (&ifr, '\0', sizeof (ifr));
  1044. strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
  1045. ifr.ifr_mtu = mtu;
  1046. if (ifunit >= 0 && ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
  1047. error("ioctl(SIOCSIFMTU): %m (line %d)", __LINE__);
  1048. }
  1049. /*
  1050. * netif_get_mtu - get the MTU on the PPP network interface.
  1051. */
  1052. int
  1053. netif_get_mtu(int unit)
  1054. {
  1055. struct ifreq ifr;
  1056. memset (&ifr, '\0', sizeof (ifr));
  1057. strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
  1058. if (ifunit >= 0 && ioctl(sock_fd, SIOCGIFMTU, (caddr_t) &ifr) < 0) {
  1059. error("ioctl(SIOCGIFMTU): %m (line %d)", __LINE__);
  1060. return 0;
  1061. }
  1062. return ifr.ifr_mtu;
  1063. }
  1064. /********************************************************************
  1065. *
  1066. * tty_send_config - configure the transmit characteristics of
  1067. * the ppp interface.
  1068. */
  1069. void tty_send_config(int mtu, u_int32_t asyncmap, int pcomp, int accomp)
  1070. {
  1071. int x;
  1072. if (!still_ppp())
  1073. return;
  1074. link_mtu = mtu;
  1075. if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
  1076. if (errno != EIO && errno != ENOTTY)
  1077. error("Couldn't set transmit async character map: %m");
  1078. ++error_count;
  1079. return;
  1080. }
  1081. x = (pcomp? SC_COMP_PROT: 0) | (accomp? SC_COMP_AC: 0)
  1082. | (sync_serial? SC_SYNC: 0);
  1083. modify_flags(ppp_fd, SC_COMP_PROT|SC_COMP_AC|SC_SYNC, x);
  1084. }
  1085. /********************************************************************
  1086. *
  1087. * tty_set_xaccm - set the extended transmit ACCM for the interface.
  1088. */
  1089. void tty_set_xaccm (ext_accm accm)
  1090. {
  1091. if (!still_ppp())
  1092. return;
  1093. if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY) {
  1094. if ( ! ok_error (errno))
  1095. warn("ioctl(set extended ACCM): %m (line %d)", __LINE__);
  1096. }
  1097. }
  1098. /********************************************************************
  1099. *
  1100. * tty_recv_config - configure the receive-side characteristics of
  1101. * the ppp interface.
  1102. */
  1103. void tty_recv_config(int mru, u_int32_t asyncmap, int pcomp, int accomp)
  1104. {
  1105. /*
  1106. * If we were called because the link has gone down then there is nothing
  1107. * which may be done. Just return without incident.
  1108. */
  1109. if (!still_ppp())
  1110. return;
  1111. /*
  1112. * Set the receiver parameters
  1113. */
  1114. if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
  1115. if (errno != EIO && errno != ENOTTY)
  1116. error("Couldn't set channel receive MRU: %m");
  1117. }
  1118. if (new_style_driver && ppp_dev_fd >= 0
  1119. && ioctl(ppp_dev_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
  1120. error("Couldn't set MRU in generic PPP layer: %m");
  1121. if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
  1122. if (errno != EIO && errno != ENOTTY)
  1123. error("Couldn't set channel receive asyncmap: %m");
  1124. }
  1125. }
  1126. /********************************************************************
  1127. *
  1128. * ccp_test - ask kernel whether a given compression method
  1129. * is acceptable for use.
  1130. */
  1131. int
  1132. ccp_test(int unit, u_char *opt_ptr, int opt_len, int for_transmit)
  1133. {
  1134. struct ppp_option_data data;
  1135. memset (&data, '\0', sizeof (data));
  1136. data.ptr = opt_ptr;
  1137. data.length = opt_len;
  1138. data.transmit = for_transmit;
  1139. if (ioctl(ppp_dev_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
  1140. return 1;
  1141. return (errno == ENOBUFS)? 0: -1;
  1142. }
  1143. /********************************************************************
  1144. *
  1145. * ccp_flags_set - inform kernel about the current state of CCP.
  1146. */
  1147. void ccp_flags_set (int unit, int isopen, int isup)
  1148. {
  1149. int x;
  1150. x = (isopen? SC_CCP_OPEN: 0) | (isup? SC_CCP_UP: 0);
  1151. if (still_ppp() && ppp_dev_fd >= 0)
  1152. modify_flags(ppp_dev_fd, SC_CCP_OPEN|SC_CCP_UP, x);
  1153. }
  1154. #ifdef PPP_FILTER
  1155. /*
  1156. * set_filters - set the active and pass filters in the kernel driver.
  1157. */
  1158. int set_filters(struct bpf_program *pass, struct bpf_program *active)
  1159. {
  1160. struct sock_fprog fp;
  1161. fp.len = pass->bf_len;
  1162. fp.filter = (struct sock_filter *) pass->bf_insns;
  1163. if (ioctl(ppp_dev_fd, PPPIOCSPASS, &fp) < 0) {
  1164. if (errno == ENOTTY)
  1165. warn("kernel does not support PPP filtering");
  1166. else
  1167. error("Couldn't set pass-filter in kernel: %m");
  1168. return 0;
  1169. }
  1170. fp.len = active->bf_len;
  1171. fp.filter = (struct sock_filter *) active->bf_insns;
  1172. if (ioctl(ppp_dev_fd, PPPIOCSACTIVE, &fp) < 0) {
  1173. error("Couldn't set active-filter in kernel: %m");
  1174. return 0;
  1175. }
  1176. return 1;
  1177. }
  1178. #endif /* PPP_FILTER */
  1179. /********************************************************************
  1180. *
  1181. * get_idle_time - return how long the link has been idle.
  1182. */
  1183. int
  1184. get_idle_time(u, ip)
  1185. int u;
  1186. struct ppp_idle *ip;
  1187. {
  1188. return ioctl(ppp_dev_fd, PPPIOCGIDLE, ip) >= 0;
  1189. }
  1190. /********************************************************************
  1191. *
  1192. * get_ppp_stats - return statistics for the link.
  1193. */
  1194. int
  1195. get_ppp_stats(u, stats)
  1196. int u;
  1197. struct pppd_stats *stats;
  1198. {
  1199. struct ifpppstatsreq req;
  1200. memset (&req, 0, sizeof (req));
  1201. req.stats_ptr = (caddr_t) &req.stats;
  1202. strlcpy(req.ifr__name, ifname, sizeof(req.ifr__name));
  1203. if (ioctl(sock_fd, SIOCGPPPSTATS, &req) < 0) {
  1204. error("Couldn't get PPP statistics: %m");
  1205. return 0;
  1206. }
  1207. stats->bytes_in = req.stats.p.ppp_ibytes;
  1208. stats->bytes_out = req.stats.p.ppp_obytes;
  1209. stats->pkts_in = req.stats.p.ppp_ipackets;
  1210. stats->pkts_out = req.stats.p.ppp_opackets;
  1211. return 1;
  1212. }
  1213. /********************************************************************
  1214. *
  1215. * ccp_fatal_error - returns 1 if decompression was disabled as a
  1216. * result of an error detected after decompression of a packet,
  1217. * 0 otherwise. This is necessary because of patent nonsense.
  1218. */
  1219. int ccp_fatal_error (int unit)
  1220. {
  1221. int flags;
  1222. if (ioctl(ppp_dev_fd, PPPIOCGFLAGS, &flags) < 0) {
  1223. error("Couldn't read compression error flags: %m");
  1224. flags = 0;
  1225. }
  1226. return flags & SC_DC_FERROR;
  1227. }
  1228. /********************************************************************
  1229. *
  1230. * path_to_procfs - find the path to the proc file system mount point
  1231. */
  1232. static char proc_path[MAXPATHLEN];
  1233. static int proc_path_len;
  1234. static char *path_to_procfs(const char *tail)
  1235. {
  1236. struct mntent *mntent;
  1237. FILE *fp;
  1238. if (proc_path_len == 0) {
  1239. /* Default the mount location of /proc */
  1240. strlcpy (proc_path, "/proc", sizeof(proc_path));
  1241. proc_path_len = 5;
  1242. fp = fopen(MOUNTED, "r");
  1243. if (fp != NULL) {
  1244. while ((mntent = getmntent(fp)) != NULL) {
  1245. if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
  1246. continue;
  1247. if (strcmp(mntent->mnt_type, "proc") == 0) {
  1248. strlcpy(proc_path, mntent->mnt_dir, sizeof(proc_path));
  1249. proc_path_len = strlen(proc_path);
  1250. break;
  1251. }
  1252. }
  1253. fclose (fp);
  1254. }
  1255. }
  1256. strlcpy(proc_path + proc_path_len, tail,
  1257. sizeof(proc_path) - proc_path_len);
  1258. return proc_path;
  1259. }
  1260. /*
  1261. * /proc/net/route parsing stuff.
  1262. */
  1263. #define ROUTE_MAX_COLS 12
  1264. FILE *route_fd = (FILE *) 0;
  1265. static char route_buffer[512];
  1266. static int route_dev_col, route_dest_col, route_gw_col;
  1267. static int route_flags_col, route_mask_col;
  1268. static int route_num_cols;
  1269. static int open_route_table (void);
  1270. static void close_route_table (void);
  1271. static int read_route_table (struct rtentry *rt);
  1272. /********************************************************************
  1273. *
  1274. * close_route_table - close the interface to the route table
  1275. */
  1276. static void close_route_table (void)
  1277. {
  1278. if (route_fd != (FILE *) 0) {
  1279. fclose (route_fd);
  1280. route_fd = (FILE *) 0;
  1281. }
  1282. }
  1283. /********************************************************************
  1284. *
  1285. * open_route_table - open the interface to the route table
  1286. */
  1287. static char route_delims[] = " \t\n";
  1288. static int open_route_table (void)
  1289. {
  1290. char *path;
  1291. close_route_table();
  1292. path = path_to_procfs("/net/route");
  1293. route_fd = fopen (path, "r");
  1294. if (route_fd == NULL) {
  1295. error("can't open routing table %s: %m", path);
  1296. return 0;
  1297. }
  1298. route_dev_col = 0; /* default to usual columns */
  1299. route_dest_col = 1;
  1300. route_gw_col = 2;
  1301. route_flags_col = 3;
  1302. route_mask_col = 7;
  1303. route_num_cols = 8;
  1304. /* parse header line */
  1305. if (fgets(route_buffer, sizeof(route_buffer), route_fd) != 0) {
  1306. char *p = route_buffer, *q;
  1307. int col;
  1308. for (col = 0; col < ROUTE_MAX_COLS; ++col) {
  1309. int used = 1;
  1310. if ((q = strtok(p, route_delims)) == 0)
  1311. break;
  1312. if (strcasecmp(q, "iface") == 0)
  1313. route_dev_col = col;
  1314. else if (strcasecmp(q, "destination") == 0)
  1315. route_dest_col = col;
  1316. else if (strcasecmp(q, "gateway") == 0)
  1317. route_gw_col = col;
  1318. else if (strcasecmp(q, "flags") == 0)
  1319. route_flags_col = col;
  1320. else if (strcasecmp(q, "mask") == 0)
  1321. route_mask_col = col;
  1322. else
  1323. used = 0;
  1324. if (used && col >= route_num_cols)
  1325. route_num_cols = col + 1;
  1326. p = NULL;
  1327. }
  1328. }
  1329. return 1;
  1330. }
  1331. /********************************************************************
  1332. *
  1333. * read_route_table - read the next entry from the route table
  1334. */
  1335. static int read_route_table(struct rtentry *rt)
  1336. {
  1337. char *cols[ROUTE_MAX_COLS], *p;
  1338. int col;
  1339. memset (rt, '\0', sizeof (struct rtentry));
  1340. if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
  1341. return 0;
  1342. p = route_buffer;
  1343. for (col = 0; col < route_num_cols; ++col) {
  1344. cols[col] = strtok(p, route_delims);
  1345. if (cols[col] == NULL)
  1346. return 0; /* didn't get enough columns */
  1347. p = NULL;
  1348. }
  1349. SIN_ADDR(rt->rt_dst) = strtoul(cols[route_dest_col], NULL, 16);
  1350. SIN_ADDR(rt->rt_gateway) = strtoul(cols[route_gw_col], NULL, 16);
  1351. SIN_ADDR(rt->rt_genmask) = strtoul(cols[route_mask_col], NULL, 16);
  1352. rt->rt_flags = (short) strtoul(cols[route_flags_col], NULL, 16);
  1353. rt->rt_dev = cols[route_dev_col];
  1354. return 1;
  1355. }
  1356. /********************************************************************
  1357. *
  1358. * defaultroute_exists - determine if there is a default route
  1359. */
  1360. static int defaultroute_exists (struct rtentry *rt)
  1361. {
  1362. int result = 0;
  1363. if (!open_route_table())
  1364. return 0;
  1365. while (read_route_table(rt) != 0) {
  1366. if ((rt->rt_flags & RTF_UP) == 0)
  1367. continue;
  1368. if (kernel_version > KVERSION(2,1,0) && SIN_ADDR(rt->rt_genmask) != 0)
  1369. continue;
  1370. if (SIN_ADDR(rt->rt_dst) == 0L) {
  1371. result = 1;
  1372. break;
  1373. }
  1374. }
  1375. close_route_table();
  1376. return result;
  1377. }
  1378. /*
  1379. * have_route_to - determine if the system has any route to
  1380. * a given IP address. `addr' is in network byte order.
  1381. * Return value is 1 if yes, 0 if no, -1 if don't know.
  1382. * For demand mode to work properly, we have to ignore routes
  1383. * through our own interface.
  1384. */
  1385. int have_route_to(u_int32_t addr)
  1386. {
  1387. struct rtentry rt;
  1388. int result = 0;
  1389. if (!open_route_table())
  1390. return -1; /* don't know */
  1391. while (read_route_table(&rt)) {
  1392. if ((rt.rt_flags & RTF_UP) == 0 || strcmp(rt.rt_dev, ifname) == 0)
  1393. continue;
  1394. if ((addr & SIN_ADDR(rt.rt_genmask)) == SIN_ADDR(rt.rt_dst)) {
  1395. result = 1;
  1396. break;
  1397. }
  1398. }
  1399. close_route_table();
  1400. return result;
  1401. }
  1402. /********************************************************************
  1403. *
  1404. * sifdefaultroute - assign a default route through the address given.
  1405. */
  1406. int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
  1407. {
  1408. struct rtentry rt;
  1409. if (defaultroute_exists(&rt) && strcmp(rt.rt_dev, ifname) != 0) {
  1410. if (rt.rt_flags & RTF_GATEWAY)
  1411. error("not replacing existing default route via %I",
  1412. SIN_ADDR(rt.rt_gateway));
  1413. else
  1414. error("not replacing existing default route through %s",
  1415. rt.rt_dev);
  1416. return 0;
  1417. }
  1418. memset (&rt, 0, sizeof (rt));
  1419. SET_SA_FAMILY (rt.rt_dst, AF_INET);
  1420. rt.rt_dev = ifname;
  1421. if (kernel_version > KVERSION(2,1,0)) {
  1422. SET_SA_FAMILY (rt.rt_genmask, AF_INET);
  1423. SIN_ADDR(rt.rt_genmask) = 0L;
  1424. }
  1425. rt.rt_flags = RTF_UP;
  1426. if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
  1427. if ( ! ok_error ( errno ))
  1428. error("default route ioctl(SIOCADDRT): %m");
  1429. return 0;
  1430. }
  1431. have_default_route = 1;
  1432. return 1;
  1433. }
  1434. /********************************************************************
  1435. *
  1436. * cifdefaultroute - delete a default route through the address given.
  1437. */
  1438. int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
  1439. {
  1440. struct rtentry rt;
  1441. have_default_route = 0;
  1442. memset (&rt, '\0', sizeof (rt));
  1443. SET_SA_FAMILY (rt.rt_dst, AF_INET);
  1444. SET_SA_FAMILY (rt.rt_gateway, AF_INET);
  1445. rt.rt_dev = ifname;
  1446. if (kernel_version > KVERSION(2,1,0)) {
  1447. SET_SA_FAMILY (rt.rt_genmask, AF_INET);
  1448. SIN_ADDR(rt.rt_genmask) = 0L;
  1449. }
  1450. rt.rt_flags = RTF_UP;
  1451. if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
  1452. if (still_ppp()) {
  1453. if ( ! ok_error ( errno ))
  1454. error("default route ioctl(SIOCDELRT): %m");
  1455. return 0;
  1456. }
  1457. }
  1458. return 1;
  1459. }
  1460. /********************************************************************
  1461. *
  1462. * sifproxyarp - Make a proxy ARP entry for the peer.
  1463. */
  1464. int sifproxyarp (int unit, u_int32_t his_adr)
  1465. {
  1466. struct arpreq arpreq;
  1467. char *forw_path;
  1468. if (has_proxy_arp == 0) {
  1469. memset (&arpreq, '\0', sizeof(arpreq));
  1470. SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
  1471. SIN_ADDR(arpreq.arp_pa) = his_adr;
  1472. arpreq.arp_flags = ATF_PERM | ATF_PUBL;
  1473. /*
  1474. * Get the hardware address of an interface on the same subnet
  1475. * as our local address.
  1476. */
  1477. if (!get_ether_addr(his_adr, &arpreq.arp_ha, proxy_arp_dev,
  1478. sizeof(proxy_arp_dev))) {
  1479. error("Cannot determine ethernet address for proxy ARP");
  1480. return 0;
  1481. }
  1482. strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
  1483. if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0) {
  1484. if ( ! ok_error ( errno ))
  1485. error("ioctl(SIOCSARP): %m");
  1486. return 0;
  1487. }
  1488. proxy_arp_addr = his_adr;
  1489. has_proxy_arp = 1;
  1490. if (tune_kernel) {
  1491. forw_path = path_to_procfs("/sys/net/ipv4/ip_forward");
  1492. if (forw_path != 0) {
  1493. int fd = open(forw_path, O_WRONLY);
  1494. if (fd >= 0) {
  1495. if (write(fd, "1", 1) != 1)
  1496. error("Couldn't enable IP forwarding: %m");
  1497. close(fd);
  1498. }
  1499. }
  1500. }
  1501. }
  1502. return 1;
  1503. }
  1504. /********************************************************************
  1505. *
  1506. * cifproxyarp - Delete the proxy ARP entry for the peer.
  1507. */
  1508. int cifproxyarp (int unit, u_int32_t his_adr)
  1509. {
  1510. struct arpreq arpreq;
  1511. if (has_proxy_arp) {
  1512. has_proxy_arp = 0;
  1513. memset (&arpreq, '\0', sizeof(arpreq));
  1514. SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
  1515. SIN_ADDR(arpreq.arp_pa) = his_adr;
  1516. arpreq.arp_flags = ATF_PERM | ATF_PUBL;
  1517. strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
  1518. if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0) {
  1519. if ( ! ok_error ( errno ))
  1520. warn("ioctl(SIOCDARP): %m");
  1521. return 0;
  1522. }
  1523. }
  1524. return 1;
  1525. }
  1526. /********************************************************************
  1527. *
  1528. * get_ether_addr - get the hardware address of an interface on the
  1529. * the same subnet as ipaddr.
  1530. */
  1531. static int get_ether_addr (u_int32_t ipaddr,
  1532. struct sockaddr *hwaddr,
  1533. char *name, int namelen)
  1534. {
  1535. struct ifreq *ifr, *ifend;
  1536. u_int32_t ina, mask;
  1537. char *aliasp;
  1538. struct ifreq ifreq, bestifreq;
  1539. struct ifconf ifc;
  1540. struct ifreq ifs[MAX_IFS];
  1541. u_int32_t bestmask=0;
  1542. int found_interface = 0;
  1543. ifc.ifc_len = sizeof(ifs);
  1544. ifc.ifc_req = ifs;
  1545. if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
  1546. if ( ! ok_error ( errno ))
  1547. error("ioctl(SIOCGIFCONF): %m (line %d)", __LINE__);
  1548. return 0;
  1549. }
  1550. /*
  1551. * Scan through looking for an interface with an Internet
  1552. * address on the same subnet as `ipaddr'.
  1553. */
  1554. ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
  1555. for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
  1556. if (ifr->ifr_addr.sa_family == AF_INET) {
  1557. ina = SIN_ADDR(ifr->ifr_addr);
  1558. strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
  1559. /*
  1560. * Check that the interface is up, and not point-to-point
  1561. * nor loopback.
  1562. */
  1563. if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
  1564. continue;
  1565. if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
  1566. continue;
  1567. /*
  1568. * Get its netmask and check that it's on the right subnet.
  1569. */
  1570. if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
  1571. continue;
  1572. mask = SIN_ADDR(ifreq.ifr_addr);
  1573. if (((ipaddr ^ ina) & mask) != 0)
  1574. continue; /* no match */
  1575. /* matched */
  1576. if (mask >= bestmask) {
  1577. /* Compare using >= instead of > -- it is possible for
  1578. an interface to have a netmask of 0.0.0.0 */
  1579. found_interface = 1;
  1580. bestifreq = ifreq;
  1581. bestmask = mask;
  1582. }
  1583. }
  1584. }
  1585. if (!found_interface) return 0;
  1586. strlcpy(name, bestifreq.ifr_name, namelen);
  1587. /* trim off the :1 in eth0:1 */
  1588. aliasp = strchr(name, ':');
  1589. if (aliasp != 0)
  1590. *aliasp = 0;
  1591. info("found interface %s for proxy arp", name);
  1592. /*
  1593. * Now get the hardware address.
  1594. */
  1595. memset (&bestifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
  1596. if (ioctl (sock_fd, SIOCGIFHWADDR, &bestifreq) < 0) {
  1597. error("SIOCGIFHWADDR(%s): %m", bestifreq.ifr_name);
  1598. return 0;
  1599. }
  1600. memcpy (hwaddr,
  1601. &bestifreq.ifr_hwaddr,
  1602. sizeof (struct sockaddr));
  1603. return 1;
  1604. }
  1605. /*
  1606. * get_if_hwaddr - get the hardware address for the specified
  1607. * network interface device.
  1608. */
  1609. int
  1610. get_if_hwaddr(u_char *addr, char *name)
  1611. {
  1612. struct ifreq ifreq;
  1613. int ret, sock_fd;
  1614. sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  1615. if (sock_fd < 0)
  1616. return 0;
  1617. memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
  1618. strlcpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
  1619. ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
  1620. close(sock_fd);
  1621. if (ret >= 0)
  1622. memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
  1623. return ret;
  1624. }
  1625. /*
  1626. * get_first_ethernet - return the name of the first ethernet-style
  1627. * interface on this system.
  1628. */
  1629. char *
  1630. get_first_ethernet()
  1631. {
  1632. return "eth0";
  1633. }
  1634. /********************************************************************
  1635. *
  1636. * Return user specified netmask, modified by any mask we might determine
  1637. * for address `addr' (in network byte order).
  1638. * Here we scan through the system's list of interfaces, looking for
  1639. * any non-point-to-point interfaces which might appear to be on the same
  1640. * network as `addr'. If we find any, we OR in their netmask to the
  1641. * user-specified netmask.
  1642. */
  1643. u_int32_t GetMask (u_int32_t addr)
  1644. {
  1645. u_int32_t mask, nmask, ina;
  1646. struct ifreq *ifr, *ifend, ifreq;
  1647. struct ifconf ifc;
  1648. struct ifreq ifs[MAX_IFS];
  1649. addr = ntohl(addr);
  1650. if (IN_CLASSA(addr)) /* determine network mask for address class */
  1651. nmask = IN_CLASSA_NET;
  1652. else if (IN_CLASSB(addr))
  1653. nmask = IN_CLASSB_NET;
  1654. else
  1655. nmask = IN_CLASSC_NET;
  1656. /* class D nets are disallowed by bad_ip_adrs */
  1657. mask = netmask | htonl(nmask);
  1658. /*
  1659. * Scan through the system's network interfaces.
  1660. */
  1661. ifc.ifc_len = sizeof(ifs);
  1662. ifc.ifc_req = ifs;
  1663. if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
  1664. if ( ! ok_error ( errno ))
  1665. warn("ioctl(SIOCGIFCONF): %m (line %d)", __LINE__);
  1666. return mask;
  1667. }
  1668. ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
  1669. for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
  1670. /*
  1671. * Check the interface's internet address.
  1672. */
  1673. if (ifr->ifr_addr.sa_family != AF_INET)
  1674. continue;
  1675. ina = SIN_ADDR(ifr->ifr_addr);
  1676. if (((ntohl(ina) ^ addr) & nmask) != 0)
  1677. continue;
  1678. /*
  1679. * Check that the interface is up, and not point-to-point nor loopback.
  1680. */
  1681. strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
  1682. if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
  1683. continue;
  1684. if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
  1685. continue;
  1686. /*
  1687. * Get its netmask and OR it into our mask.
  1688. */
  1689. if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
  1690. continue;
  1691. mask |= SIN_ADDR(ifreq.ifr_addr);
  1692. break;
  1693. }
  1694. return mask;
  1695. }
  1696. /********************************************************************
  1697. *
  1698. * Internal routine to decode the version.modification.patch level
  1699. */
  1700. static void decode_version (char *buf, int *version,
  1701. int *modification, int *patch)
  1702. {
  1703. char *endp;
  1704. *version = (int) strtoul (buf, &endp, 10);
  1705. *modification = 0;
  1706. *patch = 0;
  1707. if (endp != buf && *endp == '.') {
  1708. buf = endp + 1;
  1709. *modification = (int) strtoul (buf, &endp, 10);
  1710. if (endp != buf && *endp == '.') {
  1711. buf = endp + 1;
  1712. *patch = (int) strtoul (buf, &buf, 10);
  1713. }
  1714. }
  1715. }
  1716. /********************************************************************
  1717. *
  1718. * Procedure to determine if the PPP line discipline is registered.
  1719. */
  1720. static int
  1721. ppp_registered(void)
  1722. {
  1723. int local_fd;
  1724. int mfd = -1;
  1725. int ret = 0;
  1726. char slave[16];
  1727. /*
  1728. * We used to open the serial device and set it to the ppp line
  1729. * discipline here, in order to create a ppp unit. But that is
  1730. * not a good idea - the user might have specified a device that
  1731. * they can't open (permission, or maybe it doesn't really exist).
  1732. * So we grab a pty master/slave pair and use that.
  1733. */
  1734. if (!get_pty(&mfd, &local_fd, slave, 0)) {
  1735. no_ppp_msg = "Couldn't determine if PPP is supported (no free ptys)";
  1736. return 0;
  1737. }
  1738. /*
  1739. * Try to put the device into the PPP discipline.
  1740. */
  1741. if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0) {
  1742. error("ioctl(TIOCSETD(PPP)): %m (line %d)", __LINE__);
  1743. } else
  1744. ret = 1;
  1745. close(local_fd);
  1746. close(mfd);
  1747. return ret;
  1748. }
  1749. /********************************************************************
  1750. *
  1751. * ppp_available - check whether the system has any ppp interfaces
  1752. * (in fact we check whether we can do an ioctl on ppp0).
  1753. */
  1754. int ppp_available(void)
  1755. {
  1756. int s, ok, fd;
  1757. struct ifreq ifr;
  1758. int size;
  1759. int my_version, my_modification, my_patch;
  1760. int osmaj, osmin, ospatch;
  1761. /* get the kernel version now, since we are called before sys_init */
  1762. uname(&utsname);
  1763. osmaj = osmin = ospatch = 0;
  1764. sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch);
  1765. kernel_version = KVERSION(osmaj, osmin, ospatch);
  1766. fd = open("/dev/ppp", O_RDWR);
  1767. if (fd >= 0) {
  1768. new_style_driver = 1;
  1769. /* XXX should get from driver */
  1770. driver_version = 2;
  1771. driver_modification = 4;
  1772. driver_patch = 0;
  1773. close(fd);
  1774. return 1;
  1775. }
  1776. if (kernel_version >= KVERSION(2,3,13)) {
  1777. error("Couldn't open the /dev/ppp device: %m");
  1778. if (errno == ENOENT)
  1779. no_ppp_msg =
  1780. "You need to create the /dev/ppp device node by\n"
  1781. "executing the following command as root:\n"
  1782. " mknod /dev/ppp c 108 0\n";
  1783. else if (errno == ENODEV || errno == ENXIO)
  1784. no_ppp_msg =
  1785. "Please load the ppp_generic kernel module.\n";
  1786. return 0;
  1787. }
  1788. /* we are running on a really really old kernel */
  1789. no_ppp_msg =
  1790. "This system lacks kernel support for PPP. This could be because\n"
  1791. "the PPP kernel module could not be loaded, or because PPP was not\n"
  1792. "included in the kernel configuration. If PPP was included as a\n"
  1793. "module, try `/sbin/modprobe -v ppp'. If that fails, check that\n"
  1794. "ppp.o exists in /lib/modules/`uname -r`/net.\n"
  1795. "See README.linux file in the ppp distribution for more details.\n";
  1796. /*
  1797. * Open a socket for doing the ioctl operations.
  1798. */
  1799. s = socket(AF_INET, SOCK_DGRAM, 0);
  1800. if (s < 0)
  1801. return 0;
  1802. strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
  1803. ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
  1804. /*
  1805. * If the device did not exist then attempt to create one by putting the
  1806. * current tty into the PPP discipline. If this works then obtain the
  1807. * flags for the device again.
  1808. */
  1809. if (!ok) {
  1810. if (ppp_registered()) {
  1811. strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
  1812. ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
  1813. }
  1814. }
  1815. /*
  1816. * Ensure that the hardware address is for PPP and not something else
  1817. */
  1818. if (ok)
  1819. ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
  1820. if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
  1821. ok = 0;
  1822. /*
  1823. * This is the PPP device. Validate the version of the driver at this
  1824. * point to ensure that this program will work with the driver.
  1825. */
  1826. if (ok) {
  1827. char abBuffer [1024];
  1828. ifr.ifr_data = abBuffer;
  1829. size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
  1830. if (size < 0) {
  1831. error("Couldn't read driver version: %m");
  1832. ok = 0;
  1833. no_ppp_msg = "Sorry, couldn't verify kernel driver version\n";
  1834. } else {
  1835. decode_version(abBuffer,
  1836. &driver_version,
  1837. &driver_modification,
  1838. &driver_patch);
  1839. /*
  1840. * Validate the version of the driver against the version that we used.
  1841. */
  1842. decode_version(VERSION,
  1843. &my_version,
  1844. &my_modification,
  1845. &my_patch);
  1846. /* The version numbers must match */
  1847. if (driver_version != my_version)
  1848. ok = 0;
  1849. /* The modification levels must be legal */
  1850. if (driver_modification < 3) {
  1851. if (driver_modification >= 2) {
  1852. /* we can cope with 2.2.0 and above */
  1853. driver_is_old = 1;
  1854. } else {
  1855. ok = 0;
  1856. }
  1857. }
  1858. close (s);
  1859. if (!ok) {
  1860. slprintf(route_buffer, sizeof(route_buffer),
  1861. "Sorry - PPP driver version %d.%d.%d is out of date\n",
  1862. driver_version, driver_modification, driver_patch);
  1863. no_ppp_msg = route_buffer;
  1864. }
  1865. }
  1866. }
  1867. return ok;
  1868. }
  1869. #ifndef HAVE_LOGWTMP
  1870. /********************************************************************
  1871. *
  1872. * Update the wtmp file with the appropriate user name and tty device.
  1873. */
  1874. void logwtmp (const char *line, const char *name, const char *host)
  1875. {
  1876. struct utmp ut, *utp;
  1877. pid_t mypid = getpid();
  1878. #if __GLIBC__ < 2
  1879. int wtmp;
  1880. #endif
  1881. /*
  1882. * Update the signon database for users.
  1883. * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
  1884. */
  1885. utmpname(_PATH_UTMP);
  1886. setutent();
  1887. while ((utp = getutent()) && (utp->ut_pid != mypid))
  1888. /* nothing */;
  1889. if (utp)
  1890. memcpy(&ut, utp, sizeof(ut));
  1891. else
  1892. /* some gettys/telnetds don't initialize utmp... */
  1893. memset(&ut, 0, sizeof(ut));
  1894. if (ut.ut_id[0] == 0)
  1895. strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
  1896. strncpy(ut.ut_user, name, sizeof(ut.ut_user));
  1897. strncpy(ut.ut_line, line, sizeof(ut.ut_line));
  1898. time(&ut.ut_time);
  1899. ut.ut_type = USER_PROCESS;
  1900. ut.ut_pid = mypid;
  1901. /* Insert the host name if one is supplied */
  1902. if (*host)
  1903. strncpy (ut.ut_host, host, sizeof(ut.ut_host));
  1904. /* Insert the IP address of the remote system if IP is enabled */
  1905. if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
  1906. memcpy(&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
  1907. sizeof(ut.ut_addr));
  1908. /* CL: Makes sure that the logout works */
  1909. if (*host == 0 && *name==0)
  1910. ut.ut_host[0]=0;
  1911. pututline(&ut);
  1912. endutent();
  1913. /*
  1914. * Update the wtmp file.
  1915. */
  1916. #if __GLIBC__ >= 2
  1917. updwtmp(_PATH_WTMP, &ut);
  1918. #else
  1919. wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
  1920. if (wtmp >= 0) {
  1921. flock(wtmp, LOCK_EX);
  1922. if (write (wtmp, (char *)&ut, sizeof(ut)) != sizeof(ut))
  1923. warn("error writing %s: %m", _PATH_WTMP);
  1924. flock(wtmp, LOCK_UN);
  1925. close (wtmp);
  1926. }
  1927. #endif
  1928. }
  1929. #endif /* HAVE_LOGWTMP */
  1930. /********************************************************************
  1931. *
  1932. * sifvjcomp - config tcp header compression
  1933. */
  1934. int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
  1935. {
  1936. u_int x;
  1937. if (vjcomp) {
  1938. if (ioctl(ppp_dev_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
  1939. error("Couldn't set up TCP header compression: %m");
  1940. vjcomp = 0;
  1941. }
  1942. }
  1943. x = (vjcomp? SC_COMP_TCP: 0) | (cidcomp? 0: SC_NO_TCP_CCID);
  1944. modify_flags(ppp_dev_fd, SC_COMP_TCP|SC_NO_TCP_CCID, x);
  1945. return 1;
  1946. }
  1947. /********************************************************************
  1948. *
  1949. * sifup - Config the interface up and enable IP packets to pass.
  1950. */
  1951. int sifup(int u)
  1952. {
  1953. int ret;
  1954. if ((ret = setifstate(u, 1)))
  1955. if_is_up++;
  1956. return ret;
  1957. }
  1958. /********************************************************************
  1959. *
  1960. * sifdown - Disable the indicated protocol and config the interface
  1961. * down if there are no remaining protocols.
  1962. */
  1963. int sifdown (int u)
  1964. {
  1965. if (if_is_up && --if_is_up > 0)
  1966. return 1;
  1967. #ifdef INET6
  1968. if (if6_is_up)
  1969. return 1;
  1970. #endif /* INET6 */
  1971. return setifstate(u, 0);
  1972. }
  1973. #ifdef INET6
  1974. /********************************************************************
  1975. *
  1976. * sif6up - Config the interface up for IPv6
  1977. */
  1978. int sif6up(int u)
  1979. {
  1980. int ret;
  1981. if ((ret = setifstate(u, 1)))
  1982. if6_is_up = 1;
  1983. return ret;
  1984. }
  1985. /********************************************************************
  1986. *
  1987. * sif6down - Disable the IPv6CP protocol and config the interface
  1988. * down if there are no remaining protocols.
  1989. */
  1990. int sif6down (int u)
  1991. {
  1992. if6_is_up = 0;
  1993. if (if_is_up)
  1994. return 1;
  1995. return setifstate(u, 0);
  1996. }
  1997. #endif /* INET6 */
  1998. /********************************************************************
  1999. *
  2000. * setifstate - Config the interface up or down
  2001. */
  2002. static int setifstate (int u, int state)
  2003. {
  2004. struct ifreq ifr;
  2005. memset (&ifr, '\0', sizeof (ifr));
  2006. strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
  2007. if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
  2008. if (! ok_error (errno))
  2009. error("ioctl (SIOCGIFFLAGS): %m (line %d)", __LINE__);
  2010. return 0;
  2011. }
  2012. if (state)
  2013. ifr.ifr_flags |= IFF_UP;
  2014. else
  2015. ifr.ifr_flags &= ~IFF_UP;
  2016. ifr.ifr_flags |= IFF_POINTOPOINT;
  2017. if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
  2018. if (! ok_error (errno))
  2019. error("ioctl(SIOCSIFFLAGS): %m (line %d)", __LINE__);
  2020. return 0;
  2021. }
  2022. return 1;
  2023. }
  2024. /********************************************************************
  2025. *
  2026. * sifaddr - Config the interface IP addresses and netmask.
  2027. */
  2028. int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
  2029. u_int32_t net_mask)
  2030. {
  2031. struct ifreq ifr;
  2032. struct rtentry rt;
  2033. memset (&ifr, '\0', sizeof (ifr));
  2034. memset (&rt, '\0', sizeof (rt));
  2035. SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
  2036. SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
  2037. SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
  2038. strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
  2039. /*
  2040. * Set our IP address
  2041. */
  2042. SIN_ADDR(ifr.ifr_addr) = our_adr;
  2043. if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
  2044. if (errno != EEXIST) {
  2045. if (! ok_error (errno))
  2046. error("ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
  2047. }
  2048. else {
  2049. warn("ioctl(SIOCSIFADDR): Address already exists");
  2050. }
  2051. return (0);
  2052. }
  2053. /*
  2054. * Set the gateway address
  2055. */
  2056. if (his_adr != 0) {
  2057. SIN_ADDR(ifr.ifr_dstaddr) = his_adr;
  2058. if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
  2059. if (! ok_error (errno))
  2060. error("ioctl(SIOCSIFDSTADDR): %m (line %d)", __LINE__);
  2061. return (0);
  2062. }
  2063. }
  2064. /*
  2065. * Set the netmask.
  2066. * For recent kernels, force the netmask to 255.255.255.255.
  2067. */
  2068. if (kernel_version >= KVERSION(2,1,16))
  2069. net_mask = ~0L;
  2070. if (net_mask != 0) {
  2071. SIN_ADDR(ifr.ifr_netmask) = net_mask;
  2072. if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
  2073. if (! ok_error (errno))
  2074. error("ioctl(SIOCSIFNETMASK): %m (line %d)", __LINE__);
  2075. return (0);
  2076. }
  2077. }
  2078. /*
  2079. * Add the device route
  2080. */
  2081. if (kernel_version < KVERSION(2,1,16)) {
  2082. SET_SA_FAMILY (rt.rt_dst, AF_INET);
  2083. SET_SA_FAMILY (rt.rt_gateway, AF_INET);
  2084. rt.rt_dev = ifname;
  2085. SIN_ADDR(rt.rt_gateway) = 0L;
  2086. SIN_ADDR(rt.rt_dst) = his_adr;
  2087. rt.rt_flags = RTF_UP | RTF_HOST;
  2088. if (kernel_version > KVERSION(2,1,0)) {
  2089. SET_SA_FAMILY (rt.rt_genmask, AF_INET);
  2090. SIN_ADDR(rt.rt_genmask) = -1L;
  2091. }
  2092. if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
  2093. if (! ok_error (errno))
  2094. error("ioctl(SIOCADDRT) device route: %m (line %d)", __LINE__);
  2095. return (0);
  2096. }
  2097. }
  2098. /* set ip_dynaddr in demand mode if address changes */
  2099. if (demand && tune_kernel && !dynaddr_set
  2100. && our_old_addr && our_old_addr != our_adr) {
  2101. /* set ip_dynaddr if possible */
  2102. char *path;
  2103. int fd;
  2104. path = path_to_procfs("/sys/net/ipv4/ip_dynaddr");
  2105. if (path != 0 && (fd = open(path, O_WRONLY)) >= 0) {
  2106. if (write(fd, "1", 1) != 1)
  2107. error("Couldn't enable dynamic IP addressing: %m");
  2108. close(fd);
  2109. }
  2110. dynaddr_set = 1; /* only 1 attempt */
  2111. }
  2112. our_old_addr = 0;
  2113. return 1;
  2114. }
  2115. /********************************************************************
  2116. *
  2117. * cifaddr - Clear the interface IP addresses, and delete routes
  2118. * through the interface if possible.
  2119. */
  2120. int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
  2121. {
  2122. struct ifreq ifr;
  2123. if (kernel_version < KVERSION(2,1,16)) {
  2124. /*
  2125. * Delete the route through the device
  2126. */
  2127. struct rtentry rt;
  2128. memset (&rt, '\0', sizeof (rt));
  2129. SET_SA_FAMILY (rt.rt_dst, AF_INET);
  2130. SET_SA_FAMILY (rt.rt_gateway, AF_INET);
  2131. rt.rt_dev = ifname;
  2132. SIN_ADDR(rt.rt_gateway) = 0;
  2133. SIN_ADDR(rt.rt_dst) = his_adr;
  2134. rt.rt_flags = RTF_UP | RTF_HOST;
  2135. if (kernel_version > KVERSION(2,1,0)) {
  2136. SET_SA_FAMILY (rt.rt_genmask, AF_INET);
  2137. SIN_ADDR(rt.rt_genmask) = -1L;
  2138. }
  2139. if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
  2140. if (still_ppp() && ! ok_error (errno))
  2141. error("ioctl(SIOCDELRT) device route: %m (line %d)", __LINE__);
  2142. return (0);
  2143. }
  2144. }
  2145. /* This way it is possible to have an IPX-only or IPv6-only interface */
  2146. memset(&ifr, 0, sizeof(ifr));
  2147. SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
  2148. strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  2149. if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
  2150. if (! ok_error (errno)) {
  2151. error("ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
  2152. return 0;
  2153. }
  2154. }
  2155. our_old_addr = our_adr;
  2156. return 1;
  2157. }
  2158. #ifdef INET6
  2159. /********************************************************************
  2160. *
  2161. * sif6addr - Config the interface with an IPv6 link-local address
  2162. */
  2163. int sif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
  2164. {
  2165. struct in6_ifreq ifr6;
  2166. struct ifreq ifr;
  2167. struct in6_rtmsg rt6;
  2168. if (sock6_fd < 0) {
  2169. errno = -sock6_fd;
  2170. error("IPv6 socket creation failed: %m");
  2171. return 0;
  2172. }
  2173. memset(&ifr, 0, sizeof (ifr));
  2174. strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  2175. if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
  2176. error("sif6addr: ioctl(SIOCGIFINDEX): %m (line %d)", __LINE__);
  2177. return 0;
  2178. }
  2179. /* Local interface */
  2180. memset(&ifr6, 0, sizeof(ifr6));
  2181. IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64);
  2182. ifr6.ifr6_ifindex = ifr.ifr_ifindex;
  2183. ifr6.ifr6_prefixlen = 10;
  2184. if (ioctl(sock6_fd, SIOCSIFADDR, &ifr6) < 0) {
  2185. error("sif6addr: ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
  2186. return 0;
  2187. }
  2188. /* Route to remote host */
  2189. memset(&rt6, 0, sizeof(rt6));
  2190. IN6_LLADDR_FROM_EUI64(rt6.rtmsg_dst, his_eui64);
  2191. rt6.rtmsg_flags = RTF_UP;
  2192. rt6.rtmsg_dst_len = 10;
  2193. rt6.rtmsg_ifindex = ifr.ifr_ifindex;
  2194. rt6.rtmsg_metric = 1;
  2195. if (ioctl(sock6_fd, SIOCADDRT, &rt6) < 0) {
  2196. error("sif6addr: ioctl(SIOCADDRT): %m (line %d)", __LINE__);
  2197. return 0;
  2198. }
  2199. return 1;
  2200. }
  2201. /********************************************************************
  2202. *
  2203. * cif6addr - Remove IPv6 address from interface
  2204. */
  2205. int cif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
  2206. {
  2207. struct ifreq ifr;
  2208. struct in6_ifreq ifr6;
  2209. if (sock6_fd < 0) {
  2210. errno = -sock6_fd;
  2211. error("IPv6 socket creation failed: %m");
  2212. return 0;
  2213. }
  2214. memset(&ifr, 0, sizeof(ifr));
  2215. strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  2216. if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
  2217. error("cif6addr: ioctl(SIOCGIFINDEX): %m (line %d)", __LINE__);
  2218. return 0;
  2219. }
  2220. memset(&ifr6, 0, sizeof(ifr6));
  2221. IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64);
  2222. ifr6.ifr6_ifindex = ifr.ifr_ifindex;
  2223. ifr6.ifr6_prefixlen = 10;
  2224. if (ioctl(sock6_fd, SIOCDIFADDR, &ifr6) < 0) {
  2225. if (errno != EADDRNOTAVAIL) {
  2226. if (! ok_error (errno))
  2227. error("cif6addr: ioctl(SIOCDIFADDR): %m (line %d)", __LINE__);
  2228. }
  2229. else {
  2230. warn("cif6addr: ioctl(SIOCDIFADDR): No such address");
  2231. }
  2232. return (0);
  2233. }
  2234. return 1;
  2235. }
  2236. #endif /* INET6 */
  2237. /*
  2238. * get_pty - get a pty master/slave pair and chown the slave side
  2239. * to the uid given. Assumes slave_name points to >= 16 bytes of space.
  2240. */
  2241. int
  2242. get_pty(master_fdp, slave_fdp, slave_name, uid)
  2243. int *master_fdp;
  2244. int *slave_fdp;
  2245. char *slave_name;
  2246. int uid;
  2247. {
  2248. int i, mfd, sfd = -1;
  2249. char pty_name[16];
  2250. struct termios tios;
  2251. #ifdef TIOCGPTN
  2252. /*
  2253. * Try the unix98 way first.
  2254. */
  2255. mfd = open("/dev/ptmx", O_RDWR);
  2256. if (mfd >= 0) {
  2257. int ptn;
  2258. if (ioctl(mfd, TIOCGPTN, &ptn) >= 0) {
  2259. slprintf(pty_name, sizeof(pty_name), "/dev/pts/%d", ptn);
  2260. chmod(pty_name, S_IRUSR | S_IWUSR);
  2261. #ifdef TIOCSPTLCK
  2262. ptn = 0;
  2263. if (ioctl(mfd, TIOCSPTLCK, &ptn) < 0)
  2264. warn("Couldn't unlock pty slave %s: %m", pty_name);
  2265. #endif
  2266. if ((sfd = open(pty_name, O_RDWR | O_NOCTTY)) < 0)
  2267. warn("Couldn't open pty slave %s: %m", pty_name);
  2268. }
  2269. }
  2270. #endif /* TIOCGPTN */
  2271. if (sfd < 0) {
  2272. /* the old way - scan through the pty name space */
  2273. for (i = 0; i < 64; ++i) {
  2274. slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
  2275. 'p' + i / 16, i % 16);
  2276. mfd = open(pty_name, O_RDWR, 0);
  2277. if (mfd >= 0) {
  2278. pty_name[5] = 't';
  2279. sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
  2280. if (sfd >= 0) {
  2281. fchown(sfd, uid, -1);
  2282. fchmod(sfd, S_IRUSR | S_IWUSR);
  2283. break;
  2284. }
  2285. close(mfd);
  2286. }
  2287. }
  2288. }
  2289. if (sfd < 0)
  2290. return 0;
  2291. strlcpy(slave_name, pty_name, 16);
  2292. *master_fdp = mfd;
  2293. *slave_fdp = sfd;
  2294. if (tcgetattr(sfd, &tios) == 0) {
  2295. tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
  2296. tios.c_cflag |= CS8 | CREAD | CLOCAL;
  2297. tios.c_iflag = IGNPAR;
  2298. tios.c_oflag = 0;
  2299. tios.c_lflag = 0;
  2300. if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
  2301. warn("couldn't set attributes on pty: %m");
  2302. } else
  2303. warn("couldn't get attributes on pty: %m");
  2304. return 1;
  2305. }
  2306. /********************************************************************
  2307. *
  2308. * open_loopback - open the device we use for getting packets
  2309. * in demand mode. Under Linux, we use a pty master/slave pair.
  2310. */
  2311. int
  2312. open_ppp_loopback(void)
  2313. {
  2314. int flags;
  2315. looped = 1;
  2316. if (new_style_driver) {
  2317. /* allocate ourselves a ppp unit */
  2318. if (make_ppp_unit() < 0)
  2319. die(1);
  2320. modify_flags(ppp_dev_fd, 0, SC_LOOP_TRAFFIC);
  2321. set_kdebugflag(kdebugflag);
  2322. ppp_fd = -1;
  2323. return ppp_dev_fd;
  2324. }
  2325. if (!get_pty(&master_fd, &slave_fd, loop_name, 0))
  2326. fatal("No free pty for loopback");
  2327. set_ppp_fd(slave_fd);
  2328. flags = fcntl(master_fd, F_GETFL);
  2329. if (flags == -1 ||
  2330. fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
  2331. warn("couldn't set master loopback to nonblock: %m");
  2332. flags = fcntl(ppp_fd, F_GETFL);
  2333. if (flags == -1 ||
  2334. fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
  2335. warn("couldn't set slave loopback to nonblock: %m");
  2336. if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
  2337. fatal("ioctl(TIOCSETD): %m (line %d)", __LINE__);
  2338. /*
  2339. * Find out which interface we were given.
  2340. */
  2341. if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
  2342. fatal("ioctl(PPPIOCGUNIT): %m (line %d)", __LINE__);
  2343. /*
  2344. * Enable debug in the driver if requested.
  2345. */
  2346. set_kdebugflag (kdebugflag);
  2347. return master_fd;
  2348. }
  2349. /********************************************************************
  2350. *
  2351. * sifnpmode - Set the mode for handling packets for a given NP.
  2352. */
  2353. int
  2354. sifnpmode(u, proto, mode)
  2355. int u;
  2356. int proto;
  2357. enum NPmode mode;
  2358. {
  2359. struct npioctl npi;
  2360. npi.protocol = proto;
  2361. npi.mode = mode;
  2362. if (ioctl(ppp_dev_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
  2363. if (! ok_error (errno))
  2364. error("ioctl(PPPIOCSNPMODE, %d, %d): %m", proto, mode);
  2365. return 0;
  2366. }
  2367. return 1;
  2368. }
  2369. /********************************************************************
  2370. *
  2371. * sipxfaddr - Config the interface IPX networknumber
  2372. */
  2373. int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
  2374. {
  2375. int result = 1;
  2376. #ifdef IPX_CHANGE
  2377. int skfd;
  2378. struct ifreq ifr;
  2379. struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
  2380. skfd = socket (AF_IPX, SOCK_DGRAM, 0);
  2381. if (skfd < 0) {
  2382. if (! ok_error (errno))
  2383. dbglog("socket(AF_IPX): %m (line %d)", __LINE__);
  2384. result = 0;
  2385. }
  2386. else {
  2387. memset (&ifr, '\0', sizeof (ifr));
  2388. strlcpy (ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  2389. memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
  2390. sipx->sipx_family = AF_IPX;
  2391. sipx->sipx_port = 0;
  2392. sipx->sipx_network = htonl (network);
  2393. sipx->sipx_type = IPX_FRAME_ETHERII;
  2394. sipx->sipx_action = IPX_CRTITF;
  2395. /*
  2396. * Set the IPX device
  2397. */
  2398. if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
  2399. result = 0;
  2400. if (errno != EEXIST) {
  2401. if (! ok_error (errno))
  2402. dbglog("ioctl(SIOCSIFADDR, CRTITF): %m (line %d)", __LINE__);
  2403. }
  2404. else {
  2405. warn("ioctl(SIOCSIFADDR, CRTITF): Address already exists");
  2406. }
  2407. }
  2408. close (skfd);
  2409. }
  2410. #endif
  2411. return result;
  2412. }
  2413. /********************************************************************
  2414. *
  2415. * cipxfaddr - Clear the information for the IPX network. The IPX routes
  2416. * are removed and the device is no longer able to pass IPX
  2417. * frames.
  2418. */
  2419. int cipxfaddr (int unit)
  2420. {
  2421. int result = 1;
  2422. #ifdef IPX_CHANGE
  2423. int skfd;
  2424. struct ifreq ifr;
  2425. struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
  2426. skfd = socket (AF_IPX, SOCK_DGRAM, 0);
  2427. if (skfd < 0) {
  2428. if (! ok_error (errno))
  2429. dbglog("socket(AF_IPX): %m (line %d)", __LINE__);
  2430. result = 0;
  2431. }
  2432. else {
  2433. memset (&ifr, '\0', sizeof (ifr));
  2434. strlcpy (ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  2435. sipx->sipx_type = IPX_FRAME_ETHERII;
  2436. sipx->sipx_action = IPX_DLTITF;
  2437. sipx->sipx_family = AF_IPX;
  2438. /*
  2439. * Set the IPX device
  2440. */
  2441. if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
  2442. if (! ok_error (errno))
  2443. info("ioctl(SIOCSIFADDR, IPX_DLTITF): %m (line %d)", __LINE__);
  2444. result = 0;
  2445. }
  2446. close (skfd);
  2447. }
  2448. #endif
  2449. return result;
  2450. }
  2451. /*
  2452. * Use the hostname as part of the random number seed.
  2453. */
  2454. int
  2455. get_host_seed()
  2456. {
  2457. int h;
  2458. char *p = hostname;
  2459. h = 407;
  2460. for (p = hostname; *p != 0; ++p)
  2461. h = h * 37 + *p;
  2462. return h;
  2463. }
  2464. /********************************************************************
  2465. *
  2466. * sys_check_options - check the options that the user specified
  2467. */
  2468. int
  2469. sys_check_options(void)
  2470. {
  2471. #ifdef IPX_CHANGE
  2472. /*
  2473. * Disable the IPX protocol if the support is not present in the kernel.
  2474. */
  2475. char *path;
  2476. if (ipxcp_protent.enabled_flag) {
  2477. struct stat stat_buf;
  2478. if ( ((path = path_to_procfs("/net/ipx/interface")) == NULL
  2479. && (path = path_to_procfs("/net/ipx_interface")) == NULL)
  2480. || lstat(path, &stat_buf) < 0) {
  2481. error("IPX support is not present in the kernel\n");
  2482. ipxcp_protent.enabled_flag = 0;
  2483. }
  2484. }
  2485. #endif
  2486. if (demand && driver_is_old) {
  2487. option_error("demand dialling is not supported by kernel driver "
  2488. "version %d.%d.%d", driver_version, driver_modification,
  2489. driver_patch);
  2490. return 0;
  2491. }
  2492. if (multilink && !new_style_driver) {
  2493. warn("Warning: multilink is not supported by the kernel driver");
  2494. multilink = 0;
  2495. }
  2496. return 1;
  2497. }
  2498. #ifdef INET6
  2499. /*
  2500. * ether_to_eui64 - Convert 48-bit Ethernet address into 64-bit EUI
  2501. *
  2502. * convert the 48-bit MAC address of eth0 into EUI 64. caller also assumes
  2503. * that the system has a properly configured Ethernet interface for this
  2504. * function to return non-zero.
  2505. */
  2506. int
  2507. ether_to_eui64(eui64_t *p_eui64)
  2508. {
  2509. struct ifreq ifr;
  2510. int skfd;
  2511. const unsigned char *ptr;
  2512. skfd = socket(PF_INET6, SOCK_DGRAM, 0);
  2513. if(skfd == -1)
  2514. {
  2515. warn("could not open IPv6 socket");
  2516. return 0;
  2517. }
  2518. strcpy(ifr.ifr_name, "eth0");
  2519. if(ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
  2520. {
  2521. close(skfd);
  2522. warn("could not obtain hardware address for eth0");
  2523. return 0;
  2524. }
  2525. close(skfd);
  2526. /*
  2527. * And convert the EUI-48 into EUI-64, per RFC 2472 [sec 4.1]
  2528. */
  2529. ptr = (unsigned char *) ifr.ifr_hwaddr.sa_data;
  2530. p_eui64->e8[0] = ptr[0] | 0x02;
  2531. p_eui64->e8[1] = ptr[1];
  2532. p_eui64->e8[2] = ptr[2];
  2533. p_eui64->e8[3] = 0xFF;
  2534. p_eui64->e8[4] = 0xFE;
  2535. p_eui64->e8[5] = ptr[3];
  2536. p_eui64->e8[6] = ptr[4];
  2537. p_eui64->e8[7] = ptr[5];
  2538. return 1;
  2539. }
  2540. #endif