ipv6.c 29 KB

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