ndisc.c 47 KB

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