ip6mr.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521
  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, bool all);
  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. .fill = ip6mr_rule_fill,
  192. .nlgroup = RTNLGRP_IPV6_RULE,
  193. .policy = ip6mr_rule_policy,
  194. .owner = THIS_MODULE,
  195. };
  196. static int __net_init ip6mr_rules_init(struct net *net)
  197. {
  198. struct fib_rules_ops *ops;
  199. struct mr6_table *mrt;
  200. int err;
  201. ops = fib_rules_register(&ip6mr_rules_ops_template, net);
  202. if (IS_ERR(ops))
  203. return PTR_ERR(ops);
  204. INIT_LIST_HEAD(&net->ipv6.mr6_tables);
  205. mrt = ip6mr_new_table(net, RT6_TABLE_DFLT);
  206. if (!mrt) {
  207. err = -ENOMEM;
  208. goto err1;
  209. }
  210. err = fib_default_rule_add(ops, 0x7fff, RT6_TABLE_DFLT, 0);
  211. if (err < 0)
  212. goto err2;
  213. net->ipv6.mr6_rules_ops = ops;
  214. return 0;
  215. err2:
  216. ip6mr_free_table(mrt);
  217. err1:
  218. fib_rules_unregister(ops);
  219. return err;
  220. }
  221. static void __net_exit ip6mr_rules_exit(struct net *net)
  222. {
  223. struct mr6_table *mrt, *next;
  224. rtnl_lock();
  225. list_for_each_entry_safe(mrt, next, &net->ipv6.mr6_tables, list) {
  226. list_del(&mrt->list);
  227. ip6mr_free_table(mrt);
  228. }
  229. fib_rules_unregister(net->ipv6.mr6_rules_ops);
  230. rtnl_unlock();
  231. }
  232. #else
  233. #define ip6mr_for_each_table(mrt, net) \
  234. for (mrt = net->ipv6.mrt6; mrt; mrt = NULL)
  235. static struct mr6_table *ip6mr_get_table(struct net *net, u32 id)
  236. {
  237. return net->ipv6.mrt6;
  238. }
  239. static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6,
  240. struct mr6_table **mrt)
  241. {
  242. *mrt = net->ipv6.mrt6;
  243. return 0;
  244. }
  245. static int __net_init ip6mr_rules_init(struct net *net)
  246. {
  247. net->ipv6.mrt6 = ip6mr_new_table(net, RT6_TABLE_DFLT);
  248. return net->ipv6.mrt6 ? 0 : -ENOMEM;
  249. }
  250. static void __net_exit ip6mr_rules_exit(struct net *net)
  251. {
  252. rtnl_lock();
  253. ip6mr_free_table(net->ipv6.mrt6);
  254. net->ipv6.mrt6 = NULL;
  255. rtnl_unlock();
  256. }
  257. #endif
  258. static struct mr6_table *ip6mr_new_table(struct net *net, u32 id)
  259. {
  260. struct mr6_table *mrt;
  261. unsigned int i;
  262. mrt = ip6mr_get_table(net, id);
  263. if (mrt)
  264. return mrt;
  265. mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
  266. if (!mrt)
  267. return NULL;
  268. mrt->id = id;
  269. write_pnet(&mrt->net, net);
  270. /* Forwarding cache */
  271. for (i = 0; i < MFC6_LINES; i++)
  272. INIT_LIST_HEAD(&mrt->mfc6_cache_array[i]);
  273. INIT_LIST_HEAD(&mrt->mfc6_unres_queue);
  274. setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
  275. (unsigned long)mrt);
  276. #ifdef CONFIG_IPV6_PIMSM_V2
  277. mrt->mroute_reg_vif_num = -1;
  278. #endif
  279. #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
  280. list_add_tail_rcu(&mrt->list, &net->ipv6.mr6_tables);
  281. #endif
  282. return mrt;
  283. }
  284. static void ip6mr_free_table(struct mr6_table *mrt)
  285. {
  286. del_timer_sync(&mrt->ipmr_expire_timer);
  287. mroute_clean_tables(mrt, true);
  288. kfree(mrt);
  289. }
  290. #ifdef CONFIG_PROC_FS
  291. struct ipmr_mfc_iter {
  292. struct seq_net_private p;
  293. struct mr6_table *mrt;
  294. struct list_head *cache;
  295. int ct;
  296. };
  297. static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
  298. struct ipmr_mfc_iter *it, loff_t pos)
  299. {
  300. struct mr6_table *mrt = it->mrt;
  301. struct mfc6_cache *mfc;
  302. read_lock(&mrt_lock);
  303. for (it->ct = 0; it->ct < MFC6_LINES; it->ct++) {
  304. it->cache = &mrt->mfc6_cache_array[it->ct];
  305. list_for_each_entry(mfc, it->cache, list)
  306. if (pos-- == 0)
  307. return mfc;
  308. }
  309. read_unlock(&mrt_lock);
  310. spin_lock_bh(&mfc_unres_lock);
  311. it->cache = &mrt->mfc6_unres_queue;
  312. list_for_each_entry(mfc, it->cache, list)
  313. if (pos-- == 0)
  314. return mfc;
  315. spin_unlock_bh(&mfc_unres_lock);
  316. it->cache = NULL;
  317. return NULL;
  318. }
  319. /*
  320. * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
  321. */
  322. struct ipmr_vif_iter {
  323. struct seq_net_private p;
  324. struct mr6_table *mrt;
  325. int ct;
  326. };
  327. static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
  328. struct ipmr_vif_iter *iter,
  329. loff_t pos)
  330. {
  331. struct mr6_table *mrt = iter->mrt;
  332. for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
  333. if (!MIF_EXISTS(mrt, iter->ct))
  334. continue;
  335. if (pos-- == 0)
  336. return &mrt->vif6_table[iter->ct];
  337. }
  338. return NULL;
  339. }
  340. static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
  341. __acquires(mrt_lock)
  342. {
  343. struct ipmr_vif_iter *iter = seq->private;
  344. struct net *net = seq_file_net(seq);
  345. struct mr6_table *mrt;
  346. mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
  347. if (!mrt)
  348. return ERR_PTR(-ENOENT);
  349. iter->mrt = mrt;
  350. read_lock(&mrt_lock);
  351. return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
  352. : SEQ_START_TOKEN;
  353. }
  354. static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  355. {
  356. struct ipmr_vif_iter *iter = seq->private;
  357. struct net *net = seq_file_net(seq);
  358. struct mr6_table *mrt = iter->mrt;
  359. ++*pos;
  360. if (v == SEQ_START_TOKEN)
  361. return ip6mr_vif_seq_idx(net, iter, 0);
  362. while (++iter->ct < mrt->maxvif) {
  363. if (!MIF_EXISTS(mrt, iter->ct))
  364. continue;
  365. return &mrt->vif6_table[iter->ct];
  366. }
  367. return NULL;
  368. }
  369. static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
  370. __releases(mrt_lock)
  371. {
  372. read_unlock(&mrt_lock);
  373. }
  374. static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
  375. {
  376. struct ipmr_vif_iter *iter = seq->private;
  377. struct mr6_table *mrt = iter->mrt;
  378. if (v == SEQ_START_TOKEN) {
  379. seq_puts(seq,
  380. "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
  381. } else {
  382. const struct mif_device *vif = v;
  383. const char *name = vif->dev ? vif->dev->name : "none";
  384. seq_printf(seq,
  385. "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
  386. vif - mrt->vif6_table,
  387. name, vif->bytes_in, vif->pkt_in,
  388. vif->bytes_out, vif->pkt_out,
  389. vif->flags);
  390. }
  391. return 0;
  392. }
  393. static const struct seq_operations ip6mr_vif_seq_ops = {
  394. .start = ip6mr_vif_seq_start,
  395. .next = ip6mr_vif_seq_next,
  396. .stop = ip6mr_vif_seq_stop,
  397. .show = ip6mr_vif_seq_show,
  398. };
  399. static int ip6mr_vif_open(struct inode *inode, struct file *file)
  400. {
  401. return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
  402. sizeof(struct ipmr_vif_iter));
  403. }
  404. static const struct file_operations ip6mr_vif_fops = {
  405. .owner = THIS_MODULE,
  406. .open = ip6mr_vif_open,
  407. .read = seq_read,
  408. .llseek = seq_lseek,
  409. .release = seq_release_net,
  410. };
  411. static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
  412. {
  413. struct ipmr_mfc_iter *it = seq->private;
  414. struct net *net = seq_file_net(seq);
  415. struct mr6_table *mrt;
  416. mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
  417. if (!mrt)
  418. return ERR_PTR(-ENOENT);
  419. it->mrt = mrt;
  420. it->cache = NULL;
  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[it->ct])
  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. unregister_netdevice(dev);
  643. return NULL;
  644. }
  645. #endif
  646. /*
  647. * Delete a VIF entry
  648. */
  649. static int mif6_delete(struct mr6_table *mrt, int vifi, int notify,
  650. struct list_head *head)
  651. {
  652. struct mif_device *v;
  653. struct net_device *dev;
  654. struct inet6_dev *in6_dev;
  655. if (vifi < 0 || vifi >= mrt->maxvif)
  656. return -EADDRNOTAVAIL;
  657. v = &mrt->vif6_table[vifi];
  658. write_lock_bh(&mrt_lock);
  659. dev = v->dev;
  660. v->dev = NULL;
  661. if (!dev) {
  662. write_unlock_bh(&mrt_lock);
  663. return -EADDRNOTAVAIL;
  664. }
  665. #ifdef CONFIG_IPV6_PIMSM_V2
  666. if (vifi == mrt->mroute_reg_vif_num)
  667. mrt->mroute_reg_vif_num = -1;
  668. #endif
  669. if (vifi + 1 == mrt->maxvif) {
  670. int tmp;
  671. for (tmp = vifi - 1; tmp >= 0; tmp--) {
  672. if (MIF_EXISTS(mrt, tmp))
  673. break;
  674. }
  675. mrt->maxvif = tmp + 1;
  676. }
  677. write_unlock_bh(&mrt_lock);
  678. dev_set_allmulti(dev, -1);
  679. in6_dev = __in6_dev_get(dev);
  680. if (in6_dev) {
  681. in6_dev->cnf.mc_forwarding--;
  682. inet6_netconf_notify_devconf(dev_net(dev),
  683. NETCONFA_MC_FORWARDING,
  684. dev->ifindex, &in6_dev->cnf);
  685. }
  686. if ((v->flags & MIFF_REGISTER) && !notify)
  687. unregister_netdevice_queue(dev, head);
  688. dev_put(dev);
  689. return 0;
  690. }
  691. static inline void ip6mr_cache_free(struct mfc6_cache *c)
  692. {
  693. kmem_cache_free(mrt_cachep, c);
  694. }
  695. /* Destroy an unresolved cache entry, killing queued skbs
  696. and reporting error to netlink readers.
  697. */
  698. static void ip6mr_destroy_unres(struct mr6_table *mrt, struct mfc6_cache *c)
  699. {
  700. struct net *net = read_pnet(&mrt->net);
  701. struct sk_buff *skb;
  702. atomic_dec(&mrt->cache_resolve_queue_len);
  703. while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
  704. if (ipv6_hdr(skb)->version == 0) {
  705. struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
  706. nlh->nlmsg_type = NLMSG_ERROR;
  707. nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
  708. skb_trim(skb, nlh->nlmsg_len);
  709. ((struct nlmsgerr *)nlmsg_data(nlh))->error = -ETIMEDOUT;
  710. rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
  711. } else
  712. kfree_skb(skb);
  713. }
  714. ip6mr_cache_free(c);
  715. }
  716. /* Timer process for all the unresolved queue. */
  717. static void ipmr_do_expire_process(struct mr6_table *mrt)
  718. {
  719. unsigned long now = jiffies;
  720. unsigned long expires = 10 * HZ;
  721. struct mfc6_cache *c, *next;
  722. list_for_each_entry_safe(c, next, &mrt->mfc6_unres_queue, list) {
  723. if (time_after(c->mfc_un.unres.expires, now)) {
  724. /* not yet... */
  725. unsigned long interval = c->mfc_un.unres.expires - now;
  726. if (interval < expires)
  727. expires = interval;
  728. continue;
  729. }
  730. list_del(&c->list);
  731. mr6_netlink_event(mrt, c, RTM_DELROUTE);
  732. ip6mr_destroy_unres(mrt, c);
  733. }
  734. if (!list_empty(&mrt->mfc6_unres_queue))
  735. mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
  736. }
  737. static void ipmr_expire_process(unsigned long arg)
  738. {
  739. struct mr6_table *mrt = (struct mr6_table *)arg;
  740. if (!spin_trylock(&mfc_unres_lock)) {
  741. mod_timer(&mrt->ipmr_expire_timer, jiffies + 1);
  742. return;
  743. }
  744. if (!list_empty(&mrt->mfc6_unres_queue))
  745. ipmr_do_expire_process(mrt);
  746. spin_unlock(&mfc_unres_lock);
  747. }
  748. /* Fill oifs list. It is called under write locked mrt_lock. */
  749. static void ip6mr_update_thresholds(struct mr6_table *mrt, struct mfc6_cache *cache,
  750. unsigned char *ttls)
  751. {
  752. int vifi;
  753. cache->mfc_un.res.minvif = MAXMIFS;
  754. cache->mfc_un.res.maxvif = 0;
  755. memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
  756. for (vifi = 0; vifi < mrt->maxvif; vifi++) {
  757. if (MIF_EXISTS(mrt, vifi) &&
  758. ttls[vifi] && ttls[vifi] < 255) {
  759. cache->mfc_un.res.ttls[vifi] = ttls[vifi];
  760. if (cache->mfc_un.res.minvif > vifi)
  761. cache->mfc_un.res.minvif = vifi;
  762. if (cache->mfc_un.res.maxvif <= vifi)
  763. cache->mfc_un.res.maxvif = vifi + 1;
  764. }
  765. }
  766. cache->mfc_un.res.lastuse = jiffies;
  767. }
  768. static int mif6_add(struct net *net, struct mr6_table *mrt,
  769. struct mif6ctl *vifc, int mrtsock)
  770. {
  771. int vifi = vifc->mif6c_mifi;
  772. struct mif_device *v = &mrt->vif6_table[vifi];
  773. struct net_device *dev;
  774. struct inet6_dev *in6_dev;
  775. int err;
  776. /* Is vif busy ? */
  777. if (MIF_EXISTS(mrt, vifi))
  778. return -EADDRINUSE;
  779. switch (vifc->mif6c_flags) {
  780. #ifdef CONFIG_IPV6_PIMSM_V2
  781. case MIFF_REGISTER:
  782. /*
  783. * Special Purpose VIF in PIM
  784. * All the packets will be sent to the daemon
  785. */
  786. if (mrt->mroute_reg_vif_num >= 0)
  787. return -EADDRINUSE;
  788. dev = ip6mr_reg_vif(net, mrt);
  789. if (!dev)
  790. return -ENOBUFS;
  791. err = dev_set_allmulti(dev, 1);
  792. if (err) {
  793. unregister_netdevice(dev);
  794. dev_put(dev);
  795. return err;
  796. }
  797. break;
  798. #endif
  799. case 0:
  800. dev = dev_get_by_index(net, vifc->mif6c_pifi);
  801. if (!dev)
  802. return -EADDRNOTAVAIL;
  803. err = dev_set_allmulti(dev, 1);
  804. if (err) {
  805. dev_put(dev);
  806. return err;
  807. }
  808. break;
  809. default:
  810. return -EINVAL;
  811. }
  812. in6_dev = __in6_dev_get(dev);
  813. if (in6_dev) {
  814. in6_dev->cnf.mc_forwarding++;
  815. inet6_netconf_notify_devconf(dev_net(dev),
  816. NETCONFA_MC_FORWARDING,
  817. dev->ifindex, &in6_dev->cnf);
  818. }
  819. /*
  820. * Fill in the VIF structures
  821. */
  822. v->rate_limit = vifc->vifc_rate_limit;
  823. v->flags = vifc->mif6c_flags;
  824. if (!mrtsock)
  825. v->flags |= VIFF_STATIC;
  826. v->threshold = vifc->vifc_threshold;
  827. v->bytes_in = 0;
  828. v->bytes_out = 0;
  829. v->pkt_in = 0;
  830. v->pkt_out = 0;
  831. v->link = dev->ifindex;
  832. if (v->flags & MIFF_REGISTER)
  833. v->link = dev_get_iflink(dev);
  834. /* And finish update writing critical data */
  835. write_lock_bh(&mrt_lock);
  836. v->dev = dev;
  837. #ifdef CONFIG_IPV6_PIMSM_V2
  838. if (v->flags & MIFF_REGISTER)
  839. mrt->mroute_reg_vif_num = vifi;
  840. #endif
  841. if (vifi + 1 > mrt->maxvif)
  842. mrt->maxvif = vifi + 1;
  843. write_unlock_bh(&mrt_lock);
  844. return 0;
  845. }
  846. static struct mfc6_cache *ip6mr_cache_find(struct mr6_table *mrt,
  847. const struct in6_addr *origin,
  848. const struct in6_addr *mcastgrp)
  849. {
  850. int line = MFC6_HASH(mcastgrp, origin);
  851. struct mfc6_cache *c;
  852. list_for_each_entry(c, &mrt->mfc6_cache_array[line], list) {
  853. if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
  854. ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
  855. return c;
  856. }
  857. return NULL;
  858. }
  859. /* Look for a (*,*,oif) entry */
  860. static struct mfc6_cache *ip6mr_cache_find_any_parent(struct mr6_table *mrt,
  861. mifi_t mifi)
  862. {
  863. int line = MFC6_HASH(&in6addr_any, &in6addr_any);
  864. struct mfc6_cache *c;
  865. list_for_each_entry(c, &mrt->mfc6_cache_array[line], list)
  866. if (ipv6_addr_any(&c->mf6c_origin) &&
  867. ipv6_addr_any(&c->mf6c_mcastgrp) &&
  868. (c->mfc_un.res.ttls[mifi] < 255))
  869. return c;
  870. return NULL;
  871. }
  872. /* Look for a (*,G) entry */
  873. static struct mfc6_cache *ip6mr_cache_find_any(struct mr6_table *mrt,
  874. struct in6_addr *mcastgrp,
  875. mifi_t mifi)
  876. {
  877. int line = MFC6_HASH(mcastgrp, &in6addr_any);
  878. struct mfc6_cache *c, *proxy;
  879. if (ipv6_addr_any(mcastgrp))
  880. goto skip;
  881. list_for_each_entry(c, &mrt->mfc6_cache_array[line], list)
  882. if (ipv6_addr_any(&c->mf6c_origin) &&
  883. ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp)) {
  884. if (c->mfc_un.res.ttls[mifi] < 255)
  885. return c;
  886. /* It's ok if the mifi is part of the static tree */
  887. proxy = ip6mr_cache_find_any_parent(mrt,
  888. c->mf6c_parent);
  889. if (proxy && proxy->mfc_un.res.ttls[mifi] < 255)
  890. return c;
  891. }
  892. skip:
  893. return ip6mr_cache_find_any_parent(mrt, mifi);
  894. }
  895. /*
  896. * Allocate a multicast cache entry
  897. */
  898. static struct mfc6_cache *ip6mr_cache_alloc(void)
  899. {
  900. struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
  901. if (!c)
  902. return NULL;
  903. c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
  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. if (event != NETDEV_UNREGISTER)
  1119. return NOTIFY_DONE;
  1120. ip6mr_for_each_table(mrt, net) {
  1121. v = &mrt->vif6_table[0];
  1122. for (ct = 0; ct < mrt->maxvif; ct++, v++) {
  1123. if (v->dev == dev)
  1124. mif6_delete(mrt, ct, 1, NULL);
  1125. }
  1126. }
  1127. return NOTIFY_DONE;
  1128. }
  1129. static struct notifier_block ip6_mr_notifier = {
  1130. .notifier_call = ip6mr_device_event
  1131. };
  1132. /*
  1133. * Setup for IP multicast routing
  1134. */
  1135. static int __net_init ip6mr_net_init(struct net *net)
  1136. {
  1137. int err;
  1138. err = ip6mr_rules_init(net);
  1139. if (err < 0)
  1140. goto fail;
  1141. #ifdef CONFIG_PROC_FS
  1142. err = -ENOMEM;
  1143. if (!proc_create("ip6_mr_vif", 0, net->proc_net, &ip6mr_vif_fops))
  1144. goto proc_vif_fail;
  1145. if (!proc_create("ip6_mr_cache", 0, net->proc_net, &ip6mr_mfc_fops))
  1146. goto proc_cache_fail;
  1147. #endif
  1148. return 0;
  1149. #ifdef CONFIG_PROC_FS
  1150. proc_cache_fail:
  1151. remove_proc_entry("ip6_mr_vif", net->proc_net);
  1152. proc_vif_fail:
  1153. ip6mr_rules_exit(net);
  1154. #endif
  1155. fail:
  1156. return err;
  1157. }
  1158. static void __net_exit ip6mr_net_exit(struct net *net)
  1159. {
  1160. #ifdef CONFIG_PROC_FS
  1161. remove_proc_entry("ip6_mr_cache", net->proc_net);
  1162. remove_proc_entry("ip6_mr_vif", net->proc_net);
  1163. #endif
  1164. ip6mr_rules_exit(net);
  1165. }
  1166. static struct pernet_operations ip6mr_net_ops = {
  1167. .init = ip6mr_net_init,
  1168. .exit = ip6mr_net_exit,
  1169. };
  1170. int __init ip6_mr_init(void)
  1171. {
  1172. int err;
  1173. mrt_cachep = kmem_cache_create("ip6_mrt_cache",
  1174. sizeof(struct mfc6_cache),
  1175. 0, SLAB_HWCACHE_ALIGN,
  1176. NULL);
  1177. if (!mrt_cachep)
  1178. return -ENOMEM;
  1179. err = register_pernet_subsys(&ip6mr_net_ops);
  1180. if (err)
  1181. goto reg_pernet_fail;
  1182. err = register_netdevice_notifier(&ip6_mr_notifier);
  1183. if (err)
  1184. goto reg_notif_fail;
  1185. #ifdef CONFIG_IPV6_PIMSM_V2
  1186. if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
  1187. pr_err("%s: can't add PIM protocol\n", __func__);
  1188. err = -EAGAIN;
  1189. goto add_proto_fail;
  1190. }
  1191. #endif
  1192. rtnl_register(RTNL_FAMILY_IP6MR, RTM_GETROUTE, NULL,
  1193. ip6mr_rtm_dumproute, NULL);
  1194. return 0;
  1195. #ifdef CONFIG_IPV6_PIMSM_V2
  1196. add_proto_fail:
  1197. unregister_netdevice_notifier(&ip6_mr_notifier);
  1198. #endif
  1199. reg_notif_fail:
  1200. unregister_pernet_subsys(&ip6mr_net_ops);
  1201. reg_pernet_fail:
  1202. kmem_cache_destroy(mrt_cachep);
  1203. return err;
  1204. }
  1205. void ip6_mr_cleanup(void)
  1206. {
  1207. rtnl_unregister(RTNL_FAMILY_IP6MR, RTM_GETROUTE);
  1208. #ifdef CONFIG_IPV6_PIMSM_V2
  1209. inet6_del_protocol(&pim6_protocol, IPPROTO_PIM);
  1210. #endif
  1211. unregister_netdevice_notifier(&ip6_mr_notifier);
  1212. unregister_pernet_subsys(&ip6mr_net_ops);
  1213. kmem_cache_destroy(mrt_cachep);
  1214. }
  1215. static int ip6mr_mfc_add(struct net *net, struct mr6_table *mrt,
  1216. struct mf6cctl *mfc, int mrtsock, int parent)
  1217. {
  1218. bool found = false;
  1219. int line;
  1220. struct mfc6_cache *uc, *c;
  1221. unsigned char ttls[MAXMIFS];
  1222. int i;
  1223. if (mfc->mf6cc_parent >= MAXMIFS)
  1224. return -ENFILE;
  1225. memset(ttls, 255, MAXMIFS);
  1226. for (i = 0; i < MAXMIFS; i++) {
  1227. if (IF_ISSET(i, &mfc->mf6cc_ifset))
  1228. ttls[i] = 1;
  1229. }
  1230. line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
  1231. list_for_each_entry(c, &mrt->mfc6_cache_array[line], list) {
  1232. if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
  1233. ipv6_addr_equal(&c->mf6c_mcastgrp,
  1234. &mfc->mf6cc_mcastgrp.sin6_addr) &&
  1235. (parent == -1 || parent == mfc->mf6cc_parent)) {
  1236. found = true;
  1237. break;
  1238. }
  1239. }
  1240. if (found) {
  1241. write_lock_bh(&mrt_lock);
  1242. c->mf6c_parent = mfc->mf6cc_parent;
  1243. ip6mr_update_thresholds(mrt, c, ttls);
  1244. if (!mrtsock)
  1245. c->mfc_flags |= MFC_STATIC;
  1246. write_unlock_bh(&mrt_lock);
  1247. mr6_netlink_event(mrt, c, RTM_NEWROUTE);
  1248. return 0;
  1249. }
  1250. if (!ipv6_addr_any(&mfc->mf6cc_mcastgrp.sin6_addr) &&
  1251. !ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
  1252. return -EINVAL;
  1253. c = ip6mr_cache_alloc();
  1254. if (!c)
  1255. return -ENOMEM;
  1256. c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
  1257. c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
  1258. c->mf6c_parent = mfc->mf6cc_parent;
  1259. ip6mr_update_thresholds(mrt, c, ttls);
  1260. if (!mrtsock)
  1261. c->mfc_flags |= MFC_STATIC;
  1262. write_lock_bh(&mrt_lock);
  1263. list_add(&c->list, &mrt->mfc6_cache_array[line]);
  1264. write_unlock_bh(&mrt_lock);
  1265. /*
  1266. * Check to see if we resolved a queued list. If so we
  1267. * need to send on the frames and tidy up.
  1268. */
  1269. found = false;
  1270. spin_lock_bh(&mfc_unres_lock);
  1271. list_for_each_entry(uc, &mrt->mfc6_unres_queue, list) {
  1272. if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
  1273. ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
  1274. list_del(&uc->list);
  1275. atomic_dec(&mrt->cache_resolve_queue_len);
  1276. found = true;
  1277. break;
  1278. }
  1279. }
  1280. if (list_empty(&mrt->mfc6_unres_queue))
  1281. del_timer(&mrt->ipmr_expire_timer);
  1282. spin_unlock_bh(&mfc_unres_lock);
  1283. if (found) {
  1284. ip6mr_cache_resolve(net, mrt, uc, c);
  1285. ip6mr_cache_free(uc);
  1286. }
  1287. mr6_netlink_event(mrt, c, RTM_NEWROUTE);
  1288. return 0;
  1289. }
  1290. /*
  1291. * Close the multicast socket, and clear the vif tables etc
  1292. */
  1293. static void mroute_clean_tables(struct mr6_table *mrt, bool all)
  1294. {
  1295. int i;
  1296. LIST_HEAD(list);
  1297. struct mfc6_cache *c, *next;
  1298. /*
  1299. * Shut down all active vif entries
  1300. */
  1301. for (i = 0; i < mrt->maxvif; i++) {
  1302. if (!all && (mrt->vif6_table[i].flags & VIFF_STATIC))
  1303. continue;
  1304. mif6_delete(mrt, i, 0, &list);
  1305. }
  1306. unregister_netdevice_many(&list);
  1307. /*
  1308. * Wipe the cache
  1309. */
  1310. for (i = 0; i < MFC6_LINES; i++) {
  1311. list_for_each_entry_safe(c, next, &mrt->mfc6_cache_array[i], list) {
  1312. if (!all && (c->mfc_flags & MFC_STATIC))
  1313. continue;
  1314. write_lock_bh(&mrt_lock);
  1315. list_del(&c->list);
  1316. write_unlock_bh(&mrt_lock);
  1317. mr6_netlink_event(mrt, c, RTM_DELROUTE);
  1318. ip6mr_cache_free(c);
  1319. }
  1320. }
  1321. if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
  1322. spin_lock_bh(&mfc_unres_lock);
  1323. list_for_each_entry_safe(c, next, &mrt->mfc6_unres_queue, list) {
  1324. list_del(&c->list);
  1325. mr6_netlink_event(mrt, c, RTM_DELROUTE);
  1326. ip6mr_destroy_unres(mrt, c);
  1327. }
  1328. spin_unlock_bh(&mfc_unres_lock);
  1329. }
  1330. }
  1331. static int ip6mr_sk_init(struct mr6_table *mrt, struct sock *sk)
  1332. {
  1333. int err = 0;
  1334. struct net *net = sock_net(sk);
  1335. rtnl_lock();
  1336. write_lock_bh(&mrt_lock);
  1337. if (likely(mrt->mroute6_sk == NULL)) {
  1338. mrt->mroute6_sk = sk;
  1339. net->ipv6.devconf_all->mc_forwarding++;
  1340. } else {
  1341. err = -EADDRINUSE;
  1342. }
  1343. write_unlock_bh(&mrt_lock);
  1344. if (!err)
  1345. inet6_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
  1346. NETCONFA_IFINDEX_ALL,
  1347. net->ipv6.devconf_all);
  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. write_unlock_bh(&mrt_lock);
  1363. inet6_netconf_notify_devconf(net,
  1364. NETCONFA_MC_FORWARDING,
  1365. NETCONFA_IFINDEX_ALL,
  1366. net->ipv6.devconf_all);
  1367. mroute_clean_tables(mrt, false);
  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, 0, 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. else
  1511. raw6_sk(sk)->ip6mr_table = v;
  1512. rtnl_unlock();
  1513. return ret;
  1514. }
  1515. #endif
  1516. /*
  1517. * Spurious command, or MRT6_VERSION which you cannot
  1518. * set.
  1519. */
  1520. default:
  1521. return -ENOPROTOOPT;
  1522. }
  1523. }
  1524. /*
  1525. * Getsock opt support for the multicast routing system.
  1526. */
  1527. int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
  1528. int __user *optlen)
  1529. {
  1530. int olr;
  1531. int val;
  1532. struct net *net = sock_net(sk);
  1533. struct mr6_table *mrt;
  1534. mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
  1535. if (!mrt)
  1536. return -ENOENT;
  1537. switch (optname) {
  1538. case MRT6_VERSION:
  1539. val = 0x0305;
  1540. break;
  1541. #ifdef CONFIG_IPV6_PIMSM_V2
  1542. case MRT6_PIM:
  1543. val = mrt->mroute_do_pim;
  1544. break;
  1545. #endif
  1546. case MRT6_ASSERT:
  1547. val = mrt->mroute_do_assert;
  1548. break;
  1549. default:
  1550. return -ENOPROTOOPT;
  1551. }
  1552. if (get_user(olr, optlen))
  1553. return -EFAULT;
  1554. olr = min_t(int, olr, sizeof(int));
  1555. if (olr < 0)
  1556. return -EINVAL;
  1557. if (put_user(olr, optlen))
  1558. return -EFAULT;
  1559. if (copy_to_user(optval, &val, olr))
  1560. return -EFAULT;
  1561. return 0;
  1562. }
  1563. /*
  1564. * The IP multicast ioctl support routines.
  1565. */
  1566. int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
  1567. {
  1568. struct sioc_sg_req6 sr;
  1569. struct sioc_mif_req6 vr;
  1570. struct mif_device *vif;
  1571. struct mfc6_cache *c;
  1572. struct net *net = sock_net(sk);
  1573. struct mr6_table *mrt;
  1574. mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
  1575. if (!mrt)
  1576. return -ENOENT;
  1577. switch (cmd) {
  1578. case SIOCGETMIFCNT_IN6:
  1579. if (copy_from_user(&vr, arg, sizeof(vr)))
  1580. return -EFAULT;
  1581. if (vr.mifi >= mrt->maxvif)
  1582. return -EINVAL;
  1583. read_lock(&mrt_lock);
  1584. vif = &mrt->vif6_table[vr.mifi];
  1585. if (MIF_EXISTS(mrt, vr.mifi)) {
  1586. vr.icount = vif->pkt_in;
  1587. vr.ocount = vif->pkt_out;
  1588. vr.ibytes = vif->bytes_in;
  1589. vr.obytes = vif->bytes_out;
  1590. read_unlock(&mrt_lock);
  1591. if (copy_to_user(arg, &vr, sizeof(vr)))
  1592. return -EFAULT;
  1593. return 0;
  1594. }
  1595. read_unlock(&mrt_lock);
  1596. return -EADDRNOTAVAIL;
  1597. case SIOCGETSGCNT_IN6:
  1598. if (copy_from_user(&sr, arg, sizeof(sr)))
  1599. return -EFAULT;
  1600. read_lock(&mrt_lock);
  1601. c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
  1602. if (c) {
  1603. sr.pktcnt = c->mfc_un.res.pkt;
  1604. sr.bytecnt = c->mfc_un.res.bytes;
  1605. sr.wrong_if = c->mfc_un.res.wrong_if;
  1606. read_unlock(&mrt_lock);
  1607. if (copy_to_user(arg, &sr, sizeof(sr)))
  1608. return -EFAULT;
  1609. return 0;
  1610. }
  1611. read_unlock(&mrt_lock);
  1612. return -EADDRNOTAVAIL;
  1613. default:
  1614. return -ENOIOCTLCMD;
  1615. }
  1616. }
  1617. #ifdef CONFIG_COMPAT
  1618. struct compat_sioc_sg_req6 {
  1619. struct sockaddr_in6 src;
  1620. struct sockaddr_in6 grp;
  1621. compat_ulong_t pktcnt;
  1622. compat_ulong_t bytecnt;
  1623. compat_ulong_t wrong_if;
  1624. };
  1625. struct compat_sioc_mif_req6 {
  1626. mifi_t mifi;
  1627. compat_ulong_t icount;
  1628. compat_ulong_t ocount;
  1629. compat_ulong_t ibytes;
  1630. compat_ulong_t obytes;
  1631. };
  1632. int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
  1633. {
  1634. struct compat_sioc_sg_req6 sr;
  1635. struct compat_sioc_mif_req6 vr;
  1636. struct mif_device *vif;
  1637. struct mfc6_cache *c;
  1638. struct net *net = sock_net(sk);
  1639. struct mr6_table *mrt;
  1640. mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
  1641. if (!mrt)
  1642. return -ENOENT;
  1643. switch (cmd) {
  1644. case SIOCGETMIFCNT_IN6:
  1645. if (copy_from_user(&vr, arg, sizeof(vr)))
  1646. return -EFAULT;
  1647. if (vr.mifi >= mrt->maxvif)
  1648. return -EINVAL;
  1649. read_lock(&mrt_lock);
  1650. vif = &mrt->vif6_table[vr.mifi];
  1651. if (MIF_EXISTS(mrt, vr.mifi)) {
  1652. vr.icount = vif->pkt_in;
  1653. vr.ocount = vif->pkt_out;
  1654. vr.ibytes = vif->bytes_in;
  1655. vr.obytes = vif->bytes_out;
  1656. read_unlock(&mrt_lock);
  1657. if (copy_to_user(arg, &vr, sizeof(vr)))
  1658. return -EFAULT;
  1659. return 0;
  1660. }
  1661. read_unlock(&mrt_lock);
  1662. return -EADDRNOTAVAIL;
  1663. case SIOCGETSGCNT_IN6:
  1664. if (copy_from_user(&sr, arg, sizeof(sr)))
  1665. return -EFAULT;
  1666. read_lock(&mrt_lock);
  1667. c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
  1668. if (c) {
  1669. sr.pktcnt = c->mfc_un.res.pkt;
  1670. sr.bytecnt = c->mfc_un.res.bytes;
  1671. sr.wrong_if = c->mfc_un.res.wrong_if;
  1672. read_unlock(&mrt_lock);
  1673. if (copy_to_user(arg, &sr, sizeof(sr)))
  1674. return -EFAULT;
  1675. return 0;
  1676. }
  1677. read_unlock(&mrt_lock);
  1678. return -EADDRNOTAVAIL;
  1679. default:
  1680. return -ENOIOCTLCMD;
  1681. }
  1682. }
  1683. #endif
  1684. static inline int ip6mr_forward2_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  1685. {
  1686. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  1687. IPSTATS_MIB_OUTFORWDATAGRAMS);
  1688. __IP6_ADD_STATS(net, ip6_dst_idev(skb_dst(skb)),
  1689. IPSTATS_MIB_OUTOCTETS, skb->len);
  1690. return dst_output(net, sk, skb);
  1691. }
  1692. /*
  1693. * Processing handlers for ip6mr_forward
  1694. */
  1695. static int ip6mr_forward2(struct net *net, struct mr6_table *mrt,
  1696. struct sk_buff *skb, struct mfc6_cache *c, int vifi)
  1697. {
  1698. struct ipv6hdr *ipv6h;
  1699. struct mif_device *vif = &mrt->vif6_table[vifi];
  1700. struct net_device *dev;
  1701. struct dst_entry *dst;
  1702. struct flowi6 fl6;
  1703. if (!vif->dev)
  1704. goto out_free;
  1705. #ifdef CONFIG_IPV6_PIMSM_V2
  1706. if (vif->flags & MIFF_REGISTER) {
  1707. vif->pkt_out++;
  1708. vif->bytes_out += skb->len;
  1709. vif->dev->stats.tx_bytes += skb->len;
  1710. vif->dev->stats.tx_packets++;
  1711. ip6mr_cache_report(mrt, skb, vifi, MRT6MSG_WHOLEPKT);
  1712. goto out_free;
  1713. }
  1714. #endif
  1715. ipv6h = ipv6_hdr(skb);
  1716. fl6 = (struct flowi6) {
  1717. .flowi6_oif = vif->link,
  1718. .daddr = ipv6h->daddr,
  1719. };
  1720. dst = ip6_route_output(net, NULL, &fl6);
  1721. if (dst->error) {
  1722. dst_release(dst);
  1723. goto out_free;
  1724. }
  1725. skb_dst_drop(skb);
  1726. skb_dst_set(skb, dst);
  1727. /*
  1728. * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
  1729. * not only before forwarding, but after forwarding on all output
  1730. * interfaces. It is clear, if mrouter runs a multicasting
  1731. * program, it should receive packets not depending to what interface
  1732. * program is joined.
  1733. * If we will not make it, the program will have to join on all
  1734. * interfaces. On the other hand, multihoming host (or router, but
  1735. * not mrouter) cannot join to more than one interface - it will
  1736. * result in receiving multiple packets.
  1737. */
  1738. dev = vif->dev;
  1739. skb->dev = dev;
  1740. vif->pkt_out++;
  1741. vif->bytes_out += skb->len;
  1742. /* We are about to write */
  1743. /* XXX: extension headers? */
  1744. if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
  1745. goto out_free;
  1746. ipv6h = ipv6_hdr(skb);
  1747. ipv6h->hop_limit--;
  1748. IP6CB(skb)->flags |= IP6SKB_FORWARDED;
  1749. return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
  1750. net, NULL, skb, skb->dev, dev,
  1751. ip6mr_forward2_finish);
  1752. out_free:
  1753. kfree_skb(skb);
  1754. return 0;
  1755. }
  1756. static int ip6mr_find_vif(struct mr6_table *mrt, struct net_device *dev)
  1757. {
  1758. int ct;
  1759. for (ct = mrt->maxvif - 1; ct >= 0; ct--) {
  1760. if (mrt->vif6_table[ct].dev == dev)
  1761. break;
  1762. }
  1763. return ct;
  1764. }
  1765. static void ip6_mr_forward(struct net *net, struct mr6_table *mrt,
  1766. struct sk_buff *skb, struct mfc6_cache *cache)
  1767. {
  1768. int psend = -1;
  1769. int vif, ct;
  1770. int true_vifi = ip6mr_find_vif(mrt, skb->dev);
  1771. vif = cache->mf6c_parent;
  1772. cache->mfc_un.res.pkt++;
  1773. cache->mfc_un.res.bytes += skb->len;
  1774. cache->mfc_un.res.lastuse = jiffies;
  1775. if (ipv6_addr_any(&cache->mf6c_origin) && true_vifi >= 0) {
  1776. struct mfc6_cache *cache_proxy;
  1777. /* For an (*,G) entry, we only check that the incoming
  1778. * interface is part of the static tree.
  1779. */
  1780. cache_proxy = ip6mr_cache_find_any_parent(mrt, vif);
  1781. if (cache_proxy &&
  1782. cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
  1783. goto forward;
  1784. }
  1785. /*
  1786. * Wrong interface: drop packet and (maybe) send PIM assert.
  1787. */
  1788. if (mrt->vif6_table[vif].dev != skb->dev) {
  1789. cache->mfc_un.res.wrong_if++;
  1790. if (true_vifi >= 0 && mrt->mroute_do_assert &&
  1791. /* pimsm uses asserts, when switching from RPT to SPT,
  1792. so that we cannot check that packet arrived on an oif.
  1793. It is bad, but otherwise we would need to move pretty
  1794. large chunk of pimd to kernel. Ough... --ANK
  1795. */
  1796. (mrt->mroute_do_pim ||
  1797. cache->mfc_un.res.ttls[true_vifi] < 255) &&
  1798. time_after(jiffies,
  1799. cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
  1800. cache->mfc_un.res.last_assert = jiffies;
  1801. ip6mr_cache_report(mrt, skb, true_vifi, MRT6MSG_WRONGMIF);
  1802. }
  1803. goto dont_forward;
  1804. }
  1805. forward:
  1806. mrt->vif6_table[vif].pkt_in++;
  1807. mrt->vif6_table[vif].bytes_in += skb->len;
  1808. /*
  1809. * Forward the frame
  1810. */
  1811. if (ipv6_addr_any(&cache->mf6c_origin) &&
  1812. ipv6_addr_any(&cache->mf6c_mcastgrp)) {
  1813. if (true_vifi >= 0 &&
  1814. true_vifi != cache->mf6c_parent &&
  1815. ipv6_hdr(skb)->hop_limit >
  1816. cache->mfc_un.res.ttls[cache->mf6c_parent]) {
  1817. /* It's an (*,*) entry and the packet is not coming from
  1818. * the upstream: forward the packet to the upstream
  1819. * only.
  1820. */
  1821. psend = cache->mf6c_parent;
  1822. goto last_forward;
  1823. }
  1824. goto dont_forward;
  1825. }
  1826. for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
  1827. /* For (*,G) entry, don't forward to the incoming interface */
  1828. if ((!ipv6_addr_any(&cache->mf6c_origin) || ct != true_vifi) &&
  1829. ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
  1830. if (psend != -1) {
  1831. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1832. if (skb2)
  1833. ip6mr_forward2(net, mrt, skb2, cache, psend);
  1834. }
  1835. psend = ct;
  1836. }
  1837. }
  1838. last_forward:
  1839. if (psend != -1) {
  1840. ip6mr_forward2(net, mrt, skb, cache, psend);
  1841. return;
  1842. }
  1843. dont_forward:
  1844. kfree_skb(skb);
  1845. }
  1846. /*
  1847. * Multicast packets for forwarding arrive here
  1848. */
  1849. int ip6_mr_input(struct sk_buff *skb)
  1850. {
  1851. struct mfc6_cache *cache;
  1852. struct net *net = dev_net(skb->dev);
  1853. struct mr6_table *mrt;
  1854. struct flowi6 fl6 = {
  1855. .flowi6_iif = skb->dev->ifindex,
  1856. .flowi6_mark = skb->mark,
  1857. };
  1858. int err;
  1859. err = ip6mr_fib_lookup(net, &fl6, &mrt);
  1860. if (err < 0) {
  1861. kfree_skb(skb);
  1862. return err;
  1863. }
  1864. read_lock(&mrt_lock);
  1865. cache = ip6mr_cache_find(mrt,
  1866. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
  1867. if (!cache) {
  1868. int vif = ip6mr_find_vif(mrt, skb->dev);
  1869. if (vif >= 0)
  1870. cache = ip6mr_cache_find_any(mrt,
  1871. &ipv6_hdr(skb)->daddr,
  1872. vif);
  1873. }
  1874. /*
  1875. * No usable cache entry
  1876. */
  1877. if (!cache) {
  1878. int vif;
  1879. vif = ip6mr_find_vif(mrt, skb->dev);
  1880. if (vif >= 0) {
  1881. int err = ip6mr_cache_unresolved(mrt, vif, skb);
  1882. read_unlock(&mrt_lock);
  1883. return err;
  1884. }
  1885. read_unlock(&mrt_lock);
  1886. kfree_skb(skb);
  1887. return -ENODEV;
  1888. }
  1889. ip6_mr_forward(net, mrt, skb, cache);
  1890. read_unlock(&mrt_lock);
  1891. return 0;
  1892. }
  1893. static int __ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
  1894. struct mfc6_cache *c, struct rtmsg *rtm)
  1895. {
  1896. struct rta_mfc_stats mfcs;
  1897. struct nlattr *mp_attr;
  1898. struct rtnexthop *nhp;
  1899. unsigned long lastuse;
  1900. int ct;
  1901. /* If cache is unresolved, don't try to parse IIF and OIF */
  1902. if (c->mf6c_parent >= MAXMIFS)
  1903. return -ENOENT;
  1904. if (MIF_EXISTS(mrt, c->mf6c_parent) &&
  1905. nla_put_u32(skb, RTA_IIF, mrt->vif6_table[c->mf6c_parent].dev->ifindex) < 0)
  1906. return -EMSGSIZE;
  1907. mp_attr = nla_nest_start(skb, RTA_MULTIPATH);
  1908. if (!mp_attr)
  1909. return -EMSGSIZE;
  1910. for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
  1911. if (MIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
  1912. nhp = nla_reserve_nohdr(skb, sizeof(*nhp));
  1913. if (!nhp) {
  1914. nla_nest_cancel(skb, mp_attr);
  1915. return -EMSGSIZE;
  1916. }
  1917. nhp->rtnh_flags = 0;
  1918. nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
  1919. nhp->rtnh_ifindex = mrt->vif6_table[ct].dev->ifindex;
  1920. nhp->rtnh_len = sizeof(*nhp);
  1921. }
  1922. }
  1923. nla_nest_end(skb, mp_attr);
  1924. lastuse = READ_ONCE(c->mfc_un.res.lastuse);
  1925. lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
  1926. mfcs.mfcs_packets = c->mfc_un.res.pkt;
  1927. mfcs.mfcs_bytes = c->mfc_un.res.bytes;
  1928. mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
  1929. if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
  1930. nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
  1931. RTA_PAD))
  1932. return -EMSGSIZE;
  1933. rtm->rtm_type = RTN_MULTICAST;
  1934. return 1;
  1935. }
  1936. int ip6mr_get_route(struct net *net, struct sk_buff *skb, struct rtmsg *rtm,
  1937. int nowait, u32 portid)
  1938. {
  1939. int err;
  1940. struct mr6_table *mrt;
  1941. struct mfc6_cache *cache;
  1942. struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
  1943. mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
  1944. if (!mrt)
  1945. return -ENOENT;
  1946. read_lock(&mrt_lock);
  1947. cache = ip6mr_cache_find(mrt, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
  1948. if (!cache && skb->dev) {
  1949. int vif = ip6mr_find_vif(mrt, skb->dev);
  1950. if (vif >= 0)
  1951. cache = ip6mr_cache_find_any(mrt, &rt->rt6i_dst.addr,
  1952. vif);
  1953. }
  1954. if (!cache) {
  1955. struct sk_buff *skb2;
  1956. struct ipv6hdr *iph;
  1957. struct net_device *dev;
  1958. int vif;
  1959. if (nowait) {
  1960. read_unlock(&mrt_lock);
  1961. return -EAGAIN;
  1962. }
  1963. dev = skb->dev;
  1964. if (!dev || (vif = ip6mr_find_vif(mrt, dev)) < 0) {
  1965. read_unlock(&mrt_lock);
  1966. return -ENODEV;
  1967. }
  1968. /* really correct? */
  1969. skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  1970. if (!skb2) {
  1971. read_unlock(&mrt_lock);
  1972. return -ENOMEM;
  1973. }
  1974. NETLINK_CB(skb2).portid = portid;
  1975. skb_reset_transport_header(skb2);
  1976. skb_put(skb2, sizeof(struct ipv6hdr));
  1977. skb_reset_network_header(skb2);
  1978. iph = ipv6_hdr(skb2);
  1979. iph->version = 0;
  1980. iph->priority = 0;
  1981. iph->flow_lbl[0] = 0;
  1982. iph->flow_lbl[1] = 0;
  1983. iph->flow_lbl[2] = 0;
  1984. iph->payload_len = 0;
  1985. iph->nexthdr = IPPROTO_NONE;
  1986. iph->hop_limit = 0;
  1987. iph->saddr = rt->rt6i_src.addr;
  1988. iph->daddr = rt->rt6i_dst.addr;
  1989. err = ip6mr_cache_unresolved(mrt, vif, skb2);
  1990. read_unlock(&mrt_lock);
  1991. return err;
  1992. }
  1993. if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
  1994. cache->mfc_flags |= MFC_NOTIFY;
  1995. err = __ip6mr_fill_mroute(mrt, skb, cache, rtm);
  1996. read_unlock(&mrt_lock);
  1997. return err;
  1998. }
  1999. static int ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
  2000. u32 portid, u32 seq, struct mfc6_cache *c, int cmd,
  2001. int flags)
  2002. {
  2003. struct nlmsghdr *nlh;
  2004. struct rtmsg *rtm;
  2005. int err;
  2006. nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
  2007. if (!nlh)
  2008. return -EMSGSIZE;
  2009. rtm = nlmsg_data(nlh);
  2010. rtm->rtm_family = RTNL_FAMILY_IP6MR;
  2011. rtm->rtm_dst_len = 128;
  2012. rtm->rtm_src_len = 128;
  2013. rtm->rtm_tos = 0;
  2014. rtm->rtm_table = mrt->id;
  2015. if (nla_put_u32(skb, RTA_TABLE, mrt->id))
  2016. goto nla_put_failure;
  2017. rtm->rtm_type = RTN_MULTICAST;
  2018. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  2019. if (c->mfc_flags & MFC_STATIC)
  2020. rtm->rtm_protocol = RTPROT_STATIC;
  2021. else
  2022. rtm->rtm_protocol = RTPROT_MROUTED;
  2023. rtm->rtm_flags = 0;
  2024. if (nla_put_in6_addr(skb, RTA_SRC, &c->mf6c_origin) ||
  2025. nla_put_in6_addr(skb, RTA_DST, &c->mf6c_mcastgrp))
  2026. goto nla_put_failure;
  2027. err = __ip6mr_fill_mroute(mrt, skb, c, rtm);
  2028. /* do not break the dump if cache is unresolved */
  2029. if (err < 0 && err != -ENOENT)
  2030. goto nla_put_failure;
  2031. nlmsg_end(skb, nlh);
  2032. return 0;
  2033. nla_put_failure:
  2034. nlmsg_cancel(skb, nlh);
  2035. return -EMSGSIZE;
  2036. }
  2037. static int mr6_msgsize(bool unresolved, int maxvif)
  2038. {
  2039. size_t len =
  2040. NLMSG_ALIGN(sizeof(struct rtmsg))
  2041. + nla_total_size(4) /* RTA_TABLE */
  2042. + nla_total_size(sizeof(struct in6_addr)) /* RTA_SRC */
  2043. + nla_total_size(sizeof(struct in6_addr)) /* RTA_DST */
  2044. ;
  2045. if (!unresolved)
  2046. len = len
  2047. + nla_total_size(4) /* RTA_IIF */
  2048. + nla_total_size(0) /* RTA_MULTIPATH */
  2049. + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
  2050. /* RTA_MFC_STATS */
  2051. + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
  2052. ;
  2053. return len;
  2054. }
  2055. static void mr6_netlink_event(struct mr6_table *mrt, struct mfc6_cache *mfc,
  2056. int cmd)
  2057. {
  2058. struct net *net = read_pnet(&mrt->net);
  2059. struct sk_buff *skb;
  2060. int err = -ENOBUFS;
  2061. skb = nlmsg_new(mr6_msgsize(mfc->mf6c_parent >= MAXMIFS, mrt->maxvif),
  2062. GFP_ATOMIC);
  2063. if (!skb)
  2064. goto errout;
  2065. err = ip6mr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
  2066. if (err < 0)
  2067. goto errout;
  2068. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_MROUTE, NULL, GFP_ATOMIC);
  2069. return;
  2070. errout:
  2071. kfree_skb(skb);
  2072. if (err < 0)
  2073. rtnl_set_sk_err(net, RTNLGRP_IPV6_MROUTE, err);
  2074. }
  2075. static int ip6mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
  2076. {
  2077. struct net *net = sock_net(skb->sk);
  2078. struct mr6_table *mrt;
  2079. struct mfc6_cache *mfc;
  2080. unsigned int t = 0, s_t;
  2081. unsigned int h = 0, s_h;
  2082. unsigned int e = 0, s_e;
  2083. s_t = cb->args[0];
  2084. s_h = cb->args[1];
  2085. s_e = cb->args[2];
  2086. read_lock(&mrt_lock);
  2087. ip6mr_for_each_table(mrt, net) {
  2088. if (t < s_t)
  2089. goto next_table;
  2090. if (t > s_t)
  2091. s_h = 0;
  2092. for (h = s_h; h < MFC6_LINES; h++) {
  2093. list_for_each_entry(mfc, &mrt->mfc6_cache_array[h], list) {
  2094. if (e < s_e)
  2095. goto next_entry;
  2096. if (ip6mr_fill_mroute(mrt, skb,
  2097. NETLINK_CB(cb->skb).portid,
  2098. cb->nlh->nlmsg_seq,
  2099. mfc, RTM_NEWROUTE,
  2100. NLM_F_MULTI) < 0)
  2101. goto done;
  2102. next_entry:
  2103. e++;
  2104. }
  2105. e = s_e = 0;
  2106. }
  2107. spin_lock_bh(&mfc_unres_lock);
  2108. list_for_each_entry(mfc, &mrt->mfc6_unres_queue, list) {
  2109. if (e < s_e)
  2110. goto next_entry2;
  2111. if (ip6mr_fill_mroute(mrt, skb,
  2112. NETLINK_CB(cb->skb).portid,
  2113. cb->nlh->nlmsg_seq,
  2114. mfc, RTM_NEWROUTE,
  2115. NLM_F_MULTI) < 0) {
  2116. spin_unlock_bh(&mfc_unres_lock);
  2117. goto done;
  2118. }
  2119. next_entry2:
  2120. e++;
  2121. }
  2122. spin_unlock_bh(&mfc_unres_lock);
  2123. e = s_e = 0;
  2124. s_h = 0;
  2125. next_table:
  2126. t++;
  2127. }
  2128. done:
  2129. read_unlock(&mrt_lock);
  2130. cb->args[2] = e;
  2131. cb->args[1] = h;
  2132. cb->args[0] = t;
  2133. return skb->len;
  2134. }