ipv6cp.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. /*
  2. * ipv6cp.c - PPP IPV6 Control Protocol.
  3. *
  4. * Copyright (c) 1999 Tommi Komulainen. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. The name(s) of the authors of this software must not be used to
  19. * endorse or promote products derived from this software without
  20. * prior written permission.
  21. *
  22. * 4. Redistributions of any form whatsoever must retain the following
  23. * acknowledgment:
  24. * "This product includes software developed by Tommi Komulainen
  25. * <Tommi.Komulainen@iki.fi>".
  26. *
  27. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  28. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  29. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  30. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  31. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  32. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  33. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  34. *
  35. */
  36. /* Original version, based on RFC2023 :
  37. Copyright (c) 1995, 1996, 1997 Francis.Dupont@inria.fr, INRIA Rocquencourt,
  38. Alain.Durand@imag.fr, IMAG,
  39. Jean-Luc.Richier@imag.fr, IMAG-LSR.
  40. Copyright (c) 1998, 1999 Francis.Dupont@inria.fr, GIE DYADE,
  41. Alain.Durand@imag.fr, IMAG,
  42. Jean-Luc.Richier@imag.fr, IMAG-LSR.
  43. Ce travail a été fait au sein du GIE DYADE (Groupement d'Intérêt
  44. Économique ayant pour membres BULL S.A. et l'INRIA).
  45. Ce logiciel informatique est disponible aux conditions
  46. usuelles dans la recherche, c'est-à-dire qu'il peut
  47. être utilisé, copié, modifié, distribué à l'unique
  48. condition que ce texte soit conservé afin que
  49. l'origine de ce logiciel soit reconnue.
  50. Le nom de l'Institut National de Recherche en Informatique
  51. et en Automatique (INRIA), de l'IMAG, ou d'une personne morale
  52. ou physique ayant participé à l'élaboration de ce logiciel ne peut
  53. être utilisé sans son accord préalable explicite.
  54. Ce logiciel est fourni tel quel sans aucune garantie,
  55. support ou responsabilité d'aucune sorte.
  56. Ce logiciel est dérivé de sources d'origine
  57. "University of California at Berkeley" et
  58. "Digital Equipment Corporation" couvertes par des copyrights.
  59. L'Institut d'Informatique et de Mathématiques Appliquées de Grenoble (IMAG)
  60. est une fédération d'unités mixtes de recherche du CNRS, de l'Institut National
  61. Polytechnique de Grenoble et de l'Université Joseph Fourier regroupant
  62. sept laboratoires dont le laboratoire Logiciels, Systèmes, Réseaux (LSR).
  63. This work has been done in the context of GIE DYADE (joint R & D venture
  64. between BULL S.A. and INRIA).
  65. This software is available with usual "research" terms
  66. with the aim of retain credits of the software.
  67. Permission to use, copy, modify and distribute this software for any
  68. purpose and without fee is hereby granted, provided that the above
  69. copyright notice and this permission notice appear in all copies,
  70. and the name of INRIA, IMAG, or any contributor not be used in advertising
  71. or publicity pertaining to this material without the prior explicit
  72. permission. The software is provided "as is" without any
  73. warranties, support or liabilities of any kind.
  74. This software is derived from source code from
  75. "University of California at Berkeley" and
  76. "Digital Equipment Corporation" protected by copyrights.
  77. Grenoble's Institute of Computer Science and Applied Mathematics (IMAG)
  78. is a federation of seven research units funded by the CNRS, National
  79. Polytechnic Institute of Grenoble and University Joseph Fourier.
  80. The research unit in Software, Systems, Networks (LSR) is member of IMAG.
  81. */
  82. /*
  83. * Derived from :
  84. *
  85. *
  86. * ipcp.c - PPP IP Control Protocol.
  87. *
  88. * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
  89. *
  90. * Redistribution and use in source and binary forms, with or without
  91. * modification, are permitted provided that the following conditions
  92. * are met:
  93. *
  94. * 1. Redistributions of source code must retain the above copyright
  95. * notice, this list of conditions and the following disclaimer.
  96. *
  97. * 2. Redistributions in binary form must reproduce the above copyright
  98. * notice, this list of conditions and the following disclaimer in
  99. * the documentation and/or other materials provided with the
  100. * distribution.
  101. *
  102. * 3. The name "Carnegie Mellon University" must not be used to
  103. * endorse or promote products derived from this software without
  104. * prior written permission. For permission or any legal
  105. * details, please contact
  106. * Office of Technology Transfer
  107. * Carnegie Mellon University
  108. * 5000 Forbes Avenue
  109. * Pittsburgh, PA 15213-3890
  110. * (412) 268-4387, fax: (412) 268-7395
  111. * tech-transfer@andrew.cmu.edu
  112. *
  113. * 4. Redistributions of any form whatsoever must retain the following
  114. * acknowledgment:
  115. * "This product includes software developed by Computing Services
  116. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  117. *
  118. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  119. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  120. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  121. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  122. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  123. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  124. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  125. *
  126. * $Id: ipv6cp.c,v 1.21 2005/08/25 23:59:34 paulus Exp $
  127. */
  128. #define RCSID "$Id: ipv6cp.c,v 1.21 2005/08/25 23:59:34 paulus Exp $"
  129. /*
  130. * TODO:
  131. *
  132. * Proxy Neighbour Discovery.
  133. *
  134. * Better defines for selecting the ordering of
  135. * interface up / set address. (currently checks for __linux__,
  136. * since SVR4 && (SNI || __USLC__) didn't work properly)
  137. */
  138. #include <stdio.h>
  139. #include <stdlib.h>
  140. #include <string.h>
  141. #include <unistd.h>
  142. #include <netdb.h>
  143. #include <sys/param.h>
  144. #include <sys/types.h>
  145. #include <sys/socket.h>
  146. #include <netinet/in.h>
  147. #include <arpa/inet.h>
  148. #include "pppd.h"
  149. #include "fsm.h"
  150. #include "ipcp.h"
  151. #include "ipv6cp.h"
  152. #include "magic.h"
  153. #include "pathnames.h"
  154. static const char rcsid[] = RCSID;
  155. /* global vars */
  156. ipv6cp_options ipv6cp_wantoptions[NUM_PPP]; /* Options that we want to request */
  157. ipv6cp_options ipv6cp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
  158. ipv6cp_options ipv6cp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
  159. ipv6cp_options ipv6cp_hisoptions[NUM_PPP]; /* Options that we ack'd */
  160. int no_ifaceid_neg = 0;
  161. /* local vars */
  162. static int ipv6cp_is_up;
  163. /* Hook for a plugin to know when IPv6 protocol has come up */
  164. void (*ipv6_up_hook) __P((void)) = NULL;
  165. /* Hook for a plugin to know when IPv6 protocol has come down */
  166. void (*ipv6_down_hook) __P((void)) = NULL;
  167. /* Notifiers for when IPCPv6 goes up and down */
  168. struct notifier *ipv6_up_notifier = NULL;
  169. struct notifier *ipv6_down_notifier = NULL;
  170. /*
  171. * Callbacks for fsm code. (CI = Configuration Information)
  172. */
  173. static void ipv6cp_resetci __P((fsm *)); /* Reset our CI */
  174. static int ipv6cp_cilen __P((fsm *)); /* Return length of our CI */
  175. static void ipv6cp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
  176. static int ipv6cp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
  177. static int ipv6cp_nakci __P((fsm *, u_char *, int, int));/* Peer nak'd our CI */
  178. static int ipv6cp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
  179. static int ipv6cp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
  180. static void ipv6cp_up __P((fsm *)); /* We're UP */
  181. static void ipv6cp_down __P((fsm *)); /* We're DOWN */
  182. static void ipv6cp_finished __P((fsm *)); /* Don't need lower layer */
  183. fsm ipv6cp_fsm[NUM_PPP]; /* IPV6CP fsm structure */
  184. static fsm_callbacks ipv6cp_callbacks = { /* IPV6CP callback routines */
  185. ipv6cp_resetci, /* Reset our Configuration Information */
  186. ipv6cp_cilen, /* Length of our Configuration Information */
  187. ipv6cp_addci, /* Add our Configuration Information */
  188. ipv6cp_ackci, /* ACK our Configuration Information */
  189. ipv6cp_nakci, /* NAK our Configuration Information */
  190. ipv6cp_rejci, /* Reject our Configuration Information */
  191. ipv6cp_reqci, /* Request peer's Configuration Information */
  192. ipv6cp_up, /* Called when fsm reaches OPENED state */
  193. ipv6cp_down, /* Called when fsm leaves OPENED state */
  194. NULL, /* Called when we want the lower layer up */
  195. ipv6cp_finished, /* Called when we want the lower layer down */
  196. NULL, /* Called when Protocol-Reject received */
  197. NULL, /* Retransmission is necessary */
  198. NULL, /* Called to handle protocol-specific codes */
  199. "IPV6CP" /* String name of protocol */
  200. };
  201. /*
  202. * Command-line options.
  203. */
  204. static int setifaceid __P((char **arg));
  205. static void printifaceid __P((option_t *,
  206. void (*)(void *, char *, ...), void *));
  207. static option_t ipv6cp_option_list[] = {
  208. { "ipv6", o_special, (void *)setifaceid,
  209. "Set interface identifiers for IPV6",
  210. OPT_A2PRINTER, (void *)printifaceid },
  211. { "+ipv6", o_bool, &ipv6cp_protent.enabled_flag,
  212. "Enable IPv6 and IPv6CP", OPT_PRIO | 1 },
  213. { "noipv6", o_bool, &ipv6cp_protent.enabled_flag,
  214. "Disable IPv6 and IPv6CP", OPT_PRIOSUB },
  215. { "-ipv6", o_bool, &ipv6cp_protent.enabled_flag,
  216. "Disable IPv6 and IPv6CP", OPT_PRIOSUB | OPT_ALIAS },
  217. { "ipv6cp-accept-local", o_bool, &ipv6cp_allowoptions[0].accept_local,
  218. "Accept peer's interface identifier for us", 1 },
  219. { "ipv6cp-use-ipaddr", o_bool, &ipv6cp_allowoptions[0].use_ip,
  220. "Use (default) IPv4 address as interface identifier", 1 },
  221. { "ipv6cp-use-persistent", o_bool, &ipv6cp_wantoptions[0].use_persistent,
  222. "Use uniquely-available persistent value for link local address", 1 },
  223. { "ipv6cp-restart", o_int, &ipv6cp_fsm[0].timeouttime,
  224. "Set timeout for IPv6CP", OPT_PRIO },
  225. { "ipv6cp-max-terminate", o_int, &ipv6cp_fsm[0].maxtermtransmits,
  226. "Set max #xmits for term-reqs", OPT_PRIO },
  227. { "ipv6cp-max-configure", o_int, &ipv6cp_fsm[0].maxconfreqtransmits,
  228. "Set max #xmits for conf-reqs", OPT_PRIO },
  229. { "ipv6cp-max-failure", o_int, &ipv6cp_fsm[0].maxnakloops,
  230. "Set max #conf-naks for IPv6CP", OPT_PRIO },
  231. { NULL }
  232. };
  233. /*
  234. * Protocol entry points from main code.
  235. */
  236. static void ipv6cp_init __P((int));
  237. static void ipv6cp_open __P((int));
  238. static void ipv6cp_close __P((int, char *));
  239. static void ipv6cp_lowerup __P((int));
  240. static void ipv6cp_lowerdown __P((int));
  241. static void ipv6cp_input __P((int, u_char *, int));
  242. static void ipv6cp_protrej __P((int));
  243. static int ipv6cp_printpkt __P((u_char *, int,
  244. void (*) __P((void *, char *, ...)), void *));
  245. static void ipv6_check_options __P((void));
  246. static int ipv6_demand_conf __P((int));
  247. static int ipv6_active_pkt __P((u_char *, int));
  248. struct protent ipv6cp_protent = {
  249. PPP_IPV6CP,
  250. ipv6cp_init,
  251. ipv6cp_input,
  252. ipv6cp_protrej,
  253. ipv6cp_lowerup,
  254. ipv6cp_lowerdown,
  255. ipv6cp_open,
  256. ipv6cp_close,
  257. ipv6cp_printpkt,
  258. NULL,
  259. 0,
  260. "IPV6CP",
  261. "IPV6",
  262. ipv6cp_option_list,
  263. ipv6_check_options,
  264. ipv6_demand_conf,
  265. ipv6_active_pkt
  266. };
  267. static void ipv6cp_clear_addrs __P((int, eui64_t, eui64_t));
  268. static void ipv6cp_script __P((char *));
  269. static void ipv6cp_script_done __P((void *));
  270. /*
  271. * Lengths of configuration options.
  272. */
  273. #define CILEN_VOID 2
  274. #define CILEN_COMPRESS 4 /* length for RFC2023 compress opt. */
  275. #define CILEN_IFACEID 10 /* RFC2472, interface identifier */
  276. #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
  277. (x) == CONFNAK ? "NAK" : "REJ")
  278. /*
  279. * This state variable is used to ensure that we don't
  280. * run an ipcp-up/down script while one is already running.
  281. */
  282. static enum script_state {
  283. s_down,
  284. s_up,
  285. } ipv6cp_script_state;
  286. static pid_t ipv6cp_script_pid;
  287. /*
  288. * setifaceid - set the interface identifiers manually
  289. */
  290. static int
  291. setifaceid(argv)
  292. char **argv;
  293. {
  294. char *comma, *arg, c;
  295. ipv6cp_options *wo = &ipv6cp_wantoptions[0];
  296. struct in6_addr addr;
  297. static int prio_local, prio_remote;
  298. #define VALIDID(a) ( (((a).s6_addr32[0] == 0) && ((a).s6_addr32[1] == 0)) && \
  299. (((a).s6_addr32[2] != 0) || ((a).s6_addr32[3] != 0)) )
  300. arg = *argv;
  301. if ((comma = strchr(arg, ',')) == NULL)
  302. comma = arg + strlen(arg);
  303. /*
  304. * If comma first character, then no local identifier
  305. */
  306. if (comma != arg) {
  307. c = *comma;
  308. *comma = '\0';
  309. if (inet_pton(AF_INET6, arg, &addr) == 0 || !VALIDID(addr)) {
  310. option_error("Illegal interface identifier (local): %s", arg);
  311. return 0;
  312. }
  313. if (option_priority >= prio_local) {
  314. eui64_copy(addr.s6_addr32[2], wo->ourid);
  315. wo->opt_local = 1;
  316. prio_local = option_priority;
  317. }
  318. *comma = c;
  319. }
  320. /*
  321. * If comma last character, the no remote identifier
  322. */
  323. if (*comma != 0 && *++comma != '\0') {
  324. if (inet_pton(AF_INET6, comma, &addr) == 0 || !VALIDID(addr)) {
  325. option_error("Illegal interface identifier (remote): %s", comma);
  326. return 0;
  327. }
  328. if (option_priority >= prio_remote) {
  329. eui64_copy(addr.s6_addr32[2], wo->hisid);
  330. wo->opt_remote = 1;
  331. prio_remote = option_priority;
  332. }
  333. }
  334. if (override_value("+ipv6", option_priority, option_source))
  335. ipv6cp_protent.enabled_flag = 1;
  336. return 1;
  337. }
  338. char *llv6_ntoa(eui64_t ifaceid);
  339. static void
  340. printifaceid(opt, printer, arg)
  341. option_t *opt;
  342. void (*printer) __P((void *, char *, ...));
  343. void *arg;
  344. {
  345. ipv6cp_options *wo = &ipv6cp_wantoptions[0];
  346. if (wo->opt_local)
  347. printer(arg, "%s", llv6_ntoa(wo->ourid));
  348. printer(arg, ",");
  349. if (wo->opt_remote)
  350. printer(arg, "%s", llv6_ntoa(wo->hisid));
  351. }
  352. /*
  353. * Make a string representation of a network address.
  354. */
  355. char *
  356. llv6_ntoa(ifaceid)
  357. eui64_t ifaceid;
  358. {
  359. static char b[64];
  360. sprintf(b, "fe80::%s", eui64_ntoa(ifaceid));
  361. return b;
  362. }
  363. /*
  364. * ipv6cp_init - Initialize IPV6CP.
  365. */
  366. static void
  367. ipv6cp_init(unit)
  368. int unit;
  369. {
  370. fsm *f = &ipv6cp_fsm[unit];
  371. ipv6cp_options *wo = &ipv6cp_wantoptions[unit];
  372. ipv6cp_options *ao = &ipv6cp_allowoptions[unit];
  373. f->unit = unit;
  374. f->protocol = PPP_IPV6CP;
  375. f->callbacks = &ipv6cp_callbacks;
  376. fsm_init(&ipv6cp_fsm[unit]);
  377. memset(wo, 0, sizeof(*wo));
  378. memset(ao, 0, sizeof(*ao));
  379. wo->accept_local = 1;
  380. wo->neg_ifaceid = 1;
  381. ao->neg_ifaceid = 1;
  382. #ifdef IPV6CP_COMP
  383. wo->neg_vj = 1;
  384. ao->neg_vj = 1;
  385. wo->vj_protocol = IPV6CP_COMP;
  386. #endif
  387. }
  388. /*
  389. * ipv6cp_open - IPV6CP is allowed to come up.
  390. */
  391. static void
  392. ipv6cp_open(unit)
  393. int unit;
  394. {
  395. fsm_open(&ipv6cp_fsm[unit]);
  396. }
  397. /*
  398. * ipv6cp_close - Take IPV6CP down.
  399. */
  400. static void
  401. ipv6cp_close(unit, reason)
  402. int unit;
  403. char *reason;
  404. {
  405. fsm_close(&ipv6cp_fsm[unit], reason);
  406. }
  407. /*
  408. * ipv6cp_lowerup - The lower layer is up.
  409. */
  410. static void
  411. ipv6cp_lowerup(unit)
  412. int unit;
  413. {
  414. fsm_lowerup(&ipv6cp_fsm[unit]);
  415. }
  416. /*
  417. * ipv6cp_lowerdown - The lower layer is down.
  418. */
  419. static void
  420. ipv6cp_lowerdown(unit)
  421. int unit;
  422. {
  423. fsm_lowerdown(&ipv6cp_fsm[unit]);
  424. }
  425. /*
  426. * ipv6cp_input - Input IPV6CP packet.
  427. */
  428. static void
  429. ipv6cp_input(unit, p, len)
  430. int unit;
  431. u_char *p;
  432. int len;
  433. {
  434. fsm_input(&ipv6cp_fsm[unit], p, len);
  435. }
  436. /*
  437. * ipv6cp_protrej - A Protocol-Reject was received for IPV6CP.
  438. *
  439. * Pretend the lower layer went down, so we shut up.
  440. */
  441. static void
  442. ipv6cp_protrej(unit)
  443. int unit;
  444. {
  445. fsm_lowerdown(&ipv6cp_fsm[unit]);
  446. }
  447. /*
  448. * ipv6cp_resetci - Reset our CI.
  449. */
  450. static void
  451. ipv6cp_resetci(f)
  452. fsm *f;
  453. {
  454. ipv6cp_options *wo = &ipv6cp_wantoptions[f->unit];
  455. ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
  456. wo->req_ifaceid = wo->neg_ifaceid && ipv6cp_allowoptions[f->unit].neg_ifaceid;
  457. if (!wo->opt_local) {
  458. eui64_magic_nz(wo->ourid);
  459. }
  460. *go = *wo;
  461. eui64_zero(go->hisid); /* last proposed interface identifier */
  462. }
  463. /*
  464. * ipv6cp_cilen - Return length of our CI.
  465. */
  466. static int
  467. ipv6cp_cilen(f)
  468. fsm *f;
  469. {
  470. ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
  471. #define LENCIVJ(neg) (neg ? CILEN_COMPRESS : 0)
  472. #define LENCIIFACEID(neg) (neg ? CILEN_IFACEID : 0)
  473. return (LENCIIFACEID(go->neg_ifaceid) +
  474. LENCIVJ(go->neg_vj));
  475. }
  476. /*
  477. * ipv6cp_addci - Add our desired CIs to a packet.
  478. */
  479. static void
  480. ipv6cp_addci(f, ucp, lenp)
  481. fsm *f;
  482. u_char *ucp;
  483. int *lenp;
  484. {
  485. ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
  486. int len = *lenp;
  487. #define ADDCIVJ(opt, neg, val) \
  488. if (neg) { \
  489. int vjlen = CILEN_COMPRESS; \
  490. if (len >= vjlen) { \
  491. PUTCHAR(opt, ucp); \
  492. PUTCHAR(vjlen, ucp); \
  493. PUTSHORT(val, ucp); \
  494. len -= vjlen; \
  495. } else \
  496. neg = 0; \
  497. }
  498. #define ADDCIIFACEID(opt, neg, val1) \
  499. if (neg) { \
  500. int idlen = CILEN_IFACEID; \
  501. if (len >= idlen) { \
  502. PUTCHAR(opt, ucp); \
  503. PUTCHAR(idlen, ucp); \
  504. eui64_put(val1, ucp); \
  505. len -= idlen; \
  506. } else \
  507. neg = 0; \
  508. }
  509. ADDCIIFACEID(CI_IFACEID, go->neg_ifaceid, go->ourid);
  510. ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol);
  511. *lenp -= len;
  512. }
  513. /*
  514. * ipv6cp_ackci - Ack our CIs.
  515. *
  516. * Returns:
  517. * 0 - Ack was bad.
  518. * 1 - Ack was good.
  519. */
  520. static int
  521. ipv6cp_ackci(f, p, len)
  522. fsm *f;
  523. u_char *p;
  524. int len;
  525. {
  526. ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
  527. u_short cilen, citype, cishort;
  528. eui64_t ifaceid;
  529. /*
  530. * CIs must be in exactly the same order that we sent...
  531. * Check packet length and CI length at each step.
  532. * If we find any deviations, then this packet is bad.
  533. */
  534. #define ACKCIVJ(opt, neg, val) \
  535. if (neg) { \
  536. int vjlen = CILEN_COMPRESS; \
  537. if ((len -= vjlen) < 0) \
  538. goto bad; \
  539. GETCHAR(citype, p); \
  540. GETCHAR(cilen, p); \
  541. if (cilen != vjlen || \
  542. citype != opt) \
  543. goto bad; \
  544. GETSHORT(cishort, p); \
  545. if (cishort != val) \
  546. goto bad; \
  547. }
  548. #define ACKCIIFACEID(opt, neg, val1) \
  549. if (neg) { \
  550. int idlen = CILEN_IFACEID; \
  551. if ((len -= idlen) < 0) \
  552. goto bad; \
  553. GETCHAR(citype, p); \
  554. GETCHAR(cilen, p); \
  555. if (cilen != idlen || \
  556. citype != opt) \
  557. goto bad; \
  558. eui64_get(ifaceid, p); \
  559. if (! eui64_equals(val1, ifaceid)) \
  560. goto bad; \
  561. }
  562. ACKCIIFACEID(CI_IFACEID, go->neg_ifaceid, go->ourid);
  563. ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol);
  564. /*
  565. * If there are any remaining CIs, then this packet is bad.
  566. */
  567. if (len != 0)
  568. goto bad;
  569. return (1);
  570. bad:
  571. IPV6CPDEBUG(("ipv6cp_ackci: received bad Ack!"));
  572. return (0);
  573. }
  574. /*
  575. * ipv6cp_nakci - Peer has sent a NAK for some of our CIs.
  576. * This should not modify any state if the Nak is bad
  577. * or if IPV6CP is in the OPENED state.
  578. *
  579. * Returns:
  580. * 0 - Nak was bad.
  581. * 1 - Nak was good.
  582. */
  583. static int
  584. ipv6cp_nakci(f, p, len, treat_as_reject)
  585. fsm *f;
  586. u_char *p;
  587. int len;
  588. int treat_as_reject;
  589. {
  590. ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
  591. u_char citype, cilen, *next;
  592. u_short cishort;
  593. eui64_t ifaceid;
  594. ipv6cp_options no; /* options we've seen Naks for */
  595. ipv6cp_options try; /* options to request next time */
  596. BZERO(&no, sizeof(no));
  597. try = *go;
  598. /*
  599. * Any Nak'd CIs must be in exactly the same order that we sent.
  600. * Check packet length and CI length at each step.
  601. * If we find any deviations, then this packet is bad.
  602. */
  603. #define NAKCIIFACEID(opt, neg, code) \
  604. if (go->neg && \
  605. len >= (cilen = CILEN_IFACEID) && \
  606. p[1] == cilen && \
  607. p[0] == opt) { \
  608. len -= cilen; \
  609. INCPTR(2, p); \
  610. eui64_get(ifaceid, p); \
  611. no.neg = 1; \
  612. code \
  613. }
  614. #define NAKCIVJ(opt, neg, code) \
  615. if (go->neg && \
  616. ((cilen = p[1]) == CILEN_COMPRESS) && \
  617. len >= cilen && \
  618. p[0] == opt) { \
  619. len -= cilen; \
  620. INCPTR(2, p); \
  621. GETSHORT(cishort, p); \
  622. no.neg = 1; \
  623. code \
  624. }
  625. /*
  626. * Accept the peer's idea of {our,his} interface identifier, if different
  627. * from our idea, only if the accept_{local,remote} flag is set.
  628. */
  629. NAKCIIFACEID(CI_IFACEID, neg_ifaceid,
  630. if (treat_as_reject) {
  631. try.neg_ifaceid = 0;
  632. } else if (go->accept_local) {
  633. while (eui64_iszero(ifaceid) ||
  634. eui64_equals(ifaceid, go->hisid)) /* bad luck */
  635. eui64_magic(ifaceid);
  636. try.ourid = ifaceid;
  637. IPV6CPDEBUG(("local LL address %s", llv6_ntoa(ifaceid)));
  638. }
  639. );
  640. #ifdef IPV6CP_COMP
  641. NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
  642. {
  643. if (cishort == IPV6CP_COMP && !treat_as_reject) {
  644. try.vj_protocol = cishort;
  645. } else {
  646. try.neg_vj = 0;
  647. }
  648. }
  649. );
  650. #else
  651. NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
  652. {
  653. try.neg_vj = 0;
  654. }
  655. );
  656. #endif
  657. /*
  658. * There may be remaining CIs, if the peer is requesting negotiation
  659. * on an option that we didn't include in our request packet.
  660. * If they want to negotiate about interface identifier, we comply.
  661. * If they want us to ask for compression, we refuse.
  662. */
  663. while (len >= CILEN_VOID) {
  664. GETCHAR(citype, p);
  665. GETCHAR(cilen, p);
  666. if ( cilen < CILEN_VOID || (len -= cilen) < 0 )
  667. goto bad;
  668. next = p + cilen - 2;
  669. switch (citype) {
  670. case CI_COMPRESSTYPE:
  671. if (go->neg_vj || no.neg_vj ||
  672. (cilen != CILEN_COMPRESS))
  673. goto bad;
  674. no.neg_vj = 1;
  675. break;
  676. case CI_IFACEID:
  677. if (go->neg_ifaceid || no.neg_ifaceid || cilen != CILEN_IFACEID)
  678. goto bad;
  679. try.neg_ifaceid = 1;
  680. eui64_get(ifaceid, p);
  681. if (go->accept_local) {
  682. while (eui64_iszero(ifaceid) ||
  683. eui64_equals(ifaceid, go->hisid)) /* bad luck */
  684. eui64_magic(ifaceid);
  685. try.ourid = ifaceid;
  686. }
  687. no.neg_ifaceid = 1;
  688. break;
  689. }
  690. p = next;
  691. }
  692. /* If there is still anything left, this packet is bad. */
  693. if (len != 0)
  694. goto bad;
  695. /*
  696. * OK, the Nak is good. Now we can update state.
  697. */
  698. if (f->state != OPENED)
  699. *go = try;
  700. return 1;
  701. bad:
  702. IPV6CPDEBUG(("ipv6cp_nakci: received bad Nak!"));
  703. return 0;
  704. }
  705. /*
  706. * ipv6cp_rejci - Reject some of our CIs.
  707. */
  708. static int
  709. ipv6cp_rejci(f, p, len)
  710. fsm *f;
  711. u_char *p;
  712. int len;
  713. {
  714. ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
  715. u_char cilen;
  716. u_short cishort;
  717. eui64_t ifaceid;
  718. ipv6cp_options try; /* options to request next time */
  719. try = *go;
  720. /*
  721. * Any Rejected CIs must be in exactly the same order that we sent.
  722. * Check packet length and CI length at each step.
  723. * If we find any deviations, then this packet is bad.
  724. */
  725. #define REJCIIFACEID(opt, neg, val1) \
  726. if (go->neg && \
  727. len >= (cilen = CILEN_IFACEID) && \
  728. p[1] == cilen && \
  729. p[0] == opt) { \
  730. len -= cilen; \
  731. INCPTR(2, p); \
  732. eui64_get(ifaceid, p); \
  733. /* Check rejected value. */ \
  734. if (! eui64_equals(ifaceid, val1)) \
  735. goto bad; \
  736. try.neg = 0; \
  737. }
  738. #define REJCIVJ(opt, neg, val) \
  739. if (go->neg && \
  740. p[1] == CILEN_COMPRESS && \
  741. len >= p[1] && \
  742. p[0] == opt) { \
  743. len -= p[1]; \
  744. INCPTR(2, p); \
  745. GETSHORT(cishort, p); \
  746. /* Check rejected value. */ \
  747. if (cishort != val) \
  748. goto bad; \
  749. try.neg = 0; \
  750. }
  751. REJCIIFACEID(CI_IFACEID, neg_ifaceid, go->ourid);
  752. REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol);
  753. /*
  754. * If there are any remaining CIs, then this packet is bad.
  755. */
  756. if (len != 0)
  757. goto bad;
  758. /*
  759. * Now we can update state.
  760. */
  761. if (f->state != OPENED)
  762. *go = try;
  763. return 1;
  764. bad:
  765. IPV6CPDEBUG(("ipv6cp_rejci: received bad Reject!"));
  766. return 0;
  767. }
  768. /*
  769. * ipv6cp_reqci - Check the peer's requested CIs and send appropriate response.
  770. *
  771. * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
  772. * appropriately. If reject_if_disagree is non-zero, doesn't return
  773. * CONFNAK; returns CONFREJ if it can't return CONFACK.
  774. */
  775. static int
  776. ipv6cp_reqci(f, inp, len, reject_if_disagree)
  777. fsm *f;
  778. u_char *inp; /* Requested CIs */
  779. int *len; /* Length of requested CIs */
  780. int reject_if_disagree;
  781. {
  782. ipv6cp_options *wo = &ipv6cp_wantoptions[f->unit];
  783. ipv6cp_options *ho = &ipv6cp_hisoptions[f->unit];
  784. ipv6cp_options *ao = &ipv6cp_allowoptions[f->unit];
  785. ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
  786. u_char *cip, *next; /* Pointer to current and next CIs */
  787. u_short cilen, citype; /* Parsed len, type */
  788. u_short cishort; /* Parsed short value */
  789. eui64_t ifaceid; /* Parsed interface identifier */
  790. int rc = CONFACK; /* Final packet return code */
  791. int orc; /* Individual option return code */
  792. u_char *p; /* Pointer to next char to parse */
  793. u_char *ucp = inp; /* Pointer to current output char */
  794. int l = *len; /* Length left */
  795. /*
  796. * Reset all his options.
  797. */
  798. BZERO(ho, sizeof(*ho));
  799. /*
  800. * Process all his options.
  801. */
  802. next = inp;
  803. while (l) {
  804. orc = CONFACK; /* Assume success */
  805. cip = p = next; /* Remember begining of CI */
  806. if (l < 2 || /* Not enough data for CI header or */
  807. p[1] < 2 || /* CI length too small or */
  808. p[1] > l) { /* CI length too big? */
  809. IPV6CPDEBUG(("ipv6cp_reqci: bad CI length!"));
  810. orc = CONFREJ; /* Reject bad CI */
  811. cilen = l; /* Reject till end of packet */
  812. l = 0; /* Don't loop again */
  813. goto endswitch;
  814. }
  815. GETCHAR(citype, p); /* Parse CI type */
  816. GETCHAR(cilen, p); /* Parse CI length */
  817. l -= cilen; /* Adjust remaining length */
  818. next += cilen; /* Step to next CI */
  819. switch (citype) { /* Check CI type */
  820. case CI_IFACEID:
  821. IPV6CPDEBUG(("ipv6cp: received interface identifier "));
  822. if (!ao->neg_ifaceid ||
  823. cilen != CILEN_IFACEID) { /* Check CI length */
  824. orc = CONFREJ; /* Reject CI */
  825. break;
  826. }
  827. /*
  828. * If he has no interface identifier, or if we both have same
  829. * identifier then NAK it with new idea.
  830. * In particular, if we don't know his identifier, but he does,
  831. * then accept it.
  832. */
  833. eui64_get(ifaceid, p);
  834. IPV6CPDEBUG(("(%s)", llv6_ntoa(ifaceid)));
  835. if (eui64_iszero(ifaceid) && eui64_iszero(go->ourid)) {
  836. orc = CONFREJ; /* Reject CI */
  837. break;
  838. }
  839. if (!eui64_iszero(wo->hisid) &&
  840. !eui64_equals(ifaceid, wo->hisid) &&
  841. eui64_iszero(go->hisid)) {
  842. orc = CONFNAK;
  843. ifaceid = wo->hisid;
  844. go->hisid = ifaceid;
  845. DECPTR(sizeof(ifaceid), p);
  846. eui64_put(ifaceid, p);
  847. } else
  848. if (eui64_iszero(ifaceid) || eui64_equals(ifaceid, go->ourid)) {
  849. orc = CONFNAK;
  850. if (eui64_iszero(go->hisid)) /* first time, try option */
  851. ifaceid = wo->hisid;
  852. while (eui64_iszero(ifaceid) ||
  853. eui64_equals(ifaceid, go->ourid)) /* bad luck */
  854. eui64_magic(ifaceid);
  855. go->hisid = ifaceid;
  856. DECPTR(sizeof(ifaceid), p);
  857. eui64_put(ifaceid, p);
  858. }
  859. ho->neg_ifaceid = 1;
  860. ho->hisid = ifaceid;
  861. break;
  862. case CI_COMPRESSTYPE:
  863. IPV6CPDEBUG(("ipv6cp: received COMPRESSTYPE "));
  864. if (!ao->neg_vj ||
  865. (cilen != CILEN_COMPRESS)) {
  866. orc = CONFREJ;
  867. break;
  868. }
  869. GETSHORT(cishort, p);
  870. IPV6CPDEBUG(("(%d)", cishort));
  871. #ifdef IPV6CP_COMP
  872. if (!(cishort == IPV6CP_COMP)) {
  873. orc = CONFREJ;
  874. break;
  875. }
  876. ho->neg_vj = 1;
  877. ho->vj_protocol = cishort;
  878. break;
  879. #else
  880. orc = CONFREJ;
  881. break;
  882. #endif
  883. default:
  884. orc = CONFREJ;
  885. break;
  886. }
  887. endswitch:
  888. IPV6CPDEBUG((" (%s)\n", CODENAME(orc)));
  889. if (orc == CONFACK && /* Good CI */
  890. rc != CONFACK) /* but prior CI wasnt? */
  891. continue; /* Don't send this one */
  892. if (orc == CONFNAK) { /* Nak this CI? */
  893. if (reject_if_disagree) /* Getting fed up with sending NAKs? */
  894. orc = CONFREJ; /* Get tough if so */
  895. else {
  896. if (rc == CONFREJ) /* Rejecting prior CI? */
  897. continue; /* Don't send this one */
  898. if (rc == CONFACK) { /* Ack'd all prior CIs? */
  899. rc = CONFNAK; /* Not anymore... */
  900. ucp = inp; /* Backup */
  901. }
  902. }
  903. }
  904. if (orc == CONFREJ && /* Reject this CI */
  905. rc != CONFREJ) { /* but no prior ones? */
  906. rc = CONFREJ;
  907. ucp = inp; /* Backup */
  908. }
  909. /* Need to move CI? */
  910. if (ucp != cip)
  911. BCOPY(cip, ucp, cilen); /* Move it */
  912. /* Update output pointer */
  913. INCPTR(cilen, ucp);
  914. }
  915. /*
  916. * If we aren't rejecting this packet, and we want to negotiate
  917. * their identifier and they didn't send their identifier, then we
  918. * send a NAK with a CI_IFACEID option appended. We assume the
  919. * input buffer is long enough that we can append the extra
  920. * option safely.
  921. */
  922. if (rc != CONFREJ && !ho->neg_ifaceid &&
  923. wo->req_ifaceid && !reject_if_disagree) {
  924. if (rc == CONFACK) {
  925. rc = CONFNAK;
  926. ucp = inp; /* reset pointer */
  927. wo->req_ifaceid = 0; /* don't ask again */
  928. }
  929. PUTCHAR(CI_IFACEID, ucp);
  930. PUTCHAR(CILEN_IFACEID, ucp);
  931. eui64_put(wo->hisid, ucp);
  932. }
  933. *len = ucp - inp; /* Compute output length */
  934. IPV6CPDEBUG(("ipv6cp: returning Configure-%s", CODENAME(rc)));
  935. return (rc); /* Return final code */
  936. }
  937. /*
  938. * ipv6_check_options - check that any IP-related options are OK,
  939. * and assign appropriate defaults.
  940. */
  941. static void
  942. ipv6_check_options()
  943. {
  944. ipv6cp_options *wo = &ipv6cp_wantoptions[0];
  945. if (!ipv6cp_protent.enabled_flag)
  946. return;
  947. /*
  948. * Persistent link-local id is only used when user has not explicitly
  949. * configure/hard-code the id
  950. */
  951. if ((wo->use_persistent) && (!wo->opt_local) && (!wo->opt_remote)) {
  952. /*
  953. * On systems where there are no Ethernet interfaces used, there
  954. * may be other ways to obtain a persistent id. Right now, it
  955. * will fall back to using magic [see eui64_magic] below when
  956. * an EUI-48 from MAC address can't be obtained. Other possibilities
  957. * include obtaining EEPROM serial numbers, or some other unique
  958. * yet persistent number. On Sparc platforms, this is possible,
  959. * but too bad there's no standards yet for x86 machines.
  960. */
  961. if (ether_to_eui64(&wo->ourid)) {
  962. wo->opt_local = 1;
  963. }
  964. }
  965. if (!wo->opt_local) { /* init interface identifier */
  966. if (wo->use_ip && eui64_iszero(wo->ourid)) {
  967. eui64_setlo32(wo->ourid, ntohl(ipcp_wantoptions[0].ouraddr));
  968. if (!eui64_iszero(wo->ourid))
  969. wo->opt_local = 1;
  970. }
  971. while (eui64_iszero(wo->ourid))
  972. eui64_magic(wo->ourid);
  973. }
  974. if (!wo->opt_remote) {
  975. if (wo->use_ip && eui64_iszero(wo->hisid)) {
  976. eui64_setlo32(wo->hisid, ntohl(ipcp_wantoptions[0].hisaddr));
  977. if (!eui64_iszero(wo->hisid))
  978. wo->opt_remote = 1;
  979. }
  980. }
  981. if (demand && (eui64_iszero(wo->ourid) || eui64_iszero(wo->hisid))) {
  982. option_error("local/remote LL address required for demand-dialling\n");
  983. exit(EXIT_OPTION_ERROR);
  984. }
  985. }
  986. /*
  987. * ipv6_demand_conf - configure the interface as though
  988. * IPV6CP were up, for use with dial-on-demand.
  989. */
  990. static int
  991. ipv6_demand_conf(u)
  992. int u;
  993. {
  994. ipv6cp_options *wo = &ipv6cp_wantoptions[u];
  995. if (!sif6up(u))
  996. return 0;
  997. if (!sif6addr(u, wo->ourid, wo->hisid))
  998. return 0;
  999. #if !defined(__linux__) && !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
  1000. if (!sifup(u))
  1001. return 0;
  1002. #endif
  1003. if (!sifnpmode(u, PPP_IPV6, NPMODE_QUEUE))
  1004. return 0;
  1005. notice("ipv6_demand_conf");
  1006. notice("local LL address %s", llv6_ntoa(wo->ourid));
  1007. notice("remote LL address %s", llv6_ntoa(wo->hisid));
  1008. return 1;
  1009. }
  1010. /*
  1011. * ipv6cp_up - IPV6CP has come UP.
  1012. *
  1013. * Configure the IPv6 network interface appropriately and bring it up.
  1014. */
  1015. static void
  1016. ipv6cp_up(f)
  1017. fsm *f;
  1018. {
  1019. ipv6cp_options *ho = &ipv6cp_hisoptions[f->unit];
  1020. ipv6cp_options *go = &ipv6cp_gotoptions[f->unit];
  1021. ipv6cp_options *wo = &ipv6cp_wantoptions[f->unit];
  1022. IPV6CPDEBUG(("ipv6cp: up"));
  1023. /*
  1024. * We must have a non-zero LL address for both ends of the link.
  1025. */
  1026. if (!ho->neg_ifaceid)
  1027. ho->hisid = wo->hisid;
  1028. if(!no_ifaceid_neg) {
  1029. if (eui64_iszero(ho->hisid)) {
  1030. error("Could not determine remote LL address");
  1031. ipv6cp_close(f->unit, "Could not determine remote LL address");
  1032. return;
  1033. }
  1034. if (eui64_iszero(go->ourid)) {
  1035. error("Could not determine local LL address");
  1036. ipv6cp_close(f->unit, "Could not determine local LL address");
  1037. return;
  1038. }
  1039. if (eui64_equals(go->ourid, ho->hisid)) {
  1040. error("local and remote LL addresses are equal");
  1041. ipv6cp_close(f->unit, "local and remote LL addresses are equal");
  1042. return;
  1043. }
  1044. }
  1045. script_setenv("LLLOCAL", llv6_ntoa(go->ourid), 0);
  1046. script_setenv("LLREMOTE", llv6_ntoa(ho->hisid), 0);
  1047. #ifdef IPV6CP_COMP
  1048. /* set tcp compression */
  1049. sif6comp(f->unit, ho->neg_vj);
  1050. #endif
  1051. /*
  1052. * If we are doing dial-on-demand, the interface is already
  1053. * configured, so we put out any saved-up packets, then set the
  1054. * interface to pass IPv6 packets.
  1055. */
  1056. if (demand) {
  1057. if (! eui64_equals(go->ourid, wo->ourid) ||
  1058. ! eui64_equals(ho->hisid, wo->hisid)) {
  1059. if (! eui64_equals(go->ourid, wo->ourid))
  1060. warn("Local LL address changed to %s",
  1061. llv6_ntoa(go->ourid));
  1062. if (! eui64_equals(ho->hisid, wo->hisid))
  1063. warn("Remote LL address changed to %s",
  1064. llv6_ntoa(ho->hisid));
  1065. ipv6cp_clear_addrs(f->unit, go->ourid, ho->hisid);
  1066. /* Set the interface to the new addresses */
  1067. if (!sif6addr(f->unit, go->ourid, ho->hisid)) {
  1068. if (debug)
  1069. warn("sif6addr failed");
  1070. ipv6cp_close(f->unit, "Interface configuration failed");
  1071. return;
  1072. }
  1073. }
  1074. demand_rexmit(PPP_IPV6);
  1075. sifnpmode(f->unit, PPP_IPV6, NPMODE_PASS);
  1076. } else {
  1077. /* bring the interface up for IPv6 */
  1078. if (!sif6up(f->unit)) {
  1079. if (debug)
  1080. warn("sif6up failed (IPV6)");
  1081. ipv6cp_close(f->unit, "Interface configuration failed");
  1082. return;
  1083. }
  1084. if (!sif6addr(f->unit, go->ourid, ho->hisid)) {
  1085. if (debug)
  1086. warn("sif6addr failed");
  1087. ipv6cp_close(f->unit, "Interface configuration failed");
  1088. return;
  1089. }
  1090. sifnpmode(f->unit, PPP_IPV6, NPMODE_PASS);
  1091. notice("local LL address %s", llv6_ntoa(go->ourid));
  1092. notice("remote LL address %s", llv6_ntoa(ho->hisid));
  1093. }
  1094. np_up(f->unit, PPP_IPV6);
  1095. ipv6cp_is_up = 1;
  1096. notify(ipv6_up_notifier, 0);
  1097. if (ipv6_up_hook)
  1098. ipv6_up_hook();
  1099. /*
  1100. * Execute the ipv6-up script, like this:
  1101. * /etc/ppp/ipv6-up interface tty speed local-LL remote-LL
  1102. */
  1103. if (ipv6cp_script_state == s_down && ipv6cp_script_pid == 0) {
  1104. ipv6cp_script_state = s_up;
  1105. ipv6cp_script(_PATH_IPV6UP);
  1106. }
  1107. }
  1108. /*
  1109. * ipv6cp_down - IPV6CP has gone DOWN.
  1110. *
  1111. * Take the IPv6 network interface down, clear its addresses
  1112. * and delete routes through it.
  1113. */
  1114. static void
  1115. ipv6cp_down(f)
  1116. fsm *f;
  1117. {
  1118. IPV6CPDEBUG(("ipv6cp: down"));
  1119. update_link_stats(f->unit);
  1120. notify(ipv6_down_notifier, 0);
  1121. if (ipv6_down_hook)
  1122. ipv6_down_hook();
  1123. if (ipv6cp_is_up) {
  1124. ipv6cp_is_up = 0;
  1125. np_down(f->unit, PPP_IPV6);
  1126. }
  1127. #ifdef IPV6CP_COMP
  1128. sif6comp(f->unit, 0);
  1129. #endif
  1130. /*
  1131. * If we are doing dial-on-demand, set the interface
  1132. * to queue up outgoing packets (for now).
  1133. */
  1134. if (demand) {
  1135. sifnpmode(f->unit, PPP_IPV6, NPMODE_QUEUE);
  1136. } else {
  1137. sifnpmode(f->unit, PPP_IPV6, NPMODE_DROP);
  1138. #if !defined(__linux__) && !(defined(SVR4) && (defined(SNI) || defined(__USLC)))
  1139. sif6down(f->unit);
  1140. #endif
  1141. ipv6cp_clear_addrs(f->unit,
  1142. ipv6cp_gotoptions[f->unit].ourid,
  1143. ipv6cp_hisoptions[f->unit].hisid);
  1144. #if defined(__linux__)
  1145. sif6down(f->unit);
  1146. #elif defined(SVR4) && (defined(SNI) || defined(__USLC))
  1147. sifdown(f->unit);
  1148. #endif
  1149. }
  1150. /* Execute the ipv6-down script */
  1151. if (ipv6cp_script_state == s_up && ipv6cp_script_pid == 0) {
  1152. ipv6cp_script_state = s_down;
  1153. ipv6cp_script(_PATH_IPV6DOWN);
  1154. }
  1155. }
  1156. /*
  1157. * ipv6cp_clear_addrs() - clear the interface addresses, routes,
  1158. * proxy neighbour discovery entries, etc.
  1159. */
  1160. static void
  1161. ipv6cp_clear_addrs(unit, ourid, hisid)
  1162. int unit;
  1163. eui64_t ourid;
  1164. eui64_t hisid;
  1165. {
  1166. cif6addr(unit, ourid, hisid);
  1167. }
  1168. /*
  1169. * ipv6cp_finished - possibly shut down the lower layers.
  1170. */
  1171. static void
  1172. ipv6cp_finished(f)
  1173. fsm *f;
  1174. {
  1175. np_finished(f->unit, PPP_IPV6);
  1176. }
  1177. /*
  1178. * ipv6cp_script_done - called when the ipv6-up or ipv6-down script
  1179. * has finished.
  1180. */
  1181. static void
  1182. ipv6cp_script_done(arg)
  1183. void *arg;
  1184. {
  1185. ipv6cp_script_pid = 0;
  1186. switch (ipv6cp_script_state) {
  1187. case s_up:
  1188. if (ipv6cp_fsm[0].state != OPENED) {
  1189. ipv6cp_script_state = s_down;
  1190. ipv6cp_script(_PATH_IPV6DOWN);
  1191. }
  1192. break;
  1193. case s_down:
  1194. if (ipv6cp_fsm[0].state == OPENED) {
  1195. ipv6cp_script_state = s_up;
  1196. ipv6cp_script(_PATH_IPV6UP);
  1197. }
  1198. break;
  1199. }
  1200. }
  1201. /*
  1202. * ipv6cp_script - Execute a script with arguments
  1203. * interface-name tty-name speed local-LL remote-LL.
  1204. */
  1205. static void
  1206. ipv6cp_script(script)
  1207. char *script;
  1208. {
  1209. char strspeed[32], strlocal[32], strremote[32];
  1210. char *argv[8];
  1211. sprintf(strspeed, "%d", baud_rate);
  1212. strcpy(strlocal, llv6_ntoa(ipv6cp_gotoptions[0].ourid));
  1213. strcpy(strremote, llv6_ntoa(ipv6cp_hisoptions[0].hisid));
  1214. argv[0] = script;
  1215. argv[1] = ifname;
  1216. argv[2] = devnam;
  1217. argv[3] = strspeed;
  1218. argv[4] = strlocal;
  1219. argv[5] = strremote;
  1220. argv[6] = ipparam;
  1221. argv[7] = NULL;
  1222. ipv6cp_script_pid = run_program(script, argv, 0, ipv6cp_script_done,
  1223. NULL, 0);
  1224. }
  1225. /*
  1226. * ipv6cp_printpkt - print the contents of an IPV6CP packet.
  1227. */
  1228. static char *ipv6cp_codenames[] = {
  1229. "ConfReq", "ConfAck", "ConfNak", "ConfRej",
  1230. "TermReq", "TermAck", "CodeRej"
  1231. };
  1232. static int
  1233. ipv6cp_printpkt(p, plen, printer, arg)
  1234. u_char *p;
  1235. int plen;
  1236. void (*printer) __P((void *, char *, ...));
  1237. void *arg;
  1238. {
  1239. int code, id, len, olen;
  1240. u_char *pstart, *optend;
  1241. u_short cishort;
  1242. eui64_t ifaceid;
  1243. if (plen < HEADERLEN)
  1244. return 0;
  1245. pstart = p;
  1246. GETCHAR(code, p);
  1247. GETCHAR(id, p);
  1248. GETSHORT(len, p);
  1249. if (len < HEADERLEN || len > plen)
  1250. return 0;
  1251. if (code >= 1 && code <= sizeof(ipv6cp_codenames) / sizeof(char *))
  1252. printer(arg, " %s", ipv6cp_codenames[code-1]);
  1253. else
  1254. printer(arg, " code=0x%x", code);
  1255. printer(arg, " id=0x%x", id);
  1256. len -= HEADERLEN;
  1257. switch (code) {
  1258. case CONFREQ:
  1259. case CONFACK:
  1260. case CONFNAK:
  1261. case CONFREJ:
  1262. /* print option list */
  1263. while (len >= 2) {
  1264. GETCHAR(code, p);
  1265. GETCHAR(olen, p);
  1266. p -= 2;
  1267. if (olen < 2 || olen > len) {
  1268. break;
  1269. }
  1270. printer(arg, " <");
  1271. len -= olen;
  1272. optend = p + olen;
  1273. switch (code) {
  1274. case CI_COMPRESSTYPE:
  1275. if (olen >= CILEN_COMPRESS) {
  1276. p += 2;
  1277. GETSHORT(cishort, p);
  1278. printer(arg, "compress ");
  1279. printer(arg, "0x%x", cishort);
  1280. }
  1281. break;
  1282. case CI_IFACEID:
  1283. if (olen == CILEN_IFACEID) {
  1284. p += 2;
  1285. eui64_get(ifaceid, p);
  1286. printer(arg, "addr %s", llv6_ntoa(ifaceid));
  1287. }
  1288. break;
  1289. }
  1290. while (p < optend) {
  1291. GETCHAR(code, p);
  1292. printer(arg, " %.2x", code);
  1293. }
  1294. printer(arg, ">");
  1295. }
  1296. break;
  1297. case TERMACK:
  1298. case TERMREQ:
  1299. if (len > 0 && *p >= ' ' && *p < 0x7f) {
  1300. printer(arg, " ");
  1301. print_string((char *)p, len, printer, arg);
  1302. p += len;
  1303. len = 0;
  1304. }
  1305. break;
  1306. }
  1307. /* print the rest of the bytes in the packet */
  1308. for (; len > 0; --len) {
  1309. GETCHAR(code, p);
  1310. printer(arg, " %.2x", code);
  1311. }
  1312. return p - pstart;
  1313. }
  1314. /*
  1315. * ipv6_active_pkt - see if this IP packet is worth bringing the link up for.
  1316. * We don't bring the link up for IP fragments or for TCP FIN packets
  1317. * with no data.
  1318. */
  1319. #define IP6_HDRLEN 40 /* bytes */
  1320. #define IP6_NHDR_FRAG 44 /* fragment IPv6 header */
  1321. #define TCP_HDRLEN 20
  1322. #define TH_FIN 0x01
  1323. /*
  1324. * We use these macros because the IP header may be at an odd address,
  1325. * and some compilers might use word loads to get th_off or ip_hl.
  1326. */
  1327. #define get_ip6nh(x) (((unsigned char *)(x))[6])
  1328. #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
  1329. #define get_tcpflags(x) (((unsigned char *)(x))[13])
  1330. static int
  1331. ipv6_active_pkt(pkt, len)
  1332. u_char *pkt;
  1333. int len;
  1334. {
  1335. u_char *tcp;
  1336. len -= PPP_HDRLEN;
  1337. pkt += PPP_HDRLEN;
  1338. if (len < IP6_HDRLEN)
  1339. return 0;
  1340. if (get_ip6nh(pkt) == IP6_NHDR_FRAG)
  1341. return 0;
  1342. if (get_ip6nh(pkt) != IPPROTO_TCP)
  1343. return 1;
  1344. if (len < IP6_HDRLEN + TCP_HDRLEN)
  1345. return 0;
  1346. tcp = pkt + IP6_HDRLEN;
  1347. if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == IP6_HDRLEN + get_tcpoff(tcp) * 4)
  1348. return 0;
  1349. return 1;
  1350. }