ipv6.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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_BH(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, asoc);
  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. pr_debug("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n", __func__, skb,
  195. skb->len, &fl6->saddr, &fl6->daddr);
  196. IP6_ECN_flow_xmit(sk, fl6->flowlabel);
  197. if (!(transport->param_flags & SPP_PMTUD_ENABLE))
  198. skb->ignore_df = 1;
  199. SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS);
  200. return ip6_xmit(sk, skb, fl6, np->opt, np->tclass);
  201. }
  202. /* Returns the dst cache entry for the given source and destination ip
  203. * addresses.
  204. */
  205. static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
  206. struct flowi *fl, struct sock *sk)
  207. {
  208. struct sctp_association *asoc = t->asoc;
  209. struct dst_entry *dst = NULL;
  210. struct flowi6 *fl6 = &fl->u.ip6;
  211. struct sctp_bind_addr *bp;
  212. struct ipv6_pinfo *np = inet6_sk(sk);
  213. struct sctp_sockaddr_entry *laddr;
  214. union sctp_addr *baddr = NULL;
  215. union sctp_addr *daddr = &t->ipaddr;
  216. union sctp_addr dst_saddr;
  217. struct in6_addr *final_p, final;
  218. __u8 matchlen = 0;
  219. __u8 bmatchlen;
  220. sctp_scope_t scope;
  221. memset(fl6, 0, sizeof(struct flowi6));
  222. fl6->daddr = daddr->v6.sin6_addr;
  223. fl6->fl6_dport = daddr->v6.sin6_port;
  224. fl6->flowi6_proto = IPPROTO_SCTP;
  225. if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  226. fl6->flowi6_oif = daddr->v6.sin6_scope_id;
  227. pr_debug("%s: dst=%pI6 ", __func__, &fl6->daddr);
  228. if (asoc)
  229. fl6->fl6_sport = htons(asoc->base.bind_addr.port);
  230. if (saddr) {
  231. fl6->saddr = saddr->v6.sin6_addr;
  232. fl6->fl6_sport = saddr->v6.sin6_port;
  233. pr_debug("src=%pI6 - ", &fl6->saddr);
  234. }
  235. final_p = fl6_update_dst(fl6, np->opt, &final);
  236. dst = ip6_dst_lookup_flow(sk, fl6, final_p);
  237. if (!asoc || saddr)
  238. goto out;
  239. bp = &asoc->base.bind_addr;
  240. scope = sctp_scope(daddr);
  241. /* ip6_dst_lookup has filled in the fl6->saddr for us. Check
  242. * to see if we can use it.
  243. */
  244. if (!IS_ERR(dst)) {
  245. /* Walk through the bind address list and look for a bind
  246. * address that matches the source address of the returned dst.
  247. */
  248. sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
  249. rcu_read_lock();
  250. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  251. if (!laddr->valid || laddr->state == SCTP_ADDR_DEL ||
  252. (laddr->state != SCTP_ADDR_SRC &&
  253. !asoc->src_out_of_asoc_ok))
  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)
  275. continue;
  276. if ((laddr->state == SCTP_ADDR_SRC) &&
  277. (laddr->a.sa.sa_family == AF_INET6) &&
  278. (scope <= sctp_scope(&laddr->a))) {
  279. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  280. if (!baddr || (matchlen < bmatchlen)) {
  281. baddr = &laddr->a;
  282. matchlen = bmatchlen;
  283. }
  284. }
  285. }
  286. rcu_read_unlock();
  287. if (baddr) {
  288. fl6->saddr = baddr->v6.sin6_addr;
  289. fl6->fl6_sport = baddr->v6.sin6_port;
  290. final_p = fl6_update_dst(fl6, np->opt, &final);
  291. dst = ip6_dst_lookup_flow(sk, fl6, final_p);
  292. }
  293. out:
  294. if (!IS_ERR_OR_NULL(dst)) {
  295. struct rt6_info *rt;
  296. rt = (struct rt6_info *)dst;
  297. t->dst = dst;
  298. t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
  299. pr_debug("rt6_dst:%pI6 rt6_src:%pI6\n", &rt->rt6i_dst.addr,
  300. &fl6->saddr);
  301. } else {
  302. t->dst = NULL;
  303. pr_debug("no route\n");
  304. }
  305. }
  306. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  307. * addresses.
  308. */
  309. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  310. union sctp_addr *s2)
  311. {
  312. return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
  313. }
  314. /* Fills in the source address(saddr) based on the destination address(daddr)
  315. * and asoc's bind address list.
  316. */
  317. static void sctp_v6_get_saddr(struct sctp_sock *sk,
  318. struct sctp_transport *t,
  319. struct flowi *fl)
  320. {
  321. struct flowi6 *fl6 = &fl->u.ip6;
  322. union sctp_addr *saddr = &t->saddr;
  323. pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
  324. if (t->dst) {
  325. saddr->v6.sin6_family = AF_INET6;
  326. saddr->v6.sin6_addr = fl6->saddr;
  327. }
  328. }
  329. /* Make a copy of all potential local addresses. */
  330. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  331. struct net_device *dev)
  332. {
  333. struct inet6_dev *in6_dev;
  334. struct inet6_ifaddr *ifp;
  335. struct sctp_sockaddr_entry *addr;
  336. rcu_read_lock();
  337. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  338. rcu_read_unlock();
  339. return;
  340. }
  341. read_lock_bh(&in6_dev->lock);
  342. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  343. /* Add the address to the local list. */
  344. addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
  345. if (addr) {
  346. addr->a.v6.sin6_family = AF_INET6;
  347. addr->a.v6.sin6_port = 0;
  348. addr->a.v6.sin6_addr = ifp->addr;
  349. addr->a.v6.sin6_scope_id = dev->ifindex;
  350. addr->valid = 1;
  351. INIT_LIST_HEAD(&addr->list);
  352. list_add_tail(&addr->list, addrlist);
  353. }
  354. }
  355. read_unlock_bh(&in6_dev->lock);
  356. rcu_read_unlock();
  357. }
  358. /* Initialize a sockaddr_storage from in incoming skb. */
  359. static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb,
  360. int is_saddr)
  361. {
  362. __be16 *port;
  363. struct sctphdr *sh;
  364. port = &addr->v6.sin6_port;
  365. addr->v6.sin6_family = AF_INET6;
  366. addr->v6.sin6_flowinfo = 0; /* FIXME */
  367. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  368. sh = sctp_hdr(skb);
  369. if (is_saddr) {
  370. *port = sh->source;
  371. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  372. } else {
  373. *port = sh->dest;
  374. addr->v6.sin6_addr = ipv6_hdr(skb)->daddr;
  375. }
  376. }
  377. /* Initialize an sctp_addr from a socket. */
  378. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  379. {
  380. addr->v6.sin6_family = AF_INET6;
  381. addr->v6.sin6_port = 0;
  382. addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
  383. }
  384. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  385. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  386. {
  387. if (addr->sa.sa_family == AF_INET) {
  388. sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
  389. sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
  390. sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  391. sk->sk_v6_rcv_saddr.s6_addr32[3] =
  392. addr->v4.sin_addr.s_addr;
  393. } else {
  394. sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
  395. }
  396. }
  397. /* Initialize sk->sk_daddr from sctp_addr. */
  398. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  399. {
  400. if (addr->sa.sa_family == AF_INET) {
  401. sk->sk_v6_daddr.s6_addr32[0] = 0;
  402. sk->sk_v6_daddr.s6_addr32[1] = 0;
  403. sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
  404. sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  405. } else {
  406. sk->sk_v6_daddr = addr->v6.sin6_addr;
  407. }
  408. }
  409. /* Initialize a sctp_addr from an address parameter. */
  410. static void sctp_v6_from_addr_param(union sctp_addr *addr,
  411. union sctp_addr_param *param,
  412. __be16 port, int iif)
  413. {
  414. addr->v6.sin6_family = AF_INET6;
  415. addr->v6.sin6_port = port;
  416. addr->v6.sin6_flowinfo = 0; /* BUG */
  417. addr->v6.sin6_addr = param->v6.addr;
  418. addr->v6.sin6_scope_id = iif;
  419. }
  420. /* Initialize an address parameter from a sctp_addr and return the length
  421. * of the address parameter.
  422. */
  423. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  424. union sctp_addr_param *param)
  425. {
  426. int length = sizeof(sctp_ipv6addr_param_t);
  427. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  428. param->v6.param_hdr.length = htons(length);
  429. param->v6.addr = addr->v6.sin6_addr;
  430. return length;
  431. }
  432. /* Initialize a sctp_addr from struct in6_addr. */
  433. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  434. __be16 port)
  435. {
  436. addr->sa.sa_family = AF_INET6;
  437. addr->v6.sin6_port = port;
  438. addr->v6.sin6_addr = *saddr;
  439. }
  440. /* Compare addresses exactly.
  441. * v4-mapped-v6 is also in consideration.
  442. */
  443. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  444. const union sctp_addr *addr2)
  445. {
  446. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  447. if (addr1->sa.sa_family == AF_INET &&
  448. addr2->sa.sa_family == AF_INET6 &&
  449. ipv6_addr_v4mapped(&addr2->v6.sin6_addr)) {
  450. if (addr2->v6.sin6_port == addr1->v4.sin_port &&
  451. addr2->v6.sin6_addr.s6_addr32[3] ==
  452. addr1->v4.sin_addr.s_addr)
  453. return 1;
  454. }
  455. if (addr2->sa.sa_family == AF_INET &&
  456. addr1->sa.sa_family == AF_INET6 &&
  457. ipv6_addr_v4mapped(&addr1->v6.sin6_addr)) {
  458. if (addr1->v6.sin6_port == addr2->v4.sin_port &&
  459. addr1->v6.sin6_addr.s6_addr32[3] ==
  460. addr2->v4.sin_addr.s_addr)
  461. return 1;
  462. }
  463. return 0;
  464. }
  465. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  466. return 0;
  467. /* If this is a linklocal address, compare the scope_id. */
  468. if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  469. if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  470. (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
  471. return 0;
  472. }
  473. }
  474. return 1;
  475. }
  476. /* Initialize addr struct to INADDR_ANY. */
  477. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  478. {
  479. memset(addr, 0x00, sizeof(union sctp_addr));
  480. addr->v6.sin6_family = AF_INET6;
  481. addr->v6.sin6_port = port;
  482. }
  483. /* Is this a wildcard address? */
  484. static int sctp_v6_is_any(const union sctp_addr *addr)
  485. {
  486. return ipv6_addr_any(&addr->v6.sin6_addr);
  487. }
  488. /* Should this be available for binding? */
  489. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  490. {
  491. int type;
  492. const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
  493. type = ipv6_addr_type(in6);
  494. if (IPV6_ADDR_ANY == type)
  495. return 1;
  496. if (type == IPV6_ADDR_MAPPED) {
  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(sock_net(&sp->inet.sk), 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 && 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 sock *newsk;
  563. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  564. struct sctp6_sock *newsctp6sk;
  565. newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot);
  566. if (!newsk)
  567. goto out;
  568. sock_init_data(NULL, newsk);
  569. sctp_copy_sock(newsk, sk, asoc);
  570. sock_reset_flag(sk, SOCK_ZAPPED);
  571. newsctp6sk = (struct sctp6_sock *)newsk;
  572. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  573. sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
  574. newnp = inet6_sk(newsk);
  575. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  576. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  577. * and getpeername().
  578. */
  579. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  580. newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  581. sk_refcnt_debug_inc(newsk);
  582. if (newsk->sk_prot->init(newsk)) {
  583. sk_common_release(newsk);
  584. newsk = NULL;
  585. }
  586. out:
  587. return newsk;
  588. }
  589. /* Format a sockaddr for return to user space. This makes sure the return is
  590. * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option.
  591. */
  592. static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
  593. {
  594. if (sp->v4mapped) {
  595. if (addr->sa.sa_family == AF_INET)
  596. sctp_v4_map_v6(addr);
  597. } else {
  598. if (addr->sa.sa_family == AF_INET6 &&
  599. ipv6_addr_v4mapped(&addr->v6.sin6_addr))
  600. sctp_v6_map_v4(addr);
  601. }
  602. if (addr->sa.sa_family == AF_INET)
  603. return sizeof(struct sockaddr_in);
  604. return sizeof(struct sockaddr_in6);
  605. }
  606. /* Where did this skb come from? */
  607. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  608. {
  609. struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
  610. return opt->iif;
  611. }
  612. /* Was this packet marked by Explicit Congestion Notification? */
  613. static int sctp_v6_is_ce(const struct sk_buff *skb)
  614. {
  615. return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
  616. }
  617. /* Dump the v6 addr to the seq file. */
  618. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  619. {
  620. seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
  621. }
  622. static void sctp_v6_ecn_capable(struct sock *sk)
  623. {
  624. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  625. }
  626. /* Initialize a PF_INET msgname from a ulpevent. */
  627. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  628. char *msgname, int *addrlen)
  629. {
  630. union sctp_addr *addr;
  631. struct sctp_association *asoc;
  632. union sctp_addr *paddr;
  633. if (!msgname)
  634. return;
  635. addr = (union sctp_addr *)msgname;
  636. asoc = event->asoc;
  637. paddr = &asoc->peer.primary_addr;
  638. if (paddr->sa.sa_family == AF_INET) {
  639. addr->v4.sin_family = AF_INET;
  640. addr->v4.sin_port = htons(asoc->peer.port);
  641. addr->v4.sin_addr = paddr->v4.sin_addr;
  642. } else {
  643. addr->v6.sin6_family = AF_INET6;
  644. addr->v6.sin6_flowinfo = 0;
  645. if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  646. addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id;
  647. else
  648. addr->v6.sin6_scope_id = 0;
  649. addr->v6.sin6_port = htons(asoc->peer.port);
  650. addr->v6.sin6_addr = paddr->v6.sin6_addr;
  651. }
  652. *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr);
  653. }
  654. /* Initialize a msg_name from an inbound skb. */
  655. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  656. int *addr_len)
  657. {
  658. union sctp_addr *addr;
  659. struct sctphdr *sh;
  660. if (!msgname)
  661. return;
  662. addr = (union sctp_addr *)msgname;
  663. sh = sctp_hdr(skb);
  664. if (ip_hdr(skb)->version == 4) {
  665. addr->v4.sin_family = AF_INET;
  666. addr->v4.sin_port = sh->source;
  667. addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr;
  668. } else {
  669. addr->v6.sin6_family = AF_INET6;
  670. addr->v6.sin6_flowinfo = 0;
  671. addr->v6.sin6_port = sh->source;
  672. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  673. if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  674. struct sctp_ulpevent *ev = sctp_skb2event(skb);
  675. addr->v6.sin6_scope_id = ev->iif;
  676. }
  677. }
  678. *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr);
  679. }
  680. /* Do we support this AF? */
  681. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  682. {
  683. switch (family) {
  684. case AF_INET6:
  685. return 1;
  686. /* v4-mapped-v6 addresses */
  687. case AF_INET:
  688. if (!__ipv6_only_sock(sctp_opt2sk(sp)))
  689. return 1;
  690. default:
  691. return 0;
  692. }
  693. }
  694. /* Address matching with wildcards allowed. This extra level
  695. * of indirection lets us choose whether a PF_INET6 should
  696. * disallow any v4 addresses if we so choose.
  697. */
  698. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  699. const union sctp_addr *addr2,
  700. struct sctp_sock *opt)
  701. {
  702. struct sctp_af *af1, *af2;
  703. struct sock *sk = sctp_opt2sk(opt);
  704. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  705. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  706. if (!af1 || !af2)
  707. return 0;
  708. /* If the socket is IPv6 only, v4 addrs will not match */
  709. if (__ipv6_only_sock(sk) && af1 != af2)
  710. return 0;
  711. /* Today, wildcard AF_INET/AF_INET6. */
  712. if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
  713. return 1;
  714. if (addr1->sa.sa_family != addr2->sa.sa_family)
  715. return 0;
  716. return af1->cmp_addr(addr1, addr2);
  717. }
  718. /* Verify that the provided sockaddr looks bindable. Common verification,
  719. * has already been taken care of.
  720. */
  721. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  722. {
  723. struct sctp_af *af;
  724. /* ASSERT: address family has already been verified. */
  725. if (addr->sa.sa_family != AF_INET6)
  726. af = sctp_get_af_specific(addr->sa.sa_family);
  727. else {
  728. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  729. struct net_device *dev;
  730. if (type & IPV6_ADDR_LINKLOCAL) {
  731. struct net *net;
  732. if (!addr->v6.sin6_scope_id)
  733. return 0;
  734. net = sock_net(&opt->inet.sk);
  735. rcu_read_lock();
  736. dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id);
  737. if (!dev ||
  738. !ipv6_chk_addr(net, &addr->v6.sin6_addr, dev, 0)) {
  739. rcu_read_unlock();
  740. return 0;
  741. }
  742. rcu_read_unlock();
  743. }
  744. af = opt->pf->af;
  745. }
  746. return af->available(addr, opt);
  747. }
  748. /* Verify that the provided sockaddr looks sendable. Common verification,
  749. * has already been taken care of.
  750. */
  751. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  752. {
  753. struct sctp_af *af = NULL;
  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. if (!addr->v6.sin6_scope_id)
  762. return 0;
  763. rcu_read_lock();
  764. dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk),
  765. addr->v6.sin6_scope_id);
  766. rcu_read_unlock();
  767. if (!dev)
  768. return 0;
  769. }
  770. af = opt->pf->af;
  771. }
  772. return af != NULL;
  773. }
  774. /* Fill in Supported Address Type information for INIT and INIT-ACK
  775. * chunks. Note: In the future, we may want to look at sock options
  776. * to determine whether a PF_INET6 socket really wants to have IPV4
  777. * addresses.
  778. * Returns number of addresses supported.
  779. */
  780. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  781. __be16 *types)
  782. {
  783. types[0] = SCTP_PARAM_IPV6_ADDRESS;
  784. if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
  785. types[1] = SCTP_PARAM_IPV4_ADDRESS;
  786. return 2;
  787. }
  788. return 1;
  789. }
  790. /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */
  791. static int sctp_getname(struct socket *sock, struct sockaddr *uaddr,
  792. int *uaddr_len, int peer)
  793. {
  794. int rc;
  795. rc = inet6_getname(sock, uaddr, uaddr_len, peer);
  796. if (rc != 0)
  797. return rc;
  798. *uaddr_len = sctp_v6_addr_to_user(sctp_sk(sock->sk),
  799. (union sctp_addr *)uaddr);
  800. return rc;
  801. }
  802. static const struct proto_ops inet6_seqpacket_ops = {
  803. .family = PF_INET6,
  804. .owner = THIS_MODULE,
  805. .release = inet6_release,
  806. .bind = inet6_bind,
  807. .connect = inet_dgram_connect,
  808. .socketpair = sock_no_socketpair,
  809. .accept = inet_accept,
  810. .getname = sctp_getname,
  811. .poll = sctp_poll,
  812. .ioctl = inet6_ioctl,
  813. .listen = sctp_inet_listen,
  814. .shutdown = inet_shutdown,
  815. .setsockopt = sock_common_setsockopt,
  816. .getsockopt = sock_common_getsockopt,
  817. .sendmsg = inet_sendmsg,
  818. .recvmsg = sock_common_recvmsg,
  819. .mmap = sock_no_mmap,
  820. #ifdef CONFIG_COMPAT
  821. .compat_setsockopt = compat_sock_common_setsockopt,
  822. .compat_getsockopt = compat_sock_common_getsockopt,
  823. #endif
  824. };
  825. static struct inet_protosw sctpv6_seqpacket_protosw = {
  826. .type = SOCK_SEQPACKET,
  827. .protocol = IPPROTO_SCTP,
  828. .prot = &sctpv6_prot,
  829. .ops = &inet6_seqpacket_ops,
  830. .flags = SCTP_PROTOSW_FLAG
  831. };
  832. static struct inet_protosw sctpv6_stream_protosw = {
  833. .type = SOCK_STREAM,
  834. .protocol = IPPROTO_SCTP,
  835. .prot = &sctpv6_prot,
  836. .ops = &inet6_seqpacket_ops,
  837. .flags = SCTP_PROTOSW_FLAG,
  838. };
  839. static int sctp6_rcv(struct sk_buff *skb)
  840. {
  841. return sctp_rcv(skb) ? -1 : 0;
  842. }
  843. static const struct inet6_protocol sctpv6_protocol = {
  844. .handler = sctp6_rcv,
  845. .err_handler = sctp_v6_err,
  846. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  847. };
  848. static struct sctp_af sctp_af_inet6 = {
  849. .sa_family = AF_INET6,
  850. .sctp_xmit = sctp_v6_xmit,
  851. .setsockopt = ipv6_setsockopt,
  852. .getsockopt = ipv6_getsockopt,
  853. .get_dst = sctp_v6_get_dst,
  854. .get_saddr = sctp_v6_get_saddr,
  855. .copy_addrlist = sctp_v6_copy_addrlist,
  856. .from_skb = sctp_v6_from_skb,
  857. .from_sk = sctp_v6_from_sk,
  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_to_user = sctp_v6_addr_to_user,
  887. .to_sk_saddr = sctp_v6_to_sk_saddr,
  888. .to_sk_daddr = sctp_v6_to_sk_daddr,
  889. .af = &sctp_af_inet6,
  890. };
  891. /* Initialize IPv6 support and register with socket layer. */
  892. void sctp_v6_pf_init(void)
  893. {
  894. /* Register the SCTP specific PF_INET6 functions. */
  895. sctp_register_pf(&sctp_pf_inet6, PF_INET6);
  896. /* Register the SCTP specific AF_INET6 functions. */
  897. sctp_register_af(&sctp_af_inet6);
  898. }
  899. void sctp_v6_pf_exit(void)
  900. {
  901. list_del(&sctp_af_inet6.list);
  902. }
  903. /* Initialize IPv6 support and register with socket layer. */
  904. int sctp_v6_protosw_init(void)
  905. {
  906. int rc;
  907. rc = proto_register(&sctpv6_prot, 1);
  908. if (rc)
  909. return rc;
  910. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  911. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  912. inet6_register_protosw(&sctpv6_stream_protosw);
  913. return 0;
  914. }
  915. void sctp_v6_protosw_exit(void)
  916. {
  917. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  918. inet6_unregister_protosw(&sctpv6_stream_protosw);
  919. proto_unregister(&sctpv6_prot);
  920. }
  921. /* Register with inet6 layer. */
  922. int sctp_v6_add_protocol(void)
  923. {
  924. /* Register notifier for inet6 address additions/deletions. */
  925. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  926. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  927. return -EAGAIN;
  928. return 0;
  929. }
  930. /* Unregister with inet6 layer. */
  931. void sctp_v6_del_protocol(void)
  932. {
  933. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  934. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  935. }