ndisc.c 52 KB

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