ndisc.c 43 KB

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