ipv6.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /* SCTP kernel implementation
  2. * (C) Copyright IBM Corp. 2002, 2004
  3. * Copyright (c) 2001 Nokia, Inc.
  4. * Copyright (c) 2001 La Monte H.P. Yarroll
  5. * Copyright (c) 2002-2003 Intel Corp.
  6. *
  7. * This file is part of the SCTP kernel implementation
  8. *
  9. * SCTP over IPv6.
  10. *
  11. * This SCTP implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This SCTP implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, write to
  25. * the Free Software Foundation, 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, USA.
  27. *
  28. * Please send any bug reports or fixes you make to the
  29. * email address(es):
  30. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  31. *
  32. * Or submit a bug report through the following website:
  33. * http://www.sf.net/projects/lksctp
  34. *
  35. * Written or modified by:
  36. * Le Yanqun <yanqun.le@nokia.com>
  37. * Hui Huang <hui.huang@nokia.com>
  38. * La Monte H.P. Yarroll <piggy@acm.org>
  39. * Sridhar Samudrala <sri@us.ibm.com>
  40. * Jon Grimm <jgrimm@us.ibm.com>
  41. * Ardelle Fan <ardelle.fan@intel.com>
  42. *
  43. * Based on:
  44. * linux/net/ipv6/tcp_ipv6.c
  45. *
  46. * Any bugs reported given to us we will try to fix... any fixes shared will
  47. * be incorporated into the next SCTP release.
  48. */
  49. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  50. #include <linux/module.h>
  51. #include <linux/errno.h>
  52. #include <linux/types.h>
  53. #include <linux/socket.h>
  54. #include <linux/sockios.h>
  55. #include <linux/net.h>
  56. #include <linux/in.h>
  57. #include <linux/in6.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/init.h>
  60. #include <linux/ipsec.h>
  61. #include <linux/slab.h>
  62. #include <linux/ipv6.h>
  63. #include <linux/icmpv6.h>
  64. #include <linux/random.h>
  65. #include <linux/seq_file.h>
  66. #include <net/protocol.h>
  67. #include <net/ndisc.h>
  68. #include <net/ip.h>
  69. #include <net/ipv6.h>
  70. #include <net/transp_v6.h>
  71. #include <net/addrconf.h>
  72. #include <net/ip6_route.h>
  73. #include <net/inet_common.h>
  74. #include <net/inet_ecn.h>
  75. #include <net/sctp/sctp.h>
  76. #include <asm/uaccess.h>
  77. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  78. union sctp_addr *s2);
  79. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  80. __be16 port);
  81. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  82. const union sctp_addr *addr2);
  83. /* Event handler for inet6 address addition/deletion events.
  84. * The sctp_local_addr_list needs to be protocted by a spin lock since
  85. * multiple notifiers (say IPv4 and IPv6) may be running at the same
  86. * time and thus corrupt the list.
  87. * The reader side is protected with RCU.
  88. */
  89. static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
  90. void *ptr)
  91. {
  92. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  93. struct sctp_sockaddr_entry *addr = NULL;
  94. struct sctp_sockaddr_entry *temp;
  95. int found = 0;
  96. switch (ev) {
  97. case NETDEV_UP:
  98. addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
  99. if (addr) {
  100. addr->a.v6.sin6_family = AF_INET6;
  101. addr->a.v6.sin6_port = 0;
  102. addr->a.v6.sin6_addr = ifa->addr;
  103. addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
  104. addr->valid = 1;
  105. spin_lock_bh(&sctp_local_addr_lock);
  106. list_add_tail_rcu(&addr->list, &sctp_local_addr_list);
  107. sctp_addr_wq_mgmt(addr, SCTP_ADDR_NEW);
  108. spin_unlock_bh(&sctp_local_addr_lock);
  109. }
  110. break;
  111. case NETDEV_DOWN:
  112. spin_lock_bh(&sctp_local_addr_lock);
  113. list_for_each_entry_safe(addr, temp,
  114. &sctp_local_addr_list, list) {
  115. if (addr->a.sa.sa_family == AF_INET6 &&
  116. ipv6_addr_equal(&addr->a.v6.sin6_addr,
  117. &ifa->addr)) {
  118. sctp_addr_wq_mgmt(addr, SCTP_ADDR_DEL);
  119. found = 1;
  120. addr->valid = 0;
  121. list_del_rcu(&addr->list);
  122. break;
  123. }
  124. }
  125. spin_unlock_bh(&sctp_local_addr_lock);
  126. if (found)
  127. kfree_rcu(addr, rcu);
  128. break;
  129. }
  130. return NOTIFY_DONE;
  131. }
  132. static struct notifier_block sctp_inet6addr_notifier = {
  133. .notifier_call = sctp_inet6addr_event,
  134. };
  135. /* ICMP error handler. */
  136. SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  137. u8 type, u8 code, int offset, __be32 info)
  138. {
  139. struct inet6_dev *idev;
  140. struct sock *sk;
  141. struct sctp_association *asoc;
  142. struct sctp_transport *transport;
  143. struct ipv6_pinfo *np;
  144. sk_buff_data_t saveip, savesctp;
  145. int err;
  146. idev = in6_dev_get(skb->dev);
  147. /* Fix up skb to look at the embedded net header. */
  148. saveip = skb->network_header;
  149. savesctp = skb->transport_header;
  150. skb_reset_network_header(skb);
  151. skb_set_transport_header(skb, offset);
  152. sk = sctp_err_lookup(AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
  153. /* Put back, the original pointers. */
  154. skb->network_header = saveip;
  155. skb->transport_header = savesctp;
  156. if (!sk) {
  157. ICMP6_INC_STATS_BH(dev_net(skb->dev), idev, ICMP6_MIB_INERRORS);
  158. goto out;
  159. }
  160. /* Warning: The sock lock is held. Remember to call
  161. * sctp_err_finish!
  162. */
  163. switch (type) {
  164. case ICMPV6_PKT_TOOBIG:
  165. sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
  166. goto out_unlock;
  167. case ICMPV6_PARAMPROB:
  168. if (ICMPV6_UNK_NEXTHDR == code) {
  169. sctp_icmp_proto_unreachable(sk, asoc, transport);
  170. goto out_unlock;
  171. }
  172. break;
  173. default:
  174. break;
  175. }
  176. np = inet6_sk(sk);
  177. icmpv6_err_convert(type, code, &err);
  178. if (!sock_owned_by_user(sk) && np->recverr) {
  179. sk->sk_err = err;
  180. sk->sk_error_report(sk);
  181. } else { /* Only an error on timeout */
  182. sk->sk_err_soft = err;
  183. }
  184. out_unlock:
  185. sctp_err_finish(sk, asoc);
  186. out:
  187. if (likely(idev != NULL))
  188. in6_dev_put(idev);
  189. }
  190. static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport)
  191. {
  192. struct sock *sk = skb->sk;
  193. struct ipv6_pinfo *np = inet6_sk(sk);
  194. struct flowi6 *fl6 = &transport->fl.u.ip6;
  195. SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n",
  196. __func__, skb, skb->len,
  197. &fl6->saddr, &fl6->daddr);
  198. IP6_ECN_flow_xmit(sk, fl6->flowlabel);
  199. if (!(transport->param_flags & SPP_PMTUD_ENABLE))
  200. skb->local_df = 1;
  201. SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
  202. return ip6_xmit(sk, skb, fl6, np->opt, np->tclass);
  203. }
  204. /* Returns the dst cache entry for the given source and destination ip
  205. * addresses.
  206. */
  207. static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
  208. struct flowi *fl, struct sock *sk)
  209. {
  210. struct sctp_association *asoc = t->asoc;
  211. struct dst_entry *dst = NULL;
  212. struct flowi6 *fl6 = &fl->u.ip6;
  213. struct sctp_bind_addr *bp;
  214. struct ipv6_pinfo *np = inet6_sk(sk);
  215. struct sctp_sockaddr_entry *laddr;
  216. union sctp_addr *baddr = NULL;
  217. union sctp_addr *daddr = &t->ipaddr;
  218. union sctp_addr dst_saddr;
  219. struct in6_addr *final_p, final;
  220. __u8 matchlen = 0;
  221. __u8 bmatchlen;
  222. sctp_scope_t scope;
  223. memset(fl6, 0, sizeof(struct flowi6));
  224. fl6->daddr = daddr->v6.sin6_addr;
  225. fl6->fl6_dport = daddr->v6.sin6_port;
  226. fl6->flowi6_proto = IPPROTO_SCTP;
  227. if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  228. fl6->flowi6_oif = daddr->v6.sin6_scope_id;
  229. SCTP_DEBUG_PRINTK("%s: DST=%pI6 ", __func__, &fl6->daddr);
  230. if (asoc)
  231. fl6->fl6_sport = htons(asoc->base.bind_addr.port);
  232. if (saddr) {
  233. fl6->saddr = saddr->v6.sin6_addr;
  234. fl6->fl6_sport = saddr->v6.sin6_port;
  235. SCTP_DEBUG_PRINTK("SRC=%pI6 - ", &fl6->saddr);
  236. }
  237. final_p = fl6_update_dst(fl6, np->opt, &final);
  238. dst = ip6_dst_lookup_flow(sk, fl6, final_p, false);
  239. if (!asoc || saddr)
  240. goto out;
  241. bp = &asoc->base.bind_addr;
  242. scope = sctp_scope(daddr);
  243. /* ip6_dst_lookup has filled in the fl6->saddr for us. Check
  244. * to see if we can use it.
  245. */
  246. if (!IS_ERR(dst)) {
  247. /* Walk through the bind address list and look for a bind
  248. * address that matches the source address of the returned dst.
  249. */
  250. sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
  251. rcu_read_lock();
  252. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  253. if (!laddr->valid || (laddr->state != SCTP_ADDR_SRC))
  254. continue;
  255. /* Do not compare against v4 addrs */
  256. if ((laddr->a.sa.sa_family == AF_INET6) &&
  257. (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) {
  258. rcu_read_unlock();
  259. goto out;
  260. }
  261. }
  262. rcu_read_unlock();
  263. /* None of the bound addresses match the source address of the
  264. * dst. So release it.
  265. */
  266. dst_release(dst);
  267. dst = NULL;
  268. }
  269. /* Walk through the bind address list and try to get the
  270. * best source address for a given destination.
  271. */
  272. rcu_read_lock();
  273. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  274. if (!laddr->valid && laddr->state != SCTP_ADDR_SRC)
  275. continue;
  276. if ((laddr->a.sa.sa_family == AF_INET6) &&
  277. (scope <= sctp_scope(&laddr->a))) {
  278. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  279. if (!baddr || (matchlen < bmatchlen)) {
  280. baddr = &laddr->a;
  281. matchlen = bmatchlen;
  282. }
  283. }
  284. }
  285. rcu_read_unlock();
  286. if (baddr) {
  287. fl6->saddr = baddr->v6.sin6_addr;
  288. fl6->fl6_sport = baddr->v6.sin6_port;
  289. final_p = fl6_update_dst(fl6, np->opt, &final);
  290. dst = ip6_dst_lookup_flow(sk, fl6, final_p, false);
  291. }
  292. out:
  293. if (!IS_ERR(dst)) {
  294. struct rt6_info *rt;
  295. rt = (struct rt6_info *)dst;
  296. t->dst = dst;
  297. SCTP_DEBUG_PRINTK("rt6_dst:%pI6 rt6_src:%pI6\n",
  298. &rt->rt6i_dst.addr, &fl6->saddr);
  299. } else {
  300. t->dst = NULL;
  301. SCTP_DEBUG_PRINTK("NO ROUTE\n");
  302. }
  303. }
  304. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  305. * addresses.
  306. */
  307. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  308. union sctp_addr *s2)
  309. {
  310. return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
  311. }
  312. /* Fills in the source address(saddr) based on the destination address(daddr)
  313. * and asoc's bind address list.
  314. */
  315. static void sctp_v6_get_saddr(struct sctp_sock *sk,
  316. struct sctp_transport *t,
  317. struct flowi *fl)
  318. {
  319. struct flowi6 *fl6 = &fl->u.ip6;
  320. union sctp_addr *saddr = &t->saddr;
  321. SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
  322. if (t->dst) {
  323. saddr->v6.sin6_family = AF_INET6;
  324. saddr->v6.sin6_addr = fl6->saddr;
  325. }
  326. }
  327. /* Make a copy of all potential local addresses. */
  328. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  329. struct net_device *dev)
  330. {
  331. struct inet6_dev *in6_dev;
  332. struct inet6_ifaddr *ifp;
  333. struct sctp_sockaddr_entry *addr;
  334. rcu_read_lock();
  335. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  336. rcu_read_unlock();
  337. return;
  338. }
  339. read_lock_bh(&in6_dev->lock);
  340. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  341. /* Add the address to the local list. */
  342. addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
  343. if (addr) {
  344. addr->a.v6.sin6_family = AF_INET6;
  345. addr->a.v6.sin6_port = 0;
  346. addr->a.v6.sin6_addr = ifp->addr;
  347. addr->a.v6.sin6_scope_id = dev->ifindex;
  348. addr->valid = 1;
  349. INIT_LIST_HEAD(&addr->list);
  350. list_add_tail(&addr->list, addrlist);
  351. }
  352. }
  353. read_unlock_bh(&in6_dev->lock);
  354. rcu_read_unlock();
  355. }
  356. /* Initialize a sockaddr_storage from in incoming skb. */
  357. static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
  358. int is_saddr)
  359. {
  360. __be16 *port;
  361. struct sctphdr *sh;
  362. port = &addr->v6.sin6_port;
  363. addr->v6.sin6_family = AF_INET6;
  364. addr->v6.sin6_flowinfo = 0; /* FIXME */
  365. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  366. sh = sctp_hdr(skb);
  367. if (is_saddr) {
  368. *port = sh->source;
  369. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  370. } else {
  371. *port = sh->dest;
  372. addr->v6.sin6_addr = ipv6_hdr(skb)->daddr;
  373. }
  374. }
  375. /* Initialize an sctp_addr from a socket. */
  376. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  377. {
  378. addr->v6.sin6_family = AF_INET6;
  379. addr->v6.sin6_port = 0;
  380. addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
  381. }
  382. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  383. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  384. {
  385. if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
  386. inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
  387. inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
  388. inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  389. inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
  390. addr->v4.sin_addr.s_addr;
  391. } else {
  392. inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr;
  393. }
  394. }
  395. /* Initialize sk->sk_daddr from sctp_addr. */
  396. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  397. {
  398. if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
  399. inet6_sk(sk)->daddr.s6_addr32[0] = 0;
  400. inet6_sk(sk)->daddr.s6_addr32[1] = 0;
  401. inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
  402. inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  403. } else {
  404. inet6_sk(sk)->daddr = addr->v6.sin6_addr;
  405. }
  406. }
  407. /* Initialize a sctp_addr from an address parameter. */
  408. static void sctp_v6_from_addr_param(union sctp_addr *addr,
  409. union sctp_addr_param *param,
  410. __be16 port, int iif)
  411. {
  412. addr->v6.sin6_family = AF_INET6;
  413. addr->v6.sin6_port = port;
  414. addr->v6.sin6_flowinfo = 0; /* BUG */
  415. addr->v6.sin6_addr = param->v6.addr;
  416. addr->v6.sin6_scope_id = iif;
  417. }
  418. /* Initialize an address parameter from a sctp_addr and return the length
  419. * of the address parameter.
  420. */
  421. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  422. union sctp_addr_param *param)
  423. {
  424. int length = sizeof(sctp_ipv6addr_param_t);
  425. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  426. param->v6.param_hdr.length = htons(length);
  427. param->v6.addr = addr->v6.sin6_addr;
  428. return length;
  429. }
  430. /* Initialize a sctp_addr from struct in6_addr. */
  431. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  432. __be16 port)
  433. {
  434. addr->sa.sa_family = AF_INET6;
  435. addr->v6.sin6_port = port;
  436. addr->v6.sin6_addr = *saddr;
  437. }
  438. /* Compare addresses exactly.
  439. * v4-mapped-v6 is also in consideration.
  440. */
  441. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  442. const union sctp_addr *addr2)
  443. {
  444. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  445. if (addr1->sa.sa_family == AF_INET &&
  446. addr2->sa.sa_family == AF_INET6 &&
  447. ipv6_addr_v4mapped(&addr2->v6.sin6_addr)) {
  448. if (addr2->v6.sin6_port == addr1->v4.sin_port &&
  449. addr2->v6.sin6_addr.s6_addr32[3] ==
  450. addr1->v4.sin_addr.s_addr)
  451. return 1;
  452. }
  453. if (addr2->sa.sa_family == AF_INET &&
  454. addr1->sa.sa_family == AF_INET6 &&
  455. ipv6_addr_v4mapped(&addr1->v6.sin6_addr)) {
  456. if (addr1->v6.sin6_port == addr2->v4.sin_port &&
  457. addr1->v6.sin6_addr.s6_addr32[3] ==
  458. addr2->v4.sin_addr.s_addr)
  459. return 1;
  460. }
  461. return 0;
  462. }
  463. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  464. return 0;
  465. /* If this is a linklocal address, compare the scope_id. */
  466. if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  467. if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  468. (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
  469. return 0;
  470. }
  471. }
  472. return 1;
  473. }
  474. /* Initialize addr struct to INADDR_ANY. */
  475. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  476. {
  477. memset(addr, 0x00, sizeof(union sctp_addr));
  478. addr->v6.sin6_family = AF_INET6;
  479. addr->v6.sin6_port = port;
  480. }
  481. /* Is this a wildcard address? */
  482. static int sctp_v6_is_any(const union sctp_addr *addr)
  483. {
  484. return ipv6_addr_any(&addr->v6.sin6_addr);
  485. }
  486. /* Should this be available for binding? */
  487. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  488. {
  489. int type;
  490. const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
  491. type = ipv6_addr_type(in6);
  492. if (IPV6_ADDR_ANY == type)
  493. return 1;
  494. if (type == IPV6_ADDR_MAPPED) {
  495. if (sp && !sp->v4mapped)
  496. return 0;
  497. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  498. return 0;
  499. sctp_v6_map_v4(addr);
  500. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  501. }
  502. if (!(type & IPV6_ADDR_UNICAST))
  503. return 0;
  504. return ipv6_chk_addr(&init_net, in6, NULL, 0);
  505. }
  506. /* This function checks if the address is a valid address to be used for
  507. * SCTP.
  508. *
  509. * Output:
  510. * Return 0 - If the address is a non-unicast or an illegal address.
  511. * Return 1 - If the address is a unicast.
  512. */
  513. static int sctp_v6_addr_valid(union sctp_addr *addr,
  514. struct sctp_sock *sp,
  515. const struct sk_buff *skb)
  516. {
  517. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  518. /* Support v4-mapped-v6 address. */
  519. if (ret == IPV6_ADDR_MAPPED) {
  520. /* Note: This routine is used in input, so v4-mapped-v6
  521. * are disallowed here when there is no sctp_sock.
  522. */
  523. if (!sp || !sp->v4mapped)
  524. return 0;
  525. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  526. return 0;
  527. sctp_v6_map_v4(addr);
  528. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
  529. }
  530. /* Is this a non-unicast address */
  531. if (!(ret & IPV6_ADDR_UNICAST))
  532. return 0;
  533. return 1;
  534. }
  535. /* What is the scope of 'addr'? */
  536. static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
  537. {
  538. int v6scope;
  539. sctp_scope_t retval;
  540. /* The IPv6 scope is really a set of bit fields.
  541. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  542. */
  543. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  544. switch (v6scope) {
  545. case IFA_HOST:
  546. retval = SCTP_SCOPE_LOOPBACK;
  547. break;
  548. case IFA_LINK:
  549. retval = SCTP_SCOPE_LINK;
  550. break;
  551. case IFA_SITE:
  552. retval = SCTP_SCOPE_PRIVATE;
  553. break;
  554. default:
  555. retval = SCTP_SCOPE_GLOBAL;
  556. break;
  557. }
  558. return retval;
  559. }
  560. /* Create and initialize a new sk for the socket to be returned by accept(). */
  561. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  562. struct sctp_association *asoc)
  563. {
  564. struct sock *newsk;
  565. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  566. struct sctp6_sock *newsctp6sk;
  567. newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot);
  568. if (!newsk)
  569. goto out;
  570. sock_init_data(NULL, newsk);
  571. sctp_copy_sock(newsk, sk, asoc);
  572. sock_reset_flag(sk, SOCK_ZAPPED);
  573. newsctp6sk = (struct sctp6_sock *)newsk;
  574. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  575. sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
  576. newnp = inet6_sk(newsk);
  577. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  578. newnp->ipv6_mc_list = NULL;
  579. newnp->ipv6_ac_list = NULL;
  580. newnp->ipv6_fl_list = NULL;
  581. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  582. * and getpeername().
  583. */
  584. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  585. sk_refcnt_debug_inc(newsk);
  586. if (newsk->sk_prot->init(newsk)) {
  587. sk_common_release(newsk);
  588. newsk = NULL;
  589. }
  590. out:
  591. return newsk;
  592. }
  593. /* Map v4 address to mapped v6 address */
  594. static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
  595. {
  596. if (sp->v4mapped && AF_INET == addr->sa.sa_family)
  597. sctp_v4_map_v6(addr);
  598. }
  599. /* Where did this skb come from? */
  600. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  601. {
  602. struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
  603. return opt->iif;
  604. }
  605. /* Was this packet marked by Explicit Congestion Notification? */
  606. static int sctp_v6_is_ce(const struct sk_buff *skb)
  607. {
  608. return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
  609. }
  610. /* Dump the v6 addr to the seq file. */
  611. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  612. {
  613. seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
  614. }
  615. static void sctp_v6_ecn_capable(struct sock *sk)
  616. {
  617. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  618. }
  619. /* Initialize a PF_INET6 socket msg_name. */
  620. static void sctp_inet6_msgname(char *msgname, int *addr_len)
  621. {
  622. struct sockaddr_in6 *sin6;
  623. sin6 = (struct sockaddr_in6 *)msgname;
  624. sin6->sin6_family = AF_INET6;
  625. sin6->sin6_flowinfo = 0;
  626. sin6->sin6_scope_id = 0; /*FIXME */
  627. *addr_len = sizeof(struct sockaddr_in6);
  628. }
  629. /* Initialize a PF_INET msgname from a ulpevent. */
  630. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  631. char *msgname, int *addrlen)
  632. {
  633. struct sockaddr_in6 *sin6, *sin6from;
  634. if (msgname) {
  635. union sctp_addr *addr;
  636. struct sctp_association *asoc;
  637. asoc = event->asoc;
  638. sctp_inet6_msgname(msgname, addrlen);
  639. sin6 = (struct sockaddr_in6 *)msgname;
  640. sin6->sin6_port = htons(asoc->peer.port);
  641. addr = &asoc->peer.primary_addr;
  642. /* Note: If we go to a common v6 format, this code
  643. * will change.
  644. */
  645. /* Map ipv4 address into v4-mapped-on-v6 address. */
  646. if (sctp_sk(asoc->base.sk)->v4mapped &&
  647. AF_INET == addr->sa.sa_family) {
  648. sctp_v4_map_v6((union sctp_addr *)sin6);
  649. sin6->sin6_addr.s6_addr32[3] =
  650. addr->v4.sin_addr.s_addr;
  651. return;
  652. }
  653. sin6from = &asoc->peer.primary_addr.v6;
  654. sin6->sin6_addr = sin6from->sin6_addr;
  655. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  656. sin6->sin6_scope_id = sin6from->sin6_scope_id;
  657. }
  658. }
  659. /* Initialize a msg_name from an inbound skb. */
  660. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  661. int *addr_len)
  662. {
  663. struct sctphdr *sh;
  664. struct sockaddr_in6 *sin6;
  665. if (msgname) {
  666. sctp_inet6_msgname(msgname, addr_len);
  667. sin6 = (struct sockaddr_in6 *)msgname;
  668. sh = sctp_hdr(skb);
  669. sin6->sin6_port = sh->source;
  670. /* Map ipv4 address into v4-mapped-on-v6 address. */
  671. if (sctp_sk(skb->sk)->v4mapped &&
  672. ip_hdr(skb)->version == 4) {
  673. sctp_v4_map_v6((union sctp_addr *)sin6);
  674. sin6->sin6_addr.s6_addr32[3] = ip_hdr(skb)->saddr;
  675. return;
  676. }
  677. /* Otherwise, just copy the v6 address. */
  678. sin6->sin6_addr = ipv6_hdr(skb)->saddr;
  679. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  680. struct sctp_ulpevent *ev = sctp_skb2event(skb);
  681. sin6->sin6_scope_id = ev->iif;
  682. }
  683. }
  684. }
  685. /* Do we support this AF? */
  686. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  687. {
  688. switch (family) {
  689. case AF_INET6:
  690. return 1;
  691. /* v4-mapped-v6 addresses */
  692. case AF_INET:
  693. if (!__ipv6_only_sock(sctp_opt2sk(sp)))
  694. return 1;
  695. default:
  696. return 0;
  697. }
  698. }
  699. /* Address matching with wildcards allowed. This extra level
  700. * of indirection lets us choose whether a PF_INET6 should
  701. * disallow any v4 addresses if we so choose.
  702. */
  703. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  704. const union sctp_addr *addr2,
  705. struct sctp_sock *opt)
  706. {
  707. struct sctp_af *af1, *af2;
  708. struct sock *sk = sctp_opt2sk(opt);
  709. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  710. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  711. if (!af1 || !af2)
  712. return 0;
  713. /* If the socket is IPv6 only, v4 addrs will not match */
  714. if (__ipv6_only_sock(sk) && af1 != af2)
  715. return 0;
  716. /* Today, wildcard AF_INET/AF_INET6. */
  717. if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
  718. return 1;
  719. if (addr1->sa.sa_family != addr2->sa.sa_family)
  720. return 0;
  721. return af1->cmp_addr(addr1, addr2);
  722. }
  723. /* Verify that the provided sockaddr looks bindable. Common verification,
  724. * has already been taken care of.
  725. */
  726. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  727. {
  728. struct sctp_af *af;
  729. /* ASSERT: address family has already been verified. */
  730. if (addr->sa.sa_family != AF_INET6)
  731. af = sctp_get_af_specific(addr->sa.sa_family);
  732. else {
  733. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  734. struct net_device *dev;
  735. if (type & IPV6_ADDR_LINKLOCAL) {
  736. if (!addr->v6.sin6_scope_id)
  737. return 0;
  738. rcu_read_lock();
  739. dev = dev_get_by_index_rcu(&init_net,
  740. addr->v6.sin6_scope_id);
  741. if (!dev ||
  742. !ipv6_chk_addr(&init_net, &addr->v6.sin6_addr,
  743. dev, 0)) {
  744. rcu_read_unlock();
  745. return 0;
  746. }
  747. rcu_read_unlock();
  748. } else if (type == IPV6_ADDR_MAPPED) {
  749. if (!opt->v4mapped)
  750. return 0;
  751. }
  752. af = opt->pf->af;
  753. }
  754. return af->available(addr, opt);
  755. }
  756. /* Verify that the provided sockaddr looks sendable. Common verification,
  757. * has already been taken care of.
  758. */
  759. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  760. {
  761. struct sctp_af *af = NULL;
  762. /* ASSERT: address family has already been verified. */
  763. if (addr->sa.sa_family != AF_INET6)
  764. af = sctp_get_af_specific(addr->sa.sa_family);
  765. else {
  766. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  767. struct net_device *dev;
  768. if (type & IPV6_ADDR_LINKLOCAL) {
  769. if (!addr->v6.sin6_scope_id)
  770. return 0;
  771. rcu_read_lock();
  772. dev = dev_get_by_index_rcu(&init_net,
  773. addr->v6.sin6_scope_id);
  774. rcu_read_unlock();
  775. if (!dev)
  776. return 0;
  777. }
  778. af = opt->pf->af;
  779. }
  780. return af != NULL;
  781. }
  782. /* Fill in Supported Address Type information for INIT and INIT-ACK
  783. * chunks. Note: In the future, we may want to look at sock options
  784. * to determine whether a PF_INET6 socket really wants to have IPV4
  785. * addresses.
  786. * Returns number of addresses supported.
  787. */
  788. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  789. __be16 *types)
  790. {
  791. types[0] = SCTP_PARAM_IPV6_ADDRESS;
  792. if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
  793. types[1] = SCTP_PARAM_IPV4_ADDRESS;
  794. return 2;
  795. }
  796. return 1;
  797. }
  798. static const struct proto_ops inet6_seqpacket_ops = {
  799. .family = PF_INET6,
  800. .owner = THIS_MODULE,
  801. .release = inet6_release,
  802. .bind = inet6_bind,
  803. .connect = inet_dgram_connect,
  804. .socketpair = sock_no_socketpair,
  805. .accept = inet_accept,
  806. .getname = inet6_getname,
  807. .poll = sctp_poll,
  808. .ioctl = inet6_ioctl,
  809. .listen = sctp_inet_listen,
  810. .shutdown = inet_shutdown,
  811. .setsockopt = sock_common_setsockopt,
  812. .getsockopt = sock_common_getsockopt,
  813. .sendmsg = inet_sendmsg,
  814. .recvmsg = sock_common_recvmsg,
  815. .mmap = sock_no_mmap,
  816. #ifdef CONFIG_COMPAT
  817. .compat_setsockopt = compat_sock_common_setsockopt,
  818. .compat_getsockopt = compat_sock_common_getsockopt,
  819. #endif
  820. };
  821. static struct inet_protosw sctpv6_seqpacket_protosw = {
  822. .type = SOCK_SEQPACKET,
  823. .protocol = IPPROTO_SCTP,
  824. .prot = &sctpv6_prot,
  825. .ops = &inet6_seqpacket_ops,
  826. .no_check = 0,
  827. .flags = SCTP_PROTOSW_FLAG
  828. };
  829. static struct inet_protosw sctpv6_stream_protosw = {
  830. .type = SOCK_STREAM,
  831. .protocol = IPPROTO_SCTP,
  832. .prot = &sctpv6_prot,
  833. .ops = &inet6_seqpacket_ops,
  834. .no_check = 0,
  835. .flags = SCTP_PROTOSW_FLAG,
  836. };
  837. static int sctp6_rcv(struct sk_buff *skb)
  838. {
  839. return sctp_rcv(skb) ? -1 : 0;
  840. }
  841. static const struct inet6_protocol sctpv6_protocol = {
  842. .handler = sctp6_rcv,
  843. .err_handler = sctp_v6_err,
  844. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  845. };
  846. static struct sctp_af sctp_af_inet6 = {
  847. .sa_family = AF_INET6,
  848. .sctp_xmit = sctp_v6_xmit,
  849. .setsockopt = ipv6_setsockopt,
  850. .getsockopt = ipv6_getsockopt,
  851. .get_dst = sctp_v6_get_dst,
  852. .get_saddr = sctp_v6_get_saddr,
  853. .copy_addrlist = sctp_v6_copy_addrlist,
  854. .from_skb = sctp_v6_from_skb,
  855. .from_sk = sctp_v6_from_sk,
  856. .to_sk_saddr = sctp_v6_to_sk_saddr,
  857. .to_sk_daddr = sctp_v6_to_sk_daddr,
  858. .from_addr_param = sctp_v6_from_addr_param,
  859. .to_addr_param = sctp_v6_to_addr_param,
  860. .cmp_addr = sctp_v6_cmp_addr,
  861. .scope = sctp_v6_scope,
  862. .addr_valid = sctp_v6_addr_valid,
  863. .inaddr_any = sctp_v6_inaddr_any,
  864. .is_any = sctp_v6_is_any,
  865. .available = sctp_v6_available,
  866. .skb_iif = sctp_v6_skb_iif,
  867. .is_ce = sctp_v6_is_ce,
  868. .seq_dump_addr = sctp_v6_seq_dump_addr,
  869. .ecn_capable = sctp_v6_ecn_capable,
  870. .net_header_len = sizeof(struct ipv6hdr),
  871. .sockaddr_len = sizeof(struct sockaddr_in6),
  872. #ifdef CONFIG_COMPAT
  873. .compat_setsockopt = compat_ipv6_setsockopt,
  874. .compat_getsockopt = compat_ipv6_getsockopt,
  875. #endif
  876. };
  877. static struct sctp_pf sctp_pf_inet6 = {
  878. .event_msgname = sctp_inet6_event_msgname,
  879. .skb_msgname = sctp_inet6_skb_msgname,
  880. .af_supported = sctp_inet6_af_supported,
  881. .cmp_addr = sctp_inet6_cmp_addr,
  882. .bind_verify = sctp_inet6_bind_verify,
  883. .send_verify = sctp_inet6_send_verify,
  884. .supported_addrs = sctp_inet6_supported_addrs,
  885. .create_accept_sk = sctp_v6_create_accept_sk,
  886. .addr_v4map = sctp_v6_addr_v4map,
  887. .af = &sctp_af_inet6,
  888. };
  889. /* Initialize IPv6 support and register with socket layer. */
  890. void sctp_v6_pf_init(void)
  891. {
  892. /* Register the SCTP specific PF_INET6 functions. */
  893. sctp_register_pf(&sctp_pf_inet6, PF_INET6);
  894. /* Register the SCTP specific AF_INET6 functions. */
  895. sctp_register_af(&sctp_af_inet6);
  896. }
  897. void sctp_v6_pf_exit(void)
  898. {
  899. list_del(&sctp_af_inet6.list);
  900. }
  901. /* Initialize IPv6 support and register with socket layer. */
  902. int sctp_v6_protosw_init(void)
  903. {
  904. int rc;
  905. rc = proto_register(&sctpv6_prot, 1);
  906. if (rc)
  907. return rc;
  908. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  909. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  910. inet6_register_protosw(&sctpv6_stream_protosw);
  911. return 0;
  912. }
  913. void sctp_v6_protosw_exit(void)
  914. {
  915. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  916. inet6_unregister_protosw(&sctpv6_stream_protosw);
  917. proto_unregister(&sctpv6_prot);
  918. }
  919. /* Register with inet6 layer. */
  920. int sctp_v6_add_protocol(void)
  921. {
  922. /* Register notifier for inet6 address additions/deletions. */
  923. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  924. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  925. return -EAGAIN;
  926. return 0;
  927. }
  928. /* Unregister with inet6 layer. */
  929. void sctp_v6_del_protocol(void)
  930. {
  931. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  932. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  933. }