af_mpls.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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. }
  786. static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
  787. {
  788. struct mpls_route __rcu **platform_label;
  789. struct net *net = dev_net(dev);
  790. unsigned index;
  791. int alive;
  792. platform_label = rtnl_dereference(net->mpls.platform_label);
  793. for (index = 0; index < net->mpls.platform_labels; index++) {
  794. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  795. if (!rt)
  796. continue;
  797. alive = 0;
  798. change_nexthops(rt) {
  799. struct net_device *nh_dev =
  800. rtnl_dereference(nh->nh_dev);
  801. if (!(nh->nh_flags & nh_flags)) {
  802. alive++;
  803. continue;
  804. }
  805. if (nh_dev != dev)
  806. continue;
  807. alive++;
  808. nh->nh_flags &= ~nh_flags;
  809. } endfor_nexthops(rt);
  810. ACCESS_ONCE(rt->rt_nhn_alive) = alive;
  811. }
  812. }
  813. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  814. void *ptr)
  815. {
  816. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  817. struct mpls_dev *mdev;
  818. unsigned int flags;
  819. if (event == NETDEV_REGISTER) {
  820. /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
  821. if (dev->type == ARPHRD_ETHER ||
  822. dev->type == ARPHRD_LOOPBACK ||
  823. dev->type == ARPHRD_IPGRE ||
  824. dev->type == ARPHRD_SIT ||
  825. dev->type == ARPHRD_TUNNEL) {
  826. mdev = mpls_add_dev(dev);
  827. if (IS_ERR(mdev))
  828. return notifier_from_errno(PTR_ERR(mdev));
  829. }
  830. return NOTIFY_OK;
  831. }
  832. mdev = mpls_dev_get(dev);
  833. if (!mdev)
  834. return NOTIFY_OK;
  835. switch (event) {
  836. case NETDEV_DOWN:
  837. mpls_ifdown(dev, event);
  838. break;
  839. case NETDEV_UP:
  840. flags = dev_get_flags(dev);
  841. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  842. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  843. else
  844. mpls_ifup(dev, RTNH_F_DEAD);
  845. break;
  846. case NETDEV_CHANGE:
  847. flags = dev_get_flags(dev);
  848. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  849. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  850. else
  851. mpls_ifdown(dev, event);
  852. break;
  853. case NETDEV_UNREGISTER:
  854. mpls_ifdown(dev, event);
  855. mdev = mpls_dev_get(dev);
  856. if (mdev) {
  857. mpls_dev_sysctl_unregister(mdev);
  858. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  859. kfree_rcu(mdev, rcu);
  860. }
  861. break;
  862. case NETDEV_CHANGENAME:
  863. mdev = mpls_dev_get(dev);
  864. if (mdev) {
  865. int err;
  866. mpls_dev_sysctl_unregister(mdev);
  867. err = mpls_dev_sysctl_register(dev, mdev);
  868. if (err)
  869. return notifier_from_errno(err);
  870. }
  871. break;
  872. }
  873. return NOTIFY_OK;
  874. }
  875. static struct notifier_block mpls_dev_notifier = {
  876. .notifier_call = mpls_dev_notify,
  877. };
  878. static int nla_put_via(struct sk_buff *skb,
  879. u8 table, const void *addr, int alen)
  880. {
  881. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  882. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  883. };
  884. struct nlattr *nla;
  885. struct rtvia *via;
  886. int family = AF_UNSPEC;
  887. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  888. if (!nla)
  889. return -EMSGSIZE;
  890. if (table <= NEIGH_NR_TABLES)
  891. family = table_to_family[table];
  892. via = nla_data(nla);
  893. via->rtvia_family = family;
  894. memcpy(via->rtvia_addr, addr, alen);
  895. return 0;
  896. }
  897. int nla_put_labels(struct sk_buff *skb, int attrtype,
  898. u8 labels, const u32 label[])
  899. {
  900. struct nlattr *nla;
  901. struct mpls_shim_hdr *nla_label;
  902. bool bos;
  903. int i;
  904. nla = nla_reserve(skb, attrtype, labels*4);
  905. if (!nla)
  906. return -EMSGSIZE;
  907. nla_label = nla_data(nla);
  908. bos = true;
  909. for (i = labels - 1; i >= 0; i--) {
  910. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  911. bos = false;
  912. }
  913. return 0;
  914. }
  915. EXPORT_SYMBOL_GPL(nla_put_labels);
  916. int nla_get_labels(const struct nlattr *nla,
  917. u32 max_labels, u8 *labels, u32 label[])
  918. {
  919. unsigned len = nla_len(nla);
  920. unsigned nla_labels;
  921. struct mpls_shim_hdr *nla_label;
  922. bool bos;
  923. int i;
  924. /* len needs to be an even multiple of 4 (the label size) */
  925. if (len & 3)
  926. return -EINVAL;
  927. /* Limit the number of new labels allowed */
  928. nla_labels = len/4;
  929. if (nla_labels > max_labels)
  930. return -EINVAL;
  931. nla_label = nla_data(nla);
  932. bos = true;
  933. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  934. struct mpls_entry_decoded dec;
  935. dec = mpls_entry_decode(nla_label + i);
  936. /* Ensure the bottom of stack flag is properly set
  937. * and ttl and tc are both clear.
  938. */
  939. if ((dec.bos != bos) || dec.ttl || dec.tc)
  940. return -EINVAL;
  941. switch (dec.label) {
  942. case MPLS_LABEL_IMPLNULL:
  943. /* RFC3032: This is a label that an LSR may
  944. * assign and distribute, but which never
  945. * actually appears in the encapsulation.
  946. */
  947. return -EINVAL;
  948. }
  949. label[i] = dec.label;
  950. }
  951. *labels = nla_labels;
  952. return 0;
  953. }
  954. EXPORT_SYMBOL_GPL(nla_get_labels);
  955. int nla_get_via(const struct nlattr *nla, u8 *via_alen,
  956. u8 *via_table, u8 via_addr[])
  957. {
  958. struct rtvia *via = nla_data(nla);
  959. int err = -EINVAL;
  960. int alen;
  961. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  962. goto errout;
  963. alen = nla_len(nla) -
  964. offsetof(struct rtvia, rtvia_addr);
  965. if (alen > MAX_VIA_ALEN)
  966. goto errout;
  967. /* Validate the address family */
  968. switch (via->rtvia_family) {
  969. case AF_PACKET:
  970. *via_table = NEIGH_LINK_TABLE;
  971. break;
  972. case AF_INET:
  973. *via_table = NEIGH_ARP_TABLE;
  974. if (alen != 4)
  975. goto errout;
  976. break;
  977. case AF_INET6:
  978. *via_table = NEIGH_ND_TABLE;
  979. if (alen != 16)
  980. goto errout;
  981. break;
  982. default:
  983. /* Unsupported address family */
  984. goto errout;
  985. }
  986. memcpy(via_addr, via->rtvia_addr, alen);
  987. *via_alen = alen;
  988. err = 0;
  989. errout:
  990. return err;
  991. }
  992. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  993. struct mpls_route_config *cfg)
  994. {
  995. struct rtmsg *rtm;
  996. struct nlattr *tb[RTA_MAX+1];
  997. int index;
  998. int err;
  999. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
  1000. if (err < 0)
  1001. goto errout;
  1002. err = -EINVAL;
  1003. rtm = nlmsg_data(nlh);
  1004. memset(cfg, 0, sizeof(*cfg));
  1005. if (rtm->rtm_family != AF_MPLS)
  1006. goto errout;
  1007. if (rtm->rtm_dst_len != 20)
  1008. goto errout;
  1009. if (rtm->rtm_src_len != 0)
  1010. goto errout;
  1011. if (rtm->rtm_tos != 0)
  1012. goto errout;
  1013. if (rtm->rtm_table != RT_TABLE_MAIN)
  1014. goto errout;
  1015. /* Any value is acceptable for rtm_protocol */
  1016. /* As mpls uses destination specific addresses
  1017. * (or source specific address in the case of multicast)
  1018. * all addresses have universal scope.
  1019. */
  1020. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  1021. goto errout;
  1022. if (rtm->rtm_type != RTN_UNICAST)
  1023. goto errout;
  1024. if (rtm->rtm_flags != 0)
  1025. goto errout;
  1026. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1027. cfg->rc_protocol = rtm->rtm_protocol;
  1028. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1029. cfg->rc_nlflags = nlh->nlmsg_flags;
  1030. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1031. cfg->rc_nlinfo.nlh = nlh;
  1032. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1033. for (index = 0; index <= RTA_MAX; index++) {
  1034. struct nlattr *nla = tb[index];
  1035. if (!nla)
  1036. continue;
  1037. switch (index) {
  1038. case RTA_OIF:
  1039. cfg->rc_ifindex = nla_get_u32(nla);
  1040. break;
  1041. case RTA_NEWDST:
  1042. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1043. &cfg->rc_output_labels,
  1044. cfg->rc_output_label))
  1045. goto errout;
  1046. break;
  1047. case RTA_DST:
  1048. {
  1049. u8 label_count;
  1050. if (nla_get_labels(nla, 1, &label_count,
  1051. &cfg->rc_label))
  1052. goto errout;
  1053. /* Reserved labels may not be set */
  1054. if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
  1055. goto errout;
  1056. break;
  1057. }
  1058. case RTA_VIA:
  1059. {
  1060. if (nla_get_via(nla, &cfg->rc_via_alen,
  1061. &cfg->rc_via_table, cfg->rc_via))
  1062. goto errout;
  1063. break;
  1064. }
  1065. case RTA_MULTIPATH:
  1066. {
  1067. cfg->rc_mp = nla_data(nla);
  1068. cfg->rc_mp_len = nla_len(nla);
  1069. break;
  1070. }
  1071. default:
  1072. /* Unsupported attribute */
  1073. goto errout;
  1074. }
  1075. }
  1076. err = 0;
  1077. errout:
  1078. return err;
  1079. }
  1080. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1081. {
  1082. struct mpls_route_config cfg;
  1083. int err;
  1084. err = rtm_to_route_config(skb, nlh, &cfg);
  1085. if (err < 0)
  1086. return err;
  1087. return mpls_route_del(&cfg);
  1088. }
  1089. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1090. {
  1091. struct mpls_route_config cfg;
  1092. int err;
  1093. err = rtm_to_route_config(skb, nlh, &cfg);
  1094. if (err < 0)
  1095. return err;
  1096. return mpls_route_add(&cfg);
  1097. }
  1098. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1099. u32 label, struct mpls_route *rt, int flags)
  1100. {
  1101. struct net_device *dev;
  1102. struct nlmsghdr *nlh;
  1103. struct rtmsg *rtm;
  1104. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1105. if (nlh == NULL)
  1106. return -EMSGSIZE;
  1107. rtm = nlmsg_data(nlh);
  1108. rtm->rtm_family = AF_MPLS;
  1109. rtm->rtm_dst_len = 20;
  1110. rtm->rtm_src_len = 0;
  1111. rtm->rtm_tos = 0;
  1112. rtm->rtm_table = RT_TABLE_MAIN;
  1113. rtm->rtm_protocol = rt->rt_protocol;
  1114. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1115. rtm->rtm_type = RTN_UNICAST;
  1116. rtm->rtm_flags = 0;
  1117. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1118. goto nla_put_failure;
  1119. if (rt->rt_nhn == 1) {
  1120. const struct mpls_nh *nh = rt->rt_nh;
  1121. if (nh->nh_labels &&
  1122. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1123. nh->nh_label))
  1124. goto nla_put_failure;
  1125. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1126. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1127. nh->nh_via_alen))
  1128. goto nla_put_failure;
  1129. dev = rtnl_dereference(nh->nh_dev);
  1130. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1131. goto nla_put_failure;
  1132. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1133. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1134. if (nh->nh_flags & RTNH_F_DEAD)
  1135. rtm->rtm_flags |= RTNH_F_DEAD;
  1136. } else {
  1137. struct rtnexthop *rtnh;
  1138. struct nlattr *mp;
  1139. int dead = 0;
  1140. int linkdown = 0;
  1141. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1142. if (!mp)
  1143. goto nla_put_failure;
  1144. for_nexthops(rt) {
  1145. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1146. if (!rtnh)
  1147. goto nla_put_failure;
  1148. dev = rtnl_dereference(nh->nh_dev);
  1149. if (dev)
  1150. rtnh->rtnh_ifindex = dev->ifindex;
  1151. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1152. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1153. linkdown++;
  1154. }
  1155. if (nh->nh_flags & RTNH_F_DEAD) {
  1156. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1157. dead++;
  1158. }
  1159. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1160. nh->nh_labels,
  1161. nh->nh_label))
  1162. goto nla_put_failure;
  1163. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1164. nla_put_via(skb, nh->nh_via_table,
  1165. mpls_nh_via(rt, nh),
  1166. nh->nh_via_alen))
  1167. goto nla_put_failure;
  1168. /* length of rtnetlink header + attributes */
  1169. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1170. } endfor_nexthops(rt);
  1171. if (linkdown == rt->rt_nhn)
  1172. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1173. if (dead == rt->rt_nhn)
  1174. rtm->rtm_flags |= RTNH_F_DEAD;
  1175. nla_nest_end(skb, mp);
  1176. }
  1177. nlmsg_end(skb, nlh);
  1178. return 0;
  1179. nla_put_failure:
  1180. nlmsg_cancel(skb, nlh);
  1181. return -EMSGSIZE;
  1182. }
  1183. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1184. {
  1185. struct net *net = sock_net(skb->sk);
  1186. struct mpls_route __rcu **platform_label;
  1187. size_t platform_labels;
  1188. unsigned int index;
  1189. ASSERT_RTNL();
  1190. index = cb->args[0];
  1191. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1192. index = MPLS_LABEL_FIRST_UNRESERVED;
  1193. platform_label = rtnl_dereference(net->mpls.platform_label);
  1194. platform_labels = net->mpls.platform_labels;
  1195. for (; index < platform_labels; index++) {
  1196. struct mpls_route *rt;
  1197. rt = rtnl_dereference(platform_label[index]);
  1198. if (!rt)
  1199. continue;
  1200. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1201. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1202. index, rt, NLM_F_MULTI) < 0)
  1203. break;
  1204. }
  1205. cb->args[0] = index;
  1206. return skb->len;
  1207. }
  1208. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1209. {
  1210. size_t payload =
  1211. NLMSG_ALIGN(sizeof(struct rtmsg))
  1212. + nla_total_size(4); /* RTA_DST */
  1213. if (rt->rt_nhn == 1) {
  1214. struct mpls_nh *nh = rt->rt_nh;
  1215. if (nh->nh_dev)
  1216. payload += nla_total_size(4); /* RTA_OIF */
  1217. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1218. payload += nla_total_size(2 + nh->nh_via_alen);
  1219. if (nh->nh_labels) /* RTA_NEWDST */
  1220. payload += nla_total_size(nh->nh_labels * 4);
  1221. } else {
  1222. /* each nexthop is packed in an attribute */
  1223. size_t nhsize = 0;
  1224. for_nexthops(rt) {
  1225. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1226. /* RTA_VIA */
  1227. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1228. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1229. if (nh->nh_labels)
  1230. nhsize += nla_total_size(nh->nh_labels * 4);
  1231. } endfor_nexthops(rt);
  1232. /* nested attribute */
  1233. payload += nla_total_size(nhsize);
  1234. }
  1235. return payload;
  1236. }
  1237. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1238. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1239. unsigned int nlm_flags)
  1240. {
  1241. struct sk_buff *skb;
  1242. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1243. int err = -ENOBUFS;
  1244. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1245. if (skb == NULL)
  1246. goto errout;
  1247. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1248. if (err < 0) {
  1249. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1250. WARN_ON(err == -EMSGSIZE);
  1251. kfree_skb(skb);
  1252. goto errout;
  1253. }
  1254. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1255. return;
  1256. errout:
  1257. if (err < 0)
  1258. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1259. }
  1260. static int resize_platform_label_table(struct net *net, size_t limit)
  1261. {
  1262. size_t size = sizeof(struct mpls_route *) * limit;
  1263. size_t old_limit;
  1264. size_t cp_size;
  1265. struct mpls_route __rcu **labels = NULL, **old;
  1266. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1267. unsigned index;
  1268. if (size) {
  1269. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1270. if (!labels)
  1271. labels = vzalloc(size);
  1272. if (!labels)
  1273. goto nolabels;
  1274. }
  1275. /* In case the predefined labels need to be populated */
  1276. if (limit > MPLS_LABEL_IPV4NULL) {
  1277. struct net_device *lo = net->loopback_dev;
  1278. rt0 = mpls_rt_alloc(1, lo->addr_len);
  1279. if (!rt0)
  1280. goto nort0;
  1281. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1282. rt0->rt_protocol = RTPROT_KERNEL;
  1283. rt0->rt_payload_type = MPT_IPV4;
  1284. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1285. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1286. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1287. lo->addr_len);
  1288. }
  1289. if (limit > MPLS_LABEL_IPV6NULL) {
  1290. struct net_device *lo = net->loopback_dev;
  1291. rt2 = mpls_rt_alloc(1, lo->addr_len);
  1292. if (!rt2)
  1293. goto nort2;
  1294. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1295. rt2->rt_protocol = RTPROT_KERNEL;
  1296. rt2->rt_payload_type = MPT_IPV6;
  1297. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1298. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1299. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1300. lo->addr_len);
  1301. }
  1302. rtnl_lock();
  1303. /* Remember the original table */
  1304. old = rtnl_dereference(net->mpls.platform_label);
  1305. old_limit = net->mpls.platform_labels;
  1306. /* Free any labels beyond the new table */
  1307. for (index = limit; index < old_limit; index++)
  1308. mpls_route_update(net, index, NULL, NULL);
  1309. /* Copy over the old labels */
  1310. cp_size = size;
  1311. if (old_limit < limit)
  1312. cp_size = old_limit * sizeof(struct mpls_route *);
  1313. memcpy(labels, old, cp_size);
  1314. /* If needed set the predefined labels */
  1315. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1316. (limit > MPLS_LABEL_IPV6NULL)) {
  1317. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1318. rt2 = NULL;
  1319. }
  1320. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1321. (limit > MPLS_LABEL_IPV4NULL)) {
  1322. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1323. rt0 = NULL;
  1324. }
  1325. /* Update the global pointers */
  1326. net->mpls.platform_labels = limit;
  1327. rcu_assign_pointer(net->mpls.platform_label, labels);
  1328. rtnl_unlock();
  1329. mpls_rt_free(rt2);
  1330. mpls_rt_free(rt0);
  1331. if (old) {
  1332. synchronize_rcu();
  1333. kvfree(old);
  1334. }
  1335. return 0;
  1336. nort2:
  1337. mpls_rt_free(rt0);
  1338. nort0:
  1339. kvfree(labels);
  1340. nolabels:
  1341. return -ENOMEM;
  1342. }
  1343. static int mpls_platform_labels(struct ctl_table *table, int write,
  1344. void __user *buffer, size_t *lenp, loff_t *ppos)
  1345. {
  1346. struct net *net = table->data;
  1347. int platform_labels = net->mpls.platform_labels;
  1348. int ret;
  1349. struct ctl_table tmp = {
  1350. .procname = table->procname,
  1351. .data = &platform_labels,
  1352. .maxlen = sizeof(int),
  1353. .mode = table->mode,
  1354. .extra1 = &zero,
  1355. .extra2 = &label_limit,
  1356. };
  1357. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1358. if (write && ret == 0)
  1359. ret = resize_platform_label_table(net, platform_labels);
  1360. return ret;
  1361. }
  1362. static const struct ctl_table mpls_table[] = {
  1363. {
  1364. .procname = "platform_labels",
  1365. .data = NULL,
  1366. .maxlen = sizeof(int),
  1367. .mode = 0644,
  1368. .proc_handler = mpls_platform_labels,
  1369. },
  1370. { }
  1371. };
  1372. static int mpls_net_init(struct net *net)
  1373. {
  1374. struct ctl_table *table;
  1375. net->mpls.platform_labels = 0;
  1376. net->mpls.platform_label = NULL;
  1377. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  1378. if (table == NULL)
  1379. return -ENOMEM;
  1380. table[0].data = net;
  1381. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  1382. if (net->mpls.ctl == NULL) {
  1383. kfree(table);
  1384. return -ENOMEM;
  1385. }
  1386. return 0;
  1387. }
  1388. static void mpls_net_exit(struct net *net)
  1389. {
  1390. struct mpls_route __rcu **platform_label;
  1391. size_t platform_labels;
  1392. struct ctl_table *table;
  1393. unsigned int index;
  1394. table = net->mpls.ctl->ctl_table_arg;
  1395. unregister_net_sysctl_table(net->mpls.ctl);
  1396. kfree(table);
  1397. /* An rcu grace period has passed since there was a device in
  1398. * the network namespace (and thus the last in flight packet)
  1399. * left this network namespace. This is because
  1400. * unregister_netdevice_many and netdev_run_todo has completed
  1401. * for each network device that was in this network namespace.
  1402. *
  1403. * As such no additional rcu synchronization is necessary when
  1404. * freeing the platform_label table.
  1405. */
  1406. rtnl_lock();
  1407. platform_label = rtnl_dereference(net->mpls.platform_label);
  1408. platform_labels = net->mpls.platform_labels;
  1409. for (index = 0; index < platform_labels; index++) {
  1410. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1411. RCU_INIT_POINTER(platform_label[index], NULL);
  1412. mpls_rt_free(rt);
  1413. }
  1414. rtnl_unlock();
  1415. kvfree(platform_label);
  1416. }
  1417. static struct pernet_operations mpls_net_ops = {
  1418. .init = mpls_net_init,
  1419. .exit = mpls_net_exit,
  1420. };
  1421. static int __init mpls_init(void)
  1422. {
  1423. int err;
  1424. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1425. err = register_pernet_subsys(&mpls_net_ops);
  1426. if (err)
  1427. goto out;
  1428. err = register_netdevice_notifier(&mpls_dev_notifier);
  1429. if (err)
  1430. goto out_unregister_pernet;
  1431. dev_add_pack(&mpls_packet_type);
  1432. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1433. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1434. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1435. err = 0;
  1436. out:
  1437. return err;
  1438. out_unregister_pernet:
  1439. unregister_pernet_subsys(&mpls_net_ops);
  1440. goto out;
  1441. }
  1442. module_init(mpls_init);
  1443. static void __exit mpls_exit(void)
  1444. {
  1445. rtnl_unregister_all(PF_MPLS);
  1446. dev_remove_pack(&mpls_packet_type);
  1447. unregister_netdevice_notifier(&mpls_dev_notifier);
  1448. unregister_pernet_subsys(&mpls_net_ops);
  1449. }
  1450. module_exit(mpls_exit);
  1451. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1452. MODULE_LICENSE("GPL v2");
  1453. MODULE_ALIAS_NETPROTO(PF_MPLS);