ndisc.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. /*
  2. * Neighbour Discovery for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Mike Shaver <shaver@ingenia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. /*
  15. * Changes:
  16. *
  17. * Pierre Ynard : export userland ND options
  18. * through netlink (RDNSS support)
  19. * Lars Fenneberg : fixed MTU setting on receipt
  20. * of an RA.
  21. * Janos Farkas : kmalloc failure checks
  22. * Alexey Kuznetsov : state machine reworked
  23. * and moved to net/core.
  24. * Pekka Savola : RFC2461 validation
  25. * YOSHIFUJI Hideaki @USAGI : Verify ND options properly
  26. */
  27. /* Set to 3 to get tracing... */
  28. #define ND_DEBUG 1
  29. #define ND_PRINTK(fmt, args...) do { if (net_ratelimit()) { printk(fmt, ## args); } } while(0)
  30. #define ND_NOPRINTK(x...) do { ; } while(0)
  31. #define ND_PRINTK0 ND_PRINTK
  32. #define ND_PRINTK1 ND_NOPRINTK
  33. #define ND_PRINTK2 ND_NOPRINTK
  34. #define ND_PRINTK3 ND_NOPRINTK
  35. #if ND_DEBUG >= 1
  36. #undef ND_PRINTK1
  37. #define ND_PRINTK1 ND_PRINTK
  38. #endif
  39. #if ND_DEBUG >= 2
  40. #undef ND_PRINTK2
  41. #define ND_PRINTK2 ND_PRINTK
  42. #endif
  43. #if ND_DEBUG >= 3
  44. #undef ND_PRINTK3
  45. #define ND_PRINTK3 ND_PRINTK
  46. #endif
  47. #include <linux/module.h>
  48. #include <linux/errno.h>
  49. #include <linux/types.h>
  50. #include <linux/socket.h>
  51. #include <linux/sockios.h>
  52. #include <linux/sched.h>
  53. #include <linux/net.h>
  54. #include <linux/in6.h>
  55. #include <linux/route.h>
  56. #include <linux/init.h>
  57. #include <linux/rcupdate.h>
  58. #ifdef CONFIG_SYSCTL
  59. #include <linux/sysctl.h>
  60. #endif
  61. #include <linux/if_addr.h>
  62. #include <linux/if_arp.h>
  63. #include <linux/ipv6.h>
  64. #include <linux/icmpv6.h>
  65. #include <linux/jhash.h>
  66. #include <net/sock.h>
  67. #include <net/snmp.h>
  68. #include <net/ipv6.h>
  69. #include <net/protocol.h>
  70. #include <net/ndisc.h>
  71. #include <net/ip6_route.h>
  72. #include <net/addrconf.h>
  73. #include <net/icmp.h>
  74. #include <net/netlink.h>
  75. #include <linux/rtnetlink.h>
  76. #include <net/flow.h>
  77. #include <net/ip6_checksum.h>
  78. #include <linux/proc_fs.h>
  79. #include <linux/netfilter.h>
  80. #include <linux/netfilter_ipv6.h>
  81. static struct socket *ndisc_socket;
  82. static u32 ndisc_hash(const void *pkey, const struct net_device *dev);
  83. static int ndisc_constructor(struct neighbour *neigh);
  84. static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb);
  85. static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb);
  86. static int pndisc_constructor(struct pneigh_entry *n);
  87. static void pndisc_destructor(struct pneigh_entry *n);
  88. static void pndisc_redo(struct sk_buff *skb);
  89. static struct neigh_ops ndisc_generic_ops = {
  90. .family = AF_INET6,
  91. .solicit = ndisc_solicit,
  92. .error_report = ndisc_error_report,
  93. .output = neigh_resolve_output,
  94. .connected_output = neigh_connected_output,
  95. .hh_output = dev_queue_xmit,
  96. .queue_xmit = dev_queue_xmit,
  97. };
  98. static struct neigh_ops ndisc_hh_ops = {
  99. .family = AF_INET6,
  100. .solicit = ndisc_solicit,
  101. .error_report = ndisc_error_report,
  102. .output = neigh_resolve_output,
  103. .connected_output = neigh_resolve_output,
  104. .hh_output = dev_queue_xmit,
  105. .queue_xmit = dev_queue_xmit,
  106. };
  107. static struct neigh_ops ndisc_direct_ops = {
  108. .family = AF_INET6,
  109. .output = dev_queue_xmit,
  110. .connected_output = dev_queue_xmit,
  111. .hh_output = dev_queue_xmit,
  112. .queue_xmit = dev_queue_xmit,
  113. };
  114. struct neigh_table nd_tbl = {
  115. .family = AF_INET6,
  116. .entry_size = sizeof(struct neighbour) + sizeof(struct in6_addr),
  117. .key_len = sizeof(struct in6_addr),
  118. .hash = ndisc_hash,
  119. .constructor = ndisc_constructor,
  120. .pconstructor = pndisc_constructor,
  121. .pdestructor = pndisc_destructor,
  122. .proxy_redo = pndisc_redo,
  123. .id = "ndisc_cache",
  124. .parms = {
  125. .tbl = &nd_tbl,
  126. .base_reachable_time = 30 * HZ,
  127. .retrans_time = 1 * HZ,
  128. .gc_staletime = 60 * HZ,
  129. .reachable_time = 30 * HZ,
  130. .delay_probe_time = 5 * HZ,
  131. .queue_len = 3,
  132. .ucast_probes = 3,
  133. .mcast_probes = 3,
  134. .anycast_delay = 1 * HZ,
  135. .proxy_delay = (8 * HZ) / 10,
  136. .proxy_qlen = 64,
  137. },
  138. .gc_interval = 30 * HZ,
  139. .gc_thresh1 = 128,
  140. .gc_thresh2 = 512,
  141. .gc_thresh3 = 1024,
  142. };
  143. /* ND options */
  144. struct ndisc_options {
  145. struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
  146. #ifdef CONFIG_IPV6_ROUTE_INFO
  147. struct nd_opt_hdr *nd_opts_ri;
  148. struct nd_opt_hdr *nd_opts_ri_end;
  149. #endif
  150. struct nd_opt_hdr *nd_useropts;
  151. struct nd_opt_hdr *nd_useropts_end;
  152. };
  153. #define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
  154. #define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
  155. #define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
  156. #define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
  157. #define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
  158. #define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
  159. #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
  160. /*
  161. * Return the padding between the option length and the start of the
  162. * link addr. Currently only IP-over-InfiniBand needs this, although
  163. * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
  164. * also need a pad of 2.
  165. */
  166. static int ndisc_addr_option_pad(unsigned short type)
  167. {
  168. switch (type) {
  169. case ARPHRD_INFINIBAND: return 2;
  170. default: return 0;
  171. }
  172. }
  173. static inline int ndisc_opt_addr_space(struct net_device *dev)
  174. {
  175. return NDISC_OPT_SPACE(dev->addr_len + ndisc_addr_option_pad(dev->type));
  176. }
  177. static u8 *ndisc_fill_addr_option(u8 *opt, int type, void *data, int data_len,
  178. unsigned short addr_type)
  179. {
  180. int space = NDISC_OPT_SPACE(data_len);
  181. int pad = ndisc_addr_option_pad(addr_type);
  182. opt[0] = type;
  183. opt[1] = space>>3;
  184. memset(opt + 2, 0, pad);
  185. opt += pad;
  186. space -= pad;
  187. memcpy(opt+2, data, data_len);
  188. data_len += 2;
  189. opt += data_len;
  190. if ((space -= data_len) > 0)
  191. memset(opt, 0, space);
  192. return opt + space;
  193. }
  194. static struct nd_opt_hdr *ndisc_next_option(struct nd_opt_hdr *cur,
  195. struct nd_opt_hdr *end)
  196. {
  197. int type;
  198. if (!cur || !end || cur >= end)
  199. return NULL;
  200. type = cur->nd_opt_type;
  201. do {
  202. cur = ((void *)cur) + (cur->nd_opt_len << 3);
  203. } while(cur < end && cur->nd_opt_type != type);
  204. return (cur <= end && cur->nd_opt_type == type ? cur : NULL);
  205. }
  206. static inline int ndisc_is_useropt(struct nd_opt_hdr *opt)
  207. {
  208. return (opt->nd_opt_type == ND_OPT_RDNSS);
  209. }
  210. static struct nd_opt_hdr *ndisc_next_useropt(struct nd_opt_hdr *cur,
  211. struct nd_opt_hdr *end)
  212. {
  213. if (!cur || !end || cur >= end)
  214. return NULL;
  215. do {
  216. cur = ((void *)cur) + (cur->nd_opt_len << 3);
  217. } while(cur < end && !ndisc_is_useropt(cur));
  218. return (cur <= end && ndisc_is_useropt(cur) ? cur : NULL);
  219. }
  220. static struct ndisc_options *ndisc_parse_options(u8 *opt, int opt_len,
  221. struct ndisc_options *ndopts)
  222. {
  223. struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)opt;
  224. if (!nd_opt || opt_len < 0 || !ndopts)
  225. return NULL;
  226. memset(ndopts, 0, sizeof(*ndopts));
  227. while (opt_len) {
  228. int l;
  229. if (opt_len < sizeof(struct nd_opt_hdr))
  230. return NULL;
  231. l = nd_opt->nd_opt_len << 3;
  232. if (opt_len < l || l == 0)
  233. return NULL;
  234. switch (nd_opt->nd_opt_type) {
  235. case ND_OPT_SOURCE_LL_ADDR:
  236. case ND_OPT_TARGET_LL_ADDR:
  237. case ND_OPT_MTU:
  238. case ND_OPT_REDIRECT_HDR:
  239. if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
  240. ND_PRINTK2(KERN_WARNING
  241. "%s(): duplicated ND6 option found: type=%d\n",
  242. __FUNCTION__,
  243. nd_opt->nd_opt_type);
  244. } else {
  245. ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
  246. }
  247. break;
  248. case ND_OPT_PREFIX_INFO:
  249. ndopts->nd_opts_pi_end = nd_opt;
  250. if (!ndopts->nd_opt_array[nd_opt->nd_opt_type])
  251. ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
  252. break;
  253. #ifdef CONFIG_IPV6_ROUTE_INFO
  254. case ND_OPT_ROUTE_INFO:
  255. ndopts->nd_opts_ri_end = nd_opt;
  256. if (!ndopts->nd_opts_ri)
  257. ndopts->nd_opts_ri = nd_opt;
  258. break;
  259. #endif
  260. default:
  261. if (ndisc_is_useropt(nd_opt)) {
  262. ndopts->nd_useropts_end = nd_opt;
  263. if (!ndopts->nd_useropts)
  264. ndopts->nd_useropts = nd_opt;
  265. } else {
  266. /*
  267. * Unknown options must be silently ignored,
  268. * to accommodate future extension to the
  269. * protocol.
  270. */
  271. ND_PRINTK2(KERN_NOTICE
  272. "%s(): ignored unsupported option; type=%d, len=%d\n",
  273. __FUNCTION__,
  274. nd_opt->nd_opt_type, nd_opt->nd_opt_len);
  275. }
  276. }
  277. opt_len -= l;
  278. nd_opt = ((void *)nd_opt) + l;
  279. }
  280. return ndopts;
  281. }
  282. static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
  283. struct net_device *dev)
  284. {
  285. u8 *lladdr = (u8 *)(p + 1);
  286. int lladdrlen = p->nd_opt_len << 3;
  287. int prepad = ndisc_addr_option_pad(dev->type);
  288. if (lladdrlen != NDISC_OPT_SPACE(dev->addr_len + prepad))
  289. return NULL;
  290. return (lladdr + prepad);
  291. }
  292. int ndisc_mc_map(struct in6_addr *addr, char *buf, struct net_device *dev, int dir)
  293. {
  294. switch (dev->type) {
  295. case ARPHRD_ETHER:
  296. case ARPHRD_IEEE802: /* Not sure. Check it later. --ANK */
  297. case ARPHRD_FDDI:
  298. ipv6_eth_mc_map(addr, buf);
  299. return 0;
  300. case ARPHRD_IEEE802_TR:
  301. ipv6_tr_mc_map(addr,buf);
  302. return 0;
  303. case ARPHRD_ARCNET:
  304. ipv6_arcnet_mc_map(addr, buf);
  305. return 0;
  306. case ARPHRD_INFINIBAND:
  307. ipv6_ib_mc_map(addr, buf);
  308. return 0;
  309. default:
  310. if (dir) {
  311. memcpy(buf, dev->broadcast, dev->addr_len);
  312. return 0;
  313. }
  314. }
  315. return -EINVAL;
  316. }
  317. EXPORT_SYMBOL(ndisc_mc_map);
  318. static u32 ndisc_hash(const void *pkey, const struct net_device *dev)
  319. {
  320. const u32 *p32 = pkey;
  321. u32 addr_hash, i;
  322. addr_hash = 0;
  323. for (i = 0; i < (sizeof(struct in6_addr) / sizeof(u32)); i++)
  324. addr_hash ^= *p32++;
  325. return jhash_2words(addr_hash, dev->ifindex, nd_tbl.hash_rnd);
  326. }
  327. static int ndisc_constructor(struct neighbour *neigh)
  328. {
  329. struct in6_addr *addr = (struct in6_addr*)&neigh->primary_key;
  330. struct net_device *dev = neigh->dev;
  331. struct inet6_dev *in6_dev;
  332. struct neigh_parms *parms;
  333. int is_multicast = ipv6_addr_is_multicast(addr);
  334. rcu_read_lock();
  335. in6_dev = in6_dev_get(dev);
  336. if (in6_dev == NULL) {
  337. rcu_read_unlock();
  338. return -EINVAL;
  339. }
  340. parms = in6_dev->nd_parms;
  341. __neigh_parms_put(neigh->parms);
  342. neigh->parms = neigh_parms_clone(parms);
  343. rcu_read_unlock();
  344. neigh->type = is_multicast ? RTN_MULTICAST : RTN_UNICAST;
  345. if (!dev->header_ops) {
  346. neigh->nud_state = NUD_NOARP;
  347. neigh->ops = &ndisc_direct_ops;
  348. neigh->output = neigh->ops->queue_xmit;
  349. } else {
  350. if (is_multicast) {
  351. neigh->nud_state = NUD_NOARP;
  352. ndisc_mc_map(addr, neigh->ha, dev, 1);
  353. } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
  354. neigh->nud_state = NUD_NOARP;
  355. memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
  356. if (dev->flags&IFF_LOOPBACK)
  357. neigh->type = RTN_LOCAL;
  358. } else if (dev->flags&IFF_POINTOPOINT) {
  359. neigh->nud_state = NUD_NOARP;
  360. memcpy(neigh->ha, dev->broadcast, dev->addr_len);
  361. }
  362. if (dev->header_ops->cache)
  363. neigh->ops = &ndisc_hh_ops;
  364. else
  365. neigh->ops = &ndisc_generic_ops;
  366. if (neigh->nud_state&NUD_VALID)
  367. neigh->output = neigh->ops->connected_output;
  368. else
  369. neigh->output = neigh->ops->output;
  370. }
  371. in6_dev_put(in6_dev);
  372. return 0;
  373. }
  374. static int pndisc_constructor(struct pneigh_entry *n)
  375. {
  376. struct in6_addr *addr = (struct in6_addr*)&n->key;
  377. struct in6_addr maddr;
  378. struct net_device *dev = n->dev;
  379. if (dev == NULL || __in6_dev_get(dev) == NULL)
  380. return -EINVAL;
  381. addrconf_addr_solict_mult(addr, &maddr);
  382. ipv6_dev_mc_inc(dev, &maddr);
  383. return 0;
  384. }
  385. static void pndisc_destructor(struct pneigh_entry *n)
  386. {
  387. struct in6_addr *addr = (struct in6_addr*)&n->key;
  388. struct in6_addr maddr;
  389. struct net_device *dev = n->dev;
  390. if (dev == NULL || __in6_dev_get(dev) == NULL)
  391. return;
  392. addrconf_addr_solict_mult(addr, &maddr);
  393. ipv6_dev_mc_dec(dev, &maddr);
  394. }
  395. /*
  396. * Send a Neighbour Advertisement
  397. */
  398. static inline void ndisc_flow_init(struct flowi *fl, u8 type,
  399. struct in6_addr *saddr, struct in6_addr *daddr,
  400. int oif)
  401. {
  402. memset(fl, 0, sizeof(*fl));
  403. ipv6_addr_copy(&fl->fl6_src, saddr);
  404. ipv6_addr_copy(&fl->fl6_dst, daddr);
  405. fl->proto = IPPROTO_ICMPV6;
  406. fl->fl_icmp_type = type;
  407. fl->fl_icmp_code = 0;
  408. fl->oif = oif;
  409. security_sk_classify_flow(ndisc_socket->sk, fl);
  410. }
  411. static void __ndisc_send(struct net_device *dev,
  412. struct neighbour *neigh,
  413. struct in6_addr *daddr, struct in6_addr *saddr,
  414. struct icmp6hdr *icmp6h, struct in6_addr *target,
  415. int llinfo)
  416. {
  417. struct flowi fl;
  418. struct dst_entry *dst;
  419. struct sock *sk = ndisc_socket->sk;
  420. struct sk_buff *skb;
  421. struct icmp6hdr *hdr;
  422. struct inet6_dev *idev;
  423. int len;
  424. int err;
  425. u8 *opt, type;
  426. type = icmp6h->icmp6_type;
  427. ndisc_flow_init(&fl, type, saddr, daddr,
  428. dev->ifindex);
  429. dst = ndisc_dst_alloc(dev, neigh, daddr, ip6_output);
  430. if (!dst)
  431. return;
  432. err = xfrm_lookup(&dst, &fl, NULL, 0);
  433. if (err < 0)
  434. return;
  435. if (!dev->addr_len)
  436. llinfo = 0;
  437. len = sizeof(struct icmp6hdr) + (target ? sizeof(*target) : 0);
  438. if (llinfo)
  439. len += ndisc_opt_addr_space(dev);
  440. skb = sock_alloc_send_skb(sk,
  441. (MAX_HEADER + sizeof(struct ipv6hdr) +
  442. len + LL_RESERVED_SPACE(dev)),
  443. 1, &err);
  444. if (!skb) {
  445. ND_PRINTK0(KERN_ERR
  446. "ICMPv6 ND: %s() failed to allocate an skb.\n",
  447. __FUNCTION__);
  448. dst_release(dst);
  449. return;
  450. }
  451. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  452. ip6_nd_hdr(sk, skb, dev, saddr, daddr, IPPROTO_ICMPV6, len);
  453. skb->transport_header = skb->tail;
  454. skb_put(skb, len);
  455. hdr = (struct icmp6hdr *)skb_transport_header(skb);
  456. memcpy(hdr, icmp6h, sizeof(*hdr));
  457. opt = skb_transport_header(skb) + sizeof(struct icmp6hdr);
  458. if (target) {
  459. ipv6_addr_copy((struct in6_addr *)opt, target);
  460. opt += sizeof(*target);
  461. }
  462. if (llinfo)
  463. ndisc_fill_addr_option(opt, llinfo, dev->dev_addr,
  464. dev->addr_len, dev->type);
  465. hdr->icmp6_cksum = csum_ipv6_magic(saddr, daddr, len,
  466. IPPROTO_ICMPV6,
  467. csum_partial((__u8 *) hdr,
  468. len, 0));
  469. skb->dst = dst;
  470. idev = in6_dev_get(dst->dev);
  471. IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
  472. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, dst->dev, dst_output);
  473. if (!err) {
  474. ICMP6MSGOUT_INC_STATS(idev, type);
  475. ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
  476. }
  477. if (likely(idev != NULL))
  478. in6_dev_put(idev);
  479. }
  480. static void ndisc_send_na(struct net_device *dev, struct neighbour *neigh,
  481. struct in6_addr *daddr, struct in6_addr *solicited_addr,
  482. int router, int solicited, int override, int inc_opt)
  483. {
  484. struct in6_addr tmpaddr;
  485. struct inet6_ifaddr *ifp;
  486. struct in6_addr *src_addr;
  487. struct icmp6hdr icmp6h = {
  488. .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
  489. };
  490. /* for anycast or proxy, solicited_addr != src_addr */
  491. ifp = ipv6_get_ifaddr(solicited_addr, dev, 1);
  492. if (ifp) {
  493. src_addr = solicited_addr;
  494. if (ifp->flags & IFA_F_OPTIMISTIC)
  495. override = 0;
  496. in6_ifa_put(ifp);
  497. } else {
  498. if (ipv6_dev_get_saddr(dev, daddr, &tmpaddr))
  499. return;
  500. src_addr = &tmpaddr;
  501. }
  502. icmp6h.icmp6_router = router;
  503. icmp6h.icmp6_solicited = solicited;
  504. icmp6h.icmp6_override = override;
  505. __ndisc_send(dev, neigh, daddr, src_addr,
  506. &icmp6h, solicited_addr,
  507. inc_opt ? ND_OPT_TARGET_LL_ADDR : 0);
  508. }
  509. void ndisc_send_ns(struct net_device *dev, struct neighbour *neigh,
  510. struct in6_addr *solicit,
  511. struct in6_addr *daddr, struct in6_addr *saddr)
  512. {
  513. struct in6_addr addr_buf;
  514. struct icmp6hdr icmp6h = {
  515. .icmp6_type = NDISC_NEIGHBOUR_SOLICITATION,
  516. };
  517. if (saddr == NULL) {
  518. if (ipv6_get_lladdr(dev, &addr_buf,
  519. (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)))
  520. return;
  521. saddr = &addr_buf;
  522. }
  523. __ndisc_send(dev, neigh, daddr, saddr,
  524. &icmp6h, solicit,
  525. !ipv6_addr_any(saddr) ? ND_OPT_SOURCE_LL_ADDR : 0);
  526. }
  527. void ndisc_send_rs(struct net_device *dev, struct in6_addr *saddr,
  528. struct in6_addr *daddr)
  529. {
  530. struct icmp6hdr icmp6h = {
  531. .icmp6_type = NDISC_ROUTER_SOLICITATION,
  532. };
  533. int send_sllao = dev->addr_len;
  534. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  535. /*
  536. * According to section 2.2 of RFC 4429, we must not
  537. * send router solicitations with a sllao from
  538. * optimistic addresses, but we may send the solicitation
  539. * if we don't include the sllao. So here we check
  540. * if our address is optimistic, and if so, we
  541. * supress the inclusion of the sllao.
  542. */
  543. if (send_sllao) {
  544. struct inet6_ifaddr *ifp = ipv6_get_ifaddr(saddr, dev, 1);
  545. if (ifp) {
  546. if (ifp->flags & IFA_F_OPTIMISTIC) {
  547. send_sllao = 0;
  548. }
  549. in6_ifa_put(ifp);
  550. } else {
  551. send_sllao = 0;
  552. }
  553. }
  554. #endif
  555. __ndisc_send(dev, NULL, daddr, saddr,
  556. &icmp6h, NULL,
  557. send_sllao ? ND_OPT_SOURCE_LL_ADDR : 0);
  558. }
  559. static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb)
  560. {
  561. /*
  562. * "The sender MUST return an ICMP
  563. * destination unreachable"
  564. */
  565. dst_link_failure(skb);
  566. kfree_skb(skb);
  567. }
  568. /* Called with locked neigh: either read or both */
  569. static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb)
  570. {
  571. struct in6_addr *saddr = NULL;
  572. struct in6_addr mcaddr;
  573. struct net_device *dev = neigh->dev;
  574. struct in6_addr *target = (struct in6_addr *)&neigh->primary_key;
  575. int probes = atomic_read(&neigh->probes);
  576. if (skb && ipv6_chk_addr(&ipv6_hdr(skb)->saddr, dev, 1))
  577. saddr = &ipv6_hdr(skb)->saddr;
  578. if ((probes -= neigh->parms->ucast_probes) < 0) {
  579. if (!(neigh->nud_state & NUD_VALID)) {
  580. ND_PRINTK1(KERN_DEBUG
  581. "%s(): trying to ucast probe in NUD_INVALID: "
  582. NIP6_FMT "\n",
  583. __FUNCTION__,
  584. NIP6(*target));
  585. }
  586. ndisc_send_ns(dev, neigh, target, target, saddr);
  587. } else if ((probes -= neigh->parms->app_probes) < 0) {
  588. #ifdef CONFIG_ARPD
  589. neigh_app_ns(neigh);
  590. #endif
  591. } else {
  592. addrconf_addr_solict_mult(target, &mcaddr);
  593. ndisc_send_ns(dev, NULL, target, &mcaddr, saddr);
  594. }
  595. }
  596. static void ndisc_recv_ns(struct sk_buff *skb)
  597. {
  598. struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
  599. struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
  600. struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
  601. u8 *lladdr = NULL;
  602. u32 ndoptlen = skb->tail - (skb->transport_header +
  603. offsetof(struct nd_msg, opt));
  604. struct ndisc_options ndopts;
  605. struct net_device *dev = skb->dev;
  606. struct inet6_ifaddr *ifp;
  607. struct inet6_dev *idev = NULL;
  608. struct neighbour *neigh;
  609. struct pneigh_entry *pneigh = NULL;
  610. int dad = ipv6_addr_any(saddr);
  611. int inc;
  612. int is_router;
  613. if (ipv6_addr_is_multicast(&msg->target)) {
  614. ND_PRINTK2(KERN_WARNING
  615. "ICMPv6 NS: multicast target address");
  616. return;
  617. }
  618. /*
  619. * RFC2461 7.1.1:
  620. * DAD has to be destined for solicited node multicast address.
  621. */
  622. if (dad &&
  623. !(daddr->s6_addr32[0] == htonl(0xff020000) &&
  624. daddr->s6_addr32[1] == htonl(0x00000000) &&
  625. daddr->s6_addr32[2] == htonl(0x00000001) &&
  626. daddr->s6_addr [12] == 0xff )) {
  627. ND_PRINTK2(KERN_WARNING
  628. "ICMPv6 NS: bad DAD packet (wrong destination)\n");
  629. return;
  630. }
  631. if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts)) {
  632. ND_PRINTK2(KERN_WARNING
  633. "ICMPv6 NS: invalid ND options\n");
  634. return;
  635. }
  636. if (ndopts.nd_opts_src_lladdr) {
  637. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev);
  638. if (!lladdr) {
  639. ND_PRINTK2(KERN_WARNING
  640. "ICMPv6 NS: invalid link-layer address length\n");
  641. return;
  642. }
  643. /* RFC2461 7.1.1:
  644. * If the IP source address is the unspecified address,
  645. * there MUST NOT be source link-layer address option
  646. * in the message.
  647. */
  648. if (dad) {
  649. ND_PRINTK2(KERN_WARNING
  650. "ICMPv6 NS: bad DAD packet (link-layer address option)\n");
  651. return;
  652. }
  653. }
  654. inc = ipv6_addr_is_multicast(daddr);
  655. if ((ifp = ipv6_get_ifaddr(&msg->target, dev, 1)) != NULL) {
  656. if (ifp->flags & (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)) {
  657. if (dad) {
  658. if (dev->type == ARPHRD_IEEE802_TR) {
  659. const unsigned char *sadr;
  660. sadr = skb_mac_header(skb);
  661. if (((sadr[8] ^ dev->dev_addr[0]) & 0x7f) == 0 &&
  662. sadr[9] == dev->dev_addr[1] &&
  663. sadr[10] == dev->dev_addr[2] &&
  664. sadr[11] == dev->dev_addr[3] &&
  665. sadr[12] == dev->dev_addr[4] &&
  666. sadr[13] == dev->dev_addr[5]) {
  667. /* looped-back to us */
  668. goto out;
  669. }
  670. }
  671. /*
  672. * We are colliding with another node
  673. * who is doing DAD
  674. * so fail our DAD process
  675. */
  676. addrconf_dad_failure(ifp);
  677. return;
  678. } else {
  679. /*
  680. * This is not a dad solicitation.
  681. * If we are an optimistic node,
  682. * we should respond.
  683. * Otherwise, we should ignore it.
  684. */
  685. if (!(ifp->flags & IFA_F_OPTIMISTIC))
  686. goto out;
  687. }
  688. }
  689. idev = ifp->idev;
  690. } else {
  691. idev = in6_dev_get(dev);
  692. if (!idev) {
  693. /* XXX: count this drop? */
  694. return;
  695. }
  696. if (ipv6_chk_acast_addr(dev, &msg->target) ||
  697. (idev->cnf.forwarding &&
  698. (ipv6_devconf.proxy_ndp || idev->cnf.proxy_ndp) &&
  699. (pneigh = pneigh_lookup(&nd_tbl,
  700. &msg->target, dev, 0)) != NULL)) {
  701. if (!(NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED) &&
  702. skb->pkt_type != PACKET_HOST &&
  703. inc != 0 &&
  704. idev->nd_parms->proxy_delay != 0) {
  705. /*
  706. * for anycast or proxy,
  707. * sender should delay its response
  708. * by a random time between 0 and
  709. * MAX_ANYCAST_DELAY_TIME seconds.
  710. * (RFC2461) -- yoshfuji
  711. */
  712. struct sk_buff *n = skb_clone(skb, GFP_ATOMIC);
  713. if (n)
  714. pneigh_enqueue(&nd_tbl, idev->nd_parms, n);
  715. goto out;
  716. }
  717. } else
  718. goto out;
  719. }
  720. is_router = !!(pneigh ? pneigh->flags & NTF_ROUTER : idev->cnf.forwarding);
  721. if (dad) {
  722. struct in6_addr maddr;
  723. ipv6_addr_all_nodes(&maddr);
  724. ndisc_send_na(dev, NULL, &maddr, &msg->target,
  725. is_router, 0, (ifp != NULL), 1);
  726. goto out;
  727. }
  728. if (inc)
  729. NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_mcast);
  730. else
  731. NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_ucast);
  732. /*
  733. * update / create cache entry
  734. * for the source address
  735. */
  736. neigh = __neigh_lookup(&nd_tbl, saddr, dev,
  737. !inc || lladdr || !dev->addr_len);
  738. if (neigh)
  739. neigh_update(neigh, lladdr, NUD_STALE,
  740. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  741. NEIGH_UPDATE_F_OVERRIDE);
  742. if (neigh || !dev->header_ops) {
  743. ndisc_send_na(dev, neigh, saddr, &msg->target,
  744. is_router,
  745. 1, (ifp != NULL && inc), inc);
  746. if (neigh)
  747. neigh_release(neigh);
  748. }
  749. out:
  750. if (ifp)
  751. in6_ifa_put(ifp);
  752. else
  753. in6_dev_put(idev);
  754. return;
  755. }
  756. static void ndisc_recv_na(struct sk_buff *skb)
  757. {
  758. struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
  759. struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
  760. struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
  761. u8 *lladdr = NULL;
  762. u32 ndoptlen = skb->tail - (skb->transport_header +
  763. offsetof(struct nd_msg, opt));
  764. struct ndisc_options ndopts;
  765. struct net_device *dev = skb->dev;
  766. struct inet6_ifaddr *ifp;
  767. struct neighbour *neigh;
  768. if (skb->len < sizeof(struct nd_msg)) {
  769. ND_PRINTK2(KERN_WARNING
  770. "ICMPv6 NA: packet too short\n");
  771. return;
  772. }
  773. if (ipv6_addr_is_multicast(&msg->target)) {
  774. ND_PRINTK2(KERN_WARNING
  775. "ICMPv6 NA: target address is multicast.\n");
  776. return;
  777. }
  778. if (ipv6_addr_is_multicast(daddr) &&
  779. msg->icmph.icmp6_solicited) {
  780. ND_PRINTK2(KERN_WARNING
  781. "ICMPv6 NA: solicited NA is multicasted.\n");
  782. return;
  783. }
  784. if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts)) {
  785. ND_PRINTK2(KERN_WARNING
  786. "ICMPv6 NS: invalid ND option\n");
  787. return;
  788. }
  789. if (ndopts.nd_opts_tgt_lladdr) {
  790. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, dev);
  791. if (!lladdr) {
  792. ND_PRINTK2(KERN_WARNING
  793. "ICMPv6 NA: invalid link-layer address length\n");
  794. return;
  795. }
  796. }
  797. if ((ifp = ipv6_get_ifaddr(&msg->target, dev, 1))) {
  798. if (ifp->flags & IFA_F_TENTATIVE) {
  799. addrconf_dad_failure(ifp);
  800. return;
  801. }
  802. /* What should we make now? The advertisement
  803. is invalid, but ndisc specs say nothing
  804. about it. It could be misconfiguration, or
  805. an smart proxy agent tries to help us :-)
  806. */
  807. ND_PRINTK1(KERN_WARNING
  808. "ICMPv6 NA: someone advertises our address on %s!\n",
  809. ifp->idev->dev->name);
  810. in6_ifa_put(ifp);
  811. return;
  812. }
  813. neigh = neigh_lookup(&nd_tbl, &msg->target, dev);
  814. if (neigh) {
  815. u8 old_flags = neigh->flags;
  816. if (neigh->nud_state & NUD_FAILED)
  817. goto out;
  818. /*
  819. * Don't update the neighbor cache entry on a proxy NA from
  820. * ourselves because either the proxied node is off link or it
  821. * has already sent a NA to us.
  822. */
  823. if (lladdr && !memcmp(lladdr, dev->dev_addr, dev->addr_len) &&
  824. ipv6_devconf.forwarding && ipv6_devconf.proxy_ndp &&
  825. pneigh_lookup(&nd_tbl, &msg->target, dev, 0)) {
  826. /* XXX: idev->cnf.prixy_ndp */
  827. goto out;
  828. }
  829. neigh_update(neigh, lladdr,
  830. msg->icmph.icmp6_solicited ? NUD_REACHABLE : NUD_STALE,
  831. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  832. (msg->icmph.icmp6_override ? NEIGH_UPDATE_F_OVERRIDE : 0)|
  833. NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  834. (msg->icmph.icmp6_router ? NEIGH_UPDATE_F_ISROUTER : 0));
  835. if ((old_flags & ~neigh->flags) & NTF_ROUTER) {
  836. /*
  837. * Change: router to host
  838. */
  839. struct rt6_info *rt;
  840. rt = rt6_get_dflt_router(saddr, dev);
  841. if (rt)
  842. ip6_del_rt(rt);
  843. }
  844. out:
  845. neigh_release(neigh);
  846. }
  847. }
  848. static void ndisc_recv_rs(struct sk_buff *skb)
  849. {
  850. struct rs_msg *rs_msg = (struct rs_msg *)skb_transport_header(skb);
  851. unsigned long ndoptlen = skb->len - sizeof(*rs_msg);
  852. struct neighbour *neigh;
  853. struct inet6_dev *idev;
  854. struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
  855. struct ndisc_options ndopts;
  856. u8 *lladdr = NULL;
  857. if (skb->len < sizeof(*rs_msg))
  858. return;
  859. idev = in6_dev_get(skb->dev);
  860. if (!idev) {
  861. if (net_ratelimit())
  862. ND_PRINTK1("ICMP6 RS: can't find in6 device\n");
  863. return;
  864. }
  865. /* Don't accept RS if we're not in router mode */
  866. if (!idev->cnf.forwarding)
  867. goto out;
  868. /*
  869. * Don't update NCE if src = ::;
  870. * this implies that the source node has no ip address assigned yet.
  871. */
  872. if (ipv6_addr_any(saddr))
  873. goto out;
  874. /* Parse ND options */
  875. if (!ndisc_parse_options(rs_msg->opt, ndoptlen, &ndopts)) {
  876. if (net_ratelimit())
  877. ND_PRINTK2("ICMP6 NS: invalid ND option, ignored\n");
  878. goto out;
  879. }
  880. if (ndopts.nd_opts_src_lladdr) {
  881. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
  882. skb->dev);
  883. if (!lladdr)
  884. goto out;
  885. }
  886. neigh = __neigh_lookup(&nd_tbl, saddr, skb->dev, 1);
  887. if (neigh) {
  888. neigh_update(neigh, lladdr, NUD_STALE,
  889. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  890. NEIGH_UPDATE_F_OVERRIDE|
  891. NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
  892. neigh_release(neigh);
  893. }
  894. out:
  895. in6_dev_put(idev);
  896. }
  897. static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
  898. {
  899. struct icmp6hdr *icmp6h = (struct icmp6hdr *)skb_transport_header(ra);
  900. struct sk_buff *skb;
  901. struct nlmsghdr *nlh;
  902. struct nduseroptmsg *ndmsg;
  903. int err;
  904. int base_size = NLMSG_ALIGN(sizeof(struct nduseroptmsg)
  905. + (opt->nd_opt_len << 3));
  906. size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr));
  907. skb = nlmsg_new(msg_size, GFP_ATOMIC);
  908. if (skb == NULL) {
  909. err = -ENOBUFS;
  910. goto errout;
  911. }
  912. nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0);
  913. if (nlh == NULL) {
  914. goto nla_put_failure;
  915. }
  916. ndmsg = nlmsg_data(nlh);
  917. ndmsg->nduseropt_family = AF_INET6;
  918. ndmsg->nduseropt_icmp_type = icmp6h->icmp6_type;
  919. ndmsg->nduseropt_icmp_code = icmp6h->icmp6_code;
  920. ndmsg->nduseropt_opts_len = opt->nd_opt_len << 3;
  921. memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3);
  922. NLA_PUT(skb, NDUSEROPT_SRCADDR, sizeof(struct in6_addr),
  923. &ipv6_hdr(ra)->saddr);
  924. nlmsg_end(skb, nlh);
  925. err = rtnl_notify(skb, 0, RTNLGRP_ND_USEROPT, NULL, GFP_ATOMIC);
  926. if (err < 0)
  927. goto errout;
  928. return;
  929. nla_put_failure:
  930. nlmsg_free(skb);
  931. err = -EMSGSIZE;
  932. errout:
  933. rtnl_set_sk_err(RTNLGRP_ND_USEROPT, err);
  934. }
  935. static void ndisc_router_discovery(struct sk_buff *skb)
  936. {
  937. struct ra_msg *ra_msg = (struct ra_msg *)skb_transport_header(skb);
  938. struct neighbour *neigh = NULL;
  939. struct inet6_dev *in6_dev;
  940. struct rt6_info *rt = NULL;
  941. int lifetime;
  942. struct ndisc_options ndopts;
  943. int optlen;
  944. unsigned int pref = 0;
  945. __u8 * opt = (__u8 *)(ra_msg + 1);
  946. optlen = (skb->tail - skb->transport_header) - sizeof(struct ra_msg);
  947. if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
  948. ND_PRINTK2(KERN_WARNING
  949. "ICMPv6 RA: source address is not link-local.\n");
  950. return;
  951. }
  952. if (optlen < 0) {
  953. ND_PRINTK2(KERN_WARNING
  954. "ICMPv6 RA: packet too short\n");
  955. return;
  956. }
  957. /*
  958. * set the RA_RECV flag in the interface
  959. */
  960. in6_dev = in6_dev_get(skb->dev);
  961. if (in6_dev == NULL) {
  962. ND_PRINTK0(KERN_ERR
  963. "ICMPv6 RA: can't find inet6 device for %s.\n",
  964. skb->dev->name);
  965. return;
  966. }
  967. if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_ra) {
  968. in6_dev_put(in6_dev);
  969. return;
  970. }
  971. if (!ndisc_parse_options(opt, optlen, &ndopts)) {
  972. in6_dev_put(in6_dev);
  973. ND_PRINTK2(KERN_WARNING
  974. "ICMP6 RA: invalid ND options\n");
  975. return;
  976. }
  977. if (in6_dev->if_flags & IF_RS_SENT) {
  978. /*
  979. * flag that an RA was received after an RS was sent
  980. * out on this interface.
  981. */
  982. in6_dev->if_flags |= IF_RA_RCVD;
  983. }
  984. /*
  985. * Remember the managed/otherconf flags from most recently
  986. * received RA message (RFC 2462) -- yoshfuji
  987. */
  988. in6_dev->if_flags = (in6_dev->if_flags & ~(IF_RA_MANAGED |
  989. IF_RA_OTHERCONF)) |
  990. (ra_msg->icmph.icmp6_addrconf_managed ?
  991. IF_RA_MANAGED : 0) |
  992. (ra_msg->icmph.icmp6_addrconf_other ?
  993. IF_RA_OTHERCONF : 0);
  994. if (!in6_dev->cnf.accept_ra_defrtr)
  995. goto skip_defrtr;
  996. lifetime = ntohs(ra_msg->icmph.icmp6_rt_lifetime);
  997. #ifdef CONFIG_IPV6_ROUTER_PREF
  998. pref = ra_msg->icmph.icmp6_router_pref;
  999. /* 10b is handled as if it were 00b (medium) */
  1000. if (pref == ICMPV6_ROUTER_PREF_INVALID ||
  1001. !in6_dev->cnf.accept_ra_rtr_pref)
  1002. pref = ICMPV6_ROUTER_PREF_MEDIUM;
  1003. #endif
  1004. rt = rt6_get_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev);
  1005. if (rt)
  1006. neigh = rt->rt6i_nexthop;
  1007. if (rt && lifetime == 0) {
  1008. neigh_clone(neigh);
  1009. ip6_del_rt(rt);
  1010. rt = NULL;
  1011. }
  1012. if (rt == NULL && lifetime) {
  1013. ND_PRINTK3(KERN_DEBUG
  1014. "ICMPv6 RA: adding default router.\n");
  1015. rt = rt6_add_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev, pref);
  1016. if (rt == NULL) {
  1017. ND_PRINTK0(KERN_ERR
  1018. "ICMPv6 RA: %s() failed to add default route.\n",
  1019. __FUNCTION__);
  1020. in6_dev_put(in6_dev);
  1021. return;
  1022. }
  1023. neigh = rt->rt6i_nexthop;
  1024. if (neigh == NULL) {
  1025. ND_PRINTK0(KERN_ERR
  1026. "ICMPv6 RA: %s() got default router without neighbour.\n",
  1027. __FUNCTION__);
  1028. dst_release(&rt->u.dst);
  1029. in6_dev_put(in6_dev);
  1030. return;
  1031. }
  1032. neigh->flags |= NTF_ROUTER;
  1033. } else if (rt) {
  1034. rt->rt6i_flags |= (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
  1035. }
  1036. if (rt)
  1037. rt->rt6i_expires = jiffies + (HZ * lifetime);
  1038. if (ra_msg->icmph.icmp6_hop_limit) {
  1039. in6_dev->cnf.hop_limit = ra_msg->icmph.icmp6_hop_limit;
  1040. if (rt)
  1041. rt->u.dst.metrics[RTAX_HOPLIMIT-1] = ra_msg->icmph.icmp6_hop_limit;
  1042. }
  1043. skip_defrtr:
  1044. /*
  1045. * Update Reachable Time and Retrans Timer
  1046. */
  1047. if (in6_dev->nd_parms) {
  1048. unsigned long rtime = ntohl(ra_msg->retrans_timer);
  1049. if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/HZ) {
  1050. rtime = (rtime*HZ)/1000;
  1051. if (rtime < HZ/10)
  1052. rtime = HZ/10;
  1053. in6_dev->nd_parms->retrans_time = rtime;
  1054. in6_dev->tstamp = jiffies;
  1055. inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
  1056. }
  1057. rtime = ntohl(ra_msg->reachable_time);
  1058. if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/(3*HZ)) {
  1059. rtime = (rtime*HZ)/1000;
  1060. if (rtime < HZ/10)
  1061. rtime = HZ/10;
  1062. if (rtime != in6_dev->nd_parms->base_reachable_time) {
  1063. in6_dev->nd_parms->base_reachable_time = rtime;
  1064. in6_dev->nd_parms->gc_staletime = 3 * rtime;
  1065. in6_dev->nd_parms->reachable_time = neigh_rand_reach_time(rtime);
  1066. in6_dev->tstamp = jiffies;
  1067. inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
  1068. }
  1069. }
  1070. }
  1071. /*
  1072. * Process options.
  1073. */
  1074. if (!neigh)
  1075. neigh = __neigh_lookup(&nd_tbl, &ipv6_hdr(skb)->saddr,
  1076. skb->dev, 1);
  1077. if (neigh) {
  1078. u8 *lladdr = NULL;
  1079. if (ndopts.nd_opts_src_lladdr) {
  1080. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
  1081. skb->dev);
  1082. if (!lladdr) {
  1083. ND_PRINTK2(KERN_WARNING
  1084. "ICMPv6 RA: invalid link-layer address length\n");
  1085. goto out;
  1086. }
  1087. }
  1088. neigh_update(neigh, lladdr, NUD_STALE,
  1089. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  1090. NEIGH_UPDATE_F_OVERRIDE|
  1091. NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  1092. NEIGH_UPDATE_F_ISROUTER);
  1093. }
  1094. #ifdef CONFIG_IPV6_ROUTE_INFO
  1095. if (in6_dev->cnf.accept_ra_rtr_pref && ndopts.nd_opts_ri) {
  1096. struct nd_opt_hdr *p;
  1097. for (p = ndopts.nd_opts_ri;
  1098. p;
  1099. p = ndisc_next_option(p, ndopts.nd_opts_ri_end)) {
  1100. if (((struct route_info *)p)->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen)
  1101. continue;
  1102. rt6_route_rcv(skb->dev, (u8*)p, (p->nd_opt_len) << 3,
  1103. &ipv6_hdr(skb)->saddr);
  1104. }
  1105. }
  1106. #endif
  1107. if (in6_dev->cnf.accept_ra_pinfo && ndopts.nd_opts_pi) {
  1108. struct nd_opt_hdr *p;
  1109. for (p = ndopts.nd_opts_pi;
  1110. p;
  1111. p = ndisc_next_option(p, ndopts.nd_opts_pi_end)) {
  1112. addrconf_prefix_rcv(skb->dev, (u8*)p, (p->nd_opt_len) << 3);
  1113. }
  1114. }
  1115. if (ndopts.nd_opts_mtu) {
  1116. __be32 n;
  1117. u32 mtu;
  1118. memcpy(&n, ((u8*)(ndopts.nd_opts_mtu+1))+2, sizeof(mtu));
  1119. mtu = ntohl(n);
  1120. if (mtu < IPV6_MIN_MTU || mtu > skb->dev->mtu) {
  1121. ND_PRINTK2(KERN_WARNING
  1122. "ICMPv6 RA: invalid mtu: %d\n",
  1123. mtu);
  1124. } else if (in6_dev->cnf.mtu6 != mtu) {
  1125. in6_dev->cnf.mtu6 = mtu;
  1126. if (rt)
  1127. rt->u.dst.metrics[RTAX_MTU-1] = mtu;
  1128. rt6_mtu_change(skb->dev, mtu);
  1129. }
  1130. }
  1131. if (ndopts.nd_useropts) {
  1132. struct nd_opt_hdr *opt;
  1133. for (opt = ndopts.nd_useropts;
  1134. opt;
  1135. opt = ndisc_next_useropt(opt, ndopts.nd_useropts_end)) {
  1136. ndisc_ra_useropt(skb, opt);
  1137. }
  1138. }
  1139. if (ndopts.nd_opts_tgt_lladdr || ndopts.nd_opts_rh) {
  1140. ND_PRINTK2(KERN_WARNING
  1141. "ICMPv6 RA: invalid RA options");
  1142. }
  1143. out:
  1144. if (rt)
  1145. dst_release(&rt->u.dst);
  1146. else if (neigh)
  1147. neigh_release(neigh);
  1148. in6_dev_put(in6_dev);
  1149. }
  1150. static void ndisc_redirect_rcv(struct sk_buff *skb)
  1151. {
  1152. struct inet6_dev *in6_dev;
  1153. struct icmp6hdr *icmph;
  1154. struct in6_addr *dest;
  1155. struct in6_addr *target; /* new first hop to destination */
  1156. struct neighbour *neigh;
  1157. int on_link = 0;
  1158. struct ndisc_options ndopts;
  1159. int optlen;
  1160. u8 *lladdr = NULL;
  1161. if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
  1162. ND_PRINTK2(KERN_WARNING
  1163. "ICMPv6 Redirect: source address is not link-local.\n");
  1164. return;
  1165. }
  1166. optlen = skb->tail - skb->transport_header;
  1167. optlen -= sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr);
  1168. if (optlen < 0) {
  1169. ND_PRINTK2(KERN_WARNING
  1170. "ICMPv6 Redirect: packet too short\n");
  1171. return;
  1172. }
  1173. icmph = icmp6_hdr(skb);
  1174. target = (struct in6_addr *) (icmph + 1);
  1175. dest = target + 1;
  1176. if (ipv6_addr_is_multicast(dest)) {
  1177. ND_PRINTK2(KERN_WARNING
  1178. "ICMPv6 Redirect: destination address is multicast.\n");
  1179. return;
  1180. }
  1181. if (ipv6_addr_equal(dest, target)) {
  1182. on_link = 1;
  1183. } else if (ipv6_addr_type(target) !=
  1184. (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
  1185. ND_PRINTK2(KERN_WARNING
  1186. "ICMPv6 Redirect: target address is not link-local unicast.\n");
  1187. return;
  1188. }
  1189. in6_dev = in6_dev_get(skb->dev);
  1190. if (!in6_dev)
  1191. return;
  1192. if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) {
  1193. in6_dev_put(in6_dev);
  1194. return;
  1195. }
  1196. /* RFC2461 8.1:
  1197. * The IP source address of the Redirect MUST be the same as the current
  1198. * first-hop router for the specified ICMP Destination Address.
  1199. */
  1200. if (!ndisc_parse_options((u8*)(dest + 1), optlen, &ndopts)) {
  1201. ND_PRINTK2(KERN_WARNING
  1202. "ICMPv6 Redirect: invalid ND options\n");
  1203. in6_dev_put(in6_dev);
  1204. return;
  1205. }
  1206. if (ndopts.nd_opts_tgt_lladdr) {
  1207. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
  1208. skb->dev);
  1209. if (!lladdr) {
  1210. ND_PRINTK2(KERN_WARNING
  1211. "ICMPv6 Redirect: invalid link-layer address length\n");
  1212. in6_dev_put(in6_dev);
  1213. return;
  1214. }
  1215. }
  1216. neigh = __neigh_lookup(&nd_tbl, target, skb->dev, 1);
  1217. if (neigh) {
  1218. rt6_redirect(dest, &ipv6_hdr(skb)->daddr,
  1219. &ipv6_hdr(skb)->saddr, neigh, lladdr,
  1220. on_link);
  1221. neigh_release(neigh);
  1222. }
  1223. in6_dev_put(in6_dev);
  1224. }
  1225. void ndisc_send_redirect(struct sk_buff *skb, struct neighbour *neigh,
  1226. struct in6_addr *target)
  1227. {
  1228. struct sock *sk = ndisc_socket->sk;
  1229. int len = sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr);
  1230. struct sk_buff *buff;
  1231. struct icmp6hdr *icmph;
  1232. struct in6_addr saddr_buf;
  1233. struct in6_addr *addrp;
  1234. struct net_device *dev;
  1235. struct rt6_info *rt;
  1236. struct dst_entry *dst;
  1237. struct inet6_dev *idev;
  1238. struct flowi fl;
  1239. u8 *opt;
  1240. int rd_len;
  1241. int err;
  1242. int hlen;
  1243. u8 ha_buf[MAX_ADDR_LEN], *ha = NULL;
  1244. dev = skb->dev;
  1245. if (ipv6_get_lladdr(dev, &saddr_buf, IFA_F_TENTATIVE)) {
  1246. ND_PRINTK2(KERN_WARNING
  1247. "ICMPv6 Redirect: no link-local address on %s\n",
  1248. dev->name);
  1249. return;
  1250. }
  1251. if (!ipv6_addr_equal(&ipv6_hdr(skb)->daddr, target) &&
  1252. ipv6_addr_type(target) != (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
  1253. ND_PRINTK2(KERN_WARNING
  1254. "ICMPv6 Redirect: target address is not link-local unicast.\n");
  1255. return;
  1256. }
  1257. ndisc_flow_init(&fl, NDISC_REDIRECT, &saddr_buf, &ipv6_hdr(skb)->saddr,
  1258. dev->ifindex);
  1259. dst = ip6_route_output(NULL, &fl);
  1260. if (dst == NULL)
  1261. return;
  1262. err = xfrm_lookup(&dst, &fl, NULL, 0);
  1263. if (err)
  1264. return;
  1265. rt = (struct rt6_info *) dst;
  1266. if (rt->rt6i_flags & RTF_GATEWAY) {
  1267. ND_PRINTK2(KERN_WARNING
  1268. "ICMPv6 Redirect: destination is not a neighbour.\n");
  1269. dst_release(dst);
  1270. return;
  1271. }
  1272. if (!xrlim_allow(dst, 1*HZ)) {
  1273. dst_release(dst);
  1274. return;
  1275. }
  1276. if (dev->addr_len) {
  1277. read_lock_bh(&neigh->lock);
  1278. if (neigh->nud_state & NUD_VALID) {
  1279. memcpy(ha_buf, neigh->ha, dev->addr_len);
  1280. read_unlock_bh(&neigh->lock);
  1281. ha = ha_buf;
  1282. len += ndisc_opt_addr_space(dev);
  1283. } else
  1284. read_unlock_bh(&neigh->lock);
  1285. }
  1286. rd_len = min_t(unsigned int,
  1287. IPV6_MIN_MTU-sizeof(struct ipv6hdr)-len, skb->len + 8);
  1288. rd_len &= ~0x7;
  1289. len += rd_len;
  1290. buff = sock_alloc_send_skb(sk,
  1291. (MAX_HEADER + sizeof(struct ipv6hdr) +
  1292. len + LL_RESERVED_SPACE(dev)),
  1293. 1, &err);
  1294. if (buff == NULL) {
  1295. ND_PRINTK0(KERN_ERR
  1296. "ICMPv6 Redirect: %s() failed to allocate an skb.\n",
  1297. __FUNCTION__);
  1298. dst_release(dst);
  1299. return;
  1300. }
  1301. hlen = 0;
  1302. skb_reserve(buff, LL_RESERVED_SPACE(dev));
  1303. ip6_nd_hdr(sk, buff, dev, &saddr_buf, &ipv6_hdr(skb)->saddr,
  1304. IPPROTO_ICMPV6, len);
  1305. skb_set_transport_header(buff, skb_tail_pointer(buff) - buff->data);
  1306. skb_put(buff, len);
  1307. icmph = icmp6_hdr(buff);
  1308. memset(icmph, 0, sizeof(struct icmp6hdr));
  1309. icmph->icmp6_type = NDISC_REDIRECT;
  1310. /*
  1311. * copy target and destination addresses
  1312. */
  1313. addrp = (struct in6_addr *)(icmph + 1);
  1314. ipv6_addr_copy(addrp, target);
  1315. addrp++;
  1316. ipv6_addr_copy(addrp, &ipv6_hdr(skb)->daddr);
  1317. opt = (u8*) (addrp + 1);
  1318. /*
  1319. * include target_address option
  1320. */
  1321. if (ha)
  1322. opt = ndisc_fill_addr_option(opt, ND_OPT_TARGET_LL_ADDR, ha,
  1323. dev->addr_len, dev->type);
  1324. /*
  1325. * build redirect option and copy skb over to the new packet.
  1326. */
  1327. memset(opt, 0, 8);
  1328. *(opt++) = ND_OPT_REDIRECT_HDR;
  1329. *(opt++) = (rd_len >> 3);
  1330. opt += 6;
  1331. memcpy(opt, ipv6_hdr(skb), rd_len - 8);
  1332. icmph->icmp6_cksum = csum_ipv6_magic(&saddr_buf, &ipv6_hdr(skb)->saddr,
  1333. len, IPPROTO_ICMPV6,
  1334. csum_partial((u8 *) icmph, len, 0));
  1335. buff->dst = dst;
  1336. idev = in6_dev_get(dst->dev);
  1337. IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
  1338. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, buff, NULL, dst->dev, dst_output);
  1339. if (!err) {
  1340. ICMP6MSGOUT_INC_STATS(idev, NDISC_REDIRECT);
  1341. ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
  1342. }
  1343. if (likely(idev != NULL))
  1344. in6_dev_put(idev);
  1345. }
  1346. static void pndisc_redo(struct sk_buff *skb)
  1347. {
  1348. ndisc_recv_ns(skb);
  1349. kfree_skb(skb);
  1350. }
  1351. int ndisc_rcv(struct sk_buff *skb)
  1352. {
  1353. struct nd_msg *msg;
  1354. if (!pskb_may_pull(skb, skb->len))
  1355. return 0;
  1356. msg = (struct nd_msg *)skb_transport_header(skb);
  1357. __skb_push(skb, skb->data - skb_transport_header(skb));
  1358. if (ipv6_hdr(skb)->hop_limit != 255) {
  1359. ND_PRINTK2(KERN_WARNING
  1360. "ICMPv6 NDISC: invalid hop-limit: %d\n",
  1361. ipv6_hdr(skb)->hop_limit);
  1362. return 0;
  1363. }
  1364. if (msg->icmph.icmp6_code != 0) {
  1365. ND_PRINTK2(KERN_WARNING
  1366. "ICMPv6 NDISC: invalid ICMPv6 code: %d\n",
  1367. msg->icmph.icmp6_code);
  1368. return 0;
  1369. }
  1370. memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
  1371. switch (msg->icmph.icmp6_type) {
  1372. case NDISC_NEIGHBOUR_SOLICITATION:
  1373. ndisc_recv_ns(skb);
  1374. break;
  1375. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  1376. ndisc_recv_na(skb);
  1377. break;
  1378. case NDISC_ROUTER_SOLICITATION:
  1379. ndisc_recv_rs(skb);
  1380. break;
  1381. case NDISC_ROUTER_ADVERTISEMENT:
  1382. ndisc_router_discovery(skb);
  1383. break;
  1384. case NDISC_REDIRECT:
  1385. ndisc_redirect_rcv(skb);
  1386. break;
  1387. }
  1388. return 0;
  1389. }
  1390. static int ndisc_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1391. {
  1392. struct net_device *dev = ptr;
  1393. if (dev->nd_net != &init_net)
  1394. return NOTIFY_DONE;
  1395. switch (event) {
  1396. case NETDEV_CHANGEADDR:
  1397. neigh_changeaddr(&nd_tbl, dev);
  1398. fib6_run_gc(~0UL);
  1399. break;
  1400. case NETDEV_DOWN:
  1401. neigh_ifdown(&nd_tbl, dev);
  1402. fib6_run_gc(~0UL);
  1403. break;
  1404. default:
  1405. break;
  1406. }
  1407. return NOTIFY_DONE;
  1408. }
  1409. static struct notifier_block ndisc_netdev_notifier = {
  1410. .notifier_call = ndisc_netdev_event,
  1411. };
  1412. #ifdef CONFIG_SYSCTL
  1413. static void ndisc_warn_deprecated_sysctl(struct ctl_table *ctl,
  1414. const char *func, const char *dev_name)
  1415. {
  1416. static char warncomm[TASK_COMM_LEN];
  1417. static int warned;
  1418. if (strcmp(warncomm, current->comm) && warned < 5) {
  1419. strcpy(warncomm, current->comm);
  1420. printk(KERN_WARNING
  1421. "process `%s' is using deprecated sysctl (%s) "
  1422. "net.ipv6.neigh.%s.%s; "
  1423. "Use net.ipv6.neigh.%s.%s_ms "
  1424. "instead.\n",
  1425. warncomm, func,
  1426. dev_name, ctl->procname,
  1427. dev_name, ctl->procname);
  1428. warned++;
  1429. }
  1430. }
  1431. int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write, struct file * filp, void __user *buffer, size_t *lenp, loff_t *ppos)
  1432. {
  1433. struct net_device *dev = ctl->extra1;
  1434. struct inet6_dev *idev;
  1435. int ret;
  1436. if (ctl->ctl_name == NET_NEIGH_RETRANS_TIME ||
  1437. ctl->ctl_name == NET_NEIGH_REACHABLE_TIME)
  1438. ndisc_warn_deprecated_sysctl(ctl, "syscall", dev ? dev->name : "default");
  1439. switch (ctl->ctl_name) {
  1440. case NET_NEIGH_RETRANS_TIME:
  1441. ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  1442. break;
  1443. case NET_NEIGH_REACHABLE_TIME:
  1444. ret = proc_dointvec_jiffies(ctl, write,
  1445. filp, buffer, lenp, ppos);
  1446. break;
  1447. case NET_NEIGH_RETRANS_TIME_MS:
  1448. case NET_NEIGH_REACHABLE_TIME_MS:
  1449. ret = proc_dointvec_ms_jiffies(ctl, write,
  1450. filp, buffer, lenp, ppos);
  1451. break;
  1452. default:
  1453. ret = -1;
  1454. }
  1455. if (write && ret == 0 && dev && (idev = in6_dev_get(dev)) != NULL) {
  1456. if (ctl->ctl_name == NET_NEIGH_REACHABLE_TIME ||
  1457. ctl->ctl_name == NET_NEIGH_REACHABLE_TIME_MS)
  1458. idev->nd_parms->reachable_time = neigh_rand_reach_time(idev->nd_parms->base_reachable_time);
  1459. idev->tstamp = jiffies;
  1460. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  1461. in6_dev_put(idev);
  1462. }
  1463. return ret;
  1464. }
  1465. static int ndisc_ifinfo_sysctl_strategy(ctl_table *ctl, int __user *name,
  1466. int nlen, void __user *oldval,
  1467. size_t __user *oldlenp,
  1468. void __user *newval, size_t newlen)
  1469. {
  1470. struct net_device *dev = ctl->extra1;
  1471. struct inet6_dev *idev;
  1472. int ret;
  1473. if (ctl->ctl_name == NET_NEIGH_RETRANS_TIME ||
  1474. ctl->ctl_name == NET_NEIGH_REACHABLE_TIME)
  1475. ndisc_warn_deprecated_sysctl(ctl, "procfs", dev ? dev->name : "default");
  1476. switch (ctl->ctl_name) {
  1477. case NET_NEIGH_REACHABLE_TIME:
  1478. ret = sysctl_jiffies(ctl, name, nlen,
  1479. oldval, oldlenp, newval, newlen);
  1480. break;
  1481. case NET_NEIGH_RETRANS_TIME_MS:
  1482. case NET_NEIGH_REACHABLE_TIME_MS:
  1483. ret = sysctl_ms_jiffies(ctl, name, nlen,
  1484. oldval, oldlenp, newval, newlen);
  1485. break;
  1486. default:
  1487. ret = 0;
  1488. }
  1489. if (newval && newlen && ret > 0 &&
  1490. dev && (idev = in6_dev_get(dev)) != NULL) {
  1491. if (ctl->ctl_name == NET_NEIGH_REACHABLE_TIME ||
  1492. ctl->ctl_name == NET_NEIGH_REACHABLE_TIME_MS)
  1493. idev->nd_parms->reachable_time = neigh_rand_reach_time(idev->nd_parms->base_reachable_time);
  1494. idev->tstamp = jiffies;
  1495. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  1496. in6_dev_put(idev);
  1497. }
  1498. return ret;
  1499. }
  1500. #endif
  1501. int __init ndisc_init(struct net_proto_family *ops)
  1502. {
  1503. struct ipv6_pinfo *np;
  1504. struct sock *sk;
  1505. int err;
  1506. err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &ndisc_socket);
  1507. if (err < 0) {
  1508. ND_PRINTK0(KERN_ERR
  1509. "ICMPv6 NDISC: Failed to initialize the control socket (err %d).\n",
  1510. err);
  1511. ndisc_socket = NULL; /* For safety. */
  1512. return err;
  1513. }
  1514. sk = ndisc_socket->sk;
  1515. np = inet6_sk(sk);
  1516. sk->sk_allocation = GFP_ATOMIC;
  1517. np->hop_limit = 255;
  1518. /* Do not loopback ndisc messages */
  1519. np->mc_loop = 0;
  1520. sk->sk_prot->unhash(sk);
  1521. /*
  1522. * Initialize the neighbour table
  1523. */
  1524. neigh_table_init(&nd_tbl);
  1525. #ifdef CONFIG_SYSCTL
  1526. neigh_sysctl_register(NULL, &nd_tbl.parms, NET_IPV6, NET_IPV6_NEIGH,
  1527. "ipv6",
  1528. &ndisc_ifinfo_sysctl_change,
  1529. &ndisc_ifinfo_sysctl_strategy);
  1530. #endif
  1531. register_netdevice_notifier(&ndisc_netdev_notifier);
  1532. return 0;
  1533. }
  1534. void ndisc_cleanup(void)
  1535. {
  1536. unregister_netdevice_notifier(&ndisc_netdev_notifier);
  1537. #ifdef CONFIG_SYSCTL
  1538. neigh_sysctl_unregister(&nd_tbl.parms);
  1539. #endif
  1540. neigh_table_clear(&nd_tbl);
  1541. sock_release(ndisc_socket);
  1542. ndisc_socket = NULL; /* For safety. */
  1543. }