ndisc.c 46 KB

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