ip6mr.c 57 KB

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