ip6mr.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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/system.h>
  19. #include <asm/uaccess.h>
  20. #include <linux/types.h>
  21. #include <linux/sched.h>
  22. #include <linux/errno.h>
  23. #include <linux/timer.h>
  24. #include <linux/mm.h>
  25. #include <linux/kernel.h>
  26. #include <linux/fcntl.h>
  27. #include <linux/stat.h>
  28. #include <linux/socket.h>
  29. #include <linux/inet.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/inetdevice.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/init.h>
  35. #include <net/protocol.h>
  36. #include <linux/skbuff.h>
  37. #include <net/sock.h>
  38. #include <net/raw.h>
  39. #include <linux/notifier.h>
  40. #include <linux/if_arp.h>
  41. #include <net/checksum.h>
  42. #include <net/netlink.h>
  43. #include <net/ipv6.h>
  44. #include <net/ip6_route.h>
  45. #include <linux/mroute6.h>
  46. #include <linux/pim.h>
  47. #include <net/addrconf.h>
  48. #include <linux/netfilter_ipv6.h>
  49. #include <net/ip6_checksum.h>
  50. /* Big lock, protecting vif table, mrt cache and mroute socket state.
  51. Note that the changes are semaphored via rtnl_lock.
  52. */
  53. static DEFINE_RWLOCK(mrt_lock);
  54. /*
  55. * Multicast router control variables
  56. */
  57. #define MIF_EXISTS(_net, _idx) ((_net)->ipv6.vif6_table[_idx].dev != NULL)
  58. static struct mfc6_cache *mfc_unres_queue; /* Queue of unresolved entries */
  59. /* Special spinlock for queue of unresolved entries */
  60. static DEFINE_SPINLOCK(mfc_unres_lock);
  61. /* We return to original Alan's scheme. Hash table of resolved
  62. entries is changed only in process context and protected
  63. with weak lock mrt_lock. Queue of unresolved entries is protected
  64. with strong spinlock mfc_unres_lock.
  65. In this case data path is free of exclusive locks at all.
  66. */
  67. static struct kmem_cache *mrt_cachep __read_mostly;
  68. static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache);
  69. static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt,
  70. mifi_t mifi, int assert);
  71. static int ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm);
  72. static void mroute_clean_tables(struct net *net);
  73. #ifdef CONFIG_IPV6_PIMSM_V2
  74. static struct inet6_protocol pim6_protocol;
  75. #endif
  76. static struct timer_list ipmr_expire_timer;
  77. #ifdef CONFIG_PROC_FS
  78. struct ipmr_mfc_iter {
  79. struct seq_net_private p;
  80. struct mfc6_cache **cache;
  81. int ct;
  82. };
  83. static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
  84. struct ipmr_mfc_iter *it, loff_t pos)
  85. {
  86. struct mfc6_cache *mfc;
  87. it->cache = net->ipv6.mfc6_cache_array;
  88. read_lock(&mrt_lock);
  89. for (it->ct = 0; it->ct < MFC6_LINES; it->ct++)
  90. for (mfc = net->ipv6.mfc6_cache_array[it->ct];
  91. mfc; mfc = mfc->next)
  92. if (pos-- == 0)
  93. return mfc;
  94. read_unlock(&mrt_lock);
  95. it->cache = &mfc_unres_queue;
  96. spin_lock_bh(&mfc_unres_lock);
  97. for (mfc = mfc_unres_queue; mfc; mfc = mfc->next)
  98. if (net_eq(mfc6_net(mfc), net) &&
  99. pos-- == 0)
  100. return mfc;
  101. spin_unlock_bh(&mfc_unres_lock);
  102. it->cache = NULL;
  103. return NULL;
  104. }
  105. /*
  106. * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
  107. */
  108. struct ipmr_vif_iter {
  109. struct seq_net_private p;
  110. int ct;
  111. };
  112. static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
  113. struct ipmr_vif_iter *iter,
  114. loff_t pos)
  115. {
  116. for (iter->ct = 0; iter->ct < net->ipv6.maxvif; ++iter->ct) {
  117. if (!MIF_EXISTS(net, iter->ct))
  118. continue;
  119. if (pos-- == 0)
  120. return &net->ipv6.vif6_table[iter->ct];
  121. }
  122. return NULL;
  123. }
  124. static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
  125. __acquires(mrt_lock)
  126. {
  127. struct net *net = seq_file_net(seq);
  128. read_lock(&mrt_lock);
  129. return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
  130. : SEQ_START_TOKEN;
  131. }
  132. static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  133. {
  134. struct ipmr_vif_iter *iter = seq->private;
  135. struct net *net = seq_file_net(seq);
  136. ++*pos;
  137. if (v == SEQ_START_TOKEN)
  138. return ip6mr_vif_seq_idx(net, iter, 0);
  139. while (++iter->ct < net->ipv6.maxvif) {
  140. if (!MIF_EXISTS(net, iter->ct))
  141. continue;
  142. return &net->ipv6.vif6_table[iter->ct];
  143. }
  144. return NULL;
  145. }
  146. static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
  147. __releases(mrt_lock)
  148. {
  149. read_unlock(&mrt_lock);
  150. }
  151. static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
  152. {
  153. struct net *net = seq_file_net(seq);
  154. if (v == SEQ_START_TOKEN) {
  155. seq_puts(seq,
  156. "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
  157. } else {
  158. const struct mif_device *vif = v;
  159. const char *name = vif->dev ? vif->dev->name : "none";
  160. seq_printf(seq,
  161. "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
  162. vif - net->ipv6.vif6_table,
  163. name, vif->bytes_in, vif->pkt_in,
  164. vif->bytes_out, vif->pkt_out,
  165. vif->flags);
  166. }
  167. return 0;
  168. }
  169. static struct seq_operations ip6mr_vif_seq_ops = {
  170. .start = ip6mr_vif_seq_start,
  171. .next = ip6mr_vif_seq_next,
  172. .stop = ip6mr_vif_seq_stop,
  173. .show = ip6mr_vif_seq_show,
  174. };
  175. static int ip6mr_vif_open(struct inode *inode, struct file *file)
  176. {
  177. return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
  178. sizeof(struct ipmr_vif_iter));
  179. }
  180. static struct file_operations ip6mr_vif_fops = {
  181. .owner = THIS_MODULE,
  182. .open = ip6mr_vif_open,
  183. .read = seq_read,
  184. .llseek = seq_lseek,
  185. .release = seq_release_net,
  186. };
  187. static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
  188. {
  189. struct net *net = seq_file_net(seq);
  190. return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
  191. : SEQ_START_TOKEN;
  192. }
  193. static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  194. {
  195. struct mfc6_cache *mfc = v;
  196. struct ipmr_mfc_iter *it = seq->private;
  197. struct net *net = seq_file_net(seq);
  198. ++*pos;
  199. if (v == SEQ_START_TOKEN)
  200. return ipmr_mfc_seq_idx(net, seq->private, 0);
  201. if (mfc->next)
  202. return mfc->next;
  203. if (it->cache == &mfc_unres_queue)
  204. goto end_of_list;
  205. BUG_ON(it->cache != net->ipv6.mfc6_cache_array);
  206. while (++it->ct < MFC6_LINES) {
  207. mfc = net->ipv6.mfc6_cache_array[it->ct];
  208. if (mfc)
  209. return mfc;
  210. }
  211. /* exhausted cache_array, show unresolved */
  212. read_unlock(&mrt_lock);
  213. it->cache = &mfc_unres_queue;
  214. it->ct = 0;
  215. spin_lock_bh(&mfc_unres_lock);
  216. mfc = mfc_unres_queue;
  217. if (mfc)
  218. return mfc;
  219. end_of_list:
  220. spin_unlock_bh(&mfc_unres_lock);
  221. it->cache = NULL;
  222. return NULL;
  223. }
  224. static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
  225. {
  226. struct ipmr_mfc_iter *it = seq->private;
  227. struct net *net = seq_file_net(seq);
  228. if (it->cache == &mfc_unres_queue)
  229. spin_unlock_bh(&mfc_unres_lock);
  230. else if (it->cache == net->ipv6.mfc6_cache_array)
  231. read_unlock(&mrt_lock);
  232. }
  233. static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
  234. {
  235. int n;
  236. struct net *net = seq_file_net(seq);
  237. if (v == SEQ_START_TOKEN) {
  238. seq_puts(seq,
  239. "Group "
  240. "Origin "
  241. "Iif Pkts Bytes Wrong Oifs\n");
  242. } else {
  243. const struct mfc6_cache *mfc = v;
  244. const struct ipmr_mfc_iter *it = seq->private;
  245. seq_printf(seq, "%pI6 %pI6 %-3hd",
  246. &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
  247. mfc->mf6c_parent);
  248. if (it->cache != &mfc_unres_queue) {
  249. seq_printf(seq, " %8lu %8lu %8lu",
  250. mfc->mfc_un.res.pkt,
  251. mfc->mfc_un.res.bytes,
  252. mfc->mfc_un.res.wrong_if);
  253. for (n = mfc->mfc_un.res.minvif;
  254. n < mfc->mfc_un.res.maxvif; n++) {
  255. if (MIF_EXISTS(net, n) &&
  256. mfc->mfc_un.res.ttls[n] < 255)
  257. seq_printf(seq,
  258. " %2d:%-3d",
  259. n, mfc->mfc_un.res.ttls[n]);
  260. }
  261. } else {
  262. /* unresolved mfc_caches don't contain
  263. * pkt, bytes and wrong_if values
  264. */
  265. seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
  266. }
  267. seq_putc(seq, '\n');
  268. }
  269. return 0;
  270. }
  271. static struct seq_operations ipmr_mfc_seq_ops = {
  272. .start = ipmr_mfc_seq_start,
  273. .next = ipmr_mfc_seq_next,
  274. .stop = ipmr_mfc_seq_stop,
  275. .show = ipmr_mfc_seq_show,
  276. };
  277. static int ipmr_mfc_open(struct inode *inode, struct file *file)
  278. {
  279. return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
  280. sizeof(struct ipmr_mfc_iter));
  281. }
  282. static struct file_operations ip6mr_mfc_fops = {
  283. .owner = THIS_MODULE,
  284. .open = ipmr_mfc_open,
  285. .read = seq_read,
  286. .llseek = seq_lseek,
  287. .release = seq_release_net,
  288. };
  289. #endif
  290. #ifdef CONFIG_IPV6_PIMSM_V2
  291. static int pim6_rcv(struct sk_buff *skb)
  292. {
  293. struct pimreghdr *pim;
  294. struct ipv6hdr *encap;
  295. struct net_device *reg_dev = NULL;
  296. struct net *net = dev_net(skb->dev);
  297. int reg_vif_num = net->ipv6.mroute_reg_vif_num;
  298. if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
  299. goto drop;
  300. pim = (struct pimreghdr *)skb_transport_header(skb);
  301. if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
  302. (pim->flags & PIM_NULL_REGISTER) ||
  303. (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  304. sizeof(*pim), IPPROTO_PIM,
  305. csum_partial((void *)pim, sizeof(*pim), 0)) &&
  306. csum_fold(skb_checksum(skb, 0, skb->len, 0))))
  307. goto drop;
  308. /* check if the inner packet is destined to mcast group */
  309. encap = (struct ipv6hdr *)(skb_transport_header(skb) +
  310. sizeof(*pim));
  311. if (!ipv6_addr_is_multicast(&encap->daddr) ||
  312. encap->payload_len == 0 ||
  313. ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
  314. goto drop;
  315. read_lock(&mrt_lock);
  316. if (reg_vif_num >= 0)
  317. reg_dev = net->ipv6.vif6_table[reg_vif_num].dev;
  318. if (reg_dev)
  319. dev_hold(reg_dev);
  320. read_unlock(&mrt_lock);
  321. if (reg_dev == NULL)
  322. goto drop;
  323. skb->mac_header = skb->network_header;
  324. skb_pull(skb, (u8 *)encap - skb->data);
  325. skb_reset_network_header(skb);
  326. skb->dev = reg_dev;
  327. skb->protocol = htons(ETH_P_IPV6);
  328. skb->ip_summed = 0;
  329. skb->pkt_type = PACKET_HOST;
  330. skb_dst_drop(skb);
  331. reg_dev->stats.rx_bytes += skb->len;
  332. reg_dev->stats.rx_packets++;
  333. nf_reset(skb);
  334. netif_rx(skb);
  335. dev_put(reg_dev);
  336. return 0;
  337. drop:
  338. kfree_skb(skb);
  339. return 0;
  340. }
  341. static struct inet6_protocol pim6_protocol = {
  342. .handler = pim6_rcv,
  343. };
  344. /* Service routines creating virtual interfaces: PIMREG */
  345. static int reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
  346. {
  347. struct net *net = dev_net(dev);
  348. read_lock(&mrt_lock);
  349. dev->stats.tx_bytes += skb->len;
  350. dev->stats.tx_packets++;
  351. ip6mr_cache_report(net, skb, net->ipv6.mroute_reg_vif_num,
  352. MRT6MSG_WHOLEPKT);
  353. read_unlock(&mrt_lock);
  354. kfree_skb(skb);
  355. return 0;
  356. }
  357. static const struct net_device_ops reg_vif_netdev_ops = {
  358. .ndo_start_xmit = reg_vif_xmit,
  359. };
  360. static void reg_vif_setup(struct net_device *dev)
  361. {
  362. dev->type = ARPHRD_PIMREG;
  363. dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
  364. dev->flags = IFF_NOARP;
  365. dev->netdev_ops = &reg_vif_netdev_ops;
  366. dev->destructor = free_netdev;
  367. dev->features |= NETIF_F_NETNS_LOCAL;
  368. }
  369. static struct net_device *ip6mr_reg_vif(struct net *net)
  370. {
  371. struct net_device *dev;
  372. dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
  373. if (dev == NULL)
  374. return NULL;
  375. dev_net_set(dev, net);
  376. if (register_netdevice(dev)) {
  377. free_netdev(dev);
  378. return NULL;
  379. }
  380. dev->iflink = 0;
  381. if (dev_open(dev))
  382. goto failure;
  383. dev_hold(dev);
  384. return dev;
  385. failure:
  386. /* allow the register to be completed before unregistering. */
  387. rtnl_unlock();
  388. rtnl_lock();
  389. unregister_netdevice(dev);
  390. return NULL;
  391. }
  392. #endif
  393. /*
  394. * Delete a VIF entry
  395. */
  396. static int mif6_delete(struct net *net, int vifi)
  397. {
  398. struct mif_device *v;
  399. struct net_device *dev;
  400. struct inet6_dev *in6_dev;
  401. if (vifi < 0 || vifi >= net->ipv6.maxvif)
  402. return -EADDRNOTAVAIL;
  403. v = &net->ipv6.vif6_table[vifi];
  404. write_lock_bh(&mrt_lock);
  405. dev = v->dev;
  406. v->dev = NULL;
  407. if (!dev) {
  408. write_unlock_bh(&mrt_lock);
  409. return -EADDRNOTAVAIL;
  410. }
  411. #ifdef CONFIG_IPV6_PIMSM_V2
  412. if (vifi == net->ipv6.mroute_reg_vif_num)
  413. net->ipv6.mroute_reg_vif_num = -1;
  414. #endif
  415. if (vifi + 1 == net->ipv6.maxvif) {
  416. int tmp;
  417. for (tmp = vifi - 1; tmp >= 0; tmp--) {
  418. if (MIF_EXISTS(net, tmp))
  419. break;
  420. }
  421. net->ipv6.maxvif = tmp + 1;
  422. }
  423. write_unlock_bh(&mrt_lock);
  424. dev_set_allmulti(dev, -1);
  425. in6_dev = __in6_dev_get(dev);
  426. if (in6_dev)
  427. in6_dev->cnf.mc_forwarding--;
  428. if (v->flags & MIFF_REGISTER)
  429. unregister_netdevice(dev);
  430. dev_put(dev);
  431. return 0;
  432. }
  433. static inline void ip6mr_cache_free(struct mfc6_cache *c)
  434. {
  435. release_net(mfc6_net(c));
  436. kmem_cache_free(mrt_cachep, c);
  437. }
  438. /* Destroy an unresolved cache entry, killing queued skbs
  439. and reporting error to netlink readers.
  440. */
  441. static void ip6mr_destroy_unres(struct mfc6_cache *c)
  442. {
  443. struct sk_buff *skb;
  444. struct net *net = mfc6_net(c);
  445. atomic_dec(&net->ipv6.cache_resolve_queue_len);
  446. while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
  447. if (ipv6_hdr(skb)->version == 0) {
  448. struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
  449. nlh->nlmsg_type = NLMSG_ERROR;
  450. nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
  451. skb_trim(skb, nlh->nlmsg_len);
  452. ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
  453. rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
  454. } else
  455. kfree_skb(skb);
  456. }
  457. ip6mr_cache_free(c);
  458. }
  459. /* Single timer process for all the unresolved queue. */
  460. static void ipmr_do_expire_process(unsigned long dummy)
  461. {
  462. unsigned long now = jiffies;
  463. unsigned long expires = 10 * HZ;
  464. struct mfc6_cache *c, **cp;
  465. cp = &mfc_unres_queue;
  466. while ((c = *cp) != NULL) {
  467. if (time_after(c->mfc_un.unres.expires, now)) {
  468. /* not yet... */
  469. unsigned long interval = c->mfc_un.unres.expires - now;
  470. if (interval < expires)
  471. expires = interval;
  472. cp = &c->next;
  473. continue;
  474. }
  475. *cp = c->next;
  476. ip6mr_destroy_unres(c);
  477. }
  478. if (mfc_unres_queue != NULL)
  479. mod_timer(&ipmr_expire_timer, jiffies + expires);
  480. }
  481. static void ipmr_expire_process(unsigned long dummy)
  482. {
  483. if (!spin_trylock(&mfc_unres_lock)) {
  484. mod_timer(&ipmr_expire_timer, jiffies + 1);
  485. return;
  486. }
  487. if (mfc_unres_queue != NULL)
  488. ipmr_do_expire_process(dummy);
  489. spin_unlock(&mfc_unres_lock);
  490. }
  491. /* Fill oifs list. It is called under write locked mrt_lock. */
  492. static void ip6mr_update_thresholds(struct mfc6_cache *cache, unsigned char *ttls)
  493. {
  494. int vifi;
  495. struct net *net = mfc6_net(cache);
  496. cache->mfc_un.res.minvif = MAXMIFS;
  497. cache->mfc_un.res.maxvif = 0;
  498. memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
  499. for (vifi = 0; vifi < net->ipv6.maxvif; vifi++) {
  500. if (MIF_EXISTS(net, vifi) &&
  501. ttls[vifi] && ttls[vifi] < 255) {
  502. cache->mfc_un.res.ttls[vifi] = ttls[vifi];
  503. if (cache->mfc_un.res.minvif > vifi)
  504. cache->mfc_un.res.minvif = vifi;
  505. if (cache->mfc_un.res.maxvif <= vifi)
  506. cache->mfc_un.res.maxvif = vifi + 1;
  507. }
  508. }
  509. }
  510. static int mif6_add(struct net *net, struct mif6ctl *vifc, int mrtsock)
  511. {
  512. int vifi = vifc->mif6c_mifi;
  513. struct mif_device *v = &net->ipv6.vif6_table[vifi];
  514. struct net_device *dev;
  515. struct inet6_dev *in6_dev;
  516. int err;
  517. /* Is vif busy ? */
  518. if (MIF_EXISTS(net, vifi))
  519. return -EADDRINUSE;
  520. switch (vifc->mif6c_flags) {
  521. #ifdef CONFIG_IPV6_PIMSM_V2
  522. case MIFF_REGISTER:
  523. /*
  524. * Special Purpose VIF in PIM
  525. * All the packets will be sent to the daemon
  526. */
  527. if (net->ipv6.mroute_reg_vif_num >= 0)
  528. return -EADDRINUSE;
  529. dev = ip6mr_reg_vif(net);
  530. if (!dev)
  531. return -ENOBUFS;
  532. err = dev_set_allmulti(dev, 1);
  533. if (err) {
  534. unregister_netdevice(dev);
  535. dev_put(dev);
  536. return err;
  537. }
  538. break;
  539. #endif
  540. case 0:
  541. dev = dev_get_by_index(net, vifc->mif6c_pifi);
  542. if (!dev)
  543. return -EADDRNOTAVAIL;
  544. err = dev_set_allmulti(dev, 1);
  545. if (err) {
  546. dev_put(dev);
  547. return err;
  548. }
  549. break;
  550. default:
  551. return -EINVAL;
  552. }
  553. in6_dev = __in6_dev_get(dev);
  554. if (in6_dev)
  555. in6_dev->cnf.mc_forwarding++;
  556. /*
  557. * Fill in the VIF structures
  558. */
  559. v->rate_limit = vifc->vifc_rate_limit;
  560. v->flags = vifc->mif6c_flags;
  561. if (!mrtsock)
  562. v->flags |= VIFF_STATIC;
  563. v->threshold = vifc->vifc_threshold;
  564. v->bytes_in = 0;
  565. v->bytes_out = 0;
  566. v->pkt_in = 0;
  567. v->pkt_out = 0;
  568. v->link = dev->ifindex;
  569. if (v->flags & MIFF_REGISTER)
  570. v->link = dev->iflink;
  571. /* And finish update writing critical data */
  572. write_lock_bh(&mrt_lock);
  573. v->dev = dev;
  574. #ifdef CONFIG_IPV6_PIMSM_V2
  575. if (v->flags & MIFF_REGISTER)
  576. net->ipv6.mroute_reg_vif_num = vifi;
  577. #endif
  578. if (vifi + 1 > net->ipv6.maxvif)
  579. net->ipv6.maxvif = vifi + 1;
  580. write_unlock_bh(&mrt_lock);
  581. return 0;
  582. }
  583. static struct mfc6_cache *ip6mr_cache_find(struct net *net,
  584. struct in6_addr *origin,
  585. struct in6_addr *mcastgrp)
  586. {
  587. int line = MFC6_HASH(mcastgrp, origin);
  588. struct mfc6_cache *c;
  589. for (c = net->ipv6.mfc6_cache_array[line]; c; c = c->next) {
  590. if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
  591. ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
  592. break;
  593. }
  594. return c;
  595. }
  596. /*
  597. * Allocate a multicast cache entry
  598. */
  599. static struct mfc6_cache *ip6mr_cache_alloc(struct net *net)
  600. {
  601. struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
  602. if (c == NULL)
  603. return NULL;
  604. c->mfc_un.res.minvif = MAXMIFS;
  605. mfc6_net_set(c, net);
  606. return c;
  607. }
  608. static struct mfc6_cache *ip6mr_cache_alloc_unres(struct net *net)
  609. {
  610. struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
  611. if (c == NULL)
  612. return NULL;
  613. skb_queue_head_init(&c->mfc_un.unres.unresolved);
  614. c->mfc_un.unres.expires = jiffies + 10 * HZ;
  615. mfc6_net_set(c, net);
  616. return c;
  617. }
  618. /*
  619. * A cache entry has gone into a resolved state from queued
  620. */
  621. static void ip6mr_cache_resolve(struct mfc6_cache *uc, struct mfc6_cache *c)
  622. {
  623. struct sk_buff *skb;
  624. /*
  625. * Play the pending entries through our router
  626. */
  627. while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
  628. if (ipv6_hdr(skb)->version == 0) {
  629. int err;
  630. struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
  631. if (ip6mr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
  632. nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
  633. } else {
  634. nlh->nlmsg_type = NLMSG_ERROR;
  635. nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
  636. skb_trim(skb, nlh->nlmsg_len);
  637. ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
  638. }
  639. err = rtnl_unicast(skb, mfc6_net(uc), NETLINK_CB(skb).pid);
  640. } else
  641. ip6_mr_forward(skb, c);
  642. }
  643. }
  644. /*
  645. * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
  646. * expects the following bizarre scheme.
  647. *
  648. * Called under mrt_lock.
  649. */
  650. static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, mifi_t mifi,
  651. int assert)
  652. {
  653. struct sk_buff *skb;
  654. struct mrt6msg *msg;
  655. int ret;
  656. #ifdef CONFIG_IPV6_PIMSM_V2
  657. if (assert == MRT6MSG_WHOLEPKT)
  658. skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
  659. +sizeof(*msg));
  660. else
  661. #endif
  662. skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
  663. if (!skb)
  664. return -ENOBUFS;
  665. /* I suppose that internal messages
  666. * do not require checksums */
  667. skb->ip_summed = CHECKSUM_UNNECESSARY;
  668. #ifdef CONFIG_IPV6_PIMSM_V2
  669. if (assert == MRT6MSG_WHOLEPKT) {
  670. /* Ugly, but we have no choice with this interface.
  671. Duplicate old header, fix length etc.
  672. And all this only to mangle msg->im6_msgtype and
  673. to set msg->im6_mbz to "mbz" :-)
  674. */
  675. skb_push(skb, -skb_network_offset(pkt));
  676. skb_push(skb, sizeof(*msg));
  677. skb_reset_transport_header(skb);
  678. msg = (struct mrt6msg *)skb_transport_header(skb);
  679. msg->im6_mbz = 0;
  680. msg->im6_msgtype = MRT6MSG_WHOLEPKT;
  681. msg->im6_mif = net->ipv6.mroute_reg_vif_num;
  682. msg->im6_pad = 0;
  683. ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
  684. ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
  685. skb->ip_summed = CHECKSUM_UNNECESSARY;
  686. } else
  687. #endif
  688. {
  689. /*
  690. * Copy the IP header
  691. */
  692. skb_put(skb, sizeof(struct ipv6hdr));
  693. skb_reset_network_header(skb);
  694. skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
  695. /*
  696. * Add our header
  697. */
  698. skb_put(skb, sizeof(*msg));
  699. skb_reset_transport_header(skb);
  700. msg = (struct mrt6msg *)skb_transport_header(skb);
  701. msg->im6_mbz = 0;
  702. msg->im6_msgtype = assert;
  703. msg->im6_mif = mifi;
  704. msg->im6_pad = 0;
  705. ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
  706. ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
  707. skb_dst_set(skb, dst_clone(skb_dst(pkt)));
  708. skb->ip_summed = CHECKSUM_UNNECESSARY;
  709. }
  710. if (net->ipv6.mroute6_sk == NULL) {
  711. kfree_skb(skb);
  712. return -EINVAL;
  713. }
  714. /*
  715. * Deliver to user space multicast routing algorithms
  716. */
  717. ret = sock_queue_rcv_skb(net->ipv6.mroute6_sk, skb);
  718. if (ret < 0) {
  719. if (net_ratelimit())
  720. printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
  721. kfree_skb(skb);
  722. }
  723. return ret;
  724. }
  725. /*
  726. * Queue a packet for resolution. It gets locked cache entry!
  727. */
  728. static int
  729. ip6mr_cache_unresolved(struct net *net, mifi_t mifi, struct sk_buff *skb)
  730. {
  731. int err;
  732. struct mfc6_cache *c;
  733. spin_lock_bh(&mfc_unres_lock);
  734. for (c = mfc_unres_queue; c; c = c->next) {
  735. if (net_eq(mfc6_net(c), net) &&
  736. ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
  737. ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr))
  738. break;
  739. }
  740. if (c == NULL) {
  741. /*
  742. * Create a new entry if allowable
  743. */
  744. if (atomic_read(&net->ipv6.cache_resolve_queue_len) >= 10 ||
  745. (c = ip6mr_cache_alloc_unres(net)) == NULL) {
  746. spin_unlock_bh(&mfc_unres_lock);
  747. kfree_skb(skb);
  748. return -ENOBUFS;
  749. }
  750. /*
  751. * Fill in the new cache entry
  752. */
  753. c->mf6c_parent = -1;
  754. c->mf6c_origin = ipv6_hdr(skb)->saddr;
  755. c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
  756. /*
  757. * Reflect first query at pim6sd
  758. */
  759. err = ip6mr_cache_report(net, skb, mifi, MRT6MSG_NOCACHE);
  760. if (err < 0) {
  761. /* If the report failed throw the cache entry
  762. out - Brad Parker
  763. */
  764. spin_unlock_bh(&mfc_unres_lock);
  765. ip6mr_cache_free(c);
  766. kfree_skb(skb);
  767. return err;
  768. }
  769. atomic_inc(&net->ipv6.cache_resolve_queue_len);
  770. c->next = mfc_unres_queue;
  771. mfc_unres_queue = c;
  772. ipmr_do_expire_process(1);
  773. }
  774. /*
  775. * See if we can append the packet
  776. */
  777. if (c->mfc_un.unres.unresolved.qlen > 3) {
  778. kfree_skb(skb);
  779. err = -ENOBUFS;
  780. } else {
  781. skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
  782. err = 0;
  783. }
  784. spin_unlock_bh(&mfc_unres_lock);
  785. return err;
  786. }
  787. /*
  788. * MFC6 cache manipulation by user space
  789. */
  790. static int ip6mr_mfc_delete(struct net *net, struct mf6cctl *mfc)
  791. {
  792. int line;
  793. struct mfc6_cache *c, **cp;
  794. line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
  795. for (cp = &net->ipv6.mfc6_cache_array[line];
  796. (c = *cp) != NULL; cp = &c->next) {
  797. if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
  798. ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
  799. write_lock_bh(&mrt_lock);
  800. *cp = c->next;
  801. write_unlock_bh(&mrt_lock);
  802. ip6mr_cache_free(c);
  803. return 0;
  804. }
  805. }
  806. return -ENOENT;
  807. }
  808. static int ip6mr_device_event(struct notifier_block *this,
  809. unsigned long event, void *ptr)
  810. {
  811. struct net_device *dev = ptr;
  812. struct net *net = dev_net(dev);
  813. struct mif_device *v;
  814. int ct;
  815. if (event != NETDEV_UNREGISTER)
  816. return NOTIFY_DONE;
  817. v = &net->ipv6.vif6_table[0];
  818. for (ct = 0; ct < net->ipv6.maxvif; ct++, v++) {
  819. if (v->dev == dev)
  820. mif6_delete(net, ct);
  821. }
  822. return NOTIFY_DONE;
  823. }
  824. static struct notifier_block ip6_mr_notifier = {
  825. .notifier_call = ip6mr_device_event
  826. };
  827. /*
  828. * Setup for IP multicast routing
  829. */
  830. static int __net_init ip6mr_net_init(struct net *net)
  831. {
  832. int err = 0;
  833. net->ipv6.vif6_table = kcalloc(MAXMIFS, sizeof(struct mif_device),
  834. GFP_KERNEL);
  835. if (!net->ipv6.vif6_table) {
  836. err = -ENOMEM;
  837. goto fail;
  838. }
  839. /* Forwarding cache */
  840. net->ipv6.mfc6_cache_array = kcalloc(MFC6_LINES,
  841. sizeof(struct mfc6_cache *),
  842. GFP_KERNEL);
  843. if (!net->ipv6.mfc6_cache_array) {
  844. err = -ENOMEM;
  845. goto fail_mfc6_cache;
  846. }
  847. #ifdef CONFIG_IPV6_PIMSM_V2
  848. net->ipv6.mroute_reg_vif_num = -1;
  849. #endif
  850. #ifdef CONFIG_PROC_FS
  851. err = -ENOMEM;
  852. if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
  853. goto proc_vif_fail;
  854. if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
  855. goto proc_cache_fail;
  856. #endif
  857. return 0;
  858. #ifdef CONFIG_PROC_FS
  859. proc_cache_fail:
  860. proc_net_remove(net, "ip6_mr_vif");
  861. proc_vif_fail:
  862. kfree(net->ipv6.mfc6_cache_array);
  863. #endif
  864. fail_mfc6_cache:
  865. kfree(net->ipv6.vif6_table);
  866. fail:
  867. return err;
  868. }
  869. static void __net_exit ip6mr_net_exit(struct net *net)
  870. {
  871. #ifdef CONFIG_PROC_FS
  872. proc_net_remove(net, "ip6_mr_cache");
  873. proc_net_remove(net, "ip6_mr_vif");
  874. #endif
  875. mroute_clean_tables(net);
  876. kfree(net->ipv6.mfc6_cache_array);
  877. kfree(net->ipv6.vif6_table);
  878. }
  879. static struct pernet_operations ip6mr_net_ops = {
  880. .init = ip6mr_net_init,
  881. .exit = ip6mr_net_exit,
  882. };
  883. int __init ip6_mr_init(void)
  884. {
  885. int err;
  886. mrt_cachep = kmem_cache_create("ip6_mrt_cache",
  887. sizeof(struct mfc6_cache),
  888. 0, SLAB_HWCACHE_ALIGN,
  889. NULL);
  890. if (!mrt_cachep)
  891. return -ENOMEM;
  892. err = register_pernet_subsys(&ip6mr_net_ops);
  893. if (err)
  894. goto reg_pernet_fail;
  895. setup_timer(&ipmr_expire_timer, ipmr_expire_process, 0);
  896. err = register_netdevice_notifier(&ip6_mr_notifier);
  897. if (err)
  898. goto reg_notif_fail;
  899. #ifdef CONFIG_IPV6_PIMSM_V2
  900. if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
  901. printk(KERN_ERR "ip6_mr_init: can't add PIM protocol\n");
  902. err = -EAGAIN;
  903. goto add_proto_fail;
  904. }
  905. #endif
  906. return 0;
  907. #ifdef CONFIG_IPV6_PIMSM_V2
  908. add_proto_fail:
  909. unregister_netdevice_notifier(&ip6_mr_notifier);
  910. #endif
  911. reg_notif_fail:
  912. del_timer(&ipmr_expire_timer);
  913. unregister_pernet_subsys(&ip6mr_net_ops);
  914. reg_pernet_fail:
  915. kmem_cache_destroy(mrt_cachep);
  916. return err;
  917. }
  918. void ip6_mr_cleanup(void)
  919. {
  920. unregister_netdevice_notifier(&ip6_mr_notifier);
  921. del_timer(&ipmr_expire_timer);
  922. unregister_pernet_subsys(&ip6mr_net_ops);
  923. kmem_cache_destroy(mrt_cachep);
  924. }
  925. static int ip6mr_mfc_add(struct net *net, struct mf6cctl *mfc, int mrtsock)
  926. {
  927. int line;
  928. struct mfc6_cache *uc, *c, **cp;
  929. unsigned char ttls[MAXMIFS];
  930. int i;
  931. memset(ttls, 255, MAXMIFS);
  932. for (i = 0; i < MAXMIFS; i++) {
  933. if (IF_ISSET(i, &mfc->mf6cc_ifset))
  934. ttls[i] = 1;
  935. }
  936. line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
  937. for (cp = &net->ipv6.mfc6_cache_array[line];
  938. (c = *cp) != NULL; cp = &c->next) {
  939. if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
  940. ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr))
  941. break;
  942. }
  943. if (c != NULL) {
  944. write_lock_bh(&mrt_lock);
  945. c->mf6c_parent = mfc->mf6cc_parent;
  946. ip6mr_update_thresholds(c, ttls);
  947. if (!mrtsock)
  948. c->mfc_flags |= MFC_STATIC;
  949. write_unlock_bh(&mrt_lock);
  950. return 0;
  951. }
  952. if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
  953. return -EINVAL;
  954. c = ip6mr_cache_alloc(net);
  955. if (c == NULL)
  956. return -ENOMEM;
  957. c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
  958. c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
  959. c->mf6c_parent = mfc->mf6cc_parent;
  960. ip6mr_update_thresholds(c, ttls);
  961. if (!mrtsock)
  962. c->mfc_flags |= MFC_STATIC;
  963. write_lock_bh(&mrt_lock);
  964. c->next = net->ipv6.mfc6_cache_array[line];
  965. net->ipv6.mfc6_cache_array[line] = c;
  966. write_unlock_bh(&mrt_lock);
  967. /*
  968. * Check to see if we resolved a queued list. If so we
  969. * need to send on the frames and tidy up.
  970. */
  971. spin_lock_bh(&mfc_unres_lock);
  972. for (cp = &mfc_unres_queue; (uc = *cp) != NULL;
  973. cp = &uc->next) {
  974. if (net_eq(mfc6_net(uc), net) &&
  975. ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
  976. ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
  977. *cp = uc->next;
  978. atomic_dec(&net->ipv6.cache_resolve_queue_len);
  979. break;
  980. }
  981. }
  982. if (mfc_unres_queue == NULL)
  983. del_timer(&ipmr_expire_timer);
  984. spin_unlock_bh(&mfc_unres_lock);
  985. if (uc) {
  986. ip6mr_cache_resolve(uc, c);
  987. ip6mr_cache_free(uc);
  988. }
  989. return 0;
  990. }
  991. /*
  992. * Close the multicast socket, and clear the vif tables etc
  993. */
  994. static void mroute_clean_tables(struct net *net)
  995. {
  996. int i;
  997. /*
  998. * Shut down all active vif entries
  999. */
  1000. for (i = 0; i < net->ipv6.maxvif; i++) {
  1001. if (!(net->ipv6.vif6_table[i].flags & VIFF_STATIC))
  1002. mif6_delete(net, i);
  1003. }
  1004. /*
  1005. * Wipe the cache
  1006. */
  1007. for (i = 0; i < MFC6_LINES; i++) {
  1008. struct mfc6_cache *c, **cp;
  1009. cp = &net->ipv6.mfc6_cache_array[i];
  1010. while ((c = *cp) != NULL) {
  1011. if (c->mfc_flags & MFC_STATIC) {
  1012. cp = &c->next;
  1013. continue;
  1014. }
  1015. write_lock_bh(&mrt_lock);
  1016. *cp = c->next;
  1017. write_unlock_bh(&mrt_lock);
  1018. ip6mr_cache_free(c);
  1019. }
  1020. }
  1021. if (atomic_read(&net->ipv6.cache_resolve_queue_len) != 0) {
  1022. struct mfc6_cache *c, **cp;
  1023. spin_lock_bh(&mfc_unres_lock);
  1024. cp = &mfc_unres_queue;
  1025. while ((c = *cp) != NULL) {
  1026. if (!net_eq(mfc6_net(c), net)) {
  1027. cp = &c->next;
  1028. continue;
  1029. }
  1030. *cp = c->next;
  1031. ip6mr_destroy_unres(c);
  1032. }
  1033. spin_unlock_bh(&mfc_unres_lock);
  1034. }
  1035. }
  1036. static int ip6mr_sk_init(struct sock *sk)
  1037. {
  1038. int err = 0;
  1039. struct net *net = sock_net(sk);
  1040. rtnl_lock();
  1041. write_lock_bh(&mrt_lock);
  1042. if (likely(net->ipv6.mroute6_sk == NULL)) {
  1043. net->ipv6.mroute6_sk = sk;
  1044. net->ipv6.devconf_all->mc_forwarding++;
  1045. }
  1046. else
  1047. err = -EADDRINUSE;
  1048. write_unlock_bh(&mrt_lock);
  1049. rtnl_unlock();
  1050. return err;
  1051. }
  1052. int ip6mr_sk_done(struct sock *sk)
  1053. {
  1054. int err = 0;
  1055. struct net *net = sock_net(sk);
  1056. rtnl_lock();
  1057. if (sk == net->ipv6.mroute6_sk) {
  1058. write_lock_bh(&mrt_lock);
  1059. net->ipv6.mroute6_sk = NULL;
  1060. net->ipv6.devconf_all->mc_forwarding--;
  1061. write_unlock_bh(&mrt_lock);
  1062. mroute_clean_tables(net);
  1063. } else
  1064. err = -EACCES;
  1065. rtnl_unlock();
  1066. return err;
  1067. }
  1068. /*
  1069. * Socket options and virtual interface manipulation. The whole
  1070. * virtual interface system is a complete heap, but unfortunately
  1071. * that's how BSD mrouted happens to think. Maybe one day with a proper
  1072. * MOSPF/PIM router set up we can clean this up.
  1073. */
  1074. int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, int optlen)
  1075. {
  1076. int ret;
  1077. struct mif6ctl vif;
  1078. struct mf6cctl mfc;
  1079. mifi_t mifi;
  1080. struct net *net = sock_net(sk);
  1081. if (optname != MRT6_INIT) {
  1082. if (sk != net->ipv6.mroute6_sk && !capable(CAP_NET_ADMIN))
  1083. return -EACCES;
  1084. }
  1085. switch (optname) {
  1086. case MRT6_INIT:
  1087. if (sk->sk_type != SOCK_RAW ||
  1088. inet_sk(sk)->num != IPPROTO_ICMPV6)
  1089. return -EOPNOTSUPP;
  1090. if (optlen < sizeof(int))
  1091. return -EINVAL;
  1092. return ip6mr_sk_init(sk);
  1093. case MRT6_DONE:
  1094. return ip6mr_sk_done(sk);
  1095. case MRT6_ADD_MIF:
  1096. if (optlen < sizeof(vif))
  1097. return -EINVAL;
  1098. if (copy_from_user(&vif, optval, sizeof(vif)))
  1099. return -EFAULT;
  1100. if (vif.mif6c_mifi >= MAXMIFS)
  1101. return -ENFILE;
  1102. rtnl_lock();
  1103. ret = mif6_add(net, &vif, sk == net->ipv6.mroute6_sk);
  1104. rtnl_unlock();
  1105. return ret;
  1106. case MRT6_DEL_MIF:
  1107. if (optlen < sizeof(mifi_t))
  1108. return -EINVAL;
  1109. if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
  1110. return -EFAULT;
  1111. rtnl_lock();
  1112. ret = mif6_delete(net, mifi);
  1113. rtnl_unlock();
  1114. return ret;
  1115. /*
  1116. * Manipulate the forwarding caches. These live
  1117. * in a sort of kernel/user symbiosis.
  1118. */
  1119. case MRT6_ADD_MFC:
  1120. case MRT6_DEL_MFC:
  1121. if (optlen < sizeof(mfc))
  1122. return -EINVAL;
  1123. if (copy_from_user(&mfc, optval, sizeof(mfc)))
  1124. return -EFAULT;
  1125. rtnl_lock();
  1126. if (optname == MRT6_DEL_MFC)
  1127. ret = ip6mr_mfc_delete(net, &mfc);
  1128. else
  1129. ret = ip6mr_mfc_add(net, &mfc,
  1130. sk == net->ipv6.mroute6_sk);
  1131. rtnl_unlock();
  1132. return ret;
  1133. /*
  1134. * Control PIM assert (to activate pim will activate assert)
  1135. */
  1136. case MRT6_ASSERT:
  1137. {
  1138. int v;
  1139. if (get_user(v, (int __user *)optval))
  1140. return -EFAULT;
  1141. net->ipv6.mroute_do_assert = !!v;
  1142. return 0;
  1143. }
  1144. #ifdef CONFIG_IPV6_PIMSM_V2
  1145. case MRT6_PIM:
  1146. {
  1147. int v;
  1148. if (get_user(v, (int __user *)optval))
  1149. return -EFAULT;
  1150. v = !!v;
  1151. rtnl_lock();
  1152. ret = 0;
  1153. if (v != net->ipv6.mroute_do_pim) {
  1154. net->ipv6.mroute_do_pim = v;
  1155. net->ipv6.mroute_do_assert = v;
  1156. }
  1157. rtnl_unlock();
  1158. return ret;
  1159. }
  1160. #endif
  1161. /*
  1162. * Spurious command, or MRT6_VERSION which you cannot
  1163. * set.
  1164. */
  1165. default:
  1166. return -ENOPROTOOPT;
  1167. }
  1168. }
  1169. /*
  1170. * Getsock opt support for the multicast routing system.
  1171. */
  1172. int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
  1173. int __user *optlen)
  1174. {
  1175. int olr;
  1176. int val;
  1177. struct net *net = sock_net(sk);
  1178. switch (optname) {
  1179. case MRT6_VERSION:
  1180. val = 0x0305;
  1181. break;
  1182. #ifdef CONFIG_IPV6_PIMSM_V2
  1183. case MRT6_PIM:
  1184. val = net->ipv6.mroute_do_pim;
  1185. break;
  1186. #endif
  1187. case MRT6_ASSERT:
  1188. val = net->ipv6.mroute_do_assert;
  1189. break;
  1190. default:
  1191. return -ENOPROTOOPT;
  1192. }
  1193. if (get_user(olr, optlen))
  1194. return -EFAULT;
  1195. olr = min_t(int, olr, sizeof(int));
  1196. if (olr < 0)
  1197. return -EINVAL;
  1198. if (put_user(olr, optlen))
  1199. return -EFAULT;
  1200. if (copy_to_user(optval, &val, olr))
  1201. return -EFAULT;
  1202. return 0;
  1203. }
  1204. /*
  1205. * The IP multicast ioctl support routines.
  1206. */
  1207. int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
  1208. {
  1209. struct sioc_sg_req6 sr;
  1210. struct sioc_mif_req6 vr;
  1211. struct mif_device *vif;
  1212. struct mfc6_cache *c;
  1213. struct net *net = sock_net(sk);
  1214. switch (cmd) {
  1215. case SIOCGETMIFCNT_IN6:
  1216. if (copy_from_user(&vr, arg, sizeof(vr)))
  1217. return -EFAULT;
  1218. if (vr.mifi >= net->ipv6.maxvif)
  1219. return -EINVAL;
  1220. read_lock(&mrt_lock);
  1221. vif = &net->ipv6.vif6_table[vr.mifi];
  1222. if (MIF_EXISTS(net, vr.mifi)) {
  1223. vr.icount = vif->pkt_in;
  1224. vr.ocount = vif->pkt_out;
  1225. vr.ibytes = vif->bytes_in;
  1226. vr.obytes = vif->bytes_out;
  1227. read_unlock(&mrt_lock);
  1228. if (copy_to_user(arg, &vr, sizeof(vr)))
  1229. return -EFAULT;
  1230. return 0;
  1231. }
  1232. read_unlock(&mrt_lock);
  1233. return -EADDRNOTAVAIL;
  1234. case SIOCGETSGCNT_IN6:
  1235. if (copy_from_user(&sr, arg, sizeof(sr)))
  1236. return -EFAULT;
  1237. read_lock(&mrt_lock);
  1238. c = ip6mr_cache_find(net, &sr.src.sin6_addr, &sr.grp.sin6_addr);
  1239. if (c) {
  1240. sr.pktcnt = c->mfc_un.res.pkt;
  1241. sr.bytecnt = c->mfc_un.res.bytes;
  1242. sr.wrong_if = c->mfc_un.res.wrong_if;
  1243. read_unlock(&mrt_lock);
  1244. if (copy_to_user(arg, &sr, sizeof(sr)))
  1245. return -EFAULT;
  1246. return 0;
  1247. }
  1248. read_unlock(&mrt_lock);
  1249. return -EADDRNOTAVAIL;
  1250. default:
  1251. return -ENOIOCTLCMD;
  1252. }
  1253. }
  1254. static inline int ip6mr_forward2_finish(struct sk_buff *skb)
  1255. {
  1256. IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
  1257. IPSTATS_MIB_OUTFORWDATAGRAMS);
  1258. return dst_output(skb);
  1259. }
  1260. /*
  1261. * Processing handlers for ip6mr_forward
  1262. */
  1263. static int ip6mr_forward2(struct sk_buff *skb, struct mfc6_cache *c, int vifi)
  1264. {
  1265. struct ipv6hdr *ipv6h;
  1266. struct net *net = mfc6_net(c);
  1267. struct mif_device *vif = &net->ipv6.vif6_table[vifi];
  1268. struct net_device *dev;
  1269. struct dst_entry *dst;
  1270. struct flowi fl;
  1271. if (vif->dev == NULL)
  1272. goto out_free;
  1273. #ifdef CONFIG_IPV6_PIMSM_V2
  1274. if (vif->flags & MIFF_REGISTER) {
  1275. vif->pkt_out++;
  1276. vif->bytes_out += skb->len;
  1277. vif->dev->stats.tx_bytes += skb->len;
  1278. vif->dev->stats.tx_packets++;
  1279. ip6mr_cache_report(net, skb, vifi, MRT6MSG_WHOLEPKT);
  1280. goto out_free;
  1281. }
  1282. #endif
  1283. ipv6h = ipv6_hdr(skb);
  1284. fl = (struct flowi) {
  1285. .oif = vif->link,
  1286. .nl_u = { .ip6_u =
  1287. { .daddr = ipv6h->daddr, }
  1288. }
  1289. };
  1290. dst = ip6_route_output(net, NULL, &fl);
  1291. if (!dst)
  1292. goto out_free;
  1293. skb_dst_drop(skb);
  1294. skb_dst_set(skb, dst);
  1295. /*
  1296. * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
  1297. * not only before forwarding, but after forwarding on all output
  1298. * interfaces. It is clear, if mrouter runs a multicasting
  1299. * program, it should receive packets not depending to what interface
  1300. * program is joined.
  1301. * If we will not make it, the program will have to join on all
  1302. * interfaces. On the other hand, multihoming host (or router, but
  1303. * not mrouter) cannot join to more than one interface - it will
  1304. * result in receiving multiple packets.
  1305. */
  1306. dev = vif->dev;
  1307. skb->dev = dev;
  1308. vif->pkt_out++;
  1309. vif->bytes_out += skb->len;
  1310. /* We are about to write */
  1311. /* XXX: extension headers? */
  1312. if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
  1313. goto out_free;
  1314. ipv6h = ipv6_hdr(skb);
  1315. ipv6h->hop_limit--;
  1316. IP6CB(skb)->flags |= IP6SKB_FORWARDED;
  1317. return NF_HOOK(PF_INET6, NF_INET_FORWARD, skb, skb->dev, dev,
  1318. ip6mr_forward2_finish);
  1319. out_free:
  1320. kfree_skb(skb);
  1321. return 0;
  1322. }
  1323. static int ip6mr_find_vif(struct net_device *dev)
  1324. {
  1325. struct net *net = dev_net(dev);
  1326. int ct;
  1327. for (ct = net->ipv6.maxvif - 1; ct >= 0; ct--) {
  1328. if (net->ipv6.vif6_table[ct].dev == dev)
  1329. break;
  1330. }
  1331. return ct;
  1332. }
  1333. static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache)
  1334. {
  1335. int psend = -1;
  1336. int vif, ct;
  1337. struct net *net = mfc6_net(cache);
  1338. vif = cache->mf6c_parent;
  1339. cache->mfc_un.res.pkt++;
  1340. cache->mfc_un.res.bytes += skb->len;
  1341. /*
  1342. * Wrong interface: drop packet and (maybe) send PIM assert.
  1343. */
  1344. if (net->ipv6.vif6_table[vif].dev != skb->dev) {
  1345. int true_vifi;
  1346. cache->mfc_un.res.wrong_if++;
  1347. true_vifi = ip6mr_find_vif(skb->dev);
  1348. if (true_vifi >= 0 && net->ipv6.mroute_do_assert &&
  1349. /* pimsm uses asserts, when switching from RPT to SPT,
  1350. so that we cannot check that packet arrived on an oif.
  1351. It is bad, but otherwise we would need to move pretty
  1352. large chunk of pimd to kernel. Ough... --ANK
  1353. */
  1354. (net->ipv6.mroute_do_pim ||
  1355. cache->mfc_un.res.ttls[true_vifi] < 255) &&
  1356. time_after(jiffies,
  1357. cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
  1358. cache->mfc_un.res.last_assert = jiffies;
  1359. ip6mr_cache_report(net, skb, true_vifi, MRT6MSG_WRONGMIF);
  1360. }
  1361. goto dont_forward;
  1362. }
  1363. net->ipv6.vif6_table[vif].pkt_in++;
  1364. net->ipv6.vif6_table[vif].bytes_in += skb->len;
  1365. /*
  1366. * Forward the frame
  1367. */
  1368. for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
  1369. if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
  1370. if (psend != -1) {
  1371. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1372. if (skb2)
  1373. ip6mr_forward2(skb2, cache, psend);
  1374. }
  1375. psend = ct;
  1376. }
  1377. }
  1378. if (psend != -1) {
  1379. ip6mr_forward2(skb, cache, psend);
  1380. return 0;
  1381. }
  1382. dont_forward:
  1383. kfree_skb(skb);
  1384. return 0;
  1385. }
  1386. /*
  1387. * Multicast packets for forwarding arrive here
  1388. */
  1389. int ip6_mr_input(struct sk_buff *skb)
  1390. {
  1391. struct mfc6_cache *cache;
  1392. struct net *net = dev_net(skb->dev);
  1393. read_lock(&mrt_lock);
  1394. cache = ip6mr_cache_find(net,
  1395. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
  1396. /*
  1397. * No usable cache entry
  1398. */
  1399. if (cache == NULL) {
  1400. int vif;
  1401. vif = ip6mr_find_vif(skb->dev);
  1402. if (vif >= 0) {
  1403. int err = ip6mr_cache_unresolved(net, vif, skb);
  1404. read_unlock(&mrt_lock);
  1405. return err;
  1406. }
  1407. read_unlock(&mrt_lock);
  1408. kfree_skb(skb);
  1409. return -ENODEV;
  1410. }
  1411. ip6_mr_forward(skb, cache);
  1412. read_unlock(&mrt_lock);
  1413. return 0;
  1414. }
  1415. static int
  1416. ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm)
  1417. {
  1418. int ct;
  1419. struct rtnexthop *nhp;
  1420. struct net *net = mfc6_net(c);
  1421. struct net_device *dev = net->ipv6.vif6_table[c->mf6c_parent].dev;
  1422. u8 *b = skb_tail_pointer(skb);
  1423. struct rtattr *mp_head;
  1424. if (dev)
  1425. RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex);
  1426. mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
  1427. for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
  1428. if (c->mfc_un.res.ttls[ct] < 255) {
  1429. if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
  1430. goto rtattr_failure;
  1431. nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
  1432. nhp->rtnh_flags = 0;
  1433. nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
  1434. nhp->rtnh_ifindex = net->ipv6.vif6_table[ct].dev->ifindex;
  1435. nhp->rtnh_len = sizeof(*nhp);
  1436. }
  1437. }
  1438. mp_head->rta_type = RTA_MULTIPATH;
  1439. mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
  1440. rtm->rtm_type = RTN_MULTICAST;
  1441. return 1;
  1442. rtattr_failure:
  1443. nlmsg_trim(skb, b);
  1444. return -EMSGSIZE;
  1445. }
  1446. int ip6mr_get_route(struct net *net,
  1447. struct sk_buff *skb, struct rtmsg *rtm, int nowait)
  1448. {
  1449. int err;
  1450. struct mfc6_cache *cache;
  1451. struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
  1452. read_lock(&mrt_lock);
  1453. cache = ip6mr_cache_find(net, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
  1454. if (!cache) {
  1455. struct sk_buff *skb2;
  1456. struct ipv6hdr *iph;
  1457. struct net_device *dev;
  1458. int vif;
  1459. if (nowait) {
  1460. read_unlock(&mrt_lock);
  1461. return -EAGAIN;
  1462. }
  1463. dev = skb->dev;
  1464. if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) {
  1465. read_unlock(&mrt_lock);
  1466. return -ENODEV;
  1467. }
  1468. /* really correct? */
  1469. skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  1470. if (!skb2) {
  1471. read_unlock(&mrt_lock);
  1472. return -ENOMEM;
  1473. }
  1474. skb_reset_transport_header(skb2);
  1475. skb_put(skb2, sizeof(struct ipv6hdr));
  1476. skb_reset_network_header(skb2);
  1477. iph = ipv6_hdr(skb2);
  1478. iph->version = 0;
  1479. iph->priority = 0;
  1480. iph->flow_lbl[0] = 0;
  1481. iph->flow_lbl[1] = 0;
  1482. iph->flow_lbl[2] = 0;
  1483. iph->payload_len = 0;
  1484. iph->nexthdr = IPPROTO_NONE;
  1485. iph->hop_limit = 0;
  1486. ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
  1487. ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
  1488. err = ip6mr_cache_unresolved(net, vif, skb2);
  1489. read_unlock(&mrt_lock);
  1490. return err;
  1491. }
  1492. if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
  1493. cache->mfc_flags |= MFC_NOTIFY;
  1494. err = ip6mr_fill_mroute(skb, cache, rtm);
  1495. read_unlock(&mrt_lock);
  1496. return err;
  1497. }