af_mpls.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/vmalloc.h>
  11. #include <net/ip.h>
  12. #include <net/dst.h>
  13. #include <net/sock.h>
  14. #include <net/arp.h>
  15. #include <net/ip_fib.h>
  16. #include <net/netevent.h>
  17. #include <net/netns/generic.h>
  18. #if IS_ENABLED(CONFIG_IPV6)
  19. #include <net/ipv6.h>
  20. #include <net/addrconf.h>
  21. #endif
  22. #include <net/nexthop.h>
  23. #include "internal.h"
  24. /* Maximum number of labels to look ahead at when selecting a path of
  25. * a multipath route
  26. */
  27. #define MAX_MP_SELECT_LABELS 4
  28. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  29. static int zero = 0;
  30. static int label_limit = (1 << 20) - 1;
  31. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  32. struct nlmsghdr *nlh, struct net *net, u32 portid,
  33. unsigned int nlm_flags);
  34. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  35. {
  36. struct mpls_route *rt = NULL;
  37. if (index < net->mpls.platform_labels) {
  38. struct mpls_route __rcu **platform_label =
  39. rcu_dereference(net->mpls.platform_label);
  40. rt = rcu_dereference(platform_label[index]);
  41. }
  42. return rt;
  43. }
  44. static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
  45. {
  46. return rcu_dereference_rtnl(dev->mpls_ptr);
  47. }
  48. bool mpls_output_possible(const struct net_device *dev)
  49. {
  50. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  51. }
  52. EXPORT_SYMBOL_GPL(mpls_output_possible);
  53. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  54. {
  55. u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
  56. int nh_index = nh - rt->rt_nh;
  57. return nh0_via + rt->rt_max_alen * nh_index;
  58. }
  59. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  60. const struct mpls_nh *nh)
  61. {
  62. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  63. }
  64. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  65. {
  66. /* The size of the layer 2.5 labels to be added for this route */
  67. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  68. }
  69. unsigned int mpls_dev_mtu(const struct net_device *dev)
  70. {
  71. /* The amount of data the layer 2 frame can hold */
  72. return dev->mtu;
  73. }
  74. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  75. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  76. {
  77. if (skb->len <= mtu)
  78. return false;
  79. if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
  80. return false;
  81. return true;
  82. }
  83. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  84. static u32 mpls_multipath_hash(struct mpls_route *rt,
  85. struct sk_buff *skb, bool bos)
  86. {
  87. struct mpls_entry_decoded dec;
  88. struct mpls_shim_hdr *hdr;
  89. bool eli_seen = false;
  90. int label_index;
  91. u32 hash = 0;
  92. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
  93. label_index++) {
  94. if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
  95. break;
  96. /* Read and decode the current label */
  97. hdr = mpls_hdr(skb) + label_index;
  98. dec = mpls_entry_decode(hdr);
  99. /* RFC6790 - reserved labels MUST NOT be used as keys
  100. * for the load-balancing function
  101. */
  102. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  103. hash = jhash_1word(dec.label, hash);
  104. /* The entropy label follows the entropy label
  105. * indicator, so this means that the entropy
  106. * label was just added to the hash - no need to
  107. * go any deeper either in the label stack or in the
  108. * payload
  109. */
  110. if (eli_seen)
  111. break;
  112. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  113. eli_seen = true;
  114. }
  115. bos = dec.bos;
  116. if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
  117. sizeof(struct iphdr))) {
  118. const struct iphdr *v4hdr;
  119. v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
  120. label_index);
  121. if (v4hdr->version == 4) {
  122. hash = jhash_3words(ntohl(v4hdr->saddr),
  123. ntohl(v4hdr->daddr),
  124. v4hdr->protocol, hash);
  125. } else if (v4hdr->version == 6 &&
  126. pskb_may_pull(skb, sizeof(*hdr) * label_index +
  127. sizeof(struct ipv6hdr))) {
  128. const struct ipv6hdr *v6hdr;
  129. v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
  130. label_index);
  131. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  132. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  133. hash = jhash_1word(v6hdr->nexthdr, hash);
  134. }
  135. }
  136. }
  137. return hash;
  138. }
  139. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  140. struct sk_buff *skb, bool bos)
  141. {
  142. int alive = ACCESS_ONCE(rt->rt_nhn_alive);
  143. u32 hash = 0;
  144. int nh_index = 0;
  145. int n = 0;
  146. /* No need to look further into packet if there's only
  147. * one path
  148. */
  149. if (rt->rt_nhn == 1)
  150. goto out;
  151. if (alive <= 0)
  152. return NULL;
  153. hash = mpls_multipath_hash(rt, skb, bos);
  154. nh_index = hash % alive;
  155. if (alive == rt->rt_nhn)
  156. goto out;
  157. for_nexthops(rt) {
  158. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  159. continue;
  160. if (n == nh_index)
  161. return nh;
  162. n++;
  163. } endfor_nexthops(rt);
  164. out:
  165. return &rt->rt_nh[nh_index];
  166. }
  167. static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
  168. struct mpls_entry_decoded dec)
  169. {
  170. enum mpls_payload_type payload_type;
  171. bool success = false;
  172. /* The IPv4 code below accesses through the IPv4 header
  173. * checksum, which is 12 bytes into the packet.
  174. * The IPv6 code below accesses through the IPv6 hop limit
  175. * which is 8 bytes into the packet.
  176. *
  177. * For all supported cases there should always be at least 12
  178. * bytes of packet data present. The IPv4 header is 20 bytes
  179. * without options and the IPv6 header is always 40 bytes
  180. * long.
  181. */
  182. if (!pskb_may_pull(skb, 12))
  183. return false;
  184. payload_type = rt->rt_payload_type;
  185. if (payload_type == MPT_UNSPEC)
  186. payload_type = ip_hdr(skb)->version;
  187. switch (payload_type) {
  188. case MPT_IPV4: {
  189. struct iphdr *hdr4 = ip_hdr(skb);
  190. skb->protocol = htons(ETH_P_IP);
  191. csum_replace2(&hdr4->check,
  192. htons(hdr4->ttl << 8),
  193. htons(dec.ttl << 8));
  194. hdr4->ttl = dec.ttl;
  195. success = true;
  196. break;
  197. }
  198. case MPT_IPV6: {
  199. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  200. skb->protocol = htons(ETH_P_IPV6);
  201. hdr6->hop_limit = dec.ttl;
  202. success = true;
  203. break;
  204. }
  205. case MPT_UNSPEC:
  206. break;
  207. }
  208. return success;
  209. }
  210. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  211. struct packet_type *pt, struct net_device *orig_dev)
  212. {
  213. struct net *net = dev_net(dev);
  214. struct mpls_shim_hdr *hdr;
  215. struct mpls_route *rt;
  216. struct mpls_nh *nh;
  217. struct mpls_entry_decoded dec;
  218. struct net_device *out_dev;
  219. struct mpls_dev *mdev;
  220. unsigned int hh_len;
  221. unsigned int new_header_size;
  222. unsigned int mtu;
  223. int err;
  224. /* Careful this entire function runs inside of an rcu critical section */
  225. mdev = mpls_dev_get(dev);
  226. if (!mdev || !mdev->input_enabled)
  227. goto drop;
  228. if (skb->pkt_type != PACKET_HOST)
  229. goto drop;
  230. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  231. goto drop;
  232. if (!pskb_may_pull(skb, sizeof(*hdr)))
  233. goto drop;
  234. /* Read and decode the label */
  235. hdr = mpls_hdr(skb);
  236. dec = mpls_entry_decode(hdr);
  237. /* Pop the label */
  238. skb_pull(skb, sizeof(*hdr));
  239. skb_reset_network_header(skb);
  240. skb_orphan(skb);
  241. rt = mpls_route_input_rcu(net, dec.label);
  242. if (!rt)
  243. goto drop;
  244. nh = mpls_select_multipath(rt, skb, dec.bos);
  245. if (!nh)
  246. goto drop;
  247. /* Find the output device */
  248. out_dev = rcu_dereference(nh->nh_dev);
  249. if (!mpls_output_possible(out_dev))
  250. goto drop;
  251. if (skb_warn_if_lro(skb))
  252. goto drop;
  253. skb_forward_csum(skb);
  254. /* Verify ttl is valid */
  255. if (dec.ttl <= 1)
  256. goto drop;
  257. dec.ttl -= 1;
  258. /* Verify the destination can hold the packet */
  259. new_header_size = mpls_nh_header_size(nh);
  260. mtu = mpls_dev_mtu(out_dev);
  261. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  262. goto drop;
  263. hh_len = LL_RESERVED_SPACE(out_dev);
  264. if (!out_dev->header_ops)
  265. hh_len = 0;
  266. /* Ensure there is enough space for the headers in the skb */
  267. if (skb_cow(skb, hh_len + new_header_size))
  268. goto drop;
  269. skb->dev = out_dev;
  270. skb->protocol = htons(ETH_P_MPLS_UC);
  271. if (unlikely(!new_header_size && dec.bos)) {
  272. /* Penultimate hop popping */
  273. if (!mpls_egress(rt, skb, dec))
  274. goto drop;
  275. } else {
  276. bool bos;
  277. int i;
  278. skb_push(skb, new_header_size);
  279. skb_reset_network_header(skb);
  280. /* Push the new labels */
  281. hdr = mpls_hdr(skb);
  282. bos = dec.bos;
  283. for (i = nh->nh_labels - 1; i >= 0; i--) {
  284. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  285. dec.ttl, 0, bos);
  286. bos = false;
  287. }
  288. }
  289. /* If via wasn't specified then send out using device address */
  290. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  291. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  292. out_dev->dev_addr, skb);
  293. else
  294. err = neigh_xmit(nh->nh_via_table, out_dev,
  295. mpls_nh_via(rt, nh), skb);
  296. if (err)
  297. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  298. __func__, err);
  299. return 0;
  300. drop:
  301. kfree_skb(skb);
  302. return NET_RX_DROP;
  303. }
  304. static struct packet_type mpls_packet_type __read_mostly = {
  305. .type = cpu_to_be16(ETH_P_MPLS_UC),
  306. .func = mpls_forward,
  307. };
  308. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  309. [RTA_DST] = { .type = NLA_U32 },
  310. [RTA_OIF] = { .type = NLA_U32 },
  311. };
  312. struct mpls_route_config {
  313. u32 rc_protocol;
  314. u32 rc_ifindex;
  315. u8 rc_via_table;
  316. u8 rc_via_alen;
  317. u8 rc_via[MAX_VIA_ALEN];
  318. u32 rc_label;
  319. u8 rc_output_labels;
  320. u32 rc_output_label[MAX_NEW_LABELS];
  321. u32 rc_nlflags;
  322. enum mpls_payload_type rc_payload_type;
  323. struct nl_info rc_nlinfo;
  324. struct rtnexthop *rc_mp;
  325. int rc_mp_len;
  326. };
  327. static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
  328. {
  329. u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
  330. struct mpls_route *rt;
  331. rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
  332. VIA_ALEN_ALIGN) +
  333. num_nh * max_alen_aligned,
  334. GFP_KERNEL);
  335. if (rt) {
  336. rt->rt_nhn = num_nh;
  337. rt->rt_nhn_alive = num_nh;
  338. rt->rt_max_alen = max_alen_aligned;
  339. }
  340. return rt;
  341. }
  342. static void mpls_rt_free(struct mpls_route *rt)
  343. {
  344. if (rt)
  345. kfree_rcu(rt, rt_rcu);
  346. }
  347. static void mpls_notify_route(struct net *net, unsigned index,
  348. struct mpls_route *old, struct mpls_route *new,
  349. const struct nl_info *info)
  350. {
  351. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  352. unsigned portid = info ? info->portid : 0;
  353. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  354. struct mpls_route *rt = new ? new : old;
  355. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  356. /* Ignore reserved labels for now */
  357. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  358. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  359. }
  360. static void mpls_route_update(struct net *net, unsigned index,
  361. struct mpls_route *new,
  362. const struct nl_info *info)
  363. {
  364. struct mpls_route __rcu **platform_label;
  365. struct mpls_route *rt;
  366. ASSERT_RTNL();
  367. platform_label = rtnl_dereference(net->mpls.platform_label);
  368. rt = rtnl_dereference(platform_label[index]);
  369. rcu_assign_pointer(platform_label[index], new);
  370. mpls_notify_route(net, index, rt, new, info);
  371. /* If we removed a route free it now */
  372. mpls_rt_free(rt);
  373. }
  374. static unsigned find_free_label(struct net *net)
  375. {
  376. struct mpls_route __rcu **platform_label;
  377. size_t platform_labels;
  378. unsigned index;
  379. platform_label = rtnl_dereference(net->mpls.platform_label);
  380. platform_labels = net->mpls.platform_labels;
  381. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  382. index++) {
  383. if (!rtnl_dereference(platform_label[index]))
  384. return index;
  385. }
  386. return LABEL_NOT_SPECIFIED;
  387. }
  388. #if IS_ENABLED(CONFIG_INET)
  389. static struct net_device *inet_fib_lookup_dev(struct net *net,
  390. const void *addr)
  391. {
  392. struct net_device *dev;
  393. struct rtable *rt;
  394. struct in_addr daddr;
  395. memcpy(&daddr, addr, sizeof(struct in_addr));
  396. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  397. if (IS_ERR(rt))
  398. return ERR_CAST(rt);
  399. dev = rt->dst.dev;
  400. dev_hold(dev);
  401. ip_rt_put(rt);
  402. return dev;
  403. }
  404. #else
  405. static struct net_device *inet_fib_lookup_dev(struct net *net,
  406. const void *addr)
  407. {
  408. return ERR_PTR(-EAFNOSUPPORT);
  409. }
  410. #endif
  411. #if IS_ENABLED(CONFIG_IPV6)
  412. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  413. const void *addr)
  414. {
  415. struct net_device *dev;
  416. struct dst_entry *dst;
  417. struct flowi6 fl6;
  418. int err;
  419. if (!ipv6_stub)
  420. return ERR_PTR(-EAFNOSUPPORT);
  421. memset(&fl6, 0, sizeof(fl6));
  422. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  423. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  424. if (err)
  425. return ERR_PTR(err);
  426. dev = dst->dev;
  427. dev_hold(dev);
  428. dst_release(dst);
  429. return dev;
  430. }
  431. #else
  432. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  433. const void *addr)
  434. {
  435. return ERR_PTR(-EAFNOSUPPORT);
  436. }
  437. #endif
  438. static struct net_device *find_outdev(struct net *net,
  439. struct mpls_route *rt,
  440. struct mpls_nh *nh, int oif)
  441. {
  442. struct net_device *dev = NULL;
  443. if (!oif) {
  444. switch (nh->nh_via_table) {
  445. case NEIGH_ARP_TABLE:
  446. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  447. break;
  448. case NEIGH_ND_TABLE:
  449. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  450. break;
  451. case NEIGH_LINK_TABLE:
  452. break;
  453. }
  454. } else {
  455. dev = dev_get_by_index(net, oif);
  456. }
  457. if (!dev)
  458. return ERR_PTR(-ENODEV);
  459. if (IS_ERR(dev))
  460. return dev;
  461. /* The caller is holding rtnl anyways, so release the dev reference */
  462. dev_put(dev);
  463. return dev;
  464. }
  465. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  466. struct mpls_nh *nh, int oif)
  467. {
  468. struct net_device *dev = NULL;
  469. int err = -ENODEV;
  470. dev = find_outdev(net, rt, nh, oif);
  471. if (IS_ERR(dev)) {
  472. err = PTR_ERR(dev);
  473. dev = NULL;
  474. goto errout;
  475. }
  476. /* Ensure this is a supported device */
  477. err = -EINVAL;
  478. if (!mpls_dev_get(dev))
  479. goto errout;
  480. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  481. (dev->addr_len != nh->nh_via_alen))
  482. goto errout;
  483. RCU_INIT_POINTER(nh->nh_dev, dev);
  484. if (!(dev->flags & IFF_UP)) {
  485. nh->nh_flags |= RTNH_F_DEAD;
  486. } else {
  487. unsigned int flags;
  488. flags = dev_get_flags(dev);
  489. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  490. nh->nh_flags |= RTNH_F_LINKDOWN;
  491. }
  492. return 0;
  493. errout:
  494. return err;
  495. }
  496. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  497. struct mpls_route *rt)
  498. {
  499. struct net *net = cfg->rc_nlinfo.nl_net;
  500. struct mpls_nh *nh = rt->rt_nh;
  501. int err;
  502. int i;
  503. if (!nh)
  504. return -ENOMEM;
  505. err = -EINVAL;
  506. /* Ensure only a supported number of labels are present */
  507. if (cfg->rc_output_labels > MAX_NEW_LABELS)
  508. goto errout;
  509. nh->nh_labels = cfg->rc_output_labels;
  510. for (i = 0; i < nh->nh_labels; i++)
  511. nh->nh_label[i] = cfg->rc_output_label[i];
  512. nh->nh_via_table = cfg->rc_via_table;
  513. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  514. nh->nh_via_alen = cfg->rc_via_alen;
  515. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  516. if (err)
  517. goto errout;
  518. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  519. rt->rt_nhn_alive--;
  520. return 0;
  521. errout:
  522. return err;
  523. }
  524. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  525. struct mpls_nh *nh, int oif, struct nlattr *via,
  526. struct nlattr *newdst)
  527. {
  528. int err = -ENOMEM;
  529. if (!nh)
  530. goto errout;
  531. if (newdst) {
  532. err = nla_get_labels(newdst, MAX_NEW_LABELS,
  533. &nh->nh_labels, nh->nh_label);
  534. if (err)
  535. goto errout;
  536. }
  537. if (via) {
  538. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  539. __mpls_nh_via(rt, nh));
  540. if (err)
  541. goto errout;
  542. } else {
  543. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  544. }
  545. err = mpls_nh_assign_dev(net, rt, nh, oif);
  546. if (err)
  547. goto errout;
  548. return 0;
  549. errout:
  550. return err;
  551. }
  552. static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  553. u8 cfg_via_alen, u8 *max_via_alen)
  554. {
  555. int nhs = 0;
  556. int remaining = len;
  557. if (!rtnh) {
  558. *max_via_alen = cfg_via_alen;
  559. return 1;
  560. }
  561. *max_via_alen = 0;
  562. while (rtnh_ok(rtnh, remaining)) {
  563. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  564. int attrlen;
  565. attrlen = rtnh_attrlen(rtnh);
  566. nla = nla_find(attrs, attrlen, RTA_VIA);
  567. if (nla && nla_len(nla) >=
  568. offsetof(struct rtvia, rtvia_addr)) {
  569. int via_alen = nla_len(nla) -
  570. offsetof(struct rtvia, rtvia_addr);
  571. if (via_alen <= MAX_VIA_ALEN)
  572. *max_via_alen = max_t(u16, *max_via_alen,
  573. via_alen);
  574. }
  575. nhs++;
  576. rtnh = rtnh_next(rtnh, &remaining);
  577. }
  578. /* leftover implies invalid nexthop configuration, discard it */
  579. return remaining > 0 ? 0 : nhs;
  580. }
  581. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  582. struct mpls_route *rt)
  583. {
  584. struct rtnexthop *rtnh = cfg->rc_mp;
  585. struct nlattr *nla_via, *nla_newdst;
  586. int remaining = cfg->rc_mp_len;
  587. int nhs = 0;
  588. int err = 0;
  589. change_nexthops(rt) {
  590. int attrlen;
  591. nla_via = NULL;
  592. nla_newdst = NULL;
  593. err = -EINVAL;
  594. if (!rtnh_ok(rtnh, remaining))
  595. goto errout;
  596. /* neither weighted multipath nor any flags
  597. * are supported
  598. */
  599. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  600. goto errout;
  601. attrlen = rtnh_attrlen(rtnh);
  602. if (attrlen > 0) {
  603. struct nlattr *attrs = rtnh_attrs(rtnh);
  604. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  605. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  606. }
  607. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  608. rtnh->rtnh_ifindex, nla_via, nla_newdst);
  609. if (err)
  610. goto errout;
  611. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  612. rt->rt_nhn_alive--;
  613. rtnh = rtnh_next(rtnh, &remaining);
  614. nhs++;
  615. } endfor_nexthops(rt);
  616. rt->rt_nhn = nhs;
  617. return 0;
  618. errout:
  619. return err;
  620. }
  621. static int mpls_route_add(struct mpls_route_config *cfg)
  622. {
  623. struct mpls_route __rcu **platform_label;
  624. struct net *net = cfg->rc_nlinfo.nl_net;
  625. struct mpls_route *rt, *old;
  626. int err = -EINVAL;
  627. u8 max_via_alen;
  628. unsigned index;
  629. int nhs;
  630. index = cfg->rc_label;
  631. /* If a label was not specified during insert pick one */
  632. if ((index == LABEL_NOT_SPECIFIED) &&
  633. (cfg->rc_nlflags & NLM_F_CREATE)) {
  634. index = find_free_label(net);
  635. }
  636. /* Reserved labels may not be set */
  637. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  638. goto errout;
  639. /* The full 20 bit range may not be supported. */
  640. if (index >= net->mpls.platform_labels)
  641. goto errout;
  642. /* Append makes no sense with mpls */
  643. err = -EOPNOTSUPP;
  644. if (cfg->rc_nlflags & NLM_F_APPEND)
  645. goto errout;
  646. err = -EEXIST;
  647. platform_label = rtnl_dereference(net->mpls.platform_label);
  648. old = rtnl_dereference(platform_label[index]);
  649. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  650. goto errout;
  651. err = -EEXIST;
  652. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  653. goto errout;
  654. err = -ENOENT;
  655. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  656. goto errout;
  657. err = -EINVAL;
  658. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  659. cfg->rc_via_alen, &max_via_alen);
  660. if (nhs == 0)
  661. goto errout;
  662. err = -ENOMEM;
  663. rt = mpls_rt_alloc(nhs, max_via_alen);
  664. if (!rt)
  665. goto errout;
  666. rt->rt_protocol = cfg->rc_protocol;
  667. rt->rt_payload_type = cfg->rc_payload_type;
  668. if (cfg->rc_mp)
  669. err = mpls_nh_build_multi(cfg, rt);
  670. else
  671. err = mpls_nh_build_from_cfg(cfg, rt);
  672. if (err)
  673. goto freert;
  674. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  675. return 0;
  676. freert:
  677. mpls_rt_free(rt);
  678. errout:
  679. return err;
  680. }
  681. static int mpls_route_del(struct mpls_route_config *cfg)
  682. {
  683. struct net *net = cfg->rc_nlinfo.nl_net;
  684. unsigned index;
  685. int err = -EINVAL;
  686. index = cfg->rc_label;
  687. /* Reserved labels may not be removed */
  688. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  689. goto errout;
  690. /* The full 20 bit range may not be supported */
  691. if (index >= net->mpls.platform_labels)
  692. goto errout;
  693. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  694. err = 0;
  695. errout:
  696. return err;
  697. }
  698. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  699. (&((struct mpls_dev *)0)->field)
  700. static const struct ctl_table mpls_dev_table[] = {
  701. {
  702. .procname = "input",
  703. .maxlen = sizeof(int),
  704. .mode = 0644,
  705. .proc_handler = proc_dointvec,
  706. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  707. },
  708. { }
  709. };
  710. static int mpls_dev_sysctl_register(struct net_device *dev,
  711. struct mpls_dev *mdev)
  712. {
  713. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  714. struct ctl_table *table;
  715. int i;
  716. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  717. if (!table)
  718. goto out;
  719. /* Table data contains only offsets relative to the base of
  720. * the mdev at this point, so make them absolute.
  721. */
  722. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
  723. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  724. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  725. mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
  726. if (!mdev->sysctl)
  727. goto free;
  728. return 0;
  729. free:
  730. kfree(table);
  731. out:
  732. return -ENOBUFS;
  733. }
  734. static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
  735. {
  736. struct ctl_table *table;
  737. table = mdev->sysctl->ctl_table_arg;
  738. unregister_net_sysctl_table(mdev->sysctl);
  739. kfree(table);
  740. }
  741. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  742. {
  743. struct mpls_dev *mdev;
  744. int err = -ENOMEM;
  745. ASSERT_RTNL();
  746. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  747. if (!mdev)
  748. return ERR_PTR(err);
  749. err = mpls_dev_sysctl_register(dev, mdev);
  750. if (err)
  751. goto free;
  752. rcu_assign_pointer(dev->mpls_ptr, mdev);
  753. return mdev;
  754. free:
  755. kfree(mdev);
  756. return ERR_PTR(err);
  757. }
  758. static void mpls_ifdown(struct net_device *dev, int event)
  759. {
  760. struct mpls_route __rcu **platform_label;
  761. struct net *net = dev_net(dev);
  762. unsigned index;
  763. platform_label = rtnl_dereference(net->mpls.platform_label);
  764. for (index = 0; index < net->mpls.platform_labels; index++) {
  765. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  766. if (!rt)
  767. continue;
  768. change_nexthops(rt) {
  769. if (rtnl_dereference(nh->nh_dev) != dev)
  770. continue;
  771. switch (event) {
  772. case NETDEV_DOWN:
  773. case NETDEV_UNREGISTER:
  774. nh->nh_flags |= RTNH_F_DEAD;
  775. /* fall through */
  776. case NETDEV_CHANGE:
  777. nh->nh_flags |= RTNH_F_LINKDOWN;
  778. ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1;
  779. break;
  780. }
  781. if (event == NETDEV_UNREGISTER)
  782. RCU_INIT_POINTER(nh->nh_dev, NULL);
  783. } endfor_nexthops(rt);
  784. }
  785. return;
  786. }
  787. static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
  788. {
  789. struct mpls_route __rcu **platform_label;
  790. struct net *net = dev_net(dev);
  791. unsigned index;
  792. int alive;
  793. platform_label = rtnl_dereference(net->mpls.platform_label);
  794. for (index = 0; index < net->mpls.platform_labels; index++) {
  795. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  796. if (!rt)
  797. continue;
  798. alive = 0;
  799. change_nexthops(rt) {
  800. struct net_device *nh_dev =
  801. rtnl_dereference(nh->nh_dev);
  802. if (!(nh->nh_flags & nh_flags)) {
  803. alive++;
  804. continue;
  805. }
  806. if (nh_dev != dev)
  807. continue;
  808. alive++;
  809. nh->nh_flags &= ~nh_flags;
  810. } endfor_nexthops(rt);
  811. ACCESS_ONCE(rt->rt_nhn_alive) = alive;
  812. }
  813. return;
  814. }
  815. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  816. void *ptr)
  817. {
  818. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  819. struct mpls_dev *mdev;
  820. unsigned int flags;
  821. if (event == NETDEV_REGISTER) {
  822. /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
  823. if (dev->type == ARPHRD_ETHER ||
  824. dev->type == ARPHRD_LOOPBACK ||
  825. dev->type == ARPHRD_IPGRE ||
  826. dev->type == ARPHRD_SIT ||
  827. dev->type == ARPHRD_TUNNEL) {
  828. mdev = mpls_add_dev(dev);
  829. if (IS_ERR(mdev))
  830. return notifier_from_errno(PTR_ERR(mdev));
  831. }
  832. return NOTIFY_OK;
  833. }
  834. mdev = mpls_dev_get(dev);
  835. if (!mdev)
  836. return NOTIFY_OK;
  837. switch (event) {
  838. case NETDEV_DOWN:
  839. mpls_ifdown(dev, event);
  840. break;
  841. case NETDEV_UP:
  842. flags = dev_get_flags(dev);
  843. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  844. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  845. else
  846. mpls_ifup(dev, RTNH_F_DEAD);
  847. break;
  848. case NETDEV_CHANGE:
  849. flags = dev_get_flags(dev);
  850. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  851. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  852. else
  853. mpls_ifdown(dev, event);
  854. break;
  855. case NETDEV_UNREGISTER:
  856. mpls_ifdown(dev, event);
  857. mdev = mpls_dev_get(dev);
  858. if (mdev) {
  859. mpls_dev_sysctl_unregister(mdev);
  860. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  861. kfree_rcu(mdev, rcu);
  862. }
  863. break;
  864. case NETDEV_CHANGENAME:
  865. mdev = mpls_dev_get(dev);
  866. if (mdev) {
  867. int err;
  868. mpls_dev_sysctl_unregister(mdev);
  869. err = mpls_dev_sysctl_register(dev, mdev);
  870. if (err)
  871. return notifier_from_errno(err);
  872. }
  873. break;
  874. }
  875. return NOTIFY_OK;
  876. }
  877. static struct notifier_block mpls_dev_notifier = {
  878. .notifier_call = mpls_dev_notify,
  879. };
  880. static int nla_put_via(struct sk_buff *skb,
  881. u8 table, const void *addr, int alen)
  882. {
  883. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  884. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  885. };
  886. struct nlattr *nla;
  887. struct rtvia *via;
  888. int family = AF_UNSPEC;
  889. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  890. if (!nla)
  891. return -EMSGSIZE;
  892. if (table <= NEIGH_NR_TABLES)
  893. family = table_to_family[table];
  894. via = nla_data(nla);
  895. via->rtvia_family = family;
  896. memcpy(via->rtvia_addr, addr, alen);
  897. return 0;
  898. }
  899. int nla_put_labels(struct sk_buff *skb, int attrtype,
  900. u8 labels, const u32 label[])
  901. {
  902. struct nlattr *nla;
  903. struct mpls_shim_hdr *nla_label;
  904. bool bos;
  905. int i;
  906. nla = nla_reserve(skb, attrtype, labels*4);
  907. if (!nla)
  908. return -EMSGSIZE;
  909. nla_label = nla_data(nla);
  910. bos = true;
  911. for (i = labels - 1; i >= 0; i--) {
  912. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  913. bos = false;
  914. }
  915. return 0;
  916. }
  917. EXPORT_SYMBOL_GPL(nla_put_labels);
  918. int nla_get_labels(const struct nlattr *nla,
  919. u32 max_labels, u8 *labels, u32 label[])
  920. {
  921. unsigned len = nla_len(nla);
  922. unsigned nla_labels;
  923. struct mpls_shim_hdr *nla_label;
  924. bool bos;
  925. int i;
  926. /* len needs to be an even multiple of 4 (the label size) */
  927. if (len & 3)
  928. return -EINVAL;
  929. /* Limit the number of new labels allowed */
  930. nla_labels = len/4;
  931. if (nla_labels > max_labels)
  932. return -EINVAL;
  933. nla_label = nla_data(nla);
  934. bos = true;
  935. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  936. struct mpls_entry_decoded dec;
  937. dec = mpls_entry_decode(nla_label + i);
  938. /* Ensure the bottom of stack flag is properly set
  939. * and ttl and tc are both clear.
  940. */
  941. if ((dec.bos != bos) || dec.ttl || dec.tc)
  942. return -EINVAL;
  943. switch (dec.label) {
  944. case MPLS_LABEL_IMPLNULL:
  945. /* RFC3032: This is a label that an LSR may
  946. * assign and distribute, but which never
  947. * actually appears in the encapsulation.
  948. */
  949. return -EINVAL;
  950. }
  951. label[i] = dec.label;
  952. }
  953. *labels = nla_labels;
  954. return 0;
  955. }
  956. EXPORT_SYMBOL_GPL(nla_get_labels);
  957. int nla_get_via(const struct nlattr *nla, u8 *via_alen,
  958. u8 *via_table, u8 via_addr[])
  959. {
  960. struct rtvia *via = nla_data(nla);
  961. int err = -EINVAL;
  962. int alen;
  963. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  964. goto errout;
  965. alen = nla_len(nla) -
  966. offsetof(struct rtvia, rtvia_addr);
  967. if (alen > MAX_VIA_ALEN)
  968. goto errout;
  969. /* Validate the address family */
  970. switch (via->rtvia_family) {
  971. case AF_PACKET:
  972. *via_table = NEIGH_LINK_TABLE;
  973. break;
  974. case AF_INET:
  975. *via_table = NEIGH_ARP_TABLE;
  976. if (alen != 4)
  977. goto errout;
  978. break;
  979. case AF_INET6:
  980. *via_table = NEIGH_ND_TABLE;
  981. if (alen != 16)
  982. goto errout;
  983. break;
  984. default:
  985. /* Unsupported address family */
  986. goto errout;
  987. }
  988. memcpy(via_addr, via->rtvia_addr, alen);
  989. *via_alen = alen;
  990. err = 0;
  991. errout:
  992. return err;
  993. }
  994. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  995. struct mpls_route_config *cfg)
  996. {
  997. struct rtmsg *rtm;
  998. struct nlattr *tb[RTA_MAX+1];
  999. int index;
  1000. int err;
  1001. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
  1002. if (err < 0)
  1003. goto errout;
  1004. err = -EINVAL;
  1005. rtm = nlmsg_data(nlh);
  1006. memset(cfg, 0, sizeof(*cfg));
  1007. if (rtm->rtm_family != AF_MPLS)
  1008. goto errout;
  1009. if (rtm->rtm_dst_len != 20)
  1010. goto errout;
  1011. if (rtm->rtm_src_len != 0)
  1012. goto errout;
  1013. if (rtm->rtm_tos != 0)
  1014. goto errout;
  1015. if (rtm->rtm_table != RT_TABLE_MAIN)
  1016. goto errout;
  1017. /* Any value is acceptable for rtm_protocol */
  1018. /* As mpls uses destination specific addresses
  1019. * (or source specific address in the case of multicast)
  1020. * all addresses have universal scope.
  1021. */
  1022. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  1023. goto errout;
  1024. if (rtm->rtm_type != RTN_UNICAST)
  1025. goto errout;
  1026. if (rtm->rtm_flags != 0)
  1027. goto errout;
  1028. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1029. cfg->rc_protocol = rtm->rtm_protocol;
  1030. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1031. cfg->rc_nlflags = nlh->nlmsg_flags;
  1032. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1033. cfg->rc_nlinfo.nlh = nlh;
  1034. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1035. for (index = 0; index <= RTA_MAX; index++) {
  1036. struct nlattr *nla = tb[index];
  1037. if (!nla)
  1038. continue;
  1039. switch(index) {
  1040. case RTA_OIF:
  1041. cfg->rc_ifindex = nla_get_u32(nla);
  1042. break;
  1043. case RTA_NEWDST:
  1044. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1045. &cfg->rc_output_labels,
  1046. cfg->rc_output_label))
  1047. goto errout;
  1048. break;
  1049. case RTA_DST:
  1050. {
  1051. u8 label_count;
  1052. if (nla_get_labels(nla, 1, &label_count,
  1053. &cfg->rc_label))
  1054. goto errout;
  1055. /* Reserved labels may not be set */
  1056. if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
  1057. goto errout;
  1058. break;
  1059. }
  1060. case RTA_VIA:
  1061. {
  1062. if (nla_get_via(nla, &cfg->rc_via_alen,
  1063. &cfg->rc_via_table, cfg->rc_via))
  1064. goto errout;
  1065. break;
  1066. }
  1067. case RTA_MULTIPATH:
  1068. {
  1069. cfg->rc_mp = nla_data(nla);
  1070. cfg->rc_mp_len = nla_len(nla);
  1071. break;
  1072. }
  1073. default:
  1074. /* Unsupported attribute */
  1075. goto errout;
  1076. }
  1077. }
  1078. err = 0;
  1079. errout:
  1080. return err;
  1081. }
  1082. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1083. {
  1084. struct mpls_route_config cfg;
  1085. int err;
  1086. err = rtm_to_route_config(skb, nlh, &cfg);
  1087. if (err < 0)
  1088. return err;
  1089. return mpls_route_del(&cfg);
  1090. }
  1091. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1092. {
  1093. struct mpls_route_config cfg;
  1094. int err;
  1095. err = rtm_to_route_config(skb, nlh, &cfg);
  1096. if (err < 0)
  1097. return err;
  1098. return mpls_route_add(&cfg);
  1099. }
  1100. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1101. u32 label, struct mpls_route *rt, int flags)
  1102. {
  1103. struct net_device *dev;
  1104. struct nlmsghdr *nlh;
  1105. struct rtmsg *rtm;
  1106. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1107. if (nlh == NULL)
  1108. return -EMSGSIZE;
  1109. rtm = nlmsg_data(nlh);
  1110. rtm->rtm_family = AF_MPLS;
  1111. rtm->rtm_dst_len = 20;
  1112. rtm->rtm_src_len = 0;
  1113. rtm->rtm_tos = 0;
  1114. rtm->rtm_table = RT_TABLE_MAIN;
  1115. rtm->rtm_protocol = rt->rt_protocol;
  1116. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1117. rtm->rtm_type = RTN_UNICAST;
  1118. rtm->rtm_flags = 0;
  1119. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1120. goto nla_put_failure;
  1121. if (rt->rt_nhn == 1) {
  1122. const struct mpls_nh *nh = rt->rt_nh;
  1123. if (nh->nh_labels &&
  1124. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1125. nh->nh_label))
  1126. goto nla_put_failure;
  1127. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1128. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1129. nh->nh_via_alen))
  1130. goto nla_put_failure;
  1131. dev = rtnl_dereference(nh->nh_dev);
  1132. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1133. goto nla_put_failure;
  1134. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1135. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1136. if (nh->nh_flags & RTNH_F_DEAD)
  1137. rtm->rtm_flags |= RTNH_F_DEAD;
  1138. } else {
  1139. struct rtnexthop *rtnh;
  1140. struct nlattr *mp;
  1141. int dead = 0;
  1142. int linkdown = 0;
  1143. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1144. if (!mp)
  1145. goto nla_put_failure;
  1146. for_nexthops(rt) {
  1147. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1148. if (!rtnh)
  1149. goto nla_put_failure;
  1150. dev = rtnl_dereference(nh->nh_dev);
  1151. if (dev)
  1152. rtnh->rtnh_ifindex = dev->ifindex;
  1153. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1154. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1155. linkdown++;
  1156. }
  1157. if (nh->nh_flags & RTNH_F_DEAD) {
  1158. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1159. dead++;
  1160. }
  1161. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1162. nh->nh_labels,
  1163. nh->nh_label))
  1164. goto nla_put_failure;
  1165. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1166. nla_put_via(skb, nh->nh_via_table,
  1167. mpls_nh_via(rt, nh),
  1168. nh->nh_via_alen))
  1169. goto nla_put_failure;
  1170. /* length of rtnetlink header + attributes */
  1171. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1172. } endfor_nexthops(rt);
  1173. if (linkdown == rt->rt_nhn)
  1174. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1175. if (dead == rt->rt_nhn)
  1176. rtm->rtm_flags |= RTNH_F_DEAD;
  1177. nla_nest_end(skb, mp);
  1178. }
  1179. nlmsg_end(skb, nlh);
  1180. return 0;
  1181. nla_put_failure:
  1182. nlmsg_cancel(skb, nlh);
  1183. return -EMSGSIZE;
  1184. }
  1185. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1186. {
  1187. struct net *net = sock_net(skb->sk);
  1188. struct mpls_route __rcu **platform_label;
  1189. size_t platform_labels;
  1190. unsigned int index;
  1191. ASSERT_RTNL();
  1192. index = cb->args[0];
  1193. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1194. index = MPLS_LABEL_FIRST_UNRESERVED;
  1195. platform_label = rtnl_dereference(net->mpls.platform_label);
  1196. platform_labels = net->mpls.platform_labels;
  1197. for (; index < platform_labels; index++) {
  1198. struct mpls_route *rt;
  1199. rt = rtnl_dereference(platform_label[index]);
  1200. if (!rt)
  1201. continue;
  1202. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1203. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1204. index, rt, NLM_F_MULTI) < 0)
  1205. break;
  1206. }
  1207. cb->args[0] = index;
  1208. return skb->len;
  1209. }
  1210. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1211. {
  1212. size_t payload =
  1213. NLMSG_ALIGN(sizeof(struct rtmsg))
  1214. + nla_total_size(4); /* RTA_DST */
  1215. if (rt->rt_nhn == 1) {
  1216. struct mpls_nh *nh = rt->rt_nh;
  1217. if (nh->nh_dev)
  1218. payload += nla_total_size(4); /* RTA_OIF */
  1219. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1220. payload += nla_total_size(2 + nh->nh_via_alen);
  1221. if (nh->nh_labels) /* RTA_NEWDST */
  1222. payload += nla_total_size(nh->nh_labels * 4);
  1223. } else {
  1224. /* each nexthop is packed in an attribute */
  1225. size_t nhsize = 0;
  1226. for_nexthops(rt) {
  1227. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1228. /* RTA_VIA */
  1229. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1230. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1231. if (nh->nh_labels)
  1232. nhsize += nla_total_size(nh->nh_labels * 4);
  1233. } endfor_nexthops(rt);
  1234. /* nested attribute */
  1235. payload += nla_total_size(nhsize);
  1236. }
  1237. return payload;
  1238. }
  1239. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1240. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1241. unsigned int nlm_flags)
  1242. {
  1243. struct sk_buff *skb;
  1244. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1245. int err = -ENOBUFS;
  1246. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1247. if (skb == NULL)
  1248. goto errout;
  1249. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1250. if (err < 0) {
  1251. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1252. WARN_ON(err == -EMSGSIZE);
  1253. kfree_skb(skb);
  1254. goto errout;
  1255. }
  1256. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1257. return;
  1258. errout:
  1259. if (err < 0)
  1260. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1261. }
  1262. static int resize_platform_label_table(struct net *net, size_t limit)
  1263. {
  1264. size_t size = sizeof(struct mpls_route *) * limit;
  1265. size_t old_limit;
  1266. size_t cp_size;
  1267. struct mpls_route __rcu **labels = NULL, **old;
  1268. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1269. unsigned index;
  1270. if (size) {
  1271. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1272. if (!labels)
  1273. labels = vzalloc(size);
  1274. if (!labels)
  1275. goto nolabels;
  1276. }
  1277. /* In case the predefined labels need to be populated */
  1278. if (limit > MPLS_LABEL_IPV4NULL) {
  1279. struct net_device *lo = net->loopback_dev;
  1280. rt0 = mpls_rt_alloc(1, lo->addr_len);
  1281. if (!rt0)
  1282. goto nort0;
  1283. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1284. rt0->rt_protocol = RTPROT_KERNEL;
  1285. rt0->rt_payload_type = MPT_IPV4;
  1286. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1287. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1288. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1289. lo->addr_len);
  1290. }
  1291. if (limit > MPLS_LABEL_IPV6NULL) {
  1292. struct net_device *lo = net->loopback_dev;
  1293. rt2 = mpls_rt_alloc(1, lo->addr_len);
  1294. if (!rt2)
  1295. goto nort2;
  1296. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1297. rt2->rt_protocol = RTPROT_KERNEL;
  1298. rt2->rt_payload_type = MPT_IPV6;
  1299. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1300. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1301. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1302. lo->addr_len);
  1303. }
  1304. rtnl_lock();
  1305. /* Remember the original table */
  1306. old = rtnl_dereference(net->mpls.platform_label);
  1307. old_limit = net->mpls.platform_labels;
  1308. /* Free any labels beyond the new table */
  1309. for (index = limit; index < old_limit; index++)
  1310. mpls_route_update(net, index, NULL, NULL);
  1311. /* Copy over the old labels */
  1312. cp_size = size;
  1313. if (old_limit < limit)
  1314. cp_size = old_limit * sizeof(struct mpls_route *);
  1315. memcpy(labels, old, cp_size);
  1316. /* If needed set the predefined labels */
  1317. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1318. (limit > MPLS_LABEL_IPV6NULL)) {
  1319. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1320. rt2 = NULL;
  1321. }
  1322. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1323. (limit > MPLS_LABEL_IPV4NULL)) {
  1324. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1325. rt0 = NULL;
  1326. }
  1327. /* Update the global pointers */
  1328. net->mpls.platform_labels = limit;
  1329. rcu_assign_pointer(net->mpls.platform_label, labels);
  1330. rtnl_unlock();
  1331. mpls_rt_free(rt2);
  1332. mpls_rt_free(rt0);
  1333. if (old) {
  1334. synchronize_rcu();
  1335. kvfree(old);
  1336. }
  1337. return 0;
  1338. nort2:
  1339. mpls_rt_free(rt0);
  1340. nort0:
  1341. kvfree(labels);
  1342. nolabels:
  1343. return -ENOMEM;
  1344. }
  1345. static int mpls_platform_labels(struct ctl_table *table, int write,
  1346. void __user *buffer, size_t *lenp, loff_t *ppos)
  1347. {
  1348. struct net *net = table->data;
  1349. int platform_labels = net->mpls.platform_labels;
  1350. int ret;
  1351. struct ctl_table tmp = {
  1352. .procname = table->procname,
  1353. .data = &platform_labels,
  1354. .maxlen = sizeof(int),
  1355. .mode = table->mode,
  1356. .extra1 = &zero,
  1357. .extra2 = &label_limit,
  1358. };
  1359. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1360. if (write && ret == 0)
  1361. ret = resize_platform_label_table(net, platform_labels);
  1362. return ret;
  1363. }
  1364. static const struct ctl_table mpls_table[] = {
  1365. {
  1366. .procname = "platform_labels",
  1367. .data = NULL,
  1368. .maxlen = sizeof(int),
  1369. .mode = 0644,
  1370. .proc_handler = mpls_platform_labels,
  1371. },
  1372. { }
  1373. };
  1374. static int mpls_net_init(struct net *net)
  1375. {
  1376. struct ctl_table *table;
  1377. net->mpls.platform_labels = 0;
  1378. net->mpls.platform_label = NULL;
  1379. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  1380. if (table == NULL)
  1381. return -ENOMEM;
  1382. table[0].data = net;
  1383. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  1384. if (net->mpls.ctl == NULL) {
  1385. kfree(table);
  1386. return -ENOMEM;
  1387. }
  1388. return 0;
  1389. }
  1390. static void mpls_net_exit(struct net *net)
  1391. {
  1392. struct mpls_route __rcu **platform_label;
  1393. size_t platform_labels;
  1394. struct ctl_table *table;
  1395. unsigned int index;
  1396. table = net->mpls.ctl->ctl_table_arg;
  1397. unregister_net_sysctl_table(net->mpls.ctl);
  1398. kfree(table);
  1399. /* An rcu grace period has passed since there was a device in
  1400. * the network namespace (and thus the last in flight packet)
  1401. * left this network namespace. This is because
  1402. * unregister_netdevice_many and netdev_run_todo has completed
  1403. * for each network device that was in this network namespace.
  1404. *
  1405. * As such no additional rcu synchronization is necessary when
  1406. * freeing the platform_label table.
  1407. */
  1408. rtnl_lock();
  1409. platform_label = rtnl_dereference(net->mpls.platform_label);
  1410. platform_labels = net->mpls.platform_labels;
  1411. for (index = 0; index < platform_labels; index++) {
  1412. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1413. RCU_INIT_POINTER(platform_label[index], NULL);
  1414. mpls_rt_free(rt);
  1415. }
  1416. rtnl_unlock();
  1417. kvfree(platform_label);
  1418. }
  1419. static struct pernet_operations mpls_net_ops = {
  1420. .init = mpls_net_init,
  1421. .exit = mpls_net_exit,
  1422. };
  1423. static int __init mpls_init(void)
  1424. {
  1425. int err;
  1426. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1427. err = register_pernet_subsys(&mpls_net_ops);
  1428. if (err)
  1429. goto out;
  1430. err = register_netdevice_notifier(&mpls_dev_notifier);
  1431. if (err)
  1432. goto out_unregister_pernet;
  1433. dev_add_pack(&mpls_packet_type);
  1434. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1435. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1436. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1437. err = 0;
  1438. out:
  1439. return err;
  1440. out_unregister_pernet:
  1441. unregister_pernet_subsys(&mpls_net_ops);
  1442. goto out;
  1443. }
  1444. module_init(mpls_init);
  1445. static void __exit mpls_exit(void)
  1446. {
  1447. rtnl_unregister_all(PF_MPLS);
  1448. dev_remove_pack(&mpls_packet_type);
  1449. unregister_netdevice_notifier(&mpls_dev_notifier);
  1450. unregister_pernet_subsys(&mpls_net_ops);
  1451. }
  1452. module_exit(mpls_exit);
  1453. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1454. MODULE_LICENSE("GPL v2");
  1455. MODULE_ALIAS_NETPROTO(PF_MPLS);