ipv6.c 29 KB

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