ip6mr.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447
  1. /*
  2. * Linux IPv6 multicast routing support for BSD pim6sd
  3. * Based on net/ipv4/ipmr.c.
  4. *
  5. * (c) 2004 Mickael Hoerdt, <hoerdt@clarinet.u-strasbg.fr>
  6. * LSIIT Laboratory, Strasbourg, France
  7. * (c) 2004 Jean-Philippe Andriot, <jean-philippe.andriot@6WIND.com>
  8. * 6WIND, Paris, France
  9. * Copyright (C)2007,2008 USAGI/WIDE Project
  10. * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. */
  18. #include <linux/uaccess.h>
  19. #include <linux/types.h>
  20. #include <linux/sched.h>
  21. #include <linux/errno.h>
  22. #include <linux/mm.h>
  23. #include <linux/kernel.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/stat.h>
  26. #include <linux/socket.h>
  27. #include <linux/inet.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/init.h>
  33. #include <linux/compat.h>
  34. #include <linux/rhashtable.h>
  35. #include <net/protocol.h>
  36. #include <linux/skbuff.h>
  37. #include <net/raw.h>
  38. #include <linux/notifier.h>
  39. #include <linux/if_arp.h>
  40. #include <net/checksum.h>
  41. #include <net/netlink.h>
  42. #include <net/fib_rules.h>
  43. #include <net/ipv6.h>
  44. #include <net/ip6_route.h>
  45. #include <linux/mroute6.h>
  46. #include <linux/pim.h>
  47. #include <net/addrconf.h>
  48. #include <linux/netfilter_ipv6.h>
  49. #include <linux/export.h>
  50. #include <net/ip6_checksum.h>
  51. #include <linux/netconf.h>
  52. #include <net/ip_tunnels.h>
  53. #include <linux/nospec.h>
  54. struct ip6mr_rule {
  55. struct fib_rule common;
  56. };
  57. struct ip6mr_result {
  58. struct mr_table *mrt;
  59. };
  60. /* Big lock, protecting vif table, mrt cache and mroute socket state.
  61. Note that the changes are semaphored via rtnl_lock.
  62. */
  63. static DEFINE_RWLOCK(mrt_lock);
  64. /* Multicast router control variables */
  65. /* Special spinlock for queue of unresolved entries */
  66. static DEFINE_SPINLOCK(mfc_unres_lock);
  67. /* We return to original Alan's scheme. Hash table of resolved
  68. entries is changed only in process context and protected
  69. with weak lock mrt_lock. Queue of unresolved entries is protected
  70. with strong spinlock mfc_unres_lock.
  71. In this case data path is free of exclusive locks at all.
  72. */
  73. static struct kmem_cache *mrt_cachep __read_mostly;
  74. static struct mr_table *ip6mr_new_table(struct net *net, u32 id);
  75. static void ip6mr_free_table(struct mr_table *mrt);
  76. static void ip6_mr_forward(struct net *net, struct mr_table *mrt,
  77. struct sk_buff *skb, struct mfc6_cache *cache);
  78. static int ip6mr_cache_report(struct mr_table *mrt, struct sk_buff *pkt,
  79. mifi_t mifi, int assert);
  80. static void mr6_netlink_event(struct mr_table *mrt, struct mfc6_cache *mfc,
  81. int cmd);
  82. static void mrt6msg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
  83. static int ip6mr_rtm_dumproute(struct sk_buff *skb,
  84. struct netlink_callback *cb);
  85. static void mroute_clean_tables(struct mr_table *mrt, bool all);
  86. static void ipmr_expire_process(struct timer_list *t);
  87. #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
  88. #define ip6mr_for_each_table(mrt, net) \
  89. list_for_each_entry_rcu(mrt, &net->ipv6.mr6_tables, list)
  90. static struct mr_table *ip6mr_mr_table_iter(struct net *net,
  91. struct mr_table *mrt)
  92. {
  93. struct mr_table *ret;
  94. if (!mrt)
  95. ret = list_entry_rcu(net->ipv6.mr6_tables.next,
  96. struct mr_table, list);
  97. else
  98. ret = list_entry_rcu(mrt->list.next,
  99. struct mr_table, list);
  100. if (&ret->list == &net->ipv6.mr6_tables)
  101. return NULL;
  102. return ret;
  103. }
  104. static struct mr_table *ip6mr_get_table(struct net *net, u32 id)
  105. {
  106. struct mr_table *mrt;
  107. ip6mr_for_each_table(mrt, net) {
  108. if (mrt->id == id)
  109. return mrt;
  110. }
  111. return NULL;
  112. }
  113. static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6,
  114. struct mr_table **mrt)
  115. {
  116. int err;
  117. struct ip6mr_result res;
  118. struct fib_lookup_arg arg = {
  119. .result = &res,
  120. .flags = FIB_LOOKUP_NOREF,
  121. };
  122. err = fib_rules_lookup(net->ipv6.mr6_rules_ops,
  123. flowi6_to_flowi(flp6), 0, &arg);
  124. if (err < 0)
  125. return err;
  126. *mrt = res.mrt;
  127. return 0;
  128. }
  129. static int ip6mr_rule_action(struct fib_rule *rule, struct flowi *flp,
  130. int flags, struct fib_lookup_arg *arg)
  131. {
  132. struct ip6mr_result *res = arg->result;
  133. struct mr_table *mrt;
  134. switch (rule->action) {
  135. case FR_ACT_TO_TBL:
  136. break;
  137. case FR_ACT_UNREACHABLE:
  138. return -ENETUNREACH;
  139. case FR_ACT_PROHIBIT:
  140. return -EACCES;
  141. case FR_ACT_BLACKHOLE:
  142. default:
  143. return -EINVAL;
  144. }
  145. mrt = ip6mr_get_table(rule->fr_net, rule->table);
  146. if (!mrt)
  147. return -EAGAIN;
  148. res->mrt = mrt;
  149. return 0;
  150. }
  151. static int ip6mr_rule_match(struct fib_rule *rule, struct flowi *flp, int flags)
  152. {
  153. return 1;
  154. }
  155. static const struct nla_policy ip6mr_rule_policy[FRA_MAX + 1] = {
  156. FRA_GENERIC_POLICY,
  157. };
  158. static int ip6mr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  159. struct fib_rule_hdr *frh, struct nlattr **tb,
  160. struct netlink_ext_ack *extack)
  161. {
  162. return 0;
  163. }
  164. static int ip6mr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  165. struct nlattr **tb)
  166. {
  167. return 1;
  168. }
  169. static int ip6mr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  170. struct fib_rule_hdr *frh)
  171. {
  172. frh->dst_len = 0;
  173. frh->src_len = 0;
  174. frh->tos = 0;
  175. return 0;
  176. }
  177. static const struct fib_rules_ops __net_initconst ip6mr_rules_ops_template = {
  178. .family = RTNL_FAMILY_IP6MR,
  179. .rule_size = sizeof(struct ip6mr_rule),
  180. .addr_size = sizeof(struct in6_addr),
  181. .action = ip6mr_rule_action,
  182. .match = ip6mr_rule_match,
  183. .configure = ip6mr_rule_configure,
  184. .compare = ip6mr_rule_compare,
  185. .fill = ip6mr_rule_fill,
  186. .nlgroup = RTNLGRP_IPV6_RULE,
  187. .policy = ip6mr_rule_policy,
  188. .owner = THIS_MODULE,
  189. };
  190. static int __net_init ip6mr_rules_init(struct net *net)
  191. {
  192. struct fib_rules_ops *ops;
  193. struct mr_table *mrt;
  194. int err;
  195. ops = fib_rules_register(&ip6mr_rules_ops_template, net);
  196. if (IS_ERR(ops))
  197. return PTR_ERR(ops);
  198. INIT_LIST_HEAD(&net->ipv6.mr6_tables);
  199. mrt = ip6mr_new_table(net, RT6_TABLE_DFLT);
  200. if (IS_ERR(mrt)) {
  201. err = PTR_ERR(mrt);
  202. goto err1;
  203. }
  204. err = fib_default_rule_add(ops, 0x7fff, RT6_TABLE_DFLT, 0);
  205. if (err < 0)
  206. goto err2;
  207. net->ipv6.mr6_rules_ops = ops;
  208. return 0;
  209. err2:
  210. ip6mr_free_table(mrt);
  211. err1:
  212. fib_rules_unregister(ops);
  213. return err;
  214. }
  215. static void __net_exit ip6mr_rules_exit(struct net *net)
  216. {
  217. struct mr_table *mrt, *next;
  218. rtnl_lock();
  219. list_for_each_entry_safe(mrt, next, &net->ipv6.mr6_tables, list) {
  220. list_del(&mrt->list);
  221. ip6mr_free_table(mrt);
  222. }
  223. fib_rules_unregister(net->ipv6.mr6_rules_ops);
  224. rtnl_unlock();
  225. }
  226. static int ip6mr_rules_dump(struct net *net, struct notifier_block *nb)
  227. {
  228. return fib_rules_dump(net, nb, RTNL_FAMILY_IP6MR);
  229. }
  230. static unsigned int ip6mr_rules_seq_read(struct net *net)
  231. {
  232. return fib_rules_seq_read(net, RTNL_FAMILY_IP6MR);
  233. }
  234. bool ip6mr_rule_default(const struct fib_rule *rule)
  235. {
  236. return fib_rule_matchall(rule) && rule->action == FR_ACT_TO_TBL &&
  237. rule->table == RT6_TABLE_DFLT && !rule->l3mdev;
  238. }
  239. EXPORT_SYMBOL(ip6mr_rule_default);
  240. #else
  241. #define ip6mr_for_each_table(mrt, net) \
  242. for (mrt = net->ipv6.mrt6; mrt; mrt = NULL)
  243. static struct mr_table *ip6mr_mr_table_iter(struct net *net,
  244. struct mr_table *mrt)
  245. {
  246. if (!mrt)
  247. return net->ipv6.mrt6;
  248. return NULL;
  249. }
  250. static struct mr_table *ip6mr_get_table(struct net *net, u32 id)
  251. {
  252. return net->ipv6.mrt6;
  253. }
  254. static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6,
  255. struct mr_table **mrt)
  256. {
  257. *mrt = net->ipv6.mrt6;
  258. return 0;
  259. }
  260. static int __net_init ip6mr_rules_init(struct net *net)
  261. {
  262. struct mr_table *mrt;
  263. mrt = ip6mr_new_table(net, RT6_TABLE_DFLT);
  264. if (IS_ERR(mrt))
  265. return PTR_ERR(mrt);
  266. net->ipv6.mrt6 = mrt;
  267. return 0;
  268. }
  269. static void __net_exit ip6mr_rules_exit(struct net *net)
  270. {
  271. rtnl_lock();
  272. ip6mr_free_table(net->ipv6.mrt6);
  273. net->ipv6.mrt6 = NULL;
  274. rtnl_unlock();
  275. }
  276. static int ip6mr_rules_dump(struct net *net, struct notifier_block *nb)
  277. {
  278. return 0;
  279. }
  280. static unsigned int ip6mr_rules_seq_read(struct net *net)
  281. {
  282. return 0;
  283. }
  284. #endif
  285. static int ip6mr_hash_cmp(struct rhashtable_compare_arg *arg,
  286. const void *ptr)
  287. {
  288. const struct mfc6_cache_cmp_arg *cmparg = arg->key;
  289. struct mfc6_cache *c = (struct mfc6_cache *)ptr;
  290. return !ipv6_addr_equal(&c->mf6c_mcastgrp, &cmparg->mf6c_mcastgrp) ||
  291. !ipv6_addr_equal(&c->mf6c_origin, &cmparg->mf6c_origin);
  292. }
  293. static const struct rhashtable_params ip6mr_rht_params = {
  294. .head_offset = offsetof(struct mr_mfc, mnode),
  295. .key_offset = offsetof(struct mfc6_cache, cmparg),
  296. .key_len = sizeof(struct mfc6_cache_cmp_arg),
  297. .nelem_hint = 3,
  298. .locks_mul = 1,
  299. .obj_cmpfn = ip6mr_hash_cmp,
  300. .automatic_shrinking = true,
  301. };
  302. static void ip6mr_new_table_set(struct mr_table *mrt,
  303. struct net *net)
  304. {
  305. #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
  306. list_add_tail_rcu(&mrt->list, &net->ipv6.mr6_tables);
  307. #endif
  308. }
  309. static struct mfc6_cache_cmp_arg ip6mr_mr_table_ops_cmparg_any = {
  310. .mf6c_origin = IN6ADDR_ANY_INIT,
  311. .mf6c_mcastgrp = IN6ADDR_ANY_INIT,
  312. };
  313. static struct mr_table_ops ip6mr_mr_table_ops = {
  314. .rht_params = &ip6mr_rht_params,
  315. .cmparg_any = &ip6mr_mr_table_ops_cmparg_any,
  316. };
  317. static struct mr_table *ip6mr_new_table(struct net *net, u32 id)
  318. {
  319. struct mr_table *mrt;
  320. mrt = ip6mr_get_table(net, id);
  321. if (mrt)
  322. return mrt;
  323. return mr_table_alloc(net, id, &ip6mr_mr_table_ops,
  324. ipmr_expire_process, ip6mr_new_table_set);
  325. }
  326. static void ip6mr_free_table(struct mr_table *mrt)
  327. {
  328. del_timer_sync(&mrt->ipmr_expire_timer);
  329. mroute_clean_tables(mrt, true);
  330. rhltable_destroy(&mrt->mfc_hash);
  331. kfree(mrt);
  332. }
  333. #ifdef CONFIG_PROC_FS
  334. /* The /proc interfaces to multicast routing
  335. * /proc/ip6_mr_cache /proc/ip6_mr_vif
  336. */
  337. static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
  338. __acquires(mrt_lock)
  339. {
  340. struct mr_vif_iter *iter = seq->private;
  341. struct net *net = seq_file_net(seq);
  342. struct mr_table *mrt;
  343. mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
  344. if (!mrt)
  345. return ERR_PTR(-ENOENT);
  346. iter->mrt = mrt;
  347. read_lock(&mrt_lock);
  348. return mr_vif_seq_start(seq, pos);
  349. }
  350. static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
  351. __releases(mrt_lock)
  352. {
  353. read_unlock(&mrt_lock);
  354. }
  355. static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
  356. {
  357. struct mr_vif_iter *iter = seq->private;
  358. struct mr_table *mrt = iter->mrt;
  359. if (v == SEQ_START_TOKEN) {
  360. seq_puts(seq,
  361. "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
  362. } else {
  363. const struct vif_device *vif = v;
  364. const char *name = vif->dev ? vif->dev->name : "none";
  365. seq_printf(seq,
  366. "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
  367. vif - mrt->vif_table,
  368. name, vif->bytes_in, vif->pkt_in,
  369. vif->bytes_out, vif->pkt_out,
  370. vif->flags);
  371. }
  372. return 0;
  373. }
  374. static const struct seq_operations ip6mr_vif_seq_ops = {
  375. .start = ip6mr_vif_seq_start,
  376. .next = mr_vif_seq_next,
  377. .stop = ip6mr_vif_seq_stop,
  378. .show = ip6mr_vif_seq_show,
  379. };
  380. static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
  381. {
  382. struct net *net = seq_file_net(seq);
  383. struct mr_table *mrt;
  384. mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
  385. if (!mrt)
  386. return ERR_PTR(-ENOENT);
  387. return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
  388. }
  389. static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
  390. {
  391. int n;
  392. if (v == SEQ_START_TOKEN) {
  393. seq_puts(seq,
  394. "Group "
  395. "Origin "
  396. "Iif Pkts Bytes Wrong Oifs\n");
  397. } else {
  398. const struct mfc6_cache *mfc = v;
  399. const struct mr_mfc_iter *it = seq->private;
  400. struct mr_table *mrt = it->mrt;
  401. seq_printf(seq, "%pI6 %pI6 %-3hd",
  402. &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
  403. mfc->_c.mfc_parent);
  404. if (it->cache != &mrt->mfc_unres_queue) {
  405. seq_printf(seq, " %8lu %8lu %8lu",
  406. mfc->_c.mfc_un.res.pkt,
  407. mfc->_c.mfc_un.res.bytes,
  408. mfc->_c.mfc_un.res.wrong_if);
  409. for (n = mfc->_c.mfc_un.res.minvif;
  410. n < mfc->_c.mfc_un.res.maxvif; n++) {
  411. if (VIF_EXISTS(mrt, n) &&
  412. mfc->_c.mfc_un.res.ttls[n] < 255)
  413. seq_printf(seq,
  414. " %2d:%-3d", n,
  415. mfc->_c.mfc_un.res.ttls[n]);
  416. }
  417. } else {
  418. /* unresolved mfc_caches don't contain
  419. * pkt, bytes and wrong_if values
  420. */
  421. seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
  422. }
  423. seq_putc(seq, '\n');
  424. }
  425. return 0;
  426. }
  427. static const struct seq_operations ipmr_mfc_seq_ops = {
  428. .start = ipmr_mfc_seq_start,
  429. .next = mr_mfc_seq_next,
  430. .stop = mr_mfc_seq_stop,
  431. .show = ipmr_mfc_seq_show,
  432. };
  433. #endif
  434. #ifdef CONFIG_IPV6_PIMSM_V2
  435. static int pim6_rcv(struct sk_buff *skb)
  436. {
  437. struct pimreghdr *pim;
  438. struct ipv6hdr *encap;
  439. struct net_device *reg_dev = NULL;
  440. struct net *net = dev_net(skb->dev);
  441. struct mr_table *mrt;
  442. struct flowi6 fl6 = {
  443. .flowi6_iif = skb->dev->ifindex,
  444. .flowi6_mark = skb->mark,
  445. };
  446. int reg_vif_num;
  447. if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
  448. goto drop;
  449. pim = (struct pimreghdr *)skb_transport_header(skb);
  450. if (pim->type != ((PIM_VERSION << 4) | PIM_TYPE_REGISTER) ||
  451. (pim->flags & PIM_NULL_REGISTER) ||
  452. (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  453. sizeof(*pim), IPPROTO_PIM,
  454. csum_partial((void *)pim, sizeof(*pim), 0)) &&
  455. csum_fold(skb_checksum(skb, 0, skb->len, 0))))
  456. goto drop;
  457. /* check if the inner packet is destined to mcast group */
  458. encap = (struct ipv6hdr *)(skb_transport_header(skb) +
  459. sizeof(*pim));
  460. if (!ipv6_addr_is_multicast(&encap->daddr) ||
  461. encap->payload_len == 0 ||
  462. ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
  463. goto drop;
  464. if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0)
  465. goto drop;
  466. reg_vif_num = mrt->mroute_reg_vif_num;
  467. read_lock(&mrt_lock);
  468. if (reg_vif_num >= 0)
  469. reg_dev = mrt->vif_table[reg_vif_num].dev;
  470. if (reg_dev)
  471. dev_hold(reg_dev);
  472. read_unlock(&mrt_lock);
  473. if (!reg_dev)
  474. goto drop;
  475. skb->mac_header = skb->network_header;
  476. skb_pull(skb, (u8 *)encap - skb->data);
  477. skb_reset_network_header(skb);
  478. skb->protocol = htons(ETH_P_IPV6);
  479. skb->ip_summed = CHECKSUM_NONE;
  480. skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
  481. netif_rx(skb);
  482. dev_put(reg_dev);
  483. return 0;
  484. drop:
  485. kfree_skb(skb);
  486. return 0;
  487. }
  488. static const struct inet6_protocol pim6_protocol = {
  489. .handler = pim6_rcv,
  490. };
  491. /* Service routines creating virtual interfaces: PIMREG */
  492. static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
  493. struct net_device *dev)
  494. {
  495. struct net *net = dev_net(dev);
  496. struct mr_table *mrt;
  497. struct flowi6 fl6 = {
  498. .flowi6_oif = dev->ifindex,
  499. .flowi6_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
  500. .flowi6_mark = skb->mark,
  501. };
  502. if (!pskb_inet_may_pull(skb))
  503. goto tx_err;
  504. if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0)
  505. goto tx_err;
  506. read_lock(&mrt_lock);
  507. dev->stats.tx_bytes += skb->len;
  508. dev->stats.tx_packets++;
  509. ip6mr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, MRT6MSG_WHOLEPKT);
  510. read_unlock(&mrt_lock);
  511. kfree_skb(skb);
  512. return NETDEV_TX_OK;
  513. tx_err:
  514. dev->stats.tx_errors++;
  515. kfree_skb(skb);
  516. return NETDEV_TX_OK;
  517. }
  518. static int reg_vif_get_iflink(const struct net_device *dev)
  519. {
  520. return 0;
  521. }
  522. static const struct net_device_ops reg_vif_netdev_ops = {
  523. .ndo_start_xmit = reg_vif_xmit,
  524. .ndo_get_iflink = reg_vif_get_iflink,
  525. };
  526. static void reg_vif_setup(struct net_device *dev)
  527. {
  528. dev->type = ARPHRD_PIMREG;
  529. dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
  530. dev->flags = IFF_NOARP;
  531. dev->netdev_ops = &reg_vif_netdev_ops;
  532. dev->needs_free_netdev = true;
  533. dev->features |= NETIF_F_NETNS_LOCAL;
  534. }
  535. static struct net_device *ip6mr_reg_vif(struct net *net, struct mr_table *mrt)
  536. {
  537. struct net_device *dev;
  538. char name[IFNAMSIZ];
  539. if (mrt->id == RT6_TABLE_DFLT)
  540. sprintf(name, "pim6reg");
  541. else
  542. sprintf(name, "pim6reg%u", mrt->id);
  543. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
  544. if (!dev)
  545. return NULL;
  546. dev_net_set(dev, net);
  547. if (register_netdevice(dev)) {
  548. free_netdev(dev);
  549. return NULL;
  550. }
  551. if (dev_open(dev))
  552. goto failure;
  553. dev_hold(dev);
  554. return dev;
  555. failure:
  556. unregister_netdevice(dev);
  557. return NULL;
  558. }
  559. #endif
  560. static int call_ip6mr_vif_entry_notifiers(struct net *net,
  561. enum fib_event_type event_type,
  562. struct vif_device *vif,
  563. mifi_t vif_index, u32 tb_id)
  564. {
  565. return mr_call_vif_notifiers(net, RTNL_FAMILY_IP6MR, event_type,
  566. vif, vif_index, tb_id,
  567. &net->ipv6.ipmr_seq);
  568. }
  569. static int call_ip6mr_mfc_entry_notifiers(struct net *net,
  570. enum fib_event_type event_type,
  571. struct mfc6_cache *mfc, u32 tb_id)
  572. {
  573. return mr_call_mfc_notifiers(net, RTNL_FAMILY_IP6MR, event_type,
  574. &mfc->_c, tb_id, &net->ipv6.ipmr_seq);
  575. }
  576. /* Delete a VIF entry */
  577. static int mif6_delete(struct mr_table *mrt, int vifi, int notify,
  578. struct list_head *head)
  579. {
  580. struct vif_device *v;
  581. struct net_device *dev;
  582. struct inet6_dev *in6_dev;
  583. if (vifi < 0 || vifi >= mrt->maxvif)
  584. return -EADDRNOTAVAIL;
  585. v = &mrt->vif_table[vifi];
  586. if (VIF_EXISTS(mrt, vifi))
  587. call_ip6mr_vif_entry_notifiers(read_pnet(&mrt->net),
  588. FIB_EVENT_VIF_DEL, v, vifi,
  589. mrt->id);
  590. write_lock_bh(&mrt_lock);
  591. dev = v->dev;
  592. v->dev = NULL;
  593. if (!dev) {
  594. write_unlock_bh(&mrt_lock);
  595. return -EADDRNOTAVAIL;
  596. }
  597. #ifdef CONFIG_IPV6_PIMSM_V2
  598. if (vifi == mrt->mroute_reg_vif_num)
  599. mrt->mroute_reg_vif_num = -1;
  600. #endif
  601. if (vifi + 1 == mrt->maxvif) {
  602. int tmp;
  603. for (tmp = vifi - 1; tmp >= 0; tmp--) {
  604. if (VIF_EXISTS(mrt, tmp))
  605. break;
  606. }
  607. mrt->maxvif = tmp + 1;
  608. }
  609. write_unlock_bh(&mrt_lock);
  610. dev_set_allmulti(dev, -1);
  611. in6_dev = __in6_dev_get(dev);
  612. if (in6_dev) {
  613. in6_dev->cnf.mc_forwarding--;
  614. inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
  615. NETCONFA_MC_FORWARDING,
  616. dev->ifindex, &in6_dev->cnf);
  617. }
  618. if ((v->flags & MIFF_REGISTER) && !notify)
  619. unregister_netdevice_queue(dev, head);
  620. dev_put(dev);
  621. return 0;
  622. }
  623. static inline void ip6mr_cache_free_rcu(struct rcu_head *head)
  624. {
  625. struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
  626. kmem_cache_free(mrt_cachep, (struct mfc6_cache *)c);
  627. }
  628. static inline void ip6mr_cache_free(struct mfc6_cache *c)
  629. {
  630. call_rcu(&c->_c.rcu, ip6mr_cache_free_rcu);
  631. }
  632. /* Destroy an unresolved cache entry, killing queued skbs
  633. and reporting error to netlink readers.
  634. */
  635. static void ip6mr_destroy_unres(struct mr_table *mrt, struct mfc6_cache *c)
  636. {
  637. struct net *net = read_pnet(&mrt->net);
  638. struct sk_buff *skb;
  639. atomic_dec(&mrt->cache_resolve_queue_len);
  640. while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved)) != NULL) {
  641. if (ipv6_hdr(skb)->version == 0) {
  642. struct nlmsghdr *nlh = skb_pull(skb,
  643. sizeof(struct ipv6hdr));
  644. nlh->nlmsg_type = NLMSG_ERROR;
  645. nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
  646. skb_trim(skb, nlh->nlmsg_len);
  647. ((struct nlmsgerr *)nlmsg_data(nlh))->error = -ETIMEDOUT;
  648. rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
  649. } else
  650. kfree_skb(skb);
  651. }
  652. ip6mr_cache_free(c);
  653. }
  654. /* Timer process for all the unresolved queue. */
  655. static void ipmr_do_expire_process(struct mr_table *mrt)
  656. {
  657. unsigned long now = jiffies;
  658. unsigned long expires = 10 * HZ;
  659. struct mr_mfc *c, *next;
  660. list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
  661. if (time_after(c->mfc_un.unres.expires, now)) {
  662. /* not yet... */
  663. unsigned long interval = c->mfc_un.unres.expires - now;
  664. if (interval < expires)
  665. expires = interval;
  666. continue;
  667. }
  668. list_del(&c->list);
  669. mr6_netlink_event(mrt, (struct mfc6_cache *)c, RTM_DELROUTE);
  670. ip6mr_destroy_unres(mrt, (struct mfc6_cache *)c);
  671. }
  672. if (!list_empty(&mrt->mfc_unres_queue))
  673. mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
  674. }
  675. static void ipmr_expire_process(struct timer_list *t)
  676. {
  677. struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
  678. if (!spin_trylock(&mfc_unres_lock)) {
  679. mod_timer(&mrt->ipmr_expire_timer, jiffies + 1);
  680. return;
  681. }
  682. if (!list_empty(&mrt->mfc_unres_queue))
  683. ipmr_do_expire_process(mrt);
  684. spin_unlock(&mfc_unres_lock);
  685. }
  686. /* Fill oifs list. It is called under write locked mrt_lock. */
  687. static void ip6mr_update_thresholds(struct mr_table *mrt,
  688. struct mr_mfc *cache,
  689. unsigned char *ttls)
  690. {
  691. int vifi;
  692. cache->mfc_un.res.minvif = MAXMIFS;
  693. cache->mfc_un.res.maxvif = 0;
  694. memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
  695. for (vifi = 0; vifi < mrt->maxvif; vifi++) {
  696. if (VIF_EXISTS(mrt, vifi) &&
  697. ttls[vifi] && ttls[vifi] < 255) {
  698. cache->mfc_un.res.ttls[vifi] = ttls[vifi];
  699. if (cache->mfc_un.res.minvif > vifi)
  700. cache->mfc_un.res.minvif = vifi;
  701. if (cache->mfc_un.res.maxvif <= vifi)
  702. cache->mfc_un.res.maxvif = vifi + 1;
  703. }
  704. }
  705. cache->mfc_un.res.lastuse = jiffies;
  706. }
  707. static int mif6_add(struct net *net, struct mr_table *mrt,
  708. struct mif6ctl *vifc, int mrtsock)
  709. {
  710. int vifi = vifc->mif6c_mifi;
  711. struct vif_device *v = &mrt->vif_table[vifi];
  712. struct net_device *dev;
  713. struct inet6_dev *in6_dev;
  714. int err;
  715. /* Is vif busy ? */
  716. if (VIF_EXISTS(mrt, vifi))
  717. return -EADDRINUSE;
  718. switch (vifc->mif6c_flags) {
  719. #ifdef CONFIG_IPV6_PIMSM_V2
  720. case MIFF_REGISTER:
  721. /*
  722. * Special Purpose VIF in PIM
  723. * All the packets will be sent to the daemon
  724. */
  725. if (mrt->mroute_reg_vif_num >= 0)
  726. return -EADDRINUSE;
  727. dev = ip6mr_reg_vif(net, mrt);
  728. if (!dev)
  729. return -ENOBUFS;
  730. err = dev_set_allmulti(dev, 1);
  731. if (err) {
  732. unregister_netdevice(dev);
  733. dev_put(dev);
  734. return err;
  735. }
  736. break;
  737. #endif
  738. case 0:
  739. dev = dev_get_by_index(net, vifc->mif6c_pifi);
  740. if (!dev)
  741. return -EADDRNOTAVAIL;
  742. err = dev_set_allmulti(dev, 1);
  743. if (err) {
  744. dev_put(dev);
  745. return err;
  746. }
  747. break;
  748. default:
  749. return -EINVAL;
  750. }
  751. in6_dev = __in6_dev_get(dev);
  752. if (in6_dev) {
  753. in6_dev->cnf.mc_forwarding++;
  754. inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
  755. NETCONFA_MC_FORWARDING,
  756. dev->ifindex, &in6_dev->cnf);
  757. }
  758. /* Fill in the VIF structures */
  759. vif_device_init(v, dev, vifc->vifc_rate_limit, vifc->vifc_threshold,
  760. vifc->mif6c_flags | (!mrtsock ? VIFF_STATIC : 0),
  761. MIFF_REGISTER);
  762. /* And finish update writing critical data */
  763. write_lock_bh(&mrt_lock);
  764. v->dev = dev;
  765. #ifdef CONFIG_IPV6_PIMSM_V2
  766. if (v->flags & MIFF_REGISTER)
  767. mrt->mroute_reg_vif_num = vifi;
  768. #endif
  769. if (vifi + 1 > mrt->maxvif)
  770. mrt->maxvif = vifi + 1;
  771. write_unlock_bh(&mrt_lock);
  772. call_ip6mr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD,
  773. v, vifi, mrt->id);
  774. return 0;
  775. }
  776. static struct mfc6_cache *ip6mr_cache_find(struct mr_table *mrt,
  777. const struct in6_addr *origin,
  778. const struct in6_addr *mcastgrp)
  779. {
  780. struct mfc6_cache_cmp_arg arg = {
  781. .mf6c_origin = *origin,
  782. .mf6c_mcastgrp = *mcastgrp,
  783. };
  784. return mr_mfc_find(mrt, &arg);
  785. }
  786. /* Look for a (*,G) entry */
  787. static struct mfc6_cache *ip6mr_cache_find_any(struct mr_table *mrt,
  788. struct in6_addr *mcastgrp,
  789. mifi_t mifi)
  790. {
  791. struct mfc6_cache_cmp_arg arg = {
  792. .mf6c_origin = in6addr_any,
  793. .mf6c_mcastgrp = *mcastgrp,
  794. };
  795. if (ipv6_addr_any(mcastgrp))
  796. return mr_mfc_find_any_parent(mrt, mifi);
  797. return mr_mfc_find_any(mrt, mifi, &arg);
  798. }
  799. /* Look for a (S,G,iif) entry if parent != -1 */
  800. static struct mfc6_cache *
  801. ip6mr_cache_find_parent(struct mr_table *mrt,
  802. const struct in6_addr *origin,
  803. const struct in6_addr *mcastgrp,
  804. int parent)
  805. {
  806. struct mfc6_cache_cmp_arg arg = {
  807. .mf6c_origin = *origin,
  808. .mf6c_mcastgrp = *mcastgrp,
  809. };
  810. return mr_mfc_find_parent(mrt, &arg, parent);
  811. }
  812. /* Allocate a multicast cache entry */
  813. static struct mfc6_cache *ip6mr_cache_alloc(void)
  814. {
  815. struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
  816. if (!c)
  817. return NULL;
  818. c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
  819. c->_c.mfc_un.res.minvif = MAXMIFS;
  820. c->_c.free = ip6mr_cache_free_rcu;
  821. refcount_set(&c->_c.mfc_un.res.refcount, 1);
  822. return c;
  823. }
  824. static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
  825. {
  826. struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
  827. if (!c)
  828. return NULL;
  829. skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
  830. c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
  831. return c;
  832. }
  833. /*
  834. * A cache entry has gone into a resolved state from queued
  835. */
  836. static void ip6mr_cache_resolve(struct net *net, struct mr_table *mrt,
  837. struct mfc6_cache *uc, struct mfc6_cache *c)
  838. {
  839. struct sk_buff *skb;
  840. /*
  841. * Play the pending entries through our router
  842. */
  843. while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
  844. if (ipv6_hdr(skb)->version == 0) {
  845. struct nlmsghdr *nlh = skb_pull(skb,
  846. sizeof(struct ipv6hdr));
  847. if (mr_fill_mroute(mrt, skb, &c->_c,
  848. nlmsg_data(nlh)) > 0) {
  849. nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
  850. } else {
  851. nlh->nlmsg_type = NLMSG_ERROR;
  852. nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
  853. skb_trim(skb, nlh->nlmsg_len);
  854. ((struct nlmsgerr *)nlmsg_data(nlh))->error = -EMSGSIZE;
  855. }
  856. rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
  857. } else
  858. ip6_mr_forward(net, mrt, skb, c);
  859. }
  860. }
  861. /*
  862. * Bounce a cache query up to pim6sd and netlink.
  863. *
  864. * Called under mrt_lock.
  865. */
  866. static int ip6mr_cache_report(struct mr_table *mrt, struct sk_buff *pkt,
  867. mifi_t mifi, int assert)
  868. {
  869. struct sock *mroute6_sk;
  870. struct sk_buff *skb;
  871. struct mrt6msg *msg;
  872. int ret;
  873. #ifdef CONFIG_IPV6_PIMSM_V2
  874. if (assert == MRT6MSG_WHOLEPKT)
  875. skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
  876. +sizeof(*msg));
  877. else
  878. #endif
  879. skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
  880. if (!skb)
  881. return -ENOBUFS;
  882. /* I suppose that internal messages
  883. * do not require checksums */
  884. skb->ip_summed = CHECKSUM_UNNECESSARY;
  885. #ifdef CONFIG_IPV6_PIMSM_V2
  886. if (assert == MRT6MSG_WHOLEPKT) {
  887. /* Ugly, but we have no choice with this interface.
  888. Duplicate old header, fix length etc.
  889. And all this only to mangle msg->im6_msgtype and
  890. to set msg->im6_mbz to "mbz" :-)
  891. */
  892. skb_push(skb, -skb_network_offset(pkt));
  893. skb_push(skb, sizeof(*msg));
  894. skb_reset_transport_header(skb);
  895. msg = (struct mrt6msg *)skb_transport_header(skb);
  896. msg->im6_mbz = 0;
  897. msg->im6_msgtype = MRT6MSG_WHOLEPKT;
  898. msg->im6_mif = mrt->mroute_reg_vif_num;
  899. msg->im6_pad = 0;
  900. msg->im6_src = ipv6_hdr(pkt)->saddr;
  901. msg->im6_dst = ipv6_hdr(pkt)->daddr;
  902. skb->ip_summed = CHECKSUM_UNNECESSARY;
  903. } else
  904. #endif
  905. {
  906. /*
  907. * Copy the IP header
  908. */
  909. skb_put(skb, sizeof(struct ipv6hdr));
  910. skb_reset_network_header(skb);
  911. skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
  912. /*
  913. * Add our header
  914. */
  915. skb_put(skb, sizeof(*msg));
  916. skb_reset_transport_header(skb);
  917. msg = (struct mrt6msg *)skb_transport_header(skb);
  918. msg->im6_mbz = 0;
  919. msg->im6_msgtype = assert;
  920. msg->im6_mif = mifi;
  921. msg->im6_pad = 0;
  922. msg->im6_src = ipv6_hdr(pkt)->saddr;
  923. msg->im6_dst = ipv6_hdr(pkt)->daddr;
  924. skb_dst_set(skb, dst_clone(skb_dst(pkt)));
  925. skb->ip_summed = CHECKSUM_UNNECESSARY;
  926. }
  927. rcu_read_lock();
  928. mroute6_sk = rcu_dereference(mrt->mroute_sk);
  929. if (!mroute6_sk) {
  930. rcu_read_unlock();
  931. kfree_skb(skb);
  932. return -EINVAL;
  933. }
  934. mrt6msg_netlink_event(mrt, skb);
  935. /* Deliver to user space multicast routing algorithms */
  936. ret = sock_queue_rcv_skb(mroute6_sk, skb);
  937. rcu_read_unlock();
  938. if (ret < 0) {
  939. net_warn_ratelimited("mroute6: pending queue full, dropping entries\n");
  940. kfree_skb(skb);
  941. }
  942. return ret;
  943. }
  944. /* Queue a packet for resolution. It gets locked cache entry! */
  945. static int ip6mr_cache_unresolved(struct mr_table *mrt, mifi_t mifi,
  946. struct sk_buff *skb)
  947. {
  948. struct mfc6_cache *c;
  949. bool found = false;
  950. int err;
  951. spin_lock_bh(&mfc_unres_lock);
  952. list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
  953. if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
  954. ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) {
  955. found = true;
  956. break;
  957. }
  958. }
  959. if (!found) {
  960. /*
  961. * Create a new entry if allowable
  962. */
  963. if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
  964. (c = ip6mr_cache_alloc_unres()) == NULL) {
  965. spin_unlock_bh(&mfc_unres_lock);
  966. kfree_skb(skb);
  967. return -ENOBUFS;
  968. }
  969. /* Fill in the new cache entry */
  970. c->_c.mfc_parent = -1;
  971. c->mf6c_origin = ipv6_hdr(skb)->saddr;
  972. c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
  973. /*
  974. * Reflect first query at pim6sd
  975. */
  976. err = ip6mr_cache_report(mrt, skb, mifi, MRT6MSG_NOCACHE);
  977. if (err < 0) {
  978. /* If the report failed throw the cache entry
  979. out - Brad Parker
  980. */
  981. spin_unlock_bh(&mfc_unres_lock);
  982. ip6mr_cache_free(c);
  983. kfree_skb(skb);
  984. return err;
  985. }
  986. atomic_inc(&mrt->cache_resolve_queue_len);
  987. list_add(&c->_c.list, &mrt->mfc_unres_queue);
  988. mr6_netlink_event(mrt, c, RTM_NEWROUTE);
  989. ipmr_do_expire_process(mrt);
  990. }
  991. /* See if we can append the packet */
  992. if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
  993. kfree_skb(skb);
  994. err = -ENOBUFS;
  995. } else {
  996. skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
  997. err = 0;
  998. }
  999. spin_unlock_bh(&mfc_unres_lock);
  1000. return err;
  1001. }
  1002. /*
  1003. * MFC6 cache manipulation by user space
  1004. */
  1005. static int ip6mr_mfc_delete(struct mr_table *mrt, struct mf6cctl *mfc,
  1006. int parent)
  1007. {
  1008. struct mfc6_cache *c;
  1009. /* The entries are added/deleted only under RTNL */
  1010. rcu_read_lock();
  1011. c = ip6mr_cache_find_parent(mrt, &mfc->mf6cc_origin.sin6_addr,
  1012. &mfc->mf6cc_mcastgrp.sin6_addr, parent);
  1013. rcu_read_unlock();
  1014. if (!c)
  1015. return -ENOENT;
  1016. rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ip6mr_rht_params);
  1017. list_del_rcu(&c->_c.list);
  1018. call_ip6mr_mfc_entry_notifiers(read_pnet(&mrt->net),
  1019. FIB_EVENT_ENTRY_DEL, c, mrt->id);
  1020. mr6_netlink_event(mrt, c, RTM_DELROUTE);
  1021. mr_cache_put(&c->_c);
  1022. return 0;
  1023. }
  1024. static int ip6mr_device_event(struct notifier_block *this,
  1025. unsigned long event, void *ptr)
  1026. {
  1027. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1028. struct net *net = dev_net(dev);
  1029. struct mr_table *mrt;
  1030. struct vif_device *v;
  1031. int ct;
  1032. if (event != NETDEV_UNREGISTER)
  1033. return NOTIFY_DONE;
  1034. ip6mr_for_each_table(mrt, net) {
  1035. v = &mrt->vif_table[0];
  1036. for (ct = 0; ct < mrt->maxvif; ct++, v++) {
  1037. if (v->dev == dev)
  1038. mif6_delete(mrt, ct, 1, NULL);
  1039. }
  1040. }
  1041. return NOTIFY_DONE;
  1042. }
  1043. static unsigned int ip6mr_seq_read(struct net *net)
  1044. {
  1045. ASSERT_RTNL();
  1046. return net->ipv6.ipmr_seq + ip6mr_rules_seq_read(net);
  1047. }
  1048. static int ip6mr_dump(struct net *net, struct notifier_block *nb)
  1049. {
  1050. return mr_dump(net, nb, RTNL_FAMILY_IP6MR, ip6mr_rules_dump,
  1051. ip6mr_mr_table_iter, &mrt_lock);
  1052. }
  1053. static struct notifier_block ip6_mr_notifier = {
  1054. .notifier_call = ip6mr_device_event
  1055. };
  1056. static const struct fib_notifier_ops ip6mr_notifier_ops_template = {
  1057. .family = RTNL_FAMILY_IP6MR,
  1058. .fib_seq_read = ip6mr_seq_read,
  1059. .fib_dump = ip6mr_dump,
  1060. .owner = THIS_MODULE,
  1061. };
  1062. static int __net_init ip6mr_notifier_init(struct net *net)
  1063. {
  1064. struct fib_notifier_ops *ops;
  1065. net->ipv6.ipmr_seq = 0;
  1066. ops = fib_notifier_ops_register(&ip6mr_notifier_ops_template, net);
  1067. if (IS_ERR(ops))
  1068. return PTR_ERR(ops);
  1069. net->ipv6.ip6mr_notifier_ops = ops;
  1070. return 0;
  1071. }
  1072. static void __net_exit ip6mr_notifier_exit(struct net *net)
  1073. {
  1074. fib_notifier_ops_unregister(net->ipv6.ip6mr_notifier_ops);
  1075. net->ipv6.ip6mr_notifier_ops = NULL;
  1076. }
  1077. /* Setup for IP multicast routing */
  1078. static int __net_init ip6mr_net_init(struct net *net)
  1079. {
  1080. int err;
  1081. err = ip6mr_notifier_init(net);
  1082. if (err)
  1083. return err;
  1084. err = ip6mr_rules_init(net);
  1085. if (err < 0)
  1086. goto ip6mr_rules_fail;
  1087. #ifdef CONFIG_PROC_FS
  1088. err = -ENOMEM;
  1089. if (!proc_create_net("ip6_mr_vif", 0, net->proc_net, &ip6mr_vif_seq_ops,
  1090. sizeof(struct mr_vif_iter)))
  1091. goto proc_vif_fail;
  1092. if (!proc_create_net("ip6_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
  1093. sizeof(struct mr_mfc_iter)))
  1094. goto proc_cache_fail;
  1095. #endif
  1096. return 0;
  1097. #ifdef CONFIG_PROC_FS
  1098. proc_cache_fail:
  1099. remove_proc_entry("ip6_mr_vif", net->proc_net);
  1100. proc_vif_fail:
  1101. ip6mr_rules_exit(net);
  1102. #endif
  1103. ip6mr_rules_fail:
  1104. ip6mr_notifier_exit(net);
  1105. return err;
  1106. }
  1107. static void __net_exit ip6mr_net_exit(struct net *net)
  1108. {
  1109. #ifdef CONFIG_PROC_FS
  1110. remove_proc_entry("ip6_mr_cache", net->proc_net);
  1111. remove_proc_entry("ip6_mr_vif", net->proc_net);
  1112. #endif
  1113. ip6mr_rules_exit(net);
  1114. ip6mr_notifier_exit(net);
  1115. }
  1116. static struct pernet_operations ip6mr_net_ops = {
  1117. .init = ip6mr_net_init,
  1118. .exit = ip6mr_net_exit,
  1119. };
  1120. int __init ip6_mr_init(void)
  1121. {
  1122. int err;
  1123. mrt_cachep = kmem_cache_create("ip6_mrt_cache",
  1124. sizeof(struct mfc6_cache),
  1125. 0, SLAB_HWCACHE_ALIGN,
  1126. NULL);
  1127. if (!mrt_cachep)
  1128. return -ENOMEM;
  1129. err = register_pernet_subsys(&ip6mr_net_ops);
  1130. if (err)
  1131. goto reg_pernet_fail;
  1132. err = register_netdevice_notifier(&ip6_mr_notifier);
  1133. if (err)
  1134. goto reg_notif_fail;
  1135. #ifdef CONFIG_IPV6_PIMSM_V2
  1136. if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
  1137. pr_err("%s: can't add PIM protocol\n", __func__);
  1138. err = -EAGAIN;
  1139. goto add_proto_fail;
  1140. }
  1141. #endif
  1142. err = rtnl_register_module(THIS_MODULE, RTNL_FAMILY_IP6MR, RTM_GETROUTE,
  1143. NULL, ip6mr_rtm_dumproute, 0);
  1144. if (err == 0)
  1145. return 0;
  1146. #ifdef CONFIG_IPV6_PIMSM_V2
  1147. inet6_del_protocol(&pim6_protocol, IPPROTO_PIM);
  1148. add_proto_fail:
  1149. unregister_netdevice_notifier(&ip6_mr_notifier);
  1150. #endif
  1151. reg_notif_fail:
  1152. unregister_pernet_subsys(&ip6mr_net_ops);
  1153. reg_pernet_fail:
  1154. kmem_cache_destroy(mrt_cachep);
  1155. return err;
  1156. }
  1157. void ip6_mr_cleanup(void)
  1158. {
  1159. rtnl_unregister(RTNL_FAMILY_IP6MR, RTM_GETROUTE);
  1160. #ifdef CONFIG_IPV6_PIMSM_V2
  1161. inet6_del_protocol(&pim6_protocol, IPPROTO_PIM);
  1162. #endif
  1163. unregister_netdevice_notifier(&ip6_mr_notifier);
  1164. unregister_pernet_subsys(&ip6mr_net_ops);
  1165. kmem_cache_destroy(mrt_cachep);
  1166. }
  1167. static int ip6mr_mfc_add(struct net *net, struct mr_table *mrt,
  1168. struct mf6cctl *mfc, int mrtsock, int parent)
  1169. {
  1170. unsigned char ttls[MAXMIFS];
  1171. struct mfc6_cache *uc, *c;
  1172. struct mr_mfc *_uc;
  1173. bool found;
  1174. int i, err;
  1175. if (mfc->mf6cc_parent >= MAXMIFS)
  1176. return -ENFILE;
  1177. memset(ttls, 255, MAXMIFS);
  1178. for (i = 0; i < MAXMIFS; i++) {
  1179. if (IF_ISSET(i, &mfc->mf6cc_ifset))
  1180. ttls[i] = 1;
  1181. }
  1182. /* The entries are added/deleted only under RTNL */
  1183. rcu_read_lock();
  1184. c = ip6mr_cache_find_parent(mrt, &mfc->mf6cc_origin.sin6_addr,
  1185. &mfc->mf6cc_mcastgrp.sin6_addr, parent);
  1186. rcu_read_unlock();
  1187. if (c) {
  1188. write_lock_bh(&mrt_lock);
  1189. c->_c.mfc_parent = mfc->mf6cc_parent;
  1190. ip6mr_update_thresholds(mrt, &c->_c, ttls);
  1191. if (!mrtsock)
  1192. c->_c.mfc_flags |= MFC_STATIC;
  1193. write_unlock_bh(&mrt_lock);
  1194. call_ip6mr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE,
  1195. c, mrt->id);
  1196. mr6_netlink_event(mrt, c, RTM_NEWROUTE);
  1197. return 0;
  1198. }
  1199. if (!ipv6_addr_any(&mfc->mf6cc_mcastgrp.sin6_addr) &&
  1200. !ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
  1201. return -EINVAL;
  1202. c = ip6mr_cache_alloc();
  1203. if (!c)
  1204. return -ENOMEM;
  1205. c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
  1206. c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
  1207. c->_c.mfc_parent = mfc->mf6cc_parent;
  1208. ip6mr_update_thresholds(mrt, &c->_c, ttls);
  1209. if (!mrtsock)
  1210. c->_c.mfc_flags |= MFC_STATIC;
  1211. err = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
  1212. ip6mr_rht_params);
  1213. if (err) {
  1214. pr_err("ip6mr: rhtable insert error %d\n", err);
  1215. ip6mr_cache_free(c);
  1216. return err;
  1217. }
  1218. list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
  1219. /* Check to see if we resolved a queued list. If so we
  1220. * need to send on the frames and tidy up.
  1221. */
  1222. found = false;
  1223. spin_lock_bh(&mfc_unres_lock);
  1224. list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
  1225. uc = (struct mfc6_cache *)_uc;
  1226. if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
  1227. ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
  1228. list_del(&_uc->list);
  1229. atomic_dec(&mrt->cache_resolve_queue_len);
  1230. found = true;
  1231. break;
  1232. }
  1233. }
  1234. if (list_empty(&mrt->mfc_unres_queue))
  1235. del_timer(&mrt->ipmr_expire_timer);
  1236. spin_unlock_bh(&mfc_unres_lock);
  1237. if (found) {
  1238. ip6mr_cache_resolve(net, mrt, uc, c);
  1239. ip6mr_cache_free(uc);
  1240. }
  1241. call_ip6mr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD,
  1242. c, mrt->id);
  1243. mr6_netlink_event(mrt, c, RTM_NEWROUTE);
  1244. return 0;
  1245. }
  1246. /*
  1247. * Close the multicast socket, and clear the vif tables etc
  1248. */
  1249. static void mroute_clean_tables(struct mr_table *mrt, bool all)
  1250. {
  1251. struct mr_mfc *c, *tmp;
  1252. LIST_HEAD(list);
  1253. int i;
  1254. /* Shut down all active vif entries */
  1255. for (i = 0; i < mrt->maxvif; i++) {
  1256. if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
  1257. continue;
  1258. mif6_delete(mrt, i, 0, &list);
  1259. }
  1260. unregister_netdevice_many(&list);
  1261. /* Wipe the cache */
  1262. list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
  1263. if (!all && (c->mfc_flags & MFC_STATIC))
  1264. continue;
  1265. rhltable_remove(&mrt->mfc_hash, &c->mnode, ip6mr_rht_params);
  1266. list_del_rcu(&c->list);
  1267. call_ip6mr_mfc_entry_notifiers(read_pnet(&mrt->net),
  1268. FIB_EVENT_ENTRY_DEL,
  1269. (struct mfc6_cache *)c, mrt->id);
  1270. mr6_netlink_event(mrt, (struct mfc6_cache *)c, RTM_DELROUTE);
  1271. mr_cache_put(c);
  1272. }
  1273. if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
  1274. spin_lock_bh(&mfc_unres_lock);
  1275. list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
  1276. list_del(&c->list);
  1277. mr6_netlink_event(mrt, (struct mfc6_cache *)c,
  1278. RTM_DELROUTE);
  1279. ip6mr_destroy_unres(mrt, (struct mfc6_cache *)c);
  1280. }
  1281. spin_unlock_bh(&mfc_unres_lock);
  1282. }
  1283. }
  1284. static int ip6mr_sk_init(struct mr_table *mrt, struct sock *sk)
  1285. {
  1286. int err = 0;
  1287. struct net *net = sock_net(sk);
  1288. rtnl_lock();
  1289. write_lock_bh(&mrt_lock);
  1290. if (rtnl_dereference(mrt->mroute_sk)) {
  1291. err = -EADDRINUSE;
  1292. } else {
  1293. rcu_assign_pointer(mrt->mroute_sk, sk);
  1294. sock_set_flag(sk, SOCK_RCU_FREE);
  1295. net->ipv6.devconf_all->mc_forwarding++;
  1296. }
  1297. write_unlock_bh(&mrt_lock);
  1298. if (!err)
  1299. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1300. NETCONFA_MC_FORWARDING,
  1301. NETCONFA_IFINDEX_ALL,
  1302. net->ipv6.devconf_all);
  1303. rtnl_unlock();
  1304. return err;
  1305. }
  1306. int ip6mr_sk_done(struct sock *sk)
  1307. {
  1308. int err = -EACCES;
  1309. struct net *net = sock_net(sk);
  1310. struct mr_table *mrt;
  1311. if (sk->sk_type != SOCK_RAW ||
  1312. inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  1313. return err;
  1314. rtnl_lock();
  1315. ip6mr_for_each_table(mrt, net) {
  1316. if (sk == rtnl_dereference(mrt->mroute_sk)) {
  1317. write_lock_bh(&mrt_lock);
  1318. RCU_INIT_POINTER(mrt->mroute_sk, NULL);
  1319. /* Note that mroute_sk had SOCK_RCU_FREE set,
  1320. * so the RCU grace period before sk freeing
  1321. * is guaranteed by sk_destruct()
  1322. */
  1323. net->ipv6.devconf_all->mc_forwarding--;
  1324. write_unlock_bh(&mrt_lock);
  1325. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1326. NETCONFA_MC_FORWARDING,
  1327. NETCONFA_IFINDEX_ALL,
  1328. net->ipv6.devconf_all);
  1329. mroute_clean_tables(mrt, false);
  1330. err = 0;
  1331. break;
  1332. }
  1333. }
  1334. rtnl_unlock();
  1335. return err;
  1336. }
  1337. bool mroute6_is_socket(struct net *net, struct sk_buff *skb)
  1338. {
  1339. struct mr_table *mrt;
  1340. struct flowi6 fl6 = {
  1341. .flowi6_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
  1342. .flowi6_oif = skb->dev->ifindex,
  1343. .flowi6_mark = skb->mark,
  1344. };
  1345. if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0)
  1346. return NULL;
  1347. return rcu_access_pointer(mrt->mroute_sk);
  1348. }
  1349. EXPORT_SYMBOL(mroute6_is_socket);
  1350. /*
  1351. * Socket options and virtual interface manipulation. The whole
  1352. * virtual interface system is a complete heap, but unfortunately
  1353. * that's how BSD mrouted happens to think. Maybe one day with a proper
  1354. * MOSPF/PIM router set up we can clean this up.
  1355. */
  1356. int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
  1357. {
  1358. int ret, parent = 0;
  1359. struct mif6ctl vif;
  1360. struct mf6cctl mfc;
  1361. mifi_t mifi;
  1362. struct net *net = sock_net(sk);
  1363. struct mr_table *mrt;
  1364. if (sk->sk_type != SOCK_RAW ||
  1365. inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  1366. return -EOPNOTSUPP;
  1367. mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
  1368. if (!mrt)
  1369. return -ENOENT;
  1370. if (optname != MRT6_INIT) {
  1371. if (sk != rcu_access_pointer(mrt->mroute_sk) &&
  1372. !ns_capable(net->user_ns, CAP_NET_ADMIN))
  1373. return -EACCES;
  1374. }
  1375. switch (optname) {
  1376. case MRT6_INIT:
  1377. if (optlen < sizeof(int))
  1378. return -EINVAL;
  1379. return ip6mr_sk_init(mrt, sk);
  1380. case MRT6_DONE:
  1381. return ip6mr_sk_done(sk);
  1382. case MRT6_ADD_MIF:
  1383. if (optlen < sizeof(vif))
  1384. return -EINVAL;
  1385. if (copy_from_user(&vif, optval, sizeof(vif)))
  1386. return -EFAULT;
  1387. if (vif.mif6c_mifi >= MAXMIFS)
  1388. return -ENFILE;
  1389. rtnl_lock();
  1390. ret = mif6_add(net, mrt, &vif,
  1391. sk == rtnl_dereference(mrt->mroute_sk));
  1392. rtnl_unlock();
  1393. return ret;
  1394. case MRT6_DEL_MIF:
  1395. if (optlen < sizeof(mifi_t))
  1396. return -EINVAL;
  1397. if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
  1398. return -EFAULT;
  1399. rtnl_lock();
  1400. ret = mif6_delete(mrt, mifi, 0, NULL);
  1401. rtnl_unlock();
  1402. return ret;
  1403. /*
  1404. * Manipulate the forwarding caches. These live
  1405. * in a sort of kernel/user symbiosis.
  1406. */
  1407. case MRT6_ADD_MFC:
  1408. case MRT6_DEL_MFC:
  1409. parent = -1;
  1410. /* fall through */
  1411. case MRT6_ADD_MFC_PROXY:
  1412. case MRT6_DEL_MFC_PROXY:
  1413. if (optlen < sizeof(mfc))
  1414. return -EINVAL;
  1415. if (copy_from_user(&mfc, optval, sizeof(mfc)))
  1416. return -EFAULT;
  1417. if (parent == 0)
  1418. parent = mfc.mf6cc_parent;
  1419. rtnl_lock();
  1420. if (optname == MRT6_DEL_MFC || optname == MRT6_DEL_MFC_PROXY)
  1421. ret = ip6mr_mfc_delete(mrt, &mfc, parent);
  1422. else
  1423. ret = ip6mr_mfc_add(net, mrt, &mfc,
  1424. sk ==
  1425. rtnl_dereference(mrt->mroute_sk),
  1426. parent);
  1427. rtnl_unlock();
  1428. return ret;
  1429. /*
  1430. * Control PIM assert (to activate pim will activate assert)
  1431. */
  1432. case MRT6_ASSERT:
  1433. {
  1434. int v;
  1435. if (optlen != sizeof(v))
  1436. return -EINVAL;
  1437. if (get_user(v, (int __user *)optval))
  1438. return -EFAULT;
  1439. mrt->mroute_do_assert = v;
  1440. return 0;
  1441. }
  1442. #ifdef CONFIG_IPV6_PIMSM_V2
  1443. case MRT6_PIM:
  1444. {
  1445. int v;
  1446. if (optlen != sizeof(v))
  1447. return -EINVAL;
  1448. if (get_user(v, (int __user *)optval))
  1449. return -EFAULT;
  1450. v = !!v;
  1451. rtnl_lock();
  1452. ret = 0;
  1453. if (v != mrt->mroute_do_pim) {
  1454. mrt->mroute_do_pim = v;
  1455. mrt->mroute_do_assert = v;
  1456. }
  1457. rtnl_unlock();
  1458. return ret;
  1459. }
  1460. #endif
  1461. #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
  1462. case MRT6_TABLE:
  1463. {
  1464. u32 v;
  1465. if (optlen != sizeof(u32))
  1466. return -EINVAL;
  1467. if (get_user(v, (u32 __user *)optval))
  1468. return -EFAULT;
  1469. /* "pim6reg%u" should not exceed 16 bytes (IFNAMSIZ) */
  1470. if (v != RT_TABLE_DEFAULT && v >= 100000000)
  1471. return -EINVAL;
  1472. if (sk == rcu_access_pointer(mrt->mroute_sk))
  1473. return -EBUSY;
  1474. rtnl_lock();
  1475. ret = 0;
  1476. mrt = ip6mr_new_table(net, v);
  1477. if (IS_ERR(mrt))
  1478. ret = PTR_ERR(mrt);
  1479. else
  1480. raw6_sk(sk)->ip6mr_table = v;
  1481. rtnl_unlock();
  1482. return ret;
  1483. }
  1484. #endif
  1485. /*
  1486. * Spurious command, or MRT6_VERSION which you cannot
  1487. * set.
  1488. */
  1489. default:
  1490. return -ENOPROTOOPT;
  1491. }
  1492. }
  1493. /*
  1494. * Getsock opt support for the multicast routing system.
  1495. */
  1496. int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
  1497. int __user *optlen)
  1498. {
  1499. int olr;
  1500. int val;
  1501. struct net *net = sock_net(sk);
  1502. struct mr_table *mrt;
  1503. if (sk->sk_type != SOCK_RAW ||
  1504. inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  1505. return -EOPNOTSUPP;
  1506. mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
  1507. if (!mrt)
  1508. return -ENOENT;
  1509. switch (optname) {
  1510. case MRT6_VERSION:
  1511. val = 0x0305;
  1512. break;
  1513. #ifdef CONFIG_IPV6_PIMSM_V2
  1514. case MRT6_PIM:
  1515. val = mrt->mroute_do_pim;
  1516. break;
  1517. #endif
  1518. case MRT6_ASSERT:
  1519. val = mrt->mroute_do_assert;
  1520. break;
  1521. default:
  1522. return -ENOPROTOOPT;
  1523. }
  1524. if (get_user(olr, optlen))
  1525. return -EFAULT;
  1526. olr = min_t(int, olr, sizeof(int));
  1527. if (olr < 0)
  1528. return -EINVAL;
  1529. if (put_user(olr, optlen))
  1530. return -EFAULT;
  1531. if (copy_to_user(optval, &val, olr))
  1532. return -EFAULT;
  1533. return 0;
  1534. }
  1535. /*
  1536. * The IP multicast ioctl support routines.
  1537. */
  1538. int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
  1539. {
  1540. struct sioc_sg_req6 sr;
  1541. struct sioc_mif_req6 vr;
  1542. struct vif_device *vif;
  1543. struct mfc6_cache *c;
  1544. struct net *net = sock_net(sk);
  1545. struct mr_table *mrt;
  1546. mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
  1547. if (!mrt)
  1548. return -ENOENT;
  1549. switch (cmd) {
  1550. case SIOCGETMIFCNT_IN6:
  1551. if (copy_from_user(&vr, arg, sizeof(vr)))
  1552. return -EFAULT;
  1553. if (vr.mifi >= mrt->maxvif)
  1554. return -EINVAL;
  1555. vr.mifi = array_index_nospec(vr.mifi, mrt->maxvif);
  1556. read_lock(&mrt_lock);
  1557. vif = &mrt->vif_table[vr.mifi];
  1558. if (VIF_EXISTS(mrt, vr.mifi)) {
  1559. vr.icount = vif->pkt_in;
  1560. vr.ocount = vif->pkt_out;
  1561. vr.ibytes = vif->bytes_in;
  1562. vr.obytes = vif->bytes_out;
  1563. read_unlock(&mrt_lock);
  1564. if (copy_to_user(arg, &vr, sizeof(vr)))
  1565. return -EFAULT;
  1566. return 0;
  1567. }
  1568. read_unlock(&mrt_lock);
  1569. return -EADDRNOTAVAIL;
  1570. case SIOCGETSGCNT_IN6:
  1571. if (copy_from_user(&sr, arg, sizeof(sr)))
  1572. return -EFAULT;
  1573. rcu_read_lock();
  1574. c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
  1575. if (c) {
  1576. sr.pktcnt = c->_c.mfc_un.res.pkt;
  1577. sr.bytecnt = c->_c.mfc_un.res.bytes;
  1578. sr.wrong_if = c->_c.mfc_un.res.wrong_if;
  1579. rcu_read_unlock();
  1580. if (copy_to_user(arg, &sr, sizeof(sr)))
  1581. return -EFAULT;
  1582. return 0;
  1583. }
  1584. rcu_read_unlock();
  1585. return -EADDRNOTAVAIL;
  1586. default:
  1587. return -ENOIOCTLCMD;
  1588. }
  1589. }
  1590. #ifdef CONFIG_COMPAT
  1591. struct compat_sioc_sg_req6 {
  1592. struct sockaddr_in6 src;
  1593. struct sockaddr_in6 grp;
  1594. compat_ulong_t pktcnt;
  1595. compat_ulong_t bytecnt;
  1596. compat_ulong_t wrong_if;
  1597. };
  1598. struct compat_sioc_mif_req6 {
  1599. mifi_t mifi;
  1600. compat_ulong_t icount;
  1601. compat_ulong_t ocount;
  1602. compat_ulong_t ibytes;
  1603. compat_ulong_t obytes;
  1604. };
  1605. int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
  1606. {
  1607. struct compat_sioc_sg_req6 sr;
  1608. struct compat_sioc_mif_req6 vr;
  1609. struct vif_device *vif;
  1610. struct mfc6_cache *c;
  1611. struct net *net = sock_net(sk);
  1612. struct mr_table *mrt;
  1613. mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
  1614. if (!mrt)
  1615. return -ENOENT;
  1616. switch (cmd) {
  1617. case SIOCGETMIFCNT_IN6:
  1618. if (copy_from_user(&vr, arg, sizeof(vr)))
  1619. return -EFAULT;
  1620. if (vr.mifi >= mrt->maxvif)
  1621. return -EINVAL;
  1622. vr.mifi = array_index_nospec(vr.mifi, mrt->maxvif);
  1623. read_lock(&mrt_lock);
  1624. vif = &mrt->vif_table[vr.mifi];
  1625. if (VIF_EXISTS(mrt, vr.mifi)) {
  1626. vr.icount = vif->pkt_in;
  1627. vr.ocount = vif->pkt_out;
  1628. vr.ibytes = vif->bytes_in;
  1629. vr.obytes = vif->bytes_out;
  1630. read_unlock(&mrt_lock);
  1631. if (copy_to_user(arg, &vr, sizeof(vr)))
  1632. return -EFAULT;
  1633. return 0;
  1634. }
  1635. read_unlock(&mrt_lock);
  1636. return -EADDRNOTAVAIL;
  1637. case SIOCGETSGCNT_IN6:
  1638. if (copy_from_user(&sr, arg, sizeof(sr)))
  1639. return -EFAULT;
  1640. rcu_read_lock();
  1641. c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
  1642. if (c) {
  1643. sr.pktcnt = c->_c.mfc_un.res.pkt;
  1644. sr.bytecnt = c->_c.mfc_un.res.bytes;
  1645. sr.wrong_if = c->_c.mfc_un.res.wrong_if;
  1646. rcu_read_unlock();
  1647. if (copy_to_user(arg, &sr, sizeof(sr)))
  1648. return -EFAULT;
  1649. return 0;
  1650. }
  1651. rcu_read_unlock();
  1652. return -EADDRNOTAVAIL;
  1653. default:
  1654. return -ENOIOCTLCMD;
  1655. }
  1656. }
  1657. #endif
  1658. static inline int ip6mr_forward2_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  1659. {
  1660. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  1661. IPSTATS_MIB_OUTFORWDATAGRAMS);
  1662. IP6_ADD_STATS(net, ip6_dst_idev(skb_dst(skb)),
  1663. IPSTATS_MIB_OUTOCTETS, skb->len);
  1664. return dst_output(net, sk, skb);
  1665. }
  1666. /*
  1667. * Processing handlers for ip6mr_forward
  1668. */
  1669. static int ip6mr_forward2(struct net *net, struct mr_table *mrt,
  1670. struct sk_buff *skb, struct mfc6_cache *c, int vifi)
  1671. {
  1672. struct ipv6hdr *ipv6h;
  1673. struct vif_device *vif = &mrt->vif_table[vifi];
  1674. struct net_device *dev;
  1675. struct dst_entry *dst;
  1676. struct flowi6 fl6;
  1677. if (!vif->dev)
  1678. goto out_free;
  1679. #ifdef CONFIG_IPV6_PIMSM_V2
  1680. if (vif->flags & MIFF_REGISTER) {
  1681. vif->pkt_out++;
  1682. vif->bytes_out += skb->len;
  1683. vif->dev->stats.tx_bytes += skb->len;
  1684. vif->dev->stats.tx_packets++;
  1685. ip6mr_cache_report(mrt, skb, vifi, MRT6MSG_WHOLEPKT);
  1686. goto out_free;
  1687. }
  1688. #endif
  1689. ipv6h = ipv6_hdr(skb);
  1690. fl6 = (struct flowi6) {
  1691. .flowi6_oif = vif->link,
  1692. .daddr = ipv6h->daddr,
  1693. };
  1694. dst = ip6_route_output(net, NULL, &fl6);
  1695. if (dst->error) {
  1696. dst_release(dst);
  1697. goto out_free;
  1698. }
  1699. skb_dst_drop(skb);
  1700. skb_dst_set(skb, dst);
  1701. /*
  1702. * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
  1703. * not only before forwarding, but after forwarding on all output
  1704. * interfaces. It is clear, if mrouter runs a multicasting
  1705. * program, it should receive packets not depending to what interface
  1706. * program is joined.
  1707. * If we will not make it, the program will have to join on all
  1708. * interfaces. On the other hand, multihoming host (or router, but
  1709. * not mrouter) cannot join to more than one interface - it will
  1710. * result in receiving multiple packets.
  1711. */
  1712. dev = vif->dev;
  1713. skb->dev = dev;
  1714. vif->pkt_out++;
  1715. vif->bytes_out += skb->len;
  1716. /* We are about to write */
  1717. /* XXX: extension headers? */
  1718. if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
  1719. goto out_free;
  1720. ipv6h = ipv6_hdr(skb);
  1721. ipv6h->hop_limit--;
  1722. IP6CB(skb)->flags |= IP6SKB_FORWARDED;
  1723. return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
  1724. net, NULL, skb, skb->dev, dev,
  1725. ip6mr_forward2_finish);
  1726. out_free:
  1727. kfree_skb(skb);
  1728. return 0;
  1729. }
  1730. static int ip6mr_find_vif(struct mr_table *mrt, struct net_device *dev)
  1731. {
  1732. int ct;
  1733. for (ct = mrt->maxvif - 1; ct >= 0; ct--) {
  1734. if (mrt->vif_table[ct].dev == dev)
  1735. break;
  1736. }
  1737. return ct;
  1738. }
  1739. static void ip6_mr_forward(struct net *net, struct mr_table *mrt,
  1740. struct sk_buff *skb, struct mfc6_cache *c)
  1741. {
  1742. int psend = -1;
  1743. int vif, ct;
  1744. int true_vifi = ip6mr_find_vif(mrt, skb->dev);
  1745. vif = c->_c.mfc_parent;
  1746. c->_c.mfc_un.res.pkt++;
  1747. c->_c.mfc_un.res.bytes += skb->len;
  1748. c->_c.mfc_un.res.lastuse = jiffies;
  1749. if (ipv6_addr_any(&c->mf6c_origin) && true_vifi >= 0) {
  1750. struct mfc6_cache *cache_proxy;
  1751. /* For an (*,G) entry, we only check that the incoming
  1752. * interface is part of the static tree.
  1753. */
  1754. rcu_read_lock();
  1755. cache_proxy = mr_mfc_find_any_parent(mrt, vif);
  1756. if (cache_proxy &&
  1757. cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255) {
  1758. rcu_read_unlock();
  1759. goto forward;
  1760. }
  1761. rcu_read_unlock();
  1762. }
  1763. /*
  1764. * Wrong interface: drop packet and (maybe) send PIM assert.
  1765. */
  1766. if (mrt->vif_table[vif].dev != skb->dev) {
  1767. c->_c.mfc_un.res.wrong_if++;
  1768. if (true_vifi >= 0 && mrt->mroute_do_assert &&
  1769. /* pimsm uses asserts, when switching from RPT to SPT,
  1770. so that we cannot check that packet arrived on an oif.
  1771. It is bad, but otherwise we would need to move pretty
  1772. large chunk of pimd to kernel. Ough... --ANK
  1773. */
  1774. (mrt->mroute_do_pim ||
  1775. c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
  1776. time_after(jiffies,
  1777. c->_c.mfc_un.res.last_assert +
  1778. MFC_ASSERT_THRESH)) {
  1779. c->_c.mfc_un.res.last_assert = jiffies;
  1780. ip6mr_cache_report(mrt, skb, true_vifi, MRT6MSG_WRONGMIF);
  1781. }
  1782. goto dont_forward;
  1783. }
  1784. forward:
  1785. mrt->vif_table[vif].pkt_in++;
  1786. mrt->vif_table[vif].bytes_in += skb->len;
  1787. /*
  1788. * Forward the frame
  1789. */
  1790. if (ipv6_addr_any(&c->mf6c_origin) &&
  1791. ipv6_addr_any(&c->mf6c_mcastgrp)) {
  1792. if (true_vifi >= 0 &&
  1793. true_vifi != c->_c.mfc_parent &&
  1794. ipv6_hdr(skb)->hop_limit >
  1795. c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
  1796. /* It's an (*,*) entry and the packet is not coming from
  1797. * the upstream: forward the packet to the upstream
  1798. * only.
  1799. */
  1800. psend = c->_c.mfc_parent;
  1801. goto last_forward;
  1802. }
  1803. goto dont_forward;
  1804. }
  1805. for (ct = c->_c.mfc_un.res.maxvif - 1;
  1806. ct >= c->_c.mfc_un.res.minvif; ct--) {
  1807. /* For (*,G) entry, don't forward to the incoming interface */
  1808. if ((!ipv6_addr_any(&c->mf6c_origin) || ct != true_vifi) &&
  1809. ipv6_hdr(skb)->hop_limit > c->_c.mfc_un.res.ttls[ct]) {
  1810. if (psend != -1) {
  1811. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1812. if (skb2)
  1813. ip6mr_forward2(net, mrt, skb2,
  1814. c, psend);
  1815. }
  1816. psend = ct;
  1817. }
  1818. }
  1819. last_forward:
  1820. if (psend != -1) {
  1821. ip6mr_forward2(net, mrt, skb, c, psend);
  1822. return;
  1823. }
  1824. dont_forward:
  1825. kfree_skb(skb);
  1826. }
  1827. /*
  1828. * Multicast packets for forwarding arrive here
  1829. */
  1830. int ip6_mr_input(struct sk_buff *skb)
  1831. {
  1832. struct mfc6_cache *cache;
  1833. struct net *net = dev_net(skb->dev);
  1834. struct mr_table *mrt;
  1835. struct flowi6 fl6 = {
  1836. .flowi6_iif = skb->dev->ifindex,
  1837. .flowi6_mark = skb->mark,
  1838. };
  1839. int err;
  1840. err = ip6mr_fib_lookup(net, &fl6, &mrt);
  1841. if (err < 0) {
  1842. kfree_skb(skb);
  1843. return err;
  1844. }
  1845. read_lock(&mrt_lock);
  1846. cache = ip6mr_cache_find(mrt,
  1847. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
  1848. if (!cache) {
  1849. int vif = ip6mr_find_vif(mrt, skb->dev);
  1850. if (vif >= 0)
  1851. cache = ip6mr_cache_find_any(mrt,
  1852. &ipv6_hdr(skb)->daddr,
  1853. vif);
  1854. }
  1855. /*
  1856. * No usable cache entry
  1857. */
  1858. if (!cache) {
  1859. int vif;
  1860. vif = ip6mr_find_vif(mrt, skb->dev);
  1861. if (vif >= 0) {
  1862. int err = ip6mr_cache_unresolved(mrt, vif, skb);
  1863. read_unlock(&mrt_lock);
  1864. return err;
  1865. }
  1866. read_unlock(&mrt_lock);
  1867. kfree_skb(skb);
  1868. return -ENODEV;
  1869. }
  1870. ip6_mr_forward(net, mrt, skb, cache);
  1871. read_unlock(&mrt_lock);
  1872. return 0;
  1873. }
  1874. int ip6mr_get_route(struct net *net, struct sk_buff *skb, struct rtmsg *rtm,
  1875. u32 portid)
  1876. {
  1877. int err;
  1878. struct mr_table *mrt;
  1879. struct mfc6_cache *cache;
  1880. struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
  1881. mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
  1882. if (!mrt)
  1883. return -ENOENT;
  1884. read_lock(&mrt_lock);
  1885. cache = ip6mr_cache_find(mrt, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
  1886. if (!cache && skb->dev) {
  1887. int vif = ip6mr_find_vif(mrt, skb->dev);
  1888. if (vif >= 0)
  1889. cache = ip6mr_cache_find_any(mrt, &rt->rt6i_dst.addr,
  1890. vif);
  1891. }
  1892. if (!cache) {
  1893. struct sk_buff *skb2;
  1894. struct ipv6hdr *iph;
  1895. struct net_device *dev;
  1896. int vif;
  1897. dev = skb->dev;
  1898. if (!dev || (vif = ip6mr_find_vif(mrt, dev)) < 0) {
  1899. read_unlock(&mrt_lock);
  1900. return -ENODEV;
  1901. }
  1902. /* really correct? */
  1903. skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  1904. if (!skb2) {
  1905. read_unlock(&mrt_lock);
  1906. return -ENOMEM;
  1907. }
  1908. NETLINK_CB(skb2).portid = portid;
  1909. skb_reset_transport_header(skb2);
  1910. skb_put(skb2, sizeof(struct ipv6hdr));
  1911. skb_reset_network_header(skb2);
  1912. iph = ipv6_hdr(skb2);
  1913. iph->version = 0;
  1914. iph->priority = 0;
  1915. iph->flow_lbl[0] = 0;
  1916. iph->flow_lbl[1] = 0;
  1917. iph->flow_lbl[2] = 0;
  1918. iph->payload_len = 0;
  1919. iph->nexthdr = IPPROTO_NONE;
  1920. iph->hop_limit = 0;
  1921. iph->saddr = rt->rt6i_src.addr;
  1922. iph->daddr = rt->rt6i_dst.addr;
  1923. err = ip6mr_cache_unresolved(mrt, vif, skb2);
  1924. read_unlock(&mrt_lock);
  1925. return err;
  1926. }
  1927. err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
  1928. read_unlock(&mrt_lock);
  1929. return err;
  1930. }
  1931. static int ip6mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  1932. u32 portid, u32 seq, struct mfc6_cache *c, int cmd,
  1933. int flags)
  1934. {
  1935. struct nlmsghdr *nlh;
  1936. struct rtmsg *rtm;
  1937. int err;
  1938. nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
  1939. if (!nlh)
  1940. return -EMSGSIZE;
  1941. rtm = nlmsg_data(nlh);
  1942. rtm->rtm_family = RTNL_FAMILY_IP6MR;
  1943. rtm->rtm_dst_len = 128;
  1944. rtm->rtm_src_len = 128;
  1945. rtm->rtm_tos = 0;
  1946. rtm->rtm_table = mrt->id;
  1947. if (nla_put_u32(skb, RTA_TABLE, mrt->id))
  1948. goto nla_put_failure;
  1949. rtm->rtm_type = RTN_MULTICAST;
  1950. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1951. if (c->_c.mfc_flags & MFC_STATIC)
  1952. rtm->rtm_protocol = RTPROT_STATIC;
  1953. else
  1954. rtm->rtm_protocol = RTPROT_MROUTED;
  1955. rtm->rtm_flags = 0;
  1956. if (nla_put_in6_addr(skb, RTA_SRC, &c->mf6c_origin) ||
  1957. nla_put_in6_addr(skb, RTA_DST, &c->mf6c_mcastgrp))
  1958. goto nla_put_failure;
  1959. err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
  1960. /* do not break the dump if cache is unresolved */
  1961. if (err < 0 && err != -ENOENT)
  1962. goto nla_put_failure;
  1963. nlmsg_end(skb, nlh);
  1964. return 0;
  1965. nla_put_failure:
  1966. nlmsg_cancel(skb, nlh);
  1967. return -EMSGSIZE;
  1968. }
  1969. static int _ip6mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  1970. u32 portid, u32 seq, struct mr_mfc *c,
  1971. int cmd, int flags)
  1972. {
  1973. return ip6mr_fill_mroute(mrt, skb, portid, seq, (struct mfc6_cache *)c,
  1974. cmd, flags);
  1975. }
  1976. static int mr6_msgsize(bool unresolved, int maxvif)
  1977. {
  1978. size_t len =
  1979. NLMSG_ALIGN(sizeof(struct rtmsg))
  1980. + nla_total_size(4) /* RTA_TABLE */
  1981. + nla_total_size(sizeof(struct in6_addr)) /* RTA_SRC */
  1982. + nla_total_size(sizeof(struct in6_addr)) /* RTA_DST */
  1983. ;
  1984. if (!unresolved)
  1985. len = len
  1986. + nla_total_size(4) /* RTA_IIF */
  1987. + nla_total_size(0) /* RTA_MULTIPATH */
  1988. + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
  1989. /* RTA_MFC_STATS */
  1990. + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
  1991. ;
  1992. return len;
  1993. }
  1994. static void mr6_netlink_event(struct mr_table *mrt, struct mfc6_cache *mfc,
  1995. int cmd)
  1996. {
  1997. struct net *net = read_pnet(&mrt->net);
  1998. struct sk_buff *skb;
  1999. int err = -ENOBUFS;
  2000. skb = nlmsg_new(mr6_msgsize(mfc->_c.mfc_parent >= MAXMIFS, mrt->maxvif),
  2001. GFP_ATOMIC);
  2002. if (!skb)
  2003. goto errout;
  2004. err = ip6mr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
  2005. if (err < 0)
  2006. goto errout;
  2007. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_MROUTE, NULL, GFP_ATOMIC);
  2008. return;
  2009. errout:
  2010. kfree_skb(skb);
  2011. if (err < 0)
  2012. rtnl_set_sk_err(net, RTNLGRP_IPV6_MROUTE, err);
  2013. }
  2014. static size_t mrt6msg_netlink_msgsize(size_t payloadlen)
  2015. {
  2016. size_t len =
  2017. NLMSG_ALIGN(sizeof(struct rtgenmsg))
  2018. + nla_total_size(1) /* IP6MRA_CREPORT_MSGTYPE */
  2019. + nla_total_size(4) /* IP6MRA_CREPORT_MIF_ID */
  2020. /* IP6MRA_CREPORT_SRC_ADDR */
  2021. + nla_total_size(sizeof(struct in6_addr))
  2022. /* IP6MRA_CREPORT_DST_ADDR */
  2023. + nla_total_size(sizeof(struct in6_addr))
  2024. /* IP6MRA_CREPORT_PKT */
  2025. + nla_total_size(payloadlen)
  2026. ;
  2027. return len;
  2028. }
  2029. static void mrt6msg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
  2030. {
  2031. struct net *net = read_pnet(&mrt->net);
  2032. struct nlmsghdr *nlh;
  2033. struct rtgenmsg *rtgenm;
  2034. struct mrt6msg *msg;
  2035. struct sk_buff *skb;
  2036. struct nlattr *nla;
  2037. int payloadlen;
  2038. payloadlen = pkt->len - sizeof(struct mrt6msg);
  2039. msg = (struct mrt6msg *)skb_transport_header(pkt);
  2040. skb = nlmsg_new(mrt6msg_netlink_msgsize(payloadlen), GFP_ATOMIC);
  2041. if (!skb)
  2042. goto errout;
  2043. nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
  2044. sizeof(struct rtgenmsg), 0);
  2045. if (!nlh)
  2046. goto errout;
  2047. rtgenm = nlmsg_data(nlh);
  2048. rtgenm->rtgen_family = RTNL_FAMILY_IP6MR;
  2049. if (nla_put_u8(skb, IP6MRA_CREPORT_MSGTYPE, msg->im6_msgtype) ||
  2050. nla_put_u32(skb, IP6MRA_CREPORT_MIF_ID, msg->im6_mif) ||
  2051. nla_put_in6_addr(skb, IP6MRA_CREPORT_SRC_ADDR,
  2052. &msg->im6_src) ||
  2053. nla_put_in6_addr(skb, IP6MRA_CREPORT_DST_ADDR,
  2054. &msg->im6_dst))
  2055. goto nla_put_failure;
  2056. nla = nla_reserve(skb, IP6MRA_CREPORT_PKT, payloadlen);
  2057. if (!nla || skb_copy_bits(pkt, sizeof(struct mrt6msg),
  2058. nla_data(nla), payloadlen))
  2059. goto nla_put_failure;
  2060. nlmsg_end(skb, nlh);
  2061. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_MROUTE_R, NULL, GFP_ATOMIC);
  2062. return;
  2063. nla_put_failure:
  2064. nlmsg_cancel(skb, nlh);
  2065. errout:
  2066. kfree_skb(skb);
  2067. rtnl_set_sk_err(net, RTNLGRP_IPV6_MROUTE_R, -ENOBUFS);
  2068. }
  2069. static int ip6mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
  2070. {
  2071. return mr_rtm_dumproute(skb, cb, ip6mr_mr_table_iter,
  2072. _ip6mr_fill_mroute, &mfc_unres_lock);
  2073. }