ipmr.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828
  1. /*
  2. * IP multicast routing support for mrouted 3.6/3.8
  3. *
  4. * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
  5. * Linux Consultancy and Custom Driver Development
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Fixes:
  13. * Michael Chastain : Incorrect size of copying.
  14. * Alan Cox : Added the cache manager code
  15. * Alan Cox : Fixed the clone/copy bug and device race.
  16. * Mike McLagan : Routing by source
  17. * Malcolm Beattie : Buffer handling fixes.
  18. * Alexey Kuznetsov : Double buffer free and other fixes.
  19. * SVR Anand : Fixed several multicast bugs and problems.
  20. * Alexey Kuznetsov : Status, optimisations and more.
  21. * Brad Parker : Better behaviour on mrouted upcall
  22. * overflow.
  23. * Carlos Picoto : PIMv1 Support
  24. * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
  25. * Relax this requirement to work with older peers.
  26. *
  27. */
  28. #include <asm/uaccess.h>
  29. #include <linux/types.h>
  30. #include <linux/capability.h>
  31. #include <linux/errno.h>
  32. #include <linux/timer.h>
  33. #include <linux/mm.h>
  34. #include <linux/kernel.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/stat.h>
  37. #include <linux/socket.h>
  38. #include <linux/in.h>
  39. #include <linux/inet.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/inetdevice.h>
  42. #include <linux/igmp.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/seq_file.h>
  45. #include <linux/mroute.h>
  46. #include <linux/init.h>
  47. #include <linux/if_ether.h>
  48. #include <linux/slab.h>
  49. #include <net/net_namespace.h>
  50. #include <net/ip.h>
  51. #include <net/protocol.h>
  52. #include <linux/skbuff.h>
  53. #include <net/route.h>
  54. #include <net/sock.h>
  55. #include <net/icmp.h>
  56. #include <net/udp.h>
  57. #include <net/raw.h>
  58. #include <linux/notifier.h>
  59. #include <linux/if_arp.h>
  60. #include <linux/netfilter_ipv4.h>
  61. #include <linux/compat.h>
  62. #include <linux/export.h>
  63. #include <net/ip_tunnels.h>
  64. #include <net/checksum.h>
  65. #include <net/netlink.h>
  66. #include <net/fib_rules.h>
  67. #include <linux/netconf.h>
  68. #include <net/nexthop.h>
  69. struct ipmr_rule {
  70. struct fib_rule common;
  71. };
  72. struct ipmr_result {
  73. struct mr_table *mrt;
  74. };
  75. /* Big lock, protecting vif table, mrt cache and mroute socket state.
  76. * Note that the changes are semaphored via rtnl_lock.
  77. */
  78. static DEFINE_RWLOCK(mrt_lock);
  79. /* Multicast router control variables */
  80. /* Special spinlock for queue of unresolved entries */
  81. static DEFINE_SPINLOCK(mfc_unres_lock);
  82. /* We return to original Alan's scheme. Hash table of resolved
  83. * entries is changed only in process context and protected
  84. * with weak lock mrt_lock. Queue of unresolved entries is protected
  85. * with strong spinlock mfc_unres_lock.
  86. *
  87. * In this case data path is free of exclusive locks at all.
  88. */
  89. static struct kmem_cache *mrt_cachep __read_mostly;
  90. static struct mr_table *ipmr_new_table(struct net *net, u32 id);
  91. static void ipmr_free_table(struct mr_table *mrt);
  92. static void ip_mr_forward(struct net *net, struct mr_table *mrt,
  93. struct sk_buff *skb, struct mfc_cache *cache,
  94. int local);
  95. static int ipmr_cache_report(struct mr_table *mrt,
  96. struct sk_buff *pkt, vifi_t vifi, int assert);
  97. static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  98. struct mfc_cache *c, struct rtmsg *rtm);
  99. static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
  100. int cmd);
  101. static void mroute_clean_tables(struct mr_table *mrt, bool all);
  102. static void ipmr_expire_process(unsigned long arg);
  103. #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
  104. #define ipmr_for_each_table(mrt, net) \
  105. list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
  106. static struct mr_table *ipmr_get_table(struct net *net, u32 id)
  107. {
  108. struct mr_table *mrt;
  109. ipmr_for_each_table(mrt, net) {
  110. if (mrt->id == id)
  111. return mrt;
  112. }
  113. return NULL;
  114. }
  115. static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
  116. struct mr_table **mrt)
  117. {
  118. int err;
  119. struct ipmr_result res;
  120. struct fib_lookup_arg arg = {
  121. .result = &res,
  122. .flags = FIB_LOOKUP_NOREF,
  123. };
  124. err = fib_rules_lookup(net->ipv4.mr_rules_ops,
  125. flowi4_to_flowi(flp4), 0, &arg);
  126. if (err < 0)
  127. return err;
  128. *mrt = res.mrt;
  129. return 0;
  130. }
  131. static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
  132. int flags, struct fib_lookup_arg *arg)
  133. {
  134. struct ipmr_result *res = arg->result;
  135. struct mr_table *mrt;
  136. switch (rule->action) {
  137. case FR_ACT_TO_TBL:
  138. break;
  139. case FR_ACT_UNREACHABLE:
  140. return -ENETUNREACH;
  141. case FR_ACT_PROHIBIT:
  142. return -EACCES;
  143. case FR_ACT_BLACKHOLE:
  144. default:
  145. return -EINVAL;
  146. }
  147. mrt = ipmr_get_table(rule->fr_net, rule->table);
  148. if (!mrt)
  149. return -EAGAIN;
  150. res->mrt = mrt;
  151. return 0;
  152. }
  153. static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  154. {
  155. return 1;
  156. }
  157. static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
  158. FRA_GENERIC_POLICY,
  159. };
  160. static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  161. struct fib_rule_hdr *frh, struct nlattr **tb)
  162. {
  163. return 0;
  164. }
  165. static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  166. struct nlattr **tb)
  167. {
  168. return 1;
  169. }
  170. static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  171. struct fib_rule_hdr *frh)
  172. {
  173. frh->dst_len = 0;
  174. frh->src_len = 0;
  175. frh->tos = 0;
  176. return 0;
  177. }
  178. static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
  179. .family = RTNL_FAMILY_IPMR,
  180. .rule_size = sizeof(struct ipmr_rule),
  181. .addr_size = sizeof(u32),
  182. .action = ipmr_rule_action,
  183. .match = ipmr_rule_match,
  184. .configure = ipmr_rule_configure,
  185. .compare = ipmr_rule_compare,
  186. .fill = ipmr_rule_fill,
  187. .nlgroup = RTNLGRP_IPV4_RULE,
  188. .policy = ipmr_rule_policy,
  189. .owner = THIS_MODULE,
  190. };
  191. static int __net_init ipmr_rules_init(struct net *net)
  192. {
  193. struct fib_rules_ops *ops;
  194. struct mr_table *mrt;
  195. int err;
  196. ops = fib_rules_register(&ipmr_rules_ops_template, net);
  197. if (IS_ERR(ops))
  198. return PTR_ERR(ops);
  199. INIT_LIST_HEAD(&net->ipv4.mr_tables);
  200. mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
  201. if (IS_ERR(mrt)) {
  202. err = PTR_ERR(mrt);
  203. goto err1;
  204. }
  205. err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
  206. if (err < 0)
  207. goto err2;
  208. net->ipv4.mr_rules_ops = ops;
  209. return 0;
  210. err2:
  211. ipmr_free_table(mrt);
  212. err1:
  213. fib_rules_unregister(ops);
  214. return err;
  215. }
  216. static void __net_exit ipmr_rules_exit(struct net *net)
  217. {
  218. struct mr_table *mrt, *next;
  219. rtnl_lock();
  220. list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
  221. list_del(&mrt->list);
  222. ipmr_free_table(mrt);
  223. }
  224. fib_rules_unregister(net->ipv4.mr_rules_ops);
  225. rtnl_unlock();
  226. }
  227. #else
  228. #define ipmr_for_each_table(mrt, net) \
  229. for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
  230. static struct mr_table *ipmr_get_table(struct net *net, u32 id)
  231. {
  232. return net->ipv4.mrt;
  233. }
  234. static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
  235. struct mr_table **mrt)
  236. {
  237. *mrt = net->ipv4.mrt;
  238. return 0;
  239. }
  240. static int __net_init ipmr_rules_init(struct net *net)
  241. {
  242. struct mr_table *mrt;
  243. mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
  244. if (IS_ERR(mrt))
  245. return PTR_ERR(mrt);
  246. net->ipv4.mrt = mrt;
  247. return 0;
  248. }
  249. static void __net_exit ipmr_rules_exit(struct net *net)
  250. {
  251. rtnl_lock();
  252. ipmr_free_table(net->ipv4.mrt);
  253. net->ipv4.mrt = NULL;
  254. rtnl_unlock();
  255. }
  256. #endif
  257. static struct mr_table *ipmr_new_table(struct net *net, u32 id)
  258. {
  259. struct mr_table *mrt;
  260. unsigned int i;
  261. /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
  262. if (id != RT_TABLE_DEFAULT && id >= 1000000000)
  263. return ERR_PTR(-EINVAL);
  264. mrt = ipmr_get_table(net, id);
  265. if (mrt)
  266. return mrt;
  267. mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
  268. if (!mrt)
  269. return ERR_PTR(-ENOMEM);
  270. write_pnet(&mrt->net, net);
  271. mrt->id = id;
  272. /* Forwarding cache */
  273. for (i = 0; i < MFC_LINES; i++)
  274. INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
  275. INIT_LIST_HEAD(&mrt->mfc_unres_queue);
  276. setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
  277. (unsigned long)mrt);
  278. mrt->mroute_reg_vif_num = -1;
  279. #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
  280. list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
  281. #endif
  282. return mrt;
  283. }
  284. static void ipmr_free_table(struct mr_table *mrt)
  285. {
  286. del_timer_sync(&mrt->ipmr_expire_timer);
  287. mroute_clean_tables(mrt, true);
  288. kfree(mrt);
  289. }
  290. /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
  291. static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
  292. {
  293. struct net *net = dev_net(dev);
  294. dev_close(dev);
  295. dev = __dev_get_by_name(net, "tunl0");
  296. if (dev) {
  297. const struct net_device_ops *ops = dev->netdev_ops;
  298. struct ifreq ifr;
  299. struct ip_tunnel_parm p;
  300. memset(&p, 0, sizeof(p));
  301. p.iph.daddr = v->vifc_rmt_addr.s_addr;
  302. p.iph.saddr = v->vifc_lcl_addr.s_addr;
  303. p.iph.version = 4;
  304. p.iph.ihl = 5;
  305. p.iph.protocol = IPPROTO_IPIP;
  306. sprintf(p.name, "dvmrp%d", v->vifc_vifi);
  307. ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
  308. if (ops->ndo_do_ioctl) {
  309. mm_segment_t oldfs = get_fs();
  310. set_fs(KERNEL_DS);
  311. ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
  312. set_fs(oldfs);
  313. }
  314. }
  315. }
  316. /* Initialize ipmr pimreg/tunnel in_device */
  317. static bool ipmr_init_vif_indev(const struct net_device *dev)
  318. {
  319. struct in_device *in_dev;
  320. ASSERT_RTNL();
  321. in_dev = __in_dev_get_rtnl(dev);
  322. if (!in_dev)
  323. return false;
  324. ipv4_devconf_setall(in_dev);
  325. neigh_parms_data_state_setall(in_dev->arp_parms);
  326. IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
  327. return true;
  328. }
  329. static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
  330. {
  331. struct net_device *dev;
  332. dev = __dev_get_by_name(net, "tunl0");
  333. if (dev) {
  334. const struct net_device_ops *ops = dev->netdev_ops;
  335. int err;
  336. struct ifreq ifr;
  337. struct ip_tunnel_parm p;
  338. memset(&p, 0, sizeof(p));
  339. p.iph.daddr = v->vifc_rmt_addr.s_addr;
  340. p.iph.saddr = v->vifc_lcl_addr.s_addr;
  341. p.iph.version = 4;
  342. p.iph.ihl = 5;
  343. p.iph.protocol = IPPROTO_IPIP;
  344. sprintf(p.name, "dvmrp%d", v->vifc_vifi);
  345. ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
  346. if (ops->ndo_do_ioctl) {
  347. mm_segment_t oldfs = get_fs();
  348. set_fs(KERNEL_DS);
  349. err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
  350. set_fs(oldfs);
  351. } else {
  352. err = -EOPNOTSUPP;
  353. }
  354. dev = NULL;
  355. if (err == 0 &&
  356. (dev = __dev_get_by_name(net, p.name)) != NULL) {
  357. dev->flags |= IFF_MULTICAST;
  358. if (!ipmr_init_vif_indev(dev))
  359. goto failure;
  360. if (dev_open(dev))
  361. goto failure;
  362. dev_hold(dev);
  363. }
  364. }
  365. return dev;
  366. failure:
  367. unregister_netdevice(dev);
  368. return NULL;
  369. }
  370. #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
  371. static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
  372. {
  373. struct net *net = dev_net(dev);
  374. struct mr_table *mrt;
  375. struct flowi4 fl4 = {
  376. .flowi4_oif = dev->ifindex,
  377. .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
  378. .flowi4_mark = skb->mark,
  379. };
  380. int err;
  381. err = ipmr_fib_lookup(net, &fl4, &mrt);
  382. if (err < 0) {
  383. kfree_skb(skb);
  384. return err;
  385. }
  386. read_lock(&mrt_lock);
  387. dev->stats.tx_bytes += skb->len;
  388. dev->stats.tx_packets++;
  389. ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
  390. read_unlock(&mrt_lock);
  391. kfree_skb(skb);
  392. return NETDEV_TX_OK;
  393. }
  394. static int reg_vif_get_iflink(const struct net_device *dev)
  395. {
  396. return 0;
  397. }
  398. static const struct net_device_ops reg_vif_netdev_ops = {
  399. .ndo_start_xmit = reg_vif_xmit,
  400. .ndo_get_iflink = reg_vif_get_iflink,
  401. };
  402. static void reg_vif_setup(struct net_device *dev)
  403. {
  404. dev->type = ARPHRD_PIMREG;
  405. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
  406. dev->flags = IFF_NOARP;
  407. dev->netdev_ops = &reg_vif_netdev_ops;
  408. dev->destructor = free_netdev;
  409. dev->features |= NETIF_F_NETNS_LOCAL;
  410. }
  411. static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
  412. {
  413. struct net_device *dev;
  414. char name[IFNAMSIZ];
  415. if (mrt->id == RT_TABLE_DEFAULT)
  416. sprintf(name, "pimreg");
  417. else
  418. sprintf(name, "pimreg%u", mrt->id);
  419. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
  420. if (!dev)
  421. return NULL;
  422. dev_net_set(dev, net);
  423. if (register_netdevice(dev)) {
  424. free_netdev(dev);
  425. return NULL;
  426. }
  427. if (!ipmr_init_vif_indev(dev))
  428. goto failure;
  429. if (dev_open(dev))
  430. goto failure;
  431. dev_hold(dev);
  432. return dev;
  433. failure:
  434. unregister_netdevice(dev);
  435. return NULL;
  436. }
  437. /* called with rcu_read_lock() */
  438. static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
  439. unsigned int pimlen)
  440. {
  441. struct net_device *reg_dev = NULL;
  442. struct iphdr *encap;
  443. encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
  444. /* Check that:
  445. * a. packet is really sent to a multicast group
  446. * b. packet is not a NULL-REGISTER
  447. * c. packet is not truncated
  448. */
  449. if (!ipv4_is_multicast(encap->daddr) ||
  450. encap->tot_len == 0 ||
  451. ntohs(encap->tot_len) + pimlen > skb->len)
  452. return 1;
  453. read_lock(&mrt_lock);
  454. if (mrt->mroute_reg_vif_num >= 0)
  455. reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
  456. read_unlock(&mrt_lock);
  457. if (!reg_dev)
  458. return 1;
  459. skb->mac_header = skb->network_header;
  460. skb_pull(skb, (u8 *)encap - skb->data);
  461. skb_reset_network_header(skb);
  462. skb->protocol = htons(ETH_P_IP);
  463. skb->ip_summed = CHECKSUM_NONE;
  464. skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
  465. netif_rx(skb);
  466. return NET_RX_SUCCESS;
  467. }
  468. #else
  469. static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
  470. {
  471. return NULL;
  472. }
  473. #endif
  474. /**
  475. * vif_delete - Delete a VIF entry
  476. * @notify: Set to 1, if the caller is a notifier_call
  477. */
  478. static int vif_delete(struct mr_table *mrt, int vifi, int notify,
  479. struct list_head *head)
  480. {
  481. struct vif_device *v;
  482. struct net_device *dev;
  483. struct in_device *in_dev;
  484. if (vifi < 0 || vifi >= mrt->maxvif)
  485. return -EADDRNOTAVAIL;
  486. v = &mrt->vif_table[vifi];
  487. write_lock_bh(&mrt_lock);
  488. dev = v->dev;
  489. v->dev = NULL;
  490. if (!dev) {
  491. write_unlock_bh(&mrt_lock);
  492. return -EADDRNOTAVAIL;
  493. }
  494. if (vifi == mrt->mroute_reg_vif_num)
  495. mrt->mroute_reg_vif_num = -1;
  496. if (vifi + 1 == mrt->maxvif) {
  497. int tmp;
  498. for (tmp = vifi - 1; tmp >= 0; tmp--) {
  499. if (VIF_EXISTS(mrt, tmp))
  500. break;
  501. }
  502. mrt->maxvif = tmp+1;
  503. }
  504. write_unlock_bh(&mrt_lock);
  505. dev_set_allmulti(dev, -1);
  506. in_dev = __in_dev_get_rtnl(dev);
  507. if (in_dev) {
  508. IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
  509. inet_netconf_notify_devconf(dev_net(dev),
  510. NETCONFA_MC_FORWARDING,
  511. dev->ifindex, &in_dev->cnf);
  512. ip_rt_multicast_event(in_dev);
  513. }
  514. if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
  515. unregister_netdevice_queue(dev, head);
  516. dev_put(dev);
  517. return 0;
  518. }
  519. static void ipmr_cache_free_rcu(struct rcu_head *head)
  520. {
  521. struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
  522. kmem_cache_free(mrt_cachep, c);
  523. }
  524. static inline void ipmr_cache_free(struct mfc_cache *c)
  525. {
  526. call_rcu(&c->rcu, ipmr_cache_free_rcu);
  527. }
  528. /* Destroy an unresolved cache entry, killing queued skbs
  529. * and reporting error to netlink readers.
  530. */
  531. static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
  532. {
  533. struct net *net = read_pnet(&mrt->net);
  534. struct sk_buff *skb;
  535. struct nlmsgerr *e;
  536. atomic_dec(&mrt->cache_resolve_queue_len);
  537. while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
  538. if (ip_hdr(skb)->version == 0) {
  539. struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
  540. nlh->nlmsg_type = NLMSG_ERROR;
  541. nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
  542. skb_trim(skb, nlh->nlmsg_len);
  543. e = nlmsg_data(nlh);
  544. e->error = -ETIMEDOUT;
  545. memset(&e->msg, 0, sizeof(e->msg));
  546. rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
  547. } else {
  548. kfree_skb(skb);
  549. }
  550. }
  551. ipmr_cache_free(c);
  552. }
  553. /* Timer process for the unresolved queue. */
  554. static void ipmr_expire_process(unsigned long arg)
  555. {
  556. struct mr_table *mrt = (struct mr_table *)arg;
  557. unsigned long now;
  558. unsigned long expires;
  559. struct mfc_cache *c, *next;
  560. if (!spin_trylock(&mfc_unres_lock)) {
  561. mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
  562. return;
  563. }
  564. if (list_empty(&mrt->mfc_unres_queue))
  565. goto out;
  566. now = jiffies;
  567. expires = 10*HZ;
  568. list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
  569. if (time_after(c->mfc_un.unres.expires, now)) {
  570. unsigned long interval = c->mfc_un.unres.expires - now;
  571. if (interval < expires)
  572. expires = interval;
  573. continue;
  574. }
  575. list_del(&c->list);
  576. mroute_netlink_event(mrt, c, RTM_DELROUTE);
  577. ipmr_destroy_unres(mrt, c);
  578. }
  579. if (!list_empty(&mrt->mfc_unres_queue))
  580. mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
  581. out:
  582. spin_unlock(&mfc_unres_lock);
  583. }
  584. /* Fill oifs list. It is called under write locked mrt_lock. */
  585. static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
  586. unsigned char *ttls)
  587. {
  588. int vifi;
  589. cache->mfc_un.res.minvif = MAXVIFS;
  590. cache->mfc_un.res.maxvif = 0;
  591. memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
  592. for (vifi = 0; vifi < mrt->maxvif; vifi++) {
  593. if (VIF_EXISTS(mrt, vifi) &&
  594. ttls[vifi] && ttls[vifi] < 255) {
  595. cache->mfc_un.res.ttls[vifi] = ttls[vifi];
  596. if (cache->mfc_un.res.minvif > vifi)
  597. cache->mfc_un.res.minvif = vifi;
  598. if (cache->mfc_un.res.maxvif <= vifi)
  599. cache->mfc_un.res.maxvif = vifi + 1;
  600. }
  601. }
  602. }
  603. static int vif_add(struct net *net, struct mr_table *mrt,
  604. struct vifctl *vifc, int mrtsock)
  605. {
  606. int vifi = vifc->vifc_vifi;
  607. struct vif_device *v = &mrt->vif_table[vifi];
  608. struct net_device *dev;
  609. struct in_device *in_dev;
  610. int err;
  611. /* Is vif busy ? */
  612. if (VIF_EXISTS(mrt, vifi))
  613. return -EADDRINUSE;
  614. switch (vifc->vifc_flags) {
  615. case VIFF_REGISTER:
  616. if (!ipmr_pimsm_enabled())
  617. return -EINVAL;
  618. /* Special Purpose VIF in PIM
  619. * All the packets will be sent to the daemon
  620. */
  621. if (mrt->mroute_reg_vif_num >= 0)
  622. return -EADDRINUSE;
  623. dev = ipmr_reg_vif(net, mrt);
  624. if (!dev)
  625. return -ENOBUFS;
  626. err = dev_set_allmulti(dev, 1);
  627. if (err) {
  628. unregister_netdevice(dev);
  629. dev_put(dev);
  630. return err;
  631. }
  632. break;
  633. case VIFF_TUNNEL:
  634. dev = ipmr_new_tunnel(net, vifc);
  635. if (!dev)
  636. return -ENOBUFS;
  637. err = dev_set_allmulti(dev, 1);
  638. if (err) {
  639. ipmr_del_tunnel(dev, vifc);
  640. dev_put(dev);
  641. return err;
  642. }
  643. break;
  644. case VIFF_USE_IFINDEX:
  645. case 0:
  646. if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
  647. dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
  648. if (dev && !__in_dev_get_rtnl(dev)) {
  649. dev_put(dev);
  650. return -EADDRNOTAVAIL;
  651. }
  652. } else {
  653. dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
  654. }
  655. if (!dev)
  656. return -EADDRNOTAVAIL;
  657. err = dev_set_allmulti(dev, 1);
  658. if (err) {
  659. dev_put(dev);
  660. return err;
  661. }
  662. break;
  663. default:
  664. return -EINVAL;
  665. }
  666. in_dev = __in_dev_get_rtnl(dev);
  667. if (!in_dev) {
  668. dev_put(dev);
  669. return -EADDRNOTAVAIL;
  670. }
  671. IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
  672. inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
  673. &in_dev->cnf);
  674. ip_rt_multicast_event(in_dev);
  675. /* Fill in the VIF structures */
  676. v->rate_limit = vifc->vifc_rate_limit;
  677. v->local = vifc->vifc_lcl_addr.s_addr;
  678. v->remote = vifc->vifc_rmt_addr.s_addr;
  679. v->flags = vifc->vifc_flags;
  680. if (!mrtsock)
  681. v->flags |= VIFF_STATIC;
  682. v->threshold = vifc->vifc_threshold;
  683. v->bytes_in = 0;
  684. v->bytes_out = 0;
  685. v->pkt_in = 0;
  686. v->pkt_out = 0;
  687. v->link = dev->ifindex;
  688. if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
  689. v->link = dev_get_iflink(dev);
  690. /* And finish update writing critical data */
  691. write_lock_bh(&mrt_lock);
  692. v->dev = dev;
  693. if (v->flags & VIFF_REGISTER)
  694. mrt->mroute_reg_vif_num = vifi;
  695. if (vifi+1 > mrt->maxvif)
  696. mrt->maxvif = vifi+1;
  697. write_unlock_bh(&mrt_lock);
  698. return 0;
  699. }
  700. /* called with rcu_read_lock() */
  701. static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
  702. __be32 origin,
  703. __be32 mcastgrp)
  704. {
  705. int line = MFC_HASH(mcastgrp, origin);
  706. struct mfc_cache *c;
  707. list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
  708. if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
  709. return c;
  710. }
  711. return NULL;
  712. }
  713. /* Look for a (*,*,oif) entry */
  714. static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
  715. int vifi)
  716. {
  717. int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
  718. struct mfc_cache *c;
  719. list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
  720. if (c->mfc_origin == htonl(INADDR_ANY) &&
  721. c->mfc_mcastgrp == htonl(INADDR_ANY) &&
  722. c->mfc_un.res.ttls[vifi] < 255)
  723. return c;
  724. return NULL;
  725. }
  726. /* Look for a (*,G) entry */
  727. static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
  728. __be32 mcastgrp, int vifi)
  729. {
  730. int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
  731. struct mfc_cache *c, *proxy;
  732. if (mcastgrp == htonl(INADDR_ANY))
  733. goto skip;
  734. list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
  735. if (c->mfc_origin == htonl(INADDR_ANY) &&
  736. c->mfc_mcastgrp == mcastgrp) {
  737. if (c->mfc_un.res.ttls[vifi] < 255)
  738. return c;
  739. /* It's ok if the vifi is part of the static tree */
  740. proxy = ipmr_cache_find_any_parent(mrt,
  741. c->mfc_parent);
  742. if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
  743. return c;
  744. }
  745. skip:
  746. return ipmr_cache_find_any_parent(mrt, vifi);
  747. }
  748. /* Allocate a multicast cache entry */
  749. static struct mfc_cache *ipmr_cache_alloc(void)
  750. {
  751. struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
  752. if (c)
  753. c->mfc_un.res.minvif = MAXVIFS;
  754. return c;
  755. }
  756. static struct mfc_cache *ipmr_cache_alloc_unres(void)
  757. {
  758. struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
  759. if (c) {
  760. skb_queue_head_init(&c->mfc_un.unres.unresolved);
  761. c->mfc_un.unres.expires = jiffies + 10*HZ;
  762. }
  763. return c;
  764. }
  765. /* A cache entry has gone into a resolved state from queued */
  766. static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
  767. struct mfc_cache *uc, struct mfc_cache *c)
  768. {
  769. struct sk_buff *skb;
  770. struct nlmsgerr *e;
  771. /* Play the pending entries through our router */
  772. while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
  773. if (ip_hdr(skb)->version == 0) {
  774. struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
  775. if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
  776. nlh->nlmsg_len = skb_tail_pointer(skb) -
  777. (u8 *)nlh;
  778. } else {
  779. nlh->nlmsg_type = NLMSG_ERROR;
  780. nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
  781. skb_trim(skb, nlh->nlmsg_len);
  782. e = nlmsg_data(nlh);
  783. e->error = -EMSGSIZE;
  784. memset(&e->msg, 0, sizeof(e->msg));
  785. }
  786. rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
  787. } else {
  788. ip_mr_forward(net, mrt, skb, c, 0);
  789. }
  790. }
  791. }
  792. /* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
  793. * expects the following bizarre scheme.
  794. *
  795. * Called under mrt_lock.
  796. */
  797. static int ipmr_cache_report(struct mr_table *mrt,
  798. struct sk_buff *pkt, vifi_t vifi, int assert)
  799. {
  800. const int ihl = ip_hdrlen(pkt);
  801. struct sock *mroute_sk;
  802. struct igmphdr *igmp;
  803. struct igmpmsg *msg;
  804. struct sk_buff *skb;
  805. int ret;
  806. if (assert == IGMPMSG_WHOLEPKT)
  807. skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
  808. else
  809. skb = alloc_skb(128, GFP_ATOMIC);
  810. if (!skb)
  811. return -ENOBUFS;
  812. if (assert == IGMPMSG_WHOLEPKT) {
  813. /* Ugly, but we have no choice with this interface.
  814. * Duplicate old header, fix ihl, length etc.
  815. * And all this only to mangle msg->im_msgtype and
  816. * to set msg->im_mbz to "mbz" :-)
  817. */
  818. skb_push(skb, sizeof(struct iphdr));
  819. skb_reset_network_header(skb);
  820. skb_reset_transport_header(skb);
  821. msg = (struct igmpmsg *)skb_network_header(skb);
  822. memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
  823. msg->im_msgtype = IGMPMSG_WHOLEPKT;
  824. msg->im_mbz = 0;
  825. msg->im_vif = mrt->mroute_reg_vif_num;
  826. ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
  827. ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
  828. sizeof(struct iphdr));
  829. } else {
  830. /* Copy the IP header */
  831. skb_set_network_header(skb, skb->len);
  832. skb_put(skb, ihl);
  833. skb_copy_to_linear_data(skb, pkt->data, ihl);
  834. /* Flag to the kernel this is a route add */
  835. ip_hdr(skb)->protocol = 0;
  836. msg = (struct igmpmsg *)skb_network_header(skb);
  837. msg->im_vif = vifi;
  838. skb_dst_set(skb, dst_clone(skb_dst(pkt)));
  839. /* Add our header */
  840. igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
  841. igmp->type = assert;
  842. msg->im_msgtype = assert;
  843. igmp->code = 0;
  844. ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
  845. skb->transport_header = skb->network_header;
  846. }
  847. rcu_read_lock();
  848. mroute_sk = rcu_dereference(mrt->mroute_sk);
  849. if (!mroute_sk) {
  850. rcu_read_unlock();
  851. kfree_skb(skb);
  852. return -EINVAL;
  853. }
  854. /* Deliver to mrouted */
  855. ret = sock_queue_rcv_skb(mroute_sk, skb);
  856. rcu_read_unlock();
  857. if (ret < 0) {
  858. net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
  859. kfree_skb(skb);
  860. }
  861. return ret;
  862. }
  863. /* Queue a packet for resolution. It gets locked cache entry! */
  864. static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
  865. struct sk_buff *skb)
  866. {
  867. bool found = false;
  868. int err;
  869. struct mfc_cache *c;
  870. const struct iphdr *iph = ip_hdr(skb);
  871. spin_lock_bh(&mfc_unres_lock);
  872. list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
  873. if (c->mfc_mcastgrp == iph->daddr &&
  874. c->mfc_origin == iph->saddr) {
  875. found = true;
  876. break;
  877. }
  878. }
  879. if (!found) {
  880. /* Create a new entry if allowable */
  881. if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
  882. (c = ipmr_cache_alloc_unres()) == NULL) {
  883. spin_unlock_bh(&mfc_unres_lock);
  884. kfree_skb(skb);
  885. return -ENOBUFS;
  886. }
  887. /* Fill in the new cache entry */
  888. c->mfc_parent = -1;
  889. c->mfc_origin = iph->saddr;
  890. c->mfc_mcastgrp = iph->daddr;
  891. /* Reflect first query at mrouted. */
  892. err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
  893. if (err < 0) {
  894. /* If the report failed throw the cache entry
  895. out - Brad Parker
  896. */
  897. spin_unlock_bh(&mfc_unres_lock);
  898. ipmr_cache_free(c);
  899. kfree_skb(skb);
  900. return err;
  901. }
  902. atomic_inc(&mrt->cache_resolve_queue_len);
  903. list_add(&c->list, &mrt->mfc_unres_queue);
  904. mroute_netlink_event(mrt, c, RTM_NEWROUTE);
  905. if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
  906. mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
  907. }
  908. /* See if we can append the packet */
  909. if (c->mfc_un.unres.unresolved.qlen > 3) {
  910. kfree_skb(skb);
  911. err = -ENOBUFS;
  912. } else {
  913. skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
  914. err = 0;
  915. }
  916. spin_unlock_bh(&mfc_unres_lock);
  917. return err;
  918. }
  919. /* MFC cache manipulation by user space mroute daemon */
  920. static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
  921. {
  922. int line;
  923. struct mfc_cache *c, *next;
  924. line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
  925. list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
  926. if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
  927. c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
  928. (parent == -1 || parent == c->mfc_parent)) {
  929. list_del_rcu(&c->list);
  930. mroute_netlink_event(mrt, c, RTM_DELROUTE);
  931. ipmr_cache_free(c);
  932. return 0;
  933. }
  934. }
  935. return -ENOENT;
  936. }
  937. static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
  938. struct mfcctl *mfc, int mrtsock, int parent)
  939. {
  940. bool found = false;
  941. int line;
  942. struct mfc_cache *uc, *c;
  943. if (mfc->mfcc_parent >= MAXVIFS)
  944. return -ENFILE;
  945. line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
  946. list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
  947. if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
  948. c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
  949. (parent == -1 || parent == c->mfc_parent)) {
  950. found = true;
  951. break;
  952. }
  953. }
  954. if (found) {
  955. write_lock_bh(&mrt_lock);
  956. c->mfc_parent = mfc->mfcc_parent;
  957. ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
  958. if (!mrtsock)
  959. c->mfc_flags |= MFC_STATIC;
  960. write_unlock_bh(&mrt_lock);
  961. mroute_netlink_event(mrt, c, RTM_NEWROUTE);
  962. return 0;
  963. }
  964. if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
  965. !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
  966. return -EINVAL;
  967. c = ipmr_cache_alloc();
  968. if (!c)
  969. return -ENOMEM;
  970. c->mfc_origin = mfc->mfcc_origin.s_addr;
  971. c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
  972. c->mfc_parent = mfc->mfcc_parent;
  973. ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
  974. if (!mrtsock)
  975. c->mfc_flags |= MFC_STATIC;
  976. list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
  977. /* Check to see if we resolved a queued list. If so we
  978. * need to send on the frames and tidy up.
  979. */
  980. found = false;
  981. spin_lock_bh(&mfc_unres_lock);
  982. list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
  983. if (uc->mfc_origin == c->mfc_origin &&
  984. uc->mfc_mcastgrp == c->mfc_mcastgrp) {
  985. list_del(&uc->list);
  986. atomic_dec(&mrt->cache_resolve_queue_len);
  987. found = true;
  988. break;
  989. }
  990. }
  991. if (list_empty(&mrt->mfc_unres_queue))
  992. del_timer(&mrt->ipmr_expire_timer);
  993. spin_unlock_bh(&mfc_unres_lock);
  994. if (found) {
  995. ipmr_cache_resolve(net, mrt, uc, c);
  996. ipmr_cache_free(uc);
  997. }
  998. mroute_netlink_event(mrt, c, RTM_NEWROUTE);
  999. return 0;
  1000. }
  1001. /* Close the multicast socket, and clear the vif tables etc */
  1002. static void mroute_clean_tables(struct mr_table *mrt, bool all)
  1003. {
  1004. int i;
  1005. LIST_HEAD(list);
  1006. struct mfc_cache *c, *next;
  1007. /* Shut down all active vif entries */
  1008. for (i = 0; i < mrt->maxvif; i++) {
  1009. if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
  1010. continue;
  1011. vif_delete(mrt, i, 0, &list);
  1012. }
  1013. unregister_netdevice_many(&list);
  1014. /* Wipe the cache */
  1015. for (i = 0; i < MFC_LINES; i++) {
  1016. list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
  1017. if (!all && (c->mfc_flags & MFC_STATIC))
  1018. continue;
  1019. list_del_rcu(&c->list);
  1020. mroute_netlink_event(mrt, c, RTM_DELROUTE);
  1021. ipmr_cache_free(c);
  1022. }
  1023. }
  1024. if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
  1025. spin_lock_bh(&mfc_unres_lock);
  1026. list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
  1027. list_del(&c->list);
  1028. mroute_netlink_event(mrt, c, RTM_DELROUTE);
  1029. ipmr_destroy_unres(mrt, c);
  1030. }
  1031. spin_unlock_bh(&mfc_unres_lock);
  1032. }
  1033. }
  1034. /* called from ip_ra_control(), before an RCU grace period,
  1035. * we dont need to call synchronize_rcu() here
  1036. */
  1037. static void mrtsock_destruct(struct sock *sk)
  1038. {
  1039. struct net *net = sock_net(sk);
  1040. struct mr_table *mrt;
  1041. rtnl_lock();
  1042. ipmr_for_each_table(mrt, net) {
  1043. if (sk == rtnl_dereference(mrt->mroute_sk)) {
  1044. IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
  1045. inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
  1046. NETCONFA_IFINDEX_ALL,
  1047. net->ipv4.devconf_all);
  1048. RCU_INIT_POINTER(mrt->mroute_sk, NULL);
  1049. mroute_clean_tables(mrt, false);
  1050. }
  1051. }
  1052. rtnl_unlock();
  1053. }
  1054. /* Socket options and virtual interface manipulation. The whole
  1055. * virtual interface system is a complete heap, but unfortunately
  1056. * that's how BSD mrouted happens to think. Maybe one day with a proper
  1057. * MOSPF/PIM router set up we can clean this up.
  1058. */
  1059. int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
  1060. unsigned int optlen)
  1061. {
  1062. struct net *net = sock_net(sk);
  1063. int val, ret = 0, parent = 0;
  1064. struct mr_table *mrt;
  1065. struct vifctl vif;
  1066. struct mfcctl mfc;
  1067. u32 uval;
  1068. /* There's one exception to the lock - MRT_DONE which needs to unlock */
  1069. rtnl_lock();
  1070. if (sk->sk_type != SOCK_RAW ||
  1071. inet_sk(sk)->inet_num != IPPROTO_IGMP) {
  1072. ret = -EOPNOTSUPP;
  1073. goto out_unlock;
  1074. }
  1075. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1076. if (!mrt) {
  1077. ret = -ENOENT;
  1078. goto out_unlock;
  1079. }
  1080. if (optname != MRT_INIT) {
  1081. if (sk != rcu_access_pointer(mrt->mroute_sk) &&
  1082. !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
  1083. ret = -EACCES;
  1084. goto out_unlock;
  1085. }
  1086. }
  1087. switch (optname) {
  1088. case MRT_INIT:
  1089. if (optlen != sizeof(int)) {
  1090. ret = -EINVAL;
  1091. break;
  1092. }
  1093. if (rtnl_dereference(mrt->mroute_sk)) {
  1094. ret = -EADDRINUSE;
  1095. break;
  1096. }
  1097. ret = ip_ra_control(sk, 1, mrtsock_destruct);
  1098. if (ret == 0) {
  1099. rcu_assign_pointer(mrt->mroute_sk, sk);
  1100. IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
  1101. inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
  1102. NETCONFA_IFINDEX_ALL,
  1103. net->ipv4.devconf_all);
  1104. }
  1105. break;
  1106. case MRT_DONE:
  1107. if (sk != rcu_access_pointer(mrt->mroute_sk)) {
  1108. ret = -EACCES;
  1109. } else {
  1110. /* We need to unlock here because mrtsock_destruct takes
  1111. * care of rtnl itself and we can't change that due to
  1112. * the IP_ROUTER_ALERT setsockopt which runs without it.
  1113. */
  1114. rtnl_unlock();
  1115. ret = ip_ra_control(sk, 0, NULL);
  1116. goto out;
  1117. }
  1118. break;
  1119. case MRT_ADD_VIF:
  1120. case MRT_DEL_VIF:
  1121. if (optlen != sizeof(vif)) {
  1122. ret = -EINVAL;
  1123. break;
  1124. }
  1125. if (copy_from_user(&vif, optval, sizeof(vif))) {
  1126. ret = -EFAULT;
  1127. break;
  1128. }
  1129. if (vif.vifc_vifi >= MAXVIFS) {
  1130. ret = -ENFILE;
  1131. break;
  1132. }
  1133. if (optname == MRT_ADD_VIF) {
  1134. ret = vif_add(net, mrt, &vif,
  1135. sk == rtnl_dereference(mrt->mroute_sk));
  1136. } else {
  1137. ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
  1138. }
  1139. break;
  1140. /* Manipulate the forwarding caches. These live
  1141. * in a sort of kernel/user symbiosis.
  1142. */
  1143. case MRT_ADD_MFC:
  1144. case MRT_DEL_MFC:
  1145. parent = -1;
  1146. case MRT_ADD_MFC_PROXY:
  1147. case MRT_DEL_MFC_PROXY:
  1148. if (optlen != sizeof(mfc)) {
  1149. ret = -EINVAL;
  1150. break;
  1151. }
  1152. if (copy_from_user(&mfc, optval, sizeof(mfc))) {
  1153. ret = -EFAULT;
  1154. break;
  1155. }
  1156. if (parent == 0)
  1157. parent = mfc.mfcc_parent;
  1158. if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
  1159. ret = ipmr_mfc_delete(mrt, &mfc, parent);
  1160. else
  1161. ret = ipmr_mfc_add(net, mrt, &mfc,
  1162. sk == rtnl_dereference(mrt->mroute_sk),
  1163. parent);
  1164. break;
  1165. /* Control PIM assert. */
  1166. case MRT_ASSERT:
  1167. if (optlen != sizeof(val)) {
  1168. ret = -EINVAL;
  1169. break;
  1170. }
  1171. if (get_user(val, (int __user *)optval)) {
  1172. ret = -EFAULT;
  1173. break;
  1174. }
  1175. mrt->mroute_do_assert = val;
  1176. break;
  1177. case MRT_PIM:
  1178. if (!ipmr_pimsm_enabled()) {
  1179. ret = -ENOPROTOOPT;
  1180. break;
  1181. }
  1182. if (optlen != sizeof(val)) {
  1183. ret = -EINVAL;
  1184. break;
  1185. }
  1186. if (get_user(val, (int __user *)optval)) {
  1187. ret = -EFAULT;
  1188. break;
  1189. }
  1190. val = !!val;
  1191. if (val != mrt->mroute_do_pim) {
  1192. mrt->mroute_do_pim = val;
  1193. mrt->mroute_do_assert = val;
  1194. }
  1195. break;
  1196. case MRT_TABLE:
  1197. if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
  1198. ret = -ENOPROTOOPT;
  1199. break;
  1200. }
  1201. if (optlen != sizeof(uval)) {
  1202. ret = -EINVAL;
  1203. break;
  1204. }
  1205. if (get_user(uval, (u32 __user *)optval)) {
  1206. ret = -EFAULT;
  1207. break;
  1208. }
  1209. if (sk == rtnl_dereference(mrt->mroute_sk)) {
  1210. ret = -EBUSY;
  1211. } else {
  1212. mrt = ipmr_new_table(net, uval);
  1213. if (IS_ERR(mrt))
  1214. ret = PTR_ERR(mrt);
  1215. else
  1216. raw_sk(sk)->ipmr_table = uval;
  1217. }
  1218. break;
  1219. /* Spurious command, or MRT_VERSION which you cannot set. */
  1220. default:
  1221. ret = -ENOPROTOOPT;
  1222. }
  1223. out_unlock:
  1224. rtnl_unlock();
  1225. out:
  1226. return ret;
  1227. }
  1228. /* Getsock opt support for the multicast routing system. */
  1229. int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
  1230. {
  1231. int olr;
  1232. int val;
  1233. struct net *net = sock_net(sk);
  1234. struct mr_table *mrt;
  1235. if (sk->sk_type != SOCK_RAW ||
  1236. inet_sk(sk)->inet_num != IPPROTO_IGMP)
  1237. return -EOPNOTSUPP;
  1238. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1239. if (!mrt)
  1240. return -ENOENT;
  1241. switch (optname) {
  1242. case MRT_VERSION:
  1243. val = 0x0305;
  1244. break;
  1245. case MRT_PIM:
  1246. if (!ipmr_pimsm_enabled())
  1247. return -ENOPROTOOPT;
  1248. val = mrt->mroute_do_pim;
  1249. break;
  1250. case MRT_ASSERT:
  1251. val = mrt->mroute_do_assert;
  1252. break;
  1253. default:
  1254. return -ENOPROTOOPT;
  1255. }
  1256. if (get_user(olr, optlen))
  1257. return -EFAULT;
  1258. olr = min_t(unsigned int, olr, sizeof(int));
  1259. if (olr < 0)
  1260. return -EINVAL;
  1261. if (put_user(olr, optlen))
  1262. return -EFAULT;
  1263. if (copy_to_user(optval, &val, olr))
  1264. return -EFAULT;
  1265. return 0;
  1266. }
  1267. /* The IP multicast ioctl support routines. */
  1268. int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
  1269. {
  1270. struct sioc_sg_req sr;
  1271. struct sioc_vif_req vr;
  1272. struct vif_device *vif;
  1273. struct mfc_cache *c;
  1274. struct net *net = sock_net(sk);
  1275. struct mr_table *mrt;
  1276. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1277. if (!mrt)
  1278. return -ENOENT;
  1279. switch (cmd) {
  1280. case SIOCGETVIFCNT:
  1281. if (copy_from_user(&vr, arg, sizeof(vr)))
  1282. return -EFAULT;
  1283. if (vr.vifi >= mrt->maxvif)
  1284. return -EINVAL;
  1285. read_lock(&mrt_lock);
  1286. vif = &mrt->vif_table[vr.vifi];
  1287. if (VIF_EXISTS(mrt, vr.vifi)) {
  1288. vr.icount = vif->pkt_in;
  1289. vr.ocount = vif->pkt_out;
  1290. vr.ibytes = vif->bytes_in;
  1291. vr.obytes = vif->bytes_out;
  1292. read_unlock(&mrt_lock);
  1293. if (copy_to_user(arg, &vr, sizeof(vr)))
  1294. return -EFAULT;
  1295. return 0;
  1296. }
  1297. read_unlock(&mrt_lock);
  1298. return -EADDRNOTAVAIL;
  1299. case SIOCGETSGCNT:
  1300. if (copy_from_user(&sr, arg, sizeof(sr)))
  1301. return -EFAULT;
  1302. rcu_read_lock();
  1303. c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
  1304. if (c) {
  1305. sr.pktcnt = c->mfc_un.res.pkt;
  1306. sr.bytecnt = c->mfc_un.res.bytes;
  1307. sr.wrong_if = c->mfc_un.res.wrong_if;
  1308. rcu_read_unlock();
  1309. if (copy_to_user(arg, &sr, sizeof(sr)))
  1310. return -EFAULT;
  1311. return 0;
  1312. }
  1313. rcu_read_unlock();
  1314. return -EADDRNOTAVAIL;
  1315. default:
  1316. return -ENOIOCTLCMD;
  1317. }
  1318. }
  1319. #ifdef CONFIG_COMPAT
  1320. struct compat_sioc_sg_req {
  1321. struct in_addr src;
  1322. struct in_addr grp;
  1323. compat_ulong_t pktcnt;
  1324. compat_ulong_t bytecnt;
  1325. compat_ulong_t wrong_if;
  1326. };
  1327. struct compat_sioc_vif_req {
  1328. vifi_t vifi; /* Which iface */
  1329. compat_ulong_t icount;
  1330. compat_ulong_t ocount;
  1331. compat_ulong_t ibytes;
  1332. compat_ulong_t obytes;
  1333. };
  1334. int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
  1335. {
  1336. struct compat_sioc_sg_req sr;
  1337. struct compat_sioc_vif_req vr;
  1338. struct vif_device *vif;
  1339. struct mfc_cache *c;
  1340. struct net *net = sock_net(sk);
  1341. struct mr_table *mrt;
  1342. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1343. if (!mrt)
  1344. return -ENOENT;
  1345. switch (cmd) {
  1346. case SIOCGETVIFCNT:
  1347. if (copy_from_user(&vr, arg, sizeof(vr)))
  1348. return -EFAULT;
  1349. if (vr.vifi >= mrt->maxvif)
  1350. return -EINVAL;
  1351. read_lock(&mrt_lock);
  1352. vif = &mrt->vif_table[vr.vifi];
  1353. if (VIF_EXISTS(mrt, vr.vifi)) {
  1354. vr.icount = vif->pkt_in;
  1355. vr.ocount = vif->pkt_out;
  1356. vr.ibytes = vif->bytes_in;
  1357. vr.obytes = vif->bytes_out;
  1358. read_unlock(&mrt_lock);
  1359. if (copy_to_user(arg, &vr, sizeof(vr)))
  1360. return -EFAULT;
  1361. return 0;
  1362. }
  1363. read_unlock(&mrt_lock);
  1364. return -EADDRNOTAVAIL;
  1365. case SIOCGETSGCNT:
  1366. if (copy_from_user(&sr, arg, sizeof(sr)))
  1367. return -EFAULT;
  1368. rcu_read_lock();
  1369. c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
  1370. if (c) {
  1371. sr.pktcnt = c->mfc_un.res.pkt;
  1372. sr.bytecnt = c->mfc_un.res.bytes;
  1373. sr.wrong_if = c->mfc_un.res.wrong_if;
  1374. rcu_read_unlock();
  1375. if (copy_to_user(arg, &sr, sizeof(sr)))
  1376. return -EFAULT;
  1377. return 0;
  1378. }
  1379. rcu_read_unlock();
  1380. return -EADDRNOTAVAIL;
  1381. default:
  1382. return -ENOIOCTLCMD;
  1383. }
  1384. }
  1385. #endif
  1386. static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
  1387. {
  1388. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1389. struct net *net = dev_net(dev);
  1390. struct mr_table *mrt;
  1391. struct vif_device *v;
  1392. int ct;
  1393. if (event != NETDEV_UNREGISTER)
  1394. return NOTIFY_DONE;
  1395. ipmr_for_each_table(mrt, net) {
  1396. v = &mrt->vif_table[0];
  1397. for (ct = 0; ct < mrt->maxvif; ct++, v++) {
  1398. if (v->dev == dev)
  1399. vif_delete(mrt, ct, 1, NULL);
  1400. }
  1401. }
  1402. return NOTIFY_DONE;
  1403. }
  1404. static struct notifier_block ip_mr_notifier = {
  1405. .notifier_call = ipmr_device_event,
  1406. };
  1407. /* Encapsulate a packet by attaching a valid IPIP header to it.
  1408. * This avoids tunnel drivers and other mess and gives us the speed so
  1409. * important for multicast video.
  1410. */
  1411. static void ip_encap(struct net *net, struct sk_buff *skb,
  1412. __be32 saddr, __be32 daddr)
  1413. {
  1414. struct iphdr *iph;
  1415. const struct iphdr *old_iph = ip_hdr(skb);
  1416. skb_push(skb, sizeof(struct iphdr));
  1417. skb->transport_header = skb->network_header;
  1418. skb_reset_network_header(skb);
  1419. iph = ip_hdr(skb);
  1420. iph->version = 4;
  1421. iph->tos = old_iph->tos;
  1422. iph->ttl = old_iph->ttl;
  1423. iph->frag_off = 0;
  1424. iph->daddr = daddr;
  1425. iph->saddr = saddr;
  1426. iph->protocol = IPPROTO_IPIP;
  1427. iph->ihl = 5;
  1428. iph->tot_len = htons(skb->len);
  1429. ip_select_ident(net, skb, NULL);
  1430. ip_send_check(iph);
  1431. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1432. nf_reset(skb);
  1433. }
  1434. static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
  1435. struct sk_buff *skb)
  1436. {
  1437. struct ip_options *opt = &(IPCB(skb)->opt);
  1438. IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
  1439. IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
  1440. if (unlikely(opt->optlen))
  1441. ip_forward_options(skb);
  1442. return dst_output(net, sk, skb);
  1443. }
  1444. /* Processing handlers for ipmr_forward */
  1445. static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
  1446. struct sk_buff *skb, struct mfc_cache *c, int vifi)
  1447. {
  1448. const struct iphdr *iph = ip_hdr(skb);
  1449. struct vif_device *vif = &mrt->vif_table[vifi];
  1450. struct net_device *dev;
  1451. struct rtable *rt;
  1452. struct flowi4 fl4;
  1453. int encap = 0;
  1454. if (!vif->dev)
  1455. goto out_free;
  1456. if (vif->flags & VIFF_REGISTER) {
  1457. vif->pkt_out++;
  1458. vif->bytes_out += skb->len;
  1459. vif->dev->stats.tx_bytes += skb->len;
  1460. vif->dev->stats.tx_packets++;
  1461. ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
  1462. goto out_free;
  1463. }
  1464. if (vif->flags & VIFF_TUNNEL) {
  1465. rt = ip_route_output_ports(net, &fl4, NULL,
  1466. vif->remote, vif->local,
  1467. 0, 0,
  1468. IPPROTO_IPIP,
  1469. RT_TOS(iph->tos), vif->link);
  1470. if (IS_ERR(rt))
  1471. goto out_free;
  1472. encap = sizeof(struct iphdr);
  1473. } else {
  1474. rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
  1475. 0, 0,
  1476. IPPROTO_IPIP,
  1477. RT_TOS(iph->tos), vif->link);
  1478. if (IS_ERR(rt))
  1479. goto out_free;
  1480. }
  1481. dev = rt->dst.dev;
  1482. if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
  1483. /* Do not fragment multicasts. Alas, IPv4 does not
  1484. * allow to send ICMP, so that packets will disappear
  1485. * to blackhole.
  1486. */
  1487. IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
  1488. ip_rt_put(rt);
  1489. goto out_free;
  1490. }
  1491. encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
  1492. if (skb_cow(skb, encap)) {
  1493. ip_rt_put(rt);
  1494. goto out_free;
  1495. }
  1496. vif->pkt_out++;
  1497. vif->bytes_out += skb->len;
  1498. skb_dst_drop(skb);
  1499. skb_dst_set(skb, &rt->dst);
  1500. ip_decrease_ttl(ip_hdr(skb));
  1501. /* FIXME: forward and output firewalls used to be called here.
  1502. * What do we do with netfilter? -- RR
  1503. */
  1504. if (vif->flags & VIFF_TUNNEL) {
  1505. ip_encap(net, skb, vif->local, vif->remote);
  1506. /* FIXME: extra output firewall step used to be here. --RR */
  1507. vif->dev->stats.tx_packets++;
  1508. vif->dev->stats.tx_bytes += skb->len;
  1509. }
  1510. IPCB(skb)->flags |= IPSKB_FORWARDED;
  1511. /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
  1512. * not only before forwarding, but after forwarding on all output
  1513. * interfaces. It is clear, if mrouter runs a multicasting
  1514. * program, it should receive packets not depending to what interface
  1515. * program is joined.
  1516. * If we will not make it, the program will have to join on all
  1517. * interfaces. On the other hand, multihoming host (or router, but
  1518. * not mrouter) cannot join to more than one interface - it will
  1519. * result in receiving multiple packets.
  1520. */
  1521. NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
  1522. net, NULL, skb, skb->dev, dev,
  1523. ipmr_forward_finish);
  1524. return;
  1525. out_free:
  1526. kfree_skb(skb);
  1527. }
  1528. static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
  1529. {
  1530. int ct;
  1531. for (ct = mrt->maxvif-1; ct >= 0; ct--) {
  1532. if (mrt->vif_table[ct].dev == dev)
  1533. break;
  1534. }
  1535. return ct;
  1536. }
  1537. /* "local" means that we should preserve one skb (for local delivery) */
  1538. static void ip_mr_forward(struct net *net, struct mr_table *mrt,
  1539. struct sk_buff *skb, struct mfc_cache *cache,
  1540. int local)
  1541. {
  1542. int psend = -1;
  1543. int vif, ct;
  1544. int true_vifi = ipmr_find_vif(mrt, skb->dev);
  1545. vif = cache->mfc_parent;
  1546. cache->mfc_un.res.pkt++;
  1547. cache->mfc_un.res.bytes += skb->len;
  1548. if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
  1549. struct mfc_cache *cache_proxy;
  1550. /* For an (*,G) entry, we only check that the incomming
  1551. * interface is part of the static tree.
  1552. */
  1553. cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
  1554. if (cache_proxy &&
  1555. cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
  1556. goto forward;
  1557. }
  1558. /* Wrong interface: drop packet and (maybe) send PIM assert. */
  1559. if (mrt->vif_table[vif].dev != skb->dev) {
  1560. if (rt_is_output_route(skb_rtable(skb))) {
  1561. /* It is our own packet, looped back.
  1562. * Very complicated situation...
  1563. *
  1564. * The best workaround until routing daemons will be
  1565. * fixed is not to redistribute packet, if it was
  1566. * send through wrong interface. It means, that
  1567. * multicast applications WILL NOT work for
  1568. * (S,G), which have default multicast route pointing
  1569. * to wrong oif. In any case, it is not a good
  1570. * idea to use multicasting applications on router.
  1571. */
  1572. goto dont_forward;
  1573. }
  1574. cache->mfc_un.res.wrong_if++;
  1575. if (true_vifi >= 0 && mrt->mroute_do_assert &&
  1576. /* pimsm uses asserts, when switching from RPT to SPT,
  1577. * so that we cannot check that packet arrived on an oif.
  1578. * It is bad, but otherwise we would need to move pretty
  1579. * large chunk of pimd to kernel. Ough... --ANK
  1580. */
  1581. (mrt->mroute_do_pim ||
  1582. cache->mfc_un.res.ttls[true_vifi] < 255) &&
  1583. time_after(jiffies,
  1584. cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
  1585. cache->mfc_un.res.last_assert = jiffies;
  1586. ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
  1587. }
  1588. goto dont_forward;
  1589. }
  1590. forward:
  1591. mrt->vif_table[vif].pkt_in++;
  1592. mrt->vif_table[vif].bytes_in += skb->len;
  1593. /* Forward the frame */
  1594. if (cache->mfc_origin == htonl(INADDR_ANY) &&
  1595. cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
  1596. if (true_vifi >= 0 &&
  1597. true_vifi != cache->mfc_parent &&
  1598. ip_hdr(skb)->ttl >
  1599. cache->mfc_un.res.ttls[cache->mfc_parent]) {
  1600. /* It's an (*,*) entry and the packet is not coming from
  1601. * the upstream: forward the packet to the upstream
  1602. * only.
  1603. */
  1604. psend = cache->mfc_parent;
  1605. goto last_forward;
  1606. }
  1607. goto dont_forward;
  1608. }
  1609. for (ct = cache->mfc_un.res.maxvif - 1;
  1610. ct >= cache->mfc_un.res.minvif; ct--) {
  1611. /* For (*,G) entry, don't forward to the incoming interface */
  1612. if ((cache->mfc_origin != htonl(INADDR_ANY) ||
  1613. ct != true_vifi) &&
  1614. ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
  1615. if (psend != -1) {
  1616. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1617. if (skb2)
  1618. ipmr_queue_xmit(net, mrt, skb2, cache,
  1619. psend);
  1620. }
  1621. psend = ct;
  1622. }
  1623. }
  1624. last_forward:
  1625. if (psend != -1) {
  1626. if (local) {
  1627. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1628. if (skb2)
  1629. ipmr_queue_xmit(net, mrt, skb2, cache, psend);
  1630. } else {
  1631. ipmr_queue_xmit(net, mrt, skb, cache, psend);
  1632. return;
  1633. }
  1634. }
  1635. dont_forward:
  1636. if (!local)
  1637. kfree_skb(skb);
  1638. }
  1639. static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
  1640. {
  1641. struct rtable *rt = skb_rtable(skb);
  1642. struct iphdr *iph = ip_hdr(skb);
  1643. struct flowi4 fl4 = {
  1644. .daddr = iph->daddr,
  1645. .saddr = iph->saddr,
  1646. .flowi4_tos = RT_TOS(iph->tos),
  1647. .flowi4_oif = (rt_is_output_route(rt) ?
  1648. skb->dev->ifindex : 0),
  1649. .flowi4_iif = (rt_is_output_route(rt) ?
  1650. LOOPBACK_IFINDEX :
  1651. skb->dev->ifindex),
  1652. .flowi4_mark = skb->mark,
  1653. };
  1654. struct mr_table *mrt;
  1655. int err;
  1656. err = ipmr_fib_lookup(net, &fl4, &mrt);
  1657. if (err)
  1658. return ERR_PTR(err);
  1659. return mrt;
  1660. }
  1661. /* Multicast packets for forwarding arrive here
  1662. * Called with rcu_read_lock();
  1663. */
  1664. int ip_mr_input(struct sk_buff *skb)
  1665. {
  1666. struct mfc_cache *cache;
  1667. struct net *net = dev_net(skb->dev);
  1668. int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
  1669. struct mr_table *mrt;
  1670. /* Packet is looped back after forward, it should not be
  1671. * forwarded second time, but still can be delivered locally.
  1672. */
  1673. if (IPCB(skb)->flags & IPSKB_FORWARDED)
  1674. goto dont_forward;
  1675. mrt = ipmr_rt_fib_lookup(net, skb);
  1676. if (IS_ERR(mrt)) {
  1677. kfree_skb(skb);
  1678. return PTR_ERR(mrt);
  1679. }
  1680. if (!local) {
  1681. if (IPCB(skb)->opt.router_alert) {
  1682. if (ip_call_ra_chain(skb))
  1683. return 0;
  1684. } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
  1685. /* IGMPv1 (and broken IGMPv2 implementations sort of
  1686. * Cisco IOS <= 11.2(8)) do not put router alert
  1687. * option to IGMP packets destined to routable
  1688. * groups. It is very bad, because it means
  1689. * that we can forward NO IGMP messages.
  1690. */
  1691. struct sock *mroute_sk;
  1692. mroute_sk = rcu_dereference(mrt->mroute_sk);
  1693. if (mroute_sk) {
  1694. nf_reset(skb);
  1695. raw_rcv(mroute_sk, skb);
  1696. return 0;
  1697. }
  1698. }
  1699. }
  1700. /* already under rcu_read_lock() */
  1701. cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
  1702. if (!cache) {
  1703. int vif = ipmr_find_vif(mrt, skb->dev);
  1704. if (vif >= 0)
  1705. cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
  1706. vif);
  1707. }
  1708. /* No usable cache entry */
  1709. if (!cache) {
  1710. int vif;
  1711. if (local) {
  1712. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1713. ip_local_deliver(skb);
  1714. if (!skb2)
  1715. return -ENOBUFS;
  1716. skb = skb2;
  1717. }
  1718. read_lock(&mrt_lock);
  1719. vif = ipmr_find_vif(mrt, skb->dev);
  1720. if (vif >= 0) {
  1721. int err2 = ipmr_cache_unresolved(mrt, vif, skb);
  1722. read_unlock(&mrt_lock);
  1723. return err2;
  1724. }
  1725. read_unlock(&mrt_lock);
  1726. kfree_skb(skb);
  1727. return -ENODEV;
  1728. }
  1729. read_lock(&mrt_lock);
  1730. ip_mr_forward(net, mrt, skb, cache, local);
  1731. read_unlock(&mrt_lock);
  1732. if (local)
  1733. return ip_local_deliver(skb);
  1734. return 0;
  1735. dont_forward:
  1736. if (local)
  1737. return ip_local_deliver(skb);
  1738. kfree_skb(skb);
  1739. return 0;
  1740. }
  1741. #ifdef CONFIG_IP_PIMSM_V1
  1742. /* Handle IGMP messages of PIMv1 */
  1743. int pim_rcv_v1(struct sk_buff *skb)
  1744. {
  1745. struct igmphdr *pim;
  1746. struct net *net = dev_net(skb->dev);
  1747. struct mr_table *mrt;
  1748. if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
  1749. goto drop;
  1750. pim = igmp_hdr(skb);
  1751. mrt = ipmr_rt_fib_lookup(net, skb);
  1752. if (IS_ERR(mrt))
  1753. goto drop;
  1754. if (!mrt->mroute_do_pim ||
  1755. pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
  1756. goto drop;
  1757. if (__pim_rcv(mrt, skb, sizeof(*pim))) {
  1758. drop:
  1759. kfree_skb(skb);
  1760. }
  1761. return 0;
  1762. }
  1763. #endif
  1764. #ifdef CONFIG_IP_PIMSM_V2
  1765. static int pim_rcv(struct sk_buff *skb)
  1766. {
  1767. struct pimreghdr *pim;
  1768. struct net *net = dev_net(skb->dev);
  1769. struct mr_table *mrt;
  1770. if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
  1771. goto drop;
  1772. pim = (struct pimreghdr *)skb_transport_header(skb);
  1773. if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
  1774. (pim->flags & PIM_NULL_REGISTER) ||
  1775. (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
  1776. csum_fold(skb_checksum(skb, 0, skb->len, 0))))
  1777. goto drop;
  1778. mrt = ipmr_rt_fib_lookup(net, skb);
  1779. if (IS_ERR(mrt))
  1780. goto drop;
  1781. if (__pim_rcv(mrt, skb, sizeof(*pim))) {
  1782. drop:
  1783. kfree_skb(skb);
  1784. }
  1785. return 0;
  1786. }
  1787. #endif
  1788. static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  1789. struct mfc_cache *c, struct rtmsg *rtm)
  1790. {
  1791. int ct;
  1792. struct rtnexthop *nhp;
  1793. struct nlattr *mp_attr;
  1794. struct rta_mfc_stats mfcs;
  1795. /* If cache is unresolved, don't try to parse IIF and OIF */
  1796. if (c->mfc_parent >= MAXVIFS)
  1797. return -ENOENT;
  1798. if (VIF_EXISTS(mrt, c->mfc_parent) &&
  1799. nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
  1800. return -EMSGSIZE;
  1801. if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
  1802. return -EMSGSIZE;
  1803. for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
  1804. if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
  1805. if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
  1806. nla_nest_cancel(skb, mp_attr);
  1807. return -EMSGSIZE;
  1808. }
  1809. nhp->rtnh_flags = 0;
  1810. nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
  1811. nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
  1812. nhp->rtnh_len = sizeof(*nhp);
  1813. }
  1814. }
  1815. nla_nest_end(skb, mp_attr);
  1816. mfcs.mfcs_packets = c->mfc_un.res.pkt;
  1817. mfcs.mfcs_bytes = c->mfc_un.res.bytes;
  1818. mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
  1819. if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
  1820. return -EMSGSIZE;
  1821. rtm->rtm_type = RTN_MULTICAST;
  1822. return 1;
  1823. }
  1824. int ipmr_get_route(struct net *net, struct sk_buff *skb,
  1825. __be32 saddr, __be32 daddr,
  1826. struct rtmsg *rtm, int nowait)
  1827. {
  1828. struct mfc_cache *cache;
  1829. struct mr_table *mrt;
  1830. int err;
  1831. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  1832. if (!mrt)
  1833. return -ENOENT;
  1834. rcu_read_lock();
  1835. cache = ipmr_cache_find(mrt, saddr, daddr);
  1836. if (!cache && skb->dev) {
  1837. int vif = ipmr_find_vif(mrt, skb->dev);
  1838. if (vif >= 0)
  1839. cache = ipmr_cache_find_any(mrt, daddr, vif);
  1840. }
  1841. if (!cache) {
  1842. struct sk_buff *skb2;
  1843. struct iphdr *iph;
  1844. struct net_device *dev;
  1845. int vif = -1;
  1846. if (nowait) {
  1847. rcu_read_unlock();
  1848. return -EAGAIN;
  1849. }
  1850. dev = skb->dev;
  1851. read_lock(&mrt_lock);
  1852. if (dev)
  1853. vif = ipmr_find_vif(mrt, dev);
  1854. if (vif < 0) {
  1855. read_unlock(&mrt_lock);
  1856. rcu_read_unlock();
  1857. return -ENODEV;
  1858. }
  1859. skb2 = skb_clone(skb, GFP_ATOMIC);
  1860. if (!skb2) {
  1861. read_unlock(&mrt_lock);
  1862. rcu_read_unlock();
  1863. return -ENOMEM;
  1864. }
  1865. skb_push(skb2, sizeof(struct iphdr));
  1866. skb_reset_network_header(skb2);
  1867. iph = ip_hdr(skb2);
  1868. iph->ihl = sizeof(struct iphdr) >> 2;
  1869. iph->saddr = saddr;
  1870. iph->daddr = daddr;
  1871. iph->version = 0;
  1872. err = ipmr_cache_unresolved(mrt, vif, skb2);
  1873. read_unlock(&mrt_lock);
  1874. rcu_read_unlock();
  1875. return err;
  1876. }
  1877. read_lock(&mrt_lock);
  1878. err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
  1879. read_unlock(&mrt_lock);
  1880. rcu_read_unlock();
  1881. return err;
  1882. }
  1883. static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  1884. u32 portid, u32 seq, struct mfc_cache *c, int cmd,
  1885. int flags)
  1886. {
  1887. struct nlmsghdr *nlh;
  1888. struct rtmsg *rtm;
  1889. int err;
  1890. nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
  1891. if (!nlh)
  1892. return -EMSGSIZE;
  1893. rtm = nlmsg_data(nlh);
  1894. rtm->rtm_family = RTNL_FAMILY_IPMR;
  1895. rtm->rtm_dst_len = 32;
  1896. rtm->rtm_src_len = 32;
  1897. rtm->rtm_tos = 0;
  1898. rtm->rtm_table = mrt->id;
  1899. if (nla_put_u32(skb, RTA_TABLE, mrt->id))
  1900. goto nla_put_failure;
  1901. rtm->rtm_type = RTN_MULTICAST;
  1902. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1903. if (c->mfc_flags & MFC_STATIC)
  1904. rtm->rtm_protocol = RTPROT_STATIC;
  1905. else
  1906. rtm->rtm_protocol = RTPROT_MROUTED;
  1907. rtm->rtm_flags = 0;
  1908. if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
  1909. nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
  1910. goto nla_put_failure;
  1911. err = __ipmr_fill_mroute(mrt, skb, c, rtm);
  1912. /* do not break the dump if cache is unresolved */
  1913. if (err < 0 && err != -ENOENT)
  1914. goto nla_put_failure;
  1915. nlmsg_end(skb, nlh);
  1916. return 0;
  1917. nla_put_failure:
  1918. nlmsg_cancel(skb, nlh);
  1919. return -EMSGSIZE;
  1920. }
  1921. static size_t mroute_msgsize(bool unresolved, int maxvif)
  1922. {
  1923. size_t len =
  1924. NLMSG_ALIGN(sizeof(struct rtmsg))
  1925. + nla_total_size(4) /* RTA_TABLE */
  1926. + nla_total_size(4) /* RTA_SRC */
  1927. + nla_total_size(4) /* RTA_DST */
  1928. ;
  1929. if (!unresolved)
  1930. len = len
  1931. + nla_total_size(4) /* RTA_IIF */
  1932. + nla_total_size(0) /* RTA_MULTIPATH */
  1933. + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
  1934. /* RTA_MFC_STATS */
  1935. + nla_total_size(sizeof(struct rta_mfc_stats))
  1936. ;
  1937. return len;
  1938. }
  1939. static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
  1940. int cmd)
  1941. {
  1942. struct net *net = read_pnet(&mrt->net);
  1943. struct sk_buff *skb;
  1944. int err = -ENOBUFS;
  1945. skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
  1946. GFP_ATOMIC);
  1947. if (!skb)
  1948. goto errout;
  1949. err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
  1950. if (err < 0)
  1951. goto errout;
  1952. rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
  1953. return;
  1954. errout:
  1955. kfree_skb(skb);
  1956. if (err < 0)
  1957. rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
  1958. }
  1959. static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
  1960. {
  1961. struct net *net = sock_net(skb->sk);
  1962. struct mr_table *mrt;
  1963. struct mfc_cache *mfc;
  1964. unsigned int t = 0, s_t;
  1965. unsigned int h = 0, s_h;
  1966. unsigned int e = 0, s_e;
  1967. s_t = cb->args[0];
  1968. s_h = cb->args[1];
  1969. s_e = cb->args[2];
  1970. rcu_read_lock();
  1971. ipmr_for_each_table(mrt, net) {
  1972. if (t < s_t)
  1973. goto next_table;
  1974. if (t > s_t)
  1975. s_h = 0;
  1976. for (h = s_h; h < MFC_LINES; h++) {
  1977. list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
  1978. if (e < s_e)
  1979. goto next_entry;
  1980. if (ipmr_fill_mroute(mrt, skb,
  1981. NETLINK_CB(cb->skb).portid,
  1982. cb->nlh->nlmsg_seq,
  1983. mfc, RTM_NEWROUTE,
  1984. NLM_F_MULTI) < 0)
  1985. goto done;
  1986. next_entry:
  1987. e++;
  1988. }
  1989. e = s_e = 0;
  1990. }
  1991. spin_lock_bh(&mfc_unres_lock);
  1992. list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
  1993. if (e < s_e)
  1994. goto next_entry2;
  1995. if (ipmr_fill_mroute(mrt, skb,
  1996. NETLINK_CB(cb->skb).portid,
  1997. cb->nlh->nlmsg_seq,
  1998. mfc, RTM_NEWROUTE,
  1999. NLM_F_MULTI) < 0) {
  2000. spin_unlock_bh(&mfc_unres_lock);
  2001. goto done;
  2002. }
  2003. next_entry2:
  2004. e++;
  2005. }
  2006. spin_unlock_bh(&mfc_unres_lock);
  2007. e = s_e = 0;
  2008. s_h = 0;
  2009. next_table:
  2010. t++;
  2011. }
  2012. done:
  2013. rcu_read_unlock();
  2014. cb->args[2] = e;
  2015. cb->args[1] = h;
  2016. cb->args[0] = t;
  2017. return skb->len;
  2018. }
  2019. static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
  2020. [RTA_SRC] = { .type = NLA_U32 },
  2021. [RTA_DST] = { .type = NLA_U32 },
  2022. [RTA_IIF] = { .type = NLA_U32 },
  2023. [RTA_TABLE] = { .type = NLA_U32 },
  2024. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  2025. };
  2026. static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
  2027. {
  2028. switch (rtm_protocol) {
  2029. case RTPROT_STATIC:
  2030. case RTPROT_MROUTED:
  2031. return true;
  2032. }
  2033. return false;
  2034. }
  2035. static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
  2036. {
  2037. struct rtnexthop *rtnh = nla_data(nla);
  2038. int remaining = nla_len(nla), vifi = 0;
  2039. while (rtnh_ok(rtnh, remaining)) {
  2040. mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
  2041. if (++vifi == MAXVIFS)
  2042. break;
  2043. rtnh = rtnh_next(rtnh, &remaining);
  2044. }
  2045. return remaining > 0 ? -EINVAL : vifi;
  2046. }
  2047. /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
  2048. static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
  2049. struct mfcctl *mfcc, int *mrtsock,
  2050. struct mr_table **mrtret)
  2051. {
  2052. struct net_device *dev = NULL;
  2053. u32 tblid = RT_TABLE_DEFAULT;
  2054. struct mr_table *mrt;
  2055. struct nlattr *attr;
  2056. struct rtmsg *rtm;
  2057. int ret, rem;
  2058. ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy);
  2059. if (ret < 0)
  2060. goto out;
  2061. rtm = nlmsg_data(nlh);
  2062. ret = -EINVAL;
  2063. if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
  2064. rtm->rtm_type != RTN_MULTICAST ||
  2065. rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
  2066. !ipmr_rtm_validate_proto(rtm->rtm_protocol))
  2067. goto out;
  2068. memset(mfcc, 0, sizeof(*mfcc));
  2069. mfcc->mfcc_parent = -1;
  2070. ret = 0;
  2071. nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
  2072. switch (nla_type(attr)) {
  2073. case RTA_SRC:
  2074. mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
  2075. break;
  2076. case RTA_DST:
  2077. mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
  2078. break;
  2079. case RTA_IIF:
  2080. dev = __dev_get_by_index(net, nla_get_u32(attr));
  2081. if (!dev) {
  2082. ret = -ENODEV;
  2083. goto out;
  2084. }
  2085. break;
  2086. case RTA_MULTIPATH:
  2087. if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
  2088. ret = -EINVAL;
  2089. goto out;
  2090. }
  2091. break;
  2092. case RTA_PREFSRC:
  2093. ret = 1;
  2094. break;
  2095. case RTA_TABLE:
  2096. tblid = nla_get_u32(attr);
  2097. break;
  2098. }
  2099. }
  2100. mrt = ipmr_get_table(net, tblid);
  2101. if (!mrt) {
  2102. ret = -ENOENT;
  2103. goto out;
  2104. }
  2105. *mrtret = mrt;
  2106. *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
  2107. if (dev)
  2108. mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
  2109. out:
  2110. return ret;
  2111. }
  2112. /* takes care of both newroute and delroute */
  2113. static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh)
  2114. {
  2115. struct net *net = sock_net(skb->sk);
  2116. int ret, mrtsock, parent;
  2117. struct mr_table *tbl;
  2118. struct mfcctl mfcc;
  2119. mrtsock = 0;
  2120. tbl = NULL;
  2121. ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl);
  2122. if (ret < 0)
  2123. return ret;
  2124. parent = ret ? mfcc.mfcc_parent : -1;
  2125. if (nlh->nlmsg_type == RTM_NEWROUTE)
  2126. return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
  2127. else
  2128. return ipmr_mfc_delete(tbl, &mfcc, parent);
  2129. }
  2130. #ifdef CONFIG_PROC_FS
  2131. /* The /proc interfaces to multicast routing :
  2132. * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
  2133. */
  2134. struct ipmr_vif_iter {
  2135. struct seq_net_private p;
  2136. struct mr_table *mrt;
  2137. int ct;
  2138. };
  2139. static struct vif_device *ipmr_vif_seq_idx(struct net *net,
  2140. struct ipmr_vif_iter *iter,
  2141. loff_t pos)
  2142. {
  2143. struct mr_table *mrt = iter->mrt;
  2144. for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
  2145. if (!VIF_EXISTS(mrt, iter->ct))
  2146. continue;
  2147. if (pos-- == 0)
  2148. return &mrt->vif_table[iter->ct];
  2149. }
  2150. return NULL;
  2151. }
  2152. static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
  2153. __acquires(mrt_lock)
  2154. {
  2155. struct ipmr_vif_iter *iter = seq->private;
  2156. struct net *net = seq_file_net(seq);
  2157. struct mr_table *mrt;
  2158. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  2159. if (!mrt)
  2160. return ERR_PTR(-ENOENT);
  2161. iter->mrt = mrt;
  2162. read_lock(&mrt_lock);
  2163. return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
  2164. : SEQ_START_TOKEN;
  2165. }
  2166. static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2167. {
  2168. struct ipmr_vif_iter *iter = seq->private;
  2169. struct net *net = seq_file_net(seq);
  2170. struct mr_table *mrt = iter->mrt;
  2171. ++*pos;
  2172. if (v == SEQ_START_TOKEN)
  2173. return ipmr_vif_seq_idx(net, iter, 0);
  2174. while (++iter->ct < mrt->maxvif) {
  2175. if (!VIF_EXISTS(mrt, iter->ct))
  2176. continue;
  2177. return &mrt->vif_table[iter->ct];
  2178. }
  2179. return NULL;
  2180. }
  2181. static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
  2182. __releases(mrt_lock)
  2183. {
  2184. read_unlock(&mrt_lock);
  2185. }
  2186. static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
  2187. {
  2188. struct ipmr_vif_iter *iter = seq->private;
  2189. struct mr_table *mrt = iter->mrt;
  2190. if (v == SEQ_START_TOKEN) {
  2191. seq_puts(seq,
  2192. "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
  2193. } else {
  2194. const struct vif_device *vif = v;
  2195. const char *name = vif->dev ? vif->dev->name : "none";
  2196. seq_printf(seq,
  2197. "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
  2198. vif - mrt->vif_table,
  2199. name, vif->bytes_in, vif->pkt_in,
  2200. vif->bytes_out, vif->pkt_out,
  2201. vif->flags, vif->local, vif->remote);
  2202. }
  2203. return 0;
  2204. }
  2205. static const struct seq_operations ipmr_vif_seq_ops = {
  2206. .start = ipmr_vif_seq_start,
  2207. .next = ipmr_vif_seq_next,
  2208. .stop = ipmr_vif_seq_stop,
  2209. .show = ipmr_vif_seq_show,
  2210. };
  2211. static int ipmr_vif_open(struct inode *inode, struct file *file)
  2212. {
  2213. return seq_open_net(inode, file, &ipmr_vif_seq_ops,
  2214. sizeof(struct ipmr_vif_iter));
  2215. }
  2216. static const struct file_operations ipmr_vif_fops = {
  2217. .owner = THIS_MODULE,
  2218. .open = ipmr_vif_open,
  2219. .read = seq_read,
  2220. .llseek = seq_lseek,
  2221. .release = seq_release_net,
  2222. };
  2223. struct ipmr_mfc_iter {
  2224. struct seq_net_private p;
  2225. struct mr_table *mrt;
  2226. struct list_head *cache;
  2227. int ct;
  2228. };
  2229. static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
  2230. struct ipmr_mfc_iter *it, loff_t pos)
  2231. {
  2232. struct mr_table *mrt = it->mrt;
  2233. struct mfc_cache *mfc;
  2234. rcu_read_lock();
  2235. for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
  2236. it->cache = &mrt->mfc_cache_array[it->ct];
  2237. list_for_each_entry_rcu(mfc, it->cache, list)
  2238. if (pos-- == 0)
  2239. return mfc;
  2240. }
  2241. rcu_read_unlock();
  2242. spin_lock_bh(&mfc_unres_lock);
  2243. it->cache = &mrt->mfc_unres_queue;
  2244. list_for_each_entry(mfc, it->cache, list)
  2245. if (pos-- == 0)
  2246. return mfc;
  2247. spin_unlock_bh(&mfc_unres_lock);
  2248. it->cache = NULL;
  2249. return NULL;
  2250. }
  2251. static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
  2252. {
  2253. struct ipmr_mfc_iter *it = seq->private;
  2254. struct net *net = seq_file_net(seq);
  2255. struct mr_table *mrt;
  2256. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  2257. if (!mrt)
  2258. return ERR_PTR(-ENOENT);
  2259. it->mrt = mrt;
  2260. it->cache = NULL;
  2261. it->ct = 0;
  2262. return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
  2263. : SEQ_START_TOKEN;
  2264. }
  2265. static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2266. {
  2267. struct mfc_cache *mfc = v;
  2268. struct ipmr_mfc_iter *it = seq->private;
  2269. struct net *net = seq_file_net(seq);
  2270. struct mr_table *mrt = it->mrt;
  2271. ++*pos;
  2272. if (v == SEQ_START_TOKEN)
  2273. return ipmr_mfc_seq_idx(net, seq->private, 0);
  2274. if (mfc->list.next != it->cache)
  2275. return list_entry(mfc->list.next, struct mfc_cache, list);
  2276. if (it->cache == &mrt->mfc_unres_queue)
  2277. goto end_of_list;
  2278. BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
  2279. while (++it->ct < MFC_LINES) {
  2280. it->cache = &mrt->mfc_cache_array[it->ct];
  2281. if (list_empty(it->cache))
  2282. continue;
  2283. return list_first_entry(it->cache, struct mfc_cache, list);
  2284. }
  2285. /* exhausted cache_array, show unresolved */
  2286. rcu_read_unlock();
  2287. it->cache = &mrt->mfc_unres_queue;
  2288. it->ct = 0;
  2289. spin_lock_bh(&mfc_unres_lock);
  2290. if (!list_empty(it->cache))
  2291. return list_first_entry(it->cache, struct mfc_cache, list);
  2292. end_of_list:
  2293. spin_unlock_bh(&mfc_unres_lock);
  2294. it->cache = NULL;
  2295. return NULL;
  2296. }
  2297. static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
  2298. {
  2299. struct ipmr_mfc_iter *it = seq->private;
  2300. struct mr_table *mrt = it->mrt;
  2301. if (it->cache == &mrt->mfc_unres_queue)
  2302. spin_unlock_bh(&mfc_unres_lock);
  2303. else if (it->cache == &mrt->mfc_cache_array[it->ct])
  2304. rcu_read_unlock();
  2305. }
  2306. static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
  2307. {
  2308. int n;
  2309. if (v == SEQ_START_TOKEN) {
  2310. seq_puts(seq,
  2311. "Group Origin Iif Pkts Bytes Wrong Oifs\n");
  2312. } else {
  2313. const struct mfc_cache *mfc = v;
  2314. const struct ipmr_mfc_iter *it = seq->private;
  2315. const struct mr_table *mrt = it->mrt;
  2316. seq_printf(seq, "%08X %08X %-3hd",
  2317. (__force u32) mfc->mfc_mcastgrp,
  2318. (__force u32) mfc->mfc_origin,
  2319. mfc->mfc_parent);
  2320. if (it->cache != &mrt->mfc_unres_queue) {
  2321. seq_printf(seq, " %8lu %8lu %8lu",
  2322. mfc->mfc_un.res.pkt,
  2323. mfc->mfc_un.res.bytes,
  2324. mfc->mfc_un.res.wrong_if);
  2325. for (n = mfc->mfc_un.res.minvif;
  2326. n < mfc->mfc_un.res.maxvif; n++) {
  2327. if (VIF_EXISTS(mrt, n) &&
  2328. mfc->mfc_un.res.ttls[n] < 255)
  2329. seq_printf(seq,
  2330. " %2d:%-3d",
  2331. n, mfc->mfc_un.res.ttls[n]);
  2332. }
  2333. } else {
  2334. /* unresolved mfc_caches don't contain
  2335. * pkt, bytes and wrong_if values
  2336. */
  2337. seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
  2338. }
  2339. seq_putc(seq, '\n');
  2340. }
  2341. return 0;
  2342. }
  2343. static const struct seq_operations ipmr_mfc_seq_ops = {
  2344. .start = ipmr_mfc_seq_start,
  2345. .next = ipmr_mfc_seq_next,
  2346. .stop = ipmr_mfc_seq_stop,
  2347. .show = ipmr_mfc_seq_show,
  2348. };
  2349. static int ipmr_mfc_open(struct inode *inode, struct file *file)
  2350. {
  2351. return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
  2352. sizeof(struct ipmr_mfc_iter));
  2353. }
  2354. static const struct file_operations ipmr_mfc_fops = {
  2355. .owner = THIS_MODULE,
  2356. .open = ipmr_mfc_open,
  2357. .read = seq_read,
  2358. .llseek = seq_lseek,
  2359. .release = seq_release_net,
  2360. };
  2361. #endif
  2362. #ifdef CONFIG_IP_PIMSM_V2
  2363. static const struct net_protocol pim_protocol = {
  2364. .handler = pim_rcv,
  2365. .netns_ok = 1,
  2366. };
  2367. #endif
  2368. /* Setup for IP multicast routing */
  2369. static int __net_init ipmr_net_init(struct net *net)
  2370. {
  2371. int err;
  2372. err = ipmr_rules_init(net);
  2373. if (err < 0)
  2374. goto fail;
  2375. #ifdef CONFIG_PROC_FS
  2376. err = -ENOMEM;
  2377. if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
  2378. goto proc_vif_fail;
  2379. if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
  2380. goto proc_cache_fail;
  2381. #endif
  2382. return 0;
  2383. #ifdef CONFIG_PROC_FS
  2384. proc_cache_fail:
  2385. remove_proc_entry("ip_mr_vif", net->proc_net);
  2386. proc_vif_fail:
  2387. ipmr_rules_exit(net);
  2388. #endif
  2389. fail:
  2390. return err;
  2391. }
  2392. static void __net_exit ipmr_net_exit(struct net *net)
  2393. {
  2394. #ifdef CONFIG_PROC_FS
  2395. remove_proc_entry("ip_mr_cache", net->proc_net);
  2396. remove_proc_entry("ip_mr_vif", net->proc_net);
  2397. #endif
  2398. ipmr_rules_exit(net);
  2399. }
  2400. static struct pernet_operations ipmr_net_ops = {
  2401. .init = ipmr_net_init,
  2402. .exit = ipmr_net_exit,
  2403. };
  2404. int __init ip_mr_init(void)
  2405. {
  2406. int err;
  2407. mrt_cachep = kmem_cache_create("ip_mrt_cache",
  2408. sizeof(struct mfc_cache),
  2409. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
  2410. NULL);
  2411. err = register_pernet_subsys(&ipmr_net_ops);
  2412. if (err)
  2413. goto reg_pernet_fail;
  2414. err = register_netdevice_notifier(&ip_mr_notifier);
  2415. if (err)
  2416. goto reg_notif_fail;
  2417. #ifdef CONFIG_IP_PIMSM_V2
  2418. if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
  2419. pr_err("%s: can't add PIM protocol\n", __func__);
  2420. err = -EAGAIN;
  2421. goto add_proto_fail;
  2422. }
  2423. #endif
  2424. rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
  2425. NULL, ipmr_rtm_dumproute, NULL);
  2426. rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
  2427. ipmr_rtm_route, NULL, NULL);
  2428. rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
  2429. ipmr_rtm_route, NULL, NULL);
  2430. return 0;
  2431. #ifdef CONFIG_IP_PIMSM_V2
  2432. add_proto_fail:
  2433. unregister_netdevice_notifier(&ip_mr_notifier);
  2434. #endif
  2435. reg_notif_fail:
  2436. unregister_pernet_subsys(&ipmr_net_ops);
  2437. reg_pernet_fail:
  2438. kmem_cache_destroy(mrt_cachep);
  2439. return err;
  2440. }