ipv6.c 29 KB

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